]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/gc/go.go
[dev.ssa] Merge remote-tracking branch 'origin/master' into ssamerge
[gostls13.git] / src / cmd / compile / internal / gc / go.go
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 package gc
6
7 import (
8         "bytes"
9         "cmd/compile/internal/big"
10         "cmd/internal/obj"
11 )
12
13 // The parser's maximum stack size.
14 // We have to use a #define macro here since yacc
15 // or bison will check for its definition and use
16 // a potentially smaller value if it is undefined.
17 const (
18         NHUNK           = 50000
19         BUFSIZ          = 8192
20         NSYMB           = 500
21         NHASH           = 1024
22         MAXALIGN        = 7
23         UINF            = 100
24         PRIME1          = 3
25         BADWIDTH        = -1000000000
26         MaxStackVarSize = 10 * 1024 * 1024
27 )
28
29 const (
30         // Maximum size in bits for Mpints before signalling
31         // overflow and also mantissa precision for Mpflts.
32         Mpprec = 512
33         // Turn on for constant arithmetic debugging output.
34         Mpdebug = false
35 )
36
37 // Mpint represents an integer constant.
38 type Mpint struct {
39         Val  big.Int
40         Ovf  bool // set if Val overflowed compiler limit (sticky)
41         Rune bool // set if syntax indicates default type rune
42 }
43
44 // Mpflt represents a floating-point constant.
45 type Mpflt struct {
46         Val big.Float
47 }
48
49 // Mpcplx represents a complex constant.
50 type Mpcplx struct {
51         Real Mpflt
52         Imag Mpflt
53 }
54
55 type Val struct {
56         // U contains one of:
57         // bool     bool when n.ValCtype() == CTBOOL
58         // *Mpint   int when n.ValCtype() == CTINT, rune when n.ValCtype() == CTRUNE
59         // *Mpflt   float when n.ValCtype() == CTFLT
60         // *Mpcplx  pair of floats when n.ValCtype() == CTCPLX
61         // string   string when n.ValCtype() == CTSTR
62         // *Nilval  when n.ValCtype() == CTNIL
63         U interface{}
64 }
65
66 type NilVal struct{}
67
68 func (v Val) Ctype() Ctype {
69         switch x := v.U.(type) {
70         default:
71                 Fatalf("unexpected Ctype for %T", v.U)
72                 panic("not reached")
73         case nil:
74                 return 0
75         case *NilVal:
76                 return CTNIL
77         case bool:
78                 return CTBOOL
79         case *Mpint:
80                 if x.Rune {
81                         return CTRUNE
82                 }
83                 return CTINT
84         case *Mpflt:
85                 return CTFLT
86         case *Mpcplx:
87                 return CTCPLX
88         case string:
89                 return CTSTR
90         }
91 }
92
93 type Pkg struct {
94         Name     string // package name
95         Path     string // string literal used in import statement
96         Pathsym  *Sym
97         Prefix   string // escaped path for use in symbol table
98         Imported bool   // export data of this package was parsed
99         Exported bool   // import line written in export data
100         Direct   bool   // imported directly
101         Safe     bool   // whether the package is marked as safe
102         Syms     map[string]*Sym
103 }
104
105 type Sym struct {
106         Flags     uint8
107         Uniqgen   uint32
108         Link      *Sym
109         Importdef *Pkg   // where imported definition was found
110         Linkname  string // link name
111
112         // saved and restored by dcopy
113         Pkg        *Pkg
114         Name       string // variable name
115         Def        *Node  // definition: ONAME OTYPE OPACK or OLITERAL
116         Label      *Label // corresponding label (ephemeral)
117         Block      int32  // blocknumber to catch redeclaration
118         Lastlineno int32  // last declaration for diagnostic
119         Origpkg    *Pkg   // original package for . import
120         Lsym       *obj.LSym
121         Fsym       *Sym // funcsym
122 }
123
124 type Type struct {
125         Etype       EType
126         Nointerface bool
127         Noalg       bool
128         Chan        uint8
129         Trecur      uint8 // to detect loops
130         Printed     bool
131         Embedded    uint8 // TFIELD embedded type
132         Funarg      bool  // on TSTRUCT and TFIELD
133         Copyany     bool
134         Local       bool // created in this file
135         Deferwidth  bool
136         Broke       bool // broken type definition.
137         Isddd       bool // TFIELD is ... argument
138         Align       uint8
139         Haspointers uint8 // 0 unknown, 1 no, 2 yes
140
141         Nod    *Node // canonical OTYPE node
142         Orig   *Type // original type (type literal or predefined type)
143         Lineno int
144
145         // TFUNC
146         Thistuple int
147         Outtuple  int
148         Intuple   int
149         Outnamed  bool
150
151         Method  *Type
152         Xmethod *Type
153
154         Sym    *Sym
155         Vargen int32 // unique name for OTYPE/ONAME
156
157         Nname  *Node
158         Argwid int64
159
160         // most nodes
161         Type  *Type // actual type for TFIELD, element type for TARRAY, TCHAN, TMAP, TPTRxx
162         Width int64 // offset in TFIELD, width in all others
163
164         // TFIELD
165         Down  *Type   // next struct field, also key type in TMAP
166         Outer *Type   // outer struct
167         Note  *string // literal string annotation
168
169         // TARRAY
170         Bound int64 // negative is slice
171
172         // TMAP
173         Bucket *Type // internal type representing a hash bucket
174         Hmap   *Type // internal type representing a Hmap (map header object)
175         Hiter  *Type // internal type representing hash iterator state
176         Map    *Type // link from the above 3 internal types back to the map type.
177
178         Maplineno   int32 // first use of TFORW as map key
179         Embedlineno int32 // first use of TFORW as embedded type
180
181         // for TFORW, where to copy the eventual value to
182         Copyto []*Node
183
184         Lastfn *Node // for usefield
185 }
186
187 type Label struct {
188         Sym  *Sym
189         Def  *Node
190         Use  []*Node
191         Link *Label
192
193         // for use during gen
194         Gotopc   *obj.Prog // pointer to unresolved gotos
195         Labelpc  *obj.Prog // pointer to code
196         Breakpc  *obj.Prog // pointer to code
197         Continpc *obj.Prog // pointer to code
198
199         Used bool
200 }
201
202 type InitEntry struct {
203         Xoffset int64 // struct, array only
204         Expr    *Node // bytes of run-time computed expressions
205 }
206
207 type InitPlan struct {
208         Lit  int64
209         Zero int64
210         Expr int64
211         E    []InitEntry
212 }
213
214 const (
215         SymExport   = 1 << 0 // to be exported
216         SymPackage  = 1 << 1
217         SymExported = 1 << 2 // already written out by export
218         SymUniq     = 1 << 3
219         SymSiggen   = 1 << 4
220         SymAsm      = 1 << 5
221         SymAlgGen   = 1 << 6
222 )
223
224 var dclstack *Sym
225
226 type Iter struct {
227         Done  int
228         Tfunc *Type
229         T     *Type
230 }
231
232 type EType uint8
233
234 const (
235         Txxx = iota
236
237         TINT8
238         TUINT8
239         TINT16
240         TUINT16
241         TINT32
242         TUINT32
243         TINT64
244         TUINT64
245         TINT
246         TUINT
247         TUINTPTR
248
249         TCOMPLEX64
250         TCOMPLEX128
251
252         TFLOAT32
253         TFLOAT64
254
255         TBOOL
256
257         TPTR32
258         TPTR64
259
260         TFUNC
261         TARRAY
262         T_old_DARRAY // Doesn't seem to be used in existing code. Used now for Isddd export (see bexport.go). TODO(gri) rename.
263         TSTRUCT
264         TCHAN
265         TMAP
266         TINTER
267         TFORW
268         TFIELD
269         TANY
270         TSTRING
271         TUNSAFEPTR
272
273         // pseudo-types for literals
274         TIDEAL
275         TNIL
276         TBLANK
277
278         // pseudo-type for frame layout
279         TFUNCARGS
280         TCHANARGS
281         TINTERMETH
282
283         NTYPE
284 )
285
286 // Ctype describes the constant kind of an "ideal" (untyped) constant.
287 type Ctype int8
288
289 const (
290         CTxxx Ctype = iota
291
292         CTINT
293         CTRUNE
294         CTFLT
295         CTCPLX
296         CTSTR
297         CTBOOL
298         CTNIL
299 )
300
301 const (
302         // types of channel
303         // must match ../../../../reflect/type.go:/ChanDir
304         Crecv = 1 << 0
305         Csend = 1 << 1
306         Cboth = Crecv | Csend
307 )
308
309 // The Class of a variable/function describes the "storage class"
310 // of a variable or function. During parsing, storage classes are
311 // called declaration contexts.
312 type Class uint8
313
314 const (
315         Pxxx      Class = iota
316         PEXTERN         // global variable
317         PAUTO           // local variables
318         PPARAM          // input arguments
319         PPARAMOUT       // output results
320         PPARAMREF       // closure variable reference
321         PFUNC           // global function
322
323         PDISCARD // discard during parse of duplicate import
324
325         PHEAP = 1 << 7 // an extra bit to identify an escaped variable
326 )
327
328 const (
329         Etop      = 1 << 1 // evaluated at statement level
330         Erv       = 1 << 2 // evaluated in value context
331         Etype     = 1 << 3
332         Ecall     = 1 << 4  // call-only expressions are ok
333         Efnstruct = 1 << 5  // multivalue function returns are ok
334         Eiota     = 1 << 6  // iota is ok
335         Easgn     = 1 << 7  // assigning to expression
336         Eindir    = 1 << 8  // indirecting through expression
337         Eaddr     = 1 << 9  // taking address of expression
338         Eproc     = 1 << 10 // inside a go statement
339         Ecomplit  = 1 << 11 // type in composite literal
340 )
341
342 type Typedef struct {
343         Name   string
344         Etype  EType
345         Sameas EType
346 }
347
348 type Sig struct {
349         name   string
350         pkg    *Pkg
351         isym   *Sym
352         tsym   *Sym
353         type_  *Type
354         mtype  *Type
355         offset int32
356 }
357
358 type Dlist struct {
359         field *Type
360 }
361
362 // argument passing to/from
363 // smagic and umagic
364 type Magic struct {
365         W   int // input for both - width
366         S   int // output for both - shift
367         Bad int // output for both - unexpected failure
368
369         // magic multiplier for signed literal divisors
370         Sd int64 // input - literal divisor
371         Sm int64 // output - multiplier
372
373         // magic multiplier for unsigned literal divisors
374         Ud uint64 // input - literal divisor
375         Um uint64 // output - multiplier
376         Ua int    // output - adder
377 }
378
379 // note this is the runtime representation
380 // of the compilers arrays.
381 //
382 // typedef      struct
383 // {                                    // must not move anything
384 //      uchar   array[8];       // pointer to data
385 //      uchar   nel[4];         // number of elements
386 //      uchar   cap[4];         // allocated number of elements
387 // } Array;
388 var Array_array int // runtime offsetof(Array,array) - same for String
389
390 var Array_nel int // runtime offsetof(Array,nel) - same for String
391
392 var Array_cap int // runtime offsetof(Array,cap)
393
394 var sizeof_Array int // runtime sizeof(Array)
395
396 // note this is the runtime representation
397 // of the compilers strings.
398 //
399 // typedef      struct
400 // {                                    // must not move anything
401 //      uchar   array[8];       // pointer to data
402 //      uchar   nel[4];         // number of elements
403 // } String;
404 var sizeof_String int // runtime sizeof(String)
405
406 var dotlist [10]Dlist // size is max depth of embeddeds
407
408 // lexlineno is the line number _after_ the most recently read rune.
409 // In particular, it's advanced (or rewound) as newlines are read (or unread).
410 var lexlineno int32
411
412 // lineno is the line number at the start of the most recently lexed token.
413 var lineno int32
414
415 var pragcgobuf string
416
417 var infile string
418
419 var outfile string
420
421 var bout *obj.Biobuf
422
423 var nerrors int
424
425 var nsavederrors int
426
427 var nsyntaxerrors int
428
429 var decldepth int32
430
431 var safemode int
432
433 var nolocalimports int
434
435 var lexbuf bytes.Buffer
436 var strbuf bytes.Buffer
437 var litbuf string // LLITERAL value for use in syntax error messages
438
439 var Debug [256]int
440
441 var debugstr string
442
443 var Debug_checknil int
444 var Debug_typeassert int
445
446 var localpkg *Pkg // package being compiled
447
448 var importpkg *Pkg // package being imported
449
450 var structpkg *Pkg // package that declared struct, during import
451
452 var builtinpkg *Pkg // fake package for builtins
453
454 var gostringpkg *Pkg // fake pkg for Go strings
455
456 var itabpkg *Pkg // fake pkg for itab cache
457
458 var Runtimepkg *Pkg // package runtime
459
460 var racepkg *Pkg // package runtime/race
461
462 var msanpkg *Pkg // package runtime/msan
463
464 var typepkg *Pkg // fake package for runtime type info (headers)
465
466 var typelinkpkg *Pkg // fake package for runtime type info (data)
467
468 var weaktypepkg *Pkg // weak references to runtime type info
469
470 var unsafepkg *Pkg // package unsafe
471
472 var trackpkg *Pkg // fake package for field tracking
473
474 var Tptr EType // either TPTR32 or TPTR64
475
476 var myimportpath string
477
478 var localimport string
479
480 var asmhdr string
481
482 var Types [NTYPE]*Type
483
484 var idealstring *Type
485
486 var idealbool *Type
487
488 var bytetype *Type
489
490 var runetype *Type
491
492 var errortype *Type
493
494 var Simtype [NTYPE]EType
495
496 var (
497         Isptr     [NTYPE]bool
498         isforw    [NTYPE]bool
499         Isint     [NTYPE]bool
500         Isfloat   [NTYPE]bool
501         Iscomplex [NTYPE]bool
502         Issigned  [NTYPE]bool
503         issimple  [NTYPE]bool
504 )
505
506 var (
507         okforeq    [NTYPE]bool
508         okforadd   [NTYPE]bool
509         okforand   [NTYPE]bool
510         okfornone  [NTYPE]bool
511         okforcmp   [NTYPE]bool
512         okforbool  [NTYPE]bool
513         okforcap   [NTYPE]bool
514         okforlen   [NTYPE]bool
515         okforarith [NTYPE]bool
516         okforconst [NTYPE]bool
517 )
518
519 var (
520         okfor [OEND][]bool
521         iscmp [OEND]bool
522 )
523
524 var Minintval [NTYPE]*Mpint
525
526 var Maxintval [NTYPE]*Mpint
527
528 var minfltval [NTYPE]*Mpflt
529
530 var maxfltval [NTYPE]*Mpflt
531
532 var xtop *NodeList
533
534 var externdcl []*Node
535
536 var exportlist []*Node
537
538 var importlist []*Node // imported functions and methods with inlinable bodies
539
540 var funcsyms []*Node
541
542 var dclcontext Class // PEXTERN/PAUTO
543
544 var incannedimport int
545
546 var statuniqgen int // name generator for static temps
547
548 var iota_ int32
549
550 var lastconst *NodeList
551
552 var lasttype *Node
553
554 var Maxarg int64
555
556 var Stksize int64 // stack size for current frame
557
558 var stkptrsize int64 // prefix of stack containing pointers
559
560 var blockgen int32 // max block number
561
562 var block int32 // current block number
563
564 var hasdefer bool // flag that curfn has defer statement
565
566 var Curfn *Node
567
568 var Widthptr int
569
570 var Widthint int
571
572 var Widthreg int
573
574 var nblank *Node
575
576 var Funcdepth int32
577
578 var typecheckok bool
579
580 var compiling_runtime int
581
582 var compiling_wrappers int
583
584 var use_writebarrier int
585
586 var pure_go int
587
588 var flag_installsuffix string
589
590 var flag_race int
591
592 var flag_msan int
593
594 var flag_largemodel int
595
596 // Whether we are adding any sort of code instrumentation, such as
597 // when the race detector is enabled.
598 var instrumenting bool
599
600 var debuglive int
601
602 var Ctxt *obj.Link
603
604 var writearchive int
605
606 var bstdout obj.Biobuf
607
608 var Nacl bool
609
610 var continpc *obj.Prog
611
612 var breakpc *obj.Prog
613
614 var Pc *obj.Prog
615
616 var nodfp *Node
617
618 var Disable_checknil int
619
620 type Flow struct {
621         Prog   *obj.Prog // actual instruction
622         P1     *Flow     // predecessors of this instruction: p1,
623         P2     *Flow     // and then p2 linked though p2link.
624         P2link *Flow
625         S1     *Flow // successors of this instruction (at most two: s1 and s2).
626         S2     *Flow
627         Link   *Flow // next instruction in function code
628
629         Active int32 // usable by client
630
631         Id     int32  // sequence number in flow graph
632         Rpo    int32  // reverse post ordering
633         Loop   uint16 // x5 for every loop
634         Refset bool   // diagnostic generated
635
636         Data interface{} // for use by client
637 }
638
639 type Graph struct {
640         Start *Flow
641         Num   int
642
643         // After calling flowrpo, rpo lists the flow nodes in reverse postorder,
644         // and each non-dead Flow node f has g->rpo[f->rpo] == f.
645         Rpo []*Flow
646 }
647
648 // interface to back end
649
650 const (
651         // Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA.
652         Pseudo = 1 << 1
653
654         // There's nothing to say about the instruction,
655         // but it's still okay to see.
656         OK = 1 << 2
657
658         // Size of right-side write, or right-side read if no write.
659         SizeB = 1 << 3
660         SizeW = 1 << 4
661         SizeL = 1 << 5
662         SizeQ = 1 << 6
663         SizeF = 1 << 7
664         SizeD = 1 << 8
665
666         // Left side (Prog.from): address taken, read, write.
667         LeftAddr  = 1 << 9
668         LeftRead  = 1 << 10
669         LeftWrite = 1 << 11
670
671         // Register in middle (Prog.reg); only ever read. (arm, ppc64)
672         RegRead    = 1 << 12
673         CanRegRead = 1 << 13
674
675         // Right side (Prog.to): address taken, read, write.
676         RightAddr  = 1 << 14
677         RightRead  = 1 << 15
678         RightWrite = 1 << 16
679
680         // Instruction kinds
681         Move  = 1 << 17 // straight move
682         Conv  = 1 << 18 // size conversion
683         Cjmp  = 1 << 19 // conditional jump
684         Break = 1 << 20 // breaks control flow (no fallthrough)
685         Call  = 1 << 21 // function call
686         Jump  = 1 << 22 // jump
687         Skip  = 1 << 23 // data instruction
688
689         // Set, use, or kill of carry bit.
690         // Kill means we never look at the carry bit after this kind of instruction.
691         SetCarry  = 1 << 24
692         UseCarry  = 1 << 25
693         KillCarry = 1 << 26
694
695         // Special cases for register use. (amd64, 386)
696         ShiftCX  = 1 << 27 // possible shift by CX
697         ImulAXDX = 1 << 28 // possible multiply into DX:AX
698
699         // Instruction updates whichever of from/to is type D_OREG. (ppc64)
700         PostInc = 1 << 29
701 )
702
703 type Arch struct {
704         Thechar      int
705         Thestring    string
706         Thelinkarch  *obj.LinkArch
707         Typedefs     []Typedef
708         REGSP        int
709         REGCTXT      int
710         REGCALLX     int // BX
711         REGCALLX2    int // AX
712         REGRETURN    int // AX
713         REGMIN       int
714         REGMAX       int
715         REGZERO      int // architectural zero register, if available
716         FREGMIN      int
717         FREGMAX      int
718         MAXWIDTH     int64
719         ReservedRegs []int
720
721         AddIndex     func(*Node, int64, *Node) bool // optional
722         Betypeinit   func()
723         Bgen_float   func(*Node, bool, int, *obj.Prog) // optional
724         Cgen64       func(*Node, *Node)                // only on 32-bit systems
725         Cgenindex    func(*Node, *Node, bool) *obj.Prog
726         Cgen_bmul    func(Op, *Node, *Node, *Node) bool
727         Cgen_float   func(*Node, *Node) // optional
728         Cgen_hmul    func(*Node, *Node, *Node)
729         Cgen_shift   func(Op, bool, *Node, *Node, *Node)
730         Clearfat     func(*Node)
731         Cmp64        func(*Node, *Node, Op, int, *obj.Prog) // only on 32-bit systems
732         Defframe     func(*obj.Prog)
733         Dodiv        func(Op, *Node, *Node, *Node)
734         Excise       func(*Flow)
735         Expandchecks func(*obj.Prog)
736         Getg         func(*Node)
737         Gins         func(int, *Node, *Node) *obj.Prog
738
739         // Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied.
740         // The returned prog should be Patch'ed with the jump target.
741         // If op is not satisfied, code falls through to the next emitted instruction.
742         // Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion.
743         //
744         // Ginscmp must be able to handle all kinds of arguments for n1 and n2,
745         // not just simple registers, although it can assume that there are no
746         // function calls needed during the evaluation, and on 32-bit systems
747         // the values are guaranteed not to be 64-bit values, so no in-memory
748         // temporaries are necessary.
749         Ginscmp func(op Op, t *Type, n1, n2 *Node, likely int) *obj.Prog
750
751         // Ginsboolval inserts instructions to convert the result
752         // of a just-completed comparison to a boolean value.
753         // The first argument is the conditional jump instruction
754         // corresponding to the desired value.
755         // The second argument is the destination.
756         // If not present, Ginsboolval will be emulated with jumps.
757         Ginsboolval func(int, *Node)
758
759         Ginscon      func(int, int64, *Node)
760         Ginsnop      func()
761         Gmove        func(*Node, *Node)
762         Igenindex    func(*Node, *Node, bool) *obj.Prog
763         Linkarchinit func()
764         Peep         func(*obj.Prog)
765         Proginfo     func(*obj.Prog) // fills in Prog.Info
766         Regtyp       func(*obj.Addr) bool
767         Sameaddr     func(*obj.Addr, *obj.Addr) bool
768         Smallindir   func(*obj.Addr, *obj.Addr) bool
769         Stackaddr    func(*obj.Addr) bool
770         Blockcopy    func(*Node, *Node, int64, int64, int64)
771         Sudoaddable  func(int, *Node, *obj.Addr) bool
772         Sudoclean    func()
773         Excludedregs func() uint64
774         RtoB         func(int) uint64
775         FtoB         func(int) uint64
776         BtoR         func(uint64) int
777         BtoF         func(uint64) int
778         Optoas       func(Op, *Type) int
779         Doregbits    func(int) uint64
780         Regnames     func(*int) []string
781         Use387       bool // should 8g use 387 FP instructions instead of sse2.
782 }
783
784 var pcloc int32
785
786 var Thearch Arch
787
788 var Newproc *Node
789
790 var Deferproc *Node
791
792 var Deferreturn *Node
793
794 var Panicindex *Node
795
796 var panicslice *Node
797
798 var panicdivide *Node
799
800 var throwreturn *Node
801
802 var growslice *Node
803
804 var writebarrierptr *Node
805 var typedmemmove *Node
806
807 var panicdottype *Node