scc

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

expr.c (21830B)


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