-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyframe.go
241 lines (225 loc) · 5.88 KB
/
pyframe.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
package vm
// Init in init.go
var callStack *PyStack
var Globals map[string]PyObject
type PyFrame struct {
code *PyCode
PC int
locals map[string]PyObject
globals map[string]PyObject
cellvars map[string]PyObject
consts []PyObject
opStack *PyStack
blockStack *IntStack
}
func NewPyFrame(code *PyCode, args []PyObject, globals map[string]PyObject, consts []PyObject, cellvars map[string]PyObject) *PyFrame {
ret := new(PyFrame)
ret.code, ret.globals, ret.consts, ret.cellvars = code, globals, consts, cellvars
varNames := code.getLocals()
ret.blockStack, ret.opStack = NewIntStack(), NewPyStack()
// Bind the parameters' names to the arguments
argsP := len(args) - 1
for i := 0; i < len(args); i++ {
ret.locals[varNames[i]] = args[argsP]
argsP--
}
// Bind the cellvars to their names
for i := 0; i < len(code.getCellvars()); i++ {
name := code.getCellvars()[i]
if _, ok := ret.locals[name]; ok {
ret.cellvars[name] = ret.locals[name]
}
}
return ret
}
func (this *PyFrame) getPC() int {
return this.PC
}
func (this *PyFrame) getCode() *PyCode {
return this.code
}
func (this *PyFrame) getCellName(idx int) string {
var ret string
if idx < len(this.code.getCellvars()) {
ret = this.code.getCellvars()[idx]
} else {
ret = this.code.getFreevars()[idx-len(this.code.getCellvars())]
}
return ret
}
func (this *PyFrame) execute() PyObject {
this.PC = 0
pushFrame(this)
for {
nextInstr := this.code.getInstructions()[this.PC]
this.PC++
opcode := nextInstr.opcode
operand := nextInstr.operand
// TODO
// python exception handler
switch opcode {
case LOAD_FAST:
u := this.locals[this.code.getLocals()[operand]]
this.opStack.push(u)
case LOAD_CONST:
u := this.consts[operand]
this.opStack.push(u)
case LOAD_GLOBAL:
u := this.globals[this.code.getGlobals()[operand]]
this.opStack.push(u)
case STORE_FAST:
u := this.opStack.pop()
this.locals[this.code.getLocals()[operand]] = u
case POP_TOP:
this.opStack.pop()
case COMPARE_OP:
v := this.opStack.pop()
u := this.opStack.pop()
args := make([]PyObject, 0)
args = append(args, v)
r := CallAttr(u, cmpOp[operand], args)
this.opStack.push(r)
case SETUP_LOOP:
this.blockStack.push(operand)
case BREAK_LOOP:
this.PC = this.blockStack.pop()
case POP_BLOCK:
this.blockStack.pop()
case JUMP_FORWARD:
// This instruction is the same as JUMP_ABSOLUTE in this VM
this.PC = operand
case JUMP_ABSOLUTE:
this.PC = operand
case POP_JUMP_IF_TRUE:
u := this.opStack.pop().(*PyBool)
if u.getVal() {
this.PC = operand
}
case POP_JUMP_IF_FALSE:
u := this.opStack.pop().(*PyBool)
if !u.getVal() {
this.PC = operand
}
case RETURN_VALUE:
u := this.opStack.pop()
popFrame()
return u
case INPLACE_ADD:
fallthrough
// In fact INPLACE calculate will not pop out the second topest item
case BINARY_ADD:
v := this.opStack.pop()
u := this.opStack.pop()
args := make([]PyObject, 0)
args = append(args, v)
r := CallAttr(u, "__add__", args)
this.opStack.push(r)
case BINARY_SUBTRACT:
v := this.opStack.pop()
u := this.opStack.pop()
args := make([]PyObject, 0)
args = append(args, v)
r := CallAttr(u, "__sub__", args)
this.opStack.push(r)
case BINARY_MULTIPLY:
v := this.opStack.pop()
u := this.opStack.pop()
args := make([]PyObject, 0)
args = append(args, v)
r := CallAttr(u, "__mul__", args)
this.opStack.push(r)
case BINARY_FLOOR_DIVIDE:
v := this.opStack.pop()
u := this.opStack.pop()
args := make([]PyObject, 0)
args = append(args, v)
r := CallAttr(u, "__floordiv__", args)
this.opStack.push(r)
case ROT_TWO:
v := this.opStack.pop()
u := this.opStack.pop()
this.opStack.push(v)
this.opStack.push(u)
case CALL_FUNCTION:
args := make([]PyObject, 0)
// the parameters
for i := 0; i < operand; i++ {
args = append(args, this.opStack.pop())
}
// the function
f := this.opStack.pop()
r := CallAttr(f, "__call__", args)
this.opStack.push(r)
case LOAD_ATTR:
u := this.opStack.pop()
v := NewPyAttr(u, this.code.getGlobals()[operand])
this.opStack.push(v)
case BINARY_SUBSCR:
u := this.opStack.pop()
v := this.opStack.pop()
args := make([]PyObject, 0)
args = append(args, u)
r := CallAttr(v, "__getitem__", args)
this.opStack.push(r)
case STORE_SUBSCR:
u := this.opStack.pop()
v := this.opStack.pop()
w := this.opStack.pop()
args := []PyObject{u, w}
// The returned value must be None
_ = CallAttr(v, "__setitem__", args)
case LOAD_CLOSURE:
name := this.getCellName(operand)
this.opStack.push(this.cellvars[name])
case BUILD_TUPLE:
args := make([]PyObject, operand)
for i := 0; i < operand; i++ {
args[operand-i-1] = this.opStack.pop()
}
this.opStack.push(NewPyTuple(args))
case SELECT_TUPLE:
tuple := this.opStack.pop().(*PyTuple)
for i := tuple.size() - 1; i >= 0; i-- {
this.opStack.push(tuple.getItem(i))
}
case BUILD_LIST:
args := make([]PyObject, operand)
for i := 0; i < operand; i++ {
args[operand-i-1] = this.opStack.pop()
}
this.opStack.push(NewPyList(args))
case MAKE_CLOSURE:
u := this.opStack.pop()
v := this.opStack.pop()
f := NewPyFunction(u.(*PyCode), this.globals, v)
this.opStack.push(f)
case MAKE_FUNCTION:
u := this.opStack.pop()
f := NewPyFunction(u.(*PyCode), this.globals, nil)
this.opStack.push(f)
case DUP_TOP:
this.opStack.push(this.opStack.top())
case DELETE_FAST:
delete(this.locals, this.code.getLocals()[operand])
default:
panic("UnImplemented instruction.")
}
}
}
// For implement PyObject
func (this *PyFrame) getType() *PyType {
return nil
}
// For implement PyObject
func (this *PyFrame) toString() string {
return ""
}
func pushFrame(frame *PyFrame) {
callStack.push(frame)
if callStack.size() > 1024 {
panic("callStack overflow.")
}
}
func popFrame() {
callStack.pop()
}