-
Notifications
You must be signed in to change notification settings - Fork 5
/
as3.c
354 lines (336 loc) · 7.36 KB
/
as3.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Z-80 assembler.
* Read in expressions in
* address fields.
*/
#include "as.h"
/*
* Check if the mode of
* an ADDR is TUSER. If not, give
* an error.
*/
void istuser(ADDR *ap)
{
if ((ap->a_type&TMMODE) != TUSER)
aerr(ADDR_REQUIRED);
}
#if 0
/*
* A segment needs to meet a rule. Make sure that we don't have
* internmal inconsistency
*/
static void setsegment(SYM *s, int seg)
{
if (seg == UNKNOWN)
return;
if (s->s_segment == UNKNOWN || s->s_segment == seg) {
s->s_segment = seg;
return;
}
aerr(SEGMENT_CLASH);
}
#endif
static void chkabsolute(ADDR *a)
{
/* Not symbols, doesn't matter */
if ((a->a_type & TMMODE) != TUSER)
return;
if (a->a_segment != ABSOLUTE)
aerr(MUST_BE_ABSOLUTE);
}
static void chksegment(ADDR *left, ADDR *right, int op)
{
uint16_t m = (left->a_type & TMMODE);
if (m == TBR || m == TWR) {
if (op != '+' && op != '-')
aerr(MUST_BE_ABSOLUTE);
if ((right->a_type & TMMODE) == TUSER) {
left->a_segment = right->a_segment;
return;
}
aerr(MUST_BE_ABSOLUTE);
}
/* Not symbols, doesn't matter */
if ((left->a_type & TMMODE) != TUSER ||(right->a_type & TMMODE) != TUSER)
return;
/* Anything goes with absolutes */
if (left->a_segment == ABSOLUTE && right->a_segment == ABSOLUTE)
return;
/* This relies on ABSOLUTE being 0, an addition of segment offset and
absolute either way around produces a segment offset */
if ((left->a_segment == ABSOLUTE || right->a_segment == ABSOLUTE) &&
op == '+') {
if (left->a_sym)
right->a_sym = left->a_sym;
else if (right->a_sym)
left->a_sym = right->a_sym;
left->a_segment += right->a_segment;
return;
}
/* Subtraction within segment produces an absolute */
if (op == '-') {
/* We can't currently represent this and it's hard because
we'd actually need to pass the expression tree to the
linker. Only allow known symbols so that you can
do a-b providing a and b are in your object module */
#if 0
/* Unknown symbols may get segment forced as a result */
if (left->a_segment == UNKNOWN) {
left->a_segment = right->a_segment;
if (left->a_sym)
setsegment(left->a_sym, left->a_segment);
}
if (right->a_segment == UNKNOWN) {
right->a_segment = left->a_segment;
if (right->a_sym)
setsegment(right->a_sym, right->a_segment);
}
#else
if (left->a_segment != UNKNOWN && right->a_segment != UNKNOWN)
#endif
if (left->a_segment == right->a_segment && op == '-') {
left->a_segment = ABSOLUTE;
left->a_sym = NULL;
return;
}
/* - constant we can do. The left segment remains unchanged */
/* How useful it is depends on the target having REL_OVERFLOW
enabled. We need a sane way to set this for a reloc
if we hit this path - ie set it in the resulting addr
somewhere TODO */
if (right->a_segment == ABSOLUTE)
return;
}
left->a_sym = NULL;
aerr(MUST_BE_ABSOLUTE);
}
/*
* Expression reader,
* real work, part I. Read
* operators and resolve types.
* The "lpri" is the firewall operator
* priority, which stops the scan.
* The "paren" argument is true if
* the expression is in parentheses.
*/
void expr1(ADDR *ap, int lpri, int paren)
{
int c;
int c2;
int opri;
ADDR right;
expr2(ap);
while ((c=getnb())=='+' || c=='-' || c=='*' || c=='/') {
c2 = getnb();
/* Pass unary postifx back to the caller as some assembly
syntaxes require we can handle stuff like (X-) for
their own post dec notations */
if (c2 == ')') {
unget(c2);
break;
}
unget(c2);
opri = ADDPRI;
if (c=='*' || c=='/')
opri = MULPRI;
if (opri <= lpri)
break;
expr1(&right, opri, paren);
switch (c) {
case '+':
if ((ap->a_type&TMMODE) != TUSER)
istuser(&right);
else
ap->a_type = right.a_type;
isokaors(ap, paren);
chksegment(ap, &right, '+');
ap->a_value += right.a_value;
break;
case '-':
istuser(&right);
isokaors(ap, paren);
chksegment(ap, &right, '-');
ap->a_value -= right.a_value;
break;
case '*':
istuser(ap);
istuser(&right);
chksegment(ap, &right, '*');
ap->a_value *= right.a_value;
break;
case '/':
istuser(ap);
istuser(&right);
chksegment(ap, &right, '/');
if (right.a_value == 0)
err('z', DIVIDE_BY_ZERO);
else
ap->a_value /= right.a_value;
}
}
unget(c);
}
/*
* Expression reader,
* real work, part II. Read
* in terminals.
*/
void expr2(ADDR *ap)
{
int c;
SYM *sp;
int mode;
char id[NCPS];
c = getnb();
#ifndef TARGET_USES_SQUARE
if (c == '[') {
expr1(ap, LOPRI, 0);
if (getnb() != ']')
qerr(SQUARE_EXPECTED);
return;
}
#else
if (c == '(') {
expr1(ap, LOPRI, 0);
if (getnb() != ')')
qerr(BRACKET_EXPECTED);
return;
}
#endif
if (c == '-') {
expr1(ap, HIPRI, 0);
istuser(ap);
chkabsolute(ap);
ap->a_value = -ap->a_value;
return;
}
if (c == '~') {
expr1(ap, HIPRI, 0);
istuser(ap);
chkabsolute(ap);
ap->a_value = ~ap->a_value;
return;
}
if (c == '\'') {
ap->a_type = TUSER;
ap->a_value = get();
ap->a_segment = ABSOLUTE;
while ((c=get()) != '\'') {
if (c == '\n')
qerr(PERCENT_EXPECTED);
ap->a_value = (ap->a_value<<8) + c;
}
return;
}
if ((c>='0' && c<='9') || c == '$') {
expr3(ap, c);
return;
}
if (isalpha(c) || c == '_') {
getid(id, c);
if ((sp=lookup(id, uhash, 0)) == NULL
&& (sp=lookup(id, phash, 0)) == NULL)
sp = lookup(id, uhash, 1);
mode = sp->s_type&TMMODE;
if (mode==TBR || mode==TWR || mode==TSR || mode==TCC) {
ap->a_type = mode|sp->s_value;
ap->a_value = 0;
ap->a_segment = UNKNOWN;
return;
}
if (mode != TNEW && mode != TUSER)
qerr(SYNTAX_ERROR);
/* An external symbol has to be tracked and output in
the relocations. Any known internal symbol is just
encoded as a relocation relative to a segment */
if (mode == TNEW) {
uerr(id);
ap->a_sym = sp;
} else
ap->a_sym = NULL;
ap->a_type = TUSER;
ap->a_value = sp->s_value;
ap->a_segment = sp->s_segment;
return;
}
qerr(SYNTAX_ERROR);
}
/*
* Read in a constant. The argument
* "c" is the first character of the constant,
* and has already been validated. The number is
* gathered up (stopping on non alphanumeric).
* The radix is determined, and the number is
* converted to binary.
*/
void expr3(ADDR *ap, int c)
{
char *np1;
char *np2;
int radix;
VALUE value;
char num[40];
np1 = &num[0];
do {
if (isupper(c))
c = tolower(c);
*np1++ = c;
c = *ip++;
} while (isalnum(c));
--ip;
/* The grammar here is ambiguous in the case of $11b which should
be 11B hex, but 11b is 11 boolean. We must therefore handle the
$ case first */
if (num[0] == '$') {
np2 = &num[1];
radix = 16;
} else if (num[0] == '0' && num[1] == 'x') {
np2 = &num[2];
radix = 16;
} else {
/* Look for trailing information on the radix */
switch (*--np1) {
case 'h':
radix = 16;
break;
case 'o':
case 'q':
radix = 8;
break;
case 'b':
radix = 2;
break;
default:
radix = 10;
++np1;
}
np2 = &num[0];
/* No trailing tag, so look for 0octab, 0xhex */
if (radix == 10) {
if (*np2 == '0') {
radix = 8;
np2++;
if (*np2 == 'x') {
radix = 16;
np2++;
}
}
}
}
value = 0;
while (np2 < np1) {
if ((c = *np2++)>='0' && c<='9')
c -= '0';
else if (c>='a' && c<='f')
c -= 'a'-10;
else
err('n', INVALID_CONST);
if (c >= radix)
err('n', INVALID_CONST);
value = radix*value + c;
}
ap->a_type = TUSER;
ap->a_value = value;
ap->a_segment = ABSOLUTE;
ap->a_sym = NULL;
}