summaryrefslogtreecommitdiff
path: root/limbo/limbo.h
blob: 7e63e89825551067f9b39afba52fa120cb090605 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#include "lib9.h"
#include "bio.h"
#include "isa.h"
#include "mathi.h"

/* internal dis ops */
#define IEXC	MAXDIS
#define IEXC0	(MAXDIS+1)
#define INOOP	(MAXDIS+2)

/* temporary */
#define	LDT	1

#ifndef Extern
#define Extern extern
#endif

#define YYMAXDEPTH	200

typedef	struct Addr	Addr;
typedef	struct Case	Case;
typedef	struct Decl	Decl;
typedef	struct Desc	Desc;
typedef	struct Dlist	Dlist;
typedef	struct Except	Except;
typedef struct File	File;
typedef struct Fline	Fline;
typedef	struct Inst	Inst;
typedef	struct Label	Label;
typedef	struct Line	Line;
typedef	struct Node	Node;
typedef struct Ok	Ok;
typedef	struct Src	Src;
typedef	struct Sym	Sym;
typedef struct Szal	Szal;
typedef	struct Tattr	Tattr;
typedef	struct Teq	Teq;
typedef	struct Tpair	Tpair;
typedef	struct Type	Type;
typedef	struct Typelist	Typelist;

typedef	double		Real;
typedef	vlong		Long;

enum
{
	STemp		= NREG * IBY2WD,
	RTemp		= STemp+IBY2WD,
	DTemp		= RTemp+IBY2WD,
	MaxTemp		= DTemp+IBY2WD,
	MaxReg		= 1<<16,
	MaxAlign	= IBY2LG,
	StrSize		= 256,
	NumSize		= 32,		/* max length of printed  */
	MaxIncPath	= 32,		/* max directories in include path */
	MaxScope	= 64,		/* max nested {} */
	MaxInclude	= 32,		/* max nested include "" */
	ScopeBuiltin	= 0,
	ScopeNils	= 1,
	ScopeGlobal	= 2
};

/*
 * return tuple from expression type checking
 */
struct Ok
{
	int	ok;
	int	allok;
};

/*
 * return tuple from type sizing
 */
struct Szal
{
	int	size;
	int	align;
};

/*
 * return tuple for file/line numbering
 */
struct Fline
{
	File	*file;
	int	line;
};

struct File
{
	char	*name;
	int	abs;			/* absolute line of start of the part of file */
	int	off;			/* offset to line in the file */
	int	in;			/* absolute line where included */
	char	*act;			/* name of real file with #line fake file */
	int	actoff;			/* offset from fake line to real line */
	int	sbl;			/* symbol file number */
};

struct Line
{
	int	line;
	int	pos;			/* character within the line */
};

struct Src
{
	Line	start;
	Line	stop;
};

enum
{
	Aimm,				/* immediate */
	Amp,				/* global */
	Ampind,				/* global indirect */
	Afp,				/* activation frame */
	Afpind,				/* frame indirect */
	Apc,				/* branch */
	Adesc,				/* type descriptor immediate */
	Aoff,				/* offset in module description table */
	Anoff,			/* above encoded as -ve */
	Aerr,				/* error */
	Anone,				/* no operand */
	Aldt,				/* linkage descriptor table immediate */
	Aend
};

struct Addr
{
	long	reg;
	long	offset;
	Decl	*decl;
};

struct Inst
{
	Src	src;
	ushort	op;
	long	pc;
	uchar	reach;			/* could a control path reach this instruction? */
	uchar	sm;			/* operand addressing modes */
	uchar	mm;
	uchar	dm;
	Addr	s;			/* operands */
	Addr	m;
	Addr	d;
	Inst	*branch;		/* branch destination */
	Inst	*next;
	int	block;			/* blocks nested inside */
};

struct Case
{
	int	nlab;
	int	nsnd;
	long	offset;			/* offset in mp */
	Label	*labs;
	Node	*wild;			/* if nothing matches */
	Inst	*iwild;
};

struct Label
{
	Node	*node;
	char	isptr;			/* true if the labelled alt channel is a pointer */
	Node	*start;			/* value in range [start, stop) => code */
	Node	*stop;
	Inst	*inst;
};

enum
{
	Dtype,
	Dfn,
	Dglobal,
	Darg,
	Dlocal,
	Dconst,
	Dfield,
	Dtag,				/* pick tags */
	Dimport,			/* imported identifier */
	Dunbound,			/* unbound identified */
	Dundef,
	Dwundef,			/* undefined, but don't whine */

	Dend
};

struct Decl
{
	Src	src;			/* where declaration */
	Sym	*sym;
	uchar	store;			/* storage class */
	uchar	nid;		/* block grouping for locals */
	schar	caninline;	/* inline function */
	uchar	das;		/* declared with := */
	Decl	*dot;			/* parent adt or module */
	Type	*ty;
	int	refs;			/* number of references */
	long	offset;
	int	tag;			/* union tag */

