scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

expr.c (19785B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <assert.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "../inc/cc.h"
      8 #include "../inc/sizes.h"
      9 #include "arch.h"
     10 #include "cc1.h"
     11 
     12 #define XCHG(lp, rp, np) (np = lp, lp = rp, rp = np)
     13 
     14 Node *expr(void);
     15 
     16 int
     17 cmpnode(Node *np, TUINT val)
     18 {
     19 	Symbol *sym;
     20 	Type *tp;
     21 	TUINT mask, nodeval;
     22 
     23 	if (!np || !(np->flags & NCONST) || !np->sym)
     24 		return 0;
     25 	sym = np->sym;
     26 	tp = sym->type;
     27 
     28 	switch (tp->op) {
     29 	case PTR:
     30 	case INT:
     31 		mask = (val > 1) ? ones(np->type->size) : -1;
     32 		nodeval = (tp->prop & TSIGNED) ? sym->u.i : sym->u.u;
     33 		return (nodeval & mask) == (val & mask);
     34 	case FLOAT:
     35 		return sym->u.f == val;
     36 	}
     37 	return 0;
     38 }
     39 
     40 int
     41 isnodecmp(int op)
     42 {
     43 	switch (op) {
     44 	case OEQ:
     45 	case ONE:
     46 	case OLT:
     47 	case OGE:
     48 	case OLE:
     49 	case OGT:
     50 		return 1;
     51 	default:
     52 		return 0;
     53 	}
     54 }
     55 
     56 static Node *
     57 promote(Node *np)
     58 {
     59 	Type *tp;
     60 	Node *new;
     61 	struct limits *lim, *ilim;
     62 
     63 	tp = np->type;
     64 
     65 	switch (tp->op) {
     66 	case ENUM:
     67 	case INT:
     68 		if (tp->n.rank >= inttype->n.rank)
     69 			return np;
     70 		lim = getlimits(tp);
     71 		ilim = getlimits(inttype);
     72 		tp = (lim->max.i <= ilim->max.i) ? inttype : uinttype;
     73 		break;
     74 	case FLOAT:
     75 		/* TODO: Add support for C99 float math */
     76 		tp = doubletype;
     77 		break;
     78 	default:
     79 		abort();
     80 	}
     81 	if ((new = convert(np, tp, 1)) != NULL)
     82 		return new;
     83 	return np;
     84 }
     85 
     86 static void
     87 arithconv(Node **p1, Node **p2)
     88 {
     89 	int to = 0, s1, s2;
     90 	unsigned r1, r2;
     91 	Type *tp1, *tp2;
     92 	Node *np1, *np2;
     93 	struct limits *lp1, *lp2;
     94 
     95 	np1 = promote(*p1);
     96 	np2 = promote(*p2);
     97 
     98 	tp1 = np1->type;
     99 	tp2 = np2->type;
    100 
    101 	if (tp1 == tp2)
    102 		goto set_p1_p2;
    103 
    104 	s1 = (tp1->prop & TSIGNED) != 0;
    105 	r1 = tp1->n.rank;
    106 	lp1 = getlimits(tp1);
    107 
    108 	s2 = (tp2->prop & TSIGNED) != 0;
    109 	r2 = tp2->n.rank;
    110 	lp2 = getlimits(tp2);
    111 
    112 	if (s1 == s2 || tp1->op == FLOAT || tp2->op == FLOAT) {
    113 		to = r1 - r2;
    114 	} else if (!s1) {
    115 		if (r1 >= r2 || lp1->max.i >= lp2->max.i)
    116 			to = 1;
    117 		else
    118 			to = -1;
    119 	} else {
    120 		if (r2 >= r1 || lp2->max.i >= lp1->max.i)
    121 			to = -1;
    122 		else
    123 			to = 1;
    124 	}
    125 
    126 	if (to > 0)
    127 		np2 = convert(np2, tp1, 1);
    128 	else if (to < 0)
    129 		np1 = convert(np1, tp2, 1);
    130 		
    131 set_p1_p2:
    132 	*p1 = np1;
    133 	*p2 = np2;
    134 }
    135 
    136 static int
    137 null(Node *np)
    138 {
    139 	if (!np->flags & NCONST || np->type != pvoidtype)
    140 		return 0;
    141 	return cmpnode(np, 0);
    142 }
    143 
    144 static Node *
    145 chkternary(Node *yes, Node *no)
    146 {
    147 	yes = decay(yes);
    148 	no = decay(no);
    149 
    150 	/*
    151 	 * FIXME:
    152 	 * We are ignoring type qualifiers here,
    153 	 * but the standard has strong rules about this.
    154 	 * take a look to 6.5.15
    155 	 */
    156 
    157 	if (!eqtype(yes->type, no->type)) {
    158 		if ((yes->type->prop & TARITH) && (no->type->prop & TARITH)) {
    159 			arithconv(&yes, &no);
    160 		} else if (yes->type->op != PTR && no->type->op != PTR) {
    161 			goto wrong_type;
    162 		} else {
    163 			/* convert integer 0 to NULL */
    164 			if ((yes->type->prop & TINTEGER) && cmpnode(yes, 0))
    165 				yes = convert(yes, pvoidtype, 0);
    166 			if ((no->type->prop & TINTEGER) && cmpnode(no, 0))
    167 				no = convert(no, pvoidtype, 0);
    168 			/*
    169 			 * At this point the type of both should be
    170 			 * a pointer to something, or we have don't
    171 			 * compatible types
    172 			 */
    173 			if (yes->type->op != PTR || no->type->op != PTR)
    174 				goto wrong_type;
    175 			/*
    176 			 * If we have a null pointer constant then
    177 			 * convert to the another type
    178 			 */
    179 			if (null(yes))
    180 				yes = convert(yes, no->type, 0);
    181 			if (null(no))
    182 				no = convert(no, yes->type, 0);
    183 
    184 			if (!eqtype(yes->type, no->type))
    185 				goto wrong_type;
    186 		}
    187 	}
    188 	return node(OCOLON, yes->type, yes, no);
    189 
    190 wrong_type:
    191 	errorp("type mismatch in conditional expression");
    192 	freetree(yes);
    193 	freetree(no);
    194 	return constnode(zero);
    195 }
    196 
    197 static void
    198 chklvalue(Node *np)
    199 {
    200 	if (!(np->flags & NLVAL))
    201 		errorp("lvalue required in operation");
    202 	if (np->type == voidtype)
    203 		errorp("invalid use of void expression");
    204 }
    205 
    206 Node *
    207 decay(Node *np)
    208 {
    209 	Node *new;
    210 	Type *tp = np->type;
    211 
    212 	switch (tp->op) {
    213 	case ARY:
    214 		tp = tp->type;
    215 		if (np->op == OPTR) {
    216 			new = np->left;
    217 			free(np);
    218 			new->type = mktype(tp, PTR, 0, NULL);
    219 			return new;
    220 		}
    221 	case FTN:
    222 		new = node(OADDR, mktype(tp, PTR, 0, NULL), np, NULL);
    223 		if (np->sym && np->sym->flags & (SGLOBAL|SLOCAL|SPRIVATE))
    224 			new->flags |= NCONST;
    225 		return new;
    226 	default:
    227 		return np;
    228 	}
    229 }
    230 
    231 static Node *
    232 integerop(char op, Node *lp, Node *rp)
    233 {
    234 	if (!(lp->type->prop & TINTEGER) || !(rp->type->prop & TINTEGER))
    235 		error("operator requires integer operands");
    236 	arithconv(&lp, &rp);
    237 	return simplify(op, lp->type, lp, rp);
    238 }
    239 
    240 static Node *
    241 integeruop(char op, Node *np)
    242 {
    243 	if (!(np->type->prop & TINTEGER))
    244 		error("unary operator requires integer operand");
    245 	np = promote(np);
    246 	if (op == OCPL && np->op == OCPL)
    247 		return np->left;
    248 	return simplify(op, np->type, np, NULL);
    249 }
    250 
    251 static Node *
    252 numericaluop(char op, Node *np)
    253 {
    254 	if (!(np->type->prop & TARITH))
    255 		error("unary operator requires numerical operand");
    256 	np = promote(np);
    257 	if (op == ONEG && np->op == ONEG)
    258 		return np->left;
    259 	if (op == OADD)
    260 		return np;
    261 	return simplify(op, np->type, np, NULL);
    262 }
    263 
    264 Node *
    265 convert(Node *np, Type *newtp, char iscast)
    266 {
    267 	Type *oldtp = np->type;
    268 
    269 	if (eqtype(newtp, oldtp))
    270 		return np;
    271 
    272 	switch (oldtp->op) {
    273 	case ENUM:
    274 	case INT:
    275 	case FLOAT:
    276 		switch (newtp->op) {
    277 		case PTR:
    278 			if (oldtp->op == FLOAT || !cmpnode(np, 0) && !iscast)
    279 				return NULL;
    280 			/* PASSTHROUGH */
    281 		case INT:
    282 		case FLOAT:
    283 		case ENUM:
    284 			break;
    285 		default:
    286 			return NULL;
    287 		}
    288 		break;
    289 	case PTR:
    290 		switch (newtp->op) {
    291 		case ENUM:
    292 		case INT:
    293 		case VOID:
    294 			if (!iscast)
    295 				return NULL;
    296 			break;
    297 		case PTR:
    298 			if (iscast ||
    299 			    newtp == pvoidtype || oldtp == pvoidtype) {
    300 				np->type = newtp;
    301 				return np;
    302 			}
    303 		default:
    304 			return NULL;
    305 		}
    306 	default:
    307 		return NULL;
    308 	}
    309 	return castcode(np, newtp);
    310 }
    311 
    312 static Node *
    313 parithmetic(char op, Node *lp, Node *rp)
    314 {
    315 	Type *tp;
    316 	Node *size, *np;
    317 
    318 	if (lp->type->op != PTR)
    319 		XCHG(lp, rp, np);
    320 
    321 	tp = lp->type;
    322 	size = sizeofnode(tp->type);
    323 
    324 	if (op == OSUB && BTYPE(rp) == PTR) {
    325 		if (tp != rp->type)
    326 			goto incorrect;
    327 		lp = node(OSUB, pdifftype, lp, rp);
    328 		return node(ODIV, inttype, lp, size);
    329 	}
    330 	if (!(rp->type->prop & TINTEGER))
    331 		goto incorrect;
    332 
    333 	rp = convert(promote(rp), sizettype, 0);
    334 	rp = simplify(OMUL, sizettype, rp, size);
    335 	rp = convert(rp, tp, 1);
    336 
    337 	return simplify(OADD, tp, lp, rp);
    338 
    339 incorrect:
    340 	errorp("incorrect arithmetic operands");
    341 	return node(OADD, tp, lp, rp);
    342 }
    343 
    344 static Node *
    345 arithmetic(char op, Node *lp, Node *rp)
    346 {
    347 	Type *ltp = lp->type, *rtp = rp->type;
    348 
    349 	if ((ltp->prop & TARITH) && (rtp->prop & TARITH)) {
    350 		arithconv(&lp, &rp);
    351 	} else if ((ltp->op == PTR || rtp->op == PTR) &&
    352 	           (op == OADD || op == OSUB)) {
    353 		return parithmetic(op, rp, lp);
    354 	} else if (op != OINC && op != ODEC) {
    355 		errorp("incorrect arithmetic operands");
    356 	}
    357 	return simplify(op, lp->type, lp, rp);
    358 }
    359 
    360 static Node *
    361 pcompare(char op, Node *lp, Node *rp)
    362 {
    363 	Node *np;
    364 	int err = 0;
    365 
    366 	if (lp->type->prop & TINTEGER)
    367 		XCHG(lp, rp, np);
    368 
    369 	if (rp->type->prop & TINTEGER) {
    370 		if (!cmpnode(rp, 0))
    371 			err = 1;
    372 		rp = convert(rp, pvoidtype, 1);
    373 	} else if (rp->type->op == PTR) {
    374 		if (!eqtype(lp->type, rp->type))
    375 			err = 1;
    376 	} else {
    377 		err = 1;
    378 	}
    379 	if (err)
    380 		errorp("incompatible types in comparison");
    381 	return simplify(op, inttype, lp, rp);
    382 }
    383 
    384 static Node *
    385 compare(char op, Node *lp, Node *rp)
    386 {
    387 	Type *ltp, *rtp;
    388 
    389 	lp = decay(lp);
    390 	rp = decay(rp);
    391 
    392 	ltp = lp->type;
    393 	rtp = rp->type;
    394 
    395 	if (ltp->op == PTR || rtp->op == PTR) {
    396 		return pcompare(op, rp, lp);
    397 	} else if ((ltp->prop & TARITH) && (rtp->prop & TARITH)) {
    398 		arithconv(&lp, &rp);
    399 		return simplify(op, inttype, lp, rp);
    400 	} else {
    401 		errorp("incompatible types in comparison");
    402 		freetree(lp);
    403 		freetree(rp);
    404 		return constnode(zero);
    405 	}
    406 }
    407 
    408 int
    409 negop(int op)
    410 {
    411 	switch (op) {
    412 	case OAND: return OOR;
    413 	case OOR:  return OAND;
    414 	case OEQ:  return ONE;
    415 	case ONE:  return OEQ;
    416 	case OLT:  return OGE;
    417 	case OGE:  return OLT;
    418 	case OLE:  return OGT;
    419 	case OGT:  return OLE;
    420 	default:   abort();
    421 	}
    422 	return op;
    423 }
    424 
    425 Node *
    426 negate(Node *np)
    427 {
    428 	if (np->op == OSYM) {
    429 		assert(np->flags&NCONST && np->type->prop&TINTEGER);
    430 		np->sym = (np->sym->u.i) ? zero : one;
    431 	} else {
    432 		np->op = negop(np->op);
    433 	}
    434 
    435 	return np;
    436 }
    437 
    438 static Node *
    439 exp2cond(Node *np, char neg)
    440 {
    441 	np = decay(np);
    442 	if (np->type->prop & TAGGREG) {
    443 		errorp("used struct/union type value where scalar is required");
    444 		np = constnode(zero);
    445 	}
    446 	if (isnodecmp(np->op))
    447 		return (neg) ? negate(np) : np;
    448 	return compare((neg) ?  OEQ : ONE, np, constnode(zero));
    449 }
    450 
    451 static Node *
    452 logic(char op, Node *lp, Node *rp)
    453 {
    454 	lp = exp2cond(lp, 0);
    455 	rp = exp2cond(rp, 0);
    456 	return simplify(op, inttype, lp, rp);
    457 }
    458 
    459 static Node *
    460 field(Node *np)
    461 {
    462 	Symbol *sym;
    463 
    464 	namespace = np->type->ns;
    465 	next();
    466 	namespace = NS_IDEN;
    467 
    468 	sym = yylval.sym;
    469 	if (yytoken != IDEN)
    470 		unexpected();
    471 	next();
    472 
    473 	if (!(np->type->prop & TAGGREG)) {
    474 		errorp("request for member '%s' in something not a structure or union",
    475 		      yylval.sym->name);
    476 		goto free_np;
    477 	}
    478 	if ((sym->flags & SDECLARED) == 0) {
    479 		errorp("incorrect field in struct/union");
    480 		goto free_np;
    481 	}
    482 	np = node(OFIELD, sym->type, np, varnode(sym));
    483 	np->flags |= NLVAL;
    484 	return np;
    485 
    486 free_np:
    487 	freetree(np);
    488 	return constnode(zero);
    489 }
    490 
    491 static Node *
    492 content(char op, Node *np)
    493 {
    494 	np = decay(np);
    495 	switch (BTYPE(np)) {
    496 	case ARY:
    497 	case FTN:
    498 	case PTR:
    499 		if (np->op == OADDR) {
    500 			Node *new = np->left;
    501 			new->type = np->type->type;
    502 			free(np);
    503 			np = new;
    504 		} else {
    505 			np = node(op, np->type->type, np, NULL);
    506 		}
    507 		np->flags |= NLVAL;
    508 		return np;
    509 	default:
    510 		error("invalid argument of memory indirection");
    511 	}
    512 }
    513 
    514 static Node *
    515 array(Node *lp, Node *rp)
    516 {
    517 	Type *tp;
    518 	Node *np;
    519 
    520 	if (!(lp->type->prop & TINTEGER) && !(rp->type->prop & TINTEGER))
    521 		error("array subscript is not an integer");
    522 	np = arithmetic(OADD, decay(lp), decay(rp));
    523 	tp = np->type;
    524 	if (tp->op != PTR)
    525 		errorp("subscripted value is neither array nor pointer");
    526 	return content(OPTR, np);
    527 }
    528 
    529 static Node *
    530 assignop(char op, Node *lp, Node *rp)
    531 {
    532 	if ((rp = convert(decay(rp), lp->type, 0)) == NULL) {
    533 		errorp("incompatible types when assigning");
    534 		return lp;
    535 	}
    536 
    537 	return node(op, lp->type, lp, rp);
    538 }
    539 
    540 static Node *
    541 incdec(Node *np, char op)
    542 {
    543 	Type *tp = np->type;
    544 	Node *inc;
    545 
    546 	chklvalue(np);
    547 	np->flags |= NEFFECT;
    548 
    549 	if (!(tp->prop & TDEFINED)) {
    550 		errorp("invalid use of undefined type");
    551 		return np;
    552 	} else if (tp->prop & TARITH) {
    553 		inc = constnode(one);
    554 	} else if (tp->op == PTR) {
    555 		inc = sizeofnode(tp->type);
    556 	} else {
    557 		errorp("wrong type argument to increment or decrement");
    558 		return np;
    559 	}
    560 	return arithmetic(op, np, inc);
    561 }
    562 
    563 static Node *
    564 address(char op, Node *np)
    565 {
    566 	Node *new;
    567 
    568 	if (BTYPE(np) != FTN) {
    569 		chklvalue(np);
    570 		if (np->sym && (np->sym->flags & SREGISTER))
    571 			errorp("address of register variable '%s' requested", yytext);
    572 		if (np->op == OPTR) {
    573 			Node *new = np->left;
    574 			free(np);
    575 			return new;
    576 		}
    577 	}
    578 	new = node(op, mktype(np->type, PTR, 0, NULL), np, NULL);
    579 
    580 	if (np->sym && np->sym->flags & (SGLOBAL|SLOCAL|SPRIVATE))
    581 		new->flags |= NCONST;
    582 	return new;
    583 }
    584 
    585 static Node *
    586 negation(char op, Node *np)
    587 {
    588 	np = decay(np);
    589 	if (!(np->type->prop & TARITH) && np->type->op != PTR) {
    590 		errorp("invalid argument of unary '!'");
    591 		freetree(np);
    592 		return constnode(zero);
    593 	}
    594 	return exp2cond(np, 1);
    595 }
    596 
    597 static Symbol *
    598 notdefined(Symbol *sym)
    599 {
    600 	int isdef;
    601 
    602 	if (namespace == NS_CPP && !strcmp(sym->name, "defined")) {
    603 		disexpand = 1;
    604 		next();
    605 		expect('(');
    606 		sym = yylval.sym;
    607 		expect(IDEN);
    608 		expect(')');
    609 
    610 		isdef = (sym->flags & SDECLARED) != 0;
    611 		sym = newsym(NS_IDEN);
    612 		sym->type = inttype;
    613 		sym->flags |= SCONSTANT;
    614 		sym->u.i = isdef;
    615 		disexpand = 0;
    616 		return sym;
    617 	}
    618 	errorp("'%s' undeclared", yytext);
    619 	sym->type = inttype;
    620 	return install(sym->ns, yylval.sym);
    621 }
    622 
    623 /*************************************************************
    624  * grammar functions                                         *
    625  *************************************************************/
    626 static Node *
    627 primary(void)
    628 {
    629 	Node *np;
    630 	Symbol *sym;
    631 
    632 	sym = yylval.sym;
    633 	switch (yytoken) {
    634 	case STRING:
    635 		np = constnode(sym);
    636 		sym->flags |= SHASINIT;
    637 		emit(ODECL, sym);
    638 		emit(OINIT, np);
    639 		np = decay(varnode(sym));
    640 		next();
    641 		break;
    642 	case CONSTANT:
    643 		np = constnode(sym);
    644 		next();
    645 		break;
    646 	case IDEN:
    647 		if ((sym->flags & SDECLARED) == 0)
    648 			sym = notdefined(sym);
    649 		if (sym->flags & SCONSTANT) {
    650 			np = constnode(sym);
    651 			break;
    652 		}
    653 		sym->flags |= SUSED;
    654 		np = varnode(sym);
    655 		next();
    656 		break;
    657 	default:
    658 		unexpected();
    659 	}
    660 	return np;
    661 }
    662 
    663 static Node *
    664 arguments(Node *np)
    665 {
    666 	int toomany, n;
    667 	Node *par = NULL, *arg;
    668 	Type *argtype, **targs, *tp = np->type, *rettype;
    669 
    670 	if (tp->op == PTR && tp->type->op == FTN) {
    671 		np = content(OPTR, np);
    672 		tp = np->type;
    673 	}
    674 	if (tp->op != FTN) {
    675 		targs = (Type *[]) {ellipsistype};
    676 		n = 1;
    677 		rettype = inttype;
    678 		errorp("function or function pointer expected");
    679 	} else {
    680 		targs = tp->p.pars;
    681 		n = tp->n.elem;
    682 		rettype = tp->type;
    683 	}
    684 
    685 	expect('(');
    686 	if (yytoken == ')')
    687 		goto no_pars;
    688 	toomany = 0;
    689 
    690 	do {
    691 		arg = decay(assign());
    692 		argtype = *targs;
    693 		if (argtype == ellipsistype) {
    694 			n = 0;
    695 			switch (arg->type->op) {
    696 			case INT:
    697 				arg = promote(arg);
    698 				break;
    699 			case FLOAT:
    700 				if (arg->type == floattype)
    701 					arg = convert(arg, doubletype, 1);
    702 				break;
    703 			}
    704 			par = node(OPAR, arg->type, par, arg);
    705 			continue;
    706 		}
    707 		if (--n < 0) {
    708 			if (!toomany)
    709 				errorp("too many arguments in function call");
    710 			toomany = 1;
    711 			continue;
    712 		}
    713 		++targs;
    714 		if ((arg = convert(arg, argtype, 0)) != NULL) {
    715 			par = node(OPAR, arg->type, par, arg);
    716 			continue;
    717 		}
    718 		errorp("incompatible type for argument %d in function call",
    719 		       tp->n.elem - n + 1);
    720 	} while (accept(','));
    721 
    722 no_pars:
    723 	expect(')');
    724 	if (n > 0 && *targs != ellipsistype)
    725 		errorp("too few arguments in function call");
    726 
    727 	return node(OCALL, rettype, np, par);
    728 }
    729 
    730 static Node *
    731 postfix(Node *lp)
    732 {
    733 	Node *rp;
    734 
    735 	if (!lp)
    736 		lp = primary();
    737 	for (;;) {
    738 		switch (yytoken) {
    739 		case '[':
    740 			next();
    741 			rp = expr();
    742 			lp = array(lp, rp);
    743 			expect(']');
    744 			break;
    745 		case DEC:
    746 		case INC:
    747 			lp = incdec(lp, (yytoken == INC) ? OINC : ODEC);
    748 			next();
    749 			break;
    750 		case INDIR:
    751 			lp = content(OPTR, lp);
    752 		case '.':
    753 			lp = field(lp);
    754 			break;
    755 		case '(':
    756 			lp = arguments(lp);
    757 			lp->flags |= NEFFECT;
    758 			break;
    759 		default:
    760 			return lp;
    761 		}
    762 	}
    763 }
    764 
    765 static Node *unary(void);
    766 
    767 static Type *
    768 typeof(Node *np)
    769 {
    770 	Type *tp;
    771 
    772 	if (np == NULL)
    773 		unexpected();
    774 	tp = np->type;
    775 	freetree(np);
    776 	return tp;
    777 }
    778 
    779 static Type *
    780 sizeexp(void)
    781 {
    782 	Type *tp;
    783 
    784 	expect('(');
    785 	switch (yytoken) {
    786 	case TYPE:
    787 	case TYPEIDEN:
    788 		tp = typename();
    789 		break;
    790 	default:
    791 		tp = typeof(unary());
    792 		break;
    793 	}
    794 	expect(')');
    795 	return tp;
    796 }
    797 
    798 static Node *cast(void);
    799 
    800 static Node *
    801 unary(void)
    802 {
    803 	Node *(*fun)(char, Node *);
    804 	char op;
    805 	Type *tp;
    806 
    807 	switch (yytoken) {
    808 	case SIZEOF:
    809 		next();
    810 		tp = (yytoken == '(') ? sizeexp() : typeof(unary());
    811 		if (!(tp->prop & TDEFINED))
    812 			errorp("sizeof applied to an incomplete type");
    813 		return sizeofnode(tp);
    814 	case INC:
    815 	case DEC:
    816 		op = (yytoken == INC) ? OA_ADD : OA_SUB;
    817 		next();
    818 		return incdec(unary(), op);
    819 	case '!': op = 0;     fun = negation;     break;
    820 	case '+': op = OADD;  fun = numericaluop; break;
    821 	case '-': op = ONEG;  fun = numericaluop; break;
    822 	case '~': op = OCPL;  fun = integeruop;   break;
    823 	case '&': op = OADDR; fun = address;      break;
    824 	case '*': op = OPTR;  fun = content;      break;
    825 	default:  return postfix(NULL);
    826 	}
    827 
    828 	next();
    829 	return (*fun)(op, cast());
    830 }
    831 
    832 static Node *
    833 cast(void)
    834 {
    835 	Node *lp, *rp;
    836 	Type *tp;
    837 	static int nested;
    838 
    839 	if (!accept('('))
    840 		return unary();
    841 
    842 	switch (yytoken) {
    843 	case TQUALIFIER:
    844 	case TYPE:
    845 		tp = typename();
    846 		expect(')');
    847 
    848 		if (yytoken == '{')
    849 			return initlist(tp);
    850 
    851 		switch (tp->op) {
    852 		case ARY:
    853 			error("cast specifies an array type");
    854 		default:
    855 			lp = cast();
    856 			if ((rp = convert(lp,  tp, 1)) == NULL)
    857 				error("bad type conversion requested");
    858 			rp->flags &= ~NLVAL;
    859 			rp->flags |= lp->flags & NLVAL;
    860 		}
    861 		break;
    862 	default:
    863 		if (nested == NR_SUBEXPR)
    864 			error("too many expressions nested by parentheses");
    865 		++nested;
    866 		rp = expr();
    867 		--nested;
    868 		expect(')');
    869 		rp = postfix(rp);
    870 		break;
    871 	}
    872 
    873 	return rp;
    874 }
    875 
    876 static Node *
    877 mul(void)
    878 {
    879 	Node *np, *(*fun)(char, Node *, Node *);
    880 	char op;
    881 
    882 	np = cast();
    883 	for (;;) {
    884 		switch (yytoken) {
    885 		case '*': op = OMUL; fun = arithmetic; break;
    886 		case '/': op = ODIV; fun = arithmetic; break;
    887 		case '%': op = OMOD; fun = integerop;  break;
    888 		default: return np;
    889 		}
    890 		next();
    891 		np = (*fun)(op, np, cast());
    892 	}
    893 }
    894 
    895 static Node *
    896 add(void)
    897 {
    898 	char op;
    899 	Node *np;
    900 
    901 	np = mul();
    902 	for (;;) {
    903 		switch (yytoken) {
    904 		case '+': op = OADD; break;
    905 		case '-': op = OSUB; break;
    906 		default:  return np;
    907 		}
    908 		next();
    909 		np = arithmetic(op, np, mul());
    910 	}
    911 }
    912 
    913 static Node *
    914 shift(void)
    915 {
    916 	char op;
    917 	Node *np;
    918 
    919 	np = add();
    920 	for (;;) {
    921 		switch (yytoken) {
    922 		case SHL: op = OSHL; break;
    923 		case SHR: op = OSHR; break;
    924 		default:  return np;
    925 		}
    926 		next();
    927 		np = integerop(op, np, add());
    928 	}
    929 }
    930 
    931 static Node *
    932 relational(void)
    933 {
    934 	char op;
    935 	Node *np;
    936 
    937 	np = shift();
    938 	for (;;) {
    939 		switch (yytoken) {
    940 		case '<': op = OLT; break;
    941 		case '>': op = OGT; break;
    942 		case GE:  op = OGE; break;
    943 		case LE:  op = OLE; break;
    944 		default:  return np;
    945 		}
    946 		next();
    947 		np = compare(op, np, shift());
    948 	}
    949 }
    950 
    951 static Node *
    952 eq(void)
    953 {
    954 	char op;
    955 	Node *np;
    956 
    957 	np = relational();
    958 	for (;;) {
    959 		switch (yytoken) {
    960 		case EQ: op = OEQ; break;
    961 		case NE: op = ONE; break;
    962 		default: return np;
    963 		}
    964 		next();
    965 		np = compare(op, np, relational());
    966 	}
    967 }
    968 
    969 static Node *
    970 bit_and(void)
    971 {
    972 	Node *np;
    973 
    974 	np = eq();
    975 	while (accept('&'))
    976 		np = integerop(OBAND, np, eq());
    977 	return np;
    978 }
    979 
    980 static Node *
    981 bit_xor(void)
    982 {
    983 	Node *np;
    984 
    985 	np = bit_and();
    986 	while (accept('^'))
    987 		np = integerop(OBXOR,  np, bit_and());
    988 	return np;
    989 }
    990 
    991 static Node *
    992 bit_or(void)
    993 {
    994 	Node *np;
    995 
    996 	np = bit_xor();
    997 	while (accept('|'))
    998 		np = integerop(OBOR, np, bit_xor());
    999 	return np;
   1000 }
   1001 
   1002 static Node *
   1003 and(void)
   1004 {
   1005 	Node *np;
   1006 
   1007 	np = bit_or();
   1008 	while (accept(AND))
   1009 		np = logic(OAND, np, bit_or());
   1010 	return np;
   1011 }
   1012 
   1013 static Node *
   1014 or(void)
   1015 {
   1016 	Node *np;
   1017 
   1018 	np = and();
   1019 	while (accept(OR))
   1020 		np = logic(OOR, np, and());
   1021 	return np;
   1022 }
   1023 
   1024 static Node *
   1025 ternary(void)
   1026 {
   1027 	Node *cond;
   1028 
   1029 	cond = or();
   1030 	while (accept('?')) {
   1031 		Node *ifyes, *ifno, *np;
   1032 
   1033 		cond = exp2cond(cond, 0);
   1034 		ifyes = expr();
   1035 		expect(':');
   1036 		ifno = ternary();
   1037 		np = chkternary(ifyes, ifno);
   1038 		cond = simplify(OASK, np->type, cond, np);
   1039 	}
   1040 	return cond;
   1041 }
   1042 
   1043 Node *
   1044 assign(void)
   1045 {
   1046 	Node *np, *(*fun)(char , Node *, Node *);
   1047 	char op;
   1048 
   1049 	np = ternary();
   1050 	for (;;) {
   1051 		switch (yytoken) {
   1052 		case '=':    op = OASSIGN; fun = assignop;   break;
   1053 		case MUL_EQ: op = OA_MUL;  fun = arithmetic; break;
   1054 		case DIV_EQ: op = OA_DIV;  fun = arithmetic; break;
   1055 		case MOD_EQ: op = OA_MOD;  fun = integerop;  break;
   1056 		case ADD_EQ: op = OA_ADD;  fun = arithmetic; break;
   1057 		case SUB_EQ: op = OA_SUB;  fun = arithmetic; break;
   1058 		case SHL_EQ: op = OA_SHL;  fun = integerop;  break;
   1059 		case SHR_EQ: op = OA_SHR;  fun = integerop;  break;
   1060 		case AND_EQ: op = OA_AND;  fun = integerop;  break;
   1061 		case XOR_EQ: op = OA_XOR;  fun = integerop;  break;
   1062 		case OR_EQ:  op = OA_OR;   fun = integerop;  break;
   1063 		default: return np;
   1064 		}
   1065 		chklvalue(np);
   1066 		np->flags |= NEFFECT;
   1067 		next();
   1068 		np = (fun)(op, np, assign());
   1069 	}
   1070 }
   1071 
   1072 Node *
   1073 constexpr(void)
   1074 {
   1075 	Node *np;
   1076 
   1077 	np = ternary();
   1078 	if (!(np->flags & NCONST)) {
   1079 		freetree(np);
   1080 		return NULL;
   1081 	}
   1082 	return np;
   1083 }
   1084 
   1085 Node *
   1086 iconstexpr(void)
   1087 {
   1088 	Node *np;
   1089 
   1090 	if ((np = constexpr()) == NULL)
   1091 		return NULL;
   1092 
   1093 	if (np->type->op != INT) {
   1094 		freetree(np);
   1095 		return NULL;
   1096 	}
   1097 
   1098 	return convert(np, inttype, 0);
   1099 }
   1100 
   1101 Node *
   1102 expr(void)
   1103 {
   1104 	Node *lp, *rp;
   1105 
   1106 	lp = assign();
   1107 	while (accept(',')) {
   1108 		rp = assign();
   1109 		lp = node(OCOMMA, rp->type, lp, rp);
   1110 	}
   1111 
   1112 	return lp;
   1113 }
   1114 
   1115 Node *
   1116 condexpr(void)
   1117 {
   1118 	Node *np;
   1119 
   1120 	np = exp2cond(expr(), 0);
   1121 	if (np->flags & NCONST)
   1122 		warn("conditional expression is constant");
   1123 	return np;
   1124 }