go.h 23.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include	<u.h>
#include	<libc.h>
#include	<bio.h>

#ifndef	EXTERN
Russ Cox's avatar
Russ Cox committed
10
#define	EXTERN	extern
11 12 13 14 15 16 17 18 19 20 21
#endif
enum
{
	NHUNK		= 50000,
	BUFSIZ		= 8192,
	NSYMB		= 500,
	NHASH		= 1024,
	STRINGSZ	= 200,
	YYMAXDEPTH	= 500,
	MAXALIGN	= 7,
	UINF		= 100,
22
	HISTSZ		= 10,
23 24 25 26 27 28 29 30 31 32

	PRIME1		= 3,
	PRIME2		= 10007,
	PRIME3		= 10009,
	PRIME4		= 10037,
	PRIME5		= 10039,
	PRIME6		= 10061,
	PRIME7		= 10067,
	PRIME8		= 10079,
	PRIME9		= 10091,
33
	PRIME10		= 10093,
Ken Thompson's avatar
Ken Thompson committed
34 35

	AUNK		= 100,
36

Ken Thompson's avatar
Ken Thompson committed
37
	// these values are known by runtime
38 39
	AMEM		= 0,
	ANOEQ,
Ken Thompson's avatar
Ken Thompson committed
40 41
	ASTRING,
	AINTER,
42
	ANILINTER,
43

Russ Cox's avatar
Russ Cox committed
44
	BADWIDTH	= -1000000000
45 46 47 48 49
};

/*
 * note this is the representation
 * of the compilers string literals,
50
 * it is not the runtime representation
51
 */
52 53
typedef	struct	Strlit	Strlit;
struct	Strlit
54
{
55
	int32	len;
56 57 58
	char	s[3];	// variable
};

Ken Thompson's avatar
Ken Thompson committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
/*
 * note this is the runtime representation
 * of hashmap iterator. it is probably
 * insafe to use it this way, but it puts
 * all the changes in one place.
 * only flag is referenced from go.
 * actual placement does not matter as long
 * as the size is >= actual size.
 */
typedef	struct	Hiter	Hiter;
struct	Hiter
{
	uchar	data[8];		// return val from next
	int32	elemsize;		// size of elements in table */
	int32	changes;		// number of changes observed last time */
	int32	i;			// stack pointer in subtable_state */
	uchar	last[8];		// last hash value returned */
	uchar	h[8];			// the hash table */
	struct
	{
		uchar	sub[8];		// pointer into subtable */
		uchar	start[8];	// pointer into start of subtable */
		uchar	end[8];		// pointer into end of subtable */
		uchar	pad[8];
	} sub[4];
};

Ken Thompson's avatar
Ken Thompson committed
86 87
enum
{
88 89 90 91
	Mpscale	= 29,		// safely smaller than bits in a long
	Mpprec	= 16,		// Mpscale*Mpprec is max number of bits
	Mpnorm	= Mpprec - 1,	// significant words in a normalized float
	Mpbase	= 1L << Mpscale,
Ken Thompson's avatar
Ken Thompson committed
92
	Mpsign	= Mpbase >> 1,
93 94
	Mpmask	= Mpbase - 1,
	Mpdebug	= 0,
Ken Thompson's avatar
Ken Thompson committed
95 96 97 98 99 100 101 102 103 104 105 106 107
};

typedef	struct	Mpint	Mpint;
struct	Mpint
{
	long	a[Mpprec];
	uchar	neg;
	uchar	ovf;
};

typedef	struct	Mpflt	Mpflt;
struct	Mpflt
{
108 109
	Mpint	val;
	short	exp;
Ken Thompson's avatar
Ken Thompson committed
110 111
};

112 113 114
typedef	struct	Val	Val;
struct	Val
{
Ken Thompson's avatar
Ken Thompson committed
115 116 117 118 119 120 121
	short	ctype;
	union
	{
		short	reg;		// OREGISTER
		short	bval;		// bool value CTBOOL
		Mpint*	xval;		// int CTINT
		Mpflt*	fval;		// float CTFLT
122
		Strlit*	sval;		// string CTSTR
Ken Thompson's avatar
Ken Thompson committed
123
	} u;
124 125 126 127
};

typedef	struct	Sym	Sym;
typedef	struct	Node	Node;
128
typedef	struct	NodeList	NodeList;
129 130 131 132
typedef	struct	Type	Type;

struct	Type
{
133 134
	uchar	etype;
	uchar	chan;
135 136
	uchar	recur;		// to detect loops
	uchar	trecur;		// to detect loops
Russ Cox's avatar
Russ Cox committed
137
	uchar	printed;
Ken Thompson's avatar
Ken Thompson committed
138
	uchar	embedded;	// TFIELD embedded type
Russ Cox's avatar
Russ Cox committed
139
	uchar	siggen;
Russ Cox's avatar
Russ Cox committed
140
	uchar	funarg;
141
	uchar	copyany;
142
	uchar	local;		// created in this file
143
	uchar	deferwidth;
144

Russ Cox's avatar
Russ Cox committed
145 146
	Node*	nod;		// canonical OTYPE node

147 148 149 150
	// TFUNCT
	uchar	thistuple;
	uchar	outtuple;
	uchar	intuple;
Ken Thompson's avatar
Ken Thompson committed
151
	uchar	outnamed;
152

Ken Thompson's avatar
Ken Thompson committed
153
	Type*	method;
Russ Cox's avatar
Russ Cox committed
154
	Type*	xmethod;
Ken Thompson's avatar
Ken Thompson committed
155

156
	Sym*	sym;
157
	int32	vargen;		// unique name for OTYPE/ONAME
158

Ken Thompson's avatar
Ken Thompson committed
159 160 161
	Node*	nname;
	vlong	argwid;

162 163 164 165 166 167
	// most nodes
	Type*	type;
	vlong	width;		// offset in TFIELD, width in all others

	// TFIELD
	Type*	down;		// also used in TMAP
168
	Strlit*	note;		// literal string annotation
169 170

	// TARRAY
171
	int32	bound;		// negative is dynamic array
Russ Cox's avatar
Russ Cox committed
172

Russ Cox's avatar
Russ Cox committed
173
	int32	maplineno;	// first use of TFORW as map key
174
	int32	embedlineno;	// first use of TFORW as embedded type
175 176 177 178 179
};
#define	T	((Type*)0)

