aboutsummaryrefslogtreecommitdiff
path: root/lua.stx
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-07-31 17:46:59 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-07-31 17:46:59 -0300
commitabbf14cd32bf83d5ea5ab70977e0653a03b455c5 (patch)
tree9aecab91be6e11b04ae0c0fc692d4b19e94e5298 /lua.stx
parente8292f076d5dc1b68652467a28cea64c18517631 (diff)
downloadlua-abbf14cd32bf83d5ea5ab70977e0653a03b455c5.tar.gz
lua-abbf14cd32bf83d5ea5ab70977e0653a03b455c5.tar.bz2
lua-abbf14cd32bf83d5ea5ab70977e0653a03b455c5.zip
small optimizations; try to find previous use when coding a real constant.
Diffstat (limited to 'lua.stx')
-rw-r--r--lua.stx82
1 files changed, 45 insertions, 37 deletions
diff --git a/lua.stx b/lua.stx
index 66b27e14..45dbb7ee 100644
--- a/lua.stx
+++ b/lua.stx
@@ -1,6 +1,6 @@
1%{ 1%{
2 2
3char *rcs_luastx = "$Id: lua.stx,v 3.48 1997/07/29 20:38:45 roberto Exp roberto $"; 3char *rcs_luastx = "$Id: lua.stx,v 3.49 1997/07/30 22:00:50 roberto Exp roberto $";
4 4
5#include <stdlib.h> 5#include <stdlib.h>
6 6
@@ -50,8 +50,6 @@ static Long varbuffer[MAXVAR]; /* variables in an assignment list;
50static int nvarbuffer=0; /* number of variables at a list */ 50static int nvarbuffer=0; /* number of variables at a list */
51 51
52 52
53#define MAXFIELDS FIELDS_PER_FLUSH*2
54
55int lua_debug = 0; 53int lua_debug = 0;
56 54
57/* Internal functions */ 55/* Internal functions */
@@ -106,65 +104,75 @@ static void code_constant (int c)
106 104
107static int next_constant (void) 105static int next_constant (void)
108{ 106{
109 if (currState->f->nconsts >= currState->maxconsts) { 107 TFunc *f = currState->f;
108 if (f->nconsts >= currState->maxconsts) {
110 currState->maxconsts = 109 currState->maxconsts =
111 growvector(&currState->f->consts, currState->maxconsts, 110 growvector(&f->consts, currState->maxconsts, TObject,
112 TObject, constantEM, MAX_WORD); 111 constantEM, MAX_WORD);
113 } 112 }
114 return currState->f->nconsts++; 113 return f->nconsts++;
115} 114}
116 115
117 116
118static int string_constant (TaggedString *s) 117static int string_constant (TaggedString *s)
119{ 118{
119 TFunc *f = currState->f;
120 int c = s->u.s.constindex; 120 int c = s->u.s.constindex;
121 if (!(0 <= c && c < currState->f->nconsts && 121 if (!(0 <= c && c < f->nconsts &&
122 ttype(&currState->f->consts[c]) == LUA_T_STRING && 122 ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) {
123 tsvalue(&currState->f->consts[c]) == s)) {
124 c = next_constant(); 123 c = next_constant();
125 ttype(&currState->f->consts[c]) = LUA_T_STRING; 124 ttype(&f->consts[c]) = LUA_T_STRING;
126 tsvalue(&currState->f->consts[c]) = s; 125 tsvalue(&f->consts[c]) = s;
127 s->u.s.constindex = c; /* hint for next time */ 126 s->u.s.constindex = c; /* hint for next time */
128 luaI_releasestring(s);
129 } 127 }
128 luaI_releasestring(s);
130 return c; 129 return c;
131} 130}
132 131
133 132
134static void code_string (TaggedString *s) 133static void code_string (TaggedString *s)
135{ 134{
136 int c = string_constant(s); 135 code_constant(string_constant(s));
137 code_constant(c);
138} 136}
139 137
140static void code_float (real n) 138
139#define LIM 10
140static int real_constant (real r)
141{ 141{
142 int c = next_constant(); 142 /* check whether 'r' has appeared within the last LIM entries */
143 ttype(&currState->f->consts[c]) = LUA_T_NUMBER; 143 TObject *cnt = currState->f->consts;
144 nvalue(&currState->f->consts[c]) = n; 144 int c = currState->f->nconsts;
145 code_constant(c); 145 int lim = c < LIM ? 0 : c-LIM;
146 while (--c >= lim) {
147 if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r)
148 return c;
149 }
150 /* not found; create a new entry */
151 c = next_constant();
152 cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */
153 ttype(&cnt[c]) = LUA_T_NUMBER;
154 nvalue(&cnt[c]) = r;
155 return c;
146} 156}
147 157
148 158
149static void code_number (float f) 159static void code_number (real f)
150{ 160{
151 Word i; 161 Word i;
152 if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) { 162 if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) {
153 /* f has an (short) integer value */ 163 /* f has an (short) integer value */
154 if (i <= 2) code_byte(PUSH0 + i); 164 if (i <= 2) code_byte(PUSH0 + i);
155 else if (i <= 255) 165 else if (i <= 255) {
156 { 166 code_byte(PUSHBYTE);
157 code_byte(PUSHBYTE); 167 code_byte(i);
158 code_byte(i); 168 }
159 } 169 else {
160 else 170 code_byte(PUSHWORD);
161 { 171 code_word(i);
162 code_byte(PUSHWORD); 172 }
163 code_word(i);
164 }
165 } 173 }
166 else 174 else
167 code_float(f); 175 code_constant(real_constant(f));
168} 176}
169 177
170 178
@@ -483,7 +491,7 @@ void lua_parse (TFunc *tf)
483%union 491%union
484{ 492{
485 int vInt; 493 int vInt;
486 float vFloat; 494 real vReal;
487 char *pChar; 495 char *pChar;
488 Long vLong; 496 Long vLong;
489 TaggedString *pTStr; 497 TaggedString *pTStr;
@@ -498,7 +506,7 @@ void lua_parse (TFunc *tf)
498%token LOCAL 506%token LOCAL
499%token FUNCTION 507%token FUNCTION
500%token DOTS 508%token DOTS
501%token <vFloat> NUMBER 509%token <vReal> NUMBER
502%token <pTStr> NAME STRING 510%token <pTStr> NAME STRING
503 511
504%type <vLong> PrepJump 512%type <vLong> PrepJump