-
Notifications
You must be signed in to change notification settings - Fork 1
/
emt.c
319 lines (275 loc) · 8.28 KB
/
emt.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/TADS2/EMT.C,v 1.2 1999/05/17 02:52:11 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1991, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
emt.c - emitter
Function
Code emitter
Notes
None
Modified
09/23/91 MJRoberts - creation
*/
#include <assert.h>
#include "os.h"
#include "std.h"
#include "emt.h"
#include "opc.h"
#include "err.h"
#include "tok.h"
void emtval(emtcxdef *ctx, tokdef *tok, uchar *base)
{
ushort oplen;
switch(tok->toktyp)
{
case TOKTSYMBOL:
switch(tok->toksym.tokstyp)
{
case TOKSTARGC:
emtop(ctx, OPCARGC);
return;
case TOKSTSELF:
emtop(ctx, OPCPUSHSELF);
return;
case TOKSTOBJ:
case TOKSTFWDOBJ:
emtop(ctx, OPCPUSHOBJ);
break;
case TOKSTLOCAL:
if (tok->toksym.toksfr)
{
emtop(ctx, OPCGETDBLCL); /* debug local - specifies frame */
emtint2(ctx, ctx->emtcxfrob); /* emit frame object */
emtint2(ctx, tok->toksym.toksfr); /* emit frame offset */
}
else
emtop(ctx, OPCGETLCL);
break;
case TOKSTBIFN:
emtop(ctx, OPCBUILTIN);
emtbyte(ctx, 0);
break;
case TOKSTFUNC:
case TOKSTFWDFN:
emtop(ctx, OPCPUSHFN);
break;
case TOKSTPROP:
emtop(ctx, OPCGETPSELF);
emtbyte(ctx, 0);
break;
case TOKSTPROPSPEC:
emtop(ctx, OPCGETPSELFDATA);
break;
default:
errsig(ctx->emtcxerr, ERR_REQOBJ);
/* NOTREACHED */
}
emtint2(ctx, tok->toksym.toksval);
return;
case TOKTNUMBER:
emtop(ctx, OPCPUSHNUM);
emtint4(ctx, tok->tokval);
return;
case TOKTNIL:
emtop(ctx, OPCPUSHNIL);
return;
case TOKTTRUE:
emtop(ctx, OPCPUSHTRUE);
return;
case TOKTPOUND:
emtop(ctx, OPCPUSHPN);
emtint2(ctx, tok->tokofs);
return;
case TOKTSSTRING:
emtop(ctx, OPCPUSHSTR);
break;
case TOKTDSTRING:
emtop(ctx, OPCSAY);
break;
case TOKTLIST:
emtop(ctx, OPCPUSHLST);
emtlst(ctx, (uint)tok->tokofs, base);
return;
}
/* if we haven't returned already, we have a large operand */
oplen = osrp2(base + tok->tokofs);
emtmem(ctx, base + tok->tokofs, oplen);
}
/* emit a list value */
void emtlst(emtcxdef *ctx, uint ofs, uchar *base)
{
uint initofs;
ushort i;
emtlidef *lst;
emtledef *ele;
initofs = ctx->emtcxofs; /* save starting offset */
emtint2(ctx, 2); /* emit placeholder length */
if (ofs == 0) return; /* nothing more to do for empty list */
lst = (emtlidef *)(base + ofs);
for (i = lst->emtlicnt, ele = &lst->emtliele[i-1] ; i ; --ele, --i)
{
uchar dat;
switch(ele->emtletyp)
{
case TOKTLIST:
emtbyte(ctx, DAT_LIST);
emtlst(ctx, (uint)ele->emtleval, base);
continue;
case TOKTSSTRING:
{
uint oplen = osrp2(base + ele->emtleval);
emtbyte(ctx, DAT_SSTRING);
emtmem(ctx, base + ele->emtleval, oplen);
continue;
}
case TOKTNUMBER:
dat = DAT_NUMBER;
break;
case TOKTNIL:
dat = DAT_NIL;
break;
case TOKTTRUE:
dat = DAT_TRUE;
break;
case TOKTPOUND:
dat = DAT_PROPNUM;
goto symbol;
case TOKTFUNCTION:
dat = DAT_FNADDR;
goto symbol;
case TOKTOBJECT:
dat = DAT_OBJECT;
goto symbol;
case TOKTDOT:
dat = DAT_PROPNUM;
/* FALLTHROUGH */
symbol:
emtbyte(ctx, dat);
emtint2(ctx, (ushort)ele->emtleval);
continue;
default:
assert(FALSE);
break;
}
/* if we get here, emit datatype in dat, plus value */
emtbyte(ctx, dat);
if (dat != DAT_TRUE && dat != DAT_NIL)
emtint4(ctx, ele->emtleval);
}
oswp2(ctx->emtcxptr + initofs, ctx->emtcxofs - initofs);
}
/* reserve space for code, expanding object if needed */
void emtres(emtcxdef *ctx, ushort bytes)
{
ushort oldsiz = mcmobjsiz(ctx->emtcxmem, ctx->emtcxobj);
ushort need;
need = ctx->emtcxofs + bytes + 1;
if (need > oldsiz)
{
ushort newsiz;
newsiz = need + EMTINC;
if (newsiz < oldsiz) errsig(ctx->emtcxerr, ERR_OBJOVF);
ctx->emtcxptr = mcmrealo(ctx->emtcxmem, ctx->emtcxobj, newsiz);
assert(mcmobjsiz(ctx->emtcxmem, ctx->emtcxobj) >= need);
}
}
/* get a new temporary label */
uint emtglbl(emtcxdef *ctx)
{
uint ret = ctx->emtcxlfre;
if (ctx->emtcxlfre == EMTLLNKEND) errsig(ctx->emtcxerr, ERR_NOLBL);
ctx->emtcxlfre = ctx->emtcxlbl[ret].emtllnk;
ctx->emtcxlbl[ret].emtllnk = EMTLLNKEND; /* nothing in label's list */
ctx->emtcxlbl[ret].emtlflg = 0; /* label is not yet set */
return(ret);
}
/* get a new label and set to the current position */
uint emtgslbl(emtcxdef *ctx)
{
uint ret = ctx->emtcxlfre;
if (ctx->emtcxlfre == EMTLLNKEND) errsig(ctx->emtcxerr, ERR_NOLBL);
ctx->emtcxlfre = ctx->emtcxlbl[ret].emtllnk;
ctx->emtcxlbl[ret].emtllnk = EMTLLNKEND; /* nothing in label's list */
ctx->emtcxlbl[ret].emtlflg = EMTLFSET; /* label is not yet set */
ctx->emtcxlbl[ret].emtlofs = ctx->emtcxofs; /* set position */
return(ret);
}
/* set a temporary label */
void emtslbl(emtcxdef *ctx, noreg uint *lblp, int release)
{
uint lbl = *lblp;
emtldef *p = &ctx->emtcxlbl[lbl];
uint fwd;
emtldef *fwdp;
ushort nxt;
short diff;
ushort curpos = ctx->emtcxofs;
p->emtlofs = curpos;
p->emtlflg |= EMTLFSET;
/* run through forward reference list, fixing up each reference */
for (fwd = p->emtllnk ; fwd != EMTLLNKEND ; fwd = nxt)
{
fwdp = &ctx->emtcxlbl[fwd]; /* get pointer to current record */
nxt = fwdp->emtllnk; /* remember the next record */
/* write the fixup, and release the forward reference */
diff = curpos - fwdp->emtlofs;
oswp2(ctx->emtcxptr + fwdp->emtlofs, diff);
fwdp->emtllnk = EMTLLNKEND;
emtdlbl(ctx, &fwd);
}
p->emtllnk = EMTLLNKEND; /* there are no more forward refs for label */
if (release) emtdlbl(ctx, lblp);
}
/* release a temporary label */
void emtdlbl(emtcxdef *ctx, noreg uint *lblp)
{
/* if label has forward references, internal error: label never set */
if (ctx->emtcxlbl[*lblp].emtllnk != EMTLLNKEND)
errsig(ctx->emtcxerr, ERR_LBNOSET);
ctx->emtcxlbl[*lblp].emtllnk = ctx->emtcxlfre;
ctx->emtcxlfre = *lblp;
*lblp = EMTLLNKEND;
}
/* clear a temporary label without error checking */
void emtclbl(emtcxdef *ctx, noreg uint *lblp)
{
emtslbl(ctx, lblp, TRUE); /* clear up forward ref's, and delete */
}
/* emit a jump to a temporary label */
void emtjmp(emtcxdef *ctx, uchar op, uint lbl)
{
emtldef *p = &ctx->emtcxlbl[lbl];
short diff;
uint fwd;
emtop(ctx, op);
if (p->emtlflg & EMTLFSET)
diff = p->emtlofs - ctx->emtcxofs; /* back ref - compute delta */
else
{
diff = 0;
fwd = emtglbl(ctx); /* get a forward ref fixup reminder */
ctx->emtcxlbl[fwd].emtllnk = p->emtllnk; /* link into label's list */
ctx->emtcxlbl[fwd].emtlofs = ctx->emtcxofs; /* remember where */
p->emtllnk = fwd; /* make it new head of this label's list */
}
emtint2s(ctx, diff);
}
/* set up the labels - put all in free list */
void emtlini(emtcxdef *ctx)
{
int i;
emtldef *p;
int max;
for (max = ctx->emtcxlcnt, i = 1, p = ctx->emtcxlbl ; i < max ; ++p, ++i)
p->emtllnk = i;
p->emtllnk = EMTLLNKEND;
ctx->emtcxlfre = 0;
}