Add Class object so that we can implement getClass() and can have class lock for...
[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.Prefetch.*;
9 import Analysis.TaskStateAnalysis.SafetyAnalysis;
10 import IR.ClassDescriptor;
11 import IR.Descriptor;
12 import IR.FlagDescriptor;
13 import IR.MethodDescriptor;
14 import IR.State;
15 import IR.SymbolTable;
16 import IR.TagVarDescriptor;
17 import IR.TaskDescriptor;
18 import IR.TypeDescriptor;
19 import IR.TypeUtil;
20 import IR.VarDescriptor;
21 import IR.Tree.DNFFlag;
22 import IR.Tree.DNFFlagAtom;
23 import IR.Tree.FlagExpressionNode;
24 import IR.Tree.TagExpressionList;
25
26 public class BuildCodeMGC extends BuildCode {
27   int coreNum;
28   int tcoreNum;
29   int gcoreNum;
30   int startupcorenum;    // record the core containing startup task, s
31   // uppose only one core can have startup object
32
33   public BuildCodeMGC(State st, 
34                       Hashtable temptovar, 
35                       TypeUtil typeutil, 
36                       SafetyAnalysis sa,
37                       int coreNum, 
38                       int tcoreNum,
39                       int gcoreNum,
40                       PrefetchAnalysis pa) {
41     super(st, temptovar, typeutil, sa, pa);
42     this.coreNum = coreNum; // # of the active cores
43     this.tcoreNum = tcoreNum; // # of the total number of cores
44     this.gcoreNum = gcoreNum; // # of the cores for gc if any
45     this.startupcorenum = 0;
46   }
47
48   public void buildCode() {
49     /* Create output streams to write to */
50     PrintWriter outclassdefs=null;
51     PrintWriter outglobaldefs=null;
52     PrintWriter outstructs=null;
53     PrintWriter outmethodheader=null;
54     PrintWriter outmethod=null;
55     PrintWriter outvirtual=null;
56
57     try {
58       outstructs=new PrintWriter(new FileOutputStream(PREFIX+"structdefs.h"), true);
59       outmethodheader=new PrintWriter(new FileOutputStream(PREFIX+"methodheaders.h"), true);
60       outclassdefs=new PrintWriter(new FileOutputStream(PREFIX+"classdefs.h"), true);
61       outglobaldefs=new PrintWriter(new FileOutputStream(PREFIX+"globaldefs.h"), true);
62       outvirtual=new PrintWriter(new FileOutputStream(PREFIX+"virtualtable.h"), true);
63       outmethod=new PrintWriter(new FileOutputStream(PREFIX+"methods.c"), true);
64     } catch (Exception e) {
65       e.printStackTrace();
66       System.exit(-1);
67     }
68
69     /* Build the virtual dispatch tables */
70     super.buildVirtualTables(outvirtual);
71     
72     /* Tag the methods that are invoked by static blocks */
73     super.tagMethodInvokedByStaticBlock();
74
75     /* Output includes */
76     outmethodheader.println("#ifndef METHODHEADERS_H");
77     outmethodheader.println("#define METHODHEADERS_H");
78     outmethodheader.println("#include \"structdefs.h\"");
79
80     /* Output Structures */
81     super.outputStructs(outstructs);
82
83     outglobaldefs.println("#ifndef __GLOBALDEF_H_");
84     outglobaldefs.println("#define __GLOBALDEF_H_");
85     outglobaldefs.println("");
86     outglobaldefs.println("struct global_defs_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);
93
94     // Output function prototypes and structures for parameters
95     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
96     int numclasses = this.state.numClasses();
97     while(it.hasNext()) {
98       ClassDescriptor cn=(ClassDescriptor)it.next();
99       super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs);
100     }
101     outclassdefs.println("#endif");
102     outclassdefs.close();
103     outglobaldefs.println("};");
104     outglobaldefs.println("");
105     outglobaldefs.println("extern struct global_defs_t * global_defs_p;");
106     outglobaldefs.println("#endif");
107     outglobaldefs.flush();
108     outglobaldefs.close();
109
110     /* Build the actual methods */
111     super.outputMethods(outmethod);
112
113     /* Record maximum number of task parameters */
114     //outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
115     /* Record maximum number of all types, i.e. length of classsize[] */
116     outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays()));
117     /* Record number of total cores */
118     outstructs.println("#define NUMCORES "+this.tcoreNum);
119     /* Record number of active cores */
120     outstructs.println("#define NUMCORESACTIVE "+this.coreNum); // this.coreNum 
121                                     // can be reset by the scheduling analysis
122     /* Record number of garbage collection cores */
123     outstructs.println("#ifdef MULTICORE_GC");
124     outstructs.println("#define NUMCORES4GC "+this.gcoreNum);
125     outstructs.println("#endif");
126     /* Record number of core containing startup task */
127     outstructs.println("#define STARTUPCORE "+this.startupcorenum);
128     
129     if (state.main!=null) {
130     /* Generate main method */
131       outputMainMethod(outmethod);
132     }
133
134     /* Close files */
135     outmethodheader.println("#endif");
136     outmethodheader.close();
137     outmethod.close();
138     outstructs.println("#endif");
139     outstructs.close();
140   }
141   
142   protected void outputMainMethod(PrintWriter outmethod) {
143     outmethod.println("int mgc_main(int argc, const char *argv[]) {");
144     outmethod.println("  int i;");
145     
146     outputStaticBlocks(outmethod);
147     super.outputClassObjects(outmethod);
148     
149     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
150       outmethod.println("  struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);");
151     } else {
152       outmethod.println("  struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);");
153     }
154     outmethod.println("  for(i=1;i<argc;i++) {");
155     outmethod.println("    int length=strlen(argv[i]);");
156     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
157       outmethod.println("    struct ___String___ *newstring=NewString(NULL, argv[i], length);");
158     } else {
159       outmethod.println("    struct ___String___ *newstring=NewString(argv[i], length);");
160     }
161     outmethod.println("    ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;");
162     outmethod.println("  }");    
163
164     MethodDescriptor md=typeutil.getMain();
165     ClassDescriptor cd=typeutil.getMainClass();
166
167     outmethod.println("   {");
168     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
169       outmethod.print("       struct "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params __parameterlist__={");
170       outmethod.println("1, NULL,"+"stringarray};");
171       outmethod.println("     "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(& __parameterlist__);");
172     } else {
173       outmethod.println("     "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(stringarray);");
174     }
175     outmethod.println("   }");
176
177     outmethod.println("}");
178   }
179   
180   protected void outputStaticBlocks(PrintWriter outmethod) {
181     // execute all the static blocks and all the static field initializations
182     SymbolTable sctbl = this.state.getSClassSymbolTable();
183     Iterator it_sclasses = sctbl.getDescriptorsIterator();
184     if(it_sclasses.hasNext()) {
185       outmethod.println("#define MGC_STATIC_INIT_CHECK");
186       while(it_sclasses.hasNext()) {
187         ClassDescriptor t_cd = (ClassDescriptor)it_sclasses.next();
188         if(t_cd.getNumStaticFields() != 0) {
189           // TODO may need to invoke static field initialization here
190         }
191         MethodDescriptor t_md = (MethodDescriptor)t_cd.getMethodTable().get("staticblocks");
192         outmethod.println("   {");
193         if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
194           outmethod.print("       struct "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
195           outmethod.println("1, NULL};");
196           outmethod.println("     "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
197         } else {
198           outmethod.println("     "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
199         }
200         outmethod.println("   }");
201       }
202       outmethod.println("#undef MGC_STATIC_INIT_CHECK");
203     }
204   }
205 }