aboutsummaryrefslogtreecommitdiff
path: root/lcode.c
diff options
context:
space:
mode:
Diffstat (limited to 'lcode.c')
-rw-r--r--lcode.c44
1 files changed, 20 insertions, 24 deletions
diff --git a/lcode.c b/lcode.c
index 252249b6..49b7ed4f 100644
--- a/lcode.c
+++ b/lcode.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lcode.c,v 1.77 2001/07/17 14:30:44 roberto Exp roberto $ 2** $Id: lcode.c,v 1.78 2001/07/24 17:19:07 roberto Exp roberto $
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*/
@@ -18,6 +18,7 @@
18#include "lobject.h" 18#include "lobject.h"
19#include "lopcodes.h" 19#include "lopcodes.h"
20#include "lparser.h" 20#include "lparser.h"
21#include "ltable.h"
21 22
22 23
23#define hasjumps(e) ((e)->t != (e)->f) 24#define hasjumps(e) ((e)->t != (e)->f)
@@ -222,38 +223,33 @@ static void freeexp (FuncState *fs, expdesc *e) {
222 223
223 224
224static int addk (FuncState *fs, TObject *k) { 225static int addk (FuncState *fs, TObject *k) {
225 Proto *f = fs->f; 226 const TObject *index = luaH_get(fs->h, k);
226 luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject, 227 if (ttype(index) == LUA_TNUMBER) {
227 MAXARG_Bc, l_s("constant table overflow")); 228 lua_assert(luaO_equalObj(&fs->f->k[(int)nvalue(index)], k));
228 setobj(&f->k[fs->nk], k); 229 return (int)nvalue(index);
229 return fs->nk++; 230 }
231 else { /* constant not found; create a new entry */
232 TObject o;
233 Proto *f = fs->f;
234 luaM_growvector(fs->L, f->k, fs->nk, f->sizek, TObject,
235 MAXARG_Bc, l_s("constant table overflow"));
236 setobj(&f->k[fs->nk], k);
237 setnvalue(&o, fs->nk);
238 setobj(luaH_set(fs->L, fs->h, k), &o);
239 return fs->nk++;
240 }
230} 241}
231 242
232 243
233int luaK_stringk (FuncState *fs, TString *s) { 244int luaK_stringk (FuncState *fs, TString *s) {
234 Proto *f = fs->f; 245 TObject o;
235 int c = s->tsv.constindex; 246 setsvalue(&o, s);
236 if (c >= fs->nk || ttype(&f->k[c]) != LUA_TSTRING || tsvalue(&f->k[c]) != s) { 247 return addk(fs, &o);
237 TObject o;
238 setsvalue(&o, s);
239 c = addk(fs, &o);
240 s->tsv.constindex = (unsigned short)c; /* hint for next time */
241 }
242 return c;
243} 248}
244 249
245 250
246static int number_constant (FuncState *fs, lua_Number r) { 251static int number_constant (FuncState *fs, lua_Number r) {
247 /* check whether `r' has appeared within the last LOOKBACKNUMS entries */
248 TObject o; 252 TObject o;
249 Proto *f = fs->f;
250 int c = fs->nk;
251 int lim = c < LOOKBACKNUMS ? 0 : c-LOOKBACKNUMS;
252 while (--c >= lim) {
253 if (ttype(&f->k[c]) == LUA_TNUMBER && nvalue(&f->k[c]) == r)
254 return c;
255 }
256 /* not found; create a new entry */
257 setnvalue(&o, r); 253 setnvalue(&o, r);
258 return addk(fs, &o); 254 return addk(fs, &o);
259} 255}