muddle/src/oc_var_op.c
2024-01-28 19:26:13 -06:00

108 lines
3.3 KiB
C

/*
* 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; i++, routine += 2) __state.stack[__state.sp++] = PEEKW(routine);
} else {
for (i=0; i<numLocals; i++) __state.stack[__state.sp++] = 0;
}
// Remove return address from argument count.
args--;
// It's legal to have more arguments than locals. Toss extras.
if (args > numLocals) args = numLocals;
// Copy arguments into local variables on stack.
src = 1;
dst = __state.bp;
for (i=0; i<args; i++) {
__state.stack[dst++] = __state.operands[src++];
}
// Move PC to called routine.
__state.pc = routine;
}
}
void opcodes_put_prop(void) {
uint8_t size = 0;
uint8_t ptr = interpreterGetObjectProperty(__state.operands[0], __state.operands[1], &size);
if (ptr == 0) portDie(MSG_OP_VAR_MISSING_PROPERTY);
if (size == 1) {
POKE(ptr, (__state.operands[2] & 0xff));
} else {
POKEW(ptr, __state.operands[2]);
}
}
void opcodes_storew(void) {
POKEW(__state.operands[0] + (__state.operands[1] * 2), __state.operands[2]);
}