-
Notifications
You must be signed in to change notification settings - Fork 9
/
tree.c
1759 lines (1562 loc) · 43.9 KB
/
tree.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 2011 Leiden University. All rights reserved.
* Copyright 2014 Ecole Normale Superieure. All rights reserved.
* Copyright 2017 Sven Verdoolaege. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Leiden University.
*/
#include <string.h>
#include <isl/ctx.h>
#include <isl/id.h>
#include <isl/val.h>
#include <isl/space.h>
#include <isl/aff.h>
#include "expr.h"
#include "loc.h"
#include "tree.h"
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
static const char *type_str[] = {
[pet_tree_block] = "block",
[pet_tree_break] = "break",
[pet_tree_continue] = "continue",
[pet_tree_decl] = "declaration",
[pet_tree_decl_init] = "declaration-init",
[pet_tree_expr] = "expression",
[pet_tree_for] = "for",
[pet_tree_infinite_loop] = "infinite-loop",
[pet_tree_if] = "if",
[pet_tree_if_else] = "if-else",
[pet_tree_while] = "while",
[pet_tree_return] = "return",
};
/* Return a textual representation of the type "type".
*/
const char *pet_tree_type_str(enum pet_tree_type type)
{
if (type < 0)
return "error";
return type_str[type];
}
/* Extract a type from its textual representation "str".
*/
enum pet_tree_type pet_tree_str_type(const char *str)
{
int i;
for (i = 0; i < ARRAY_SIZE(type_str); ++i)
if (!strcmp(type_str[i], str))
return i;
return pet_tree_error;
}
/* Return a new pet_tree of the given type.
*
* The location is initializaed to pet_loc_dummy.
*/
__isl_give pet_tree *pet_tree_alloc(isl_ctx *ctx, enum pet_tree_type type)
{
pet_tree *tree;
tree = isl_calloc_type(ctx, struct pet_tree);
if (!tree)
return NULL;
tree->ctx = ctx;
isl_ctx_ref(ctx);
tree->ref = 1;
tree->type = type;
tree->loc = &pet_loc_dummy;
return tree;
}
/* Return a new pet_tree representing the declaration (without initialization)
* of the variable "var".
*/
__isl_give pet_tree *pet_tree_new_decl(__isl_take pet_expr *var)
{
isl_ctx *ctx;
pet_tree *tree;
if (!var)
return NULL;
ctx = pet_expr_get_ctx(var);
tree = pet_tree_alloc(ctx, pet_tree_decl);
if (!tree)
goto error;
tree->u.d.var = var;
return tree;
error:
pet_expr_free(var);
return NULL;
}
/* Return a new pet_tree representing the declaration of the variable "var"
* with initial value "init".
*/
__isl_give pet_tree *pet_tree_new_decl_init(__isl_take pet_expr *var,
__isl_take pet_expr *init)
{
isl_ctx *ctx;
pet_tree *tree;
if (!var || !init)
goto error;
ctx = pet_expr_get_ctx(var);
tree = pet_tree_alloc(ctx, pet_tree_decl_init);
if (!tree)
goto error;
tree->u.d.var = var;
tree->u.d.init = init;
return tree;
error:
pet_expr_free(var);
pet_expr_free(init);
return NULL;
}
/* Return a new pet_tree representing the expression "expr".
*/
__isl_give pet_tree *pet_tree_new_expr(__isl_take pet_expr *expr)
{
isl_ctx *ctx;
pet_tree *tree;
if (!expr)
return NULL;
ctx = pet_expr_get_ctx(expr);
tree = pet_tree_alloc(ctx, pet_tree_expr);
if (!tree)
goto error;
tree->u.e.expr = expr;
return tree;
error:
pet_expr_free(expr);
return NULL;
}
/* Return a new pet_tree representing the return of expression "expr".
*/
__isl_give pet_tree *pet_tree_new_return(__isl_take pet_expr *expr)
{
isl_ctx *ctx;
pet_tree *tree;
if (!expr)
return NULL;
ctx = pet_expr_get_ctx(expr);
tree = pet_tree_alloc(ctx, pet_tree_return);
if (!tree)
goto error;
tree->u.e.expr = expr;
return tree;
error:
pet_expr_free(expr);
return NULL;
}
/* Return a new pet_tree representing an initially empty sequence
* of trees with room for "n" trees.
* "block" indicates whether the sequence has its own scope.
*/
__isl_give pet_tree *pet_tree_new_block(isl_ctx *ctx, int block, int n)
{
pet_tree *tree;
tree = pet_tree_alloc(ctx, pet_tree_block);
if (!tree)
return NULL;
tree->u.b.block = block;
tree->u.b.n = 0;
tree->u.b.max = n;
tree->u.b.child = isl_calloc_array(ctx, pet_tree *, n);
if (n && !tree->u.b.child)
return pet_tree_free(tree);
return tree;
}
/* Return a new pet_tree representing a break statement.
*/
__isl_give pet_tree *pet_tree_new_break(isl_ctx *ctx)
{
return pet_tree_alloc(ctx, pet_tree_break);
}
/* Return a new pet_tree representing a continue statement.
*/
__isl_give pet_tree *pet_tree_new_continue(isl_ctx *ctx)
{
return pet_tree_alloc(ctx, pet_tree_continue);
}
/* Return a new pet_tree representing a for loop
* with induction variable "iv", initial value for the induction
* variable "init", loop condition "cond", induction variable increment "inc"
* and loop body "body". "declared" indicates whether the induction variable
* is declared by the loop. "independent" is set if the for loop is marked
* independent.
*
* The location of the loop is initialized to that of the body.
*/
__isl_give pet_tree *pet_tree_new_for(int independent, int declared,
__isl_take pet_expr *iv, __isl_take pet_expr *init,
__isl_take pet_expr *cond, __isl_take pet_expr *inc,
__isl_take pet_tree *body)
{
isl_ctx *ctx;
pet_tree *tree;
if (!iv || !init || !cond || !inc || !body)
goto error;
ctx = pet_tree_get_ctx(body);
tree = pet_tree_alloc(ctx, pet_tree_for);
if (!tree)
goto error;
tree->u.l.independent = independent;
tree->u.l.declared = declared;
tree->u.l.iv = iv;
tree->u.l.init = init;
tree->u.l.cond = cond;
tree->u.l.inc = inc;
tree->u.l.body = body;
tree->loc = pet_tree_get_loc(body);
if (!tree->loc)
return pet_tree_free(tree);
return tree;
error:
pet_expr_free(iv);
pet_expr_free(init);
pet_expr_free(cond);
pet_expr_free(inc);
pet_tree_free(body);
return NULL;
}
/* Return a new pet_tree representing a while loop
* with loop condition "cond" and loop body "body".
*
* The location of the loop is initialized to that of the body.
*/
__isl_give pet_tree *pet_tree_new_while(__isl_take pet_expr *cond,
__isl_take pet_tree *body)
{
isl_ctx *ctx;
pet_tree *tree;
if (!cond || !body)
goto error;
ctx = pet_tree_get_ctx(body);
tree = pet_tree_alloc(ctx, pet_tree_while);
if (!tree)
goto error;
tree->u.l.cond = cond;
tree->u.l.body = body;
tree->loc = pet_tree_get_loc(body);
if (!tree->loc)
return pet_tree_free(tree);
return tree;
error:
pet_expr_free(cond);
pet_tree_free(body);
return NULL;
}
/* Return a new pet_tree representing an infinite loop
* with loop body "body".
*
* The location of the loop is initialized to that of the body.
*/
__isl_give pet_tree *pet_tree_new_infinite_loop(__isl_take pet_tree *body)
{
isl_ctx *ctx;
pet_tree *tree;
if (!body)
return NULL;
ctx = pet_tree_get_ctx(body);
tree = pet_tree_alloc(ctx, pet_tree_infinite_loop);
if (!tree)
return pet_tree_free(body);
tree->u.l.body = body;
tree->loc = pet_tree_get_loc(body);
if (!tree->loc)
return pet_tree_free(tree);
return tree;
}
/* Return a new pet_tree representing an if statement
* with condition "cond" and then branch "then_body".
*
* The location of the if statement is initialized to that of the body.
*/
__isl_give pet_tree *pet_tree_new_if(__isl_take pet_expr *cond,
__isl_take pet_tree *then_body)
{
isl_ctx *ctx;
pet_tree *tree;
if (!cond || !then_body)
goto error;
ctx = pet_tree_get_ctx(then_body);
tree = pet_tree_alloc(ctx, pet_tree_if);
if (!tree)
goto error;
tree->u.i.cond = cond;
tree->u.i.then_body = then_body;
tree->loc = pet_tree_get_loc(then_body);
if (!tree->loc)
return pet_tree_free(tree);
return tree;
error:
pet_expr_free(cond);
pet_tree_free(then_body);
return NULL;
}
/* Return a new pet_tree representing an if statement
* with condition "cond", then branch "then_body" and else branch "else_body".
*
* The location of the if statement is initialized to cover
* those of the bodies.
*/
__isl_give pet_tree *pet_tree_new_if_else(__isl_take pet_expr *cond,
__isl_take pet_tree *then_body, __isl_take pet_tree *else_body)
{
isl_ctx *ctx;
pet_tree *tree;
if (!cond || !then_body || !else_body)
goto error;
ctx = pet_tree_get_ctx(then_body);
tree = pet_tree_alloc(ctx, pet_tree_if_else);
if (!tree)
goto error;
tree->u.i.cond = cond;
tree->u.i.then_body = then_body;
tree->u.i.else_body = else_body;
tree->loc = pet_tree_get_loc(then_body);
tree->loc = pet_loc_update_start_end_from_loc(tree->loc,
else_body->loc);
if (!tree->loc)
return pet_tree_free(tree);
return tree;
error:
pet_expr_free(cond);
pet_tree_free(then_body);
pet_tree_free(else_body);
return NULL;
}
/* Return an independent duplicate of "tree".
*/
static __isl_give pet_tree *pet_tree_dup(__isl_keep pet_tree *tree)
{
int i;
pet_tree *dup;
if (!tree)
return NULL;
switch (tree->type) {
case pet_tree_error:
return NULL;
case pet_tree_block:
dup = pet_tree_new_block(tree->ctx, tree->u.b.block,
tree->u.b.n);
for (i = 0; i < tree->u.b.n; ++i)
dup = pet_tree_block_add_child(dup,
pet_tree_copy(tree->u.b.child[i]));
break;
case pet_tree_break:
dup = pet_tree_new_break(tree->ctx);
break;
case pet_tree_continue:
dup = pet_tree_new_continue(tree->ctx);
break;
case pet_tree_decl:
dup = pet_tree_new_decl(pet_expr_copy(tree->u.d.var));
break;
case pet_tree_decl_init:
dup = pet_tree_new_decl_init(pet_expr_copy(tree->u.d.var),
pet_expr_copy(tree->u.d.init));
break;
case pet_tree_expr:
dup = pet_tree_new_expr(pet_expr_copy(tree->u.e.expr));
break;
case pet_tree_return:
dup = pet_tree_new_return(pet_expr_copy(tree->u.e.expr));
break;
case pet_tree_for:
dup = pet_tree_new_for(tree->u.l.independent,
tree->u.l.declared,
pet_expr_copy(tree->u.l.iv), pet_expr_copy(tree->u.l.init),
pet_expr_copy(tree->u.l.cond), pet_expr_copy(tree->u.l.inc),
pet_tree_copy(tree->u.l.body));
break;
case pet_tree_while:
dup = pet_tree_new_while(pet_expr_copy(tree->u.l.cond),
pet_tree_copy(tree->u.l.body));
break;
case pet_tree_infinite_loop:
dup = pet_tree_new_infinite_loop(pet_tree_copy(tree->u.l.body));
break;
case pet_tree_if:
dup = pet_tree_new_if(pet_expr_copy(tree->u.i.cond),
pet_tree_copy(tree->u.i.then_body));
break;
case pet_tree_if_else:
dup = pet_tree_new_if_else(pet_expr_copy(tree->u.i.cond),
pet_tree_copy(tree->u.i.then_body),
pet_tree_copy(tree->u.i.else_body));
break;
}
if (!dup)
return NULL;
pet_loc_free(dup->loc);
dup->loc = pet_loc_copy(tree->loc);
if (!dup->loc)
return pet_tree_free(dup);
if (tree->label) {
dup->label = isl_id_copy(tree->label);
if (!dup->label)
return pet_tree_free(dup);
}
return dup;
}
/* Return a pet_tree that is equal to "tree" and that has only one reference.
*/
__isl_give pet_tree *pet_tree_cow(__isl_take pet_tree *tree)
{
if (!tree)
return NULL;
if (tree->ref == 1)
return tree;
tree->ref--;
return pet_tree_dup(tree);
}
/* Return an extra reference to "tree".
*/
__isl_give pet_tree *pet_tree_copy(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
tree->ref++;
return tree;
}
/* Free a reference to "tree".
*/
__isl_null pet_tree *pet_tree_free(__isl_take pet_tree *tree)
{
int i;
if (!tree)
return NULL;
if (--tree->ref > 0)
return NULL;
pet_loc_free(tree->loc);
isl_id_free(tree->label);
switch (tree->type) {
case pet_tree_error:
break;
case pet_tree_block:
for (i = 0; i < tree->u.b.n; ++i)
pet_tree_free(tree->u.b.child[i]);
free(tree->u.b.child);
break;
case pet_tree_break:
case pet_tree_continue:
break;
case pet_tree_decl_init:
pet_expr_free(tree->u.d.init);
case pet_tree_decl:
pet_expr_free(tree->u.d.var);
break;
case pet_tree_expr:
case pet_tree_return:
pet_expr_free(tree->u.e.expr);
break;
case pet_tree_for:
pet_expr_free(tree->u.l.iv);
pet_expr_free(tree->u.l.init);
pet_expr_free(tree->u.l.inc);
case pet_tree_while:
pet_expr_free(tree->u.l.cond);
case pet_tree_infinite_loop:
pet_tree_free(tree->u.l.body);
break;
case pet_tree_if_else:
pet_tree_free(tree->u.i.else_body);
case pet_tree_if:
pet_expr_free(tree->u.i.cond);
pet_tree_free(tree->u.i.then_body);
break;
}
isl_ctx_deref(tree->ctx);
free(tree);
return NULL;
}
/* Return the isl_ctx in which "tree" was created.
*/
isl_ctx *pet_tree_get_ctx(__isl_keep pet_tree *tree)
{
return tree ? tree->ctx : NULL;
}
/* Return the location of "tree".
*/
__isl_give pet_loc *pet_tree_get_loc(__isl_keep pet_tree *tree)
{
return tree ? pet_loc_copy(tree->loc) : NULL;
}
/* Return the type of "tree".
*/
enum pet_tree_type pet_tree_get_type(__isl_keep pet_tree *tree)
{
if (!tree)
return pet_tree_error;
return tree->type;
}
/* Replace the location of "tree" by "loc".
*/
__isl_give pet_tree *pet_tree_set_loc(__isl_take pet_tree *tree,
__isl_take pet_loc *loc)
{
tree = pet_tree_cow(tree);
if (!tree || !loc)
goto error;
pet_loc_free(tree->loc);
tree->loc = loc;
return tree;
error:
pet_loc_free(loc);
pet_tree_free(tree);
return NULL;
}
/* Replace the label of "tree" by "label".
*/
__isl_give pet_tree *pet_tree_set_label(__isl_take pet_tree *tree,
__isl_take isl_id *label)
{
tree = pet_tree_cow(tree);
if (!tree || !label)
goto error;
isl_id_free(tree->label);
tree->label = label;
return tree;
error:
isl_id_free(label);
return pet_tree_free(tree);
}
/* Given an expression tree "tree", return the associated expression.
*/
__isl_give pet_expr *pet_tree_expr_get_expr(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_expr)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not an expression tree", return NULL);
return pet_expr_copy(tree->u.e.expr);
}
/* Given a return tree "tree", return the returned expression.
*/
__isl_give pet_expr *pet_tree_return_get_expr(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_return)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a return tree", return NULL);
return pet_expr_copy(tree->u.e.expr);
}
/* Given a block tree "tree", return the number of children in the sequence.
*/
int pet_tree_block_n_child(__isl_keep pet_tree *tree)
{
if (!tree)
return -1;
if (pet_tree_get_type(tree) != pet_tree_block)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a block tree", return -1);
return tree->u.b.n;
}
/* Add "child" to the sequence of trees represented by "block".
*
* Update the location of "block" to include that of "child".
*/
__isl_give pet_tree *pet_tree_block_add_child(__isl_take pet_tree *block,
__isl_take pet_tree *child)
{
block = pet_tree_cow(block);
if (!block || !child)
goto error;
if (block->type != pet_tree_block)
isl_die(pet_tree_get_ctx(block), isl_error_invalid,
"not a block tree", goto error);
if (block->u.b.n >= block->u.b.max)
isl_die(pet_tree_get_ctx(block), isl_error_invalid,
"out of space in block", goto error);
block->loc = pet_loc_update_start_end_from_loc(block->loc, child->loc);
block->u.b.child[block->u.b.n++] = child;
if (!block->loc)
return pet_tree_free(block);
return block;
error:
pet_tree_free(block);
pet_tree_free(child);
return NULL;
}
/* Does the block tree "tree" have its own scope?
*/
int pet_tree_block_get_block(__isl_keep pet_tree *tree)
{
if (!tree)
return -1;
if (pet_tree_get_type(tree) != pet_tree_block)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a block tree", return -1);
return tree->u.b.block;
}
/* Set the block property (whether or not the block tree has its own scope)
* of "tree" to "is_block".
*/
__isl_give pet_tree *pet_tree_block_set_block(__isl_take pet_tree *tree,
int is_block)
{
if (!tree)
return NULL;
if (tree->type != pet_tree_block)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a block tree", return pet_tree_free(tree));
if (tree->u.b.block == is_block)
return tree;
tree = pet_tree_cow(tree);
if (!tree)
return NULL;
tree->u.b.block = is_block;
return tree;
}
/* Given a block tree "tree", return the child at position "pos".
*/
__isl_give pet_tree *pet_tree_block_get_child(__isl_keep pet_tree *tree,
int pos)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_block)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a block tree", return NULL);
if (pos < 0 || pos >= tree->u.b.n)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"position out of bounds", return NULL);
return pet_tree_copy(tree->u.b.child[pos]);
}
/* Does "tree" represent a declaration (with or without initialization)?
*/
int pet_tree_is_decl(__isl_keep pet_tree *tree)
{
if (!tree)
return -1;
switch (pet_tree_get_type(tree)) {
case pet_tree_decl:
case pet_tree_decl_init:
return 1;
default:
return 0;
}
}
/* Given a declaration tree "tree", return the variable that is being
* declared.
*/
__isl_give pet_expr *pet_tree_decl_get_var(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (!pet_tree_is_decl(tree))
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a decl tree", return NULL);
return pet_expr_copy(tree->u.d.var);
}
/* Given a declaration tree with initialization "tree",
* return the initial value of the declared variable.
*/
__isl_give pet_expr *pet_tree_decl_get_init(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_decl_init)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a decl_init tree", return NULL);
return pet_expr_copy(tree->u.d.init);
}
/* Given an if tree "tree", return the if condition.
*/
__isl_give pet_expr *pet_tree_if_get_cond(__isl_keep pet_tree *tree)
{
enum pet_tree_type type;
if (!tree)
return NULL;
type = pet_tree_get_type(tree);
if (type != pet_tree_if && type != pet_tree_if_else)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not an if tree", return NULL);
return pet_expr_copy(tree->u.i.cond);
}
/* Given an if tree "tree", return the body of the then branch.
*/
__isl_give pet_tree *pet_tree_if_get_then(__isl_keep pet_tree *tree)
{
enum pet_tree_type type;
if (!tree)
return NULL;
type = pet_tree_get_type(tree);
if (type != pet_tree_if && type != pet_tree_if_else)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not an if tree", return NULL);
return pet_tree_copy(tree->u.i.then_body);
}
/* Given an if tree with an else branch "tree",
* return the body of that else branch.
*/
__isl_give pet_tree *pet_tree_if_get_else(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_if_else)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not an if tree with an else branch", return NULL);
return pet_tree_copy(tree->u.i.else_body);
}
/* Does "tree" represent some type of loop?
*/
int pet_tree_is_loop(__isl_keep pet_tree *tree)
{
if (!tree)
return -1;
switch (pet_tree_get_type(tree)) {
case pet_tree_for:
case pet_tree_infinite_loop:
case pet_tree_while:
return 1;
default:
return 0;
}
}
/* Given a for loop "tree", return the induction variable.
*/
__isl_give pet_expr *pet_tree_loop_get_var(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_for)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a for tree", return NULL);
return pet_expr_copy(tree->u.l.iv);
}
/* Given a for loop "tree", return the initial value of the induction variable.
*/
__isl_give pet_expr *pet_tree_loop_get_init(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_for)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a for tree", return NULL);
return pet_expr_copy(tree->u.l.init);
}
/* Given a loop "tree", return the loop condition.
*
* For an infinite loop, the loop condition always holds,
* so we simply return "1".
*/
__isl_give pet_expr *pet_tree_loop_get_cond(__isl_keep pet_tree *tree)
{
enum pet_tree_type type;
if (!tree)
return NULL;
type = pet_tree_get_type(tree);
if (type == pet_tree_infinite_loop)
return pet_expr_new_int(isl_val_one(pet_tree_get_ctx(tree)));
if (type != pet_tree_for && type != pet_tree_while)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a for or while tree", return NULL);
return pet_expr_copy(tree->u.l.cond);
}
/* Given a for loop "tree", return the increment of the induction variable.
*/
__isl_give pet_expr *pet_tree_loop_get_inc(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (pet_tree_get_type(tree) != pet_tree_for)
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a for tree", return NULL);
return pet_expr_copy(tree->u.l.inc);
}
/* Given a loop tree "tree", return the body.
*/
__isl_give pet_tree *pet_tree_loop_get_body(__isl_keep pet_tree *tree)
{
if (!tree)
return NULL;
if (!pet_tree_is_loop(tree))
isl_die(pet_tree_get_ctx(tree), isl_error_invalid,
"not a loop tree", return NULL);
return pet_tree_copy(tree->u.l.body);
}
/* Call "fn" on each node of "tree", including "tree" itself.
*
* Return 0 on success and -1 on error, where "fn" returning a negative
* value is treated as an error.
*/
int pet_tree_foreach_sub_tree(__isl_keep pet_tree *tree,
int (*fn)(__isl_keep pet_tree *tree, void *user), void *user)
{
int i;
if (!tree)
return -1;
if (fn(tree, user) < 0)
return -1;
switch (tree->type) {
case pet_tree_error:
return -1;
case pet_tree_block:
for (i = 0; i < tree->u.b.n; ++i)
if (pet_tree_foreach_sub_tree(tree->u.b.child[i],
fn, user) < 0)
return -1;
break;
case pet_tree_break:
case pet_tree_continue:
case pet_tree_decl:
case pet_tree_decl_init:
case pet_tree_expr:
case pet_tree_return:
break;
case pet_tree_if:
if (pet_tree_foreach_sub_tree(tree->u.i.then_body,
fn, user) < 0)
return -1;
break;
case pet_tree_if_else:
if (pet_tree_foreach_sub_tree(tree->u.i.then_body,
fn, user) < 0)
return -1;
if (pet_tree_foreach_sub_tree(tree->u.i.else_body,
fn, user) < 0)
return -1;
break;
case pet_tree_while:
case pet_tree_for:
case pet_tree_infinite_loop:
if (pet_tree_foreach_sub_tree(tree->u.l.body, fn, user) < 0)
return -1;
break;
}
return 0;
}
/* Intermediate data structure for foreach_expr.
*
* "fn" is the function that needs to be called on each expression.
* "user" is the user argument to be passed to "fn".
*/
struct pet_tree_foreach_expr_data {
int (*fn)(__isl_keep pet_expr *expr, void *user);
void *user;
};
/* Call data->fn on each expression in the "tree" object.
* This function is used as a callback to pet_tree_foreach_sub_tree
* to implement pet_tree_foreach_expr.
*
* Return 0 on success and -1 on error, where data->fn returning a negative
* value is treated as an error.
*/
static int foreach_expr(__isl_keep pet_tree *tree, void *user)
{
struct pet_tree_foreach_expr_data *data = user;