struct	Node
{
180 181 182 183 184
	uchar	op;
	uchar	ullman;		// sethi/ullman number
	uchar	addable;	// type of addressability - 0 is not addressable
	uchar	trecur;		// to detect loops
	uchar	etype;		// op for OASOP, etype for OTYPE, exclam for export
185
	uchar	class;		// PPARAM, PAUTO, PEXTERN, etc
186
	uchar	method;		// OCALLMETH name
Ken Thompson's avatar
Ken Thompson committed
187
	uchar	iota;		// OLITERAL made from iota
Ken Thompson's avatar
Ken Thompson committed
188
	uchar	embedded;	// ODCLFIELD embedded type
189
	uchar	colas;		// OAS resulting from :=
Russ Cox's avatar
Russ Cox committed
190
	uchar	diag;		// already printed error about this
191
	uchar	noescape;	// ONAME never move to heap
Russ Cox's avatar
Russ Cox committed
192
	uchar	funcdepth;
Russ Cox's avatar
Russ Cox committed
193
	uchar	builtin;	// built-in name, like len or close
194
	uchar	walkdef;
195
	uchar	typecheck;
196
	uchar	local;
Russ Cox's avatar
Russ Cox committed
197
	uchar	initorder;
198 199 200 201 202

	// most nodes
	Node*	left;
	Node*	right;
	Type*	type;
203 204
	NodeList*	list;
	NodeList*	rlist;
205 206

	// for-body
207
	NodeList*	ninit;
208 209
	Node*	ntest;
	Node*	nincr;
210
	NodeList*	nbody;
211 212

	// if-body
213
	NodeList*	nelse;
214 215 216 217 218 219

	// cases
	Node*	ncase;

	// func
	Node*	nname;
220
	Node*	shortname;
221 222 223
	NodeList*	enter;
	NodeList*	exit;
	NodeList*	cvars;	// closure params
224
	NodeList*	dcl;	// autodcl for this func/closure
225

Ken Thompson's avatar
Ken Thompson committed
226
	// OLITERAL/OREGISTER
227 228
	Val	val;

229 230 231 232
	// ONAME
	Node*	ntype;
	Node*	defn;

233 234 235 236 237
	// ONAME func param with PHEAP
	Node*	heapaddr;	// temp holding heap address of param
	Node*	stackparam;	// OPARAM node referring to stack copy of param
	Node*	alloc;	// allocation call

Russ Cox's avatar
Russ Cox committed
238 239 240 241
	// ONAME closure param with PPARAMREF
	Node*	outer;	// outer PPARAMREF in nested closure
	Node*	closure;	// ONAME/PHEAP <-> ONAME/PPARAMREF

242 243
	Sym*	psym;		// import
	Sym*	sym;		// various
244 245
	int32	vargen;		// unique name for OTYPE/ONAME
	int32	lineno;
246
	vlong	xoffset;
Russ Cox's avatar
Russ Cox committed
247
	int32	ostk;
248 249 250
};
#define	N	((Node*)0)

251
struct	NodeList
252 253 254 255 256 257
{
	Node*	n;
	NodeList*	next;
	NodeList*	end;
};

Russ Cox's avatar
Russ Cox committed
258
enum
259
{
Russ Cox's avatar
Russ Cox committed
260 261 262 263 264 265 266
	SymExport	= 1<<0,
	SymPackage	= 1<<1,
	SymExported	= 1<<2,
	SymImported	= 1<<3,
	SymUniq		= 1<<4,
	SymSiggen	= 1<<5,
};
267

Russ Cox's avatar
Russ Cox committed
268 269 270 271
struct	Sym
{
	ushort	lexical;
	uchar	flags;
272
	uchar	sym;		// huffman encoding in object file
Russ Cox's avatar
Russ Cox committed
273
	Sym*	link;
274

Russ Cox's avatar
Russ Cox committed
275
	// saved and restored by dcopy
276 277
	char*	package;	// package name
	char*	name;		// variable name
Russ Cox's avatar
Russ Cox committed
278
	Node*	def;		// definition: ONAME OTYPE OPACK or OLITERAL
Russ Cox's avatar
Russ Cox committed
279
	int32	block;		// blocknumber to catch redeclaration
Ken Thompson's avatar
Ken Thompson committed
280
	int32	lastlineno;	// last declaration for diagnostic
281 282 283 284 285 286 287 288 289 290 291 292 293
};
#define	S	((Sym*)0)

typedef	struct	Iter	Iter;
struct	Iter
{
	int	done;
	Type*	tfunc;
	Type*	t;
	Node**	an;
	Node*	n;
};

294 295 296 297 298
typedef	struct	Hist	Hist;
struct	Hist
{
	Hist*	link;
	char*	name;
299 300
	int32	line;
	int32	offset;
301 302 303
};
#define	H	((Hist*)0)

304 305 306 307
enum
{
	OXXX,

308 309 310 311 312 313 314 315
	// names
	ONAME,
	ONONAME,
	OTYPE,
	OPACK,
	OLITERAL,

