-
Notifications
You must be signed in to change notification settings - Fork 0
/
isl_ast.c
3940 lines (3440 loc) · 101 KB
/
isl_ast.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012-2013 Ecole Normale Superieure
* Copyright 2022 Cerebras Systems
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Cerebras Systems, 1237 E Arques Ave, Sunnyvale, CA, USA
*/
#include <string.h>
#include <isl/id.h>
#include <isl/stream.h>
#include <isl/val.h>
#include <isl_ast_private.h>
#undef EL_BASE
#define EL_BASE ast_expr
#include <isl_list_templ.c>
#undef EL_BASE
#define EL_BASE ast_node
#include <isl_list_templ.c>
isl_ctx *isl_ast_print_options_get_ctx(
__isl_keep isl_ast_print_options *options)
{
return options ? options->ctx : NULL;
}
__isl_give isl_ast_print_options *isl_ast_print_options_alloc(isl_ctx *ctx)
{
isl_ast_print_options *options;
options = isl_calloc_type(ctx, isl_ast_print_options);
if (!options)
return NULL;
options->ctx = ctx;
isl_ctx_ref(ctx);
options->ref = 1;
return options;
}
__isl_give isl_ast_print_options *isl_ast_print_options_dup(
__isl_keep isl_ast_print_options *options)
{
isl_ctx *ctx;
isl_ast_print_options *dup;
if (!options)
return NULL;
ctx = isl_ast_print_options_get_ctx(options);
dup = isl_ast_print_options_alloc(ctx);
if (!dup)
return NULL;
dup->print_for = options->print_for;
dup->print_for_user = options->print_for_user;
dup->print_user = options->print_user;
dup->print_user_user = options->print_user_user;
return dup;
}
__isl_give isl_ast_print_options *isl_ast_print_options_cow(
__isl_take isl_ast_print_options *options)
{
if (!options)
return NULL;
if (options->ref == 1)
return options;
options->ref--;
return isl_ast_print_options_dup(options);
}
__isl_give isl_ast_print_options *isl_ast_print_options_copy(
__isl_keep isl_ast_print_options *options)
{
if (!options)
return NULL;
options->ref++;
return options;
}
__isl_null isl_ast_print_options *isl_ast_print_options_free(
__isl_take isl_ast_print_options *options)
{
if (!options)
return NULL;
if (--options->ref > 0)
return NULL;
isl_ctx_deref(options->ctx);
free(options);
return NULL;
}
/* Set the print_user callback of "options" to "print_user".
*
* If this callback is set, then it is used to print user nodes in the AST.
* Otherwise, the expression associated to the user node is printed.
*/
__isl_give isl_ast_print_options *isl_ast_print_options_set_print_user(
__isl_take isl_ast_print_options *options,
__isl_give isl_printer *(*print_user)(__isl_take isl_printer *p,
__isl_take isl_ast_print_options *options,
__isl_keep isl_ast_node *node, void *user),
void *user)
{
options = isl_ast_print_options_cow(options);
if (!options)
return NULL;
options->print_user = print_user;
options->print_user_user = user;
return options;
}
/* Set the print_for callback of "options" to "print_for".
*
* If this callback is set, then it used to print for nodes in the AST.
*/
__isl_give isl_ast_print_options *isl_ast_print_options_set_print_for(
__isl_take isl_ast_print_options *options,
__isl_give isl_printer *(*print_for)(__isl_take isl_printer *p,
__isl_take isl_ast_print_options *options,
__isl_keep isl_ast_node *node, void *user),
void *user)
{
options = isl_ast_print_options_cow(options);
if (!options)
return NULL;
options->print_for = print_for;
options->print_for_user = user;
return options;
}
/* Create a new operation expression of operation type "op",
* with arguments "args".
*/
static __isl_give isl_ast_expr *alloc_op(enum isl_ast_expr_op_type op,
__isl_take isl_ast_expr_list *args)
{
isl_ctx *ctx;
isl_ast_expr *expr;
if (!args)
return NULL;
ctx = isl_ast_expr_list_get_ctx(args);
expr = isl_calloc_type(ctx, isl_ast_expr);
if (!expr)
goto error;
expr->ctx = ctx;
isl_ctx_ref(ctx);
expr->ref = 1;
expr->type = isl_ast_expr_op;
expr->u.op.op = op;
expr->u.op.args = args;
return expr;
error:
isl_ast_expr_list_free(args);
return NULL;
}
/* Create a new operation expression of operation type "op",
* which will end up having "n_arg" arguments.
* The caller still needs to add those arguments.
*/
__isl_give isl_ast_expr *isl_ast_expr_alloc_op(isl_ctx *ctx,
enum isl_ast_expr_op_type op, int n_arg)
{
isl_ast_expr_list *args;
args = isl_ast_expr_list_alloc(ctx, n_arg);
return alloc_op(op, args);
}
__isl_give isl_ast_expr *isl_ast_expr_copy(__isl_keep isl_ast_expr *expr)
{
if (!expr)
return NULL;
expr->ref++;
return expr;
}
__isl_give isl_ast_expr *isl_ast_expr_dup(__isl_keep isl_ast_expr *expr)
{
isl_ast_expr *dup;
if (!expr)
return NULL;
switch (expr->type) {
case isl_ast_expr_int:
dup = isl_ast_expr_from_val(isl_val_copy(expr->u.v));
break;
case isl_ast_expr_id:
dup = isl_ast_expr_from_id(isl_id_copy(expr->u.id));
break;
case isl_ast_expr_op:
dup = alloc_op(expr->u.op.op,
isl_ast_expr_list_copy(expr->u.op.args));
break;
case isl_ast_expr_error:
dup = NULL;
}
if (!dup)
return NULL;
return dup;
}
__isl_give isl_ast_expr *isl_ast_expr_cow(__isl_take isl_ast_expr *expr)
{
if (!expr)
return NULL;
if (expr->ref == 1)
return expr;
expr->ref--;
return isl_ast_expr_dup(expr);
}
__isl_null isl_ast_expr *isl_ast_expr_free(__isl_take isl_ast_expr *expr)
{
if (!expr)
return NULL;
if (--expr->ref > 0)
return NULL;
isl_ctx_deref(expr->ctx);
switch (expr->type) {
case isl_ast_expr_int:
isl_val_free(expr->u.v);
break;
case isl_ast_expr_id:
isl_id_free(expr->u.id);
break;
case isl_ast_expr_op:
isl_ast_expr_list_free(expr->u.op.args);
break;
case isl_ast_expr_error:
break;
}
free(expr);
return NULL;
}
isl_ctx *isl_ast_expr_get_ctx(__isl_keep isl_ast_expr *expr)
{
return expr ? expr->ctx : NULL;
}
enum isl_ast_expr_type isl_ast_expr_get_type(__isl_keep isl_ast_expr *expr)
{
return expr ? expr->type : isl_ast_expr_error;
}
/* Return the integer value represented by "expr".
*/
__isl_give isl_val *isl_ast_expr_int_get_val(__isl_keep isl_ast_expr *expr)
{
if (!expr)
return NULL;
if (expr->type != isl_ast_expr_int)
isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
"expression not an int", return NULL);
return isl_val_copy(expr->u.v);
}
/* This is an alternative name for the function above.
*/
__isl_give isl_val *isl_ast_expr_get_val(__isl_keep isl_ast_expr *expr)
{
return isl_ast_expr_int_get_val(expr);
}
__isl_give isl_id *isl_ast_expr_id_get_id(__isl_keep isl_ast_expr *expr)
{
if (!expr)
return NULL;
if (expr->type != isl_ast_expr_id)
isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
"expression not an identifier", return NULL);
return isl_id_copy(expr->u.id);
}
/* This is an alternative name for the function above.
*/
__isl_give isl_id *isl_ast_expr_get_id(__isl_keep isl_ast_expr *expr)
{
return isl_ast_expr_id_get_id(expr);
}
/* Check that "expr" is of type isl_ast_expr_op.
*/
static isl_stat isl_ast_expr_check_op(__isl_keep isl_ast_expr *expr)
{
if (!expr)
return isl_stat_error;
if (expr->type != isl_ast_expr_op)
isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
"expression not an operation", return isl_stat_error);
return isl_stat_ok;
}
/* Return the type of operation represented by "expr".
*/
enum isl_ast_expr_op_type isl_ast_expr_op_get_type(
__isl_keep isl_ast_expr *expr)
{
if (isl_ast_expr_check_op(expr) < 0)
return isl_ast_expr_op_error;
return expr->u.op.op;
}
/* This is an alternative name for the function above.
*/
enum isl_ast_expr_op_type isl_ast_expr_get_op_type(
__isl_keep isl_ast_expr *expr)
{
return isl_ast_expr_op_get_type(expr);
}
/* Return the number of arguments of the operation represented by "expr".
*/
isl_size isl_ast_expr_op_get_n_arg(__isl_keep isl_ast_expr *expr)
{
if (isl_ast_expr_check_op(expr) < 0)
return isl_size_error;
return isl_ast_expr_list_size(expr->u.op.args);
}
/* This is an alternative name for the function above.
*/
isl_size isl_ast_expr_get_op_n_arg(__isl_keep isl_ast_expr *expr)
{
return isl_ast_expr_op_get_n_arg(expr);
}
/* Return the argument at position "pos" of the operation represented by "expr".
*/
__isl_give isl_ast_expr *isl_ast_expr_op_get_arg(__isl_keep isl_ast_expr *expr,
int pos)
{
if (isl_ast_expr_check_op(expr) < 0)
return NULL;
return isl_ast_expr_list_get_at(expr->u.op.args, pos);
}
/* This is an alternative name for the function above.
*/
__isl_give isl_ast_expr *isl_ast_expr_get_op_arg(__isl_keep isl_ast_expr *expr,
int pos)
{
return isl_ast_expr_op_get_arg(expr, pos);
}
/* Return a copy of the arguments of the operation represented by "expr".
*/
static __isl_give isl_ast_expr_list *isl_ast_expr_op_get_args(
__isl_keep isl_ast_expr *expr)
{
if (isl_ast_expr_check_op(expr) < 0)
return NULL;
return isl_ast_expr_list_copy(expr->u.op.args);
}
/* Return the arguments of the operation expression "expr".
* This may be either a copy or the arguments themselves
* if there is only one reference to "expr".
* This allows the arguments to be modified inplace
* if both "expr" and its arguments have only a single reference.
* The caller is not allowed to modify "expr" between this call and
* the subsequent call to isl_ast_expr_op_restore_args.
* The only exception is that isl_ast_expr_free can be called instead.
*/
static __isl_give isl_ast_expr_list *isl_ast_expr_op_take_args(
__isl_keep isl_ast_expr *expr)
{
isl_ast_expr_list *args;
if (isl_ast_expr_check_op(expr) < 0)
return NULL;
if (expr->ref != 1)
return isl_ast_expr_op_get_args(expr);
args = expr->u.op.args;
expr->u.op.args = NULL;
return args;
}
/* Set the arguments of the operation expression "expr" to "args",
* where the arguments of "args" may be missing
* due to a preceding call to isl_ast_expr_op_take_args.
* However, in this case, "expr" only has a single reference and
* then the call to isl_ast_expr_cow has no effect.
*/
static __isl_give isl_ast_expr *isl_ast_expr_op_restore_args(
__isl_take isl_ast_expr *expr, __isl_take isl_ast_expr_list *args)
{
if (isl_ast_expr_check_op(expr) < 0 || !args)
goto error;
if (expr->u.op.args == args) {
isl_ast_expr_list_free(args);
return expr;
}
expr = isl_ast_expr_cow(expr);
if (!expr)
goto error;
isl_ast_expr_list_free(expr->u.op.args);
expr->u.op.args = args;
return expr;
error:
isl_ast_expr_free(expr);
isl_ast_expr_list_free(args);
return NULL;
}
/* Add "arg" to the arguments of the operation expression "expr".
*/
__isl_give isl_ast_expr *isl_ast_expr_op_add_arg(__isl_take isl_ast_expr *expr,
__isl_take isl_ast_expr *arg)
{
isl_ast_expr_list *args;
args = isl_ast_expr_op_take_args(expr);
args = isl_ast_expr_list_add(args, arg);
expr = isl_ast_expr_op_restore_args(expr, args);
return expr;
}
/* Replace the argument at position "pos" of "expr" by "arg".
*/
__isl_give isl_ast_expr *isl_ast_expr_set_op_arg(__isl_take isl_ast_expr *expr,
int pos, __isl_take isl_ast_expr *arg)
{
isl_ast_expr_list *args;
args = isl_ast_expr_op_take_args(expr);
args = isl_ast_expr_list_set_at(args, pos, arg);
expr = isl_ast_expr_op_restore_args(expr, args);
return expr;
}
/* Are the lists of AST expressions "list1" and "list2" the same?
*/
static isl_bool isl_ast_expr_list_is_equal(__isl_keep isl_ast_expr_list *list1,
__isl_keep isl_ast_expr_list *list2)
{
int i;
isl_size n1, n2;
if (!list1 || !list2)
return isl_bool_error;
if (list1 == list2)
return isl_bool_true;
n1 = isl_ast_expr_list_size(list1);
n2 = isl_ast_expr_list_size(list2);
if (n1 < 0 || n2 < 0)
return isl_bool_error;
if (n1 != n2)
return isl_bool_false;
for (i = 0; i < n1; ++i) {
isl_ast_expr *expr1, *expr2;
isl_bool equal;
expr1 = isl_ast_expr_list_get_at(list1, i);
expr2 = isl_ast_expr_list_get_at(list2, i);
equal = isl_ast_expr_is_equal(expr1, expr2);
isl_ast_expr_free(expr1);
isl_ast_expr_free(expr2);
if (equal < 0 || !equal)
return equal;
}
return isl_bool_true;
}
/* Is "expr1" equal to "expr2"?
*/
isl_bool isl_ast_expr_is_equal(__isl_keep isl_ast_expr *expr1,
__isl_keep isl_ast_expr *expr2)
{
if (!expr1 || !expr2)
return isl_bool_error;
if (expr1 == expr2)
return isl_bool_true;
if (expr1->type != expr2->type)
return isl_bool_false;
switch (expr1->type) {
case isl_ast_expr_int:
return isl_val_eq(expr1->u.v, expr2->u.v);
case isl_ast_expr_id:
return isl_bool_ok(expr1->u.id == expr2->u.id);
case isl_ast_expr_op:
if (expr1->u.op.op != expr2->u.op.op)
return isl_bool_false;
return isl_ast_expr_list_is_equal(expr1->u.op.args,
expr2->u.op.args);
case isl_ast_expr_error:
return isl_bool_error;
}
isl_die(isl_ast_expr_get_ctx(expr1), isl_error_internal,
"unhandled case", return isl_bool_error);
}
/* Create a new id expression representing "id".
*/
__isl_give isl_ast_expr *isl_ast_expr_from_id(__isl_take isl_id *id)
{
isl_ctx *ctx;
isl_ast_expr *expr;
if (!id)
return NULL;
ctx = isl_id_get_ctx(id);
expr = isl_calloc_type(ctx, isl_ast_expr);
if (!expr)
goto error;
expr->ctx = ctx;
isl_ctx_ref(ctx);
expr->ref = 1;
expr->type = isl_ast_expr_id;
expr->u.id = id;
return expr;
error:
isl_id_free(id);
return NULL;
}
/* Create a new integer expression representing "i".
*/
__isl_give isl_ast_expr *isl_ast_expr_alloc_int_si(isl_ctx *ctx, int i)
{
isl_ast_expr *expr;
expr = isl_calloc_type(ctx, isl_ast_expr);
if (!expr)
return NULL;
expr->ctx = ctx;
isl_ctx_ref(ctx);
expr->ref = 1;
expr->type = isl_ast_expr_int;
expr->u.v = isl_val_int_from_si(ctx, i);
if (!expr->u.v)
return isl_ast_expr_free(expr);
return expr;
}
/* Create a new integer expression representing "v".
*/
__isl_give isl_ast_expr *isl_ast_expr_from_val(__isl_take isl_val *v)
{
isl_ctx *ctx;
isl_ast_expr *expr;
if (!v)
return NULL;
if (!isl_val_is_int(v))
isl_die(isl_val_get_ctx(v), isl_error_invalid,
"expecting integer value", goto error);
ctx = isl_val_get_ctx(v);
expr = isl_calloc_type(ctx, isl_ast_expr);
if (!expr)
goto error;
expr->ctx = ctx;
isl_ctx_ref(ctx);
expr->ref = 1;
expr->type = isl_ast_expr_int;
expr->u.v = v;
return expr;
error:
isl_val_free(v);
return NULL;
}
/* Create an expression representing the unary operation "type" applied to
* "arg".
*/
__isl_give isl_ast_expr *isl_ast_expr_alloc_unary(
enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg)
{
isl_ctx *ctx;
isl_ast_expr *expr = NULL;
isl_ast_expr_list *args;
if (!arg)
return NULL;
ctx = isl_ast_expr_get_ctx(arg);
expr = isl_ast_expr_alloc_op(ctx, type, 1);
args = isl_ast_expr_op_take_args(expr);
args = isl_ast_expr_list_add(args, arg);
expr = isl_ast_expr_op_restore_args(expr, args);
return expr;
}
/* Create an expression representing the negation of "arg".
*/
__isl_give isl_ast_expr *isl_ast_expr_neg(__isl_take isl_ast_expr *arg)
{
return isl_ast_expr_alloc_unary(isl_ast_expr_op_minus, arg);
}
/* Create an expression representing the address of "expr".
*/
__isl_give isl_ast_expr *isl_ast_expr_address_of(__isl_take isl_ast_expr *expr)
{
if (!expr)
return NULL;
if (isl_ast_expr_get_type(expr) != isl_ast_expr_op ||
isl_ast_expr_get_op_type(expr) != isl_ast_expr_op_access)
isl_die(isl_ast_expr_get_ctx(expr), isl_error_invalid,
"can only take address of access expressions",
return isl_ast_expr_free(expr));
return isl_ast_expr_alloc_unary(isl_ast_expr_op_address_of, expr);
}
/* Create an expression representing the binary operation "type"
* applied to "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_alloc_binary(
enum isl_ast_expr_op_type type,
__isl_take isl_ast_expr *expr1, __isl_take isl_ast_expr *expr2)
{
isl_ctx *ctx;
isl_ast_expr *expr = NULL;
isl_ast_expr_list *args;
if (!expr1 || !expr2)
goto error;
ctx = isl_ast_expr_get_ctx(expr1);
expr = isl_ast_expr_alloc_op(ctx, type, 2);
args = isl_ast_expr_op_take_args(expr);
args = isl_ast_expr_list_add(args, expr1);
args = isl_ast_expr_list_add(args, expr2);
expr = isl_ast_expr_op_restore_args(expr, args);
return expr;
error:
isl_ast_expr_free(expr1);
isl_ast_expr_free(expr2);
return NULL;
}
/* Create an expression representing the sum of "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_add(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_add, expr1, expr2);
}
/* Create an expression representing the difference of "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_sub(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_sub, expr1, expr2);
}
/* Create an expression representing the product of "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_mul(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_mul, expr1, expr2);
}
/* Create an expression representing the quotient of "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_div(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_div, expr1, expr2);
}
/* Create an expression representing the quotient of the integer
* division of "expr1" by "expr2", where "expr1" is known to be
* non-negative.
*/
__isl_give isl_ast_expr *isl_ast_expr_pdiv_q(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_q, expr1, expr2);
}
/* Create an expression representing the remainder of the integer
* division of "expr1" by "expr2", where "expr1" is known to be
* non-negative.
*/
__isl_give isl_ast_expr *isl_ast_expr_pdiv_r(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_pdiv_r, expr1, expr2);
}
/* Create an expression representing the conjunction of "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_and(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_and, expr1, expr2);
}
/* Create an expression representing the conjunction of "expr1" and "expr2",
* where "expr2" is evaluated only if "expr1" is evaluated to true.
*/
__isl_give isl_ast_expr *isl_ast_expr_and_then(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_and_then, expr1, expr2);
}
/* Create an expression representing the disjunction of "expr1" and "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_or(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_or, expr1, expr2);
}
/* Create an expression representing the disjunction of "expr1" and "expr2",
* where "expr2" is evaluated only if "expr1" is evaluated to false.
*/
__isl_give isl_ast_expr *isl_ast_expr_or_else(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_or_else, expr1, expr2);
}
/* Create an expression representing "expr1" less than or equal to "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_le(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_le, expr1, expr2);
}
/* Create an expression representing "expr1" less than "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_lt(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_lt, expr1, expr2);
}
/* Create an expression representing "expr1" greater than or equal to "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_ge(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_ge, expr1, expr2);
}
/* Create an expression representing "expr1" greater than "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_gt(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_gt, expr1, expr2);
}
/* Create an expression representing "expr1" equal to "expr2".
*/
__isl_give isl_ast_expr *isl_ast_expr_eq(__isl_take isl_ast_expr *expr1,
__isl_take isl_ast_expr *expr2)
{
return isl_ast_expr_alloc_binary(isl_ast_expr_op_eq, expr1, expr2);
}
/* Create an expression of type "type" with as arguments "arg0" followed
* by "arguments".
*/
static __isl_give isl_ast_expr *ast_expr_with_arguments(
enum isl_ast_expr_op_type type, __isl_take isl_ast_expr *arg0,
__isl_take isl_ast_expr_list *arguments)
{
arguments = isl_ast_expr_list_insert(arguments, 0, arg0);
return alloc_op(type, arguments);
}
/* Create an expression representing an access to "array" with index
* expressions "indices".
*/
__isl_give isl_ast_expr *isl_ast_expr_access(__isl_take isl_ast_expr *array,
__isl_take isl_ast_expr_list *indices)
{
return ast_expr_with_arguments(isl_ast_expr_op_access, array, indices);
}
/* Create an expression representing a call to "function" with argument
* expressions "arguments".
*/
__isl_give isl_ast_expr *isl_ast_expr_call(__isl_take isl_ast_expr *function,
__isl_take isl_ast_expr_list *arguments)
{
return ast_expr_with_arguments(isl_ast_expr_op_call, function, arguments);
}
/* Wrapper around isl_ast_expr_substitute_ids for use
* as an isl_ast_expr_list_map callback.
*/
static __isl_give isl_ast_expr *substitute_ids(__isl_take isl_ast_expr *expr,
void *user)
{
isl_id_to_ast_expr *id2expr = user;
return isl_ast_expr_substitute_ids(expr,
isl_id_to_ast_expr_copy(id2expr));
}
/* For each subexpression of "expr" of type isl_ast_expr_id,
* if it appears in "id2expr", then replace it by the corresponding
* expression.
*/
__isl_give isl_ast_expr *isl_ast_expr_substitute_ids(
__isl_take isl_ast_expr *expr, __isl_take isl_id_to_ast_expr *id2expr)
{
isl_maybe_isl_ast_expr m;
isl_ast_expr_list *args;
if (!expr || !id2expr)
goto error;
switch (expr->type) {
case isl_ast_expr_int:
break;
case isl_ast_expr_id:
m = isl_id_to_ast_expr_try_get(id2expr, expr->u.id);
if (m.valid < 0)
goto error;
if (!m.valid)
break;
isl_ast_expr_free(expr);
expr = m.value;
break;
case isl_ast_expr_op:
args = isl_ast_expr_op_take_args(expr);
args = isl_ast_expr_list_map(args, &substitute_ids, id2expr);
expr = isl_ast_expr_op_restore_args(expr, args);
break;
case isl_ast_expr_error:
expr = isl_ast_expr_free(expr);
break;
}
isl_id_to_ast_expr_free(id2expr);
return expr;
error:
isl_ast_expr_free(expr);
isl_id_to_ast_expr_free(id2expr);
return NULL;
}
isl_ctx *isl_ast_node_get_ctx(__isl_keep isl_ast_node *node)
{
return node ? node->ctx : NULL;
}
enum isl_ast_node_type isl_ast_node_get_type(__isl_keep isl_ast_node *node)
{
return node ? node->type : isl_ast_node_error;
}
__isl_give isl_ast_node *isl_ast_node_alloc(isl_ctx *ctx,
enum isl_ast_node_type type)
{
isl_ast_node *node;
node = isl_calloc_type(ctx, isl_ast_node);
if (!node)
return NULL;
node->ctx = ctx;
isl_ctx_ref(ctx);
node->ref = 1;
node->type = type;
return node;
}
/* Create an if node with the given guard.
*
* The then body needs to be filled in later.
*/
__isl_give isl_ast_node *isl_ast_node_alloc_if(__isl_take isl_ast_expr *guard)
{
isl_ast_node *node;
if (!guard)
return NULL;
node = isl_ast_node_alloc(isl_ast_expr_get_ctx(guard), isl_ast_node_if);
if (!node)
goto error;
node->u.i.guard = guard;
return node;
error:
isl_ast_expr_free(guard);
return NULL;
}
/* Create a for node with the given iterator.
*
* The remaining fields need to be filled in later.
*/
__isl_give isl_ast_node *isl_ast_node_alloc_for(__isl_take isl_id *id)
{
isl_ast_node *node;
isl_ctx *ctx;
if (!id)
return NULL;
ctx = isl_id_get_ctx(id);
node = isl_ast_node_alloc(ctx, isl_ast_node_for);
if (!node)
goto error;
node->u.f.iterator = isl_ast_expr_from_id(id);
if (!node->u.f.iterator)
return isl_ast_node_free(node);
return node;
error:
isl_id_free(id);
return NULL;
}
/* Create a mark node, marking "node" with "id".
*/
__isl_give isl_ast_node *isl_ast_node_alloc_mark(__isl_take isl_id *id,
__isl_take isl_ast_node *node)
{
isl_ctx *ctx;
isl_ast_node *mark;
if (!id || !node)
goto error;
ctx = isl_id_get_ctx(id);
mark = isl_ast_node_alloc(ctx, isl_ast_node_mark);
if (!mark)
goto error;
mark->u.m.mark = id;
mark->u.m.node = node;
return mark;
error:
isl_id_free(id);
isl_ast_node_free(node);