/
instr.go
866 lines (764 loc) · 24.1 KB
/
instr.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
package migoinfer
import (
"github.com/JorgeGCoelho/gospal/v2/callctx"
"github.com/JorgeGCoelho/gospal/v2/fn"
"github.com/JorgeGCoelho/gospal/v2/funcs"
"github.com/JorgeGCoelho/gospal/v2/store"
"github.com/JorgeGCoelho/gospal/v2/store/chans"
"github.com/JorgeGCoelho/gospal/v2/store/mems"
"github.com/JorgeGCoelho/gospal/v2/store/muts"
"github.com/JorgeGCoelho/gospal/v2/store/structs"
"github.com/JorgeGCoelho/migo/v3"
"github.com/fatih/color"
"github.com/pkg/errors"
"go/token"
"go/types"
"golang.org/x/tools/go/ssa"
"strings"
)
// Instruction is a visitor for related instructions within a block.
type Instruction struct {
Callee *funcs.Instance // Instance of this function.
callctx.Context // Function context.
Env *Environment // Program environment.
MiGo *migo.Function // MiGo function definition of current block.
*Exported // Local variables.
*Logger
}
func NewInstruction(callee *funcs.Instance, ctx callctx.Context, env *Environment, migoFn *migo.Function) *Instruction {
i := Instruction{
Callee: callee,
Context: ctx,
Env: env,
MiGo: migoFn,
}
return &i
}
func (v *Instruction) VisitInstr(instr ssa.Instruction) {
switch instr := instr.(type) {
case *ssa.Alloc:
v.Debugf("%s Alloc: %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitAlloc(instr)
case *ssa.BinOp:
v.VisitBinOp(instr)
case *ssa.Call:
v.Debugf("%s Call: %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitCall(instr)
case *ssa.ChangeInterface:
v.VisitChangeInterface(instr)
case *ssa.ChangeType:
v.Debugf("%s ChangeType: %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitChangeType(instr)
case *ssa.Convert:
v.VisitConvert(instr)
case *ssa.DebugRef:
v.VisitDebugRef(instr)
case *ssa.Defer:
v.VisitDefer(instr)
case *ssa.Extract:
v.VisitExtract(instr)
case *ssa.Field:
v.VisitField(instr)
case *ssa.FieldAddr:
v.Debugf("%s FieldAddr: %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitFieldAddr(instr)
case *ssa.Go:
v.Debugf("%s Go: %s\n\t%s",
v.Module(), instr, v.Env.getPos(instr))
v.VisitGo(instr)
case *ssa.If:
v.VisitIf(instr)
case *ssa.Index:
v.VisitIndex(instr)
case *ssa.IndexAddr:
v.VisitIndexAddr(instr)
case *ssa.Jump:
v.VisitJump(instr)
case *ssa.Lookup:
v.VisitLookup(instr)
case *ssa.MakeChan:
v.Debugf("%s MakeChan %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitMakeChan(instr)
case *ssa.MakeClosure:
v.Debugf("%s MakeClosure %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitMakeClosure(instr)
case *ssa.MakeInterface:
v.Debugf("%s MakeInterface %s = %s\n\t%s",
v.Module(), instr.Name(), instr, v.Env.getPos(instr))
v.VisitMakeInterface(instr)
case *ssa.MakeMap:
v.VisitMakeMap(instr)
case *ssa.MakeSlice:
v.VisitMakeSlice(instr)
case *ssa.MapUpdate:
v.VisitMapUpdate(instr)
case *ssa.Next:
v.VisitNext(instr)
case *ssa.Panic:
v.VisitPanic(instr)
case *ssa.Phi:
v.VisitPhi(instr)
case *ssa.Range:
v.VisitRange(instr)
case *ssa.Return:
v.VisitReturn(instr)
case *ssa.RunDefers:
v.VisitRunDefers(instr)
case *ssa.Select:
v.VisitSelect(instr)
case *ssa.Send:
v.Debugf("%s Send: %s\n\t%s",
v.Module(), instr, v.Env.getPos(instr))
v.VisitSend(instr)
case *ssa.Slice:
v.VisitSlice(instr)
case *ssa.Store:
v.VisitStore(instr)
case *ssa.TypeAssert:
v.VisitTypeAssert(instr)
case *ssa.UnOp:
v.VisitUnOp(instr)
default:
v.Fatalf("%s Unhandled instruction %q (%T)\n\t%s",
v.Module(), instr, instr, v.Env.getPos(instr))
}
}
func typeIsMutex(t types.Type) bool {
if ptr, ok := t.(*types.Pointer); ok && ptr.Elem().String() == "sync.Mutex" {
return true
}
return false
}
func typeIsRWMutex(t types.Type) bool {
if ptr, ok := t.(*types.Pointer); ok && ptr.Elem().String() == "sync.RWMutex" {
return true
}
return false
}
func (v *Instruction) VisitAlloc(instr *ssa.Alloc) {
t := instr.Type().(*types.Pointer).Elem()
switch t := t.Underlying().(type) {
case *types.Struct:
v.Debugf("%s Allocate struct: %T", v.Module(), t)
if updater, ok := v.Context.(callctx.Updater); ok {
updater.PutUniq(instr, structs.New(v.Callee, instr))
}
if typeIsMutex(instr.Type()) {
newmut := v.newMut(instr)
v.Export(newmut)
v.MiGo.AddStmts(migoNewMutex(newmut))
}
if typeIsRWMutex(instr.Type()) {
newmut := v.newMut(instr)
v.Export(newmut)
v.MiGo.AddStmts(migoNewRWMutex(newmut))
}
case *types.Basic:
// Note: this only handles non-struct (flat) types.
newmem := v.newMem(instr)
v.Export(newmem)
v.MiGo.AddStmts(migoNewMem(newmem))
default:
v.Debugf("%s Alloc %s = type %s (delay write)",
v.Module(), instr.Name(), t.String())
}
}
func (v *Instruction) VisitBinOp(instr *ssa.BinOp) {
}
func (v *Instruction) VisitCall(instr *ssa.Call) {
def := v.createDefinition(instr.Common())
if def == nil {
return
}
if _, ok := v.Env.VisitedFunc[instr.Common()]; ok {
return
}
v.Env.VisitedFunc[instr.Common()] = true
v.doCall(instr, def)
}
func (v *Instruction) VisitChangeInterface(instr *ssa.ChangeInterface) {
}
func (v *Instruction) VisitChangeType(instr *ssa.ChangeType) {
if _, ok := instr.X.Type().(*types.Chan); ok {
v.Put(instr, v.Get(instr.X))
}
}
func (v *Instruction) VisitConvert(instr *ssa.Convert) {
}
func (v *Instruction) VisitDebugRef(instr *ssa.DebugRef) {
}
func (v *Instruction) VisitDefer(instr *ssa.Defer) {
}
func (v *Instruction) VisitExtract(instr *ssa.Extract) {
}
func (v *Instruction) VisitField(instr *ssa.Field) {
}
func (v *Instruction) VisitFieldAddr(instr *ssa.FieldAddr) {
switch struc := v.Get(instr.X).(type) {
case *structs.Struct:
if field := struc.Fields[instr.Field]; field != nil {
if fieldVal := v.Get(field); fieldVal != nil {
v.Debugf("%s Field %s exists, connect %s ↔ %s", v.Module(), field.Name(), instr.Name(), field.Name())
// This is a hack to fix parameter export.
// Get(store.Key)/Unexport uses Name() to lookup (since name is unique).
// Export/FindExported uses the actual pointer to lookup (so that the actual reference to ssa.MakeChan is kept)
// By overwriting field with fieldVal which Get retrieves
// The old dangling Exported field will point to a real fieldVal
v.Put(field, fieldVal)
v.Unexport(field)
v.Export(field)
v.Put(instr, fieldVal)
}
} else {
// Put object in storage.
if updater, ok := v.Context.(callctx.Updater); ok {
updater.PutObj(instr, instr.X)
}
struc.Fields[instr.Field] = instr
}
case store.MockValue:
v.Debugf("%s struct undefined\n\t%s", v.Module(), v.Env.getPos(instr))
default:
v.Warnf("%s FieldAddr: %v is not a struct\t%s\n\t%s",
v.Module(), instr.X, instr.X.Type().Underlying(), v.Env.getPos(instr))
}
}
func (v *Instruction) VisitGo(instr *ssa.Go) {
def := v.createDefinition(instr.Common())
if def == nil {
return
}
if _, ok := v.Env.VisitedFunc[instr.Common()]; ok {
return
}
v.Env.VisitedFunc[instr.Common()] = true
v.doGo(instr, def)
}
func (v *Instruction) VisitIf(instr *ssa.If) {
}
func (v *Instruction) VisitIndex(instr *ssa.Index) {
}
func (v *Instruction) VisitIndexAddr(instr *ssa.IndexAddr) {
}
func (v *Instruction) VisitJump(instr *ssa.Jump) {
}
func (v *Instruction) VisitLookup(instr *ssa.Lookup) {
}
func (v *Instruction) VisitMakeChan(instr *ssa.MakeChan) {
newch := v.newChan(instr)
isReturnValue := v.Callee.Definition().IsReturn(instr)
var isParameter bool
str, field, isField := getStruct(instr)
if !isField { // Could be that the field is stored by *t0 = make(chan)
for _, ref := range *instr.Referrers() {
switch ref := ref.(type) {
case *ssa.Store:
if ref.Val == instr {
str, field, isField = getStruct(ref.Addr)
}
}
}
}
if isField {
for _, param := range v.Callee.Definition().Parameters[:v.Callee.Definition().NParam+v.Callee.Definition().NFreeVar] {
if param == str {
isParameter = true
}
}
if s, ok := v.Get(str).(*structs.Struct); ok {
s.Fields[field] = instr
}
}
if isReturnValue || isParameter {
v.Debugf("%s %s = MakeChan skipped\n\treturn value? %t\n\tparameter? %t",
v.Module(),
instr.Name(), isReturnValue, isParameter)
v.MiGo.AddStmts(&migo.TauStatement{})
return
}
v.Put(instr, newch)
v.Export(instr)
v.MiGo.AddStmts(migoNewChan(v.Logger, instr, newch))
}
func (v *Instruction) VisitMakeClosure(instr *ssa.MakeClosure) {
def := funcs.MakeClosureDefinition(instr.Fn.(*ssa.Function), instr.Bindings)
v.Put(instr, def) // For calling the closure.
v.Put(instr.Fn, def) // For reusing the closure.
f := v.Get(instr)
v.Debugf("%s ↳ %s", v.Module(), f.(*funcs.Definition).String())
}
func (v *Instruction) VisitMakeInterface(instr *ssa.MakeInterface) {
iface := v.Get(instr.X)
v.Debugf("%s iface → %v", v.Module(), iface)
v.Put(instr, iface)
}
func (v *Instruction) VisitMakeMap(instr *ssa.MakeMap) {
}
func (v *Instruction) VisitMakeSlice(instr *ssa.MakeSlice) {
}
func (v *Instruction) VisitMapUpdate(instr *ssa.MapUpdate) {
}
func (v *Instruction) VisitNext(instr *ssa.Next) {
}
func (v *Instruction) VisitPanic(instr *ssa.Panic) {
}
func (v *Instruction) VisitPhi(instr *ssa.Phi) {
}
func (v *Instruction) VisitRange(instr *ssa.Range) {
}
func (v *Instruction) VisitReturn(instr *ssa.Return) {
}
func (v *Instruction) VisitRunDefers(instr *ssa.RunDefers) {
}
func (v *Instruction) VisitSelect(instr *ssa.Select) {
v.MiGo.AddStmts(v.getSelectCases(instr))
}
func (v *Instruction) VisitSend(instr *ssa.Send) {
v.MiGo.AddStmts(migoSend(v, instr.Chan, v.Get(instr.Chan), instr.Pos()))
}
func (v *Instruction) VisitSlice(instr *ssa.Slice) {
handle := v.Get(instr.X)
if instr.Low == nil && instr.High == nil { // Full slice.
v.Put(instr, handle)
}
}
func (v *Instruction) VisitStore(instr *ssa.Store) {
val := v.Get(instr.Val)
if val != nil {
if isChan(instr.Val) {
// if storing chan T to a *chan T type
// the destination *instr.Addr should use the original channel (instr.Val).
v.Put(instr.Addr, val)
} else {
// normal store
// keep instr.Addr pointing to originally declared Mem.
if strings.Contains(v.Get(instr.Addr).UniqName(), "mem") {
v.MiGo.AddStmts(migoWrite(v, instr.Addr))
}
}
} else {
v.Fatalf("Store: %s is not defined", instr.Val.Name())
}
}
func (v *Instruction) VisitTypeAssert(instr *ssa.TypeAssert) {
v.Put(instr, v.Get(instr.X))
}
func (v *Instruction) VisitUnOp(instr *ssa.UnOp) {
switch instr.Op {
case token.ARROW:
v.MiGo.AddStmts(migoRecv(v, instr.X, v.Get(instr.X), instr.Pos()))
case token.MUL:
if isPtrBasic(instr.X) {
if strings.Contains(v.Get(instr.X).UniqName(), "mem") {
v.MiGo.AddStmts(migoRead(v, instr.X))
}
} else {
if _, err := callctx.Deref(v.Context, instr.X, instr); err != nil {
v.Env.Errors <- errors.WithStack(err) // internal error.
}
}
default:
v.Debugf("%s UnOp", v.Module(), instr)
}
}
// SetLogger sets logger for Instruction.
func (v *Instruction) SetLogger(l *Logger) {
v.Logger = &Logger{
SugaredLogger: l.SugaredLogger,
module: color.RedString("instr"),
}
}
func (v *Instruction) createDefinition(c *ssa.CallCommon) *funcs.Definition {
if !c.IsInvoke() {
switch fn := c.Value.(type) {
case *ssa.Function, *ssa.MakeClosure:
def, ok := v.Get(fn).(*funcs.Definition)
if !ok {
def = funcs.MakeDefinition(c.StaticCallee())
v.Put(fn, def)
}
v.Debugf("%s ↳ def %s", v.Module(), def.String())
return def
case *ssa.Builtin:
if fn.Name() == "close" {
if len(c.Args) != 1 {
v.Fatal("%s inconsistent: close should have 1 arg",
v.Module())
}
exported := v.FindExported(v.Context, v.Get(c.Args[0]))
v.MiGo.AddStmts(&migo.CloseStatement{Chan: exported.Name()})
}
v.Debugf("%s %v", v.Module(), fn)
}
return nil
}
// Invoke mode.
// Calls method on an interface, we find the struct that implements the
// functions in the interface, by tracing back the MakeInterface instruction
// of the SSA register, but this can fail (e.g. determined at runtime)
//
// Meth: The method (of an interface) being called.
// Var: The implementation variable - this may be an interface but
// LookupImpl will find the real underlying implementation variable.
v.Debugf("%s invoke call: %s, lookup function\n\tMeth:\t%s\n\tVar:\t%s\t%T",
v.Module(), c, c.Method, c.Value, c.Value,
)
if c.Value != nil {
implFn, err := fn.LookupImpl(v.Env.Info.Prog, c.Method, c.Value)
if err != nil {
v.Warnf("%s Cannot find method %v for invoke call: %v\n\tMeth: %s\n\tImpl: %s:%s",
v.Module(), c, err,
c.Method.String(),
c.Value.Name(), c.Value.Type().String())
return nil // skip
}
if implFn.Synthetic != "" {
implFn = fn.FindConcrete(v.Env.Info.Prog, implFn)
}
def, ok := v.Get(implFn).(*funcs.Definition)
if !ok {
def = funcs.MakeDefinition(implFn)
v.Put(implFn, def)
}
v.Debugf("%s ↳ invoke %s", v.Module(), def.String())
return def
}
v.Debugf("%s definition not created: call impl is nil", v.Module())
return nil
}
func (v *Instruction) doCall(c *ssa.Call, def *funcs.Definition) {
call := funcs.MakeCall(def, c.Common(), c)
if call == nil {
v.Warnf("%s Skipping nil call %s", v.Module(), c.Common())
return
}
v.Debugf("%s Definition: %v", v.Module(), def.String())
v.Debugf("%s Call: %v", v.Module(), call.String())
fn := NewFunction(call, v.Context, v.Env)
fn.SetLogger(v.Logger)
v.Debugf("%s Context at caller: %v%v", v.Module(), v.Context, v.Exported)
v.Debugf("%s Context at callee: %v%v", v.Module(), fn.Context, fn.Exported)
fn.exportParams()
if len(call.Function().Blocks) == 0 {
// Since the function does not have body,
// calling it will not produce migo definitions.
// Instead of trying to visit the function, skip over this.
return
}
if def := call.Definition(); def.NParam > 0 {
switch {
case typeIsRWMutex(def.Param(0).Type()):
if call.Function().Name() == "RLock" {
v.MiGo.AddStmts(migoRLock(call.Args[0]))
return
}
if call.Function().Name() == "RUnlock" {
v.MiGo.AddStmts(migoRUnlock(call.Args[0]))
return
}
fallthrough
case typeIsMutex(def.Param(0).Type()):
if call.Function().Name() == "Lock" {
v.MiGo.AddStmts(migoLock(call.Args[0]))
return
}
if call.Function().Name() == "Unlock" {
v.MiGo.AddStmts(migoUnlock(call.Args[0]))
return
}
}
}
fn.EnterFunc(call.Function())
stmt := &migo.CallStatement{Name: fn.Callee.Name()}
v.bindCallParameters(call, fn)
// Before adding call statement, handle return values.
for i := range call.Parameters[call.NParam()+call.NBind():] {
callerName := call.Return(i)
caller := v.Get(callerName)
callee := fn.Get(call.Definition().Return(i))
if caller != callee {
v.Put(callerName, callee)
if isChan(callerName) { // Caller is a channel.
if _, ok := callerName.(store.Unused); !ok {
if calleeCh, ok := callee.(*chans.Chan); ok {
v.MiGo.AddStmts(migoNewChan(v.Logger, callerName, calleeCh))
v.Export(callerName) // Export caller name
} else {
// Callee does not initialise channel.
}
}
}
}
}
// Convert type Chan parameters to MiGo parameters.
migoParams := paramsToMigoParam(v, fn, call)
stmt.AddParams(migoParams...)
if b, ok := fn.Analyser.(*Block); ok {
for _, data := range b.meta {
data.migoFunc.AddParams(migoParams...)
}
}
v.MiGo.AddStmts(stmt)
}
func (v *Instruction) doGo(g *ssa.Go, def *funcs.Definition) {
call := funcs.MakeCall(def, g.Common(), nil)
if call == nil {
v.Infof("%s Skipping nil go %s", v.Module(), g.Common())
return
}
v.Debugf("%s Definition: %v", v.Module(), def.String())
v.Debugf("%s Go/Call: %v", v.Module(), call.String())
fn := NewFunction(call, v.Context, v.Env)
fn.SetLogger(v.Logger)
v.Debugf("%s Context at caller: %v%v", v.Module(), v.Context, v.Exported)
v.Debugf("%s Context at callee: %v%v", v.Module(), fn.Context, fn.Exported)
fn.exportParams()
fn.EnterFunc(call.Function())
stmt := &migo.SpawnStatement{Name: fn.Callee.Name()}
v.bindCallParameters(call, fn)
// Convert type Chan parameters to MiGo parameters.
migoParams := paramsToMigoParam(v, fn, call)
stmt.AddParams(migoParams...)
if b, ok := fn.Analyser.(*Block); ok {
for _, data := range b.meta {
data.migoFunc.AddParams(migoParams...)
}
}
v.MiGo.AddStmts(stmt)
}
// getStruct returns the field variable and field index if the given value is a
// struct field.
func getStruct(value ssa.Value) (ssa.Value, int, bool) {
switch value := value.(type) {
case *ssa.FieldAddr:
return value.X, value.Field, true
case *ssa.UnOp:
if value.Op == token.MUL {
return getStruct(value.X)
}
}
return nil, -1, false
}
// getParameterName looks for a parent struct or returns the origin value if it
// is not part of a struct.
func (v *Instruction) getParameterName(value ssa.Value) migo.NamedVar {
if str, fld, ok := getStruct(value); ok {
if s, ok := v.Get(str).(*structs.Struct); ok {
return s.Fields[fld]
}
}
// No parameter name.
return value
}
// newChan creates a new channel instance
func (v *Instruction) newChan(ch ssa.Value) *chans.Chan {
var bufSize int64
bufsz, ok := ch.(*ssa.MakeChan).Size.(*ssa.Const)
if !ok {
v.Env.Errors <- ErrChanBufSzNonStatic{Pos: v.Env.Info.FSet.Position(ch.Pos())}
bufSize = 1
} else {
bufSize = bufsz.Int64()
}
newch := chans.New(v.Callee, ch, bufSize)
if updater, ok := v.Context.(callctx.Updater); ok {
updater.PutUniq(ch, newch)
} else {
v.Fatal("Cannot update context")
}
return newch
}
// newMem creates a new Mem for memory allocation as val.
func (v *Instruction) newMem(val ssa.Value) *mems.Mem {
newmem := mems.New(v.Callee, val)
if u, ok := v.Context.(callctx.Updater); ok {
u.PutUniq(val, newmem)
} else {
v.Fatal("Cannot update context")
}
return newmem
}
// newMut creates a new Mut.
func (v *Instruction) newMut(val ssa.Value) *muts.Mut {
newmut := muts.New(v.Callee, val)
if u, ok := v.Context.(callctx.Updater); ok {
u.PutUniq(val, newmut)
} else {
v.Fatal("Cannot update context")
}
return newmut
}
const (
selectCaseIndex = 0
selectCaseValue = 1
)
func (v *Instruction) getSelectCases(sel *ssa.Select) migo.Statement {
nCases := len(sel.States)
if !sel.Blocking {
nCases++
}
stmt := &migo.SelectStatement{Cases: make([][]migo.Statement, nCases)}
var migoParams []*migo.Parameter
for _, name := range v.Exported.names {
migoParams = append(migoParams, &migo.Parameter{Callee: name, Caller: name})
}
if !sel.Blocking {
stmt.Cases[nCases-1] = append(stmt.Cases[nCases-1], &migo.TauStatement{})
}
for _, selCase := range *sel.Referrers() {
switch c := selCase.(type) {
case *ssa.Extract:
// Find all select-index tests.
switch c.Index {
case selectCaseIndex:
for _, selTest := range *c.Referrers() {
switch selTest := selTest.(type) {
case *ssa.BinOp: // Select branch is this form, t_test = t_index == intval
if con, ok := selTest.Y.(*ssa.Const); selTest.X == c && selTest.Op == token.EQL && ok {
idx := int(con.Int64())
bodyGuard := v.selBodyGuard(sel, idx)
bodyBlk, defaultBlk := v.selBodyBlock(sel, idx, selTest.Block())
if bodyBlk != nil {
stmt.Cases[idx] = append(stmt.Cases[idx], bodyGuard)
v.Debugf("%s Select index #%d block #%d (%s)", v.Module(), idx, bodyBlk.Index, bodyBlk.Comment)
} else {
v.Debugf("%s Select index #%d no continuation", v.Module(), idx)
}
if defaultBlk != nil {
stmt.Cases[idx+1] = append(stmt.Cases[idx+1], migoCall(v.Callee.Name(), defaultBlk, v.Exported))
}
if bodyBlk != nil { // Return (no continuation)
stmt.Cases[idx] = append(stmt.Cases[idx], migoCall(v.Callee.Name(), bodyBlk, v.Exported))
}
}
default:
v.Fatal("%s Unexpected select-index test expression",
v.Module(), selTest.String())
}
}
}
}
}
return stmt
}
// selBodyGuard returns the guard action of a select case (except for default).
func (v *Instruction) selBodyGuard(sel *ssa.Select, caseIdx int) migo.Statement {
selCase := sel.States[caseIdx]
// Select guard actions then jump to body blocks
switch selCase.Dir {
case types.SendOnly:
return migoSend(v, selCase.Chan, v.Get(selCase.Chan), selCase.Pos)
case types.RecvOnly:
return migoRecv(v, selCase.Chan, v.Get(selCase.Chan), selCase.Pos)
default:
v.Fatalf("%s Select case is guarded by neither send nor receive.\n\t%s",
v.Module(), v.Env.getPos(sel))
}
return nil
}
// selBodyBlock returns the body block of a select case and the default case if
// the block is is the last case.
//
// if tauBlk is not nil, caseIdx is guaranteed to be the last case.
func (v *Instruction) selBodyBlock(sel *ssa.Select, caseIdx int, testBlk *ssa.BasicBlock) (bodyBlk, tauBlk *ssa.BasicBlock) {
switch inst := testBlk.Instrs[len(testBlk.Instrs)-1].(type) {
case *ssa.If: // Normal case.
if isLastCase := caseIdx == len(sel.States)-1 && !sel.Blocking; isLastCase {
v.Debugf("%s Select default block #%d.\n\t%s",
v.Module(), caseIdx+1, v.Env.getPos(sel))
return inst.Block().Succs[0], inst.Block().Succs[1]
}
return inst.Block().Succs[0], nil
case *ssa.Jump: // Else branch empty, followed by continuation of select.
v.Debugf("%s Select default block empty (jump).\n\t%s",
v.Module(), v.Env.getPos(sel))
return inst.Block().Succs[0], nil
case *ssa.Return: // Else branch empty and no continuation after select.
v.Debugf("%s Select default block empty (return).\n\t%s",
v.Module(), v.Env.getPos(sel))
return nil, nil
default:
v.Fatalf("%s Select case has unrecognised last instruction in block.\n\t%s",
v.Module(), v.Env.getPos(inst))
}
return nil, nil
}
// bindCallParameters takes a function call and matches up the definition
// paramters with the call arguments.
func (v *Instruction) bindCallParameters(call *funcs.Call, fn *Function) {
handleNilChanArg := func(arg, param store.Key) {
v.Debugf("%s Handle nilchan for %s(⋯)\n\t%s\n\t\tat caller %s=%#v\n\t\tat callee %s=%#v",
v.Module(), call.Function().String(), v.Env.getPos(call.Function()),
arg.Name(), arg, param.Name(), param)
switch calleeChan := fn.Get(param).(type) {
case *chans.Chan:
v.MiGo.AddStmts(migoNewChan(v.Logger, arg, calleeChan))
v.Export(arg)
case store.MockValue:
// nilchan argument handling is delayed at migo generation time.
// See paramsToMigoParam()
}
}
// Before adding call statement, handle nil parameters.
for i := range call.Parameters[:call.NParam()+call.NBind()] {
arg, param := call.Param(i), call.Definition().Param(i)
if isStruct(arg) {
argStruct := v.Get(arg)
paramStruct := fn.Get(param)
if mock, ok := argStruct.(store.MockValue); ok {
v.Debugf("%s %s is a nil struct (arg) (type:%s)",
v.Module(), arg.Name(), arg.Type().String())
argStruct = structs.New(mock, arg.(ssa.Value))
} else if _, ok := argStruct.(*structs.Struct); !ok {
argStruct = structs.New(mock, arg.(ssa.Value))
}
if mock, ok := paramStruct.(store.MockValue); ok {
v.Debugf("%s %s is a nil struct (param) (type:%s)",
v.Module(), param.Name(), param.Type().String())
paramStruct = structs.New(mock, param.(ssa.Value))
} else if _, ok := paramStruct.(*structs.Struct); !ok {
paramStruct = structs.New(mock, arg.(ssa.Value))
}
argFields := argStruct.(*structs.Struct).Expand()
prmFields := paramStruct.(*structs.Struct).Expand()
for i := 0; i < len(argFields); i++ {
switch argField := argFields[i].(type) {
case structs.SField:
prmField := prmFields[i].(structs.SField)
if isChan(argField) && argField.Key == nil && prmField.Key != nil {
// Handle nil channel:
// argField is !defined, prmField is defined (in body).
handleNilChanArg(argField, prmField.Key)
}
// Field defined inside function, add defined value and
// update struct.Fields
if argField.Key == nil && prmField.Key != nil {
v.Put(argField, fn.Get(prmField.Key))
argField.Struct.Fields[argField.Index] = argField
}
case *structs.Struct:
// Ignore.
}
}
}
if isChan(arg) {
if _, ok := v.Get(arg).(store.MockValue); ok {
if _, isPhi := arg.(*ssa.Phi); !isPhi {
handleNilChanArg(arg, param)
}
}
}
if isPtrBasic(arg) {
v.Export(arg)
}
if typeIsRWMutex(arg.Type()) || typeIsMutex(arg.Type()) {
v.Export(arg)
}
}
}