	// exprs
Russ Cox's avatar
Russ Cox committed
316
	OADD, OSUB, OOR, OXOR, OADDSTR,
317
	OADDR,
318
	OANDAND,
Russ Cox's avatar
Russ Cox committed
319
	OAPPENDSTR,
320
	OARRAY,
Russ Cox's avatar
Russ Cox committed
321
	OARRAYBYTESTR, OARRAYRUNESTR,
322
	OAS, OAS2, OAS2MAPW, OAS2FUNC, OAS2RECV, OAS2MAPR, OAS2DOTTYPE, OASOP,
323 324 325 326 327
	OBAD,
	OCALL, OCALLFUNC, OCALLMETH, OCALLINTER,
	OCAP,
	OCLOSE,
	OCLOSED,
328
	OCLOSURE,
Russ Cox's avatar
Russ Cox committed
329 330 331 332
	OCMPIFACE, OCMPSTR,
	OCOMPLIT, OMAPLIT, OSTRUCTLIT, OARRAYLIT,
	OCOMPSLICE, OCOMPMAP,
	OCONV, OCONVNOP, OCONVA2S, OCONVIFACE, OCONVSLICE,
333
	ODCL, ODCLFUNC, ODCLFIELD, ODCLCONST, ODCLTYPE,
334
	ODOT, ODOTPTR, ODOTMETH, ODOTINTER, OXDOT,
335
	ODOTTYPE,
336 337 338
	OEQ, ONE, OLT, OLE, OGE, OGT,
	OFUNC,
	OIND,
Russ Cox's avatar
Russ Cox committed
339
	OINDEX, OINDEXSTR, OINDEXMAP,
Russ Cox's avatar
Russ Cox committed
340
	OKEY, OPARAM,
341
	OLEN,
Russ Cox's avatar
Russ Cox committed
342
	OMAKE, OMAKECHAN, OMAKEMAP, OMAKESLICE,
343 344
	OHMUL, ORRC, OLRC,	// high-mul and rotate-carry
	OMUL, ODIV, OMOD, OLSH, ORSH, OAND, OANDNOT,
345 346 347 348
	ONEW,
	ONOT, OCOM, OPLUS, OMINUS,
	OOROR,
	OPANIC, OPANICN, OPRINT, OPRINTN,
Russ Cox's avatar
Russ Cox committed
349 350
	OSEND, OSENDNB,
	OSLICE, OSLICEARR, OSLICESTR,
351
	ORECV,
Russ Cox's avatar
Russ Cox committed
352
	ORUNESTR,
Russ Cox's avatar
Russ Cox committed
353
	OSELRECV,
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	// stmts
	OBLOCK,
	OBREAK,
	OCASE, OXCASE,
	OCONTINUE,
	ODEFER,
	OEMPTY,
	OFALL, OXFALL,
	OFOR,
	OGOTO,
	OIF,
	OLABEL,
	OPROC,
	ORANGE,
	ORETURN,
	OSELECT,
	OSWITCH,
	OTYPECASE,
	OTYPESW,

	// types
	OTCHAN,
	OTMAP,
	OTSTRUCT,
	OTINTER,
	OTFUNC,
	OTARRAY,

	// for back ends
	OCMP, ODEC, OEXTEND, OINC, OREGISTER, OINDREG,
385 386 387 388 389 390 391 392 393 394 395

	OEND,
};
enum
{
	Txxx,			// 0

	TINT8,	TUINT8,		// 1
	TINT16,	TUINT16,
	TINT32,	TUINT32,
	TINT64,	TUINT64,
Ken Thompson's avatar
Ken Thompson committed
396
	TINT, TUINT, TUINTPTR,
397

Ken Thompson's avatar
Ken Thompson committed
398
	TFLOAT32,		// 12
399 400
	TFLOAT64,
	TFLOAT80,
Ken Thompson's avatar
Ken Thompson committed
401
	TFLOAT,
402

Ken Thompson's avatar
Ken Thompson committed
403
	TBOOL,			// 16
404

Ken Thompson's avatar
Ken Thompson committed
405
	TPTR32, TPTR64,		// 17
406

Ken Thompson's avatar
Ken Thompson committed
407
	TDDD,			// 19
408 409
	TFUNC,
	TARRAY,
410
	T_old_DARRAY,
Russ Cox's avatar
Russ Cox committed
411
	TSTRUCT,		// 23
412 413
	TCHAN,
	TMAP,
Russ Cox's avatar
Russ Cox committed
414
	TINTER,			// 26
415 416 417 418 419
	TFORW,
	TFIELD,
	TANY,
	TSTRING,

420 421 422 423
	// pseudo-types for literals
	TIDEAL,
	TNIL,

Ken Thompson's avatar
Ken Thompson committed
424
	NTYPE,
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
};
enum
{
	CTxxx,

	CTINT,
	CTFLT,
	CTSTR,
	CTBOOL,
	CTNIL,
};

enum
{
	/* types of channel */
440
	/* must match ../../pkg/nreflect/type.go:/Chandir */
441
	Cxxx,
Russ Cox's avatar
Russ Cox committed
442 443 444
	Crecv = 1<<0,
	Csend = 1<<1,
	Cboth = Crecv | Csend,
445 446 447 448 449 450 451 452 453
};

enum
{
	Pxxx,

	PEXTERN,	// declaration context
	PAUTO,
	PPARAM,
454
	PPARAMOUT,
Russ Cox's avatar
Russ Cox committed
455
	PPARAMREF,	// param passed by reference
Russ Cox's avatar
Russ Cox committed
456
	PFUNC,
457 458

	PHEAP = 1<<7,
459 460
};

461 462
enum
{
463
	Etop = 1<<1,	// evaluated at statement level
Russ Cox's avatar
Russ Cox committed
464 465
	Erv = 1<<2,	// evaluated in value context
	Etype = 1<<3,
Russ Cox's avatar
Russ Cox committed
466 467
	Ecall = 1<<4,	// call-only expressions are ok
	Efnstruct = 1<<5,	// multivalue function returns are ok
Russ Cox's avatar
Russ Cox committed
468
	Eiota = 1<<6,		// iota is ok
469 470
};

Russ Cox's avatar
Russ Cox committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
#define	BITS	5
#define	NVAR	(BITS*sizeof(uint32)*8)

typedef	struct	Bits	Bits;
struct	Bits
{
	uint32	b[BITS];
};

EXTERN	Bits	zbits;

typedef	struct	Var	Var;
struct	Var
{
	vlong	offset;
	Sym*	sym;
487
	int	width;
Russ Cox's avatar
Russ Cox committed
488 489 490 491 492 493
	char	name;
	char	etype;
};

EXTERN	Var	var[NVAR];

494 495 496 497 498 499 500 501 502
typedef	struct	Typedef	Typedef;
struct	Typedef
{
	char*	name;
	int	etype;
	int	sameas;
};

extern	Typedef	typedefs[];
Russ Cox's avatar
Russ Cox committed
503

504 505 506 507
typedef	struct	Sig	Sig;
struct Sig
{
	char*	name;
508 509 510 511
	char*	package;
	Sym*	isym;
	Sym*	tsym;
	Type*	type;
512 513 514 515 516 517
	uint32	hash;
	int32	perm;
	int32	offset;
	Sig*	link;
};

