diff --git a/ChangeLog b/ChangeLog index 3d5dc53..2ba706d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Sun Apr 26 23:35:07 2009 Minero Aoki + + * net/loveruby/cflat/ir: implement IR#dump. + Sun Apr 26 22:41:08 2009 Minero Aoki * net/loveruby/cflat/compiler/IRGenerator.java (BlockNode): static diff --git a/net/loveruby/cflat/ast/DefinedFunction.java b/net/loveruby/cflat/ast/DefinedFunction.java index 5b54075..cfb0629 100644 --- a/net/loveruby/cflat/ast/DefinedFunction.java +++ b/net/loveruby/cflat/ast/DefinedFunction.java @@ -68,12 +68,6 @@ public class DefinedFunction extends Function { d.printMember("body", body); } - protected void _dump(net.loveruby.cflat.ir.Dumper d) { - d.printMember("name", name); - d.printMember("isPrivate", isPrivate); - d.printStmts("ir", ir); - } - public T accept(DeclarationVisitor visitor) { return visitor.visit(this); } diff --git a/net/loveruby/cflat/ir/Addr.java b/net/loveruby/cflat/ir/Addr.java index b36f188..4aad96e 100644 --- a/net/loveruby/cflat/ir/Addr.java +++ b/net/loveruby/cflat/ir/Addr.java @@ -14,4 +14,8 @@ public class Addr extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("expr", expr); + } } diff --git a/net/loveruby/cflat/ir/Bin.java b/net/loveruby/cflat/ir/Bin.java index 4b9e772..9142718 100644 --- a/net/loveruby/cflat/ir/Bin.java +++ b/net/loveruby/cflat/ir/Bin.java @@ -19,4 +19,10 @@ public class Bin extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("op", op.toString()); + d.printMember("left", left); + d.printMember("right", right); + } } diff --git a/net/loveruby/cflat/ir/BranchIf.java b/net/loveruby/cflat/ir/BranchIf.java index c922ce7..63c1564 100644 --- a/net/loveruby/cflat/ir/BranchIf.java +++ b/net/loveruby/cflat/ir/BranchIf.java @@ -33,5 +33,7 @@ public class BranchIf extends Stmt { protected void _dump(Dumper d) { d.printMember("cond", cond); + d.printMember("thenLabel", thenLabel); + d.printMember("elseLabel", elseLabel); } } diff --git a/net/loveruby/cflat/ir/Call.java b/net/loveruby/cflat/ir/Call.java index 7bf10f4..497492e 100644 --- a/net/loveruby/cflat/ir/Call.java +++ b/net/loveruby/cflat/ir/Call.java @@ -59,4 +59,9 @@ public class Call extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("expr", expr); + d.printMembers("args", args); + } } diff --git a/net/loveruby/cflat/ir/Case.java b/net/loveruby/cflat/ir/Case.java index bb2f272..8c6f3f6 100644 --- a/net/loveruby/cflat/ir/Case.java +++ b/net/loveruby/cflat/ir/Case.java @@ -1,7 +1,7 @@ package net.loveruby.cflat.ir; import net.loveruby.cflat.asm.Label; -public class Case { +public class Case implements Dumpable { public long value; public Label label; @@ -9,4 +9,10 @@ public class Case { this.value = value; this.label = label; } + + public void dump(Dumper d) { + d.printClass(this); + d.printMember("value", value); + d.printMember("label", label); + } } diff --git a/net/loveruby/cflat/ir/Dumpable.java b/net/loveruby/cflat/ir/Dumpable.java new file mode 100644 index 0000000..a3bc1ac --- /dev/null +++ b/net/loveruby/cflat/ir/Dumpable.java @@ -0,0 +1,5 @@ +package net.loveruby.cflat.ir; + +public interface Dumpable { + void dump(Dumper d); +} diff --git a/net/loveruby/cflat/ir/Dumper.java b/net/loveruby/cflat/ir/Dumper.java index e205eb4..676adbc 100644 --- a/net/loveruby/cflat/ir/Dumper.java +++ b/net/loveruby/cflat/ir/Dumper.java @@ -1,6 +1,9 @@ package net.loveruby.cflat.ir; import net.loveruby.cflat.ast.DefinedVariable; import net.loveruby.cflat.ast.DefinedFunction; +import net.loveruby.cflat.ast.Location; +import net.loveruby.cflat.asm.Label; +import net.loveruby.cflat.type.Type; import java.util.List; import java.io.PrintStream; @@ -13,39 +16,106 @@ public class Dumper { numIndent = 0; } + public void printClass(Object obj) { + printIndent(); + stream.println("<<" + obj.getClass().getSimpleName() + ">>"); + } + + public void printClass(Object obj, Location loc) { + printIndent(); + stream.println("<<" + obj.getClass().getSimpleName() + ">> (" + loc + ")"); + } + public void printMember(String name, int memb) { - // FIXME + printPair(name, "" + memb); } public void printMember(String name, long memb) { - // FIXME + printPair(name, "" + memb); } public void printMember(String name, boolean memb) { - // FIXME + printPair(name, "" + memb); } public void printMember(String name, String memb) { - // FIXME + printPair(name, memb); } - public void printMember(String name, Stmt memb) { - // FIXME + public void printMember(String name, Label memb) { + printPair(name, memb.toString()); } - public void printMember(String name, Expr memb) { - // FIXME + public void printMember(String name, Type memb) { + printPair(name, memb.toString()); } - public void printStmts(String name, List memb) { - // FIXME + private void printPair(String name, String value) { + printIndent(); + stream.println(name + ": " + value); } - public void printVariables(String name, List memb) { - // FIXME + public void printMember(String name, Dumpable memb) { + printIndent(); + if (memb == null) { + stream.println(name + ": null"); + } + else { + stream.println(name + ":"); + indent(); + memb.dump(this); + unindent(); + } } - public void printFunctions(String name, List memb) { - // FIXME + public void printMembers(String name, List elems) { + printIndent(); + stream.println(name + ":"); + indent(); + for (Dumpable elem : elems) { + elem.dump(this); + } + unindent(); + } + + public void printVars(String name, List vars) { + printIndent(); + stream.println(name + ":"); + indent(); + for (DefinedVariable var : vars) { + printClass(var, var.location()); + printMember("name", var.name()); + printMember("isPrivate", var.isPrivate()); + printMember("type", var.type()); + printMember("initializer", var.ir()); + } + unindent(); + } + + public void printFuncs(String name, List funcs) { + printIndent(); + stream.println(name + ":"); + indent(); + for (DefinedFunction f : funcs) { + printClass(f, f.location()); + printMember("name", f.name()); + printMember("isPrivate", f.isPrivate()); + printMember("type", f.type()); + printMembers("body", f.ir()); + } + unindent(); + } + + private void indent() { numIndent++; } + private void unindent() { numIndent--; } + + static final private String indentString = " "; + + private void printIndent() { + int n = numIndent; + while (n > 0) { + stream.print(indentString); + n--; + } } } diff --git a/net/loveruby/cflat/ir/Expr.java b/net/loveruby/cflat/ir/Expr.java index 71e599c..b1bc9eb 100644 --- a/net/loveruby/cflat/ir/Expr.java +++ b/net/loveruby/cflat/ir/Expr.java @@ -2,7 +2,7 @@ package net.loveruby.cflat.ir; import net.loveruby.cflat.type.Type; import net.loveruby.cflat.asm.*; -abstract public class Expr { +abstract public class Expr implements Dumpable { protected Type type; protected Expr(Type type) { @@ -32,4 +32,11 @@ abstract public class Expr { } abstract public E accept(IRVisitor visitor); + + public void dump(Dumper d) { + d.printClass(this); + _dump(d); + } + + abstract protected void _dump(Dumper d); } diff --git a/net/loveruby/cflat/ir/ExprStmt.java b/net/loveruby/cflat/ir/ExprStmt.java index b52409e..81dd75d 100644 --- a/net/loveruby/cflat/ir/ExprStmt.java +++ b/net/loveruby/cflat/ir/ExprStmt.java @@ -13,11 +13,11 @@ public class ExprStmt extends Stmt { return expr; } - protected void _dump(Dumper d) { - d.printMember("expr", expr); - } - public S accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("expr", expr); + } } diff --git a/net/loveruby/cflat/ir/IR.java b/net/loveruby/cflat/ir/IR.java index 67a38c0..783b3b1 100644 --- a/net/loveruby/cflat/ir/IR.java +++ b/net/loveruby/cflat/ir/IR.java @@ -93,15 +93,13 @@ public class IR { } public void dump() { - dump(System.err); + dump(System.out); } public void dump(PrintStream s) { - throw new Error("FIXME: IR#dump"); - } - - protected void _dump(Dumper d) { - d.printVariables("variables", defvars); - d.printFunctions("functions", defuns); + Dumper d = new Dumper(s); + d.printClass(this, source); + d.printVars("variables", defvars); + d.printFuncs("functions", defuns); } } diff --git a/net/loveruby/cflat/ir/IntValue.java b/net/loveruby/cflat/ir/IntValue.java index baaa15d..f8a8666 100644 --- a/net/loveruby/cflat/ir/IntValue.java +++ b/net/loveruby/cflat/ir/IntValue.java @@ -27,4 +27,8 @@ public class IntValue extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("value", value); + } } diff --git a/net/loveruby/cflat/ir/Jump.java b/net/loveruby/cflat/ir/Jump.java index 3017d3f..5d93026 100644 --- a/net/loveruby/cflat/ir/Jump.java +++ b/net/loveruby/cflat/ir/Jump.java @@ -19,5 +19,6 @@ public class Jump extends Stmt { } protected void _dump(Dumper d) { + d.printMember("label", label); } } diff --git a/net/loveruby/cflat/ir/LabelStmt.java b/net/loveruby/cflat/ir/LabelStmt.java index e82b5c1..9056531 100644 --- a/net/loveruby/cflat/ir/LabelStmt.java +++ b/net/loveruby/cflat/ir/LabelStmt.java @@ -19,5 +19,6 @@ public class LabelStmt extends Stmt { } protected void _dump(Dumper d) { + d.printMember("label", label); } } diff --git a/net/loveruby/cflat/ir/Mem.java b/net/loveruby/cflat/ir/Mem.java index 755a6a6..ebf98d7 100644 --- a/net/loveruby/cflat/ir/Mem.java +++ b/net/loveruby/cflat/ir/Mem.java @@ -14,4 +14,8 @@ public class Mem extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("expr", expr); + } } diff --git a/net/loveruby/cflat/ir/Stmt.java b/net/loveruby/cflat/ir/Stmt.java index e7fc6be..077dcc1 100644 --- a/net/loveruby/cflat/ir/Stmt.java +++ b/net/loveruby/cflat/ir/Stmt.java @@ -1,7 +1,7 @@ package net.loveruby.cflat.ir; import net.loveruby.cflat.ast.Location; -abstract public class Stmt { +abstract public class Stmt implements Dumpable { protected Location location; public Stmt(Location loc) { @@ -14,5 +14,10 @@ abstract public class Stmt { return location; } + public void dump(Dumper d) { + d.printClass(this, location); + _dump(d); + } + abstract protected void _dump(Dumper d); } diff --git a/net/loveruby/cflat/ir/StringValue.java b/net/loveruby/cflat/ir/StringValue.java index c5a7893..db0274f 100644 --- a/net/loveruby/cflat/ir/StringValue.java +++ b/net/loveruby/cflat/ir/StringValue.java @@ -14,7 +14,6 @@ public class StringValue extends Expr { public ConstantEntry entry() { return entry; } public Symbol symbol() { - checkEntry(); return entry.symbol(); } @@ -32,13 +31,11 @@ public class StringValue extends Expr { return entry.address(); } - protected void checkEntry() { - if (entry == null) { - throw new Error("StringLiteralNode#entry not resolved"); - } - } - public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("entry", entry.toString()); + } } diff --git a/net/loveruby/cflat/ir/Switch.java b/net/loveruby/cflat/ir/Switch.java index bd63209..14fe64a 100644 --- a/net/loveruby/cflat/ir/Switch.java +++ b/net/loveruby/cflat/ir/Switch.java @@ -39,5 +39,8 @@ public class Switch extends Stmt { protected void _dump(Dumper d) { d.printMember("cond", cond); + d.printMembers("cases", cases); + d.printMember("defaultLabel", defaultLabel); + d.printMember("endLabel", endLabel); } } diff --git a/net/loveruby/cflat/ir/Uni.java b/net/loveruby/cflat/ir/Uni.java index 1d15422..9d8a89e 100644 --- a/net/loveruby/cflat/ir/Uni.java +++ b/net/loveruby/cflat/ir/Uni.java @@ -17,4 +17,9 @@ public class Uni extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("op", op.toString()); + d.printMember("expr", expr); + } } diff --git a/net/loveruby/cflat/ir/Var.java b/net/loveruby/cflat/ir/Var.java index 1125ecf..71fb09b 100644 --- a/net/loveruby/cflat/ir/Var.java +++ b/net/loveruby/cflat/ir/Var.java @@ -27,4 +27,8 @@ public class Var extends Expr { public E accept(IRVisitor visitor) { return visitor.visit(this); } + + protected void _dump(Dumper d) { + d.printMember("entity", entity.name()); + } }