Debian Codegraph @

terps/tads/tads2/run.h

raw symbols SCIP
1 /*
2 $Header: d:/cvsroot/tads/TADS2/RUN.H,v 1.2 1999/05/17 02:52:13 MJRoberts Exp $
3 */
4
5 /*
6 * Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
7 *
8 * Please see the accompanying license file, LICENSE.TXT, for information
9 * on using and copying this software.
10 */
11 /*
12 Name
13 run.h - definitions for code execution
14 Function
15 Definitions for code execution
16 Notes
17 The preprocessor symbol RUNFAST can be defined if run-time checking
18 of stack overflow, stack underflow, and other unusual but potentially
19 dangerous conditions is to be turned off. This will result in
20 somewhat faster run-time performance, but run-time errors could be
21 disastrous.
22 Modified
23 10/20/91 MJRoberts - creation
24 */
25
26 #ifndef RUN_INCLUDED
27 #define RUN_INCLUDED
28
29 #include "os.h"
30 #ifndef STD_INCLUDED
31 # include "std.h"
32 #endif
33 #ifndef OBJ_INCLUDED
34 # include "obj.h"
35 #endif
36 #ifndef PRP_INCLUDED
37 # include "prp.h"
38 #endif
39 #ifndef OPC_INCLUDED
40 # include "opc.h"
41 #endif
42 #ifndef TIO_INCLUDED
43 # include "tio.h"
44 #endif
45 #ifndef DBG_INCLUDED
46 # include "dbg.h"
47 #endif
48 #ifndef TOK_INCLUDED
49 # include "tok.h"
50 #endif
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 /* forward declarations */
57 struct bifcxdef;
58
59 /* stack element - the stack is an array of these structures */
60 struct runsdef
61 {
62 uchar runstyp; /* type of element */
63 union
64 {
65 long runsvnum; /* numeric value */
66 objnum runsvobj; /* object value */
67 prpnum runsvprp; /* property number value */
68 uchar *runsvstr; /* string/list value */
69 } runsv;
70 };
71 typedef struct runsdef runsdef;
72
73 /* external function control structure */
74 struct runxdef
75 {
76 char runxnam[TOKNAMMAX + 1]; /* name of external function */
77 int (*runxptr)(void *); /* pointer to memory containing code */
78 };
79 typedef struct runxdef runxdef;
80
81 /* external function context structure - passed to user exits */
82 struct runuxdef
83 {
84 struct runcxdef osfar_t *runuxctx; /* run-time context */
85 struct runufdef osfar_t *runuxvec; /* vector of functions */
86 int runuxargc; /* count of arguments to function */
87 };
88 typedef struct runuxdef runuxdef;
89
90 /* external function callback vector */
91 struct runufdef
92 {
93 int (osfar_t *runuftyp)(runuxdef *); /* type of top of stack */
94 long (osfar_t *runufnpo)(runuxdef *); /* pop a number */
95 uchar *(osfar_t *runufspo)(runuxdef *); /* pop a string */
96 void (osfar_t *runufdsc)(runuxdef *); /* discard item at top of stack */
97 void (osfar_t *runufnpu)(runuxdef *, long); /* push a number */
98 void (osfar_t *runufspu)(runuxdef *, uchar *); /* push alloc'd string */
99 void (osfar_t *runufcspu)(runuxdef *, char *); /* push a C-string */
100 uchar *(osfar_t *runufsal)(runuxdef *, int); /* allocate a new string */
101 void (osfar_t *runuflpu)(runuxdef *, int);/* push DAT_TRUE or DAT_NIL */
102 };
103 typedef struct runufdef runufdef;
104
105 /* execution context */
106 struct runcxdef
107 {
108 errcxdef *runcxerr; /* error management context */
109 mcmcxdef *runcxmem; /* cache manager context for object references */
110 runsdef *runcxstk; /* base of interpreter stack */
111 runsdef *runcxstop; /* top of stack */
112 runsdef *runcxsp; /* current stack pointer (stack grows upwards) */
113 runsdef *runcxbp; /* base pointer */
114 uchar *runcxheap; /* run-time variable-length object heap */
115 uchar *runcxhp; /* current heap pointer */
116 uchar *runcxhtop; /* top of heap */
117 objucxdef *runcxundo; /* undo context */
118 tiocxdef *runcxtio; /* text I/O context */
119 void *runcxbcx; /* context for built-in callback functions */
120 void (**runcxbi)(struct bifcxdef *ctx, int argc);
121 /* built-in functions */
122 struct dbgcxdef *runcxdbg; /* debugger context */
123 struct voccxdef *runcxvoc; /* player command parser context */
124 void (*runcxdmd)(void *ctx, objnum obj, prpnum prp);
125 /* demand-loader callback function */
126 void *runcxdmc; /* demand-loader callback context */
127 runxdef *runcxext; /* external function array */
128 int runcxexc; /* count of external functions */
129 uint runcxlofs; /* offset of last line record encountered */
130 char *runcxgamename; /* name of the .GAM file */
131 char *runcxgamepath; /* absolute directory path of .GAM file */
132 };
133 typedef struct runcxdef runcxdef;
134
135 /* execute a function, given the function object number */
136 void runfn(runcxdef *ctx, noreg objnum objn, int argc);
137
138 /*
139 * Execute p-code given a pointer to the code. p is the actual pointer
140 * to the first byte of code to be executed. self is the object to be
141 * used for the special 'self' pseudo-object, and target is the object
142 * whose data are actually being executed. targprop is the property being
143 * executed; 0 is used for functions.
144 */
145 void runexe(runcxdef *ctx, uchar *p, objnum self, objnum target,
146 prpnum targprop, int argc);
147
148 /* push a value onto the stack */
149 void runpush(runcxdef *ctx, dattyp typ, runsdef *val);
150
151 /* push a value onto the stack that's already in the heap */
152 void runrepush(runcxdef *ctx, runsdef *val);
153
154 /* push a number onto the stack */
155 void runpnum(runcxdef *ctx, long val);
156
157 /* push an object onto the stack */
158 void runpobj(runcxdef *ctx, objnum obj);
159
160 /* push nil */
161 void runpnil(runcxdef *ctx);
162
163 /* push a value onto the stack from a buffer (propdef, list) */
164 void runpbuf(runcxdef *ctx, int typ, void *val);
165
166 /* push a counted-length string onto the stack */
167 void runpstr(runcxdef *ctx, char *str, int len, int sav);
168
169 /*
170 * Push a C-style string onto the stack, converting escape codes. If
171 * the character contains backslashes, newline, or tab characters, we'll
172 * convert these characters to their escaped equivalent.
173 */
174 void runpushcstr(runcxdef *ctx, char *str, size_t len, int sav);
175
176 /*
177 * Push a property onto the stack. codepp is a pointer to the caller's
178 * code pointer, which will be updated if necessary; callobj and
179 * callofsp are the object and starting offset within the object of the
180 * code being executed by the caller, which are needed to update
181 * *codepp. Property 0 is used if a function is being executed. obj
182 * and prop are the object and property number whose value is to be
183 * pushed. If 'inh' is TRUE, it means that only a property inherited
184 * by 'obj' is to be considered; this is used for "pass"/"inherited"
185 * operations, with the current target object given as 'obj'.
186 */
187 void runpprop(runcxdef *ctx, uchar *noreg *codepp, objnum callobj,
188 prpnum callprop, noreg objnum obj, prpnum prop, int inh,
189 int argc, objnum self);
190
191 /* top level runpprop, when caller is not executing in an object */
192 /* void runppr(runcxdef *ctx, objnum obj, prpnum prp, int argc); */
193 #define runppr(ctx, obj, prp, argc) \
194 runpprop(ctx, (uchar **)0, (objnum)0, (prpnum)0, obj, prp, FALSE, argc, obj)
195
196 /* discard top element on stack */
197 /* void rundisc(runcxdef *ctx); */
198 #define rundisc(ctx) (runstkund(ctx), (--((ctx)->runcxsp)))
199
200 /* pop the top element on the stack */
201 /* void runpop(runcxdef *ctx, runsdef *val); */
202 #define runpop(ctx, v) \
203 (runstkund(ctx), memcpy(v, (--((ctx)->runcxsp)), (size_t)sizeof(runsdef)))
204
205 /* pop a numeric value, signalling an error if not a number */
206 /* long runpopnum(runcxdef *ctx); */
207 #define runpopnum(ctx) \
208 (runstkund(ctx), ((--((ctx)->runcxsp))->runstyp!=DAT_NUMBER ? \
209 (runsig(ctx,ERR_REQNUM), (long)0) : \
210 ((ctx)->runcxsp->runsv.runsvnum)))
211
212 /* pop an object, signalling an error if not an object */
213 /* objnum runpopobj(runcxdef *ctx); */
214 #define runpopobj(ctx) \
215 (runstkund(ctx), ((--(ctx)->runcxsp))->runstyp!=DAT_OBJECT ? \
216 (runsig(ctx,ERR_REQVOB), (objnum)0) : \
217 ((ctx)->runcxsp->runsv.runsvobj))
218
219 /* pop an object or nil - returns MCMONINV if the value is nil */
220 #define runpopobjnil(ctx) \
221 (runstkund(ctx), ((--(ctx)->runcxsp))->runstyp==DAT_OBJECT ? \
222 ((ctx)->runcxsp->runsv.runsvobj) : \
223 ((ctx)->runcxsp->runstyp==DAT_NIL ? MCMONINV : \
224 (runsig(ctx,ERR_REQVOB), (objnum)0)))
225
226 /* pop a list, signalling an error if not a list */
227 /* uchar *runpoplst(runcxdef *ctx); */
228 #define runpoplst(ctx) \
229 (runstkund(ctx), ((--(ctx)->runcxsp))->runstyp!=DAT_LIST ? \
230 (runsig(ctx,ERR_REQVLS), (uchar *)0) : \
231 (uchar *)((ctx)->runcxsp->runsv.runsvstr))
232
233 /* pop a property number, signalling an error if not a property number */
234 /* prpnum runpopprp(runcxdef *ctx); */
235 #define runpopprp(ctx) \
236 (runstkund(ctx), ((--(ctx)->runcxsp))->runstyp!=DAT_PROPNUM ? \
237 (runsig(ctx,ERR_REQVPR), (prpnum)0) : \
238 ((ctx)->runcxsp->runsv.runsvprp))
239
240
241 /* pop function pointer */
242 /* objnum runpopfn(runcxdef *ctx); */
243 #define runpopfn(ctx) \
244 ((objnum)(runstkund(ctx), ((--(ctx)->runcxsp))->runstyp!=DAT_FNADDR ? \
245 (runsig(ctx,ERR_REQVFN), (objnum)0) : \
246 ((ctx)->runcxsp->runsv.runsvobj)))
247
248 /* pop a string value */
249 /* char *runpopstr(runcxdef *ctx); */
250 #define runpopstr(ctx) \
251 (runstkund(ctx), ((--((ctx)->runcxsp))->runstyp!=DAT_SSTRING ? \
252 (runsig(ctx,ERR_REQSTR), (uchar *)0) : \
253 ((ctx)->runcxsp->runsv.runsvstr)))
254
255
256 /* pop a logical value - TRUE for DAT_TRUE, FALSE for DAT_NIL */
257 /* int runpoplog(runcxdef *ctx); */
258 #define runpoplog(ctx) \
259 ((--((ctx)->runcxsp))->runstyp==DAT_TRUE ? TRUE : \
260 (ctx)->runcxsp->runstyp==DAT_NIL ? FALSE : \
261 (runsig(ctx, ERR_REQLOG), 0))
262
263 /* get type of top of stack */
264 /* int runtostyp(runcxdef *ctx); */
265 #define runtostyp(ctx) (((ctx)->runcxsp - 1)->runstyp)
266
267 /* determine if top of stack is logical value (returns TRUE if so) */
268 /* int runtoslog(runcxdef *ctx); */
269 #define runtoslog(ctx) \
270 (runtostyp(ctx) == DAT_TRUE || runtostyp(ctx) == DAT_NIL)
271
272 /* convert C logical to TADS logical (TRUE->DAT_TRUE, FALSE->DAT_NIL) */
273 /* int runclog(int log); */
274 #define runclog(l) ((l) ? DAT_TRUE : DAT_NIL)
275
276 /* compare magnitudes of numbers/strings on top of stack; strcmp-like value */
277 int runmcmp(runcxdef *ctx);
278
279 /* TRUE if items at top of stack are equal, FALSE otherwise */
280 int runeq(runcxdef *ctx);
281
282 /* check for stack underflow */
283 /* void runstkund(runcxdef *ctx); */
284
285 /* check for stack overflow */
286 /* void runstkovf(runcxdef *ctx); */
287
288 /*
289 * Check to ensure we have enough arguments to pass to a function or method
290 * call - this simply ensures we have enough data in the current frame.
291 * This is important because the called function will be able to write to
292 * our frame. If we don't have enough arguments, we'll push enough 'nil'
293 * values to meet the need.
294 */
295 #define runcheckargc(ctx, nargc) \
296 while ((ctx)->runcxsp - (ctx)->runcxbp < *(nargc)) \
297 runpnil(ctx)
298
299 #ifdef RUNFAST
300 # define runstkovf(ctx) (DISCARD 0)
301 # define runstkund(ctx) (DISCARD 0)
302 #else /* RUNFAST */
303 # define runstkovf(ctx) \
304 ((ctx)->runcxsp >= (ctx)->runcxstop ? (runsig(ctx, ERR_STKOVF), \
305 DISCARD 0) : DISCARD 0)
306 # define runstkund(ctx) \
307 ((ctx)->runcxsp == (ctx)->runcxstk ? runsig(ctx, ERR_STKUND), \
308 DISCARD 0 : DISCARD 0)
309 #endif /* RUNFAST */
310
311 /* reserve space in heap, collecting garbage if necessary */
312 /* void runhres(runcxdef *ctx, uint siz, uint below); */
313 #define runhres(ctx, siz, below) \
314 ((uint)((ctx)->runcxhtop - (ctx)->runcxhp) > (uint)(siz) ? DISCARD 0 : \
315 (runhcmp(ctx, siz, below, (runsdef *)0, (runsdef *)0, (runsdef *)0),\
316 DISCARD 0))
317
318 /* reserve space, with various amounts of saving */
319 #define runhres1(ctx, siz, below, val1) \
320 ((uint)((ctx)->runcxhtop - (ctx)->runcxhp) > (uint)(siz) ? DISCARD 0 : \
321 (runhcmp(ctx, siz, below, val1, (runsdef *)0, (runsdef *)0), DISCARD 0))
322
323 #define runhres2(ctx, siz, below, val1, val2) \
324 ((uint)((ctx)->runcxhtop - (ctx)->runcxhp) > (uint)(siz) ? DISCARD 0 : \
325 (runhcmp(ctx, siz, below, val1, val2, (runsdef *)0), DISCARD 0))
326
327 #define runhres3(ctx, siz, below, val1, val2, val3) \
328 ((uint)((ctx)->runcxhtop - (ctx)->runcxhp) > (uint)(siz) ? DISCARD 0 : \
329 (runhcmp(ctx, siz, below, val1, val2, val3), DISCARD 0))
330
331 /* garbage collect heap, making sure 'siz' bytes are available afterwards */
332 void runhcmp(runcxdef *ctx, uint siz, uint below,
333 runsdef *val1, runsdef *val2, runsdef *val3);
334
335 /* determine size of a data item */
336 int runsiz(runsdef *item);
337
338 /* find a sublist within a list, returning pointer to sublist or NULL */
339 uchar *runfind(uchar *list, runsdef *item);
340
341 /* add two runsdef values, returning result in *val */
342 void runadd(runcxdef *ctx, runsdef *val, runsdef *val2, uint below);
343
344 /*
345 * subtract val2 from val, returning result in *val; return TRUE if
346 * value changed, FALSE otherwise (this is returned when subtracting
347 * something from a list that isn't in the list)
348 */
349 int runsub(runcxdef *ctx, runsdef *val, runsdef *val2, uint below);
350
351 /* restore code pointer from object.property + offset */
352 uchar *runcprst(runcxdef *ctx, uint ofs, objnum obj, prpnum prop);
353
354 /* leave a stack frame, removing arguments */
355 /* void runleave(runcxdef *ctx, uint parms); */
356 #define runleave(ctx, parms) \
357 (((ctx)->runcxsp = (ctx)->runcxbp), \
358 ((ctx)->runcxbp = (runsdef *)((--((ctx)->runcxsp))->runsv.runsvstr)), \
359 ((ctx)->runcxsp -= (parms)))
360
361 /* reset run-time: throw away entire stack and heap */
362 /* void runrst(runcxdef *ctx); */
363 #define runrst(ctx) (((ctx)->runcxsp = (ctx)->runcxstk), \
364 ((ctx)->runcxhp = (ctx)->runcxheap), \
365 dbgrst(ctx->runcxdbg))
366
367 /* set up runtime status line display */
368 void runistat(struct voccxdef *vctx, struct runcxdef *rctx,
369 struct tiocxdef *tctx);
370
371 /* signal a run-time error - allows debugger trapping */
372 void runsign(runcxdef *ctx, int err);
373
374 /* sign a run-time error with zero arguments */
375 #define runsig(ctx, err) (errargc((ctx)->runcxerr,0),runsign(ctx,err))
376
377 /* signal a run-time error with one argument */
378 #define runsig1(ctx, err, typ, arg) \
379 (errargv((ctx)->runcxerr,0,typ,arg),errargc((ctx)->runcxerr,1),\
380 runsign(ctx,err))
381
382 /* draw status line */
383 void runstat(void);
384
385 /* initialize output status */
386 void runistat(struct voccxdef *vctx, struct runcxdef *rctx,
387 struct tiocxdef *tctx);
388
389 #ifdef __cplusplus
390 }
391 #endif
392
393 #endif /* RUN_INCLUDED */