	uchar	scope;			/* in which it was declared */
	uchar	handler;		/* fn has exception handler in body */
	Decl	*next;			/* list in same scope, field or argument list, etc. */
	Decl	*old;			/* declaration of the symbol in enclosing scope */

	Node	*eimport;		/* expr from which imported */
	Decl	*importid;		/* identifier imported */
	Decl	*timport;		/* stack of identifiers importing a type */

	Node	*init;			/* data initialization */
	int	tref;			/* 1 => is a tmp; >=2 => tmp in use */
	char	cycle;			/* can create a cycle */
	char	cyc;			/* so labelled in source */
	char	cycerr;			/* delivered an error message for cycle? */
	char	implicit;		/* implicit first argument in an adt? */

	Decl	*iface;			/* used external declarations in a module */

	Decl	*locals;		/* locals for a function */
	Decl *link;			/* pointer to parent function or function argument or local share or parent type dec */
	Inst	*pc;			/* start of function */
	/* Inst	*endpc; */			/* limit of function - unused */

	Desc	*desc;			/* heap descriptor */
};

struct Desc
{
	int	id;			/* dis type identifier */
	uchar	used;			/* actually used in output? */
	uchar	*map;			/* byte map of pointers */
	long	size;			/* length of the object */
	long	nmap;			/* length of good bytes in map */
	Desc	*next;
};

struct Dlist
{
	Decl *d;
	Dlist *next;
};

struct Except
{
	Inst *p1;		/* first pc covered */
	Inst *p2;		/* last pc not covered */
	Case *c;		/* exception case instructions */
	Decl *d;		/* exception definition if any */
	Node *zn;		/* list of nodes to zero in handler */
	Desc *desc;	/* descriptor map for above */
	int ne;		/* number of exceptions (ie not strings) in case */
	Except *next;
};

struct Sym
{
	ushort	token;
	char	*name;
	int	len;
	int	hash;
	Sym	*next;
	Decl	*decl;
	Decl	*unbound;		/* place holder for unbound symbols */
};

/*
 * ops for nodes
 */
enum
{
	Oadd = 1,
	Oaddas,
	Oadr,
	Oadtdecl,
	Oalt,
	Oand,
	Oandand,
	Oandas,
	Oarray,
	Oas,
	Obreak,
	Ocall,
	Ocase,
	Ocast,
	Ochan,
	Ocomma,
	Ocomp,
	Ocondecl,
	Ocons,
	Oconst,
	Ocont,
	Odas,
	Odec,
	Odiv,
	Odivas,
	Odo,
	Odot,
	Oelem,
	Oeq,
	Oexcept,
	Oexdecl,
	Oexit,
	Oexp,
	Oexpas,
	Oexstmt,
	Ofielddecl,
	Ofnptr,
	Ofor,
	Ofunc,
	Ogeq,
	Ogt,
	Ohd,
	Oif,
	Oimport,
	Oinc,
	Oind,
	Oindex,
	Oinds,
	Oindx,
	Oinv,
	Ojmp,
	Olabel,
	Olen,
	Oleq,
	Oload,
	Olsh,
	Olshas,
	Olt,
	Omdot,
	Omod,
	Omodas,
	Omoddecl,
	Omul,
	Omulas,
	Oname,
	Oneg,
	Oneq,
	Onot,
	Onothing,
	Oor,
	Ooras,
	Ooror,
	Opick,
	Opickdecl,
	Opredec,
	Opreinc,
	Oraise,
	Orange,
	Orcv,
	Oref,
	Oret,
	Orsh,
	Orshas,
	Oscope,
	Oself,
	Oseq,
	Oslice,
	Osnd,
	Ospawn,
	Osub,
	Osubas,
	Otagof,
	Otl,
	Otuple,
	Otype,
	Otypedecl,
	Oused,
	Ovardecl,
	Ovardecli,
	Owild,
	Oxor,
	Oxoras,

	Oend
};

/*
 * moves
 */
enum
{
	Mas,
	Mcons,
	Mhd,
	Mtl,

	Mend
};

/*
 * addressability
 */
enum
{
	Rreg,				/* v(fp) */
	Rmreg,				/* v(mp) */
	Roff,				/* $v */
	Rnoff,			/* $v encoded as -ve */
	Rdesc,				/* $v */
	Rdescp,				/* $v */
	Rconst,				/* $v */
	Ralways,			/* preceeding are always addressable */
	Radr,				/* v(v(fp)) */
	Rmadr,				/* v(v(mp)) */
	Rcant,				/* following are not quite addressable */
	Rpc,				/* branch address */
	Rmpc,				/* cross module branch address */
	Rareg,				/* $v(fp) */
	Ramreg,				/* $v(mp) */
	Raadr,				/* $v(v(fp)) */
	Ramadr,				/* $v(v(mp)) */
	Rldt,				/* $v */

