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

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include	<u.h>
     6  #include	<libc.h>
     7  #include	"go.h"
     8  #include	"md5.h"
     9  #include	"y.tab.h"
    10  #include	"yerr.h"
    11  
    12  typedef struct Error Error;
    13  struct Error
    14  {
    15  	int lineno;
    16  	int seq;
    17  	char *msg;
    18  };
    19  static Error *err;
    20  static int nerr;
    21  static int merr;
    22  
    23  void
    24  errorexit(void)
    25  {
    26  	flusherrors();
    27  	if(outfile)
    28  		remove(outfile);
    29  	exits("error");
    30  }
    31  
    32  extern int yychar;
    33  int
    34  parserline(void)
    35  {
    36  	if(yychar != 0 && yychar != -2)	// parser has one symbol lookahead
    37  		return prevlineno;
    38  	return lineno;
    39  }
    40  
    41  static void
    42  adderr(int line, char *fmt, va_list arg)
    43  {
    44  	Fmt f;
    45  	Error *p;
    46  
    47  	fmtstrinit(&f);
    48  	fmtprint(&f, "%L: ", line);
    49  	fmtvprint(&f, fmt, arg);
    50  	fmtprint(&f, "\n");
    51  
    52  	if(nerr >= merr) {
    53  		if(merr == 0)
    54  			merr = 16;
    55  		else
    56  			merr *= 2;
    57  		p = realloc(err, merr*sizeof err[0]);
    58  		if(p == nil) {
    59  			merr = nerr;
    60  			flusherrors();
    61  			print("out of memory\n");
    62  			errorexit();
    63  		}
    64  		err = p;
    65  	}
    66  	err[nerr].seq = nerr;
    67  	err[nerr].lineno = line;
    68  	err[nerr].msg = fmtstrflush(&f);
    69  	nerr++;
    70  }
    71  
    72  static int
    73  errcmp(const void *va, const void *vb)
    74  {
    75  	Error *a, *b;
    76  
    77  	a = (Error*)va;
    78  	b = (Error*)vb;
    79  	if(a->lineno != b->lineno)
    80  		return a->lineno - b->lineno;
    81  	if(a->seq != b->seq)
    82  		return a->seq - b->seq;
    83  	return strcmp(a->msg, b->msg);
    84  }
    85  
    86  void
    87  flusherrors(void)
    88  {
    89  	int i;
    90  
    91  	if(nerr == 0)
    92  		return;
    93  	qsort(err, nerr, sizeof err[0], errcmp);
    94  	for(i=0; i<nerr; i++)
    95  		if(i==0 || strcmp(err[i].msg, err[i-1].msg) != 0)
    96  			print("%s", err[i].msg);
    97  	nerr = 0;
    98  }
    99  
   100  static void
   101  hcrash(void)
   102  {
   103  	if(debug['h']) {
   104  		flusherrors();
   105  		if(outfile)
   106  			remove(outfile);
   107  		*(volatile int*)0 = 0;
   108  	}
   109  }
   110  
   111  void
   112  yyerrorl(int line, char *fmt, ...)
   113  {
   114  	va_list arg;
   115  
   116  	va_start(arg, fmt);
   117  	adderr(line, fmt, arg);
   118  	va_end(arg);
   119  
   120  	hcrash();
   121  	nerrors++;
   122  	if(nsavederrors+nerrors >= 10 && !debug['e']) {
   123  		flusherrors();
   124  		print("%L: too many errors\n", line);
   125  		errorexit();
   126  	}
   127  }
   128  
   129  extern int yystate, yychar;
   130  
   131  void
   132  yyerror(char *fmt, ...)
   133  {
   134  	int i;
   135  	static int lastsyntax;
   136  	va_list arg;
   137  	char buf[512], *p;
   138  
   139  	if(strncmp(fmt, "syntax error", 12) == 0) {
   140  		nsyntaxerrors++;
   141  		
   142  		if(debug['x'])	
   143  			print("yyerror: yystate=%d yychar=%d\n", yystate, yychar);
   144  
   145  		// An unexpected EOF caused a syntax error. Use the previous
   146  		// line number since getc generated a fake newline character.
   147  		if(curio.eofnl)
   148  			lexlineno = prevlineno;
   149  
   150  		// only one syntax error per line
   151  		if(lastsyntax == lexlineno)
   152  			return;
   153  		lastsyntax = lexlineno;
   154  			
   155  		if(strstr(fmt, "{ or {") || strstr(fmt, " or ?") || strstr(fmt, " or @")) {
   156  			// The grammar has { and LBRACE but both show up as {.
   157  			// Rewrite syntax error referring to "{ or {" to say just "{".
   158  			strecpy(buf, buf+sizeof buf, fmt);
   159  			p = strstr(buf, "{ or {");
   160  			if(p)
   161  				memmove(p+1, p+6, strlen(p+6)+1);
   162  			
   163  			// The grammar has ? and @ but only for reading imports.
   164  			// Silence them in ordinary errors.
   165  			p = strstr(buf, " or ?");
   166  			if(p)
   167  				memmove(p, p+5, strlen(p+5)+1);
   168  			p = strstr(buf, " or @");
   169  			if(p)
   170  				memmove(p, p+5, strlen(p+5)+1);
   171  			fmt = buf;
   172  		}
   173  		
   174  		// look for parse state-specific errors in list (see go.errors).
   175  		for(i=0; i<nelem(yymsg); i++) {
   176  			if(yymsg[i].yystate == yystate && yymsg[i].yychar == yychar) {
   177  				yyerrorl(lexlineno, "syntax error: %s", yymsg[i].msg);
   178  				return;
   179  			}
   180  		}
   181  		
   182  		// plain "syntax error" gets "near foo" added
   183  		if(strcmp(fmt, "syntax error") == 0) {
   184  			yyerrorl(lexlineno, "syntax error near %s", lexbuf);
   185  			return;
   186  		}
   187  		
   188  		// if bison says "syntax error, more info"; print "syntax error: more info".
   189  		if(fmt[12] == ',') {
   190  			yyerrorl(lexlineno, "syntax error:%s", fmt+13);
   191  			return;
   192  		}
   193  
   194  		yyerrorl(lexlineno, "%s", fmt);
   195  		return;
   196  	}
   197  
   198  	va_start(arg, fmt);
   199  	adderr(parserline(), fmt, arg);
   200  	va_end(arg);
   201  
   202  	hcrash();
   203  	nerrors++;
   204  	if(nsavederrors+nerrors >= 10 && !debug['e']) {
   205  		flusherrors();
   206  		print("%L: too many errors\n", parserline());
   207  		errorexit();
   208  	}
   209  }
   210  
   211  void
   212  warn(char *fmt, ...)
   213  {
   214  	va_list arg;
   215  
   216  	va_start(arg, fmt);
   217  	adderr(parserline(), fmt, arg);
   218  	va_end(arg);
   219  
   220  	hcrash();
   221  }
   222  
   223  void
   224  warnl(int line, char *fmt, ...)
   225  {
   226  	va_list arg;
   227  
   228  	va_start(arg, fmt);
   229  	adderr(line, fmt, arg);
   230  	va_end(arg);
   231  	if(debug['m'])
   232  		flusherrors();
   233  }
   234  
   235  void
   236  fatal(char *fmt, ...)
   237  {
   238  	va_list arg;
   239  
   240  	flusherrors();
   241  
   242  	print("%L: internal compiler error: ", lineno);
   243  	va_start(arg, fmt);
   244  	vfprint(1, fmt, arg);
   245  	va_end(arg);
   246  	print("\n");
   247  	
   248  	// If this is a released compiler version, ask for a bug report.
   249  	if(strncmp(getgoversion(), "release", 7) == 0) {
   250  		print("\n");
   251  		print("Please file a bug report including a short program that triggers the error.\n");
   252  		print("http://code.google.com/p/go/issues/entry?template=compilerbug\n");
   253  	}
   254  	hcrash();
   255  	errorexit();
   256  }
   257  
   258  void
   259  linehist(char *file, int32 off, int relative)
   260  {
   261  	Hist *h;
   262  	char *cp;
   263  
   264  	if(debug['i']) {
   265  		if(file != nil) {
   266  			if(off < 0)
   267  				print("pragma %s", file);
   268  			else
   269  			if(off > 0)
   270  				print("line %s", file);
   271  			else
   272  				print("import %s", file);
   273  		} else
   274  			print("end of import");
   275  		print(" at line %L\n", lexlineno);
   276  	}
   277  
   278  	if(off < 0 && file[0] != '/' && !relative) {
   279  		cp = mal(strlen(file) + strlen(pathname) + 2);
   280  		sprint(cp, "%s/%s", pathname, file);
   281  		file = cp;
   282  	}
   283  
   284  	h = mal(sizeof(Hist));
   285  	h->name = file;
   286  	h->line = lexlineno;
   287  	h->offset = off;
   288  	h->link = H;
   289  	if(ehist == H) {
   290  		hist = h;
   291  		ehist = h;
   292  		return;
   293  	}
   294  	ehist->link = h;
   295  	ehist = h;
   296  }
   297  
   298  int32
   299  setlineno(Node *n)
   300  {
   301  	int32 lno;
   302  
   303  	lno = lineno;
   304  	if(n != N)
   305  	switch(n->op) {
   306  	case ONAME:
   307  	case OTYPE:
   308  	case OPACK:
   309  	case OLITERAL:
   310  		break;
   311  	default:
   312  		lineno = n->lineno;
   313  		if(lineno == 0) {
   314  			if(debug['K'])
   315  				warn("setlineno: line 0");
   316  			lineno = lno;
   317  		}
   318  	}
   319  	return lno;
   320  }
   321  
   322  uint32
   323  stringhash(char *p)
   324  {
   325  	int32 h;
   326  	int c;
   327  
   328  	h = 0;
   329  	for(;;) {
   330  		c = *p++;
   331  		if(c == 0)
   332  			break;
   333  		h = h*PRIME1 + c;
   334  	}
   335  
   336  	if(h < 0) {
   337  		h = -h;
   338  		if(h < 0)
   339  			h = 0;
   340  	}
   341  	return h;
   342  }
   343  
   344  Sym*
   345  lookup(char *name)
   346  {
   347  	return pkglookup(name, localpkg);
   348  }
   349  
   350  Sym*
   351  pkglookup(char *name, Pkg *pkg)
   352  {
   353  	Sym *s;
   354  	uint32 h;
   355  	int c;
   356  
   357  	h = stringhash(name) % NHASH;
   358  	c = name[0];
   359  	for(s = hash[h]; s != S; s = s->link) {
   360  		if(s->name[0] != c || s->pkg != pkg)
   361  			continue;
   362  		if(strcmp(s->name, name) == 0)
   363  			return s;
   364  	}
   365  
   366  	s = mal(sizeof(*s));
   367  	s->name = mal(strlen(name)+1);
   368  	strcpy(s->name, name);
   369  
   370  	s->pkg = pkg;
   371  
   372  	s->link = hash[h];
   373  	hash[h] = s;
   374  	s->lexical = LNAME;
   375  
   376  	return s;
   377  }
   378  
   379  Sym*
   380  restrictlookup(char *name, Pkg *pkg)
   381  {
   382  	if(!exportname(name) && pkg != localpkg)
   383  		yyerror("cannot refer to unexported name %s.%s", pkg->name, name);
   384  	return pkglookup(name, pkg);
   385  }
   386  
   387  
   388  // find all the exported symbols in package opkg
   389  // and make them available in the current package
   390  void
   391  importdot(Pkg *opkg, Node *pack)
   392  {
   393  	Sym *s, *s1;
   394  	uint32 h;
   395  	int n;
   396  	char *pkgerror;
   397  
   398  	n = 0;
   399  	for(h=0; h<NHASH; h++) {
   400  		for(s = hash[h]; s != S; s = s->link) {
   401  			if(s->pkg != opkg)
   402  				continue;
   403  			if(s->def == N)
   404  				continue;
   405  			if(!exportname(s->name) || utfrune(s->name, 0xb7))	// 0xb7 = center dot
   406  				continue;
   407  			s1 = lookup(s->name);
   408  			if(s1->def != N) {
   409  				pkgerror = smprint("during import \"%Z\"", opkg->path);
   410  				redeclare(s1, pkgerror);
   411  				continue;
   412  			}
   413  			s1->def = s->def;
   414  			s1->block = s->block;
   415  			s1->def->pack = pack;
   416  			s1->origpkg = opkg;
   417  			n++;
   418  		}
   419  	}
   420  	if(n == 0) {
   421  		// can't possibly be used - there were no symbols
   422  		yyerrorl(pack->lineno, "imported and not used: \"%Z\"", opkg->path);
   423  	}
   424  }
   425  
   426  static void
   427  gethunk(void)
   428  {
   429  	char *h;
   430  	int32 nh;
   431  
   432  	nh = NHUNK;
   433  	if(thunk >= 10L*NHUNK)
   434  		nh = 10L*NHUNK;
   435  	h = (char*)malloc(nh);
   436  	if(h == nil) {
   437  		flusherrors();
   438  		yyerror("out of memory");
   439  		errorexit();
   440  	}
   441  	hunk = h;
   442  	nhunk = nh;
   443  	thunk += nh;
   444  }
   445  
   446  void*
   447  mal(int32 n)
   448  {
   449  	void *p;
   450  
   451  	if(n >= NHUNK) {
   452  		p = malloc(n);
   453  		if(p == nil) {
   454  			flusherrors();
   455  			yyerror("out of memory");
   456  			errorexit();
   457  		}
   458  		memset(p, 0, n);
   459  		return p;
   460  	}
   461  
   462  	while((uintptr)hunk & MAXALIGN) {
   463  		hunk++;
   464  		nhunk--;
   465  	}
   466  	if(nhunk < n)
   467  		gethunk();
   468  
   469  	p = hunk;
   470  	nhunk -= n;
   471  	hunk += n;
   472  	memset(p, 0, n);
   473  	return p;
   474  }
   475  
   476  void*
   477  remal(void *p, int32 on, int32 n)
   478  {
   479  	void *q;
   480  
   481  	q = (uchar*)p + on;
   482  	if(q != hunk || nhunk < n) {
   483  		if(on+n >= NHUNK) {
   484  			q = mal(on+n);
   485  			memmove(q, p, on);
   486  			return q;
   487  		}
   488  		if(nhunk < on+n)
   489  			gethunk();
   490  		memmove(hunk, p, on);
   491  		p = hunk;
   492  		hunk += on;
   493  		nhunk -= on;
   494  	}
   495  	hunk += n;
   496  	nhunk -= n;
   497  	return p;
   498  }
   499  
   500  Node*
   501  nod(int op, Node *nleft, Node *nright)
   502  {
   503  	Node *n;
   504  
   505  	n = mal(sizeof(*n));
   506  	n->op = op;
   507  	n->left = nleft;
   508  	n->right = nright;
   509  	n->lineno = parserline();
   510  	n->xoffset = BADWIDTH;
   511  	n->orig = n;
   512  	n->curfn = curfn;
   513  	return n;
   514  }
   515  
   516  void
   517  saveorignode(Node *n)
   518  {
   519  	Node *norig;
   520  
   521  	if(n->orig != N)
   522  		return;
   523  	norig = nod(n->op, N, N);
   524  	*norig = *n;
   525  	n->orig = norig;
   526  }
   527  
   528  // ispaddedfield returns whether the given field
   529  // is followed by padding. For the case where t is
   530  // the last field, total gives the size of the enclosing struct.
   531  static int
   532  ispaddedfield(Type *t, vlong total)
   533  {
   534  	if(t->etype != TFIELD)
   535  		fatal("ispaddedfield called non-field %T", t);
   536  	if(t->down == T)
   537  		return t->width + t->type->width != total;
   538  	return t->width + t->type->width != t->down->width;
   539  }
   540  
   541  int
   542  algtype1(Type *t, Type **bad)
   543  {
   544  	int a, ret;
   545  	Type *t1;
   546  	
   547  	if(bad)
   548  		*bad = T;
   549  
   550  	switch(t->etype) {
   551  	case TANY:
   552  	case TFORW:
   553  		// will be defined later.
   554  		*bad = t;
   555  		return -1;
   556  
   557  	case TINT8:
   558  	case TUINT8:
   559  	case TINT16:
   560  	case TUINT16:
   561  	case TINT32:
   562  	case TUINT32:
   563  	case TINT64:
   564  	case TUINT64:
   565  	case TINT:
   566  	case TUINT:
   567  	case TUINTPTR:
   568  	case TBOOL:
   569  	case TPTR32:
   570  	case TPTR64:
   571  	case TCHAN:
   572  	case TUNSAFEPTR:
   573  		return AMEM;
   574  
   575  	case TFUNC:
   576  	case TMAP:
   577  		if(bad)
   578  			*bad = t;
   579  		return ANOEQ;
   580  
   581  	case TFLOAT32:
   582  		return AFLOAT32;
   583  
   584  	case TFLOAT64:
   585  		return AFLOAT64;
   586  
   587  	case TCOMPLEX64:
   588  		return ACPLX64;
   589  
   590  	case TCOMPLEX128:
   591  		return ACPLX128;
   592  
   593  	case TSTRING:
   594  		return ASTRING;
   595  	
   596  	case TINTER:
   597  		if(isnilinter(t))
   598  			return ANILINTER;
   599  		return AINTER;
   600  	
   601  	case TARRAY:
   602  		if(isslice(t)) {
   603  			if(bad)
   604  				*bad = t;
   605  			return ANOEQ;
   606  		}
   607  		if(t->bound == 0)
   608  			return AMEM;
   609  		a = algtype1(t->type, bad);
   610  		if(a == ANOEQ || a == AMEM) {
   611  			if(a == ANOEQ && bad)
   612  				*bad = t;
   613  			return a;
   614  		}
   615  		return -1;  // needs special compare
   616  
   617  	case TSTRUCT:
   618  		if(t->type != T && t->type->down == T) {
   619  			// One-field struct is same as that one field alone.
   620  			return algtype1(t->type->type, bad);
   621  		}
   622  		ret = AMEM;
   623  		for(t1=t->type; t1!=T; t1=t1->down) {
   624  			// Blank fields and padding must be ignored,
   625  			// so need special compare.
   626  			if(isblanksym(t1->sym) || ispaddedfield(t1, t->width)) {
   627  				ret = -1;
   628  				continue;
   629  			}
   630  			a = algtype1(t1->type, bad);
   631  			if(a == ANOEQ)
   632  				return ANOEQ;  // not comparable
   633  			if(a != AMEM)
   634  				ret = -1;  // needs special compare
   635  		}
   636  		return ret;
   637  	}
   638  
   639  	fatal("algtype1: unexpected type %T", t);
   640  	return 0;
   641  }
   642  
   643  int
   644  algtype(Type *t)
   645  {
   646  	int a;
   647  	
   648  	a = algtype1(t, nil);
   649  	if(a == AMEM || a == ANOEQ) {
   650  		if(isslice(t))
   651  			return ASLICE;
   652  		switch(t->width) {
   653  		case 0:
   654  			return a + AMEM0 - AMEM;
   655  		case 1:
   656  			return a + AMEM8 - AMEM;
   657  		case 2:
   658  			return a + AMEM16 - AMEM;
   659  		case 4:
   660  			return a + AMEM32 - AMEM;
   661  		case 8:
   662  			return a + AMEM64 - AMEM;
   663  		case 16:
   664  			return a + AMEM128 - AMEM;
   665  		}
   666  	}
   667  	return a;
   668  }
   669  
   670  Type*
   671  maptype(Type *key, Type *val)
   672  {
   673  	Type *t;
   674  	Type *bad;
   675  	int atype;
   676  
   677  	if(key != nil) {
   678  		atype = algtype1(key, &bad);
   679  		switch(bad == T ? key->etype : bad->etype) {
   680  		default:
   681  			if(atype == ANOEQ)
   682  				yyerror("invalid map key type %T", key);
   683  			break;
   684  		case TANY:
   685  			// will be resolved later.
   686  			break;
   687  		case TFORW:
   688  			// map[key] used during definition of key.
   689  			// postpone check until key is fully defined.
   690  			// if there are multiple uses of map[key]
   691  			// before key is fully defined, the error
   692  			// will only be printed for the first one.
   693  			// good enough.
   694  			if(key->maplineno == 0)
   695  				key->maplineno = lineno;
   696  			break;
   697  		}
   698  	}
   699  	t = typ(TMAP);
   700  	t->down = key;
   701  	t->type = val;
   702  	return t;
   703  }
   704  
   705  Type*
   706  typ(int et)
   707  {
   708  	Type *t;
   709  
   710  	t = mal(sizeof(*t));
   711  	t->etype = et;
   712  	t->width = BADWIDTH;
   713  	t->lineno = lineno;
   714  	t->orig = t;
   715  	return t;
   716  }
   717  
   718  static int
   719  methcmp(const void *va, const void *vb)
   720  {
   721  	Type *a, *b;
   722  	int i;
   723  	
   724  	a = *(Type**)va;
   725  	b = *(Type**)vb;
   726  	if(a->sym == S && b->sym == S)
   727  		return 0;
   728  	if(a->sym == S)
   729  		return -1;
   730  	if(b->sym == S)
   731  		return 1;
   732  	i = strcmp(a->sym->name, b->sym->name);
   733  	if(i != 0)
   734  		return i;
   735  	if(!exportname(a->sym->name)) {
   736  		i = strcmp(a->sym->pkg->path->s, b->sym->pkg->path->s);
   737  		if(i != 0)
   738  			return i;
   739  	}
   740  	return 0;
   741  }
   742  
   743  Type*
   744  sortinter(Type *t)
   745  {
   746  	Type *f;
   747  	int i;
   748  	Type **a;
   749  	
   750  	if(t->type == nil || t->type->down == nil)
   751  		return t;
   752  
   753  	i=0;
   754  	for(f=t->type; f; f=f->down)
   755  		i++;
   756  	a = mal(i*sizeof f);
   757  	i = 0;
   758  	for(f=t->type; f; f=f->down)
   759  		a[i++] = f;
   760  	qsort(a, i, sizeof a[0], methcmp);
   761  	while(i-- > 0) {
   762  		a[i]->down = f;
   763  		f = a[i];
   764  	}
   765  	t->type = f;
   766  	return t;
   767  }
   768  
   769  Node*
   770  nodintconst(int64 v)
   771  {
   772  	Node *c;
   773  
   774  	c = nod(OLITERAL, N, N);
   775  	c->addable = 1;
   776  	c->val.u.xval = mal(sizeof(*c->val.u.xval));
   777  	mpmovecfix(c->val.u.xval, v);
   778  	c->val.ctype = CTINT;
   779  	c->type = types[TIDEAL];
   780  	ullmancalc(c);
   781  	return c;
   782  }
   783  
   784  Node*
   785  nodfltconst(Mpflt* v)
   786  {
   787  	Node *c;
   788  
   789  	c = nod(OLITERAL, N, N);
   790  	c->addable = 1;
   791  	c->val.u.fval = mal(sizeof(*c->val.u.fval));
   792  	mpmovefltflt(c->val.u.fval, v);
   793  	c->val.ctype = CTFLT;
   794  	c->type = types[TIDEAL];
   795  	ullmancalc(c);
   796  	return c;
   797  }
   798  
   799  void
   800  nodconst(Node *n, Type *t, int64 v)
   801  {
   802  	memset(n, 0, sizeof(*n));
   803  	n->op = OLITERAL;
   804  	n->addable = 1;
   805  	ullmancalc(n);
   806  	n->val.u.xval = mal(sizeof(*n->val.u.xval));
   807  	mpmovecfix(n->val.u.xval, v);
   808  	n->val.ctype = CTINT;
   809  	n->type = t;
   810  
   811  	if(isfloat[t->etype])
   812  		fatal("nodconst: bad type %T", t);
   813  }
   814  
   815  Node*
   816  nodnil(void)
   817  {
   818  	Node *c;
   819  
   820  	c = nodintconst(0);
   821  	c->val.ctype = CTNIL;
   822  	c->type = types[TNIL];
   823  	return c;
   824  }
   825  
   826  Node*
   827  nodbool(int b)
   828  {
   829  	Node *c;
   830  
   831  	c = nodintconst(0);
   832  	c->val.ctype = CTBOOL;
   833  	c->val.u.bval = b;
   834  	c->type = idealbool;
   835  	return c;
   836  }
   837  
   838  Type*
   839  aindex(Node *b, Type *t)
   840  {
   841  	Type *r;
   842  	int64 bound;
   843  
   844  	bound = -1;	// open bound
   845  	typecheck(&b, Erv);
   846  	if(b != nil) {
   847  		switch(consttype(b)) {
   848  		default:
   849  			yyerror("array bound must be an integer expression");
   850  			break;
   851  		case CTINT:
   852  		case CTRUNE:
   853  			bound = mpgetfix(b->val.u.xval);
   854  			if(bound < 0)
   855  				yyerror("array bound must be non negative");
   856  			break;
   857  		}
   858  	}
   859  
   860  	// fixed array
   861  	r = typ(TARRAY);
   862  	r->type = t;
   863  	r->bound = bound;
   864  	return r;
   865  }
   866  
   867  Node*
   868  treecopy(Node *n)
   869  {
   870  	Node *m;
   871  
   872  	if(n == N)
   873  		return N;
   874  
   875  	switch(n->op) {
   876  	default:
   877  		m = nod(OXXX, N, N);
   878  		*m = *n;
   879  		m->orig = m;
   880  		m->left = treecopy(n->left);
   881  		m->right = treecopy(n->right);
   882  		m->list = listtreecopy(n->list);
   883  		if(m->defn)
   884  			abort();
   885  		break;
   886  
   887  	case ONONAME:
   888  		if(n->sym == lookup("iota")) {
   889  			// Not sure yet whether this is the real iota,
   890  			// but make a copy of the Node* just in case,
   891  			// so that all the copies of this const definition
   892  			// don't have the same iota value.
   893  			m = nod(OXXX, N, N);
   894  			*m = *n;
   895  			m->iota = iota;
   896  			break;
   897  		}
   898  		// fall through
   899  	case ONAME:
   900  	case OLITERAL:
   901  	case OTYPE:
   902  		m = n;
   903  		break;
   904  	}
   905  	return m;
   906  }
   907  
   908  
   909  int
   910  isnil(Node *n)
   911  {
   912  	if(n == N)
   913  		return 0;
   914  	if(n->op != OLITERAL)
   915  		return 0;
   916  	if(n->val.ctype != CTNIL)
   917  		return 0;
   918  	return 1;
   919  }
   920  
   921  int
   922  isptrto(Type *t, int et)
   923  {
   924  	if(t == T)
   925  		return 0;
   926  	if(!isptr[t->etype])
   927  		return 0;
   928  	t = t->type;
   929  	if(t == T)
   930  		return 0;
   931  	if(t->etype != et)
   932  		return 0;
   933  	return 1;
   934  }
   935  
   936  int
   937  istype(Type *t, int et)
   938  {
   939  	return t != T && t->etype == et;
   940  }
   941  
   942  int
   943  isfixedarray(Type *t)
   944  {
   945  	return t != T && t->etype == TARRAY && t->bound >= 0;
   946  }
   947  
   948  int
   949  isslice(Type *t)
   950  {
   951  	return t != T && t->etype == TARRAY && t->bound < 0;
   952  }
   953  
   954  int
   955  isblank(Node *n)
   956  {
   957  	if(n == N)
   958  		return 0;
   959  	return isblanksym(n->sym);
   960  }
   961  
   962  int
   963  isblanksym(Sym *s)
   964  {
   965  	char *p;
   966  
   967  	if(s == S)
   968  		return 0;
   969  	p = s->name;
   970  	if(p == nil)
   971  		return 0;
   972  	return p[0] == '_' && p[1] == '\0';
   973  }
   974  
   975  int
   976  isinter(Type *t)
   977  {
   978  	return t != T && t->etype == TINTER;
   979  }
   980  
   981  int
   982  isnilinter(Type *t)
   983  {
   984  	if(!isinter(t))
   985  		return 0;
   986  	if(t->type != T)
   987  		return 0;
   988  	return 1;
   989  }
   990  
   991  int
   992  isideal(Type *t)
   993  {
   994  	if(t == T)
   995  		return 0;
   996  	if(t == idealstring || t == idealbool)
   997  		return 1;
   998  	switch(t->etype) {
   999  	case TNIL:
  1000  	case TIDEAL:
  1001  		return 1;
  1002  	}
  1003  	return 0;
  1004  }
  1005  
  1006  /*
  1007   * given receiver of type t (t == r or t == *r)
  1008   * return type to hang methods off (r).
  1009   */
  1010  Type*
  1011  methtype(Type *t, int mustname)
  1012  {
  1013  	if(t == T)
  1014  		return T;
  1015  
  1016  	// strip away pointer if it's there
  1017  	if(isptr[t->etype]) {
  1018  		if(t->sym != S)
  1019  			return T;
  1020  		t = t->type;
  1021  		if(t == T)
  1022  			return T;
  1023  	}
  1024  
  1025  	// need a type name
  1026  	if(t->sym == S && (mustname || t->etype != TSTRUCT))
  1027  		return T;
  1028  
  1029  	// check types
  1030  	if(!issimple[t->etype])
  1031  	switch(t->etype) {
  1032  	default:
  1033  		return T;
  1034  	case TSTRUCT:
  1035  	case TARRAY:
  1036  	case TMAP:
  1037  	case TCHAN:
  1038  	case TSTRING:
  1039  	case TFUNC:
  1040  		break;
  1041  	}
  1042  
  1043  	return t;
  1044  }
  1045  
  1046  int
  1047  cplxsubtype(int et)
  1048  {
  1049  	switch(et) {
  1050  	case TCOMPLEX64:
  1051  		return TFLOAT32;
  1052  	case TCOMPLEX128:
  1053  		return TFLOAT64;
  1054  	}
  1055  	fatal("cplxsubtype: %E\n", et);
  1056  	return 0;
  1057  }
  1058  
  1059  static int
  1060  eqnote(Strlit *a, Strlit *b)
  1061  {
  1062  	if(a == b)
  1063  		return 1;
  1064  	if(a == nil || b == nil)
  1065  		return 0;
  1066  	if(a->len != b->len)
  1067  		return 0;
  1068  	return memcmp(a->s, b->s, a->len) == 0;
  1069  }
  1070  
  1071  typedef struct TypePairList TypePairList;
  1072  struct TypePairList
  1073  {
  1074  	Type *t1;
  1075  	Type *t2;
  1076  	TypePairList *next;
  1077  };
  1078  
  1079  static int
  1080  onlist(TypePairList *l, Type *t1, Type *t2) 
  1081  {
  1082  	for(; l; l=l->next)
  1083  		if((l->t1 == t1 && l->t2 == t2) || (l->t1 == t2 && l->t2 == t1))
  1084  			return 1;
  1085  	return 0;
  1086  }
  1087  
  1088  static int eqtype1(Type*, Type*, TypePairList*);
  1089  
  1090  // Return 1 if t1 and t2 are identical, following the spec rules.
  1091  //
  1092  // Any cyclic type must go through a named type, and if one is
  1093  // named, it is only identical to the other if they are the same
  1094  // pointer (t1 == t2), so there's no chance of chasing cycles
  1095  // ad infinitum, so no need for a depth counter.
  1096  int
  1097  eqtype(Type *t1, Type *t2)
  1098  {
  1099  	return eqtype1(t1, t2, nil);
  1100  }
  1101  
  1102  static int
  1103  eqtype1(Type *t1, Type *t2, TypePairList *assumed_equal)
  1104  {
  1105  	TypePairList l;
  1106  
  1107  	if(t1 == t2)
  1108  		return 1;
  1109  	if(t1 == T || t2 == T || t1->etype != t2->etype)
  1110  		return 0;
  1111  	if(t1->sym || t2->sym) {
  1112  		// Special case: we keep byte and uint8 separate
  1113  		// for error messages.  Treat them as equal.
  1114  		switch(t1->etype) {
  1115  		case TUINT8:
  1116  			if((t1 == types[TUINT8] || t1 == bytetype) && (t2 == types[TUINT8] || t2 == bytetype))
  1117  				return 1;
  1118  			break;
  1119  		case TINT:
  1120  		case TINT32:
  1121  			if((t1 == types[runetype->etype] || t1 == runetype) && (t2 == types[runetype->etype] || t2 == runetype))
  1122  				return 1;
  1123  			break;
  1124  		}
  1125  		return 0;
  1126  	}
  1127  
  1128  	if(onlist(assumed_equal, t1, t2))
  1129  		return 1;
  1130  	l.next = assumed_equal;
  1131  	l.t1 = t1;
  1132  	l.t2 = t2;
  1133  
  1134  	switch(t1->etype) {
  1135  	case TINTER:
  1136  	case TSTRUCT:
  1137  		for(t1=t1->type, t2=t2->type; t1 && t2; t1=t1->down, t2=t2->down) {
  1138  			if(t1->etype != TFIELD || t2->etype != TFIELD)
  1139  				fatal("struct/interface missing field: %T %T", t1, t2);
  1140  			if(t1->sym != t2->sym || t1->embedded != t2->embedded || !eqtype1(t1->type, t2->type, &l) || !eqnote(t1->note, t2->note))
  1141  				goto no;
  1142  		}
  1143  		if(t1 == T && t2 == T)
  1144  			goto yes;
  1145  		goto no;
  1146  
  1147  	case TFUNC:
  1148  		// Loop over structs: receiver, in, out.
  1149  		for(t1=t1->type, t2=t2->type; t1 && t2; t1=t1->down, t2=t2->down) {
  1150  			Type *ta, *tb;
  1151  
  1152  			if(t1->etype != TSTRUCT || t2->etype != TSTRUCT)
  1153  				fatal("func missing struct: %T %T", t1, t2);
  1154  
  1155  			// Loop over fields in structs, ignoring argument names.
  1156  			for(ta=t1->type, tb=t2->type; ta && tb; ta=ta->down, tb=tb->down) {
  1157  				if(ta->etype != TFIELD || tb->etype != TFIELD)
  1158  					fatal("func struct missing field: %T %T", ta, tb);
  1159  				if(ta->isddd != tb->isddd || !eqtype1(ta->type, tb->type, &l))
  1160  					goto no;
  1161  			}
  1162  			if(ta != T || tb != T)
  1163  				goto no;
  1164  		}
  1165  		if(t1 == T && t2 == T)
  1166  			goto yes;
  1167  		goto no;
  1168  	
  1169  	case TARRAY:
  1170  		if(t1->bound != t2->bound)
  1171  			goto no;
  1172  		break;
  1173  	
  1174  	case TCHAN:
  1175  		if(t1->chan != t2->chan)
  1176  			goto no;
  1177  		break;
  1178  	}
  1179  
  1180  	if(eqtype1(t1->down, t2->down, &l) && eqtype1(t1->type, t2->type, &l))
  1181  		goto yes;
  1182  	goto no;
  1183  
  1184  yes:
  1185  	return 1;
  1186  
  1187  no:
  1188  	return 0;
  1189  }
  1190  
  1191  // Are t1 and t2 equal struct types when field names are ignored?
  1192  // For deciding whether the result struct from g can be copied
  1193  // directly when compiling f(g()).
  1194  int
  1195  eqtypenoname(Type *t1, Type *t2)
  1196  {
  1197  	if(t1 == T || t2 == T || t1->etype != TSTRUCT || t2->etype != TSTRUCT)
  1198  		return 0;
  1199  
  1200  	t1 = t1->type;
  1201  	t2 = t2->type;
  1202  	for(;;) {
  1203  		if(!eqtype(t1, t2))
  1204  			return 0;
  1205  		if(t1 == T)
  1206  			return 1;
  1207  		t1 = t1->down;
  1208  		t2 = t2->down;
  1209  	}
  1210  }
  1211  
  1212  // Is type src assignment compatible to type dst?
  1213  // If so, return op code to use in conversion.
  1214  // If not, return 0.
  1215  int
  1216  assignop(Type *src, Type *dst, char **why)
  1217  {
  1218  	Type *missing, *have;
  1219  	int ptr;
  1220  
  1221  	if(why != nil)
  1222  		*why = "";
  1223  
  1224  	// TODO(rsc,lvd): This behaves poorly in the presence of inlining.
  1225  	// https://code.google.com/p/go/issues/detail?id=2795
  1226  	if(safemode && importpkg == nil && src != T && src->etype == TUNSAFEPTR) {
  1227  		yyerror("cannot use unsafe.Pointer");
  1228  		errorexit();
  1229  	}
  1230  
  1231  	if(src == dst)
  1232  		return OCONVNOP;
  1233  	if(src == T || dst == T || src->etype == TFORW || dst->etype == TFORW || src->orig == T || dst->orig == T)
  1234  		return 0;
  1235  
  1236  	// 1. src type is identical to dst.
  1237  	if(eqtype(src, dst))
  1238  		return OCONVNOP;
  1239  	
  1240  	// 2. src and dst have identical underlying types
  1241  	// and either src or dst is not a named type or
  1242  	// both are interface types.
  1243  	if(eqtype(src->orig, dst->orig) && (src->sym == S || dst->sym == S || src->etype == TINTER))
  1244  		return OCONVNOP;
  1245  
  1246  	// 3. dst is an interface type and src implements dst.
  1247  	if(dst->etype == TINTER && src->etype != TNIL) {
  1248  		if(implements(src, dst, &missing, &have, &ptr))
  1249  			return OCONVIFACE;
  1250  
  1251  		// we'll have complained about this method anyway, supress spurious messages.
  1252  		if(have && have->sym == missing->sym && (have->type->broke || missing->type->broke))
  1253  			return OCONVIFACE;
  1254  
  1255  		if(why != nil) {
  1256  			if(isptrto(src, TINTER))
  1257  				*why = smprint(":\n\t%T is pointer to interface, not interface", src);
  1258  			else if(have && have->sym == missing->sym)
  1259  				*why = smprint(":\n\t%T does not implement %T (wrong type for %S method)\n"
  1260  					"\t\thave %S%hhT\n\t\twant %S%hhT", src, dst, missing->sym,
  1261  					have->sym, have->type, missing->sym, missing->type);
  1262  			else if(ptr)
  1263  				*why = smprint(":\n\t%T does not implement %T (%S method requires pointer receiver)",
  1264  					src, dst, missing->sym);
  1265  			else if(have)
  1266  				*why = smprint(":\n\t%T does not implement %T (missing %S method)\n"
  1267  					"\t\thave %S%hhT\n\t\twant %S%hhT", src, dst, missing->sym,
  1268  					have->sym, have->type, missing->sym, missing->type);
  1269  			else
  1270  				*why = smprint(":\n\t%T does not implement %T (missing %S method)",
  1271  					src, dst, missing->sym);
  1272  		}
  1273  		return 0;
  1274  	}
  1275  	if(isptrto(dst, TINTER)) {
  1276  		if(why != nil)
  1277  			*why = smprint(":\n\t%T is pointer to interface, not interface", dst);
  1278  		return 0;
  1279  	}
  1280  	if(src->etype == TINTER && dst->etype != TBLANK) {
  1281  		if(why != nil && implements(dst, src, &missing, &have, &ptr))
  1282  			*why = ": need type assertion";
  1283  		return 0;
  1284  	}
  1285  
  1286  	// 4. src is a bidirectional channel value, dst is a channel type,
  1287  	// src and dst have identical element types, and
  1288  	// either src or dst is not a named type.
  1289  	if(src->etype == TCHAN && src->chan == Cboth && dst->etype == TCHAN)
  1290  	if(eqtype(src->type, dst->type) && (src->sym == S || dst->sym == S))
  1291  		return OCONVNOP;
  1292  
  1293  	// 5. src is the predeclared identifier nil and dst is a nillable type.
  1294  	if(src->etype == TNIL) {
  1295  		switch(dst->etype) {
  1296  		case TARRAY:
  1297  			if(dst->bound != -100)	// not slice
  1298  				break;
  1299  		case TPTR32:
  1300  		case TPTR64:
  1301  		case TFUNC:
  1302  		case TMAP:
  1303  		case TCHAN:
  1304  		case TINTER:
  1305  			return OCONVNOP;
  1306  		}
  1307  	}
  1308  
  1309  	// 6. rule about untyped constants - already converted by defaultlit.
  1310  	
  1311  	// 7. Any typed value can be assigned to the blank identifier.
  1312  	if(dst->etype == TBLANK)
  1313  		return OCONVNOP;
  1314  
  1315  	return 0;
  1316  }
  1317  
  1318  // Can we convert a value of type src to a value of type dst?
  1319  // If so, return op code to use in conversion (maybe OCONVNOP).
  1320  // If not, return 0.
  1321  int
  1322  convertop(Type *src, Type *dst, char **why)
  1323  {
  1324  	int op;
  1325  	
  1326  	if(why != nil)
  1327  		*why = "";
  1328  
  1329  	if(src == dst)
  1330  		return OCONVNOP;
  1331  	if(src == T || dst == T)
  1332  		return 0;
  1333  	
  1334  	// 1. src can be assigned to dst.
  1335  	if((op = assignop(src, dst, why)) != 0)
  1336  		return op;
  1337  
  1338  	// The rules for interfaces are no different in conversions
  1339  	// than assignments.  If interfaces are involved, stop now
  1340  	// with the good message from assignop.
  1341  	// Otherwise clear the error.
  1342  	if(src->etype == TINTER || dst->etype == TINTER)
  1343  		return 0;
  1344  	if(why != nil)
  1345  		*why = "";
  1346  
  1347  	// 2. src and dst have identical underlying types.
  1348  	if(eqtype(src->orig, dst->orig))
  1349  		return OCONVNOP;
  1350  	
  1351  	// 3. src and dst are unnamed pointer types 
  1352  	// and their base types have identical underlying types.
  1353  	if(isptr[src->etype] && isptr[dst->etype] && src->sym == S && dst->sym == S)
  1354  	if(eqtype(src->type->orig, dst->type->orig))
  1355  		return OCONVNOP;
  1356  
  1357  	// 4. src and dst are both integer or floating point types.
  1358  	if((isint[src->etype] || isfloat[src->etype]) && (isint[dst->etype] || isfloat[dst->etype])) {
  1359  		if(simtype[src->etype] == simtype[dst->etype])
  1360  			return OCONVNOP;
  1361  		return OCONV;
  1362  	}
  1363  
  1364  	// 5. src and dst are both complex types.
  1365  	if(iscomplex[src->etype] && iscomplex[dst->etype]) {
  1366  		if(simtype[src->etype] == simtype[dst->etype])
  1367  			return OCONVNOP;
  1368  		return OCONV;
  1369  	}
  1370  
  1371  	// 6. src is an integer or has type []byte or []rune
  1372  	// and dst is a string type.
  1373  	if(isint[src->etype] && dst->etype == TSTRING)
  1374  		return ORUNESTR;
  1375  
  1376  	if(isslice(src) && dst->etype == TSTRING) {
  1377  		if(src->type->etype == bytetype->etype)
  1378  			return OARRAYBYTESTR;
  1379  		if(src->type->etype == runetype->etype)
  1380  			return OARRAYRUNESTR;
  1381  	}
  1382  	
  1383  	// 7. src is a string and dst is []byte or []rune.
  1384  	// String to slice.
  1385  	if(src->etype == TSTRING && isslice(dst)) {
  1386  		if(dst->type->etype == bytetype->etype)
  1387  			return OSTRARRAYBYTE;
  1388  		if(dst->type->etype == runetype->etype)
  1389  			return OSTRARRAYRUNE;
  1390  	}
  1391  	
  1392  	// 8. src is a pointer or uintptr and dst is unsafe.Pointer.
  1393  	if((isptr[src->etype] || src->etype == TUINTPTR) && dst->etype == TUNSAFEPTR)
  1394  		return OCONVNOP;
  1395  
  1396  	// 9. src is unsafe.Pointer and dst is a pointer or uintptr.
  1397  	if(src->etype == TUNSAFEPTR && (isptr[dst->etype] || dst->etype == TUINTPTR))
  1398  		return OCONVNOP;
  1399  
  1400  	return 0;
  1401  }
  1402  
  1403  // Convert node n for assignment to type t.
  1404  Node*
  1405  assignconv(Node *n, Type *t, char *context)
  1406  {
  1407  	int op;
  1408  	Node *r, *old;
  1409  	char *why;
  1410  	
  1411  	if(n == N || n->type == T || n->type->broke)
  1412  		return n;
  1413  
  1414  	old = n;
  1415  	old->diag++;  // silence errors about n; we'll issue one below
  1416  	defaultlit(&n, t);
  1417  	old->diag--;
  1418  	if(t->etype == TBLANK)
  1419  		return n;
  1420  
  1421  	// Convert ideal bool from comparison to plain bool
  1422  	// if the next step is non-bool (like interface{}).
  1423  	if(n->type == idealbool && t->etype != TBOOL) {
  1424  		if(n->op == ONAME || n->op == OLITERAL) {
  1425  			r = nod(OCONVNOP, n, N);
  1426  			r->type = types[TBOOL];
  1427  			r->typecheck = 1;
  1428  			r->implicit = 1;
  1429  			n = r;
  1430  		}
  1431  	}
  1432  
  1433  	if(eqtype(n->type, t))
  1434  		return n;
  1435  
  1436  	op = assignop(n->type, t, &why);
  1437  	if(op == 0) {
  1438  		yyerror("cannot use %lN as type %T in %s%s", n, t, context, why);
  1439  		op = OCONV;
  1440  	}
  1441  
  1442  	r = nod(op, n, N);
  1443  	r->type = t;
  1444  	r->typecheck = 1;
  1445  	r->implicit = 1;
  1446  	r->orig = n->orig;
  1447  	return r;
  1448  }
  1449  
  1450  static int
  1451  subtype(Type **stp, Type *t, int d)
  1452  {
  1453  	Type *st;
  1454  
  1455  loop:
  1456  	st = *stp;
  1457  	if(st == T)
  1458  		return 0;
  1459  
  1460  	d++;
  1461  	if(d >= 10)
  1462  		return 0;
  1463  
  1464  	switch(st->etype) {
  1465  	default:
  1466  		return 0;
  1467  
  1468  	case TPTR32:
  1469  	case TPTR64:
  1470  	case TCHAN:
  1471  	case TARRAY:
  1472  		stp = &st->type;
  1473  		goto loop;
  1474  
  1475  	case TANY:
  1476  		if(!st->copyany)
  1477  			return 0;
  1478  		*stp = t;
  1479  		break;
  1480  
  1481  	case TMAP:
  1482  		if(subtype(&st->down, t, d))
  1483  			break;
  1484  		stp = &st->type;
  1485  		goto loop;
  1486  
  1487  	case TFUNC:
  1488  		for(;;) {
  1489  			if(subtype(&st->type, t, d))
  1490  				break;
  1491  			if(subtype(&st->type->down->down, t, d))
  1492  				break;
  1493  			if(subtype(&st->type->down, t, d))
  1494  				break;
  1495  			return 0;
  1496  		}
  1497  		break;
  1498  
  1499  	case TSTRUCT:
  1500  		for(st=st->type; st!=T; st=st->down)
  1501  			if(subtype(&st->type, t, d))
  1502  				return 1;
  1503  		return 0;
  1504  	}
  1505  	return 1;
  1506  }
  1507  
  1508  /*
  1509   * Is this a 64-bit type?
  1510   */
  1511  int
  1512  is64(Type *t)
  1513  {
  1514  	if(t == T)
  1515  		return 0;
  1516  	switch(simtype[t->etype]) {
  1517  	case TINT64:
  1518  	case TUINT64:
  1519  	case TPTR64:
  1520  		return 1;
  1521  	}
  1522  	return 0;
  1523  }
  1524  
  1525  /*
  1526   * Is a conversion between t1 and t2 a no-op?
  1527   */
  1528  int
  1529  noconv(Type *t1, Type *t2)
  1530  {
  1531  	int e1, e2;
  1532  
  1533  	e1 = simtype[t1->etype];
  1534  	e2 = simtype[t2->etype];
  1535  
  1536  	switch(e1) {
  1537  	case TINT8:
  1538  	case TUINT8:
  1539  		return e2 == TINT8 || e2 == TUINT8;
  1540  
  1541  	case TINT16:
  1542  	case TUINT16:
  1543  		return e2 == TINT16 || e2 == TUINT16;
  1544  
  1545  	case TINT32:
  1546  	case TUINT32:
  1547  	case TPTR32:
  1548  		return e2 == TINT32 || e2 == TUINT32 || e2 == TPTR32;
  1549  
  1550  	case TINT64:
  1551  	case TUINT64:
  1552  	case TPTR64:
  1553  		return e2 == TINT64 || e2 == TUINT64 || e2 == TPTR64;
  1554  
  1555  	case TFLOAT32:
  1556  		return e2 == TFLOAT32;
  1557  
  1558  	case TFLOAT64:
  1559  		return e2 == TFLOAT64;
  1560  	}
  1561  	return 0;
  1562  }
  1563  
  1564  void
  1565  argtype(Node *on, Type *t)
  1566  {
  1567  	dowidth(t);
  1568  	if(!subtype(&on->type, t, 0))
  1569  		fatal("argtype: failed %N %T\n", on, t);
  1570  }
  1571  
  1572  Type*
  1573  shallow(Type *t)
  1574  {
  1575  	Type *nt;
  1576  
  1577  	if(t == T)
  1578  		return T;
  1579  	nt = typ(0);
  1580  	*nt = *t;
  1581  	if(t->orig == t)
  1582  		nt->orig = nt;
  1583  	return nt;
  1584  }
  1585  
  1586  static Type*
  1587  deep(Type *t)
  1588  {
  1589  	Type *nt, *xt;
  1590  
  1591  	if(t == T)
  1592  		return T;
  1593  
  1594  	switch(t->etype) {
  1595  	default:
  1596  		nt = t;	// share from here down
  1597  		break;
  1598  
  1599  	case TANY:
  1600  		nt = shallow(t);
  1601  		nt->copyany = 1;
  1602  		break;
  1603  
  1604  	case TPTR32:
  1605  	case TPTR64:
  1606  	case TCHAN:
  1607  	case TARRAY:
  1608  		nt = shallow(t);
  1609  		nt->type = deep(t->type);
  1610  		break;
  1611  
  1612  	case TMAP:
  1613  		nt = shallow(t);
  1614  		nt->down = deep(t->down);
  1615  		nt->type = deep(t->type);
  1616  		break;
  1617  
  1618  	case TFUNC:
  1619  		nt = shallow(t);
  1620  		nt->type = deep(t->type);
  1621  		nt->type->down = deep(t->type->down);
  1622  		nt->type->down->down = deep(t->type->down->down);
  1623  		break;
  1624  
  1625  	case TSTRUCT:
  1626  		nt = shallow(t);
  1627  		nt->type = shallow(t->type);
  1628  		xt = nt->type;
  1629  
  1630  		for(t=t->type; t!=T; t=t->down) {
  1631  			xt->type = deep(t->type);
  1632  			xt->down = shallow(t->down);
  1633  			xt = xt->down;
  1634  		}
  1635  		break;
  1636  	}
  1637  	return nt;
  1638  }
  1639  
  1640  Node*
  1641  syslook(char *name, int copy)
  1642  {
  1643  	Sym *s;
  1644  	Node *n;
  1645  
  1646  	s = pkglookup(name, runtimepkg);
  1647  	if(s == S || s->def == N)
  1648  		fatal("syslook: can't find runtime.%s", name);
  1649  
  1650  	if(!copy)
  1651  		return s->def;
  1652  
  1653  	n = nod(0, N, N);
  1654  	*n = *s->def;
  1655  	n->type = deep(s->def->type);
  1656  
  1657  	return n;
  1658  }
  1659  
  1660  /*
  1661   * compute a hash value for type t.
  1662   * if t is a method type, ignore the receiver
  1663   * so that the hash can be used in interface checks.
  1664   * %T already contains
  1665   * all the necessary logic to generate a representation
  1666   * of the type that completely describes it.
  1667   * using smprint here avoids duplicating that code.
  1668   * using md5 here is overkill, but i got tired of
  1669   * accidental collisions making the runtime think
  1670   * two types are equal when they really aren't.
  1671   */
  1672  uint32
  1673  typehash(Type *t)
  1674  {
  1675  	char *p;
  1676  	MD5 d;
  1677  
  1678  	if(t->thistuple) {
  1679  		// hide method receiver from Tpretty
  1680  		t->thistuple = 0;
  1681  		p = smprint("%-uT", t);
  1682  		t->thistuple = 1;
  1683  	} else
  1684  		p = smprint("%-uT", t);
  1685  	//print("typehash: %s\n", p);
  1686  	md5reset(&d);
  1687  	md5write(&d, (uchar*)p, strlen(p));
  1688  	free(p);
  1689  	return md5sum(&d);
  1690  }
  1691  
  1692  Type*
  1693  ptrto(Type *t)
  1694  {
  1695  	Type *t1;
  1696  
  1697  	if(tptr == 0)
  1698  		fatal("ptrto: no tptr");
  1699  	t1 = typ(tptr);
  1700  	t1->type = t;
  1701  	t1->width = widthptr;
  1702  	t1->align = widthptr;
  1703  	return t1;
  1704  }
  1705  
  1706  void
  1707  frame(int context)
  1708  {
  1709  	char *p;
  1710  	NodeList *l;
  1711  	Node *n;
  1712  	int flag;
  1713  
  1714  	p = "stack";
  1715  	l = nil;
  1716  	if(curfn)
  1717  		l = curfn->dcl;
  1718  	if(context) {
  1719  		p = "external";
  1720  		l = externdcl;
  1721  	}
  1722  
  1723  	flag = 1;
  1724  	for(; l; l=l->next) {
  1725  		n = l->n;
  1726  		switch(n->op) {
  1727  		case ONAME:
  1728  			if(flag)
  1729  				print("--- %s frame ---\n", p);
  1730  			print("%O %S G%d %T\n", n->op, n->sym, n->vargen, n->type);
  1731  			flag = 0;
  1732  			break;
  1733  
  1734  		case OTYPE:
  1735  			if(flag)
  1736  				print("--- %s frame ---\n", p);
  1737  			print("%O %T\n", n->op, n->type);
  1738  			flag = 0;
  1739  			break;
  1740  		}
  1741  	}
  1742  }
  1743  
  1744  /*
  1745   * calculate sethi/ullman number
  1746   * roughly how many registers needed to
  1747   * compile a node. used to compile the
  1748   * hardest side first to minimize registers.
  1749   */
  1750  void
  1751  ullmancalc(Node *n)
  1752  {
  1753  	int ul, ur;
  1754  
  1755  	if(n == N)
  1756  		return;
  1757  
  1758  	if(n->ninit != nil) {
  1759  		ul = UINF;
  1760  		goto out;
  1761  	}
  1762  
  1763  	switch(n->op) {
  1764  	case OREGISTER:
  1765  	case OLITERAL:
  1766  	case ONAME:
  1767  		ul = 1;
  1768  		if(n->class == PPARAMREF || (n->class & PHEAP))
  1769  			ul++;
  1770  		goto out;
  1771  	case OCALL:
  1772  	case OCALLFUNC:
  1773  	case OCALLMETH:
  1774  	case OCALLINTER:
  1775  		ul = UINF;
  1776  		goto out;
  1777  	case OANDAND:
  1778  	case OOROR:
  1779  		// hard with race detector
  1780  		if(flag_race) {
  1781  			ul = UINF;
  1782  			goto out;
  1783  		}
  1784  	}
  1785  	ul = 1;
  1786  	if(n->left != N)
  1787  		ul = n->left->ullman;
  1788  	ur = 1;
  1789  	if(n->right != N)
  1790  		ur = n->right->ullman;
  1791  	if(ul == ur)
  1792  		ul += 1;
  1793  	if(ur > ul)
  1794  		ul = ur;
  1795  
  1796  out:
  1797  	if(ul > 200)
  1798  		ul = 200; // clamp to uchar with room to grow
  1799  	n->ullman = ul;
  1800  }
  1801  
  1802  void
  1803  badtype(int o, Type *tl, Type *tr)
  1804  {
  1805  	Fmt fmt;
  1806  	char *s;
  1807  	
  1808  	fmtstrinit(&fmt);
  1809  	if(tl != T)
  1810  		fmtprint(&fmt, "\n	%T", tl);
  1811  	if(tr != T)
  1812  		fmtprint(&fmt, "\n	%T", tr);
  1813  
  1814  	// common mistake: *struct and *interface.
  1815  	if(tl && tr && isptr[tl->etype] && isptr[tr->etype]) {
  1816  		if(tl->type->etype == TSTRUCT && tr->type->etype == TINTER)
  1817  			fmtprint(&fmt, "\n	(*struct vs *interface)");
  1818  		else if(tl->type->etype == TINTER && tr->type->etype == TSTRUCT)
  1819  			fmtprint(&fmt, "\n	(*interface vs *struct)");
  1820  	}
  1821  	s = fmtstrflush(&fmt);
  1822  	yyerror("illegal types for operand: %O%s", o, s);
  1823  }
  1824  
  1825  /*
  1826   * iterator to walk a structure declaration
  1827   */
  1828  Type*
  1829  structfirst(Iter *s, Type **nn)
  1830  {
  1831  	Type *n, *t;
  1832  
  1833  	n = *nn;
  1834  	if(n == T)
  1835  		goto bad;
  1836  
  1837  	switch(n->etype) {
  1838  	default:
  1839  		goto bad;
  1840  
  1841  	case TSTRUCT:
  1842  	case TINTER:
  1843  	case TFUNC:
  1844  		break;
  1845  	}
  1846  
  1847  	t = n->type;
  1848  	if(t == T)
  1849  		goto rnil;
  1850  
  1851  	if(t->etype != TFIELD)
  1852  		fatal("structfirst: not field %T", t);
  1853  
  1854  	s->t = t;
  1855  	return t;
  1856  
  1857  bad:
  1858  	fatal("structfirst: not struct %T", n);
  1859  
  1860  rnil:
  1861  	return T;
  1862  }
  1863  
  1864  Type*
  1865  structnext(Iter *s)
  1866  {
  1867  	Type *n, *t;
  1868  
  1869  	n = s->t;
  1870  	t = n->down;
  1871  	if(t == T)
  1872  		goto rnil;
  1873  
  1874  	if(t->etype != TFIELD)
  1875  		goto bad;
  1876  
  1877  	s->t = t;
  1878  	return t;
  1879  
  1880  bad:
  1881  	fatal("structnext: not struct %T", n);
  1882  
  1883  rnil:
  1884  	return T;
  1885  }
  1886  
  1887  /*
  1888   * iterator to this and inargs in a function
  1889   */
  1890  Type*
  1891  funcfirst(Iter *s, Type *t)
  1892  {
  1893  	Type *fp;
  1894  
  1895  	if(t == T)
  1896  		goto bad;
  1897  
  1898  	if(t->etype != TFUNC)
  1899  		goto bad;
  1900  
  1901  	s->tfunc = t;
  1902  	s->done = 0;
  1903  	fp = structfirst(s, getthis(t));
  1904  	if(fp == T) {
  1905  		s->done = 1;
  1906  		fp = structfirst(s, getinarg(t));
  1907  	}
  1908  	return fp;
  1909  
  1910  bad:
  1911  	fatal("funcfirst: not func %T", t);
  1912  	return T;
  1913  }
  1914  
  1915  Type*
  1916  funcnext(Iter *s)
  1917  {
  1918  	Type *fp;
  1919  
  1920  	fp = structnext(s);
  1921  	if(fp == T && !s->done) {
  1922  		s->done = 1;
  1923  		fp = structfirst(s, getinarg(s->tfunc));
  1924  	}
  1925  	return fp;
  1926  }
  1927  
  1928  Type**
  1929  getthis(Type *t)
  1930  {
  1931  	if(t->etype != TFUNC)
  1932  		fatal("getthis: not a func %T", t);
  1933  	return &t->type;
  1934  }
  1935  
  1936  Type**
  1937  getoutarg(Type *t)
  1938  {
  1939  	if(t->etype != TFUNC)
  1940  		fatal("getoutarg: not a func %T", t);
  1941  	return &t->type->down;
  1942  }
  1943  
  1944  Type**
  1945  getinarg(Type *t)
  1946  {
  1947  	if(t->etype != TFUNC)
  1948  		fatal("getinarg: not a func %T", t);
  1949  	return &t->type->down->down;
  1950  }
  1951  
  1952  Type*
  1953  getthisx(Type *t)
  1954  {
  1955  	return *getthis(t);
  1956  }
  1957  
  1958  Type*
  1959  getoutargx(Type *t)
  1960  {
  1961  	return *getoutarg(t);
  1962  }
  1963  
  1964  Type*
  1965  getinargx(Type *t)
  1966  {
  1967  	return *getinarg(t);
  1968  }
  1969  
  1970  /*
  1971   * return !(op)
  1972   * eg == <=> !=
  1973   */
  1974  int
  1975  brcom(int a)
  1976  {
  1977  	switch(a) {
  1978  	case OEQ:	return ONE;
  1979  	case ONE:	return OEQ;
  1980  	case OLT:	return OGE;
  1981  	case OGT:	return OLE;
  1982  	case OLE:	return OGT;
  1983  	case OGE:	return OLT;
  1984  	}
  1985  	fatal("brcom: no com for %A\n", a);
  1986  	return a;
  1987  }
  1988  
  1989  /*
  1990   * return reverse(op)
  1991   * eg a op b <=> b r(op) a
  1992   */
  1993  int
  1994  brrev(int a)
  1995  {
  1996  	switch(a) {
  1997  	case OEQ:	return OEQ;
  1998  	case ONE:	return ONE;
  1999  	case OLT:	return OGT;
  2000  	case OGT:	return OLT;
  2001  	case OLE:	return OGE;
  2002  	case OGE:	return OLE;
  2003  	}
  2004  	fatal("brcom: no rev for %A\n", a);
  2005  	return a;
  2006  }
  2007  
  2008  /*
  2009   * return side effect-free n, appending side effects to init.
  2010   * result is assignable if n is.
  2011   */
  2012  Node*
  2013  safeexpr(Node *n, NodeList **init)
  2014  {
  2015  	Node *l;
  2016  	Node *r;
  2017  	Node *a;
  2018  
  2019  	if(n == N)
  2020  		return N;
  2021  
  2022  	if(n->ninit) {
  2023  		walkstmtlist(n->ninit);
  2024  		*init = concat(*init, n->ninit);
  2025  		n->ninit = nil;
  2026  	}
  2027  
  2028  	switch(n->op) {
  2029  	case ONAME:
  2030  	case OLITERAL:
  2031  		return n;
  2032  
  2033  	case ODOT:
  2034  		l = safeexpr(n->left, init);
  2035  		if(l == n->left)
  2036  			return n;
  2037  		r = nod(OXXX, N, N);
  2038  		*r = *n;
  2039  		r->left = l;
  2040  		typecheck(&r, Erv);
  2041  		walkexpr(&r, init);
  2042  		return r;
  2043  
  2044  	case ODOTPTR:
  2045  	case OIND:
  2046  		l = safeexpr(n->left, init);
  2047  		if(l == n->left)
  2048  			return n;
  2049  		a = nod(OXXX, N, N);
  2050  		*a = *n;
  2051  		a->left = l;
  2052  		walkexpr(&a, init);
  2053  		return a;
  2054  
  2055  	case OINDEX:
  2056  	case OINDEXMAP:
  2057  		l = safeexpr(n->left, init);
  2058  		r = safeexpr(n->right, init);
  2059  		if(l == n->left && r == n->right)
  2060  			return n;
  2061  		a = nod(OXXX, N, N);
  2062  		*a = *n;
  2063  		a->left = l;
  2064  		a->right = r;
  2065  		walkexpr(&a, init);
  2066  		return a;
  2067  	}
  2068  
  2069  	// make a copy; must not be used as an lvalue
  2070  	if(islvalue(n))
  2071  		fatal("missing lvalue case in safeexpr: %N", n);
  2072  	return cheapexpr(n, init);
  2073  }
  2074  
  2075  Node*
  2076  copyexpr(Node *n, Type *t, NodeList **init)
  2077  {
  2078  	Node *a, *l;
  2079  	
  2080  	l = temp(t);
  2081  	a = nod(OAS, l, n);
  2082  	typecheck(&a, Etop);
  2083  	walkexpr(&a, init);
  2084  	*init = list(*init, a);
  2085  	return l;
  2086  }
  2087  
  2088  /*
  2089   * return side-effect free and cheap n, appending side effects to init.
  2090   * result may not be assignable.
  2091   */
  2092  Node*
  2093  cheapexpr(Node *n, NodeList **init)
  2094  {
  2095  	switch(n->op) {
  2096  	case ONAME:
  2097  	case OLITERAL:
  2098  		return n;
  2099  	}
  2100  
  2101  	return copyexpr(n, n->type, init);
  2102  }
  2103  
  2104  /*
  2105   * return n in a local variable of type t if it is not already.
  2106   * the value is guaranteed not to change except by direct
  2107   * assignment to it.
  2108   */
  2109  Node*
  2110  localexpr(Node *n, Type *t, NodeList **init)
  2111  {
  2112  	if(n->op == ONAME && !n->addrtaken &&
  2113  		(n->class == PAUTO || n->class == PPARAM || n->class == PPARAMOUT) &&
  2114  		convertop(n->type, t, nil) == OCONVNOP)
  2115  		return n;
  2116  	
  2117  	return copyexpr(n, t, init);
  2118  }
  2119  
  2120  void
  2121  setmaxarg(Type *t)
  2122  {
  2123  	int64 w;
  2124  
  2125  	dowidth(t);
  2126  	w = t->argwid;
  2127  	if(t->argwid >= MAXWIDTH)
  2128  		fatal("bad argwid %T", t);
  2129  	if(w > maxarg)
  2130  		maxarg = w;
  2131  }
  2132  
  2133  /*
  2134   * unicode-aware case-insensitive strcmp
  2135   */
  2136  
  2137  static int
  2138  ucistrcmp(char *p, char *q)
  2139  {
  2140  	Rune rp, rq;
  2141  
  2142  	while(*p || *q) {
  2143  		if(*p == 0)
  2144  			return +1;
  2145  		if(*q == 0)
  2146  			return -1;
  2147  		p += chartorune(&rp, p);
  2148  		q += chartorune(&rq, q);
  2149  		rp = tolowerrune(rp);
  2150  		rq = tolowerrune(rq);
  2151  		if(rp < rq)
  2152  			return -1;
  2153  		if(rp > rq)
  2154  			return +1;
  2155  	}
  2156  	return 0;
  2157  }
  2158  
  2159  /*
  2160   * code to resolve elided DOTs
  2161   * in embedded types
  2162   */
  2163  
  2164  // search depth 0 --
  2165  // return count of fields+methods
  2166  // found with a given name
  2167  static int
  2168  lookdot0(Sym *s, Type *t, Type **save, int ignorecase)
  2169  {
  2170  	Type *f, *u;
  2171  	int c;
  2172  
  2173  	u = t;
  2174  	if(isptr[u->etype])
  2175  		u = u->type;
  2176  
  2177  	c = 0;
  2178  	if(u->etype == TSTRUCT || u->etype == TINTER) {
  2179  		for(f=u->type; f!=T; f=f->down)
  2180  			if(f->sym == s || (ignorecase && ucistrcmp(f->sym->name, s->name) == 0)) {
  2181  				if(save)
  2182  					*save = f;
  2183  				c++;
  2184  			}
  2185  	}
  2186  	u = methtype(t, 0);
  2187  	if(u != T) {
  2188  		for(f=u->method; f!=T; f=f->down)
  2189  			if(f->embedded == 0 && (f->sym == s || (ignorecase && ucistrcmp(f->sym->name, s->name) == 0))) {
  2190  				if(save)
  2191  					*save = f;
  2192  				c++;
  2193  			}
  2194  	}
  2195  	return c;
  2196  }
  2197  
  2198  // search depth d for field/method s --
  2199  // return count of fields+methods
  2200  // found at search depth.
  2201  // answer is in dotlist array and
  2202  // count of number of ways is returned.
  2203  int
  2204  adddot1(Sym *s, Type *t, int d, Type **save, int ignorecase)
  2205  {
  2206  	Type *f, *u;
  2207  	int c, a;
  2208  
  2209  	if(t->trecur)
  2210  		return 0;
  2211  	t->trecur = 1;
  2212  
  2213  	if(d == 0) {
  2214  		c = lookdot0(s, t, save, ignorecase);
  2215  		goto out;
  2216  	}
  2217  
  2218  	c = 0;
  2219  	u = t;
  2220  	if(isptr[u->etype])
  2221  		u = u->type;
  2222  	if(u->etype != TSTRUCT && u->etype != TINTER)
  2223  		goto out;
  2224  
  2225  	d--;
  2226  	for(f=u->type; f!=T; f=f->down) {
  2227  		if(!f->embedded)
  2228  			continue;
  2229  		if(f->sym == S)
  2230  			continue;
  2231  		a = adddot1(s, f->type, d, save, ignorecase);
  2232  		if(a != 0 && c == 0)
  2233  			dotlist[d].field = f;
  2234  		c += a;
  2235  	}
  2236  
  2237  out:
  2238  	t->trecur = 0;
  2239  	return c;
  2240  }
  2241  
  2242  // in T.field
  2243  // find missing fields that
  2244  // will give shortest unique addressing.
  2245  // modify the tree with missing type names.
  2246  Node*
  2247  adddot(Node *n)
  2248  {
  2249  	Type *t;
  2250  	Sym *s;
  2251  	int c, d;
  2252  
  2253  	typecheck(&n->left, Etype|Erv);
  2254  	t = n->left->type;
  2255  	if(t == T)
  2256  		goto ret;
  2257  	
  2258  	if(n->left->op == OTYPE)
  2259  		goto ret;
  2260  
  2261  	if(n->right->op != ONAME)
  2262  		goto ret;
  2263  	s = n->right->sym;
  2264  	if(s == S)
  2265  		goto ret;
  2266  
  2267  	for(d=0; d<nelem(dotlist); d++) {
  2268  		c = adddot1(s, t, d, nil, 0);
  2269  		if(c > 0)
  2270  			goto out;
  2271  	}
  2272  	goto ret;
  2273  
  2274  out:
  2275  	if(c > 1) {
  2276  		yyerror("ambiguous selector %N", n);
  2277  		n->left = N;
  2278  		return n;
  2279  	}
  2280  
  2281  	// rebuild elided dots
  2282  	for(c=d-1; c>=0; c--)
  2283  		n->left = nod(ODOT, n->left, newname(dotlist[c].field->sym));
  2284  ret:
  2285  	return n;
  2286  }
  2287  
  2288  
  2289  /*
  2290   * code to help generate trampoline
  2291   * functions for methods on embedded
  2292   * subtypes.
  2293   * these are approx the same as
  2294   * the corresponding adddot routines
  2295   * except that they expect to be called
  2296   * with unique tasks and they return
  2297   * the actual methods.
  2298   */
  2299  
  2300  typedef	struct	Symlink	Symlink;
  2301  struct	Symlink
  2302  {
  2303  	Type*		field;
  2304  	uchar		good;
  2305  	uchar		followptr;
  2306  	Symlink*	link;
  2307  };
  2308  static	Symlink*	slist;
  2309  
  2310  static void
  2311  expand0(Type *t, int followptr)
  2312  {
  2313  	Type *f, *u;
  2314  	Symlink *sl;
  2315  
  2316  	u = t;
  2317  	if(isptr[u->etype]) {
  2318  		followptr = 1;
  2319  		u = u->type;
  2320  	}
  2321  
  2322  	if(u->etype == TINTER) {
  2323  		for(f=u->type; f!=T; f=f->down) {
  2324  			if(f->sym->flags & SymUniq)
  2325  				continue;
  2326  			f->sym->flags |= SymUniq;
  2327  			sl = mal(sizeof(*sl));
  2328  			sl->field = f;
  2329  			sl->link = slist;
  2330  			sl->followptr = followptr;
  2331  			slist = sl;
  2332  		}
  2333  		return;
  2334  	}
  2335  
  2336  	u = methtype(t, 0);
  2337  	if(u != T) {
  2338  		for(f=u->method; f!=T; f=f->down) {
  2339  			if(f->sym->flags & SymUniq)
  2340  				continue;
  2341  			f->sym->flags |= SymUniq;
  2342  			sl = mal(sizeof(*sl));
  2343  			sl->field = f;
  2344  			sl->link = slist;
  2345  			sl->followptr = followptr;
  2346  			slist = sl;
  2347  		}
  2348  	}
  2349  }
  2350  
  2351  static void
  2352  expand1(Type *t, int d, int followptr)
  2353  {
  2354  	Type *f, *u;
  2355  
  2356  	if(t->trecur)
  2357  		return;
  2358  	if(d == 0)
  2359  		return;
  2360  	t->trecur = 1;
  2361  
  2362  	if(d != nelem(dotlist)-1)
  2363  		expand0(t, followptr);
  2364  
  2365  	u = t;
  2366  	if(isptr[u->etype]) {
  2367  		followptr = 1;
  2368  		u = u->type;
  2369  	}
  2370  	if(u->etype != TSTRUCT && u->etype != TINTER)
  2371  		goto out;
  2372  
  2373  	for(f=u->type; f!=T; f=f->down) {
  2374  		if(!f->embedded)
  2375  			continue;
  2376  		if(f->sym == S)
  2377  			continue;
  2378  		expand1(f->type, d-1, followptr);
  2379  	}
  2380  
  2381  out:
  2382  	t->trecur = 0;
  2383  }
  2384  
  2385  void
  2386  expandmeth(Type *t)
  2387  {
  2388  	Symlink *sl;
  2389  	Type *f;
  2390  	int c, d;
  2391  
  2392  	if(t == T || t->xmethod != nil)
  2393  		return;
  2394  
  2395  	// mark top-level method symbols
  2396  	// so that expand1 doesn't consider them.
  2397  	for(f=t->method; f != nil; f=f->down)
  2398  		f->sym->flags |= SymUniq;
  2399  
  2400  	// generate all reachable methods
  2401  	slist = nil;
  2402  	expand1(t, nelem(dotlist)-1, 0);
  2403  
  2404  	// check each method to be uniquely reachable
  2405  	for(sl=slist; sl!=nil; sl=sl->link) {
  2406  		sl->field->sym->flags &= ~SymUniq;
  2407  		for(d=0; d<nelem(dotlist); d++) {
  2408  			c = adddot1(sl->field->sym, t, d, &f, 0);
  2409  			if(c == 0)
  2410  				continue;
  2411  			if(c == 1) {
  2412  				// addot1 may have dug out arbitrary fields, we only want methods.
  2413  				if(f->type->etype == TFUNC && f->type->thistuple > 0) {
  2414  					sl->good = 1;
  2415  					sl->field = f;
  2416  				}
  2417  			}
  2418  			break;
  2419  		}
  2420  	}
  2421  
  2422  	for(f=t->method; f != nil; f=f->down)
  2423  		f->sym->flags &= ~SymUniq;
  2424  
  2425  	t->xmethod = t->method;
  2426  	for(sl=slist; sl!=nil; sl=sl->link) {
  2427  		if(sl->good) {
  2428  			// add it to the base type method list
  2429  			f = typ(TFIELD);
  2430  			*f = *sl->field;
  2431  			f->embedded = 1;	// needs a trampoline
  2432  			if(sl->followptr)
  2433  				f->embedded = 2;
  2434  			f->down = t->xmethod;
  2435  			t->xmethod = f;
  2436  		}
  2437  	}
  2438  }
  2439  
  2440  /*
  2441   * Given funarg struct list, return list of ODCLFIELD Node fn args.
  2442   */
  2443  static NodeList*
  2444  structargs(Type **tl, int mustname)
  2445  {
  2446  	Iter savet;
  2447  	Node *a, *n;
  2448  	NodeList *args;
  2449  	Type *t;
  2450  	char buf[100];
  2451  	int gen;
  2452  
  2453  	args = nil;
  2454  	gen = 0;
  2455  	for(t = structfirst(&savet, tl); t != T; t = structnext(&savet)) {
  2456  		n = N;
  2457  		if(mustname && (t->sym == nil || strcmp(t->sym->name, "_") == 0)) {
  2458  			// invent a name so that we can refer to it in the trampoline
  2459  			snprint(buf, sizeof buf, ".anon%d", gen++);
  2460  			n = newname(lookup(buf));
  2461  		} else if(t->sym)
  2462  			n = newname(t->sym);
  2463  		a = nod(ODCLFIELD, n, typenod(t->type));
  2464  		a->isddd = t->isddd;
  2465  		if(n != N)
  2466  			n->isddd = t->isddd;
  2467  		args = list(args, a);
  2468  	}
  2469  	return args;
  2470  }
  2471  
  2472  /*
  2473   * Generate a wrapper function to convert from
  2474   * a receiver of type T to a receiver of type U.
  2475   * That is,
  2476   *
  2477   *	func (t T) M() {
  2478   *		...
  2479   *	}
  2480   *
  2481   * already exists; this function generates
  2482   *
  2483   *	func (u U) M() {
  2484   *		u.M()
  2485   *	}
  2486   *
  2487   * where the types T and U are such that u.M() is valid
  2488   * and calls the T.M method.
  2489   * The resulting function is for use in method tables.
  2490   *
  2491   *	rcvr - U
  2492   *	method - M func (t T)(), a TFIELD type struct
  2493   *	newnam - the eventual mangled name of this function
  2494   */
  2495  void
  2496  genwrapper(Type *rcvr, Type *method, Sym *newnam, int iface)
  2497  {
  2498  	Node *this, *fn, *call, *n, *t, *pad;
  2499  	NodeList *l, *args, *in, *out;
  2500  	Type *tpad;
  2501  	int isddd;
  2502  	Val v;
  2503  
  2504  	if(debug['r'])
  2505  		print("genwrapper rcvrtype=%T method=%T newnam=%S\n",
  2506  			rcvr, method, newnam);
  2507  
  2508  	lineno = 1;	// less confusing than end of input
  2509  
  2510  	dclcontext = PEXTERN;
  2511  	markdcl();
  2512  
  2513  	this = nod(ODCLFIELD, newname(lookup(".this")), typenod(rcvr));
  2514  	this->left->ntype = this->right;
  2515  	in = structargs(getinarg(method->type), 1);
  2516  	out = structargs(getoutarg(method->type), 0);
  2517  
  2518  	t = nod(OTFUNC, N, N);
  2519  	l = list1(this);
  2520  	if(iface && rcvr->width < types[tptr]->width) {
  2521  		// Building method for interface table and receiver
  2522  		// is smaller than the single pointer-sized word
  2523  		// that the interface call will pass in.
  2524  		// Add a dummy padding argument after the
  2525  		// receiver to make up the difference.
  2526  		tpad = typ(TARRAY);
  2527  		tpad->type = types[TUINT8];
  2528  		tpad->bound = types[tptr]->width - rcvr->width;
  2529  		pad = nod(ODCLFIELD, newname(lookup(".pad")), typenod(tpad));
  2530  		l = list(l, pad);
  2531  	}
  2532  	t->list = concat(l, in);
  2533  	t->rlist = out;
  2534  
  2535  	fn = nod(ODCLFUNC, N, N);
  2536  	fn->nname = newname(newnam);
  2537  	fn->nname->defn = fn;
  2538  	fn->nname->ntype = t;
  2539  	declare(fn->nname, PFUNC);
  2540  	funchdr(fn);
  2541  
  2542  	// arg list
  2543  	args = nil;
  2544  	isddd = 0;
  2545  	for(l=in; l; l=l->next) {
  2546  		args = list(args, l->n->left);
  2547  		isddd = l->n->left->isddd;
  2548  	}
  2549  	
  2550  	// generate nil pointer check for better error
  2551  	if(isptr[rcvr->etype] && rcvr->type == getthisx(method->type)->type->type) {
  2552  		// generating wrapper from *T to T.
  2553  		n = nod(OIF, N, N);
  2554  		n->ntest = nod(OEQ, this->left, nodnil());
  2555  		// these strings are already in the reflect tables,
  2556  		// so no space cost to use them here.
  2557  		l = nil;
  2558  		v.ctype = CTSTR;
  2559  		v.u.sval = strlit(rcvr->type->sym->pkg->name);  // package name
  2560  		l = list(l, nodlit(v));
  2561  		v.u.sval = strlit(rcvr->type->sym->name);  // type name
  2562  		l = list(l, nodlit(v));
  2563  		v.u.sval = strlit(method->sym->name);
  2564  		l = list(l, nodlit(v));  // method name
  2565  		call = nod(OCALL, syslook("panicwrap", 0), N);
  2566  		call->list = l;
  2567  		n->nbody = list1(call);
  2568  		fn->nbody = list(fn->nbody, n);
  2569  	}
  2570  
  2571  	// generate call
  2572  	call = nod(OCALL, adddot(nod(OXDOT, this->left, newname(method->sym))), N);
  2573  	call->list = args;
  2574  	call->isddd = isddd;
  2575  	if(method->type->outtuple > 0) {
  2576  		n = nod(ORETURN, N, N);
  2577  		n->list = list1(call);
  2578  		call = n;
  2579  	}
  2580  	fn->nbody = list(fn->nbody, call);
  2581  
  2582  	if(0 && debug['r'])
  2583  		dumplist("genwrapper body", fn->nbody);
  2584  
  2585  	funcbody(fn);
  2586  	curfn = fn;
  2587  	// wrappers where T is anonymous (struct{ NamedType }) can be duplicated.
  2588  	if(rcvr->etype == TSTRUCT || isptr[rcvr->etype] && rcvr->type->etype == TSTRUCT)
  2589  		fn->dupok = 1;
  2590  	typecheck(&fn, Etop);
  2591  	typechecklist(fn->nbody, Etop);
  2592  	inlcalls(fn);
  2593  	curfn = nil;
  2594  	funccompile(fn, 0);
  2595  }
  2596  
  2597  static Node*
  2598  hashmem(Type *t)
  2599  {
  2600  	Node *tfn, *n;
  2601  	Sym *sym;
  2602  	
  2603  	sym = pkglookup("memhash", runtimepkg);
  2604  
  2605  	n = newname(sym);
  2606  	n->class = PFUNC;
  2607  	tfn = nod(OTFUNC, N, N);
  2608  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(types[TUINTPTR]))));
  2609  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2610  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t))));
  2611  	typecheck(&tfn, Etype);
  2612  	n->type = tfn->type;
  2613  	return n;
  2614  }
  2615  
  2616  static Node*
  2617  hashfor(Type *t)
  2618  {
  2619  	int a;
  2620  	Sym *sym;
  2621  	Node *tfn, *n;
  2622  
  2623  	a = algtype1(t, nil);
  2624  	switch(a) {
  2625  	case AMEM:
  2626  		return hashmem(t);
  2627  	case AINTER:
  2628  		sym = pkglookup("interhash", runtimepkg);
  2629  		break;
  2630  	case ANILINTER:
  2631  		sym = pkglookup("nilinterhash", runtimepkg);
  2632  		break;
  2633  	case ASTRING:
  2634  		sym = pkglookup("strhash", runtimepkg);
  2635  		break;
  2636  	case AFLOAT32:
  2637  		sym = pkglookup("f32hash", runtimepkg);
  2638  		break;
  2639  	case AFLOAT64:
  2640  		sym = pkglookup("f64hash", runtimepkg);
  2641  		break;
  2642  	case ACPLX64:
  2643  		sym = pkglookup("c64hash", runtimepkg);
  2644  		break;
  2645  	case ACPLX128:
  2646  		sym = pkglookup("c128hash", runtimepkg);
  2647  		break;
  2648  	default:
  2649  		sym = typesymprefix(".hash", t);
  2650  		break;
  2651  	}
  2652  
  2653  	n = newname(sym);
  2654  	n->class = PFUNC;
  2655  	tfn = nod(OTFUNC, N, N);
  2656  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(types[TUINTPTR]))));
  2657  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(types[TUINTPTR])));
  2658  	tfn->list = list(tfn->list, nod(ODCLFIELD, N, typenod(ptrto(t))));
  2659  	typecheck(&tfn, Etype);
  2660  	n->type = tfn->type;
  2661  	return n;
  2662  }
  2663  
  2664  /*
  2665   * Generate a helper function to compute the hash of a value of type t.
  2666   */
  2667  void
  2668  genhash(Sym *sym, Type *t)
  2669  {
  2670  	Node *n, *fn, *np, *nh, *ni, *call, *nx, *na, *tfn;
  2671  	Node *hashel;
  2672  	Type *first, *t1;
  2673  	int old_safemode;
  2674  	int64 size, mul, offend;
  2675  
  2676  	if(debug['r'])
  2677  		print("genhash %S %T\n", sym, t);
  2678  
  2679  	lineno = 1;  // less confusing than end of input
  2680  	dclcontext = PEXTERN;
  2681  	markdcl();
  2682  
  2683  	// func sym(h *uintptr, s uintptr, p *T)
  2684  	fn = nod(ODCLFUNC, N, N);
  2685  	fn->nname = newname(sym);
  2686  	fn->nname->class = PFUNC;
  2687  	tfn = nod(OTFUNC, N, N);
  2688  	fn->nname->ntype = tfn;
  2689  
  2690  	n = nod(ODCLFIELD, newname(lookup("h")), typenod(ptrto(types[TUINTPTR])));
  2691  	tfn->list = list(tfn->list, n);
  2692  	nh = n->left;
  2693  	n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR]));
  2694  	tfn->list = list(tfn->list, n);
  2695  	n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t)));
  2696  	tfn->list = list(tfn->list, n);
  2697  	np = n->left;
  2698  
  2699  	funchdr(fn);
  2700  	typecheck(&fn->nname->ntype, Etype);
  2701  
  2702  	// genhash is only called for types that have equality but
  2703  	// cannot be handled by the standard algorithms,
  2704  	// so t must be either an array or a struct.
  2705  	switch(t->etype) {
  2706  	default:
  2707  		fatal("genhash %T", t);
  2708  	case TARRAY:
  2709  		if(isslice(t))
  2710  			fatal("genhash %T", t);
  2711  		// An array of pure memory would be handled by the
  2712  		// standard algorithm, so the element type must not be
  2713  		// pure memory.
  2714  		hashel = hashfor(t->type);
  2715  		n = nod(ORANGE, N, nod(OIND, np, N));
  2716  		ni = newname(lookup("i"));
  2717  		ni->type = types[TINT];
  2718  		n->list = list1(ni);
  2719  		n->colas = 1;
  2720  		colasdefn(n->list, n);
  2721  		ni = n->list->n;
  2722  
  2723  		// *h = *h<<3 | *h>>61
  2724  		n->nbody = list(n->nbody,
  2725  			nod(OAS,
  2726  				nod(OIND, nh, N),
  2727  				nod(OOR,
  2728  					nod(OLSH, nod(OIND, nh, N), nodintconst(3)),
  2729  					nod(ORSH, nod(OIND, nh, N), nodintconst(widthptr*8-3)))));
  2730  
  2731  		// *h *= mul
  2732  		// Same multipliers as in runtime.memhash.
  2733  		if(widthptr == 4)
  2734  			mul = 3267000013LL;
  2735  		else
  2736  			mul = 23344194077549503LL;
  2737  		n->nbody = list(n->nbody,
  2738  			nod(OAS,
  2739  				nod(OIND, nh, N),
  2740  				nod(OMUL, nod(OIND, nh, N), nodintconst(mul))));
  2741  
  2742  		// hashel(h, sizeof(p[i]), &p[i])
  2743  		call = nod(OCALL, hashel, N);
  2744  		call->list = list(call->list, nh);
  2745  		call->list = list(call->list, nodintconst(t->type->width));
  2746  		nx = nod(OINDEX, np, ni);
  2747  		nx->bounded = 1;
  2748  		na = nod(OADDR, nx, N);
  2749  		na->etype = 1;  // no escape to heap
  2750  		call->list = list(call->list, na);
  2751  		n->nbody = list(n->nbody, call);
  2752  
  2753  		fn->nbody = list(fn->nbody, n);
  2754  		break;
  2755  
  2756  	case TSTRUCT:
  2757  		// Walk the struct using memhash for runs of AMEM
  2758  		// and calling specific hash functions for the others.
  2759  		first = T;
  2760  		offend = 0;
  2761  		for(t1=t->type;; t1=t1->down) {
  2762  			if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) {
  2763  				offend = t1->width + t1->type->width;
  2764  				if(first == T)
  2765  					first = t1;
  2766  				// If it's a memory field but it's padded, stop here.
  2767  				if(ispaddedfield(t1, t->width))
  2768  					t1 = t1->down;
  2769  				else
  2770  					continue;
  2771  			}
  2772  			// Run memhash for fields up to this one.
  2773  			if(first != T) {
  2774  				size = offend - first->width; // first->width is offset
  2775  				hashel = hashmem(first->type);
  2776  				// hashel(h, size, &p.first)
  2777  				call = nod(OCALL, hashel, N);
  2778  				call->list = list(call->list, nh);
  2779  				call->list = list(call->list, nodintconst(size));
  2780  				nx = nod(OXDOT, np, newname(first->sym));  // TODO: fields from other packages?
  2781  				na = nod(OADDR, nx, N);
  2782  				na->etype = 1;  // no escape to heap
  2783  				call->list = list(call->list, na);
  2784  				fn->nbody = list(fn->nbody, call);
  2785  
  2786  				first = T;
  2787  			}
  2788  			if(t1 == T)
  2789  				break;
  2790  			if(isblanksym(t1->sym))
  2791  				continue;
  2792  
  2793  			// Run hash for this field.
  2794  			hashel = hashfor(t1->type);
  2795  			// hashel(h, size, &p.t1)
  2796  			call = nod(OCALL, hashel, N);
  2797  			call->list = list(call->list, nh);
  2798  			call->list = list(call->list, nodintconst(t1->type->width));
  2799  			nx = nod(OXDOT, np, newname(t1->sym));  // TODO: fields from other packages?
  2800  			na = nod(OADDR, nx, N);
  2801  			na->etype = 1;  // no escape to heap
  2802  			call->list = list(call->list, na);
  2803  			fn->nbody = list(fn->nbody, call);
  2804  		}
  2805  		// make sure body is not empty.
  2806  		fn->nbody = list(fn->nbody, nod(ORETURN, N, N));
  2807  		break;
  2808  	}
  2809  
  2810  	if(debug['r'])
  2811  		dumplist("genhash body", fn->nbody);
  2812  
  2813  	funcbody(fn);
  2814  	curfn = fn;
  2815  	fn->dupok = 1;
  2816  	typecheck(&fn, Etop);
  2817  	typechecklist(fn->nbody, Etop);
  2818  	curfn = nil;
  2819  
  2820  	// Disable safemode while compiling this code: the code we
  2821  	// generate internally can refer to unsafe.Pointer.
  2822  	// In this case it can happen if we need to generate an ==
  2823  	// for a struct containing a reflect.Value, which itself has
  2824  	// an unexported field of type unsafe.Pointer.
  2825  	old_safemode = safemode;
  2826  	safemode = 0;
  2827  	funccompile(fn, 0);
  2828  	safemode = old_safemode;
  2829  }
  2830  
  2831  // Return node for
  2832  //	if p.field != q.field { *eq = false; return }
  2833  static Node*
  2834  eqfield(Node *p, Node *q, Node *field, Node *eq)
  2835  {
  2836  	Node *nif, *nx, *ny;
  2837  
  2838  	nx = nod(OXDOT, p, field);
  2839  	ny = nod(OXDOT, q, field);
  2840  	nif = nod(OIF, N, N);
  2841  	nif->ntest = nod(ONE, nx, ny);
  2842  	nif->nbody = list(nif->nbody, nod(OAS, nod(OIND, eq, N), nodbool(0)));
  2843  	nif->nbody = list(nif->nbody, nod(ORETURN, N, N));
  2844  	return nif;
  2845  }
  2846  
  2847  static Node*
  2848  eqmemfunc(vlong size, Type *type)
  2849  {
  2850  	char buf[30];
  2851  	Node *fn;
  2852  
  2853  	switch(size) {
  2854  	default:
  2855  		fn = syslook("memequal", 1);
  2856  		break;
  2857  	case 1:
  2858  	case 2:
  2859  	case 4:
  2860  	case 8:
  2861  	case 16:
  2862  		snprint(buf, sizeof buf, "memequal%d", (int)size*8);
  2863  		fn = syslook(buf, 1);
  2864  		break;
  2865  	}
  2866  	argtype(fn, type);
  2867  	argtype(fn, type);
  2868  	return fn;
  2869  }
  2870  
  2871  // Return node for
  2872  //	if memequal(size, &p.field, &q.field, eq); !*eq { return }
  2873  static Node*
  2874  eqmem(Node *p, Node *q, Node *field, vlong size, Node *eq)
  2875  {
  2876  	Node *nif, *nx, *ny, *call;
  2877  
  2878  	nx = nod(OADDR, nod(OXDOT, p, field), N);
  2879  	nx->etype = 1;  // does not escape
  2880  	ny = nod(OADDR, nod(OXDOT, q, field), N);
  2881  	ny->etype = 1;  // does not escape
  2882  	typecheck(&nx, Erv);
  2883  	typecheck(&ny, Erv);
  2884  
  2885  	call = nod(OCALL, eqmemfunc(size, nx->type->type), N);
  2886  	call->list = list(call->list, eq);
  2887  	call->list = list(call->list, nodintconst(size));
  2888  	call->list = list(call->list, nx);
  2889  	call->list = list(call->list, ny);
  2890  
  2891  	nif = nod(OIF, N, N);
  2892  	nif->ninit = list(nif->ninit, call);
  2893  	nif->ntest = nod(ONOT, nod(OIND, eq, N), N);
  2894  	nif->nbody = list(nif->nbody, nod(ORETURN, N, N));
  2895  	return nif;
  2896  }
  2897  
  2898  /*
  2899   * Generate a helper function to check equality of two values of type t.
  2900   */
  2901  void
  2902  geneq(Sym *sym, Type *t)
  2903  {
  2904  	Node *n, *fn, *np, *neq, *nq, *tfn, *nif, *ni, *nx, *ny, *nrange;
  2905  	Type *t1, *first;
  2906  	int old_safemode;
  2907  	int64 size;
  2908  	int64 offend;
  2909  
  2910  	if(debug['r'])
  2911  		print("geneq %S %T\n", sym, t);
  2912  
  2913  	lineno = 1;  // less confusing than end of input
  2914  	dclcontext = PEXTERN;
  2915  	markdcl();
  2916  
  2917  	// func sym(eq *bool, s uintptr, p, q *T)
  2918  	fn = nod(ODCLFUNC, N, N);
  2919  	fn->nname = newname(sym);
  2920  	fn->nname->class = PFUNC;
  2921  	tfn = nod(OTFUNC, N, N);
  2922  	fn->nname->ntype = tfn;
  2923  
  2924  	n = nod(ODCLFIELD, newname(lookup("eq")), typenod(ptrto(types[TBOOL])));
  2925  	tfn->list = list(tfn->list, n);
  2926  	neq = n->left;
  2927  	n = nod(ODCLFIELD, newname(lookup("s")), typenod(types[TUINTPTR]));
  2928  	tfn->list = list(tfn->list, n);
  2929  	n = nod(ODCLFIELD, newname(lookup("p")), typenod(ptrto(t)));
  2930  	tfn->list = list(tfn->list, n);
  2931  	np = n->left;
  2932  	n = nod(ODCLFIELD, newname(lookup("q")), typenod(ptrto(t)));
  2933  	tfn->list = list(tfn->list, n);
  2934  	nq = n->left;
  2935  
  2936  	funchdr(fn);
  2937  
  2938  	// geneq is only called for types that have equality but
  2939  	// cannot be handled by the standard algorithms,
  2940  	// so t must be either an array or a struct.
  2941  	switch(t->etype) {
  2942  	default:
  2943  		fatal("geneq %T", t);
  2944  	case TARRAY:
  2945  		if(isslice(t))
  2946  			fatal("geneq %T", t);
  2947  		// An array of pure memory would be handled by the
  2948  		// standard memequal, so the element type must not be
  2949  		// pure memory.  Even if we unrolled the range loop,
  2950  		// each iteration would be a function call, so don't bother
  2951  		// unrolling.
  2952  		nrange = nod(ORANGE, N, nod(OIND, np, N));
  2953  		ni = newname(lookup("i"));
  2954  		ni->type = types[TINT];
  2955  		nrange->list = list1(ni);
  2956  		nrange->colas = 1;
  2957  		colasdefn(nrange->list, nrange);
  2958  		ni = nrange->list->n;
  2959  		
  2960  		// if p[i] != q[i] { *eq = false; return }
  2961  		nx = nod(OINDEX, np, ni);
  2962  		nx->bounded = 1;
  2963  		ny = nod(OINDEX, nq, ni);
  2964  		ny->bounded = 1;
  2965  
  2966  		nif = nod(OIF, N, N);
  2967  		nif->ntest = nod(ONE, nx, ny);
  2968  		nif->nbody = list(nif->nbody, nod(OAS, nod(OIND, neq, N), nodbool(0)));
  2969  		nif->nbody = list(nif->nbody, nod(ORETURN, N, N));
  2970  		nrange->nbody = list(nrange->nbody, nif);
  2971  		fn->nbody = list(fn->nbody, nrange);
  2972  
  2973  		// *eq = true;
  2974  		fn->nbody = list(fn->nbody, nod(OAS, nod(OIND, neq, N), nodbool(1)));
  2975  		break;
  2976  
  2977  	case TSTRUCT:
  2978  		// Walk the struct using memequal for runs of AMEM
  2979  		// and calling specific equality tests for the others.
  2980  		// Skip blank-named fields.
  2981  		first = T;
  2982  		offend = 0;
  2983  		for(t1=t->type;; t1=t1->down) {
  2984  			if(t1 != T && algtype1(t1->type, nil) == AMEM && !isblanksym(t1->sym)) {
  2985  				offend = t1->width + t1->type->width;
  2986  				if(first == T)
  2987  					first = t1;
  2988  				// If it's a memory field but it's padded, stop here.
  2989  				if(ispaddedfield(t1, t->width))
  2990  					t1 = t1->down;
  2991  				else
  2992  					continue;
  2993  			}
  2994  			// Run memequal for fields up to this one.
  2995  			// TODO(rsc): All the calls to newname are wrong for
  2996  			// cross-package unexported fields.
  2997  			if(first != T) {
  2998  				if(first->down == t1) {
  2999  					fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym), neq));
  3000  				} else if(first->down->down == t1) {
  3001  					fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym), neq));
  3002  					first = first->down;
  3003  					if(!isblanksym(first->sym))
  3004  						fn->nbody = list(fn->nbody, eqfield(np, nq, newname(first->sym), neq));
  3005  				} else {
  3006  					// More than two fields: use memequal.
  3007  					size = offend - first->width; // first->width is offset
  3008  					fn->nbody = list(fn->nbody, eqmem(np, nq, newname(first->sym), size, neq));
  3009  				}
  3010  				first = T;
  3011  			}
  3012  			if(t1 == T)
  3013  				break;
  3014  			if(isblanksym(t1->sym))
  3015  				continue;
  3016  
  3017  			// Check this field, which is not just memory.
  3018  			fn->nbody = list(fn->nbody, eqfield(np, nq, newname(t1->sym), neq));
  3019  		}
  3020  
  3021  		// *eq = true;
  3022  		fn->nbody = list(fn->nbody, nod(OAS, nod(OIND, neq, N), nodbool(1)));
  3023  		break;
  3024  	}
  3025  
  3026  	if(debug['r'])
  3027  		dumplist("geneq body", fn->nbody);
  3028  
  3029  	funcbody(fn);
  3030  	curfn = fn;
  3031  	fn->dupok = 1;
  3032  	typecheck(&fn, Etop);
  3033  	typechecklist(fn->nbody, Etop);
  3034  	curfn = nil;
  3035  	
  3036  	// Disable safemode while compiling this code: the code we
  3037  	// generate internally can refer to unsafe.Pointer.
  3038  	// In this case it can happen if we need to generate an ==
  3039  	// for a struct containing a reflect.Value, which itself has
  3040  	// an unexported field of type unsafe.Pointer.
  3041  	old_safemode = safemode;
  3042  	safemode = 0;
  3043  	funccompile(fn, 0);
  3044  	safemode = old_safemode;
  3045  }
  3046  
  3047  static Type*
  3048  ifacelookdot(Sym *s, Type *t, int *followptr, int ignorecase)
  3049  {
  3050  	int i, c, d;
  3051  	Type *m;
  3052  
  3053  	*followptr = 0;
  3054  
  3055  	if(t == T)
  3056  		return T;
  3057  
  3058  	for(d=0; d<nelem(dotlist); d++) {
  3059  		c = adddot1(s, t, d, &m, ignorecase);
  3060  		if(c > 1) {
  3061  			yyerror("%T.%S is ambiguous", t, s);
  3062  			return T;
  3063  		}
  3064  		if(c == 1) {
  3065  			for(i=0; i<d; i++) {
  3066  				if(isptr[dotlist[i].field->type->etype]) {
  3067  					*followptr = 1;
  3068  					break;
  3069  				}
  3070  			}
  3071  			if(m->type->etype != TFUNC || m->type->thistuple == 0) {
  3072  				yyerror("%T.%S is a field, not a method", t, s);
  3073  				return T;
  3074  			}
  3075  			return m;
  3076  		}
  3077  	}
  3078  	return T;
  3079  }
  3080  
  3081  int
  3082  implements(Type *t, Type *iface, Type **m, Type **samename, int *ptr)
  3083  {
  3084  	Type *t0, *im, *tm, *rcvr, *imtype;
  3085  	int followptr;
  3086  
  3087  	t0 = t;
  3088  	if(t == T)
  3089  		return 0;
  3090  
  3091  	// if this is too slow,
  3092  	// could sort these first
  3093  	// and then do one loop.
  3094  
  3095  	if(t->etype == TINTER) {
  3096  		for(im=iface->type; im; im=im->down) {
  3097  			for(tm=t->type; tm; tm=tm->down) {
  3098  				if(tm->sym == im->sym) {
  3099  					if(eqtype(tm->type, im->type))
  3100  						goto found;
  3101  					*m = im;
  3102  					*samename = tm;
  3103  					*ptr = 0;
  3104  					return 0;
  3105  				}
  3106  			}
  3107  			*m = im;
  3108  			*samename = nil;
  3109  			*ptr = 0;
  3110  			return 0;
  3111  		found:;
  3112  		}
  3113  		return 1;
  3114  	}
  3115  
  3116  	t = methtype(t, 0);
  3117  	if(t != T)
  3118  		expandmeth(t);
  3119  	for(im=iface->type; im; im=im->down) {
  3120  		imtype = methodfunc(im->type, 0);
  3121  		tm = ifacelookdot(im->sym, t, &followptr, 0);
  3122  		if(tm == T || tm->nointerface || !eqtype(methodfunc(tm->type, 0), imtype)) {
  3123  			if(tm == T)
  3124  				tm = ifacelookdot(im->sym, t, &followptr, 1);
  3125  			*m = im;
  3126  			*samename = tm;
  3127  			*ptr = 0;
  3128  			return 0;
  3129  		}
  3130  		// if pointer receiver in method,
  3131  		// the method does not exist for value types.
  3132  		rcvr = getthisx(tm->type)->type->type;
  3133  		if(isptr[rcvr->etype] && !isptr[t0->etype] && !followptr && !isifacemethod(tm->type)) {
  3134  			if(0 && debug['r'])
  3135  				yyerror("interface pointer mismatch");
  3136  
  3137  			*m = im;
  3138  			*samename = nil;
  3139  			*ptr = 1;
  3140  			return 0;
  3141  		}
  3142  	}
  3143  	return 1;
  3144  }
  3145  
  3146  /*
  3147   * even simpler simtype; get rid of ptr, bool.
  3148   * assuming that the front end has rejected
  3149   * all the invalid conversions (like ptr -> bool)
  3150   */
  3151  int
  3152  simsimtype(Type *t)
  3153  {
  3154  	int et;
  3155  
  3156  	if(t == 0)
  3157  		return 0;
  3158  
  3159  	et = simtype[t->etype];
  3160  	switch(et) {
  3161  	case TPTR32:
  3162  		et = TUINT32;
  3163  		break;
  3164  	case TPTR64:
  3165  		et = TUINT64;
  3166  		break;
  3167  	case TBOOL:
  3168  		et = TUINT8;
  3169  		break;
  3170  	}
  3171  	return et;
  3172  }
  3173  
  3174  NodeList*
  3175  concat(NodeList *a, NodeList *b)
  3176  {
  3177  	if(a == nil)
  3178  		return b;
  3179  	if(b == nil)
  3180  		return a;
  3181  
  3182  	a->end->next = b;
  3183  	a->end = b->end;
  3184  	b->end = nil;
  3185  	return a;
  3186  }
  3187  
  3188  NodeList*
  3189  list1(Node *n)
  3190  {
  3191  	NodeList *l;
  3192  
  3193  	if(n == nil)
  3194  		return nil;
  3195  	if(n->op == OBLOCK && n->ninit == nil) {
  3196  		// Flatten list and steal storage.
  3197  		// Poison pointer to catch errant uses.
  3198  		l = n->list;
  3199  		n->list = (NodeList*)1;
  3200  		return l;
  3201  	}
  3202  	l = mal(sizeof *l);
  3203  	l->n = n;
  3204  	l->end = l;
  3205  	return l;
  3206  }
  3207  
  3208  NodeList*
  3209  list(NodeList *l, Node *n)
  3210  {
  3211  	return concat(l, list1(n));
  3212  }
  3213  
  3214  void
  3215  listsort(NodeList** l, int(*f)(Node*, Node*))
  3216  {
  3217  	NodeList *l1, *l2, *le;
  3218  
  3219  	if(*l == nil || (*l)->next == nil)
  3220  		return;
  3221  
  3222  	l1 = *l;
  3223  	l2 = *l;
  3224  	for(;;) {
  3225  		l2 = l2->next;
  3226  		if(l2 == nil)
  3227  			break;
  3228  		l2 = l2->next;
  3229  		if(l2 == nil)
  3230  			break;
  3231  		l1 = l1->next;
  3232  	}
  3233  
  3234  	l2 = l1->next;
  3235  	l1->next = nil;
  3236  	l2->end = (*l)->end;
  3237  	(*l)->end = l1;
  3238  
  3239  	l1 = *l;
  3240  	listsort(&l1, f);
  3241  	listsort(&l2, f);
  3242  
  3243  	if((*f)(l1->n, l2->n) < 0) {
  3244  		*l = l1;
  3245  	} else {
  3246  		*l = l2;
  3247  		l2 = l1;
  3248  		l1 = *l;
  3249  	}
  3250  
  3251  	// now l1 == *l; and l1 < l2
  3252  
  3253  	while ((l1 != nil) && (l2 != nil)) {
  3254  		while ((l1->next != nil) && (*f)(l1->next->n, l2->n) < 0)
  3255  			l1 = l1->next;
  3256  		
  3257  		// l1 is last one from l1 that is < l2
  3258  		le = l1->next;		// le is the rest of l1, first one that is >= l2
  3259  		if(le != nil)
  3260  			le->end = (*l)->end;
  3261  
  3262  		(*l)->end = l1;		// cut *l at l1
  3263  		*l = concat(*l, l2);	// glue l2 to *l's tail
  3264  
  3265  		l1 = l2;		// l1 is the first element of *l that is < the new l2
  3266  		l2 = le;		// ... because l2 now is the old tail of l1
  3267  	}
  3268  
  3269  	*l = concat(*l, l2);		// any remainder 
  3270  }
  3271  
  3272  NodeList*
  3273  listtreecopy(NodeList *l)
  3274  {
  3275  	NodeList *out;
  3276  
  3277  	out = nil;
  3278  	for(; l; l=l->next)
  3279  		out = list(out, treecopy(l->n));
  3280  	return out;
  3281  }
  3282  
  3283  Node*
  3284  liststmt(NodeList *l)
  3285  {
  3286  	Node *n;
  3287  
  3288  	n = nod(OBLOCK, N, N);
  3289  	n->list = l;
  3290  	if(l)
  3291  		n->lineno = l->n->lineno;
  3292  	return n;
  3293  }
  3294  
  3295  /*
  3296   * return nelem of list
  3297   */
  3298  int
  3299  count(NodeList *l)
  3300  {
  3301  	vlong n;
  3302  
  3303  	n = 0;
  3304  	for(; l; l=l->next)
  3305  		n++;
  3306  	if((int)n != n) { // Overflow.
  3307  		yyerror("too many elements in list");
  3308  	}
  3309  	return n;
  3310  }
  3311  
  3312  /*
  3313   * return nelem of list
  3314   */
  3315  int
  3316  structcount(Type *t)
  3317  {
  3318  	int v;
  3319  	Iter s;
  3320  
  3321  	v = 0;
  3322  	for(t = structfirst(&s, &t); t != T; t = structnext(&s))
  3323  		v++;
  3324  	return v;
  3325  }
  3326  
  3327  /*
  3328   * return power of 2 of the constant
  3329   * operand. -1 if it is not a power of 2.
  3330   * 1000+ if it is a -(power of 2)
  3331   */
  3332  int
  3333  powtwo(Node *n)
  3334  {
  3335  	uvlong v, b;
  3336  	int i;
  3337  
  3338  	if(n == N || n->op != OLITERAL || n->type == T)
  3339  		goto no;
  3340  	if(!isint[n->type->etype])
  3341  		goto no;
  3342  
  3343  	v = mpgetfix(n->val.u.xval);
  3344  	b = 1ULL;
  3345  	for(i=0; i<64; i++) {
  3346  		if(b == v)
  3347  			return i;
  3348  		b = b<<1;
  3349  	}
  3350  
  3351  	if(!issigned[n->type->etype])
  3352  		goto no;
  3353  
  3354  	v = -v;
  3355  	b = 1ULL;
  3356  	for(i=0; i<64; i++) {
  3357  		if(b == v)
  3358  			return i+1000;
  3359  		b = b<<1;
  3360  	}
  3361  
  3362  no:
  3363  	return -1;
  3364  }
  3365  
  3366  /*
  3367   * return the unsigned type for
  3368   * a signed integer type.
  3369   * returns T if input is not a
  3370   * signed integer type.
  3371   */
  3372  Type*
  3373  tounsigned(Type *t)
  3374  {
  3375  
  3376  	// this is types[et+1], but not sure
  3377  	// that this relation is immutable
  3378  	switch(t->etype) {
  3379  	default:
  3380  		print("tounsigned: unknown type %T\n", t);
  3381  		t = T;
  3382  		break;
  3383  	case TINT:
  3384  		t = types[TUINT];
  3385  		break;
  3386  	case TINT8:
  3387  		t = types[TUINT8];
  3388  		break;
  3389  	case TINT16:
  3390  		t = types[TUINT16];
  3391  		break;
  3392  	case TINT32:
  3393  		t = types[TUINT32];
  3394  		break;
  3395  	case TINT64:
  3396  		t = types[TUINT64];
  3397  		break;
  3398  	}
  3399  	return t;
  3400  }
  3401  
  3402  /*
  3403   * magic number for signed division
  3404   * see hacker's delight chapter 10
  3405   */
  3406  void
  3407  smagic(Magic *m)
  3408  {
  3409  	int p;
  3410  	uint64 ad, anc, delta, q1, r1, q2, r2, t;
  3411  	uint64 mask, two31;
  3412  
  3413  	m->bad = 0;
  3414  	switch(m->w) {
  3415  	default:
  3416  		m->bad = 1;
  3417  		return;
  3418  	case 8:
  3419  		mask = 0xffLL;
  3420  		break;
  3421  	case 16:
  3422  		mask = 0xffffLL;
  3423  		break;
  3424  	case 32:
  3425  		mask = 0xffffffffLL;
  3426  		break;
  3427  	case 64:
  3428  		mask = 0xffffffffffffffffLL;
  3429  		break;
  3430  	}
  3431  	two31 = mask ^ (mask>>1);
  3432  
  3433  	p = m->w-1;
  3434  	ad = m->sd;
  3435  	if(m->sd < 0)
  3436  		ad = -m->sd;
  3437  
  3438  	// bad denominators
  3439  	if(ad == 0 || ad == 1 || ad == two31) {
  3440  		m->bad = 1;
  3441  		return;
  3442  	}
  3443  
  3444  	t = two31;
  3445  	ad &= mask;
  3446  
  3447  	anc = t - 1 - t%ad;
  3448  	anc &= mask;
  3449  
  3450  	q1 = two31/anc;
  3451  	r1 = two31 - q1*anc;
  3452  	q1 &= mask;
  3453  	r1 &= mask;
  3454  
  3455  	q2 = two31/ad;
  3456  	r2 = two31 - q2*ad;
  3457  	q2 &= mask;
  3458  	r2 &= mask;
  3459  
  3460  	for(;;) {
  3461  		p++;
  3462  		q1 <<= 1;
  3463  		r1 <<= 1;
  3464  		q1 &= mask;
  3465  		r1 &= mask;
  3466  		if(r1 >= anc) {
  3467  			q1++;
  3468  			r1 -= anc;
  3469  			q1 &= mask;
  3470  			r1 &= mask;
  3471  		}
  3472  
  3473  		q2 <<= 1;
  3474  		r2 <<= 1;
  3475  		q2 &= mask;
  3476  		r2 &= mask;
  3477  		if(r2 >= ad) {
  3478  			q2++;
  3479  			r2 -= ad;
  3480  			q2 &= mask;
  3481  			r2 &= mask;
  3482  		}
  3483  
  3484  		delta = ad - r2;
  3485  		delta &= mask;
  3486  		if(q1 < delta || (q1 == delta && r1 == 0)) {
  3487  			continue;
  3488  		}
  3489  		break;
  3490  	}
  3491  
  3492  	m->sm = q2+1;
  3493  	if(m->sm & two31)
  3494  		m->sm |= ~mask;
  3495  	m->s = p-m->w;
  3496  }
  3497  
  3498  /*
  3499   * magic number for unsigned division
  3500   * see hacker's delight chapter 10
  3501   */
  3502  void
  3503  umagic(Magic *m)
  3504  {
  3505  	int p;
  3506  	uint64 nc, delta, q1, r1, q2, r2;
  3507  	uint64 mask, two31;
  3508  
  3509  	m->bad = 0;
  3510  	m->ua = 0;
  3511  
  3512  	switch(m->w) {
  3513  	default:
  3514  		m->bad = 1;
  3515  		return;
  3516  	case 8:
  3517  		mask = 0xffLL;
  3518  		break;
  3519  	case 16:
  3520  		mask = 0xffffLL;
  3521  		break;
  3522  	case 32:
  3523  		mask = 0xffffffffLL;
  3524  		break;
  3525  	case 64:
  3526  		mask = 0xffffffffffffffffLL;
  3527  		break;
  3528  	}
  3529  	two31 = mask ^ (mask>>1);
  3530  
  3531  	m->ud &= mask;
  3532  	if(m->ud == 0 || m->ud == two31) {
  3533  		m->bad = 1;
  3534  		return;
  3535  	}
  3536  	nc = mask - (-m->ud&mask)%m->ud;
  3537  	p = m->w-1;
  3538  
  3539  	q1 = two31/nc;
  3540  	r1 = two31 - q1*nc;
  3541  	q1 &= mask;
  3542  	r1 &= mask;
  3543  
  3544  	q2 = (two31-1) / m->ud;
  3545  	r2 = (two31-1) - q2*m->ud;
  3546  	q2 &= mask;
  3547  	r2 &= mask;
  3548  
  3549  	for(;;) {
  3550  		p++;
  3551  		if(r1 >= nc-r1) {
  3552  			q1 <<= 1;
  3553  			q1++;
  3554  			r1 <<= 1;
  3555  			r1 -= nc;
  3556  		} else {
  3557  			q1 <<= 1;
  3558  			r1 <<= 1;
  3559  		}
  3560  		q1 &= mask;
  3561  		r1 &= mask;
  3562  		if(r2+1 >= m->ud-r2) {
  3563  			if(q2 >= two31-1) {
  3564  				m->ua = 1;
  3565  			}
  3566  			q2 <<= 1;
  3567  			q2++;
  3568  			r2 <<= 1;
  3569  			r2++;
  3570  			r2 -= m->ud;
  3571  		} else {
  3572  			if(q2 >= two31) {
  3573  				m->ua = 1;
  3574  			}
  3575  			q2 <<= 1;
  3576  			r2 <<= 1;
  3577  			r2++;
  3578  		}
  3579  		q2 &= mask;
  3580  		r2 &= mask;
  3581  
  3582  		delta = m->ud - 1 - r2;
  3583  		delta &= mask;
  3584  
  3585  		if(p < m->w+m->w)
  3586  		if(q1 < delta || (q1 == delta && r1 == 0)) {
  3587  			continue;
  3588  		}
  3589  		break;
  3590  	}
  3591  	m->um = q2+1;
  3592  	m->s = p-m->w;
  3593  }
  3594  
  3595  Sym*
  3596  ngotype(Node *n)
  3597  {
  3598  	if(n->type != T)
  3599  		return typenamesym(n->type);
  3600  	return S;
  3601  }
  3602  
  3603  /*
  3604   * Convert raw string to the prefix that will be used in the symbol
  3605   * table.  All control characters, space, '%' and '"', as well as
  3606   * non-7-bit clean bytes turn into %xx.  The period needs escaping
  3607   * only in the last segment of the path, and it makes for happier
  3608   * users if we escape that as little as possible.
  3609   *
  3610   * If you edit this, edit ../ld/lib.c:/^pathtoprefix copy too.
  3611   */
  3612  static char*
  3613  pathtoprefix(char *s)
  3614  {
  3615  	static char hex[] = "0123456789abcdef";
  3616  	char *p, *r, *w, *l;
  3617  	int n;
  3618  
  3619  	// find first character past the last slash, if any.
  3620  	l = s;
  3621  	for(r=s; *r; r++)
  3622  		if(*r == '/')
  3623  			l = r+1;
  3624  
  3625  	// check for chars that need escaping
  3626  	n = 0;
  3627  	for(r=s; *r; r++)
  3628  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f)
  3629  			n++;
  3630  
  3631  	// quick exit
  3632  	if(n == 0)
  3633  		return s;
  3634  
  3635  	// escape
  3636  	p = mal((r-s)+1+2*n);
  3637  	for(r=s, w=p; *r; r++) {
  3638  		if(*r <= ' ' || (*r == '.' && r >= l) || *r == '%' || *r == '"' || *r >= 0x7f) {
  3639  			*w++ = '%';
  3640  			*w++ = hex[(*r>>4)&0xF];
  3641  			*w++ = hex[*r&0xF];
  3642  		} else
  3643  			*w++ = *r;
  3644  	}
  3645  	*w = '\0';
  3646  	return p;
  3647  }
  3648  
  3649  Pkg*
  3650  mkpkg(Strlit *path)
  3651  {
  3652  	Pkg *p;
  3653  	int h;
  3654  
  3655  	h = stringhash(path->s) & (nelem(phash)-1);
  3656  	for(p=phash[h]; p; p=p->link)
  3657  		if(p->path->len == path->len && memcmp(path->s, p->path->s, path->len) == 0)
  3658  			return p;
  3659  
  3660  	p = mal(sizeof *p);
  3661  	p->path = path;
  3662  	p->prefix = pathtoprefix(path->s);
  3663  	p->link = phash[h];
  3664  	phash[h] = p;
  3665  	return p;
  3666  }
  3667  
  3668  Strlit*
  3669  strlit(char *s)
  3670  {
  3671  	Strlit *t;
  3672  	
  3673  	t = mal(sizeof *t + strlen(s));
  3674  	strcpy(t->s, s);
  3675  	t->len = strlen(s);
  3676  	return t;
  3677  }
  3678  
  3679  void
  3680  addinit(Node **np, NodeList *init)
  3681  {
  3682  	Node *n;
  3683  	
  3684  	if(init == nil)
  3685  		return;
  3686  
  3687  	n = *np;
  3688  	switch(n->op) {
  3689  	case ONAME:
  3690  	case OLITERAL:
  3691  		// There may be multiple refs to this node;
  3692  		// introduce OCONVNOP to hold init list.
  3693  		n = nod(OCONVNOP, n, N);
  3694  		n->type = n->left->type;
  3695  		n->typecheck = 1;
  3696  		*np = n;
  3697  		break;
  3698  	}
  3699  	n->ninit = concat(init, n->ninit);
  3700  	n->ullman = UINF;
  3701  }
  3702  
  3703  static char* reservedimports[] = {
  3704  	"go",
  3705  	"type",
  3706  };
  3707  
  3708  int
  3709  isbadimport(Strlit *path)
  3710  {
  3711  	int i;
  3712  	char *s;
  3713  	Rune r;
  3714  
  3715  	if(strlen(path->s) != path->len) {
  3716  		yyerror("import path contains NUL");
  3717  		return 1;
  3718  	}
  3719  	
  3720  	for(i=0; i<nelem(reservedimports); i++) {
  3721  		if(strcmp(path->s, reservedimports[i]) == 0) {
  3722  			yyerror("import path \"%s\" is reserved and cannot be used", path->s);
  3723  			return 1;
  3724  		}
  3725  	}
  3726  
  3727  	s = path->s;
  3728  	while(*s) {
  3729  		s += chartorune(&r, s);
  3730  		if(r == Runeerror) {
  3731  			yyerror("import path contains invalid UTF-8 sequence: \"%Z\"", path);
  3732  			return 1;
  3733  		}
  3734  		if(r < 0x20 || r == 0x7f) {
  3735  			yyerror("import path contains control character: \"%Z\"", path);
  3736  			return 1;
  3737  		}
  3738  		if(r == '\\') {
  3739  			yyerror("import path contains backslash; use slash: \"%Z\"", path);
  3740  			return 1;
  3741  		}
  3742  		if(isspacerune(r)) {
  3743  			yyerror("import path contains space character: \"%Z\"", path);
  3744  			return 1;
  3745  		}
  3746  		if(utfrune("!\"#$%&'()*,:;<=>?[]^`{|}", r)) {
  3747  			yyerror("import path contains invalid character '%C': \"%Z\"", r, path);
  3748  			return 1;
  3749  		}
  3750  	}
  3751  	return 0;
  3752  }
  3753  
  3754  void
  3755  checknotnil(Node *x, NodeList **init)
  3756  {
  3757  	Node *n;
  3758  	
  3759  	if(isinter(x->type)) {
  3760  		x = nod(OITAB, x, N);
  3761  		typecheck(&x, Erv);
  3762  	}
  3763  	n = nod(OCHECKNOTNIL, x, N);
  3764  	n->typecheck = 1;
  3765  	*init = list(*init, n);
  3766  }