implements PR-O3.1
This commit is contained in:
+30
@@ -13,6 +13,9 @@ public class BytecodeEmitter {
|
||||
private static final int OP_HALT = 0x01;
|
||||
private static final int OP_RET = 0x51;
|
||||
private static final int OP_CALL = 0x50;
|
||||
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_HOSTCALL = 0x71;
|
||||
private static final int OP_SYSCALL = 0x70;
|
||||
private static final int OP_INTRINSIC = 0x72;
|
||||
@@ -42,6 +45,18 @@ public class BytecodeEmitter {
|
||||
writeOpU32(code, OP_CALL, 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())));
|
||||
}
|
||||
case JMP_IF_TRUE -> {
|
||||
writeOpU32(code, OP_JMP_IF_TRUE, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case JMP_IF_FALSE -> {
|
||||
writeOpU32(code, OP_JMP_IF_FALSE, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case INTRINSIC -> {
|
||||
writeOpU32(code, OP_INTRINSIC, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
@@ -149,6 +164,9 @@ public class BytecodeEmitter {
|
||||
HALT,
|
||||
RET,
|
||||
CALL_FUNC,
|
||||
JMP,
|
||||
JMP_IF_TRUE,
|
||||
JMP_IF_FALSE,
|
||||
HOSTCALL,
|
||||
RAW_SYSCALL,
|
||||
INTRINSIC,
|
||||
@@ -212,6 +230,18 @@ public class BytecodeEmitter {
|
||||
return new Operation(OperationKind.HOSTCALL, 0, decl, expectedArgSlots, expectedRetSlots, span);
|
||||
}
|
||||
|
||||
public static Operation jmp(final int targetPc, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.JMP, targetPc, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation jmpIfTrue(final int targetPc, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.JMP_IF_TRUE, targetPc, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation jmpIfFalse(final int targetPc, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.JMP_IF_FALSE, targetPc, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation rawSyscall(final int syscallId) {
|
||||
return new Operation(OperationKind.RAW_SYSCALL, syscallId, null, null, null, null);
|
||||
}
|
||||
|
||||
+1
@@ -5,4 +5,5 @@ public enum IRVMLoweringErrorCode {
|
||||
LOWER_IRVM_MISSING_CALLEE,
|
||||
LOWER_IRVM_CALL_ARG_SLOTS_MISMATCH,
|
||||
LOWER_IRVM_CALL_RET_SLOTS_MISMATCH,
|
||||
LOWER_IRVM_MISSING_JUMP_TARGET,
|
||||
}
|
||||
|
||||
+62
@@ -49,16 +49,21 @@ public class LowerToIRVMService {
|
||||
final var fn = ordered.get(i);
|
||||
final var instructions = new ArrayList<IRVMInstruction>(fn.instructions().size());
|
||||
final var operations = new ArrayList<BytecodeEmitter.Operation>(fn.instructions().size());
|
||||
final var labelToPc = new HashMap<String, Integer>();
|
||||
final var jumpPatches = new ArrayList<JumpPatch>();
|
||||
var functionPc = 0;
|
||||
for (final var instr : fn.instructions()) {
|
||||
final var sourceSpan = toBytecodeSpan(fn.fileId().getId(), instr.span());
|
||||
switch (instr.kind()) {
|
||||
case HALT -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.HALT, null));
|
||||
operations.add(BytecodeEmitter.Operation.halt(sourceSpan));
|
||||
functionPc += IRVMOp.HALT.immediateSize() + 2;
|
||||
}
|
||||
case RET -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.RET, null));
|
||||
operations.add(BytecodeEmitter.Operation.ret(sourceSpan));
|
||||
functionPc += IRVMOp.RET.immediateSize() + 2;
|
||||
}
|
||||
case CALL_FUNC -> {
|
||||
final var key = callableKey(instr.calleeModuleKey(), instr.calleeCallableName());
|
||||
@@ -89,6 +94,7 @@ public class LowerToIRVMService {
|
||||
}
|
||||
instructions.add(new IRVMInstruction(IRVMOp.CALL, calleeId));
|
||||
operations.add(BytecodeEmitter.Operation.callFunc(calleeId, sourceSpan));
|
||||
functionPc += IRVMOp.CALL.immediateSize() + 2;
|
||||
}
|
||||
case CALL_HOST -> {
|
||||
final var host = instr.hostCall();
|
||||
@@ -103,14 +109,62 @@ public class LowerToIRVMService {
|
||||
host.argSlots(),
|
||||
host.retSlots(),
|
||||
sourceSpan));
|
||||
functionPc += IRVMOp.HOSTCALL.immediateSize() + 2;
|
||||
}
|
||||
case CALL_INTRINSIC -> {
|
||||
final var intrinsic = instr.intrinsicCall();
|
||||
instructions.add(new IRVMInstruction(IRVMOp.INTRINSIC, intrinsic.intrinsicId()));
|
||||
operations.add(BytecodeEmitter.Operation.intrinsic(intrinsic.intrinsicId(), sourceSpan));
|
||||
functionPc += IRVMOp.INTRINSIC.immediateSize() + 2;
|
||||
}
|
||||
case LABEL -> {
|
||||
final var label = instr.label();
|
||||
if (labelToPc.putIfAbsent(label, functionPc) != null) {
|
||||
throw new IRVMLoweringException(
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_JUMP_TARGET,
|
||||
"duplicate label in function '" + fn.callableName() + "': " + label);
|
||||
}
|
||||
}
|
||||
case JMP -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.JMP, 0));
|
||||
operations.add(BytecodeEmitter.Operation.jmp(0, sourceSpan));
|
||||
jumpPatches.add(new JumpPatch(instructions.size() - 1, operations.size() - 1, instr.targetLabel(), IRVMOp.JMP, sourceSpan));
|
||||
functionPc += IRVMOp.JMP.immediateSize() + 2;
|
||||
}
|
||||
case JMP_IF_TRUE -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.JMP_IF_TRUE, 0));
|
||||
operations.add(BytecodeEmitter.Operation.jmpIfTrue(0, sourceSpan));
|
||||
jumpPatches.add(new JumpPatch(instructions.size() - 1, operations.size() - 1, instr.targetLabel(), IRVMOp.JMP_IF_TRUE, sourceSpan));
|
||||
functionPc += IRVMOp.JMP_IF_TRUE.immediateSize() + 2;
|
||||
}
|
||||
case JMP_IF_FALSE -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.JMP_IF_FALSE, 0));
|
||||
operations.add(BytecodeEmitter.Operation.jmpIfFalse(0, sourceSpan));
|
||||
jumpPatches.add(new JumpPatch(instructions.size() - 1, operations.size() - 1, instr.targetLabel(), IRVMOp.JMP_IF_FALSE, sourceSpan));
|
||||
functionPc += IRVMOp.JMP_IF_FALSE.immediateSize() + 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (final var patch : jumpPatches) {
|
||||
final var targetPc = labelToPc.get(patch.targetLabel());
|
||||
if (targetPc == null) {
|
||||
throw new IRVMLoweringException(
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_JUMP_TARGET,
|
||||
"missing jump target label '" + patch.targetLabel() + "' in function '" + fn.callableName() + "'");
|
||||
}
|
||||
instructions.set(patch.instructionIndex(), new IRVMInstruction(patch.op(), targetPc));
|
||||
final BytecodeEmitter.Operation operation;
|
||||
if (patch.op() == IRVMOp.JMP) {
|
||||
operation = BytecodeEmitter.Operation.jmp(targetPc, patch.span());
|
||||
} else if (patch.op() == IRVMOp.JMP_IF_TRUE) {
|
||||
operation = BytecodeEmitter.Operation.jmpIfTrue(targetPc, patch.span());
|
||||
} else if (patch.op() == IRVMOp.JMP_IF_FALSE) {
|
||||
operation = BytecodeEmitter.Operation.jmpIfFalse(targetPc, patch.span());
|
||||
} else {
|
||||
throw new IllegalStateException("unexpected jump op: " + patch.op().name());
|
||||
}
|
||||
operations.set(patch.operationIndex(), operation);
|
||||
}
|
||||
loweredFunctions.add(new IRVMFunction(
|
||||
fn.callableName(),
|
||||
fn.paramSlots(),
|
||||
@@ -187,4 +241,12 @@ public class LowerToIRVMService {
|
||||
}
|
||||
return (int) value;
|
||||
}
|
||||
|
||||
private record JumpPatch(
|
||||
int instructionIndex,
|
||||
int operationIndex,
|
||||
String targetLabel,
|
||||
IRVMOp op,
|
||||
BytecodeModule.SourceSpan span) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user