Coverage Report

Created: 2017-06-27 13:53

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/isl/isl_ast.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2012-2013 Ecole Normale Superieure
3
 *
4
 * Use of this software is governed by the MIT license
5
 *
6
 * Written by Sven Verdoolaege,
7
 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8
 */
9
10
#include <string.h>
11
12
#include <isl_ast_private.h>
13
14
#undef BASE
15
#define BASE ast_expr
16
17
#include <isl_list_templ.c>
18
19
#undef BASE
20
#define BASE ast_node
21
22
#include <isl_list_templ.c>
23
24
isl_ctx *isl_ast_print_options_get_ctx(
25
  __isl_keep isl_ast_print_options *options)
26
0
{
27
0
  return options ? options->ctx : NULL;
28
0
}
29
30
__isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
31
149
{
32
149
  isl_ast_print_options *options;
33
149
34
149
  options = isl_calloc_type(ctx, isl_ast_print_options);
35
149
  if (!options)
36
0
    return NULL;
37
149
38
149
  options->ctx = ctx;
39
149
  isl_ctx_ref(ctx);
40
149
  options->ref = 1;
41
149
42
149
  return options;
43
149
}
44
45
__isl_give isl_ast_print_options *isl_ast_print_options_dup(
46
  __isl_keep isl_ast_print_options *options)
47
0
{
48
0
  isl_ctx *ctx;
49
0
  isl_ast_print_options *dup;
50
0
51
0
  if (!options)
52
0
    return NULL;
53
0
54
0
  ctx = isl_ast_print_options_get_ctx(options);
55
0
  dup = isl_ast_print_options_alloc(ctx);
56
0
  if (!dup)
57
0
    return NULL;
58
0
59
0
  dup->print_for = options->print_for;
60
0
  dup->print_for_user = options->print_for_user;
61
0
  dup->print_user = options->print_user;
62
0
  dup->print_user_user = options->print_user_user;
63
0
64
0
  return dup;
65
0
}
66
67
__isl_give isl_ast_print_options *isl_ast_print_options_cow(
68
  __isl_take isl_ast_print_options *options)
69
149
{
70
149
  if (!options)
71
0
    return NULL;
72
149
73
149
  
if (149
options->ref == 1149
)
74
149
    return options;
75
0
  options->ref--;
76
0
  return isl_ast_print_options_dup(options);
77
149
}
78
79
__isl_give isl_ast_print_options *isl_ast_print_options_copy(
80
  __isl_keep isl_ast_print_options *options)
81
690
{
82
690
  if (!options)
83
0
    return NULL;
84
690
85
690
  options->ref++;
86
690
  return options;
87
690
}
88
89
__isl_null isl_ast_print_options *isl_ast_print_options_free(
90
  __isl_take isl_ast_print_options *options)
91
839
{
92
839
  if (!options)
93
0
    return NULL;
94
839
95
839
  
if (839
--options->ref > 0839
)
96
690
    return NULL;
97
839
98
149
  isl_ctx_deref(options->ctx);
99
149
100
149
  free(options);
101
149
  return NULL;
102
839
}
103
104
/* Set the print_user callback of "options" to "print_user".
105
 *
106
 * If this callback is set, then it used to print user nodes in the AST.
107
 * Otherwise, the expression associated to the user node is printed.
108
 */
109
__isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
110
  __isl_take isl_ast_print_options *options,
111
  __isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
112
    __isl_take isl_ast_print_options *options,
113
    __isl_keep isl_ast_node *node, void *user),
114
  void *user)
115
0
{
116
0
  options = isl_ast_print_options_cow(options);
117
0
  if (!options)
118
0
    return NULL;
119
0
120
0
  options->print_user = print_user;
121
0
  options->print_user_user = user;
122
0
123
0
  return options;
124
0
}
125
126
/* Set the print_for callback of "options" to "print_for".
127
 *
128
 * If this callback is set, then it used to print for nodes in the AST.
129
 */
130
__isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
131
  __isl_take isl_ast_print_options *options,
132
  __isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
133
    __isl_take isl_ast_print_options *options,
134
    __isl_keep isl_ast_node *node, void *user),
135
  void *user)
136
149
{
137
149
  options = isl_ast_print_options_cow(options);
138
149
  if (!options)
139
0
    return NULL;
140
149
141
149
  options->print_for = print_for;
142
149
  options->print_for_user = user;
143
149
144
149
  return options;
145
149
}
146
147
__isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
148
9.14k
{
149
9.14k
  if (!expr)
150
0
    return NULL;
151
9.14k
152
9.14k
  expr->ref++;
153
9.14k
  return expr;
154
9.14k
}
155
156
__isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
157
0
{
158
0
  int i;
159
0
  isl_ctx *ctx;
160
0
  isl_ast_expr *dup;
161
0
162
0
  if (!expr)
163
0
    return NULL;
164
0
165
0
  ctx = isl_ast_expr_get_ctx(expr);
166
0
  switch (expr->type) {
167
0
  case isl_ast_expr_int:
168
0
    dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
169
0
    break;
170
0
  case isl_ast_expr_id:
171
0
    dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
172
0
    break;
173
0
  case isl_ast_expr_op:
174
0
    dup = isl_ast_expr_alloc_op(ctx,
175
0
              expr->u.op.op, expr->u.op.n_arg);
176
0
    if (!dup)
177
0
      return NULL;
178
0
    
for (i = 0; 0
i < expr->u.op.n_arg0
;
++i0
)
179
0
      dup->u.op.args[i] =
180
0
        isl_ast_expr_copy(expr->u.op.args[i]);
181
0
    break;
182
0
  case isl_ast_expr_error:
183
0
    dup = NULL;
184
0
  }
185
0
186
0
  
if (0
!dup0
)
187
0
    return NULL;
188
0
189
0
  return dup;
190
0
}
191
192
__isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
193
5.80k
{
194
5.80k
  if (!expr)
195
0
    return NULL;
196
5.80k
197
5.80k
  
if (5.80k
expr->ref == 15.80k
)
198
5.80k
    return expr;
199
0
  expr->ref--;
200
0
  return isl_ast_expr_dup(expr);
201
5.80k
}
202
203
__isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
204
50.7k
{
205
50.7k
  int i;
206
50.7k
207
50.7k
  if (!expr)
208
5.80k
    return NULL;
209
50.7k
210
44.9k
  
if (44.9k
--expr->ref > 044.9k
)
211
9.14k
    return NULL;
212
44.9k
213
35.8k
  isl_ctx_deref(expr->ctx);
214
35.8k
215
35.8k
  switch (expr->type) {
216
17.6k
  case isl_ast_expr_int:
217
17.6k
    isl_val_free(expr->u.v);
218
17.6k
    break;
219
7.78k
  case isl_ast_expr_id:
220
7.78k
    isl_id_free(expr->u.id);
221
7.78k
    break;
222
10.3k
  case isl_ast_expr_op:
223
10.3k
    if (expr->u.op.args)
224
31.8k
      
for (i = 0; 10.3k
i < expr->u.op.n_arg31.8k
;
++i21.4k
)
225
21.4k
        isl_ast_expr_free(expr->u.op.args[i]);
226
10.3k
    free(expr->u.op.args);
227
10.3k
    break;
228
0
  case isl_ast_expr_error:
229
0
    break;
230
35.8k
  }
231
35.8k
232
35.8k
  free(expr);
233
35.8k
  return NULL;
234
35.8k
}
235
236
isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
237
9.62k
{
238
9.62k
  return expr ? expr->ctx : NULL;
239
9.62k
}
240
241
enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
242
7.73k
{
243
7.73k
  return expr ? 
expr->type7.73k
:
isl_ast_expr_error0
;
244
7.73k
}
245
246
/* Return the integer value represented by "expr".
247
 */
248
__isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
249
2.15k
{
250
2.15k
  if (!expr)
251
0
    return NULL;
252
2.15k
  
if (2.15k
expr->type != isl_ast_expr_int2.15k
)
253
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
254
2.15k
      "expression not an int", return NULL);
255
2.15k
  return isl_val_copy(expr->u.v);
256
2.15k
}
257
258
__isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
259
3.33k
{
260
3.33k
  if (!expr)
261
0
    return NULL;
262
3.33k
  
if (3.33k
expr->type != isl_ast_expr_id3.33k
)
263
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
264
3.33k
      "expression not an identifier", return NULL);
265
3.33k
266
3.33k
  return isl_id_copy(expr->u.id);
267
3.33k
}
268
269
enum isl_ast_op_type isl_ast_expr_get_op_type(__isl_keep isl_ast_expr *expr)
270
4.67k
{
271
4.67k
  if (!expr)
272
0
    return isl_ast_op_error;
273
4.67k
  
if (4.67k
expr->type != isl_ast_expr_op4.67k
)
274
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
275
4.67k
      "expression not an operation", return isl_ast_op_error);
276
4.67k
  return expr->u.op.op;
277
4.67k
}
278
279
int isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
280
2.27k
{
281
2.27k
  if (!expr)
282
0
    return -1;
283
2.27k
  
if (2.27k
expr->type != isl_ast_expr_op2.27k
)
284
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
285
2.27k
      "expression not an operation", return -1);
286
2.27k
  return expr->u.op.n_arg;
287
2.27k
}
288
289
__isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
290
  int pos)
291
5.93k
{
292
5.93k
  if (!expr)
293
0
    return NULL;
294
5.93k
  
if (5.93k
expr->type != isl_ast_expr_op5.93k
)
295
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
296
5.93k
      "expression not an operation", return NULL);
297
5.93k
  
if (5.93k
pos < 0 || 5.93k
pos >= expr->u.op.n_arg5.93k
)
298
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
299
5.93k
      "index out of bounds", return NULL);
300
5.93k
301
5.93k
  return isl_ast_expr_copy(expr->u.op.args[pos]);
302
5.93k
}
303
304
/* Replace the argument at position "pos" of "expr" by "arg".
305
 */
306
__isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
307
  int pos, __isl_take isl_ast_expr *arg)
308
5.80k
{
309
5.80k
  expr = isl_ast_expr_cow(expr);
310
5.80k
  if (
!expr || 5.80k
!arg5.80k
)
311
0
    goto error;
312
5.80k
  
if (5.80k
expr->type != isl_ast_expr_op5.80k
)
313
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
314
5.80k
      "expression not an operation", goto error);
315
5.80k
  
if (5.80k
pos < 0 || 5.80k
pos >= expr->u.op.n_arg5.80k
)
316
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
317
5.80k
      "index out of bounds", goto error);
318
5.80k
319
5.80k
  isl_ast_expr_free(expr->u.op.args[pos]);
320
5.80k
  expr->u.op.args[pos] = arg;
321
5.80k
322
5.80k
  return expr;
323
0
error:
324
0
  isl_ast_expr_free(arg);
325
0
  return isl_ast_expr_free(expr);
326
5.80k
}
327
328
/* Is "expr1" equal to "expr2"?
329
 */
330
isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
331
  __isl_keep isl_ast_expr *expr2)
332
2
{
333
2
  int i;
334
2
335
2
  if (
!expr1 || 2
!expr22
)
336
0
    return isl_bool_error;
337
2
338
2
  
if (2
expr1 == expr22
)
339
1
    return isl_bool_true;
340
1
  
if (1
expr1->type != expr2->type1
)
341
0
    return isl_bool_false;
342
1
  switch (expr1->type) {
343
0
  case isl_ast_expr_int:
344
0
    return isl_val_eq(expr1->u.v, expr2->u.v);
345
0
  case isl_ast_expr_id:
346
0
    return expr1->u.id == expr2->u.id;
347
1
  case isl_ast_expr_op:
348
1
    if (expr1->u.op.op != expr2->u.op.op)
349
0
      return isl_bool_false;
350
1
    
if (1
expr1->u.op.n_arg != expr2->u.op.n_arg1
)
351
0
      return isl_bool_false;
352
2
    
for (i = 0; 1
i < expr1->u.op.n_arg2
;
++i1
)
{1
353
1
      isl_bool equal;
354
1
      equal = isl_ast_expr_is_equal(expr1->u.op.args[i],
355
1
              expr2->u.op.args[i]);
356
1
      if (
equal < 0 || 1
!equal1
)
357
0
        return equal;
358
1
    }
359
1
    return 1;
360
0
  case isl_ast_expr_error:
361
0
    return isl_bool_error;
362
1
  }
363
1
364
0
  
isl_die0
(isl_ast_expr_get_ctx(expr1), isl_error_internal,0
365
0
    "unhandled case", return isl_bool_error);
366
0
}
367
368
/* Create a new operation expression of operation type "op",
369
 * with "n_arg" as yet unspecified arguments.
370
 */
371
__isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
372
  enum isl_ast_op_type op, int n_arg)
373
10.3k
{
374
10.3k
  isl_ast_expr *expr;
375
10.3k
376
10.3k
  expr = isl_calloc_type(ctx, isl_ast_expr);
377
10.3k
  if (!expr)
378
0
    return NULL;
379
10.3k
380
10.3k
  expr->ctx = ctx;
381
10.3k
  isl_ctx_ref(ctx);
382
10.3k
  expr->ref = 1;
383
10.3k
  expr->type = isl_ast_expr_op;
384
10.3k
  expr->u.op.op = op;
385
10.3k
  expr->u.op.n_arg = n_arg;
386
10.3k
  expr->u.op.args = isl_calloc_array(ctx, isl_ast_expr *, n_arg);
387
10.3k
388
10.3k
  if (
n_arg && 10.3k
!expr->u.op.args10.3k
)
389
0
    return isl_ast_expr_free(expr);
390
10.3k
391
10.3k
  return expr;
392
10.3k
}
393
394
/* Create a new id expression representing "id".
395
 */
396
__isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
397
7.78k
{
398
7.78k
  isl_ctx *ctx;
399
7.78k
  isl_ast_expr *expr;
400
7.78k
401
7.78k
  if (!id)
402
0
    return NULL;
403
7.78k
404
7.78k
  ctx = isl_id_get_ctx(id);
405
7.78k
  expr = isl_calloc_type(ctx, isl_ast_expr);
406
7.78k
  if (!expr)
407
0
    goto error;
408
7.78k
409
7.78k
  expr->ctx = ctx;
410
7.78k
  isl_ctx_ref(ctx);
411
7.78k
  expr->ref = 1;
412
7.78k
  expr->type = isl_ast_expr_id;
413
7.78k
  expr->u.id = id;
414
7.78k
415
7.78k
  return expr;
416
0
error:
417
0
  isl_id_free(id);
418
0
  return NULL;
419
7.78k
}
420
421
/* Create a new integer expression representing "i".
422
 */
