1 -------------------------------------------------------------------------------
2 -- Copyright (c) 2005-2013 Kein-Hong Man, Fabien Fleutot and others.
4 -- All rights reserved.
6 -- This program and the accompanying materials are made available
7 -- under the terms of the Eclipse Public License v1.0 which
8 -- accompanies this distribution, and is available at
9 -- http://www.eclipse.org/legal/epl-v10.html
11 -- This program and the accompanying materials are also made available
12 -- under the terms of the MIT public license which accompanies this
13 -- distribution, and is available at http://www.lua.org/license.html
16 -- Kein-Hong Man - Initial implementation for Lua 5.0, part of Yueliang
17 -- Fabien Fleutot - Port to Lua 5.1, integration with Metalua
19 -------------------------------------------------------------------------------
21 --[[--------------------------------------------------------------------
26 Lua 5 virtual machine opcodes in Lua
27 This file is part of Yueliang.
29 Copyright (c) 2005 Kein-Hong Man <khman@users.sf.net>
30 The COPYRIGHT file describes the conditions
31 under which this software may be distributed.
33 See the ChangeLog for more information.
35 ------------------------------------------------------------------------
37 [FF] Slightly modified, mainly to produce Lua 5.1 bytecode.
39 ----------------------------------------------------------------------]]
41 --[[--------------------------------------------------------------------
43 -- * an Instruction is a table with OP, A, B, C, Bx elements; this
44 -- should allow instruction handling to work with doubles and ints
46 -- luaP:Instruction(i): convert field elements to a 4-char string
47 -- luaP:DecodeInst(x): convert 4-char string into field elements
48 -- * WARNING luaP:Instruction outputs instructions encoded in little-
49 -- endian form and field size and positions are hard-coded
50 ----------------------------------------------------------------------]]
52 local function debugf() end
57 ===========================================================================
58 We assume that instructions are unsigned numbers.
59 All instructions have an opcode in the first 6 bits.
60 Instructions can have the following fields:
64 'Bx' : 18 bits ('B' and 'C' together)
67 A signed argument is represented in excess K; that is, the number
68 value is the unsigned value minus K. K is exactly the maximum value
69 for that argument (so that -max is represented by 0, and +max is
70 represented by 2*max), which is half the maximum for the corresponding
72 ===========================================================================
75 luaP.OpMode = {"iABC", "iABx", "iAsBx"} -- basic instruction format
77 ------------------------------------------------------------------------
78 -- size and position of opcode arguments.
79 -- * WARNING size and position is hard-coded elsewhere in this script
80 ------------------------------------------------------------------------
83 luaP.SIZE_Bx = luaP.SIZE_C + luaP.SIZE_B
88 luaP.POS_C = luaP.SIZE_OP
89 luaP.POS_B = luaP.POS_C + luaP.SIZE_C
90 luaP.POS_Bx = luaP.POS_C
91 luaP.POS_A = luaP.POS_B + luaP.SIZE_B
94 luaP.BITRK = 2^(luaP.SIZE_B - 1)
95 function luaP:ISK(x) return x >= self.BITRK end
96 luaP.MAXINDEXRK = luaP.BITRK - 1
97 function luaP:RKASK(x)
98 if x < self.BITRK then return x+self.BITRK else return x end
103 ------------------------------------------------------------------------
104 -- limits for opcode arguments.
105 -- we use (signed) int to manipulate most arguments,
106 -- so they must fit in BITS_INT-1 bits (-1 for sign)
107 ------------------------------------------------------------------------
108 -- removed "#if SIZE_Bx < BITS_INT-1" test, assume this script is
109 -- running on a Lua VM with double or int as LUA_NUMBER
111 luaP.MAXARG_Bx = math.ldexp(1, luaP.SIZE_Bx) - 1
112 luaP.MAXARG_sBx = math.floor(luaP.MAXARG_Bx / 2) -- 'sBx' is signed
114 luaP.MAXARG_A = math.ldexp(1, luaP.SIZE_A) - 1
115 luaP.MAXARG_B = math.ldexp(1, luaP.SIZE_B) - 1
116 luaP.MAXARG_C = math.ldexp(1, luaP.SIZE_C) - 1
118 -- creates a mask with 'n' 1 bits at position 'p'
119 -- MASK1(n,p) deleted
120 -- creates a mask with 'n' 0 bits at position 'p'
121 -- MASK0(n,p) deleted
123 --[[--------------------------------------------------------------------
124 Visual representation for reference:
126 31 | | | 0 bit position
127 +-----+-----+-----+----------+
128 | B | C | A | Opcode | iABC format
129 +-----+-----+-----+----------+
130 - 9 - 9 - 8 - 6 - field sizes
131 +-----+-----+-----+----------+
132 | [s]Bx | A | Opcode | iABx | iAsBx format
133 +-----+-----+-----+----------+
134 ----------------------------------------------------------------------]]
136 ------------------------------------------------------------------------
137 -- the following macros help to manipulate instructions
138 -- * changed to a table object representation, very clean compared to
139 -- the [nightmare] alternatives of using a number or a string
140 ------------------------------------------------------------------------
142 -- these accept or return opcodes in the form of string names
143 function luaP:GET_OPCODE(i) return self.ROpCode[i.OP] end
144 function luaP:SET_OPCODE(i, o) i.OP = self.OpCode[o] end
146 function luaP:GETARG_A(i) return i.A end
147 function luaP:SETARG_A(i, u) i.A = u end
149 function luaP:GETARG_B(i) return i.B end
150 function luaP:SETARG_B(i, b) i.B = b end
152 function luaP:GETARG_C(i) return i.C end
153 function luaP:SETARG_C(i, b) i.C = b end
155 function luaP:GETARG_Bx(i) return i.Bx end
156 function luaP:SETARG_Bx(i, b) i.Bx = b end
158 function luaP:GETARG_sBx(i) return i.Bx - self.MAXARG_sBx end
159 function luaP:SETARG_sBx(i, b) i.Bx = b + self.MAXARG_sBx end
161 function luaP:CREATE_ABC(o,a,b,c)
162 return {OP = self.OpCode[o], A = a, B = b, C = c}
165 function luaP:CREATE_ABx(o,a,bc)
166 return {OP = self.OpCode[o], A = a, Bx = bc}
169 ------------------------------------------------------------------------
170 -- Bit shuffling stuffs
171 ------------------------------------------------------------------------
173 if false and pcall (require, 'bit') then
174 ------------------------------------------------------------------------
175 -- Return a 4-char string little-endian encoded form of an instruction
176 ------------------------------------------------------------------------
177 function luaP:Instruction(i)
181 ------------------------------------------------------------------------
182 -- Version without bit manipulation library.
183 ------------------------------------------------------------------------
184 local p2 = {1,2,4,8,16,32,64,128,256, 512, 1024, 2048, 4096}
185 -- keeps [n] bits from [x]
186 local function keep (x, n) return x % p2[n+1] end
187 -- shifts bits of [x] [n] places to the right
188 local function srb (x,n) return math.floor (x / p2[n+1]) end
189 -- shifts bits of [x] [n] places to the left
190 local function slb (x,n) return x * p2[n+1] end
192 ------------------------------------------------------------------------
193 -- Return a 4-char string little-endian encoded form of an instruction
194 ------------------------------------------------------------------------
195 function luaP:Instruction(i)
196 -- printf("Instr->string: %s %s", self.opnames[i.OP], table.tostring(i))
198 -- change to OP/A/B/C format if needed
199 if i.Bx then i.C = keep (i.Bx, 9); i.B = srb (i.Bx, 9) end
200 -- c0 = 6B from opcode + 2LSB from A (flushed to MSB)
201 c0 = i.OP + slb (keep (i.A, 2), 6)
202 -- c1 = 6MSB from A + 2LSB from C (flushed to MSB)
203 c1 = srb (i.A, 2) + slb (keep (i.C, 2), 6)
204 -- c2 = 7MSB from C + 1LSB from B (flushed to MSB)
205 c2 = srb (i.C, 2) + slb (keep (i.B, 1), 7)
208 --printf ("Instruction: %s %s", self.opnames[i.OP], tostringv (i))
209 --printf ("Bin encoding: %x %x %x %x", c0, c1, c2, c3)
210 return string.char(c0, c1, c2, c3)
213 ------------------------------------------------------------------------
214 -- decodes a 4-char little-endian string into an instruction struct
215 ------------------------------------------------------------------------
216 function luaP:DecodeInst(x)
217 error "Not implemented"
220 ------------------------------------------------------------------------
221 -- invalid register that fits in 8 bits
222 ------------------------------------------------------------------------
223 luaP.NO_REG = luaP.MAXARG_A
225 ------------------------------------------------------------------------
227 -- Kst(x) - constant (in constant table)
228 -- RK(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
229 ------------------------------------------------------------------------
231 ------------------------------------------------------------------------
232 -- grep "ORDER OP" if you change these enums
233 ------------------------------------------------------------------------
235 --[[--------------------------------------------------------------------
236 Lua virtual machine opcodes (enum OpCode):
237 ------------------------------------------------------------------------
238 name args description
239 ------------------------------------------------------------------------
240 OP_MOVE A B R(A) := R(B)
241 OP_LOADK A Bx R(A) := Kst(Bx)
242 OP_LOADBOOL A B C R(A) := (Bool)B; if (C) PC++
243 OP_LOADNIL A B R(A) := ... := R(B) := nil
244 OP_GETUPVAL A B R(A) := UpValue[B]
245 OP_GETGLOBAL A Bx R(A) := Gbl[Kst(Bx)]
246 OP_GETTABLE A B C R(A) := R(B)[RK(C)]
247 OP_SETGLOBAL A Bx Gbl[Kst(Bx)] := R(A)
248 OP_SETUPVAL A B UpValue[B] := R(A)
249 OP_SETTABLE A B C R(A)[RK(B)] := RK(C)
250 OP_NEWTABLE A B C R(A) := {} (size = B,C)
251 OP_SELF A B C R(A+1) := R(B); R(A) := R(B)[RK(C)]
252 OP_ADD A B C R(A) := RK(B) + RK(C)
253 OP_SUB A B C R(A) := RK(B) - RK(C)
254 OP_MUL A B C R(A) := RK(B) * RK(C)
255 OP_DIV A B C R(A) := RK(B) / RK(C)
256 OP_POW A B C R(A) := RK(B) ^ RK(C)
257 OP_UNM A B R(A) := -R(B)
258 OP_NOT A B R(A) := not R(B)
259 OP_CONCAT A B C R(A) := R(B).. ... ..R(C)
261 OP_EQ A B C if ((RK(B) == RK(C)) ~= A) then pc++
262 OP_LT A B C if ((RK(B) < RK(C)) ~= A) then pc++
263 OP_LE A B C if ((RK(B) <= RK(C)) ~= A) then pc++
264 OP_TEST A B C if (R(B) <=> C) then R(A) := R(B) else pc++
265 OP_CALL A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1))
266 OP_TAILCALL A B C return R(A)(R(A+1), ... ,R(A+B-1))
267 OP_RETURN A B return R(A), ... ,R(A+B-2) (see note)
268 OP_FORLOOP A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx
269 OP_TFORLOOP A C R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
270 if R(A+2) ~= nil then pc++
271 OP_TFORPREP A sBx if type(R(A)) == table then R(A+1):=R(A), R(A):=next;
273 OP_SETLIST A Bx R(A)[Bx-Bx%FPF+i] := R(A+i), 1 <= i <= Bx%FPF+1
274 OP_SETLISTO A Bx (see note)
275 OP_CLOSE A close all variables in the stack up to (>=) R(A)
276 OP_CLOSURE A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))
277 ----------------------------------------------------------------------]]
279 luaP.opnames = {} -- opcode names
280 luaP.OpCode = {} -- lookup name -> number
281 luaP.ROpCode = {} -- lookup number -> name
284 for v in string.gfind([[
333 ===========================================================================
335 (1) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
336 and can be 0: OP_CALL then sets 'top' to last_result+1, so
337 next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use 'top'.
339 (2) In OP_RETURN, if (B == 0) then return up to 'top'
341 (3) For comparisons, B specifies what conditions the test should accept.
343 (4) All 'skips' (pc++) assume that next instruction is a jump
345 (5) OP_SETLISTO is used when the last item in a table constructor is a
346 function, so the number of elements set is up to top of stack
347 ===========================================================================
350 ------------------------------------------------------------------------
351 -- masks for instruction properties
352 ------------------------------------------------------------------------
353 -- was enum OpModeMask:
354 luaP.OpModeBreg = 2 -- B is a register
355 luaP.OpModeBrk = 3 -- B is a register/constant
356 luaP.OpModeCrk = 4 -- C is a register/constant
357 luaP.OpModesetA = 5 -- instruction set register A
358 luaP.OpModeK = 6 -- Bx is a constant
359 luaP.OpModeT = 1 -- operator is a test
361 ------------------------------------------------------------------------
362 -- get opcode mode, e.g. "iABC"
363 ------------------------------------------------------------------------
364 function luaP:getOpMode(m)
366 --printv(self.OpCode[m])
367 --printv(self.opmodes [self.OpCode[m]+1])
368 return self.OpMode[tonumber(string.sub(self.opmodes[self.OpCode[m] + 1], 7, 7))]
371 ------------------------------------------------------------------------
372 -- test an instruction property flag
373 -- * b is a string, e.g. "OpModeBreg"
374 ------------------------------------------------------------------------
375 function luaP:testOpMode(m, b)
376 return (string.sub(self.opmodes[self.OpCode[m] + 1], self[b], self[b]) == "1")
379 -- number of list items to accumulate before a SETLIST instruction
380 -- (must be a power of 2)
381 -- * used in lparser, lvm, ldebug, ltests
382 luaP.LFIELDS_PER_FLUSH = 50 --FF updated to match 5.1
384 -- luaP_opnames[] is set above, as the luaP.opnames table
385 -- opmode(t,b,bk,ck,sa,k,m) deleted
387 --[[--------------------------------------------------------------------
388 Legend for luaP:opmodes:
389 1 T -> T (is a test?)
390 2 B -> B is a register
391 3 b -> B is an RK register/constant combination
392 4 C -> C is an RK register/constant combination
393 5 A -> register A is set by the opcode
394 6 K -> Bx is a constant
395 7 m -> 1 if iABC layout,
398 ----------------------------------------------------------------------]]
402 "0100101", -- OP_MOVE 0
403 "0000112", -- OP_LOADK
404 "0000101", -- OP_LOADBOOL
405 "0100101", -- OP_LOADNIL
406 "0000101", -- OP_GETUPVAL
407 "0000112", -- OP_GETGLOBAL 5
408 "0101101", -- OP_GETTABLE
409 "0000012", -- OP_SETGLOBAL
410 "0000001", -- OP_SETUPVAL
411 "0011001", -- OP_SETTABLE
412 "0000101", -- OP_NEWTABLE 10
413 "0101101", -- OP_SELF
417 "0011101", -- OP_DIV 15
422 "0100101", -- OP_LEN 20
423 "0101101", -- OP_CONCAT
427 "1011001", -- OP_LE 25
428 "1000101", -- OP_TEST
429 "1100101", -- OP_TESTSET
430 "0000001", -- OP_CALL
431 "0000001", -- OP_TAILCALL
432 "0000001", -- OP_RETURN 30
433 "0000003", -- OP_FORLOOP
434 "0000103", -- OP_FORPREP
435 "1000101", -- OP_TFORLOOP
436 "0000001", -- OP_SETLIST
437 "0000001", -- OP_CLOSE 35
438 "0000102", -- OP_CLOSURE
439 "0000101" -- OP_VARARG