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