summaryrefslogtreecommitdiff
path: root/bcc/express.c
blob: bceff0ef56a50eadc10fbe0ff15340171c49b7dd (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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* express.c - expression parsing routines for bcc */

/* Copyright (C) 1992 Bruce Evans */

#include "bcc.h"
#include "gencode.h"
#include "parse.h"
#include "reg.h"
#include "sc.h"
#include "scan.h"
#include "table.h"		/* just for charptr for string constant */
#include "type.h"

PRIVATE unsigned insizeof;	/* nest level for getsizeof */
				/* used to avoid aborting undefined idents */
				/* to 0 when they appear in a cpp expression */
				/* under sizeof */

/* names of expression functions are related to the 15 precedence levels */
/* on p49 of K & R */

FORWARD struct nodestruct *cast_exp P((void));
FORWARD struct nodestruct *expr2 P((void));
FORWARD struct nodestruct *exp3to12 P((fastin_pt lprecedence));
FORWARD struct nodestruct *listargs P((void));
FORWARD struct nodestruct *postfix_exp P((bool_pt seenlp));
FORWARD struct nodestruct *primary_exp P((void));
FORWARD struct nodestruct *unary_exp P((void));

PRIVATE struct nodestruct *cast_exp()
{
    struct nodestruct *nodeptr;
    scalar_t scalar;
    struct typestruct *vartype;

    if (sym != LPAREN)
	return unary_exp();
    nextsym();
    if ((vartype = typename()) == NULL)
	return postfix_exp(TRUE);
    rparen();
    scalar = (nodeptr = cast_exp())->nodetype->scalar;
    if (vartype->scalar & INT && scalar & (CHAR | SHORT | INT)
	&& !((vartype->scalar ^ scalar) & UNSIGNED))
    {
/* No ancient switch on low mem systems */
#ifndef VERY_SMALL_MEMORY
	/* In ancient UNIX C, casts remain lvalues */
	if (!ancient)
#endif
	    nodeptr->flags &= ~LVALUE;
	return nodeptr;		/* skip casts that are default promotions */
    }
    return castnode(vartype, nodeptr);
}

PUBLIC struct nodestruct *assignment_exp()
{
    struct nodestruct *lhs;
    op_pt op;

    lhs = expr2();
    if (sym >= ASSIGNOP && sym <= SUBABOP)	/* assign-op syms in order! */
    {
	op = sym;
	nextsym();
	lhs = node(op, lhs, assignment_exp());
    }
    return lhs;
}

PUBLIC struct nodestruct *expression()
{
    struct nodestruct *lhs;

    lhs = assignment_exp();
    while (sym == COMMA)
    {
	nextsym();
	lhs = node(COMMAOP, lhs, assignment_exp());
    }
    return lhs;
}

PRIVATE struct nodestruct *expr2()
{
    struct nodestruct *lhs;
    struct nodestruct *rhs;

    lhs = exp3to12(0);
    if (sym == CONDOP)
    {
	nextsym();
	rhs = expression();
	colon();
	lhs = node(CONDOP, lhs, node(COLONOP, rhs, expr2()));
    }
    return lhs;
}

PRIVATE struct nodestruct *exp3to12(lprecedence)
fastin_pt lprecedence;
{
    struct nodestruct *lhs;
    op_pt op;
    fastin_t rprecedence;

    lhs = cast_exp();
    while (TRUE)
    {
	rprecedence = 0;
	switch (sym)
	{
	case LOGOROP:
	    if ((fastin_t) lprecedence <= 1)
		rprecedence = 2;
	    break;
	case LOGANDOP:
	    if ((fastin_t) lprecedence <= 3)
		rprecedence = 4;
	    break;
	case OROP:
	    if ((fastin_t) lprecedence <= 5)
		rprecedence = 6;
	    break;
	case EOROP:
	    if ((fastin_t) lprecedence <= 7)
		rprecedence = 8;
	    break;
	case AMPERSAND:
	    if ((fastin_t) lprecedence <= 9)
	    {
		sym = ANDOP;
		rprecedence = 10;
	    }
	    break;
	case EQOP:
	case NEOP:
	    if ((fastin_t) lprecedence <= 11)
		rprecedence = 12;
	    break;
	case GEOP:
	case GTOP:
	case LEOP:
	case LTOP:
	    if ((fastin_t) lprecedence <= 13)
		rprecedence = 14;
	    break;
	case SLOP:
	case SROP:
	    if ((fastin_t) lprecedence <= 15)
		rprecedence = 16;
	    break;
	case HYPHEN:
	    if ((fastin_t) lprecedence <= 17)
	    {
		sym = SUBOP;
		rprecedence = 18;
	    }
	    break;
	case ADDOP:
	    if ((fastin_t) lprecedence <= 17)
		rprecedence = 18;
	    break;
	case STAR:
	    if ((fastin_t) lprecedence <= 19)
	    {
		sym = MULOP;
		rprecedence = 20;
	    }
	    break;
	case DIVOP:
	case MODOP:
	    if ((fastin_t) lprecedence <= 19)
		rprecedence = 20;
	    break;
	}
	if (rprecedence == 0)
	    break;
	op = sym;
	nextsym();
	lhs = node(op, lhs, exp3to12(rprecedence));
    }
    return lhs;
}

PRIVATE struct nodestruct *listargs()
{
    struct nodestruct *parent;
    struct nodestruct *nextright;

    if (sym == RPAREN)
    {
	nextsym();
	return NULLNODE;
    }
    parent = node(arg1op, assignment_exp(), NULLNODE);
    nextright = parent;
    while (sym == COMMA)
    {
	nextsym();
	nextright = nextright->right
	    = node(LISTOP, assignment_exp(), NULLNODE);
    }
    rparen();
    return parent;
}

PRIVATE struct nodestruct *postfix_exp(seenlp)
bool_pt seenlp;
{
    struct nodestruct *nodeptr;
    struct symstruct *symptr;

    if (seenlp)
    {
	nodeptr = expression();
	rparen();
    }
    else
	nodeptr = primary_exp();
    while (TRUE)
    {
	switch (sym)
	{
	case DECSYM:
	    nextsym();
	    nodeptr = node(POSTDECOP, nodeptr, NULLNODE);
	    continue;
	case INCSYM:
	    nextsym();
	    nodeptr = node(POSTINCOP, nodeptr, NULLNODE);
	    continue;
	case LBRACKET:
	    nextsym();
	    nodeptr = node(INDIRECTOP, node(ADDOP, nodeptr, expression()),
			   NULLNODE);
	    rbracket();
	    continue;
	case LPAREN:
	    nextsym();
	    nodeptr = node(FUNCOP, nodeptr, listargs());
	    {
		register struct nodestruct *np;

		for (np = nodeptr->right; np != NULL; np = np->right)
		{
		    if (np->nodetype->scalar & RSCALAR)
		    {
			np = nodeptr->left.nodeptr;
			if (np->tag != LEAF)
			    printf_fp = TRUE;
			else
			{
			    unsigned len;
			    register char *name;

			    name = np->left.symptr->name.namep;
			    if ((len = strlen(name)) >= 6
				&& strcmp(name + len - 6, "printf") == 0)
				printf_fp = TRUE;
			}
			break;
		    }
		}
		for (np = nodeptr->right; np != NULL; np = np->right)
		{
		    if (np->nodetype->constructor & POINTER
			&& np->nodetype->nexttype->scalar & RSCALAR)
		    {
			np = nodeptr->left.nodeptr;
			if (np->tag != LEAF)
			    scanf_fp = TRUE;
			else
			{
			    unsigned len;
			    register char *name;

			    name = np->left.symptr->name.namep;
			    if ((len = strlen(name)) >= 5
				&& strcmp(name + len - 5, "scanf") == 0)
				scanf_fp = TRUE;
			}
			break;
		    }
		}
	    }
	    continue;
	case STRUCPTROP:
	    nodeptr = node(INDIRECTOP, nodeptr, NULLNODE);
	case STRUCELTOP:
	    nextsym();
/* On memory constrained systems we save space that would be needed to keep */
/* track of anonymous structure members by not implementing lookup in them */
/* at all. */
#ifdef VERY_SMALL_MEMORY
            gs2name[0] = nodeptr->nodetype->structkey[0];
            gs2name[1] = nodeptr->nodetype->structkey[1];
            if ((gsymptr = findlorg(gs2name)) == NULL)
#else
	    if ((gsymptr = findstrm(nodeptr->nodetype, gs2name)) == NULL)
#endif
	    {
		error("undefined structure element");
		gsymptr = addglb(gs2name, itype);
	    }
	    symptr = exprsym(gsymptr);
	    nextsym();
	    nodeptr = node(STRUCELTOP, nodeptr, leafnode(symptr));
	    continue;
	default:
	    return nodeptr;
	}
    }
}

PRIVATE struct nodestruct *primary_exp()
{
    bool_t isdefined;
    struct nodestruct *nodeptr;
    uoffset_T stringlen;
    struct symstruct *symptr;
    struct symstruct *symptr1;
    bool_t waslparen;

    switch (sym)
    {
    case IDENT:
	if (incppexpr && !insizeof)
	{
cpp_ident:
	    nextsym();
	    return leafnode(constsym((value_t) 0));
	}
	if ((symptr = gsymptr) != NULL)
	    nextsym();
	else
	{
	    symptr = addglb(gsname, fitype);
	    nextsym();
	    if (sym != LPAREN)
	    {
		error2error(symptr->name.namea, " undeclared");
		symptr->indcount = 1;
		symptr->type = itype;
	    }
	}
	symptr1 = exprsym(symptr);
	if (symptr->flags & STATIC && symptr->level != GLBLEVEL)
	{
	    symptr1->flags |= LABELLED;
	    symptr1->offset.offi = 0;
	    symptr1->name.label = symptr->offset.offlabel;
	}
	nodeptr = leafnode(symptr1);
	if (!(nodeptr->nodetype->constructor & (ARRAY | FUNCTION | VOID)))
	    nodeptr->flags = LVALUE;
	return nodeptr;
    case TYPEDEFNAME:
	if (incppexpr && !insizeof)
	    goto cpp_ident;	/* else fall through */
    default:
	error("bad expression");
	constant.value.v = 0;
	constant.type = itype;
    case CHARCONST:
    case INTCONST:		/* this includes enumeration-constants */
	symptr = constsym(constant.value.v);
	symptr->type = constant.type;
	if (!(ltype->scalar & LONG))
	{
	    if (symptr->type == ltype)
		symptr->type = itype;
	    else if (symptr->type == ultype)
		symptr->type = uitype;
	}
	nextsym();
	return leafnode(symptr);
    case DEFINEDSYM:
	waslparen = isdefined = FALSE;
	if (!blanksident())
	{
	    nextsym();
	    if (sym != LPAREN)
		lparen();
	    else
		waslparen = TRUE;
	}
	if (waslparen && !blanksident())
	    needvarname();
	else
	{
	    if ((symptr = findlorg(gsname)) != NULL &&
		symptr->flags == DEFINITION)
		isdefined = TRUE;
	    nextsym();
	}
	if (waslparen)
	    rparen();
	return leafnode(constsym((value_t) isdefined));
    case FLOATCONST:
	symptr = constsym((value_t) 0);
	symptr->type = constant.type;
	symptr->offset.offd = qmalloc(sizeof *symptr->offset.offd);
	*symptr->offset.offd = constant.value.d;
	nextsym();
	return leafnode(symptr);
    case LPAREN:
	nextsym();
	nodeptr = expression();
	rparen();
	return nodeptr;
    case STRINGCONST:
	symptr = constsym((value_t) 0);
	symptr->storage = GLOBAL;
	symptr->flags = LABELLED | STRING;
	/* string length before defstr() or prefix() updates charptr */
	stringlen = charptr - constant.value.s + 1;
	symptr->name.label = defstr(constant.value.s, charptr, FALSE);
	symptr->type = prefix(ARRAY, stringlen, ctype);
	nextsym();
	return leafnode(symptr);
    }
}

PRIVATE struct nodestruct *unary_exp()
{
    value_t size;
    struct typestruct *vartype;

    switch (sym)
    {
    case ADDOP:
	nextsym();
	return cast_exp();
    case AMPERSAND:
	nextsym();
	return node(ADDRESSOP, cast_exp(), NULLNODE);	/* maybe unary_exp */
    case DECSYM:
	nextsym();
	return node(PREDECOP, unary_exp(), NULLNODE);
    case HYPHEN:
	nextsym();
	return node(NEGOP, cast_exp(), NULLNODE);
    case INCSYM:
	nextsym();
	return node(PREINCOP, unary_exp(), NULLNODE);
    case LOGNOTOP:
	nextsym();
	return node(LOGNOTOP, cast_exp(), NULLNODE);
    case NOTOP:
	nextsym();
	return node(NOTOP, cast_exp(), NULLNODE);
    case SIZEOFSYM:
	nextsym();
	++insizeof;
	if (sym != LPAREN)
	    size = unary_exp()->nodetype->typesize;
	else
	{
	    nextsym();
	    if ((vartype = typename()) != NULL)
	    {
		rparen();
		size = vartype->typesize;
	    }
	    else
		size = postfix_exp(TRUE)->nodetype->typesize;
	}
	--insizeof;
	return leafnode(constsym(size));
    case STAR:
	nextsym();
	return node(INDIRECTOP, cast_exp(), NULLNODE);	/* maybe unary_exp */
    }
    return postfix_exp(FALSE);
}