Debian Codegraph @

terps/glulxe/funcs.c

raw symbols SCIP
1 /* funcs.c: Glulxe function-handling functions.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8
9 /* enter_function():
10 This writes a new call frame onto the stack, at stackptr. It leaves
11 frameptr pointing to the frame (ie, the original stackptr value.)
12 argc and argv are an array of arguments. Note that if argc is zero,
13 argv may be NULL.
14 */
15 void enter_function(glui32 funcaddr, glui32 argc, glui32 *argv)
16 {
17 int ix, jx;
18 acceleration_func accelfunc;
19 int locallen;
20 int functype;
21 glui32 modeaddr, opaddr, val;
22 int loctype, locnum;
23 glui32 addr = funcaddr;
24
25 accelfunc = accel_get_func(addr);
26 if (accelfunc) {
27 profile_in(addr, stackptr, TRUE);
28 val = accelfunc(argc, argv);
29 profile_out(stackptr);
30 pop_callstub(val);
31 return;
32 }
33
34 profile_in(addr, stackptr, FALSE);
35
36 /* Check the Glulx type identifier byte. */
37 functype = Mem1(addr);
38 if (functype != 0xC0 && functype != 0xC1) {
39 if (functype >= 0xC0 && functype <= 0xDF)
40 fatal_error_i("Call to unknown type of function.", addr);
41 else
42 fatal_error_i("Call to non-function.", addr);
43 }
44 addr++;
45
46 /* Bump the frameptr to the top. */
47 frameptr = stackptr;
48
49 /* Go through the function's locals-format list, copying it to the
50 call frame. At the same time, we work out how much space the locals
51 will actually take up. (Including padding.) */
52 ix = 0;
53 locallen = 0;
54 while (1) {
55 /* Grab two bytes from the locals-format list. These are
56 unsigned (0..255 range). */
57 loctype = Mem1(addr);
58 addr++;
59 locnum = Mem1(addr);
60 addr++;
61
62 /* Copy them into the call frame. */
63 StkW1(frameptr+8+2*ix, loctype);
64 StkW1(frameptr+8+2*ix+1, locnum);
65 ix++;
66
67 /* If the type is zero, we're done, except possibly for two more
68 zero bytes in the call frame (to ensure 4-byte alignment.) */
69 if (loctype == 0) {
70 /* Make sure ix is even. */
71 if (ix & 1) {
72 StkW1(frameptr+8+2*ix, 0);
73 StkW1(frameptr+8+2*ix+1, 0);
74 ix++;
75 }
76 break;
77 }
78
79 /* Pad to 4-byte or 2-byte alignment if these locals are 4 or 2
80 bytes long. */
81 if (loctype == 4) {
82 while (locallen & 3)
83 locallen++;
84 }
85 else if (loctype == 2) {
86 while (locallen & 1)
87 locallen++;
88 }
89 else if (loctype == 1) {
90 /* no padding */
91 }
92 else {
93 fatal_error("Illegal local type in locals-format list.");
94 }
95
96 /* Add the length of the locals themselves. */
97 locallen += (loctype * locnum);
98 }
99
100 /* Pad the locals to 4-byte alignment. */
101 while (locallen & 3)
102 locallen++;
103
104 /* We now know how long the locals-frame and locals segments are. */
105 localsbase = frameptr+8+2*ix;
106 valstackbase = localsbase+locallen;
107
108 /* Test for stack overflow. */
109 /* This really isn't good enough; if the format list overflowed the
110 stack, we've already written outside the stack array. */
111 if (valstackbase >= stacksize)
112 fatal_error("Stack overflow in function call.");
113
114 /* Fill in the beginning of the stack frame. */
115 StkW4(frameptr+4, 8+2*ix);
116 StkW4(frameptr, 8+2*ix+locallen);
117
118 /* Set the stackptr and PC. */
119 stackptr = valstackbase;
120 pc = addr;
121
122 /* Zero out all the locals. */
123 for (jx=0; jx<locallen; jx++)
124 StkW1(localsbase+jx, 0);
125
126 if (functype == 0xC0) {
127 /* Push the function arguments on the stack. The locals have already
128 been zeroed. */
129 if (stackptr+4*(argc+1) >= stacksize)
130 fatal_error("Stack overflow in function arguments.");
131 for (ix=0; ix<argc; ix++) {
132 val = argv[(argc-1)-ix];
133 StkW4(stackptr, val);
134 stackptr += 4;
135 }
136 StkW4(stackptr, argc);
137 stackptr += 4;
138 }
139 else {
140 /* Copy in function arguments. This is a bit gross, since we have to
141 follow the locals format. If there are fewer arguments than locals,
142 that's fine -- we've already zeroed out this space. If there are
143 more arguments than locals, the extras are silently dropped. */
144 modeaddr = frameptr+8;
145 opaddr = localsbase;
146 ix = 0;
147 while (ix < argc) {
148 loctype = Stk1(modeaddr);
149 modeaddr++;
150 locnum = Stk1(modeaddr);
151 modeaddr++;
152 if (loctype == 0)
153 break;
154 if (loctype == 4) {
155 while (opaddr & 3)
156 opaddr++;
157 while (ix < argc && locnum) {
158 val = argv[ix];
159 StkW4(opaddr, val);
160 opaddr += 4;
161 ix++;
162 locnum--;
163 }
164 }
165 else if (loctype == 2) {
166 while (opaddr & 1)
167 opaddr++;
168 while (ix < argc && locnum) {
169 val = argv[ix] & 0xFFFF;
170 StkW2(opaddr, val);
171 opaddr += 2;
172 ix++;
173 locnum--;
174 }
175 }
176 else if (loctype == 1) {
177 while (ix < argc && locnum) {
178 val = argv[ix] & 0xFF;
179 StkW1(opaddr, val);
180 opaddr += 1;
181 ix++;
182 locnum--;
183 }
184 }
185 }
186 }
187
188 /* If the debugger is compiled in, check for a breakpoint on this
189 function. (Checking the function address, not the starting PC.) */
190 debugger_check_func_breakpoint(funcaddr);
191 }
192
193 /* leave_function():
194 Pop the current call frame off the stack. This is very simple.
195 */
196 void leave_function()
197 {
198 profile_out(stackptr);
199 stackptr = frameptr;
200 }
201
202 /* push_callstub():
203 Push the magic four values on the stack: result destination,
204 PC, and frameptr.
205 */
206 void push_callstub(glui32 desttype, glui32 destaddr)
207 {
208 if (stackptr+16 > stacksize)
209 fatal_error("Stack overflow in callstub.");
210 StkW4(stackptr+0, desttype);
211 StkW4(stackptr+4, destaddr);
212 StkW4(stackptr+8, pc);
213 StkW4(stackptr+12, frameptr);
214 stackptr += 16;
215 }
216
217 /* pop_callstub():
218 Remove the magic four values from the stack, and use them. The
219 returnvalue, whatever it is, is put at the result destination;
220 the PC and frameptr registers are set.
221 */
222 void pop_callstub(glui32 returnvalue)
223 {
224 glui32 desttype, destaddr;
225 glui32 newpc, newframeptr;
226
227 if (stackptr < 16)
228 fatal_error("Stack underflow in callstub.");
229 stackptr -= 16;
230
231 newframeptr = Stk4(stackptr+12);
232 newpc = Stk4(stackptr+8);
233 destaddr = Stk4(stackptr+4);
234 desttype = Stk4(stackptr+0);
235
236 pc = newpc;
237 frameptr = newframeptr;
238
239 /* Recompute valstackbase and localsbase */
240 valstackbase = frameptr + Stk4(frameptr);
241 localsbase = frameptr + Stk4(frameptr+4);
242
243 switch (desttype) {
244
245 case 0x11:
246 fatal_error("String-terminator call stub at end of function call.");
247 break;
248
249 case 0x10:
250 /* This call stub was pushed during a string-decoding operation!
251 We have to restart it. (Note that the return value is discarded.) */
252 stream_string(pc, 0xE1, destaddr);
253 break;
254
255 case 0x12:
256 /* This call stub was pushed during a number-printing operation.
257 Restart that. (Return value discarded.) */
258 stream_num(pc, TRUE, destaddr);
259 break;
260
261 case 0x13:
262 /* This call stub was pushed during a C-string printing operation.
263 We have to restart it. (Note that the return value is discarded.) */
264 stream_string(pc, 0xE0, destaddr);
265 break;
266
267 case 0x14:
268 /* This call stub was pushed during a Unicode printing operation.
269 We have to restart it. (Note that the return value is discarded.) */
270 stream_string(pc, 0xE2, destaddr);
271 break;
272
273 default:
274 /* We're back in the original frame, so we can store the returnvalue.
275 (If we tried to do this before resetting frameptr, a result
276 destination on the stack would go astray.) */
277 store_operand(desttype, destaddr, returnvalue);
278 break;
279 }
280 }
281
282 /* pop_callstub_string():
283 Remove the magic four values, but interpret them as a string restart
284 state. Returns zero if it's a termination stub, or returns the
285 restart address. The bitnum is extra.
286 */
287 glui32 pop_callstub_string(int *bitnum)
288 {
289 glui32 desttype, destaddr, newpc;
290
291 if (stackptr < 16)
292 fatal_error("Stack underflow in callstub.");
293 stackptr -= 16;
294
295 newpc = Stk4(stackptr+8);
296 destaddr = Stk4(stackptr+4);
297 desttype = Stk4(stackptr+0);
298
299 pc = newpc;
300
301 if (desttype == 0x11) {
302 return 0;
303 }
304 if (desttype == 0x10) {
305 *bitnum = destaddr;
306 return pc;
307 }
308
309 fatal_error("Function-terminator call stub at end of string.");
310 return 0;
311 }
312