interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / lopcodes.h

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