-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.c
126 lines (102 loc) · 3.43 KB
/
stack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Zerp: a Z-machine interpreter
stack.c : z-machine stacks - value and call
*/
#include <stdio.h>
#include <string.h>
#ifdef TARGET_OS_MAC
#include <GlkClient/glk.h>
#else
#include "glk.h"
#endif /* TARGET_OS_MAC */
#include "zerp.h"
#include "opcodes.h"
#include "stack.h"
int stack_push(zword_t value) {
if (zSP >= zStackTop) {
fatal_error("Value stack overflow!\n");
}
*(++zSP) = value;
LOG(ZDEBUG, "\nStack: push - size now %li, frame usage %li (pushed #%x)", 2051 - (zSP - zStack), (zSP - zFP->sp) - 1, value);
return value;
}
zword_t stack_pop() {
zword_t value;
if (zSP < zFP->sp) {
fatal_error("Value stack underflow!\n");
}
value = *(zSP--);
LOG(ZDEBUG, "\nStack: pop - size now %li, frame usage %li (value #%x)", 2051 - (zSP - zStack), (zSP - zFP->sp) - 1, value);
return value;
}
/*
In the seven opcodes that take indirect variable references (inc, dec,
inc_chk, dec_chk, load, store, pull), an indirect reference to the stack
pointer does not push or pull the top item of the stack - it is read
or written in place.
We will call this peeking and poking the stack (because 80s retro is in, dude.)
*/
zword_t stack_peek() {
LOG(ZDEBUG, "\nStack: pop - size now %li, frame usage %li (pushed #%x)", 2051 - (zSP - zStack), (zSP - zFP->sp) - 1, *(zSP));
return *(zSP);
}
zword_t stack_poke(zword_t value) {
LOG(ZDEBUG, "\nStack: push - size now %li, frame usage %li (value #%x)", 2051 - (zSP - zStack), (zSP - zFP->sp) - 1, value);
return *(zSP) = value;
}
zstack_frame_t * call_zroutine(packed_addr_t address, zoperand_t *operands, zbyte_t ret_store, int keep_return){
zbyte_t local_count;
zstack_frame_t *newFrame;
int i = 0;
packed_addr_t routine;
routine = address;
if (address == 0) {
variable_set(ret_store, 0);
return;
}
newFrame = zFP +1;
if (newFrame > zCallStackTop)
fatal_error("Call stack overflow");
/* stack frames will be reused, so zero the new one out */
memset(newFrame, 0, sizeof(zstack_frame_t));
newFrame->pc = zPC;
newFrame->sp = zSP;
newFrame->ret_store = ret_store;
newFrame->ret_keep = keep_return;
local_count = get_byte(address++);
if (zGameVersion < Z_VERSION_5) {
/* defaults for locals stored after local count in V3/V4 */
for (i = 0; i < local_count; i++) {
newFrame->locals[i] = get_word(address);
address += 2;
}
}
i = 0;
while (operands->type != NONE) {
newFrame->args |= (1 << i);
newFrame->locals[i++] = operands->type == VARIABLE ? variable_get(operands->bytes) : operands->bytes;
operands++;
}
LOG(ZDEBUG, "\nCALL $%x -> V%03i", routine, ret_store)
zPC = address;
zSP++;
return zFP++;
}
zstack_frame_t *return_zroutine(zword_t ret_value) {
zbyte_t ret_store;
int ret_keep;
if ((zFP - 1) < zCallStack)
fatal_error("Call stack underflow");
if (zFP->ret_keep)
LOG(ZDEBUG, "\nReturned %i into V%x (%05x)", ret_value, zFP->ret_store, zFP->pc)
LOG(ZDEBUG, "\nStack: returned, discarded %li outstanding items (stack top now #%hn, size %li, frame usage %li)",
zSP - zFP->sp, (zFP - 1)->sp, (zSP - (zFP - 1)->sp) - (zSP - zFP->sp),(zFP - 1) - zCallStack)
zSP = zFP->sp;
zPC = zFP->pc;
ret_store = zFP->ret_store;
ret_keep = zFP->ret_keep;
zFP--;
if (ret_keep)
variable_set(ret_store, ret_value);
return zFP;
}