int tag=0;
String localsprefix="___locals___";
String paramsprefix="___params___";
- private static final boolean GENERATEPRECISEGC=true;
+ public static boolean GENERATEPRECISEGC=false;
+ public static String PREFIX="";
public BuildCode(State st, Hashtable temptovar) {
state=st;
PrintWriter outmethodheader=null;
PrintWriter outmethod=null;
try {
- OutputStream str=new FileOutputStream("structdefs.h");
+ OutputStream str=new FileOutputStream(PREFIX+"structdefs.h");
outstructs=new java.io.PrintWriter(str, true);
- str=new FileOutputStream("methodheaders.h");
+ str=new FileOutputStream(PREFIX+"methodheaders.h");
outmethodheader=new java.io.PrintWriter(str, true);
- str=new FileOutputStream("classdefs.h");
+ str=new FileOutputStream(PREFIX+"classdefs.h");
outclassdefs=new java.io.PrintWriter(str, true);
- str=new FileOutputStream("methods.c");
+ str=new FileOutputStream(PREFIX+"methods.c");
outmethod=new java.io.PrintWriter(str, true);
} catch (Exception e) {
e.printStackTrace();
/* Build the actual methods */
outmethod.println("#include \"methodheaders.h\"");
+ outmethod.println("#include <runtime.h>");
Iterator classit=state.getClassSymbolTable().getDescriptorsIterator();
while(classit.hasNext()) {
ClassDescriptor cn=(ClassDescriptor)classit.next();
if (printcomma)
headersout.print(", ");
printcomma=true;
- headersout.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
+ if (temp.getType().isClass())
+ headersout.print("struct " + temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
+ else
+ headersout.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
}
headersout.println(");\n");
}
private void generateFlatMethod(FlatMethod fm, PrintWriter output) {
MethodDescriptor md=fm.getMethod();
- ClassDescriptor cn=md.getClassDesc();
+ ClassDescriptor cn=md.getClassDesc();
ParamsObject objectparams=(ParamsObject)paramstable.get(md);
generateHeader(md,output);
for(int i=0;i<objecttemp.numPrimitives();i++) {
TempDescriptor td=objecttemp.getPrimitive(i);
TypeDescriptor type=td.getType();
- if (type.isClass())
+ System.out.println(td.getSafeSymbol());
+ if (type.isNull())
+ output.println(" void * "+td.getSafeSymbol()+";");
+ else if (type.isClass())
output.println(" struct "+type.getSafeSymbol()+" * "+td.getSafeSymbol()+";");
else
output.println(" "+type.getSafeSymbol()+" "+td.getSafeSymbol()+";");
if (GENERATEPRECISEGC) {
output.print("&__parameterlist__");
needcomma=true;
+ } else {
+ if (fc.getThis()!=null) {
+ output.print(generateTemp(fm,fc.getThis()));
+ needcomma=true;
+ }
}
for(int i=0;i<fc.numArgs();i++) {
VarDescriptor var=md.getParameter(i);
if (printcomma)
output.print(", ");
printcomma=true;
- output.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
+ if (temp.getType().isClass())
+ output.print("struct "+temp.getType().getSafeSymbol()+" * "+temp.getSafeSymbol());
+ else
+ output.print(temp.getType().getSafeSymbol()+" "+temp.getSafeSymbol());
}
output.print(")");
}
TempDescriptor temp_left=TempDescriptor.tempFactory("leftop",on.getLeft().getType());
TempDescriptor temp_right=null;
+ Operation op=on.getOp();
+ if (op.getOp()==Operation.POSTINC||
+ op.getOp()==Operation.POSTDEC||
+ op.getOp()==Operation.PREINC||
+ op.getOp()==Operation.PREDEC) {
+ LiteralNode ln=new LiteralNode("int",new Integer(1));
+ ln.setType(new TypeDescriptor(TypeDescriptor.INT));
+ AssignmentNode an=new AssignmentNode(on.getLeft(),ln,
+ new AssignOperation((op.getOp()==Operation.POSTINC||op.getOp()==Operation.PREINC)?AssignOperation.PLUSEQ:AssignOperation.MINUSEQ));
+ if (op.getOp()==Operation.POSTINC||
+ op.getOp()==Operation.POSTDEC) {
+ NodePair left=flattenExpressionNode(on.getLeft(),out_temp);
+ NodePair assign=flattenAssignmentNode(an,temp_left);
+ left.getEnd().addNext(assign.getBegin());
+ return new NodePair(left.getBegin(),assign.getEnd());
+ } else {
+ NodePair assign=flattenAssignmentNode(an,out_temp);
+ return assign;
+ }
+ }
+
NodePair left=flattenExpressionNode(on.getLeft(),temp_left);
NodePair right;
if (on.getRight()!=null) {
FlatNop nop=new FlatNop();
right=new NodePair(nop,nop);
}
- Operation op=on.getOp();
+
+ if (op.getOp()==Operation.LOGIC_OR) {
+ /* Need to do shortcircuiting */
+ FlatCondBranch fcb=new FlatCondBranch(temp_left);
+ FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
+ FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
+ FlatNop fnop=new FlatNop();
+ left.getEnd().addNext(fcb);
+ fcb.addFalseNext(right.getBegin());
+ right.getEnd().addNext(fon2);
+ fon2.addNext(fnop);
+ fcb.addTrueNext(fon1);
+ fon1.addNext(fnop);
+ return new NodePair(left.getBegin(), fnop);
+ } else if (op.getOp()==Operation.LOGIC_AND) {
+ /* Need to do shortcircuiting */
+ FlatCondBranch fcb=new FlatCondBranch(temp_left);
+ FlatOpNode fon1=new FlatOpNode(out_temp,temp_left,null,new Operation(Operation.ASSIGN));
+ FlatOpNode fon2=new FlatOpNode(out_temp,temp_right,null,new Operation(Operation.ASSIGN));
+ FlatNop fnop=new FlatNop();
+ left.getEnd().addNext(fcb);
+ fcb.addTrueNext(right.getBegin());
+ right.getEnd().addNext(fon2);
+ fon2.addNext(fnop);
+ fcb.addFalseNext(fon1);
+ fon1.addNext(fnop);
+ return new NodePair(left.getBegin(), fnop);
+ }
+
FlatOpNode fon=new FlatOpNode(out_temp,temp_left,temp_right,op);
left.getEnd().addNext(right.getBegin());
right.getEnd().addNext(fon);
if (bestmd==null)
throw new Error("No method found for :"+min.printNode(0));
min.setMethod(bestmd);
+ /* Check whether we need to set this parameter to implied this */
+ if (!bestmd.isStatic()) {
+ if (min.getExpression()==null) {
+ ExpressionNode en=new NameNode(new NameDescriptor("this"));
+ min.setExpression(en);
+ checkExpressionNode(md, nametable, min.getExpression(), null);
+ }
+ }
+
}
return "double";
else if (isFloat())
return "float";
- else throw new Error();
+ else throw new Error("Error Type: "+type);
}
public String getSafeDescriptor() {
public class Main {
public static void main(String args[]) throws Exception {
if (args.length<1) {
- System.out.println("Must input source file");
- System.exit(-1);
+ System.out.println("Must input source file");
+ System.exit(-1);
}
- Reader fr = new BufferedReader(new FileReader(args[0]));
- Lex.Lexer l = new Lex.Lexer(fr);
- java_cup.runtime.lr_parser g;
- g = new Parse.Parser(l);
- ParseNode p=(ParseNode) g./*debug_*/parse().value;
- State state=new State(p);
-
- BuildIR bir=new BuildIR(state);
- bir.buildtree();
-
- TypeUtil tu=new TypeUtil(state);
-
- SemanticCheck sc=new SemanticCheck(state,tu);
- sc.semanticCheck();
-
- BuildFlat bf=new BuildFlat(state);
- bf.buildFlat();
-
- BuildCode bc=new BuildCode(state, bf.getMap());
- bc.buildCode();
-
- System.exit(l.numErrors());
+ for(int i=1;i<args.length;i++) {
+ String option=args[i];
+ if (option.equals("-precise"))
+ IR.Flat.BuildCode.GENERATEPRECISEGC=true;
+ else if (option.equals("-dir"))
+ IR.Flat.BuildCode.PREFIX=args[++i]+"/";
+ else if (option.equals("-help")) {
+ System.out.println("-dir outputdirectory -- output code in outputdirectory");
+ System.out.println("-precise -- use precise garbage collection");
+ System.out.println("-help -- print out help");
+ System.exit(0);
+ }
+ }
+ Reader fr = new BufferedReader(new FileReader(args[0]));
+ Lex.Lexer l = new Lex.Lexer(fr);
+ java_cup.runtime.lr_parser g;
+ g = new Parse.Parser(l);
+ ParseNode p=(ParseNode) g./*debug_*/parse().value;
+ State state=new State(p);
+
+ BuildIR bir=new BuildIR(state);
+ bir.buildtree();
+
+ TypeUtil tu=new TypeUtil(state);
+
+ SemanticCheck sc=new SemanticCheck(state,tu);
+ sc.semanticCheck();
+
+ BuildFlat bf=new BuildFlat(state);
+ bf.buildFlat();
+
+ BuildCode bc=new BuildCode(state, bf.getMap());
+ bc.buildCode();
+
+ System.exit(l.numErrors());
}
}