github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/cc/com.c (about)

     1  // Inferno utils/cc/com.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/cc/com.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors.  All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  #include <u.h>
    32  #include "cc.h"
    33  
    34  int compar(Node*, int);
    35  
    36  void
    37  complex(Node *n)
    38  {
    39  
    40  	if(n == Z)
    41  		return;
    42  
    43  	nearln = n->lineno;
    44  	if(debug['t'])
    45  		if(n->op != OCONST)
    46  			prtree(n, "pre complex");
    47  	if(tcom(n))
    48  		return;
    49  	if(debug['t'])
    50  		if(n->op != OCONST)
    51  			prtree(n, "t complex");
    52  	ccom(n);
    53  	if(debug['t'])
    54  		if(n->op != OCONST)
    55  			prtree(n, "c complex");
    56  	acom(n);
    57  	if(debug['t'])
    58  		if(n->op != OCONST)
    59  			prtree(n, "a complex");
    60  	xcom(n);
    61  	if(debug['t'])
    62  		if(n->op != OCONST)
    63  			prtree(n, "x complex");
    64  }
    65  
    66  /*
    67   * evaluate types
    68   * evaluate lvalues (addable == 1)
    69   */
    70  enum
    71  {
    72  	ADDROF	= 1<<0,
    73  	CASTOF	= 1<<1,
    74  	ADDROP	= 1<<2,
    75  };
    76  
    77  int
    78  tcom(Node *n)
    79  {
    80  
    81  	return tcomo(n, ADDROF);
    82  }
    83  
    84  int
    85  tcomo(Node *n, int f)
    86  {
    87  	Node *l, *r;
    88  	Type *t;
    89  	int o;
    90  
    91  	if(n == Z) {
    92  		diag(Z, "Z in tcom");
    93  		errorexit();
    94  	}
    95  	n->addable = 0;
    96  	l = n->left;
    97  	r = n->right;
    98  
    99  	switch(n->op) {
   100  	default:
   101  		diag(n, "unknown op in type complex: %O", n->op);
   102  		goto bad;
   103  
   104  	case ODOTDOT:
   105  		/*
   106  		 * tcom has already been called on this subtree
   107  		 */
   108  		*n = *n->left;
   109  		if(n->type == T)
   110  			goto bad;
   111  		break;
   112  
   113  	case OCAST:
   114  		if(n->type == T)
   115  			break;
   116  		if(n->type->width == types[TLONG]->width) {
   117  			if(tcomo(l, ADDROF|CASTOF))
   118  				goto bad;
   119  		} else
   120  			if(tcom(l))
   121  				goto bad;
   122  		if(isfunct(n))
   123  			break;
   124  		if(tcompat(n, l->type, n->type, tcast))
   125  			goto bad;
   126  		break;
   127  
   128  	case ORETURN:
   129  		if(l == Z) {
   130  			if(n->type->etype != TVOID)
   131  				diag(n, "null return of a typed function");
   132  			break;
   133  		}
   134  		if(tcom(l))
   135  			goto bad;
   136  		typeext(n->type, l);
   137  		if(tcompat(n, n->type, l->type, tasign))
   138  			break;
   139  		constas(n, n->type, l->type);
   140  		if(!sametype(n->type, l->type)) {
   141  			l = new1(OCAST, l, Z);
   142  			l->type = n->type;
   143  			n->left = l;
   144  		}
   145  		break;
   146  
   147  	case OASI:	/* same as as, but no test for const */
   148  		n->op = OAS;
   149  		o = tcom(l);
   150  		if(o | tcom(r))
   151  			goto bad;
   152  
   153  		typeext(l->type, r);
   154  		if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
   155  			goto bad;
   156  		if(!sametype(l->type, r->type)) {
   157  			r = new1(OCAST, r, Z);
   158  			r->type = l->type;
   159  			n->right = r;
   160  		}
   161  		n->type = l->type;
   162  		break;
   163  
   164  	case OAS:
   165  		o = tcom(l);
   166  		if(o | tcom(r))
   167  			goto bad;
   168  		if(tlvalue(l))
   169  			goto bad;
   170  		if(isfunct(n))
   171  			break;
   172  		typeext(l->type, r);
   173  		if(tcompat(n, l->type, r->type, tasign))
   174  			goto bad;
   175  		constas(n, l->type, r->type);
   176  		if(!sametype(l->type, r->type)) {
   177  			r = new1(OCAST, r, Z);
   178  			r->type = l->type;
   179  			n->right = r;
   180  		}
   181  		n->type = l->type;
   182  		break;
   183  
   184  	case OASADD:
   185  	case OASSUB:
   186  		o = tcom(l);
   187  		if(o | tcom(r))
   188  			goto bad;
   189  		if(tlvalue(l))
   190  			goto bad;
   191  		if(isfunct(n))
   192  			break;
   193  		typeext1(l->type, r);
   194  		if(tcompat(n, l->type, r->type, tasadd))
   195  			goto bad;
   196  		constas(n, l->type, r->type);
   197  		t = l->type;
   198  		arith(n, 0);
   199  		while(n->left->op == OCAST)
   200  			n->left = n->left->left;
   201  		if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
   202  			r = new1(OCAST, n->right, Z);
   203  			r->type = t;
   204  			n->right = r;
   205  			n->type = t;
   206  		}
   207  		break;
   208  
   209  	case OASMUL:
   210  	case OASLMUL:
   211  	case OASDIV:
   212  	case OASLDIV:
   213  		o = tcom(l);
   214  		if(o | tcom(r))
   215  			goto bad;
   216  		if(tlvalue(l))
   217  			goto bad;
   218  		if(isfunct(n))
   219  			break;
   220  		typeext1(l->type, r);
   221  		if(tcompat(n, l->type, r->type, tmul))
   222  			goto bad;
   223  		constas(n, l->type, r->type);
   224  		t = l->type;
   225  		arith(n, 0);
   226  		while(n->left->op == OCAST)
   227  			n->left = n->left->left;
   228  		if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
   229  			r = new1(OCAST, n->right, Z);
   230  			r->type = t;
   231  			n->right = r;
   232  			n->type = t;
   233  		}
   234  		if(typeu[n->type->etype]) {
   235  			if(n->op == OASDIV)
   236  				n->op = OASLDIV;
   237  			if(n->op == OASMUL)
   238  				n->op = OASLMUL;
   239  		}
   240  		break;
   241  
   242  	case OASLSHR:
   243  	case OASASHR:
   244  	case OASASHL:
   245  		o = tcom(l);
   246  		if(o | tcom(r))
   247  			goto bad;
   248  		if(tlvalue(l))
   249  			goto bad;
   250  		if(isfunct(n))
   251  			break;
   252  		if(tcompat(n, l->type, r->type, tand))
   253  			goto bad;
   254  		n->type = l->type;
   255  		if(typeu[n->type->etype]) {
   256  			if(n->op == OASASHR)
   257  				n->op = OASLSHR;
   258  		}
   259  		break;
   260  
   261  	case OASMOD:
   262  	case OASLMOD:
   263  	case OASOR:
   264  	case OASAND:
   265  	case OASXOR:
   266  		o = tcom(l);
   267  		if(o | tcom(r))
   268  			goto bad;
   269  		if(tlvalue(l))
   270  			goto bad;
   271  		if(isfunct(n))
   272  			break;
   273  		if(tcompat(n, l->type, r->type, tand))
   274  			goto bad;
   275  		t = l->type;
   276  		arith(n, 0);
   277  		while(n->left->op == OCAST)
   278  			n->left = n->left->left;
   279  		if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
   280  			r = new1(OCAST, n->right, Z);
   281  			r->type = t;
   282  			n->right = r;
   283  			n->type = t;
   284  		}
   285  		if(typeu[n->type->etype]) {
   286  			if(n->op == OASMOD)
   287  				n->op = OASLMOD;
   288  		}
   289  		break;
   290  
   291  	case OPREINC:
   292  	case OPREDEC:
   293  	case OPOSTINC:
   294  	case OPOSTDEC:
   295  		if(tcom(l))
   296  			goto bad;
   297  		if(tlvalue(l))
   298  			goto bad;
   299  		if(isfunct(n))
   300  			break;
   301  		if(tcompat(n, l->type, types[TINT], tadd))
   302  			goto bad;
   303  		n->type = l->type;
   304  		if(n->type->etype == TIND)
   305  		if(n->type->link->width < 1)
   306  			diag(n, "inc/dec of a void pointer");
   307  		break;
   308  
   309  	case OEQ:
   310  	case ONE:
   311  		o = tcom(l);
   312  		if(o | tcom(r))
   313  			goto bad;
   314  		if(isfunct(n))
   315  			break;
   316  		typeext(l->type, r);
   317  		typeext(r->type, l);
   318  		if(tcompat(n, l->type, r->type, trel))
   319  			goto bad;
   320  		arith(n, 0);
   321  		n->type = types[TINT];
   322  		break;
   323  
   324  	case OLT:
   325  	case OGE:
   326  	case OGT:
   327  	case OLE:
   328  		o = tcom(l);
   329  		if(o | tcom(r))
   330  			goto bad;
   331  		if(isfunct(n))
   332  			break;
   333  		typeext1(l->type, r);
   334  		typeext1(r->type, l);
   335  		if(tcompat(n, l->type, r->type, trel))
   336  			goto bad;
   337  		arith(n, 0);
   338  		if(typeu[n->type->etype])
   339  			n->op = logrel[relindex(n->op)];
   340  		n->type = types[TINT];
   341  		break;
   342  
   343  	case OCOND:
   344  		o = tcom(l);
   345  		o |= tcom(r->left);
   346  		if(o | tcom(r->right))
   347  			goto bad;
   348  		if(r->right->type->etype == TIND && vconst(r->left) == 0) {
   349  			r->left->type = r->right->type;
   350  			r->left->vconst = 0;
   351  		}
   352  		if(r->left->type->etype == TIND && vconst(r->right) == 0) {
   353  			r->right->type = r->left->type;
   354  			r->right->vconst = 0;
   355  		}
   356  		if(sametype(r->right->type, r->left->type)) {
   357  			r->type = r->right->type;
   358  			n->type = r->type;
   359  			break;
   360  		}
   361  		if(tcompat(r, r->left->type, r->right->type, trel))
   362  			goto bad;
   363  		arith(r, 0);
   364  		n->type = r->type;
   365  		break;
   366  
   367  	case OADD:
   368  		o = tcom(l);
   369  		if(o | tcom(r))
   370  			goto bad;
   371  		if(isfunct(n))
   372  			break;
   373  		if(tcompat(n, l->type, r->type, tadd))
   374  			goto bad;
   375  		arith(n, 1);
   376  		break;
   377  
   378  	case OSUB:
   379  		o = tcom(l);
   380  		if(o | tcom(r))
   381  			goto bad;
   382  		if(isfunct(n))
   383  			break;
   384  		if(tcompat(n, l->type, r->type, tsub))
   385  			goto bad;
   386  		arith(n, 1);
   387  		break;
   388  
   389  	case OMUL:
   390  	case OLMUL:
   391  	case ODIV:
   392  	case OLDIV:
   393  		o = tcom(l);
   394  		if(o | tcom(r))
   395  			goto bad;
   396  		if(isfunct(n))
   397  			break;
   398  		if(tcompat(n, l->type, r->type, tmul))
   399  			goto bad;
   400  		arith(n, 1);
   401  		if(typeu[n->type->etype]) {
   402  			if(n->op == ODIV)
   403  				n->op = OLDIV;
   404  			if(n->op == OMUL)
   405  				n->op = OLMUL;
   406  		}
   407  		break;
   408  
   409  	case OLSHR:
   410  	case OASHL:
   411  	case OASHR:
   412  		o = tcom(l);
   413  		if(o | tcom(r))
   414  			goto bad;
   415  		if(isfunct(n))
   416  			break;
   417  		if(tcompat(n, l->type, r->type, tand))
   418  			goto bad;
   419  		n->right = Z;
   420  		arith(n, 1);
   421  		n->right = new1(OCAST, r, Z);
   422  		n->right->type = types[TINT];
   423  		if(typeu[n->type->etype])
   424  			if(n->op == OASHR)
   425  				n->op = OLSHR;
   426  		break;
   427  
   428  	case OAND:
   429  	case OOR:
   430  	case OXOR:
   431  		o = tcom(l);
   432  		if(o | tcom(r))
   433  			goto bad;
   434  		if(isfunct(n))
   435  			break;
   436  		if(tcompat(n, l->type, r->type, tand))
   437  			goto bad;
   438  		arith(n, 1);
   439  		break;
   440  
   441  	case OMOD:
   442  	case OLMOD:
   443  		o = tcom(l);
   444  		if(o | tcom(r))
   445  			goto bad;
   446  		if(isfunct(n))
   447  			break;
   448  		if(tcompat(n, l->type, r->type, tand))
   449  			goto bad;
   450  		arith(n, 1);
   451  		if(typeu[n->type->etype])
   452  			n->op = OLMOD;
   453  		break;
   454  
   455  	case OPOS:
   456  		if(tcom(l))
   457  			goto bad;
   458  		if(isfunct(n))
   459  			break;
   460  
   461  		r = l;
   462  		l = new(OCONST, Z, Z);
   463  		l->vconst = 0;
   464  		l->type = types[TINT];
   465  		n->op = OADD;
   466  		n->right = r;
   467  		n->left = l;
   468  
   469  		if(tcom(l))
   470  			goto bad;
   471  		if(tcompat(n, l->type, r->type, tsub))
   472  			goto bad;
   473  		arith(n, 1);
   474  		break;
   475  
   476  	case ONEG:
   477  		if(tcom(l))
   478  			goto bad;
   479  		if(isfunct(n))
   480  			break;
   481  
   482  		if(!machcap(n)) {
   483  			r = l;
   484  			l = new(OCONST, Z, Z);
   485  			l->vconst = 0;
   486  			l->type = types[TINT];
   487  			n->op = OSUB;
   488  			n->right = r;
   489  			n->left = l;
   490  
   491  			if(tcom(l))
   492  				goto bad;
   493  			if(tcompat(n, l->type, r->type, tsub))
   494  				goto bad;
   495  		}
   496  		arith(n, 1);
   497  		break;
   498  
   499  	case OCOM:
   500  		if(tcom(l))
   501  			goto bad;
   502  		if(isfunct(n))
   503  			break;
   504  
   505  		if(!machcap(n)) {
   506  			r = l;
   507  			l = new(OCONST, Z, Z);
   508  			l->vconst = -1;
   509  			l->type = types[TINT];
   510  			n->op = OXOR;
   511  			n->right = r;
   512  			n->left = l;
   513  
   514  			if(tcom(l))
   515  				goto bad;
   516  			if(tcompat(n, l->type, r->type, tand))
   517  				goto bad;
   518  		}
   519  		arith(n, 1);
   520  		break;
   521  
   522  	case ONOT:
   523  		if(tcom(l))
   524  			goto bad;
   525  		if(isfunct(n))
   526  			break;
   527  		if(tcompat(n, T, l->type, tnot))
   528  			goto bad;
   529  		n->type = types[TINT];
   530  		break;
   531  
   532  	case OANDAND:
   533  	case OOROR:
   534  		o = tcom(l);
   535  		if(o | tcom(r))
   536  			goto bad;
   537  		if(tcompat(n, T, l->type, tnot) |
   538  		   tcompat(n, T, r->type, tnot))
   539  			goto bad;
   540  		n->type = types[TINT];
   541  		break;
   542  
   543  	case OCOMMA:
   544  		o = tcom(l);
   545  		if(o | tcom(r))
   546  			goto bad;
   547  		n->type = r->type;
   548  		break;
   549  
   550  
   551  	case OSIGN:	/* extension signof(type) returns a hash */
   552  		if(l != Z) {
   553  			if(l->op != OSTRING && l->op != OLSTRING)
   554  				if(tcomo(l, 0))
   555  					goto bad;
   556  			if(l->op == OBIT) {
   557  				diag(n, "signof bitfield");
   558  				goto bad;
   559  			}
   560  			n->type = l->type;
   561  		}
   562  		if(n->type == T)
   563  			goto bad;
   564  		if(n->type->width < 0) {
   565  			diag(n, "signof undefined type");
   566  			goto bad;
   567  		}
   568  		n->op = OCONST;
   569  		n->left = Z;
   570  		n->right = Z;
   571  		n->vconst = convvtox(signature(n->type), TULONG);
   572  		n->type = types[TULONG];
   573  		break;
   574  
   575  	case OSIZE:
   576  		if(l != Z) {
   577  			if(l->op != OSTRING && l->op != OLSTRING)
   578  				if(tcomo(l, 0))
   579  					goto bad;
   580  			if(l->op == OBIT) {
   581  				diag(n, "sizeof bitfield");
   582  				goto bad;
   583  			}
   584  			n->type = l->type;
   585  		}
   586  		if(n->type == T)
   587  			goto bad;
   588  		if(n->type->width <= 0) {
   589  			diag(n, "sizeof undefined type");
   590  			goto bad;
   591  		}
   592  		if(n->type->etype == TFUNC) {
   593  			diag(n, "sizeof function");
   594  			goto bad;
   595  		}
   596  		n->op = OCONST;
   597  		n->left = Z;
   598  		n->right = Z;
   599  		n->vconst = convvtox(n->type->width, TINT);
   600  		n->type = types[TINT];
   601  		break;
   602  
   603  	case OFUNC:
   604  		o = tcomo(l, 0);
   605  		if(o)
   606  			goto bad;
   607  		if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
   608  			l = new1(OIND, l, Z);
   609  			l->type = l->left->type->link;
   610  			n->left = l;
   611  		}
   612  		if(tcompat(n, T, l->type, tfunct))
   613  			goto bad;
   614  		if(o | tcoma(l, r, l->type->down, 1))
   615  			goto bad;
   616  		n->type = l->type->link;
   617  		if(!debug['B'])
   618  			if(l->type->down == T || l->type->down->etype == TOLD) {
   619  				nerrors--;
   620  				diag(n, "function args not checked: %F", l);
   621  			}
   622  		dpcheck(n);
   623  		break;
   624  
   625  	case ONAME:
   626  		if(n->type == T) {
   627  			diag(n, "name not declared: %F", n);
   628  			goto bad;
   629  		}
   630  		if(n->type->etype == TENUM) {
   631  			n->op = OCONST;
   632  			n->type = n->sym->tenum;
   633  			if(!typefd[n->type->etype])
   634  				n->vconst = n->sym->vconst;
   635  			else
   636  				n->fconst = n->sym->fconst;
   637  			break;
   638  		}
   639  		n->addable = 1;
   640  		if(n->class == CEXREG) {
   641  			n->op = OREGISTER;
   642  			// on 386 or amd64, "extern register" generates
   643  			// memory references relative to the
   644  			// gs or fs segment.
   645  			if(thechar == '8' || thechar == '6')	// [sic]
   646  				n->op = OEXREG;
   647  			n->reg = n->sym->offset;
   648  			n->xoffset = 0;
   649  			break;
   650  		}
   651  		break;
   652  
   653  	case OLSTRING:
   654  		if(n->type->link != types[TUSHORT]) {
   655  			o = outstring(0, 0);
   656  			while(o & 3) {
   657  				ushort a[1];
   658  				a[0] = 0;
   659  				outlstring(a, sizeof(ushort));
   660  				o = outlstring(0, 0);
   661  			}
   662  		}
   663  		n->op = ONAME;
   664  		n->xoffset = outlstring(n->rstring, n->type->width);
   665  		n->addable = 1;
   666  		break;
   667  
   668  	case OSTRING:
   669  		if(n->type->link != types[TCHAR]) {
   670  			o = outstring(0, 0);
   671  			while(o & 3) {
   672  				outstring("", 1);
   673  				o = outstring(0, 0);
   674  			}
   675  		}
   676  		n->op = ONAME;
   677  		n->xoffset = outstring(n->cstring, n->type->width);
   678  		n->addable = 1;
   679  		break;
   680  
   681  	case OCONST:
   682  		break;
   683  
   684  	case ODOT:
   685  		if(tcom(l))
   686  			goto bad;
   687  		if(tcompat(n, T, l->type, tdot))
   688  			goto bad;
   689  		if(tcomd(n))
   690  			goto bad;
   691  		break;
   692  
   693  	case OADDR:
   694  		if(tcomo(l, ADDROP))
   695  			goto bad;
   696  		if(tlvalue(l))
   697  			goto bad;
   698  		if(l->type->nbits) {
   699  			diag(n, "address of a bit field");
   700  			goto bad;
   701  		}
   702  		if(l->op == OREGISTER) {
   703  			diag(n, "address of a register");
   704  			goto bad;
   705  		}
   706  		n->type = typ(TIND, l->type);
   707  		n->type->width = types[TIND]->width;
   708  		break;
   709  
   710  	case OIND:
   711  		if(tcom(l))
   712  			goto bad;
   713  		if(tcompat(n, T, l->type, tindir))
   714  			goto bad;
   715  		n->type = l->type->link;
   716  		n->addable = 1;
   717  		break;
   718  
   719  	case OSTRUCT:
   720  		if(tcomx(n))
   721  			goto bad;
   722  		break;
   723  	}
   724  	t = n->type;
   725  	if(t == T)
   726  		goto bad;
   727  	if(t->width < 0) {
   728  		snap(t);
   729  		if(t->width < 0) {
   730  			if(typesu[t->etype] && t->tag)
   731  				diag(n, "structure not fully declared %s", t->tag->name);
   732  			else
   733  				diag(n, "structure not fully declared");
   734  			goto bad;
   735  		}
   736  	}
   737  	if(typeaf[t->etype]) {
   738  		if(f & ADDROF)
   739  			goto addaddr;
   740  		if(f & ADDROP)
   741  			warn(n, "address of array/func ignored");
   742  	}
   743  	return 0;
   744  
   745  addaddr:
   746  	if(tlvalue(n))
   747  		goto bad;
   748  	l = new1(OXXX, Z, Z);
   749  	*l = *n;
   750  	n->op = OADDR;
   751  	if(l->type->etype == TARRAY)
   752  		l->type = l->type->link;
   753  	n->left = l;
   754  	n->right = Z;
   755  	n->addable = 0;
   756  	n->type = typ(TIND, l->type);
   757  	n->type->width = types[TIND]->width;
   758  	return 0;
   759  
   760  bad:
   761  	n->type = T;
   762  	return 1;
   763  }
   764  
   765  int
   766  tcoma(Node *l, Node *n, Type *t, int f)
   767  {
   768  	Node *n1;
   769  	int o;
   770  
   771  	if(t != T)
   772  	if(t->etype == TOLD || t->etype == TDOT)	/* .../old in prototype */
   773  		t = T;
   774  	if(n == Z) {
   775  		if(t != T && !sametype(t, types[TVOID])) {
   776  			diag(n, "not enough function arguments: %F", l);
   777  			return 1;
   778  		}
   779  		return 0;
   780  	}
   781  	if(n->op == OLIST) {
   782  		o = tcoma(l, n->left, t, 0);
   783  		if(t != T) {
   784  			t = t->down;
   785  			if(t == T)
   786  				t = types[TVOID];
   787  		}
   788  		return o | tcoma(l, n->right, t, 1);
   789  	}
   790  	if(f && t != T)
   791  		tcoma(l, Z, t->down, 0);
   792  	if(tcom(n) || tcompat(n, T, n->type, targ))
   793  		return 1;
   794  	if(sametype(t, types[TVOID])) {
   795  		diag(n, "too many function arguments: %F", l);
   796  		return 1;
   797  	}
   798  	if(t != T) {
   799  		typeext(t, n);
   800  		if(stcompat(nodproto, t, n->type, tasign)) {
   801  			diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
   802  				n->type, t, l);
   803  			return 1;
   804  		}
   805  //		switch(t->etype) {
   806  //		case TCHAR:
   807  //		case TSHORT:
   808  //			t = types[TINT];
   809  //			break;
   810  //
   811  //		case TUCHAR:
   812  //		case TUSHORT:
   813  //			t = types[TUINT];
   814  //			break;
   815  //		}
   816  	} else {
   817  		switch(n->type->etype) {
   818  		case TCHAR:
   819  		case TSHORT:
   820  			t = types[TINT];
   821  			break;
   822  
   823  		case TUCHAR:
   824  		case TUSHORT:
   825  			t = types[TUINT];
   826  			break;
   827  
   828  		case TFLOAT:
   829  			t = types[TDOUBLE];
   830  		}
   831  	}
   832  
   833  	if(t != T && !sametype(t, n->type)) {
   834  		n1 = new1(OXXX, Z, Z);
   835  		*n1 = *n;
   836  		n->op = OCAST;
   837  		n->left = n1;
   838  		n->right = Z;
   839  		n->type = t;
   840  		n->addable = 0;
   841  	}
   842  	return 0;
   843  }
   844  
   845  int
   846  tcomd(Node *n)
   847  {
   848  	Type *t;
   849  	int32 o;
   850  
   851  	o = 0;
   852  	t = dotsearch(n->sym, n->left->type->link, n, &o);
   853  	if(t == T) {
   854  		diag(n, "not a member of struct/union: %F", n);
   855  		return 1;
   856  	}
   857  	makedot(n, t, o);
   858  	return 0;
   859  }
   860  
   861  int
   862  tcomx(Node *n)
   863  {
   864  	Type *t;
   865  	Node *l, *r, **ar, **al;
   866  	int e;
   867  
   868  	e = 0;
   869  	if(n->type->etype != TSTRUCT) {
   870  		diag(n, "constructor must be a structure");
   871  		return 1;
   872  	}
   873  	l = invert(n->left);
   874  	n->left = l;
   875  	al = &n->left;
   876  	for(t = n->type->link; t != T; t = t->down) {
   877  		if(l == Z) {
   878  			diag(n, "constructor list too short");
   879  			return 1;
   880  		}
   881  		if(l->op == OLIST) {
   882  			r = l->left;
   883  			ar = &l->left;
   884  			al = &l->right;
   885  			l = l->right;
   886  		} else {
   887  			r = l;
   888  			ar = al;
   889  			l = Z;
   890  		}
   891  		if(tcom(r))
   892  			e++;
   893  		typeext(t, r);
   894  		if(tcompat(n, t, r->type, tasign))
   895  			e++;
   896  		constas(n, t, r->type);
   897  		if(!e && !sametype(t, r->type)) {
   898  			r = new1(OCAST, r, Z);
   899  			r->type = t;
   900  			*ar = r;
   901  		}
   902  	}
   903  	if(l != Z) {
   904  		diag(n, "constructor list too long");
   905  		return 1;
   906  	}
   907  	return e;
   908  }
   909  
   910  int
   911  tlvalue(Node *n)
   912  {
   913  
   914  	if(!n->addable) {
   915  		diag(n, "not an l-value");
   916  		return 1;
   917  	}
   918  	return 0;
   919  }
   920  
   921  /*
   922   *	general rewrite
   923   *	(IND(ADDR x)) ==> x
   924   *	(ADDR(IND x)) ==> x
   925   *	remove some zero operands
   926   *	remove no op casts
   927   *	evaluate constants
   928   */
   929  void
   930  ccom(Node *n)
   931  {
   932  	Node *l, *r;
   933  	int t;
   934  
   935  loop:
   936  	if(n == Z)
   937  		return;
   938  	l = n->left;
   939  	r = n->right;
   940  	switch(n->op) {
   941  
   942  	case OAS:
   943  	case OASXOR:
   944  	case OASAND:
   945  	case OASOR:
   946  	case OASMOD:
   947  	case OASLMOD:
   948  	case OASLSHR:
   949  	case OASASHR:
   950  	case OASASHL:
   951  	case OASDIV:
   952  	case OASLDIV:
   953  	case OASMUL:
   954  	case OASLMUL:
   955  	case OASSUB:
   956  	case OASADD:
   957  		ccom(l);
   958  		ccom(r);
   959  		if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
   960  		if(r->op == OCONST) {
   961  			t = n->type->width * 8;	/* bits per byte */
   962  			if(r->vconst >= t || r->vconst < 0)
   963  				warn(n, "stupid shift: %lld", r->vconst);
   964  		}
   965  		break;
   966  
   967  	case OCAST:
   968  		ccom(l);
   969  		if(l->op == OCONST) {
   970  			evconst(n);
   971  			if(n->op == OCONST)
   972  				break;
   973  		}
   974  		if(nocast(l->type, n->type)) {
   975  			l->type = n->type;
   976  			*n = *l;
   977  		}
   978  		break;
   979  
   980  	case OCOND:
   981  		ccom(l);
   982  		ccom(r);
   983  		if(l->op == OCONST)
   984  			if(vconst(l) == 0)
   985  				*n = *r->right;
   986  			else
   987  				*n = *r->left;
   988  		break;
   989  
   990  	case OREGISTER:
   991  	case OINDREG:
   992  	case OCONST:
   993  	case ONAME:
   994  		break;
   995  
   996  	case OADDR:
   997  		ccom(l);
   998  		l->etype = TVOID;
   999  		if(l->op == OIND) {
  1000  			l->left->type = n->type;
  1001  			*n = *l->left;
  1002  			break;
  1003  		}
  1004  		goto common;
  1005  
  1006  	case OIND:
  1007  		ccom(l);
  1008  		if(l->op == OADDR) {
  1009  			l->left->type = n->type;
  1010  			*n = *l->left;
  1011  			break;
  1012  		}
  1013  		goto common;
  1014  
  1015  	case OEQ:
  1016  	case ONE:
  1017  
  1018  	case OLE:
  1019  	case OGE:
  1020  	case OLT:
  1021  	case OGT:
  1022  
  1023  	case OLS:
  1024  	case OHS:
  1025  	case OLO:
  1026  	case OHI:
  1027  		ccom(l);
  1028  		ccom(r);
  1029  		if(compar(n, 0) || compar(n, 1))
  1030  			break;
  1031  		relcon(l, r);
  1032  		relcon(r, l);
  1033  		goto common;
  1034  
  1035  	case OASHR:
  1036  	case OASHL:
  1037  	case OLSHR:
  1038  		ccom(l);
  1039  		if(vconst(l) == 0 && !side(r)) {
  1040  			*n = *l;
  1041  			break;
  1042  		}
  1043  		ccom(r);
  1044  		if(vconst(r) == 0) {
  1045  			*n = *l;
  1046  			break;
  1047  		}
  1048  		if(r->op == OCONST) {
  1049  			t = n->type->width * 8;	/* bits per byte */
  1050  			if(r->vconst >= t || r->vconst <= -t)
  1051  				warn(n, "stupid shift: %lld", r->vconst);
  1052  		}
  1053  		goto common;
  1054  
  1055  	case OMUL:
  1056  	case OLMUL:
  1057  		ccom(l);
  1058  		t = vconst(l);
  1059  		if(t == 0 && !side(r)) {
  1060  			*n = *l;
  1061  			break;
  1062  		}
  1063  		if(t == 1) {
  1064  			*n = *r;
  1065  			goto loop;
  1066  		}
  1067  		ccom(r);
  1068  		t = vconst(r);
  1069  		if(t == 0 && !side(l)) {
  1070  			*n = *r;
  1071  			break;
  1072  		}
  1073  		if(t == 1) {
  1074  			*n = *l;
  1075  			break;
  1076  		}
  1077  		goto common;
  1078  
  1079  	case ODIV:
  1080  	case OLDIV:
  1081  		ccom(l);
  1082  		if(vconst(l) == 0 && !side(r)) {
  1083  			*n = *l;
  1084  			break;
  1085  		}
  1086  		ccom(r);
  1087  		t = vconst(r);
  1088  		if(t == 0) {
  1089  			diag(n, "divide check");
  1090  			*n = *r;
  1091  			break;
  1092  		}
  1093  		if(t == 1) {
  1094  			*n = *l;
  1095  			break;
  1096  		}
  1097  		goto common;
  1098  
  1099  	case OSUB:
  1100  		ccom(r);
  1101  		if(r->op == OCONST) {
  1102  			if(typefd[r->type->etype]) {
  1103  				n->op = OADD;
  1104  				r->fconst = -r->fconst;
  1105  				goto loop;
  1106  			} else {
  1107  				n->op = OADD;
  1108  				r->vconst = -r->vconst;
  1109  				goto loop;
  1110  			}
  1111  		}
  1112  		ccom(l);
  1113  		goto common;
  1114  
  1115  	case OXOR:
  1116  	case OOR:
  1117  	case OADD:
  1118  		ccom(l);
  1119  		if(vconst(l) == 0) {
  1120  			*n = *r;
  1121  			goto loop;
  1122  		}
  1123  		ccom(r);
  1124  		if(vconst(r) == 0) {
  1125  			*n = *l;
  1126  			break;
  1127  		}
  1128  		goto commute;
  1129  
  1130  	case OAND:
  1131  		ccom(l);
  1132  		ccom(r);
  1133  		if(vconst(l) == 0 && !side(r)) {
  1134  			*n = *l;
  1135  			break;
  1136  		}
  1137  		if(vconst(r) == 0 && !side(l)) {
  1138  			*n = *r;
  1139  			break;
  1140  		}
  1141  
  1142  	commute:
  1143  		/* look for commutative constant */
  1144  		if(r->op == OCONST) {
  1145  			if(l->op == n->op) {
  1146  				if(l->left->op == OCONST) {
  1147  					n->right = l->right;
  1148  					l->right = r;
  1149  					goto loop;
  1150  				}
  1151  				if(l->right->op == OCONST) {
  1152  					n->right = l->left;
  1153  					l->left = r;
  1154  					goto loop;
  1155  				}
  1156  			}
  1157  		}
  1158  		if(l->op == OCONST) {
  1159  			if(r->op == n->op) {
  1160  				if(r->left->op == OCONST) {
  1161  					n->left = r->right;
  1162  					r->right = l;
  1163  					goto loop;
  1164  				}
  1165  				if(r->right->op == OCONST) {
  1166  					n->left = r->left;
  1167  					r->left = l;
  1168  					goto loop;
  1169  				}
  1170  			}
  1171  		}
  1172  		goto common;
  1173  
  1174  	case OANDAND:
  1175  		ccom(l);
  1176  		if(vconst(l) == 0) {
  1177  			*n = *l;
  1178  			break;
  1179  		}
  1180  		ccom(r);
  1181  		goto common;
  1182  
  1183  	case OOROR:
  1184  		ccom(l);
  1185  		if(l->op == OCONST && l->vconst != 0) {
  1186  			*n = *l;
  1187  			n->vconst = 1;
  1188  			break;
  1189  		}
  1190  		ccom(r);
  1191  		goto common;
  1192  
  1193  	default:
  1194  		if(l != Z)
  1195  			ccom(l);
  1196  		if(r != Z)
  1197  			ccom(r);
  1198  	common:
  1199  		if(l != Z)
  1200  		if(l->op != OCONST)
  1201  			break;
  1202  		if(r != Z)
  1203  		if(r->op != OCONST)
  1204  			break;
  1205  		evconst(n);
  1206  	}
  1207  }
  1208  
  1209  /*	OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
  1210  static char *cmps[12] = 
  1211  {
  1212  	"==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
  1213  };
  1214  
  1215  /* 128-bit numbers */
  1216  typedef struct Big Big;
  1217  struct Big
  1218  {
  1219  	vlong a;
  1220  	uvlong b;
  1221  };
  1222  static int
  1223  cmp(Big x, Big y)
  1224  {
  1225  	if(x.a != y.a){
  1226  		if(x.a < y.a)
  1227  			return -1;
  1228  		return 1;
  1229  	}
  1230  	if(x.b != y.b){
  1231  		if(x.b < y.b)
  1232  			return -1;
  1233  		return 1;
  1234  	}
  1235  	return 0;
  1236  }
  1237  static Big
  1238  add(Big x, int y)
  1239  {
  1240  	uvlong ob;
  1241  	
  1242  	ob = x.b;
  1243  	x.b += y;
  1244  	if(y > 0 && x.b < ob)
  1245  		x.a++;
  1246  	if(y < 0 && x.b > ob)
  1247  		x.a--;
  1248  	return x;
  1249  } 
  1250  
  1251  Big
  1252  big(vlong a, uvlong b)
  1253  {
  1254  	Big x;
  1255  
  1256  	x.a = a;
  1257  	x.b = b;
  1258  	return x;
  1259  }
  1260  
  1261  int
  1262  compar(Node *n, int reverse)
  1263  {
  1264  	Big lo, hi, x;
  1265  	int op;
  1266  	char xbuf[40], cmpbuf[50];
  1267  	Node *l, *r;
  1268  	Type *lt, *rt;
  1269  
  1270  	/*
  1271  	 * The point of this function is to diagnose comparisons 
  1272  	 * that can never be true or that look misleading because
  1273  	 * of the `usual arithmetic conversions'.  As an example 
  1274  	 * of the latter, if x is a ulong, then if(x <= -1) really means
  1275  	 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
  1276  	 * what it says (but 8c compiles it wrong anyway).
  1277  	 */
  1278  
  1279  	if(reverse){
  1280  		r = n->left;
  1281  		l = n->right;
  1282  		op = comrel[relindex(n->op)];
  1283  	}else{
  1284  		l = n->left;
  1285  		r = n->right;
  1286  		op = n->op;
  1287  	}
  1288  
  1289  	/*
  1290  	 * Skip over left casts to find out the original expression range.
  1291  	 */
  1292  	while(l->op == OCAST)
  1293  		l = l->left;
  1294  	if(l->op == OCONST)
  1295  		return 0;
  1296  	lt = l->type;
  1297  	if(l->op == ONAME && l->sym->type){
  1298  		lt = l->sym->type;
  1299  		if(lt->etype == TARRAY)
  1300  			lt = lt->link;
  1301  	}
  1302  	if(lt == T)
  1303  		return 0;
  1304  	if(lt->etype == TXXX || lt->etype > TUVLONG)
  1305  		return 0;
  1306  	
  1307  	/*
  1308  	 * Skip over the right casts to find the on-screen value.
  1309  	 */
  1310  	if(r->op != OCONST)
  1311  		return 0;
  1312  	while(r->oldop == OCAST && !r->xcast)
  1313  		r = r->left;
  1314  	rt = r->type;
  1315  	if(rt == T)
  1316  		return 0;
  1317  
  1318  	x.b = r->vconst;
  1319  	x.a = 0;
  1320  	if((rt->etype&1) && r->vconst < 0)	/* signed negative */
  1321  		x.a = ~0ULL;
  1322  
  1323  	if((lt->etype&1)==0){
  1324  		/* unsigned */
  1325  		lo = big(0, 0);
  1326  		if(lt->width == 8)
  1327  			hi = big(0, ~0ULL);
  1328  		else
  1329  			hi = big(0, (1LL<<(l->type->width*8))-1);
  1330  	}else{
  1331  		lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
  1332  		hi = big(0, (1LL<<(l->type->width*8-1))-1);
  1333  	}
  1334  
  1335  	switch(op){
  1336  	case OLT:
  1337  	case OLO:
  1338  	case OGE:
  1339  	case OHS:
  1340  		if(cmp(x, lo) <= 0)
  1341  			goto useless;
  1342  		if(cmp(x, add(hi, 1)) >= 0)
  1343  			goto useless;
  1344  		break;
  1345  	case OLE:
  1346  	case OLS:
  1347  	case OGT:
  1348  	case OHI:
  1349  		if(cmp(x, add(lo, -1)) <= 0)
  1350  			goto useless;
  1351  		if(cmp(x, hi) >= 0)
  1352  			goto useless;
  1353  		break;
  1354  	case OEQ:
  1355  	case ONE:
  1356  		/*
  1357  		 * Don't warn about comparisons if the expression
  1358  		 * is as wide as the value: the compiler-supplied casts
  1359  		 * will make both outcomes possible.
  1360  		 */
  1361  		if(lt->width >= rt->width && debug['w'] < 2)
  1362  			return 0;
  1363  		if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
  1364  			goto useless;
  1365  		break;
  1366  	}
  1367  	return 0;
  1368  
  1369  useless:
  1370  	if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
  1371  		snprint(xbuf, sizeof xbuf, "%lld", x.b);
  1372  	else if(x.a == 0)
  1373  		snprint(xbuf, sizeof xbuf, "%#llux", x.b);
  1374  	else
  1375  		snprint(xbuf, sizeof xbuf, "%#llx", x.b);
  1376  	if(reverse)
  1377  		snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
  1378  			xbuf, cmps[relindex(n->op)], lt);
  1379  	else
  1380  		snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
  1381  			lt, cmps[relindex(n->op)], xbuf);
  1382  	warn(n, "useless or misleading comparison: %s", cmpbuf);
  1383  	return 0;
  1384  }
  1385