]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/ir/stmt.go
81d139cf127f54cf65e2f10d4e55dadb527acab3
[gostls13.git] / src / cmd / compile / internal / ir / stmt.go
1 // Copyright 2020 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 ir
6
7 import (
8         "cmd/compile/internal/base"
9         "cmd/compile/internal/types"
10         "cmd/internal/obj"
11         "cmd/internal/src"
12         "go/constant"
13 )
14
15 // A Decl is a declaration of a const, type, or var. (A declared func is a Func.)
16 type Decl struct {
17         miniNode
18         X *Name // the thing being declared
19 }
20
21 func NewDecl(pos src.XPos, op Op, x *Name) *Decl {
22         n := &Decl{X: x}
23         n.pos = pos
24         switch op {
25         default:
26                 panic("invalid Decl op " + op.String())
27         case ODCL:
28                 n.op = op
29         }
30         return n
31 }
32
33 func (*Decl) isStmt() {}
34
35 // A Stmt is a Node that can appear as a statement.
36 // This includes statement-like expressions such as f().
37 //
38 // (It's possible it should include <-c, but that would require
39 // splitting ORECV out of UnaryExpr, which hasn't yet been
40 // necessary. Maybe instead we will introduce ExprStmt at
41 // some point.)
42 type Stmt interface {
43         Node
44         isStmt()
45 }
46
47 // A miniStmt is a miniNode with extra fields common to statements.
48 type miniStmt struct {
49         miniNode
50         init Nodes
51 }
52
53 func (*miniStmt) isStmt() {}
54
55 func (n *miniStmt) Init() Nodes     { return n.init }
56 func (n *miniStmt) SetInit(x Nodes) { n.init = x }
57 func (n *miniStmt) PtrInit() *Nodes { return &n.init }
58
59 // An AssignListStmt is an assignment statement with
60 // more than one item on at least one side: Lhs = Rhs.
61 // If Def is true, the assignment is a :=.
62 type AssignListStmt struct {
63         miniStmt
64         Lhs Nodes
65         Def bool
66         Rhs Nodes
67 }
68
69 func NewAssignListStmt(pos src.XPos, op Op, lhs, rhs []Node) *AssignListStmt {
70         n := &AssignListStmt{}
71         n.pos = pos
72         n.SetOp(op)
73         n.Lhs = lhs
74         n.Rhs = rhs
75         return n
76 }
77
78 func (n *AssignListStmt) SetOp(op Op) {
79         switch op {
80         default:
81                 panic(n.no("SetOp " + op.String()))
82         case OAS2, OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV, OSELRECV2:
83                 n.op = op
84         }
85 }
86
87 // An AssignStmt is a simple assignment statement: X = Y.
88 // If Def is true, the assignment is a :=.
89 type AssignStmt struct {
90         miniStmt
91         X   Node
92         Def bool
93         Y   Node
94 }
95
96 func NewAssignStmt(pos src.XPos, x, y Node) *AssignStmt {
97         n := &AssignStmt{X: x, Y: y}
98         n.pos = pos
99         n.op = OAS
100         return n
101 }
102
103 func (n *AssignStmt) SetOp(op Op) {
104         switch op {
105         default:
106                 panic(n.no("SetOp " + op.String()))
107         case OAS:
108                 n.op = op
109         }
110 }
111
112 // An AssignOpStmt is an AsOp= assignment statement: X AsOp= Y.
113 type AssignOpStmt struct {
114         miniStmt
115         X      Node
116         AsOp   Op // OADD etc
117         Y      Node
118         IncDec bool // actually ++ or --
119 }
120
121 func NewAssignOpStmt(pos src.XPos, asOp Op, x, y Node) *AssignOpStmt {
122         n := &AssignOpStmt{AsOp: asOp, X: x, Y: y}
123         n.pos = pos
124         n.op = OASOP
125         return n
126 }
127
128 // A BlockStmt is a block: { List }.
129 type BlockStmt struct {
130         miniStmt
131         List Nodes
132 }
133
134 func NewBlockStmt(pos src.XPos, list []Node) *BlockStmt {
135         n := &BlockStmt{}
136         n.pos = pos
137         if !pos.IsKnown() {
138                 n.pos = base.Pos
139                 if len(list) > 0 {
140                         n.pos = list[0].Pos()
141                 }
142         }
143         n.op = OBLOCK
144         n.List = list
145         return n
146 }
147
148 // A BranchStmt is a break, continue, fallthrough, or goto statement.
149 type BranchStmt struct {
150         miniStmt
151         Label *types.Sym // label if present
152 }
153
154 func NewBranchStmt(pos src.XPos, op Op, label *types.Sym) *BranchStmt {
155         switch op {
156         case OBREAK, OCONTINUE, OFALL, OGOTO:
157                 // ok
158         default:
159                 panic("NewBranch " + op.String())
160         }
161         n := &BranchStmt{Label: label}
162         n.pos = pos
163         n.op = op
164         return n
165 }
166
167 func (n *BranchStmt) SetOp(op Op) {
168         switch op {
169         default:
170                 panic(n.no("SetOp " + op.String()))
171         case OBREAK, OCONTINUE, OFALL, OGOTO:
172                 n.op = op
173         }
174 }
175
176 func (n *BranchStmt) Sym() *types.Sym { return n.Label }
177
178 // A CaseClause is a case statement in a switch or select: case List: Body.
179 type CaseClause struct {
180         miniStmt
181         Var  *Name // declared variable for this case in type switch
182         List Nodes // list of expressions for switch, early select
183
184         // RTypes is a list of RType expressions, which are copied to the
185         // corresponding OEQ nodes that are emitted when switch statements
186         // are desugared. RTypes[i] must be non-nil if the emitted
187         // comparison for List[i] will be a mixed interface/concrete
188         // comparison; see reflectdata.CompareRType for details.
189         //
190         // Because mixed interface/concrete switch cases are rare, we allow
191         // len(RTypes) < len(List). Missing entries are implicitly nil.
192         RTypes Nodes
193
194         Body Nodes
195 }
196
197 func NewCaseStmt(pos src.XPos, list, body []Node) *CaseClause {
198         n := &CaseClause{List: list, Body: body}
199         n.pos = pos
200         n.op = OCASE
201         return n
202 }
203
204 type CommClause struct {
205         miniStmt
206         Comm Node // communication case
207         Body Nodes
208 }
209
210 func NewCommStmt(pos src.XPos, comm Node, body []Node) *CommClause {
211         n := &CommClause{Comm: comm, Body: body}
212         n.pos = pos
213         n.op = OCASE
214         return n
215 }
216
217 // A ForStmt is a non-range for loop: for Init; Cond; Post { Body }
218 type ForStmt struct {
219         miniStmt
220         Label        *types.Sym
221         Cond         Node
222         Post         Node
223         Body         Nodes
224         DistinctVars bool
225 }
226
227 func NewForStmt(pos src.XPos, init Node, cond, post Node, body []Node, distinctVars bool) *ForStmt {
228         n := &ForStmt{Cond: cond, Post: post}
229         n.pos = pos
230         n.op = OFOR
231         if init != nil {
232                 n.init = []Node{init}
233         }
234         n.Body = body
235         n.DistinctVars = distinctVars
236         return n
237 }
238
239 // A GoDeferStmt is a go or defer statement: go Call / defer Call.
240 //
241 // The two opcodes use a single syntax because the implementations
242 // are very similar: both are concerned with saving Call and running it
243 // in a different context (a separate goroutine or a later time).
244 type GoDeferStmt struct {
245         miniStmt
246         Call    Node
247         DeferAt Expr
248 }
249
250 func NewGoDeferStmt(pos src.XPos, op Op, call Node) *GoDeferStmt {
251         n := &GoDeferStmt{Call: call}
252         n.pos = pos
253         switch op {
254         case ODEFER, OGO:
255                 n.op = op
256         default:
257                 panic("NewGoDeferStmt " + op.String())
258         }
259         return n
260 }
261
262 // An IfStmt is a return statement: if Init; Cond { Body } else { Else }.
263 type IfStmt struct {
264         miniStmt
265         Cond   Node
266         Body   Nodes
267         Else   Nodes
268         Likely bool // code layout hint
269 }
270
271 func NewIfStmt(pos src.XPos, cond Node, body, els []Node) *IfStmt {
272         n := &IfStmt{Cond: cond}
273         n.pos = pos
274         n.op = OIF
275         n.Body = body
276         n.Else = els
277         return n
278 }
279
280 // A JumpTableStmt is used to implement switches. Its semantics are:
281 //
282 //      tmp := jt.Idx
283 //      if tmp == Cases[0] goto Targets[0]
284 //      if tmp == Cases[1] goto Targets[1]
285 //      ...
286 //      if tmp == Cases[n] goto Targets[n]
287 //
288 // Note that a JumpTableStmt is more like a multiway-goto than
289 // a multiway-if. In particular, the case bodies are just
290 // labels to jump to, not full Nodes lists.
291 type JumpTableStmt struct {
292         miniStmt
293
294         // Value used to index the jump table.
295         // We support only integer types that
296         // are at most the size of a uintptr.
297         Idx Node
298
299         // If Idx is equal to Cases[i], jump to Targets[i].
300         // Cases entries must be distinct and in increasing order.
301         // The length of Cases and Targets must be equal.
302         Cases   []constant.Value
303         Targets []*types.Sym
304 }
305
306 func NewJumpTableStmt(pos src.XPos, idx Node) *JumpTableStmt {
307         n := &JumpTableStmt{Idx: idx}
308         n.pos = pos
309         n.op = OJUMPTABLE
310         return n
311 }
312
313 // An InterfaceSwitchStmt is used to implement type switches.
314 // Its semantics are:
315 //
316 //     if RuntimeType implements Descriptor.Cases[0] {
317 //         Case, Itab = 0, itab<RuntimeType, Descriptor.Cases[0]>
318 //     } else if RuntimeType implements Descriptor.Cases[1] {
319 //         Case, Itab = 1, itab<RuntimeType, Descriptor.Cases[1]>
320 //     ...
321 //     } else if RuntimeType implements Descriptor.Cases[N-1] {
322 //         Case, Itab = N-1, itab<RuntimeType, Descriptor.Cases[N-1]>
323 //     } else {
324 //         Case, Itab = len(cases), nil
325 //     }
326 // RuntimeType must be a non-nil *runtime._type.
327 // Descriptor must represent an abi.InterfaceSwitch global variable.
328 type InterfaceSwitchStmt struct {
329         miniStmt
330
331         Case        Node
332         Itab        Node
333         RuntimeType Node
334         Descriptor  *obj.LSym
335 }
336
337 func NewInterfaceSwitchStmt(pos src.XPos, case_, itab, runtimeType Node, descriptor *obj.LSym) *InterfaceSwitchStmt {
338         n := &InterfaceSwitchStmt{
339                 Case:        case_,
340                 Itab:        itab,
341                 RuntimeType: runtimeType,
342                 Descriptor:  descriptor,
343         }
344         n.pos = pos
345         n.op = OINTERFACESWITCH
346         return n
347 }
348
349 // An InlineMarkStmt is a marker placed just before an inlined body.
350 type InlineMarkStmt struct {
351         miniStmt
352         Index int64
353 }
354
355 func NewInlineMarkStmt(pos src.XPos, index int64) *InlineMarkStmt {
356         n := &InlineMarkStmt{Index: index}
357         n.pos = pos
358         n.op = OINLMARK
359         return n
360 }
361
362 func (n *InlineMarkStmt) Offset() int64     { return n.Index }
363 func (n *InlineMarkStmt) SetOffset(x int64) { n.Index = x }
364
365 // A LabelStmt is a label statement (just the label, not including the statement it labels).
366 type LabelStmt struct {
367         miniStmt
368         Label *types.Sym // "Label:"
369 }
370
371 func NewLabelStmt(pos src.XPos, label *types.Sym) *LabelStmt {
372         n := &LabelStmt{Label: label}
373         n.pos = pos
374         n.op = OLABEL
375         return n
376 }
377
378 func (n *LabelStmt) Sym() *types.Sym { return n.Label }
379
380 // A RangeStmt is a range loop: for Key, Value = range X { Body }
381 type RangeStmt struct {
382         miniStmt
383         Label        *types.Sym
384         Def          bool
385         X            Node
386         RType        Node `mknode:"-"` // see reflectdata/helpers.go
387         Key          Node
388         Value        Node
389         Body         Nodes
390         DistinctVars bool
391         Prealloc     *Name
392
393         // When desugaring the RangeStmt during walk, the assignments to Key
394         // and Value may require OCONVIFACE operations. If so, these fields
395         // will be copied to their respective ConvExpr fields.
396         KeyTypeWord   Node `mknode:"-"`
397         KeySrcRType   Node `mknode:"-"`
398         ValueTypeWord Node `mknode:"-"`
399         ValueSrcRType Node `mknode:"-"`
400 }
401
402 func NewRangeStmt(pos src.XPos, key, value, x Node, body []Node, distinctVars bool) *RangeStmt {
403         n := &RangeStmt{X: x, Key: key, Value: value}
404         n.pos = pos
405         n.op = ORANGE
406         n.Body = body
407         n.DistinctVars = distinctVars
408         return n
409 }
410
411 // A ReturnStmt is a return statement.
412 type ReturnStmt struct {
413         miniStmt
414         Results Nodes // return list
415 }
416
417 func NewReturnStmt(pos src.XPos, results []Node) *ReturnStmt {
418         n := &ReturnStmt{}
419         n.pos = pos
420         n.op = ORETURN
421         n.Results = results
422         return n
423 }
424
425 // A SelectStmt is a block: { Cases }.
426 type SelectStmt struct {
427         miniStmt
428         Label *types.Sym
429         Cases []*CommClause
430
431         // TODO(rsc): Instead of recording here, replace with a block?
432         Compiled Nodes // compiled form, after walkSelect
433 }
434
435 func NewSelectStmt(pos src.XPos, cases []*CommClause) *SelectStmt {
436         n := &SelectStmt{Cases: cases}
437         n.pos = pos
438         n.op = OSELECT
439         return n
440 }
441
442 // A SendStmt is a send statement: X <- Y.
443 type SendStmt struct {
444         miniStmt
445         Chan  Node
446         Value Node
447 }
448
449 func NewSendStmt(pos src.XPos, ch, value Node) *SendStmt {
450         n := &SendStmt{Chan: ch, Value: value}
451         n.pos = pos
452         n.op = OSEND
453         return n
454 }
455
456 // A SwitchStmt is a switch statement: switch Init; Tag { Cases }.
457 type SwitchStmt struct {
458         miniStmt
459         Tag   Node
460         Cases []*CaseClause
461         Label *types.Sym
462
463         // TODO(rsc): Instead of recording here, replace with a block?
464         Compiled Nodes // compiled form, after walkSwitch
465 }
466
467 func NewSwitchStmt(pos src.XPos, tag Node, cases []*CaseClause) *SwitchStmt {
468         n := &SwitchStmt{Tag: tag, Cases: cases}
469         n.pos = pos
470         n.op = OSWITCH
471         return n
472 }
473
474 // A TailCallStmt is a tail call statement, which is used for back-end
475 // code generation to jump directly to another function entirely.
476 type TailCallStmt struct {
477         miniStmt
478         Call *CallExpr // the underlying call
479 }
480
481 func NewTailCallStmt(pos src.XPos, call *CallExpr) *TailCallStmt {
482         n := &TailCallStmt{Call: call}
483         n.pos = pos
484         n.op = OTAILCALL
485         return n
486 }
487
488 // A TypeSwitchGuard is the [Name :=] X.(type) in a type switch.
489 type TypeSwitchGuard struct {
490         miniNode
491         Tag  *Ident
492         X    Node
493         Used bool
494 }
495
496 func NewTypeSwitchGuard(pos src.XPos, tag *Ident, x Node) *TypeSwitchGuard {
497         n := &TypeSwitchGuard{Tag: tag, X: x}
498         n.pos = pos
499         n.op = OTYPESW
500         return n
501 }