* net/loveruby/cflat/compiler/CodeGenerator.java: remove unused labels.

* net/loveruby/cflat/asm/AsmStatistics.java: collect label usage.
* net/loveruby/cflat/asm/DirectMemoryReference.java: support collectStatistics.
* net/loveruby/cflat/asm/Literal.java: ditto.
* net/loveruby/cflat/asm/LabelRef.java: ditto.
* net/loveruby/cflat/asm/IntegerLiteral.java: ditto.


git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@4054 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
Minero Aoki 2008-09-23 16:10:42 +00:00
parent f024475c8e
commit f94faf7fde
8 changed files with 62 additions and 8 deletions

View File

@ -1,3 +1,19 @@
Wed Sep 24 01:10:36 2008 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/compiler/CodeGenerator.java: remove unused
labels.
* net/loveruby/cflat/asm/AsmStatistics.java: collect label usage.
* net/loveruby/cflat/asm/DirectMemoryReference.java: support
collectStatistics.
* net/loveruby/cflat/asm/Literal.java: ditto.
* net/loveruby/cflat/asm/LabelRef.java: ditto.
* net/loveruby/cflat/asm/IntegerLiteral.java: ditto.
Wed Sep 24 00:48:37 2008 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/asm/PeepholeOptimizer.java: unify insn

5
ToDo
View File

@ -3,9 +3,6 @@
== Current
* check composite Type#equals, #hashCode (?)
* -O (jump elimination)
* label renaming
* remove useless jump
* Label -> Symbol
* all Symbols should be got from objects
* named/unnamed Label
@ -252,3 +249,5 @@
- generate prologue/epilogue after function body.
- -O (fast immediate load)
- check "puts = str"
- -O (jump elimination)
- useless label elimination

View File

@ -2,8 +2,9 @@ package net.loveruby.cflat.asm;
import java.util.*;
public class AsmStatistics {
protected Map registerUsage;
protected Map insnUsage;
protected Map registerUsage; // Map<Register, Integer>
protected Map insnUsage; // Map<String, Integer>
protected Map labelUsage; // Map<Label, Integer>
static public AsmStatistics collect(List assemblies) {
AsmStatistics stats = new AsmStatistics();
@ -18,13 +19,14 @@ public class AsmStatistics {
public AsmStatistics() {
registerUsage = new HashMap();
insnUsage = new HashMap();
labelUsage = new HashMap();
}
public boolean doesRegisterUsed(Register reg) {
return numRegisterUsage(reg) > 0;
return numRegisterUsed(reg) > 0;
}
public int numRegisterUsage(Register reg) {
public int numRegisterUsed(Register reg) {
return fetchCount(registerUsage, reg);
}
@ -40,6 +42,18 @@ public class AsmStatistics {
incrementCount(insnUsage, insn);
}
public boolean doesLabelUsed(Label label) {
return numLabelUsed(label) > 0;
}
public int numLabelUsed(Label label) {
return fetchCount(labelUsage, label);
}
public void labelUsed(Label label) {
incrementCount(labelUsage, label);
}
protected int fetchCount(Map m, Object key) {
Integer n = (Integer)m.get(key);
if (n == null) {

View File

@ -16,7 +16,7 @@ public class DirectMemoryReference extends MemoryReference {
}
public void collectStatistics(AsmStatistics stats) {
// does nothing
value.collectStatistics(stats);
}
public String toSource() {

View File

@ -21,6 +21,10 @@ public class IntegerLiteral extends Literal {
return this;
}
public void collectStatistics(AsmStatistics stats) {
// does nothing
}
public String toSource() {
return new Long(value).toString();
}

View File

@ -15,6 +15,10 @@ public class LabelRef extends Literal {
return label.name();
}
public void collectStatistics(AsmStatistics stats) {
stats.labelUsed(label);
}
public String toSource() {
return symbol();
}

View File

@ -2,4 +2,5 @@ package net.loveruby.cflat.asm;
abstract public class Literal {
abstract public String toSource();
abstract public void collectStatistics(AsmStatistics stats);
}

View File

@ -217,6 +217,7 @@ public class CodeGenerator extends Visitor implements ASTLHSVisitor {
protected void compileFunctionBody(DefinedFunction func) {
List bodyAsms = compileStmts(func);
AsmStatistics stats = AsmStatistics.collect(bodyAsms);
bodyAsms = reduceLabels(bodyAsms, stats);
List saveRegs = usedCalleeSavedRegisters(stats);
long lvarBytes = allocateLocalVariables(func.body().scope(),
saveRegs.size());
@ -234,6 +235,21 @@ public class CodeGenerator extends Visitor implements ASTLHSVisitor {
return optimizer.optimize(popAssembler().assemblies());
}
protected List reduceLabels(List assemblies, AsmStatistics stats) {
List result = new ArrayList();
Iterator asms = assemblies.iterator();
while (asms.hasNext()) {
Assembly asm = (Assembly)asms.next();
if (asm.isLabel() && ! stats.doesLabelUsed((Label)asm)) {
;
}
else {
result.add(asm);
}
}
return result;
}
// #@@range/compile{
protected void compile(Node n) {
if (verboseAsm) {