package IR.Flat;
+import java.util.Vector;
public class FlatNode {
+ Vector next;
+
public String toString() {
throw new Error();
}
+ public void addNext(FlatNode n) {
+ next.add(n);
+ }
}
package IR;
import IR.Tree.*;
+import IR.Flat.*;
+import IR.*;
import java.util.*;
public class State {
public State(ParseNode parsetree) {
globals=new SymbolTable();
this.parsetree=parsetree;
- this.set=new HashSet();
+ this.classset=new HashSet();
+ this.treemethodmap=new Hashtable();
+ this.flatmethodmap=new Hashtable();
}
+
public SymbolTable globals;
public ParseNode parsetree;
- public HashSet set;
+ public HashSet classset;
+ public Hashtable treemethodmap;
+ public Hashtable flatmethodmap;
public static TypeDescriptor getTypeDescriptor(int t) {
TypeDescriptor td=new TypeDescriptor(t);
return td;
}
+
public static TypeDescriptor getTypeDescriptor(NameDescriptor n) {
TypeDescriptor td=new TypeDescriptor(n);
return td;
}
- public void addClass(ClassNode tdn) {
- set.add(tdn);
+
+ public void addClass(ClassDescriptor tdn) {
+ classset.add(tdn);
+ }
+
+ public BlockNode getMethodBody(MethodDescriptor md) {
+ return (BlockNode)treemethodmap.get(md);
+
+ }
+
+ public FlatMethod getMethodFlat(MethodDescriptor md) {
+ return (FlatMethod)flatmethodmap.get(md);
+ }
+
+ public void addTreeCode(MethodDescriptor md, BlockNode bn) {
+ treemethodmap.put(md,bn);
+ }
+
+ public void addFlatCode(MethodDescriptor md, FlatMethod bn) {
+ flatmethodmap.put(md,bn);
}
}
public String printNode(int indent) {
return left.printNode(indent)+" "+op.toString()+" "+right.printNode(indent);
}
+
+ public int kind() {
+ return Kind.AssignmentNode;
+ }
}
public String printNode(int indent) {
return en.printNode(indent);
}
+
+ public int kind() {
+ return Kind.BlockExpressionNode;
+ }
}
package IR.Tree;
import java.util.Vector;
-class BlockNode extends TreeNode {
+public class BlockNode extends TreeNode {
Vector blockstatements;
int printStyle=0;
public final static int NORMAL=0;
public void addBlockStatement(BlockStatementNode bsn) {
blockstatements.add(bsn);
}
+
public void setStyle(int style) {
printStyle=style;
}
+ public int size() {
+ return blockstatements.size();
+ }
+
+ public BlockStatementNode get(int i) {
+ return (BlockStatementNode) blockstatements.get(i);
+ }
+
public String printNode(int indent) {
if (printStyle==NORMAL) {
String st="{\n";
return st;
} else throw new Error();
}
+
+ public int kind() {
+ return Kind.BlockNode;
+ }
}
package IR.Tree;
-class BlockStatementNode extends TreeNode {
+public class BlockStatementNode extends TreeNode {
public BlockStatementNode() {
}
ParseNode type_pn=pnv.elementAt(i);
if (isEmpty(type_pn)) /* Skip the semicolon */
continue;
- ClassNode cn=parseTypeDecl(type_pn);
+ ClassDescriptor cn=parseTypeDecl(type_pn);
state.addClass(cn);
}
}
}
- public ClassNode parseTypeDecl(ParseNode pn) {
+ public ClassDescriptor parseTypeDecl(ParseNode pn) {
if (isNode(pn, "class_declaration")) {
- ClassNode cn=new ClassNode();
+ ClassDescriptor cn=new ClassDescriptor();
cn.setName(pn.getChild("name").getTerminal());
if (!isEmpty(pn.getChild("super").getTerminal())) {
/* parse superclass name */
} else throw new Error();
}
- private void parseClassBody(ClassNode cn, ParseNode pn) {
+ private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
ParseNode decls=pn.getChild("class_body_declaration_list");
if (decls!=null) {
ParseNodeVector pnv=decls.getChildren();
}
}
- private void parseClassMember(ClassNode cn, ParseNode pn) {
+ private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
ParseNode fieldnode=pn.getChild("field");
if (fieldnode!=null) {
}
- private void parseFieldDecl(ClassNode cn,ParseNode pn) {
+ private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
ParseNode mn=pn.getChild("modifier");
Modifiers m=parseModifiersList(mn);
}
- private void parseMethodDecl(ClassNode cn, ParseNode pn) {
+ private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
ParseNode headern=pn.getChild("method_header");
ParseNode bodyn=pn.getChild("body");
MethodDescriptor md=parseMethodHeader(headern);
BlockNode bn=parseBlock(bodyn);
- cn.addMethod(md,bn);
+ cn.addMethod(md);
+ state.addTreeCode(md,bn);
}
public BlockNode parseBlock(ParseNode pn) {
else
return "("+etd.printNode(indentlevel)+")"+exp.printNode(indentlevel);
}
+
+ public int kind() {
+ return Kind.CastNode;
+ }
}
+++ /dev/null
-package IR.Tree;
-import java.util.Vector;
-import java.util.Hashtable;
-import IR.FieldDescriptor;
-import IR.MethodDescriptor;
-import IR.NameDescriptor;
-
-public class ClassNode extends TreeNode {
- ClassNode() {
- classname=null;
- superclass=null;
- fields=new Vector();
- methods=new Vector();
- methodmap=new Hashtable();
- }
- String classname;
- NameDescriptor superclass;
- Modifiers modifiers;
- Vector fields;
- Vector methods;
- Hashtable methodmap;
-
- public String printNode(int indent) {
- String st=modifiers.toString()+"class "+classname;
- if (superclass!=null)
- st+="extends "+superclass.toString();
- st+=" {\n";
- indent+=INDENT;
- for(int i=0;i<fields.size();i++) {
- FieldDescriptor fd=(FieldDescriptor)fields.get(i);
- st+=printSpace(indent)+fd.toString()+"\n";
- }
- if (fields.size()>0)
- st+="\n";
-
- for(int i=0;i<methods.size();i++) {
- MethodDescriptor md=(MethodDescriptor)methods.get(i);
- st+=printSpace(indent)+md.toString()+" ";
- BlockNode bn=(BlockNode)methodmap.get(md);
- st+=bn.printNode(indent)+"\n\n";
- }
- st+="}\n";
- return st;
- }
-
- public void addField(FieldDescriptor fd) {
- fields.add(fd);
- }
-
- public void addMethod(MethodDescriptor md, BlockNode b) {
- methods.add(md);
- methodmap.put(md,b);
- }
-
- public void setModifiers(Modifiers modifiers) {
- this.modifiers=modifiers;
- }
- void setName(String name) {
- classname=name;
- }
- void setSuper(NameDescriptor superclass) {
- this.superclass=superclass;
- }
-}
}
return st+")";
}
+
+ public int kind() {
+ return Kind.CreateObjectNode;
+ }
}
return vd.toString();
}
+ public int kind() {
+ return Kind.DeclarationNode;
+ }
}
public String printNode(int indent) {
return left.printNode(indent)+"."+fieldname;
}
+ public int kind() {
+ return Kind.FieldAccessNode;
+ }
}
else
return "if("+cond.printNode(indent)+") "+true_st.printNode(indent)+" else "+ else_st.printNode(indent);
}
+ public int kind() {
+ return Kind.IfStatementNode;
+ }
}
}
return new_st;
}
+ public int kind() {
+ return Kind.LiteralNode;
+ }
}
} else throw new Error();
}
+ public int kind() {
+ return Kind.LoopNode;
+ }
}
}
return st+")";
}
+ public int kind() {
+ return Kind.MethodInvokeNode;
+ }
}
public String printNode(int indent) {
return name.toString();
}
+ public int kind() {
+ return Kind.NameNode;
+ }
}
else
return left.printNode(indent)+" "+op.toString()+" "+right.printNode(indent);
}
+ public int kind() {
+ return Kind.OpNode;
+ }
}
else
return "return "+en.printNode(indent);
}
-
+ public int kind() {
+ return Kind.ReturnNode;
+ }
}
public String printNode(int indent) {
return bn.printNode(indent);
}
-
+ public int kind() {
+ return Kind.SubBlockNode;
+ }
}
package IR.Tree;
-class TreeNode {
+public class TreeNode {
public static final int INDENT=2;
public String printNode(int indent) {
sp+=" ";
return sp;
}
+ public int kind() {
+ throw new Error();
+ }
}
import java.io.FileReader;
import IR.Tree.ParseNode;
import IR.Tree.BuildIR;
+import IR.Flat.BuildFlat;
import IR.State;
-/* Test skeleton for java parser/lexer.
- * Copyright (C) 1998 C. Scott Ananian <cananian@alumni.princeton.edu>
- * This is released under the terms of the GPL with NO WARRANTY.
- * See the file COPYING for more details.
- */
-
public class Main {
public static void main(String args[]) throws Exception {
if (args.length<1) {
State state=new State(p);
BuildIR bir=new BuildIR(state);
bir.buildtree();
+
+ BuildFlat bf=new BuildFlat(state);
+ bf.buildflat();
+
System.exit(l.numErrors());
}
}