423
__isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
424
12.5k
{
425
12.5k
  isl_ast_expr *expr;
426
12.5k
427
12.5k
  expr = isl_calloc_type(ctx, isl_ast_expr);
428
12.5k
  if (!expr)
429
0
    return NULL;
430
12.5k
431
12.5k
  expr->ctx = ctx;
432
12.5k
  isl_ctx_ref(ctx);
433
12.5k
  expr->ref = 1;
434
12.5k
  expr->type = isl_ast_expr_int;
435
12.5k
  expr->u.v = isl_val_int_from_si(ctx, i);
436
12.5k
  if (!expr->u.v)
437
0
    return isl_ast_expr_free(expr);
438
12.5k
439
12.5k
  return expr;
440
12.5k
}
441
442
/* Create a new integer expression representing "v".
443
 */
444
__isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
445
5.14k
{
446
5.14k
  isl_ctx *ctx;
447
5.14k
  isl_ast_expr *expr;
448
5.14k
449
5.14k
  if (!v)
450
0
    return NULL;
451
5.14k
  
if (5.14k
!isl_val_is_int(v)5.14k
)
452
0
    isl_die(isl_val_get_ctx(v), isl_error_invalid,
453
5.14k
      "expecting integer value", goto error);
454
5.14k
455
5.14k
  ctx = isl_val_get_ctx(v);
456
5.14k
  expr = isl_calloc_type(ctx, isl_ast_expr);
457
5.14k
  if (!expr)
458
0
    goto error;
459
5.14k
460
5.14k
  expr->ctx = ctx;
461
5.14k
  isl_ctx_ref(ctx);
462
5.14k
  expr->ref = 1;
463
5.14k
  expr->type = isl_ast_expr_int;
464
5.14k
  expr->u.v = v;
465
5.14k
466
5.14k
  return expr;
467
0
error:
468
0
  isl_val_free(v);
469
0
  return NULL;
470
5.14k
}
471
472
/* Create an expression representing the unary operation "type" applied to
473
 * "arg".
474
 */
475
__isl_give isl_ast_expr *isl_ast_expr_alloc_unary(enum isl_ast_op_type type,
476
  __isl_take isl_ast_expr *arg)
477
870
{
478
870
  isl_ctx *ctx;
479
870
  isl_ast_expr *expr = NULL;
480
870
481
870
  if (!arg)
482
0
    return NULL;
483
870
484
870
  ctx = isl_ast_expr_get_ctx(arg);
485
870
  expr = isl_ast_expr_alloc_op(ctx, type, 1);
486
870
  if (!expr)
487
0
    goto error;
488
870
489
870
  expr->u.op.args[0] = arg;
490
870
491
870
  return expr;
492
0
error:
493
0
  isl_ast_expr_free(arg);
494
0
  return NULL;
495
870
}
496
497
/* Create an expression representing the negation of "arg".
498
 */
499
__isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
500
54
{
501
54
  return isl_ast_expr_alloc_unary(isl_ast_op_minus, arg);
502
54
}
503
504
/* Create an expression representing the address of "expr".
505
 */
506
__isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
507
816
{
508
816
  if (!expr)
509
0
    return NULL;
510
816
511
816
  
if (816
isl_ast_expr_get_type(expr) != isl_ast_expr_op ||816
512
816
      isl_ast_expr_get_op_type(expr) != isl_ast_op_access)
513
0
    isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
514
816
      "can only take address of access expressions",
515
816
      return isl_ast_expr_free(expr));
516
816
517
816
  return isl_ast_expr_alloc_unary(isl_ast_op_address_of, expr);
518
816
}
519
520
/* Create an expression representing the binary operation "type"
521
 * applied to "expr1" and "expr2".
522
 */
523
__isl_give isl_ast_expr *isl_ast_expr_alloc_binary(enum isl_ast_op_type type,
524
  __isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
525
7.29k
{
526
7.29k
  isl_ctx *ctx;
527
7.29k
  isl_ast_expr *expr = NULL;
528
7.29k
529
7.29k
  if (
!expr1 || 7.29k
!expr27.29k
)
530
0
    goto error;
531
7.29k
532
7.29k
  ctx = isl_ast_expr_get_ctx(expr1);
533
7.29k
  expr = isl_ast_expr_alloc_op(ctx, type, 2);
534
7.29k
  if (!expr)
535
0
    goto error;
536
7.29k
537
7.29k
  expr->u.op.args[0] = expr1;
538
7.29k
  expr->u.op.args[1] = expr2;
539
7.29k
540
7.29k
  return expr;
541
0
error:
542
0
  isl_ast_expr_free(expr1);
543
0
  isl_ast_expr_free(expr2);
544
0
  return NULL;
545
7.29k
}
546
547
/* Create an expression representing the sum of "expr1" and "expr2".
548
 */
549
__isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
550
  __isl_take isl_ast_expr *expr2)
551
2.41k
{
552
2.41k
  return isl_ast_expr_alloc_binary(isl_ast_op_add, expr1, expr2);
553
2.41k
}
554
555
/* Create an expression representing the difference of "expr1" and "expr2".
556
 */
557
__isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
558
  __isl_take isl_ast_expr *expr2)
559
113
{
560
113
  return isl_ast_expr_alloc_binary(isl_ast_op_sub, expr1, expr2);
561
113
}
562
563
/* Create an expression representing the product of "expr1" and "expr2".
564
 */
565
__isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
566
  __isl_take isl_ast_expr *expr2)
567
2.43k
{
568
2.43k
  return isl_ast_expr_alloc_binary(isl_ast_op_mul, expr1, expr2);
569
2.43k
}
570
571
/* Create an expression representing the quotient of "expr1" and "expr2".
572
 */
573
__isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
574
  __isl_take isl_ast_expr *expr2)
575
7
{
576
7
  return isl_ast_expr_alloc_binary(isl_ast_op_div, expr1, expr2);
577
7
}
578
579
/* Create an expression representing the quotient of the integer
580
 * division of "expr1" by "expr2", where "expr1" is known to be
581
 * non-negative.
582
 */
583
__isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
584
  __isl_take isl_ast_expr *expr2)
585
0
{
586
0
  return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_q, expr1, expr2);
587
0
}
588
589
/* Create an expression representing the remainder of the integer
590
 * division of "expr1" by "expr2", where "expr1" is known to be
591
 * non-negative.
592
 */
593
__isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
594
  __isl_take isl_ast_expr *expr2)
595
0
{
596
0
  return isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr1, expr2);
597
0
}
598
599
/* Create an expression representing the conjunction of "expr1" and "expr2".
600
 */
601
__isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
602
  __isl_take isl_ast_expr *expr2)
603
374
{
604
374
  return isl_ast_expr_alloc_binary(isl_ast_op_and, expr1, expr2);
605
374
}
606
607
/* Create an expression representing the conjunction of "expr1" and "expr2",
608
 * where "expr2" is evaluated only if "expr1" is evaluated to true.
609
 */
610
__isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
611
  __isl_take isl_ast_expr *expr2)
612
0
{
613
0
  return isl_ast_expr_alloc_binary(isl_ast_op_and_then, expr1, expr2);
614
0
}
615
616
/* Create an expression representing the disjunction of "expr1" and "expr2".
617
 */
618
__isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
619
  __isl_take isl_ast_expr *expr2)
620
262
{
621
262
  return isl_ast_expr_alloc_binary(isl_ast_op_or, expr1, expr2);
622
262
}
623
624
/* Create an expression representing the disjunction of "expr1" and "expr2",
625
 * where "expr2" is evaluated only if "expr1" is evaluated to false.
626
 */
627
__isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
628
  __isl_take isl_ast_expr *expr2)
629
0
{
630
0
  return isl_ast_expr_alloc_binary(isl_ast_op_or_else, expr1, expr2);
631
0
}
632
633
/* Create an expression representing "expr1" less than or equal to "expr2".
634
 */
635
__isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
636
  __isl_take isl_ast_expr *expr2)
637
310
{
638
310
  return isl_ast_expr_alloc_binary(isl_ast_op_le, expr1, expr2);
639
310
}
640
641
/* Create an expression representing "expr1" less than "expr2".
642
 */
643
__isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
644
  __isl_take isl_ast_expr *expr2)
645
0
{
646
0
  return isl_ast_expr_alloc_binary(isl_ast_op_lt, expr1, expr2);
647
0
}
648
649
/* Create an expression representing "expr1" greater than or equal to "expr2".
650
 */
651
__isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
652
  __isl_take isl_ast_expr *expr2)
653
0
{
654
0
  return isl_ast_expr_alloc_binary(isl_ast_op_ge, expr1, expr2);
655
0
}
656
657
/* Create an expression representing "expr1" greater than "expr2".
658
 */
659
__isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
660
  __isl_take isl_ast_expr *expr2)
661
0
{
662
0
  return isl_ast_expr_alloc_binary(isl_ast_op_gt, expr1, expr2);
663
0
}
664
665
/* Create an expression representing "expr1" equal to "expr2".
666
 */
667
__isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
668
  __isl_take isl_ast_expr *expr2)
669
132
{
670
132
  return isl_ast_expr_alloc_binary(isl_ast_op_eq, expr1, expr2);
671
132
}
672
673
/* Create an expression of type "type" with as arguments "arg0" followed
674
 * by "arguments".
675
 */
676
static __isl_give isl_ast_expr *ast_expr_with_arguments(
677
  enum isl_ast_op_type type, __isl_take isl_ast_expr *arg0,
678
  __isl_take isl_ast_expr_list *arguments)
679
0
{
680
0
  int i, n;
681
0
  isl_ctx *ctx;
682
0
  isl_ast_expr *res = NULL;
683
0
684
0
  if (
!arg0 || 0
!arguments0
)
685
0
    goto error;
686
0
687
0
  ctx = isl_ast_expr_get_ctx(arg0);
688
0
  n = isl_ast_expr_list_n_ast_expr(arguments);
689
0
  res = isl_ast_expr_alloc_op(ctx, type, 1 + n);
690
0
  if (!res)
691
0
    goto error;
692
0
  
for (i = 0; 0
i < n0
;
++i0
)
{0
693
0
    isl_ast_expr *arg;
694
0
    arg = isl_ast_expr_list_get_ast_expr(arguments, i);
695
0
    res->u.op.args[1 + i] = arg;
696
0
    if (!arg)
697
0
      goto error;
698
0
  }
699
0
  res->u.op.args[0] = arg0;
700
0
701
0
  isl_ast_expr_list_free(arguments);
702
0
  return res;
703
0
error:
704
0
  isl_ast_expr_free(arg0);
705
0
  isl_ast_expr_list_free(arguments);
706
0
  isl_ast_expr_free(res);
707
0
  return NULL;
708
0
}
709
710
/* Create an expression representing an access to "array" with index
711
 * expressions "indices".
712
 */
713
__isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
714
  __isl_take isl_ast_expr_list *indices)
715
0
{
716
0
  return ast_expr_with_arguments(isl_ast_op_access, array, indices);
717
0
}
718
719
/* Create an expression representing a call to "function" with argument
720
 * expressions "arguments".
721
 */
722
__isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
723
  __isl_take isl_ast_expr_list *arguments)
724
0
{
725
0
  return ast_expr_with_arguments(isl_ast_op_call, function, arguments);
726
0
}
727
728
/* For each subexpression of "expr" of type isl_ast_expr_id,
729
 * if it appears in "id2expr", then replace it by the corresponding
730
 * expression.
731
 */
732
__isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
733
  __isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
734
0
{
735
0
  int i;
736
0
  isl_maybe_isl_ast_expr m;
737
0
738
0
  if (
!expr || 0
!id2expr0
)
739
0
    goto error;
740
0
741
0
  switch (expr->type) {
742
0
  case isl_ast_expr_int:
743
0
    break;
744
0
  case isl_ast_expr_id:
745
0
    m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
746
0
    if (m.valid < 0)
747
0
      goto error;
748
0
    
if (0
!m.valid0
)
749
0
      break;
750
0
    isl_ast_expr_free(expr);
751
0
    expr = m.value;
752
0
    break;
753
0
  case isl_ast_expr_op:
754
0
    for (i = 0; 
i < expr->u.op.n_arg0
;
++i0
)
{0
755
0
      isl_ast_expr *arg;
756
0
      arg = isl_ast_expr_copy(expr->u.op.args[i]);
757
0
      arg = isl_ast_expr_substitute_ids(arg,
758
0
              isl_id_to_ast_expr_copy(id2expr));
759
0
      if (
arg == expr->u.op.args[i]0
)
{0
760
0
        isl_ast_expr_free(arg);
761
0
        continue;
762
0
      }
763
0
      
if (0
!arg0
)
764
0
        expr = isl_ast_expr_free(expr);
765
0
      expr = isl_ast_expr_cow(expr);
766
0
      if (
!expr0
)
{0
767
0
        isl_ast_expr_free(arg);
768
0
        break;
769
0
      }
770
0
      isl_ast_expr_free(expr->u.op.args[i]);
771
0
      expr->u.op.args[i] = arg;
772
0
    }
773
0
    break;
774
0
  case isl_ast_expr_error:
775
0
    expr = isl_ast_expr_free(expr);
776
0
    break;
777
0
  }
778
0
779
0
  isl_id_to_ast_expr_free(id2expr);
780
0
  return expr;
781
0
error:
782
0
  isl_ast_expr_free(expr);
783
0
  isl_id_to_ast_expr_free(id2expr);
784
0
  return NULL;
785
0
}
786
787
isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
788
3.94k
{
789
3.94k
  return node ? node->ctx : NULL;
790
3.94k
}
791
792
enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
793
904
{
794
904
  return node ? 
node->type904
:
isl_ast_node_error0
;
795
904
}
796
797
__isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
798
  enum isl_ast_node_type type)
799
2.56k
{
800
2.56k
  isl_ast_node *node;
801
2.56k
802
2.56k
  node = isl_calloc_type(ctx, isl_ast_node);
803
2.56k
  if (!node)
804
0
    return NULL;
805
2.56k
806
2.56k
  node->ctx = ctx;
807
2.56k
  isl_ctx_ref(ctx);
808
2.56k
  node->ref = 1;
809
2.56k
  node->type = type;
810
2.56k
811
2.56k
  return node;
812
2.56k
}
813
814
/* Create an if node with the given guard.
815
 *
816
 * The then body needs to be filled in later.
817
 */
