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