diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-08-27 12:16:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2001-08-27 12:16:28 -0300 |
commit | c3d72096c4841ebc04a0615917d71f22aaa5e5ff (patch) | |
tree | 45b143ed4284ba9b30fdae965aef0a047aab6deb | |
parent | 7afc74ff07907294a2162600023a9c61a64cdbd5 (diff) | |
download | lua-c3d72096c4841ebc04a0615917d71f22aaa5e5ff.tar.gz lua-c3d72096c4841ebc04a0615917d71f22aaa5e5ff.tar.bz2 lua-c3d72096c4841ebc04a0615917d71f22aaa5e5ff.zip |
use a table to find (and reuse) constants when parsing
-rw-r--r-- | lcode.c | 44 | ||||
-rw-r--r-- | llimits.h | 8 | ||||
-rw-r--r-- | lobject.h | 5 | ||||
-rw-r--r-- | lparser.c | 6 | ||||
-rw-r--r-- | lparser.h | 4 | ||||
-rw-r--r-- | lstring.c | 3 |
6 files changed, 32 insertions, 38 deletions
@@ -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 | ||
224 | static int addk (FuncState *fs, TObject *k) { | 225 | static 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 | ||
233 | int luaK_stringk (FuncState *fs, TString *s) { | 244 | int 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 | ||
246 | static int number_constant (FuncState *fs, lua_Number r) { | 251 | static 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 | } |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llimits.h,v 1.29 2001/06/05 18:17:01 roberto Exp roberto $ | 2 | ** $Id: llimits.h,v 1.30 2001/06/05 20:01:09 roberto Exp roberto $ |
3 | ** Limits, basic types, and some other `installation-dependent' definitions | 3 | ** Limits, basic types, and some other `installation-dependent' definitions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -124,10 +124,4 @@ typedef unsigned long Instruction; | |||
124 | 124 | ||
125 | 125 | ||
126 | 126 | ||
127 | /* maximum lookback to find a real constant (for code generation) */ | ||
128 | #ifndef LOOKBACKNUMS | ||
129 | #define LOOKBACKNUMS 40 /* arbitrary constant */ | ||
130 | #endif | ||
131 | |||
132 | |||
133 | #endif | 127 | #endif |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lobject.h,v 1.108 2001/06/28 14:48:44 roberto Exp roberto $ | 2 | ** $Id: lobject.h,v 1.109 2001/06/28 14:56:25 roberto Exp roberto $ |
3 | ** Type definitions for Lua objects | 3 | ** Type definitions for Lua objects |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -90,8 +90,7 @@ typedef union TString { | |||
90 | struct { | 90 | struct { |
91 | lu_hash hash; | 91 | lu_hash hash; |
92 | size_t len; | 92 | size_t len; |
93 | unsigned short constindex; /* hint to reuse constants */ | 93 | int marked; |
94 | short marked; | ||
95 | union TString *nexthash; /* chain for hash table */ | 94 | union TString *nexthash; /* chain for hash table */ |
96 | } tsv; | 95 | } tsv; |
97 | } TString; | 96 | } TString; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.c,v 1.152 2001/07/10 20:02:22 roberto Exp roberto $ | 2 | ** $Id: lparser.c,v 1.153 2001/08/10 20:53:03 roberto Exp roberto $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -318,6 +318,7 @@ static void open_func (LexState *ls, FuncState *fs) { | |||
318 | fs->jlt = NO_JUMP; | 318 | fs->jlt = NO_JUMP; |
319 | fs->freereg = 0; | 319 | fs->freereg = 0; |
320 | fs->nk = 0; | 320 | fs->nk = 0; |
321 | fs->h = luaH_new(ls->L, 0); | ||
321 | fs->np = 0; | 322 | fs->np = 0; |
322 | fs->nlineinfo = 0; | 323 | fs->nlineinfo = 0; |
323 | fs->nlocvars = 0; | 324 | fs->nlocvars = 0; |
@@ -339,6 +340,9 @@ static void close_func (LexState *ls) { | |||
339 | luaK_codeABC(fs, OP_RETURN, 0, 0, 0); /* final return */ | 340 | luaK_codeABC(fs, OP_RETURN, 0, 0, 0); /* final return */ |
340 | luaK_getlabel(fs); /* close eventual list of pending jumps */ | 341 | luaK_getlabel(fs); /* close eventual list of pending jumps */ |
341 | removelocalvars(ls, fs->nactloc); | 342 | removelocalvars(ls, fs->nactloc); |
343 | lua_assert(G(L)->roottable == fs->h); | ||
344 | G(L)->roottable = fs->h->next; | ||
345 | luaH_free(L, fs->h); | ||
342 | luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); | 346 | luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction); |
343 | f->sizecode = fs->pc; | 347 | f->sizecode = fs->pc; |
344 | luaM_reallocvector(L, f->k, f->sizek, fs->nk, TObject); | 348 | luaM_reallocvector(L, f->k, f->sizek, fs->nk, TObject); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lparser.h,v 1.32 2001/06/28 14:56:25 roberto Exp roberto $ | 2 | ** $Id: lparser.h,v 1.33 2001/08/10 20:53:03 roberto Exp roberto $ |
3 | ** Lua Parser | 3 | ** Lua Parser |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -8,6 +8,7 @@ | |||
8 | #define lparser_h | 8 | #define lparser_h |
9 | 9 | ||
10 | #include "lobject.h" | 10 | #include "lobject.h" |
11 | #include "ltable.h" | ||
11 | #include "lzio.h" | 12 | #include "lzio.h" |
12 | 13 | ||
13 | 14 | ||
@@ -53,6 +54,7 @@ typedef struct FuncState { | |||
53 | int jlt; /* list of jumps to `lasttarget' */ | 54 | int jlt; /* list of jumps to `lasttarget' */ |
54 | int freereg; /* first free register */ | 55 | int freereg; /* first free register */ |
55 | int nk; /* number of elements in `k' */ | 56 | int nk; /* number of elements in `k' */ |
57 | Hash *h; /* table to find (and reuse) elements in `k' */ | ||
56 | int np; /* number of elements in `p' */ | 58 | int np; /* number of elements in `p' */ |
57 | int nlineinfo; /* number of elements in `lineinfo' */ | 59 | int nlineinfo; /* number of elements in `lineinfo' */ |
58 | int nlocvars; /* number of elements in `locvars' */ | 60 | int nlocvars; /* number of elements in `locvars' */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.64 2001/06/07 15:01:21 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.65 2001/06/15 20:36:57 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -54,7 +54,6 @@ static TString *newlstr (lua_State *L, const l_char *str, size_t l, lu_hash h) { | |||
54 | ts->tsv.len = l; | 54 | ts->tsv.len = l; |
55 | ts->tsv.hash = h; | 55 | ts->tsv.hash = h; |
56 | ts->tsv.marked = 0; | 56 | ts->tsv.marked = 0; |
57 | ts->tsv.constindex = 0; | ||
58 | memcpy(getstr(ts), str, l*sizeof(l_char)); | 57 | memcpy(getstr(ts), str, l*sizeof(l_char)); |
59 | getstr(ts)[l] = l_c('\0'); /* ending 0 */ | 58 | getstr(ts)[l] = l_c('\0'); /* ending 0 */ |
60 | tb = &G(L)->strt; | 59 | tb = &G(L)->strt; |