diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-10-04 16:01:53 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-10-04 16:01:53 -0300 |
| commit | 5ee87acd6b920a6a122277cd817aea4cfd618671 (patch) | |
| tree | 5e931c1db32c2be6378fde498ffcc5ee9dec0d6a /lcode.c | |
| parent | 5d834bdf571ef3ee343b21fde2d49de1e4ea08b4 (diff) | |
| download | lua-5ee87acd6b920a6a122277cd817aea4cfd618671.tar.gz lua-5ee87acd6b920a6a122277cd817aea4cfd618671.tar.bz2 lua-5ee87acd6b920a6a122277cd817aea4cfd618671.zip | |
small optimization for boolean constants + new format for SETLIST opcode
Diffstat (limited to 'lcode.c')
| -rw-r--r-- | lcode.c | 29 |
1 files changed, 26 insertions, 3 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lcode.c,v 2.5 2004/07/16 13:30:53 roberto Exp roberto $ | 2 | ** $Id: lcode.c,v 2.6 2004/08/24 20:09:11 roberto Exp $ |
| 3 | ** Code generator for Lua | 3 | ** Code generator for Lua |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -243,7 +243,14 @@ int luaK_numberK (FuncState *fs, lua_Number r) { | |||
| 243 | } | 243 | } |
| 244 | 244 | ||
| 245 | 245 | ||
| 246 | static int nil_constant (FuncState *fs) { | 246 | static int boolK (FuncState *fs, int b) { |
| 247 | TValue o; | ||
| 248 | setbvalue(&o, b); | ||
| 249 | return addk(fs, &o, &o); | ||
| 250 | } | ||
| 251 | |||
| 252 | |||
| 253 | static int nilK (FuncState *fs) { | ||
| 247 | TValue k, v; | 254 | TValue k, v; |
| 248 | setnilvalue(&v); | 255 | setnilvalue(&v); |
| 249 | /* cannot use nil as key; instead use table itself to represent nil */ | 256 | /* cannot use nil as key; instead use table itself to represent nil */ |
| @@ -417,9 +424,11 @@ void luaK_exp2val (FuncState *fs, expdesc *e) { | |||
| 417 | int luaK_exp2RK (FuncState *fs, expdesc *e) { | 424 | int luaK_exp2RK (FuncState *fs, expdesc *e) { |
| 418 | luaK_exp2val(fs, e); | 425 | luaK_exp2val(fs, e); |
| 419 | switch (e->k) { | 426 | switch (e->k) { |
| 427 | case VTRUE: | ||
| 428 | case VFALSE: | ||
| 420 | case VNIL: { | 429 | case VNIL: { |
| 421 | if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ | 430 | if (fs->nk <= MAXINDEXRK) { /* constant fit in RK operand? */ |
| 422 | e->info = nil_constant(fs); | 431 | e->info = (e->k == VNIL) ? nilK(fs) : boolK(fs, (e->k == VTRUE)); |
| 423 | e->k = VK; | 432 | e->k = VK; |
| 424 | return RKASK(e->info); | 433 | return RKASK(e->info); |
| 425 | } | 434 | } |
| @@ -735,3 +744,17 @@ int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) { | |||
| 735 | return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); | 744 | return luaK_code(fs, CREATE_ABx(o, a, bc), fs->ls->lastline); |
| 736 | } | 745 | } |
| 737 | 746 | ||
| 747 | |||
| 748 | void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) { | ||
| 749 | int c = (nelems - 1)/LFIELDS_PER_FLUSH + 1; | ||
| 750 | int b = (tostore == LUA_MULTRET) ? 0 : tostore; | ||
| 751 | lua_assert(tostore != 0); | ||
| 752 | if (c <= MAXARG_C) | ||
| 753 | luaK_codeABC(fs, OP_SETLIST, base, b, c); | ||
| 754 | else { | ||
| 755 | luaK_codeABC(fs, OP_SETLIST, base, b, 0); | ||
| 756 | luaK_code(fs, cast(Instruction, c), fs->ls->lastline); | ||
| 757 | } | ||
| 758 | fs->freereg = base + 1; /* free registers with list values */ | ||
| 759 | } | ||
| 760 | |||