818
__isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
819
129
{
820
129
  isl_ast_node *node;
821
129
822
129
  if (!guard)
823
0
    return NULL;
824
129
825
129
  node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
826
129
  if (!node)
827
0
    goto error;
828
129
  node->u.i.guard = guard;
829
129
830
129
  return node;
831
0
error:
832
0
  isl_ast_expr_free(guard);
833
0
  return NULL;
834
129
}
835
836
/* Create a for node with the given iterator.
837
 *
838
 * The remaining fields need to be filled in later.
839
 */
840
__isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
841
683
{
842
683
  isl_ast_node *node;
843
683
  isl_ctx *ctx;
844
683
845
683
  if (!id)
846
0
    return NULL;
847
683
848
683
  ctx = isl_id_get_ctx(id);
849
683
  node = isl_ast_node_alloc(ctx, isl_ast_node_for);
850
683
  if (!node)
851
0
    goto error;
852
683
853
683
  node->u.f.iterator = isl_ast_expr_from_id(id);
854
683
  if (!node->u.f.iterator)
855
0
    return isl_ast_node_free(node);
856
683
857
683
  return node;
858
0
error:
859
0
  isl_id_free(id);
860
0
  return NULL;
861
683
}
862
863
/* Create a mark node, marking "node" with "id".
864
 */
865
__isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
866
  __isl_take isl_ast_node *node)
867
117
{
868
117
  isl_ctx *ctx;
869
117
  isl_ast_node *mark;
870
117
871
117
  if (
!id || 117
!node117
)
872
0
    goto error;
873
117
874
117
  ctx = isl_id_get_ctx(id);
875
117
  mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
876
117
  if (!mark)
877
0
    goto error;
878
117
879
117
  mark->u.m.mark = id;
880
117
  mark->u.m.node = node;
881
117
882
117
  return mark;
883
0
error:
884
0
  isl_id_free(id);
885
0
  isl_ast_node_free(node);
886
0
  return NULL;
887
117
}
888
889
/* Create a user node evaluating "expr".
890
 */
891
__isl_give isl_ast_node *isl_ast_node_alloc_user(__isl_take isl_ast_expr *expr)
892
1.32k
{
893
1.32k
  isl_ctx *ctx;
894
1.32k
  isl_ast_node *node;
895
1.32k
896
1.32k
  if (!expr)
897
0
    return NULL;
898
1.32k
899
1.32k
  ctx = isl_ast_expr_get_ctx(expr);
900
1.32k
  node = isl_ast_node_alloc(ctx, isl_ast_node_user);
901
1.32k
  if (!node)
902
0
    goto error;
903
1.32k
904
1.32k
  node->u.e.expr = expr;
905
1.32k
906
1.32k
  return node;
907
0
error:
908
0
  isl_ast_expr_free(expr);
909
0
  return NULL;
910
1.32k
}
911
912
/* Create a block node with the given children.
913
 */
914
__isl_give isl_ast_node *isl_ast_node_alloc_block(
915
  __isl_take isl_ast_node_list *list)
916
315
{
917
315
  isl_ast_node *node;
918
315
  isl_ctx *ctx;
919
315
920
315
  if (!list)
921
0
    return NULL;
922
315
923
315
  ctx = isl_ast_node_list_get_ctx(list);
924
315
  node = isl_ast_node_alloc(ctx, isl_ast_node_block);
925
315
  if (!node)
926
0
    goto error;
927
315
928
315
  node->u.b.children = list;
929
315
930
315
  return node;
931
0
error:
932
0
  isl_ast_node_list_free(list);
933
0
  return NULL;
934
315
}
935
936
/* Represent the given list of nodes as a single node, either by
937
 * extract the node from a single element list or by creating
938
 * a block node with the list of nodes as children.
939
 */
940
__isl_give isl_ast_node *isl_ast_node_from_ast_node_list(
941
  __isl_take isl_ast_node_list *list)
942
2.73k
{
943
2.73k
  isl_ast_node *node;
944
2.73k
945
2.73k
  if (isl_ast_node_list_n_ast_node(list) != 1)
946
222
    return isl_ast_node_alloc_block(list);
947
2.73k
948
2.51k
  node = isl_ast_node_list_get_ast_node(list, 0);
949
2.51k
  isl_ast_node_list_free(list);
950
2.51k
951
2.51k
  return node;
952
2.73k
}
953
954
__isl_give isl_ast_node *isl_ast_node_copy(__isl_keep isl_ast_node *node)
955
7.13k
{
956
7.13k
  if (!node)
957
0
    return NULL;
958
7.13k
959
7.13k
  node->ref++;
960
7.13k
  return node;
961
7.13k
}
962
963
__isl_give isl_ast_node *isl_ast_node_dup(__isl_keep isl_ast_node *node)
964
0
{
965
0
  isl_ast_node *dup;
966
0
967
0
  if (!node)
968
0
    return NULL;
969
0
970
0
  dup = isl_ast_node_alloc(isl_ast_node_get_ctx(node), node->type);
971
0
  if (!dup)
972
0
    return NULL;
973
0
974
0
  switch (node->type) {
975
0
  case isl_ast_node_if:
976
0
    dup->u.i.guard = isl_ast_expr_copy(node->u.i.guard);
977
0
    dup->u.i.then = isl_ast_node_copy(node->u.i.then);
978
0
    dup->u.i.else_node = isl_ast_node_copy(node->u.i.else_node);
979
0
    if (
!dup->u.i.guard || 0
!dup->u.i.then0
||
980
0
        
(node->u.i.else_node && 0
!dup->u.i.else_node0
))
981
0
      return isl_ast_node_free(dup);
982
0
    break;
983
0
  case isl_ast_node_for:
984
0
    dup->u.f.iterator = isl_ast_expr_copy(node->u.f.iterator);
985
0
    dup->u.f.init = isl_ast_expr_copy(node->u.f.init);
986
0
    dup->u.f.cond = isl_ast_expr_copy(node->u.f.cond);
987
0
    dup->u.f.inc = isl_ast_expr_copy(node->u.f.inc);
988
0
    dup->u.f.body = isl_ast_node_copy(node->u.f.body);
989
0
    if (
!dup->u.f.iterator || 0
!dup->u.f.init0
||
!dup->u.f.cond0
||
990
0
        
!dup->u.f.inc0
||
!dup->u.f.body0
)
991
0
      return isl_ast_node_free(dup);
992
0
    break;
993
0
  case isl_ast_node_block:
994
0
    dup->u.b.children = isl_ast_node_list_copy(node->u.b.children);
995
0
    if (!dup->u.b.children)
996
0
      return isl_ast_node_free(dup);
997
0
    break;
998
0
  case isl_ast_node_mark:
999
0
    dup->u.m.mark = isl_id_copy(node->u.m.mark);
1000
0
    dup->u.m.node = isl_ast_node_copy(node->u.m.node);
1001
0
    if (
!dup->u.m.mark || 0
!dup->u.m.node0
)
1002
0
      return isl_ast_node_free(dup);
1003
0
    break;
1004
0
  case isl_ast_node_user:
1005
0
    dup->u.e.expr = isl_ast_expr_copy(node->u.e.expr);
1006
0
    if (!dup->u.e.expr)
1007
0
      return isl_ast_node_free(dup);
1008
0
    break;
1009
0
  case isl_ast_node_error:
1010
0
    break;
1011
0
  }
1012
0
1013
0
  return dup;
1014
0
}
1015
1016
__isl_give isl_ast_node *isl_ast_node_cow(__isl_take isl_ast_node *node)
1017
2.38k
{
1018
2.38k
  if (!node)
1019
0
    return NULL;
1020
2.38k
1021
2.38k
  
if (2.38k
node->ref == 12.38k
)
1022
2.38k
    return node;
1023
0
  node->ref--;
1024
0
  return isl_ast_node_dup(node);
1025
2.38k
}
1026
1027
__isl_null isl_ast_node *isl_ast_node_free(__isl_take isl_ast_node *node)
1028
10.6k
{
1029
10.6k
  if (!node)
1030
914
    return NULL;
1031
10.6k
1032
9.70k
  
if (9.70k
--node->ref > 09.70k
)
1033
7.13k
    return NULL;
1034
9.70k
1035
2.56k
  switch (node->type) {
1036
129
  case isl_ast_node_if:
1037
129
    isl_ast_expr_free(node->u.i.guard);
1038
129
    isl_ast_node_free(node->u.i.then);
1039
129
    isl_ast_node_free(node->u.i.else_node);
1040
129
    break;
1041
683
  case isl_ast_node_for:
1042
683
    isl_ast_expr_free(node->u.f.iterator);
1043
683
    isl_ast_expr_free(node->u.f.init);
1044
683
    isl_ast_expr_free(node->u.f.cond);
1045
683
    isl_ast_expr_free(node->u.f.inc);
1046
683
    isl_ast_node_free(node->u.f.body);
1047
683
    break;
1048
315
  case isl_ast_node_block:
1049
315
    isl_ast_node_list_free(node->u.b.children);
1050
315
    break;
1051
117
  case isl_ast_node_mark:
1052
117
    isl_id_free(node->u.m.mark);
1053
117
    isl_ast_node_free(node->u.m.node);
1054
117
    break;
1055
1.32k
  case isl_ast_node_user:
1056
1.32k
    isl_ast_expr_free(node->u.e.expr);
1057
1.32k
    break;
1058
0
  case isl_ast_node_error:
1059
0
    break;
1060
2.56k
  }
1061
2.56k
1062
2.56k
  isl_id_free(node->annotation);
1063
2.56k
  isl_ctx_deref(node->ctx);
1064
2.56k
  free(node);
1065
2.56k
1066
2.56k
  return NULL;
1067
2.56k
}
1068
1069
/* Replace the body of the for node "node" by "body".
1070
 */
1071
__isl_give isl_ast_node *isl_ast_node_for_set_body(
1072
  __isl_take isl_ast_node *node, __isl_take isl_ast_node *body)
1073
683
{
1074
683
  node = isl_ast_node_cow(node);
1075
683
  if (
!node || 683
!body683
)
1076
0
    goto error;
1077
683
  
if (683
node->type != isl_ast_node_for683
)
1078
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1079
683
      "not a for node", goto error);
1080
683
1081
683
  isl_ast_node_free(node->u.f.body);
1082
683
  node->u.f.body = body;
1083
683
1084
683
  return node;
1085
0
error:
1086
0
  isl_ast_node_free(node);
1087
0
  isl_ast_node_free(body);
1088
0
  return NULL;
1089
683
}
1090
1091
__isl_give isl_ast_node *isl_ast_node_for_get_body(
1092
  __isl_keep isl_ast_node *node)
1093
294
{
1094
294
  if (!node)
1095
0
    return NULL;
1096
294
  
if (294
node->type != isl_ast_node_for294
)
1097
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1098
294
      "not a for node", return NULL);
1099
294
  return isl_ast_node_copy(node->u.f.body);
1100
294
}
1101
1102
/* Mark the given for node as being degenerate.
1103
 */
1104
__isl_give isl_ast_node *isl_ast_node_for_mark_degenerate(
1105
  __isl_take isl_ast_node *node)
1106
0
{
1107
0
  node = isl_ast_node_cow(node);
1108
0
  if (!node)
1109
0
    return NULL;
1110
0
  node->u.f.degenerate = 1;
1111
0
  return node;
1112
0
}
1113
1114
isl_bool isl_ast_node_for_is_degenerate(__isl_keep isl_ast_node *node)
1115
0
{
1116
0
  if (!node)
1117
0
    return isl_bool_error;
1118
0
  
if (0
node->type != isl_ast_node_for0
)
1119
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1120
0
      "not a for node", return isl_bool_error);
1121
0
  return node->u.f.degenerate;
1122
0
}
1123
1124
__isl_give isl_ast_expr *isl_ast_node_for_get_iterator(
1125
  __isl_keep isl_ast_node *node)
1126
546
{
1127
546
  if (!node)
1128
0
    return NULL;
1129
546
  
if (546
node->type != isl_ast_node_for546
)
1130
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1131
546
      "not a for node", return NULL);
1132
546
  return isl_ast_expr_copy(node->u.f.iterator);
1133
546
}
1134
1135
__isl_give isl_ast_expr *isl_ast_node_for_get_init(
1136
  __isl_keep isl_ast_node *node)
1137
293
{
1138
293
  if (!node)
1139
0
    return NULL;
1140
293
  
if (293
node->type != isl_ast_node_for293
)
1141
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1142
293
      "not a for node", return NULL);
1143
293
  return isl_ast_expr_copy(node->u.f.init);
1144
293
}
1145
1146
/* Return the condition expression of the given for node.
1147
 *
1148
 * If the for node is degenerate, then the condition is not explicitly
1149
 * stored in the node.  Instead, it is constructed as
1150
 *
1151
 *  iterator <= init
1152
 */
1153
__isl_give isl_ast_expr *isl_ast_node_for_get_cond(
1154
  __isl_keep isl_ast_node *node)
1155
274
{
1156
274
  if (!node)
1157
0
    return NULL;
1158
274
  
if (274
node->type != isl_ast_node_for274
)
1159
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1160
274
      "not a for node", return NULL);
1161
274
  
if (274
!node->u.f.degenerate274
)
1162
274
    return isl_ast_expr_copy(node->u.f.cond);
1163
274
1164
0
  return isl_ast_expr_alloc_binary(isl_ast_op_le,
1165
0
        isl_ast_expr_copy(node->u.f.iterator),
1166
0
        isl_ast_expr_copy(node->u.f.init));
1167
274
}
1168
1169
/* Return the increment of the given for node.
1170
 *
1171
 * If the for node is degenerate, then the increment is not explicitly
1172
 * stored in the node.  We simply return "1".
1173
 */
1174
__isl_give isl_ast_expr *isl_ast_node_for_get_inc(
1175
  __isl_keep isl_ast_node *node)
1176
293
{
1177
293
  if (!node)
1178
0
    return NULL;
1179
293
  
if (293
node->type != isl_ast_node_for293
)
1180
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1181
293
      "not a for node", return NULL);
1182
293
  