518 519 520 521 522
typedef	struct	Io	Io;
struct	Io
{
	char*	infile;
	Biobuf*	bin;
523
	int32	ilineno;
524
	int	peekc;
Ken Thompson's avatar
Ken Thompson committed
525
	int	peekc1;	// second peekc for ...
526 527 528
	char*	cp;	// used for content when bin==nil
};

529 530 531
typedef	struct	Dlist	Dlist;
struct	Dlist
{
Ken Thompson's avatar
Ken Thompson committed
532
	Type*	field;
533 534
};

535 536 537 538 539 540 541
typedef	struct	Idir	Idir;
struct Idir
{
	Idir*	link;
	char*	dir;
};

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
/*
 * argument passing to/from
 * smagic and umagic
 */
typedef	struct	Magic Magic;
struct	Magic
{
	int	w;	// input for both - width
	int	s;	// output for both - shift
	int	bad;	// output for both - unexpected failure

	// magic multiplier for signed literal divisors
	int64	sd;	// input - literal divisor
	int64	sm;	// output - multiplier

	// magic multiplier for unsigned literal divisors
	uint64	ud;	// input - literal divisor
	uint64	um;	// output - multiplier
	int	ua;	// output - adder
};

563 564 565 566 567 568 569 570 571 572 573
/*
 * note this is the runtime representation
 * of the compilers arrays.
 *
 * typedef	struct
 * {				// must not move anything
 * 	uchar	array[8];	// pointer to data
 * 	uchar	nel[4];		// number of elements
 * 	uchar	cap[4];		// allocated number of elements
 * } Array;
 */
574 575
EXTERN	int	Array_array;	// runtime offsetof(Array,array) - same for String
EXTERN	int	Array_nel;	// runtime offsetof(Array,nel) - same for String
576 577 578
EXTERN	int	Array_cap;	// runtime offsetof(Array,cap)
EXTERN	int	sizeof_Array;	// runtime sizeof(Array)

579 580 581 582 583 584 585 586 587 588 589 590 591

/*
 * note this is the runtime representation
 * of the compilers strings.
 *
 * typedef	struct
 * {				// must not move anything
 * 	uchar	array[8];	// pointer to data
 * 	uchar	nel[4];		// number of elements
 * } String;
 */
EXTERN	int	sizeof_String;	// runtime sizeof(String)

592 593
EXTERN	Dlist	dotlist[10];	// size is max depth of embeddeds

594 595
EXTERN	Io	curio;
EXTERN	Io	pushedio;
596
EXTERN	int32	lineno;
597
EXTERN	int32	prevlineno;
598 599 600 601
EXTERN	char*	pathname;
EXTERN	Hist*	hist;
EXTERN	Hist*	ehist;

Ken Thompson's avatar
Ken Thompson committed
602
EXTERN	char*	infile;
603 604 605 606
EXTERN	char*	outfile;
EXTERN	char*	package;
EXTERN	Biobuf*	bout;
EXTERN	int	nerrors;
Russ Cox's avatar
Russ Cox committed
607
EXTERN	int	nsyntaxerrors;
608
EXTERN	char	namebuf[NSYMB];
609
EXTERN	char	lexbuf[NSYMB];
610 611 612
EXTERN	char	debug[256];
EXTERN	Sym*	hash[NHASH];
EXTERN	Sym*	pkgmyname;	// my name for package
Ken Thompson's avatar
Ken Thompson committed
613
EXTERN	Sym*	pkgimportname;	// package name from imported package
614 615
EXTERN	int	tptr;		// either TPTR32 or TPTR64
extern	char*	sysimport;
Ken Thompson's avatar
Ken Thompson committed
616
extern	char*	unsafeimport;
Ken Thompson's avatar
Ken Thompson committed
617
EXTERN	char*	filename;	// name to uniqify names
618
EXTERN	Idir*	idirs;
619 620

EXTERN	Type*	types[NTYPE];
621
EXTERN	Type*	idealstring;
Ken Thompson's avatar
Ken Thompson committed
622
EXTERN	uchar	simtype[NTYPE];
623
EXTERN	uchar	isptr[NTYPE];
Russ Cox's avatar
Russ Cox committed
624
EXTERN	uchar	isforw[NTYPE];
625 626 627
EXTERN	uchar	isint[NTYPE];
EXTERN	uchar	isfloat[NTYPE];
EXTERN	uchar	issigned[NTYPE];
628
EXTERN	uchar	issimple[NTYPE];
629

630 631 632
EXTERN	uchar	okforeq[NTYPE];
EXTERN	uchar	okforadd[NTYPE];
EXTERN	uchar	okforand[NTYPE];
633 634 635 636 637 638 639
EXTERN	uchar	okfornone[NTYPE];
EXTERN	uchar	okforcmp[NTYPE];
EXTERN	uchar	okforbool[NTYPE];
EXTERN	uchar	okforcap[NTYPE];
EXTERN	uchar	okforlen[NTYPE];
EXTERN	uchar	okforarith[NTYPE];
EXTERN	uchar*	okfor[OEND];
Russ Cox's avatar
Russ Cox committed
640
EXTERN	uchar	iscmp[OEND];
Ken Thompson's avatar
Ken Thompson committed
641 642 643 644 645

EXTERN	Mpint*	minintval[NTYPE];
EXTERN	Mpint*	maxintval[NTYPE];
EXTERN	Mpflt*	minfltval[NTYPE];
EXTERN	Mpflt*	maxfltval[NTYPE];
646

Russ Cox's avatar
Russ Cox committed
647
EXTERN	NodeList*	xtop;
Russ Cox's avatar
Russ Cox committed
648
EXTERN	NodeList*	externdcl;
649
EXTERN	NodeList*	closures;
Russ Cox's avatar
Russ Cox committed
650 651
EXTERN	NodeList*	exportlist;
EXTERN	NodeList*	typelist;
652
EXTERN	int	dclcontext;		// PEXTERN/PAUTO
Ken Thompson's avatar
Ken Thompson committed
653
EXTERN	int	inimportsys;
Ken Thompson's avatar
Ken Thompson committed
654
EXTERN	int	initflag;		// compiling the init fn
655
EXTERN	int	statuniqgen;		// name generator for static temps
Russ Cox's avatar
Russ Cox committed
656
EXTERN	int	loophack;
657

