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);
72 /* Tag the methods that are invoked by static blocks */
73 super.tagMethodInvokedByStaticBlock();
76 outmethodheader.println("#ifndef METHODHEADERS_H");
77 outmethodheader.println("#define METHODHEADERS_H");
78 outmethodheader.println("#include \"structdefs.h\"");
80 /* Output Structures */
81 super.outputStructs(outstructs);
83 outglobaldefs.println("#ifndef __GLOBALDEF_H_");
84 outglobaldefs.println("#define __GLOBALDEF_H_");
85 outglobaldefs.println("");
86 outglobaldefs.println("struct global_defs_t {");
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);
94 // Output function prototypes and structures for parameters
95 Iterator it=state.getClassSymbolTable().getDescriptorsIterator();
96 int numclasses = this.state.numClasses();
98 ClassDescriptor cn=(ClassDescriptor)it.next();
99 super.generateCallStructs(cn, outclassdefs, outstructs, outmethodheader, outglobaldefs);
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();
110 /* Build the actual methods */
111 super.outputMethods(outmethod);
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);
129 if (state.main!=null) {
130 /* Generate main method */
131 outputMainMethod(outmethod);
135 outmethodheader.println("#endif");
136 outmethodheader.close();
138 outstructs.println("#endif");
142 protected void outputMainMethod(PrintWriter outmethod) {
143 outmethod.println("int mgc_main(int argc, const char *argv[]) {");
144 outmethod.println(" int i;");
146 outputStaticBlocks(outmethod);
147 super.outputClassObjects(outmethod);
149 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
150 outmethod.println(" struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);");
152 outmethod.println(" struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);");
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);");
159 outmethod.println(" struct ___String___ *newstring=NewString(argv[i], length);");
161 outmethod.println(" ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;");
162 outmethod.println(" }");
164 MethodDescriptor md=typeutil.getMain();
165 ClassDescriptor cd=typeutil.getMainClass();
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__);");
173 outmethod.println(" "+cd.getSafeSymbol()+md.getSafeSymbol()+"_"+md.getSafeMethodDescriptor()+"(stringarray);");
175 outmethod.println(" }");
177 outmethod.println("}");
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
191 MethodDescriptor t_md = (MethodDescriptor)t_cd.getMethodTable().get("staticblocks");
193 outmethod.println(" {");
194 if ((GENERATEPRECISEGC) || (this.state.MULTICOREGC)) {
195 outmethod.print(" struct "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"_params __parameterlist__={");
196 outmethod.println("1, NULL};");
197 outmethod.println(" "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"(& __parameterlist__);");
199 outmethod.println(" "+t_cd.getSafeSymbol()+t_md.getSafeSymbol()+"_"+t_md.getSafeMethodDescriptor()+"();");
201 outmethod.println(" }");
204 outmethod.println("#undef MGC_STATIC_INIT_CHECK");