From 05cfb9bf2461785ec621b490747f48b96344017f Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 17 Aug 2024 12:35:43 +0100 Subject: sin, cos, more checks --- com.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'com.c') diff --git a/com.c b/com.c index f610bd7..432ebb4 100644 --- a/com.c +++ b/com.c @@ -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)"); } -- cgit v1.2.3