658
EXTERN	uint32	iota;
659
EXTERN	NodeList*	lastconst;
660
EXTERN	Node*	lasttype;
661
EXTERN	int32	maxarg;
662
EXTERN	int32	stksize;		// stack size for current frame
Russ Cox's avatar
Russ Cox committed
663 664
EXTERN	int32	blockgen;		// max block number
EXTERN	int32	block;			// current block number
Ken Thompson's avatar
defer  
Ken Thompson committed
665
EXTERN	int	hasdefer;		// flag that curfn has defer statetment
666

Russ Cox's avatar
Russ Cox committed
667 668
EXTERN	Node*	curfn;

669 670 671
EXTERN	int	maxround;
EXTERN	int	widthptr;

Ken Thompson's avatar
Ken Thompson committed
672
EXTERN	Node*	typeswvar;
673

Russ Cox's avatar
Russ Cox committed
674
EXTERN	char*	structpkg;
675 676
extern	int	thechar;
extern	char*	thestring;
677
EXTERN	char*	hunk;
678 679
EXTERN	int32	nhunk;
EXTERN	int32	thunk;
680

Russ Cox's avatar
Russ Cox committed
681
EXTERN	int	exporting;
Russ Cox's avatar
Russ Cox committed
682
EXTERN	int	noargnames;
Russ Cox's avatar
Russ Cox committed
683

Russ Cox's avatar
Russ Cox committed
684
EXTERN	int	funcdepth;
685
EXTERN	int	typecheckok;
Russ Cox's avatar
Russ Cox committed
686 687


688 689 690 691 692 693 694 695
/*
 *	y.tab.c
 */
int	yyparse(void);

/*
 *	lex.c
 */
Ken Thompson's avatar
Ken Thompson committed
696
void	setfilename(char*);
697
void	addidir(char*);
698
void	importfile(Val*);
Ken Thompson's avatar
Ken Thompson committed
699
void	cannedimports(char*, char*);
700
void	unimportfile();
701
int32	yylex(void);
702
void	yyoptsemi(int);
Russ Cox's avatar
Russ Cox committed
703
void	typeinit(void);
704 705
void	lexinit(void);
char*	lexname(int);
706
int32	getr(void);
707
int	getnsc(void);
Ken Thompson's avatar
Ken Thompson committed
708
int	escchar(int, int*, vlong*);
709 710 711 712 713
int	getc(void);
void	ungetc(int);
void	mkpackage(char*);

/*
Ken Thompson's avatar
Ken Thompson committed
714
 *	mparith1.c
715
 */
Ken Thompson's avatar
Ken Thompson committed
716 717
int	mpcmpfixflt(Mpint *a, Mpflt *b);
int	mpcmpfltfix(Mpflt *a, Mpint *b);
Ken Thompson's avatar
Ken Thompson committed
718 719
int	mpcmpfixfix(Mpint *a, Mpint *b);
int	mpcmpfixc(Mpint *b, vlong c);
Ken Thompson's avatar
Ken Thompson committed
720 721
int	mpcmpfltflt(Mpflt *a, Mpflt *b);
int	mpcmpfltc(Mpflt *b, double c);
Ken Thompson's avatar
Ken Thompson committed
722 723
void	mpsubfixfix(Mpint *a, Mpint *b);
void	mpsubfltflt(Mpflt *a, Mpflt *b);
Ken Thompson's avatar
Ken Thompson committed
724 725 726 727
void	mpaddcfix(Mpint *a, vlong c);
void	mpaddcflt(Mpflt *a, double c);
void	mpmulcfix(Mpint *a, vlong c);
void	mpmulcflt(Mpflt *a, double c);
Ken Thompson's avatar
Ken Thompson committed
728
void	mpdivfixfix(Mpint *a, Mpint *b);
Ken Thompson's avatar
Ken Thompson committed
729 730 731
void	mpmodfixfix(Mpint *a, Mpint *b);
void	mpatofix(Mpint *a, char *s);
void	mpatoflt(Mpflt *a, char *s);
732
int	mpmovefltfix(Mpint *a, Mpflt *b);
Ken Thompson's avatar
Ken Thompson committed
733
void	mpmovefixflt(Mpflt *a, Mpint *b);
Ken Thompson's avatar
Ken Thompson committed
734
int	Bconv(Fmt*);
Ken Thompson's avatar
Ken Thompson committed
735

Ken Thompson's avatar
Ken Thompson committed
736 737 738 739 740 741 742 743
/*
 *	mparith2.c
 */
void	mpmovefixfix(Mpint *a, Mpint *b);
void	mpmovecfix(Mpint *a, vlong v);
int	mptestfix(Mpint *a);
void	mpaddfixfix(Mpint *a, Mpint *b);
void	mpmulfixfix(Mpint *a, Mpint *b);
744
void	mpmulfract(Mpint *a, Mpint *b);
Ken Thompson's avatar
Ken Thompson committed
745
void	mpdivmodfixfix(Mpint *q, Mpint *r, Mpint *n, Mpint *d);
746
void	mpdivfract(Mpint *a, Mpint *b);
Ken Thompson's avatar
Ken Thompson committed
747
void	mpnegfix(Mpint *a);
Ken Thompson's avatar
Ken Thompson committed
748
void	mpandfixfix(Mpint *a, Mpint *b);
749
void	mpandnotfixfix(Mpint *a, Mpint *b);
Ken Thompson's avatar
Ken Thompson committed
750 751 752 753 754 755
void	mplshfixfix(Mpint *a, Mpint *b);
void	mporfixfix(Mpint *a, Mpint *b);
void	mprshfixfix(Mpint *a, Mpint *b);
void	mpxorfixfix(Mpint *a, Mpint *b);
void	mpcomfix(Mpint *a);
vlong	mpgetfix(Mpint *a);
756
void	mpshiftfix(Mpint *a, int s);
Ken Thompson's avatar
Ken Thompson committed
757

Ken Thompson's avatar
Ken Thompson committed
758 759 760
/*
 *	mparith3.c
 */
