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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
|
/* Implement looping actions for CHILL.
Copyright (C) 1992, 93, 1994 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <limits.h>
#include "config.h"
#include "tree.h"
#include "ch-tree.h"
#include "lex.h"
#include "flags.h"
#include "actions.h"
#include "input.h"
#include "obstack.h"
#include "assert.h"
#include "rtl.h"
/* if the user codes '-flocal-loop-counter' on the command line,
ch-actions.c (lang_decode_option) will set this flag. */
int flag_local_loop_counter = 0;
extern tree chill_truthvalue_conversion PROTO((tree));
extern rtx emit_line_note PROTO((char *, int));
extern void error PROTO((char *, ...));
extern rtx expand_assignment PROTO((tree, tree, int, int));
extern void save_expr_under_name PROTO((tree, tree));
extern void stamp_nesting_label PROTO((tree));
extern int int_fits_type_p PROTO((tree, tree));
extern void warning PROTO((char *, ...));
/* forward declarations */
static int classify_loop PROTO((void));
static int declare_temps PROTO((void));
static int initialize_iter_var PROTO((void));
static int maybe_skip_loop PROTO((void));
static int top_loop_end_check PROTO((void));
static int bottom_loop_end_check PROTO((void));
static int increment_temps PROTO((void));
static tree build_temporary_variable PROTO((char *, tree));
static tree maybe_make_for_temp PROTO((tree, char *, tree));
static tree chill_unsigned_type PROTO((tree));
/* In terms of the parameters passed to build_loop_iterator,
* there are several types of loops. They are encoded by
* the ITER_TYPE enumeration.
*
* 1) DO FOR EVER; ... OD
* indicated by a NULL_TREE start_exp, step_exp and end_exp,
* condition == NULL, in_flag = 0, and ever_flag == 1 in the
* first ITERATOR.
*
* 2) DO WHILE cond; ... OD
* indicated by NULL_TREE start_exp, step_exp and end_exp,
* in_flag = 0, and condition != NULL.
*
* 3) DO; ... OD
* indicated by NULL_TREEs in start_exp, step_exp and end_exp,
* condition != NULL, in_flag == 0 and ever_flag == 0. This
* is not really a loop, but a compound statement.
*
* 4) DO FOR user_var := start_exp
* [DOWN] TO end_exp BY step_exp; ... DO
* indicated by non-NULL_TREE start_exp, step_exp and end_exp.
*
* 5) DO FOR user_var [DOWN] IN discrete_mode; ... OD
* indicated by in_flag == 1. start_exp is a non-NULL_TREE
* discrete mode, with an optional down_flag.
*
* 6) DO FOR user_var [DOWN] IN powerset_expr; ... OD
* indicated by in_flag == 1. start_exp is a non-NULL_TREE
* powerset mode, with an optional down_flag.
*
* 7) DO FOR user_var [DOWN] IN location; ... OD
* indicated by in_flag == 1. start_exp is a non-NULL_TREE
* location mode, with an optional down_flag.
*/
typedef enum
{
DO_UNUSED,
DO_FOREVER,
DO_WHILE,
DO_OD,
DO_STEP,
DO_RANGE,
DO_POWERSET,
DO_LOC,
DO_LOC_VARYING
} ITER_TYPE;
typedef struct iterator
{
/* These variables only have meaning in the first ITERATOR structure. */
ITER_TYPE itype; /* type of this iterator */
int error_flag; /* TRUE if no loop was started due to
user error */
tree condition; /* WHILE condition expression */
int down_flag; /* TRUE if DOWN was coded */
/* These variables have meaning in every ITERATOR structure. */
tree user_var; /* user's explicit iteration variable */
tree start_exp; /* user's start expression
or IN expression of a FOR .. IN*/
tree step_exp; /* user's step expression */
tree end_exp; /* user's end expression */
tree start_temp; /* temp holding evaluated start_exp */
tree end_temp; /* temp holding evaluated end_exp */
tree step_temp; /* temp holding evaluated step_exp */
tree powerset_temp; /* temp holding user's initial powerset expression */
tree loc_ptr_temp; /* temp holding count for LOC enumeration ptr */
tree iter_var; /* hidden variable for the loop */
tree iter_type; /* hidden variable's type */
tree base_type; /* LOC enumeration base type */
struct iterator *next; /* ptr to next iterator for this loop */
} ITERATOR;
/*
* There's an entry like this for each nested DO loop.
* The list is maintained by push_loop_block
* and pop_loop_block.
*/
typedef struct loop {
struct loop *nxt_level; /* pointer to enclosing loop */
ITERATOR *iter_list; /* iterators for the current loop */
} LOOP;
static LOOP *loop_stack = (LOOP *)0;
#if 0
Here is a CHILL DO FOR statement:
DO FOR user_var := start_exp BY step_exp [DOWN] TO end_exp
WHILE condition;
For this loop to be 'safe', like a Pascal FOR loop, the start,
end, and increment expressions are computed once, before the
assignment to the iteration variable and saved in temporaries,
before the first assignment of the iteration variable, so the
following works:
FOR i := (i+1) TO (i+10) DO
To prevent changes to the start/end/step expressions from
effecting the loop''s termination, and to make the loop end-check
as simple as possible, we evaluate the step expression into
a temporary and compute a hidden iteration count before entering
the loop''s body. User code cannot effect the counter, and the
end-loop check simply decrements the counter and checks for zero.
The whole phrase FOR iter := ... TO end_exp can be repeated
multiple times, with different user-iteration variables. This
is discussed later.
The loop counter calculations need careful design since a loop
from MININT TO MAXINT must work, in the precision of integers.
Here''s how it works, in C:
0) The DO ... OD loop is simply a block with
its own scope.
1) The DO FOR EVER is simply implemented:
loop_top:
.
. body of loop
.
goto loop_top
end_loop:
2) The DO WHILE is also simple:
loop_top:
if (!condition) goto end_loop
.
. body of loop
.
goto loop_top
end_loop:
3) The DO FOR [while condition] loop (no DOWN)
push a new scope,
decl iter_var
step_temp = step_exp
start_temp = start_exp
end_temp = end_exp
if (end_exp < start_exp) goto end_loop
/* following line is all unsigned arithmetic */
iter_var = (end_exp - start_exp + step_exp) / step_exp
user_var = start_temp
loop_top:
if (!condition) goto end_loop
.
. body of loop
.
iter_var--
if (iter_var == 0) goto end_loop
user_var += step_temp
goto loop_top
end_loop:
pop scope
4) The proposed CHILL for [while condition] loop (with DOWN)
push a new scope,
decl iter
step_temp = step_exp
start_temp = start_exp
end_temp = end_exp
if (end_exp > start_exp) goto end_loop
/* following line is all unsigned arithmetic */
iter_var = (start_exp - end_exp + step_exp) / step_exp
user_var = start_temp
loop_top:
if (!condition) goto end_loop
.
. body of loop
.
iter_var--
if (iter_var == 0) goto end_loop
user_var -= step_temp
goto loop_top
end_loop:
pop scope
5) The range loop, which iterates over a mode''s possible
values, works just like the above step loops, but with
the start and end values taken from the mode''s lower
and upper domain values.
6) The FOR IN loop, where a location enumeration is
specified (see spec on page 81 of Z.200, bottom
of page 186):
push a new scope,
decl iter_var as an unsigned integer
loc_ptr_temp as pointer to a composite base type
if array is varying
iter_var = array''s length field
else
iter_var = sizeof array / sizeof base_type
loc_ptr_temp = &of highest or lowest indexable entry
loop_top:
if (!condition) goto end_loop
.
. body of loop
.
iter_var--
if (iter_var == 0) goto end_loop
loc_ptr_temp +/-= sizeof array base_type
goto loop_top
end_loop:
pop scope
7) The DO FOR (DOWN) IN powerset_exp
push a new scope,
decl powerset_temp
decl iterator as basetype of powerset
powerset_temp := start_exp
loop_top:
/* if DOWN */
if (__flsetclrpowerset () == 0) goto end_loop;
/* not DOWN */
if (__ffsetclrpowerset () == 0) goto end_loop;
if (!condition) goto end_loop
.
. body of loop
.
goto loop_top
end_loop:
pop scope
So, here''s the general DO FOR schema, as implemented here:
classify_loop -- what type of loop have we?
-- build_iterator does some of this, also
expand_start_loop -- start the loop''s control scope
-- start scope for synthesized loop variables
declare_temps -- create, initialize temporary variables
maybe_skip_loop -- skip loop if end conditions unsatisfiable
initialize_iter_var -- initialize the iteration counter
-- initialize user''s loop variable
expand_start_loop -- generate top-of-loop label
top_loop_end_check -- generate while code and/or
powerset find-a-bit function call
.
.
. user''s loop body code
.
.
bottom_loop_end_check -- exit if counter has become zero
increment_temps -- update temps for next iteration
expand_end_loop -- generate jump back to top of loop
expand_end_cond -- generate label for end of conditional
-- end of scope for synthesized loop variables
free_iterators -- free up iterator space
When there are two or more iterator phrases, each of the
above loop steps must act upon all iterators. For example,
the 'increment_temps' step must increment all temporaries
(associated with all iterators).
NOTE: Z.200, section 10.1 says that a block is ...
"the actions statement list in a do action, including any
loop counter and while control". This means that an exp-
ression in a WHILE control can include references to the
loop counters created for the loop''s exclusive use.
Example:
DCL a (1:10) INT;
DCL j INT;
DO FOR j IN a WHILE j > 0;
...
OD;
The 'j' referenced in the while is the loc-identity 'j'
created inside the loop''s scope, and NOT the 'j' declared
before the loop.
#endif
/*
* The following routines are called directly by the
* CHILL parser.
*/
void
push_loop_block ()
{
LOOP *temp = (LOOP *)xmalloc (sizeof (LOOP));
/* push a new loop onto the stack */
temp->nxt_level = loop_stack;
temp->iter_list = (ITERATOR *)0;
loop_stack = temp;
}
void
pop_loop_block ()
{
LOOP *do_temp = loop_stack;
ITERATOR *ip;
/* pop loop block off the list */
loop_stack = do_temp->nxt_level;
/* free the loop's iterator blocks */
ip = do_temp->iter_list;
while (ip != NULL)
{
ITERATOR *temp = ip->next;
free (ip);
ip = temp;
}
free (do_temp);
}
void
begin_loop_scope ()
{
ITERATOR *firstp = loop_stack->iter_list;
if (pass < 2)
return;
/*
* We need to classify the loop and declare its temporaries
* here, so as to define them before the WHILE condition
* (if any) is parsed. The WHILE expression may refer to
* a temporary.
*/
if (classify_loop ())
return;
if (firstp->itype != DO_OD)
declare_temps ();
clear_last_expr ();
push_momentary ();
expand_start_bindings (0);
}
void
end_loop_scope (opt_label)
tree opt_label;
{
if (opt_label)
possibly_define_exit_label (opt_label);
poplevel (0, 0, 0);
if (pass < 2)
return;
expand_end_bindings (getdecls (), kept_level_p (), 0);
pop_momentary ();
}
/* The iterator structure records all aspects of a
* 'FOR i := start [DOWN] TO end' clause or
* 'FOR i IN modename' or 'FOR i IN powerset' clause.
* It's saved on the iter_list of the current LOOP.
*/
void
build_loop_iterator (user_var, start_exp, step_exp, end_exp,
down_flag, in_flag, ever_flag)
tree user_var, start_exp, step_exp, end_exp;
int down_flag, in_flag, ever_flag;
{
ITERATOR *ip = (ITERATOR *)xmalloc (sizeof (ITERATOR));
/* chain this iterator onto the current loop */
if (loop_stack->iter_list == NULL)
loop_stack->iter_list = ip;
else
{
ITERATOR *temp = loop_stack->iter_list;
while (temp->next != NULL)
temp = temp->next;
temp->next = ip;
}
ip->itype = DO_UNUSED;
ip->user_var = user_var;
ip->start_exp = start_exp;
ip->step_exp = step_exp;
ip->end_exp = end_exp;
ip->condition = NULL_TREE;
ip->start_temp = NULL_TREE;
ip->end_temp = NULL_TREE;
ip->step_temp = NULL_TREE;
ip->down_flag = down_flag;
ip->powerset_temp = NULL_TREE;
ip->iter_var = NULL_TREE;
ip->iter_type = NULL_TREE;
ip->loc_ptr_temp = NULL_TREE;
ip->error_flag = 1; /* assume error will be found */
ip->next = (ITERATOR *)0;
if (ever_flag)
ip->itype = DO_FOREVER;
else if (in_flag && start_exp != NULL_TREE)
{
if (TREE_CODE (start_exp) == ERROR_MARK)
return;
if (TREE_CODE (TREE_TYPE (start_exp)) == SET_TYPE)
ip->itype = DO_POWERSET;
else if (discrete_type_p (TREE_TYPE (ip->start_exp)))
ip->itype = DO_RANGE;
else if (TREE_CODE (TREE_TYPE (ip->start_exp)) == ARRAY_TYPE)
ip->itype = DO_LOC;
else if (chill_varying_type_p (TREE_TYPE (ip->start_exp)))
ip->itype = DO_LOC_VARYING;
else
{
error ("Loop's IN expression is not a composite object");
return;
}
}
else if (start_exp == NULL_TREE && end_exp == NULL_TREE
&& step_exp == NULL_TREE && !down_flag)
ip->itype = DO_OD;
else
{
/* FIXME: Move this to the lexer? */
#define CST_FITS_INT(NODE) (TREE_CODE(NODE) == INTEGER_CST &&\
int_fits_type_p (NODE, integer_type_node))
tree max_prec_type = integer_type_node;
if (! discrete_type_p (TREE_TYPE (ip->start_exp)))
{
error ("start expr must have discrete mode");
return;
}
if (TREE_CODE (TREE_TYPE (ip->start_exp)) == ENUMERAL_TYPE
&& CH_ENUM_IS_NUMBERED (TREE_TYPE (ip->start_exp)))
{
error ("DO FOR start expression is a numbered SET");
return;
}
if (TREE_CODE (TREE_TYPE (ip->end_exp)) == ENUMERAL_TYPE
&& CH_ENUM_IS_NUMBERED (TREE_TYPE (ip->end_exp)))
{
error ("TO expression is a numbered SET");
return;
}
/* Convert all three expressions to a common precision,
which is the largest precision they exhibit, but
INTEGER_CST nodes are built in the lexer as
long_integer_type nodes. We'll treat convert them to
integer_type_nodes if possible, for faster loop times. */
if (TYPE_PRECISION (max_prec_type) <
TYPE_PRECISION (TREE_TYPE (ip->start_exp))
&& !CST_FITS_INT (ip->start_exp))
max_prec_type = TREE_TYPE (ip->start_exp);
if (! discrete_type_p (TREE_TYPE (ip->end_exp)))
{
error ("TO expr must have discrete mode");
return;
}
if (! CH_COMPATIBLE (ip->start_exp,
TREE_TYPE (ip->end_exp)))
{
error ("start expr and TO expr must be compatible");
return;
}
if (TYPE_PRECISION (max_prec_type) <
TYPE_PRECISION (TREE_TYPE (ip->end_exp))
&& !CST_FITS_INT (ip->end_exp))
max_prec_type = TREE_TYPE (ip->end_exp);
if (ip->step_exp != NULL_TREE)
{
/* assure that default 'BY 1' gets a useful type */
if (ip->step_exp == integer_one_node)
ip->step_exp = convert (TREE_TYPE (ip->start_exp),
ip->step_exp);
if (! discrete_type_p (TREE_TYPE (ip->step_exp)))
{
error ("BY expr must have discrete mode");
return;
}
if (! CH_COMPATIBLE (ip->start_exp,
TREE_TYPE (ip->step_exp)))
{
error ("start expr and BY expr must be compatible");
return;
}
if (TYPE_PRECISION (max_prec_type) <
TYPE_PRECISION (TREE_TYPE (ip->step_exp))
&& !CST_FITS_INT (ip->step_exp))
max_prec_type = TREE_TYPE (ip->step_exp);
}
if (TREE_CODE (ip->start_exp) == INTEGER_CST
&& TREE_CODE (ip->end_exp) == INTEGER_CST
&& compare_int_csts (ip->down_flag ? LT_EXPR : GT_EXPR,
ip->start_exp, ip->end_exp))
warning ("body of DO FOR will never execute");
ip->start_exp =
convert (max_prec_type, ip->start_exp);
ip->end_exp =
convert (max_prec_type, ip->end_exp);
if (ip->step_exp != NULL_TREE)
{
ip->step_exp =
convert (max_prec_type, ip->step_exp);
if (TREE_CODE (ip->step_exp) != INTEGER_CST)
{
/* generate runtime check for negative BY expr */
ip->step_exp =
check_range (ip->step_exp, ip->step_exp,
integer_zero_node, NULL_TREE);
}
else if (compare_int_csts (LE_EXPR, ip->step_exp, integer_zero_node))
{
error ("BY expression is negative or zero");
return;
}
}
ip->itype = DO_STEP;
}
ip->error_flag = 0; /* no errors! */
}
void
build_loop_start (while_control, start_label)
tree while_control, start_label;
{
ITERATOR *firstp = loop_stack->iter_list;
firstp->condition = while_control;
if (firstp->error_flag)
return;
/* We didn't know at begin_loop_scope time about the condition;
adjust iterator type now. */
if (firstp->itype == DO_OD && firstp->condition)
firstp->itype = DO_WHILE;
if (initialize_iter_var ())
return;
if (maybe_skip_loop ())
return;
/* use the label as an 'exit' label,
'goto' needs another sort of label */
expand_start_loop (start_label != NULL_TREE);
if (top_loop_end_check ())
return;
emit_line_note (input_filename, lineno);
}
/*
* Called after the last action of the loop body
* has been parsed.
*/
void
build_loop_end ()
{
ITERATOR *ip = loop_stack->iter_list;
emit_line_note (input_filename, lineno);
if (ip->error_flag)
return;
if (bottom_loop_end_check ())
return;
if (increment_temps ())
return;
if (ip->itype != DO_OD)
{
expand_end_loop ();
for (; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_LOC_VARYING:
case DO_STEP:
expand_end_cond ();
break;
default:
break;
}
}
}
}
/*
* The rest of the routines in this file are called from
* the above three routines.
*/
static int
classify_loop ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
firstp->error_flag = 0;
if (firstp->itype == DO_UNUSED || firstp->itype == DO_OD)
{
/* if we have just DO .. OD, do nothing - this is just a
BEGIN .. END without creating a new scope, and no looping */
if (firstp->condition != NULL_TREE)
firstp->itype = DO_WHILE;
else
firstp->itype = DO_OD;
}
/* Issue a warning if the any loop counter is mentioned more
than once in the iterator list. */
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_FOREVER:
case DO_WHILE:
break;
case DO_STEP:
case DO_RANGE:
case DO_POWERSET:
case DO_LOC:
case DO_LOC_VARYING:
/* FIXME: check for name uniqueness */
break;
default:
;
}
}
return firstp->error_flag;
}
/*
* Reserve space for any loop-control temporaries, initialize them
*/
static int
declare_temps ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
tree start_ptr;
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_FOREVER:
case DO_WHILE:
break;
case DO_STEP:
ip->iter_type = chill_unsigned_type (TREE_TYPE (ip->start_exp));
/* create, initialize temporaries if expressions aren't constant */
ip->start_temp = maybe_make_for_temp (ip->start_exp, "for_start",
ip->iter_type);
ip->end_temp = maybe_make_for_temp (ip->end_exp, "for_end",
ip->iter_type);
/* this is just the step-expression */
ip->step_temp = maybe_make_for_temp (ip->step_exp, "for_step",
ip->iter_type);
goto do_step_range;
case DO_RANGE:
ip->iter_type = chill_unsigned_type_node;
ip->start_temp =
(ip->down_flag ? build_chill_upper : build_chill_lower)(TREE_TYPE (ip->start_exp));
ip->end_temp =
(ip->down_flag ? build_chill_lower : build_chill_upper)(TREE_TYPE (ip->start_exp));
ip->step_temp = integer_one_node;
do_step_range:
if (flag_local_loop_counter)
{
/* (re-)declare the user's iteration variable in the
loop's scope. */
tree id_node = ip->user_var;
IDENTIFIER_LOCAL_VALUE (id_node) = ip->user_var =
decl_temp1 (id_node, ip->iter_type, 0, NULL_TREE,
0, 0);
}
else
{
/* in this case, it's a previously-declared
VAR_DECL node, checked in build_loop_iterator. */
if (TREE_CODE (ip->user_var) == IDENTIFIER_NODE)
ip->user_var = lookup_name (ip->user_var);
if (ip->user_var == NULL_TREE)
{
error ("loop identifier undeclared");
ip->error_flag = 1;
return 1;
}
}
ip->iter_var =
decl_temp1 (get_unique_identifier ("iter_var"),
ip->iter_type, 0, NULL_TREE, 0, 0);
break;
case DO_POWERSET:
ip->iter_type = chill_unsigned_type (
TYPE_DOMAIN (TREE_TYPE (ip->start_exp)));
if (flag_local_loop_counter)
{
/* declare the user's iteration variable in the loop's scope. */
/* in this case, it's just an IDENTIFIER_NODE */
ip->user_var =
decl_temp1 (ip->user_var, ip->iter_type, 0, NULL_TREE, 0, 0);
}
else
{
/* in this case, it's a previously-declared VAR_DECL node */
ip->user_var = lookup_name (ip->user_var);
}
/* the user's powerset-expression, evaluated and saved in a temp */
ip->powerset_temp = maybe_make_for_temp (ip->start_exp, "for_set",
TREE_TYPE (ip->start_exp));
mark_addressable (ip->powerset_temp);
break;
case DO_LOC:
case DO_LOC_VARYING:
ip->iter_type = chill_unsigned_type_node;
/* create the counter temp */
ip->iter_var =
build_temporary_variable ("iter_var", ip->iter_type);
if (!CH_LOCATION_P (ip->start_exp))
ip->start_exp
= decl_temp1 (get_unique_identifier ("iter_loc"),
TREE_TYPE (ip->start_exp), 0,
ip->start_exp, 0, 0);
if (ip->itype == DO_LOC)
{
tree array_type = TREE_TYPE (ip->start_exp);
tree ptr_type;
tree temp;
if (TREE_CODE (TREE_TYPE (array_type)) == BOOLEAN_TYPE)
{
error ("Can't iterate through array of BOOL");
ip->error_flag = 1;
return ip->error_flag;
}
/* FIXME: check for array type in ip->start_exp */
/* create pointer temporary */
ip->base_type = TREE_TYPE (array_type);
ptr_type = build_pointer_type (ip->base_type);
ip->loc_ptr_temp =
build_temporary_variable ("loc_ptr_tmp", ptr_type);
/* declare the user's iteration variable in
the loop's scope, as an expression, to be
passed to build_component_ref later */
save_expr_under_name (ip->user_var,
build1 (INDIRECT_REF, ip->base_type,
ip->loc_ptr_temp));
/* FIXME: see stor_layout */
ip->step_temp = size_in_bytes (ip->base_type);
temp = TYPE_DOMAIN (array_type);
/* pointer to first array entry to look at */
start_ptr = build1 (ADDR_EXPR, ptr_type, ip->start_exp);
mark_addressable (ip->start_exp);
ip->start_temp = ip->down_flag ?
fold (build (PLUS_EXPR, ptr_type,
start_ptr,
fold (build (MULT_EXPR, integer_type_node, ip->step_temp,
fold (build (MINUS_EXPR, integer_type_node,
TYPE_MAX_VALUE (temp),
TYPE_MIN_VALUE (temp)))))))
: start_ptr;
}
else
{
tree array_length =
convert (integer_type_node,
build_component_ref (ip->start_exp, var_length_id));
tree array_type = TREE_TYPE (TREE_CHAIN (
TYPE_FIELDS (TREE_TYPE (ip->start_exp))));
tree array_data_ptr =
build_component_ref (ip->start_exp, var_data_id);
tree ptr_type;
if (TREE_CODE (TREE_TYPE (array_type)) == BOOLEAN_TYPE)
{
error ("Can't iterate through array of BOOL");
firstp->error_flag = 1;
return firstp->error_flag;
}
/* create pointer temporary */
ip->base_type = TREE_TYPE (array_type);
ptr_type = build_pointer_type (ip->base_type);
ip->loc_ptr_temp =
build_temporary_variable ("loc_ptr_temp", ptr_type);
/* declare the user's iteration variable in
the loop's scope, as an expression, to be
passed to build_component_ref later */
save_expr_under_name (ip->user_var,
build1 (INDIRECT_REF, ip->base_type,
ip->loc_ptr_temp));
/* FIXME: see stor_layout */
ip->step_temp = size_in_bytes (ip->base_type);
/* pointer to first array entry to look at */
start_ptr = build1 (ADDR_EXPR, ptr_type, array_data_ptr);
mark_addressable (array_data_ptr);
ip->start_temp = ip->down_flag ?
fold (build (PLUS_EXPR, ptr_type,
start_ptr,
fold (build (MULT_EXPR, integer_type_node, ip->step_temp,
fold (build (MINUS_EXPR, integer_type_node,
array_length,
integer_one_node))))))
: start_ptr;
}
default:
;
}
}
return firstp->error_flag;
}
/*
* Initialize the hidden iteration-control variables,
* and the user's explicit loop variable.
*/
static int
initialize_iter_var ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_FOREVER:
case DO_WHILE:
break;
case DO_STEP:
case DO_RANGE:
{
tree count =
fold (build (PLUS_EXPR, ip->iter_type, integer_one_node,
fold (build (TRUNC_DIV_EXPR, ip->iter_type,
convert (ip->iter_type,
fold (build (MINUS_EXPR, ip->iter_type,
ip->down_flag ? ip->start_temp : ip->end_temp,
ip->down_flag ? ip->end_temp : ip->start_temp))),
ip->step_temp))));
/* initialize the loop's hidden counter variable */
expand_expr_stmt (
build_chill_modify_expr (ip->iter_var, count));
/* initialize user's variable */
expand_expr_stmt (
build_chill_modify_expr (ip->user_var, ip->start_temp));
}
break;
case DO_POWERSET:
break;
case DO_LOC:
{
tree array_type = TREE_TYPE (ip->start_exp);
tree array_length =
fold (build (TRUNC_DIV_EXPR, integer_type_node,
size_in_bytes (array_type),
size_in_bytes (TREE_TYPE (array_type))));
expand_expr_stmt (
build_chill_modify_expr (ip->iter_var, array_length));
goto do_loc_common;
}
case DO_LOC_VARYING:
expand_expr_stmt (
build_chill_modify_expr (ip->iter_var,
convert (integer_type_node,
build_component_ref (ip->start_exp, var_length_id))));
do_loc_common:
expand_expr_stmt (
build_chill_modify_expr (ip->loc_ptr_temp,
ip->start_temp));
break;
default:
;
}
}
return firstp->error_flag;
}
/* Generate code to skip the whole loop, if start expression not
* <= end expression (or >= for DOWN loops). This comparison must
* *NOT* be done in unsigned mode, or it will fail.
* Also, skip processing an empty VARYING array.
*/
static int
maybe_skip_loop ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_STEP:
expand_start_cond (
build (ip->down_flag ? GE_EXPR : LE_EXPR,
TREE_TYPE (ip->start_exp),
ip->start_exp, ip->end_exp), 0);
break;
case DO_LOC_VARYING:
{ tree array_length =
convert (integer_type_node,
build_component_ref (ip->start_exp, var_length_id));
expand_start_cond (
build (NE_EXPR, TREE_TYPE (array_length),
array_length, integer_zero_node), 0);
break;
}
default:
break;
}
}
return 0;
}
/*
* Check at the top of the loop for a termination
*/
static int
top_loop_end_check ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
/* now, exit the loop if the condition isn't TRUE. */
if (firstp->condition)
{
expand_exit_loop_if_false (0,
chill_truthvalue_conversion (firstp->condition));
}
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_FOREVER:
case DO_WHILE:
case DO_STEP:
case DO_RANGE:
break;
case DO_POWERSET:
{
tree temp1;
char *func_name;
if (ip->down_flag)
func_name = "__flsetclrpowerset";
else
func_name = "__ffsetclrpowerset";
temp1 = TYPE_MIN_VALUE
(TYPE_DOMAIN (TREE_TYPE (ip->powerset_temp)));
expand_exit_loop_if_false (0,
build_chill_function_call (lookup_name (get_identifier (func_name)),
tree_cons (NULL_TREE, force_addr_of (ip->powerset_temp),
tree_cons (NULL_TREE, powersetlen (ip->powerset_temp),
tree_cons (NULL_TREE, force_addr_of (ip->user_var),
tree_cons (NULL_TREE, size_in_bytes (TREE_TYPE (ip->user_var)),
tree_cons (NULL_TREE,
convert (long_integer_type_node, temp1),
NULL_TREE)))))));
}
break;
case DO_LOC:
case DO_LOC_VARYING:
break;
default:
;
}
}
return firstp->error_flag;
}
/*
* Check generated temporaries for loop's end
*/
static int
bottom_loop_end_check ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
emit_line_note (input_filename, lineno);
/* now, generate code to check each loop counter for termination */
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_FOREVER:
case DO_WHILE:
break;
case DO_STEP:
case DO_RANGE:
case DO_LOC:
case DO_LOC_VARYING:
/* decrement iteration counter by one */
chill_expand_assignment (ip->iter_var, MINUS_EXPR, integer_one_node);
/* exit if it's zero */
expand_exit_loop_if_false (0,
build (NE_EXPR, boolean_type_node,
ip->iter_var,
integer_zero_node));
break;
case DO_POWERSET:
break;
default:
;
}
}
return firstp->error_flag;
}
/*
* increment the loop-control variables.
*/
static int
increment_temps ()
{
ITERATOR *firstp = loop_stack->iter_list, *ip;
for (ip = firstp; ip != NULL; ip = ip->next)
{
switch (ip->itype)
{
case DO_FOREVER:
case DO_WHILE:
break;
case DO_STEP:
case DO_RANGE:
{
tree delta =
fold (build (ip->down_flag ? MINUS_EXPR : PLUS_EXPR,
TREE_TYPE (ip->user_var), ip->user_var,
ip->step_temp));
expand_expr_stmt (
build_chill_modify_expr (ip->user_var, delta));
}
break;
case DO_LOC:
case DO_LOC_VARYING:
/* This statement uses the C semantics, so that
the pointer is actually incremented by the
length of the object pointed to. */
#if 1
expand_expr_stmt (
build_modify_expr (ip->loc_ptr_temp,
ip->down_flag ? MINUS_EXPR : PLUS_EXPR,
integer_one_node));
#else
{
enum tree_code op = ip->down_flag ? MINUS_EXPR : PLUS_EXPR;
tree el_type = TREE_TYPE (TREE_TYPE (ip->loc_ptr_temp));
chill_expand_assignment (ip->loc_ptr_temp, NOP_EXPR,
build (op,
TREE_TYPE (ip->loc_ptr_temp),
ip->loc_ptr_temp,
size_in_bytes (el_type)));
}
#endif
break;
case DO_POWERSET:
break;
default:
;
}
}
return firstp->error_flag;
}
/*
* Generate a (temporary) unique identifier_node of
* the form "__tmp_%s_%d"
*/
tree
get_unique_identifier (lead)
char *lead;
{
char idbuf [256];
static int idcount = 0;
sprintf (idbuf, "__tmp_%s_%d", lead ? lead : "", idcount++);
return get_identifier (idbuf);
}
/*
* build a temporary variable, given its NAME and TYPE.
* The name will have a number appended to assure uniqueness.
* return its DECL node.
*/
static tree
build_temporary_variable (name, type)
char *name;
tree type;
{
return decl_temp1 (get_unique_identifier (name), type, 0, NULL_TREE, 0, 0);
}
/*
* If the given expression isn't a constant, build a temp for it
* and evaluate the expression into the temp. Return the tree
* representing either the original constant expression or the
* temp which now contains the expression's value.
*/
static tree
maybe_make_for_temp (exp, temp_name, exp_type)
tree exp;
char *temp_name;
tree exp_type;
{
tree result = exp;
if (exp != NULL_TREE)
{
/* if exp isn't constant, create a temporary for its value */
if (TREE_CONSTANT (exp))
{
/* FIXME: assure that TREE_TYPE (result) == ip->exp_type */
result = convert (exp_type, exp);
}
else {
/* build temp, assign the value */
result = decl_temp1 (get_unique_identifier (temp_name), exp_type, 0,
exp, 0, 0);
}
}
return result;
}
/*
* Adapt the C unsigned_type function to CHILL - we need to
* account for any CHILL-specific integer types here. So far,
* the 16-bit integer type is the only one.
*/
static tree
chill_unsigned_type (type)
tree type;
{
extern tree chill_unsigned_type_node;
tree type1 = TYPE_MAIN_VARIANT (type);
if (type1 == chill_integer_type_node)
return chill_unsigned_type_node;
else
return unsigned_type (type);
}
|