changes
[IRC.git] / Robust / src / IR / Flat / BuildCodeMGC.java
1 package IR.Flat;
2
3 import java.io.FileOutputStream;
4 import java.io.PrintWriter;
5 import java.util.Hashtable;
6 import java.util.Iterator;
7
8 import Analysis.TaskStateAnalysis.SafetyAnalysis;
9 import IR.ClassDescriptor;
10 import IR.MethodDescriptor;
11 import IR.State;
12 import IR.SymbolTable;
13 import IR.TypeUtil;
14
15 public class BuildCodeMGC extends BuildCode {
16   int coreNum;
17   int tcoreNum;
18   int gcoreNum;
19   int startupcorenum;    // record the core containing startup task, s
20   // uppose only one core can have startup object
21
22   public BuildCodeMGC(State st, 
23                       Hashtable temptovar, 
24                       TypeUtil typeutil, 
25                       SafetyAnalysis sa,
26                       int coreNum, 
27                       int tcoreNum,
28                       int gcoreNum) {
29     super(st, temptovar, typeutil, sa);
30     this.coreNum = coreNum; // # of the active cores
31     this.tcoreNum = tcoreNum; // # of the total number of cores
32     this.gcoreNum = gcoreNum; // # of the cores for gc if any
33     this.startupcorenum = 0;
34   }
35
36   public void buildCode() {
37     /* Create output streams to write to */
38     PrintWriter outclassdefs=null;
39     PrintWriter outglobaldefs=null;
40     PrintWriter outglobaldefsprim=null;
41     PrintWriter outstructs=null;
42     PrintWriter outmethodheader=null;
43     PrintWriter outmethod=null;
44     PrintWriter outvirtual=null;
45
46     try {
47       outstructs=new PrintWriter(new FileOutputStream(PREFIX+"structdefs.h"), true);
48       outmethodheader=new PrintWriter(new FileOutputStream(PREFIX+"methodheaders.h"), true);
49       outclassdefs=new PrintWriter(new FileOutputStream(PREFIX+"classdefs.h"), true);
50       outglobaldefs=new PrintWriter(new FileOutputStream(PREFIX+"globaldefs.h"), true);
51       outglobaldefsprim=new PrintWriter(new FileOutputStream(PREFIX+"globaldefsprim.h"), true);
52       outvirtual=new PrintWriter(new FileOutputStream(PREFIX+"virtualtable.h"), true);
53       outmethod=new PrintWriter(new FileOutputStream(PREFIX+"methods.c"), true);
54     } catch (Exception e) {
55       e.printStackTrace();
56       System.exit(-1);
57     }
58     
59     /* Fix field safe symbols due to shadowing */
60     FieldShadow.handleFieldShadow(state);
61
62     /* Build the virtual dispatch tables */
63     super.buildVirtualTables(outvirtual);
64     
65     /* Tag the methods that are invoked by static blocks */
66     super.tagMethodInvokedByStaticBlock();
67
68     /* Output includes */
69     outmethodheader.println("#ifndef METHODHEADERS_H");
70     outmethodheader.println("#define METHODHEADERS_H");
71     outmethodheader.println("#include \"structdefs.h\"");
72
73     /* Output Structures */
74     super.outputStructs(outstructs);
75
76     outglobaldefs.println("#ifndef __GLOBALDEF_H_");
77     outglobaldefs.println("#define __GLOBALDEF_H_");
78     outglobaldefs.println("");
79     outglobaldefs.println("struct global_defs_t {");
80     outglobaldefs.println("  int size;");
81     outglobaldefs.println("  void * next;");
82
83     outglobaldefsprim.println("#ifndef __GLOBALDEFPRIM_H_");
84     outglobaldefsprim.println("#define __GLOBALDEFPRIM_H_");
85     outglobaldefsprim.println("");
86     outglobaldefsprim.println("struct global_defsprim_t {");
87     
88     // Output the C class declarations
89     // These could mutually reference each other    
90     outclassdefs.println("#ifndef __CLASSDEF_H_");
91     outclassdefs.println("#define __CLASSDEF_H_");
92     super.outputClassDeclarations(outclassdefs, outglobaldefs, outglobaldefsprim);
93
94     // Output function prototypes and structures for parameters
95     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
96     while(it.hasNext()) {
97       ClassDescriptor cn=(ClassDescriptor)it.next();
98       super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs, outglobaldefsprim);
99     }
100     // TODO add version for normal Java later
101     outclassdefs.println("#include \"globaldefs.h\"");
102     outclassdefs.println("#include \"globaldefsprim.h\"");
103     outclassdefs.println("#endif");
104     outclassdefs.close();
105     outglobaldefs.println("};");
106     outglobaldefs.println("");
107     outglobaldefs.println("extern struct global_defs_t * global_defs_p;");
108     outglobaldefs.println("#endif");
109     outglobaldefs.flush();
110     outglobaldefs.close();
111
112     outglobaldefsprim.println("};");
113     outglobaldefsprim.println("");
114     outglobaldefsprim.println("extern struct global_defsprim_t * global_defsprim_p;");
115     outglobaldefsprim.println("#endif");
116     outglobaldefsprim.flush();
117     outglobaldefsprim.close();
118
119     /* Build the actual methods */
120     super.outputMethods(outmethod);
121
122     /* Record maximum number of task parameters */
123     //outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
124     /* Record maximum number of all types, i.e. length of classsize[] */
125     outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays() + state.numInterfaces()));
126     /* Record number of total cores */
127     outstructs.println("#define NUMCORES "+this.tcoreNum);
128     /* Record number of active cores */
129     outstructs.println("#define NUMCORESACTIVE "+this.coreNum); // this.coreNum 
130                                     // can be reset by the scheduling analysis
131     /* Record number of garbage collection cores */
132     outstructs.println("#ifdef MULTICORE_GC");
133     outstructs.println("#define NUMCORES4GC "+this.gcoreNum);
134     outstructs.println("#endif");
135     /* Record number of core containing startup task */
136     outstructs.println("#define STARTUPCORE "+this.startupcorenum);
137     
138     if (state.main!=null) {
139     /* Generate main method */
140       outputMainMethod(outmethod);
141     }
142
143     /* Close files */
144     outmethodheader.println("#endif");
145     outmethodheader.close();
146     outmethod.close();
147     outstructs.println("#endif");
148     outstructs.close();
149   }
150   
151   protected void outputMainMethod(PrintWriter outmethod) {
152     outmethod.println("int mgc_main(int argc, const char *argv[]) {");
153     outmethod.println("  int i;");
154     
155     if (state.MULTICOREGC) {
156       outmethod.println("  global_defs_p->size="+globaldefscount+";");
157       outmethod.println("  for(i=0;i<"+globaldefscount+";i++) {");
158       outmethod.println("    ((struct garbagelist *)global_defs_p)->array[i]=NULL;");
159       outmethod.println("  }");
160     }
161     
162     outputStaticBlocks(outmethod);
163     outputClassObjects(outmethod);
164     
165     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
166       outmethod.println("  struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);");
167     } else {
168       outmethod.println("  struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);");
169     }
170     outmethod.println("  for(i=1;i<argc;i++) {");
171     outmethod.println("    int length=strlen(argv[i]);");
172     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
173       outmethod.println("    struct ___String___ *newstring=NewString(NULL, argv[i], length);");
174     } else {
175       outmethod.println("    struct ___String___ *newstring=NewString(argv[i], length);");
176     }
177     outmethod.println("    ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;");
178     outmethod.println("  }");    
179
180     MethodDescriptor md=typeutil.getMain();
181     ClassDescriptor cd=typeutil.getMainClass();
182
183     outmethod.println("   {");
184     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
185       outmethod.print("       struct "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params __parameterlist__={");
186       outmethod.println("1, NULL,"+"stringarray};");
187       outmethod.println("     "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(& __parameterlist__);");
188     } else {
189       outmethod.println("     "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(stringarray);");
190     }
191     outmethod.println("   }");
192
193     outmethod.println("}");
194   }
195   
196   protected void outputClassObjects(PrintWriter outmethod) {
197     // for each class, initialize its Class object
198     if(state.MGC) {
199       SymbolTable ctbl = this.state.getClassSymbolTable();
200       Iterator it_classes = ctbl.getDescriptorsIterator();
201
202       while(it_classes.hasNext()) {
203         ClassDescriptor t_cd = (ClassDescriptor)it_classes.next();
204         // TODO initialize the Class object for this class  ++
205         outmethod.println(" {");
206         if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
207           outmethod.println("    struct garbagelist dummy={0,NULL};");
208           outmethod.println("    global_defs_p->"+t_cd.getSafeSymbol()+"classobj = allocate_new(&dummy, " + typeutil.getClass(TypeUtil.ObjectClass).getId() + ");");
209         } else {
210           outmethod.println("    global_defs_p->"+t_cd.getSafeSymbol()+"classobj = allocate_new(" + typeutil.getClass(TypeUtil.ObjectClass).getId() + ");");
211         }
212         outmethod.println("    global_defs_p->"+t_cd.getSafeSymbol()+"classobj->type = " + t_cd.getId() + ";");
213         
214         outmethod.println("    initlock((struct ___Object___ *)((global_defs_p->"+t_cd.getSafeSymbol()+"classobj)));");
215         outmethod.println(" }");
216         
217       }
218     } // else TODO normal java version 
219   }
220 }