761 762
int	sigfig(Mpflt *a);
void	mpnorm(Mpflt *a);
Ken Thompson's avatar
Ken Thompson committed
763 764 765 766 767 768 769 770
void	mpmovefltflt(Mpflt *a, Mpflt *b);
void	mpmovecflt(Mpflt *a, double f);
int	mptestflt(Mpflt *a);
void	mpaddfltflt(Mpflt *a, Mpflt *b);
void	mpmulfltflt(Mpflt *a, Mpflt *b);
void	mpdivfltflt(Mpflt *a, Mpflt *b);
void	mpnegflt(Mpflt *a);
double	mpgetflt(Mpflt *a);
771
int	Fconv(Fmt*);
772 773 774 775

/*
 *	subr.c
 */
776 777
void*	mal(int32);
void*	remal(void*, int32, int32);
778
void	errorexit(void);
779
uint32	stringhash(char*);
780 781
Sym*	lookup(char*);
Sym*	pkglookup(char*, char*);
782
Sym*	restrictlookup(char*, char*);
Russ Cox's avatar
6g:  
Russ Cox committed
783
void	importdot(Sym*);
784 785 786
void	yyerror(char*, ...);
void	warn(char*, ...);
void	fatal(char*, ...);
787
void	linehist(char*, int32, int);
788
int32	setlineno(Node*);
789
Node*	nod(int, Node*, Node*);
790
Node*	nodlit(Val);
791
Type*	typ(int);
Ken Thompson's avatar
Ken Thompson committed
792
int	algtype(Type*);
793 794
void	dodump(Node*, int);
void	dump(char*, Node*);
795
void	dumplist(char*, NodeList*);
796 797 798
Type*	aindex(Node*, Type*);
int	isnil(Node*);
int	isptrto(Type*, int);
Russ Cox's avatar
Russ Cox committed
799
int	istype(Type*, int);
800 801
int	isfixedarray(Type*);
int	isslice(Type*);
802
int	isinter(Type*);
Ken Thompson's avatar
Ken Thompson committed
803
int	isnilinter(Type*);
Ken Thompson's avatar
Ken Thompson committed
804
int	isddd(Type*);
805
Type*	maptype(Type*, Type*);
Ken Thompson's avatar
Ken Thompson committed
806
Type*	methtype(Type*);
807
Node*	typename(Type*);
808
int	eqtype(Type*, Type*);
Russ Cox's avatar
6g:  
Russ Cox committed
809
int	cvttype(Type*, Type*);
Russ Cox's avatar
Russ Cox committed
810
int	eqtypenoname(Type*, Type*);
811
void	argtype(Node*, Type*);
812
int	eqargs(Type*, Type*);
813
uint32	typehash(Type*, int, int);
814
void	frame(int);
815
Node*	nodintconst(int64);
816
void	nodconst(Node*, Type*, int64);
817 818
Node*	nodnil(void);
Node*	nodbool(int);
819 820 821
void	ullmancalc(Node*);
void	badtype(int, Type*, Type*);
Type*	ptrto(Type*);
822
NodeList*	cleanidlist(NodeList*);
823
Node*	syslook(char*, int);
Ken Thompson's avatar
Ken Thompson committed
824
Node*	treecopy(Node*);
825
NodeList*	listtreecopy(NodeList*);
Russ Cox's avatar
Russ Cox committed
826 827
int	isselect(Node*);
void	tempname(Node*, Type*);
828
Node*	staticname(Type*);
829
int	iscomposite(Type*);
830
Node*	callnew(Type*);
831
Node*	saferef(Node*, NodeList**);
832 833
int	is64(Type*);
int	noconv(Type*, Type*);
834 835 836 837 838
NodeList*	list1(Node*);
NodeList*	list(NodeList*, Node*);
NodeList*	concat(NodeList*, NodeList*);
int		count(NodeList*);
Node*	liststmt(NodeList*);
839 840 841 842 843 844 845 846 847 848 849 850 851 852

Type**	getthis(Type*);
Type**	getoutarg(Type*);
Type**	getinarg(Type*);

Type*	getthisx(Type*);
Type*	getoutargx(Type*);
Type*	getinargx(Type*);

Type*	structfirst(Iter*, Type**);
Type*	structnext(Iter*);
Type*	funcfirst(Iter*, Type*);
Type*	funcnext(Iter*);

853 854 855 856 857 858
int	brcom(int);
int	brrev(int);
void	setmaxarg(Type*);
int	dotoffset(Node*, int*, Node**);
void	tempname(Node*, Type*);

859 860
int	Econv(Fmt*);
int	Jconv(Fmt*);
861
int	Lconv(Fmt*);
862 863 864 865
int	Oconv(Fmt*);
int	Sconv(Fmt*);
int	Tconv(Fmt*);
int	Nconv(Fmt*);
Russ Cox's avatar
Russ Cox committed
866
void	exprfmt(Fmt*, Node*, int);
867
int	Wconv(Fmt*);
868 869
int	Zconv(Fmt*);

Russ Cox's avatar
Russ Cox committed
870 871
int	lookdot0(Sym*, Type*, Type**);
int	adddot1(Sym*, Type*, int, Type**);
872 873
Node*	adddot(Node*);
void	expandmeth(Sym*, Type*);
874
void	genwrapper(Type*, Type*, Sym*);
875

Russ Cox's avatar
Russ Cox committed
876 877
int	simsimtype(Type*);

878 879 880 881 882
int	powtwo(Node*);
Type*	tounsigned(Type*);
void	smagic(Magic*);
void	umagic(Magic*);

883 884 885
/*
 *	dcl.c
 */
