-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathir_rose.cc
4826 lines (3704 loc) · 178 KB
/
ir_rose.cc
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) 2009-2010 University of Utah
All Rights Reserved.
Purpose:
CHiLL's rose interface.
Notes:
Array supports mixed pointer and array type in a single declaration.
History:
02/23/2009 Created by Chun Chen.
*****************************************************************************/
#ifdef FRONTEND_ROSE
#include <string>
#include "ir_rose.hh"
#include "ir_rose_utils.hh"
#include <code_gen/CG_roseRepr.h> // TODO remove
#include <code_gen/CG_chillRepr.h>
#include <code_gen/CG_chillBuilder.h>
#include <code_gen/rose_attributes.h>
#include "chill_ast.hh"
#include "ir_chill.hh" // should be ir_chill.hh , not ir_clang.hh
int IR_Code::ir_pointer_counter = 23; // TODO this dos nothing ???
int IR_Code::ir_array_counter = 1;
using namespace SageBuilder;
using namespace SageInterface;
using namespace omega;
// a global variable. it's OK.
SgProject *OneAndOnlySageProject; // a global
// more globals. These may be dumb. TODO
// in ir_chill.cc vector<chillAST_VarDecl *> VariableDeclarations;
// in ir_chill.cc vector<chillAST_FunctionDecl *> FunctionDeclarations;
//vector< chillAST_ > // named struct types ??
// TODO move to ir_rose.hh
SgNode *toplevel;
// temp
void die() { debug_fprintf(stderr, "\ndie()\n"); int *i=0; int j=i[0]; }
void ConvertRosePreprocessing( SgNode *sg, chillAST_node *n ) { // add preprocessing, attached to sg, to n
SgLocatedNode *locatedNode = isSgLocatedNode (sg);
if (locatedNode != NULL) {
Sg_File_Info *FI = locatedNode->get_file_info();
std::string nodefile; // file this node came from
//nodefile = FI->get_raw_filename();
nodefile = FI->get_filenameString () ;
if (!strstr( nodefile.c_str(), "rose_edg_required_macros")) { // ignore this file
//debug_fprintf(stderr, "\nfound a located node from raw file %s line %d col %d\n",
// nodefile.c_str(), FI->get_line(), FI->get_col());
}
AttachedPreprocessingInfoType *comments = locatedNode->getAttachedPreprocessingInfo ();
if (comments != NULL) {
debug_fprintf(stderr, "\nhey! comments! on a %s\n", n->getTypeString());
printf ("-----------------------------------------------\n");
printf ("Found a %s with preprocessing Info attached:\n", n->getTypeString());
printf ("(memory address: %p Sage type: %s) in file \n%s (line %d column %d) \n",
locatedNode,
locatedNode->class_name ().c_str (),
(locatedNode->get_file_info ()->get_filenameString ()).c_str (),
locatedNode->get_file_info ()->get_line(),
locatedNode->get_file_info ()->get_col() );
fflush(stdout);
AttachedPreprocessingInfoType::iterator i;
int counter = 0;
for (i = comments->begin (); i != comments->end (); i++) counter++;
debug_fprintf(stderr, "%d preprocessing info\n\n", counter);
counter = 0;
for (i = comments->begin (); i != comments->end (); i++){
printf("-------------PreprocessingInfo #%d ----------- : \n",counter++);
//printf("classification = %s:\n String format = %s\n",
// PreprocessingInfo::directiveTypeName((*i)->getTypeOfDirective ()). c_str (),
// (*i)->getString ().c_str ());
// this logic seems REALLY WRONG
CHILL_PREPROCESSING_POSITION p = CHILL_PREPROCESSING_POSITIONUNKNOWN;
printf ("relative position is = ");
if ((*i)->getRelativePosition () == PreprocessingInfo::inside) {
printf ("inside\n");
// ??? t =
}
else if ((*i)->getRelativePosition () == PreprocessingInfo::before) {
printf ("before\n");
p = CHILL_PREPROCESSING_LINEBEFORE;
}
else if ((*i)->getRelativePosition () == PreprocessingInfo::after) {
printf ("after\n");
p = CHILL_PREPROCESSING_TOTHERIGHT;
}
fflush(stdout);
char *blurb = strdup( (*i)->getString().c_str() );
chillAST_node *pre;
string preproctype = string( PreprocessingInfo::directiveTypeName((*i)->getTypeOfDirective ()). c_str () );
char *thing = strdup( (*i)->getString ().c_str ());
if (preproctype == "CplusplusStyleComment") {
debug_fprintf(stderr, "a comment %s\n", thing);
chillAST_Preprocessing *pp = new chillAST_Preprocessing( p, CHILL_PREPROCESSING_COMMENT, blurb);
n-> preprocessinginfo.push_back( pp);
}
if (preproctype == "CpreprocessorDefineDeclaration") {
debug_fprintf(stderr, "a #define %s\n", thing);
chillAST_Preprocessing *pp = new chillAST_Preprocessing( p, CHILL_PREPROCESSING_POUNDDEFINE, blurb);
n-> preprocessinginfo.push_back( pp);
}
if (preproctype == "CpreprocessorIncludeDeclaration") {
debug_fprintf(stderr, "a #include %s\n", thing);
chillAST_Preprocessing *pp = new chillAST_Preprocessing( p, CHILL_PREPROCESSING_POUNDINCLUDE, blurb);
n-> preprocessinginfo.push_back( pp);
}
} // for each comment attached to a node
fflush(stdout);
} // comments != NULL
fflush(stdout);
} // located node
} // ConvertRosePreprocessing
chillAST_node * ConvertRoseFile( SgNode *sg, const char *filename )// the entire file
{
debug_fprintf(stderr, "ConvertRoseFile( SgGlobal *sg, filename %s );\n", filename);
chillAST_SourceFile * topnode = new chillAST_SourceFile( filename ); // empty
std::string sourcefile = "";
ConvertRosePreprocessing( sg, topnode ); // handle preprocessing attached to the rose file node
SgLocatedNode *locatedNode = isSgLocatedNode (sg);
if (locatedNode != NULL) {
debug_fprintf(stderr, "TOPMOST located node\n");
Sg_File_Info *FI = locatedNode->get_file_info();
sourcefile = FI->get_raw_filename();
sourcefile = FI->get_filename();
debug_fprintf(stderr, "sourcefile is '%s'\n", sourcefile.c_str());
}
topnode->setFrontend("rose");
topnode->chill_array_counter = 1;
topnode->chill_scalar_counter = 0;
std::vector<std::pair< SgNode *, std::string > > topnodes = sg->returnDataMemberPointers();
int numtop = topnodes.size();
debug_fprintf(stderr, "%d top nodes\n", topnodes.size());
for (int i=0; i<numtop; i++) {
SgNode *n = topnodes[i].first;
string blurb = topnodes[i].second;
// we want to ignore all the builtins
//debug_fprintf(stderr, "%3d/%d %p %s ", i, numtop, n, blurb.c_str());
//debug_fprintf(stderr, "node %s\n", n->class_name().c_str());
std::string nodefile = ""; // file this node came from
locatedNode = NULL;
if (n && (locatedNode = isSgLocatedNode (n))) { // purposeful assignment not equality check
Sg_File_Info *FI = locatedNode->get_file_info();
nodefile = FI->get_filenameString () ;
if (!strstr( nodefile.c_str(), "rose_edg_required_macros")) { // denugging, but ignore this file
//if (nodefile != sourcefile) {
//debug_fprintf(stderr, "\nfound a located node from raw file %s line %d col %d\n",
// nodefile.c_str(), FI->get_line(), FI->get_col());
}
}
// not sure what these are; ignoring except for debugging info
if ( n == NULL ) {
debug_fprintf(stderr, "topnode %d of %d, first == NULL?? blurb %s\n", i, numtop, blurb.c_str());
}
else { // n exists. deal with each type of thing
if ( isSg_File_Info(n) ) {
Sg_File_Info *FI = (Sg_File_Info *) n;
debug_fprintf(stderr, "top node %d/%d Sg_File_Info\n", i, numtop);
string fname = FI->get_filenameString () ;
debug_fprintf(stderr, "file %s line %d col %d\n", fname.c_str(), FI->get_line(), FI->get_col());
}
else if ( isSgSymbolTable(n) ) {
SgSymbolTable *ST = (SgSymbolTable *) n;
debug_fprintf(stderr, "top node %d/%d SgSymbolTable (IGNORING)\n", i, numtop);
//ST->print(); fflush(stdout);
}
else if ( isSgFunctionDeclaration(n) ) {
//debug_fprintf(stderr, "it's a function DECLARATION\n");
SgFunctionDeclaration *fd = (SgFunctionDeclaration *)n;
const char *name = strdup( fd->get_name().str()) ;
//const char *name = fd->get_name().str() ;
//debug_fprintf(stderr, "name %p\n", name );
if (strncmp("__builtin", name, 9) &&//if name DOESN'T start with __builtin
strcmp("__sync_lock_test_and_set", name) &&
strcmp("__sync_lock_release", name)
) // ignore builtins. I can't find a better way to test
{
debug_fprintf(stderr, "\nfunctiondecl %s blurb %s\n", name, blurb.c_str());
bool samefile = (nodefile == sourcefile);
debug_fprintf(stderr, "nodefile %s\nsourcefile %s\n", nodefile.c_str(), sourcefile.c_str());
if (samefile) debug_fprintf(stderr, "SAME FILE\n");
else debug_fprintf(stderr, "NOT THE SAME FILE\n");
//debug_fprintf(stderr, "\n\n%3d %p %s ", i, n, blurb.c_str());
//debug_fprintf(stderr, "node %s\n", n->class_name().c_str());
//debug_fprintf(stderr, "adding function decl %s because it is not a builtin\n", name);
//debug_fprintf(stderr, "topnode has %d children\n", topnode->getNumChildren());
chillAST_node *node = ConvertRoseFunctionDecl(fd, topnode );
//debug_fprintf(stderr, "after convert, topnode has %d children\n", topnode->getNumChildren());
node ->isFromSourceFile = samefile;
node->filename = strdup(nodefile.c_str());
//debug_fprintf(stderr, "ir_rose.cc adding function %s as child of topnode\n\n", name);
//topnode->addChild( node ); // this is done in the convert
//debug_fprintf(stderr, "topnode now has %d children\n", topnode->getNumChildren());
}
else {
//debug_fprintf(stderr, "ignoring %s\n", name);
}
}
else if (isSgVariableDeclaration(n)) {
debug_fprintf(stderr, "\n\nA TOP LEVEL GLOBAL VARIABLE\n");
debug_fprintf(stderr, "\n%3d %p %s \n", i, n, blurb.c_str());
//debug_fprintf(stderr, "node %s\n", n->class_name().c_str());
SgVariableDeclaration *rosevd = (SgVariableDeclaration *)n;
chillAST_node *vd = ConvertRoseVarDecl( rosevd, topnode);
vd->isFromSourceFile = (nodefile == sourcefile);
vd->filename = strdup(nodefile.c_str());
debug_fprintf(stderr, "global "); vd->print(); printf("\n"); fflush(stdout);
if (vd->parent) {
debug_fprintf(stderr, "global has parent of type %s\n", vd->parent->getTypeString());
if (vd->parent->isSourceFile()) { // ?? other cases TODO
debug_fprintf(stderr, "adding global variable as child of sourcefile\n");
vd->parent->addChild(vd); // seems wrong
}
}
else debug_fprintf(stderr, "global has parent no parent\n");
// topnode->addChild( vd ); // done in convert
}
else if (isSgClassDeclaration(n)) {
debug_fprintf(stderr, "\n\nA TOP LEVEL CLASS OR STRUCT DEFINITION\n");
debug_fprintf(stderr, "\n%3d %p %s \n", i, n, blurb.c_str());
//debug_fprintf(stderr, "node %s\n", n->class_name().c_str());
SgClassDeclaration *SD = (SgClassDeclaration *)n;
SgClassDeclaration::class_types class_type = SD->get_class_type();
if (class_type == SgClassDeclaration::e_struct) {
//debug_fprintf(stderr, "a struct\n");
// what we really want is the class DEFINTION, not the DECLARATION
SgClassDefinition *def = SD->get_definition();
chillAST_node *structdef = ConvertRoseStructDefinition( def, topnode ); // really a recorddecl
structdef->isFromSourceFile = (nodefile == sourcefile);
structdef->filename = strdup(nodefile.c_str());
//debug_fprintf(stderr, "struct is %p\n", structdef);
chillAST_RecordDecl *RD = ( chillAST_RecordDecl *) structdef;
// figure out what file the struct decl is in
SgLocatedNode *locatedNode = isSgLocatedNode (SD);
if (locatedNode != NULL) {
debug_fprintf(stderr, "know the location of the struct definition\n");
Sg_File_Info *FI = locatedNode->get_file_info();
std::string nodefile = FI->get_filename();
debug_fprintf(stderr, "nodefile is '%s'\n", nodefile.c_str());
if (nodefile == topnode->SourceFileName) {
debug_fprintf(stderr, "adding struct DEFINITION %s to children of topnode\n", RD->getName());
topnode->addChild( structdef ); // adds the STRUCT but not the individual members
// TODO this should only happen if the class declaration is in the same file as topnode (a source file)
// this is done above debug_fprintf(stderr, "*** we need to add struct definition to globals ***\n");
}
}
else debug_fprintf(stderr, "DON'T know the location of the struct definition\n");
}
else {
debug_fprintf(stderr, "unhandled top node SgClassDeclaration that is not a struct!\n");
exit(-1);
}
}
//else {
// if (strncmp("__builtin", name, 9)) debug_fprintf(stderr, "decl %s is a forward declaration or a builtin\n", name );
//}
//}
else if (isSgTypedefDeclaration(n)) { // sometimes structs are this
debug_fprintf(stderr, "\nTYPEDEF\n\n");
debug_fprintf(stderr, "\n\n%3d %p %s ", i, n, blurb.c_str());
debug_fprintf(stderr, "node %s\n", n->class_name().c_str());
//debug_fprintf(stderr, "\nsometimes structs are this calling ConvertRoseTypeDefDecl\n");
SgTypedefDeclaration *TDD = (SgTypedefDeclaration *) n;
chillAST_TypedefDecl *td = (chillAST_TypedefDecl *)ConvertRoseTypeDefDecl( TDD, topnode );
td->isFromSourceFile = (nodefile == sourcefile);
td->filename = strdup(nodefile.c_str());
topnode->addChild( td );
topnode->addTypedefToTypedefTable( td );
//debug_fprintf(stderr, "done with SgTypedefDeclaration\n");
}
else {
//debug_fprintf(stderr, "\n\n%3d %p %s ", i, n, blurb.c_str());
//debug_fprintf(stderr, "node %s\n", n->class_name().c_str());
debug_fprintf(stderr, "unhandled top node %d/%d of type %s\n", i, numtop, n->class_name().c_str());
}
} // non-NULL node
} // for each top level node
//debug_fprintf(stderr, "ConvertRoseFile(), returning topnode\n");
topnode->dump();
return topnode;
}
// shorten really ugly unnamed struct names, or just strdup to turn const char * to char *
char *shortenRoseUnnamedName( const char *origname ) { // usually a TYPE not a name ...
// this used to be something like "
if (origname == NULL) return NULL; // ??
int l = strlen(origname);
//debug_fprintf(stderr, "\nshortenRoseUnnamedName( origname %d characters ) origname was '%s'\n", l, origname);
if ( l > 25 ) {
//debug_fprintf(stderr, "long (variable type) name is '%s'\n", origname );
if ( (!strncmp( "__unnamed_class", origname, 15)) ||
(!strncmp( "struct __unnamed_class", origname, 22)) ||
(!strncmp( "__anonymous_", origname, 13))) {
//debug_fprintf(stderr, "an unnamed struct with %d characters in the name!\n", strlen(origname));
// this was the beginning of dealing with unnamed struct names inside unnamed structs, but that seems not to be needed
//string o( origname );
//std::size_t first = o.find( string("__unnamed_class"));
//debug_fprintf(stderr, "first %d\n", first);
//
//string rest = o.substr( first + 5 );
//debug_fprintf(stderr, "rest %s\n", rest.c_str());
//std::size_t next = rest.find( string("__unnamed_class"));
//debug_fprintf(stderr, "next %d\n", next);
bool startsWithStruct = 0 == strncmp( "struct ", origname, 7);
string buh( origname );
string underlinel( "_L" );
std::size_t found = buh.find( underlinel );
if (found!=std::string::npos) {
//debug_fprintf(stderr, "it has _L at %d\n", found);
int linenumber;
sscanf( &origname[2 + found], "%d", &linenumber );
//debug_fprintf(stderr, "line number %d\n", linenumber);
char newname[128];
if (startsWithStruct)
sprintf(newname, "struct unnamedStructAtLine%d\0", linenumber);
else
sprintf(newname, "unnamedStructAtLine%d\0", linenumber);
char *shortname = strdup(newname);
//debug_fprintf(stderr, "shortened name is %s\n\n", shortname);
return shortname;
}
}
}
//debug_fprintf(stderr, "unable to shorten '%s'\n", origname);
return strdup(origname); // unable to shorten but still have to copy
}
char * shortenRoseStructMemberName( const char *oldname ) {
char *temp = strdup(oldname);
//debug_fprintf(stderr, "shortenRoseStructMemberName( '%s' )\n", oldname);
if (rindex(oldname, ':')) {
int i = rindex(oldname, ':') - oldname;
//debug_fprintf(stderr, "last part i=%d '%s'\n", i, &(temp[i]));
if (oldname[i-1] == ':') {
char *shorter = strdup(&oldname[i+1]); // starting after ::
free(temp);
return shorter;
}
}
return temp;
}
chillAST_node * ConvertRoseFunctionDecl( SgFunctionDeclaration *fd , chillAST_node *parent)
{
const char *functionname = strdup( fd->get_name().str());
debug_fprintf(stderr, "\nConvertRoseFunctionDecl( %s )\n", functionname);
// need return type
SgType *rt = fd->get_orig_return_type();
string temp = rt->unparseToString(); // so it stays in scope !!
const char *returntype = temp.c_str();
//debug_fprintf(stderr, "return type %s\n", returntype);
chillAST_FunctionDecl *chillFD = new chillAST_FunctionDecl( returntype, functionname, parent, (void *)fd /* unique */ );
ConvertRosePreprocessing( fd, chillFD); // before doing the function decl itself?
// add parameters
std::vector<SgInitializedName*> args = fd->get_args();
int numargs = args.size();
for (int i=0; i<numargs; i++) {
chillAST_VarDecl *chillPVD = (chillAST_VarDecl *)ConvertRoseParamVarDecl( args[i], chillFD );
chillFD->addParameter(chillPVD);
// already done inside ConvertRoseParamVarDecl VariableDeclarations.push_back(chillPVD); // global?
}
// add body IF THERE IS ONE
SgFunctionDefinition *funcdef = fd->get_definition();
if (funcdef) {
SgBasicBlock *bodybasicblock = funcdef->get_body();
debug_fprintf(stderr, "got body\n");
std::vector<SgStatement* > statements = bodybasicblock->get_statements();
int num_statements = statements.size();
debug_fprintf(stderr, "%d statements in FunctionDecl body\n", num_statements);
// create a compound statement for the function body, to hold the rest of the statements
chillAST_CompoundStmt *chillCS = new chillAST_CompoundStmt;
chillCS->setParent( chillFD );
debug_fprintf(stderr, "chillCS is %p\n", chillCS);
for (int i=0; i<num_statements; i++) {
SgStatement* statement = statements[i];
debug_fprintf(stderr, "\nstatement %d %s\n", i, statement->unparseToString().c_str());
//debug_fprintf(stderr,"calling ConvertRoseGenericAST with parent %p\n",chillCS);
debug_fprintf(stderr, "calling ConvertRoseGenericAST\n", chillCS);
chillAST_node *n = ConvertRoseGenericAST( statement, chillCS );
if (n) {
chillCS->addChild( n );
}
}
chillFD->setBody ( chillCS );
}
else {
//debug_fprintf(stderr, "function %s is a forward declaration or external\n", functionname);
chillFD->setForward();
}
debug_fprintf(stderr, "ir_rose.cc adding %s to FunctionDeclarations\n", functionname);
FunctionDeclarations.push_back(chillFD);
return chillFD;
}
// todo initname for vardecl ???
chillAST_node * ConvertRoseParamVarDecl( SgInitializedName *vardecl, chillAST_node *parent )
{
//debug_fprintf(stderr, "ConvertRoseParamVarDecl() ");
chillAST_VarDecl *chillVD = (chillAST_VarDecl *) ConvertRoseInitName( vardecl, parent );
chillVD->isAParameter = true;
debug_fprintf(stderr, "new parameter:\n");
//chillVD->dump(); printf("\n"); fflush(stdout); // dump in ConvertRoseInitName
return chillVD;
}
char *ConvertSgArrayTypeToString( SgArrayType* AT ) {
char *arraypart = strdup(""); // leak
SgExpression* indexExp = AT->get_index();
if(indexExp) {
//debug_fprintf(stderr, "indexExp %s\n", indexExp->unparseToString().c_str());
if ( SgBinaryOp *BO = isSgBinaryOp(indexExp) ) {
//debug_fprintf(stderr, "Binop\n");
chillAST_BinaryOperator *cbo = (chillAST_BinaryOperator *)ConvertRoseBinaryOp( BO, NULL );
int val = cbo->evalAsInt();
//cbo->print(); printf(" = %d\n", val); fflush(stdout);
//debug_fprintf(stderr, "manufacturing binop arraypart '[%d]'\n", val);
char *leak = (char *)malloc( 64 * sizeof(char));
sprintf(leak, "[%d]\0", val);
arraypart = leak;
// fix vartype?
//char *tmp = vartype;
//char *ind = index(tmp, '[');
//if (ind) {
// char *newstr = (char *)malloc( 1 + sizeof( tmp ));
// *ind = '\0';
// sprintf(newstr, "%s[%d]\0", tmp, val );
// vartype = newstr;
// free(tmp);
//}
}
else {
//free(arraypart);
char *number = ulhack(strdup( indexExp->unparseToString().c_str() )) ;
arraypart = (char *)malloc (3 + strlen(number));
sprintf(arraypart, "[%s]\0", number);
free(number);
}
//debug_fprintf(stderr, "arraypart %s\n", arraypart);
//arraypart = splitTypeInfo(vartype); // do before possible mucking with vartype
}
SgArrayType* arraybase = isSgArrayType(AT->get_base_type());
if (arraybase) {
char *first = ConvertSgArrayTypeToString( arraybase ); // recurse;
//debug_fprintf(stderr, "concatting %s %s\n", first, arraypart );
// concat
int lenfirst = strlen(first);
int lensecond = strlen(arraypart);
char *concatted = (char *)malloc( lenfirst + lensecond + 2 ); // could be 1?
strcpy(concatted, first);
strcat(concatted, arraypart);
//debug_fprintf(stderr, "concatted is %s\n", concatted);
free( first );
free( arraypart );
arraypart = concatted;
}
return arraypart;
}
chillAST_node *find_wacky_vartype( const char *typ, chillAST_node *parent ) {
// handle most cases quickly
char *t = parseUnderlyingType(strdup(typ));
//debug_fprintf(stderr, "underlying '%s'\n", t);
if ( 0 == strcmp("int", t) ||
0 == strcmp("double", t) ||
// 0 == strcmp("float", t) ||
0 == strcmp("float", t) ) return NULL;
//debug_fprintf(stderr, "OK, looking for %s\n", t);
if (!parent) {
//debug_fprintf(stderr, "no parent?\n");
return NULL;
}
chillAST_node *buh = parent->findDatatype( t );
//if (!buh) debug_fprintf(stderr, "could not find typedef for %s\n", t);
//else {
//debug_fprintf(stderr, "buh IS "); buh->print(); fflush(stdout);
//}
return buh;
}
chillAST_node * ConvertRoseInitName( SgInitializedName *initname, chillAST_node *parent ) // TODO probably wrong
{
debug_fprintf(stderr, "\n\n*** ConvertRoseInitName() %s\n", initname->unparseToString().c_str());
debug_fprintf(stderr, "initname %s\n", initname->unparseToString().c_str());
int numattr = initname->numberOfAttributes();
//debug_fprintf(stderr, "initname has %d attributes\n", numattr);
char *varname = shortenRoseStructMemberName( initname->unparseToString().c_str() );
debug_fprintf(stderr, "varname '%s'\n", varname);
//VariantT V;
//V = initname->variantT();
//debug_fprintf(stderr,"variantT %d %s\n", V, roseGlobalVariantNameList[V]);
SgType *typ = initname->get_type();
// !! if typ->unparseToString()->c_str(), the string and therefore the pointer to char are freed before the next statement !
string really = typ->unparseToString();
const char *otype = really.c_str();
debug_fprintf(stderr, "original vartype 0x%x '%s'\n", otype, otype);
bool restricted = isRestrict( otype );
// if this is a struct, the vartype may be a huge mess. make it nicer
char *vartype = parseUnderlyingType(restricthack( shortenRoseUnnamedName( otype )));
//debug_fprintf(stderr, "prettied vartype '%s'\n", vartype);
char *arraypart;// = strdup(""); // leak // splitTypeInfo(vartype); // do before possible mucking with vartype
arraypart = parseArrayParts( strdup(otype) );
debug_fprintf(stderr, "HACK vartype %s arraypart %s\n", vartype, arraypart);
// need underlying type to pass to constructor? double and arraypart **, not type double ** and arraypart **
SgDeclarationStatement *dec = initname->get_declaration();
SgDeclarationStatement *defdec = dec->get_definingDeclaration();
if ( !strncmp(vartype, "struct ", 7) ) {
debug_fprintf(stderr, "this is a struct ???\n");
//debug_fprintf(stderr, "\ndec %s\n", dec->unparseToString().c_str());
std::vector< std::pair< SgNode *, std::string > > subparts = dec->returnDataMemberPointers();
SgClassDeclaration *CD = NULL;
chillAST_RecordDecl *RD = new chillAST_RecordDecl( vartype, otype, parent);
int numsub = subparts.size();
//RD->uniquePtr = dec;
//debug_fprintf(stderr, "%d subparts\n", numsub);
for (int i=0; i<numsub; i++) {
SgNode *thing = subparts[i].first;
string name = subparts[i].second;
//debug_fprintf(stderr, "name %s\n", name.c_str());
//debug_fprintf(stderr, "\nsubpart %2d %s\n", i, name.c_str());
//if (!thing) debug_fprintf(stderr, "thing NULL\n");
//else debug_fprintf(stderr, "ConvertRoseInitName() thing is of type %s\n", roseGlobalVariantNameList[ thing->variantT() ]);
if (name == string("baseTypeDefiningDeclaration")) {
CD = (SgClassDeclaration *)thing;
//debug_fprintf(stderr, "me: %p defining %p\n", defdecc, CD);
//if (CD)
// debug_fprintf(stderr, "\ndefining %s\n", CD->unparseToString().c_str());
//else
// debug_fprintf(stderr, "\ndefining (NULL)\n");
}
//if (name == string("variables")) { // apparently just the variable name?
// SgInitializedName *IN = (SgInitializedName *)thing;
// debug_fprintf(stderr, "variables %s\n", IN->unparseToString().c_str());
//}
}
if (CD) { // once more with feeling
//debug_fprintf(stderr, "\n\n\n"); // CD: %s", CD->unparseToString());
subparts = CD->returnDataMemberPointers();
numsub = subparts.size();
//debug_fprintf(stderr, "%d subparts\n", numsub);
for (int i=0; i<numsub; i++) {
SgNode *thing = subparts[i].first;
string name = subparts[i].second;
//debug_fprintf(stderr, "\nsubpart %2d %s\n", i, name.c_str());
//if (!thing) debug_fprintf(stderr, "thing NULL\n");
//else debug_fprintf(stderr, "ConvertRoseInitName() thing is of type %s\n", roseGlobalVariantNameList[ thing->variantT() ]);
}
}
debug_fprintf(stderr, "OK, NOW WHAT convertroseinitname\n");
//die();
exit(-1);
}
// figure out if this is some non-standard typedef'd type
chillAST_node *def = find_wacky_vartype( vartype, parent );
//if (def) debug_fprintf(stderr, "OK, this is a typedef or struct we have to account for\n");
//arraypart = parseArrayParts( vartype ); // need to use decl before vartype has parts stripped out
//this is wrong. "something *" is not being flagged as array or pointer
//in addition, if vartype is a typedef, I think it's being missed.
if (isSgArrayType(typ)) {
//debug_fprintf(stderr, "ARRAY TYPE\n");
//if (arraypart) debug_fprintf(stderr, "but arraypart is already '%s'\n", arraypart);
SgArrayType *AT = (SgArrayType *)typ;
//if (arraypart) free(arraypart);
if (!arraypart) arraypart = ConvertSgArrayTypeToString( AT );
debug_fprintf(stderr, "in convertroseinitname(), arraypart %s\n", arraypart);
//SgArrayType* arraybase = isSgArrayType(t->get_base_type());
//SgExpression* indexExp = AT->get_index();
//if(indexExp) {
//
// debug_fprintf(stderr, "indexExp %s\n", indexExp->unparseToString().c_str());
// if ( SgBinaryOp *BO = isSgBinaryOp(indexExp) ) {
// //debug_fprintf(stderr, "Binop\n");
// chillAST_BinaryOperator *cbo = (chillAST_BinaryOperator *)ConvertRoseBinaryOp( BO, NULL );
// int val = cbo->evalAsInt();
// //cbo->print(); printf(" = %d\n", val); fflush(stdout);
// //debug_fprintf(stderr, "manufacturing binop arraypart '[%d]'\n", val);
// char *leak = (char *)malloc( 64 * sizeof(char));
// sprintf(leak, "[%d]\0", val);
// arraypart = leak;
// fix vartype?
//char *tmp = vartype;
char *ind = index(vartype, '[');
if (ind) {
//char *newstr = (char *)malloc( 1 + sizeof( tmp ));
*ind = '\0';
//sprintf(newstr, "%s %s\0", tmp, arraypart );
//vartype = newstr;
//free(tmp);
}
// }
// arraypart = splitTypeInfo(vartype); // do before possible mucking with vartype
// debug_fprintf(stderr, "vartype = '%s'\n", vartype);
// debug_fprintf(stderr, "arraypart = '%s'\n", arraypart);
//}
}
if (arraypart == NULL) arraypart = strdup(""); // leak
//debug_fprintf(stderr, "vartype = '%s'\n", vartype);
//debug_fprintf(stderr, "arraypart = '%s'\n", arraypart);
//SgDeclarationStatement *DS = initname->get_declaration();
//V = DS->variantT();
//debug_fprintf(stderr,"declaration statement variantT %d %s\n", V, roseGlobalVariantNameList[V]);
char *bracket = index(vartype, '{');
if (bracket) { // remove extra for structs
*bracket = '\0';
if (*(bracket-1) == ' ') *(bracket-1) = '\0';
}
//debug_fprintf(stderr,"%s %s ", vartype, varname); debug_fprintf(stderr,"arraypart = '%s'\n", arraypart);
chillAST_VarDecl * chillVD = NULL;
if (def) {
if (def->isRecordDecl()) {
//debug_fprintf(stderr, "vardecl of a STRUCT\n");
chillVD = new chillAST_VarDecl((chillAST_RecordDecl*)def, varname, arraypart, parent);
}
else if (def->isTypeDefDecl()) {
//debug_fprintf(stderr, "vardecl of a typedef\n");
chillVD = new chillAST_VarDecl((chillAST_TypedefDecl*)def, varname, arraypart, parent);
}
else {
debug_fprintf(stderr, "def but not a recorddecl or a typedefdecl?\n");
exit(-1);
}
}
else {
//debug_fprintf(stderr, "\n*** creating new chillAST_VarDecl ***\n");
chillVD = new chillAST_VarDecl( vartype, varname, arraypart, (void *)initname, parent);
}
chillVD->isRestrict = restricted; // TODO nicer way
chillVD->uniquePtr = defdec;
debug_fprintf(stderr, "ConvertRoseInitName() storing variable declaration '%s' with unique value %p\n", varname, chillVD->uniquePtr );
// store this away for declrefexpr that references it!
VariableDeclarations.push_back(chillVD);
//debug_fprintf(stderr, "ConvertRoseInitName() END\n");
// check for an initializer int i = 0;
SgInitializer * initptr = initname->get_initptr();
if (initptr) {
debug_fprintf(stderr, "%s gets initialized\n", chillVD->varname);
chillAST_node *init = ConvertRoseGenericAST( initptr, parent); // NULL);
chillVD->setInit( init );
}
//chillVD->dump(); printf("\n"); fflush(stdout);
return chillVD;
}
char *fixUnnamedStructType( char *otype, char *entiredecl ) // deal with unnamed struct messiness
{
char *result = otype; // default is to not change anything
// see if otype looks like an unnamed struct
if ( 0 == strncmp(otype, "struct", 6)) { // it's a struct
// find the first non-space character starting at position 8
int l = strlen(otype);
//debug_fprintf(stderr, "%d chars in '%s'\n", l, otype);
for (int i=6; i<l; i++) {
char c = otype[i];
//debug_fprintf(stderr, "char %d is '%c'\n", i, c);
if (c != ' ') {
if (c == '{') {
// first nonblank is open bracket, it's an unnamed struct
//debug_fprintf(stderr, "it's an unnamed struct!\n");
//debug_fprintf(stderr, "want to get the type from '%s'\n", entiredecl);
char *decl = strdup(entiredecl);
if (strncmp(decl, "struct ", 7)) { // make sure entiredecl looks like "struct something"
debug_fprintf(stderr, "ir_rose.ccERROR, trying to get name of an unnamed struct from '%s'\n", decl);
exit(-1);
}
char *bettertype = decl;
char *p = bettertype + 6;
// handle possible lots of spaces (should never happen)
//debug_fprintf(stderr, "bettertype '%s'\n", bettertype);
l = strlen(bettertype);
for (int j=6; j<l; j++) {
if (*p == ' ') { // ignore initial spaces after "struct"
p++;
}
else break; // not a space, we should be pointing at the start of the name
}
// find the name. end name at first space if any
l = strlen(p); // how many chars we haven't looked at yet (off by one?)
for (int j=0; j<l; j++) {
if (*p != ' ') { // include non spaces
p++;
}
else { // a space, end the name
*p = '\0';
break;
}
}
debug_fprintf(stderr, "unnamed struct '%s'\n", bettertype);
result = bettertype;
break;
}
else {
// first nonblank looks like a struct name - leave the type alone
break;
}
}
}
}
return result;
}
chillAST_node * ConvertRoseVarDecl( SgVariableDeclaration *vardecl, chillAST_node *parent )
{
debug_fprintf(stderr, "\nConvertRoseVarDecl() \n");
SgDeclarationStatement *defdecl = vardecl->get_definingDeclaration(); // unique
debug_fprintf(stderr, "defdecl %p\n", defdecl);
if (defdecl == NULL) defdecl = vardecl;
std::vector<SgInitializedName* > names = vardecl->get_variables();
if (1 != names.size()) debug_fprintf(stderr, "%d initialized names\n", names.size());
char *entiredecl = strdup( vardecl->unparseToString().c_str());
if ( names.size() > 1 ) {
debug_fprintf(stderr, "ConvertRoseVarDecl() %s\n", entiredecl);
debug_fprintf(stderr, "too many decls in a decl!\n");
exit(-1);
}
// first, get the type. this may be a really ugly thing for an unnamed struct (not with more recent rose)
SgInitializedName* iname = names[0];
SgType *typ = iname->get_type();
char *ovarname = strdup(iname->unparseToString().c_str()); // original
char *otype = strdup(typ->unparseToString().c_str()); // original
//this otype is useless, as it does not deal with unnamed structs well
debug_fprintf(stderr, "entiredecl: '%s'\n", entiredecl);
debug_fprintf(stderr, "original vartype '%s'\n", otype);
debug_fprintf(stderr, "original varname '%s'\n", ovarname);
char *shorttype = otype; // shortenRoseUnnamedName( otype ); // this used to be REALLY LONG for unnamed structs
bool restricted = isRestrict( otype ) ; // is __restrict__ in there?
//if (restricted) debug_fprintf(stderr, "RESTRICTED\n");
//else debug_fprintf(stderr, "NOT RESTRICTED\n");
//char *fixedtype = fixUnnamedStructType( otype, entiredecl );
//debug_fprintf(stderr, "SHORT TYPE IS %s\n", shorttype);
otype = restricthack( otype); // remove __restrict__
char *vartype = fixUnnamedStructType( otype, entiredecl );
char *arraypart = splitTypeInfo(vartype);
char *varname = shortenRoseStructMemberName( ovarname );
if (strlen(arraypart) > 1)
debug_fprintf(stderr, "vartype: %s\nvarname: %s\narraypart %s\n\n", vartype, varname, arraypart);
else
debug_fprintf(stderr, "vartype: %s\nvarname: %s\n\n", vartype, varname);
bool unnamedstruct = false; // ususally not
char *nameOfStruct = vartype;
if ( !strncmp( vartype, "struct ", 7)) {
debug_fprintf(stderr, "it is a struct of type '%s' \n", vartype);
nameOfStruct = &vartype[7];
debug_fprintf(stderr, "struct name is '%s'\n", nameOfStruct);
if ('{' == nameOfStruct[0]) {
debug_fprintf(stderr, "it's an unnamed struct\n");
unnamedstruct = true;
}
}
// this if handles structs DEFINITIONS (and typedefs?)
// things like
// struct { int i} b; // this is an unnamed struct. the vardecl for b will have a BaseTypeDefiningDeclaration
//
// struct a { int i; } b; // this will ALSO have a BaseTypeDefiningDeclaration, for struct a
chillAST_RecordDecl *RD = NULL;
SgDeclarationStatement *defining = NULL;
if (vardecl->get_variableDeclarationContainsBaseTypeDefiningDeclaration()) {
//struct {
// struct { a,b,c} d,e;
//}
// d willhave a defining decl. e will not
debug_fprintf(stderr, "in ConvertRoseVarDecl(), there is a defining declaration (a struct or typedef?)\n");
SgDeclarationStatement *DS = vardecl->get_baseTypeDefiningDeclaration();
//debug_fprintf(stderr, "DS type %s\n", roseGlobalVariantNameList[DS->variantT()]);
if (SgClassDeclaration *CD = isSgClassDeclaration(DS)) {
debug_fprintf(stderr, "it's a ClassDeclaration\n");
SgClassDeclaration::class_types class_type = CD->get_class_type();
if (class_type == SgClassDeclaration::e_struct) {
debug_fprintf(stderr, "it's a ClassDeclaration of a struct\n");
// RD should be the RecordDecl that says what's in the struct
RD = (chillAST_RecordDecl *) ConvertRoseStructDeclaration( CD, parent );
debug_fprintf(stderr, "\nhere is the struct definition:\n"); RD->print(); printf("\n"); fflush(stdout);
//debug_fprintf(stderr, "we need to declare a variable of this STRUCT type named %s\n", varname);
// do we need to remember this struct type somewhere in case there are more of them?
}
}
}
if ( !strncmp( vartype, "struct ", 7)) {
debug_fprintf(stderr, "it is a struct of type '%s' \n", vartype);
if (RD) {
debug_fprintf(stderr, "we know what the struct definition looks like because it was part of this vardecl\n");
chillAST_VarDecl *vd = new chillAST_VarDecl( RD, varname, "", parent);
//parent->addChild( vd ); // ??
vd->setStruct( true );
RD->setUnnamed ( unnamedstruct ) ;
vd->uniquePtr = defdecl; // ??
debug_fprintf(stderr, "dammit setting %s uniquePtr to %p the SgVariableDeclaration that defined it?\n", vd->varname, vd->uniquePtr);
//debug_fprintf(stderr, "setting that it IS A STRUCT\n");
//if (vd->isAStruct()) debug_fprintf(stderr, "yes, it is!\n"); else debug_fprintf(stderr, "no, it isn't!\n");
vd->isRestrict = restricted;
debug_fprintf(stderr, "STORING vardecl %s in global VariableDeclarations %d\n", vd->varname, VariableDeclarations.size());
VariableDeclarations.push_back( vd );
return vd;