dc115aea69d2d09fbc104c3a6e54f14c12a37629
[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     /* Output includes */
73     outmethodheader.println("#ifndef METHODHEADERS_H");
74     outmethodheader.println("#define METHODHEADERS_H");
75     outmethodheader.println("#include \"structdefs.h\"");
76
77     /* Output Structures */
78     super.outputStructs(outstructs);
79
80     outglobaldefs.println("#ifndef __GLOBALDEF_H_");
81     outglobaldefs.println("#define __GLOBALDEF_H_");
82     outglobaldefs.println("");
83     outglobaldefs.println("struct global_defs_t {");
84     
85     // Output the C class declarations
86     // These could mutually reference each other    
87     outclassdefs.println("#ifndef __CLASSDEF_H_");
88     outclassdefs.println("#define __CLASSDEF_H_");
89     super.outputClassDeclarations(outclassdefs, outglobaldefs);
90
91     // Output function prototypes and structures for parameters
92     Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
93     int numclasses = this.state.numClasses();
94     while(it.hasNext()) {
95       ClassDescriptor cn=(ClassDescriptor)it.next();
96       super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs);
97     }
98     outclassdefs.println("#endif");
99     outclassdefs.close();
100     outglobaldefs.println("};");
101     outglobaldefs.println("");
102     outglobaldefs.println("extern struct global_defs_t * global_defs_p;");
103     outglobaldefs.println("#endif");
104     outglobaldefs.flush();
105     outglobaldefs.close();
106
107     /* Build the actual methods */
108     super.outputMethods(outmethod);
109
110     /* Record maximum number of task parameters */
111     //outstructs.println("#define MAXTASKPARAMS "+maxtaskparams);
112     /* Record maximum number of all types, i.e. length of classsize[] */
113     outstructs.println("#define NUMTYPES "+(state.numClasses() + state.numArrays()));
114     /* Record number of total cores */
115     outstructs.println("#define NUMCORES "+this.tcoreNum);
116     /* Record number of active cores */
117     outstructs.println("#define NUMCORESACTIVE "+this.coreNum); // this.coreNum 
118                                     // can be reset by the scheduling analysis
119     /* Record number of garbage collection cores */
120     outstructs.println("#ifdef MULTICORE_GC");
121     outstructs.println("#define NUMCORES4GC "+this.gcoreNum);
122     outstructs.println("#endif");
123     /* Record number of core containing startup task */
124     outstructs.println("#define STARTUPCORE "+this.startupcorenum);
125     
126     if (state.main!=null) {
127     /* Generate main method */
128       outputMainMethod(outmethod);
129     }
130
131     /* Close files */
132     outmethodheader.println("#endif");
133     outmethodheader.close();
134     outmethod.close();
135     outstructs.println("#endif");
136     outstructs.close();
137   }
138   
139   protected void outputMainMethod(PrintWriter outmethod) {
140     outmethod.println("int mgc_main(int argc, const char *argv[]) {");
141     outmethod.println("  int i;");
142     
143     // execute all the static blocks in random order 
144     // TODO may need more careful about the execution order
145     SymbolTable sbt = this.state.getStaticBlockSymbolTable();
146     Iterator it_staticblocks = state.getStaticBlockSymbolTable().getDescriptorsIterator();
147     while(it_staticblocks.hasNext()) {
148       MethodDescriptor t_md = (MethodDescriptor) it_staticblocks.next();
149       ClassDescriptor t_cd = t_md.getClassDesc();
150       outmethod.println("   {");
151       if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
152         outmethod.print("       struct "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
153         outmethod.println("1, NULL};");
154         outmethod.println("     "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
155       } else {
156         outmethod.println("     "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
157       }
158       outmethod.println("   }");
159     }
160     
161     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
162       outmethod.println("  struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);");
163     } else {
164       outmethod.println("  struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);");
165     }
166     outmethod.println("  for(i=1;i<argc;i++) {");
167     outmethod.println("    int length=strlen(argv[i]);");
168     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
169       outmethod.println("    struct ___String___ *newstring=NewString(NULL, argv[i], length);");
170     } else {
171       outmethod.println("    struct ___String___ *newstring=NewString(argv[i], length);");
172     }
173     outmethod.println("    ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;");
174     outmethod.println("  }");    
175
176     MethodDescriptor md=typeutil.getMain();
177     ClassDescriptor cd=typeutil.getMainClass();
178
179     outmethod.println("   {");
180     if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
181       outmethod.print("       struct "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"_params __parameterlist__={");
182       outmethod.println("1, NULL,"+"stringarray};");
183       outmethod.println("     "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(& __parameterlist__);");
184     } else {
185       outmethod.println("     "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(stringarray);");
186     }
187     outmethod.println("   }");
188
189     outmethod.println("}");
190   }
191 }