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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
|
// Inferno utils/cc/cc.h
// http://code.google.com/p/inferno-os/source/browse/utils/cc/cc.h
//
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
// Portions Copyright © 1997-1999 Vita Nuova Limited
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
// Portions Copyright © 2004,2006 Bruce Ellis
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
// Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <libc.h>
#include <bio.h>
#ifndef EXTERN
#define EXTERN extern
#endif
#undef getc
#undef ungetc
#undef BUFSIZ
#define getc ccgetc
#define ungetc ccungetc
typedef struct Node Node;
typedef struct Sym Sym;
typedef struct Type Type;
typedef struct Funct Funct;
typedef struct Decl Decl;
typedef struct Io Io;
typedef struct Hist Hist;
typedef struct Term Term;
typedef struct Init Init;
typedef struct Bits Bits;
typedef struct Dynimp Dynimp;
typedef struct Dynexp Dynexp;
#define BUFSIZ 8192
#define NSYMB 500
#define NHASH 1024
#define STRINGSZ 200
#define HISTSZ 20
#define YYMAXDEPTH 500
#define NTERM 10
#define MAXALIGN 7
#define SIGN(n) ((uvlong)1<<(n-1))
#define MASK(n) (SIGN(n)|(SIGN(n)-1))
#define BITS 5
#define NVAR (BITS*sizeof(uint32)*8)
struct Bits
{
uint32 b[BITS];
};
struct Node
{
Node* left;
Node* right;
void* label;
int32 pc;
int reg;
int32 xoffset;
double fconst; /* fp constant */
vlong vconst; /* non fp const */
char* cstring; /* character string */
ushort* rstring; /* rune string */
Sym* sym;
Type* type;
int32 lineno;
uchar op;
uchar oldop;
uchar xcast;
uchar class;
uchar etype;
uchar complex;
uchar addable;
uchar scale;
uchar garb;
};
#define Z ((Node*)0)
struct Sym
{
Sym* link;
Type* type;
Type* suetag;
Type* tenum;
char* macro;
int32 varlineno;
int32 offset;
vlong vconst;
double fconst;
Node* label;
ushort lexical;
char *name;
ushort block;
ushort sueblock;
uchar class;
uchar sym;
uchar aused;
uchar sig;
uchar dataflag;
};
#define S ((Sym*)0)
enum{
SIGNONE = 0,
SIGDONE = 1,
SIGINTERN = 2,
SIGNINTERN = 1729*325*1729,
};
struct Decl
{
Decl* link;
Sym* sym;
Type* type;
int32 varlineno;
int32 offset;
short val;
ushort block;
uchar class;
uchar aused;
};
#define D ((Decl*)0)
struct Type
{
Sym* sym;
Sym* tag;
Funct* funct;
Type* link;
Type* down;
int32 width;
int32 offset;
int32 lineno;
uchar shift;
uchar nbits;
uchar etype;
uchar garb;
uchar align;
};
#define T ((Type*)0)
#define NODECL ((void(*)(int, Type*, Sym*))0)
struct Init /* general purpose initialization */
{
int code;
uint32 value;
char* s;
};
EXTERN struct
{
char* p;
int c;
} fi;
struct Io
{
Io* link;
char* p;
char b[BUFSIZ];
short c;
short f;
};
#define I ((Io*)0)
struct Hist
{
Hist* link;
char* name;
int32 line;
int32 offset;
};
#define H ((Hist*)0)
EXTERN Hist* hist;
struct Term
{
vlong mult;
Node *node;
};
enum
{
Axxx,
Ael1,
Ael2,
Asu2,
Aarg0,
Aarg1,
Aarg2,
Aaut3,
NALIGN,
};
enum
{
DMARK,
DAUTO,
DSUE,
DLABEL,
};
enum
{
OXXX,
OADD,
OADDR,
OAND,
OANDAND,
OARRAY,
OAS,
OASI,
OASADD,
OASAND,
OASASHL,
OASASHR,
OASDIV,
OASHL,
OASHR,
OASLDIV,
OASLMOD,
OASLMUL,
OASLSHR,
OASMOD,
OASMUL,
OASOR,
OASSUB,
OASXOR,
OBIT,
OBREAK,
OCASE,
OCAST,
OCOMMA,
OCOND,
OCONST,
OCONTINUE,
ODIV,
ODOT,
ODOTDOT,
ODWHILE,
OENUM,
OEQ,
OEXREG,
OFOR,
OFUNC,
OGE,
OGOTO,
OGT,
OHI,
OHS,
OIF,
OIND,
OINDREG,
OINIT,
OLABEL,
OLDIV,
OLE,
OLIST,
OLMOD,
OLMUL,
OLO,
OLS,
OLSHR,
OLT,
OMOD,
OMUL,
ONAME,
ONE,
ONOT,
OOR,
OOROR,
OPOSTDEC,
OPOSTINC,
OPREDEC,
OPREINC,
OPREFETCH,
OPROTO,
OREGISTER,
ORETURN,
OSET,
OSIGN,
OSIZE,
OSTRING,
OLSTRING,
OSTRUCT,
OSUB,
OSWITCH,
OUNION,
OUSED,
OWHILE,
OXOR,
ONEG,
OCOM,
OPOS,
OELEM,
OTST, /* used in some compilers */
OINDEX,
OFAS,
OREGPAIR,
OROTL,
OEND
};
enum
{
TXXX,
TCHAR,
TUCHAR,
TSHORT,
TUSHORT,
TINT,
TUINT,
TLONG,
TULONG,
TVLONG,
TUVLONG,
TFLOAT,
TDOUBLE,
TIND,
TFUNC,
TARRAY,
TVOID,
TSTRUCT,
TUNION,
TENUM,
NTYPE,
TAUTO = NTYPE,
TEXTERN,
TSTATIC,
TTYPEDEF,
TTYPESTR,
TREGISTER,
TCONSTNT,
TVOLATILE,
TUNSIGNED,
TSIGNED,
TDOT,
TFILE,
TOLD,
NALLTYPES,
};
enum
{
CXXX,
CAUTO,
CEXTERN,
CGLOBL,
CSTATIC,
CLOCAL,
CTYPEDEF,
CTYPESTR,
CPARAM,
CSELEM,
CLABEL,
CEXREG,
NCTYPES,
};
enum
{
GXXX = 0,
GCONSTNT = 1<<0,
GVOLATILE = 1<<1,
NGTYPES = 1<<2,
GINCOMPLETE = 1<<2,
};
enum
{
BCHAR = 1L<<TCHAR,
BUCHAR = 1L<<TUCHAR,
BSHORT = 1L<<TSHORT,
BUSHORT = 1L<<TUSHORT,
BINT = 1L<<TINT,
BUINT = 1L<<TUINT,
BLONG = 1L<<TLONG,
BULONG = 1L<<TULONG,
BVLONG = 1L<<TVLONG,
BUVLONG = 1L<<TUVLONG,
BFLOAT = 1L<<TFLOAT,
BDOUBLE = 1L<<TDOUBLE,
BIND = 1L<<TIND,
BFUNC = 1L<<TFUNC,
BARRAY = 1L<<TARRAY,
BVOID = 1L<<TVOID,
BSTRUCT = 1L<<TSTRUCT,
BUNION = 1L<<TUNION,
BENUM = 1L<<TENUM,
BFILE = 1L<<TFILE,
BDOT = 1L<<TDOT,
BCONSTNT = 1L<<TCONSTNT,
BVOLATILE = 1L<<TVOLATILE,
BUNSIGNED = 1L<<TUNSIGNED,
BSIGNED = 1L<<TSIGNED,
BAUTO = 1L<<TAUTO,
BEXTERN = 1L<<TEXTERN,
BSTATIC = 1L<<TSTATIC,
BTYPEDEF = 1L<<TTYPEDEF,
BTYPESTR = 1L<<TTYPESTR,
BREGISTER = 1L<<TREGISTER,
BINTEGER = BCHAR|BUCHAR|BSHORT|BUSHORT|BINT|BUINT|
BLONG|BULONG|BVLONG|BUVLONG,
BNUMBER = BINTEGER|BFLOAT|BDOUBLE,
/* these can be overloaded with complex types */
BCLASS = BAUTO|BEXTERN|BSTATIC|BTYPEDEF|BTYPESTR|BREGISTER,
BGARB = BCONSTNT|BVOLATILE,
};
struct Funct
{
Sym* sym[OEND];
Sym* castto[NTYPE];
Sym* castfr[NTYPE];
};
EXTERN struct
{
Type* tenum; /* type of entire enum */
Type* cenum; /* type of current enum run */
vlong lastenum; /* value of current enum */
double floatenum; /* value of current enum */
} en;
EXTERN int autobn;
EXTERN int32 autoffset;
EXTERN int blockno;
EXTERN Decl* dclstack;
EXTERN int debug[256];
EXTERN Hist* ehist;
EXTERN int32 firstbit;
EXTERN Sym* firstarg;
EXTERN Type* firstargtype;
EXTERN Decl* firstdcl;
EXTERN int fperror;
EXTERN Sym* hash[NHASH];
EXTERN char* hunk;
EXTERN char** include;
EXTERN Io* iofree;
EXTERN Io* ionext;
EXTERN Io* iostack;
EXTERN int32 lastbit;
EXTERN char lastclass;
EXTERN Type* lastdcl;
EXTERN int32 lastfield;
EXTERN Type* lasttype;
EXTERN int32 lineno;
EXTERN int32 nearln;
EXTERN int nerrors;
EXTERN int newflag;
EXTERN int32 nhunk;
EXTERN int ninclude;
EXTERN Node* nodproto;
EXTERN Node* nodcast;
EXTERN int32 nsymb;
EXTERN Biobuf outbuf;
EXTERN Biobuf diagbuf;
EXTERN char* outfile;
EXTERN char* pathname;
EXTERN int peekc;
EXTERN int32 stkoff;
EXTERN Type* strf;
EXTERN Type* strl;
EXTERN char* symb;
EXTERN Sym* symstring;
EXTERN int taggen;
EXTERN Type* tfield;
EXTERN Type* tufield;
EXTERN int thechar;
EXTERN char* thestring;
EXTERN Type* thisfn;
EXTERN int32 thunk;
EXTERN Type* types[NALLTYPES];
EXTERN Type* fntypes[NALLTYPES];
EXTERN Node* initlist;
EXTERN Term term[NTERM];
EXTERN int nterm;
EXTERN int packflg;
EXTERN int fproundflg;
EXTERN int textflag;
EXTERN int dataflag;
EXTERN int flag_largemodel;
EXTERN int ncontin;
EXTERN int canreach;
EXTERN int warnreach;
EXTERN Bits zbits;
EXTERN Fmt pragcgobuf;
extern char *onames[], *tnames[], *gnames[];
extern char *cnames[], *qnames[], *bnames[];
extern uchar tab[NTYPE][NTYPE];
extern uchar comrel[], invrel[], logrel[];
extern int32 ncast[], tadd[], tand[];
extern int32 targ[], tasadd[], tasign[], tcast[];
extern int32 tdot[], tfunct[], tindir[], tmul[];
extern int32 tnot[], trel[], tsub[];
extern uchar typeaf[];
extern uchar typefd[];
extern uchar typei[];
extern uchar typesu[];
extern uchar typesuv[];
extern uchar typeu[];
extern uchar typev[];
extern uchar typec[];
extern uchar typeh[];
extern uchar typeil[];
extern uchar typeilp[];
extern uchar typechl[];
extern uchar typechlv[];
extern uchar typechlvp[];
extern uchar typechlp[];
extern uchar typechlpfd[];
EXTERN uchar* typeword;
EXTERN uchar* typecmplx;
extern uint32 thash1;
extern uint32 thash2;
extern uint32 thash3;
extern uint32 thash[];
/*
* compat.c/unix.c/windows.c
*/
int systemtype(int);
int pathchar(void);
/*
* parser
*/
int yyparse(void);
int mpatov(char*, vlong*);
/*
* lex.c
*/
void* allocn(void*, int32, int32);
void* alloc(int32);
void ensuresymb(int32);
void cinit(void);
int compile(char*, char**, int);
void errorexit(void);
int filbuf(void);
int getc(void);
int32 getr(void);
int getnsc(void);
Sym* lookup(void);
void main(int, char*[]);
void newfile(char*, int);
void newio(void);
void pushio(void);
int32 escchar(int32, int, int);
Sym* slookup(char*);
void syminit(Sym*);
void unget(int);
int32 yylex(void);
int Lconv(Fmt*);
int Tconv(Fmt*);
int FNconv(Fmt*);
int Oconv(Fmt*);
int Qconv(Fmt*);
int VBconv(Fmt*);
void setinclude(char*);
/*
* mac.c
*/
void dodefine(char*);
void domacro(void);
Sym* getsym(void);
int32 getnsn(void);
void linehist(char*, int);
void macdef(void);
void macprag(void);
void macend(void);
void macexpand(Sym*, char*);
void macif(int);
void macinc(void);
void maclin(void);
void macund(void);
/*
* dcl.c
*/
Node* doinit(Sym*, Type*, int32, Node*);
Type* tcopy(Type*);
Node* init1(Sym*, Type*, int32, int);
Node* newlist(Node*, Node*);
void adecl(int, Type*, Sym*);
int anyproto(Node*);
void argmark(Node*, int);
void dbgdecl(Sym*);
Node* dcllabel(Sym*, int);
Node* dodecl(void(*)(int, Type*, Sym*), int, Type*, Node*);
Sym* mkstatic(Sym*);
void doenum(Sym*, Node*);
void snap(Type*);
Type* dotag(Sym*, int, int);
void edecl(int, Type*, Sym*);
Type* fnproto(Node*);
Type* fnproto1(Node*);
void markdcl(void);
Type* paramconv(Type*, int);
void pdecl(int, Type*, Sym*);
Decl* push(void);
Decl* push1(Sym*);
Node* revertdcl(void);
int32 xround(int32, int);
int rsametype(Type*, Type*, int, int);
int sametype(Type*, Type*);
uint32 sign(Sym*);
uint32 signature(Type*);
void sualign(Type*);
void tmerge(Type*, Sym*);
void walkparam(Node*, int);
void xdecl(int, Type*, Sym*);
Node* contig(Sym*, Node*, int32);
/*
* com.c
*/
void ccom(Node*);
void complex(Node*);
int tcom(Node*);
int tcoma(Node*, Node*, Type*, int);
int tcomd(Node*);
int tcomo(Node*, int);
int tcomx(Node*);
int tlvalue(Node*);
void constas(Node*, Type*, Type*);
/*
* con.c
*/
void acom(Node*);
void acom1(vlong, Node*);
void acom2(Node*, Type*);
int acomcmp1(const void*, const void*);
int acomcmp2(const void*, const void*);
int addo(Node*);
void evconst(Node*);
/*
* funct.c
*/
int isfunct(Node*);
void dclfunct(Type*, Sym*);
/*
* sub.c
*/
void arith(Node*, int);
int deadheads(Node*);
Type* dotsearch(Sym*, Type*, Node*, int32*);
int32 dotoffset(Type*, Type*, Node*);
void gethunk(void);
Node* invert(Node*);
int bitno(int32);
void makedot(Node*, Type*, int32);
int mixedasop(Type*, Type*);
Node* new(int, Node*, Node*);
Node* new1(int, Node*, Node*);
int nilcast(Type*, Type*);
int nocast(Type*, Type*);
void prtree(Node*, char*);
void prtree1(Node*, int, int);
void relcon(Node*, Node*);
int relindex(int);
int simpleg(int32);
Type* garbt(Type*, int32);
int simplec(int32);
Type* simplet(int32);
int stcompat(Node*, Type*, Type*, int32[]);
int tcompat(Node*, Type*, Type*, int32[]);
void tinit(void);
Type* typ(int, Type*);
Type* copytyp(Type*);
void typeext(Type*, Node*);
void typeext1(Type*, Node*);
int side(Node*);
int vconst(Node*);
int xlog2(uvlong);
int vlog(Node*);
int topbit(uint32);
void simplifyshift(Node*);
int32 typebitor(int32, int32);
void diag(Node*, char*, ...);
void warn(Node*, char*, ...);
void yyerror(char*, ...);
void fatal(Node*, char*, ...);
/*
* acid.c
*/
void acidtype(Type*);
void acidvar(Sym*);
/*
* godefs.c
*/
int Uconv(Fmt*);
void godeftype(Type*);
void godefvar(Sym*);
/*
* bits.c
*/
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);
/*
* dpchk.c
*/
void dpcheck(Node*);
void arginit(void);
void pragvararg(void);
void pragpack(void);
void pragfpround(void);
void pragdataflag(void);
void pragtextflag(void);
void pragincomplete(void);
void pragcgo(char*);
/*
* calls to machine depend part
*/
void codgen(Node*, Node*);
void gclean(void);
void gextern(Sym*, Node*, int32, int32);
void ginit(void);
int32 outstring(char*, int32);
int32 outlstring(ushort*, int32);
void sextern(Sym*, Node*, int32, int32);
void xcom(Node*);
int32 exreg(Type*);
int32 align(int32, Type*, int, int32*);
int32 maxround(int32, int32);
extern schar ewidth[];
/*
* com64
*/
int com64(Node*);
void com64init(void);
void bool64(Node*);
double convvtof(vlong);
vlong convftov(double);
double convftox(double, int);
vlong convvtox(vlong, int);
/*
* machcap
*/
int machcap(Node*);
#pragma varargck argpos warn 2
#pragma varargck argpos diag 2
#pragma varargck argpos yyerror 1
#pragma varargck type "F" Node*
#pragma varargck type "L" int32
#pragma varargck type "Q" int32
#pragma varargck type "O" int
#pragma varargck type "O" uint
#pragma varargck type "T" Type*
#pragma varargck type "U" char*
#pragma varargck type "|" int
enum
{
Plan9 = 1<<0,
Unix = 1<<1,
Windows = 1<<2,
};
int pathchar(void);
int systemtype(int);
void* alloc(int32 n);
void* allocn(void*, int32, int32);
|