new test main with code directly (fixes) runtime runing
This commit is contained in:
+30
@@ -17,6 +17,9 @@ public class BytecodeEmitter {
|
||||
private static final int OP_JMP = 0x02;
|
||||
private static final int OP_JMP_IF_FALSE = 0x03;
|
||||
private static final int OP_JMP_IF_TRUE = 0x04;
|
||||
private static final int OP_PUSH_CONST = 0x10;
|
||||
private static final int OP_GET_LOCAL = 0x42;
|
||||
private static final int OP_PUSH_I32 = 0x17;
|
||||
private static final int OP_HOSTCALL = 0x71;
|
||||
private static final int OP_SYSCALL = 0x70;
|
||||
private static final int OP_INTRINSIC = 0x72;
|
||||
@@ -48,6 +51,18 @@ public class BytecodeEmitter {
|
||||
writeOpU32(code, OP_CALL, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case PUSH_CONST -> {
|
||||
writeOpU32(code, OP_PUSH_CONST, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case GET_LOCAL -> {
|
||||
writeOpU32(code, OP_GET_LOCAL, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case PUSH_I32 -> {
|
||||
writeOpU32(code, OP_PUSH_I32, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case JMP -> {
|
||||
writeOpU32(code, OP_JMP, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
@@ -194,6 +209,9 @@ public class BytecodeEmitter {
|
||||
public enum OperationKind {
|
||||
HALT,
|
||||
RET,
|
||||
PUSH_CONST,
|
||||
GET_LOCAL,
|
||||
PUSH_I32,
|
||||
CALL_FUNC,
|
||||
JMP,
|
||||
JMP_IF_TRUE,
|
||||
@@ -254,6 +272,18 @@ public class BytecodeEmitter {
|
||||
return new Operation(OperationKind.CALL_FUNC, funcId, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation pushConst(final int constPoolIndex, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.PUSH_CONST, constPoolIndex, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation getLocal(final int slot, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.GET_LOCAL, slot, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation pushI32(final int value, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.PUSH_I32, value, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation hostcall(
|
||||
final BytecodeModule.SyscallDecl decl,
|
||||
final Integer expectedArgSlots,
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@ public record IRVMOp(
|
||||
|
||||
public static final IRVMOp HALT = new IRVMOp("HALT", 0x01, 0, 0, 0, false, true, false);
|
||||
public static final IRVMOp RET = new IRVMOp("RET", 0x51, 0, 0, 0, false, true, false);
|
||||
public static final IRVMOp PUSH_CONST = new IRVMOp("PUSH_CONST", 0x10, 4, 0, 1, false, false, false);
|
||||
public static final IRVMOp CALL = new IRVMOp("CALL", 0x50, 4, 0, 0, false, false, false);
|
||||
public static final IRVMOp JMP = new IRVMOp("JMP", 0x02, 4, 0, 0, true, true, false);
|
||||
public static final IRVMOp JMP_IF_TRUE = new IRVMOp("JMP_IF_TRUE", 0x04, 4, 1, 0, true, false, false);
|
||||
@@ -19,6 +20,6 @@ public record IRVMOp(
|
||||
public static final IRVMOp HOSTCALL = new IRVMOp("HOSTCALL", 0x71, 4, 0, 0, false, false, false);
|
||||
public static final IRVMOp INTRINSIC = new IRVMOp("INTRINSIC", 0x72, 4, 0, 0, false, false, false);
|
||||
public static final IRVMOp PUSH_I32 = new IRVMOp("PUSH_I32", 0x17, 4, 0, 1, false, false, false);
|
||||
public static final IRVMOp GET_LOCAL = new IRVMOp("GET_LOCAL", 0x42, 4, 0, 1, false, false, false);
|
||||
public static final IRVMOp INTERNAL_EXT = new IRVMOp("IRVM_EXT_NOP", 0xFFFF, 0, 0, 0, false, false, true);
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -11,6 +11,8 @@ public final class IRVMProfileFeatureGate {
|
||||
"core-v1", Set.of(
|
||||
IRVMOp.HALT,
|
||||
IRVMOp.RET,
|
||||
IRVMOp.PUSH_CONST,
|
||||
IRVMOp.GET_LOCAL,
|
||||
IRVMOp.CALL,
|
||||
IRVMOp.JMP,
|
||||
IRVMOp.JMP_IF_TRUE,
|
||||
@@ -21,6 +23,8 @@ public final class IRVMProfileFeatureGate {
|
||||
"experimental-v1", Set.of(
|
||||
IRVMOp.HALT,
|
||||
IRVMOp.RET,
|
||||
IRVMOp.PUSH_CONST,
|
||||
IRVMOp.GET_LOCAL,
|
||||
IRVMOp.CALL,
|
||||
IRVMOp.JMP,
|
||||
IRVMOp.JMP_IF_TRUE,
|
||||
|
||||
+9
@@ -92,6 +92,9 @@ public record IRVMProgram(
|
||||
return switch (instruction.op().opcode()) {
|
||||
case 0x01 -> operation.kind() == BytecodeEmitter.OperationKind.HALT;
|
||||
case 0x51 -> operation.kind() == BytecodeEmitter.OperationKind.RET;
|
||||
case 0x10 -> operation.kind() == BytecodeEmitter.OperationKind.PUSH_CONST && operation.immediate() == immediate;
|
||||
case 0x42 -> operation.kind() == BytecodeEmitter.OperationKind.GET_LOCAL && operation.immediate() == immediate;
|
||||
case 0x17 -> operation.kind() == BytecodeEmitter.OperationKind.PUSH_I32 && operation.immediate() == immediate;
|
||||
case 0x50 -> operation.kind() == BytecodeEmitter.OperationKind.CALL_FUNC && operation.immediate() == immediate;
|
||||
case 0x02 -> operation.kind() == BytecodeEmitter.OperationKind.JMP && operation.immediate() == immediate;
|
||||
case 0x04 -> operation.kind() == BytecodeEmitter.OperationKind.JMP_IF_TRUE && operation.immediate() == immediate;
|
||||
@@ -153,6 +156,9 @@ public record IRVMProgram(
|
||||
return switch (instruction.op().opcode()) {
|
||||
case 0x01 -> BytecodeEmitter.Operation.halt();
|
||||
case 0x51 -> BytecodeEmitter.Operation.ret();
|
||||
case 0x10 -> BytecodeEmitter.Operation.pushConst(immediate, null);
|
||||
case 0x42 -> BytecodeEmitter.Operation.getLocal(immediate, null);
|
||||
case 0x17 -> BytecodeEmitter.Operation.pushI32(immediate, null);
|
||||
case 0x50 -> BytecodeEmitter.Operation.callFunc(immediate);
|
||||
case 0x02 -> BytecodeEmitter.Operation.jmp(immediate, null);
|
||||
case 0x04 -> BytecodeEmitter.Operation.jmpIfTrue(immediate, null);
|
||||
@@ -204,6 +210,9 @@ public record IRVMProgram(
|
||||
return switch (operation.kind()) {
|
||||
case HALT -> new IRVMInstruction(IRVMOp.HALT, null);
|
||||
case RET -> new IRVMInstruction(IRVMOp.RET, null);
|
||||
case PUSH_CONST -> new IRVMInstruction(IRVMOp.PUSH_CONST, operation.immediate());
|
||||
case GET_LOCAL -> new IRVMInstruction(IRVMOp.GET_LOCAL, operation.immediate());
|
||||
case PUSH_I32 -> new IRVMInstruction(IRVMOp.PUSH_I32, operation.immediate());
|
||||
case CALL_FUNC -> new IRVMInstruction(IRVMOp.CALL, operation.immediate());
|
||||
case JMP -> new IRVMInstruction(IRVMOp.JMP, operation.immediate());
|
||||
case JMP_IF_TRUE -> new IRVMInstruction(IRVMOp.JMP_IF_TRUE, operation.immediate());
|
||||
|
||||
+41
-1
@@ -56,6 +56,8 @@ public class LowerToIRVMService {
|
||||
|
||||
final var loweredFunctions = new ArrayList<IRVMFunction>(ordered.size());
|
||||
final var emissionFunctions = new ArrayList<BytecodeEmitter.FunctionPlan>(ordered.size());
|
||||
final var constPool = new ArrayList<BytecodeModule.ConstantPoolEntry>();
|
||||
final var constPoolIndexByString = new HashMap<String, Integer>();
|
||||
|
||||
for (var i = 0; i < ordered.size(); i++) {
|
||||
final var fn = ordered.get(i);
|
||||
@@ -67,6 +69,44 @@ public class LowerToIRVMService {
|
||||
for (final var instr : fn.instructions()) {
|
||||
final var sourceSpan = toBytecodeSpan(fn.fileId().getId(), instr.span());
|
||||
switch (instr.kind()) {
|
||||
case PUSH_I32 -> {
|
||||
final int value;
|
||||
try {
|
||||
value = Integer.parseInt(instr.label());
|
||||
} catch (NumberFormatException e) {
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_INVALID_INTRINSIC_ID,
|
||||
"invalid PUSH_I32 literal payload: " + instr.label());
|
||||
}
|
||||
instructions.add(new IRVMInstruction(IRVMOp.PUSH_I32, value));
|
||||
operations.add(BytecodeEmitter.Operation.pushI32(value, sourceSpan));
|
||||
functionPc += IRVMOp.PUSH_I32.immediateSize() + 2;
|
||||
}
|
||||
case PUSH_CONST -> {
|
||||
final var value = instr.label() == null ? "" : instr.label();
|
||||
final var constPoolIndex = constPoolIndexByString.computeIfAbsent(value, ignored -> {
|
||||
constPool.add(new BytecodeModule.StringConstant(value));
|
||||
return constPool.size() - 1;
|
||||
});
|
||||
instructions.add(new IRVMInstruction(IRVMOp.PUSH_CONST, constPoolIndex));
|
||||
operations.add(BytecodeEmitter.Operation.pushConst(constPoolIndex, sourceSpan));
|
||||
functionPc += IRVMOp.PUSH_CONST.immediateSize() + 2;
|
||||
}
|
||||
case GET_LOCAL -> {
|
||||
final var slot = instr.expectedArgSlots();
|
||||
if (slot == null) {
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
|
||||
"missing local slot for GET_LOCAL");
|
||||
}
|
||||
instructions.add(new IRVMInstruction(IRVMOp.GET_LOCAL, slot));
|
||||
operations.add(BytecodeEmitter.Operation.getLocal(slot, sourceSpan));
|
||||
functionPc += IRVMOp.GET_LOCAL.immediateSize() + 2;
|
||||
}
|
||||
case HALT -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.HALT, null));
|
||||
operations.add(BytecodeEmitter.Operation.halt(sourceSpan));
|
||||
@@ -246,7 +286,7 @@ public class LowerToIRVMService {
|
||||
new IRVMModule(vmProfile, ReadOnlyList.wrap(loweredFunctions)),
|
||||
new BytecodeEmitter.EmissionPlan(
|
||||
0,
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.wrap(constPool),
|
||||
ReadOnlyList.empty(),
|
||||
ReadOnlyList.wrap(emissionFunctions)));
|
||||
validator.validate(program, false);
|
||||
|
||||
Reference in New Issue
Block a user