feat: implement capturable locals realloc

This commit is contained in:
TopchetoEU 2024-09-05 17:14:59 +03:00
parent 4bfc062aaf
commit 07411f62c8
Signed by: topchetoeu
GPG Key ID: 6531B8583E5F6ED4
2 changed files with 19 additions and 6 deletions

View File

@ -64,7 +64,8 @@ public class Instruction {
GLOB_DEF(0x62), GLOB_DEF(0x62),
STACK_ALLOC(0x70), STACK_ALLOC(0x70),
STACK_FREE(0x71); STACK_REALLOC(0x71),
STACK_FREE(0x72);
private static final HashMap<Integer, Type> types = new HashMap<>(); private static final HashMap<Integer, Type> types = new HashMap<>();
public final int numeric; public final int numeric;
@ -456,11 +457,14 @@ public class Instruction {
return new Instruction(Type.OPERATION, op); return new Instruction(Type.OPERATION, op);
} }
public static Instruction stackAlloc(int i) { public static Instruction stackAlloc(int n) {
return new Instruction(Type.STACK_ALLOC, i); return new Instruction(Type.STACK_ALLOC, n);
} }
public static Instruction stackFree(int i) { public static Instruction stackRealloc(int n) {
return new Instruction(Type.STACK_FREE, i); return new Instruction(Type.STACK_REALLOC, n);
}
public static Instruction stackFree(int n) {
return new Instruction(Type.STACK_FREE, n);
} }
@Override public String toString() { @Override public String toString() {

View File

@ -505,7 +505,15 @@ public class InstructionRunner {
int n = instr.get(0); int n = instr.get(0);
for (var i = 0; i < n; i++) frame.locals.add(new Value[] { Value.UNDEFINED }); for (var i = 0; i < n; i++) frame.locals.add(new Value[] { Value.UNDEFINED });
frame.codePtr++;
return null;
}
private static Value execStackRealloc(Environment env, Instruction instr, Frame frame) {
int n = instr.get(0);
for (var i = frame.locals.size() - n; i < frame.locals.size(); i++) frame.locals.set(i, new Value[] { frame.locals.get(i)[0] });
frame.codePtr++; frame.codePtr++;
return null; return null;
} }
@ -576,6 +584,7 @@ public class InstructionRunner {
case GLOB_SET: return exexGlobSet(env, instr, frame); case GLOB_SET: return exexGlobSet(env, instr, frame);
case STACK_ALLOC: return execStackAlloc(env, instr, frame); case STACK_ALLOC: return execStackAlloc(env, instr, frame);
case STACK_REALLOC: return execStackRealloc(env, instr, frame);
case STACK_FREE: return execStackFree(env, instr, frame); case STACK_FREE: return execStackFree(env, instr, frame);
default: throw EngineException.ofSyntax("Invalid instruction " + instr.type.name() + "."); default: throw EngineException.ofSyntax("Invalid instruction " + instr.type.name() + ".");