Skip to content

Commit

Permalink
Worked on register allocation across control flow branches, still not…
Browse files Browse the repository at this point in the history
… working correctly
  • Loading branch information
danielperano committed Dec 31, 2024
1 parent 2e415ff commit 996f3ad
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 54 deletions.
12 changes: 8 additions & 4 deletions Lang/src/main/java/chipmunk/compiler/assembler/Operands.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ public Operands(int reserved){

public Operand push(HVMType type){
types.push(type);
return new Operand(calculateRegister(), type);
return new Operand(currentTos(), type);
}

public Operand pop(){
return new Operand(calculateRegister(), types.pop());
return new Operand(currentTos(), types.pop());
}

public Operand dup(){
var type = types.peek();
types.push(type);
return new Operand(calculateRegister(), type);
return new Operand(currentTos(), type);
}

private int calculateRegister(){
public int currentTos(){
return reserved + types.size() - 1;
}

public int nextPushedTos(){
return currentTos() + 1;
}

}
19 changes: 19 additions & 0 deletions Lang/src/main/java/chipmunk/vm/hvm/HvmCodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class HvmCodeBuilder {
protected final IpRemapping remapping;
protected final RegisterStates registerStates;
protected final Operands operands;
protected final TosStates tosStates;

protected final Map<Integer, Supplier<Opcode>> deferredOps;
protected final Map<Integer, Supplier<Symbol>> deferredSymbols;
Expand All @@ -50,6 +51,7 @@ public HvmCodeBuilder(BinaryMethod method){
remapping = new IpRemapping();
registerStates = new RegisterStates(); // Currently used only by get/set local
operands = new Operands(method.getArgCount() + method.getLocalCount());
tosStates = new TosStates();
deferredOps = new HashMap<>();
deferredSymbols = new HashMap<>();
ip = 0;
Expand Down Expand Up @@ -78,9 +80,26 @@ public int appendSymbol(Symbol symbol){
public int appendOpcode(Opcode op){
var hIp = builder.appendOpcode(op);
remapping.remap(lastIp, ip, hIp);
tosStates.markOpcode(hIp, op.dst());
return hIp;
}

public void markJump(int target){
markJump(target, getTos());
}

public void markJump(int target, int tos){
tosStates.markOpcode(target, tos);
}

public int getTos(){
var reg = tosStates.getTosRegister(ip);
if(reg == -1){
reg = operands.currentTos();
}
return reg;
}

public int deferOpcode(Supplier<Opcode> factory){
var hIp = appendOpcode(new Opcode(0xFF));
deferredOps.put(hIp, factory);
Expand Down
Loading

0 comments on commit 996f3ad

Please sign in to comment.