if (293
!node->u.f.degenerate293
)
1183
293
    return isl_ast_expr_copy(node->u.f.inc);
1184
0
  return isl_ast_expr_alloc_int_si(isl_ast_node_get_ctx(node), 1);
1185
293
}
1186
1187
/* Replace the then branch of the if node "node" by "child".
1188
 */
1189
__isl_give isl_ast_node *isl_ast_node_if_set_then(
1190
  __isl_take isl_ast_node *node, __isl_take isl_ast_node *child)
1191
129
{
1192
129
  node = isl_ast_node_cow(node);
1193
129
  if (
!node || 129
!child129
)
1194
0
    goto error;
1195
129
  
if (129
node->type != isl_ast_node_if129
)
1196
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1197
129
      "not an if node", goto error);
1198
129
1199
129
  isl_ast_node_free(node->u.i.then);
1200
129
  node->u.i.then = child;
1201
129
1202
129
  return node;
1203
0
error:
1204
0
  isl_ast_node_free(node);
1205
0
  isl_ast_node_free(child);
1206
0
  return NULL;
1207
129
}
1208
1209
__isl_give isl_ast_node *isl_ast_node_if_get_then(
1210
  __isl_keep isl_ast_node *node)
1211
59
{
1212
59
  if (!node)
1213
0
    return NULL;
1214
59
  
if (59
node->type != isl_ast_node_if59
)
1215
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1216
59
      "not an if node", return NULL);
1217
59
  return isl_ast_node_copy(node->u.i.then);
1218
59
}
1219
1220
isl_bool isl_ast_node_if_has_else(
1221
  __isl_keep isl_ast_node *node)
1222
59
{
1223
59
  if (!node)
1224
0
    return isl_bool_error;
1225
59
  
if (59
node->type != isl_ast_node_if59
)
1226
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1227
59
      "not an if node", return isl_bool_error);
1228
59
  return node->u.i.else_node != NULL;
1229
59
}
1230
1231
__isl_give isl_ast_node *isl_ast_node_if_get_else(
1232
  __isl_keep isl_ast_node *node)
1233
10
{
1234
10
  if (!node)
1235
0
    return NULL;
1236
10
  
if (10
node->type != isl_ast_node_if10
)
1237
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1238
10
      "not an if node", return NULL);
1239
10
  return isl_ast_node_copy(node->u.i.else_node);
1240
10
}
1241
1242
__isl_give isl_ast_expr *isl_ast_node_if_get_cond(
1243
  __isl_keep isl_ast_node *node)
1244
59
{
1245
59
  if (!node)
1246
0
    return NULL;
1247
59
  
if (59
node->type != isl_ast_node_if59
)
1248
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1249
59
      "not a guard node", return NULL);
1250
59
  return isl_ast_expr_copy(node->u.i.guard);
1251
59
}
1252
1253
__isl_give isl_ast_node_list *isl_ast_node_block_get_children(
1254
  __isl_keep isl_ast_node *node)
1255
174
{
1256
174
  if (!node)
1257
0
    return NULL;
1258
174
  
if (174
node->type != isl_ast_node_block174
)
1259
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1260
174
      "not a block node", return NULL);
1261
174
  return isl_ast_node_list_copy(node->u.b.children);
1262
174
}
1263
1264
__isl_give isl_ast_expr *isl_ast_node_user_get_expr(
1265
  __isl_keep isl_ast_node *node)
1266
414
{
1267
414
  if (!node)
1268
0
    return NULL;
1269
414
  
if (414
node->type != isl_ast_node_user414
)
1270
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1271
414
      "not a user node", return NULL);
1272
414
1273
414
  return isl_ast_expr_copy(node->u.e.expr);
1274
414
}
1275
1276
/* Return the mark identifier of the mark node "node".
1277
 */
1278
__isl_give isl_id *isl_ast_node_mark_get_id(__isl_keep isl_ast_node *node)
1279
55
{
1280
55
  if (!node)
1281
0
    return NULL;
1282
55
  
if (55
node->type != isl_ast_node_mark55
)
1283
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1284
55
      "not a mark node", return NULL);
1285
55
1286
55
  return isl_id_copy(node->u.m.mark);
1287
55
}
1288
1289
/* Return the node marked by mark node "node".
1290
 */
1291
__isl_give isl_ast_node *isl_ast_node_mark_get_node(
1292
  __isl_keep isl_ast_node *node)
1293
15
{
1294
15
  if (!node)
1295
0
    return NULL;
1296
15
  
if (15
node->type != isl_ast_node_mark15
)
1297
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
1298
15
      "not a mark node", return NULL);
1299
15
1300
15
  return isl_ast_node_copy(node->u.m.node);
1301
15
}
1302
1303
__isl_give isl_id *isl_ast_node_get_annotation(__isl_keep isl_ast_node *node)
1304
2.79k
{
1305
2.79k
  return node ? isl_id_copy(node->annotation) : NULL;
1306
2.79k
}
1307
1308
/* Replace node->annotation by "annotation".
1309
 */
1310
__isl_give isl_ast_node *isl_ast_node_set_annotation(
1311
  __isl_take isl_ast_node *node, __isl_take isl_id *annotation)
1312
1.57k
{
1313
1.57k
  node = isl_ast_node_cow(node);
1314
1.57k
  if (
!node || 1.57k
!annotation1.57k
)
1315
0
    goto error;
1316
1.57k
1317
1.57k
  isl_id_free(node->annotation);
1318
1.57k
  node->annotation = annotation;
1319
1.57k
1320
1.57k
  return node;
1321
0
error:
1322
0
  isl_id_free(annotation);
1323
0
  return isl_ast_node_free(node);
1324
1.57k
}
1325
1326
/* Traverse the elements of "list" and all their descendants
1327
 * in depth first preorder.
1328
 *
1329
 * Return isl_stat_ok on success and isl_stat_error on failure.
1330
 */
1331
static isl_stat nodelist_foreach(__isl_keep isl_ast_node_list *list,
1332
  isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1333
1
{
1334
1
  int i;
1335
1
1336
1
  if (!list)
1337
0
    return isl_stat_error;
1338
1
1339
3
  
for (i = 0; 1
i < list->n3
;
++i2
)
{2
1340
2
    isl_stat ok;
1341
2
    isl_ast_node *node = list->p[i];
1342
2
1343
2
    ok = isl_ast_node_foreach_descendant_top_down(node, fn, user);
1344
2
    if (ok < 0)
1345
0
      return isl_stat_error;
1346
2
  }
1347
1
1348
1
  return isl_stat_ok;
1349
1
}
1350
1351
/* Traverse the descendants of "node" (including the node itself)
1352
 * in depth first preorder.
1353
 *
1354
 * If "fn" returns isl_bool_error on any of the nodes, then the traversal
1355
 * is aborted.
1356
 * If "fn" returns isl_bool_false on any of the nodes, then the subtree rooted
1357
 * at that node is skipped.
1358
 *
1359
 * Return isl_stat_ok on success and isl_stat_error on failure.
1360
 */
1361
isl_stat isl_ast_node_foreach_descendant_top_down(
1362
  __isl_keep isl_ast_node *node,
1363
  isl_bool (*fn)(__isl_keep isl_ast_node *node, void *user), void *user)
1364
36
{
1365
36
  isl_bool more;
1366
36
  isl_stat ok;
1367
36
1368
36
  if (!node)
1369
0
    return isl_stat_error;
1370
36
1371
36
  more = fn(node, user);
1372
36
  if (more < 0)
1373
0
    return isl_stat_error;
1374
36
  
if (36
!more36
)
1375
0
    return isl_stat_ok;
1376
36
1377
36
  switch (node->type) {
1378
17
  case isl_ast_node_for:
1379
17
    node = node->u.f.body;
1380
17
    return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1381
0
  case isl_ast_node_if:
1382
0
    ok = isl_ast_node_foreach_descendant_top_down(node->u.i.then,
1383
0
                fn, user);
1384
0
    if (ok < 0)
1385
0
      return isl_stat_error;
1386
0
    
if (0
!node->u.i.else_node0
)
1387
0
      return isl_stat_ok;
1388
0
    node = node->u.i.else_node;
1389
0
    return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1390
1
  case isl_ast_node_block:
1391
1
    return nodelist_foreach(node->u.b.children, fn, user);
1392
0
  case isl_ast_node_mark:
1393
0
    node = node->u.m.node;
1394
0
    return isl_ast_node_foreach_descendant_top_down(node, fn, user);
1395
18
  case isl_ast_node_user:
1396
18
    break;
1397
0
  case isl_ast_node_error:
1398
0
    return isl_stat_error;
1399
36
  }
1400
36
1401
18
  return isl_stat_ok;
1402
36
}
1403
1404
/* Textual C representation of the various operators.
1405
 */
1406
static char *op_str_c[] = {
1407
  [isl_ast_op_and] = "&&",
1408
  [isl_ast_op_and_then] = "&&",
1409
  [isl_ast_op_or] = "||",
1410
  [isl_ast_op_or_else] = "||",
1411
  [isl_ast_op_max] = "max",
1412
  [isl_ast_op_min] = "min",
1413
  [isl_ast_op_minus] = "-",
1414
  [isl_ast_op_add] = "+",
1415
  [isl_ast_op_sub] = "-",
1416
  [isl_ast_op_mul] = "*",
1417
  [isl_ast_op_fdiv_q] = "floord",
1418
  [isl_ast_op_pdiv_q] = "/",
1419
  [isl_ast_op_pdiv_r] = "%",
1420
  [isl_ast_op_zdiv_r] = "%",
1421
  [isl_ast_op_div] = "/",
1422
  [isl_ast_op_eq] = "==",
1423
  [isl_ast_op_le] = "<=",
1424
  [isl_ast_op_ge] = ">=",
1425
  [isl_ast_op_lt] = "<",
1426
  [isl_ast_op_gt] = ">",
1427
  [isl_ast_op_member] = ".",
1428
  [isl_ast_op_address_of] = "&"
1429
};
1430
1431
/* Precedence in C of the various operators.
1432
 * Based on http://en.wikipedia.org/wiki/Operators_in_C_and_C++
1433
 * Lowest value means highest precedence.
1434
 */
1435
static int op_prec[] = {
1436
  [isl_ast_op_and] = 13,
1437
  [isl_ast_op_and_then] = 13,
1438
  [isl_ast_op_or] = 14,
1439
  [isl_ast_op_or_else] = 14,
1440
  [isl_ast_op_max] = 2,
1441
  [isl_ast_op_min] = 2,
1442
  [isl_ast_op_minus] = 3,
1443
  [isl_ast_op_add] = 6,
1444
  [isl_ast_op_sub] = 6,
1445
  [isl_ast_op_mul] = 5,
1446
  [isl_ast_op_div] = 5,
1447
  [isl_ast_op_fdiv_q] = 2,
1448
  [isl_ast_op_pdiv_q] = 5,
1449
  [isl_ast_op_pdiv_r] = 5,
1450
  [isl_ast_op_zdiv_r] = 5,
1451
  [isl_ast_op_cond] = 15,
1452
  [isl_ast_op_select] = 15,
1453
  [isl_ast_op_eq] = 9,
1454
  [isl_ast_op_le] = 8,
1455
  [isl_ast_op_ge] = 8,
1456
  [isl_ast_op_lt] = 8,
1457
  [isl_ast_op_gt] = 8,
1458
  [isl_ast_op_call] = 2,
1459
  [isl_ast_op_access] = 2,
1460
  [isl_ast_op_member] = 2,
1461
  [isl_ast_op_address_of] = 3
1462
};
1463
1464
/* Is the operator left-to-right associative?
1465
 */
1466
static int op_left[] = {
1467
  [isl_ast_op_and] = 1,
1468
  [isl_ast_op_and_then] = 1,
1469
  [isl_ast_op_or] = 1,
1470
  [isl_ast_op_or_else] = 1,
1471
  [isl_ast_op_max] = 1,
1472
  [isl_ast_op_min] = 1,
1473
  [isl_ast_op_minus] = 0,
1474
  [isl_ast_op_add] = 1,
1475
  [isl_ast_op_sub] = 1,
1476
  [isl_ast_op_mul] = 1,
1477
  [isl_ast_op_div] = 1,
1478
  [isl_ast_op_fdiv_q] = 1,
1479
  [isl_ast_op_pdiv_q] = 1,
1480
  [isl_ast_op_pdiv_r] = 1,
1481
  [isl_ast_op_zdiv_r] = 1,
1482
  [isl_ast_op_cond] = 0,
1483
  [isl_ast_op_select] = 0,
1484
  [isl_ast_op_eq] = 1,
1485
  [isl_ast_op_le] = 1,
1486
  [isl_ast_op_ge] = 1,
1487
  [isl_ast_op_lt] = 1,
1488
  [isl_ast_op_gt] = 1,
1489
  [isl_ast_op_call] = 1,
1490
  [isl_ast_op_access] = 1,
1491
  [isl_ast_op_member] = 1,
1492
  [isl_ast_op_address_of] = 0
1493
};
1494
1495
static int is_and(enum isl_ast_op_type op)
1496
163
{
1497
146
  return op == isl_ast_op_and || op == isl_ast_op_and_then;
1498
163
}
1499
1500
static int is_or(enum isl_ast_op_type op)
1501
3.60k
{
1502
3.43k
  return op == isl_ast_op_or || op == isl_ast_op_or_else;
1503
3.60k
}
1504
1505
static int is_add_sub(enum isl_ast_op_type op)
1506
3.58k
{
1507
849
  return op == isl_ast_op_add || op == isl_ast_op_sub;
1508
3.58k
}
1509
1510
static int is_div_mod(enum isl_ast_op_type op)
1511
2.76k
{
1512
2.76k
  return op == isl_ast_op_div ||
1513
2.76k
         op == isl_ast_op_pdiv_r ||
1514
2.76k
         op == isl_ast_op_zdiv_r;
1515
2.76k
}
1516
1517
static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1518
  __isl_keep isl_ast_expr *expr);