	Rend
};

#define PARENS	1
#define TEMP		2
#define FNPTRA	4	/* argument */
#define FNPTR2	8	/* 2nd parameter */
#define FNPTRN	16	/* use -ve offset */
#define FNPTR		(FNPTRA|FNPTR2|FNPTRN)

struct Node
{
	Src	src;
	uchar	op;
	uchar	addable;
	uchar	flags;
	uchar	temps;
	Node	*left;
	Node	*right;
	Type	*ty;
	Decl	*decl;
	Long	val;			/* for Oconst */
	Real	rval;			/* for Oconst */
};

enum
{
	/*
	 * types visible to limbo
	 */
	Tnone	= 0,
	Tadt,
	Tadtpick,			/* pick case of an adt */
	Tarray,
	Tbig,				/* 64 bit int */
	Tbyte,				/* 8 bit unsigned int */
	Tchan,
	Treal,
	Tfn,
	Tint,				/* 32 bit int */
	Tlist,
	Tmodule,
	Tref,
	Tstring,
	Ttuple,
	Texception,
	Tfix,
	Tpoly,

	/*
	 * internal use types
	 */
	Tainit,				/* array initializers */
	Talt,				/* alt channels */
	Tany,				/* type of nil */
	Tarrow,				/* unresolved ty->id types */
	Tcase,				/* case labels */
	Tcasel,				/* case big labels */
	Tcasec,				/* case string labels */
	Tdot,				/* unresolved ty.id types */
	Terror,
	Tgoto,				/* goto labels */
	Tid,				/* id with unknown type */
	Tiface,				/* module interface */
	Texcept,			/* exception handler tables */
	Tinst,			/* instantiated adt */

	Tend
};

enum
{
	OKbind		= 1 << 0,	/* type decls are bound */
	OKverify	= 1 << 1,	/* type looks ok */
	OKsized		= 1 << 2,	/* started figuring size */
	OKref		= 1 << 3,	/* recorded use of type */
	OKclass		= 1 << 4,	/* equivalence class found */
	OKcyc		= 1 << 5,	/* checked for cycles */
	OKcycsize	= 1 << 6,	/* checked for cycles and size */
	OKmodref	= 1 << 7,	/* started checking for a module handle */

	OKmask		= 0xff,

	/*
	 * recursive marks
	 */
	TReq		= 1 << 0,
	TRcom		= 1 << 1,
	TRcyc		= 1 << 2,
	TRvis		= 1 << 3,
};

/* type flags */
#define	FULLARGS	1	/* all hidden args added */
#define	INST	2		/* instantiated adt */
#define	CYCLIC	4	/* cyclic type */
#define	POLY	8	/* polymorphic types inside */
#define	NOPOLY	16	/* no polymorphic types inside */

struct Type
{
	Src	src;
	uchar	kind;
	uchar	varargs;		/* if a function, ends with vargs? */
	uchar	ok;			/* set when type is verified */
	uchar	linkall;		/* put all iface fns in external linkage? */
	uchar	rec;			/* in the middle of recursive type */
	uchar	cons;		/* exception constant */
	uchar	align;		/* alignment in bytes */
	uchar	flags;
	int	sbl;			/* slot in .sbl adt table */
	long	sig;			/* signature for dynamic type check */
	long	size;			/* storage required, in bytes */
	Decl	*decl;
	Type	*tof;
	Decl	*ids;
	Decl	*tags;			/* tagged fields in an adt */
	Decl *polys;			/* polymorphic fields in fn or adt */
	Case	*cse;			/* case or goto labels */
	Type	*teq;			/* temporary equiv class for equiv checking */
	Type	*tcom;			/* temporary equiv class for compat checking */
	Teq	*eq;			/* real equiv class */
	Node *val;		/* for Tfix, Tfn, Tadt only */
	union {
		Node *eraises;		/* for Tfn only */
		Typelist *tlist;		/* for Tinst only */
		Tpair *tmap;		/* for Tadt only */
	} u;
};

/*
 * type equivalence classes
 */
struct Teq
{
	int	id;		/* for signing */
	Type	*ty;		/* an instance of the class */
	Teq	*eq;		/* used to link eq sets */
};

struct Tattr
{
	char	isptr;
	char	refable;
	char	conable;
	char	big;
	char	vis;			/* type visible to users */
};

enum {
	Sother,
	Sloop,
	Sscope
};

struct Tpair
{
	Type *t1;
	Type *t2;
	Tpair *nxt;
};

struct Typelist
{
	Type *t;
	Typelist *nxt;
};
	
