sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
lopcodes.h
Go to the documentation of this file.
1/*
2** $Id: lopcodes.h $
3** Opcodes for Lua virtual machine
4** See Copyright Notice in lua.h
5*/
6
11#ifndef lopcodes_h
12#define lopcodes_h
13
14#include "llimits.h"
15
16
17/*===========================================================================
18 We assume that instructions are unsigned 32-bit integers.
19 All instructions have an opcode in the first 7 bits.
20 Instructions can have the following formats:
21
22 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
23 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
24iABC C(8) | B(8) |k| A(8) | Op(7) |
25iABx Bx(17) | A(8) | Op(7) |
26iAsBx sBx (signed)(17) | A(8) | Op(7) |
27iAx Ax(25) | Op(7) |
28isJ sJ (signed)(25) | Op(7) |
29
30 A signed argument is represented in excess K: the represented value is
31 the written unsigned value minus K, where K is half the maximum for the
32 corresponding unsigned argument.
33===========================================================================*/
34
35
36enum OpMode {iABC, iABx, iAsBx, iAx, isJ}; /* basic instruction formats */
37
38
39/*
40** size and position of opcode arguments.
41*/
42#define SIZE_C 8
43#define SIZE_B 8
44#define SIZE_Bx (SIZE_C + SIZE_B + 1)
45#define SIZE_A 8
46#define SIZE_Ax (SIZE_Bx + SIZE_A)
47#define SIZE_sJ (SIZE_Bx + SIZE_A)
48
49#define SIZE_OP 7
50
51#define POS_OP 0
52
53#define POS_A (POS_OP + SIZE_OP)
54#define POS_k (POS_A + SIZE_A)
55#define POS_B (POS_k + 1)
56#define POS_C (POS_B + SIZE_B)
57
58#define POS_Bx POS_k
59
60#define POS_Ax POS_A
61
62#define POS_sJ POS_A
63
64
65/*
66** limits for opcode arguments.
67** we use (signed) 'int' to manipulate most arguments,
68** so they must fit in ints.
69*/
70
71/* Check whether type 'int' has at least 'b' bits ('b' < 32) */
72#define L_INTHASBITS(b) ((UINT_MAX >> ((b) - 1)) >= 1)
73
74
75#if L_INTHASBITS(SIZE_Bx)
76#define MAXARG_Bx ((1<<SIZE_Bx)-1)
77#else
78#define MAXARG_Bx MAX_INT
79#endif
80
81#define OFFSET_sBx (MAXARG_Bx>>1) /* 'sBx' is signed */
82
83
84#if L_INTHASBITS(SIZE_Ax)
85#define MAXARG_Ax ((1<<SIZE_Ax)-1)
86#else
87#define MAXARG_Ax MAX_INT
88#endif
89
90#if L_INTHASBITS(SIZE_sJ)
91#define MAXARG_sJ ((1 << SIZE_sJ) - 1)
92#else
93#define MAXARG_sJ MAX_INT
94#endif
95
96#define OFFSET_sJ (MAXARG_sJ >> 1)
97
98
99#define MAXARG_A ((1<<SIZE_A)-1)
100#define MAXARG_B ((1<<SIZE_B)-1)
101#define MAXARG_C ((1<<SIZE_C)-1)
102#define OFFSET_sC (MAXARG_C >> 1)
103
104#define int2sC(i) ((i) + OFFSET_sC)
105#define sC2int(i) ((i) - OFFSET_sC)
106
107
108/* creates a mask with 'n' 1 bits at position 'p' */
109#define MASK1(n,p) ((~((~(Instruction)0)<<(n)))<<(p))
110
111/* creates a mask with 'n' 0 bits at position 'p' */
112#define MASK0(n,p) (~MASK1(n,p))
113
114/*
115** the following macros help to manipulate instructions
116*/
117
118#define GET_OPCODE(i) (cast(OpCode, ((i)>>POS_OP) & MASK1(SIZE_OP,0)))
119#define SET_OPCODE(i,o) ((i) = (((i)&MASK0(SIZE_OP,POS_OP)) | \
120 ((cast(Instruction, o)<<POS_OP)&MASK1(SIZE_OP,POS_OP))))
121
122#define checkopm(i,m) (getOpMode(GET_OPCODE(i)) == m)
123
124
125#define getarg(i,pos,size) (cast_int(((i)>>(pos)) & MASK1(size,0)))
126#define setarg(i,v,pos,size) ((i) = (((i)&MASK0(size,pos)) | \
127 ((cast(Instruction, v)<<pos)&MASK1(size,pos))))
128
129#define GETARG_A(i) getarg(i, POS_A, SIZE_A)
130#define SETARG_A(i,v) setarg(i, v, POS_A, SIZE_A)
131
132#define GETARG_B(i) check_exp(checkopm(i, iABC), getarg(i, POS_B, SIZE_B))
133#define GETARG_sB(i) sC2int(GETARG_B(i))
134#define SETARG_B(i,v) setarg(i, v, POS_B, SIZE_B)
135
136#define GETARG_C(i) check_exp(checkopm(i, iABC), getarg(i, POS_C, SIZE_C))
137#define GETARG_sC(i) sC2int(GETARG_C(i))
138#define SETARG_C(i,v) setarg(i, v, POS_C, SIZE_C)
139
140#define TESTARG_k(i) check_exp(checkopm(i, iABC), (cast_int(((i) & (1u << POS_k)))))
141#define GETARG_k(i) check_exp(checkopm(i, iABC), getarg(i, POS_k, 1))
142#define SETARG_k(i,v) setarg(i, v, POS_k, 1)
143
144#define GETARG_Bx(i) check_exp(checkopm(i, iABx), getarg(i, POS_Bx, SIZE_Bx))
145#define SETARG_Bx(i,v) setarg(i, v, POS_Bx, SIZE_Bx)
146
147#define GETARG_Ax(i) check_exp(checkopm(i, iAx), getarg(i, POS_Ax, SIZE_Ax))
148#define SETARG_Ax(i,v) setarg(i, v, POS_Ax, SIZE_Ax)
149
150#define GETARG_sBx(i) \
151 check_exp(checkopm(i, iAsBx), getarg(i, POS_Bx, SIZE_Bx) - OFFSET_sBx)
152#define SETARG_sBx(i,b) SETARG_Bx((i),cast_uint((b)+OFFSET_sBx))
153
154#define GETARG_sJ(i) \
155 check_exp(checkopm(i, isJ), getarg(i, POS_sJ, SIZE_sJ) - OFFSET_sJ)
156#define SETARG_sJ(i,j) \
157 setarg(i, cast_uint((j)+OFFSET_sJ), POS_sJ, SIZE_sJ)
158
159
160#define CREATE_ABCk(o,a,b,c,k) ((cast(Instruction, o)<<POS_OP) \
161 | (cast(Instruction, a)<<POS_A) \
162 | (cast(Instruction, b)<<POS_B) \
163 | (cast(Instruction, c)<<POS_C) \
164 | (cast(Instruction, k)<<POS_k))
165
166#define CREATE_ABx(o,a,bc) ((cast(Instruction, o)<<POS_OP) \
167 | (cast(Instruction, a)<<POS_A) \
168 | (cast(Instruction, bc)<<POS_Bx))
169
170#define CREATE_Ax(o,a) ((cast(Instruction, o)<<POS_OP) \
171 | (cast(Instruction, a)<<POS_Ax))
172
173#define CREATE_sJ(o,j,k) ((cast(Instruction, o) << POS_OP) \
174 | (cast(Instruction, j) << POS_sJ) \
175 | (cast(Instruction, k) << POS_k))
176
177
178#if !defined(MAXINDEXRK) /* (for debugging only) */
179#define MAXINDEXRK MAXARG_B
180#endif
181
182
183/*
184** invalid register that fits in 8 bits
185*/
186#define NO_REG MAXARG_A
187
188
189/*
190** R[x] - register
191** K[x] - constant (in constant table)
192** RK(x) == if k(i) then K[x] else R[x]
193*/
194
195
196/*
197** Grep "ORDER OP" if you change these enums. Opcodes marked with a (*)
198** has extra descriptions in the notes after the enumeration.
199*/
200
201typedef enum {
202/*----------------------------------------------------------------------
203 name args description
204------------------------------------------------------------------------*/
205OP_MOVE,/* A B R[A] := R[B] */
206OP_LOADI,/* A sBx R[A] := sBx */
207OP_LOADF,/* A sBx R[A] := (lua_Number)sBx */
208OP_LOADK,/* A Bx R[A] := K[Bx] */
209OP_LOADKX,/* A R[A] := K[extra arg] */
210OP_LOADFALSE,/* A R[A] := false */
211OP_LFALSESKIP,/*A R[A] := false; pc++ (*) */
212OP_LOADTRUE,/* A R[A] := true */
213OP_LOADNIL,/* A B R[A], R[A+1], ..., R[A+B] := nil */
214OP_GETUPVAL,/* A B R[A] := UpValue[B] */
215OP_SETUPVAL,/* A B UpValue[B] := R[A] */
216
217OP_GETTABUP,/* A B C R[A] := UpValue[B][K[C]:string] */
218OP_GETTABLE,/* A B C R[A] := R[B][R[C]] */
219OP_GETI,/* A B C R[A] := R[B][C] */
220OP_GETFIELD,/* A B C R[A] := R[B][K[C]:string] */
221
222OP_SETTABUP,/* A B C UpValue[A][K[B]:string] := RK(C) */
223OP_SETTABLE,/* A B C R[A][R[B]] := RK(C) */
224OP_SETI,/* A B C R[A][B] := RK(C) */
225OP_SETFIELD,/* A B C R[A][K[B]:string] := RK(C) */
226
227OP_NEWTABLE,/* A B C k R[A] := {} */
228
229OP_SELF,/* A B C R[A+1] := R[B]; R[A] := R[B][RK(C):string] */
230
231OP_ADDI,/* A B sC R[A] := R[B] + sC */
232
233OP_ADDK,/* A B C R[A] := R[B] + K[C]:number */
234OP_SUBK,/* A B C R[A] := R[B] - K[C]:number */
235OP_MULK,/* A B C R[A] := R[B] * K[C]:number */
236OP_MODK,/* A B C R[A] := R[B] % K[C]:number */
237OP_POWK,/* A B C R[A] := R[B] ^ K[C]:number */
238OP_DIVK,/* A B C R[A] := R[B] / K[C]:number */
239OP_IDIVK,/* A B C R[A] := R[B] // K[C]:number */
240
241OP_BANDK,/* A B C R[A] := R[B] & K[C]:integer */
242OP_BORK,/* A B C R[A] := R[B] | K[C]:integer */
243OP_BXORK,/* A B C R[A] := R[B] ~ K[C]:integer */
244
245OP_SHRI,/* A B sC R[A] := R[B] >> sC */
246OP_SHLI,/* A B sC R[A] := sC << R[B] */
247
248OP_ADD,/* A B C R[A] := R[B] + R[C] */
249OP_SUB,/* A B C R[A] := R[B] - R[C] */
250OP_MUL,/* A B C R[A] := R[B] * R[C] */
251OP_MOD,/* A B C R[A] := R[B] % R[C] */
252OP_POW,/* A B C R[A] := R[B] ^ R[C] */
253OP_DIV,/* A B C R[A] := R[B] / R[C] */
254OP_IDIV,/* A B C R[A] := R[B] // R[C] */
255
256OP_BAND,/* A B C R[A] := R[B] & R[C] */
257OP_BOR,/* A B C R[A] := R[B] | R[C] */
258OP_BXOR,/* A B C R[A] := R[B] ~ R[C] */
259OP_SHL,/* A B C R[A] := R[B] << R[C] */
260OP_SHR,/* A B C R[A] := R[B] >> R[C] */
261
262OP_MMBIN,/* A B C call C metamethod over R[A] and R[B] (*) */
263OP_MMBINI,/* A sB C k call C metamethod over R[A] and sB */
264OP_MMBINK,/* A B C k call C metamethod over R[A] and K[B] */
265
266OP_UNM,/* A B R[A] := -R[B] */
267OP_BNOT,/* A B R[A] := ~R[B] */
268OP_NOT,/* A B R[A] := not R[B] */
269OP_LEN,/* A B R[A] := #R[B] (length operator) */
270
271OP_CONCAT,/* A B R[A] := R[A].. ... ..R[A + B - 1] */
272
273OP_CLOSE,/* A close all upvalues >= R[A] */
274OP_TBC,/* A mark variable A "to be closed" */
275OP_JMP,/* sJ pc += sJ */
276OP_EQ,/* A B k if ((R[A] == R[B]) ~= k) then pc++ */
277OP_LT,/* A B k if ((R[A] < R[B]) ~= k) then pc++ */
278OP_LE,/* A B k if ((R[A] <= R[B]) ~= k) then pc++ */
279
280OP_EQK,/* A B k if ((R[A] == K[B]) ~= k) then pc++ */
281OP_EQI,/* A sB k if ((R[A] == sB) ~= k) then pc++ */
282OP_LTI,/* A sB k if ((R[A] < sB) ~= k) then pc++ */
283OP_LEI,/* A sB k if ((R[A] <= sB) ~= k) then pc++ */
284OP_GTI,/* A sB k if ((R[A] > sB) ~= k) then pc++ */
285OP_GEI,/* A sB k if ((R[A] >= sB) ~= k) then pc++ */
286
287OP_TEST,/* A k if (not R[A] == k) then pc++ */
288OP_TESTSET,/* A B k if (not R[B] == k) then pc++ else R[A] := R[B] (*) */
289
290OP_CALL,/* A B C R[A], ... ,R[A+C-2] := R[A](R[A+1], ... ,R[A+B-1]) */
291OP_TAILCALL,/* A B C k return R[A](R[A+1], ... ,R[A+B-1]) */
292
293OP_RETURN,/* A B C k return R[A], ... ,R[A+B-2] (see note) */
294OP_RETURN0,/* return */
295OP_RETURN1,/* A return R[A] */
296
297OP_FORLOOP,/* A Bx update counters; if loop continues then pc-=Bx; */
298OP_FORPREP,/* A Bx <check values and prepare counters>;
299 if not to run then pc+=Bx+1; */
300
301OP_TFORPREP,/* A Bx create upvalue for R[A + 3]; pc+=Bx */
302OP_TFORCALL,/* A C R[A+4], ... ,R[A+3+C] := R[A](R[A+1], R[A+2]); */
303OP_TFORLOOP,/* A Bx if R[A+2] ~= nil then { R[A]=R[A+2]; pc -= Bx } */
304
305OP_SETLIST,/* A B C k R[A][C+i] := R[A+i], 1 <= i <= B */
306
307OP_CLOSURE,/* A Bx R[A] := closure(KPROTO[Bx]) */
308
309OP_VARARG,/* A C R[A], R[A+1], ..., R[A+C-2] = vararg */
310
311OP_VARARGPREP,/*A (adjust vararg parameters) */
312
313OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */
314} OpCode;
315
316
317#define NUM_OPCODES ((int)(OP_EXTRAARG) + 1)
318
319
320
321/*===========================================================================
322 Notes:
323
324 (*) Opcode OP_LFALSESKIP is used to convert a condition to a boolean
325 value, in a code equivalent to (not cond ? false : true). (It
326 produces false and skips the next instruction producing true.)
327
328 (*) Opcodes OP_MMBIN and variants follow each arithmetic and
329 bitwise opcode. If the operation succeeds, it skips this next
330 opcode. Otherwise, this opcode calls the corresponding metamethod.
331
332 (*) Opcode OP_TESTSET is used in short-circuit expressions that need
333 both to jump and to produce a value, such as (a = b or c).
334
335 (*) In OP_CALL, if (B == 0) then B = top - A. If (C == 0), then
336 'top' is set to last_result+1, so next open instruction (OP_CALL,
337 OP_RETURN*, OP_SETLIST) may use 'top'.
338
339 (*) In OP_VARARG, if (C == 0) then use actual number of varargs and
340 set top (like in OP_CALL with C == 0).
341
342 (*) In OP_RETURN, if (B == 0) then return up to 'top'.
343
344 (*) In OP_LOADKX and OP_NEWTABLE, the next instruction is always
345 OP_EXTRAARG.
346
347 (*) In OP_SETLIST, if (B == 0) then real B = 'top'; if k, then
348 real C = EXTRAARG _ C (the bits of EXTRAARG concatenated with the
349 bits of C).
350
351 (*) In OP_NEWTABLE, B is log2 of the hash size (which is always a
352 power of 2) plus 1, or zero for size zero. If not k, the array size
353 is C. Otherwise, the array size is EXTRAARG _ C.
354
355 (*) For comparisons, k specifies what condition the test should accept
356 (true or false).
357
358 (*) In OP_MMBINI/OP_MMBINK, k means the arguments were flipped
359 (the constant is the first operand).
360
361 (*) All 'skips' (pc++) assume that next instruction is a jump.
362
363 (*) In instructions OP_RETURN/OP_TAILCALL, 'k' specifies that the
364 function builds upvalues, which may need to be closed. C > 0 means
365 the function is vararg, so that its 'func' must be corrected before
366 returning; in this case, (C - 1) is its number of fixed parameters.
367
368 (*) In comparisons with an immediate operand, C signals whether the
369 original operand was a float. (It must be corrected in case of
370 metamethods.)
371
372===========================================================================*/
373
374
375/*
376** masks for instruction properties. The format is:
377** bits 0-2: op mode
378** bit 3: instruction set register A
379** bit 4: operator is a test (next instruction must be a jump)
380** bit 5: instruction uses 'L->top' set by previous instruction (when B == 0)
381** bit 6: instruction sets 'L->top' for next instruction (when C == 0)
382** bit 7: instruction is an MM instruction (call a metamethod)
383*/
384
385LUAI_DDEC(const lu_byte luaP_opmodes[NUM_OPCODES];)
386
387#define getOpMode(m) (cast(enum OpMode, luaP_opmodes[m] & 7))
388#define testAMode(m) (luaP_opmodes[m] & (1 << 3))
389#define testTMode(m) (luaP_opmodes[m] & (1 << 4))
390#define testITMode(m) (luaP_opmodes[m] & (1 << 5))
391#define testOTMode(m) (luaP_opmodes[m] & (1 << 6))
392#define testMMMode(m) (luaP_opmodes[m] & (1 << 7))
393
394/* "out top" (set top for next instruction) */
395#define isOT(i) \
396 ((testOTMode(GET_OPCODE(i)) && GETARG_C(i) == 0) || \
397 GET_OPCODE(i) == OP_TAILCALL)
398
399/* "in top" (uses top from previous instruction) */
400#define isIT(i) (testITMode(GET_OPCODE(i)) && GETARG_B(i) == 0)
401
402#define opmode(mm,ot,it,t,a,m) \
403 (((mm) << 7) | ((ot) << 6) | ((it) << 5) | ((t) << 4) | ((a) << 3) | (m))
404
405
406/* number of list items to accumulate before a SETLIST instruction */
407#define LFIELDS_PER_FLUSH 50
408
409#endif