1519
1520
/* Do we need/want parentheses around "expr" as a subexpression of
1521
 * an "op" operation?  If "left" is set, then "expr" is the left-most
1522
 * operand.
1523
 *
1524
 * We only need parentheses if "expr" represents an operation.
1525
 *
1526
 * If op has a higher precedence than expr->u.op.op, then we need
1527
 * parentheses.
1528
 * If op and expr->u.op.op have the same precedence, but the operations
1529
 * are performed in an order that is different from the associativity,
1530
 * then we need parentheses.
1531
 *
1532
 * An and inside an or technically does not require parentheses,
1533
 * but some compilers complain about that, so we add them anyway.
1534
 *
1535
 * Computations such as "a / b * c" and "a % b + c" can be somewhat
1536
 * difficult to read, so we add parentheses for those as well.
1537
 */
1538
static int sub_expr_need_parens(enum isl_ast_op_type op,
1539
  __isl_keep isl_ast_expr *expr, int left)
1540
11.3k
{
1541
11.3k
  if (expr->type != isl_ast_expr_op)
1542
7.64k
    return 0;
1543
11.3k
1544
3.68k
  
if (3.68k
op_prec[expr->u.op.op] > op_prec[op]3.68k
)
1545
81
    return 1;
1546
3.60k
  
if (3.60k
op_prec[expr->u.op.op] == op_prec[op] && 3.60k
left != op_left[op]528
)
1547
2
    return 1;
1548
3.60k
1549
3.60k
  
if (3.60k
is_or(op) && 3.60k
is_and(expr->u.op.op)163
)
1550
17
    return 1;
1551
3.58k
  
if (3.58k
op == isl_ast_op_mul && 3.58k
expr->u.op.op != isl_ast_op_mul2
&&
1552
2
      op_prec[expr->u.op.op] == op_prec[op])
1553
0
    return 1;
1554
3.58k
  
if (3.58k
is_add_sub(op) && 3.58k
is_div_mod(expr->u.op.op)2.76k
)
1555
2
    return 1;
1556
3.58k
1557
3.58k
  return 0;
1558
3.58k
}
1559
1560
/* Print "expr" as a subexpression of an "op" operation in C format.
1561
 * If "left" is set, then "expr" is the left-most operand.
1562
 */
1563
static __isl_give isl_printer *print_sub_expr_c(__isl_take isl_printer *p,
1564
  enum isl_ast_op_type op, __isl_keep isl_ast_expr *expr, int left)
1565
11.3k
{
1566
11.3k
  int need_parens;
1567
11.3k
1568
11.3k
  need_parens = sub_expr_need_parens(op, expr, left);
1569
11.3k
1570
11.3k
  if (need_parens)
1571
102
    p = isl_printer_print_str(p, "(");
1572
11.3k
  p = print_ast_expr_c(p, expr);
1573
11.3k
  if (need_parens)
1574
102
    p = isl_printer_print_str(p, ")");
1575
11.3k
  return p;
1576
11.3k
}
1577
1578
0
#define isl_ast_op_last isl_ast_op_address_of
1579
1580
/* Data structure that holds the user-specified textual
1581
 * representations for the operators in C format.
1582
 * The entries are either NULL or copies of strings.
1583
 * A NULL entry means that the default name should be used.
1584
 */
1585
struct isl_ast_op_names {
1586
  char *op_str[isl_ast_op_last + 1];
1587
};
1588
1589
/* Create an empty struct isl_ast_op_names.
1590
 */
1591
static void *create_names(isl_ctx *ctx)
1592
0
{
1593
0
  return isl_calloc_type(ctx, struct isl_ast_op_names);
1594
0
}
1595
1596
/* Free a struct isl_ast_op_names along with all memory
1597
 * owned by the struct.
1598
 */
1599
static void free_names(void *user)
1600
0
{
1601
0
  int i;
1602
0
  struct isl_ast_op_names *names = user;
1603
0
1604
0
  if (!user)
1605
0
    return;
1606
0
1607
0
  
for (i = 0; 0
i <= 0
isl_ast_op_last0
;
++i0
)
1608
0
    free(names->op_str[i]);
1609
0
  free(user);
1610
0
}
1611
1612
/* Create an identifier that is used to store
1613
 * an isl_ast_op_names note.
1614
 */
1615
static __isl_give isl_id *names_id(isl_ctx *ctx)
1616
5.81k
{
1617
5.81k
  return isl_id_alloc(ctx, "isl_ast_op_type_names", NULL);
1618
5.81k
}
1619
1620
/* Ensure that "p" has a note identified by "id".
1621
 * If there is no such note yet, then it is created by "note_create" and
1622
 * scheduled do be freed by "note_free".
1623
 */
1624
static __isl_give isl_printer *alloc_note(__isl_take isl_printer *p,
1625
  __isl_keep isl_id *id, void *(*note_create)(isl_ctx *),
1626
  void (*note_free)(void *))
1627
0
{
1628
0
  isl_ctx *ctx;
1629
0
  isl_id *note_id;
1630
0
  isl_bool has_note;
1631
0
  void *note;
1632
0
1633
0
  has_note = isl_printer_has_note(p, id);
1634
0
  if (has_note < 0)
1635
0
    return isl_printer_free(p);
1636
0
  
if (0
has_note0
)
1637
0
    return p;
1638
0
1639
0
  ctx = isl_printer_get_ctx(p);
1640
0
  note = note_create(ctx);
1641
0
  if (!note)
1642
0
    return isl_printer_free(p);
1643
0
  note_id = isl_id_alloc(ctx, NULL, note);
1644
0
  if (!note_id)
1645
0
    note_free(note);
1646
0
  else
1647
0
    note_id = isl_id_set_free_user(note_id, note_free);
1648
0
1649
0
  p = isl_printer_set_note(p, isl_id_copy(id), note_id);
1650
0
1651
0
  return p;
1652
0
}
1653
1654
/* Ensure that "p" has an isl_ast_op_names note identified by "id".
1655
 */
1656
static __isl_give isl_printer *alloc_names(__isl_take isl_printer *p,
1657
  __isl_keep isl_id *id)
1658
0
{
1659
0
  return alloc_note(p, id, &create_names, &free_names);
1660
0
}
1661
1662
/* Retrieve the note identified by "id" from "p".
1663
 * The note is assumed to exist.
1664
 */
1665
static void *get_note(__isl_keep isl_printer *p, __isl_keep isl_id *id)
1666
0
{
1667
0
  void *note;
1668
0
1669
0
  id = isl_printer_get_note(p, isl_id_copy(id));
1670
0
  note = isl_id_get_user(id);
1671
0
  isl_id_free(id);
1672
0
1673
0
  return note;
1674
0
}
1675
1676
/* Use "name" to print operations of type "type" to "p".
1677
 *
1678
 * Store the name in an isl_ast_op_names note attached to "p", such that
1679
 * it can be retrieved by get_op_str.
1680
 */
1681
__isl_give isl_printer *isl_ast_op_type_set_print_name(
1682
  __isl_take isl_printer *p, enum isl_ast_op_type type,
1683
  __isl_keep const char *name)
1684
0
{
1685
0
  isl_id *id;
1686
0
  struct isl_ast_op_names *names;
1687
0
1688
0
  if (!p)
1689
0
    return NULL;
1690
0
  
if (0
type > 0
isl_ast_op_last0
)
1691
0
    isl_die(isl_printer_get_ctx(p), isl_error_invalid,
1692
0
      "invalid type", return isl_printer_free(p));
1693
0
1694
0
  id = names_id(isl_printer_get_ctx(p));
1695
0
  p = alloc_names(p, id);
1696
0
  names = get_note(p, id);
1697
0
  isl_id_free(id);
1698
0
  if (!names)
1699
0
    return isl_printer_free(p);
1700
0
  free(names->op_str[type]);
1701
0
  names->op_str[type] = strdup(name);
1702
0
1703
0
  return p;
1704
0
}
1705
1706
/* Return the textual representation of "type" in C format.
1707
 *
1708
 * If there is a user-specified name in an isl_ast_op_names note
1709
 * associated to "p", then return that.
1710
 * Otherwise, return the default name in op_str.
1711
 */
1712
static const char *get_op_str_c(__isl_keep isl_printer *p,
1713
  enum isl_ast_op_type type)
1714
5.81k
{
1715
5.81k
  isl_id *id;
1716
5.81k
  isl_bool has_names;
1717
5.81k
  struct isl_ast_op_names *names = NULL;
1718
5.81k
1719
5.81k
  id = names_id(isl_printer_get_ctx(p));
1720
5.81k
  has_names = isl_printer_has_note(p, id);
1721
5.81k
  if (
has_names >= 0 && 5.81k
has_names5.81k
)
1722
0
    names = get_note(p, id);
1723
5.81k
  isl_id_free(id);
1724
5.81k
  if (
names && 5.81k
names->op_str[type]0
)
1725
0
    return names->op_str[type];
1726
5.81k
  return op_str_c[type];
1727
5.81k
}
1728
1729
/* Print a min or max reduction "expr" in C format.
1730
 */
1731
static __isl_give isl_printer *print_min_max_c(__isl_take isl_printer *p,
1732
  __isl_keep isl_ast_expr *expr)
1733
43
{
1734
43
  int i = 0;
1735
43
1736
86
  for (i = 1; 
i < expr->u.op.n_arg86
;
++i43
)
{43
1737
43
    p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1738
43
    p = isl_printer_print_str(p, "(");
1739
43
  }
1740
43
  p = isl_printer_print_ast_expr(p, expr->u.op.args[0]);
1741
86
  for (i = 1; 
i < expr->u.op.n_arg86
;
++i43
)
{43
1742
43
    p = isl_printer_print_str(p, ", ");
1743
43
    p = print_ast_expr_c(p, expr->u.op.args[i]);
1744
43
    p = isl_printer_print_str(p, ")");
1745
43
  }
1746
43
1747
43
  return p;
1748
43
}
1749
1750
/* Print a function call "expr" in C format.
1751
 *
1752
 * The first argument represents the function to be called.
1753
 */
1754
static __isl_give isl_printer *print_call_c(__isl_take isl_printer *p,
1755
  __isl_keep isl_ast_expr *expr)
1756
877
{
1757
877
  int i = 0;
1758
877
1759
877
  p = print_ast_expr_c(p, expr->u.op.args[0]);
1760
877
  p = isl_printer_print_str(p, "(");
1761
3.13k
  for (i = 1; 
i < expr->u.op.n_arg3.13k
;
++i2.25k
)
{2.25k
1762
2.25k
    if (i != 1)
1763
1.38k
      p = isl_printer_print_str(p, ", ");
1764
2.25k
    p = print_ast_expr_c(p, expr->u.op.args[i]);
1765
2.25k
  }
1766
877
  p = isl_printer_print_str(p, ")");
1767
877
1768
877
  return p;
1769
877
}
1770
1771
/* Print an array access "expr" in C format.
1772
 *
1773
 * The first argument represents the array being accessed.
1774
 */
1775
static __isl_give isl_printer *print_access_c(__isl_take isl_printer *p,
1776
  __isl_keep isl_ast_expr *expr)
1777
184
{
1778
184
  int i = 0;
1779
184
1780
184
  p = print_ast_expr_c(p, expr->u.op.args[0]);
1781
464
  for (i = 1; 
i < expr->u.op.n_arg464
;
++i280
)
{280
1782
280
    p = isl_printer_print_str(p, "[");
1783
280
    p = print_ast_expr_c(p, expr->u.op.args[i]);
1784
280
    p = isl_printer_print_str(p, "]");
1785
280
  }
1786
184
1787
184
  return p;
1788
184
}
1789
1790
/* Print "expr" to "p" in C format.
1791
 */
1792
static __isl_give isl_printer *print_ast_expr_c(__isl_take isl_printer *p,
1793
  __isl_keep isl_ast_expr *expr)
1794
17.2k
{
1795
17.2k
  if (!p)
1796
0
    return NULL;
1797
17.2k
  
if (17.2k
!expr17.2k
)
1798
0
    return isl_printer_free(p);
1799
17.2k
1800
17.2k
  switch (expr->type) {
1801
6.88k
  case isl_ast_expr_op:
1802
6.88k
    if (
expr->u.op.op == isl_ast_op_call6.88k
)
{877
1803
877
      p = print_call_c(p, expr);
1804
877
      break;
1805
877
    }
1806
6.00k
    
if (6.00k
expr->u.op.op == isl_ast_op_access6.00k
)
{184
1807
184
      p = print_access_c(p, expr);
1808
184
      break;
1809
184
    }
1810
5.82k
    
if (5.82k
expr->u.op.n_arg == 15.82k
)
{203
1811
203
      p = isl_printer_print_str(p,
1812
203
            get_op_str_c(p, expr->u.op.op));
1813
203
      p = print_sub_expr_c(p, expr->u.op.op,
1814
203
            expr->u.op.args[0], 0);
1815
203
      break;
1816
203
    }
1817
5.61k
    
if (5.61k
expr->u.op.op == isl_ast_op_fdiv_q5.61k
)
{8
1818
8
      const char *name = get_op_str_c(p, isl_ast_op_fdiv_q);
1819
8
      p = isl_printer_print_str(p, name);
1820
8
      p = isl_printer_print_str(p, "(");
1821
8
      p = print_ast_expr_c(p, expr->u.op.args[0]);
1822
8
      p = isl_printer_print_str(p, ", ");
1823
8
      p = print_ast_expr_c(p, expr->u.op.args[1]);
1824
8
      p = isl_printer_print_str(p, ")");
1825
8
      break;
1826
8
    }
1827
5.60k
    
if (5.60k
expr->u.op.op == isl_ast_op_max ||5.60k
1828
5.60k
        
expr->u.op.op == isl_ast_op_min5.60k
)
{43
1829
43
      p = print_min_max_c(p, expr);
1830
43
      break;
1831
43
    }
1832
5.56k
    
if (5.56k
expr->u.op.op == isl_ast_op_cond ||5.56k
1833
5.56k
        
expr->u.op.op == isl_ast_op_select5.56k
)
{3
1834
3
      p = print_ast_expr_c(p, expr->u.op.args[0]);
1835
3
      p = isl_printer_print_str(p, " ? ");
1836
3
      p = print_ast_expr_c(p, expr->u.op.args[1]);
1837
3
      p = isl_printer_print_str(p, " : ");
1838
3
      p = print_ast_expr_c(p, expr->u.op.args[2]);
1839
3
      break;
1840
3
    }
1841
5.56k
    
if (5.56k
expr->u.op.n_arg != 25.56k
)
1842
0
      isl_die(isl_printer_get_ctx(p), isl_error_internal,
1843
5.56k
        "operation should have two arguments",
1844
5.56k
        return isl_printer_free(p));
1845
5.56k
    p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[0], 1);
1846
5.56k
    if (expr->u.op.op != isl_ast_op_member)
1847
5.56k
      p = isl_printer_print_str(p, " ");
1848
5.56k
    p = isl_printer_print_str(p, get_op_str_c(p, expr->u.op.op));
1849
5.56k
    if (expr->u.op.op != isl_ast_op_member)
1850
5.56k
      p = isl_printer_print_str(p, " ");
1851
5.56k
    p = print_sub_expr_c(p, expr->u.op.op, expr->u.op.args[1], 0);
1852
5.56k
    break;
1853
5.23k
  case isl_ast_expr_id:
1854
5.23k
    p = isl_printer_print_str(p, isl_id_get_name(expr->u.id));
1855
5.23k
    break;
1856
5.14k
  case isl_ast_expr_int:
1857
5.14k
    p = isl_printer_print_val(p, expr->u.v);
1858
5.14k
    break;
1859
0
  case isl_ast_expr_error:
1860
0
    break;
1861
17.2k
  }
