aboutsummaryrefslogtreecommitdiff
path: root/lcode.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-28 11:18:14 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2024-06-28 11:18:14 -0300
commitc403e456b66ddacf7f8f974323e9cffdfe6365d4 (patch)
tree2e25027eface85c3156359e4ec182b4a9215903b /lcode.c
parent6ac7219da31df0238dc33c2d4457f69bfe0c1e79 (diff)
downloadlua-c403e456b66ddacf7f8f974323e9cffdfe6365d4.tar.gz
lua-c403e456b66ddacf7f8f974323e9cffdfe6365d4.tar.bz2
lua-c403e456b66ddacf7f8f974323e9cffdfe6365d4.zip
New instruction format for SETLIST/NEWTABLE
New instruction format 'ivABC' (a variant of iABC where parameter vC has 10 bits) allows constructors of up to 1024 elements to be coded without EXTRAARG.
Diffstat (limited to 'lcode.c')
-rw-r--r--lcode.c59
1 files changed, 34 insertions, 25 deletions
diff --git a/lcode.c b/lcode.c
index e120f0db..c1fce37f 100644
--- a/lcode.c
+++ b/lcode.c
@@ -390,32 +390,40 @@ int luaK_code (FuncState *fs, Instruction i) {
390** Format and emit an 'iABC' instruction. (Assertions check consistency 390** Format and emit an 'iABC' instruction. (Assertions check consistency
391** of parameters versus opcode.) 391** of parameters versus opcode.)
392*/ 392*/
393int luaK_codeABCk (FuncState *fs, OpCode o, int a, int b, int c, int k) { 393int luaK_codeABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
394 lua_assert(getOpMode(o) == iABC); 394 lua_assert(getOpMode(o) == iABC);
395 lua_assert(a <= MAXARG_A && b <= MAXARG_B && 395 lua_assert(A <= MAXARG_A && B <= MAXARG_B &&
396 c <= MAXARG_C && (k & ~1) == 0); 396 C <= MAXARG_C && (k & ~1) == 0);
397 return luaK_code(fs, CREATE_ABCk(o, a, b, c, k)); 397 return luaK_code(fs, CREATE_ABCk(o, A, B, C, k));
398}
399
400
401int luaK_codevABCk (FuncState *fs, OpCode o, int A, int B, int C, int k) {
402 lua_assert(getOpMode(o) == ivABC);
403 lua_assert(A <= MAXARG_A && B <= MAXARG_vB &&
404 C <= MAXARG_vC && (k & ~1) == 0);
405 return luaK_code(fs, CREATE_vABCk(o, A, B, C, k));
398} 406}
399 407
400 408
401/* 409/*
402** Format and emit an 'iABx' instruction. 410** Format and emit an 'iABx' instruction.
403*/ 411*/
404int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { 412int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bc) {
405 lua_assert(getOpMode(o) == iABx); 413 lua_assert(getOpMode(o) == iABx);
406 lua_assert(a <= MAXARG_A && bc <= MAXARG_Bx); 414 lua_assert(A <= MAXARG_A && Bc <= MAXARG_Bx);
407 return luaK_code(fs, CREATE_ABx(o, a, bc)); 415 return luaK_code(fs, CREATE_ABx(o, A, Bc));
408} 416}
409 417
410 418
411/* 419/*
412** Format and emit an 'iAsBx' instruction. 420** Format and emit an 'iAsBx' instruction.
413*/ 421*/
414static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) { 422static int codeAsBx (FuncState *fs, OpCode o, int A, int Bc) {
415 unsigned int b = bc + OFFSET_sBx; 423 unsigned int b = cast_uint(Bc) + OFFSET_sBx;
416 lua_assert(getOpMode(o) == iAsBx); 424 lua_assert(getOpMode(o) == iAsBx);
417 lua_assert(a <= MAXARG_A && b <= MAXARG_Bx); 425 lua_assert(A <= MAXARG_A && b <= MAXARG_Bx);
418 return luaK_code(fs, CREATE_ABx(o, a, b)); 426 return luaK_code(fs, CREATE_ABx(o, A, b));
419} 427}
420 428
421 429
@@ -423,7 +431,7 @@ static int codeAsBx (FuncState *fs, OpCode o, int a, int bc) {
423** Format and emit an 'isJ' instruction. 431** Format and emit an 'isJ' instruction.
424*/ 432*/
425static int codesJ (FuncState *fs, OpCode o, int sj, int k) { 433static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
426 unsigned int j = sj + OFFSET_sJ; 434 unsigned int j = cast_uint(sj) + OFFSET_sJ;
427 lua_assert(getOpMode(o) == isJ); 435 lua_assert(getOpMode(o) == isJ);
428 lua_assert(j <= MAXARG_sJ && (k & ~1) == 0); 436 lua_assert(j <= MAXARG_sJ && (k & ~1) == 0);
429 return luaK_code(fs, CREATE_sJ(o, j, k)); 437 return luaK_code(fs, CREATE_sJ(o, j, k));
@@ -433,9 +441,9 @@ static int codesJ (FuncState *fs, OpCode o, int sj, int k) {
433/* 441/*
434** Emit an "extra argument" instruction (format 'iAx') 442** Emit an "extra argument" instruction (format 'iAx')
435*/ 443*/
436static int codeextraarg (FuncState *fs, int a) { 444static int codeextraarg (FuncState *fs, int A) {
437 lua_assert(a <= MAXARG_Ax); 445 lua_assert(A <= MAXARG_Ax);
438 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a)); 446 return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, A));
439} 447}
440 448
441 449
@@ -1032,10 +1040,10 @@ static int exp2RK (FuncState *fs, expdesc *e) {
1032} 1040}
1033 1041
1034 1042
1035static void codeABRK (FuncState *fs, OpCode o, int a, int b, 1043static void codeABRK (FuncState *fs, OpCode o, int A, int B,
1036 expdesc *ec) { 1044 expdesc *ec) {
1037 int k = exp2RK(fs, ec); 1045 int k = exp2RK(fs, ec);
1038 luaK_codeABCk(fs, o, a, b, ec->u.info, k); 1046 luaK_codeABCk(fs, o, A, B, ec->u.info, k);
1039} 1047}
1040 1048
1041 1049
@@ -1788,10 +1796,10 @@ void luaK_fixline (FuncState *fs, int line) {
1788void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) { 1796void luaK_settablesize (FuncState *fs, int pc, int ra, int asize, int hsize) {
1789 Instruction *inst = &fs->f->code[pc]; 1797 Instruction *inst = &fs->f->code[pc];
1790 int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */ 1798 int rb = (hsize != 0) ? luaO_ceillog2(hsize) + 1 : 0; /* hash size */
1791 int extra = asize / (MAXARG_C + 1); /* higher bits of array size */ 1799 int extra = asize / (MAXARG_vC + 1); /* higher bits of array size */
1792 int rc = asize % (MAXARG_C + 1); /* lower bits of array size */ 1800 int rc = asize % (MAXARG_vC + 1); /* lower bits of array size */
1793 int k = (extra > 0); /* true iff needs extra argument */ 1801 int k = (extra > 0); /* true iff needs extra argument */
1794 *inst = CREATE_ABCk(OP_NEWTABLE, ra, rb, rc, k); 1802 *inst = CREATE_vABCk(OP_NEWTABLE, ra, rb, rc, k);
1795 *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra); 1803 *(inst + 1) = CREATE_Ax(OP_EXTRAARG, extra);
1796} 1804}
1797 1805
@@ -1807,12 +1815,12 @@ void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {
1807 lua_assert(tostore != 0); 1815 lua_assert(tostore != 0);
1808 if (tostore == LUA_MULTRET) 1816 if (tostore == LUA_MULTRET)
1809 tostore = 0; 1817 tostore = 0;
1810 if (nelems <= MAXARG_C) 1818 if (nelems <= MAXARG_vC)
1811 luaK_codeABC(fs, OP_SETLIST, base, tostore, nelems); 1819 luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 0);
1812 else { 1820 else {
1813 int extra = nelems / (MAXARG_C + 1); 1821 int extra = nelems / (MAXARG_vC + 1);
1814 nelems %= (MAXARG_C + 1); 1822 nelems %= (MAXARG_vC + 1);
1815 luaK_codeABCk(fs, OP_SETLIST, base, tostore, nelems, 1); 1823 luaK_codevABCk(fs, OP_SETLIST, base, tostore, nelems, 1);
1816 codeextraarg(fs, extra); 1824 codeextraarg(fs, extra);
1817 } 1825 }
1818 fs->freereg = base + 1; /* free registers with list values */ 1826 fs->freereg = base + 1; /* free registers with list values */
@@ -1839,6 +1847,7 @@ static int finaltarget (Instruction *code, int i) {
1839** Do a final pass over the code of a function, doing small peephole 1847** Do a final pass over the code of a function, doing small peephole
1840** optimizations and adjustments. 1848** optimizations and adjustments.
1841*/ 1849*/
1850#include "lopnames.h"
1842void luaK_finish (FuncState *fs) { 1851void luaK_finish (FuncState *fs) {
1843 int i; 1852 int i;
1844 Proto *p = fs->f; 1853 Proto *p = fs->f;