* net/loveruby/cflat/ir: implement IR#dump.

git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@4156 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
Minero Aoki 2009-04-26 14:35:10 +00:00
parent 04f5d8e6cc
commit 524db547c6
21 changed files with 166 additions and 41 deletions

View File

@ -1,3 +1,7 @@
Sun Apr 26 23:35:07 2009 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/ir: implement IR#dump.
Sun Apr 26 22:41:08 2009 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/compiler/IRGenerator.java (BlockNode): static

View File

@ -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> T accept(DeclarationVisitor<T> visitor) {
return visitor.visit(this);
}

View File

@ -14,4 +14,8 @@ public class Addr extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("expr", expr);
}
}

View File

@ -19,4 +19,10 @@ public class Bin extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("op", op.toString());
d.printMember("left", left);
d.printMember("right", right);
}
}

View File

@ -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);
}
}

View File

@ -59,4 +59,9 @@ public class Call extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("expr", expr);
d.printMembers("args", args);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,5 @@
package net.loveruby.cflat.ir;
public interface Dumpable {
void dump(Dumper d);
}

View File

@ -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<Stmt> memb) {
// FIXME
private void printPair(String name, String value) {
printIndent();
stream.println(name + ": " + value);
}
public void printVariables(String name, List<DefinedVariable> 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<DefinedFunction> memb) {
// FIXME
public void printMembers(String name, List<? extends Dumpable> elems) {
printIndent();
stream.println(name + ":");
indent();
for (Dumpable elem : elems) {
elem.dump(this);
}
unindent();
}
public void printVars(String name, List<DefinedVariable> 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<DefinedFunction> 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--;
}
}
}

View File

@ -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 <S,E> E accept(IRVisitor<S,E> visitor);
public void dump(Dumper d) {
d.printClass(this);
_dump(d);
}
abstract protected void _dump(Dumper d);
}

View File

@ -13,11 +13,11 @@ public class ExprStmt extends Stmt {
return expr;
}
protected void _dump(Dumper d) {
d.printMember("expr", expr);
}
public <S,E> S accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("expr", expr);
}
}

View File

@ -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);
}
}

View File

@ -27,4 +27,8 @@ public class IntValue extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("value", value);
}
}

View File

@ -19,5 +19,6 @@ public class Jump extends Stmt {
}
protected void _dump(Dumper d) {
d.printMember("label", label);
}
}

View File

@ -19,5 +19,6 @@ public class LabelStmt extends Stmt {
}
protected void _dump(Dumper d) {
d.printMember("label", label);
}
}

View File

@ -14,4 +14,8 @@ public class Mem extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("expr", expr);
}
}

View File

@ -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);
}

View File

@ -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 <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("entry", entry.toString());
}
}

View File

@ -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);
}
}

View File

@ -17,4 +17,9 @@ public class Uni extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("op", op.toString());
d.printMember("expr", expr);
}
}

View File

@ -27,4 +27,8 @@ public class Var extends Expr {
public <S,E> E accept(IRVisitor<S,E> visitor) {
return visitor.visit(this);
}
protected void _dump(Dumper d) {
d.printMember("entity", entity.name());
}
}