forked from pete/pez
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpezdef.h
282 lines (234 loc) · 7.95 KB
/
pezdef.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Definitions for components of PEZ
This file contains the definitions for modules within the PEZ
language system itself. Definitions used by programs that link to
PEZ are in PEZ.H, which is included by this file
automatically.
See doc/CREDITS for information about the authors.
This program is in the public domain.
*/
#ifndef __PEZDEF_H
#define __PEZDEF_H
#include "pez.h" /* Define user linkage structures */
/* Word flag bits */
#define IMMEDIATE 1 /* Word is immediate */
#define WORDUSED 2 /* Word used by program */
#define WORDHIDDEN 4 /* Word is hidden from lookup */
/* Data types */
/* Stack items occupied by a dictionary word definition */
#define Dictwordl ((sizeof(pez_dictword) + (sizeof(pez_stackitem)-1)) \
/ sizeof(pez_stackitem))
/* Token types */
#define TokNull 0 /* Nothing scanned */
#define TokWord 1 /* Word stored in token name buffer */
#define TokInt 2 /* Integer scanned */
#define TokReal 3 /* Real number scanned */
#define TokString 4 /* String scanned */
/* Primitive definition table entry */
struct primfcn {
char *pname;
pez_wordp pcode;
};
/* Internal state marker item */
#ifdef EXPORT
#define Exported
extern void P_create(pez_instance *p), P_dodoes(pez_instance *p);
#else /* EXPORT */
#define Exported static
#endif /* EXPORT */
#ifdef EXPORT
extern
#endif
void stakover(pez_instance *p), rstakover(pez_instance *p),
heapover(pez_instance *p), badpointer(pez_instance *p),
stakunder(pez_instance *p), rstakunder(pez_instance *p);
/* Functions called by exported extensions. */
extern void pez_primdef(pez_instance *p, struct primfcn *pt),
pez_error(pez_instance *p, char *kind);
extern pez_dictword *pez_lookup(pez_instance *p, char *name),
*pez_vardef(pez_instance *p, char *name, int size);
extern pez_stackitem *pez_body(pez_instance *p, pez_dictword *dw);
extern int pez_exec(pez_instance *p, pez_dictword *dw);
#ifdef EXPORT
extern char *pez_fgetsp(pez_instance *p, char *s, int n, FILE *stream);
#endif
/* If explicit alignment is not requested, enable it in any case for
known CPU types that require alignment. */
#ifndef ALIGNMENT
#ifdef sparc
#define ALIGNMENT
#endif
#endif
#ifdef __TURBOC__
#define Keyhit() (kbhit() ? getch() : 0)
/* DOS needs poll to detect break */
#define Keybreak() { static int n=0; if ((n = (n+1) & 127) == 0) kbhit(); }
#ifdef __MSDOS__
#define MSDOS
#endif
#endif
#ifdef MSDOS
#define FBmode /* DOS requires binary file flag */
#endif
#ifdef Macintosh
#define FBmode /* Macintosh requires binary file flag */
#endif
#ifdef OS2 /* OS/2 requires binary file flag */
#define FBmode
#endif
#ifdef HIGHC
#define unlink(x) remove(x) /* High C library is brain-dead */
pragma On(PCC_msgs); /* High C compiler is brain-dead */
#endif
/* STATIC is defined as "static" on platforms which require this
specification on declarations for forward-referenced functions. */
#define STATIC static
#ifdef ADS /* Definitions if we're an ADS app */
#define printf ads_printf /* Print through ADS */
#define Keybreak() do {\
static int n=0;\
if ((n=(n+1)&127)==0) {UbI(); broken=ads_usrbrk();}\
} while(0)
#endif
#ifndef Keybreak
#define Keybreak()
#endif
/* Dynamic storage manipulation primitives */
/* Stack access definitions */
#define S0 p->stk[-1] // Top of stack
#define S1 p->stk[-2] // Next on stack
#define S2 p->stk[-3] // Third on stack
#define S3 p->stk[-4] // Fourth on stack
#define S4 p->stk[-5] // Fifth on stack
#define S5 p->stk[-6] // Sixth on stack
#define Pop p->stk-- // Pop the top item off the stack
#define Pop2 p->stk -= 2 // Pop two items off the stack
#define Npop(n) p->stk -= (n) // Pop N items off the stack
// TODO: See Realpush().
#define Push *p->stk++ // Push item onto stack
/*
As above, but FFI equivalents.
Note that, as we don't share dictionaries across instances, we can assume
that the Pez instance we have is the one that will be in use when the
function is called.
Conventions (because this can get confusing and comments inside macros are
bad enough): r is a useful register, sr is a swap register. Swap registers
come last, after the non-register arguments.
*/
// Loads cell n into register r.
#define jit_ld_S(n, r) do { \
jit_ldi_p((r), &p->stk); \
jit_subi_p((r), (r), (n + 1) * sizeof(pez_stackitem)); \
jit_ldr_p((r), (r)); \
} while(0)
// r = address of stack pointer
// r = *(r - 8)
// Loads S0 into register r
#define jit_ld_S0(r) jit_ld_S(0, r)
// jit_Npop(number_of_things_to_pop, register_to_use_for_popping)
#define jit_Npop(n, r) do { \
jit_ldi_p((r), &p->stk); \
jit_subi_p((r), (r), n * sizeof(pez_stackitem)); \
jit_sti_p(&p->stk, (r)); \
} while(0)
#define jit_Pop(r) jit_Npop(1, r);
// Push register r onto the stack.
#define jit_Pushr(r, sr) do { \
jit_ldi_p(sr, &p->stk); \
jit_str_p(sr, r); \
jit_addi_p(sr, sr, sizeof(pez_stackitem)); \
jit_sti_p(&p->stk, sr); \
} while(0)
#ifdef MEMSTAT
#define Mss(n) if ((p->stk+(n))>p->stack) p->stackmax = p->stk+(n);
#define Msr(n) if ((p->rstk+(n))>p->rstack) p->rstackmax = p->rstk+(n);
#define Msh(n) if ((p->hptr+(n))>p->heap) p->heapmax = p->hptr+(n);
#else
#define Mss(n)
#define Msr(n)
#define Msh(n)
#endif
#ifdef BOUNDS_CHECK
#define Memerrs
#define Sl(x) if ((p->stk - p->stack)<(x)) {stakunder(p); return Memerrs;}
#define So(n) do {\
Mss(n); if((p->stk+(n)) > p->stacktop){stakover(p); return Memerrs;} \
} while(0)
#else
#define Sl(x)
#define So(n)
#endif
/* Return stack access definitions */
#define R0 p->rstk[-1] /* Top of return stack */
#define R1 p->rstk[-2] /* Next on return stack */
#define R2 p->rstk[-3] /* Third on return stack */
#define Rpop p->rstk-- /* Pop return stack */
#define Rpush *p->rstk++ /* Push return stack */
#ifdef BOUNDS_CHECK
#define Rsl(x) if((p->rstk - p->rstack)<(x)) {rstakunder(p); return Memerrs;}
#define Rso(n) Msr(n) \
if((p->rstk + (n)) > p->rstacktop){rstakover(p); return Memerrs;}
#else
#define Rsl(x)
#define Rso(n)
#endif
/* Heap access definitions */
#ifdef BOUNDS_CHECK
#define Ho(n) do { \
Msh(n); if((p->hptr+(n)) > p->heaptop){heapover(p); return Memerrs;} \
} while(0);
#else
#define Ho(n)
#endif
#ifdef RESTRICTED_POINTERS
#define Hpc(n) do {\
if((((pez_stackitem *)(n)) < p->heapbot)||\
(((pez_stackitem *)(n)) >= p->heaptop)){\
badpointer(p);\
return Memerrs;\
}\
} while(0)
#else
#define Hpc(n)
#endif
#define Hstore *p->hptr++ // Store item on heap
// Item on heap with overflow check
#define Hsingle(val) do { Ho(1); *p->hptr++ = val; } while(0)
#define state (*p->heap) // Execution state is first heap word
#define prim inline static void // Attributes of primitive functions
/* Floating point macros: */
// How many cells a float takes up:
#define Realsize (sizeof(pez_real)/sizeof(pez_stackitem))
// Pop a float:
#define Realpop p->stk -= Realsize
// Pop two floats:
#define Realpop2 p->stk -= (2 * Realsize)
#define REALSTACK ((pez_real *)p->stk)
#ifdef ALIGNMENT
#define REAL0 *((pez_real *)memcpy((char *)&rbuf0, (char *)(REALSTACK - 1), \
sizeof(pez_real)))
#define REAL1 *((pez_real *)memcpy((char *)&rbuf1, (char *)(REALSTACK - 2), \
sizeof(pez_real)))
#define REAL2 *((pez_real *)memcpy((char *)&rbuf2, (char *)(REALSTACK - 3), \
sizeof(pez_real)))
#define SREAL0(x) rbuf2=(x); (void)memcpy((char *)(REALSTACK - 1), \
(char *)&rbuf2, sizeof(pez_real))
#define SREAL1(x) rbuf2=(x); (void)memcpy((char *)(REALSTACK - 2), \
(char *)&rbuf2, sizeof(pez_real))
#else
#define REAL0 REALSTACK[-1]
#define REAL1 REALSTACK[-2]
#define REAL2 REALSTACK[-3]
#define SREAL0(x) REAL0 = (x)
#define SREAL1(x) REAL1 = (x)
#endif
#define Realpush(x) do { \
So(Realsize); ((pez_real *)p->stk++)[0] = (x); } while(0)
#ifdef TRACE
#define tracing if(p->trace)
#else // TRACE
#define tracing if(0)
#endif
// Miscellaneous conveniences
#define Immediate(word) (di->wname[0] & IMMEDIATE)
#endif