-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Began drafting assembler for running on HVM instead of current JVM co…
…mpiler.
- Loading branch information
1 parent
452cb1b
commit ec5074f
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
Lang/src/main/java/chipmunk/compiler/assembler/HVMAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package chipmunk.compiler.assembler; | ||
|
||
import myworld.hummingbird.Executable; | ||
import myworld.hummingbird.Opcode; | ||
import myworld.hummingbird.Opcodes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Stack; | ||
|
||
public class HVMAssembler { | ||
|
||
private final Stack<HVMType> types = new Stack<>(); | ||
private final RegisterAllocator registers = new RegisterAllocator(); | ||
protected final Executable.Builder builder = new Executable.Builder(); | ||
|
||
public void push(Object value){ | ||
if(value instanceof Integer || value instanceof Long){ | ||
types.push(HVMType.LONG); | ||
}else if(value instanceof Float || value instanceof Double){ | ||
types.push(HVMType.DOUBLE); | ||
}else if(value instanceof String){ | ||
types.push(HVMType.STRING_REF); | ||
}else{ | ||
// TODO - invalid constant type | ||
} | ||
|
||
// TODO - Strings have to be encoded in the data section, with the address pushed as a constant. | ||
builder.appendOpcode(Opcodes.CONST(0, value)); // TODO - register allocation | ||
} | ||
|
||
public void add(){ | ||
// TODO - for each Chipmunk opcode we have to inspect the HVM operand types and select the | ||
// opcode to use, possibly emitting conversion opcodes (converting longs to doubles, for example). | ||
var a = types.pop(); | ||
var b = types.pop(); | ||
if(a != b){ | ||
promoteType(a, b); | ||
} | ||
|
||
switch (a){ | ||
case LONG -> builder.appendOpcode(Opcodes.ADD(registers.pushRegister(), 0, 0)); | ||
case DOUBLE -> builder.appendOpcode(Opcodes.DADD(registers.pushRegister(), 0, 0)); | ||
} | ||
} | ||
|
||
protected void promoteType(HVMType a, HVMType b){ | ||
if(a == HVMType.LONG && b == HVMType.DOUBLE){ | ||
// TODO - promote register with a | ||
builder.appendOpcode(Opcodes.L2D(0, 0)); | ||
}else if(a == HVMType.DOUBLE && b == HVMType.LONG){ | ||
// TODO - promote register with b | ||
builder.appendOpcode(Opcodes.L2D(0, 0)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package chipmunk.compiler.assembler; | ||
|
||
// TODO - when accounting for object types, this won't be an enum anymore | ||
public enum HVMType { | ||
LONG, | ||
DOUBLE, | ||
STRING_REF, | ||
OBJECT_REF | ||
} |
16 changes: 16 additions & 0 deletions
16
Lang/src/main/java/chipmunk/compiler/assembler/RegisterAllocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package chipmunk.compiler.assembler; | ||
|
||
// TODO - support permanently reserving registers (such as for constants) | ||
public class RegisterAllocator { | ||
private int register; | ||
|
||
public int pushRegister(){ | ||
var r = register; | ||
register++; | ||
return r; | ||
} | ||
|
||
public void popRegister(){ | ||
register--; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters