summaryrefslogtreecommitdiff
path: root/as/express.c
blob: 51537c3a42e38c28a6707fb9f550769d840cc057 (plain)
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/* express.c - expression handler for assembler */

#include "syshead.h"
#include "const.h"
#include "type.h"
#include "address.h"
#include "globvar.h"
#include "scan.h"
#include "source.h"

FORWARD void experror P((char * err_str));
FORWARD void expundefined P((void));
FORWARD void simple2 P((void));
FORWARD void simple P((void));
FORWARD void term P((void));
FORWARD void factor2 P((void));

PUBLIC void absexpres()
{
    expres();
    chkabs();
}

/* check lastexp.data is abs */

PUBLIC void chkabs()
{
    if (lastexp.data & RELBIT)
    {
	if (pass == last_pass)
	    error(ABSREQ);
	expundefined();
    }
}

PRIVATE void experror(err_str)
char * err_str;
{
    error(err_str);
    expundefined();
}

PRIVATE void expundefined()
{
    if( last_pass == 1 )
       lastexp.data = FORBIT | UNDBIT;
    else
       lastexp.data = UNDBIT;
}

PUBLIC void nonimpexpres()
{
    expres();
    if (lastexp.data & IMPBIT)
	experror(NONIMPREQ);
}

/* generate relocation error if pass 2, make lastexp.data forward&undefined */

PUBLIC void showrelbad()
{
    if (pass == last_pass)
	error(RELBAD);
    expundefined();
}

PUBLIC void symabsexpres()
{
    getsym();
    absexpres();
}

PUBLIC void symexpres()
{
    getsym();
    expres();
}

/*
  expres() parses expression = simple expression [op simple expression],
  where op is =, < or >.
  Parameters: sym, number in number, identifier from symname to lineptr - 1.
  Returns value in lastexp.
*/

PUBLIC void expres()
{
    offset_t leftoffset;

    simple();
    leftoffset = lastexp.offset;
    if (sym == EQOP)
    {
	simple2();
	if (leftoffset == lastexp.offset)
	    lastexp.offset = -1;
	else
	    lastexp.offset = 0;
    }
    else if (sym == LESSTHAN)
    {
	/* context-sensitive, LESSTHAN really means less than here */
	simple2();
	if (leftoffset < lastexp.offset)
	    lastexp.offset = -1;
	else
	    lastexp.offset = 0;
    }
    else if (sym == GREATERTHAN)
    {
	/* context-sensitive, GREATERTHAN really means greater than here */
	simple2();
	if (leftoffset > lastexp.offset)
	    lastexp.offset = -1;
	else
	    lastexp.offset = 0;
    }
}

/* get symbol and 2nd simple expression, check both rel or both abs */

PRIVATE void simple2()
{
    unsigned char leftdata;

    leftdata = lastexp.data;
    getsym();
    simple();
    if ((leftdata | lastexp.data) & IMPBIT ||
	(leftdata ^ lastexp.data) & (RELBIT | SEGM))
	showrelbad();
    else
	lastexp.data = (leftdata & lastexp.data) & ~(RELBIT | SEGM);
}

/*
  simple() parses simple expression = [+-] term {op term},
  where op is +, -, or \ (OR).
*/

PRIVATE void simple()
{
    offset_t leftoffset;
    unsigned char leftdata;

    if (sym == ADDOP || sym == SUBOP)
	lastexp.data = lastexp.offset = 0;
    else
	term();
    while (TRUE)
    {
	leftoffset = lastexp.offset;
	leftdata = lastexp.data;
	if (sym == ADDOP)
	{
	    getsym();
	    term();
	    if (leftdata & lastexp.data & RELBIT)
		showrelbad();	/* rel + rel no good */
	    else
		lastexp.data |= leftdata;
	    lastexp.offset += leftoffset;
	}
	else if (sym == SUBOP)
	{
	    getsym();
	    term();
	    /* check not abs - rel or rel - rel with mismatch */
	    if (lastexp.data & RELBIT &&
		(!(leftdata & RELBIT) ||
		 (leftdata | lastexp.data) & IMPBIT ||
		 (leftdata ^ lastexp.data) & (RELBIT | SEGM)))
		showrelbad();
	    else
		lastexp.data = ((leftdata | lastexp.data) & ~(RELBIT | SEGM))
			     | ((leftdata ^ lastexp.data) &  (RELBIT | SEGM));
	    lastexp.offset = leftoffset - lastexp.offset;
	}
	else if (sym == OROP)
	{
	    getsym();
	    term();
	    lastexp.data |= leftdata;
	    chkabs();		/* both must be absolute */
	    lastexp.offset |= leftoffset;
	}
	else
	    return;
    }
}

/* term() parses term = factor {op factor}, where op is *, /, &, <<, or >>. */

PRIVATE void term()
{
    offset_t leftoffset;

    factor();
    while (TRUE)
    {
	leftoffset = lastexp.offset;
	if (sym == STAR)
	{
	    /* context-sensitive, STAR means multiplication here */
	    factor2();
	    lastexp.offset *= leftoffset;
	}
	else if (sym == SLASH)
	{
	    /* context-sensitive, SLASH means division here */
	    factor2();
	    lastexp.offset = leftoffset / lastexp.offset;
	}
	else if (sym == ANDOP)
	{
	    factor2();
	    lastexp.offset &= leftoffset;
	}
	else if (sym == SLOP)
	{
	    factor2();
	    lastexp.offset = leftoffset << lastexp.offset;
	}
	else if (sym == SROP)
	{
	    factor2();
	    lastexp.offset = leftoffset >> lastexp.offset;
	}
	else
	    return;
    }
}

/* get symbol and 2nd or later factor, check both abs */

PRIVATE void factor2()
{
    unsigned char leftdata;

    leftdata = lastexp.data;
    getsym();
    factor();
    lastexp.data |= leftdata;
    chkabs();
}

/*
  factor() parses factor = number | identifier | * | (expression) | ! factor,
  ! is complementation. Returns value in lastexp.offset, possible flags
  IMPBIT, FORBIT, RELBIT and UNDBIT in lastexp.data, and segment in SEGM
  part of lastexp.data, and lastexp.sym at imported symbol if IMPBIT.
  If the factor is an identifier, LOOKUP is used to get its value
  (so the ident is installed in the symbol table if necessary, with
  default flags inidata). If the identifier is not a label,
  (could be imported, or later in the program), its FORBIT is set.
  The expression FORBIT, IMPBIT, RELBIT, UNDBIT and SEGM are then
  taken from the identifier.
*/

PUBLIC void factor()
{
    switch (sym)
    {
    case SLASH:
	/* context-sensitive, SLASH means a hex number here */
	context_hexconst();
    case INTCONST:
	lastexp.data = 0;	/* absolute & not forward or undefined */
	lastexp.offset = number;
	getsym();
	return;
    case IDENT:
	{
	    register struct sym_s *symptr;

	    symptr = gsymptr;
	    if (symptr->type & (MNREGBIT | MACBIT))
		experror(symptr->type & MACBIT ? MACUID :
			 symptr->data & REGBIT ? REGUID : MNUID);
	    else
	    {
		if (!(symptr->type & (LABIT | VARBIT)))
		{
                    if( last_pass == 1 )
		        symptr->data |= FORBIT;
		    lastexp.sym = symptr;
		}
		if (pass != last_pass)
		{
                    if( last_pass == 1 )
		        lastexp.data = symptr->data &
			    (FORBIT | RELBIT | UNDBIT | SEGM);
		    else
		        lastexp.data = symptr->data &
			    (RELBIT | UNDBIT | SEGM);
				/* possible flags for pass 1 */
		    lastexp.offset = symptr->value_reg_or_op.value;
		}
		else
		{
		    if ((lastexp.data = symptr->data) & IMPBIT)
			lastexp.offset = 0;	/* value != 0 for commons */
						/* OK even if UNDBIT */
		    else
		    {
			lastexp.offset = symptr->value_reg_or_op.value;
			if (lastexp.data & UNDBIT)
			    experror(UNBLAB);
		    }
		}
	    }
	    getsym();
	    return;
	}
#ifndef MC6809
    case LBRACKET:
	if (!asld_compatible)
	    break;		/* error, LPAREN is the grouping symbol */
	getsym();
	expres();
	if (sym != RBRACKET)
	    error(RBEXP);
	else
	    getsym();
	return;
#endif
    case LPAREN:
#ifndef MC6809
	if (asld_compatible)
	    break;		/* error, LBRACKET is the grouping symbol */
#endif
	getsym();
	expres();
	if (sym != RPAREN)
	    error(RPEXP);
	else
	    getsym();
	return;
    case NOTOP:
	getsym();
	factor();
	chkabs();
	lastexp.offset = ~lastexp.offset;
	return;
    case ADDOP:
	getsym();
	factor();
	return;
    case SUBOP:
	getsym();
	factor();
	chkabs();
	lastexp.offset = -lastexp.offset;
	return;
    case STAR:
	/* context-sensitive, STAR means location counter here */
	lastexp.offset = lc;
	if ((lastexp.data = lcdata) & UNDBIT && pass == last_pass)
	    experror(UNBLAB);
	getsym();
	return;
    }
    experror(FACEXP);
}

/*
  string compare for IFC/ELSEIFC
  expects (<string1>,<string2>)
  returns logical value in lastexp
*/

PUBLIC void scompare()
{
    /* prepare flags for OK, lastexp.offset for error */
    lastexp.data = lastexp.offset = 0;
    if (sym != LPAREN)
	experror(LPEXP);
    else
    {
	register char *string1;
	register char *string2;

	for (string2 = string1 = lineptr; *string2 != ','; ++string2)
	    if (*string2 == 0 || *string2 == ')')
	    {
		symname = string2;
		experror(COMEXP);
		return;
	    }
	string2++;
	while (*string1++ == *string2++)
	    ;
	if (string2[-1] == ')')
	{
	    if (string1[-1] == ',')
		lastexp.offset = TRUE;	/* else leave FALSE */
	    lineptr = string2;
	}
	else			/* FALSE, keep reading to verify syntax */
	{
	    for (; *string2 != ')'; ++string2)
		if (*string2 == 0 || *string2 == ',')
		{
		    symname = string2;
		    experror(RPEXP);
		}
	    lineptr = ++string2;
	}
	getsym();
    }
}