3 import java.io.FileOutputStream;
4 import java.io.PrintWriter;
5 import java.util.Hashtable;
6 import java.util.Iterator;
8 import Analysis.Prefetch.*;
9 import Analysis.TaskStateAnalysis.SafetyAnalysis;
10 import IR.ClassDescriptor;
12 import IR.FlagDescriptor;
13 import IR.MethodDescriptor;
15 import IR.SymbolTable;
16 import IR.TagVarDescriptor;
17 import IR.TaskDescriptor;
18 import IR.TypeDescriptor;
20 import IR.VarDescriptor;
21 import IR.Tree.DNFFlag;
22 import IR.Tree.DNFFlagAtom;
23 import IR.Tree.FlagExpressionNode;
24 import IR.Tree.TagExpressionList;
26 public class BuildCodeMGC extends BuildCode {
30 int startupcorenum; // record the core containing startup task, s
31 // uppose only one core can have startup object
33 public BuildCodeMGC(State st,
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;
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;
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) {
69 /* Build the virtual dispatch tables */
70 super.buildVirtualTables(outvirtual);
73 outmethodheader.println("#ifndef METHODHEADERS_H");
74 outmethodheader.println("#define METHODHEADERS_H");
75 outmethodheader.println("#include \"structdefs.h\"");
77 /* Output Structures */
78 super.outputStructs(outstructs);
80 outglobaldefs.println("#ifndef __GLOBALDEF_H_");
81 outglobaldefs.println("#define __GLOBALDEF_H_");
82 outglobaldefs.println("");
83 outglobaldefs.println("struct global_defs_t {");
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);
91 // Output function prototypes and structures for parameters
92 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
93 int numclasses = this.state.numClasses();
95 ClassDescriptor cn=(ClassDescriptor)it.next();
96 super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs);
98 outclassdefs.println("#endif");
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();
107 /* Build the actual methods */
108 super.outputMethods(outmethod);
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);
126 if (state.main!=null) {
127 /* Generate main method */
128 outputMainMethod(outmethod);
132 outmethodheader.println("#endif");
133 outmethodheader.close();
135 outstructs.println("#endif");
139 protected void outputMainMethod(PrintWriter outmethod) {
140 outmethod.println("int mgc_main(int argc, const char *argv[]) {");
141 outmethod.println(" int i;");
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__);");
156 outmethod.println(" "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
158 outmethod.println(" }");
161 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
162 outmethod.println(" struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);");
164 outmethod.println(" struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);");
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);");
171 outmethod.println(" struct ___String___ *newstring=NewString(argv[i], length);");
173 outmethod.println(" ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;");
174 outmethod.println(" }");
176 MethodDescriptor md=typeutil.getMain();
177 ClassDescriptor cd=typeutil.getMainClass();
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__);");
185 outmethod.println(" "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(stringarray);");
187 outmethod.println(" }");
189 outmethod.println("}");