-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasm.lua
411 lines (362 loc) · 12.3 KB
/
asm.lua
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
local band, lshift = band, lshift
local InstructionTypes = { }
local sbxBias = 131071 -- (2^18 - 1) >> 1
function InstructionTypes.ABC(opcode, a, b, c)
a = lshift(a, 6)
b = lshift(b, 23)
c = lshift(c, 14)
return band(opcode + a + b + c, 2^32 - 1)
end
function InstructionTypes.ABx(opcode, a, bx)
a = lshift(a, 6)
bx = lshift(bx, 14)
return band(opcode + a + bx, 2^32 - 1)
end
function InstructionTypes.AsBx(opcode, a, sbx)
a = lshift(a, 6)
sbx = sbx + sbxBias
sbx = lshift(sbx, 14)
return band(opcode + a + sbx, 2^32 - 1)
end
function InstructionTypes.AB(opcode, a, b)
return InstructionTypes.ABC(opcode, a, b, 0)
end
function InstructionTypes.AC(opcode, a, c)
return InstructionTypes.ABC(opcode, a, 0, c)
end
function InstructionTypes.A(opcode, a)
return InstructionTypes.ABC(opcode, a, 0, 0)
end
function InstructionTypes.sBx(opcode, sbx)
return InstructionTypes.AsBx(opcode, 0, sbx)
end
local Op = { }
Op.MOVE = {opcode = 0, type = InstructionTypes.AB }
Op.LOADK = {opcode = 1, type = InstructionTypes.ABx }
Op.LOADBOOL = {opcode = 2, type = InstructionTypes.ABC }
Op.LOADNIL = {opcode = 3, type = InstructionTypes.AB }
Op.GETUPVAL = {opcode = 4, type = InstructionTypes.AB }
Op.GETGLOBAL = {opcode = 5, type = InstructionTypes.ABx }
Op.GETTABLE = {opcode = 6, type = InstructionTypes.ABC }
Op.SETGLOBAL = {opcode = 7, type = InstructionTypes.ABx }
Op.SETUPVAL = {opcode = 8, type = InstructionTypes.AB }
Op.SETTABLE = {opcode = 9, type = InstructionTypes.ABC }
Op.NEWTABLE = {opcode = 10, type = InstructionTypes.ABC }
Op.SELF = {opcode = 11, type = InstructionTypes.ABC }
Op.ADD = {opcode = 12, type = InstructionTypes.ABC }
Op.SUB = {opcode = 13, type = InstructionTypes.ABC }
Op.MUL = {opcode = 14, type = InstructionTypes.ABC }
Op.DIV = {opcode = 15, type = InstructionTypes.ABC }
Op.MOD = {opcode = 16, type = InstructionTypes.ABC }
Op.POW = {opcode = 17, type = InstructionTypes.ABC }
Op.UNM = {opcode = 18, type = InstructionTypes.AB }
Op.NOT = {opcode = 19, type = InstructionTypes.AB }
Op.LEN = {opcode = 20, type = InstructionTypes.AB }
Op.CONCAT = {opcode = 21, type = InstructionTypes.ABC }
Op.JMP = {opcode = 22, type = InstructionTypes.sBx }
Op.EQ = {opcode = 23, type = InstructionTypes.ABC }
Op.LT = {opcode = 24, type = InstructionTypes.ABC }
Op.LE = {opcode = 25, type = InstructionTypes.ABC }
Op.TEST = {opcode = 26, type = InstructionTypes.AC }
Op.TESTSET = {opcode = 27, type = InstructionTypes.ABC }
Op.CALL = {opcode = 28, type = InstructionTypes.ABC }
Op.TAILCALL = {opcode = 29, type = InstructionTypes.ABC }
Op.RETURN = {opcode = 30, type = InstructionTypes.AB }
Op.FORLOOP = {opcode = 31, type = InstructionTypes.AsBx}
Op.FORPREP = {opcode = 32, type = InstructionTypes.AsBx}
Op.TFORLOOP = {opcode = 33, type = InstructionTypes.AC }
Op.SETLIST = {opcode = 34, type = InstructionTypes.ABC }
Op.CLOSE = {opcode = 35, type = InstructionTypes.A }
Op.CLOSURE = {opcode = 36, type = InstructionTypes.ABx }
Op.VARARG = {opcode = 37, type = InstructionTypes.AB }
for k,v in pairs(Op) do
v.name = k
end
function makeChunkStream(numParams)
local stream = { }
local lastIndex = 0
local constants = { }
local sourceLinePositions = {}
local nilIndex = nil
local instns = { }
local register = numParams - 1
local maxRegister = register -- just tracking the highest we go
local debugComments = { }
local debugAnnotations = { }
local debugCode = { }
function stream.getMaxRegister()
return maxRegister
end
function stream.comment(comment)
debugComments[#instns + 1] = debugComments[#instns + 1] or {}
table.insert(debugComments[#instns + 1], comment)
end
function stream.annotate(annot)
debugAnnotations[#instns + 1] = debugAnnotations[#instns + 1] or {}
table.insert(debugAnnotations[#instns + 1], annot)
end
function stream.getConstant(value)
local index
if value == nil then
if not nilIndex then
nilIndex = lastIndex
lastIndex = lastIndex + 1
end
index = nilIndex
else
index = constants[value]
if not index then
constants[value] = lastIndex
index = constants[value]
lastIndex = lastIndex + 1
end
end
return index
end
function stream.allocNilRK()
local constant = stream.getConstant(nil)
local rk
if constant > 255 then
rk = stream.alloc()
stream.LOADK(rk, constant)
else
rk = bor(256, constant)
end
return rk
end
function stream.allocRK(value, ...)
if value == nil then
return
end
local constant = stream.getConstant(value)
local rk
if constant > 255 then
rk = stream.alloc()
stream.LOADK(rk, constant)
else
rk = bor(256, constant)
end
return rk, stream.allocRK(...)
end
function stream.freeRK(k, ...)
if k == nil then
return
end
if k < 256 then
stream.free()
end
stream.freeRK(...)
end
function stream.emit(op, ...)
local ok, inst = pcall(op.type, op.opcode, ...)
assert(ok, inst, 2)
table.insert(instns, inst)
table.insert(debugCode, "[" .. (#instns) .. "] " .. op.name .. " " .. table.concat({...}, " "))
sourceLinePositions[#instns] = #instns
return #instns
end
function stream.startJump()
table.insert(instns, 0)
table.insert(debugCode, "")
sourceLinePositions[#instns] = #instns
return {instruction = #instns}
end
function stream.startBackwardJump(startInstruction)
return {instruction = startInstruction or #instns, backward = true}
end
function stream.fixJump(jump)
if not jump.backward then
local jumpID = jump.instruction
instns[jumpID] = Op.JMP.type(Op.JMP.opcode, #instns - jumpID)
debugCode[jumpID] = "[" .. (jumpID) .. "] JMP " .. (#instns - jumpID)
else
stream.JMP(jump.instruction - (#instns + 1))
end
end
function stream.getInstructionCount()
return #instns
end
function stream.alloc(n)
n = n or 1
local ret = { }
for i = 1, n do
register = register + 1
maxRegister = math.max(register, maxRegister)
ret[i] = register
stream.createPool(register)
end
return unpack(ret)
end
function stream.free(n)
n = n or 1
local ret = { }
for i = n, 1, -1 do
ret[i] = register
stream.removeFromPool(register)
register = register - 1
end
return unpack(ret)
end
function stream.alignToRegister(n)
n = n or 0
if n <= register then
return stream.free(register - n)
else
return stream.alloc(n - register)
end
end
function stream.peek(n)
return register - n
end
for k,op in pairs(Op) do
stream[k] = function(...)
return stream.emit(op, ...)
end
end
-- value pools are lists of registers known to share the same value
local valuePools = { }
function stream.findPool(reg)
for poolIndex,pool in ipairs(valuePools) do
for registerIndex,r in ipairs(pool) do
if r == reg then
return pool, registerIndex, poolIndex
end
end
end
end
function stream.getPool(reg)
local pool, registerIndex, poolIndex = stream.findPool(reg)
if not pool then
pool, registerIndex, poolIndex = stream.createPool(reg)
end
return pool, registerIndex, poolIndex
end
function stream.removeFromPool(reg)
local pool, registerIndex, poolIndex = stream.findPool(reg)
if pool then
table.remove(pool, registerIndex)
if #pool == 0 then
table.remove(valuePools, poolIndex)
end
end
end
function stream.createPool(reg)
stream.removeFromPool(reg)
local pool = {reg}
table.insert(valuePools, pool)
return pool, 1, #valuePools
end
function stream.addToPool(add, to)
local toPool = stream.getPool(to)
if not toPool then
toPool = stream.createPool(to)
end
local addPool = stream.getPool(add)
if addPool and addPool ~= toPool then
stream.removeFromPool(add)
end
if addPool ~= toPool then
table.insert(toPool, add)
end
return toPool
end
function stream.clearValuePools()
valuePools = { }
end
-- overwrite ops
local assigners = {
"LOADK",
"LOADBOOL",
"GETUPVAL",
"GETGLOBAL",
"GETTABLE",
"NEWTABLE",
"ADD",
"SUB",
"MUL",
"DIV",
"MOD",
"POW",
"UNM",
"NOT",
"LEN",
"CONCAT"
}
for i,opName in ipairs(assigners) do
local old = stream[opName]
stream[opName] = function(rAssignTo, ...)
stream.createPool(rAssignTo)
return old(rAssignTo, ...)
end
end
local oldMove = stream.MOVE
function stream.MOVE(a, b)
if a ~= b then
stream.removeFromPool(a)
stream.addToPool(a, b)
end
return oldMove(a, b)
end
local oldLoadnil = stream.LOADNIL
function stream.LOADNIL(a, b)
for r=a,b do
stream.createPool(r)
end
return oldLoadnil(a, b)
end
local oldCall = stream.CALL
function stream.CALL(a, b, c)
local numArgs = b == 0 and stream.getMaxRegister() - a or b - 1
local numReturns = c == 0 and stream.getMaxRegister() - a or c - 1
for r=a, a + math.max(numArgs, numReturns) do
stream.createPool(r)
end
return oldCall(a, b, c)
end
local oldClose = stream.CLOSE
function stream.CLOSE(a)
for i=a,stream.getMaxRegister() do
stream.createPool(i)
end
return oldClose(a)
end
function stream.compile(platform, name)
local dump = makeDumpster(platform)
dump.dumpString(name) -- source name
dump.dumpInteger(1) -- line defined
dump.dumpInteger(#instns) -- last line defined
dump.dumpByte(0) -- number of upvalues
dump.dumpByte(numParams) -- number of parameters
dump.dumpByte(0) -- is vararg
dump.dumpByte(math.max(2, maxRegister + 1)) -- max stack size
dump.dumpInstructionsList(instns) -- instructions
dump.dumpConstantsList(constants, nilIndex) -- constants
dump.dumpInteger(0) -- empty function prototype list
dump.dumpSourceLinePositions(sourceLinePositions) -- line numbers
dump.dumpInteger(0) -- empty list of locals
dump.dumpInteger(0) -- empty list of upvalues
return dump.toString()
end
function stream.getDebugCode()
local code = ""
for i,v in ipairs(debugCode) do
if debugAnnotations[i] then
code = code .. "\n" .. table.concat(debugAnnotations[i], "\n") .. "\n"
end
code = code .. (" "):rep(4) .. v
if debugComments[i] then
code = code .. (" "):rep(25 - #v) .. ";" .. table.concat(debugComments[i], "\n" .. (" "):rep(29) .. ";")
end
code = code .. "\n"
end
return code
end
-- utility functions
function stream.asmLoadk(r, k)
stream.LOADK(r, stream.getConstant(k))
end
function stream.asmClose()
stream.CLOSE(stream.peek(0) + 1)
end
return stream
end