The Go Programming Language

Text file src/cmd/gc/subr.c

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

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.