scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

expr.c (21606B)


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