-
Notifications
You must be signed in to change notification settings - Fork 6
/
parser.yy
1931 lines (1747 loc) · 66.2 KB
/
parser.yy
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 (C) 2008 University of Southern California.
Copyright (C) 2009-2010 University of Utah.
All Rights Reserved.
Purpose:
CHiLL script yacc parser
Notes:
History:
01/2008 created by Chun Chen
*****************************************************************************/
%{
/* Substitute the variable and function names. */
#define yyparse zzparse
#define yylex zzlex
#define yyerror zzerror
#define yylval zzlval
#define yychar zzchar
#define yydebug zzdebug
#define yynerrs zznerrs
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include "parser.hh"
#include "parser.tab.hh"
#include <omega.h>
#include "ir_code.hh"
#include "loop.hh"
// should these be mutually exclusive?
#ifdef FRONTEND_ROSE
#include "ir_rose.hh"
#endif
#ifdef FRONTEND_CLANG
#include "ir_clang.hh"
#endif
using namespace omega;
extern int yydebug;
void yyerror(const char *);
int yylex();
extern int yylex(); // ??
// ?? yyFlexLexer lexer;
namespace {
enum COMPILER_FRONT_END {FE_NULL, FE_SUIF, FE_ROSE, FE_CLANG, FE_GNU}; // a historical summary. SUIF is NO LONGER VALID
COMPILER_FRONT_END frontend_compiler = FE_NULL;
char *source_filename = NULL;
char *dest_filename = NULL;
#if defined(FRONTEND_ROSE) || defined(FRONTEND_CLANG)
char* procedure_name = NULL; // used by Rose and Clang
#endif
int loop_num_start, loop_num_end;
Loop *myloop = NULL; // the one loop we're currently modifying
}
#define PRINT_ERR_LINENO {if (is_interactive) debug_fprintf(stderr, "\n"); else debug_fprintf(stderr, " at line %d\n", lexer.lineno()-1);}
std::map<std::string, int> parameter_tab;
bool is_interactive;
const char *PROMPT_STRING = ">>>";
IR_Code *ir_code = NULL;
std::vector<IR_Control *> ir_controls;
std::vector<int> loops;
%}
%union {
int val;
float fval;
bool bool_val;
char *name;
std::vector<int> *vec;
std::vector<std::string> *string_vec;
std::vector<std::vector<int> > *mat;
std::map<std::string, int> *tab;
std::vector<std::map<std::string, int> > *tab_lst;
std::pair<std::vector<std::map<std::string, int> >, std::map<std::string, int> > *eq_term_pair;
}
%token <val> NUMBER LEVEL
%token <bool_val> TRUEORFALSE
%token <name> FILENAME PROCEDURENAME VARIABLE FREEVAR STRING
%token SOURCE DEST PROCEDURE FORMAT LOOP PERMUTE ORIGINAL TILE UNROLL
%token FIND_STENCIL_SHAPE STENCIL_TEMP
%token SPLIT UNROLL_EXTRA REDUCE SPLIT_WITH_ALIGNMENT
%token PRAGMA PREFETCH
%token DATACOPY DATACOPY_PRIVATIZED
%token FLATTEN SCALAR_EXPAND NORMALIZE ELLIFY COMPACT MAKE_DENSE SET_ARRAY_SIZE
%token NONSINGULAR EXIT KNOWN SKEW SHIFT SHIFT_TO
%token FUSE DISTRIBUTE REMOVE_DEP SCALE REVERSE PEEL
%token STRIDED COUNTED NUM_STATEMENT CEIL FLOOR
%token PRINT PRINT_CODE PRINT_DEP PRINT_IS PRINT_STRUCTURE ADD_GHOSTS
%token NE LE GE EQ
%type <vec> vector vector_number
%type <string_vec> vector_string string_vector
/* TODO: %type <eq_term_pair> cond_term cond */
%type <tab> cond_term
%type <tab_lst> cond
%type <mat> matrix matrix_part
%type <val> expr
%type <fval> float_expr
%destructor {delete []$$; } FILENAME VARIABLE FREEVAR
%destructor {delete $$; } vector vector_number cond_term cond matrix matrix_part
%left '>' '<' NE LE GE
%left '+' '-'
%left '*' '/'
%left '%'
%left UMINUS
%%
script : /* empty */
| script command
;
vector : '[' vector_number ']' {$$ = $2;}
;
vector_number : {$$ = new std::vector<int>();}
| expr {$$ = new std::vector<int>(); $$->push_back($1);}
| vector_number ',' expr {$$ = $1; $$->push_back($3);}
;
vector_string : '[' string_vector ']' {$$ = $2;}
;
string_vector : {$$ = new std::vector<std::string>();}
| VARIABLE {$$ = new std::vector<std::string>(); $$->push_back($1);}
| string_vector ',' VARIABLE {$$ = $1; $$->push_back($3);}
;
matrix: '[' matrix_part ']' {$$ = $2;}
matrix_part : vector {$$ = new std::vector<std::vector<int> >(); $$->push_back(*$1); delete $1;}
| matrix_part ',' vector {$$ = $1; $$->push_back(*$3); delete $3;}
expr : NUMBER {$$ = $1;}
| VARIABLE {
std::map<std::string, int>::iterator it = parameter_tab.find(std::string($1));
if (it != parameter_tab.end()) {
$$ = it->second;
delete []$1;
}
else {
if (is_interactive)
debug_fprintf(stderr, "variable \"%s\" undefined\n", $1);
else
debug_fprintf(stderr, "variable \"%s\" undefined at line %d\n", $1, lexer.lineno());
delete []$1;
if (!is_interactive)
exit(2);
}
}
| NUM_STATEMENT '(' ')' {
if (myloop == NULL)
$$ = 0;
else
$$ = myloop->num_statement();
}
| CEIL '(' float_expr ')' {
$$ = ceil($3);
}
| FLOOR '(' float_expr ')' {
$$ = floor($3);
}
| '(' expr ')' {$$ = $2;}
| expr '-' expr {$$ = $1-$3;}
| expr '+' expr {$$ = $1+$3;}
| expr '*' expr {$$ = $1*$3;}
| expr '/' expr {$$ = $1/$3;}
| '-' expr %prec UMINUS {$$ = -$2;}
;
float_expr : NUMBER {$$ = $1;}
| VARIABLE {
std::map<std::string, int>::iterator it = parameter_tab.find(std::string($1));
if (it != parameter_tab.end()) {
$$ = it->second;
delete []$1;
}
else {
if (is_interactive)
debug_fprintf(stderr, "variable \"%s\" undefined\n", $1);
else
debug_fprintf(stderr, "variable \"%s\" undefined at line %d\n", $1, lexer.lineno());
delete []$1;
if (!is_interactive)
exit(2);
}
}
| NUM_STATEMENT '(' ')' {
if (myloop == NULL)
$$ = 0;
else
$$ = myloop->num_statement();
}
| CEIL '(' float_expr ')' {
$$ = ceil($3);
}
| FLOOR '(' float_expr ')' {
$$ = floor($3);
}
| '(' float_expr ')' {$$ = $2;}
| float_expr '-' float_expr {$$ = $1-$3;}
| float_expr '+' float_expr {$$ = $1+$3;}
| float_expr '*' float_expr {$$ = $1*$3;}
| float_expr '/' float_expr {$$ = $1/$3;}
| '-' float_expr %prec UMINUS {$$ = -$2;}
;
cond : cond_term GE cond_term {
for (std::map<std::string, int>::iterator it = $3->begin(); it != $3->end(); it++)
(*$1)[it->first] -= it->second;
$$ = new std::vector<std::map<std::string, int> >();
$$->push_back(*$1);
delete $1;
delete $3;
}
| cond_term '>' cond_term {
debug_fprintf(stderr, "cond_term '>' cond_term\n");
for (std::map<std::string, int>::iterator it = $3->begin(); it != $3->end(); it++) {
debug_fprintf(stderr, "first %s second %d\n", it->first.c_str(), it->second);
(*$1)[it->first] -= it->second;
}
$$ = new std::vector<std::map<std::string, int> >();
(*$1)[to_string(0)] -= 1;
$$->push_back(*$1);
delete $1;
delete $3;
}
| cond_term '<' cond_term {
for (std::map<std::string, int>::iterator it = $1->begin(); it != $1->end(); it++)
(*$3)[it->first] -= it->second;
$$ = new std::vector<std::map<std::string, int> >();
(*$3)[to_string(0)] -= 1;
$$->push_back(*$3);
delete $1;
delete $3;
}
| cond_term LE cond_term {
for (std::map<std::string, int>::iterator it = $1->begin(); it != $1->end(); it++)
(*$3)[it->first] -= it->second;
$$ = new std::vector<std::map<std::string, int> >();
$$->push_back(*$3);
delete $1;
delete $3;
}
| cond_term EQ cond_term {
for (std::map<std::string, int>::iterator it = $3->begin(); it != $3->end(); it++)
(*$1)[it->first] -= it->second;
$$ = new std::vector<std::map<std::string, int> >();
$$->push_back(*$1);
for (std::map<std::string, int>::iterator it = $1->begin(); it != $1->end(); it++)
it->second = -it->second;
$$->push_back(*$1);
delete $1;
delete $3;
}
;
cond_term : NUMBER {$$ = new std::map<std::string, int>(); (*$$)[to_string(0)] = $1;}
| LEVEL {$$ = new std::map<std::string, int>(); (*$$)[to_string($1)] = 1;}
| VARIABLE {
$$ = new std::map<std::string, int>();
std::map<std::string, int>::iterator it = parameter_tab.find(std::string($1));
if (it != parameter_tab.end())
(*$$)[to_string(0)] = it->second;
else
(*$$)[std::string($1)] = 1;
delete []$1;
}
| '(' cond_term ')' {$$ = $2;}
| cond_term '-' cond_term {
for (std::map<std::string, int>::iterator it = $3->begin(); it != $3->end(); it++)
(*$1)[it->first] -= it->second;
$$ = $1;
delete $3;
}
| cond_term '+' cond_term {
for (std::map<std::string, int>::iterator it = $3->begin(); it != $3->end(); it++)
(*$1)[it->first] += it->second;
$$ = $1;
delete $3;
}
| cond_term '*' cond_term {
(*$1)[to_string(0)] += 0;
(*$3)[to_string(0)] += 0;
if ($1->size() == 1) {
int t = (*$1)[to_string(0)];
for (std::map<std::string, int>::iterator it = $3->begin(); it != $3->end(); it++)
it->second *= t;
$$ = $3;
delete $1;
}
else if ($3->size() == 1) {
int t = (*$3)[to_string(0)];
for (std::map<std::string, int>::iterator it = $1->begin(); it != $1->end(); it++)
it->second *= t;
$$ = $1;
delete $3;
}
else {
if (is_interactive)
debug_fprintf(stderr, "require Presburger formula\n");
else
debug_fprintf(stderr, "require Presburger formula at line %d\n", lexer.lineno());
delete $1;
delete $3;
exit(2);
}
}
| '-' cond_term %prec UMINUS {
for (std::map<std::string, int>::iterator it = $2->begin(); it != $2->end(); it++)
it->second = -(it->second);
$$ = $2;
}
;
command : '\n' { if (is_interactive) printf("%s ", PROMPT_STRING); }
| error '\n' { if (!is_interactive) exit(2); else printf("%s ", PROMPT_STRING); }
| SOURCE ':' FILENAME '\n' {
if (source_filename != NULL) {
debug_fprintf(stderr, "only one file can be handled in a script");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
//debug_fprintf(stderr, "source: %s\n", std::string($3).c_str());
source_filename = $3;
if (is_interactive)
printf("%s ", PROMPT_STRING);
}
| DEST ':' FILENAME '\n' {
if (dest_filename != NULL) {
debug_fprintf(stderr, "only one destination file can be handled in a script");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
//debug_fprintf(stderr, "dest: %s\n", std::string($3).c_str());
dest_filename = $3;
if (is_interactive)
printf("%s ", PROMPT_STRING);
}
| PROCEDURE ':' VARIABLE '\n' {
#if defined(FRONTEND_ROSE) || defined(FRONTEND_CLANG)
if (procedure_name != NULL) {
debug_fprintf(stderr, "only one procedure can be handled in a script");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
procedure_name = $3;
//debug_fprintf(stderr, "procedure is %s\n", procedure_name);
if (is_interactive)
printf("%s ", PROMPT_STRING);
#else
debug_fprintf(stderr, "Please configure IR type to ROSE or CLANG!!: procedure name for ROSE or CLANG/LLVM");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#endif
}
| PROCEDURE ':' NUMBER '\n' { // this works only for SUIF (no longer supported)
#if defined(FRONTEND_ROSE) || defined(FRONTEND_CLANG)
debug_fprintf(stderr, "Please specify procedure's name and not number!!");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#else
debug_fprintf(stderr, "Please configure FRONTEND to ROSE or CLANG!!");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#endif
}
| FORMAT ':' FILENAME '\n' { // TODO this is really front end, not IR format
debug_fprintf(stderr, "format: %s\n", std::string($3).c_str());
if (frontend_compiler != FE_NULL) {
debug_fprintf(stderr, "compiler intermediate format already specified");
PRINT_ERR_LINENO;
delete []$3;
if (!is_interactive)
exit(2);
}
else {
//debug_fprintf(stderr, "format %s\n", std::string($3).c_str());
if(std::string($3) == "rose" || std::string($3) == "ROSE") {
frontend_compiler = FE_ROSE;
delete []$3;
}
else if(std::string($3) == "clang" || std::string($3) == "CLANG") {
frontend_compiler = FE_CLANG;
delete []$3;
}
else {
debug_fprintf(stderr, "unrecognized front end compiler. pick rose or clang\n");
PRINT_ERR_LINENO;
delete []$3;
if (!is_interactive)
exit(2);
}
}
if (is_interactive)
printf("%s ", PROMPT_STRING);
}
| LOOP ':' NUMBER '\n' {
if (source_filename == NULL) {
debug_fprintf(stderr, "source file not set when initializing the loop");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
else {
if (ir_code == NULL) {
//debug_fprintf(stderr, "LOOP ':' NUMBER parse the file because we haven't yet\n");
/* parse the file because we haven't yet */
#if defined(FRONTEND_ROSE) || defined(FRONTEND_CLANG)
if (procedure_name == NULL)
procedure_name = "main";
#else
debug_fprintf(stderr, "Please configure FRONTEND to ROSE or CLANG!!");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#endif
switch (frontend_compiler) {
case FE_CLANG:
#ifdef FRONTEND_CLANG
debug_fprintf(stderr, "clang parsing the file\n");
ir_code = new IR_clangCode(source_filename, procedure_name);
#else
debug_fprintf(stderr, "CLANG front end not installed");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#endif
break;
case FE_ROSE:
//debug_fprintf(stderr, "FE_ROSE\n");
#ifdef FRONTEND_ROSE
//debug_fprintf(stderr, "LOOP ir_code = new IR_roseCode(source_filename, procedure_name);\n");
ir_code = new IR_roseCode(source_filename, procedure_name, dest_filename);
//debug_fprintf(stderr, "LOOP RETURN ir_code = new IR_roseCode(source_filename, procedure_name);\n");
#else
debug_fprintf(stderr, "ROSE IR not installed");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#endif
break;
case FE_NULL:
debug_fprintf(stderr, "compiler IR format not specified");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
break;
}
/* get the code associated with the procedure */
//debug_fprintf(stderr, "parser.yy L504 yyparse block = ir_code->GetCode();\n");
IR_Block *irblock = ir_code->GetCode(); // block that is / enclosing the FUNCTION DEFINITION ??
//debug_fprintf(stderr, "parser.yy L506 irblock from getcode is %p\n", irblock);
//IR_roseblock *rb = (IR_roseblock *)irblock;
//debug_fprintf(stderr, "cheating ast is %p\n", rb->chillAST);
ir_controls = ir_code->FindOneLevelControlStructure(irblock);
for (int i = 0; i < ir_controls.size(); i++) {
if (ir_controls[i]->type() == IR_CONTROL_LOOP)
loops.push_back(i);
}
delete irblock;
}
//debug_fprintf(stderr, "(parser.yy) I found %ld loops in the procedure\n", loops.size());
if (myloop != NULL && myloop->isInitialized()) {
//debug_fprintf(stderr, "there is already a myloop and it is initialized\n");
debug_fprintf(stderr, "have to update <clang/rose/gcc> Intermediate Representation before changing the next loop\n");
/* we are processing multiple loops. replace the <clang/rose/gcc> IR
for loops we've already changed (?) */
if (loop_num_start == loop_num_end) {
/* we changed one loop before (?) */
//debug_fprintf(stderr, "replacing code for a single loop in <clang/rose/gcc> IR\n");
ir_code->ReplaceCode(ir_controls[loops[loop_num_start]], myloop->getCode());
ir_controls[loops[loop_num_start]] = NULL;
}
else {
/* we changed multiple loops before (?) */
//debug_fprintf(stderr, "replacing code for multiple loops in <clang/rose/gcc> IR\n");
std::vector<IR_Control *> parm;
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++)
parm.push_back(ir_controls[i]);
IR_Block *block = ir_code->MergeNeighboringControlStructures(parm);
ir_code->ReplaceCode(block, myloop->getCode());
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++) {
delete ir_controls[i];
ir_controls[i] = NULL;
}
}
//debug_fprintf(stderr, "deleting old myloop\n");
delete myloop;
myloop = NULL;
}
loop_num_start = loop_num_end = $3;
if (loop_num_start >= loops.size()) {
debug_fprintf(stderr, "loop %d does not exist", loop_num_start);
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
if (ir_controls[loops[loop_num_start]] == NULL) {
debug_fprintf(stderr, "loop %d has already been transformed", loop_num_start);
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
//debug_fprintf(stderr, "\nparse.yy L 505 making a new myloop loop num start %d\n",loop_num_start ) ;
//debug_fprintf(stderr, "\n*** ROSE (parser.yy) making a new myloop\n");
myloop = new Loop(ir_controls[loops[loop_num_start]]);
//debug_fprintf(stderr, "\nparse.yy L 559, made a new loop\n");
//debug_fprintf(stderr, "\n(start dump of loop I'm about to change )\n");
// myloop->dump(); fflush(stdout); debug_fprintf(stderr, "(end dump)\n\n(start printcode)\n");
//// this dies TODO myloop->printCode(); fflush(stdout);
//debug_fprintf(stderr, "(end printcode)\n\n(internal loop strucuure)\n");
//myloop->print_internal_loop_structure(); fflush(stdout);
//debug_fprintf(stderr, "\n(iteration space)\n");
//myloop->printIterationSpace(); fflush(stdout);
//debug_fprintf(stderr, "\n");
}
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| LOOP ':' NUMBER '-' NUMBER '\n' {
/* modify a range of loops */
//debug_fprintf(stderr, "loop: a-b\n");
if (source_filename == NULL) {
debug_fprintf(stderr, "source file not set when initializing the loop");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
else {
if (ir_code == NULL) {
if (procedure_name == NULL)
procedure_name = "main";
switch (frontend_compiler) {
case FE_ROSE:
#ifdef FRONTEND_ROSE
ir_code = new IR_roseCode(source_filename, procedure_name, dest_filename);
#else
debug_fprintf(stderr, "ROSE IR not installed");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
#endif
break;
// todo case FE_CLANG
case FE_NULL:
debug_fprintf(stderr, "compiler IR format not specified");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
break;
}
IR_Block *block = ir_code->GetCode();
//debug_fprintf(stderr, "calling FindOneLevelControlStructure for loop: a-b\n");
ir_controls = ir_code->FindOneLevelControlStructure(block);
for (int i = 0; i < ir_controls.size(); i++)
if (ir_controls[i]->type() == IR_CONTROL_LOOP)
loops.push_back(i);
delete block;
}
if (myloop != NULL && myloop->isInitialized()) {
if (loop_num_start == loop_num_end) {
ir_code->ReplaceCode(ir_controls[loops[loop_num_start]], myloop->getCode());
ir_controls[loops[loop_num_start]] = NULL;
}
else {
std::vector<IR_Control *> parm;
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++)
parm.push_back(ir_controls[i]);
IR_Block *block = ir_code->MergeNeighboringControlStructures(parm);
ir_code->ReplaceCode(block, myloop->getCode());
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++) {
delete ir_controls[i];
ir_controls[i] = NULL;
}
}
delete myloop;
}
loop_num_start = $3;
loop_num_end = $5;
if ($5 < $3) {
debug_fprintf(stderr, "the last loop must be after the start loop");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
if (loop_num_end >= loops.size()) {
debug_fprintf(stderr, "loop %d does not exist", loop_num_end);
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
std::vector<IR_Control *> parm;
for (int i = loops[loop_num_start]; i <= loops[loop_num_end]; i++) {
if (ir_controls[i] == NULL) {
debug_fprintf(stderr, "loop has already been processed");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
parm.push_back(ir_controls[i]);
}
IR_Block *block = ir_code->MergeNeighboringControlStructures(parm);
myloop = new Loop(block);
delete block;
}
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| PRINT '\n' {
//printf("\n*** parser: print\n"); fflush(stdout);
if (myloop == NULL) {
debug_fprintf(stderr, "loop not initialized");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
else {
myloop->printCode();
}
if (is_interactive) printf("%s ", PROMPT_STRING); else printf("\n");
}
| PRINT PRINT_CODE '\n' {
if (myloop == NULL) {
debug_fprintf(stderr, "loop not initialized");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
else {
if (!is_interactive) {
if (loop_num_start != loop_num_end)
std::cout << "/* procedure :" << procedure_name << " loop #" << loop_num_start << "-" << loop_num_end << " */" << std::endl;
else
std::cout << "/* procedure :" << procedure_name << " loop #" << loop_num_start << " */" << std::endl;
}
myloop->printCode();
}
if (is_interactive) printf("%s ", PROMPT_STRING); else printf("\n");
}
| PRINT PRINT_DEP '\n' {
if (myloop == NULL) {
debug_fprintf(stderr, "loop not initialized");
PRINT_ERR_LINENO;
if (!is_interactive)
YYABORT;
}
else {
myloop->printDependenceGraph();
}
if (is_interactive) printf("%s ", PROMPT_STRING); else printf("\n");
}
| PRINT PRINT_IS '\n' {
if (myloop == NULL) {
debug_fprintf(stderr, "loop not initialized");
PRINT_ERR_LINENO;
if (!is_interactive)
YYABORT;
}
else {
myloop->printIterationSpace();
}
if (is_interactive) printf("%s ", PROMPT_STRING); else printf("\n");
}
| PRINT PRINT_STRUCTURE '\n' {
if (myloop == NULL) {
debug_fprintf(stderr, "loop not initialized");
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
else {
myloop->print_internal_loop_structure();
}
if (is_interactive) printf("%s ", PROMPT_STRING); else printf("\n");
}
| PRINT expr '\n' {
/* if (parameter_tab.find(std::string($2)) == parameter_tab.end()) {
debug_fprintf(stderr, "cannot print undefined variable %s\n", $2);
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
std::cout << parameter_tab[std::string($2)] << std::endl;
*/
std::cout << $2 << std::endl;
if (is_interactive) printf("%s ", PROMPT_STRING); else printf("\n");
}
| EXIT '\n' { return(0); }
| VARIABLE '=' expr '\n' {
parameter_tab[std::string($1)] = $3;
delete []$1;
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| KNOWN '(' cond ')' '\n' {
fflush(stdout);
debug_fprintf(stderr, "\nKNOWN\n");
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
debug_fprintf(stderr, "KNOWN myloop (that's a good thing)\n");
int num_dim = myloop->known.n_set();
debug_fprintf(stderr, "num_dim %d\n", num_dim);
Relation rel(num_dim);
F_And *f_root = rel.add_and();
int limit = $3->size();
for (int j = 0; j < $3->size(); j++) {
debug_fprintf(stderr, "j %d/%d\n", j,limit );
GEQ_Handle h = f_root->add_GEQ();
int icount = 0;
for (std::map<std::string, int>::iterator it = (*$3)[j].begin(); it != (*$3)[j].end(); it++) {
debug_fprintf(stderr, "icount %d\n", icount++);
debug_fprintf(stderr, "it->first %s\n", it->first.c_str());
debug_fprintf(stderr, "it->second %d\n", it->second);
}
debug_fprintf(stderr, "\n\nAGAIN\n");
icount = 0;
for (std::map<std::string, int>::iterator it = (*$3)[j].begin(); it != (*$3)[j].end(); it++) {
debug_fprintf(stderr, "icount %d\n", icount++);
debug_fprintf(stderr, "it->first %s\n", it->first.c_str());
debug_fprintf(stderr, "it->second %d\n", it->second);
//try {
debug_fprintf(stderr, "about to try and interpret '%s' as an integer\n", it->first.c_str());
// this function is onlyin c++11
//int dim = std::stoi(it->first); // this is supposed to be caught if the arg is a variable name
//this returns 0 if the beginning of the string is not a number
//std::istringstream(it->first) >> dim;
if ("0" == it->first) {
int dim = 0; // weak
//int dim from_string<int>(it->first); // this is supposed to be caught if the arg is a variable name
debug_fprintf(stderr, "interpreted as the number %d\n", dim);
if (dim == 0)
h.update_const(it->second);
else {
debug_fprintf(stderr, "wait, what?\n");
throw std::invalid_argument("only symbolic variables are allowed in known condition");
}
}
else { //
//catch (std::ios::failure e) {
debug_fprintf(stderr, "caught a failure\n");
Free_Var_Decl *g = NULL;
debug_fprintf(stderr, "%d freevars\n", myloop->freevar.size());
debug_fprintf(stderr, "it->first %s\n", it->first.c_str());
for (unsigned i = 0; i < myloop->freevar.size(); i++) {
debug_fprintf(stderr, "var %d ", i);
std::string name = myloop->freevar[i]->base_name();
debug_fprintf(stderr, "%s\n", name.c_str());
if (name == it->first) {
g = myloop->freevar[i];
break;
}
}
if (g == NULL) {
debug_fprintf(stderr, "parser, g NULL\n");
throw std::invalid_argument("symbolic variable " + it->first + " not found");
}
else
h.update_coef(rel.get_local(g), it->second);
//}
} // else
//}
}
}
myloop->addKnown(rel);
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive) {
delete $3;
exit(2);
}
}
delete $3;
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| REMOVE_DEP '(' NUMBER ',' NUMBER ')' '\n' {
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
myloop->removeDependence($3, $5);
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive)
YYABORT;
}
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| ORIGINAL '(' ')' '\n' {
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
myloop->original();
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| PERMUTE '(' vector ')' '\n' {
//debug_fprintf(stderr, "\n\n\n*** PERMUTE\n");
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
myloop->permute(*$3);
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive) {
delete $3;
exit(2);
}
}
delete $3;
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| PERMUTE '(' expr ',' NUMBER ',' vector ')' '\n' {
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
myloop->permute($3, $5, *$7);
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive) {
delete $7;
exit(2);
}
}
delete $7;
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| PERMUTE '(' vector ',' vector ')' '\n' {
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
std::set<int> active;
for (int i = 0; i < (*$3).size(); i++)
active.insert((*$3)[i]);
myloop->permute(active, *$5);
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive) {
delete $3;
delete $5;
exit(2);
}
}
delete $3;
delete $5;
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| PRAGMA '(' NUMBER ',' NUMBER ',' STRING ')' '\n' {
myloop->pragma($3,$5,$7);
}
| PREFETCH '(' NUMBER ',' NUMBER ',' STRING ',' expr ')' '\n' {
myloop->prefetch($3, $5, $7, $9);
}
| TILE '(' expr ',' NUMBER ',' expr ')' '\n' {
//debug_fprintf(stderr, "TILE (3)\n");
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
myloop->tile($3,$5,$7);
}
catch (const std::exception &e) {
debug_fprintf(stderr, e.what());
PRINT_ERR_LINENO;
if (!is_interactive)
exit(2);
}
if (is_interactive) printf("%s ", PROMPT_STRING);
}
| FIND_STENCIL_SHAPE '(' NUMBER ')' {
printf("\n*** parser: find stencil shape\n"); fflush(stdout);
myloop->find_stencil_shape( $3 );
}
| STENCIL_TEMP '(' NUMBER ')' {
printf("\n*** parser: protonu's stencil\n"); fflush(stdout);
myloop->stencilASEPadded( $3 );
printf("\n*** parser: protonu's stencil DONE\n"); fflush(stdout);
}
| FLATTEN '(' NUMBER ',' VARIABLE ',' vector ',' VARIABLE ')' '\n' {
try {
if (myloop == NULL)
throw std::runtime_error("loop not initialized");
std::vector<int> loop_levels;
for (int i = 0; i < (*$7).size(); i++)