Skip to content

Commit

Permalink
Began drafting assembler for running on HVM instead of current JVM co…
Browse files Browse the repository at this point in the history
…mpiler.
  • Loading branch information
danielperano committed Dec 15, 2024
1 parent 452cb1b commit ec5074f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Lang/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ group = 'myworld'
version = rootProject.ext.buildVersion

repositories {
mavenLocal()
mavenCentral()
}

Expand All @@ -41,6 +42,7 @@ configurations {
dependencies {

implementation 'org.ow2.asm:asm:9.0'
implementation 'myworld:hummingbird:0.0.1-SNAPSHOT'

testImplementation 'org.apache.groovy:groovy:4.0.21'
testImplementation 'org.spockframework:spock-core:2.3-groovy-4.0'
Expand Down
57 changes: 57 additions & 0 deletions Lang/src/main/java/chipmunk/compiler/assembler/HVMAssembler.java
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));
}
}

}
9 changes: 9 additions & 0 deletions Lang/src/main/java/chipmunk/compiler/assembler/HVMType.java
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
}
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--;
}
}
1 change: 1 addition & 0 deletions Lang/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
open module chipmunk.lang {
requires jdk.dynalink;
requires org.objectweb.asm;
requires hummingbird;
exports chipmunk;
exports chipmunk.binary;
exports chipmunk.compiler;
Expand Down

0 comments on commit ec5074f

Please sign in to comment.