Russ Cox's avatar
Russ Cox committed
886
void	declare(Node*, int);
887 888
Type*	dodcltype(Type*);
void	updatetype(Type*, Type*);
889
void	defaultlit(Node**, Type*);
Russ Cox's avatar
Russ Cox committed
890
void	defaultlit2(Node**, Node**, int);
Russ Cox's avatar
Russ Cox committed
891
int	structcount(Type*);
892
void	addmethod(Sym*, Type*, int);
893
Node*	methodname(Node*, Type*);
894
Node*	methodname1(Node*, Node*);
Russ Cox's avatar
Russ Cox committed
895
Sym*	methodsym(Sym*, Type*);
896
Type*	functype(Node*, NodeList*, NodeList*);
897 898
char*	thistypenam(Node*);
void	funcnam(Type*, char*);
Ken Thompson's avatar
init  
Ken Thompson committed
899
Node*	renameinit(Node*);
900 901
void	funchdr(Node*);
void	funcbody(Node*);
Russ Cox's avatar
Russ Cox committed
902
Node*	typenod(Type*);
903 904
Type*	dostruct(NodeList*, int);
Type**	stotype(NodeList*, int, Type**);
905
Type*	sortinter(Type*);
Ken Thompson's avatar
Ken Thompson committed
906 907
void	markdcl(void);
void	popdcl(void);
908
void	poptodcl(void);
909
void	dumpdcl(char*);
910 911 912 913
void	markdclstack(void);
void	testdclstack(void);
Sym*	pushdcl(Sym*);
void	addvar(Node*, Type*, int);
914 915
void	addtyp(Type*, int);
void	addconst(Node*, Node*, int);
916
Node*	fakethis(void);
917
int	isifacemethod(Type*);
918
Node*	dclname(Sym*);
919 920 921 922
Node*	newname(Sym*);
Node*	oldname(Sym*);
Type*	newtype(Sym*);
Type*	oldtype(Sym*);
923
void	fninit(NodeList*);
924
Node*	nametodcl(Node*, Type*);
925
NodeList*	checkarglist(NodeList*);
926 927 928
void	checkwidth(Type*);
void	defercheckwidth(void);
void	resumecheckwidth(void);
Ken Thompson's avatar
Ken Thompson committed
929
Node*	embedded(Sym*);
Russ Cox's avatar
Russ Cox committed
930
NodeList*	variter(NodeList*, Node*, NodeList*);
931
NodeList*	constiter(NodeList*, Node*, NodeList*);
932

933
Node*	unsafenmagic(Node*, NodeList*);
Russ Cox's avatar
Russ Cox committed
934
void	dclchecks(void);
935 936 937 938 939 940 941 942 943 944 945 946 947 948
void	funccompile(Node*);

Node*	typedcl0(Sym*);
Node*	typedcl1(Node*, Node*, int);
void	typedcl2(Type*, Type*);

/*
 * closure.c
 */
void	closurehdr(Node*);
Node*	closurebody(NodeList*);
void	typecheckclosure(Node*);
Node*	walkclosure(Node*, NodeList**);

Russ Cox's avatar
Russ Cox committed
949

Ken Thompson's avatar
Ken Thompson committed
950 951 952 953
/*
 * sinit.c
 */

954
NodeList*	initfix(NodeList*);
Ken Thompson's avatar
Ken Thompson committed
955

956 957 958
/*
 *	export.c
 */
Russ Cox's avatar
6g:  
Russ Cox committed
959
void	renameimports(void);
Russ Cox's avatar
Russ Cox committed
960
void	autoexport(Node*, int);
961
int	exportname(char*);
Russ Cox's avatar
Russ Cox committed
962 963
void	exportsym(Node*);
void	packagesym(Node*);
964 965 966 967 968
void	dumpe(Sym*);
void	dumpexport(void);
void	dumpexporttype(Sym*);
void	dumpexportvar(Sym*);
void	dumpexportconst(Sym*);
Russ Cox's avatar
Russ Cox committed
969
void	importconst(Sym *s, Type *t, Node *v);
Russ Cox's avatar
Russ Cox committed
970
void	importmethod(Sym *s, Type *t);
971
void	importtype(Type *s, Type *t);
Russ Cox's avatar
Russ Cox committed
972
void	importvar(Sym *s, Type *t, int ctxt);
Russ Cox's avatar
Russ Cox committed
973
void	checkimports(void);
Russ Cox's avatar
Russ Cox committed
974
Type*	pkgtype(Sym*);
975
Sym*	importsym(Sym*, int);
976 977 978 979 980

/*
 *	walk.c
 */
void	walk(Node*);
981
void	walkstmt(Node**);
982
void	walkstmtlist(NodeList*);
983
void	walkexprlist(NodeList*, NodeList**);
984
void	walkconv(Node**, NodeList**);
985
void	walkdottype(Node*, NodeList**);
Ken Thompson's avatar
Ken Thompson committed
986
void	walkas(Node*);
Ken Thompson's avatar
Ken Thompson committed
987
void	walkswitch(Node*);
988
void	walkrange(Node*);
Ken Thompson's avatar
Ken Thompson committed
989
void	walkselect(Node*);
990
void	walkdot(Node*, NodeList**);
991
void	walkexpr(Node**, NodeList**);
Russ Cox's avatar
Russ Cox committed
992 993 994
Node*	mkcall(char*, Type*, NodeList**, ...);
Node*	mkcall1(Node*, Type*, NodeList**, ...);
Node*	chanfn(char*, int, Type*);
995 996 997 998
Node*	ascompatee1(int, Node*, Node*, NodeList**);
NodeList*	ascompatee(int, NodeList*, NodeList*, NodeList**);
NodeList*	ascompatet(int, NodeList*, Type**, int, NodeList**);
NodeList*	ascompatte(int, Type**, NodeList*, int, NodeList**);
999
Node*	mapop(Node*, NodeList**);
Ken Thompson's avatar
Ken Thompson committed
1000
Type*	fixchan(Type*);
Russ Cox's avatar
Russ Cox committed
1001
Node*	ifacecvt(Type*, Node*, int, NodeList**);
1002 1003 1004
int	ifaceas(Type*, Type*, int);
int	ifaceas1(Type*, Type*, int);
void	ifacecheck(Type*, Type*, int, int);
1005
void	runifacechecks(void);
1006 1007
Node*	convas(Node*, NodeList**);
Node*	colas(NodeList*, NodeList*);
1008
void	colasdefn(NodeList*, Node*);
1009 1010 1011 1012 1013 1014
NodeList*	reorder1(NodeList*);
NodeList*	reorder3(NodeList*);
NodeList*	reorder4(NodeList*);
Node*	structlit(Node*, Node*, NodeList**);
Node*	arraylit(Node*, Node*, NodeList**);
Node*	maplit(Node*, Node*, NodeList**);
1015
void	heapmoves(void);
1016 1017
void	walkdeflist(NodeList*);
void	walkdef(Node*);
1018
void	typechecklist(NodeList*, int);
1019
void	typecheckswitch(Node*);
Russ Cox's avatar
Russ Cox committed
1020
void	typecheckselect(Node*);
1021
void	typecheckrange(Node*);
Russ Cox's avatar
Russ Cox committed
1022
Node*	typecheckconv(Node*, Node*, Type*, int);
1023
int	checkconv(Type*, Type*, int, int*, int*);
1024
Node*	typecheck(Node**, int);
1025 1026 1027 1028

