Replace table.getn/foreach/foreachi with bytecode builtins.

This commit is contained in:
Mike Pall 2013-02-23 02:17:50 +01:00
parent 73ef845fca
commit 60e380fd93
8 changed files with 37 additions and 99 deletions

View File

@ -4,12 +4,20 @@ static const int libbc_endian = 0;
static const uint8_t libbc_code[] = { static const uint8_t libbc_code[] = {
0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0, 0,1,2,0,0,1,2,24,1,0,0,76,1,2,0,241,135,158,166,3,220,203,178,130,4,0,1,2,0,
0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0 0,1,2,24,1,0,0,76,1,2,0,243,244,148,165,20,198,190,199,252,3,0,2,9,0,0,0,15,
16,0,12,0,16,1,9,0,41,2,1,0,21,3,0,0,41,4,1,0,77,2,8,128,18,6,1,0,18,7,5,0,
59,8,5,0,66,6,3,2,10,6,0,0,88,7,1,128,76,6,2,0,79,2,248,127,75,0,1,0,0,2,10,
0,0,0,16,16,0,12,0,16,1,9,0,43,2,0,0,18,3,0,0,41,4,0,0,88,5,7,128,18,7,1,0,
18,8,5,0,18,9,6,0,66,7,3,2,10,7,0,0,88,8,1,128,76,7,2,0,70,5,3,3,82,5,247,127,
75,0,1,0,0,1,2,0,0,0,3,16,0,12,0,21,1,0,0,76,1,2,0,0
}; };
static const struct { const char *name; int ofs; } libbc_map[] = { static const struct { const char *name; int ofs; } libbc_map[] = {
{"math_deg",0}, {"math_deg",0},
{"math_rad",25}, {"math_rad",25},
{NULL,50} {"table_foreachi",50},
{"table_foreach",117},
{"table_getn",188},
{NULL,207}
}; };

View File

