r4986@macbookpro: aamine | 2009-05-26 16:07:49 +0900

* net/loveruby/cflat/compiler/Compiler.java: --dump-asm now dumps AssemblyFile.
 * net/loveruby/cflat/compiler/Compiler.java: new option --print-asm, prints assembly source text.
 * net/loveruby/cflat/compiler/CompilerMode.java: new enum PrintAsm.
 * net/loveruby/cflat/compiler/Options.java: new option --print-asm.
 * net/loveruby/cflat/sysdep/x86/AssemblyFile.java: new method #dump.
 * net/loveruby/cflat/sysdep/x86/Register.java: new method #dump.
 * net/loveruby/cflat/asm: new method #dump.
 


git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@4264 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
Minero Aoki 2009-05-26 07:52:44 +00:00
parent 144393f6d4
commit 8049035053
23 changed files with 114 additions and 5 deletions

View File

@ -1,3 +1,24 @@
Tue May 26 16:07:33 2009 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/compiler/Compiler.java: --dump-asm now dumps
AssemblyFile.
* net/loveruby/cflat/compiler/Compiler.java: new option
--print-asm, prints assembly source text.
* net/loveruby/cflat/compiler/CompilerMode.java: new enum
PrintAsm.
* net/loveruby/cflat/compiler/Options.java: new option
--print-asm.
* net/loveruby/cflat/sysdep/x86/AssemblyFile.java: new method
#dump.
* net/loveruby/cflat/sysdep/x86/Register.java: new method #dump.
* net/loveruby/cflat/asm: new method #dump.
Tue May 26 15:16:04 2009 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/sysdep/x86/AssemblyFile.java: refactoring:

View File

@ -18,4 +18,8 @@ public class AbsoluteAddress extends Operand {
public String toSource(SymbolTable table) {
return "*" + register.toSource(table);
}
public String dump() {
return "(AbsoluteAddress " + register.dump() + ")";
}
}

View File