/*
 *	const.c
 */
1029 1030
void	convlit1(Node**, Type*, int);
void	convlit(Node**, Type*);
1031 1032
void	evconst(Node*);
int	cmpslit(Node *l, Node *r);
Ken Thompson's avatar
Ken Thompson committed
1033
int	smallintconst(Node*);
Ken Thompson's avatar
Ken Thompson committed
1034
long	nonnegconst(Node*);
1035 1036
int	consttype(Node*);
int	isconst(Node*, int);
Russ Cox's avatar
Russ Cox committed
1037 1038
Mpflt*	truncfltlit(Mpflt*, Type*);
void	convconst(Node*, Type*, Val*);
1039

Russ Cox's avatar
Russ Cox committed
1040
/*
1041 1042 1043 1044
 *	align.c
 */
uint32	rnd(uint32, uint32);
void	dowidth(Type*);
1045
int	argsize(Type*);
1046 1047 1048

/*
 *	bits.c
Russ Cox's avatar
Russ Cox committed
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
 */
Bits	bor(Bits, Bits);
Bits	band(Bits, Bits);
Bits	bnot(Bits);
int	bany(Bits*);
int	bnum(Bits);
Bits	blsh(uint);
int	beq(Bits, Bits);
int	bset(Bits, uint);
int	Qconv(Fmt *fp);
int	bitno(int32);
1060

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
/*
 *	gen.c
 */
typedef	struct	Prog	Prog;
#define	P	((Prog*)0)

typedef	struct	Label Label;
struct	Label
{
	uchar	op;		// OGOTO/OLABEL
	Sym*	sym;
	Prog*	label;		// pointer to code
	Prog*	breakpc;	// pointer to code
	Prog*	continpc;	// pointer to code
	Label*	link;
};
#define	L	((Label*)0)

EXTERN	Label*	labellist;
EXTERN	Label*	findlab(Sym*);

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
typedef	struct	Plist	Plist;
struct	Plist
{
	Node*	name;
	Prog*	firstpc;
	int	recur;
	Plist*	link;
};

EXTERN	Plist*	plist;
EXTERN	Plist*	plast;

1094 1095 1096 1097 1098
EXTERN	Prog*	continpc;
EXTERN	Prog*	breakpc;
EXTERN	Prog*	pc;
EXTERN	Prog*	firstpc;

1099 1100 1101 1102
EXTERN	int	yylast;
EXTERN	int	yynext;
EXTERN	int	yysemi;

1103 1104 1105 1106 1107 1108 1109 1110
void	allocparams(void);
void	cgen_as(Node *nl, Node *nr);
void	cgen_callmeth(Node *n, int proc);
void	cgen_dcl(Node *n);
void	cgen_proc(Node *n, int proc);
void	checklabels(void);
Label*	findlab(Sym *s);
void	gen(Node *n);
1111
void	genlist(NodeList *l);
1112 1113
void	newlab(int op, Sym *s);
Node*	sysfunc(char *name);
1114
Plist*	newplist(void);
1115

1116 1117 1118 1119 1120 1121 1122 1123
/*
 *	obj.c
 */
void	Bputdot(Biobuf *b);
void	dumpglobls(void);
void	dumpobj(void);
void	ieeedtod(uint64 *ieee, double native);
void	outhist(Biobuf *b);
1124 1125

/*
1126
 *	arch-specific gen.c/gsubr.c/obj.c
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
 */
void	betypeinit(void);
vlong	convvtox(vlong, int);
void	compile(Node*);
void	proglist(void);
int	optopop(int);
void	dumpobj(void);
void	dowidth(Type*);
void	argspace(int32);
Node*	nodarg(Type*, int);
Type*	deep(Type*);
Type*	shallow(Type*);
Prog*	gjmp(Prog*);
void	patch(Prog*, Prog*);
void	bgen(Node *n, int true, Prog *to);
void	cgen_asop(Node *n);
void	cgen_call(Node *n, int proc);
void	cgen_callinter(Node *n, Node *res, int proc);
void	cgen_ret(Node *n);
int	isfat(Type*);
void	clearfat(Node *n);
void	cgen(Node*, Node*);
void	gused(Node*);
1150
void	dumptypestructs(void);
1151
void	dumpfuncs(void);
1152
void	dumpdata(void);
1153 1154 1155 1156 1157 1158 1159
void	ggloblnod(Node *nam, int32 width);
void	ggloblsym(Sym *s, int32 width, int dupok);
void	zfile(Biobuf *b, char *p, int n);
void	zhist(Biobuf *b, int line, vlong offset);
void	zname(Biobuf *b, Sym *s, int t);
void	nopout(Prog*);
int	dstringptr(Sym *s, int off, char *str);
Russ Cox's avatar
Russ Cox committed
1160 1161 1162 1163 1164
int	dgostringptr(Sym*, int off, char *str);
int	dgostrlitptr(Sym*, int off, Strlit*);
int	dsymptr(Sym *s, int off, Sym *x, int xoff);
int	duint8(Sym *s, int off, uint8 v);
int	duint16(Sym *s, int off, uint16 v);
1165
int	duint32(Sym *s, int off, uint32 v);
Russ Cox's avatar
Russ Cox committed
1166 1167
int	duint64(Sym *s, int off, uint64 v);
int	duintptr(Sym *s, int off, uint64 v);
1168
int	duintxx(Sym *s, int off, uint64 v, int wid);
Russ Cox's avatar
Russ Cox committed
1169
void	genembedtramp(Type*, Type*, Sym*);
1170 1171
int	gen_as_init(Node*, Node*);