aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/lopcodes.lua
diff options
context:
space:
mode:
Diffstat (limited to 'LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/lopcodes.lua')
-rw-r--r--LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/lopcodes.lua349
1 files changed, 0 insertions, 349 deletions
diff --git a/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/lopcodes.lua b/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/lopcodes.lua
deleted file mode 100644
index 0c4eebd..0000000
--- a/LuaSL/testLua/yueliang-0.4.1/orig-5.0.3/lopcodes.lua
+++ /dev/null
@@ -1,349 +0,0 @@
1--[[--------------------------------------------------------------------
2
3 lopcodes.lua
4 Lua 5 virtual machine opcodes in Lua
5 This file is part of Yueliang.
6
7 Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
8 The COPYRIGHT file describes the conditions
9 under which this software may be distributed.
10
11 See the ChangeLog for more information.
12
13----------------------------------------------------------------------]]
14
15--[[--------------------------------------------------------------------
16-- Notes:
17-- * an Instruction is a table with OP, A, B, C, Bx elements; this
18-- should allow instruction handling to work with doubles and ints
19-- * Added:
20-- luaP:Instruction(i): convert field elements to a 4-char string
21-- luaP:DecodeInst(x): convert 4-char string into field elements
22-- * WARNING luaP:Instruction outputs instructions encoded in little-
23-- endian form and field size and positions are hard-coded
24----------------------------------------------------------------------]]
25
26luaP = {}
27
28--[[
29===========================================================================
30 We assume that instructions are unsigned numbers.
31 All instructions have an opcode in the first 6 bits.
32 Instructions can have the following fields:
33 'A' : 8 bits
34 'B' : 9 bits
35 'C' : 9 bits
36 'Bx' : 18 bits ('B' and 'C' together)
37 'sBx' : signed Bx
38
39 A signed argument is represented in excess K; that is, the number
40 value is the unsigned value minus K. K is exactly the maximum value
41 for that argument (so that -max is represented by 0, and +max is
42 represented by 2*max), which is half the maximum for the corresponding
43 unsigned argument.
44===========================================================================
45--]]
46
47luaP.OpMode = {"iABC", "iABx", "iAsBx"} -- basic instruction format
48
49------------------------------------------------------------------------
50-- size and position of opcode arguments.
51-- * WARNING size and position is hard-coded elsewhere in this script
52------------------------------------------------------------------------
53luaP.SIZE_C = 9
54luaP.SIZE_B = 9
55luaP.SIZE_Bx = luaP.SIZE_C + luaP.SIZE_B
56luaP.SIZE_A = 8
57
58luaP.SIZE_OP = 6
59
60luaP.POS_C = luaP.SIZE_OP
61luaP.POS_B = luaP.POS_C + luaP.SIZE_C
62luaP.POS_Bx = luaP.POS_C
63luaP.POS_A = luaP.POS_B + luaP.SIZE_B
64
65------------------------------------------------------------------------
66-- limits for opcode arguments.
67-- we use (signed) int to manipulate most arguments,
68-- so they must fit in BITS_INT-1 bits (-1 for sign)
69------------------------------------------------------------------------
70-- removed "#if SIZE_Bx < BITS_INT-1" test, assume this script is
71-- running on a Lua VM with double or int as LUA_NUMBER
72
73luaP.MAXARG_Bx = math.ldexp(1, luaP.SIZE_Bx) - 1
74luaP.MAXARG_sBx = math.floor(luaP.MAXARG_Bx / 2) -- 'sBx' is signed
75
76luaP.MAXARG_A = math.ldexp(1, luaP.SIZE_A) - 1
77luaP.MAXARG_B = math.ldexp(1, luaP.SIZE_B) - 1
78luaP.MAXARG_C = math.ldexp(1, luaP.SIZE_C) - 1
79
80-- creates a mask with 'n' 1 bits at position 'p'
81-- MASK1(n,p) deleted
82-- creates a mask with 'n' 0 bits at position 'p'
83-- MASK0(n,p) deleted
84
85--[[--------------------------------------------------------------------
86 Visual representation for reference:
87
88 31 | | | 0 bit position
89 +-----+-----+-----+----------+
90 | A | B | C | Opcode | iABC format
91 +-----+-----+-----+----------+
92 - 8 - 9 - 9 - 6 - field sizes
93 +-----+-----+-----+----------+
94 | A | [s]Bx | Opcode | iABx | iAsBx format
95 +-----+-----+-----+----------+
96----------------------------------------------------------------------]]
97
98------------------------------------------------------------------------
99-- the following macros help to manipulate instructions
100-- * changed to a table object representation, very clean compared to
101-- the [nightmare] alternatives of using a number or a string
102------------------------------------------------------------------------
103
104-- these accept or return opcodes in the form of string names
105function luaP:GET_OPCODE(i) return self.ROpCode[i.OP] end
106function luaP:SET_OPCODE(i, o) i.OP = self.OpCode[o] end
107
108function luaP:GETARG_A(i) return i.A end
109function luaP:SETARG_A(i, u) i.A = u end
110
111function luaP:GETARG_B(i) return i.B end
112function luaP:SETARG_B(i, b) i.B = b end
113
114function luaP:GETARG_C(i) return i.C end
115function luaP:SETARG_C(i, b) i.C = b end
116
117function luaP:GETARG_Bx(i) return i.Bx end
118function luaP:SETARG_Bx(i, b) i.Bx = b end
119
120function luaP:GETARG_sBx(i) return i.Bx - self.MAXARG_sBx end
121function luaP:SETARG_sBx(i, b) i.Bx = b + self.MAXARG_sBx end
122
123function luaP:CREATE_ABC(o,a,b,c)
124 return {OP = self.OpCode[o], A = a, B = b, C = c}
125end
126
127function luaP:CREATE_ABx(o,a,bc)
128 return {OP = self.OpCode[o], A = a, Bx = bc}
129end
130
131------------------------------------------------------------------------
132-- returns a 4-char string little-endian encoded form of an instruction
133------------------------------------------------------------------------
134function luaP:Instruction(i)
135 local I, c0, c1, c2, c3
136 if i.Bx then
137 -- change to OP/A/B/C format
138 i.C = math.mod(i.Bx, 512)
139 i.B = math.floor(i.Bx / 512)
140 end
141 I = i.C * 64 + i.OP
142 c0 = math.mod(I, 256)
143 I = i.B * 128 + math.floor(I / 256) -- 7 bits of C left
144 c1 = math.mod(I, 256)
145 I = math.floor(I / 256) -- 8 bits of B left
146 c2 = math.mod(I, 256)
147 c3 = math.mod(i.A, 256)
148 return string.char(c0, c1, c2, c3)
149end
150
151------------------------------------------------------------------------
152-- decodes a 4-char little-endian string into an instruction struct
153------------------------------------------------------------------------
154function luaP:DecodeInst(x)
155 local i = {}
156 local I = string.byte(x, 1)
157 local op = math.mod(I, 64)
158 i.OP = op
159 I = string.byte(x, 2) * 4 + math.floor(I / 64) -- 2 bits of c0 left
160 i.C = math.mod(I, 512)
161 i.B = string.byte(x, 3) * 2 + math.floor(I / 128) -- 1 bit of c2 left
162 i.A = string.byte(x, 4)
163 local opmode = self.OpMode[tonumber(string.sub(self.opmodes[op + 1], 7, 7))]
164 if opmode ~= "iABC" then
165 i.Bx = i.B * 512 + i.C
166 end
167 return i
168end
169
170------------------------------------------------------------------------
171-- invalid register that fits in 8 bits
172------------------------------------------------------------------------
173luaP.NO_REG = luaP.MAXARG_A
174
175------------------------------------------------------------------------
176-- R(x) - register
177-- Kst(x) - constant (in constant table)
178-- RK(x) == if x < MAXSTACK then R(x) else Kst(x-MAXSTACK)
179------------------------------------------------------------------------
180
181------------------------------------------------------------------------
182-- grep "ORDER OP" if you change these enums
183------------------------------------------------------------------------
184
185--[[--------------------------------------------------------------------
186Lua virtual machine opcodes (enum OpCode):
187------------------------------------------------------------------------
188name args description
189------------------------------------------------------------------------
190OP_MOVE A B R(A) := R(B)
191OP_LOADK A Bx R(A) := Kst(Bx)
192OP_LOADBOOL A B C R(A) := (Bool)B; if (C) PC++
193OP_LOADNIL A B R(A) := ... := R(B) := nil
194OP_GETUPVAL A B R(A) := UpValue[B]
195OP_GETGLOBAL A Bx R(A) := Gbl[Kst(Bx)]
196OP_GETTABLE A B C R(A) := R(B)[RK(C)]
197OP_SETGLOBAL A Bx Gbl[Kst(Bx)] := R(A)
198OP_SETUPVAL A B UpValue[B] := R(A)
199OP_SETTABLE A B C R(A)[RK(B)] := RK(C)
200OP_NEWTABLE A B C R(A) := {} (size = B,C)
201OP_SELF A B C R(A+1) := R(B); R(A) := R(B)[RK(C)]
202OP_ADD A B C R(A) := RK(B) + RK(C)
203OP_SUB A B C R(A) := RK(B) - RK(C)
204OP_MUL A B C R(A) := RK(B) * RK(C)
205OP_DIV A B C R(A) := RK(B) / RK(C)
206OP_POW A B C R(A) := RK(B) ^ RK(C)
207OP_UNM A B R(A) := -R(B)
208OP_NOT A B R(A) := not R(B)
209OP_CONCAT A B C R(A) := R(B).. ... ..R(C)
210OP_JMP sBx PC += sBx
211OP_EQ A B C if ((RK(B) == RK(C)) ~= A) then pc++
212OP_LT A B C if ((RK(B) < RK(C)) ~= A) then pc++
213OP_LE A B C if ((RK(B) <= RK(C)) ~= A) then pc++
214OP_TEST A B C if (R(B) <=> C) then R(A) := R(B) else pc++
215OP_CALL A B C R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1))
216OP_TAILCALL A B C return R(A)(R(A+1), ... ,R(A+B-1))
217OP_RETURN A B return R(A), ... ,R(A+B-2) (see note)
218OP_FORLOOP A sBx R(A)+=R(A+2); if R(A) <?= R(A+1) then PC+= sBx
219OP_TFORLOOP A C R(A+2), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
220 if R(A+2) ~= nil then pc++
221OP_TFORPREP A sBx if type(R(A)) == table then R(A+1):=R(A), R(A):=next;
222 PC += sBx
223OP_SETLIST A Bx R(A)[Bx-Bx%FPF+i] := R(A+i), 1 <= i <= Bx%FPF+1
224OP_SETLISTO A Bx (see note)
225OP_CLOSE A close all variables in the stack up to (>=) R(A)
226OP_CLOSURE A Bx R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))
227----------------------------------------------------------------------]]
228
229luaP.opnames = {} -- opcode names
230luaP.OpCode = {} -- lookup name -> number
231luaP.ROpCode = {} -- lookup number -> name
232
233-- ORDER OP
234local i = 0
235for v in string.gfind([[
236MOVE LOADK LOADBOOL LOADNIL GETUPVAL
237GETGLOBAL GETTABLE SETGLOBAL SETUPVAL SETTABLE
238NEWTABLE SELF ADD SUB MUL
239DIV POW UNM NOT CONCAT
240JMP EQ LT LE TEST
241CALL TAILCALL RETURN FORLOOP TFORLOOP
242TFORPREP SETLIST SETLISTO CLOSE CLOSURE
243]], "%S+") do
244 local n = "OP_"..v
245 luaP.opnames[i] = v
246 luaP.OpCode[n] = i
247 luaP.ROpCode[i] = n
248 i = i + 1
249end
250luaP.NUM_OPCODES = i
251
252--[[
253===========================================================================
254 Notes:
255 (1) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
256 and can be 0: OP_CALL then sets 'top' to last_result+1, so
257 next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use 'top'.
258
259 (2) In OP_RETURN, if (B == 0) then return up to 'top'
260
261 (3) For comparisons, B specifies what conditions the test should accept.
262
263 (4) All 'skips' (pc++) assume that next instruction is a jump
264
265 (5) OP_SETLISTO is used when the last item in a table constructor is a
266 function, so the number of elements set is up to top of stack
267===========================================================================
268--]]
269
270------------------------------------------------------------------------
271-- masks for instruction properties
272------------------------------------------------------------------------
273-- was enum OpModeMask:
274luaP.OpModeBreg = 2 -- B is a register
275luaP.OpModeBrk = 3 -- B is a register/constant
276luaP.OpModeCrk = 4 -- C is a register/constant
277luaP.OpModesetA = 5 -- instruction set register A
278luaP.OpModeK = 6 -- Bx is a constant
279luaP.OpModeT = 1 -- operator is a test
280
281------------------------------------------------------------------------
282-- get opcode mode, e.g. "iABC"
283------------------------------------------------------------------------
284function luaP:getOpMode(m)
285 return self.OpMode[tonumber(string.sub(self.opmodes[self.OpCode[m] + 1], 7, 7))]
286end
287
288------------------------------------------------------------------------
289-- test an instruction property flag
290-- * b is a string, e.g. "OpModeBreg"
291------------------------------------------------------------------------
292function luaP:testOpMode(m, b)
293 return (string.sub(self.opmodes[self.OpCode[m] + 1], self[b], self[b]) == "1")
294end
295
296-- number of list items to accumulate before a SETLIST instruction
297-- (must be a power of 2)
298-- * used in lparser, lvm, ldebug, ltests
299luaP.LFIELDS_PER_FLUSH = 32
300
301-- luaP_opnames[] is set above, as the luaP.opnames table
302-- opmode(t,b,bk,ck,sa,k,m) deleted
303
304--[[--------------------------------------------------------------------
305 Legend for luaP:opmodes:
306 T -> T B -> B mode -> m, where iABC = 1
307 Bk -> b Ck -> C iABx = 2
308 sA -> A K -> K iAsBx = 3
309----------------------------------------------------------------------]]
310
311-- ORDER OP
312luaP.opmodes = {
313-- TBbCAKm opcode
314 "0100101", -- OP_MOVE
315 "0000112", -- OP_LOADK
316 "0000101", -- OP_LOADBOOL
317 "0100101", -- OP_LOADNIL
318 "0000101", -- OP_GETUPVAL
319 "0000112", -- OP_GETGLOBAL
320 "0101101", -- OP_GETTABLE
321 "0000012", -- OP_SETGLOBAL
322 "0000001", -- OP_SETUPVAL
323 "0011001", -- OP_SETTABLE
324 "0000101", -- OP_NEWTABLE
325 "0101101", -- OP_SELF
326 "0011101", -- OP_ADD
327 "0011101", -- OP_SUB
328 "0011101", -- OP_MUL
329 "0011101", -- OP_DIV
330 "0011101", -- OP_POW
331 "0100101", -- OP_UNM
332 "0100101", -- OP_NOT
333 "0101101", -- OP_CONCAT
334 "0000003", -- OP_JMP
335 "1011001", -- OP_EQ
336 "1011001", -- OP_LT
337 "1011001", -- OP_LE
338 "1100101", -- OP_TEST
339 "0000001", -- OP_CALL
340 "0000001", -- OP_TAILCALL
341 "0000001", -- OP_RETURN
342 "0000003", -- OP_FORLOOP
343 "1000001", -- OP_TFORLOOP
344 "0000003", -- OP_TFORPREP
345 "0000002", -- OP_SETLIST
346 "0000002", -- OP_SETLISTO
347 "0000001", -- OP_CLOSE
348 "0000102", -- OP_CLOSURE
349}