/
ssa.go
648 lines (518 loc) · 11.7 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
package ssa
import (
"sync"
"github.com/yaklang/yaklang/common/utils/omap"
)
type ErrorLogger interface {
NewError(ErrorKind, ErrorTag, string)
}
type Instruction interface {
ErrorLogger
GetOpcode() Opcode
// function
GetFunc() *Function
SetFunc(*Function)
// block
GetBlock() *BasicBlock
SetBlock(*BasicBlock)
// program
GetProgram() *Program
GetName() string
SetName(variable string)
GetVerboseName() string
GetShortVerboseName() string
SetVerboseName(string)
GetId() int64 // for identify
SetId(int64)
// position
GetRange() *Range
SetRange(*Range)
// Scope
// SetScope(*Scope)
// GetScope() *Scope
// extern
IsExtern() bool
SetExtern(bool)
GetVariable(string) *Variable
GetLastVariable() *Variable
GetAllVariables() map[string]*Variable
AddVariable(*Variable)
SelfDelete()
}
type (
Users []User
Values []Value
)
// data-flow
type Node interface {
// string
String() string
// for graph
HasUsers() bool
GetUsers() Users
HasValues() bool
GetValues() Values
IsUndefined() bool
}
type Typed interface {
// Node
// type
GetType() Type
SetType(Type)
}
type MemberCall interface {
// object member caller
IsObject() bool
AddMember(Value, Value)
GetMember(Value) (Value, bool)
GetIndexMember(int) (Value, bool)
GetStringMember(string) (Value, bool)
DeleteMember( /*key*/ Value) // delete by key
GetAllMember() map[Value]Value // map[key]value
// ReplaceMember( /* key */ Value /* value */, Value) // replace old-value with new-value
// member, member callee
IsMember() bool
SetObject(Value)
SetKey(Value)
GetKey() Value
GetObject() Value
// ReplaceObject(Value) // replace old-object to new-object
}
// basic handle item (interface)
type Value interface {
Node
Instruction
MemberCall
Typed
Maskable
AddUser(User)
RemoveUser(User)
}
type Maskable interface {
AddMask(Value)
GetMask() []Value
Masked() bool
}
type User interface {
Node
Instruction
ReplaceValue(Value, Value)
}
// both instruction and value
type Program struct {
// package list
Packages map[string]*Package
Cache *Cache
// class blue print
ClassBluePrint map[string]*ClassBluePrint
errors SSAErrors
// for build
buildOnce sync.Once
}
type Package struct {
Name string
// point to program
Prog *Program
// function list
Funcs map[string]*Function
}
// implement Value
type Function struct {
anValue
// package, double link
Package *Package
// just function parameter and all return instruction
Param []*Parameter
paramMap map[Value]int // for get parameter index
ParamLength int
Return []*Return
// Type
Type *FunctionType
// BasicBlock list
Blocks []*BasicBlock
// First and End block
EnterBlock *BasicBlock
ExitBlock *BasicBlock
// For Defer semantic
// this block will always execute when the function exits,
// regardless of whether the function returns normally or exits due to a panic.
DeferBlock *BasicBlock
// for closure function
FreeValues map[string]*Parameter // store the captured variable form parent-function, just contain name, and type is Parameter
// closure function side effects
// TODO: currently, this value is not being used, but it should be utilized in the future.
SideEffects []*FunctionSideEffect
// closure function double link. parentFunc <-> childFuncs
parent *Function // parent function; can be nil if there is no parent function
ChildFuncs []*Function // child function within this function
// extern lib
cacheExternInstance map[string]Value // lib and value
externType map[string]Type
// include / require / eval-code / import packet
referenceFiles *omap.OrderedMap[string, string]
// ssa error
errComment ErrorComment
// ================ for build
// builder
builder *FunctionBuilder
// this function is variadic parameter, for function type create
hasEllipsis bool
}
func (f *Function) PushReferenceFile(file, code string) {
f.referenceFiles.Set(file, code)
}
func (f *Function) FirstBlockInstruction() []Instruction {
if len(f.Blocks) > 0 {
return f.Blocks[0].Insts
}
return nil
}
var (
_ Node = (*Function)(nil)
_ Value = (*Function)(nil)
)
// implement Value
type BasicBlock struct {
anValue
Index int
// BasicBlock graph
Preds, Succs []*BasicBlock
/*
if Condition == true: this block reach
*/
Condition Value
// instruction list
Insts []Instruction
Phis []*Phi
// error catch
Handler *ErrorHandler
// for build
symbolTable map[string]Values
ScopeTable *Scope
finish bool // if emitJump finish!
}
func (b *BasicBlock) GetType() Type {
return nil
}
func (b *BasicBlock) SetType(ts Type) {
}
var (
_ Node = (*BasicBlock)(nil)
_ Value = (*BasicBlock)(nil)
)
// ========================================= Value ===============================
// ================================= Spec Value
// ----------- Phi
type Phi struct {
anValue
Edge []Value // edge[i] from phi.Block.Preds[i]
// what instruction create this control-flow merge?
// branch *Instruction // loop or if :
// for build
create bool // for ReadVariable method
wit1, wit2 Value // witness for trivial-phi
}
var (
_ Node = (*Phi)(nil)
_ Value = (*Phi)(nil)
_ User = (*Phi)(nil)
_ Instruction = (*Phi)(nil)
)
// ----------- externLib
type ExternLib struct {
anValue
table map[string]any
builder *FunctionBuilder
MemberMap map[string]Value
Member []Value
}
var (
_ Node = (*ExternLib)(nil)
_ Value = (*ExternLib)(nil)
_ User = (*ExternLib)(nil)
)
// ----------- Parameter
type Parameter struct {
anValue
IsFreeValue bool
defaultValue Value
FormalParameterIndex int
// if this flag set, this parameter will be set to member call,
// if pass a as parameter, it will be set to `a.Key` is parameter
IsMemberCall bool
MemberCallObjectIndex int
MemberCallKey Value
}
func (p *Parameter) GetDefault() Value {
return p.defaultValue
}
func (p *Parameter) SetDefault(v Value) {
p.defaultValue = v
}
var (
_ Node = (*Parameter)(nil)
_ Value = (*Parameter)(nil)
)
// ================================= Normal Value
// ----------- Const
// ConstInst also have block pointer, which block set this const to variable
type ConstInst struct {
*Const
anValue
Unary int
isIdentify bool // field key
Origin User
}
// ConstInst cont set Type
func (c *ConstInst) GetType() Type { return c.anValue.GetType() }
func (c *ConstInst) SetType(ts Type) { c.anValue.SetType(ts) }
var (
_ Node = (*ConstInst)(nil)
_ Value = (*ConstInst)(nil)
_ User = (*ConstInst)(nil)
_ Instruction = (*ConstInst)(nil)
)
// ----------- Undefined
// UndefinedKind : mark undefined value type
type UndefinedKind int
const (
// UndefinedValue normal undefined value
UndefinedValue UndefinedKind = iota
// UndefinedMemberInValid member call but not this key
UndefinedMemberInValid
// UndefinedMemberValid member call, has this key, but not this value, this shouldn't mark error
UndefinedMemberValid
)
type Undefined struct {
anValue
Kind UndefinedKind
}
func (u *Undefined) IsUndefined() bool { return true }
var (
_ Node = (*Undefined)(nil)
_ Value = (*Undefined)(nil)
_ Instruction = (*Undefined)(nil)
)
// ----------- BinOp
type BinOp struct {
anValue
Op BinaryOpcode
X, Y Value
}
var (
_ Value = (*BinOp)(nil)
_ User = (*BinOp)(nil)
_ Node = (*BinOp)(nil)
_ Instruction = (*BinOp)(nil)
)
type UnOp struct {
anValue
Op UnaryOpcode
X Value
}
var (
_ Value = (*UnOp)(nil)
_ User = (*UnOp)(nil)
_ Node = (*UnOp)(nil)
_ Instruction = (*UnOp)(nil)
)
// ================================= Function Call
// ----------- Call
// call instruction call method function with args as argument
type Call struct {
// call is a value
anValue
// for call function
Method Value
Args []Value
Binding map[string]Value
// go function
Async bool
Unpack bool
// caller
// caller Value
// ~ drop error
IsDropError bool
IsEllipsis bool
}
var (
_ Node = (*Call)(nil)
_ Value = (*Call)(nil)
_ User = (*Call)(nil)
_ Instruction = (*Call)(nil)
)
// ----------- SideEffect
type SideEffect struct {
anValue
CallSite *Call // call instruction
Value Value // modify to this value
}
var (
_ Node = (*SideEffect)(nil)
_ Value = (*SideEffect)(nil)
_ User = (*SideEffect)(nil)
_ Instruction = (*SideEffect)(nil)
)
// ----------- Return
// The Return instruction returns values and control back to the calling
// function.
type Return struct {
anValue
Results []Value
}
var (
_ Node = (*Return)(nil)
_ User = (*Return)(nil)
_ Value = (*Return)(nil)
_ Instruction = (*Return)(nil)
)
// ================================= Memory Value
// ----------- Make
type Make struct {
anValue
// when slice
low, high, step Value
parentI Value // parent interface
// when slice or map
Len, Cap Value
}
var (
_ Node = (*Make)(nil)
_ Value = (*Make)(nil)
_ User = (*Make)(nil)
_ Instruction = (*Make)(nil)
)
// ------------- Next
type Next struct {
anValue
Iter Value
InNext bool // "in" grammar
}
var (
_ Node = (*Next)(nil)
_ User = (*Next)(nil)
_ Value = (*Next)(nil)
_ Instruction = (*Next)(nil)
)
// ================================= Assert Value
// ----------- assert
type Assert struct {
anInstruction
Cond Value
Msg string
MsgValue Value
}
var (
_ Node = (*Assert)(nil)
_ User = (*Assert)(nil)
_ Instruction = (*Assert)(nil)
)
// ----------- Type-cast
// cast value -> type
type TypeCast struct {
anValue
Value Value
}
var (
_ Node = (*TypeCast)(nil)
_ Value = (*TypeCast)(nil)
_ User = (*TypeCast)(nil)
_ Instruction = (*TypeCast)(nil)
)
// ------------- type value
type TypeValue struct {
anValue
}
var (
_ Node = (*TypeValue)(nil)
_ Value = (*TypeValue)(nil)
_ Instruction = (*TypeValue)(nil)
)
// ================================= Error Handler
// ------------- ErrorHandler
type ErrorHandler struct {
anInstruction
try, catch, final, done *BasicBlock
}
var _ Instruction = (*ErrorHandler)(nil)
// -------------- PANIC
type Panic struct {
anValue
Info Value
}
var (
_ Node = (*Panic)(nil)
_ User = (*Panic)(nil)
_ Instruction = (*Panic)(nil)
)
// --------------- RECOVER
type Recover struct {
anValue
}
var (
_ Node = (*Recover)(nil)
_ Value = (*Recover)(nil)
_ Instruction = (*Recover)(nil)
)
// control-flow instructions ----------------------------------------
// jump / if / return / call / switch
// ----------- Jump
// The Jump instruction transfers control to the sole successor of its
// owning block.
//
// the block containing Jump instruction only have one successor block
type Jump struct {
anInstruction
To *BasicBlock
}
var _ Instruction = (*Jump)(nil)
// ----------- IF
// The If instruction transfers control to one of the two successors
// of its owning block, depending on the boolean Cond: the first if
// true, the second if false.
type If struct {
anInstruction
Cond Value
True *BasicBlock
False *BasicBlock
}
var (
_ Node = (*If)(nil)
_ User = (*If)(nil)
_ Instruction = (*If)(nil)
)
// ----------- For
// for loop
type Loop struct {
anInstruction
Body, Exit *BasicBlock
Init, Cond, Step Value
Key Value
}
var (
_ Node = (*Loop)(nil)
_ User = (*Loop)(nil)
_ Instruction = (*Loop)(nil)
)
// ----------- Switch
type SwitchLabel struct {
Value Value
Dest *BasicBlock
}
func NewSwitchLabel(v Value, dest *BasicBlock) SwitchLabel {
return SwitchLabel{
Value: v,
Dest: dest,
}
}
type Switch struct {
anInstruction
Cond Value
DefaultBlock *BasicBlock
Label []SwitchLabel
}
var (
_ Node = (*Switch)(nil)
_ User = (*Switch)(nil)
_ Instruction = (*Switch)(nil)
)