1862
17.2k
1863
17.2k
  return p;
1864
17.2k
}
1865
1866
/* Textual representation of the isl_ast_op_type elements
1867
 * for use in a YAML representation of an isl_ast_expr.
1868
 */
1869
static char *op_str[] = {
1870
  [isl_ast_op_and] = "and",
1871
  [isl_ast_op_and_then] = "and_then",
1872
  [isl_ast_op_or] = "or",
1873
  [isl_ast_op_or_else] = "or_else",
1874
  [isl_ast_op_max] = "max",
1875
  [isl_ast_op_min] = "min",
1876
  [isl_ast_op_minus] = "minus",
1877
  [isl_ast_op_add] = "add",
1878
  [isl_ast_op_sub] = "sub",
1879
  [isl_ast_op_mul] = "mul",
1880
  [isl_ast_op_div] = "div",
1881
  [isl_ast_op_fdiv_q] = "fdiv_q",
1882
  [isl_ast_op_pdiv_q] = "pdiv_q",
1883
  [isl_ast_op_pdiv_r] = "pdiv_r",
1884
  [isl_ast_op_zdiv_r] = "zdiv_r",
1885
  [isl_ast_op_cond] = "cond",
1886
  [isl_ast_op_select] = "select",
1887
  [isl_ast_op_eq] = "eq",
1888
  [isl_ast_op_le] = "le",
1889
  [isl_ast_op_lt] = "lt",
1890
  [isl_ast_op_ge] = "ge",
1891
  [isl_ast_op_gt] = "gt",
1892
  [isl_ast_op_call] = "call",
1893
  [isl_ast_op_access] = "access",
1894
  [isl_ast_op_member] = "member",
1895
  [isl_ast_op_address_of] = "address_of"
1896
};
1897
1898
static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1899
  __isl_keep isl_ast_expr *expr);
1900
1901
/* Print the arguments of "expr" to "p" in isl format.
1902
 *
1903
 * If there are no arguments, then nothing needs to be printed.
1904
 * Otherwise add an "args" key to the current mapping with as value
1905
 * the list of arguments of "expr".
1906
 */
1907
static __isl_give isl_printer *print_arguments(__isl_take isl_printer *p,
1908
  __isl_keep isl_ast_expr *expr)
1909
0
{
1910
0
  int i, n;
1911
0
1912
0
  n = isl_ast_expr_get_op_n_arg(expr);
1913
0
  if (n < 0)
1914
0
    return isl_printer_free(p);
1915
0
  
if (0
n == 00
)
1916
0
    return p;
1917
0
1918
0
  p = isl_printer_print_str(p, "args");
1919
0
  p = isl_printer_yaml_next(p);
1920
0
  p = isl_printer_yaml_start_sequence(p);
1921
0
  for (i = 0; 
i < n0
;
++i0
)
{0
1922
0
    isl_ast_expr *arg;
1923
0
1924
0
    arg = isl_ast_expr_get_op_arg(expr, i);
1925
0
    p = print_ast_expr_isl(p, arg);
1926
0
    isl_ast_expr_free(arg);
1927
0
    p = isl_printer_yaml_next(p);
1928
0
  }
1929
0
  p = isl_printer_yaml_end_sequence(p);
1930
0
1931
0
  return p;
1932
0
}
1933
1934
/* Print "expr" to "p" in isl format.
1935
 *
1936
 * In particular, print the isl_ast_expr as a YAML document.
1937
 */
1938
static __isl_give isl_printer *print_ast_expr_isl(__isl_take isl_printer *p,
1939
  __isl_keep isl_ast_expr *expr)
1940
0
{
1941
0
  enum isl_ast_expr_type type;
1942
0
  enum isl_ast_op_type op;
1943
0
  isl_id *id;
1944
0
  isl_val *v;
1945
0
1946
0
  if (!expr)
1947
0
    return isl_printer_free(p);
1948
0
1949
0
  p = isl_printer_yaml_start_mapping(p);
1950
0
  type = isl_ast_expr_get_type(expr);
1951
0
  switch (type) {
1952
0
  case isl_ast_expr_error:
1953
0
    return isl_printer_free(p);
1954
0
  case isl_ast_expr_op:
1955
0
    op = isl_ast_expr_get_op_type(expr);
1956
0
    if (op == isl_ast_op_error)
1957
0
      return isl_printer_free(p);
1958
0
    p = isl_printer_print_str(p, "op");
1959
0
    p = isl_printer_yaml_next(p);
1960
0
    p = isl_printer_print_str(p, op_str[op]);
1961
0
    p = isl_printer_yaml_next(p);
1962
0
    p = print_arguments(p, expr);
1963
0
    break;
1964
0
  case isl_ast_expr_id:
1965
0
    p = isl_printer_print_str(p, "id");
1966
0
    p = isl_printer_yaml_next(p);
1967
0
    id = isl_ast_expr_get_id(expr);
1968
0
    p = isl_printer_print_id(p, id);
1969
0
    isl_id_free(id);
1970
0
    break;
1971
0
  case isl_ast_expr_int:
1972
0
    p = isl_printer_print_str(p, "val");
1973
0
    p = isl_printer_yaml_next(p);
1974
0
    v = isl_ast_expr_get_val(expr);
1975
0
    p = isl_printer_print_val(p, v);
1976
0
    isl_val_free(v);
1977
0
    break;
1978
0
  }
1979
0
  p = isl_printer_yaml_end_mapping(p);
1980
0
1981
0
  return p;
1982
0
}
1983
1984
/* Print "expr" to "p".
1985
 *
1986
 * Only an isl and a C format are supported.
1987
 */
1988
__isl_give isl_printer *isl_printer_print_ast_expr(__isl_take isl_printer *p,
1989
  __isl_keep isl_ast_expr *expr)
1990
2.26k
{
1991
2.26k
  int format;
1992
2.26k
1993
2.26k
  if (!p)
1994
0
    return NULL;
1995
2.26k
1996
2.26k
  format = isl_printer_get_output_format(p);
1997
2.26k
  switch (format) {
1998
0
  
case 0
ISL_FORMAT_ISL0
:
1999
0
    p = print_ast_expr_isl(p, expr);
2000
0
    break;
2001
2.26k
  
case 2.26k
ISL_FORMAT_C2.26k
:
2002
2.26k
    p = print_ast_expr_c(p, expr);
2003
2.26k
    break;
2004
0
  default:
2005
0
    isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2006
2.26k
      "output format not supported for ast_expr",
2007
2.26k
      return isl_printer_free(p));
2008
2.26k
  }
2009
2.26k
2010
2.26k
  return p;
2011
2.26k
}
2012
2013
static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2014
  __isl_keep isl_ast_node *node);
2015
2016
/* Print a YAML sequence containing the entries in "list" to "p".
2017
 */
2018
static __isl_give isl_printer *print_ast_node_list(__isl_take isl_printer *p,
2019
  __isl_keep isl_ast_node_list *list)
2020
0
{
2021
0
  int i, n;
2022
0
2023
0
  n = isl_ast_node_list_n_ast_node(list);
2024
0
  if (n < 0)
2025
0
    return isl_printer_free(p);
2026
0
2027
0
  p = isl_printer_yaml_start_sequence(p);
2028
0
  for (i = 0; 
i < n0
;
++i0
)
{0
2029
0
    isl_ast_node *node;
2030
0
2031
0
    node = isl_ast_node_list_get_ast_node(list, i);
2032
0
    p = print_ast_node_isl(p, node);
2033
0
    isl_ast_node_free(node);
2034
0
    p = isl_printer_yaml_next(p);
2035
0
  }
2036
0
  p = isl_printer_yaml_end_sequence(p);
2037
0
2038
0
  return p;
2039
0
}
2040
2041
/* Print "node" to "p" in "isl format".
2042
 *
2043
 * In particular, print the isl_ast_node as a YAML document.
2044
 */
2045
static __isl_give isl_printer *print_ast_node_isl(__isl_take isl_printer *p,
2046
  __isl_keep isl_ast_node *node)
2047
0
{
2048
0
  switch (node->type) {
2049
0
  case isl_ast_node_for:
2050
0
    p = isl_printer_yaml_start_mapping(p);
2051
0
    p = isl_printer_print_str(p, "iterator");
2052
0
    p = isl_printer_yaml_next(p);
2053
0
    p = isl_printer_print_ast_expr(p, node->u.f.iterator);
2054
0
    p = isl_printer_yaml_next(p);
2055
0
    if (
node->u.f.degenerate0
)
{0
2056
0
      p = isl_printer_print_str(p, "value");
2057
0
      p = isl_printer_yaml_next(p);
2058
0
      p = isl_printer_print_ast_expr(p, node->u.f.init);
2059
0
      p = isl_printer_yaml_next(p);
2060
0
    } else {
2061
0
      p = isl_printer_print_str(p, "init");
2062
0
      p = isl_printer_yaml_next(p);
2063
0
      p = isl_printer_print_ast_expr(p, node->u.f.init);
2064
0
      p = isl_printer_yaml_next(p);
2065
0
      p = isl_printer_print_str(p, "cond");
2066
0
      p = isl_printer_yaml_next(p);
2067
0
      p = isl_printer_print_ast_expr(p, node->u.f.cond);
2068
0
      p = isl_printer_yaml_next(p);
2069
0
      p = isl_printer_print_str(p, "inc");
2070
0
      p = isl_printer_yaml_next(p);
2071
0
      p = isl_printer_print_ast_expr(p, node->u.f.inc);
2072
0
      p = isl_printer_yaml_next(p);
2073
0
    }
2074
0
    if (
node->u.f.body0
)
{0
2075
0
      p = isl_printer_print_str(p, "body");
2076
0
      p = isl_printer_yaml_next(p);
2077
0
      p = isl_printer_print_ast_node(p, node->u.f.body);
2078
0
      p = isl_printer_yaml_next(p);
2079
0
    }
2080
0
    p = isl_printer_yaml_end_mapping(p);
2081
0
    break;
2082
0
  case isl_ast_node_mark:
2083
0
    p = isl_printer_yaml_start_mapping(p);
2084
0
    p = isl_printer_print_str(p, "mark");
2085
0
    p = isl_printer_yaml_next(p);
2086
0
    p = isl_printer_print_id(p, node->u.m.mark);
2087
0
    p = isl_printer_yaml_next(p);
2088
0
    p = isl_printer_print_str(p, "node");
2089
0
    p = isl_printer_yaml_next(p);
2090
0
    p = isl_printer_print_ast_node(p, node->u.m.node);
2091
0
    p = isl_printer_yaml_end_mapping(p);
2092
0
    break;
2093
0
  case isl_ast_node_user:
2094
0
    p = isl_printer_yaml_start_mapping(p);
2095
0
    p = isl_printer_print_str(p, "user");
2096
0
    p = isl_printer_yaml_next(p);
2097
0
    p = isl_printer_print_ast_expr(p, node->u.e.expr);
2098
0
    p = isl_printer_yaml_end_mapping(p);
2099
0
    break;
2100
0
  case isl_ast_node_if:
2101
0
    p = isl_printer_yaml_start_mapping(p);
2102
0
    p = isl_printer_print_str(p, "guard");
2103
0
    p = isl_printer_yaml_next(p);
2104
0
    p = isl_printer_print_ast_expr(p, node->u.i.guard);
2105
0
    p = isl_printer_yaml_next(p);
2106
0
    if (
node->u.i.then0
)
{0
2107
0
      p = isl_printer_print_str(p, "then");
2108
0
      p = isl_printer_yaml_next(p);
2109
0
      p = isl_printer_print_ast_node(p, node->u.i.then);
2110
0
      p = isl_printer_yaml_next(p);
2111
0
    }
2112
0
    if (
node->u.i.else_node0
)
{0
2113
0
      p = isl_printer_print_str(p, "else");
2114
0
      p = isl_printer_yaml_next(p);
2115
0
      p = isl_printer_print_ast_node(p, node->u.i.else_node);
2116
0
    }
2117
0
    p = isl_printer_yaml_end_mapping(p);
2118
0
    break;
2119
0
  case isl_ast_node_block:
2120
0
    p = print_ast_node_list(p, node->u.b.children);
2121
0
    break;
2122
0
  case isl_ast_node_error:
2123
0
    break;
2124
0
  }
2125
0
  return p;
2126
0
}
2127
2128
/* Do we need to print a block around the body "node" of a for or if node?
2129
 *
2130
 * If the node is a block, then we need to print a block.
2131
 * Also if the node is a degenerate for then we will print it as
2132
 * an assignment followed by the body of the for loop, so we need a block
2133
 * as well.
2134
 * If the node is an if node with an else, then we print a block
2135
 * to avoid spurious dangling else warnings emitted by some compilers.
2136
 * If the node is a mark, then in principle, we would have to check
2137
 * the child of the mark node.  However, even if the child would not
2138
 * require us to print a block, for readability it is probably best
2139
 * to print a block anyway.
2140
 * If the ast_always_print_block option has been set, then we print a block.
2141
 */