Extern	Decl	**adts;
Extern	Sym	*anontupsym;		/* name assigned to all anonymouse tuples */
Extern	int	arrayz;
Extern	int	asmsym;			/* generate symbols in assembly language? */
Extern	Biobuf	*bins[MaxInclude];
Extern	int	blocks;
Extern	Biobuf	*bout;			/* output file */
Extern	Biobuf	*bsym;			/* symbol output file; nil => no sym out */
Extern	double	canonnan;		/* standard nan */
Extern	uchar	casttab[Tend][Tend];	/* instruction to cast from [1] to [2] */
Extern	long	constval;
Extern	Decl	*curfn;
Extern	char	debug[256];
Extern	Desc	*descriptors;		/* list of all possible descriptors */
Extern	int	dontcompile;		/* dis header flag */
Extern	int	dowarn;
Extern	char	*emitcode;		/* emit stub routines for system module functions */
Extern	int	emitdyn;		/* emit stub routines as above but for dynamic modules */
Extern	int	emitstub;		/* emit type and call frames for system modules */
Extern	char	*emittab;		/* emit table of runtime functions for this module */
Extern	int	errors;
Extern	char	escmap[256];
Extern	Inst	*firstinst;
Extern	long	fixss;			/* set extent from command line */
Extern	Decl	*fndecls;
Extern	Decl	**fns;
Extern	int	gendis;			/* generate dis or asm? */
Extern	Decl	*impdecl;			/* id of implementation module or union if many */
Extern	Dlist	*impdecls;		/* id(s) of implementation module(s) */
/* Extern	Sym	*impmod;	*/	/* name of implementation module */
Extern	Decl	*impmods;		/* name of implementation module(s) */
Extern	Decl	*iota;
Extern	uchar	isbyteinst[256];
Extern	int	isfatal;
Extern	int	isrelop[Oend];
Extern	uchar	isused[Oend];
Extern	Inst	*lastinst;
Extern	int	lenadts;
Extern	int	maxerr;
Extern	int	maxlabdep;		/* maximum nesting of breakable/continuable statements */
Extern	long	maxstack;		/* max size of a stack frame called */
Extern	int	mustcompile;		/* dis header flag */
Extern	int	nadts;
Extern	int	newfnptr;		/* ISELF and -ve indices */
Extern	int	nfns;
Extern	Decl	*nildecl;		/* declaration for limbo's nil */
Extern	int	nlabel;
Extern	int	dontinline;
Extern	Line	noline;
Extern	Src	nosrc;
Extern	uchar	opcommute[Oend];
Extern	int	opind[Tend];
Extern	uchar	oprelinvert[Oend];
Extern	int	optims;
Extern	char	*outfile;
Extern	Type	*precasttab[Tend][Tend];
Extern	int	scope;
Extern	Decl	*selfdecl;		/* declaration for limbo's self */
Extern	uchar	sideeffect[Oend];
Extern	char	*signdump;		/* dump sig for this fn */
Extern	int	superwarn;
Extern	char	*symfile;
Extern	Type	*tany;
Extern	Type	*tbig;
Extern	Type	*tbyte;
Extern	Type	*terror;
Extern	Type	*tint;
Extern	Type	*tnone;
Extern	Type	*treal;
Extern	Node	*tree;
Extern	Type	*tstring;
Extern	Type *texception;
Extern	Type	*tunknown;
Extern	Type *tfnptr;
Extern	Type	*rtexception;
Extern	char	unescmap[256];
Extern	Src	unifysrc;
Extern	Node	znode;

extern	int	*blockstack;
extern	int	blockdep;
extern	int	nblocks;
extern	File	**files;
extern	int	nfiles;
extern	uchar	chantab[Tend];
extern	uchar	disoptab[Oend+1][7];
extern	char	*instname[];
extern	char	*kindname[Tend];
extern	uchar	movetab[Mend][Tend];
extern	char	*opname[];
extern	int	setisbyteinst[];
extern	int	setisused[];
extern	int	setsideeffect[];
extern	char	*storename[Dend];
extern	int	storespace[Dend];
extern	Tattr	tattr[Tend];

#include "fns.h"

#pragma varargck	type	"D"	Decl*
#pragma varargck	type	"I"	Inst*
#pragma varargck	type	"K"	Decl*
#pragma varargck	type	"k"	Decl*
#pragma varargck	type	"L"	Line
#pragma varargck	type	"M"	Desc*
#pragma varargck	type	"n"	Node*
#pragma varargck	type	"O"	int
#pragma varargck	type	"O"	uint
#pragma varargck	type	"g"	double
#pragma varargck	type	"Q"	Node*
#pragma varargck	type	"R"	Type*
#pragma varargck	type	"T"	Type*
#pragma varargck	type	"t"	Type*
#pragma varargck	type	"U"	Src
#pragma varargck	type	"v"	Node*
#pragma	varargck	type	"V"	Node*