diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-11 13:56:03 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2024-12-11 13:56:03 -0300 |
commit | 25a491fe349fc52b69ece2ecbcb0b0189decb36f (patch) | |
tree | 1d7e61274c587e0838e2ea1fa1905bbc4d74c5f8 /lcode.c | |
parent | b4b616bdf2beb161b89930cc71a06936e8531b2c (diff) | |
download | lua-25a491fe349fc52b69ece2ecbcb0b0189decb36f.tar.gz lua-25a491fe349fc52b69ece2ecbcb0b0189decb36f.tar.bz2 lua-25a491fe349fc52b69ece2ecbcb0b0189decb36f.zip |
OP_SELF restricted to constant short strings
Optimize this opcode for the common case. For long names or method
calls after too many constants, operation can be coded as a move
followed by 'gettable'.
Diffstat (limited to 'lcode.c')
-rw-r--r-- | lcode.c | 43 |
1 files changed, 27 insertions, 16 deletions
@@ -1086,22 +1086,6 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { | |||
1086 | 1086 | ||
1087 | 1087 | ||
1088 | /* | 1088 | /* |
1089 | ** Emit SELF instruction (convert expression 'e' into 'e:key(e,'). | ||
1090 | */ | ||
1091 | void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { | ||
1092 | int ereg; | ||
1093 | luaK_exp2anyreg(fs, e); | ||
1094 | ereg = e->u.info; /* register where 'e' was placed */ | ||
1095 | freeexp(fs, e); | ||
1096 | e->u.info = fs->freereg; /* base register for op_self */ | ||
1097 | e->k = VNONRELOC; /* self expression has a fixed register */ | ||
1098 | luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */ | ||
1099 | codeABRK(fs, OP_SELF, e->u.info, ereg, key); | ||
1100 | freeexp(fs, key); | ||
1101 | } | ||
1102 | |||
1103 | |||
1104 | /* | ||
1105 | ** Negate condition 'e' (where 'e' is a comparison). | 1089 | ** Negate condition 'e' (where 'e' is a comparison). |
1106 | */ | 1090 | */ |
1107 | static void negatecondition (FuncState *fs, expdesc *e) { | 1091 | static void negatecondition (FuncState *fs, expdesc *e) { |
@@ -1276,6 +1260,33 @@ static int isSCnumber (expdesc *e, int *pi, int *isfloat) { | |||
1276 | 1260 | ||
1277 | 1261 | ||
1278 | /* | 1262 | /* |
1263 | ** Emit SELF instruction or equivalent: the code will convert | ||
1264 | ** expression 'e' into 'e.key(e,'. | ||
1265 | */ | ||
1266 | void luaK_self (FuncState *fs, expdesc *e, expdesc *key) { | ||
1267 | int ereg, base; | ||
1268 | luaK_exp2anyreg(fs, e); | ||
1269 | ereg = e->u.info; /* register where 'e' (the receiver) was placed */ | ||
1270 | freeexp(fs, e); | ||
1271 | base = e->u.info = fs->freereg; /* base register for op_self */ | ||
1272 | e->k = VNONRELOC; /* self expression has a fixed register */ | ||
1273 | luaK_reserveregs(fs, 2); /* method and 'self' produced by op_self */ | ||
1274 | lua_assert(key->k == VKSTR); | ||
1275 | /* is method name a short string in a valid K index? */ | ||
1276 | if (strisshr(key->u.strval) && luaK_exp2K(fs, key)) { | ||
1277 | /* can use 'self' opcode */ | ||
1278 | luaK_codeABCk(fs, OP_SELF, base, ereg, key->u.info, 0); | ||
1279 | } | ||
1280 | else { /* cannot use 'self' opcode; use move+gettable */ | ||
1281 | luaK_exp2anyreg(fs, key); /* put method name in a register */ | ||
1282 | luaK_codeABC(fs, OP_MOVE, base + 1, ereg, 0); /* copy self to base+1 */ | ||
1283 | luaK_codeABC(fs, OP_GETTABLE, base, ereg, key->u.info); /* get method */ | ||
1284 | } | ||
1285 | freeexp(fs, key); | ||
1286 | } | ||
1287 | |||
1288 | |||
1289 | /* | ||
1279 | ** Create expression 't[k]'. 't' must have its final result already in a | 1290 | ** Create expression 't[k]'. 't' must have its final result already in a |
1280 | ** register or upvalue. Upvalues can only be indexed by literal strings. | 1291 | ** register or upvalue. Upvalues can only be indexed by literal strings. |
1281 | ** Keys can be literal strings in the constant table or arbitrary | 1292 | ** Keys can be literal strings in the constant table or arbitrary |