2142
static int need_block(__isl_keep isl_ast_node *node)
2143
412
{
2144
412
  isl_ctx *ctx;
2145
412
2146
412
  if (node->type == isl_ast_node_block)
2147
41
    return 1;
2148
371
  
if (371
node->type == isl_ast_node_for && 371
node->u.f.degenerate132
)
2149
0
    return 1;
2150
371
  
if (371
node->type == isl_ast_node_if && 371
node->u.i.else_node7
)
2151
5
    return 1;
2152
366
  
if (366
node->type == isl_ast_node_mark366
)
2153
46
    return 1;
2154
366
2155
320
  ctx = isl_ast_node_get_ctx(node);
2156
320
  return isl_options_get_ast_always_print_block(ctx);
2157
366
}
2158
2159
static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2160
  __isl_keep isl_ast_node *node,
2161
  __isl_keep isl_ast_print_options *options, int in_block, int in_list);
2162
static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2163
  __isl_keep isl_ast_node *node,
2164
  __isl_keep isl_ast_print_options *options, int new_line,
2165
  int force_block);
2166
2167
/* Print the body "node" of a for or if node.
2168
 * If "else_node" is set, then it is printed as well.
2169
 * If "force_block" is set, then print out the body as a block.
2170
 *
2171
 * We first check if we need to print out a block.
2172
 * We always print out a block if there is an else node to make
2173
 * sure that the else node is matched to the correct if node.
2174
 * For consistency, the corresponding else node is also printed as a block.
2175
 *
2176
 * If the else node is itself an if, then we print it as
2177
 *
2178
 *  } else if (..) {
2179
 *  }
2180
 *
2181
 * Otherwise the else node is printed as
2182
 *
2183
 *  } else {
2184
 *    node
2185
 *  }
2186
 */
2187
static __isl_give isl_printer *print_body_c(__isl_take isl_printer *p,
2188
  __isl_keep isl_ast_node *node, __isl_keep isl_ast_node *else_node,
2189
  __isl_keep isl_ast_print_options *options, int force_block)
2190
441
{
2191
441
  if (!node)
2192
0
    return isl_printer_free(p);
2193
441
2194
441
  
if (441
!force_block && 441
!else_node426
&&
!need_block(node)412
)
{320
2195
320
    p = isl_printer_end_line(p);
2196
320
    p = isl_printer_indent(p, 2);
2197
320
    p = isl_ast_node_print(node, p,
2198
320
          isl_ast_print_options_copy(options));
2199
320
    p = isl_printer_indent(p, -2);
2200
320
    return p;
2201
320
  }
2202
441
2203
121
  p = isl_printer_print_str(p, " {");
2204
121
  p = isl_printer_end_line(p);
2205
121
  p = isl_printer_indent(p, 2);
2206
121
  p = print_ast_node_c(p, node, options, 1, 0);
2207
121
  p = isl_printer_indent(p, -2);
2208
121
  p = isl_printer_start_line(p);
2209
121
  p = isl_printer_print_str(p, "}");
2210
121
  if (
else_node121
)
{15
2211
15
    if (
else_node->type == isl_ast_node_if15
)
{5
2212
5
      p = isl_printer_print_str(p, " else ");
2213
5
      p = print_if_c(p, else_node, options, 0, 1);
2214
10
    } else {
2215
10
      p = isl_printer_print_str(p, " else");
2216
10
      p = print_body_c(p, else_node, NULL, options, 1);
2217
10
    }
2218
15
  } else
2219
106
    p = isl_printer_end_line(p);
2220
121
2221
121
  return p;
2222
441
}
2223
2224
/* Print the start of a compound statement.
2225
 */
2226
static __isl_give isl_printer *start_block(__isl_take isl_printer *p)
2227
38
{
2228
38
  p = isl_printer_start_line(p);
2229
38
  p = isl_printer_print_str(p, "{");
2230
38
  p = isl_printer_end_line(p);
2231
38
  p = isl_printer_indent(p, 2);
2232
38
2233
38
  return p;
2234
38
}
2235
2236
/* Print the end of a compound statement.
2237
 */
2238
static __isl_give isl_printer *end_block(__isl_take isl_printer *p)
2239
38
{
2240
38
  p = isl_printer_indent(p, -2);
2241
38
  p = isl_printer_start_line(p);
2242
38
  p = isl_printer_print_str(p, "}");
2243
38
  p = isl_printer_end_line(p);
2244
38
2245
38
  return p;
2246
38
}
2247
2248
/* Print the for node "node".
2249
 *
2250
 * If the for node is degenerate, it is printed as
2251
 *
2252
 *  type iterator = init;
2253
 *  body
2254
 *
2255
 * Otherwise, it is printed as
2256
 *
2257
 *  for (type iterator = init; cond; iterator += inc)
2258
 *    body
2259
 *
2260
 * "in_block" is set if we are currently inside a block.
2261
 * "in_list" is set if the current node is not alone in the block.
2262
 * If we are not in a block or if the current not is not alone in the block
2263
 * then we print a block around a degenerate for loop such that the variable
2264
 * declaration will not conflict with any potential other declaration
2265
 * of the same variable.
2266
 */
