-
Notifications
You must be signed in to change notification settings - Fork 2
/
opcode.go
370 lines (288 loc) · 7.89 KB
/
opcode.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
/*
Copyright 2016-2017 by Milo Christiansen
This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use of
this software.
Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product, an
acknowledgment in the product documentation would be appreciated but is not
required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
package lua
import "fmt"
// I want to be able to load binaries written by the standard Lua compiler, so I may as well
// use the standard opcode format.
//
// From a performance view point it would probably be better to waste some memory and use
// structures instead of hoping the go compiler will inline all this bit twiddling...
type opCode uint
const (
opMove opCode = iota
opLoadK
opLoadKEx
opLoadBool
opLoadNil
opGetUpValue
opGetTableUp
opGetTable
opSetTableUp
opSetUpValue
opSetTable
opNewTable
opSelf
// These exported values are used in calls to Arith
OpAdd
OpSub
OpMul
OpMod
OpPow
OpDiv
OpIDiv
OpBinAND
OpBinOR
OpBinXOR
OpBinShiftL
OpBinShiftR
OpUMinus
OpBinNot
opNot
opLength
opConcat
// These exported values are used in calls to Compare
opJump
OpEqual
OpLessThan
OpLessOrEqual
opTest
opTestSet
opCall
opTailCall
opReturn
opForLoop
opForPrep
opTForCall
opTForLoop
opSetList
opClosure
opVarArg
opExtraArg
opCodeCount int = iota
)
var opNames = []string{
"MOVE",
"LOADK",
"LOADKX",
"LOADBOOL",
"LOADNIL",
"GETUPVAL",
"GETTABUP",
"GETTABLE",
"SETTABUP",
"SETUPVAL",
"SETTABLE",
"NEWTABLE",
"SELF",
"ADD",
"SUB",
"MUL",
"MOD",
"POW",
"DIV",
"IDIV",
"BAND",
"BOR",
"BXOR",
"SHL",
"SHR",
"UNM",
"BNOT",
"NOT",
"LEN",
"CONCAT",
"JMP",
"EQ",
"LT",
"LE",
"TEST",
"TESTSET",
"CALL",
"TAILCALL",
"RETURN",
"FORLOOP",
"FORPREP",
"TFORCALL",
"TFORLOOP",
"SETLIST",
"CLOSURE",
"VARARG",
"EXTRAARG",
}
const (
sizeC = 9
sizeB = 9
sizeBx = sizeC + sizeB
sizeA = 8
sizeAx = sizeC + sizeB + sizeA
sizeOp = 6
posOp = 0
posA = posOp + sizeOp
posC = posA + sizeA
posB = posC + sizeC
posBx = posC
posAx = posA
bitRK = 1 << (sizeB - 1)
maxIndexRK = bitRK - 1
maxArgAx = 1<<sizeAx - 1
maxArgBx = 1<<sizeBx - 1
maxArgSBx = maxArgBx >> 1
maxArgA = 1<<sizeA - 1
maxArgB = 1<<sizeB - 1
maxArgC = 1<<sizeC - 1
fieldsPerFlush = 50
)
func isK(x int) bool { return 0 != x&bitRK }
func indexK(r int) int { return r & ^bitRK }
func rkAsK(r int) int { return r | bitRK }
type instruction uint32
// creates a mask with 'n' 1 bits at position 'p'
func mask1(n, p uint) instruction { return ^(^instruction(0) << n) << p }
// creates a mask with 'n' 0 bits at position 'p'
func mask0(n, p uint) instruction { return ^mask1(n, p) }
func (i instruction) getOpCode() opCode {
return opCode(i >> posOp & (1<<sizeOp - 1))
}
func (i *instruction) setOpCode(op opCode) {
i.setArg(posOp, sizeOp, int(op))
}
func (i instruction) getArg(pos, size uint) int {
return int(i >> pos & mask1(size, 0))
}
func (i *instruction) setArg(pos, size uint, arg int) {
*i = *i&mask0(size, pos) | instruction(arg)<<pos&mask1(size, pos)
}
// Inline for performance.
func (i instruction) a() int { return int(i >> posA & maxArgA) }
func (i instruction) b() int { return int(i >> posB & maxArgB) }
func (i instruction) c() int { return int(i >> posC & maxArgC) }
func (i instruction) bx() int { return int(i >> posBx & maxArgBx) }
func (i instruction) ax() int { return int(i >> posAx & maxArgAx) }
func (i instruction) sbx() int { return int(i>>posBx&maxArgBx) - maxArgSBx }
func (i *instruction) setA(arg int) { i.setArg(posA, sizeA, arg) }
func (i *instruction) setB(arg int) { i.setArg(posB, sizeB, arg) }
func (i *instruction) setC(arg int) { i.setArg(posC, sizeC, arg) }
func (i *instruction) setBx(arg int) { i.setArg(posBx, sizeBx, arg) }
func (i *instruction) setAx(arg int) { i.setArg(posAx, sizeAx, arg) }
func (i *instruction) setSBx(arg int) { i.setArg(posBx, sizeBx, arg+maxArgSBx) }
func createABC(op opCode, a, b, c int) instruction {
return instruction(op)<<posOp | instruction(a)<<posA | instruction(b)<<posB | instruction(c)<<posC
}
func createABx(op opCode, a, bx int) instruction {
return instruction(op)<<posOp | instruction(a)<<posA | instruction(bx)<<posBx
}
func createAsBx(op opCode, a, sbx int) instruction {
return instruction(op)<<posOp | instruction(a)<<posA | instruction(sbx+maxArgSBx)<<posBx
}
func createAx(op opCode, a int) instruction {
return instruction(op)<<posOp | instruction(a)<<posAx
}
type opType struct {
a, ax, b, bx, sbx, c int8 // 0: unused, 1: used, 2: used RK, 3: used float 8
}
var opModes = []opType{
// a, ax, b, bx, sbx, c opCode
opType{1, 0, 1, 0, 0, 0}, // opMove
opType{1, 0, 0, 1, 0, 0}, // opLoadK
opType{1, 0, 0, 0, 0, 0}, // opLoadKEx
opType{1, 0, 1, 0, 0, 1}, // opLoadBool
opType{1, 0, 1, 0, 0, 0}, // opLoadNil
opType{1, 0, 1, 0, 0, 0}, // opGetUpValue
opType{1, 0, 1, 0, 0, 2}, // opGetTableUp
opType{1, 0, 1, 0, 0, 2}, // opGetTable
opType{1, 0, 2, 0, 0, 2}, // opSetTableUp
opType{1, 0, 1, 0, 0, 0}, // opSetUpValue
opType{1, 0, 2, 0, 0, 2}, // opSetTable
opType{1, 0, 3, 0, 0, 3}, // opNewTable
opType{1, 0, 1, 0, 0, 2}, // opSelf
opType{1, 0, 2, 0, 0, 2}, // opAdd
opType{1, 0, 2, 0, 0, 2}, // opSub
opType{1, 0, 2, 0, 0, 2}, // opMul
opType{1, 0, 2, 0, 0, 2}, // opMod
opType{1, 0, 2, 0, 0, 2}, // opPow
opType{1, 0, 2, 0, 0, 2}, // opDiv
opType{1, 0, 2, 0, 0, 2}, // opIDiv
opType{1, 0, 2, 0, 0, 2}, // opBinAND
opType{1, 0, 2, 0, 0, 2}, // opBinOR
opType{1, 0, 2, 0, 0, 2}, // opBinXOR
opType{1, 0, 2, 0, 0, 2}, // opBinShiftL
opType{1, 0, 2, 0, 0, 2}, // opBinShiftR
opType{1, 0, 2, 0, 0, 0}, // opUMinus
opType{1, 0, 2, 0, 0, 0}, // opBinNot
opType{1, 0, 2, 0, 0, 0}, // opNot
opType{1, 0, 1, 0, 0, 0}, // opLength
opType{1, 0, 1, 0, 0, 1}, // opConcat
opType{1, 0, 0, 0, 1, 0}, // opJump
opType{1, 0, 2, 0, 0, 2}, // opEqual
opType{1, 0, 2, 0, 0, 2}, // opLessThan
opType{1, 0, 2, 0, 0, 2}, // opLessOrEqual
opType{1, 0, 0, 0, 0, 1}, // opTest
opType{1, 0, 1, 0, 0, 1}, // opTestSet
opType{1, 0, 1, 0, 0, 1}, // opCall
opType{1, 0, 1, 0, 0, 0}, // opTailCall
opType{1, 0, 1, 0, 0, 0}, // opReturn
opType{1, 0, 0, 0, 1, 0}, // opForLoop
opType{1, 0, 0, 0, 1, 0}, // opForPrep
opType{1, 0, 0, 0, 0, 1}, // opTForCall
opType{1, 0, 0, 0, 1, 0}, // opTForLoop
opType{1, 0, 1, 0, 0, 1}, // opSetList
opType{1, 0, 0, 1, 0, 0}, // opClosure
opType{1, 0, 1, 0, 0, 0}, // opVarArg
opType{0, 1, 0, 0, 0, 0}, // opExtraArg
}
func (i instruction) String() string {
op := i.getOpCode()
out := opNames[op]
mode := opModes[op]
if mode.a != 0 {
out = fmt.Sprintf("%s\tA:%d", out, i.a())
}
if mode.ax != 0 {
out = fmt.Sprintf("%s\tAX:%d", out, i.ax())
}
switch mode.b {
case 1:
out = fmt.Sprintf("%s\tB:%d", out, i.b())
case 2:
if isK(i.b()) {
out = fmt.Sprintf("%s\tB:k(%d)", out, indexK(i.b()))
} else {
out = fmt.Sprintf("%s\tB:r(%d)", out, i.b())
}
case 3:
out = fmt.Sprintf("%s\tB:float8(%d)", out, intFromFloat8(float8(i.b())))
}
if mode.bx != 0 {
out = fmt.Sprintf("%s\tBX:%d", out, i.bx())
}
if mode.sbx != 0 {
out = fmt.Sprintf("%s\tSBX:%d", out, i.sbx())
}
switch mode.c {
case 1:
out = fmt.Sprintf("%s\tC:%d", out, i.c())
case 2:
if isK(i.c()) {
out = fmt.Sprintf("%s\tC:k(%d)", out, indexK(i.c()))
} else {
out = fmt.Sprintf("%s\tC:r(%d)", out, i.c())
}
case 3:
out = fmt.Sprintf("%s\tC:float8(%d)", out, intFromFloat8(float8(i.c())))
}
return out
}