/* * Copyright (c) 2024 Scott Duensing, scott@kangaroopunch.com * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "oc_var_op.h" #include "state.h" #include "memory.h" #include "messages.h" #include "portme.h" #include "interpreter.h" #include "story.h" void opcodes_call(void) { uint8_t args = __state.operandCount; uint8_t storeId = PEEK(__state.pc++); uint32_t routine; uint8_t numLocals; int8_t i; uint16_t src; uint8_t dst; if ((args == 0) && (__state.operands[0] == 0)) { // Legal no-op; store 0 to return value and bounce. POKEW(interpreterVariableAddress(storeId, 1), 0); } else { routine = memoryUnpackAddress(__state.operands[0], MEMORY_ROUTINE); numLocals = PEEK(routine++); if (numLocals > 15) portDie(MSG_OP_VAR_TOO_MANY_LOCALS); if (__state.sp + 5 + numLocals >= sizeof(__state.stack)) portDie(MSG_OP_VAR_STACK_OVERFLOW); // Save where we should store the call's result. __state.stack[__state.sp++] = storeId; // Next instruction to run upon return. __state.stack[__state.sp++] = __state.pc & 0xffff; __state.stack[__state.sp++] = ((__state.pc >> 16) & 0xffff); // Current base pointer. __state.stack[__state.sp++] = __state.bp; // Number of locals. __state.stack[__state.sp++] = numLocals; // New base pointer. __state.bp = __state.sp; if (storyVersion() <= 4) { for (i=0; i numLocals) args = numLocals; // Copy arguments into local variables on stack. src = 1; dst = __state.bp; for (i=0; i