diff options
Diffstat (limited to 'com.c')
-rw-r--r-- | com.c | 21 |
1 files changed, 20 insertions, 1 deletions
@@ -9,6 +9,7 @@ #include "util.h" #include "lib.h" #include "read.h" +#include "dis.h" #define BYTECODE(C) (C->ch->bc) @@ -363,7 +364,7 @@ static void for_form(Compiler *C, ObjArr *a, Op _, int flags) { cpl_constop(C, OP_LOADK, VAL_NUM(1)); cpl_op(C, OP_ADD); cpl_op(C, OP_SETLOCAL); - cpl_op(C, islot); + cpl_byte(C, islot); cpl_op(C, OP_DROP); cpl_op(C, OP_REDO); size_t ph_A = placeholder(C); @@ -593,6 +594,12 @@ static BuiltinForm *find_builtinform(char *name) { } static void cpl_expr(Compiler *C, Val v, int flags) { + int stack_cur_a = C->stack_cur; + int nlocals_a = 0; + if (C->scope) { + nlocals_a = C->scope->nlocals; + } + switch (val_type(v)) { case TY_NUM: case TY_NIL: case TY_BOOL: cpl_constop(C, OP_LOADK, v); @@ -636,6 +643,18 @@ static void cpl_expr(Compiler *C, Val v, int flags) { } break; } + + int stack_cur_b = C->stack_cur; + int nlocals_b = C->stack_cur; + if (C->scope) { + nlocals_b = C->scope->nlocals; + } + + // every badthing expression returns exactly one value, + // and might declare some locals as well, which also live on the stack + // so (returned values) = (stack change) - (new locals) = 1 + CHECK( (stack_cur_b - stack_cur_a) - (nlocals_b - nlocals_a) == 1, + "stack corruption (compiler bug)"); } |