-
Notifications
You must be signed in to change notification settings - Fork 171
/
call_frame.go
287 lines (231 loc) · 5.74 KB
/
call_frame.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
package vm
import (
"sync"
)
type callFrameStack struct {
callFrames []callFrame
pointer int
}
type baseFrame struct {
// environment pointer, points to the call frame we want to get locals from
ep *normalCallFrame
self Object
locals []*Pointer
// local pointer
lPr int
isBlock bool
isSourceBlock bool
// for helping stop the frame execution
isRemoved bool
blockFrame *normalCallFrame
sync.RWMutex
sourceLine int
fileName string
}
type callFrame interface {
// Getters
Self() Object
BlockFrame() *normalCallFrame
IsBlock() bool
IsSourceBlock() bool
IsRemoved() bool
setAsRemoved()
EP() *normalCallFrame
Locals() []*Pointer
LocalPtr() int
SourceLine() int
FileName() string
getLCL(index, depth int) *Pointer
insertLCL(index, depth int, value Object)
storeConstant(constName string, constant interface{}) *Pointer
lookupConstantUnderAllScope(constName string) *Pointer
lookupConstantUnderCurrentScope(constName string) *Pointer
lookupConstantInCurrentScope(constName string) *Pointer
inspect() string
stopExecution()
}
type goMethodCallFrame struct {
*baseFrame
method builtinMethodBody
argPtr int
argCount int
receiver Object
name string
}
func (cf *goMethodCallFrame) stopExecution() {}
type normalCallFrame struct {
*baseFrame
instructionSet *instructionSet
// program counter
pc int
}
func (n *normalCallFrame) instructionsCount() int {
return len(n.instructionSet.instructions)
}
func (n *normalCallFrame) stopExecution() {
n.pc = n.instructionsCount()
}
func (b *baseFrame) Self() Object {
return b.self
}
func (b *baseFrame) BlockFrame() *normalCallFrame {
return b.blockFrame
}
func (b *baseFrame) IsBlock() bool {
return b.isBlock
}
func (b *baseFrame) IsRemoved() bool {
return b.isRemoved
}
func (b *baseFrame) setAsRemoved() {
b.isRemoved = true
}
func (b *baseFrame) IsSourceBlock() bool {
return b.isSourceBlock
}
func (b *baseFrame) EP() *normalCallFrame {
return b.ep
}
func (b *baseFrame) Locals() []*Pointer {
return b.locals
}
func (b *baseFrame) LocalPtr() int {
return b.lPr
}
func (b *baseFrame) SourceLine() int {
return b.sourceLine
}
func (b *baseFrame) FileName() string {
return b.fileName
}
// We use lock on every local variable retrieval and insertion.
// The main scenario is when multiple threads want to access local variables outside it's block
// Since they share same block frame, they will all access to that frame's locals.
// TODO: Find a better way to fix this, or prevent thread from accessing outside locals.
func (b *baseFrame) getLCL(index, depth int) (p *Pointer) {
if depth == 0 {
b.RLock()
p = b.locals[index]
b.RUnlock()
return
}
return b.blockFrame.ep.getLCL(index, depth-1)
}
func (b *baseFrame) insertLCL(index, depth int, value Object) {
existedLCL := b.getLCL(index, depth)
if existedLCL != nil {
existedLCL.Target = value
return
}
b.Lock()
if index >= len(b.locals) {
b.locals = append(b.locals, nil)
copy(b.locals[index:], b.locals[index:])
}
b.locals[index] = &Pointer{Target: value}
if index >= b.lPr {
b.lPr = index + 1
}
b.Unlock()
}
func (b *baseFrame) storeConstant(constName string, constant interface{}) *Pointer {
var ptr *Pointer
switch c := constant.(type) {
case *Pointer:
ptr = c
case Object:
ptr = &Pointer{Target: c}
}
switch scope := b.self.(type) {
case *RClass:
scope.constants[constName] = ptr
if class, ok := ptr.Target.(*RClass); ok {
class.scope = scope
}
default:
c := b.self.Class()
c.constants[constName] = ptr
}
return ptr
}
func (b *baseFrame) lookupConstantUnderAllScope(constName string) *Pointer {
var c *Pointer
switch scope := b.self.(type) {
case *RClass:
c = scope.lookupConstantUnderAllScope(constName)
default:
scopeClass := scope.Class()
c = scopeClass.lookupConstantUnderAllScope(constName)
}
return c
}
func (b *baseFrame) lookupConstantUnderCurrentScope(constName string) *Pointer {
var c *Pointer
switch scope := b.self.(type) {
case *RClass:
c = scope.lookupConstantUnderCurrentScope(constName)
default:
scopeClass := scope.Class()
c = scopeClass.lookupConstantUnderCurrentScope(constName)
}
return c
}
func (b *baseFrame) lookupConstantInCurrentScope(constName string) *Pointer {
var c *Pointer
switch scope := b.self.(type) {
case *RClass:
c = scope.lookupConstantInCurrentScope(constName)
default:
scopeClass := scope.Class()
c = scopeClass.lookupConstantInCurrentScope(constName)
}
return c
}
func (cfs *callFrameStack) push(cf callFrame) {
if cf == nil {
panic("Callframe can't be nil!")
}
if len(cfs.callFrames) <= cfs.pointer {
cfs.callFrames = append(cfs.callFrames, cf)
} else {
cfs.callFrames[cfs.pointer] = cf
}
cfs.pointer++
}
func (cfs *callFrameStack) pop() callFrame {
var cf callFrame
if len(cfs.callFrames) < 1 {
panic("Nothing to pop!")
}
if cfs.pointer > 0 {
cfs.pointer--
}
cf = cfs.callFrames[cfs.pointer]
cfs.callFrames[cfs.pointer] = nil
return cf
}
func (cfs *callFrameStack) top() callFrame {
if cfs.pointer > 0 {
return cfs.callFrames[cfs.pointer-1]
}
return nil
}
func newNormalCallFrame(is *instructionSet, filename string, sourceLine int) *normalCallFrame {
return &normalCallFrame{baseFrame: &baseFrame{locals: make([]*Pointer, 5), lPr: 0, fileName: filename, sourceLine: sourceLine}, instructionSet: is, pc: 0}
}
func newGoMethodCallFrame(m builtinMethodBody, receiver Object, argCount, argPtr int, n, filename string, sourceLine int, blockFrame *normalCallFrame) *goMethodCallFrame {
return &goMethodCallFrame{
baseFrame: &baseFrame{
locals: make([]*Pointer, 5),
lPr: 0,
fileName: filename,
sourceLine: sourceLine,
blockFrame: blockFrame,
},
method: m,
name: n,
receiver: receiver,
argCount: argCount,
argPtr: argPtr,
}
}