2267
static __isl_give isl_printer *print_for_c(__isl_take isl_printer *p,
2268
  __isl_keep isl_ast_node *node,
2269
  __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2270
370
{
2271
370
  isl_id *id;
2272
370
  const char *name;
2273
370
  const char *type;
2274
370
2275
370
  type = isl_options_get_ast_iterator_type(isl_printer_get_ctx(p));
2276
370
  if (
!node->u.f.degenerate370
)
{370
2277
370
    id = isl_ast_expr_get_id(node->u.f.iterator);
2278
370
    name = isl_id_get_name(id);
2279
370
    isl_id_free(id);
2280
370
    p = isl_printer_start_line(p);
2281
370
    p = isl_printer_print_str(p, "for (");
2282
370
    p = isl_printer_print_str(p, type);
2283
370
    p = isl_printer_print_str(p, " ");
2284
370
    p = isl_printer_print_str(p, name);
2285
370
    p = isl_printer_print_str(p, " = ");
2286
370
    p = isl_printer_print_ast_expr(p, node->u.f.init);
2287
370
    p = isl_printer_print_str(p, "; ");
2288
370
    p = isl_printer_print_ast_expr(p, node->u.f.cond);
2289
370
    p = isl_printer_print_str(p, "; ");
2290
370
    p = isl_printer_print_str(p, name);
2291
370
    p = isl_printer_print_str(p, " += ");
2292
370
    p = isl_printer_print_ast_expr(p, node->u.f.inc);
2293
370
    p = isl_printer_print_str(p, ")");
2294
370
    p = print_body_c(p, node->u.f.body, NULL, options, 0);
2295
0
  } else {
2296
0
    id = isl_ast_expr_get_id(node->u.f.iterator);
2297
0
    name = isl_id_get_name(id);
2298
0
    isl_id_free(id);
2299
0
    if (
!in_block || 0
in_list0
)
2300
0
      p = start_block(p);
2301
0
    p = isl_printer_start_line(p);
2302
0
    p = isl_printer_print_str(p, type);
2303
0
    p = isl_printer_print_str(p, " ");
2304
0
    p = isl_printer_print_str(p, name);
2305
0
    p = isl_printer_print_str(p, " = ");
2306
0
    p = isl_printer_print_ast_expr(p, node->u.f.init);
2307
0
    p = isl_printer_print_str(p, ";");
2308
0
    p = isl_printer_end_line(p);
2309
0
    p = print_ast_node_c(p, node->u.f.body, options, 1, 0);
2310
0
    if (
!in_block || 0
in_list0
)
2311
0
      p = end_block(p);
2312
0
  }
2313
370
2314
370
  return p;
2315
370
}
2316
2317
/* Print the if node "node".
2318
 * If "new_line" is set then the if node should be printed on a new line.
2319
 * If "force_block" is set, then print out the body as a block.
2320
 */
2321
static __isl_give isl_printer *print_if_c(__isl_take isl_printer *p,
2322
  __isl_keep isl_ast_node *node,
2323
  __isl_keep isl_ast_print_options *options, int new_line,
2324
  int force_block)
2325
61
{
2326
61
  if (new_line)
2327
56
    p = isl_printer_start_line(p);
2328
61
  p = isl_printer_print_str(p, "if (");
2329
61
  p = isl_printer_print_ast_expr(p, node->u.i.guard);
2330
61
  p = isl_printer_print_str(p, ")");
2331
61
  p = print_body_c(p, node->u.i.then, node->u.i.else_node, options,
2332
61
      force_block);
2333
61
2334
61
  return p;
2335
61
}
2336
2337
/* Print the "node" to "p".
2338
 *
2339
 * "in_block" is set if we are currently inside a block.
2340
 * If so, we do not print a block around the children of a block node.
2341
 * We do this to avoid an extra block around the body of a degenerate
2342
 * for node.
2343
 *
2344
 * "in_list" is set if the current node is not alone in the block.
2345
 */
2346
static __isl_give isl_printer *print_ast_node_c(__isl_take isl_printer *p,
2347
  __isl_keep isl_ast_node *node,
2348
  __isl_keep isl_ast_print_options *options, int in_block, int in_list)
2349
1.52k
{
2350
1.52k
  switch (node->type) {
2351
370
  case isl_ast_node_for:
2352
370
    if (options->print_for)
2353
370
      return options->print_for(p,
2354
370
          isl_ast_print_options_copy(options),
2355
370
          node, options->print_for_user);
2356
0
    p = print_for_c(p, node, options, in_block, in_list);
2357
0
    break;
2358
56
  case isl_ast_node_if:
2359
56
    p = print_if_c(p, node, options, 1, 0);
2360
56
    break;
2361
122
  case isl_ast_node_block:
2362
122
    if (!in_block)
2363
38
      p = start_block(p);
2364
122
    p = isl_ast_node_list_print(node->u.b.children, p, options);
2365
122
    if (!in_block)
2366
38
      p = end_block(p);
2367
122
    break;
2368
102
  case isl_ast_node_mark:
2369
102
    p = isl_printer_start_line(p);
2370
102
    p = isl_printer_print_str(p, "// ");
2371
102
    p = isl_printer_print_str(p, isl_id_get_name(node->u.m.mark));
2372
102
    p = isl_printer_end_line(p);
2373
102
    p = print_ast_node_c(p, node->u.m.node, options, 0, in_list);
2374
102
    break;
2375
877
  case isl_ast_node_user:
2376
877
    if (options->print_user)
2377
0
      return options->print_user(p,
2378
0
          isl_ast_print_options_copy(options),
2379
0
          node, options->print_user_user);
2380
877
    p = isl_printer_start_line(p);
2381
877
    p = isl_printer_print_ast_expr(p, node->u.e.expr);
2382
877
    p = isl_printer_print_str(p, ";");
2383
877
    p = isl_printer_end_line(p);
2384
877
    break;
2385
0
  case isl_ast_node_error:
2386
0
    break;
2387
1.52k
  }
2388
1.15k
  return p;
2389
1.52k
}
2390
2391
/* Print the for node "node" to "p".
2392
 */
2393
__isl_give isl_printer *isl_ast_node_for_print(__isl_keep isl_ast_node *node,
2394
  __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2395
370
{
2396
370
  if (
!node || 370
!options370
)
2397
0
    goto error;
2398
370
  
if (370
node->type != isl_ast_node_for370
)
2399
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2400
370
      "not a for node", goto error);
2401
370
  p = print_for_c(p, node, options, 0, 0);
2402
370
  isl_ast_print_options_free(options);
2403
370
  return p;
2404
0
error:
2405
0
  isl_ast_print_options_free(options);
2406
0
  isl_printer_free(p);
2407
0
  return NULL;
2408
370
}
2409
2410
/* Print the if node "node" to "p".
2411
 */
2412
__isl_give isl_printer *isl_ast_node_if_print(__isl_keep isl_ast_node *node,
2413
  __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2414
0
{
2415
0
  if (
!node || 0
!options0
)
2416
0
    goto error;
2417
0
  
if (0
node->type != isl_ast_node_if0
)
2418
0
    isl_die(isl_ast_node_get_ctx(node), isl_error_invalid,
2419
0
      "not an if node", goto error);
2420
0
  p = print_if_c(p, node, options, 1, 0);
2421
0
  isl_ast_print_options_free(options);
2422
0
  return p;
2423
0
error:
2424
0
  isl_ast_print_options_free(options);
2425
0
  isl_printer_free(p);
2426
0
  return NULL;
2427
0
}
2428
2429
/* Print "node" to "p".
2430
 */
2431
__isl_give isl_printer *isl_ast_node_print(__isl_keep isl_ast_node *node,
2432
  __isl_take isl_printer *p, __isl_take isl_ast_print_options *options)
2433
469
{
2434
469
  if (
!options || 469
!node469
)
2435
0
    goto error;
2436
469
  p = print_ast_node_c(p, node, options, 0, 0);
2437
469
  isl_ast_print_options_free(options);
2438
469
  return p;
2439
0
error:
2440
0
  isl_ast_print_options_free(options);
2441
0
  isl_printer_free(p);
2442
0
  return NULL;
2443
469
}
2444
2445
/* Print "node" to "p".
2446
 */
2447
__isl_give isl_printer *isl_printer_print_ast_node(__isl_take isl_printer *p,
2448
  __isl_keep isl_ast_node *node)
2449
0
{
2450
0
  int format;
2451
0
  isl_ast_print_options *options;
2452
0
2453
0
  if (!p)
2454
0
    return NULL;
2455
0
2456
0
  format = isl_printer_get_output_format(p);
2457
0
  switch (format) {
2458
0
  
case 0
ISL_FORMAT_ISL0
:
2459
0
    p = print_ast_node_isl(p, node);
2460
0
    break;
2461
0
  
case 0
ISL_FORMAT_C0
:
2462
0
    options = isl_ast_print_options_alloc(isl_printer_get_ctx(p));
2463
0
    p = isl_ast_node_print(node, p, options);
2464
0
    break;
2465
0
  default:
2466
0
    isl_die(isl_printer_get_ctx(p), isl_error_unsupported,
2467
0
      "output format not supported for ast_node",
2468
0
      return isl_printer_free(p));
2469
0
  }
2470
0
2471
0
  return p;
2472
0
}
2473
2474
/* Print the list of nodes "list" to "p".
2475
 */
2476
__isl_give isl_printer *isl_ast_node_list_print(
2477
  __isl_keep isl_ast_node_list *list, __isl_take isl_printer *p,
2478
  __isl_keep isl_ast_print_options *options)
2479
122
{
2480
122
  int i;
2481
122
2482
122
  if (
!p || 122
!list122
||
!options122
)
2483
0
    return isl_printer_free(p);
2484
122
2485
957
  
for (i = 0; 122
i < list->n957
;
++i835
)
2486
835
    p = print_ast_node_c(p, list->p[i], options, 1, 1);
2487
122
2488
122
  return p;
2489
122
}
2490
2491
0
#define ISL_AST_MACRO_FLOORD  (1 << 0)
2492
0
#define ISL_AST_MACRO_MIN (1 << 1)
2493
0
#define ISL_AST_MACRO_MAX (1 << 2)
2494
0
#define ISL_AST_MACRO_ALL 
(0
ISL_AST_MACRO_FLOORD0
| \
2495
0
         ISL_AST_MACRO_MIN | \
2496
0
         ISL_AST_MACRO_MAX)
2497
2498
/* If "expr" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2499
 * then set the corresponding bit in "macros".
2500
 */
2501
static int ast_expr_required_macros(__isl_keep isl_ast_expr *expr, int macros)
2502
0
{
2503
0
  int i;
2504
0
2505
0
  if (
macros == 0
ISL_AST_MACRO_ALL0
)
2506
0
    return macros;
2507
0
2508
0
  
if (0
expr->type != isl_ast_expr_op0
)
2509
0
    return macros;
2510
0
2511
0
  
if (0
expr->u.op.op == isl_ast_op_min0
)
2512
0
    
macros |= 0
ISL_AST_MACRO_MIN0
;
2513
0
  if (expr->u.op.op == isl_ast_op_max)
2514
0
    
macros |= 0
ISL_AST_MACRO_MAX0
;
2515
0
  if (expr->u.op.op == isl_ast_op_fdiv_q)
2516
0
    
macros |= 0
ISL_AST_MACRO_FLOORD0
;
2517
0
2518
0
  for (i = 0; 
i < expr->u.op.n_arg0
;
++i0
)
2519
0
    macros = ast_expr_required_macros(expr->u.op.args[i], macros);
2520
0
2521
0
  return macros;
2522
0
}
2523
2524
static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2525
  int macros);
2526
2527
/* If "node" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2528
 * then set the corresponding bit in "macros".
2529
 */
2530
static int ast_node_required_macros(__isl_keep isl_ast_node *node, int macros)
2531
0
{
2532
0
  if (
macros == 0
ISL_AST_MACRO_ALL0
)
2533
0
    return macros;
2534
0
2535
0
  switch (node->type) {
2536
0
  case isl_ast_node_for:
2537
0
    macros = ast_expr_required_macros(node->u.f.init, macros);
2538
0
    if (
!node->u.f.degenerate0
)
{0
2539
0
      macros = ast_expr_required_macros(node->u.f.cond,
2540
0
                macros);
2541
0
      macros = ast_expr_required_macros(node->u.f.inc,
2542
0
                macros);
2543
0
    }
2544
0
    macros = ast_node_required_macros(node->u.f.body, macros);
2545
0
    break;
2546
0
  case isl_ast_node_if:
2547
0
    macros = ast_expr_required_macros(node->u.i.guard, macros);
2548
0
    macros = ast_node_required_macros(node->u.i.then, macros);
2549
0
    if (node->u.i.else_node)
2550
0
      macros = ast_node_required_macros(node->u.i.else_node,
2551
0
                macros);
2552
0
    break;
2553
0
  case isl_ast_node_block:
2554
0
    macros = ast_node_list_required_macros(node->u.b.children,
2555
0
              macros);
2556
0
    break;
2557
0
  case isl_ast_node_mark:
2558
0
    macros = ast_node_required_macros(node->u.m.node, macros);
2559
0
    break;
2560
0
  case isl_ast_node_user:
2561
0
    macros = ast_expr_required_macros(node->u.e.expr, macros);
2562
0
    break;
2563
0
  case isl_ast_node_error:
2564
0
    break;
2565
0
  }
2566
0
2567
0
  return macros;
2568
0
}
2569
2570
/* If "list" contains an isl_ast_op_min, isl_ast_op_max or isl_ast_op_fdiv_q
2571
 * then set the corresponding bit in "macros".
2572
 */
2573
static int ast_node_list_required_macros(__isl_keep isl_ast_node_list *list,
2574
  int macros)
2575
0
{
2576
0
  int i;
2577
0
2578
0
  for (i = 0; 
i < list->n0
;
++i0
)
2579
0
    macros = ast_node_required_macros(list->p[i], macros);
2580
0
2581
0
  return macros;
2582
0
}
2583
2584
/* Data structure for keeping track of whether a macro definition
2585
 * for a given type has already been printed.
2586
 * The value is zero if no definition has been printed and non-zero otherwise.
2587
 */
2588
struct isl_ast_op_printed {
2589
  char printed[isl_ast_op_last + 1];
2590
};
2591
2592
/* Create an empty struct isl_ast_op_printed.
2593
 */
2594
static void *create_printed(isl_ctx *ctx)
2595
0
{
2596
0
  return isl_calloc_type(ctx, struct isl_ast_op_printed);
2597
0
}
2598
2599
/* Free a struct isl_ast_op_printed.
2600
 */
2601
static void free_printed(void *user)
2602
0
{
2603
0
  free(user);
2604
0
}
2605
2606
/* Ensure that "p" has an isl_ast_op_printed note identified by "id".
2607
 */
2608
static __isl_give isl_printer *alloc_printed(__isl_take isl_printer *p,
2609
  __isl_keep isl_id *id)
2610
0
{
2611
0
  return alloc_note(p, id, &create_printed, &free_printed);
2612
0
}
2613
2614
/* Create an identifier that is used to store
2615
 * an isl_ast_op_printed note.
2616
 */
2617
static __isl_give isl_id *printed_id(isl_ctx *ctx)
2618
0
{
2619
0
  return isl_id_alloc(ctx, "isl_ast_op_type_printed", NULL);
2620
0
}
2621
2622
/* Did the user specify that a macro definition should only be
2623
 * printed once and has a macro definition for "type" already
2624
 * been printed to "p"?
2625
 * If definitions should only be printed once, but a definition
2626
 * for "p" has not yet been printed, then mark it as having been
2627
 * printed so that it will not printed again.
2628
 * The actual printing is taken care of by the caller.
2629
 */
2630
static isl_bool already_printed_once(__isl_keep isl_printer *p,
2631
  enum isl_ast_op_type type)
2632
0
{
2633
0
  isl_ctx *ctx;
2634
0
  isl_id *id;
2635
0
  struct isl_ast_op_printed *printed;
2636
0
2637
0
  if (!p)
2638
0
    return isl_bool_error;
2639
0
2640
0
  ctx = isl_printer_get_ctx(p);
2641
0
  if (!isl_options_get_ast_print_macro_once(ctx))
2642
0
    return isl_bool_false;
2643
0
2644
0
  
if (0
type > 0
isl_ast_op_last0
)
2645
0
    isl_die(isl_printer_get_ctx(p), isl_error_invalid,
2646
0
      "invalid type", return isl_bool_error);
2647
0
2648
0
  id = printed_id(isl_printer_get_ctx(p));
2649
0
  p = alloc_printed(p, id);
2650
0
  printed = get_note(p, id);
2651
0
  isl_id_free(id);
2652
0
  if (!printed)
2653
0
    return isl_bool_error;
2654
0
2655
0
  
if (0
printed->printed[type]0
)
2656
0
    return isl_bool_true;
2657
0
2658
0
  printed->printed[type] = 1;
2659
0
  return isl_bool_false;
2660
0
}
2661
2662
/* Print a macro definition for the operator "type".
2663
 *
2664
 * If the user has specified that a macro definition should
2665
 * only be printed once to any given printer and if the macro definition
2666
 * has already been printed to "p", then do not print the definition.
2667
 */
2668
__isl_give isl_printer *isl_ast_op_type_print_macro(
2669
  enum isl_ast_op_type type, __isl_take isl_printer *p)
2670
0
{
2671
0
  isl_bool skip;
2672
0
2673
0
  skip = already_printed_once(p, type);
2674
0
  if (skip < 0)
2675
0
    return isl_printer_free(p);
2676
0
  
if (0
skip0
)
2677
0
    return p;
2678
0
2679
0
  switch (type) {
2680
0
  case isl_ast_op_min:
2681
0
    p = isl_printer_start_line(p);
2682
0
    p = isl_printer_print_str(p, "#define ");
2683
0
    p = isl_printer_print_str(p, get_op_str_c(p, type));
2684
0
    p = isl_printer_print_str(p,
2685
0
      "(x,y)    ((x) < (y) ? (x) : (y))");
2686
0
    p = isl_printer_end_line(p);
2687
0
    break;
2688
0
  case isl_ast_op_max:
2689
0
    p = isl_printer_start_line(p);
2690
0
    p = isl_printer_print_str(p, "#define ");
2691
0
    p = isl_printer_print_str(p, get_op_str_c(p, type));
2692
0
    p = isl_printer_print_str(p,
2693
0
      "(x,y)    ((x) > (y) ? (x) : (y))");
2694
0
    p = isl_printer_end_line(p);
2695
0
    break;
2696
0
  case isl_ast_op_fdiv_q:
2697
0
    p = isl_printer_start_line(p);
2698
0
    p = isl_printer_print_str(p, "#define ");
2699
0
    p = isl_printer_print_str(p, get_op_str_c(p, type));
2700
0
    p = isl_printer_print_str(p,
2701
0
      "(n,d) "
2702
0
      "(((n)<0) ? -((-(n)+(d)-1)/(d)) : (n)/(d))");
2703
0
    p = isl_printer_end_line(p);
2704
0
    break;
2705
0
  default:
2706
0
    break;
2707
0
  }
2708
0
2709
0
  return p;
2710
0
}
2711
2712
/* Call "fn" for each type of operation represented in the "macros"
2713
 * bit vector.
2714
 */
2715
static isl_stat foreach_ast_op_type(int macros,
2716
  isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2717
0
{
2718
0
  if (
macros & 0
ISL_AST_MACRO_MIN0
&&
fn(isl_ast_op_min, user) < 00
)
2719
0
    return isl_stat_error;
2720
0
  
if (0
macros & 0
ISL_AST_MACRO_MAX0
&&
fn(isl_ast_op_max, user) < 00
)
2721
0
    return isl_stat_error;
2722
0
  
if (0
macros & 0
ISL_AST_MACRO_FLOORD0
&&
fn(isl_ast_op_fdiv_q, user) < 00
)
2723
0
    return isl_stat_error;
2724
0
2725
0
  return isl_stat_ok;
2726
0
}
2727
2728
/* Call "fn" for each type of operation that appears in "expr"
2729
 * and that requires a macro definition.
2730
 */
2731
isl_stat isl_ast_expr_foreach_ast_op_type(__isl_keep isl_ast_expr *expr,
2732
  isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2733
0
{
2734
0
  int macros;
2735
0
2736
0
  if (!expr)
2737
0
    return isl_stat_error;
2738
0
2739
0
  macros = ast_expr_required_macros(expr, 0);
2740
0
  return foreach_ast_op_type(macros, fn, user);
2741
0
}
2742
2743
/* Call "fn" for each type of operation that appears in "node"
2744
 * and that requires a macro definition.
2745
 */
2746
isl_stat isl_ast_node_foreach_ast_op_type(__isl_keep isl_ast_node *node,
2747
  isl_stat (*fn)(enum isl_ast_op_type type, void *user), void *user)
2748
0
{
2749
0
  int macros;
2750
0
2751
0
  if (!node)
2752
0
    return isl_stat_error;
2753
0
2754
0
  macros = ast_node_required_macros(node, 0);
2755
0
  return foreach_ast_op_type(macros, fn, user);
2756
0
}
2757
2758
static isl_stat ast_op_type_print_macro(enum isl_ast_op_type type, void *user)
2759
0
{
2760
0
  isl_printer **p = user;
2761
0
2762
0
  *p = isl_ast_op_type_print_macro(type, *p);
2763
0
2764
0
  return isl_stat_ok;
2765
0
}
2766
2767
/* Print macro definitions for all the macros used in the result
2768
 * of printing "expr".
2769
 */
2770
__isl_give isl_printer *isl_ast_expr_print_macros(
2771
  __isl_keep isl_ast_expr *expr, __isl_take isl_printer *p)
2772
0
{
2773
0
  if (isl_ast_expr_foreach_ast_op_type(expr,
2774
0
              &ast_op_type_print_macro, &p) < 0)
2775
0
    return isl_printer_free(p);
2776
0
  return p;
2777
0
}
2778
2779
/* Print macro definitions for all the macros used in the result
2780
 * of printing "node".
2781
 */
2782
__isl_give isl_printer *isl_ast_node_print_macros(
2783
  __isl_keep isl_ast_node *node, __isl_take isl_printer *p)
2784
0
{
2785
0
  if (isl_ast_node_foreach_ast_op_type(node,
2786
0
              &ast_op_type_print_macro, &p) < 0)
2787
0
    return isl_printer_free(p);
2788
0
  return p;
2789
0
}
2790
2791
/* Return a string containing C code representing this isl_ast_expr.
2792
 */
2793
__isl_give char *isl_ast_expr_to_C_str(__isl_keep isl_ast_expr *expr)
2794
2
{
2795
2
  isl_printer *p;
2796
2
  char *str;
2797
2
2798
2
  if (!expr)
2799
0
    return NULL;
2800
2
2801
2
  p = isl_printer_to_str(isl_ast_expr_get_ctx(expr));
2802
2
  p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2803
2
  p = isl_printer_print_ast_expr(p, expr);
2804
2
2805
2
  str = isl_printer_get_str(p);
2806
2
2807
2
  isl_printer_free(p);
2808
2
2809
2
  return str;
2810
2
}
2811
2812
/* Return a string containing C code representing this isl_ast_node.
2813
 */
2814
__isl_give char *isl_ast_node_to_C_str(__isl_keep isl_ast_node *node)
2815
0
{
2816
0
  isl_printer *p;
2817
0
  char *str;
2818
0
2819
0
  if (!node)
2820
0
    return NULL;
2821
0
2822
0
  p = isl_printer_to_str(isl_ast_node_get_ctx(node));
2823
0
  p = isl_printer_set_output_format(p, ISL_FORMAT_C);
2824
0
  p = isl_printer_print_ast_node(p, node);
2825
0
2826
0
  str = isl_printer_get_str(p);
2827
0
2828
0
  isl_printer_free(p);
2829
0
2830
0
  return str;
2831
0
}