summaryrefslogtreecommitdiff
path: root/src/lj_parse.c
diff options
context:
space:
mode:
authorMike Pall <mike>2010-02-05 00:07:32 +0100
committerMike Pall <mike>2010-02-05 00:07:32 +0100
commitc8d55e850624bc237532fd103d1591b64d291081 (patch)
treef18a416f7deea9266e88d1e915c0b03c34df70bb /src/lj_parse.c
parent370c868c311368175045e788bcdd67a8be37dd76 (diff)
downloadluajit-c8d55e850624bc237532fd103d1591b64d291081.tar.gz
luajit-c8d55e850624bc237532fd103d1591b64d291081.tar.bz2
luajit-c8d55e850624bc237532fd103d1591b64d291081.zip
32/64 bit memory ref cleanup, part 1: GCproto ->bc and ->k.
Diffstat (limited to 'src/lj_parse.c')
-rw-r--r--src/lj_parse.c63
1 files changed, 36 insertions, 27 deletions
diff --git a/src/lj_parse.c b/src/lj_parse.c
index 8e94faa4..1a596d72 100644
--- a/src/lj_parse.c
+++ b/src/lj_parse.c
@@ -152,7 +152,7 @@ LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what)
152 152
153static BCPos getjump(FuncState *fs, BCPos pc) 153static BCPos getjump(FuncState *fs, BCPos pc)
154{ 154{
155 ptrdiff_t delta = bc_j(fs->pt->bc[pc]); 155 ptrdiff_t delta = bc_j(proto_ins(fs->pt, pc));
156 if ((BCPos)delta == NO_JMP) 156 if ((BCPos)delta == NO_JMP)
157 return NO_JMP; 157 return NO_JMP;
158 else 158 else
@@ -162,7 +162,7 @@ static BCPos getjump(FuncState *fs, BCPos pc)
162static int need_value(FuncState *fs, BCPos list) 162static int need_value(FuncState *fs, BCPos list)
163{ 163{
164 for (; list != NO_JMP; list = getjump(fs, list)) { 164 for (; list != NO_JMP; list = getjump(fs, list)) {
165 BCOp op = bc_op(fs->pt->bc[list >= 1 ? list-1 : list]); 165 BCOp op = bc_op(proto_ins(fs->pt, list >= 1 ? list-1 : list));
166 if (!(op == BC_ISTC || op == BC_ISFC)) return 1; 166 if (!(op == BC_ISTC || op == BC_ISFC)) return 1;
167 } 167 }
168 return 0; /* Not found. */ 168 return 0; /* Not found. */
@@ -170,7 +170,7 @@ static int need_value(FuncState *fs, BCPos list)
170 170
171static int patchtestreg(FuncState *fs, BCPos pc, BCReg reg) 171static int patchtestreg(FuncState *fs, BCPos pc, BCReg reg)
172{ 172{
173 BCIns *i = &fs->pt->bc[pc >= 1 ? pc-1 : pc]; 173 BCIns *i = proto_insptr(fs->pt, pc >= 1 ? pc-1 : pc);
174 BCOp op = bc_op(*i); 174 BCOp op = bc_op(*i);
175 if (!(op == BC_ISTC || op == BC_ISFC)) 175 if (!(op == BC_ISTC || op == BC_ISFC))
176 return 0; /* cannot patch other instructions */ 176 return 0; /* cannot patch other instructions */
@@ -191,7 +191,7 @@ static void removevalues(FuncState *fs, BCPos list)
191 191
192static void fixjump(FuncState *fs, BCPos pc, BCPos dest) 192static void fixjump(FuncState *fs, BCPos pc, BCPos dest)
193{ 193{
194 BCIns *jmp = &fs->pt->bc[pc]; 194 BCIns *jmp = proto_insptr(fs->pt, pc);
195 BCPos offset = dest-(pc+1)+BCBIAS_J; 195 BCPos offset = dest-(pc+1)+BCBIAS_J;
196 lua_assert(dest != NO_JMP); 196 lua_assert(dest != NO_JMP);
197 if (offset > BCMAX_D) 197 if (offset > BCMAX_D)
@@ -251,11 +251,14 @@ static BCPos emitINS(FuncState *fs, BCIns i)
251 fs->jpc = NO_JMP; 251 fs->jpc = NO_JMP;
252 pt = fs->pt; 252 pt = fs->pt;
253 if (LJ_UNLIKELY(fs->pc >= pt->sizebc)) { 253 if (LJ_UNLIKELY(fs->pc >= pt->sizebc)) {
254 BCIns *bc;
254 checklimit(fs, fs->pc, LJ_MAX_BCINS, "bytecode instructions"); 255 checklimit(fs, fs->pc, LJ_MAX_BCINS, "bytecode instructions");
255 lj_mem_growvec(fs->L, pt->bc, pt->sizebc, LJ_MAX_BCINS, BCIns); 256 bc = proto_bc(pt);
257 lj_mem_growvec(fs->L, bc, pt->sizebc, LJ_MAX_BCINS, BCIns);
258 setmref(pt->bc, bc);
256 lj_mem_growvec(fs->L, pt->lineinfo, pt->sizelineinfo, LJ_MAX_BCINS, BCLine); 259 lj_mem_growvec(fs->L, pt->lineinfo, pt->sizelineinfo, LJ_MAX_BCINS, BCLine);
257 } 260 }
258 pt->bc[fs->pc] = i; 261 *proto_insptr(pt, fs->pc) = i;
259 pt->lineinfo[fs->pc] = fs->ls->lastline; 262 pt->lineinfo[fs->pc] = fs->ls->lastline;
260 return fs->pc++; 263 return fs->pc++;
261} 264}
@@ -264,15 +267,16 @@ static BCPos emitINS(FuncState *fs, BCIns i)
264#define emitAD(fs, o, a, d) emitINS(fs, BCINS_AD(o, a, d)) 267#define emitAD(fs, o, a, d) emitINS(fs, BCINS_AD(o, a, d))
265#define emitAJ(fs, o, a, j) emitINS(fs, BCINS_AJ(o, a, j)) 268#define emitAJ(fs, o, a, j) emitINS(fs, BCINS_AJ(o, a, j))
266 269
267#define bcptr(fs, e) (&(fs)->pt->bc[(e)->u.s.info]) 270#define bcptr(fs, e) (proto_insptr((fs)->pt, (e)->u.s.info))
268 271
269static BCPos emit_jump(FuncState *fs) 272static BCPos emit_jump(FuncState *fs)
270{ 273{
271 BCPos jpc = fs->jpc; /* save list of jumps to here */ 274 BCPos jpc = fs->jpc; /* save list of jumps to here */
272 BCPos j = fs->pc - 1; 275 BCPos j = fs->pc - 1;
273 fs->jpc = NO_JMP; 276 fs->jpc = NO_JMP;
274 if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(fs->pt->bc[j]) == BC_UCLO) 277 if ((int32_t)j >= (int32_t)fs->lasttarget &&
275 setbc_j(&fs->pt->bc[j], NO_JMP); 278 bc_op(proto_ins(fs->pt, j)) == BC_UCLO)
279 setbc_j(proto_insptr(fs->pt, j), NO_JMP);
276 else 280 else
277 j = emitAJ(fs, BC_JMP, fs->freereg, NO_JMP); 281 j = emitAJ(fs, BC_JMP, fs->freereg, NO_JMP);
278 concatjumps(fs, &j, jpc); /* keep them on hold */ 282 concatjumps(fs, &j, jpc); /* keep them on hold */
@@ -334,7 +338,7 @@ static void nilK(FuncState *fs, BCReg from, BCReg n)
334 BCIns *pr; 338 BCIns *pr;
335 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */ 339 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
336 BCReg pfrom, pto; 340 BCReg pfrom, pto;
337 pr = &fs->pt->bc[fs->pc-1]; 341 pr = proto_insptr(fs->pt, fs->pc-1);
338 pfrom = bc_a(*pr); 342 pfrom = bc_a(*pr);
339 switch (bc_op(*pr)) { 343 switch (bc_op(*pr)) {
340 case BC_KPRI: 344 case BC_KPRI:
@@ -1136,21 +1140,22 @@ static void collectk(FuncState *fs, GCproto *pt)
1136 Node *node; 1140 Node *node;
1137 BCReg nkgc; 1141 BCReg nkgc;
1138 MSize i, hmask, sizek; 1142 MSize i, hmask, sizek;
1139 GCRef *kstart; 1143 GCRef *kptr;
1140 checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants"); 1144 checklimitgt(fs, fs->nkn, BCMAX_D+1, "constants");
1141 checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants"); 1145 checklimitgt(fs, fs->nkgc, BCMAX_D+1, "constants");
1142 nkgc = round_nkgc(fs->nkgc); 1146 nkgc = round_nkgc(fs->nkgc);
1143 sizek = (MSize)(nkgc*sizeof(GCRef) + fs->nkn*sizeof(lua_Number)); 1147 sizek = (MSize)(nkgc*sizeof(GCRef) + fs->nkn*sizeof(lua_Number));
1144 kstart = lj_mem_newt(fs->L, sizek, GCRef); 1148 kptr = lj_mem_newt(fs->L, sizek, GCRef);
1145 if (nkgc) setgcrefnull(kstart[0]); /* May be uninitialized otherwise. */ 1149 if (nkgc) setgcrefnull(kptr[0]); /* May be uninitialized otherwise. */
1146 pt->k.gc = kstart + nkgc; 1150 kptr += nkgc;
1151 setmref(pt->k, kptr);
1147 pt->sizekn = fs->nkn; 1152 pt->sizekn = fs->nkn;
1148 pt->sizekgc = fs->nkgc; 1153 pt->sizekgc = fs->nkgc;
1149 kt = fs->kt; 1154 kt = fs->kt;
1150 array = tvref(kt->array); 1155 array = tvref(kt->array);
1151 for (i = 0; i < kt->asize; i++) 1156 for (i = 0; i < kt->asize; i++)
1152 if (tvisnum(&array[i])) 1157 if (tvisnum(&array[i]))
1153 pt->k.n[array[i].u32.lo] = cast_num(i); 1158 ((lua_Number *)kptr)[array[i].u32.lo] = cast_num(i);
1154 node = noderef(kt->node); 1159 node = noderef(kt->node);
1155 hmask = kt->hmask; 1160 hmask = kt->hmask;
1156 for (i = 0; i <= hmask; i++) { 1161 for (i = 0; i <= hmask; i++) {
@@ -1158,10 +1163,10 @@ static void collectk(FuncState *fs, GCproto *pt)
1158 if (tvisnum(&n->val)) { 1163 if (tvisnum(&n->val)) {
1159 ptrdiff_t kidx = (ptrdiff_t)n->val.u32.lo; 1164 ptrdiff_t kidx = (ptrdiff_t)n->val.u32.lo;
1160 if (tvisnum(&n->key)) { 1165 if (tvisnum(&n->key)) {
1161 pt->k.n[kidx] = numV(&n->key); 1166 ((lua_Number *)kptr)[kidx] = numV(&n->key);
1162 } else { 1167 } else {
1163 GCobj *o = gcV(&n->key); 1168 GCobj *o = gcV(&n->key);
1164 setgcref(pt->k.gc[~kidx], o); 1169 setgcref(kptr[~kidx], o);
1165 lj_gc_objbarrier(fs->L, pt, o); 1170 lj_gc_objbarrier(fs->L, pt, o);
1166 } 1171 }
1167 } 1172 }
@@ -1184,7 +1189,7 @@ static void finalret(FuncState *fs, GCproto *pt)
1184{ 1189{
1185 BCPos lastpc = fs->pc; 1190 BCPos lastpc = fs->pc;
1186 if (lastpc > fs->lasttarget) { 1191 if (lastpc > fs->lasttarget) {
1187 switch (bc_op(pt->bc[lastpc-1])) { 1192 switch (bc_op(proto_ins(pt, lastpc-1))) {
1188 case BC_CALLMT: case BC_CALLT: 1193 case BC_CALLMT: case BC_CALLT:
1189 case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: 1194 case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1190 goto suppress_return; /* already got a return */ 1195 goto suppress_return; /* already got a return */
@@ -1195,21 +1200,22 @@ static void finalret(FuncState *fs, GCproto *pt)
1195 emitAJ(fs, BC_UCLO, 0, 0); 1200 emitAJ(fs, BC_UCLO, 0, 0);
1196 emitAD(fs, BC_RET0, 0, 1); /* final return */ 1201 emitAD(fs, BC_RET0, 0, 1); /* final return */
1197suppress_return: 1202suppress_return:
1198 /* may need to fixup returns encoded before first function was created */ 1203 /* May need to fixup returns encoded before first function was created. */
1199 if (fs->pt->flags & PROTO_FIXUP_RETURN) { 1204 if (fs->pt->flags & PROTO_FIXUP_RETURN) {
1200 BCPos pc; 1205 BCPos pc;
1201 for (pc = 0; pc < lastpc; pc++) { 1206 for (pc = 0; pc < lastpc; pc++) {
1202 BCIns i = pt->bc[pc]; 1207 BCIns i = proto_ins(pt, pc);
1203 BCPos offset; 1208 BCPos offset;
1204 switch (bc_op(i)) { 1209 switch (bc_op(i)) {
1205 case BC_CALLMT: case BC_CALLT: 1210 case BC_CALLMT: case BC_CALLT:
1206 case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1: 1211 case BC_RETM: case BC_RET: case BC_RET0: case BC_RET1:
1207 offset = emitINS(fs, i)-(pc+1)+BCBIAS_J; /* copy return ins */ 1212 offset = emitINS(fs, i)-(pc+1)+BCBIAS_J; /* Copy return ins. */
1208 if (offset > BCMAX_D) 1213 if (offset > BCMAX_D)
1209 err_syntax(fs->ls, LJ_ERR_XFIXUP); 1214 err_syntax(fs->ls, LJ_ERR_XFIXUP);
1210 pt->bc[pc] = BCINS_AD(BC_UCLO, 0, offset); /* replace w/ UCLO+branch */ 1215 /* Replace with UCLO plus branch. */
1216 *proto_insptr(pt, pc) = BCINS_AD(BC_UCLO, 0, offset);
1211 break; 1217 break;
1212 case BC_UCLO: return; /* we're done */ 1218 case BC_UCLO: return; /* We're done. */
1213 default: break; 1219 default: break;
1214 } 1220 }
1215 } 1221 }
@@ -1221,9 +1227,12 @@ static void close_func(LexState *ls)
1221 lua_State *L = ls->L; 1227 lua_State *L = ls->L;
1222 FuncState *fs = ls->fs; 1228 FuncState *fs = ls->fs;
1223 GCproto *pt = fs->pt; 1229 GCproto *pt = fs->pt;
1230 BCIns *bc;
1224 removevars(ls, 0); 1231 removevars(ls, 0);
1225 finalret(fs, pt); 1232 finalret(fs, pt);
1226 lj_mem_reallocvec(L, pt->bc, pt->sizebc, fs->pc, BCIns); 1233 bc = proto_bc(pt);
1234 lj_mem_reallocvec(L, bc, pt->sizebc, fs->pc, BCIns);
1235 setmref(pt->bc, bc);
1227 pt->sizebc = fs->pc; 1236 pt->sizebc = fs->pc;
1228 collectk(fs, pt); 1237 collectk(fs, pt);
1229 collectuv(fs, pt); 1238 collectuv(fs, pt);
@@ -1336,7 +1345,7 @@ static void constructor(LexState *ls, ExpDesc *e)
1336 BCReg kidx; 1345 BCReg kidx;
1337 t = lj_tab_new(fs->L, 0, 0); 1346 t = lj_tab_new(fs->L, 0, 0);
1338 kidx = gcK(fs, obj2gco(t), LJ_TTAB); 1347 kidx = gcK(fs, obj2gco(t), LJ_TTAB);
1339 fs->pt->bc[pc] = BCINS_AD(BC_TDUP, freg-1, kidx); 1348 *proto_insptr(fs->pt, pc) = BCINS_AD(BC_TDUP, freg-1, kidx);
1340 } 1349 }
1341 vcall = 0; 1350 vcall = 0;
1342 kexp2tv(&k, &key); 1351 kexp2tv(&k, &key);
@@ -1353,7 +1362,7 @@ static void constructor(LexState *ls, ExpDesc *e)
1353 } 1362 }
1354 checkmatch(ls, '}', '{', line); 1363 checkmatch(ls, '}', '{', line);
1355 if (vcall) { 1364 if (vcall) {
1356 BCIns *i = &fs->pt->bc[fs->pc-1]; 1365 BCIns *i = proto_insptr(fs->pt, fs->pc-1);
1357 ExpDesc en; 1366 ExpDesc en;
1358 lua_assert(bc_a(*i)==freg && bc_op(*i) == (narr>256?BC_TSETV:BC_TSETB)); 1367 lua_assert(bc_a(*i)==freg && bc_op(*i) == (narr>256?BC_TSETV:BC_TSETB));
1359 init_exp(&en, VKNUM, 0); 1368 init_exp(&en, VKNUM, 0);
@@ -1373,7 +1382,7 @@ static void constructor(LexState *ls, ExpDesc *e)
1373 if (!needarr) narr = 0; 1382 if (!needarr) narr = 0;
1374 else if (narr < 3) narr = 3; 1383 else if (narr < 3) narr = 3;
1375 else if (narr > 0x7ff) narr = 0x7ff; 1384 else if (narr > 0x7ff) narr = 0x7ff;
1376 setbc_d(&fs->pt->bc[pc], (uint32_t)narr | (hsize2hbits(nhash) << 11)); 1385 setbc_d(proto_insptr(fs->pt, pc), (uint32_t)narr|(hsize2hbits(nhash)<<11));
1377 } 1386 }
1378} 1387}
1379 1388