1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-06-03 07:29:37 +00:00

initial attempts at register optimization

This commit is contained in:
jespergravgaard 2017-05-18 00:06:31 +02:00
parent d5ff6e3ea2
commit f8aa837eda
11 changed files with 63 additions and 6 deletions

View File

@ -28,9 +28,15 @@ public class AsmFragment {
StringBuilder signature = new StringBuilder();
if(conditionalJump.getRValue1()!=null) {
signature.append(bind(conditionalJump.getRValue1()));
}
if(conditionalJump.getOperator()!=null) {
signature.append(conditionalJump.getOperator().getOperator());
}
signature.append(bind(conditionalJump.getRValue2()));
if(conditionalJump.getRValue2() instanceof ConstantInteger && ((ConstantInteger) conditionalJump.getRValue2()).getNumber()==0) {
signature.append("0");
} else {
signature.append(bind(conditionalJump.getRValue2()));
}
signature.append("?");
signature.append(bind(conditionalJump.getDestination()));
setSignature(signature.toString());
@ -58,7 +64,15 @@ public class AsmFragment {
if (operator != null) {
signature.append(operator.getOperator());
}
signature.append(bind(rValue2));
if(
rValue2 instanceof ConstantInteger &&
((ConstantInteger) rValue2).getNumber()==1 &&
operator!=null &&
(operator.getOperator().equals("-") || operator.getOperator().equals("+"))) {
signature.append("1");
} else {
signature.append(bind(rValue2));
}
return signature.toString();
}
@ -108,6 +122,10 @@ public class AsmFragment {
String name = "zpbo" + nextZpBoolIdx++;
bindings.put(name, value);
return name;
} else if (RegisterAllocation.RegisterType.REG_X_BYTE.equals(register.getType())) {
String name = "xby" ;
bindings.put(name, value);
return name;
} else {
throw new RuntimeException("Binding of register type not supported " + register.getType());
}

View File

@ -22,6 +22,10 @@ public class Pass3RegisterAllocation {
allocation.allocate(var, new RegisterAllocation.RegisterZpBool(currentZp++));
}
}
allocation.allocate(symbols.getVariable("i#1"), RegisterAllocation.getRegisterX());
allocation.allocate(symbols.getVariable("i#3"), RegisterAllocation.getRegisterX());
allocation.allocate(symbols.getVariable("n1#1"), new RegisterAllocation.RegisterZpByte(8));
allocation.allocate(symbols.getVariable("n1#2"), new RegisterAllocation.RegisterZpByte(8));
symbols.setAllocation(allocation);
}

View File

@ -36,7 +36,7 @@ public class RegisterAllocation {
/** The register type. */
public enum RegisterType {
ZP_BYTE, ZP_BOOL
ZP_BYTE, ZP_BOOL, REG_X_BYTE
};
/** A zero page address used as a register for a single byte variable. */
@ -86,6 +86,23 @@ public class RegisterAllocation {
}
}
/** The X register. */
public static class RegisterXByte implements Register {
@Override
public RegisterType getType() {
return RegisterType.REG_X_BYTE;
}
@Override
public String toString() {
return "reg byte x";
}
}
public static Register getRegisterX() {
return new RegisterXByte();
}
@Override
public String toString() {
StringBuffer out = new StringBuffer();

View File

@ -1,5 +1,7 @@
package dk.camelot64.kickc.icl;
import sun.jvm.hotspot.debugger.cdbg.Sym;
import java.util.*;
/**
@ -75,6 +77,10 @@ public class SymbolTable {
return version;
}
public Variable getVariable(String name) {
return (Variable) symbols.get(name);
}
public void setAllocation(RegisterAllocation allocation) {
this.allocation = allocation;
}

View File

@ -0,0 +1 @@
ldx #{coby1}

View File

@ -0,0 +1 @@
dex

View File

@ -0,0 +1 @@
// x=x

View File

@ -0,0 +1,2 @@
cpx #0
bne {la1}

View File

@ -0,0 +1,2 @@
lda {zpby1}
bne {la1}

View File

@ -0,0 +1,5 @@
lda {zpby1}
cmp #{coby1}
beq !+
bcs {la1}
!:

View File

@ -1,10 +1,10 @@
byte n1 = 0;
byte n2 = 1;
byte i = 0;
byte i = 12;
byte fib = 0;
while(i<=11) {
while(i>0) {
fib = n1 + n2;
n1 = n2;
n2 = fib;
i = i + 1;
i = i - 1;
}