-
-
Notifications
You must be signed in to change notification settings - Fork 360
/
ssa.go
1895 lines (1710 loc) · 58.2 KB
/
ssa.go
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 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.
package ir
// This package defines a high-level intermediate representation for
// Go programs using static single-information (SSI) form.
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"sync"
"golang.org/x/tools/go/types/typeutil"
)
type ID int
// A Program is a partial or complete Go program converted to IR form.
type Program struct {
Fset *token.FileSet // position information for the files of this Program
PrintFunc string // create ir.html for function specified in PrintFunc
imported map[string]*Package // all importable Packages, keyed by import path
packages map[*types.Package]*Package // all loaded Packages, keyed by object
mode BuilderMode // set of mode bits for IR construction
MethodSets typeutil.MethodSetCache // cache of type-checker's method-sets
methodsMu sync.Mutex // guards the following maps:
methodSets typeutil.Map // maps type to its concrete methodSet
runtimeTypes typeutil.Map // types for which rtypes are needed
canon typeutil.Map // type canonicalization map
bounds map[*types.Func]*Function // bounds for curried x.Method closures
thunks map[selectionKey]*Function // thunks for T.Method expressions
}
// A Package is a single analyzed Go package containing Members for
// all package-level functions, variables, constants and types it
// declares. These may be accessed directly via Members, or via the
// type-specific accessor methods Func, Type, Var and Const.
//
// Members also contains entries for "init" (the synthetic package
// initializer) and "init#%d", the nth declared init function,
// and unspecified other things too.
//
type Package struct {
Prog *Program // the owning program
Pkg *types.Package // the corresponding go/types.Package
Members map[string]Member // all package members keyed by name (incl. init and init#%d)
Functions []*Function // all functions, excluding anonymous ones
values map[types.Object]Value // package members (incl. types and methods), keyed by object
init *Function // Func("init"); the package's init function
debug bool // include full debug info in this package
printFunc string // which function to print in HTML form
// The following fields are set transiently, then cleared
// after building.
buildOnce sync.Once // ensures package building occurs once
ninit int32 // number of init functions
info *types.Info // package type information
files []*ast.File // package ASTs
}
// A Member is a member of a Go package, implemented by *NamedConst,
// *Global, *Function, or *Type; they are created by package-level
// const, var, func and type declarations respectively.
//
type Member interface {
Name() string // declared name of the package member
String() string // package-qualified name of the package member
RelString(*types.Package) string // like String, but relative refs are unqualified
Object() types.Object // typechecker's object for this member, if any
Type() types.Type // type of the package member
Token() token.Token // token.{VAR,FUNC,CONST,TYPE}
Package() *Package // the containing package
}
// A Type is a Member of a Package representing a package-level named type.
type Type struct {
object *types.TypeName
pkg *Package
}
// A NamedConst is a Member of a Package representing a package-level
// named constant.
//
// Pos() returns the position of the declaring ast.ValueSpec.Names[*]
// identifier.
//
// NB: a NamedConst is not a Value; it contains a constant Value, which
// it augments with the name and position of its 'const' declaration.
//
type NamedConst struct {
object *types.Const
Value *Const
pkg *Package
}
// A Value is an IR value that can be referenced by an instruction.
type Value interface {
setID(ID)
// Name returns the name of this value, and determines how
// this Value appears when used as an operand of an
// Instruction.
//
// This is the same as the source name for Parameters,
// Builtins, Functions, FreeVars, Globals.
// For constants, it is a representation of the constant's value
// and type. For all other Values this is the name of the
// virtual register defined by the instruction.
//
// The name of an IR Value is not semantically significant,
// and may not even be unique within a function.
Name() string
// ID returns the ID of this value. IDs are unique within a single
// function and are densely numbered, but may contain gaps.
// Values and other Instructions share the same ID space.
// Globally, values are identified by their addresses. However,
// IDs exist to facilitate efficient storage of mappings between
// values and data when analysing functions.
//
// NB: IDs are allocated late in the IR construction process and
// are not available to early stages of said process.
ID() ID
// If this value is an Instruction, String returns its
// disassembled form; otherwise it returns unspecified
// human-readable information about the Value, such as its
// kind, name and type.
String() string
// Type returns the type of this value. Many instructions
// (e.g. IndexAddr) change their behaviour depending on the
// types of their operands.
Type() types.Type
// Parent returns the function to which this Value belongs.
// It returns nil for named Functions, Builtin and Global.
Parent() *Function
// Referrers returns the list of instructions that have this
// value as one of their operands; it may contain duplicates
// if an instruction has a repeated operand.
//
// Referrers actually returns a pointer through which the
// caller may perform mutations to the object's state.
//
// Referrers is currently only defined if Parent()!=nil,
// i.e. for the function-local values FreeVar, Parameter,
// Functions (iff anonymous) and all value-defining instructions.
// It returns nil for named Functions, Builtin and Global.
//
// Instruction.Operands contains the inverse of this relation.
Referrers() *[]Instruction
Operands(rands []*Value) []*Value // nil for non-Instructions
// Source returns the AST node responsible for creating this
// value. A single AST node may be responsible for more than one
// value, and not all values have an associated AST node.
//
// Do not use this method to find a Value given an ast.Expr; use
// ValueForExpr instead.
Source() ast.Node
// Pos returns Source().Pos() if Source is not nil, else it
// returns token.NoPos.
Pos() token.Pos
}
// An Instruction is an IR instruction that computes a new Value or
// has some effect.
//
// An Instruction that defines a value (e.g. BinOp) also implements
// the Value interface; an Instruction that only has an effect (e.g. Store)
// does not.
//
type Instruction interface {
setSource(ast.Node)
setID(ID)
// String returns the disassembled form of this value.
//
// Examples of Instructions that are Values:
// "BinOp <int> {+} t1 t2" (BinOp)
// "Call <int> len t1" (Call)
// Note that the name of the Value is not printed.
//
// Examples of Instructions that are not Values:
// "Return t1" (Return)
// "Store {int} t2 t1" (Store)
//
// (The separation of Value.Name() from Value.String() is useful
// for some analyses which distinguish the operation from the
// value it defines, e.g., 'y = local int' is both an allocation
// of memory 'local int' and a definition of a pointer y.)
String() string
// ID returns the ID of this instruction. IDs are unique within a single
// function and are densely numbered, but may contain gaps.
// Globally, instructions are identified by their addresses. However,
// IDs exist to facilitate efficient storage of mappings between
// instructions and data when analysing functions.
//
// NB: IDs are allocated late in the IR construction process and
// are not available to early stages of said process.
ID() ID
// Parent returns the function to which this instruction
// belongs.
Parent() *Function
// Block returns the basic block to which this instruction
// belongs.
Block() *BasicBlock
// setBlock sets the basic block to which this instruction belongs.
setBlock(*BasicBlock)
// Operands returns the operands of this instruction: the
// set of Values it references.
//
// Specifically, it appends their addresses to rands, a
// user-provided slice, and returns the resulting slice,
// permitting avoidance of memory allocation.
//
// The operands are appended in undefined order, but the order
// is consistent for a given Instruction; the addresses are
// always non-nil but may point to a nil Value. Clients may
// store through the pointers, e.g. to effect a value
// renaming.
//
// Value.Referrers is a subset of the inverse of this
// relation. (Referrers are not tracked for all types of
// Values.)
Operands(rands []*Value) []*Value
Referrers() *[]Instruction // nil for non-Values
// Source returns the AST node responsible for creating this
// instruction. A single AST node may be responsible for more than
// one instruction, and not all instructions have an associated
// AST node.
Source() ast.Node
// Pos returns Source().Pos() if Source is not nil, else it
// returns token.NoPos.
Pos() token.Pos
}
// A Node is a node in the IR value graph. Every concrete type that
// implements Node is also either a Value, an Instruction, or both.
//
// Node contains the methods common to Value and Instruction, plus the
// Operands and Referrers methods generalized to return nil for
// non-Instructions and non-Values, respectively.
//
// Node is provided to simplify IR graph algorithms. Clients should
// use the more specific and informative Value or Instruction
// interfaces where appropriate.
//
type Node interface {
setID(ID)
// Common methods:
ID() ID
String() string
Source() ast.Node
Pos() token.Pos
Parent() *Function
// Partial methods:
Operands(rands []*Value) []*Value // nil for non-Instructions
Referrers() *[]Instruction // nil for non-Values
}
type Synthetic int
const (
SyntheticLoadedFromExportData Synthetic = iota + 1
SyntheticPackageInitializer
SyntheticThunk
SyntheticWrapper
SyntheticBound
)
func (syn Synthetic) String() string {
switch syn {
case SyntheticLoadedFromExportData:
return "loaded from export data"
case SyntheticPackageInitializer:
return "package initializer"
case SyntheticThunk:
return "thunk"
case SyntheticWrapper:
return "wrapper"
case SyntheticBound:
return "bound"
default:
return fmt.Sprintf("Synthetic(%d)", syn)
}
}
// Function represents the parameters, results, and code of a function
// or method.
//
// If Blocks is nil, this indicates an external function for which no
// Go source code is available. In this case, FreeVars and Locals
// are nil too. Clients performing whole-program analysis must
// handle external functions specially.
//
// Blocks contains the function's control-flow graph (CFG).
// Blocks[0] is the function entry point; block order is not otherwise
// semantically significant, though it may affect the readability of
// the disassembly.
// To iterate over the blocks in dominance order, use DomPreorder().
//
// A nested function (Parent()!=nil) that refers to one or more
// lexically enclosing local variables ("free variables") has FreeVars.
// Such functions cannot be called directly but require a
// value created by MakeClosure which, via its Bindings, supplies
// values for these parameters.
//
// If the function is a method (Signature.Recv() != nil) then the first
// element of Params is the receiver parameter.
//
// A Go package may declare many functions called "init".
// For each one, Object().Name() returns "init" but Name() returns
// "init#1", etc, in declaration order.
//
// Pos() returns the declaring ast.FuncLit.Type.Func or the position
// of the ast.FuncDecl.Name, if the function was explicit in the
// source. Synthetic wrappers, for which Synthetic != "", may share
// the same position as the function they wrap.
// Syntax.Pos() always returns the position of the declaring "func" token.
//
// Type() returns the function's Signature.
//
type Function struct {
node
name string
object types.Object // a declared *types.Func or one of its wrappers
method *types.Selection // info about provenance of synthetic methods
Signature *types.Signature
Synthetic Synthetic
parent *Function // enclosing function if anon; nil if global
Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error)
Prog *Program // enclosing program
Params []*Parameter // function parameters; for methods, includes receiver
FreeVars []*FreeVar // free variables whose values must be supplied by closure
Locals []*Alloc // local variables of this function
Blocks []*BasicBlock // basic blocks of the function; nil => external
Exit *BasicBlock // The function's exit block
AnonFuncs []*Function // anonymous functions directly beneath this one
referrers []Instruction // referring instructions (iff Parent() != nil)
NoReturn NoReturn // Calling this function will always terminate control flow.
*functionBody
}
type NoReturn uint8
const (
Returns NoReturn = iota
AlwaysExits
AlwaysUnwinds
NeverReturns
)
type functionBody struct {
// The following fields are set transiently during building,
// then cleared.
currentBlock *BasicBlock // where to emit code
objects map[types.Object]Value // addresses of local variables
namedResults []*Alloc // tuple of named results
implicitResults []*Alloc // tuple of results
targets *targets // linked stack of branch targets
lblocks map[*ast.Object]*lblock // labelled blocks
consts []*Const
wr *HTMLWriter
fakeExits BlockSet
blocksets [5]BlockSet
hasDefer bool
}
func (fn *Function) results() []*Alloc {
if len(fn.namedResults) > 0 {
return fn.namedResults
}
return fn.implicitResults
}
// BasicBlock represents an IR basic block.
//
// The final element of Instrs is always an explicit transfer of
// control (If, Jump, Return, Panic, or Unreachable).
//
// A block may contain no Instructions only if it is unreachable,
// i.e., Preds is nil. Empty blocks are typically pruned.
//
// BasicBlocks and their Preds/Succs relation form a (possibly cyclic)
// graph independent of the IR Value graph: the control-flow graph or
// CFG. It is illegal for multiple edges to exist between the same
// pair of blocks.
//
// Each BasicBlock is also a node in the dominator tree of the CFG.
// The tree may be navigated using Idom()/Dominees() and queried using
// Dominates().
//
// The order of Preds and Succs is significant (to Phi and If
// instructions, respectively).
//
type BasicBlock struct {
Index int // index of this block within Parent().Blocks
Comment string // optional label; no semantic significance
parent *Function // parent function
Instrs []Instruction // instructions in order
Preds, Succs []*BasicBlock // predecessors and successors
succs2 [2]*BasicBlock // initial space for Succs
dom domInfo // dominator tree info
pdom domInfo // post-dominator tree info
post int
gaps int // number of nil Instrs (transient)
rundefers int // number of rundefers (transient)
}
// Pure values ----------------------------------------
// A FreeVar represents a free variable of the function to which it
// belongs.
//
// FreeVars are used to implement anonymous functions, whose free
// variables are lexically captured in a closure formed by
// MakeClosure. The value of such a free var is an Alloc or another
// FreeVar and is considered a potentially escaping heap address, with
// pointer type.
//
// FreeVars are also used to implement bound method closures. Such a
// free var represents the receiver value and may be of any type that
// has concrete methods.
//
// Pos() returns the position of the value that was captured, which
// belongs to an enclosing function.
//
type FreeVar struct {
node
name string
typ types.Type
parent *Function
referrers []Instruction
// Transiently needed during building.
outer Value // the Value captured from the enclosing context.
}
// A Parameter represents an input parameter of a function.
//
type Parameter struct {
register
name string
object types.Object // a *types.Var; nil for non-source locals
}
// A Const represents the value of a constant expression.
//
// The underlying type of a constant may be any boolean, numeric, or
// string type. In addition, a Const may represent the nil value of
// any reference type---interface, map, channel, pointer, slice, or
// function---but not "untyped nil".
//
// All source-level constant expressions are represented by a Const
// of the same type and value.
//
// Value holds the exact value of the constant, independent of its
// Type(), using the same representation as package go/constant uses for
// constants, or nil for a typed nil value.
//
// Pos() returns token.NoPos.
//
// Example printed form:
// Const <int> {42}
// Const <untyped string> {"test"}
// Const <MyComplex> {(3 + 4i)}
//
type Const struct {
register
Value constant.Value
}
// A Global is a named Value holding the address of a package-level
// variable.
//
// Pos() returns the position of the ast.ValueSpec.Names[*]
// identifier.
//
type Global struct {
node
name string
object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard
typ types.Type
Pkg *Package
}
// A Builtin represents a specific use of a built-in function, e.g. len.
//
// Builtins are immutable values. Builtins do not have addresses.
// Builtins can only appear in CallCommon.Func.
//
// Name() indicates the function: one of the built-in functions from the
// Go spec (excluding "make" and "new") or one of these ir-defined
// intrinsics:
//
// // wrapnilchk returns ptr if non-nil, panics otherwise.
// // (For use in indirection wrappers.)
// func ir:wrapnilchk(ptr *T, recvType, methodName string) *T
//
// // noreturnWasPanic returns true if the previously called
// // function panicked, false if it exited the process.
// func ir:noreturnWasPanic() bool
//
// Object() returns a *types.Builtin for built-ins defined by the spec,
// nil for others.
//
// Type() returns a *types.Signature representing the effective
// signature of the built-in for this call.
//
type Builtin struct {
node
name string
sig *types.Signature
}
// Value-defining instructions ----------------------------------------
// The Alloc instruction reserves space for a variable of the given type,
// zero-initializes it, and yields its address.
//
// Alloc values are always addresses, and have pointer types, so the
// type of the allocated variable is actually
// Type().Underlying().(*types.Pointer).Elem().
//
// If Heap is false, Alloc allocates space in the function's
// activation record (frame); we refer to an Alloc(Heap=false) as a
// "stack" alloc. Each stack Alloc returns the same address each time
// it is executed within the same activation; the space is
// re-initialized to zero.
//
// If Heap is true, Alloc allocates space in the heap; we
// refer to an Alloc(Heap=true) as a "heap" alloc. Each heap Alloc
// returns a different address each time it is executed.
//
// When Alloc is applied to a channel, map or slice type, it returns
// the address of an uninitialized (nil) reference of that kind; store
// the result of MakeSlice, MakeMap or MakeChan in that location to
// instantiate these types.
//
// Pos() returns the ast.CompositeLit.Lbrace for a composite literal,
// or the ast.CallExpr.Rparen for a call to new() or for a call that
// allocates a varargs slice.
//
// Example printed form:
// t1 = StackAlloc <*int>
// t2 = HeapAlloc <*int> (new)
//
type Alloc struct {
register
Heap bool
index int // dense numbering; for lifting
}
var _ Instruction = (*Sigma)(nil)
var _ Value = (*Sigma)(nil)
// The Sigma instruction represents an SSI σ-node, which splits values
// at branches in the control flow.
//
// Conceptually, σ-nodes exist at the end of blocks that branch and
// constitute parallel assignments to one value per destination block.
// However, such a representation would be awkward to work with, so
// instead we place σ-nodes at the beginning of branch targets. The
// From field denotes to which incoming edge the node applies.
//
// Within a block, all σ-nodes must appear before all non-σ nodes.
//
// Example printed form:
// t2 = Sigma <int> [#0] t1 (x)
//
type Sigma struct {
register
From *BasicBlock
X Value
live bool // used during lifting
}
// The Phi instruction represents an SSA φ-node, which combines values
// that differ across incoming control-flow edges and yields a new
// value. Within a block, all φ-nodes must appear before all non-φ, non-σ
// nodes.
//
// Pos() returns the position of the && or || for short-circuit
// control-flow joins, or that of the *Alloc for φ-nodes inserted
// during SSA renaming.
//
// Example printed form:
// t3 = Phi <int> 2:t1 4:t2 (x)
//
type Phi struct {
register
Edges []Value // Edges[i] is value for Block().Preds[i]
live bool // used during lifting
}
// The Call instruction represents a function or method call.
//
// The Call instruction yields the function result if there is exactly
// one. Otherwise it returns a tuple, the components of which are
// accessed via Extract.
//
// See CallCommon for generic function call documentation.
//
// Pos() returns the ast.CallExpr.Lparen, if explicit in the source.
//
// Example printed form:
// t3 = Call <()> println t1 t2
// t4 = Call <()> foo$1
// t6 = Invoke <string> t5.String
//
type Call struct {
register
Call CallCommon
}
// The BinOp instruction yields the result of binary operation X Op Y.
//
// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source.
//
// Example printed form:
// t3 = BinOp <int> {+} t2 t1
//
type BinOp struct {
register
// One of:
// ADD SUB MUL QUO REM + - * / %
// AND OR XOR SHL SHR AND_NOT & | ^ << >> &^
// EQL NEQ LSS LEQ GTR GEQ == != < <= < >=
Op token.Token
X, Y Value
}
// The UnOp instruction yields the result of Op X.
// XOR is bitwise complement.
// SUB is negation.
// NOT is logical negation.
//
//
// Example printed form:
// t2 = UnOp <int> {^} t1
//
type UnOp struct {
register
Op token.Token // One of: NOT SUB XOR ! - ^
X Value
}
// The Load instruction loads a value from a memory address.
//
// For implicit memory loads, Pos() returns the position of the
// most closely associated source-level construct; the details are not
// specified.
//
// Example printed form:
// t2 = Load <int> t1
//
type Load struct {
register
X Value
}
// The ChangeType instruction applies to X a value-preserving type
// change to Type().
//
// Type changes are permitted:
// - between a named type and its underlying type.
// - between two named types of the same underlying type.
// - between (possibly named) pointers to identical base types.
// - from a bidirectional channel to a read- or write-channel,
// optionally adding/removing a name.
//
// This operation cannot fail dynamically.
//
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
// from an explicit conversion in the source.
//
// Example printed form:
// t2 = ChangeType <*T> t1
//
type ChangeType struct {
register
X Value
}
// The Convert instruction yields the conversion of value X to type
// Type(). One or both of those types is basic (but possibly named).
//
// A conversion may change the value and representation of its operand.
// Conversions are permitted:
// - between real numeric types.
// - between complex numeric types.
// - between string and []byte or []rune.
// - between pointers and unsafe.Pointer.
// - between unsafe.Pointer and uintptr.
// - from (Unicode) integer to (UTF-8) string.
// A conversion may imply a type name change also.
//
// This operation cannot fail dynamically.
//
// Conversions of untyped string/number/bool constants to a specific
// representation are eliminated during IR construction.
//
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
// from an explicit conversion in the source.
//
// Example printed form:
// t2 = Convert <[]byte> t1
//
type Convert struct {
register
X Value
}
// ChangeInterface constructs a value of one interface type from a
// value of another interface type known to be assignable to it.
// This operation cannot fail.
//
// Pos() returns the ast.CallExpr.Lparen if the instruction arose from
// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the
// instruction arose from an explicit e.(T) operation; or token.NoPos
// otherwise.
//
// Example printed form:
// t2 = ChangeInterface <I1> t1
//
type ChangeInterface struct {
register
X Value
}
// MakeInterface constructs an instance of an interface type from a
// value of a concrete type.
//
// Use Program.MethodSets.MethodSet(X.Type()) to find the method-set
// of X, and Program.MethodValue(m) to find the implementation of a method.
//
// To construct the zero value of an interface type T, use:
// NewConst(constant.MakeNil(), T, pos)
//
// Pos() returns the ast.CallExpr.Lparen, if the instruction arose
// from an explicit conversion in the source.
//
// Example printed form:
// t2 = MakeInterface <interface{}> t1
//
type MakeInterface struct {
register
X Value
}
// The MakeClosure instruction yields a closure value whose code is
// Fn and whose free variables' values are supplied by Bindings.
//
// Type() returns a (possibly named) *types.Signature.
//
// Pos() returns the ast.FuncLit.Type.Func for a function literal
// closure or the ast.SelectorExpr.Sel for a bound method closure.
//
// Example printed form:
// t1 = MakeClosure <func()> foo$1 t1 t2
// t5 = MakeClosure <func(int)> (T).foo$bound t4
//
type MakeClosure struct {
register
Fn Value // always a *Function
Bindings []Value // values for each free variable in Fn.FreeVars
}
// The MakeMap instruction creates a new hash-table-based map object
// and yields a value of kind map.
//
// Type() returns a (possibly named) *types.Map.
//
// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or
// the ast.CompositeLit.Lbrack if created by a literal.
//
// Example printed form:
// t1 = MakeMap <map[string]int>
// t2 = MakeMap <StringIntMap> t1
//
type MakeMap struct {
register
Reserve Value // initial space reservation; nil => default
}
// The MakeChan instruction creates a new channel object and yields a
// value of kind chan.
//
// Type() returns a (possibly named) *types.Chan.
//
// Pos() returns the ast.CallExpr.Lparen for the make(chan) that
// created it.
//
// Example printed form:
// t3 = MakeChan <chan int> t1
// t4 = MakeChan <chan IntChan> t2
//
type MakeChan struct {
register
Size Value // int; size of buffer; zero => synchronous.
}
// The MakeSlice instruction yields a slice of length Len backed by a
// newly allocated array of length Cap.
//
// Both Len and Cap must be non-nil Values of integer type.
//
// (Alloc(types.Array) followed by Slice will not suffice because
// Alloc can only create arrays of constant length.)
//
// Type() returns a (possibly named) *types.Slice.
//
// Pos() returns the ast.CallExpr.Lparen for the make([]T) that
// created it.
//
// Example printed form:
// t3 = MakeSlice <[]string> t1 t2
// t4 = MakeSlice <StringSlice> t1 t2
//
type MakeSlice struct {
register
Len Value
Cap Value
}
// The Slice instruction yields a slice of an existing string, slice
// or *array X between optional integer bounds Low and High.
//
// Dynamically, this instruction panics if X evaluates to a nil *array
// pointer.
//
// Type() returns string if the type of X was string, otherwise a
// *types.Slice with the same element type as X.
//
// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice
// operation, the ast.CompositeLit.Lbrace if created by a literal, or
// NoPos if not explicit in the source (e.g. a variadic argument slice).
//
// Example printed form:
// t4 = Slice <[]int> t3 t2 t1 <nil>
//
type Slice struct {
register
X Value // slice, string, or *array
Low, High, Max Value // each may be nil
}
// The FieldAddr instruction yields the address of Field of *struct X.
//
// The field is identified by its index within the field list of the
// struct type of X.
//
// Dynamically, this instruction panics if X evaluates to a nil
// pointer.
//
// Type() returns a (possibly named) *types.Pointer.
//
// Pos() returns the position of the ast.SelectorExpr.Sel for the
// field, if explicit in the source.
//
// Example printed form:
// t2 = FieldAddr <*int> [0] (X) t1
//
type FieldAddr struct {
register
X Value // *struct
Field int // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field)
}
// The Field instruction yields the Field of struct X.
//
// The field is identified by its index within the field list of the
// struct type of X; by using numeric indices we avoid ambiguity of
// package-local identifiers and permit compact representations.
//
// Pos() returns the position of the ast.SelectorExpr.Sel for the
// field, if explicit in the source.
//
// Example printed form:
// t2 = FieldAddr <int> [0] (X) t1
//
type Field struct {
register
X Value // struct
Field int // index into X.Type().(*types.Struct).Fields
}
// The IndexAddr instruction yields the address of the element at
// index Index of collection X. Index is an integer expression.
//
// The elements of maps and strings are not addressable; use StringLookup, MapLookup or
// MapUpdate instead.
//
// Dynamically, this instruction panics if X evaluates to a nil *array
// pointer.
//
// Type() returns a (possibly named) *types.Pointer.
//
// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
// explicit in the source.
//
// Example printed form:
// t3 = IndexAddr <*int> t2 t1
//
type IndexAddr struct {
register
X Value // slice or *array,
Index Value // numeric index
}
// The Index instruction yields element Index of array X.
//
// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if
// explicit in the source.
//
// Example printed form:
// t3 = Index <int> t2 t1
//
type Index struct {
register
X Value // array
Index Value // integer index
}
// The MapLookup instruction yields element Index of collection X, a map.
//
// If CommaOk, the result is a 2-tuple of the value above and a
// boolean indicating the result of a map membership test for the key.
// The components of the tuple are accessed using Extract.
//
// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
//
// Example printed form:
// t4 = MapLookup <string> t3 t1
// t6 = MapLookup <(string, bool)> t3 t2
//
type MapLookup struct {
register
X Value // map
Index Value // key-typed index
CommaOk bool // return a value,ok pair
}
// The StringLookup instruction yields element Index of collection X, a string.
// Index is an integer expression.
//
// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source.
//
// Example printed form:
// t3 = StringLookup <uint8> t2 t1
//
type StringLookup struct {
register
X Value // string
Index Value // numeric index
}
// SelectState is a helper for Select.
// It represents one goal state and its corresponding communication.
//
type SelectState struct {
Dir types.ChanDir // direction of case (SendOnly or RecvOnly)
Chan Value // channel to use (for send or receive)
Send Value // value to send (for send)
Pos token.Pos // position of token.ARROW
DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode]
}