@ -2,6 +2,7 @@ package net.loveruby.cflat.asm;
abstract public class Assembly {
abstract public String toSource(SymbolTable table);
abstract public String dump();
public boolean isInstruction() {
return false;

View File

@ -1,4 +1,5 @@
package net.loveruby.cflat.asm;
import net.loveruby.cflat.utils.TextUtils;
public class Comment extends Assembly {
protected String string;
@ -28,4 +29,8 @@ public class Comment extends Assembly {
}
return buf.toString();
}
public String dump() {
return "(Comment " + TextUtils.dumpString(string) + ")";
}
}

View File

@ -34,4 +34,8 @@ public class DirectMemoryReference extends MemoryReference {
protected int cmp(DirectMemoryReference mem) {
return value.compareTo(mem.value);
}
public String dump() {
return "(DirectMemoryReference " + value.dump() + ")";
}
}

View File

@ -1,4 +1,5 @@
package net.loveruby.cflat.asm;
import net.loveruby.cflat.utils.TextUtils;
public class Directive extends Assembly {
protected String content;
@ -14,4 +15,8 @@ public class Directive extends Assembly {
public String toSource(SymbolTable table) {
return this.content;
}
public String dump() {
return "(Directive " + TextUtils.dumpString(content.trim()) + ")";
}
}

View File

@ -28,4 +28,8 @@ public class ImmediateValue extends Operand {
public String toSource(SymbolTable table) {
return "$" + expr.toSource(table);
}
public String dump() {
return "(ImmediateValue " + expr.dump() + ")";
}
}

View File

@ -70,4 +70,9 @@ public class IndirectMemoryReference extends MemoryReference {
protected int cmp(IndirectMemoryReference mem) {
return offset.compareTo(mem.offset);
}
public String dump() {
return "(IndirectMemoryReference "
+ offset.dump() + " " + base.dump() + ")";
}
}

View File

@ -1,4 +1,5 @@
package net.loveruby.cflat.asm;
import net.loveruby.cflat.utils.TextUtils;
public class Instruction extends Assembly {
protected String mnemonic;
@ -109,4 +110,17 @@ public class Instruction extends Assembly {
public String toString() {
return "#<Insn " + mnemonic + ">";
}
public String dump() {
StringBuilder buf = new StringBuilder();
buf.append("(Instruction ");
buf.append(TextUtils.dumpString(mnemonic));
buf.append(" ");
buf.append(TextUtils.dumpString(suffix));
for (Operand oper : operands) {
buf.append(" ").append(oper.dump());
}
buf.append(")");
return buf.toString();
}
}

View File

@ -45,7 +45,7 @@ public class IntegerLiteral implements Literal {
}
public String toString() {
return "$" + value;
return new Long(value).toString();
}
public int compareTo(Literal lit) {
@ -67,4 +67,8 @@ public class IntegerLiteral implements Literal {
public int cmp(SuffixedSymbol sym) {
return -1;
}
public String dump() {
return "(IntegerLiteral " + new Long(value).toString() + ")";
}
}

View File

@ -22,4 +22,8 @@ public class Label extends Assembly {
public String toSource(SymbolTable table) {
return symbol.toSource(table) + ":";
}
public String dump() {
return "(Label " + symbol.dump() + ")";
}
}

View File

@ -3,6 +3,7 @@ package net.loveruby.cflat.asm;
public interface Literal extends Comparable<Literal> {
public String toSource();
public String toSource(SymbolTable table);
public String dump();
public void collectStatistics(Statistics stats);
public boolean isZero();
public Literal plus(long diff);

View File

@ -42,4 +42,8 @@ public class NamedSymbol extends BaseSymbol {
public int cmp(SuffixedSymbol sym) {
return toString().compareTo(sym.toString());
}
public String dump() {
return "(NamedSymbol " + name + ")";
}
}

View File

@ -2,6 +2,7 @@ package net.loveruby.cflat.asm;
abstract public class Operand implements OperandPattern {
abstract public String toSource(SymbolTable table);
abstract public String dump();
public boolean isRegister() {
return false;

View File

@ -9,5 +9,6 @@ abstract public class Register extends Operand {
stats.registerUsed(this);
}
abstract public String toSource(SymbolTable table);
abstract public String toSource(SymbolTable syms);
abstract public String dump();
}

View File

@ -1,4 +1,5 @@
package net.loveruby.cflat.asm;
import net.loveruby.cflat.utils.TextUtils;
public class SuffixedSymbol implements Symbol {
protected Symbol base;
@ -56,4 +57,9 @@ public class SuffixedSymbol implements Symbol {
public int cmp(SuffixedSymbol sym) {
return toString().compareTo(sym.toString());
}
public String dump() {
return "(SuffixedSymbol " + base.dump() +
" " + TextUtils.dumpString(suffix) + ")";
}
}

View File

@ -3,4 +3,5 @@ package net.loveruby.cflat.asm;
public interface Symbol extends Literal {
public String name();
public String toString();
public String dump();
}

View File

@ -40,4 +40,8 @@ public class UnnamedSymbol extends BaseSymbol {
public int cmp(SuffixedSymbol sym) {
return 1;
}
public String dump() {
return "(UnnamedSymbol @" + Integer.toHexString(hashCode()) + ")";
}
}

View File

@ -116,6 +116,7 @@ public class Compiler {
if (dumpIR(ir, opts.mode())) return;
AssemblyFile asm = generateAssembly(ir, opts);
if (dumpAsm(asm, opts.mode())) return;
if (printAsm(asm, opts.mode())) return;
writeFile(destPath, asm.toSource());
}
@ -252,6 +253,16 @@ public class Compiler {
private boolean dumpAsm(AssemblyFile asm, CompilerMode mode) {
if (mode == CompilerMode.DumpAsm) {
asm.dump(System.out);
return true;
}
else {
return false;
}
}
private boolean printAsm(AssemblyFile asm, CompilerMode mode) {
if (mode == CompilerMode.PrintAsm) {
System.out.print(asm.toSource());
return true;
}

View File

@ -13,6 +13,7 @@ enum CompilerMode {
DumpReference ("--dump-reference"),
DumpIR ("--dump-ir"),
DumpAsm ("--dump-asm"),
PrintAsm ("--print-asm"),
Compile ("-S"),
Assemble ("-c"),
Link ("--link");
@ -29,6 +30,7 @@ enum CompilerMode {
modes.put("--dump-reference", DumpReference);
modes.put("--dump-ir", DumpIR);
modes.put("--dump-asm", DumpAsm);
modes.put("--print-asm", PrintAsm);
modes.put("-S", Compile);
modes.put("-c", Assemble);
}

View File

@ -328,10 +328,11 @@ class Options {
// --dump-stmt is a hidden option.
// --dump-expr is a hidden option.
out.println(" --dump-ast Dumps AST and quit.");
out.println(" --dump-semantic Dumps AST after semantic check and quit.");
out.println(" --dump-semantic Dumps AST after semantic checks and quit.");
// --dump-reference is a hidden option.
out.println(" --dump-ir Dumps IR and quit.");
out.println(" --dump-asm Prints an assembly source and quit.");
out.println(" --dump-asm Dumps AssemblyFile and quit.");
out.println(" --print-asm Prints assembly code and quit.");
out.println(" -S Generates an assembly file and quit.");
out.println(" -c Generates an object file and quit.");
out.println(" -o PATH Places output in file PATH.");

View File

@ -46,7 +46,9 @@ public class AssemblyFile implements net.loveruby.cflat.sysdep.AssemblyFile {
}
public void dump(PrintStream s) {
// FIXME
for (Assembly asm : assemblies) {
s.println(asm.dump());
}
}
void comment(String str) {

View File

@ -65,4 +65,8 @@ class Register extends net.loveruby.cflat.asm.Register {
throw new Error("does not have lower-byte register: " + _class);
}
}
public String dump() {
return "(Register " + _class.toString() + " " + type.toString() + ")";
}
}