aritmetics
This commit is contained in:
+185
@@ -18,7 +18,25 @@ public class BytecodeEmitter {
|
||||
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_POP = 0x11;
|
||||
private static final int OP_PUSH_BOOL = 0x16;
|
||||
private static final int OP_ADD = 0x20;
|
||||
private static final int OP_SUB = 0x21;
|
||||
private static final int OP_MUL = 0x22;
|
||||
private static final int OP_DIV = 0x23;
|
||||
private static final int OP_MOD = 0x24;
|
||||
private static final int OP_EQ = 0x30;
|
||||
private static final int OP_NEQ = 0x31;
|
||||
private static final int OP_LT = 0x32;
|
||||
private static final int OP_GT = 0x33;
|
||||
private static final int OP_AND = 0x34;
|
||||
private static final int OP_OR = 0x35;
|
||||
private static final int OP_NOT = 0x36;
|
||||
private static final int OP_LTE = 0x3C;
|
||||
private static final int OP_GTE = 0x3D;
|
||||
private static final int OP_NEG = 0x3E;
|
||||
private static final int OP_GET_LOCAL = 0x42;
|
||||
private static final int OP_SET_LOCAL = 0x43;
|
||||
private static final int OP_PUSH_I32 = 0x17;
|
||||
private static final int OP_HOSTCALL = 0x71;
|
||||
private static final int OP_SYSCALL = 0x70;
|
||||
@@ -55,14 +73,86 @@ public class BytecodeEmitter {
|
||||
writeOpU32(code, OP_PUSH_CONST, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case POP -> {
|
||||
writeOpNoImm(code, OP_POP);
|
||||
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 SET_LOCAL -> {
|
||||
writeOpU32(code, OP_SET_LOCAL, op.immediate());
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case PUSH_BOOL -> {
|
||||
writeOpU8(code, OP_PUSH_BOOL, op.immediate() == 0 ? 0 : 1);
|
||||
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 ADD -> {
|
||||
writeOpNoImm(code, OP_ADD);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case SUB -> {
|
||||
writeOpNoImm(code, OP_SUB);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case MUL -> {
|
||||
writeOpNoImm(code, OP_MUL);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case DIV -> {
|
||||
writeOpNoImm(code, OP_DIV);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case MOD -> {
|
||||
writeOpNoImm(code, OP_MOD);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case EQ -> {
|
||||
writeOpNoImm(code, OP_EQ);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case NEQ -> {
|
||||
writeOpNoImm(code, OP_NEQ);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case LT -> {
|
||||
writeOpNoImm(code, OP_LT);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case LTE -> {
|
||||
writeOpNoImm(code, OP_LTE);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case GT -> {
|
||||
writeOpNoImm(code, OP_GT);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case GTE -> {
|
||||
writeOpNoImm(code, OP_GTE);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case AND -> {
|
||||
writeOpNoImm(code, OP_AND);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case OR -> {
|
||||
writeOpNoImm(code, OP_OR);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case NOT -> {
|
||||
writeOpNoImm(code, OP_NOT);
|
||||
spans.add(new BytecodeFunctionLayoutBuilder.InstructionSpan(pc, spanOrUnknown(op.span())));
|
||||
}
|
||||
case NEG -> {
|
||||
writeOpNoImm(code, OP_NEG);
|
||||
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())));
|
||||
@@ -136,6 +226,11 @@ public class BytecodeEmitter {
|
||||
out.writeBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(immediate).array());
|
||||
}
|
||||
|
||||
private static void writeOpU8(final ByteArrayOutputStream out, final int opcode, final int immediate) {
|
||||
out.writeBytes(ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN).putShort((short) opcode).array());
|
||||
out.write(immediate & 0xFF);
|
||||
}
|
||||
|
||||
private static BytecodeModule.SourceSpan spanOrUnknown(final BytecodeModule.SourceSpan span) {
|
||||
if (span == null) {
|
||||
return new BytecodeModule.SourceSpan(-1, 0, 0);
|
||||
@@ -210,8 +305,26 @@ public class BytecodeEmitter {
|
||||
HALT,
|
||||
RET,
|
||||
PUSH_CONST,
|
||||
POP,
|
||||
GET_LOCAL,
|
||||
SET_LOCAL,
|
||||
PUSH_BOOL,
|
||||
PUSH_I32,
|
||||
ADD,
|
||||
SUB,
|
||||
MUL,
|
||||
DIV,
|
||||
MOD,
|
||||
EQ,
|
||||
NEQ,
|
||||
LT,
|
||||
LTE,
|
||||
GT,
|
||||
GTE,
|
||||
AND,
|
||||
OR,
|
||||
NOT,
|
||||
NEG,
|
||||
CALL_FUNC,
|
||||
JMP,
|
||||
JMP_IF_TRUE,
|
||||
@@ -276,14 +389,86 @@ public class BytecodeEmitter {
|
||||
return new Operation(OperationKind.PUSH_CONST, constPoolIndex, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation pop(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.POP, 0, 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 setLocal(final int slot, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.SET_LOCAL, slot, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation pushBool(final boolean value, final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.PUSH_BOOL, value ? 1 : 0, 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 add(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.ADD, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation sub(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.SUB, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation mul(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.MUL, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation div(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.DIV, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation mod(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.MOD, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation eq(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.EQ, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation neq(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.NEQ, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation lt(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.LT, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation lte(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.LTE, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation gt(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.GT, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation gte(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.GTE, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation and(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.AND, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation or(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.OR, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation not(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.NOT, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation neg(final BytecodeModule.SourceSpan span) {
|
||||
return new Operation(OperationKind.NEG, 0, null, null, null, span);
|
||||
}
|
||||
|
||||
public static Operation hostcall(
|
||||
final BytecodeModule.SyscallDecl decl,
|
||||
final Integer expectedArgSlots,
|
||||
|
||||
+16
@@ -9,11 +9,27 @@ final class BytecodeOpcodeLayout {
|
||||
Map.entry(0x03, 6),
|
||||
Map.entry(0x04, 6),
|
||||
Map.entry(0x10, 6),
|
||||
Map.entry(0x11, 2),
|
||||
Map.entry(0x14, 10),
|
||||
Map.entry(0x15, 10),
|
||||
Map.entry(0x16, 3),
|
||||
Map.entry(0x17, 6),
|
||||
Map.entry(0x18, 6),
|
||||
Map.entry(0x20, 2),
|
||||
Map.entry(0x21, 2),
|
||||
Map.entry(0x22, 2),
|
||||
Map.entry(0x23, 2),
|
||||
Map.entry(0x24, 2),
|
||||
Map.entry(0x30, 2),
|
||||
Map.entry(0x31, 2),
|
||||
Map.entry(0x32, 2),
|
||||
Map.entry(0x33, 2),
|
||||
Map.entry(0x34, 2),
|
||||
Map.entry(0x35, 2),
|
||||
Map.entry(0x36, 2),
|
||||
Map.entry(0x3C, 2),
|
||||
Map.entry(0x3D, 2),
|
||||
Map.entry(0x3E, 2),
|
||||
Map.entry(0x40, 6),
|
||||
Map.entry(0x41, 6),
|
||||
Map.entry(0x42, 6),
|
||||
|
||||
+18
@@ -13,13 +13,31 @@ 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 POP = new IRVMOp("POP", 0x11, 0, 1, 0, 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);
|
||||
public static final IRVMOp JMP_IF_FALSE = new IRVMOp("JMP_IF_FALSE", 0x03, 4, 1, 0, true, false, false);
|
||||
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_BOOL = new IRVMOp("PUSH_BOOL", 0x16, 1, 0, 1, false, false, false);
|
||||
public static final IRVMOp PUSH_I32 = new IRVMOp("PUSH_I32", 0x17, 4, 0, 1, false, false, false);
|
||||
public static final IRVMOp ADD = new IRVMOp("ADD", 0x20, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp SUB = new IRVMOp("SUB", 0x21, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp MUL = new IRVMOp("MUL", 0x22, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp DIV = new IRVMOp("DIV", 0x23, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp MOD = new IRVMOp("MOD", 0x24, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp EQ = new IRVMOp("EQ", 0x30, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp NEQ = new IRVMOp("NEQ", 0x31, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp LT = new IRVMOp("LT", 0x32, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp GT = new IRVMOp("GT", 0x33, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp AND = new IRVMOp("AND", 0x34, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp OR = new IRVMOp("OR", 0x35, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp NOT = new IRVMOp("NOT", 0x36, 0, 1, 1, false, false, false);
|
||||
public static final IRVMOp LTE = new IRVMOp("LTE", 0x3C, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp GTE = new IRVMOp("GTE", 0x3D, 0, 2, 1, false, false, false);
|
||||
public static final IRVMOp NEG = new IRVMOp("NEG", 0x3E, 0, 1, 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 SET_LOCAL = new IRVMOp("SET_LOCAL", 0x43, 4, 1, 0, false, false, false);
|
||||
public static final IRVMOp INTERNAL_EXT = new IRVMOp("IRVM_EXT_NOP", 0xFFFF, 0, 0, 0, false, false, true);
|
||||
}
|
||||
|
||||
+36
@@ -12,25 +12,61 @@ public final class IRVMProfileFeatureGate {
|
||||
IRVMOp.HALT,
|
||||
IRVMOp.RET,
|
||||
IRVMOp.PUSH_CONST,
|
||||
IRVMOp.POP,
|
||||
IRVMOp.GET_LOCAL,
|
||||
IRVMOp.SET_LOCAL,
|
||||
IRVMOp.CALL,
|
||||
IRVMOp.JMP,
|
||||
IRVMOp.JMP_IF_TRUE,
|
||||
IRVMOp.JMP_IF_FALSE,
|
||||
IRVMOp.HOSTCALL,
|
||||
IRVMOp.INTRINSIC,
|
||||
IRVMOp.PUSH_BOOL,
|
||||
IRVMOp.ADD,
|
||||
IRVMOp.SUB,
|
||||
IRVMOp.MUL,
|
||||
IRVMOp.DIV,
|
||||
IRVMOp.MOD,
|
||||
IRVMOp.EQ,
|
||||
IRVMOp.NEQ,
|
||||
IRVMOp.LT,
|
||||
IRVMOp.LTE,
|
||||
IRVMOp.GT,
|
||||
IRVMOp.GTE,
|
||||
IRVMOp.AND,
|
||||
IRVMOp.OR,
|
||||
IRVMOp.NOT,
|
||||
IRVMOp.NEG,
|
||||
IRVMOp.PUSH_I32),
|
||||
"experimental-v1", Set.of(
|
||||
IRVMOp.HALT,
|
||||
IRVMOp.RET,
|
||||
IRVMOp.PUSH_CONST,
|
||||
IRVMOp.POP,
|
||||
IRVMOp.GET_LOCAL,
|
||||
IRVMOp.SET_LOCAL,
|
||||
IRVMOp.CALL,
|
||||
IRVMOp.JMP,
|
||||
IRVMOp.JMP_IF_TRUE,
|
||||
IRVMOp.JMP_IF_FALSE,
|
||||
IRVMOp.HOSTCALL,
|
||||
IRVMOp.INTRINSIC,
|
||||
IRVMOp.PUSH_BOOL,
|
||||
IRVMOp.ADD,
|
||||
IRVMOp.SUB,
|
||||
IRVMOp.MUL,
|
||||
IRVMOp.DIV,
|
||||
IRVMOp.MOD,
|
||||
IRVMOp.EQ,
|
||||
IRVMOp.NEQ,
|
||||
IRVMOp.LT,
|
||||
IRVMOp.LTE,
|
||||
IRVMOp.GT,
|
||||
IRVMOp.GTE,
|
||||
IRVMOp.AND,
|
||||
IRVMOp.OR,
|
||||
IRVMOp.NOT,
|
||||
IRVMOp.NEG,
|
||||
IRVMOp.PUSH_I32,
|
||||
IRVMOp.INTERNAL_EXT)));
|
||||
}
|
||||
|
||||
+54
@@ -93,8 +93,26 @@ public record IRVMProgram(
|
||||
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 0x11 -> operation.kind() == BytecodeEmitter.OperationKind.POP;
|
||||
case 0x16 -> operation.kind() == BytecodeEmitter.OperationKind.PUSH_BOOL;
|
||||
case 0x42 -> operation.kind() == BytecodeEmitter.OperationKind.GET_LOCAL && operation.immediate() == immediate;
|
||||
case 0x43 -> operation.kind() == BytecodeEmitter.OperationKind.SET_LOCAL && operation.immediate() == immediate;
|
||||
case 0x17 -> operation.kind() == BytecodeEmitter.OperationKind.PUSH_I32 && operation.immediate() == immediate;
|
||||
case 0x20 -> operation.kind() == BytecodeEmitter.OperationKind.ADD;
|
||||
case 0x21 -> operation.kind() == BytecodeEmitter.OperationKind.SUB;
|
||||
case 0x22 -> operation.kind() == BytecodeEmitter.OperationKind.MUL;
|
||||
case 0x23 -> operation.kind() == BytecodeEmitter.OperationKind.DIV;
|
||||
case 0x24 -> operation.kind() == BytecodeEmitter.OperationKind.MOD;
|
||||
case 0x30 -> operation.kind() == BytecodeEmitter.OperationKind.EQ;
|
||||
case 0x31 -> operation.kind() == BytecodeEmitter.OperationKind.NEQ;
|
||||
case 0x32 -> operation.kind() == BytecodeEmitter.OperationKind.LT;
|
||||
case 0x33 -> operation.kind() == BytecodeEmitter.OperationKind.GT;
|
||||
case 0x34 -> operation.kind() == BytecodeEmitter.OperationKind.AND;
|
||||
case 0x35 -> operation.kind() == BytecodeEmitter.OperationKind.OR;
|
||||
case 0x36 -> operation.kind() == BytecodeEmitter.OperationKind.NOT;
|
||||
case 0x3C -> operation.kind() == BytecodeEmitter.OperationKind.LTE;
|
||||
case 0x3D -> operation.kind() == BytecodeEmitter.OperationKind.GTE;
|
||||
case 0x3E -> operation.kind() == BytecodeEmitter.OperationKind.NEG;
|
||||
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;
|
||||
@@ -157,8 +175,26 @@ public record IRVMProgram(
|
||||
case 0x01 -> BytecodeEmitter.Operation.halt();
|
||||
case 0x51 -> BytecodeEmitter.Operation.ret();
|
||||
case 0x10 -> BytecodeEmitter.Operation.pushConst(immediate, null);
|
||||
case 0x11 -> BytecodeEmitter.Operation.pop(null);
|
||||
case 0x16 -> BytecodeEmitter.Operation.pushBool(immediate != 0, null);
|
||||
case 0x42 -> BytecodeEmitter.Operation.getLocal(immediate, null);
|
||||
case 0x43 -> BytecodeEmitter.Operation.setLocal(immediate, null);
|
||||
case 0x17 -> BytecodeEmitter.Operation.pushI32(immediate, null);
|
||||
case 0x20 -> BytecodeEmitter.Operation.add(null);
|
||||
case 0x21 -> BytecodeEmitter.Operation.sub(null);
|
||||
case 0x22 -> BytecodeEmitter.Operation.mul(null);
|
||||
case 0x23 -> BytecodeEmitter.Operation.div(null);
|
||||
case 0x24 -> BytecodeEmitter.Operation.mod(null);
|
||||
case 0x30 -> BytecodeEmitter.Operation.eq(null);
|
||||
case 0x31 -> BytecodeEmitter.Operation.neq(null);
|
||||
case 0x32 -> BytecodeEmitter.Operation.lt(null);
|
||||
case 0x33 -> BytecodeEmitter.Operation.gt(null);
|
||||
case 0x34 -> BytecodeEmitter.Operation.and(null);
|
||||
case 0x35 -> BytecodeEmitter.Operation.or(null);
|
||||
case 0x36 -> BytecodeEmitter.Operation.not(null);
|
||||
case 0x3C -> BytecodeEmitter.Operation.lte(null);
|
||||
case 0x3D -> BytecodeEmitter.Operation.gte(null);
|
||||
case 0x3E -> BytecodeEmitter.Operation.neg(null);
|
||||
case 0x50 -> BytecodeEmitter.Operation.callFunc(immediate);
|
||||
case 0x02 -> BytecodeEmitter.Operation.jmp(immediate, null);
|
||||
case 0x04 -> BytecodeEmitter.Operation.jmpIfTrue(immediate, null);
|
||||
@@ -211,8 +247,26 @@ public record IRVMProgram(
|
||||
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 POP -> new IRVMInstruction(IRVMOp.POP, null);
|
||||
case PUSH_BOOL -> new IRVMInstruction(IRVMOp.PUSH_BOOL, operation.immediate());
|
||||
case GET_LOCAL -> new IRVMInstruction(IRVMOp.GET_LOCAL, operation.immediate());
|
||||
case SET_LOCAL -> new IRVMInstruction(IRVMOp.SET_LOCAL, operation.immediate());
|
||||
case PUSH_I32 -> new IRVMInstruction(IRVMOp.PUSH_I32, operation.immediate());
|
||||
case ADD -> new IRVMInstruction(IRVMOp.ADD, null);
|
||||
case SUB -> new IRVMInstruction(IRVMOp.SUB, null);
|
||||
case MUL -> new IRVMInstruction(IRVMOp.MUL, null);
|
||||
case DIV -> new IRVMInstruction(IRVMOp.DIV, null);
|
||||
case MOD -> new IRVMInstruction(IRVMOp.MOD, null);
|
||||
case EQ -> new IRVMInstruction(IRVMOp.EQ, null);
|
||||
case NEQ -> new IRVMInstruction(IRVMOp.NEQ, null);
|
||||
case LT -> new IRVMInstruction(IRVMOp.LT, null);
|
||||
case LTE -> new IRVMInstruction(IRVMOp.LTE, null);
|
||||
case GT -> new IRVMInstruction(IRVMOp.GT, null);
|
||||
case GTE -> new IRVMInstruction(IRVMOp.GTE, null);
|
||||
case AND -> new IRVMInstruction(IRVMOp.AND, null);
|
||||
case OR -> new IRVMInstruction(IRVMOp.OR, null);
|
||||
case NOT -> new IRVMInstruction(IRVMOp.NOT, null);
|
||||
case NEG -> new IRVMInstruction(IRVMOp.NEG, null);
|
||||
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());
|
||||
|
||||
+111
@@ -90,6 +90,24 @@ public class LowerToIRVMService {
|
||||
operations.add(BytecodeEmitter.Operation.pushConst(constPoolIndex, sourceSpan));
|
||||
functionPc += IRVMOp.PUSH_CONST.immediateSize() + 2;
|
||||
}
|
||||
case PUSH_BOOL -> {
|
||||
final var raw = instr.label() == null ? "" : instr.label().trim().toLowerCase();
|
||||
final Integer value = switch (raw) {
|
||||
case "1", "true" -> 1;
|
||||
case "0", "false" -> 0;
|
||||
default -> null;
|
||||
};
|
||||
if (value == null) {
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_INVALID_INTRINSIC_ID,
|
||||
"invalid PUSH_BOOL literal payload: " + instr.label());
|
||||
}
|
||||
instructions.add(new IRVMInstruction(IRVMOp.PUSH_BOOL, value));
|
||||
operations.add(BytecodeEmitter.Operation.pushBool(value != 0, sourceSpan));
|
||||
functionPc += IRVMOp.PUSH_BOOL.immediateSize() + 2;
|
||||
}
|
||||
case GET_LOCAL -> {
|
||||
final var slot = instr.expectedArgSlots();
|
||||
if (slot == null) {
|
||||
@@ -103,6 +121,99 @@ public class LowerToIRVMService {
|
||||
operations.add(BytecodeEmitter.Operation.getLocal(slot, sourceSpan));
|
||||
functionPc += IRVMOp.GET_LOCAL.immediateSize() + 2;
|
||||
}
|
||||
case SET_LOCAL -> {
|
||||
final var slot = instr.expectedArgSlots();
|
||||
if (slot == null) {
|
||||
throw loweringError(
|
||||
fn,
|
||||
instr,
|
||||
IRVMLoweringErrorCode.LOWER_IRVM_MISSING_CALLEE,
|
||||
"missing local slot for SET_LOCAL");
|
||||
}
|
||||
instructions.add(new IRVMInstruction(IRVMOp.SET_LOCAL, slot));
|
||||
operations.add(BytecodeEmitter.Operation.setLocal(slot, sourceSpan));
|
||||
functionPc += IRVMOp.SET_LOCAL.immediateSize() + 2;
|
||||
}
|
||||
case POP -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.POP, null));
|
||||
operations.add(BytecodeEmitter.Operation.pop(sourceSpan));
|
||||
functionPc += IRVMOp.POP.immediateSize() + 2;
|
||||
}
|
||||
case ADD -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.ADD, null));
|
||||
operations.add(BytecodeEmitter.Operation.add(sourceSpan));
|
||||
functionPc += IRVMOp.ADD.immediateSize() + 2;
|
||||
}
|
||||
case SUB -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.SUB, null));
|
||||
operations.add(BytecodeEmitter.Operation.sub(sourceSpan));
|
||||
functionPc += IRVMOp.SUB.immediateSize() + 2;
|
||||
}
|
||||
case MUL -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.MUL, null));
|
||||
operations.add(BytecodeEmitter.Operation.mul(sourceSpan));
|
||||
functionPc += IRVMOp.MUL.immediateSize() + 2;
|
||||
}
|
||||
case DIV -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.DIV, null));
|
||||
operations.add(BytecodeEmitter.Operation.div(sourceSpan));
|
||||
functionPc += IRVMOp.DIV.immediateSize() + 2;
|
||||
}
|
||||
case MOD -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.MOD, null));
|
||||
operations.add(BytecodeEmitter.Operation.mod(sourceSpan));
|
||||
functionPc += IRVMOp.MOD.immediateSize() + 2;
|
||||
}
|
||||
case EQ -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.EQ, null));
|
||||
operations.add(BytecodeEmitter.Operation.eq(sourceSpan));
|
||||
functionPc += IRVMOp.EQ.immediateSize() + 2;
|
||||
}
|
||||
case NEQ -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.NEQ, null));
|
||||
operations.add(BytecodeEmitter.Operation.neq(sourceSpan));
|
||||
functionPc += IRVMOp.NEQ.immediateSize() + 2;
|
||||
}
|
||||
case LT -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.LT, null));
|
||||
operations.add(BytecodeEmitter.Operation.lt(sourceSpan));
|
||||
functionPc += IRVMOp.LT.immediateSize() + 2;
|
||||
}
|
||||
case LTE -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.LTE, null));
|
||||
operations.add(BytecodeEmitter.Operation.lte(sourceSpan));
|
||||
functionPc += IRVMOp.LTE.immediateSize() + 2;
|
||||
}
|
||||
case GT -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.GT, null));
|
||||
operations.add(BytecodeEmitter.Operation.gt(sourceSpan));
|
||||
functionPc += IRVMOp.GT.immediateSize() + 2;
|
||||
}
|
||||
case GTE -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.GTE, null));
|
||||
operations.add(BytecodeEmitter.Operation.gte(sourceSpan));
|
||||
functionPc += IRVMOp.GTE.immediateSize() + 2;
|
||||
}
|
||||
case AND -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.AND, null));
|
||||
operations.add(BytecodeEmitter.Operation.and(sourceSpan));
|
||||
functionPc += IRVMOp.AND.immediateSize() + 2;
|
||||
}
|
||||
case OR -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.OR, null));
|
||||
operations.add(BytecodeEmitter.Operation.or(sourceSpan));
|
||||
functionPc += IRVMOp.OR.immediateSize() + 2;
|
||||
}
|
||||
case NOT -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.NOT, null));
|
||||
operations.add(BytecodeEmitter.Operation.not(sourceSpan));
|
||||
functionPc += IRVMOp.NOT.immediateSize() + 2;
|
||||
}
|
||||
case NEG -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.NEG, null));
|
||||
operations.add(BytecodeEmitter.Operation.neg(sourceSpan));
|
||||
functionPc += IRVMOp.NEG.immediateSize() + 2;
|
||||
}
|
||||
case HALT -> {
|
||||
instructions.add(new IRVMInstruction(IRVMOp.HALT, null));
|
||||
operations.add(BytecodeEmitter.Operation.halt(sourceSpan));
|
||||
|
||||
Reference in New Issue
Block a user