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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
|
/*****************************************************************************
Copyright (C) 2008 University of Southern California
Copyright (C) 2009-2010 University of Utah
All Rights Reserved.
Purpose:
generate chill AST code for omega
Notes:
History:
02/01/06 created by Chun Chen
*****************************************************************************/
#include <iostream>
#include <stack>
#include <code_gen/CG_chillBuilder.h>
#include <code_gen/CGdebug.h>
#include <code_gen/codegen_error.h>
namespace omega {
// substitute at chill AST level
chillAST_Node *substituteChill( const char *oldvar, CG_chillRepr *newvar, chillAST_Node *n, chillAST_Node *parent = NULL ) {
if (n == NULL) {
CG_ERROR(" pointer n == NULL\n");
exit(-1);
}
chillAST_Node *r = n;
switch (n->getType()) {
case CHILLAST_NODE_BINARYOPERATOR:
case CHILLAST_NODE_UNARYOPERATOR:
case CHILLAST_NODE_FORSTMT:
case CHILLAST_NODE_IFSTMT:
case CHILLAST_NODE_COMPOUNDSTMT:
case CHILLAST_NODE_RETURNSTMT:
case CHILLAST_NODE_PARENEXPR:
case CHILLAST_NODE_CALLEXPR:
case CHILLAST_NODE_IMPLICITCASTEXPR:
case CHILLAST_NODE_CSTYLECASTEXPR:
case CHILLAST_NODE_ARRAYSUBSCRIPTEXPR:
case CHILLAST_NODE_MEMBEREXPR:
for (int i=0;i<n->getNumChildren();++i)
n->setChild(i,substituteChill(oldvar, newvar,n->getChild(i)));
break;
case CHILLAST_NODE_DECLREFEXPR: {
chillAST_DeclRefExpr *DRE = (chillAST_DeclRefExpr *) n;
if (!strcmp( oldvar, DRE->declarationName)) {
std::vector<chillAST_Node*> newnodes = newvar->chillnodes;
chillAST_Node *firstn = newnodes[0]->clone();
r = firstn;
}
}
break;
case CHILLAST_NODE_INTEGERLITERAL:
case CHILLAST_NODE_FLOATINGLITERAL:
// No op
break;
default:
CHILL_ERROR("UNHANDLED statement of type %s %s\n",n->getTypeString());
exit(-1);
}
return r;
}
CG_chillBuilder::CG_chillBuilder() {
toplevel = NULL;
currentfunction = NULL; // not very useful
symtab_ = symtab2_ = NULL;
}
CG_chillBuilder::CG_chillBuilder(chillAST_SourceFile *top, chillAST_FunctionDecl *func) {
toplevel = top;
currentfunction = func;
symtab_ = currentfunction->getParameters();
symtab2_ = currentfunction->getBody()->getSymbolTable();
}
CG_chillBuilder::~CG_chillBuilder() {
}
//-----------------------------------------------------------------------------
// place holder generation NOT IN CG_outputBuilder
//-----------------------------------------------------------------------------
//
// FIXME: Function isn't working fully yet
//
CG_outputRepr* CG_chillBuilder::CreatePlaceHolder (int indent,
CG_outputRepr *stmt,
Tuple<CG_outputRepr*> &funcList,
Tuple<std::string> &loop_vars) const {
fprintf(stderr, "CG_chillBuilder::CreatePlaceHolder() TODO \n");
exit(-1); // DFL
return NULL;
/*
if(Expr *expr = static_cast<CG_chillRepr *>(stmt)->GetExpression()) {
for(int i=1; i<= funcList.size(); ++i) {
if (funcList[i] == NULL)
continue;
CG_chillRepr *repr = static_cast<CG_chillRepr*>(funcList[i]);
Expr* op = repr->GetExpression();
delete repr;
Expr *exp = expr;
if(isa<BinaryOperator>(exp)) {
//substitute(static_cast<BinaryOperator*>(exp)->getLHS(), loop_vars[i], op, exp);
//substitute(static_cast<BinaryOperator*>(exp)->getRHS(), loop_vars[i], op, exp);
}
else if(isa<UnaryOperator>(exp))
//substitute(static_cast<UnaryOperator*>(exp)->getSubExpr(), loop_vars[i], op, exp);
}
return new CG_chillRepr(expr);
} else {
StmtList *tnl = static_cast<CG_chillRepr *>(stmt)->GetCode();
for(int i=1; i<= funcList.size(); ++i) {
if (funcList[i] == NULL)
continue;
CG_chillRepr *repr = static_cast<CG_chillRepr*>(funcList[i]);
Expr* op = repr->GetExpression();
delete repr;
for(unsigned j=0; j<tnl->size(); ++j) {
Expr *exp = static_cast<Expr *>((*tnl)[j]);
if(isa<BinaryOperator>(exp)) {
//substitute(static_cast<BinaryOperator*>(exp)->getLHS(), loop_vars[i], op, exp);
//substitute(static_cast<BinaryOperator*>(exp)->getRHS(), loop_vars[i], op, exp);
}
else if(isa<UnaryOperator>(exp))
//substitute(static_cast<UnaryOperator*>(exp)->getSubExpr(), loop_vars[i], op, exp);
}
}
return new CG_chillRepr(*tnl);
}
*/
}
//----------------------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateSubstitutedStmt(int indent,
CG_outputRepr *stmt,
const std::vector<std::string> &vars,
std::vector<CG_outputRepr*> &subs,
bool actuallyPrint) const {
int numvars = vars.size();
int numsubs = subs.size();
if (numvars != numsubs)
throw codegen_error("Unequal number of original vars and subs");
if (numsubs == 0) {
std::vector<chillAST_Node*> nodes = ((CG_chillRepr *) stmt)->getChillCode();
// no cloning !!
return new CG_chillRepr( nodes );
}
CG_chillRepr *old = (CG_chillRepr *) stmt;
std::vector<chillAST_Node*> oldnodes = old->getChillCode();
for (int j=0; j<numsubs; j++) {
if (subs[j] != NULL) {
// find the type of thing we'll be using to replace the old variable
CG_chillRepr *CRSub = (CG_chillRepr *)(subs[j]);
std::vector<chillAST_Node*> nodes = CRSub->chillnodes;
if (1 != nodes.size() ) { // always just one?
CG_ERROR("Replacement is not one statement\n");
exit(-1);
}
for (int i=0; i<oldnodes.size(); i++) {
oldnodes[i] = substituteChill( vars[j].c_str(), CRSub, oldnodes[i]);
}
}
}
return new CG_chillRepr( oldnodes );
}
CG_outputRepr* CG_chillBuilder::CreateAssignment(int indent,
CG_outputRepr *lhs,
CG_outputRepr *rhs) const {
if(lhs == NULL || rhs == NULL) {
CG_ERROR("Code generation: Missing lhs or rhs\n");
return NULL;
}
CG_chillRepr *clhs = (CG_chillRepr *) lhs;
CG_chillRepr *crhs = (CG_chillRepr *) rhs;
chillAST_Node *lAST = clhs->chillnodes[0]; // always just one?
chillAST_Node *rAST = crhs->chillnodes[0]; // always just one?
chillAST_BinaryOperator *bop = new chillAST_BinaryOperator(lAST->clone(), "=", rAST->clone()); // clone??
delete lhs; delete rhs;
return new CG_chillRepr(bop);
}
CG_outputRepr* CG_chillBuilder::CreatePlusAssignment(int indent, // +=
CG_outputRepr *lhs,
CG_outputRepr *rhs) const {
if(lhs == NULL || rhs == NULL) {
CHILL_ERROR("Code generation: Missing lhs or rhs\n");
return NULL;
}
CG_chillRepr *clhs = (CG_chillRepr *) lhs;
CG_chillRepr *crhs = (CG_chillRepr *) rhs;
chillAST_Node *lAST = clhs->chillnodes[0]; // always just one?
chillAST_Node *rAST = crhs->chillnodes[0]; // always just one?
chillAST_BinaryOperator *bop = new chillAST_BinaryOperator(lAST->clone(), "+=", rAST->clone()); // clone??
delete lhs; delete rhs;
return new CG_chillRepr(bop);
}
//-----------------------------------------------------------------------------
// function invocation generation
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateInvoke(const std::string &fname,
std::vector<CG_outputRepr*> &list) const {
CG_DEBUG_PRINT("CG_roseBuilder::CreateInvoke( fname %s, ...)\n", fname.c_str());
if (fname == std::string("max") || fname == std::string("min")) {
if (list.size() == 0) { return NULL; }
else if (list.size() == 1) { return list[1]; }
else {
int last = list.size()-1;
CG_outputRepr *CGOR;
CG_chillRepr *CGCR;
char macroname[32];
char op;
if (fname == std::string("max")) op = '>';
else op = '<';
// TODO >, check number of args etc
chillAST_Node *ternary = lessthanmacro( ((CG_chillRepr*) list[0])->chillnodes[0],
((CG_chillRepr*) list[1])->chillnodes[0]);
CG_chillRepr *repr = new CG_chillRepr( ternary );
return repr;
}
}
else {
// try to find the function name, for a function in this file
const char *name = fname.c_str();
chillAST_SourceFile *src = toplevel; // todo don't be dumb
chillAST_Node *def = src->findCall(name);
if (!def) {
CG_ERROR("CG_chillBuilder::CreateInvoke( %s ), can't find a function or macro by that name\n", name);
exit(-1);
}
if (def->isMacroDefinition() || def->isFunctionDecl()) {
chillAST_CallExpr *CE = new chillAST_CallExpr( def );
int numparams = list.size();
for (int i=0; i<numparams; i++) {
CG_chillRepr *CR = (CG_chillRepr *) list[i];
CE->addArg( CR->GetCode() );
}
return new CG_chillRepr( CE );
}
// todo addarg()
//int numargs;
//std::vector<class chillAST_Node*> args;
fprintf(stderr, "Code generation: invoke function io_call not implemented\n");
return NULL;
}
}
//-----------------------------------------------------------------------------
// comment generation - NOTE: Not handled
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateComment(int indent, const std::string &commentText) const {
return NULL;
}
CG_outputRepr* CG_chillBuilder::CreateNullStatement() const {
return new CG_chillRepr( new chillAST_NoOp() );
}
//---------------------------------------------------------------------------
// Attribute generation
//---------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateAttribute(CG_outputRepr *control,
const std::string &commentText) const {
//fprintf(stderr, "in CG_chillBuilder.cc (OMEGA) CG_chillBuilder::CreateAttribute()\n");
//fprintf(stderr, "comment = '%s'\n", commentText.c_str());
CG_chillRepr *CR = (CG_chillRepr *) control;
int numnodes = CR->chillnodes.size();
//fprintf(stderr, "%d chill nodes\n", numnodes);
if (numnodes > 0) {
//fprintf(stderr, "adding a comment to a %s\n", CR->chillnodes[0]->getTypeString());
CR->chillnodes[0]->metacomment = strdup( commentText.c_str());
}
else {
fprintf(stderr, "CG_chillBuilder::CreateAttribute no chillnodes to attach comment to???\n");
}
return static_cast<CG_chillRepr*>(control);
};
//-----------------------------------------------------------------------------
// if stmt gen operations
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateIf(int indent,
CG_outputRepr *guardList,
CG_outputRepr *true_stmtList,
CG_outputRepr *false_stmtList) const {
//fprintf(stderr, "CG_chillBuilder::CreateIf()\n");
if (true_stmtList == NULL && false_stmtList == NULL) {
delete guardList;
return NULL;
}
else if (guardList == NULL) { // this seems odd
return StmtListAppend(true_stmtList, false_stmtList);
}
std::vector<chillAST_Node*> vectorcode = static_cast<CG_chillRepr*>(guardList)->getChillCode();
if (vectorcode.size() != 1 ) {
fprintf(stderr, "CG_chillBuilder.cc IfStmt conditional is multiple statements?\n");
exit(-1);
}
chillAST_Node *conditional = vectorcode[0];
chillAST_CompoundStmt *then_part = NULL;
chillAST_CompoundStmt *else_part = NULL;
if (true_stmtList != NULL) {
then_part = new chillAST_CompoundStmt( );
vectorcode = static_cast<CG_chillRepr*>(true_stmtList)->getChillCode();
for (int i=0; i<vectorcode.size(); i++) then_part->addChild( vectorcode[i] );
}
if (false_stmtList != NULL) {
else_part = new chillAST_CompoundStmt( );
vectorcode = static_cast<CG_chillRepr*>(false_stmtList)->getChillCode();
for (int i=0; i<vectorcode.size(); i++) else_part->addChild( vectorcode[i] );
}
chillAST_IfStmt *if_stmt = new chillAST_IfStmt( conditional, then_part, else_part);
delete guardList;
delete true_stmtList;
delete false_stmtList;
return new CG_chillRepr( if_stmt );
}
//-----------------------------------------------------------------------------
// inductive variable generation, to be used in CreateLoop as control
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateInductive(CG_outputRepr *index,
CG_outputRepr *lower,
CG_outputRepr *upper,
CG_outputRepr *step) const {
fprintf(stderr, "\nCG_chillBuilder::CreateInductive()\n");
if (index == NULL || lower == NULL || upper == NULL) {
fprintf(stderr, "Code generation: invalid arguments to CreateInductive\n");
return NULL;
}
if (step == NULL) {
//IntegerLiteral *ilit = new (astContext_)IntegerLiteral(*astContext_, llvm::APInt(32, 1), bint->desugar(), SourceLocation());
//step = new CG_chillRepr(ilit);
chillAST_IntegerLiteral *intlit = new chillAST_IntegerLiteral(1);
step = new CG_chillRepr(intlit);
}
//static_cast<CG_chillRepr*>(index)->printChillNodes();
//static_cast<CG_chillRepr*>(lower)->printChillNodes();
//static_cast<CG_chillRepr*>(upper)->printChillNodes();
//static_cast<CG_chillRepr*>(step )->printChillNodes();
// index should be a DeclRefExpr
std::vector<chillAST_Node*> nodes = static_cast<CG_chillRepr*>(index)->getChillCode();
//fprintf(stderr, "%d index nodes\n", nodes.size());
chillAST_Node *indexnode = nodes[0];
if (strcmp("DeclRefExpr", indexnode->getTypeString())) {
fprintf(stderr, "CG_chillBuilder::CreateInductive index is not a DeclRefExpr\n");
if (indexnode->isIntegerLiteral()) fprintf(stderr, "isIntegerLiteral()\n");
fprintf(stderr, "index is %s\n", indexnode->getTypeString());
indexnode->print(); printf("\n"); fflush(stdout);
indexnode->dump(); printf("\n\n"); fflush(stdout);
int *i = 0; int j = i[0];
exit(-1);
}
nodes = static_cast<CG_chillRepr*>(lower)->getChillCode();
//fprintf(stderr, "%d lower nodes\n", nodes.size());
chillAST_Node *lowernode = nodes[0];
//fprintf(stderr, "lower node is %s\n", lowernode->getTypeString());
nodes = static_cast<CG_chillRepr*>(upper)->getChillCode();
//fprintf(stderr, "%d upper nodes\n", nodes.size());
chillAST_Node *uppernode = nodes[0];
//fprintf(stderr, "upper node is %s\n", uppernode->getTypeString());
nodes = static_cast<CG_chillRepr*>(step)->getChillCode();
//fprintf(stderr, "%d step nodes\n", nodes.size());
chillAST_Node *stepnode = nodes[0];
//fprintf(stderr, "step node is %s\n", stepnode->getTypeString());
// unclear is this will always be the same
// TODO error checking && incr vs decr
chillAST_BinaryOperator *init = new chillAST_BinaryOperator( indexnode, "=", lowernode);
chillAST_BinaryOperator *cond = new chillAST_BinaryOperator( indexnode, "<=", uppernode);
chillAST_BinaryOperator *incr = new chillAST_BinaryOperator( indexnode, "+=", stepnode);
chillAST_ForStmt *loop = new chillAST_ForStmt( init, cond, incr, NULL /* NULL BODY DANGER! */);
return new CG_chillRepr(loop);
/*
//vector<chillAST_Node*> indexnodes = static_cast<CG_chillRepr*>(index)->getChillCode();
chillAST_DeclRefExpr *index_decl
Expr *lower_bound; // = static_cast<CG_chillRepr*>(lower)->getChillCode();
Expr *upper_bound; // = static_cast<CG_chillRepr*>(upper)->getChillCode();
Expr *step_size ; // = static_cast<CG_chillRepr*>(step)->getChillCode();
fprintf(stderr, "gonna die in CG_chillBuilder ~line 459\n");
chillAST_BinaryOperator *for_init_stmt = NULL; // new (astContext_)BinaryOperator(index_decl, lower_bound, BO_Assign, index_decl->getType(), SourceLocation());
chillAST_BinaryOperator *test = NULL; // new (astContext_)BinaryOperator(index_decl, upper_bound, BO_LT, index_decl->getType(), SourceLocation());
chillAST_BinaryOperator *increment = NULL; // new (astContext_)BinaryOperator(index_decl, step_size, BO_AddAssign, index_decl->getType(), SourceLocation());
// For Body is null.. Take care of unnecessary parens!
ForStmt *for_stmt = NULL; // new (astContext_)ForStmt(*astContext_, for_init_stmt, test, static_cast<VarDecl*>(index_decl->getDecl()), increment, NULL, SourceLocation(), SourceLocation(), SourceLocation());
delete index;
delete lower;
delete upper;
delete step;
StmtList sl;
sl.push_back(for_stmt);
return new CG_chillRepr(sl);
*/
}
//-----------------------------------------------------------------------------
// Pragma Attribute
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreatePragmaAttribute(CG_outputRepr *stmt, int looplevel, const std::string &pragmaText) const {
fprintf(stderr, "CG_chillBuilder::CreatePragmaAttribute() TODO\n");
exit(-1);
// TODO effectively a comment?
/*
SgNode *tnl = static_cast<CG_chillRepr*>(stmt)->tnl_;
CodeInsertionAttribute* attr = NULL;
if (!tnl->attributeExists("code_insertion")) {
attr = new CodeInsertionAttribute();
tnl->setAttribute("code_insertion", attr);
}
else {
attr = static_cast<CodeInsertionAttribute*>(tnl->getAttribute("code_insertion"));
}
attr->add(new PragmaInsertion(looplevel, pragmaText));
*/
return stmt;
}
//-----------------------------------------------------------------------------
// Prefetch Attribute
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreatePrefetchAttribute(CG_outputRepr* stmt, int looplevel, const std::string &arrName, int hint) const {
fprintf(stderr, "CG_chillBuilder::CreatePrefetchAttribute() TODO\n");
exit(-1);
// TODO
/*
SgNode *tnl = static_cast<CG_chillRepr*>(stmt)->tnl_;
CodeInsertionAttribute *attr = getOrCreateCodeInsertionAttribute(tnl);
attr->add(new MMPrefetchInsertion(looplevel, arrName, hint));
*/
return stmt;
}
//-----------------------------------------------------------------------------
// loop stmt generation
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateLoop(int indent,
CG_outputRepr *control,
CG_outputRepr *stmtList) const {
//fprintf(stderr, "CG_chillBuilder::CreateLoop( indent %d)\n", indent);
if (stmtList == NULL) {
delete control;
return NULL;
}
else if (control == NULL) {
fprintf(stderr, "Code generation: no inductive for this loop\n");
return stmtList;
}
// We assume the for statement is already created (using CreateInductive)
std::vector<chillAST_Node*> code = static_cast<CG_chillRepr*>(control)->getChillCode();
chillAST_ForStmt *forstmt = (chillAST_ForStmt *)(code[0]);
std::vector<chillAST_Node*> statements = static_cast<CG_chillRepr*>(stmtList)->getChillCode();
//static_cast<CG_chillRepr*>(stmtList)->printChillNodes(); printf("\n"); fflush(stdout);
chillAST_CompoundStmt *cs = new chillAST_CompoundStmt();
for (int i=0; i<statements.size(); i++) {
cs->addChild( statements[i] );
}
forstmt->setBody(cs);
delete stmtList;
return control;
}
//---------------------------------------------------------------------------
// copy operation, NULL parameter allowed. this function makes pointer
// handling uniform regardless NULL status
//---------------------------------------------------------------------------
/*
virtual CG_outputRepr* CG_chillBuilder::CreateCopy(CG_outputRepr *original) const {
if (original == NULL)
return NULL;
else
return original->clone();
}
*/
//-----------------------------------------------------------------------------
// basic int, identifier gen operations
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateInt(int i) const {
fprintf(stderr, "CG_chillBuilder::CreateInt( %d )\n",i);
chillAST_IntegerLiteral *il = new chillAST_IntegerLiteral(i); // parent not available
return new CG_chillRepr(il);
}
CG_outputRepr* CG_chillBuilder::CreateFloat(float f) const {
//fprintf(stderr, "CG_chillBuilder::CreateFloat( %f )\n", f);
chillAST_FloatingLiteral *fl = new chillAST_FloatingLiteral(f, 1, NULL); // parent not available
return new CG_chillRepr(fl);
}
CG_outputRepr* CG_chillBuilder::CreateDouble(double d) const {
//fprintf(stderr, "CG_chillBuilder::CreateInt( %f )\n",d);
chillAST_FloatingLiteral *dl = new chillAST_FloatingLiteral(d, 1, NULL); // parent not available
return new CG_chillRepr(dl);
}
//----------------------------------------------------------------------------------------
bool CG_chillBuilder::isInteger(CG_outputRepr *op) const{
CG_chillRepr *cr = (CG_chillRepr *)op;
return cr->chillnodes[0]->isIntegerLiteral();
}
//----------------------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateIdent(const std::string &_s) const {
fprintf(stderr, "CG_chillBuilder::CreateIdent( %s )\n", _s.c_str());
chillAST_VarDecl* already_parameter = symbolTableFindName(symtab_, _s.c_str());
chillAST_VarDecl* already_internal = symbolTableFindName(symtab2_, _s.c_str());
if ( already_parameter ) {
fprintf(stderr, "%s was already a parameter??\n", _s.c_str());
}
if ( already_internal ) {
//fprintf(stderr, "%s was already defined in the function body\n", _s.c_str());
//printSymbolTable(symtab2_); printf("dammit\n"); fflush(stdout);
}
if ( (!already_parameter) && (! already_internal)) {
fprintf(stderr, "CG_roseBuilder.cc L919 adding symbol %s to symtab2_ because it was not already there\n", _s.c_str());
//fprintf(stderr, "parameters were: %p\n", symtab_);
//printSymbolTable( symtab_ );
//fprintf(stderr, "\nbody symbols were: %p\n", symtab2_);
//printSymbolTable( symtab2_ );
//fprintf(stderr, "\n\n");
//fprintf(stderr, "there were already %d entries in body\n", symtab2_->size());
// this is copying roseBuilder, but is probably wrong. it is assuming
// that the ident is a direct child of the current function
chillAST_VarDecl *vd = new chillAST_VarDecl( "int", _s.c_str(), "", currentfunction->getBody()); // parent not available TODO
currentfunction->addVariableToScope( vd ); // use symtab2_ ?
chillAST_DeclRefExpr *dre = new chillAST_DeclRefExpr( "int", _s.c_str(), (chillAST_Node*)vd); // parent not available
//fprintf(stderr, "made a new chillRepr from "); dre->dump(); fflush(stdout);
return new CG_chillRepr( dre );
}
// variable was already defined as either a parameter or internal variable to the function.
// NOW WHAT?? gotta return something
chillAST_VarDecl *vd = currentfunction->getVariableDeclaration( _s.c_str() );
//fprintf(stderr, "vd %p\n", vd);
chillAST_DeclRefExpr *dre = new chillAST_DeclRefExpr( "int", _s.c_str(), (chillAST_Node*)vd); // parent not available
return new CG_chillRepr( dre );
}
//-----------------------------------------------------------------------------
// binary arithmetic operations
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreatePlus(CG_outputRepr *lop,
CG_outputRepr *rop) const {
fprintf(stderr, "CG_chillBuilder::CreatePlus()\n");
if(rop == NULL) return lop; // ??
else if(lop == NULL) return rop;
chillAST_Node *left = ((CG_chillRepr*)lop)->chillnodes[0];
chillAST_Node *right = ((CG_chillRepr*)rop)->chillnodes[0];
chillAST_BinaryOperator *bop = new chillAST_BinaryOperator( left, "+", right); // parent not available
return new CG_chillRepr( bop );
/*
Expr *lhs = static_cast<CG_chillRepr*>(lop)->GetExpression();
Expr *rhs = static_cast<CG_chillRepr*>(rop)->GetExpression();
// Not sure about type!!
fprintf(stderr, "about to die in CG_chillBuilder ~line 628 CREATE PLUS\n");
BinaryOperator *ins = new (astContext_)BinaryOperator(lhs,
rhs,
BO_Add,
lhs->getType(), // qualifyier type
VK_LValue, //Expression Value Kind, following the C++11 scheme
OK_Ordinary, // expression object kind, A further classification of the kind of object referenced by an l-value or x-value.
SourceLocation(),
false ); // fpContractable ??
delete lop; delete rop;
//fprintf(stderr, " NEW binary operator 0x%x\n", ins);
fprintf(stderr, "CG_chillBuilder::CreatePlus ins 0x%x\n", ins);
return new CG_chillRepr(ins);
*/
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateMinus(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "CG_chillBuilder::CreateMinus( lop %p rop %p)\n", lop, rop);
fprintf(stderr, "CG_chillBuilder::CreateMinus()\n");
if(rop == NULL) {
fprintf(stderr, "CG_chillBuilder::CreateMinus(), right side is NULL\n");
return lop; // from protonu's version.
int *i = 0;
int j = i[0]; // segfault
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
if(clop == NULL) { // this is really a unary operator ???
//fprintf(stderr, "CG_chillBuilder::CreateMinus() unary\n");
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
chillAST_UnaryOperator *ins = new chillAST_UnaryOperator("-", true, rAST->clone()); // clone?
delete crop; // ?? note: the chillRepr, not the chillAST_Node
return new CG_chillRepr(ins);
} else {
//fprintf(stderr, "binary\n");
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//lAST->print(); printf(" - ");
//rAST->print(); printf("\n"); fflush(stdout);
chillAST_BinaryOperator *bop = new chillAST_BinaryOperator(lAST->clone(), "-", rAST->clone()); // clone??
delete clop; delete crop; // ?? note: the chillReprs, not the chillAST_Nodes
return new CG_chillRepr(bop);
}
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateTimes(CG_outputRepr *lop,
CG_outputRepr *rop) const {
fprintf(stderr, "CG_chillBuilder::CreateTimes()\n");
if (rop == NULL || lop == NULL) {
if (rop != NULL) {
rop->clear();
delete rop;
}
if (lop != NULL) {
lop->clear();
delete lop;
}
return NULL;
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
fprintf(stderr, "building ");
lAST->print(0, stderr);
fprintf(stderr, " * ");
rAST->print(0, stderr);
fprintf(stderr, "\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "*", rAST);
delete lop; delete rop; // ??
//fprintf(stderr, "CG_chillBuilder::CreateTimes() returning a CG_chillRepr with a binop inside\n");
return new CG_chillRepr( binop );
}
//-----------------------------------------------------------------------------
// CG_outputRepr *CG_chillBuilder::CreateDivide(CG_outputRepr *lop, CG_outputRepr *rop) const {
// return CreateIntegerFloor(lop, rop);
// }
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateIntegerDivide(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "CG_chillBuilder::CreatIntegerDivide()\n");
if (rop == NULL) {
fprintf(stderr, "Code generation: divide by NULL\n");
return NULL;
}
else if ( lop == NULL ) {
delete rop;
return NULL;
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, " / ");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "/", rAST);
delete lop; delete rop; // ??
return new CG_chillRepr( binop );
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateIntegerFloor(CG_outputRepr* lop, CG_outputRepr* rop) const {
//fprintf(stderr, "CG_chillBuilder::CreateIntegerFloor()\n");
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, " / ");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "/", rAST);
return new CG_chillRepr( binop );
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateIntegerMod(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "CG_chillBuilder::CreateIntegerMod() NEEDS WORK\n");
//fprintf(stderr, "LHS "); lop->dump();
//fprintf(stderr, "RHS "); rop->dump();
CG_chillRepr *l = (CG_chillRepr *) lop;
CG_chillRepr *r = (CG_chillRepr *) rop;
chillAST_Node *lhs = l->GetCode();
chillAST_Node *rhs = r->GetCode();
chillAST_BinaryOperator *BO = new chillAST_BinaryOperator(lhs, "%", rhs );
return new CG_chillRepr(BO);
/*
if (rop == NULL || lop == NULL) {
return NULL;
}
Expr *op1 = static_cast<CG_chillRepr*>(lop)->GetExpression();
Expr *op2 = static_cast<CG_chillRepr*>(rop)->GetExpression();
// Not sure about type!!
fprintf(stderr, "gonna die in CG_chillBuilder.cc ~line 394\n");
BinaryOperator *ins = NULL; // new (astContext_)BinaryOperator(op1, op2, BO_Rem, op1->getType(), SourceLocation());
delete lop; delete rop;
return new CG_chillRepr(ins);
*/
}
//-----------------------------------------------------------------------------
CG_outputRepr *CG_chillBuilder::CreateIntegerCeil(CG_outputRepr *lop, CG_outputRepr *rop) const {
return CreateMinus(NULL, CreateIntegerFloor(CreateMinus(NULL, lop), rop));
}
//-----------------------------------------------------------------------------
// binary logical operations
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateAnd(CG_outputRepr *lop,
CG_outputRepr *rop) const {
fprintf(stderr, "CG_chillBuilder::CreateAnd()\n");
if (rop == NULL)
return lop;
else if (lop == NULL)
return rop;
/* if (rop == NULL || lop == NULL ) {
fprintf(stderr, "returning NULL!\n");
return NULL;
}*/
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, " && ");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "&&", rAST);
return new CG_chillRepr( binop );
}
//-----------------------------------------------------------------------------
// binary relational operations
//-----------------------------------------------------------------------------
// CG_outputRepr* CG_chillBuilder::CreateGE(CG_outputRepr *lop, // use the outputBuilder version
// CG_outputRepr *rop) const {
//
// Expr *op1 = static_cast<CG_chillRepr*>(lop)->GetExpression();
// Expr *op2 = static_cast<CG_chillRepr*>(rop)->GetExpression();
// Not sure about type!!
// fprintf(stderr, "about to die in CG_chillBuilder ~line 480\n");
// BinaryOperator *ins = NULL; // new (astContext_)BinaryOperator(op1, op2, BO_GE, op1->getType(), SourceLocation());
// delete lop; delete rop;
// return new CG_chillRepr(ins);
// }
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateLE(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "CG_chillBuilder::CreateLE()\n");
if (rop == NULL || lop == NULL) {
return NULL;
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, " <= ");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "<=", rAST);
delete lop; delete rop; // ??
return new CG_chillRepr( binop );
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateEQ(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "CG_chillBuilder::CreateEQ()\n");
if (rop == NULL || lop == NULL) {
return NULL;
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, " = ");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "==", rAST);
delete lop; delete rop; // ??
return new CG_chillRepr( binop );
}
CG_outputRepr* CG_chillBuilder::CreateNEQ(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "CG_chillBuilder::CreateNEQ()\n");
if (rop == NULL || lop == NULL) {
return NULL;
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, " != ");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, "!=", rAST);
delete lop; delete rop; // ??
return new CG_chillRepr( binop );
}
CG_outputRepr* CG_chillBuilder::CreateDotExpression(CG_outputRepr *lop,
CG_outputRepr *rop) const {
//fprintf(stderr, "\nCG_chillBuilder::CreateDotExpression()\n");
if (rop == NULL || lop == NULL) {
return NULL;
}
CG_chillRepr *clop = (CG_chillRepr *) lop;
CG_chillRepr *crop = (CG_chillRepr *) rop;
chillAST_Node *lAST = clop->chillnodes[0]; // always just one?
chillAST_Node *rAST = crop->chillnodes[0]; // always just one?
//fprintf(stderr, "left is %s, right is %s\n", lAST->getTypeString(), rAST->getTypeString());
if ( !rAST->isVarDecl()) {
fprintf(stderr, "CG_chillBuilder::CreateDotExpression() right is a %s, not a vardecl\n",
rAST->getTypeString());
exit(-1);
}
chillAST_VarDecl *rvd = (chillAST_VarDecl *)rAST;
//fprintf(stderr, "building ");
//lAST->print(0, stderr);
//fprintf(stderr, ".");
//rAST->print(0, stderr);
//fprintf(stderr, " ??\n");
//chillAST_BinaryOperator *binop = new chillAST_BinaryOperator( lAST, ".", rAST, NULL);
// MemberExpr should be a DeclRefExpr on the left?
chillAST_DeclRefExpr *DRE = NULL;
if (lAST->isDeclRefExpr()) DRE = (chillAST_DeclRefExpr *)lAST;
if (lAST->isVarDecl()) {
// make a DeclRefExpr ? probably an error upstream of here in this case
DRE = new chillAST_DeclRefExpr( (chillAST_VarDecl *)lAST );
}
if (!DRE) {
fprintf(stderr, "CG_chillBuilder::CreateDotExpression(), can't create base\n");
exit(-1);
}
chillAST_MemberExpr *memexpr = new chillAST_MemberExpr( DRE, rvd->varname, NULL, CHILLAST_MEMBER_EXP_DOT );
//delete lop; delete rop; // ??
return new CG_chillRepr( memexpr );
}
//-----------------------------------------------------------------------------
// stmt list gen operations
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::CreateStmtList(CG_outputRepr *singleton) const {
//fprintf(stderr, "CG_chillBuilder::CreateStmtList()\n");
if(singleton == NULL) return NULL;
exit(-1); // DFL
return( NULL );
/*
StmtList *tnl = static_cast<CG_chillRepr *>(singleton)->GetCode();
if(tnl->empty()) {
StmtList foo;
fprintf(stderr, "gonna die soon CG_chillBuilder::CreateStmtList()\n"); exit(-1);
//foo.push_back(static_cast<CG_chillRepr*>(singleton)->op_);
return new CG_chillRepr(foo);
}
delete singleton;
return new CG_chillRepr(*tnl);
*/
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::StmtListInsertLast(CG_outputRepr *list,
CG_outputRepr *node) const {
return StmtListAppend(list, node);
}
//-----------------------------------------------------------------------------
CG_outputRepr* CG_chillBuilder::StmtListAppend(CG_outputRepr *list1,
CG_outputRepr *list2) const {
//fprintf(stderr, "CG_chillBuilder::StmtListAppend()\n");
if(list1 == NULL) return list2;
else if(list2 == NULL) return list1;
CG_chillRepr *cr1 = (CG_chillRepr *)list1;
CG_chillRepr *cr2 = (CG_chillRepr *)list2;
int numtoadd = cr2->chillnodes.size();
//fprintf(stderr, "before: %d nodes and %d nodes\n", cr1->chillnodes.size(), numtoadd );
for (int i=0; i<numtoadd; i++){
(cr1->chillnodes).push_back(cr2->chillnodes[i] );
}
//fprintf(stderr, "after %d nodes\n", cr1->chillnodes.size() );
delete list2;
return list1;
}
bool CG_chillBuilder::QueryInspectorType(const std::string &varName) const {
fprintf(stderr, "CG_chillBuilder::QueryInspectorType( %s )\n", varName.c_str());
int *i=0; int j= i[0];
return false;
}
CG_outputRepr* CG_chillBuilder::CreateArrayRefExpression(const std::string &_s,
CG_outputRepr *rop) const {
fprintf(stderr, "CG_chillBuilder::CreateArrayRefExpression() DIE\n");
fprintf(stderr, "string s '%s'\n", _s.c_str());
rop->dump();
int *i=0; int j = i[0];
exit(-1);
}
CG_outputRepr* CG_chillBuilder::CreateArrayRefExpression(CG_outputRepr*left,
CG_outputRepr*right) const{
chillAST_Node *l = ((CG_chillRepr *)left)->GetCode();
chillAST_Node *r = ((CG_chillRepr *)right)->GetCode();
chillAST_Node *base = NULL;
if (l->isDeclRefExpr()) base = l;
if (l->isMemberExpr()) base = l;
if (l->isVarDecl()) { // ??
// make a declRefExpr that uses VarDecl l
base = (chillAST_Node *) new chillAST_DeclRefExpr( (chillAST_VarDecl *)l );
}
if (!base) {
fprintf(stderr, "CG_chillBuilder::CreateArrayRefExpression(), left is %s\n", l->getTypeString());
exit(-1);
}
chillAST_ArraySubscriptExpr *ASE = new chillAST_ArraySubscriptExpr( base, r, NULL, 0); // unique TODO
return new CG_chillRepr( ASE );
}
CG_outputRepr* CG_chillBuilder::ObtainInspectorData(const std::string &_s, const std::string &member_name) const{
fprintf(stderr, "CG_chillBuilder::ObtainInspectorData( %s, %s)\n",
_s.c_str(), member_name.c_str());
//WTF
return ObtainInspectorRange( _s, member_name );
}
CG_outputRepr *CG_chillBuilder::CreateAddressOf(CG_outputRepr* op) const {
fprintf(stderr, "CG_chillBuilder::CreateAddressOf()\n");
exit(-1);
}
CG_outputRepr* CG_chillBuilder::CreateBreakStatement() const {
fprintf(stderr, "CG_chillBuilder::CreateBreakStatement()\n");
exit(-1);
}
CG_outputRepr *CG_chillBuilder::CreateStatementFromExpression(CG_outputRepr *exp) const {
fprintf(stderr, "CG_chillBuilder::CreateStatementFromExpression()\n");
exit(-1);
}
CG_outputRepr *CG_chillBuilder::CreateStruct(const std::string struct_name,
std::vector<std::string> data_members,
std::vector<CG_outputRepr *> data_types)
{
fprintf(stderr, "\nCG_chillBuilder::CreateStruct( %s )\n", struct_name.c_str());
/* WRONG - a typedef
// NEED TO ADD TYPEDEF TO ... SOMETHING
chillAST_TypedefDecl *tdd = new chillAST_TypedefDecl( ) ;
tdd->setStructName(struct_name.c_str());
tdd->setStruct( true );
int n_memb = data_members.size();
int n_data_types = data_types.size();
for (int i=0; i<n_memb; i++) {
chillAST_VarDecl *vd;
fprintf(stderr, "member %s type ", data_members[i].c_str());
if (i <n_data_types) {
vd = (chillAST_VarDecl *) ((CG_chillRepr *)data_types[i])->GetCode();
vd->varname = strdup( data_members[i].c_str() );
bool simplepointer = (vd->numdimensions == 1 && !vd->knownArraySizes);
if (simplepointer) fprintf(stderr, "pointer to ");
fprintf(stderr, "%s\n", vd->vartype );
if (vd->numdimensions > 0 && vd->knownArraySizes) {
for (int k=0; k<vd->numdimensions; k++) fprintf(stderr, "[%d]", vd->arraysizes[k]);
}
}
else {
fprintf(stderr, "type int BY DEFAULT (bad idea)\n");
vd = new chillAST_VarDecl( "int", data_members[i].c_str(), "", NULL);
}
// add vd to suparts of the struct typedef
tdd->subparts.push_back( vd );
fprintf(stderr, "\n");
}
// put the typedef in the top level ... for now TODO
toplevel->insertChild( 0, tdd);
return new CG_chillRepr( tdd );
*/
chillAST_RecordDecl *rd = new chillAST_RecordDecl(struct_name.c_str(), NULL);
rd->setParent(toplevel);
rd->setStruct( true );
// SO FAR, struct has no members!
toplevel->insertChild(0, rd); // inserts at beginning of file, (after defines?)
// note: parent at top level so far TODO
//toplevel->print(); printf("\n\n"); fflush(stdout);
int n_memb = data_members.size();
int n_data_types = data_types.size();
// add struct members
for (int i=0; i<n_memb; i++) {
chillAST_VarDecl *vd = NULL;
//fprintf(stderr, "%d member %s type ", i, data_members[i].c_str());
if (i < n_data_types) {
// this should always happen, formerly, if no data type was
// specified, it was an int. bad idea
vd = (chillAST_VarDecl *) ((CG_chillRepr *)data_types[i])->GetCode();
// vd did not have a name before
vd->varname = strdup( data_members[i].c_str() );
vd->parent = rd; // ??
bool simplepointer = (vd->numdimensions == 1 && !vd->knownArraySizes);
if (simplepointer) {
fprintf(stderr, "struct member %s is pointer to %s\n", vd->varname, vd->vartype);
vd->arraypointerpart = strdup("*"); // ??
}
else {
//fprintf(stderr, "struct member %s is not a pointer TODO!\n", vd->varname);
fprintf(stderr, "struct member %s is %s\n", vd->varname, vd->vartype);
// it should be good to go ???
}
//vd->print(); printf("\n"); fflush(stdout);
//fprintf(stderr, "%s\n", vd->vartype );
//if (vd->numdimensions > 0 && vd->knownArraySizes) {
// for (int k=0; k<vd->numdimensions; k++) fprintf(stderr, "[%d]", vd->arraysizes[k]);
//}
}
else {
fprintf(stderr, "int BY DEFAULT (bad idea) FIXME\n"); // TODO
vd = new chillAST_VarDecl( "int", data_members[i].c_str(), "", NULL);
}
rd->addSubpart( vd );
//fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
return new CG_chillRepr( rd );
}
CG_outputRepr *CG_chillBuilder::CreateClassInstance(std::string name , // TODO can't make array
CG_outputRepr *class_def){
fprintf(stderr, "CG_chillBuilder::CreateClassInstance( %s )\n", name.c_str());
CG_chillRepr *CD = (CG_chillRepr *)class_def;
chillAST_Node *n = CD->GetCode();
//fprintf(stderr, "class def is of type %s\n", n->getTypeString());
//n->print(); printf("\n"); fflush(stdout);
if (n->isTypeDefDecl()) {
chillAST_TypedefDecl *tdd = (chillAST_TypedefDecl *)n;
//tdd->print(); printf("\n"); fflush(stdout);
chillAST_VarDecl *vd = new chillAST_VarDecl( tdd, name.c_str(), "");
// we need to add this to function ?? TODO
//fprintf(stderr, "adding typedef instance to symbolTable\n");
chillAST_SymbolTable *st = currentfunction->getBody()->getSymbolTable();
//printSymbolTable(st);
currentfunction->getBody()->addVariableToScope( vd ); // TODO
currentfunction->getBody()->insertChild(0, vd); // TODO
//printSymbolTable(st);
return new CG_chillRepr( vd );
}
if (n->isRecordDecl()) {
fprintf(stderr, "a RecordDecl\n");
chillAST_RecordDecl *rd = (chillAST_RecordDecl *) n;
rd->print(); printf("\n"); fflush(stdout);
rd->dump(); printf("\n"); fflush(stdout);
chillAST_VarDecl *vd = new chillAST_VarDecl( rd, name.c_str(), "");
//fprintf(stderr, "CG_chillBuilder.cc, adding struct instance to body of function's symbolTable\n");
// we need to add this to function ?? TODO
currentfunction->getBody()->addVariableToScope( vd );
currentfunction->getBody()->insertChild(0, vd);
//printf("\nafter adding vardecl, source is:\n");
currentfunction->getBody()->print(); fflush(stdout);
//printf("\nafter adding vardecl, symbol table is:\n");
chillAST_SymbolTable *st = currentfunction->getBody()->getSymbolTable();
//printSymbolTable(st); fflush(stdout);
return new CG_chillRepr( vd );
}
fprintf(stderr, "ERROR: CG_chillBuilder::CreateClassInstance() not sent a class or struct\n");
int *i=0; int j = i[0];
return NULL;
}
CG_outputRepr *CG_chillBuilder::lookup_member_data(CG_outputRepr* classtype,
std::string varName,
CG_outputRepr *instance) {
//fprintf(stderr, "CG_chillBuilder::lookup_member_data( %s )\n", varName.c_str());
chillAST_VarDecl* sub = NULL;
CG_chillRepr *CR = (CG_chillRepr *)classtype;
chillAST_Node *classnode = CR->GetCode();
//fprintf(stderr, "classnode is %s\n", classnode->getTypeString()); classnode->print(); printf("\n"); fflush(stdout);
if (! ( classnode->isTypeDefDecl() ||
classnode->isRecordDecl() )) {
fprintf(stderr, "ERROR: CG_chillBuilder::lookup_member_data(), classnode is not a TypeDefDecl or a RecordDecl\n");
exit(-1);
}
CG_chillRepr *CI = (CG_chillRepr *)instance;
chillAST_Node *in = CI->GetCode();
//fprintf(stderr, "instance is %s\n", in->getTypeString());
//in->print(); printf("\n"); fflush(stdout);
if ( !in->isVarDecl() ) { // error, instance needs to be a vardecl
fprintf(stderr, "ERROR: CG_chillBuilder::lookup_member_data() instance needs to be a VarDecl, not a %s", in->getTypeString());
exit(-1);
}
chillAST_VarDecl *vd = (chillAST_VarDecl *)in;
if (vd->typedefinition != classnode &&
vd->vardef != classnode) {
fprintf(stderr, "vd: typedef %p vardev %p classnode %p\n", vd->typedefinition, vd->vardef, classnode);
fprintf(stderr, "CG_chillBuilder::lookup_member_data(), instance is not of correct class \n");
exit(-1);
}
if (classnode->isTypeDefDecl()){
chillAST_TypedefDecl *tdd = (chillAST_TypedefDecl *)classnode;
if ( !tdd->isAStruct() ) {
fprintf(stderr, "ERROR: CG_chillBuilder::lookup_member_data() instance must be a struct or class\n");
exit(-1);
}
sub = tdd->findSubpart( varName.c_str() );
}
if (classnode->isRecordDecl()){
chillAST_RecordDecl *rd = (chillAST_RecordDecl *)classnode;
if ( !rd->isAStruct() ) {
fprintf(stderr, "ERROR: CG_chillBuilder::lookup_member_data() instance must be a struct or class\n");
exit(-1);
}
//fprintf(stderr, "looking for member (subpart) %s in RecordDecl\n", varName.c_str());
sub = rd->findSubpart( varName.c_str() );
}
if (!sub) {
fprintf(stderr, "CG_chillBuilder::lookup_member_data(), variable %s is not submember of class/struct\n");
exit(-1);
}
//fprintf(stderr, "subpart (member) %s is\n", varName.c_str()); sub->print(); printf("\n"); fflush(stdout);
return( new CG_chillRepr( sub ) ); // the vardecl inside the struct typedef
}
CG_outputRepr* CG_chillBuilder::CreatePointer(std::string &name) const {
//fprintf(stderr, "CG_chillBuilder::CreatePointer( %s )\n", name.c_str());
chillAST_VarDecl *vd = new chillAST_VarDecl( "int", name.c_str(), "*", currentfunction->getBody());
//vd->print(); printf("\n"); fflush(stdout);
//vd->dump(); printf("\n"); fflush(stdout);
//printSymbolTable( currentfunction->getBody()->getSymbolTable() );
chillAST_DeclRefExpr *dre = new chillAST_DeclRefExpr( vd ); // ??
return new CG_chillRepr( dre ); // need a declrefexpr?
}
CG_outputRepr* CG_chillBuilder::ObtainInspectorRange(const std::string &structname, const std::string &member) const {
//fprintf(stderr, "CG_chillBuilder::ObtainInspectorRange(%s, %s )\n", structname.c_str(), member.c_str());
// find a struct/class with name structname and member member
// return a Member access (or binary dot op )
// seems like you need to know where (scoping) to look for the struct definition
std::vector<chillAST_VarDecl*> decls;
currentfunction->gatherVarDecls( decls );
//fprintf(stderr, "\nfunc has %d vardecls (looking for %s)\n", decls.size(), structname.c_str());
chillAST_VarDecl *thestructvd = NULL;
for (int i=0; i<decls.size(); i++) {
chillAST_VarDecl *vd = decls[i];
//vd->print(); printf("\n"); fflush(stdout);
if (structname == vd->varname) {
//fprintf(stderr, "found it!\n");
thestructvd = vd;
break;
}
}
if (!thestructvd) {
fprintf(stderr, "CG_chillBuilder::ObtainInspectorRange could not find variable named %s in current function\n", structname.c_str());
exit(-1);
}
// make sure the variable is a struct with a member with the correct name
chillAST_RecordDecl *rd = thestructvd->getStructDef();
if ( !rd ) {
fprintf(stderr, "CG_chillBuilder::ObtainInspectorRange(), variable %s is not a struct/class\n", structname.c_str());
exit(-1);
}
chillAST_VarDecl *sub = rd->findSubpart( member.c_str() );
if (!sub) {
fprintf(stderr, "CG_chillBuilder::ObtainInspectorRange(), struct/class %s has no member named %s\n", structname.c_str(), member.c_str());
exit(-1);
}
// build up a member expression (or a binop with dot operation?? )
// make a declrefexpr that refers to this variable definition
chillAST_DeclRefExpr *DRE = new chillAST_DeclRefExpr( thestructvd );
chillAST_MemberExpr *ME = new chillAST_MemberExpr( DRE, member.c_str(), NULL); // uniq TODO
return new CG_chillRepr( ME );
}
} // namespace
|