2 import IR.Tree.Modifiers;
3 import IR.Tree.FlagExpressionNode;
4 import IR.Tree.DNFFlag;
5 import IR.Tree.DNFFlagAtom;
6 import IR.Tree.TagExpressionList;
7 import IR.Tree.OffsetNode;
14 import Analysis.TaskStateAnalysis.FlagState;
15 import Analysis.TaskStateAnalysis.FlagComparator;
16 import Analysis.TaskStateAnalysis.OptionalTaskDescriptor;
17 import Analysis.TaskStateAnalysis.Predicate;
18 import Analysis.TaskStateAnalysis.SafetyAnalysis;
19 import Analysis.TaskStateAnalysis.TaskIndex;
20 import Analysis.CallGraph.CallGraph;
21 import Analysis.Loops.GlobalFieldType;
22 import Util.CodePrinter;
24 public class BuildCode {
27 Hashtable paramstable;
32 String localsprefix="___locals___";
33 String localsprefixaddr="&"+localsprefix;
34 String localsprefixderef=localsprefix+".";
35 String fcrevert="___fcrevert___";
36 String paramsprefix="___params___";
37 String nextobjstr="___nextobject___";
38 String localcopystr="___localcopy___";
39 public static boolean GENERATEPRECISEGC=false;
40 public static String PREFIX="";
41 public static String arraytype="ArrayObject";
42 public static int flagcount = 0;
45 protected int maxtaskparams=0;
46 protected int maxcount=0;
47 ClassDescriptor[] cdarray;
48 ClassDescriptor[] ifarray;
49 TypeDescriptor[] arraytable;
52 Hashtable<String, ClassDescriptor> printedfieldstbl;
53 int globaldefscount=0;
54 boolean mgcstaticinit = false;
56 public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil) {
57 this(st, temptovar, typeutil, null);
60 public BuildCode(State st, Hashtable temptovar, TypeUtil typeutil, SafetyAnalysis sa) {
63 callgraph=new CallGraph(state);
64 this.temptovar=temptovar;
65 paramstable=new Hashtable();
66 tempstable=new Hashtable();
67 fieldorder=new Hashtable();
68 flagorder=new Hashtable();
69 this.typeutil=typeutil;
71 virtualcalls=new Virtual(state, null);
72 printedfieldstbl = new Hashtable<String, ClassDescriptor>();
75 /** The buildCode method outputs C code for all the methods. The Flat
76 * versions of the methods must already be generated and stored in
77 * the State object. */
80 public void buildCode() {
81 /* Create output streams to write to */
82 PrintWriter outclassdefs=null;
83 PrintWriter outstructs=null;
84 PrintWriter outrepairstructs=null;
85 PrintWriter outmethodheader=null;
86 PrintWriter outmethod=null;
87 PrintWriter outvirtual=null;
88 PrintWriter outtask=null;
89 PrintWriter outtaskdefs=null;
90 PrintWriter outoptionalarrays=null;
91 PrintWriter optionalheaders=null;
92 PrintWriter outglobaldefs=null;
93 PrintWriter outglobaldefsprim=null;
96 buildCodeSetup(); //EXTENSION POINT
97 outstructs=new CodePrinter(new FileOutputStream(PREFIX+"structdefs.h"), true);
98 outmethodheader=new CodePrinter(new FileOutputStream(PREFIX+"methodheaders.h"), true);
99 outclassdefs=new CodePrinter(new FileOutputStream(PREFIX+"classdefs.h"), true);
100 outglobaldefs=new CodePrinter(new FileOutputStream(PREFIX+"globaldefs.h"), true);
101 outglobaldefsprim=new CodePrinter(new FileOutputStream(PREFIX+"globaldefsprim.h"), true);
102 outmethod=new CodePrinter(new FileOutputStream(PREFIX+"methods.c"), true);
103 outvirtual=new CodePrinter(new FileOutputStream(PREFIX+"virtualtable.h"), true);
105 outtask=new CodePrinter(new FileOutputStream(PREFIX+"task.h"), true);
106 outtaskdefs=new CodePrinter(new FileOutputStream(PREFIX+"taskdefs.c"), true);
107 if (state.OPTIONAL) {
108 outoptionalarrays=new CodePrinter(new FileOutputStream(PREFIX+"optionalarrays.c"), true);
109 optionalheaders=new CodePrinter(new FileOutputStream(PREFIX+"optionalstruct.h"), true);
112 if (state.structfile!=null) {
113 outrepairstructs=new CodePrinter(new FileOutputStream(PREFIX+state.structfile+".struct"), true);
115 } catch (Exception e) {
120 /* Fix field safe symbols due to shadowing */
121 FieldShadow.handleFieldShadow(state);
123 /* Build the virtual dispatch tables */
124 buildVirtualTables(outvirtual);
126 /* Tag the methods that are invoked by static blocks */
127 tagMethodInvokedByStaticBlock();
129 /* Output includes */
130 outmethodheader.println("#ifndef METHODHEADERS_H");
131 outmethodheader.println("#define METHODHEADERS_H");
132 outmethodheader.println("#include \"structdefs.h\"");
134 if (state.EVENTMONITOR) {
135 outmethodheader.println("#include \"monitor.h\"");
138 additionalIncludesMethodsHeader(outmethodheader);
140 /* Output Structures */
141 outputStructs(outstructs);
143 // Output the C class declarations
144 // These could mutually reference each other
146 outglobaldefs.println("#ifndef __GLOBALDEF_H_");
147 outglobaldefs.println("#define __GLOBALDEF_H_");
148 outglobaldefs.println("");
149 outglobaldefs.println("struct global_defs_t {");
150 outglobaldefs.println(" int size;");
151 outglobaldefs.println(" void * next;");
152 outglobaldefsprim.println("#ifndef __GLOBALDEFPRIM_H_");
153 outglobaldefsprim.println("#define __GLOBALDEFPRIM_H_");
154 outglobaldefsprim.println("");
155 outglobaldefsprim.println("struct global_defsprim_t {");
157 outclassdefs.println("#ifndef __CLASSDEF_H_");
158 outclassdefs.println("#define __CLASSDEF_H_");
159 outputClassDeclarations(outclassdefs, outglobaldefs, outglobaldefsprim);
161 // Output function prototypes and structures for parameters
162 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
163 while(it.hasNext()) {
164 ClassDescriptor cn=(ClassDescriptor)it.next();
165 generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs, outglobaldefsprim);
167 outclassdefs.println("#include \"globaldefs.h\"");
168 outclassdefs.println("#include \"globaldefsprim.h\"");
169 outclassdefs.println("#endif");
170 outclassdefs.close();
172 outglobaldefs.println("};");
173 outglobaldefs.println("");
174 outglobaldefs.println("extern struct global_defs_t * global_defs_p;");
175 outglobaldefs.println("#endif");
176 outglobaldefs.flush();
177 outglobaldefs.close();
179 outglobaldefsprim.println("};");
180 outglobaldefsprim.println("");
181 outglobaldefsprim.println("extern struct global_defsprim_t * global_defsprim_p;");
182 outglobaldefsprim.println("#endif");
183 outglobaldefsprim.flush();
184 outglobaldefsprim.close();
187 /* Map flags to integers */
188 /* The runtime keeps track of flags using these integers */
189 it=state.getClassSymbolTable().getDescriptorsIterator();
190 while(it.hasNext()) {
191 ClassDescriptor cn=(ClassDescriptor)it.next();
195 generateTaskStructs(outstructs, outmethodheader);
197 /* Outputs generic task structures if this is a task
199 outputTaskTypes(outtask);
203 // an opportunity for subclasses to do extra
205 preCodeGenInitialization();
208 /* Build the actual methods */
209 outputMethods(outmethod);
212 // opportunity for subclasses to gen extra code
213 additionalCodeGen(outmethodheader,
219 /* Output code for tasks */
220 outputTaskCode(outtaskdefs, outmethod);
222 /* Record maximum number of task parameters */
223 outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
224 } else if (state.main!=null) {
225 /* Generate main method */
226 outputMainMethod(outmethod);
229 /* Generate information for task with optional parameters */
230 if (state.TASK&&state.OPTIONAL) {
231 generateOptionalArrays(outoptionalarrays, optionalheaders, state.getAnalysisResult(), state.getOptionalTaskDescriptors());
232 outoptionalarrays.close();
235 /* Output structure definitions for repair tool */
236 if (state.structfile!=null) {
237 buildRepairStructs(outrepairstructs);
238 outrepairstructs.close();
242 outmethodheader.println("#endif");
243 outmethodheader.close();
245 outstructs.println("#endif");
250 postCodeGenCleanUp();
253 /* This method goes though the call graph and check which methods are really
254 * invoked and should be generated
256 protected void checkMethods2Gen() {
257 MethodDescriptor md=(state.main==null)?null:typeutil.getMain();
260 // check the methods to be generated
261 state.setGenAllMethods(false);
263 // generate all methods
266 this.state.addMethod2gen(md);
268 Iterator it_classes = this.state.getClassSymbolTable().getDescriptorsIterator();
269 while(it_classes.hasNext()) {
270 ClassDescriptor cd = (ClassDescriptor)it_classes.next();
271 Iterator it_methods = cd.getMethodTable().getDescriptorsIterator();
272 while(it_methods.hasNext()) {
273 md = (MethodDescriptor)it_methods.next();
274 if(md.isStaticBlock() || md.getModifiers().isNative() || this.callgraph.getCallerSet(md).size() > 0
275 || (cd.getSymbol().equals("Thread") && md.getSymbol().equals("staticStart"))) {
276 this.state.addMethod2gen(md);
283 /* This method goes though the call graph and tag those methods that are
284 * invoked inside static blocks
286 protected void tagMethodInvokedByStaticBlock() {
287 Iterator it_sclasses = this.state.getSClassSymbolTable().getDescriptorsIterator();
288 MethodDescriptor current_md=null;
289 HashSet tovisit=new HashSet();
290 HashSet visited=new HashSet();
292 while(it_sclasses.hasNext()) {
293 ClassDescriptor cd = (ClassDescriptor)it_sclasses.next();
294 MethodDescriptor md = (MethodDescriptor)cd.getMethodTable().get("staticblocks");
300 while(!tovisit.isEmpty()) {
301 current_md=(MethodDescriptor)tovisit.iterator().next();
302 tovisit.remove(current_md);
303 visited.add(current_md);
304 Iterator it_callee = this.callgraph.getCalleeSet(current_md).iterator();
305 while(it_callee.hasNext()) {
306 Descriptor d = (Descriptor)it_callee.next();
307 if(d instanceof MethodDescriptor) {
308 if(!visited.contains(d)) {
309 ((MethodDescriptor)d).setIsInvokedByStatic(true);
317 /* This code generates code for each static block and static field
319 protected void outputStaticBlocks(PrintWriter outmethod) {
320 // execute all the static blocks and all the static field initializations
321 // execute all the static blocks and all the static field initializations
322 SymbolTable sctbl = this.state.getSClassSymbolTable();
323 Iterator it_sclasses = sctbl.getDescriptorsIterator();
324 if(it_sclasses.hasNext()) {
325 while(it_sclasses.hasNext()) {
326 ClassDescriptor t_cd = (ClassDescriptor)it_sclasses.next();
327 MethodDescriptor t_md = (MethodDescriptor)t_cd.getMethodTable().get("staticblocks");
329 outmethod.println(" {");
330 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
331 outmethod.print(" struct "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
332 outmethod.println("0, NULL};");
333 outmethod.println(" "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
335 outmethod.println(" "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
337 outmethod.println(" }");
343 /* This code generates code to create a Class object for each class for
346 protected void outputClassObjects(PrintWriter outmethod) {
347 // for each class, initialize its Class object
348 SymbolTable ctbl = this.state.getClassSymbolTable();
349 for(Iterator it_classes = ctbl.getDescriptorsIterator();it_classes.hasNext();) {
350 ClassDescriptor t_cd = (ClassDescriptor)it_classes.next();
351 outmethod.println(" {");
352 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
353 outmethod.println(" struct garbagelist dummy={0,NULL};");
354 outmethod.println(" global_defs_p->"+t_cd.getSafeSymbol()+"classobj = allocate_new(&dummy, " + typeutil.getClass(TypeUtil.ObjectClass).getId() + ");");
356 outmethod.println(" global_defs_p->"+t_cd.getSafeSymbol()+"classobj = allocate_new("+typeutil.getClass(TypeUtil.ObjectClass).getId() + ");");
357 outmethod.println(" global_defs_p->"+t_cd.getSafeSymbol()+"classobj->type = " + t_cd.getId() + ";");
358 outmethod.println(" }");
362 /* This code just generates the main C method for java programs.
363 * The main C method packs up the arguments into a string array
364 * and passes it to the java main method. */
366 protected void outputMainMethod(PrintWriter outmethod) {
367 outmethod.println("int main(int argc, const char *argv[]) {");
368 outmethod.println(" int i;");
369 outmethod.println(" global_defs_p=calloc(1, sizeof(struct global_defs_t));");
370 outmethod.println(" global_defsprim_p=calloc(1, sizeof(struct global_defsprim_t));");
371 if (GENERATEPRECISEGC) {
372 outmethod.println(" global_defs_p->size="+globaldefscount+";");
373 outmethod.println(" for(i=0;i<"+globaldefscount+";i++) {");
374 outmethod.println(" ((struct garbagelist *)global_defs_p)->array[i]=NULL;");
375 outmethod.println(" }");
377 outputStaticBlocks(outmethod);
378 outputClassObjects(outmethod);
381 additionalCodeAtTopOfMain(outmethod);
384 outmethod.println("initializethreads();");
387 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
388 outmethod.println(" struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);");
390 outmethod.println(" struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);");
392 outmethod.println(" for(i=1;i<argc;i++) {");
393 outmethod.println(" int length=strlen(argv[i]);");
395 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
396 outmethod.println(" struct ___String___ *newstring=NewString(NULL, argv[i], length);");
398 outmethod.println(" struct ___String___ *newstring=NewString(argv[i], length);");
400 outmethod.println(" ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;");
401 outmethod.println(" }");
403 MethodDescriptor md=typeutil.getMain();
404 ClassDescriptor cd=typeutil.getMainClass();
406 outmethod.println(" {");
407 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
408 outmethod.print(" struct "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params __parameterlist__={");
409 outmethod.println("1, NULL,"+"stringarray};");
410 outmethod.println(" "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(& __parameterlist__);");
412 outmethod.println(" "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(stringarray);");
414 outmethod.println(" }");
417 outmethod.println("pthread_mutex_lock(&gclistlock);");
418 outmethod.println("threadcount--;");
419 outmethod.println("pthread_cond_signal(&gccond);");
420 outmethod.println("pthread_mutex_unlock(&gclistlock);");
423 if (state.EVENTMONITOR) {
424 outmethod.println("dumpdata();");
428 outmethod.println("pthread_exit(NULL);");
431 additionalCodeAtBottomOfMain(outmethod);
433 outmethod.println("}");
436 /* This method outputs code for each task. */
438 protected void outputTaskCode(PrintWriter outtaskdefs, PrintWriter outmethod) {
439 /* Compile task based program */
440 outtaskdefs.println("#include \"task.h\"");
441 outtaskdefs.println("#include \"methodheaders.h\"");
442 Iterator taskit=state.getTaskSymbolTable().getDescriptorsIterator();
443 while(taskit.hasNext()) {
444 TaskDescriptor td=(TaskDescriptor)taskit.next();
445 FlatMethod fm=state.getMethodFlat(td);
446 generateFlatMethod(fm, outmethod);
447 generateTaskDescriptor(outtaskdefs, fm, td);
450 //Output task descriptors
451 taskit=state.getTaskSymbolTable().getDescriptorsIterator();
452 outtaskdefs.println("struct taskdescriptor * taskarray[]= {");
454 while(taskit.hasNext()) {
455 TaskDescriptor td=(TaskDescriptor)taskit.next();
459 outtaskdefs.println(",");
460 outtaskdefs.print("&task_"+td.getSafeSymbol());
462 outtaskdefs.println("};");
464 outtaskdefs.println("int numtasks="+state.getTaskSymbolTable().getValueSet().size()+";");
468 /* This method outputs most of the methods.c file. This includes
469 * some standard includes and then an array with the sizes of
470 * objets and array that stores supertype and then the code for
471 * the Java methods.. */
472 protected void outputMethods(PrintWriter outmethod) {
473 outmethod.println("#include \"methodheaders.h\"");
474 outmethod.println("#include \"virtualtable.h\"");
475 outmethod.println("#include \"runtime.h\"");
477 // always include: compiler directives will leave out
478 // instrumentation when option is not set
479 outmethod.println("#include \"coreprof/coreprof.h\"");
481 if (state.FASTCHECK) {
482 outmethod.println("#include \"localobjects.h\"");
484 if(state.MULTICORE) {
486 outmethod.println("#include \"task.h\"");
488 outmethod.println("#include \"multicoreruntime.h\"");
489 outmethod.println("#include \"runtime_arch.h\"");
491 if (state.THREAD||state.DSM||state.SINGLETM) {
492 outmethod.println("#include <thread.h>");
495 outmethod.println("#include \"thread.h\"");
497 if (state.main!=null) {
498 outmethod.println("#include <string.h>");
500 if (state.CONSCHECK) {
501 outmethod.println("#include \"checkers.h\"");
505 additionalIncludesMethodsImplementation(outmethod);
507 outmethod.println("struct global_defs_t * global_defs_p;");
508 outmethod.println("struct global_defsprim_t * global_defsprim_p;");
509 //Store the sizes of classes & array elements
510 generateSizeArray(outmethod);
512 //Store table of supertypes
513 generateSuperTypeTable(outmethod);
515 //Store the layout of classes
516 generateLayoutStructs(outmethod);
519 additionalCodeAtTopMethodsImplementation(outmethod);
521 generateMethods(outmethod);
524 protected void generateMethods(PrintWriter outmethod) {
525 /* Generate code for methods */
526 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
527 while(classit.hasNext()) {
528 ClassDescriptor cn=(ClassDescriptor)classit.next();
529 Iterator methodit=cn.getMethods();
530 while(methodit.hasNext()) {
531 /* Classify parameters */
532 MethodDescriptor md=(MethodDescriptor)methodit.next();
533 if(!this.state.genAllMethods) {
534 boolean foundmatch = false;
535 Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
536 for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
537 MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
538 if (md.matches(matchmd)) {
547 FlatMethod fm=state.getMethodFlat(md);
548 if (!md.getModifiers().isNative()) {
549 generateFlatMethod(fm, outmethod);
555 protected void outputStructs(PrintWriter outstructs) {
556 outstructs.println("#ifndef STRUCTDEFS_H");
557 outstructs.println("#define STRUCTDEFS_H");
558 outstructs.println("#include \"classdefs.h\"");
559 outstructs.println("#ifndef INTPTR");
560 outstructs.println("#ifdef BIT64");
561 outstructs.println("#define INTPTR long");
562 outstructs.println("#else");
563 outstructs.println("#define INTPTR int");
564 outstructs.println("#endif");
565 outstructs.println("#endif");
568 additionalIncludesStructsHeader(outstructs);
571 /* Output #defines that the runtime uses to determine type
572 * numbers for various objects it needs */
573 outstructs.println("#define MAXCOUNT "+maxcount);
575 outstructs.println("#define STRINGARRAYTYPE "+
576 (state.getArrayNumber(
577 (new TypeDescriptor(typeutil.getClass(TypeUtil.StringClass))).makeArray(state))+state.numClasses()));
579 outstructs.println("#define OBJECTARRAYTYPE "+
580 (state.getArrayNumber(
581 (new TypeDescriptor(typeutil.getClass(TypeUtil.ObjectClass))).makeArray(state))+state.numClasses()));
584 outstructs.println("#define STRINGTYPE "+typeutil.getClass(TypeUtil.StringClass).getId());
585 outstructs.println("#define CHARARRAYTYPE "+
586 (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.CHAR)).makeArray(state))+state.numClasses()));
588 outstructs.println("#define BYTEARRAYTYPE "+
589 (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state))+state.numClasses()));
591 outstructs.println("#define BYTEARRAYARRAYTYPE "+
592 (state.getArrayNumber((new TypeDescriptor(TypeDescriptor.BYTE)).makeArray(state).makeArray(state))+state.numClasses()));
594 outstructs.println("#define NUMCLASSES "+state.numClasses());
595 int totalClassSize = state.numClasses() + state.numArrays() + state.numInterfaces();
596 outstructs.println("#define TOTALNUMCLASSANDARRAY "+ totalClassSize);
598 outstructs.println("#define STARTUPTYPE "+typeutil.getClass(TypeUtil.StartupClass).getId());
599 outstructs.println("#define TAGTYPE "+typeutil.getClass(TypeUtil.TagClass).getId());
600 outstructs.println("#define TAGARRAYTYPE "+
601 (state.getArrayNumber(new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass)).makeArray(state))+state.numClasses()));
605 protected void outputClassDeclarations(PrintWriter outclassdefs, PrintWriter outglobaldefs, PrintWriter outglobaldefsprim) {
606 if (state.THREAD||state.DSM||state.SINGLETM)
607 outclassdefs.println("#include <pthread.h>");
608 outclassdefs.println("#ifndef INTPTR");
609 outclassdefs.println("#ifdef BIT64");
610 outclassdefs.println("#define INTPTR long");
611 outclassdefs.println("#else");
612 outclassdefs.println("#define INTPTR int");
613 outclassdefs.println("#endif");
614 outclassdefs.println("#endif");
616 outclassdefs.println("#include \"optionalstruct.h\"");
617 outclassdefs.println("struct "+arraytype+";");
618 /* Start by declaring all structs */
619 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
620 while(it.hasNext()) {
621 ClassDescriptor cn=(ClassDescriptor)it.next();
622 outclassdefs.println("struct "+cn.getSafeSymbol()+";");
624 if((cn.getNumStaticFields() != 0) || (cn.getNumStaticBlocks() != 0)) {
625 // this class has static fields/blocks, need to add a global flag to
626 // indicate if its static fields have been initialized and/or if its
627 // static blocks have been executed
628 outglobaldefsprim.println(" int "+cn.getSafeSymbol()+"static_block_exe_flag;");
631 // for each class, create a global object
632 outglobaldefs.println(" struct ___Object___ *"+cn.getSafeSymbol()+"classobj;");
635 outclassdefs.println("");
636 //Print out definition for array type
637 outclassdefs.println("struct "+arraytype+" {");
638 outclassdefs.println(" int type;");
641 additionalClassObjectFields(outclassdefs);
644 if (state.EVENTMONITOR) {
645 outclassdefs.println(" int objuid;");
648 outclassdefs.println(" pthread_t tid;");
649 outclassdefs.println(" void * lockentry;");
650 outclassdefs.println(" int lockcount;");
653 outclassdefs.println(" int mutex;");
654 outclassdefs.println(" volatile int notifycount;");
655 outclassdefs.println(" volatile int objlock;");
656 if(state.MULTICOREGC) {
657 outclassdefs.println(" int marked;");
661 outclassdefs.println(" int flag;");
662 if(!state.MULTICORE) {
663 outclassdefs.println(" void * flagptr;");
665 outclassdefs.println(" int version;");
666 outclassdefs.println(" int * lock;"); // lock entry for this obj
667 outclassdefs.println(" int mutex;");
668 outclassdefs.println(" int lockcount;");
669 if(state.MULTICOREGC) {
670 outclassdefs.println(" int marked;");
674 outclassdefs.println(" int numfses;");
675 outclassdefs.println(" int * fses;");
679 printClassStruct(typeutil.getClass(TypeUtil.ObjectClass), outclassdefs, outglobaldefs, outglobaldefsprim);
680 printedfieldstbl.clear();
681 printExtraArrayFields(outclassdefs);
682 if (state.ARRAYPAD) {
683 outclassdefs.println(" int paddingforarray;");
686 outclassdefs.println(" int ___length___;");
687 outclassdefs.println("};\n");
690 // TODO add version for normal Java later
691 outclassdefs.println("");
692 //Print out definition for Class type
693 outclassdefs.println("struct Class {");
694 outclassdefs.println(" int type;");
697 additionalClassObjectFields(outclassdefs);
700 if (state.EVENTMONITOR) {
701 outclassdefs.println(" int objuid;");
704 outclassdefs.println(" pthread_t tid;");
705 outclassdefs.println(" void * lockentry;");
706 outclassdefs.println(" int lockcount;");
709 outclassdefs.println(" int mutex;");
710 outclassdefs.println(" volatile int notifycount;");
711 outclassdefs.println(" volatile int objlock;");
712 if(state.MULTICOREGC) {
713 outclassdefs.println(" int marked;");
717 outclassdefs.println(" int flag;");
718 if(!state.MULTICORE) {
719 outclassdefs.println(" void * flagptr;");
721 outclassdefs.println(" int version;");
722 outclassdefs.println(" int * lock;"); // lock entry for this obj
723 outclassdefs.println(" int mutex;");
724 outclassdefs.println(" int lockcount;");
725 if(state.MULTICOREGC) {
726 outclassdefs.println(" int marked;");
730 outclassdefs.println(" int numfses;");
731 outclassdefs.println(" int * fses;");
734 printClassStruct(typeutil.getClass(TypeUtil.ObjectClass), outclassdefs, outglobaldefs, outglobaldefsprim);
735 printedfieldstbl.clear();
736 outclassdefs.println("};\n");
739 outclassdefs.println("");
740 outclassdefs.println("extern int classsize[];");
741 outclassdefs.println("extern int hasflags[];");
742 outclassdefs.println("extern unsigned INTPTR * pointerarray[];");
743 outclassdefs.println("extern int* supertypes[];");
744 outclassdefs.println("");
747 /** Prints out definitions for generic task structures */
749 protected void outputTaskTypes(PrintWriter outtask) {
750 outtask.println("#ifndef _TASK_H");
751 outtask.println("#define _TASK_H");
752 outtask.println("struct parameterdescriptor {");
753 outtask.println("int type;");
754 outtask.println("int numberterms;");
755 outtask.println("int *intarray;");
756 outtask.println("void * queue;");
757 outtask.println("int numbertags;");
758 outtask.println("int *tagarray;");
759 outtask.println("};");
761 outtask.println("struct taskdescriptor {");
762 outtask.println("void * taskptr;");
763 outtask.println("int numParameters;");
764 outtask.println(" int numTotal;");
765 outtask.println("struct parameterdescriptor **descriptorarray;");
766 outtask.println("char * name;");
767 outtask.println("};");
768 outtask.println("extern struct taskdescriptor * taskarray[];");
769 outtask.println("extern numtasks;");
770 outtask.println("#endif");
774 protected void buildRepairStructs(PrintWriter outrepairstructs) {
775 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
776 while(classit.hasNext()) {
777 ClassDescriptor cn=(ClassDescriptor)classit.next();
778 outrepairstructs.println("structure "+cn.getSymbol()+" {");
779 outrepairstructs.println(" int __type__;");
781 outrepairstructs.println(" int __flag__;");
782 if(!state.MULTICORE) {
783 outrepairstructs.println(" int __flagptr__;");
786 printRepairStruct(cn, outrepairstructs);
787 outrepairstructs.println("}\n");
790 for(int i=0; i<state.numArrays(); i++) {
791 TypeDescriptor tdarray=arraytable[i];
792 TypeDescriptor tdelement=tdarray.dereference();
793 outrepairstructs.println("structure "+arraytype+"_"+state.getArrayNumber(tdarray)+" {");
794 outrepairstructs.println(" int __type__;");
795 printRepairStruct(typeutil.getClass(TypeUtil.ObjectClass), outrepairstructs);
796 outrepairstructs.println(" int length;");
797 outrepairstructs.println("}\n");
801 protected void printRepairStruct(ClassDescriptor cn, PrintWriter output) {
802 ClassDescriptor sp=cn.getSuperDesc();
804 printRepairStruct(sp, output);
806 Vector fields=(Vector)fieldorder.get(cn);
808 for(int i=0; i<fields.size(); i++) {
809 FieldDescriptor fd=(FieldDescriptor)fields.get(i);
810 if (fd.getType().isArray()) {
811 output.println(" "+arraytype+"_"+ state.getArrayNumber(fd.getType()) +" * "+fd.getSymbol()+";");
812 } else if (fd.getType().isClass())
813 output.println(" "+fd.getType().getRepairSymbol()+" * "+fd.getSymbol()+";");
814 else if (fd.getType().isFloat())
815 output.println(" int "+fd.getSymbol()+"; /* really float */");
817 output.println(" "+fd.getType().getRepairSymbol()+" "+fd.getSymbol()+";");
821 /** This method outputs TaskDescriptor information */
822 protected void generateTaskDescriptor(PrintWriter output, FlatMethod fm, TaskDescriptor task) {
823 for (int i=0; i<task.numParameters(); i++) {
824 VarDescriptor param_var=task.getParameter(i);
825 TypeDescriptor param_type=task.getParamType(i);
826 FlagExpressionNode param_flag=task.getFlag(param_var);
827 TagExpressionList param_tag=task.getTag(param_var);
830 if (param_flag==null) {
831 output.println("int parameterdnf_"+i+"_"+task.getSafeSymbol()+"[]={");
832 output.println("0x0, 0x0 };");
835 DNFFlag dflag=param_flag.getDNF();
836 dnfterms=dflag.size();
838 Hashtable flags=(Hashtable)flagorder.get(param_type.getClassDesc());
839 output.println("int parameterdnf_"+i+"_"+task.getSafeSymbol()+"[]={");
840 for(int j=0; j<dflag.size(); j++) {
843 Vector term=dflag.get(j);
846 for(int k=0; k<term.size(); k++) {
847 DNFFlagAtom dfa=(DNFFlagAtom)term.get(k);
848 FlagDescriptor fd=dfa.getFlag();
849 boolean negated=dfa.getNegated();
850 int flagid=1<<((Integer)flags.get(fd)).intValue();
855 output.print("0x"+Integer.toHexString(andmask)+", 0x"+Integer.toHexString(checkmask));
857 output.println("};");
860 output.println("int parametertag_"+i+"_"+task.getSafeSymbol()+"[]={");
862 for(int j=0; j<param_tag.numTags(); j++) {
865 /* for each tag we need */
866 /* which slot it is */
867 /* what type it is */
868 TagVarDescriptor tvd=(TagVarDescriptor)task.getParameterTable().get(param_tag.getName(j));
869 TempDescriptor tmp=param_tag.getTemp(j);
870 int slot=fm.getTagInt(tmp);
871 output.println(slot+", "+state.getTagId(tvd.getTag()));
873 output.println("};");
875 output.println("struct parameterdescriptor parameter_"+i+"_"+task.getSafeSymbol()+"={");
876 output.println("/* type */"+param_type.getClassDesc().getId()+",");
877 output.println("/* number of DNF terms */"+dnfterms+",");
878 output.println("parameterdnf_"+i+"_"+task.getSafeSymbol()+",");
879 output.println("0,");
881 output.println("/* number of tags */"+param_tag.numTags()+",");
883 output.println("/* number of tags */ 0,");
884 output.println("parametertag_"+i+"_"+task.getSafeSymbol());
885 output.println("};");
889 output.println("struct parameterdescriptor * parameterdescriptors_"+task.getSafeSymbol()+"[] = {");
890 for (int i=0; i<task.numParameters(); i++) {
893 output.print("¶meter_"+i+"_"+task.getSafeSymbol());
895 output.println("};");
897 output.println("struct taskdescriptor task_"+task.getSafeSymbol()+"={");
898 output.println("&"+task.getSafeSymbol()+",");
899 output.println("/* number of parameters */" +task.numParameters() + ",");
900 int numtotal=task.numParameters()+fm.numTags();
901 output.println("/* number total parameters */" +numtotal + ",");
902 output.println("parameterdescriptors_"+task.getSafeSymbol()+",");
903 output.println("\""+task.getSymbol()+"\"");
904 output.println("};");
908 /** The buildVirtualTables method outputs the virtual dispatch
909 * tables for methods. */
911 protected void buildVirtualTables(PrintWriter outvirtual) {
912 Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
913 while(classit.hasNext()) {
914 ClassDescriptor cd=(ClassDescriptor)classit.next();
915 if (virtualcalls.getMethodCount(cd)>maxcount)
916 maxcount=virtualcalls.getMethodCount(cd);
918 MethodDescriptor[][] virtualtable=null;
919 virtualtable=new MethodDescriptor[state.numClasses()+state.numArrays()][maxcount];
921 /* Fill in virtual table */
922 classit=state.getClassSymbolTable().getDescriptorsIterator();
923 while(classit.hasNext()) {
924 ClassDescriptor cd=(ClassDescriptor)classit.next();
925 if(cd.isInterface()) {
928 fillinRow(cd, virtualtable, cd.getId());
931 ClassDescriptor objectcd=typeutil.getClass(TypeUtil.ObjectClass);
932 Iterator arrayit=state.getArrayIterator();
933 while(arrayit.hasNext()) {
934 TypeDescriptor td=(TypeDescriptor)arrayit.next();
935 int id=state.getArrayNumber(td);
936 fillinRow(objectcd, virtualtable, id+state.numClasses());
939 outvirtual.print("void * virtualtable[]={");
940 boolean needcomma=false;
941 for(int i=0; i<state.numClasses()+state.numArrays(); i++) {
942 for(int j=0; j<maxcount; j++) {
944 outvirtual.print(", ");
945 if (virtualtable[i][j]!=null) {
946 MethodDescriptor md=virtualtable[i][j];
947 outvirtual.print("& "+md.getClassDesc().getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor());
949 outvirtual.print("0");
953 outvirtual.println("");
955 outvirtual.println("};");
959 protected void fillinRow(ClassDescriptor cd, MethodDescriptor[][] virtualtable, int rownum) {
960 /* Get inherited methods */
961 Iterator it_sifs = cd.getSuperInterfaces();
962 while(it_sifs.hasNext()) {
963 ClassDescriptor superif = (ClassDescriptor)it_sifs.next();
964 fillinRow(superif, virtualtable, rownum);
966 if (cd.getSuperDesc()!=null)
967 fillinRow(cd.getSuperDesc(), virtualtable, rownum);
968 /* Override them with our methods */
969 for(Iterator it=cd.getMethods(); it.hasNext(); ) {
970 MethodDescriptor md=(MethodDescriptor)it.next();
971 if (md.isStatic()||md.getReturnType()==null)
973 boolean foundmatch = false;
974 if(this.state.genAllMethods) {
977 Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
978 for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
979 MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
980 if (md.matches(matchmd)) {
989 int methodnum = virtualcalls.getMethodNumber(md);
990 virtualtable[rownum][methodnum]=md;
994 /** Generate array that contains the sizes of class objects. The
995 * object allocation functions in the runtime use this
998 protected void generateSizeArray(PrintWriter outclassdefs) {
999 outclassdefs.print("extern struct prefetchCountStats * evalPrefetch;\n");
1000 generateSizeArrayExtensions(outclassdefs);
1002 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
1003 cdarray=new ClassDescriptor[state.numClasses()];
1004 ifarray = new ClassDescriptor[state.numInterfaces()];
1006 int interfaceid = 0;
1007 while(it.hasNext()) {
1008 ClassDescriptor cd=(ClassDescriptor)it.next();
1009 if(cd.isInterface()) {
1010 ifarray[cd.getId()] = cd;
1012 cdarray[cd.getId()] = cd;
1016 arraytable=new TypeDescriptor[state.numArrays()];
1018 Iterator arrayit=state.getArrayIterator();
1019 while(arrayit.hasNext()) {
1020 TypeDescriptor td=(TypeDescriptor)arrayit.next();
1021 int id=state.getArrayNumber(td);
1025 /* Print out types */
1026 outclassdefs.println("/* ");
1027 for(int i=0; i<state.numClasses(); i++) {
1028 ClassDescriptor cd=cdarray[i];
1030 outclassdefs.println("NULL " + i);
1032 outclassdefs.println(cd +" "+i);
1036 for(int i=0; i<state.numArrays(); i++) {
1037 TypeDescriptor arraytd=arraytable[i];
1038 outclassdefs.println(arraytd.toPrettyString() +" "+(i+state.numClasses()));
1041 for(int i=0; i<state.numInterfaces(); i++) {
1042 ClassDescriptor ifcd = ifarray[i];
1043 outclassdefs.println(ifcd +" "+(i+state.numClasses()+state.numArrays()));
1046 outclassdefs.println("*/");
1049 outclassdefs.print("int classsize[]={");
1051 boolean needcomma=false;
1052 for(int i=0; i<state.numClasses(); i++) {
1054 outclassdefs.print(", ");
1056 outclassdefs.print("sizeof(struct "+cdarray[i].getSafeSymbol()+")");
1058 outclassdefs.print("0");
1064 for(int i=0; i<state.numArrays(); i++) {
1066 outclassdefs.print(", ");
1067 TypeDescriptor tdelement=arraytable[i].dereference();
1068 if (tdelement.isArray()||tdelement.isClass()||tdelement.isNull())
1069 outclassdefs.print("sizeof(void *)");
1071 outclassdefs.print("sizeof("+tdelement.getSafeSymbol()+")");
1075 for(int i=0; i<state.numInterfaces(); i++) {
1077 outclassdefs.print(", ");
1078 outclassdefs.print("sizeof(struct "+ifarray[i].getSafeSymbol()+")");
1082 outclassdefs.println("};");
1084 ClassDescriptor objectclass=typeutil.getClass(TypeUtil.ObjectClass);
1086 outclassdefs.print("int typearray[]={");
1087 for(int i=0; i<state.numClasses(); i++) {
1088 ClassDescriptor cd=cdarray[i];
1089 ClassDescriptor supercd=i>0 ? cd.getSuperDesc() : null;
1090 if(supercd != null && supercd.isInterface()) {
1091 throw new Error("Super class can not be interfaces");
1094 outclassdefs.print(", ");
1096 outclassdefs.print("-1");
1098 outclassdefs.print(supercd.getId());
1102 for(int i=0; i<state.numArrays(); i++) {
1103 TypeDescriptor arraytd=arraytable[i];
1104 ClassDescriptor arraycd=arraytd.getClassDesc();
1105 if (arraycd==null) {
1107 outclassdefs.print(", ");
1108 outclassdefs.print(objectclass.getId());
1112 ClassDescriptor cd=arraycd.getSuperDesc();
1115 TypeDescriptor supertd=new TypeDescriptor(cd);
1116 supertd.setArrayCount(arraytd.getArrayCount());
1117 type=state.getArrayNumber(supertd);
1119 type+=state.numClasses();
1122 cd=cd.getSuperDesc();
1125 outclassdefs.print(", ");
1126 outclassdefs.print(type);
1130 for(int i=0; i<state.numInterfaces(); i++) {
1131 ClassDescriptor cd=ifarray[i];
1132 ClassDescriptor supercd=cd.getSuperDesc();
1133 if(supercd != null && supercd.isInterface()) {
1134 throw new Error("Super class can not be interfaces");
1137 outclassdefs.print(", ");
1139 outclassdefs.print("-1");
1141 outclassdefs.print(supercd.getId());
1145 outclassdefs.println("};");
1150 outclassdefs.print("int typearray2[]={");
1151 for(int i=0; i<state.numArrays(); i++) {
1152 TypeDescriptor arraytd=arraytable[i];
1153 ClassDescriptor arraycd=arraytd.getClassDesc();
1154 if (arraycd==null) {
1156 outclassdefs.print(", ");
1157 outclassdefs.print("-1");
1161 ClassDescriptor cd=arraycd.getSuperDesc();
1162 int level=arraytd.getArrayCount()-1;
1164 for(; level>0; level--) {
1165 TypeDescriptor supertd=new TypeDescriptor(objectclass);
1166 supertd.setArrayCount(level);
1167 type=state.getArrayNumber(supertd);
1169 type+=state.numClasses();
1174 outclassdefs.print(", ");
1175 outclassdefs.print(type);
1179 outclassdefs.println("};");
1182 /** Constructs params and temp objects for each method or task.
1183 * These objects tell the compiler which temps need to be
1186 protected void generateTempStructs(FlatMethod fm) {
1187 MethodDescriptor md=fm.getMethod();
1188 TaskDescriptor task=fm.getTask();
1189 ParamsObject objectparams=md!=null ? new ParamsObject(md,tag++) : new ParamsObject(task, tag++);
1191 paramstable.put(md, objectparams);
1193 paramstable.put(task, objectparams);
1195 for(int i=0; i<fm.numParameters(); i++) {
1196 TempDescriptor temp=fm.getParameter(i);
1197 TypeDescriptor type=temp.getType();
1198 if (type.isPtr()&&((GENERATEPRECISEGC) || (this.state.MULTICOREGC)))
1199 objectparams.addPtr(temp);
1201 objectparams.addPrim(temp);
1204 for(int i=0; i<fm.numTags(); i++) {
1205 TempDescriptor temp=fm.getTag(i);
1206 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC))
1207 objectparams.addPtr(temp);
1209 objectparams.addPrim(temp);
1212 TempObject objecttemps=md!=null ? new TempObject(objectparams,md,tag++) : new TempObject(objectparams, task, tag++);
1214 tempstable.put(md, objecttemps);
1216 tempstable.put(task, objecttemps);
1218 for(Iterator nodeit=fm.getNodeSet().iterator(); nodeit.hasNext(); ) {
1219 FlatNode fn=(FlatNode)nodeit.next();
1220 TempDescriptor[] writes=fn.writesTemps();
1221 for(int i=0; i<writes.length; i++) {
1222 TempDescriptor temp=writes[i];
1223 TypeDescriptor type=temp.getType();
1224 if (type.isPtr()&&((GENERATEPRECISEGC) || (this.state.MULTICOREGC)))
1225 objecttemps.addPtr(temp);
1227 objecttemps.addPrim(temp);
1232 /** This method outputs the following information about classes
1234 * (1) For classes, what are the locations of pointers.
1235 * (2) For arrays, does the array contain pointers or primitives.
1236 * (3) For classes, does the class contain flags.
1239 protected void generateLayoutStructs(PrintWriter output) {
1240 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
1241 while(it.hasNext()) {
1242 ClassDescriptor cn=(ClassDescriptor)it.next();
1243 output.println("unsigned INTPTR "+cn.getSafeSymbol()+"_pointers[]={");
1244 Iterator allit=cn.getFieldTable().getAllDescriptorsIterator();
1246 while(allit.hasNext()) {
1247 FieldDescriptor fd=(FieldDescriptor)allit.next();
1248 TypeDescriptor type=fd.getType();
1252 output.print(count);
1253 allit=cn.getFieldTable().getAllDescriptorsIterator();
1254 while(allit.hasNext()) {
1255 FieldDescriptor fd=(FieldDescriptor)allit.next();
1259 TypeDescriptor type=fd.getType();
1261 output.println(",");
1262 output.print("((unsigned INTPTR)&(((struct "+cn.getSafeSymbol() +" *)0)->"+
1263 fd.getSafeSymbol()+"))");
1266 output.println("};");
1268 output.println("unsigned INTPTR * pointerarray[]={");
1269 boolean needcomma=false;
1270 for(int i=0; i<state.numClasses(); i++) {
1271 ClassDescriptor cn=cdarray[i];
1273 output.println(",");
1276 output.print(cn.getSafeSymbol()+"_pointers");
1278 output.print("NULL");
1282 for(int i=0; i<state.numArrays(); i++) {
1284 output.println(", ");
1285 TypeDescriptor tdelement=arraytable[i].dereference();
1286 if (tdelement.isArray()||tdelement.isClass())
1287 output.print("((unsigned INTPTR *)1)");
1293 output.println("};");
1295 output.println("int hasflags[]={");
1296 for(int i=0; i<state.numClasses(); i++) {
1297 ClassDescriptor cn=cdarray[i];
1299 output.println(", ");
1301 if ((cn != null) && (cn.hasFlags()))
1306 output.println("};");
1309 private int checkarraysupertype(ClassDescriptor arraycd, TypeDescriptor arraytd) {
1312 TypeDescriptor supertd=new TypeDescriptor(arraycd);
1313 supertd.setArrayCount(arraytd.getArrayCount());
1314 type=state.getArrayNumber(supertd);
1319 ClassDescriptor cd = arraycd.getSuperDesc();
1321 type = checkarraysupertype(cd, arraytd);
1327 Iterator it_sifs = arraycd.getSuperInterfaces();
1328 while(it_sifs.hasNext()) {
1329 ClassDescriptor ifcd = (ClassDescriptor)it_sifs.next();
1330 type = checkarraysupertype(ifcd, arraytd);
1340 /** Print out table to give us supertypes */
1341 protected void generateSuperTypeTable(PrintWriter output) {
1342 ClassDescriptor objectclass=typeutil.getClass(TypeUtil.ObjectClass);
1343 for(int i=0; i<state.numClasses(); i++) {
1344 ClassDescriptor cn=cdarray[i];
1348 output.print("int supertypes" + cn.getSafeSymbol() + "[] = {");
1349 boolean ncomma = false;
1351 if((cn != null) && (cn.getSuperDesc() != null)) {
1354 Iterator it_sifs = cn != null? cn.getSuperInterfaces() : null;
1355 while(it_sifs != null && it_sifs.hasNext()) {
1361 if ((cn != null) && (cn.getSuperDesc()!=null)) {
1365 ClassDescriptor cdsuper=cn.getSuperDesc();
1366 output.print(cdsuper.getId());
1368 it_sifs = cn != null? cn.getSuperInterfaces() : null;
1369 while(it_sifs != null && it_sifs.hasNext()) {
1373 output.print(((ClassDescriptor)it_sifs.next()).getId()+state.numClasses()+state.numArrays());
1376 output.println("};");
1379 for(int i=0; i<state.numArrays(); i++) {
1380 TypeDescriptor arraytd=arraytable[i];
1381 ClassDescriptor arraycd=arraytd.getClassDesc();
1382 output.print("int supertypes___arraytype___" + (i+state.numClasses()) + "[] = {");
1383 boolean ncomma = false;
1385 if (arraycd==null) {
1389 output.print(objectclass.getId());
1390 output.println("};");
1393 if((arraycd != null) && (arraycd.getSuperDesc() != null)) {
1396 Iterator it_sifs = arraycd != null? arraycd.getSuperInterfaces() : null;
1397 while(it_sifs != null && it_sifs.hasNext()) {
1403 if ((arraycd != null) && (arraycd.getSuperDesc()!=null)) {
1404 ClassDescriptor cd=arraycd.getSuperDesc();
1407 type = checkarraysupertype(cd, arraytd);
1409 type += state.numClasses();
1416 it_sifs = arraycd != null? arraycd.getSuperInterfaces() : null;
1417 while(it_sifs != null && it_sifs.hasNext()) {
1418 ClassDescriptor ifcd = (ClassDescriptor)it_sifs.next();
1419 int type = checkarraysupertype(ifcd , arraytd);
1421 type += state.numClasses();
1427 output.println("};");
1430 for(int i=0; i<state.numInterfaces(); i++) {
1431 ClassDescriptor cn=ifarray[i];
1435 output.print("int supertypes" + cn.getSafeSymbol() + "[] = {");
1436 boolean ncomma = false;
1438 if((cn != null) && (cn.getSuperDesc() != null)) {
1441 Iterator it_sifs = cn != null? cn.getSuperInterfaces() : null;
1442 while(it_sifs != null && it_sifs.hasNext()) {
1448 if ((cn != null) && (cn.getSuperDesc()!=null)) {
1452 ClassDescriptor cdsuper=cn.getSuperDesc();
1453 output.print(cdsuper.getId());
1455 it_sifs = cn != null? cn.getSuperInterfaces() : null;
1456 while(it_sifs != null && it_sifs.hasNext()) {
1460 output.print(((ClassDescriptor)it_sifs.next()).getId()+state.numClasses()+state.numArrays());
1463 output.println("};");
1466 output.println("int* supertypes[]={");
1467 boolean needcomma=false;
1468 for(int i=0; i<state.numClasses(); i++) {
1469 ClassDescriptor cn=cdarray[i];
1471 output.println(",");
1474 output.print("supertypes" + cn.getSafeSymbol());
1480 for(int i=0; i<state.numArrays(); i++) {
1482 output.println(",");
1484 output.print("supertypes___arraytype___" + (i+state.numClasses()));
1487 for(int i=0; i<state.numInterfaces(); i++) {
1488 ClassDescriptor cn=ifarray[i];
1490 output.println(",");
1492 output.print("supertypes" + cn.getSafeSymbol());
1494 output.println("};");
1497 /** Force consistent field ordering between inherited classes. */
1499 protected void printClassStruct(ClassDescriptor cn, PrintWriter classdefout, PrintWriter globaldefout, PrintWriter globaldefprimout) {
1501 ClassDescriptor sp=cn.getSuperDesc();
1503 printClassStruct(sp, classdefout, /*globaldefout*/ null, null);
1505 SymbolTable sitbl = cn.getSuperInterfaceTable();
1506 Iterator it_sifs = sitbl.getDescriptorsIterator();
1507 while(it_sifs.hasNext()) {
1508 ClassDescriptor si = (ClassDescriptor)it_sifs.next();
1509 printClassStruct(si, classdefout, /*globaldefout*/ null, null);
1512 if (!fieldorder.containsKey(cn)) {
1513 Vector fields=new Vector();
1514 fieldorder.put(cn,fields);
1516 Vector fieldvec=cn.getFieldVec();
1517 for(int i=0; i<fieldvec.size(); i++) {
1518 FieldDescriptor fd=(FieldDescriptor)fieldvec.get(i);
1519 if((sp != null) && sp.getFieldTable().contains(fd.getSymbol())) {
1522 it_sifs = sitbl.getDescriptorsIterator();
1523 boolean hasprinted = false;
1524 while(it_sifs.hasNext()) {
1525 ClassDescriptor si = (ClassDescriptor)it_sifs.next();
1526 if(si.getFieldTable().contains(fd.getSymbol())) {
1532 // this field has been defined in the super class
1539 //Vector fields=(Vector)fieldorder.get(cn);
1541 Vector fields = cn.getFieldVec();
1543 for(int i=0; i<fields.size(); i++) {
1544 FieldDescriptor fd=(FieldDescriptor)fields.get(i);
1545 String fstring = fd.getSafeSymbol();
1546 if(printedfieldstbl.containsKey(fstring)) {
1547 printedfieldstbl.put(fstring, cn);
1550 printedfieldstbl.put(fstring, cn);
1552 if (fd.getType().isClass()
1553 && fd.getType().getClassDesc().isEnum()) {
1554 classdefout.println(" int " + fd.getSafeSymbol() + ";");
1555 } else if (fd.getType().isClass()||fd.getType().isArray()) {
1556 if (fd.isStatic()) {
1557 // TODO add version for normal Java later
1559 if(globaldefout != null) {
1560 if(fd.isVolatile()) {
1561 globaldefout.println(" volatile struct "+fd.getType().getSafeSymbol()+ " * "+fd.getSafeSymbol()+";");
1563 globaldefout.println(" struct "+fd.getType().getSafeSymbol()+ " * "+fd.getSafeSymbol()+";");
1567 } else if (fd.isVolatile()) {
1569 classdefout.println(" volatile struct "+fd.getType().getSafeSymbol()+ " * "+fd.getSafeSymbol()+";");
1571 classdefout.println(" struct "+fd.getType().getSafeSymbol()+" * "+fd.getSafeSymbol()+";");
1573 } else if (fd.isStatic()) {
1574 // TODO add version for normal Java later
1576 if(globaldefout != null) {
1577 if(fd.isVolatile()) {
1578 globaldefprimout.println(" volatile "+fd.getType().getSafeSymbol()+ " "+fd.getSafeSymbol()+";");
1580 globaldefprimout.println(" "+fd.getType().getSafeSymbol()+ " "+fd.getSafeSymbol()+";");
1584 } else if (fd.isVolatile()) {
1586 classdefout.println(" volatile "+fd.getType().getSafeSymbol()+ " "+fd.getSafeSymbol()+";");
1588 classdefout.println(" "+fd.getType().getSafeSymbol()+" "+fd.getSafeSymbol()+";");
1593 /* Map flags to integers consistently between inherited
1596 protected void mapFlags(ClassDescriptor cn) {
1597 ClassDescriptor sp=cn.getSuperDesc();
1601 if (!flagorder.containsKey(cn)) {
1602 Hashtable flags=new Hashtable();
1603 flagorder.put(cn,flags);
1605 Hashtable superflags=(Hashtable)flagorder.get(sp);
1606 Iterator superflagit=superflags.keySet().iterator();
1607 while(superflagit.hasNext()) {
1608 FlagDescriptor fd=(FlagDescriptor)superflagit.next();
1609 Integer number=(Integer)superflags.get(fd);
1610 flags.put(fd, number);
1611 if ((number.intValue()+1)>max)
1612 max=number.intValue()+1;
1616 Iterator flagit=cn.getFlags();
1617 while(flagit.hasNext()) {
1618 FlagDescriptor fd=(FlagDescriptor)flagit.next();
1619 if (sp==null||!sp.getFlagTable().contains(fd.getSymbol()))
1620 flags.put(fd, new Integer(max++));
1626 /** This function outputs (1) structures that parameters are
1627 * passed in (when PRECISE GC is enabled) and (2) function
1628 * prototypes for the methods */
1630 protected void generateCallStructs(ClassDescriptor cn, PrintWriter classdefout, PrintWriter output, PrintWriter headersout, PrintWriter globaldefout, PrintWriter globaldefprimout) {
1631 /* Output class structure */
1632 classdefout.println("struct "+cn.getSafeSymbol()+" {");
1633 classdefout.println(" int type;");
1636 additionalClassObjectFields(classdefout);
1639 if (state.EVENTMONITOR) {
1640 classdefout.println(" int objuid;");
1643 classdefout.println(" pthread_t tid;");
1644 classdefout.println(" void * lockentry;");
1645 classdefout.println(" int lockcount;");
1648 classdefout.println(" int mutex;");
1649 classdefout.println(" volatile int notifycount;");
1650 classdefout.println(" volatile int objlock;");
1651 if(state.MULTICOREGC) {
1652 classdefout.println(" int marked;");
1656 classdefout.println(" int flag;");
1657 if((!state.MULTICORE) || (cn.getSymbol().equals("TagDescriptor"))) {
1658 classdefout.println(" void * flagptr;");
1659 } else if (state.MULTICORE) {
1660 classdefout.println(" int version;");
1661 classdefout.println(" int * lock;"); // lock entry for this obj
1662 classdefout.println(" int mutex;");
1663 classdefout.println(" int lockcount;");
1664 if(state.MULTICOREGC) {
1665 classdefout.println(" int marked;");
1668 if (state.OPTIONAL) {
1669 classdefout.println(" int numfses;");
1670 classdefout.println(" int * fses;");
1673 printClassStruct(cn, classdefout, globaldefout, globaldefprimout);
1674 printedfieldstbl.clear(); // = new Hashtable<String, ClassDescriptor>();
1675 classdefout.println("};\n");
1676 generateCallStructsMethods(cn, output, headersout);
1680 protected void generateCallStructsMethods(ClassDescriptor cn, PrintWriter output, PrintWriter headersout) {
1681 for(Iterator methodit=cn.getMethods(); methodit.hasNext(); ) {
1682 MethodDescriptor md=(MethodDescriptor)methodit.next();
1683 boolean foundmatch = false;
1684 if(this.state.genAllMethods) {
1687 Set vec_md = this.state.getMethod2gen().getSet(md.getSymbol());
1688 for(Iterator matchit=vec_md.iterator(); matchit.hasNext();) {
1689 MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
1690 if (md.matches(matchmd)) {
1697 generateMethod(cn, md, headersout, output);
1702 protected void generateMethodParam(ClassDescriptor cn, MethodDescriptor md, PrintWriter output) {
1703 /* Output parameter structure */
1704 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1705 if(md.isInvokedByStatic() && !md.isStaticBlock() && !md.getModifiers().isNative()) {
1706 // generate the staticinit version
1707 String mdstring = md.getSafeMethodDescriptor() + "staticinit";
1709 ParamsObject objectparams=(ParamsObject) paramstable.get(md);
1710 output.println("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_params {");
1711 output.println(" int size;");
1712 output.println(" void * next;");
1713 for(int i=0; i<objectparams.numPointers(); i++) {
1714 TempDescriptor temp=objectparams.getPointer(i);
1715 if(temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
1716 output.println(" int " + temp.getSafeSymbol() + ";");
1718 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1721 output.println("};\n");
1724 ParamsObject objectparams=(ParamsObject) paramstable.get(md);
1725 output.println("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params {");
1726 output.println(" int size;");
1727 output.println(" void * next;");
1728 for(int i=0; i<objectparams.numPointers(); i++) {
1729 TempDescriptor temp=objectparams.getPointer(i);
1730 if(temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
1731 output.println(" int " + temp.getSafeSymbol() + ";");
1733 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1736 output.println("};\n");
1740 protected void generateMethod(ClassDescriptor cn, MethodDescriptor md, PrintWriter headersout, PrintWriter output) {
1741 FlatMethod fm=state.getMethodFlat(md);
1742 generateTempStructs(fm);
1744 ParamsObject objectparams=(ParamsObject) paramstable.get(md);
1745 TempObject objecttemps=(TempObject) tempstable.get(md);
1747 boolean printcomma = false;
1749 generateMethodParam(cn, md, output);
1751 if(md.isInvokedByStatic() && !md.isStaticBlock() && !md.getModifiers().isNative()) {
1752 // generate the staticinit version
1753 String mdstring = md.getSafeMethodDescriptor() + "staticinit";
1755 /* Output temp structure */
1756 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1757 output.println("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_locals {");
1758 output.println(" int size;");
1759 output.println(" void * next;");
1760 for(int i=0; i<objecttemps.numPointers(); i++) {
1761 TempDescriptor temp=objecttemps.getPointer(i);
1762 if (!temp.getType().isArray() && temp.getType().isNull())
1763 output.println(" void * "+temp.getSafeSymbol()+";");
1765 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1767 output.println("};\n");
1770 headersout.println("#define D"+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+" 1");
1771 /* First the return type */
1772 if (md.getReturnType()!=null) {
1773 if(md.getReturnType().isClass() && md.getReturnType().getClassDesc().isEnum()) {
1774 headersout.println(" int ");
1775 } else if (md.getReturnType().isClass()||md.getReturnType().isArray())
1776 headersout.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
1778 headersout.print(md.getReturnType().getSafeSymbol()+" ");
1780 //catch the constructor case
1781 headersout.print("void ");
1783 /* Next the method name */
1784 headersout.print(cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"(");
1786 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1787 headersout.print("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_params * "+paramsprefix);
1791 /* Output parameter list*/
1792 for(int i=0; i<objectparams.numPrimitives(); i++) {
1793 TempDescriptor temp=objectparams.getPrimitive(i);
1795 headersout.print(", ");
1797 if(temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
1798 headersout.print("int " + temp.getSafeSymbol());
1799 } else if (temp.getType().isClass()||temp.getType().isArray())
1800 headersout.print("struct " + temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
1802 headersout.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
1804 headersout.println(");\n");
1807 /* Output temp structure */
1808 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1809 output.println("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_locals {");
1810 output.println(" int size;");
1811 output.println(" void * next;");
1812 for(int i=0; i<objecttemps.numPointers(); i++) {
1813 TempDescriptor temp=objecttemps.getPointer(i);
1814 if (!temp.getType().isArray() && temp.getType().isNull())
1815 output.println(" void * "+temp.getSafeSymbol()+";");
1817 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1819 output.println("};\n");
1822 /********* Output method declaration ***********/
1823 headersout.println("#define D"+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+" 1");
1824 /* First the return type */
1825 if (md.getReturnType()!=null) {
1826 if(md.getReturnType().isClass() && md.getReturnType().getClassDesc().isEnum()) {
1827 headersout.println(" int ");
1828 } else if (md.getReturnType().isClass()||md.getReturnType().isArray())
1829 headersout.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
1831 headersout.print(md.getReturnType().getSafeSymbol()+" ");
1833 //catch the constructor case
1834 headersout.print("void ");
1836 /* Next the method name */
1837 headersout.print(cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(");
1839 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1840 headersout.print("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params * "+paramsprefix);
1844 /* Output parameter list*/
1845 for(int i=0; i<objectparams.numPrimitives(); i++) {
1846 TempDescriptor temp=objectparams.getPrimitive(i);
1848 headersout.print(", ");
1850 if(temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
1851 headersout.print("int " + temp.getSafeSymbol());
1852 } else if (temp.getType().isClass()||temp.getType().isArray())
1853 headersout.print("struct " + temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
1855 headersout.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
1857 headersout.println(");\n");
1861 /** This function outputs (1) structures that parameters are
1862 * passed in (when PRECISE GC is enabled) and (2) function
1863 * prototypes for the tasks */
1865 protected void generateTaskStructs(PrintWriter output, PrintWriter headersout) {
1866 /* Cycle through tasks */
1867 Iterator taskit=state.getTaskSymbolTable().getDescriptorsIterator();
1869 while(taskit.hasNext()) {
1870 /* Classify parameters */
1871 TaskDescriptor task=(TaskDescriptor)taskit.next();
1872 FlatMethod fm=state.getMethodFlat(task);
1873 generateTempStructs(fm);
1875 ParamsObject objectparams=(ParamsObject) paramstable.get(task);
1876 TempObject objecttemps=(TempObject) tempstable.get(task);
1878 /* Output parameter structure */
1879 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1880 output.println("struct "+task.getSafeSymbol()+"_params {");
1881 output.println(" int size;");
1882 output.println(" void * next;");
1883 for(int i=0; i<objectparams.numPointers(); i++) {
1884 TempDescriptor temp=objectparams.getPointer(i);
1885 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1888 output.println("};\n");
1889 if ((objectparams.numPointers()+fm.numTags())>maxtaskparams) {
1890 maxtaskparams=objectparams.numPointers()+fm.numTags();
1894 /* Output temp structure */
1895 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1896 output.println("struct "+task.getSafeSymbol()+"_locals {");
1897 output.println(" int size;");
1898 output.println(" void * next;");
1899 for(int i=0; i<objecttemps.numPointers(); i++) {
1900 TempDescriptor temp=objecttemps.getPointer(i);
1901 if (!temp.getType().isArray() && temp.getType().isNull())
1902 output.println(" void * "+temp.getSafeSymbol()+";");
1903 else if(temp.getType().isTag())
1904 output.println(" struct "+
1905 (new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass))).getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1907 output.println(" struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+";");
1909 output.println("};\n");
1912 /* Output task declaration */
1913 headersout.print("void " + task.getSafeSymbol()+"(");
1915 boolean printcomma=false;
1916 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1917 headersout.print("struct "+task.getSafeSymbol()+"_params * "+paramsprefix);
1919 headersout.print("void * parameterarray[]");
1920 headersout.println(");\n");
1924 protected void generateFlatMethod(FlatMethod fm, PrintWriter output) {
1925 if (State.PRINTFLAT)
1926 System.out.println(fm.printMethod());
1927 MethodDescriptor md=fm.getMethod();
1928 TaskDescriptor task=fm.getTask();
1929 ClassDescriptor cn=md!=null ? md.getClassDesc() : null;
1930 ParamsObject objectparams=(ParamsObject)paramstable.get(md!=null ? md : task);
1932 if((md != null) && md.isInvokedByStatic() && !md.isStaticBlock() && !md.getModifiers().isNative()) {
1933 // generate a special static init version
1934 mgcstaticinit = true;
1935 String mdstring = md.getSafeMethodDescriptor() + "staticinit";
1937 generateHeader(fm, md!=null ? md : task,output);
1938 TempObject objecttemp=(TempObject) tempstable.get(md!=null ? md : task);
1940 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1941 output.print(" struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_locals "+localsprefix+"={");
1942 output.print(objecttemp.numPointers()+",");
1943 output.print(paramsprefix);
1944 for(int j=0; j<objecttemp.numPointers(); j++)
1945 output.print(", NULL");
1946 output.println("};");
1949 for(int i=0; i<objecttemp.numPrimitives(); i++) {
1950 TempDescriptor td=objecttemp.getPrimitive(i);
1951 TypeDescriptor type=td.getType();
1952 if (type.isNull() && !type.isArray())
1953 output.println(" void * "+td.getSafeSymbol()+";");
1954 else if (type.isClass() && type.getClassDesc().isEnum()) {
1955 output.println(" int " + td.getSafeSymbol() + ";");
1956 } else if (type.isClass()||type.isArray())
1957 output.println(" struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
1959 output.println(" "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
1962 additionalCodeAtTopFlatMethodBody(output, fm);
1964 /* Check to see if we need to do a GC if this is a
1965 * multi-threaded program...*/
1967 if (((state.OOOJAVA||state.THREAD)&&GENERATEPRECISEGC)
1968 || this.state.MULTICOREGC) {
1969 //Don't bother if we aren't in recursive methods...The loops case will catch it
1970 if (callgraph.getAllMethods(md).contains(md)) {
1971 if (this.state.MULTICOREGC) {
1972 output.println("if(gcflag) gc("+localsprefixaddr+");");
1974 output.println("if (unlikely(needtocollect)) checkcollect("+localsprefixaddr+");");
1979 generateCode(fm.getNext(0), fm, null, output);
1981 output.println("}\n\n");
1983 mgcstaticinit = false;
1986 generateHeader(fm, md!=null ? md : task,output);
1987 TempObject objecttemp=(TempObject) tempstable.get(md!=null ? md : task);
1989 if((md != null) && (md.isStaticBlock())) {
1990 mgcstaticinit = true;
1993 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
1995 output.print(" struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_locals "+localsprefix+"={");
1997 output.print(" struct "+task.getSafeSymbol()+"_locals "+localsprefix+"={");
1998 output.print(objecttemp.numPointers()+",");
1999 output.print(paramsprefix);
2000 for(int j=0; j<objecttemp.numPointers(); j++)
2001 output.print(", NULL");
2002 output.println("};");
2005 for(int i=0; i<objecttemp.numPrimitives(); i++) {
2006 TempDescriptor td=objecttemp.getPrimitive(i);
2007 TypeDescriptor type=td.getType();
2008 if (type.isNull() && !type.isArray())
2009 output.println(" void * "+td.getSafeSymbol()+";");
2010 else if (type.isClass() && type.getClassDesc().isEnum()) {
2011 output.println(" int " + td.getSafeSymbol() + ";");
2012 } else if (type.isClass()||type.isArray())
2013 output.println(" struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
2015 output.println(" "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
2018 additionalCodeAtTopFlatMethodBody(output, fm);
2020 /* Check to see if we need to do a GC if this is a
2021 * multi-threaded program...*/
2023 if (((state.OOOJAVA||state.THREAD)&&GENERATEPRECISEGC)
2024 || this.state.MULTICOREGC) {
2025 //Don't bother if we aren't in recursive methods...The loops case will catch it
2026 if (callgraph.getAllMethods(md).contains(md)) {
2027 if (this.state.MULTICOREGC) {
2028 output.println("if(gcflag) gc("+localsprefixaddr+");");
2030 output.println("if (unlikely(needtocollect)) checkcollect("+localsprefixaddr+");");
2035 if(fm.getMethod().isStaticBlock()) {
2036 // a static block, check if it has been executed
2037 output.println(" if(global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag != 0) {");
2038 output.println(" return;");
2039 output.println(" }");
2043 generateCode(fm.getNext(0), fm, null, output);
2045 output.println("}\n\n");
2047 mgcstaticinit = false;
2050 protected void generateCode(FlatNode first,
2052 Set<FlatNode> stopset,
2053 PrintWriter output) {
2055 /* Assign labels to FlatNode's if necessary.*/
2057 Hashtable<FlatNode, Integer> nodetolabel;
2059 nodetolabel=assignLabels(first, stopset);
2061 Set<FlatNode> storeset=null;
2062 HashSet<FlatNode> genset=null;
2063 HashSet<FlatNode> refset=null;
2064 Set<FlatNode> unionset=null;
2066 /* Do the actual code generation */
2067 FlatNode current_node=null;
2068 HashSet tovisit=new HashSet();
2069 HashSet visited=new HashSet();
2071 while(current_node!=null||!tovisit.isEmpty()) {
2072 if (current_node==null) {
2073 current_node=(FlatNode)tovisit.iterator().next();
2074 tovisit.remove(current_node);
2075 } else if (tovisit.contains(current_node)) {
2076 tovisit.remove(current_node);
2078 visited.add(current_node);
2079 if (nodetolabel.containsKey(current_node)) {
2080 output.println("L"+nodetolabel.get(current_node)+":");
2082 if (state.INSTRUCTIONFAILURE) {
2084 output.println("if ((++instructioncount)>failurecount) {instructioncount=0;injectinstructionfailure();}");
2086 output.println("if ((--instructioncount)==0) injectinstructionfailure();");
2088 if (current_node.numNext()==0||stopset!=null&&stopset.contains(current_node)) {
2090 generateFlatNode(fm, current_node, output);
2092 if (state.OOOJAVA && stopset!=null) {
2093 assert first.getPrev(0) instanceof FlatSESEEnterNode;
2094 assert current_node instanceof FlatSESEExitNode;
2095 FlatSESEEnterNode fsen = (FlatSESEEnterNode) first.getPrev(0);
2096 FlatSESEExitNode fsxn = (FlatSESEExitNode) current_node;
2097 assert fsen.getFlatExit().equals(fsxn);
2098 assert fsxn.getFlatEnter().equals(fsen);
2100 if (current_node.kind()!=FKind.FlatReturnNode) {
2101 if((fm.getMethod() != null) && (fm.getMethod().isStaticBlock())) {
2102 // a static block, check if it has been executed
2103 output.println(" global_defsprim_p->" + fm.getMethod().getClassDesc().getSafeSymbol()+"static_block_exe_flag = 1;");
2106 output.println(" return;");
2109 } else if(current_node.numNext()==1) {
2111 if (state.OOOJAVA &&
2112 current_node.kind()==FKind.FlatSESEEnterNode) {
2113 FlatSESEEnterNode fsen = (FlatSESEEnterNode)current_node;
2114 generateFlatNode(fm, current_node, output);
2115 nextnode=fsen.getFlatExit().getNext(0);
2118 generateFlatNode(fm, current_node, output);
2119 nextnode=current_node.getNext(0);
2121 if (visited.contains(nextnode)) {
2122 output.println("goto L"+nodetolabel.get(nextnode)+";");
2125 current_node=nextnode;
2126 } else if (current_node.numNext()==2) {
2129 generateFlatCondBranch(fm, (FlatCondBranch)current_node, "L"+nodetolabel.get(current_node.getNext(1)), output);
2130 if (!visited.contains(current_node.getNext(1)))
2131 tovisit.add(current_node.getNext(1));
2132 if (visited.contains(current_node.getNext(0))) {
2133 output.println("goto L"+nodetolabel.get(current_node.getNext(0))+";");
2136 current_node=current_node.getNext(0);
2137 } else throw new Error();
2141 protected Hashtable<FlatNode, Integer> assignLabels(FlatNode first, Set<FlatNode> lastset) {
2142 HashSet tovisit=new HashSet();
2143 HashSet visited=new HashSet();
2145 Hashtable<FlatNode, Integer> nodetolabel=new Hashtable<FlatNode, Integer>();
2148 /*Assign labels first. A node needs a label if the previous
2149 * node has two exits or this node is a join point. */
2151 while(!tovisit.isEmpty()) {
2152 FlatNode fn=(FlatNode)tovisit.iterator().next();
2157 if(lastset!=null&&lastset.contains(fn)) {
2158 // if last is not null and matches, don't go
2159 // any further for assigning labels
2163 for(int i=0; i<fn.numNext(); i++) {
2164 FlatNode nn=fn.getNext(i);
2167 //1) Edge >1 of node
2168 nodetolabel.put(nn,new Integer(labelindex++));
2170 if (!visited.contains(nn)&&!tovisit.contains(nn)) {
2174 nodetolabel.put(nn,new Integer(labelindex++));
2181 /** Generate text string that corresponds to the TempDescriptor td. */
2182 protected String generateTemp(FlatMethod fm, TempDescriptor td) {
2183 MethodDescriptor md=fm.getMethod();
2184 TaskDescriptor task=fm.getTask();
2185 TempObject objecttemps=(TempObject) tempstable.get(md!=null ? md : task);
2187 if (objecttemps.isLocalPrim(td)||objecttemps.isParamPrim(td)) {
2188 return td.getSafeSymbol();
2191 if (objecttemps.isLocalPtr(td)) {
2192 return localsprefixderef+td.getSafeSymbol();
2195 if (objecttemps.isParamPtr(td)) {
2196 return paramsprefix+"->"+td.getSafeSymbol();
2204 protected void generateFlatNode(FlatMethod fm, FlatNode fn, PrintWriter output) {
2205 if(state.LINENUM) printSourceLineNumber(fm,fn,output);
2206 additionalCodePreNode(fm, fn, output);
2209 case FKind.FlatAtomicEnterNode:
2210 generateFlatAtomicEnterNode(fm, (FlatAtomicEnterNode) fn, output);
2213 case FKind.FlatAtomicExitNode:
2214 generateFlatAtomicExitNode(fm, (FlatAtomicExitNode) fn, output);
2217 case FKind.FlatInstanceOfNode:
2218 generateFlatInstanceOfNode(fm, (FlatInstanceOfNode)fn, output);
2221 case FKind.FlatSESEEnterNode:
2222 generateFlatSESEEnterNode(fm, (FlatSESEEnterNode)fn, output);
2225 case FKind.FlatSESEExitNode:
2226 generateFlatSESEExitNode(fm, (FlatSESEExitNode)fn, output);
2229 case FKind.FlatWriteDynamicVarNode:
2230 generateFlatWriteDynamicVarNode(fm, (FlatWriteDynamicVarNode)fn, output);
2233 case FKind.FlatGlobalConvNode:
2234 generateFlatGlobalConvNode(fm, (FlatGlobalConvNode) fn, output);
2237 case FKind.FlatTagDeclaration:
2238 generateFlatTagDeclaration(fm, (FlatTagDeclaration) fn,output);
2241 case FKind.FlatCall:
2242 generateFlatCall(fm, (FlatCall) fn,output);
2245 case FKind.FlatFieldNode:
2246 generateFlatFieldNode(fm, (FlatFieldNode) fn,output);
2249 case FKind.FlatElementNode:
2250 generateFlatElementNode(fm, (FlatElementNode) fn,output);
2253 case FKind.FlatSetElementNode:
2254 generateFlatSetElementNode(fm, (FlatSetElementNode) fn,output);
2257 case FKind.FlatSetFieldNode:
2258 generateFlatSetFieldNode(fm, (FlatSetFieldNode) fn,output);
2262 generateFlatNew(fm, (FlatNew) fn,output);
2265 case FKind.FlatOpNode:
2266 generateFlatOpNode(fm, (FlatOpNode) fn,output);
2269 case FKind.FlatCastNode:
2270 generateFlatCastNode(fm, (FlatCastNode) fn,output);
2273 case FKind.FlatLiteralNode:
2274 generateFlatLiteralNode(fm, (FlatLiteralNode) fn,output);
2277 case FKind.FlatReturnNode:
2278 generateFlatReturnNode(fm, (FlatReturnNode) fn,output);
2282 output.println("/* nop */");
2285 case FKind.FlatGenReachNode:
2286 // this node is just for generating a reach graph
2287 // in disjointness analysis at a particular program point
2290 case FKind.FlatExit:
2291 output.println("/* exit */");
2294 case FKind.FlatBackEdge:
2295 generateFlatBackEdge(fm, (FlatBackEdge)fn, output);
2298 case FKind.FlatCheckNode:
2299 generateFlatCheckNode(fm, (FlatCheckNode) fn, output);
2302 case FKind.FlatFlagActionNode:
2303 generateFlatFlagActionNode(fm, (FlatFlagActionNode) fn, output);
2306 case FKind.FlatPrefetchNode:
2307 generateFlatPrefetchNode(fm, (FlatPrefetchNode) fn, output);
2310 case FKind.FlatOffsetNode:
2311 generateFlatOffsetNode(fm, (FlatOffsetNode)fn, output);
2318 additionalCodePostNode(fm, fn, output);
2321 public void generateFlatBackEdge(FlatMethod fm, FlatBackEdge fn, PrintWriter output) {
2322 if (((state.OOOJAVA||state.THREAD)&&GENERATEPRECISEGC)
2323 || (this.state.MULTICOREGC)) {
2324 if(this.state.MULTICOREGC) {
2325 output.println("if (gcflag) gc("+localsprefixaddr+");");
2327 output.println("if (unlikely(needtocollect)) checkcollect("+localsprefixaddr+");");
2330 output.println("/* nop */");
2333 public void generateFlatOffsetNode(FlatMethod fm, FlatOffsetNode fofn, PrintWriter output) {
2334 output.println("/* FlatOffsetNode */");
2335 FieldDescriptor fd=fofn.getField();
2336 if(!fd.isStatic()) {
2337 output.println(generateTemp(fm, fofn.getDst())+ " = (short)(int) (&((struct "+fofn.getClassType().getSafeSymbol() +" *)0)->"+
2338 fd.getSafeSymbol()+");");
2340 output.println("/* offset */");
2343 public void generateFlatPrefetchNode(FlatMethod fm, FlatPrefetchNode fpn, PrintWriter output) {
2346 public void generateFlatGlobalConvNode(FlatMethod fm, FlatGlobalConvNode fgcn, PrintWriter output) {
2349 public void generateFlatInstanceOfNode(FlatMethod fm, FlatInstanceOfNode fion, PrintWriter output) {
2352 if (fion.getType().isArray()) {
2353 type=state.getArrayNumber(fion.getType())+state.numClasses();
2354 } else if (fion.getType().getClassDesc().isInterface()) {
2355 type=fion.getType().getClassDesc().getId()+state.numClasses()+state.numArrays();
2357 type=fion.getType().getClassDesc().getId();
2359 if (fion.getSrc().getType().isArray()) {
2360 otype=state.getArrayNumber(fion.getSrc().getType())+state.numClasses();
2361 } else if (fion.getSrc().getType().getClassDesc().isInterface()) {
2362 otype=fion.getSrc().getType().getClassDesc().getId()+state.numClasses()+state.numArrays();
2364 otype=fion.getSrc().getType().getClassDesc().getId();
2367 if (fion.getType().getSymbol().equals(TypeUtil.ObjectClass))
2368 output.println(generateTemp(fm, fion.getDst())+"=(" + generateTemp(fm,fion.getSrc()) + "!= NULL);");
2370 output.println(generateTemp(fm, fion.getDst())+"=instanceof("+generateTemp(fm,fion.getSrc())+","+type+");");
2374 public void generateFlatAtomicEnterNode(FlatMethod fm, FlatAtomicEnterNode faen, PrintWriter output) {
2377 public void generateFlatAtomicExitNode(FlatMethod fm, FlatAtomicExitNode faen, PrintWriter output) {
2380 public void generateFlatSESEEnterNode(FlatMethod fm,
2381 FlatSESEEnterNode fsen,
2382 PrintWriter output) {
2383 // if OOOJAVA flag is off, okay that SESE nodes are in IR graph,
2384 // just skip over them and code generates exactly the same
2387 public void generateFlatSESEExitNode(FlatMethod fm,
2388 FlatSESEExitNode fsexn,
2389 PrintWriter output) {
2390 // if OOOJAVA flag is off, okay that SESE nodes are in IR graph,
2391 // just skip over them and code generates exactly the same
2394 public void generateFlatWriteDynamicVarNode(FlatMethod fm,
2395 FlatWriteDynamicVarNode fwdvn,
2396 PrintWriter output) {
2400 protected void generateFlatCheckNode(FlatMethod fm, FlatCheckNode fcn, PrintWriter output) {
2401 if (state.CONSCHECK) {
2402 String specname=fcn.getSpec();
2403 String varname="repairstate___";
2404 output.println("{");
2405 output.println("struct "+specname+"_state * "+varname+"=allocate"+specname+"_state();");
2407 TempDescriptor[] temps=fcn.getTemps();
2408 String[] vars=fcn.getVars();
2409 for(int i=0; i<temps.length; i++) {
2410 output.println(varname+"->"+vars[i]+"=(unsigned int)"+generateTemp(fm, temps[i])+";");
2413 output.println("if (doanalysis"+specname+"("+varname+")) {");
2414 output.println("free"+specname+"_state("+varname+");");
2415 output.println("} else {");
2416 output.println("/* Bad invariant */");
2417 output.println("free"+specname+"_state("+varname+");");
2418 output.println("abort_task();");
2419 output.println("}");
2420 output.println("}");
2424 protected void generateFlatCall(FlatMethod fm, FlatCall fc, PrintWriter output) {
2425 MethodDescriptor md=fc.getMethod();
2426 ParamsObject objectparams=(ParamsObject)paramstable.get(md);
2427 ClassDescriptor cn=md.getClassDesc();
2428 String mdstring = md.getSafeMethodDescriptor();
2429 if(mgcstaticinit && !md.isStaticBlock() && !md.getModifiers().isNative()) {
2430 mdstring += "staticinit";
2433 // if the called method is a static block or a static method or a constructor
2434 // need to check if it can be invoked inside some static block
2435 if((md.isStatic() || md.isStaticBlock() || md.isConstructor()) &&
2436 ((fm.getMethod() != null) && ((fm.getMethod().isStaticBlock()) || (fm.getMethod().isInvokedByStatic())))) {
2437 if(!md.isInvokedByStatic()) {
2438 System.err.println("Error: a method that is invoked inside a static block is not tagged!");
2440 // is a static block or is invoked in some static block
2441 ClassDescriptor cd = fm.getMethod().getClassDesc();
2443 // the same class, do nothing
2444 } else if(mgcstaticinit) {
2445 // generate static init check code if it has not done static init in main()
2446 if((cn.getNumStaticFields() != 0) || (cn.getNumStaticBlocks() != 0)) {
2447 // need to check if the class' static fields have been initialized and/or
2448 // its static blocks have been executed
2449 output.println("if(global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag == 0) {");
2450 if(cn.getNumStaticBlocks() != 0) {
2451 MethodDescriptor t_md = (MethodDescriptor)cn.getMethodTable().get("staticblocks");
2452 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2453 output.print(" struct "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
2454 output.println("0, NULL};");
2455 output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
2457 output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
2460 output.println(" global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag = 1;");
2462 output.println("}");
2466 if((md.getSymbol().equals("MonitorEnter") || md.getSymbol().equals("MonitorExit")) && fc.getThis().getSymbol().equals("classobj")) {
2467 output.println("{");
2468 // call MonitorEnter/MonitorExit on a class obj
2469 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2470 output.print(" struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params __parameterlist__={");
2471 output.println("1," + localsprefixaddr + ", global_defs_p->"+ fc.getThis().getType().getClassDesc().getSafeSymbol() +"classobj};");
2472 output.println(" "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(& __parameterlist__);");
2474 output.println(" " + cn.getSafeSymbol()+md.getSafeSymbol()+"_"
2475 + md.getSafeMethodDescriptor() + "((struct ___Object___*)(global_defs_p->"
2476 + fc.getThis().getType().getClassDesc().getSafeSymbol() +"classobj));");
2478 output.println("}");
2482 output.println("{");
2483 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2484 output.print(" struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_params __parameterlist__={");
2485 output.print(objectparams.numPointers());
2486 output.print(", "+localsprefixaddr);
2487 if (md.getThis()!=null) {
2489 output.print("(struct "+md.getThis().getType().getSafeSymbol() +" *)"+ generateTemp(fm,fc.getThis()));
2491 if (fc.getThis()!=null&&md.getThis()==null) {
2492 System.out.println("WARNING!!!!!!!!!!!!");
2493 System.out.println("Source code calls static method "+md+" on an object in "+fm.getMethod()+"!");
2497 for(int i=0; i<fc.numArgs(); i++) {
2498 Descriptor var=md.getParameter(i);
2499 TempDescriptor paramtemp=(TempDescriptor)temptovar.get(var);
2500 if (objectparams.isParamPtr(paramtemp)) {
2501 TempDescriptor targ=fc.getArg(i);
2503 TypeDescriptor td=md.getParamType(i);
2505 output.print("(struct "+(new TypeDescriptor(typeutil.getClass(TypeUtil.TagClass))).getSafeSymbol() +" *)"+generateTemp(fm, targ));
2507 output.print("(struct "+md.getParamType(i).getSafeSymbol() +" *)"+generateTemp(fm, targ));
2510 output.println("};");
2515 if (fc.getReturnTemp()!=null)
2516 output.print(generateTemp(fm,fc.getReturnTemp())+"=");
2518 /* Do we need to do virtual dispatch? */
2519 if (md.isStatic()||md.getReturnType()==null||singleCall(fc.getThis().getType().getClassDesc(),md)) {
2521 output.print(cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring);
2525 if (md.getReturnType().isClass() && md.getReturnType().getClassDesc().isEnum()) {
2526 output.print("int ");
2527 } else if (md.getReturnType().isClass()||md.getReturnType().isArray())
2528 output.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
2530 output.print(md.getReturnType().getSafeSymbol()+" ");
2531 output.print("(*)(");
2533 boolean printcomma=false;
2534 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2535 output.print("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_params * ");
2539 for(int i=0; i<objectparams.numPrimitives(); i++) {
2540 TempDescriptor temp=objectparams.getPrimitive(i);
2544 if (temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
2545 output.print("int ");
2546 } else if (temp.getType().isClass()||temp.getType().isArray())
2547 output.print("struct " + temp.getType().getSafeSymbol()+" * ");
2549 output.print(temp.getType().getSafeSymbol());
2553 output.print("))virtualtable["+generateTemp(fm,fc.getThis())+"->type*"+maxcount+"+"+virtualcalls.getMethodNumber(md)+"])");
2557 boolean needcomma=false;
2558 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2559 output.print("&__parameterlist__");
2563 if (!GENERATEPRECISEGC && !this.state.MULTICOREGC) {
2564 if (fc.getThis()!=null) {
2565 TypeDescriptor ptd=null;
2566 if(md.getThis() != null) {
2567 ptd = md.getThis().getType();
2569 ptd = fc.getThis().getType();
2573 if(ptd.isClass() && ptd.getClassDesc().isEnum()) {
2575 } else if (ptd.isClass()&&!ptd.isArray())
2576 output.print("(struct "+ptd.getSafeSymbol()+" *) ");
2577 output.print(generateTemp(fm,fc.getThis()));
2582 for(int i=0; i<fc.numArgs(); i++) {
2583 Descriptor var=md.getParameter(i);
2584 TempDescriptor paramtemp=(TempDescriptor)temptovar.get(var);
2585 if (objectparams.isParamPrim(paramtemp)) {
2586 TempDescriptor targ=fc.getArg(i);
2590 TypeDescriptor ptd=md.getParamType(i);
2591 if (ptd.isClass() && ptd.getClassDesc().isEnum()) {
2593 } else if (ptd.isClass()&&!ptd.isArray())
2594 output.print("(struct "+ptd.getSafeSymbol()+" *) ");
2595 output.print(generateTemp(fm, targ));
2599 output.println(");");
2600 output.println(" }");
2603 protected boolean singleCall(ClassDescriptor thiscd, MethodDescriptor md) {
2604 if(thiscd.isInterface()) {
2605 // for interfaces, always need virtual dispatch
2608 Set subclasses=typeutil.getSubClasses(thiscd);
2609 if (subclasses==null)
2611 for(Iterator classit=subclasses.iterator(); classit.hasNext(); ) {
2612 ClassDescriptor cd=(ClassDescriptor)classit.next();
2613 Set possiblematches=cd.getMethodTable().getSetFromSameScope(md.getSymbol());
2614 for(Iterator matchit=possiblematches.iterator(); matchit.hasNext(); ) {
2615 MethodDescriptor matchmd=(MethodDescriptor)matchit.next();
2616 if (md.matches(matchmd))
2624 protected void generateFlatFieldNode(FlatMethod fm, FlatFieldNode ffn, PrintWriter output) {
2626 if(ffn.getField().isStatic()) {
2628 if((fm.getMethod().isStaticBlock()) || (fm.getMethod().isInvokedByStatic())) {
2629 // is a static block or is invoked in some static block
2630 ClassDescriptor cd = fm.getMethod().getClassDesc();
2631 ClassDescriptor cn = ffn.getSrc().getType().getClassDesc();
2633 // the same class, do nothing
2634 } else if(mgcstaticinit) {
2635 // generate the static init check code if has not done the static init in main()
2636 if((cn.getNumStaticFields() != 0) || (cn.getNumStaticBlocks() != 0)) {
2637 // need to check if the class' static fields have been initialized and/or
2638 // its static blocks have been executed
2639 output.println("if(global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag == 0) {");
2640 if(cn.getNumStaticBlocks() != 0) {
2641 MethodDescriptor t_md = (MethodDescriptor)cn.getMethodTable().get("staticblocks");
2642 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2643 output.print(" struct "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
2644 output.println("0, NULL};");
2645 output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
2647 output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
2650 output.println(" global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag = 1;");
2652 output.println("}");
2656 // redirect to the global_defs_p structure
2657 if((ffn.getField().isStatic()) || (ffn.getSrc().getType().isClassNameRef())) {
2658 // reference to the static field with Class name
2659 if (ffn.getField().getType().isPtr())
2660 output.println(generateTemp(fm, ffn.getDst())+"=global_defs_p->"+ffn.getField().getSafeSymbol()+";");
2662 output.println(generateTemp(fm, ffn.getDst())+"=global_defsprim_p->"+ffn.getField().getSafeSymbol()+";");
2664 output.println(generateTemp(fm, ffn.getDst())+"=*"+ generateTemp(fm,ffn.getSrc())+"->"+ ffn.getField().getSafeSymbol()+";");
2666 } else if (ffn.getField().isEnum()) {
2667 // an Enum value, directly replace the field access as int
2668 output.println(generateTemp(fm, ffn.getDst()) + "=" + ffn.getField().enumValue() + ";");
2670 output.println(generateTemp(fm, ffn.getDst())+"="+ generateTemp(fm,ffn.getSrc())+"->"+ ffn.getField().getSafeSymbol()+";");
2675 protected void generateFlatSetFieldNode(FlatMethod fm, FlatSetFieldNode fsfn, PrintWriter output) {
2676 if (fsfn.getField().getSymbol().equals("length")&&fsfn.getDst().getType().isArray())
2677 throw new Error("Can't set array length");
2678 if (state.FASTCHECK) {
2679 String dst=generateTemp(fm, fsfn.getDst());
2680 output.println("if(!"+dst+"->"+localcopystr+") {");
2681 /* Link object into list */
2682 if (GENERATEPRECISEGC || this.state.MULTICOREGC)
2683 output.println("COPY_OBJ((struct garbagelist *)"+localsprefixaddr+",(struct ___Object___ *)"+dst+");");
2685 output.println("COPY_OBJ("+dst+");");
2686 output.println(dst+"->"+nextobjstr+"="+fcrevert+";");
2687 output.println(fcrevert+"=(struct ___Object___ *)"+dst+";");
2688 output.println("}");
2691 if(fsfn.getField().isStatic()) {
2693 if((fm.getMethod().isStaticBlock()) || (fm.getMethod().isInvokedByStatic())) {
2694 // is a static block or is invoked in some static block
2695 ClassDescriptor cd = fm.getMethod().getClassDesc();
2696 ClassDescriptor cn = fsfn.getDst().getType().getClassDesc();
2698 // the same class, do nothing
2699 } else if(mgcstaticinit){
2700 // generate static init check code if has not done the static init in main()
2701 if((cn.getNumStaticFields() != 0) || (cn.getNumStaticBlocks() != 0)) {
2702 // need to check if the class' static fields have been initialized and/or
2703 // its static blocks have been executed
2704 output.println("if(global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag == 0) {");
2705 if(cn.getNumStaticBlocks() != 0) {
2706 MethodDescriptor t_md = (MethodDescriptor)cn.getMethodTable().get("staticblocks");
2707 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2708 output.print(" struct "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
2709 output.println("0, NULL};");
2710 output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
2712 output.println(" "+cn.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
2715 output.println(" global_defsprim_p->" + cn.getSafeSymbol()+"static_block_exe_flag = 1;");
2717 output.println("}");
2721 // redirect to the global_defs_p structure
2722 if((fsfn.getDst().getType().isClassNameRef()) || (fsfn.getField().isStatic())) {
2723 // reference to the static field with Class name
2724 if (fsfn.getField().getType().isPtr())
2725 output.println("global_defs_p->" +
2726 fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc())+";");
2728 output.println("global_defsprim_p->" +
2729 fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc())+";");
2731 output.println("*"+generateTemp(fm, fsfn.getDst())+"->"+
2732 fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc())+";");
2735 output.println(generateTemp(fm, fsfn.getDst())+"->"+
2736 fsfn.getField().getSafeSymbol()+"="+ generateTemp(fm,fsfn.getSrc())+";");
2741 protected void generateFlatElementNode(FlatMethod fm, FlatElementNode fen, PrintWriter output) {
2742 TypeDescriptor elementtype=fen.getSrc().getType().dereference();
2745 if (elementtype.isClass() && elementtype.getClassDesc().isEnum()) {
2747 } else if (elementtype.isArray()||elementtype.isClass())
2750 type=elementtype.getSafeSymbol()+" ";
2752 if (this.state.ARRAYBOUNDARYCHECK && fen.needsBoundsCheck()) {
2753 output.println("if (unlikely(((unsigned int)"+generateTemp(fm, fen.getIndex())+") >= "+generateTemp(fm,fen.getSrc()) + "->___length___))");
2754 output.println("failedboundschk();");
2756 output.println(generateTemp(fm, fen.getDst())+"=(("+ type+"*)(((char *) &("+ generateTemp(fm,fen.getSrc())+"->___length___))+sizeof(int)))["+generateTemp(fm, fen.getIndex())+"];");
2759 protected void generateFlatSetElementNode(FlatMethod fm, FlatSetElementNode fsen, PrintWriter output) {
2760 //TODO: need dynamic check to make sure this assignment is actually legal
2761 //Because Object[] could actually be something more specific...ie. Integer[]
2763 TypeDescriptor elementtype=fsen.getDst().getType().dereference();
2766 if (elementtype.isClass() && elementtype.getClassDesc().isEnum()) {
2768 } else if (elementtype.isArray()||elementtype.isClass() || (elementtype.isNull()))
2771 type=elementtype.getSafeSymbol()+" ";
2773 if (this.state.ARRAYBOUNDARYCHECK && fsen.needsBoundsCheck()) {
2774 output.println("if (unlikely(((unsigned int)"+generateTemp(fm, fsen.getIndex())+") >= "+generateTemp(fm,fsen.getDst()) + "->___length___))");
2775 output.println("failedboundschk();");
2777 if (state.FASTCHECK) {
2778 String dst=generateTemp(fm, fsen.getDst());
2779 output.println("if(!"+dst+"->"+localcopystr+") {");
2780 /* Link object into list */
2781 if (GENERATEPRECISEGC || this.state.MULTICOREGC)
2782 output.println("COPY_OBJ((struct garbagelist *)"+localsprefixaddr+",(struct ___Object___ *)"+dst+");");
2784 output.println("COPY_OBJ("+dst+");");
2785 output.println(dst+"->"+nextobjstr+"="+fcrevert+";");
2786 output.println(fcrevert+"=(struct ___Object___ *)"+dst+";");
2787 output.println("}");
2789 output.println("(("+type +"*)(((char *) &("+ generateTemp(fm,fsen.getDst())+"->___length___))+sizeof(int)))["+generateTemp(fm, fsen.getIndex())+"]="+generateTemp(fm,fsen.getSrc())+";");
2793 protected void generateFlatNew(FlatMethod fm, FlatNew fn, PrintWriter output) {
2794 if (fn.getType().isArray()) {
2795 int arrayid=state.getArrayNumber(fn.getType())+state.numClasses();
2796 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2797 output.println(generateTemp(fm,fn.getDst())+"=allocate_newarray("+localsprefixaddr+", "+arrayid+", "+generateTemp(fm, fn.getSize())+");");
2799 output.println(generateTemp(fm,fn.getDst())+"=allocate_newarray("+arrayid+", "+generateTemp(fm, fn.getSize())+");");
2802 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2803 output.println(generateTemp(fm,fn.getDst())+"=allocate_new("+localsprefixaddr+", "+fn.getType().getClassDesc().getId()+");");
2805 output.println(generateTemp(fm,fn.getDst())+"=allocate_new("+fn.getType().getClassDesc().getId()+");");
2808 if (state.FASTCHECK) {
2809 String dst=generateTemp(fm,fn.getDst());
2810 output.println(dst+"->___localcopy___=(struct ___Object___*)1;");
2811 output.println(dst+"->"+nextobjstr+"="+fcrevert+";");
2812 output.println(fcrevert+"=(struct ___Object___ *)"+dst+";");
2816 protected void generateFlatTagDeclaration(FlatMethod fm, FlatTagDeclaration fn, PrintWriter output) {
2817 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2818 output.println(generateTemp(fm,fn.getDst())+"=allocate_tag("+localsprefixaddr+", "+state.getTagId(fn.getType())+");");
2820 output.println(generateTemp(fm,fn.getDst())+"=allocate_tag("+state.getTagId(fn.getType())+");");
2824 protected void generateFlatOpNode(FlatMethod fm, FlatOpNode fon, PrintWriter output) {
2825 if (fon.getRight()!=null) {
2826 if (fon.getOp().getOp()==Operation.URIGHTSHIFT) {
2827 if (fon.getLeft().getType().isLong())
2828 output.println(generateTemp(fm, fon.getDest())+" = ((unsigned long long)"+generateTemp(fm, fon.getLeft())+")>>"+generateTemp(fm,fon.getRight())+";");
2830 output.println(generateTemp(fm, fon.getDest())+" = ((unsigned int)"+generateTemp(fm, fon.getLeft())+")>>"+generateTemp(fm,fon.getRight())+";");
2833 output.println(generateTemp(fm, fon.getDest())+" = "+generateTemp(fm, fon.getLeft())+fon.getOp().toString()+generateTemp(fm,fon.getRight())+";");
2834 } else if (fon.getOp().getOp()==Operation.ASSIGN)
2835 output.println(generateTemp(fm, fon.getDest())+" = "+generateTemp(fm, fon.getLeft())+";");
2836 else if (fon.getOp().getOp()==Operation.UNARYPLUS)
2837 output.println(generateTemp(fm, fon.getDest())+" = "+generateTemp(fm, fon.getLeft())+";");
2838 else if (fon.getOp().getOp()==Operation.UNARYMINUS)
2839 output.println(generateTemp(fm, fon.getDest())+" = -"+generateTemp(fm, fon.getLeft())+";");
2840 else if (fon.getOp().getOp()==Operation.LOGIC_NOT)
2841 output.println(generateTemp(fm, fon.getDest())+" = !"+generateTemp(fm, fon.getLeft())+";");
2842 else if (fon.getOp().getOp()==Operation.COMP)
2843 output.println(generateTemp(fm, fon.getDest())+" = ~"+generateTemp(fm, fon.getLeft())+";");
2844 else if (fon.getOp().getOp()==Operation.ISAVAILABLE) {
2845 output.println(generateTemp(fm, fon.getDest())+" = "+generateTemp(fm, fon.getLeft())+"->fses==NULL;");
2847 output.println(generateTemp(fm, fon.getDest())+fon.getOp().toString()+generateTemp(fm, fon.getLeft())+";");
2850 protected void generateFlatCastNode(FlatMethod fm, FlatCastNode fcn, PrintWriter output) {
2851 /* TODO: Do type check here */
2852 if (fcn.getType().isArray()) {
2853 output.println(generateTemp(fm,fcn.getDst())+"=(struct ArrayObject *)"+generateTemp(fm,fcn.getSrc())+";");
2854 } else if (fcn.getType().isClass() && fcn.getType().getClassDesc().isEnum()) {
2855 output.println(generateTemp(fm,fcn.getDst())+"=(int)"+generateTemp(fm,fcn.getSrc())+";");
2856 } else if (fcn.getType().isClass())
2857 output.println(generateTemp(fm,fcn.getDst())+"=(struct "+fcn.getType().getSafeSymbol()+" *)"+generateTemp(fm,fcn.getSrc())+";");
2859 output.println(generateTemp(fm,fcn.getDst())+"=("+fcn.getType().getSafeSymbol()+")"+generateTemp(fm,fcn.getSrc())+";");
2862 protected void generateFlatLiteralNode(FlatMethod fm, FlatLiteralNode fln, PrintWriter output) {
2863 if (fln.getValue()==null)
2864 output.println(generateTemp(fm, fln.getDst())+"=0;");
2865 else if (fln.getType().getSymbol().equals(TypeUtil.StringClass)) {
2866 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2867 output.println(generateTemp(fm, fln.getDst())+"=NewString("+localsprefixaddr+", \""+FlatLiteralNode.escapeString((String)fln.getValue())+"\","+((String)fln.getValue()).length()+");");
2869 output.println(generateTemp(fm, fln.getDst())+"=NewString(\""+FlatLiteralNode.escapeString((String)fln.getValue())+"\","+((String)fln.getValue()).length()+");");
2871 } else if (fln.getType().isBoolean()) {
2872 if (((Boolean)fln.getValue()).booleanValue())
2873 output.println(generateTemp(fm, fln.getDst())+"=1;");
2875 output.println(generateTemp(fm, fln.getDst())+"=0;");
2876 } else if (fln.getType().isChar()) {
2877 String st=FlatLiteralNode.escapeString(fln.getValue().toString());
2878 output.println(generateTemp(fm, fln.getDst())+"='"+st+"';");
2879 } else if (fln.getType().isLong()) {
2880 output.println(generateTemp(fm, fln.getDst())+"="+fln.getValue()+"LL;");
2882 output.println(generateTemp(fm, fln.getDst())+"="+fln.getValue()+";");
2885 protected void generateFlatReturnNode(FlatMethod fm, FlatReturnNode frn, PrintWriter output) {
2886 if((fm.getMethod() != null) && (fm.getMethod().isStaticBlock())) {
2887 // a static block, check if it has been executed
2888 output.println(" global_defsprim_p->" + fm.getMethod().getClassDesc().getSafeSymbol()+"static_block_exe_flag = 1;");
2892 if (frn.getReturnTemp()!=null) {
2893 if (frn.getReturnTemp().getType().isPtr())
2894 output.println("return (struct "+fm.getMethod().getReturnType().getSafeSymbol()+"*)"+generateTemp(fm, frn.getReturnTemp())+";");
2896 output.println("return "+generateTemp(fm, frn.getReturnTemp())+";");
2898 output.println("return;");
2902 protected void generateFlatCondBranch(FlatMethod fm, FlatCondBranch fcb, String label, PrintWriter output) {
2903 output.println("if (!"+generateTemp(fm, fcb.getTest())+") goto "+label+";");
2906 /** This method generates header information for the method or
2907 * task referenced by the Descriptor des. */
2908 protected void generateHeader(FlatMethod fm, Descriptor des, PrintWriter output) {
2909 generateHeader(fm, des, output, false);
2912 protected void generateHeader(FlatMethod fm, Descriptor des, PrintWriter output, boolean addSESErecord) {
2914 ParamsObject objectparams=(ParamsObject)paramstable.get(des);
2915 MethodDescriptor md=null;
2916 TaskDescriptor task=null;
2917 if (des instanceof MethodDescriptor)
2918 md=(MethodDescriptor) des;
2920 task=(TaskDescriptor) des;
2921 String mdstring = md != null ? md.getSafeMethodDescriptor() : null;
2923 ClassDescriptor cn=md!=null ? md.getClassDesc() : null;
2925 if (md!=null&&md.getReturnType()!=null) {
2926 if (md.getReturnType().isClass() && md.getReturnType().getClassDesc().isEnum()) {
2927 output.print("int ");
2928 } else if (md.getReturnType().isClass()||md.getReturnType().isArray())
2929 output.print("struct " + md.getReturnType().getSafeSymbol()+" * ");
2931 output.print(md.getReturnType().getSafeSymbol()+" ");
2933 //catch the constructor case
2934 output.print("void ");
2936 if(mgcstaticinit && !md.isStaticBlock() && !md.getModifiers().isNative()) {
2937 mdstring += "staticinit";
2939 output.print(cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"(");
2941 output.print(task.getSafeSymbol()+"(");
2943 boolean printcomma=false;
2944 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
2946 output.print("struct "+cn.getSafeSymbol()+md.getSafeSymbol()+"_"+mdstring+"_params * "+paramsprefix);
2948 output.print("struct "+task.getSafeSymbol()+"_params * "+paramsprefix);
2954 for(int i=0; i<objectparams.numPrimitives(); i++) {
2955 TempDescriptor temp=objectparams.getPrimitive(i);
2959 if(temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
2960 output.print("int " + temp.getSafeSymbol());
2961 } else if (temp.getType().isClass()||temp.getType().isArray())
2962 output.print("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
2964 output.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
2966 output.println(") {");
2967 } else if (!GENERATEPRECISEGC && !this.state.MULTICOREGC) {
2968 /* Imprecise Task */
2969 output.println("void * parameterarray[]) {");
2970 /* Unpack variables */
2971 for(int i=0; i<objectparams.numPrimitives(); i++) {
2972 TempDescriptor temp=objectparams.getPrimitive(i);
2973 if(temp.getType().isClass() && temp.getType().getClassDesc().isEnum()) {
2974 output.print("int " + temp.getSafeSymbol() + "=parameterarray["+i+"];");
2976 output.println("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol()+"=parameterarray["+i+"];");
2979 for(int i=0; i<fm.numTags(); i++) {
2980 TempDescriptor temp=fm.getTag(i);
2981 int offset=i+objectparams.numPrimitives();
2982 output.println("struct ___TagDescriptor___ * "+temp.getSafeSymbol()+"=parameterarray["+offset+"];");
2985 if ((objectparams.numPrimitives()+fm.numTags())>maxtaskparams)
2986 maxtaskparams=objectparams.numPrimitives()+fm.numTags();
2987 } else output.println(") {");
2990 public void generateFlatFlagActionNode(FlatMethod fm, FlatFlagActionNode ffan, PrintWriter output) {
2991 output.println("/* FlatFlagActionNode */");
2994 /* Process tag changes */
2995 Relation tagsettable=new Relation();
2996 Relation tagcleartable=new Relation();
2998 Iterator tagsit=ffan.getTempTagPairs();
2999 while (tagsit.hasNext()) {
3000 TempTagPair ttp=(TempTagPair) tagsit.next();
3001 TempDescriptor objtmp=ttp.getTemp();
3002 TagDescriptor tag=ttp.getTag();
3003 TempDescriptor tagtmp=ttp.getTagTemp();
3004 boolean tagstatus=ffan.getTagChange(ttp);
3006 tagsettable.put(objtmp, tagtmp);
3008 tagcleartable.put(objtmp, tagtmp);
3013 Hashtable flagandtable=new Hashtable();
3014 Hashtable flagortable=new Hashtable();
3016 /* Process flag changes */
3017 Iterator flagsit=ffan.getTempFlagPairs();
3018 while(flagsit.hasNext()) {
3019 TempFlagPair tfp=(TempFlagPair)flagsit.next();
3020 TempDescriptor temp=tfp.getTemp();
3021 Hashtable flagtable=(Hashtable)flagorder.get(temp.getType().getClassDesc());
3022 FlagDescriptor flag=tfp.getFlag();
3024 //Newly allocate objects that don't set any flags case
3025 if (flagortable.containsKey(temp)) {
3029 flagortable.put(temp,new Integer(mask));
3031 int flagid=1<<((Integer)flagtable.get(flag)).intValue();
3032 boolean flagstatus=ffan.getFlagChange(tfp);
3035 if (flagortable.containsKey(temp)) {
3036 mask=((Integer)flagortable.get(temp)).intValue();
3039 flagortable.put(temp,new Integer(mask));
3041 int mask=0xFFFFFFFF;
3042 if (flagandtable.containsKey(temp)) {
3043 mask=((Integer)flagandtable.get(temp)).intValue();
3045 mask&=(0xFFFFFFFF^flagid);
3046 flagandtable.put(temp,new Integer(mask));
3052 HashSet flagtagset=new HashSet();
3053 flagtagset.addAll(flagortable.keySet());
3054 flagtagset.addAll(flagandtable.keySet());
3055 flagtagset.addAll(tagsettable.keySet());
3056 flagtagset.addAll(tagcleartable.keySet());
3058 Iterator ftit=flagtagset.iterator();
3059 while(ftit.hasNext()) {
3060 TempDescriptor temp=(TempDescriptor)ftit.next();
3063 Set tagtmps=tagcleartable.get(temp);
3064 if (tagtmps!=null) {
3065 Iterator tagit=tagtmps.iterator();
3066 while(tagit.hasNext()) {
3067 TempDescriptor tagtmp=(TempDescriptor)tagit.next();
3068 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC))
3069 output.println("tagclear("+localsprefixaddr+", (struct ___Object___ *)"+generateTemp(fm, temp)+", "+generateTemp(fm,tagtmp)+");");
3071 output.println("tagclear((struct ___Object___ *)"+generateTemp(fm, temp)+", "+generateTemp(fm,tagtmp)+");");
3075 tagtmps=tagsettable.get(temp);
3076 if (tagtmps!=null) {
3077 Iterator tagit=tagtmps.iterator();
3078 while(tagit.hasNext()) {
3079 TempDescriptor tagtmp=(TempDescriptor)tagit.next();
3080 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC))
3081 output.println("tagset("+localsprefixaddr+", (struct ___Object___ *)"+generateTemp(fm, temp)+", "+generateTemp(fm,tagtmp)+");");
3083 output.println("tagset((struct ___Object___ *)"+generateTemp(fm, temp)+", "+generateTemp(fm,tagtmp)+");");
3088 int andmask=0xFFFFFFF;
3090 if (flagortable.containsKey(temp))
3091 ormask=((Integer)flagortable.get(temp)).intValue();
3092 if (flagandtable.containsKey(temp))
3093 andmask=((Integer)flagandtable.get(temp)).intValue();
3094 generateFlagOrAnd(ffan, fm, temp, output, ormask, andmask);
3095 generateObjectDistribute(ffan, fm, temp, output);
3099 protected void generateFlagOrAnd(FlatFlagActionNode ffan, FlatMethod fm, TempDescriptor temp,
3100 PrintWriter output, int ormask, int andmask) {
3101 if (ffan.getTaskType()==FlatFlagActionNode.NEWOBJECT) {
3102 output.println("flagorandinit("+generateTemp(fm, temp)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
3104 output.println("flagorand("+generateTemp(fm, temp)+", 0x"+Integer.toHexString(ormask)+", 0x"+Integer.toHexString(andmask)+");");
3108 protected void generateObjectDistribute(FlatFlagActionNode ffan, FlatMethod fm, TempDescriptor temp, PrintWriter output) {
3109 output.println("enqueueObject("+generateTemp(fm, temp)+");");
3112 void generateOptionalHeader(PrintWriter headers) {
3115 headers.println("#include \"task.h\"\n\n");
3116 headers.println("#ifndef _OPTIONAL_STRUCT_");
3117 headers.println("#define _OPTIONAL_STRUCT_");
3119 //STRUCT PREDICATEMEMBER
3120 headers.println("struct predicatemember{");
3121 headers.println("int type;");
3122 headers.println("int numdnfterms;");
3123 headers.println("int * flags;");
3124 headers.println("int numtags;");
3125 headers.println("int * tags;\n};\n\n");
3127 //STRUCT OPTIONALTASKDESCRIPTOR
3128 headers.println("struct optionaltaskdescriptor{");
3129 headers.println("struct taskdescriptor * task;");
3130 headers.println("int index;");
3131 headers.println("int numenterflags;");
3132 headers.println("int * enterflags;");
3133 headers.println("int numpredicatemembers;");
3134 headers.println("struct predicatemember ** predicatememberarray;");
3135 headers.println("};\n\n");
3137 //STRUCT TASKFAILURE
3138 headers.println("struct taskfailure {");
3139 headers.println("struct taskdescriptor * task;");
3140 headers.println("int index;");
3141 headers.println("int numoptionaltaskdescriptors;");
3142 headers.println("struct optionaltaskdescriptor ** optionaltaskdescriptorarray;\n};\n\n");
3144 //STRUCT FSANALYSISWRAPPER
3145 headers.println("struct fsanalysiswrapper{");
3146 headers.println("int flags;");
3147 headers.println("int numtags;");
3148 headers.println("int * tags;");
3149 headers.println("int numtaskfailures;");
3150 headers.println("struct taskfailure ** taskfailurearray;");
3151 headers.println("int numoptionaltaskdescriptors;");
3152 headers.println("struct optionaltaskdescriptor ** optionaltaskdescriptorarray;\n};\n\n");
3154 //STRUCT CLASSANALYSISWRAPPER
3155 headers.println("struct classanalysiswrapper{");
3156 headers.println("int type;");
3157 headers.println("int numotd;");
3158 headers.println("struct optionaltaskdescriptor ** otdarray;");
3159 headers.println("int numfsanalysiswrappers;");
3160 headers.println("struct fsanalysiswrapper ** fsanalysiswrapperarray;\n};");
3162 headers.println("extern struct classanalysiswrapper * classanalysiswrapperarray[];");
3164 Iterator taskit=state.getTaskSymbolTable().getDescriptorsIterator();
3165 while(taskit.hasNext()) {
3166 TaskDescriptor td=(TaskDescriptor)taskit.next();
3167 headers.println("extern struct taskdescriptor task_"+td.getSafeSymbol()+";");
3172 //CHECK OVER THIS -- THERE COULD BE SOME ERRORS HERE
3173 int generateOptionalPredicate(Predicate predicate, OptionalTaskDescriptor otd, ClassDescriptor cdtemp, PrintWriter output) {
3174 int predicateindex = 0;
3175 //iterate through the classes concerned by the predicate
3176 Set c_vard = predicate.vardescriptors;
3177 Hashtable<TempDescriptor, Integer> slotnumber=new Hashtable<TempDescriptor, Integer>();
3180 for(Iterator vard_it = c_vard.iterator(); vard_it.hasNext(); ) {
3181 VarDescriptor vard = (VarDescriptor)vard_it.next();
3182 TypeDescriptor typed = vard.getType();
3184 //generate for flags
3185 HashSet fen_hashset = predicate.flags.get(vard.getSymbol());
3186 output.println("int predicateflags_"+predicateindex+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+"[]={");
3188 if (fen_hashset!=null) {
3189 for (Iterator fen_it = fen_hashset.iterator(); fen_it.hasNext(); ) {
3190 FlagExpressionNode fen = (FlagExpressionNode)fen_it.next();
3192 DNFFlag dflag=fen.getDNF();
3193 numberterms+=dflag.size();
3195 Hashtable flags=(Hashtable)flagorder.get(typed.getClassDesc());
3197 for(int j=0; j<dflag.size(); j++) {
3199 output.println(",");
3200 Vector term=dflag.get(j);
3203 for(int k=0; k<term.size(); k++) {
3204 DNFFlagAtom dfa=(DNFFlagAtom)term.get(k);
3205 FlagDescriptor fd=dfa.getFlag();
3206 boolean negated=dfa.getNegated();
3207 int flagid=1<<((Integer)flags.get(fd)).intValue();
3212 output.print("/*andmask*/0x"+Integer.toHexString(andmask)+", /*checkmask*/0x"+Integer.toHexString(checkmask));
3217 output.println("};\n");
3220 TagExpressionList tagel = predicate.tags.get(vard.getSymbol());
3221 output.println("int predicatetags_"+predicateindex+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+"[]={");
3224 for(int j=0; j<tagel.numTags(); j++) {
3226 output.println(",");
3227 TempDescriptor tmp=tagel.getTemp(j);
3228 if (!slotnumber.containsKey(tmp)) {
3229 Integer slotint=new Integer(current_slot++);
3230 slotnumber.put(tmp,slotint);
3232 int slot=slotnumber.get(tmp).intValue();
3233 output.println("/* slot */"+ slot+", /*tagid*/"+state.getTagId(tmp.getTag()));
3235 numtags = tagel.numTags();
3237 output.println("};");
3239 //store the result into a predicatemember struct
3240 output.println("struct predicatemember predicatemember_"+predicateindex+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+"={");
3241 output.println("/*type*/"+typed.getClassDesc().getId()+",");
3242 output.println("/* number of dnf terms */"+numberterms+",");
3243 output.println("predicateflags_"+predicateindex+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+",");
3244 output.println("/* number of tag */"+numtags+",");
3245 output.println("predicatetags_"+predicateindex+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+",");
3246 output.println("};\n");
3251 //generate an array that stores the entire predicate
3252 output.println("struct predicatemember * predicatememberarray_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+"[]={");
3253 for( int j = 0; j<predicateindex; j++) {
3254 if( j != predicateindex-1) output.println("&predicatemember_"+j+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+",");
3255 else output.println("&predicatemember_"+j+"_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol());
3257 output.println("};\n");
3258 return predicateindex;
3262 void generateOptionalArrays(PrintWriter output, PrintWriter headers, Hashtable<ClassDescriptor, Hashtable<FlagState, Set<OptionalTaskDescriptor>>> safeexecution, Hashtable optionaltaskdescriptors) {
3263 generateOptionalHeader(headers);
3265 output.println("#include \"optionalstruct.h\"\n\n");
3266 output.println("#include \"stdlib.h\"\n");
3268 HashSet processedcd = new HashSet();
3270 Enumeration e = safeexecution.keys();
3271 while (e.hasMoreElements()) {
3274 ClassDescriptor cdtemp=(ClassDescriptor)e.nextElement();
3275 Hashtable flaginfo=(Hashtable)flagorder.get(cdtemp); //will be used several times
3277 //Generate the struct of optionals
3278 Collection c_otd = ((Hashtable)optionaltaskdescriptors.get(cdtemp)).values();
3279 numotd = c_otd.size();
3280 if(maxotd<numotd) maxotd = numotd;
3281 if( !c_otd.isEmpty() ) {
3282 for(Iterator otd_it = c_otd.iterator(); otd_it.hasNext(); ) {
3283 OptionalTaskDescriptor otd = (OptionalTaskDescriptor)otd_it.next();
3285 //generate the int arrays for the predicate
3286 Predicate predicate = otd.predicate;
3287 int predicateindex = generateOptionalPredicate(predicate, otd, cdtemp, output);
3288 TreeSet<Integer> fsset=new TreeSet<Integer>();
3289 //iterate through possible FSes corresponding to
3290 //the state when entering
3292 for(Iterator fses = otd.enterflagstates.iterator(); fses.hasNext(); ) {
3293 FlagState fs = (FlagState)fses.next();
3295 for(Iterator flags = fs.getFlags(); flags.hasNext(); ) {
3296 FlagDescriptor flagd = (FlagDescriptor)flags.next();
3297 int id=1<<((Integer)flaginfo.get(flagd)).intValue();
3300 fsset.add(new Integer(flagid));
3301 //tag information not needed because tag
3302 //changes are not tolerated.
3305 output.println("int enterflag_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+"[]={");
3306 boolean needcomma=false;
3307 for(Iterator<Integer> it=fsset.iterator(); it.hasNext(); ) {
3310 output.println(it.next());
3313 output.println("};\n");
3316 //generate optionaltaskdescriptor that actually
3317 //includes exit fses, predicate and the task
3319 output.println("struct optionaltaskdescriptor optionaltaskdescriptor_"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+"={");
3320 output.println("&task_"+otd.td.getSafeSymbol()+",");
3321 output.println("/*index*/"+otd.getIndex()+",");
3322 output.println("/*number of enter flags*/"+fsset.size()+",");
3323 output.println("enterflag_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+",");
3324 output.println("/*number of members */"+predicateindex+",");
3325 output.println("predicatememberarray_OTD"+otd.getuid()+"_"+cdtemp.getSafeSymbol()+",");
3326 output.println("};\n");
3330 // if there are no optionals, there is no need to build the rest of the struct
3332 output.println("struct optionaltaskdescriptor * otdarray"+cdtemp.getSafeSymbol()+"[]={");
3333 c_otd = ((Hashtable)optionaltaskdescriptors.get(cdtemp)).values();
3334 if( !c_otd.isEmpty() ) {
3335 boolean needcomma=false;
3336 for(Iterator otd_it = c_otd.iterator(); otd_it.hasNext(); ) {
3337 OptionalTaskDescriptor otd = (OptionalTaskDescriptor)otd_it.next();
3339 output.println(",");
3341 output.println("&optionaltaskdescriptor_"+otd.getuid()+"_"+cdtemp.getSafeSymbol());
3344 output.println("};\n");
3346 //get all the possible flagstates reachable by an object
3347 Hashtable hashtbtemp = safeexecution.get(cdtemp);
3349 TreeSet fsts=new TreeSet(new FlagComparator(flaginfo));
3350 fsts.addAll(hashtbtemp.keySet());
3351 for(Iterator fsit=fsts.iterator(); fsit.hasNext(); ) {
3352 FlagState fs = (FlagState)fsit.next();
3355 //get the set of OptionalTaskDescriptors corresponding
3356 HashSet<OptionalTaskDescriptor> availabletasks = (HashSet<OptionalTaskDescriptor>)hashtbtemp.get(fs);
3357 //iterate through the OptionalTaskDescriptors and
3358 //store the pointers to the optionals struct (see on
3359 //top) into an array
3361 output.println("struct optionaltaskdescriptor * optionaltaskdescriptorarray_FS"+fscounter+"_"+cdtemp.getSafeSymbol()+"[] = {");
3362 for(Iterator<OptionalTaskDescriptor> mos = ordertd(availabletasks).iterator(); mos.hasNext(); ) {
3363 OptionalTaskDescriptor mm = mos.next();
3365 output.println("&optionaltaskdescriptor_"+mm.getuid()+"_"+cdtemp.getSafeSymbol());
3367 output.println("&optionaltaskdescriptor_"+mm.getuid()+"_"+cdtemp.getSafeSymbol()+",");
3370 output.println("};\n");
3372 //process flag information (what the flag after failure is) so we know what optionaltaskdescriptors to choose.
3375 for(Iterator flags = fs.getFlags(); flags.hasNext(); ) {
3376 FlagDescriptor flagd = (FlagDescriptor)flags.next();
3377 int id=1<<((Integer)flaginfo.get(flagd)).intValue();
3381 //process tag information
3384 boolean first = true;
3385 Enumeration tag_enum = fs.getTags();
3386 output.println("int tags_FS"+fscounter+"_"+cdtemp.getSafeSymbol()+"[]={");
3387 while(tag_enum.hasMoreElements()) {
3389 TagDescriptor tagd = (TagDescriptor)tag_enum.nextElement();
3393 output.println(", ");
3394 output.println("/*tagid*/"+state.getTagId(tagd));
3396 output.println("};");
3398 Set<TaskIndex> tiset=sa.getTaskIndex(fs);
3399 for(Iterator<TaskIndex> itti=tiset.iterator(); itti.hasNext(); ) {
3400 TaskIndex ti=itti.next();
3404 Set<OptionalTaskDescriptor> otdset=sa.getOptions(fs, ti);
3406 output.print("struct optionaltaskdescriptor * optionaltaskfailure_FS"+fscounter+"_"+ti.getTask().getSafeSymbol()+"_"+ti.getIndex()+"_array[] = {");
3407 boolean needcomma=false;
3408 for(Iterator<OptionalTaskDescriptor> otdit=ordertd(otdset).iterator(); otdit.hasNext(); ) {
3409 OptionalTaskDescriptor otd=otdit.next();
3413 output.println("&optionaltaskdescriptor_"+otd.getuid()+"_"+cdtemp.getSafeSymbol());
3415 output.println("};");
3417 output.print("struct taskfailure taskfailure_FS"+fscounter+"_"+ti.getTask().getSafeSymbol()+"_"+ti.getIndex()+" = {");
3418 output.print("&task_"+ti.getTask().getSafeSymbol()+", ");
3419 output.print(ti.getIndex()+", ");
3420 output.print(otdset.size()+", ");
3421 output.print("optionaltaskfailure_FS"+fscounter+"_"+ti.getTask().getSafeSymbol()+"_"+ti.getIndex()+"_array");
3422 output.println("};");
3425 tiset=sa.getTaskIndex(fs);
3426 boolean needcomma=false;
3428 output.println("struct taskfailure * taskfailurearray"+fscounter+"_"+cdtemp.getSafeSymbol()+"[]={");
3429 for(Iterator<TaskIndex> itti=tiset.iterator(); itti.hasNext(); ) {
3430 TaskIndex ti=itti.next();
3431 if (ti.isRuntime()) {
3438 output.print("&taskfailure_FS"+fscounter+"_"+ti.getTask().getSafeSymbol()+"_"+ti.getIndex());
3440 output.println("};\n");
3442 //Store the result in fsanalysiswrapper
3444 output.println("struct fsanalysiswrapper fsanalysiswrapper_FS"+fscounter+"_"+cdtemp.getSafeSymbol()+"={");
3445 output.println("/*flag*/"+flagid+",");
3446 output.println("/* number of tags*/"+tagcounter+",");
3447 output.println("tags_FS"+fscounter+"_"+cdtemp.getSafeSymbol()+",");
3448 output.println("/* numtask failures */"+(tiset.size()-runtimeti)+",");
3449 output.println("taskfailurearray"+fscounter+"_"+cdtemp.getSafeSymbol()+",");
3450 output.println("/* number of optionaltaskdescriptors */"+availabletasks.size()+",");
3451 output.println("optionaltaskdescriptorarray_FS"+fscounter+"_"+cdtemp.getSafeSymbol());
3452 output.println("};\n");
3456 //Build the array of fsanalysiswrappers
3457 output.println("struct fsanalysiswrapper * fsanalysiswrapperarray_"+cdtemp.getSafeSymbol()+"[] = {");
3458 boolean needcomma=false;
3459 for(int i = 0; i<fscounter; i++) {
3460 if (needcomma) output.print(",");
3461 output.println("&fsanalysiswrapper_FS"+(i+1)+"_"+cdtemp.getSafeSymbol());
3464 output.println("};");
3466 //Build the classanalysiswrapper referring to the previous array
3467 output.println("struct classanalysiswrapper classanalysiswrapper_"+cdtemp.getSafeSymbol()+"={");
3468 output.println("/*type*/"+cdtemp.getId()+",");
3469 output.println("/*numotd*/"+numotd+",");
3470 output.println("otdarray"+cdtemp.getSafeSymbol()+",");
3471 output.println("/* number of fsanalysiswrappers */"+fscounter+",");
3472 output.println("fsanalysiswrapperarray_"+cdtemp.getSafeSymbol()+"};\n");
3473 processedcd.add(cdtemp);
3476 //build an array containing every classes for which code has been build
3477 output.println("struct classanalysiswrapper * classanalysiswrapperarray[]={");
3478 for(int i=0; i<state.numClasses(); i++) {
3479 ClassDescriptor cn=cdarray[i];
3482 if ((cn != null) && (processedcd.contains(cn)))
3483 output.print("&classanalysiswrapper_"+cn.getSafeSymbol());
3485 output.print("NULL");
3487 output.println("};");
3489 output.println("#define MAXOTD "+maxotd);
3490 headers.println("#endif");
3493 public List<OptionalTaskDescriptor> ordertd(Set<OptionalTaskDescriptor> otdset) {
3494 Relation r=new Relation();
3495 for(Iterator<OptionalTaskDescriptor>otdit=otdset.iterator(); otdit.hasNext(); ) {
3496 OptionalTaskDescriptor otd=otdit.next();
3497 TaskIndex ti=new TaskIndex(otd.td, otd.getIndex());
3501 LinkedList<OptionalTaskDescriptor> l=new LinkedList<OptionalTaskDescriptor>();
3502 for(Iterator it=r.keySet().iterator(); it.hasNext(); ) {
3503 Set s=r.get(it.next());
3504 for(Iterator it2=s.iterator(); it2.hasNext(); ) {
3505 OptionalTaskDescriptor otd=(OptionalTaskDescriptor)it2.next();
3513 // override these methods in a subclass of BuildCode
3514 // to generate code for additional systems
3515 protected void printExtraArrayFields(PrintWriter outclassdefs) {
3517 protected void outputTransCode(PrintWriter output) {
3519 protected void buildCodeSetup() {
3521 protected void generateSizeArrayExtensions(PrintWriter outclassdefs) {
3523 protected void additionalIncludesMethodsHeader(PrintWriter outmethodheader) {
3525 protected void preCodeGenInitialization() {
3527 protected void postCodeGenCleanUp() {
3529 protected void additionalCodeGen(PrintWriter outmethodheader,
3530 PrintWriter outstructs,
3531 PrintWriter outmethod) {
3533 protected void additionalCodeAtTopOfMain(PrintWriter outmethod) {
3535 protected void additionalCodeAtBottomOfMain(PrintWriter outmethod) {
3537 protected void additionalIncludesMethodsImplementation(PrintWriter outmethod) {
3539 protected void additionalIncludesStructsHeader(PrintWriter outstructs) {
3541 protected void additionalClassObjectFields(PrintWriter outclassdefs) {
3543 protected void additionalCodeAtTopMethodsImplementation(PrintWriter outmethod) {
3545 protected void additionalCodeAtTopFlatMethodBody(PrintWriter output, FlatMethod fm) {
3547 protected void additionalCodePreNode(FlatMethod fm, FlatNode fn, PrintWriter output) {
3549 protected void additionalCodePostNode(FlatMethod fm, FlatNode fn, PrintWriter output) {
3552 private void printSourceLineNumber(FlatMethod fm, FlatNode fn, PrintWriter output) {
3553 // we do not print out line number if no one explicitly set the number
3554 if(fn.getNumLine()!=-1){
3556 int lineNum=fn.getNumLine();
3558 // do not generate the line number if it is same as the previous one
3559 boolean needtoprint;
3560 if(fn.prev.size()==0){
3566 for(int i=0;i<fn.prev.size();i++){
3567 int prevLineNum=((FlatNode)fn.prev.get(i)).getNumLine();
3568 if(prevLineNum!=lineNum){
3574 output.println("// "+fm.getMethod().getClassDesc().getSourceFileName()+":"+fn.getNumLine());