@ -23,50 +23,34 @@
#define LJLIB_MODULE_table #define LJLIB_MODULE_table
LJLIB_CF(table_foreachi) LJLIB_LUA(table_foreachi) /*
{ function(t, f)
GCtab *t = lj_lib_checktab(L, 1); CHECK_tab(t)
GCfunc *func = lj_lib_checkfunc(L, 2); CHECK_func(f)
MSize i, n = lj_tab_len(t); for i=1,#t do
for (i = 1; i <= n; i++) { local r = f(i, t[i])
cTValue *val; if r ~= nil then return r end
setfuncV(L, L->top, func); end
setintV(L->top+1, i); end
val = lj_tab_getint(t, (int32_t)i); */
if (val) { copyTV(L, L->top+2, val); } else { setnilV(L->top+2); }
L->top += 3;
lua_call(L, 2, 1);
if (!tvisnil(L->top-1))
return 1;
L->top--;
}
return 0;
}
LJLIB_CF(table_foreach) LJLIB_LUA(table_foreach) /*
{ function(t, f)
GCtab *t = lj_lib_checktab(L, 1); CHECK_tab(t)
GCfunc *func = lj_lib_checkfunc(L, 2); CHECK_func(f)
L->top = L->base+3; for k, v in PAIRS(t) do
setnilV(L->top-1); local r = f(k, v)
while (lj_tab_next(L, t, L->top-1)) { if r ~= nil then return r end
copyTV(L, L->top+2, L->top); end
copyTV(L, L->top+1, L->top-1); end
setfuncV(L, L->top, func); */
L->top += 3;
lua_call(L, 2, 1);
if (!tvisnil(L->top-1))
return 1;
L->top--;
}
return 0;
}
LJLIB_ASM(table_getn) LJLIB_REC(.) LJLIB_LUA(table_getn) /*
{ function(t)
lj_lib_checktab(L, 1); CHECK_tab(t)
return FFH_UNREACHABLE; return #t
} end
*/
LJLIB_CF(table_maxn) LJLIB_CF(table_maxn)
{ {

View File

@ -60,7 +60,7 @@ typedef uint16_t HotCount;
#define HOTCOUNT_CALL 1 #define HOTCOUNT_CALL 1
/* This solves a circular dependency problem -- bump as needed. Sigh. */ /* This solves a circular dependency problem -- bump as needed. Sigh. */
#define GG_NUM_ASMFF 60 #define GG_NUM_ASMFF 59
#define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF) #define GG_LEN_DDISP (BC__MAX + GG_NUM_ASMFF)
#define GG_LEN_SDISP BC_FUNCF #define GG_LEN_SDISP BC_FUNCF

View File

@ -729,14 +729,6 @@ static void LJ_FASTCALL recff_string_range(jit_State *J, RecordFFData *rd)
/* -- Table library fast functions ---------------------------------------- */ /* -- Table library fast functions ---------------------------------------- */
static void LJ_FASTCALL recff_table_getn(jit_State *J, RecordFFData *rd)
{
if (tref_istab(J->base[0]))
J->base[0] = lj_ir_call(J, IRCALL_lj_tab_len, J->base[0]);
/* else: Interpreter will throw. */
UNUSED(rd);
}
static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd) static void LJ_FASTCALL recff_table_remove(jit_State *J, RecordFFData *rd)
{ {
TRef tab = J->base[0]; TRef tab = J->base[0];

View File

@ -1861,17 +1861,6 @@ static void build_subroutines(BuildCtx *ctx)
|ffstring_case string_lower, 65 |ffstring_case string_lower, 65
|ffstring_case string_upper, 97 |ffstring_case string_upper, 97
| |
|//-- Table library ------------------------------------------------------
|
|.ffunc_1 table_getn
| checktab CARG2, ->fff_fallback
| .IOS mov RA, BASE
| bl extern lj_tab_len // (GCtab *t)
| // Returns uint32_t (but less than 2^31).
| .IOS mov BASE, RA
| mvn CARG2, #~LJ_TISNUM
| b ->fff_restv
|
|//-- Bit library -------------------------------------------------------- |//-- Bit library --------------------------------------------------------
| |
|// FP number to bit conversion for soft-float. Clobbers r0-r3. |// FP number to bit conversion for soft-float. Clobbers r0-r3.

View File

@ -1812,18 +1812,6 @@ static void build_subroutines(BuildCtx *ctx)
|ffstring_case string_lower, 65 |ffstring_case string_lower, 65
|ffstring_case string_upper, 97 |ffstring_case string_upper, 97
| |
|//-- Table library ------------------------------------------------------
|
|.ffunc_1 table_getn
| li AT, LJ_TTAB
| bne CARG3, AT, ->fff_fallback
|. load_got lj_tab_len
| call_intern lj_tab_len // (GCtab *t)
|. nop
| // Returns uint32_t (but less than 2^31).
| b ->fff_resi
|. nop
|
|//-- Bit library -------------------------------------------------------- |//-- Bit library --------------------------------------------------------
| |
|.macro .ffunc_bit, name |.macro .ffunc_bit, name

View File

@ -2281,14 +2281,6 @@ static void build_subroutines(BuildCtx *ctx)
|ffstring_case string_lower, 65 |ffstring_case string_lower, 65
|ffstring_case string_upper, 97 |ffstring_case string_upper, 97
| |
|//-- Table library ------------------------------------------------------
|
|.ffunc_1 table_getn
| checktab CARG3; bne ->fff_fallback
| bl extern lj_tab_len // (GCtab *t)
| // Returns uint32_t (but less than 2^31).
| b ->fff_resi
|
|//-- Bit library -------------------------------------------------------- |//-- Bit library --------------------------------------------------------
| |
|.macro .ffunc_bit, name |.macro .ffunc_bit, name

View File

@ -2434,21 +2434,6 @@ static void build_subroutines(BuildCtx *ctx)
|ffstring_case string_lower, 0x41, 0x5a |ffstring_case string_lower, 0x41, 0x5a
|ffstring_case string_upper, 0x61, 0x7a |ffstring_case string_upper, 0x61, 0x7a
| |
|//-- Table library ------------------------------------------------------
|
|.ffunc_1 table_getn
| cmp dword [BASE+4], LJ_TTAB; jne ->fff_fallback
| mov RB, BASE // Save BASE.
| mov TAB:FCARG1, [BASE]
| call extern lj_tab_len@4 // LJ_FASTCALL (GCtab *t)
| // Length of table returned in eax (RD).
| mov BASE, RB // Restore BASE.
|.if DUALNUM
| mov RB, RD; jmp ->fff_resi
|.else
| cvtsi2sd xmm0, RD; jmp ->fff_resxmm0
|.endif
|
|//-- Bit library -------------------------------------------------------- |//-- Bit library --------------------------------------------------------
| |
|.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!). |.define TOBIT_BIAS, 0x59c00000 // 2^52 + 2^51 (float, not double!).