aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--src/lib_jit.c6
-rw-r--r--src/lj_dispatch.c6
-rw-r--r--src/lj_err.c14
-rw-r--r--src/lj_func.c8
-rw-r--r--src/lj_gc.c2
-rw-r--r--src/lj_gdbjit.c5
-rw-r--r--src/lj_obj.h22
-rw-r--r--src/lj_parse.c63
-rw-r--r--src/lj_record.c14
-rw-r--r--src/lj_trace.c10
10 files changed, 84 insertions, 66 deletions
diff --git a/src/lib_jit.c b/src/lib_jit.c
index 0728fc3a..ef6fd465 100644
--- a/src/lib_jit.c
+++ b/src/lib_jit.c
@@ -209,7 +209,7 @@ LJLIB_CF(jit_util_funcbc)
209 GCproto *pt = check_Lproto(L, 0); 209 GCproto *pt = check_Lproto(L, 0);
210 BCPos pc = (BCPos)lj_lib_checkint(L, 2) - 1; 210 BCPos pc = (BCPos)lj_lib_checkint(L, 2) - 1;
211 if (pc < pt->sizebc) { 211 if (pc < pt->sizebc) {
212 BCIns ins = pt->bc[pc]; 212 BCIns ins = proto_ins(pt, pc);
213 BCOp op = bc_op(ins); 213 BCOp op = bc_op(ins);
214 lua_assert(op < BC__MAX); 214 lua_assert(op < BC__MAX);
215 setintV(L->top, ins); 215 setintV(L->top, ins);
@@ -227,12 +227,12 @@ LJLIB_CF(jit_util_funck)
227 ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2); 227 ptrdiff_t idx = (ptrdiff_t)lj_lib_checkint(L, 2);
228 if (idx >= 0) { 228 if (idx >= 0) {
229 if (idx < (ptrdiff_t)pt->sizekn) { 229 if (idx < (ptrdiff_t)pt->sizekn) {
230 setnumV(L->top-1, pt->k.n[idx]); 230 setnumV(L->top-1, proto_knum(pt, idx));
231 return 1; 231 return 1;
232 } 232 }
233 } else { 233 } else {
234 if (~idx < (ptrdiff_t)pt->sizekgc) { 234 if (~idx < (ptrdiff_t)pt->sizekgc) {
235 GCobj *gc = gcref(pt->k.gc[idx]); 235 GCobj *gc = proto_kgc(pt, idx);
236 setgcV(L, L->top-1, &gc->gch, ~gc->gch.gct); 236 setgcV(L, L->top-1, &gc->gch, ~gc->gch.gct);
237 return 1; 237 return 1;
238 } 238 }
diff --git a/src/lj_dispatch.c b/src/lj_dispatch.c
index a94afea9..09cb2f5d 100644
--- a/src/lj_dispatch.c
+++ b/src/lj_dispatch.c
@@ -117,7 +117,7 @@ static void setptmode_all(global_State *g, GCproto *pt, int mode)
117{ 117{
118 ptrdiff_t i; 118 ptrdiff_t i;
119 for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) { 119 for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) {
120 GCobj *o = gcref(pt->k.gc[i]); 120 GCobj *o = proto_kgc(pt, i);
121 if (o->gch.gct == ~LJ_TPROTO) { 121 if (o->gch.gct == ~LJ_TPROTO) {
122 setptmode(g, gco2pt(o), mode); 122 setptmode(g, gco2pt(o), mode);
123 setptmode_all(g, gco2pt(o), mode); 123 setptmode_all(g, gco2pt(o), mode);
@@ -302,8 +302,8 @@ void LJ_FASTCALL lj_dispatch_ins(lua_State *L, const BCIns *pc)
302 callhook(L, LUA_HOOKCOUNT, -1); 302 callhook(L, LUA_HOOKCOUNT, -1);
303 } 303 }
304 if ((g->hookmask & LUA_MASKLINE) && pt->lineinfo) { 304 if ((g->hookmask & LUA_MASKLINE) && pt->lineinfo) {
305 BCPos npc = (BCPos)(pc - pt->bc)-1; 305 BCPos npc = proto_bcpos(pt, pc) - 1;
306 BCPos opc = (BCPos)(oldpc - pt->bc)-1; 306 BCPos opc = proto_bcpos(pt, oldpc) - 1;
307 BCLine line = pt->lineinfo[npc]; 307 BCLine line = pt->lineinfo[npc];
308 if (npc == 0 || pc <= oldpc || 308 if (npc == 0 || pc <= oldpc ||
309 opc >= pt->sizebc || line != pt->lineinfo[opc]) { 309 opc >= pt->sizebc || line != pt->lineinfo[opc]) {
diff --git a/src/lj_err.c b/src/lj_err.c
index 56ca0c37..d1e705ea 100644
--- a/src/lj_err.c
+++ b/src/lj_err.c
@@ -120,7 +120,7 @@ static BCPos currentpc(lua_State *L, GCfunc *fn, cTValue *nextframe)
120 ins = cframe_pc(cf); 120 ins = cframe_pc(cf);
121 } 121 }
122 } 122 }
123 return (BCPos)((ins - funcproto(fn)->bc) - 1); 123 return proto_bcpos(funcproto(fn), ins) - 1;
124} 124}
125 125
126static BCLine currentline(lua_State *L, GCfunc *fn, cTValue *nextframe) 126static BCLine currentline(lua_State *L, GCfunc *fn, cTValue *nextframe)
@@ -149,9 +149,9 @@ static const char *getobjname(GCproto *pt, const BCIns *ip, BCReg slot,
149{ 149{
150 const char *lname; 150 const char *lname;
151restart: 151restart:
152 lname = getvarname(pt, (BCPos)(ip - pt->bc), slot); 152 lname = getvarname(pt, proto_bcpos(pt, ip), slot);
153 if (lname != NULL) { *name = lname; return "local"; } 153 if (lname != NULL) { *name = lname; return "local"; }
154 while (--ip >= pt->bc) { 154 while (--ip >= proto_bc(pt)) {
155 BCIns ins = *ip; 155 BCIns ins = *ip;
156 BCOp op = bc_op(ins); 156 BCOp op = bc_op(ins);
157 BCReg ra = bc_a(ins); 157 BCReg ra = bc_a(ins);
@@ -164,11 +164,11 @@ restart:
164 if (ra == slot) { slot = bc_d(ins); goto restart; } 164 if (ra == slot) { slot = bc_d(ins); goto restart; }
165 break; 165 break;
166 case BC_GGET: 166 case BC_GGET:
167 *name = strdata(gco2str(gcref(pt->k.gc[~(ptrdiff_t)bc_d(ins)]))); 167 *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_d(ins))));
168 return "global"; 168 return "global";
169 case BC_TGETS: 169 case BC_TGETS:
170 *name = strdata(gco2str(gcref(pt->k.gc[~(ptrdiff_t)bc_c(ins)]))); 170 *name = strdata(gco2str(proto_kgc(pt, ~(ptrdiff_t)bc_c(ins))));
171 if (ip > pt->bc) { 171 if (ip > proto_bc(pt)) {
172 BCIns insp = ip[-1]; 172 BCIns insp = ip[-1];
173 if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1 && 173 if (bc_op(insp) == BC_MOV && bc_a(insp) == ra+1 &&
174 bc_d(insp) == bc_b(ins)) 174 bc_d(insp) == bc_b(ins))
@@ -201,7 +201,7 @@ static const char *getfuncname(lua_State *L, TValue *frame, const char **name)
201 if (pc == ~(BCPos)0) 201 if (pc == ~(BCPos)0)
202 return NULL; 202 return NULL;
203 lua_assert(pc < funcproto(fn)->sizebc); 203 lua_assert(pc < funcproto(fn)->sizebc);
204 ip = &funcproto(fn)->bc[pc]; 204 ip = &proto_bc(funcproto(fn))[pc];
205 mm = bcmode_mm(bc_op(*ip)); 205 mm = bcmode_mm(bc_op(*ip));
206 if (mm == MM_call) { 206 if (mm == MM_call) {
207 BCReg slot = bc_a(*ip); 207 BCReg slot = bc_a(*ip);
diff --git a/src/lj_func.c b/src/lj_func.c
index 078ced92..27a8322f 100644
--- a/src/lj_func.c
+++ b/src/lj_func.c
@@ -26,8 +26,8 @@ GCproto *lj_func_newproto(lua_State *L)
26 pt->sizeuv = 0; 26 pt->sizeuv = 0;
27 pt->flags = 0; 27 pt->flags = 0;
28 pt->trace = 0; 28 pt->trace = 0;
29 pt->k.n = NULL; 29 setmref(pt->k, NULL);
30 pt->bc = NULL; 30 setmref(pt->bc, NULL);
31 pt->uv = NULL; 31 pt->uv = NULL;
32 pt->sizebc = 0; 32 pt->sizebc = 0;
33 pt->sizekgc = 0; 33 pt->sizekgc = 0;
@@ -49,8 +49,8 @@ void LJ_FASTCALL lj_func_freeproto(global_State *g, GCproto *pt)
49 MSize nkgc = round_nkgc(pt->sizekgc); 49 MSize nkgc = round_nkgc(pt->sizekgc);
50 MSize sizek = nkgc*(MSize)sizeof(GCRef) + 50 MSize sizek = nkgc*(MSize)sizeof(GCRef) +
51 pt->sizekn*(MSize)sizeof(lua_Number); 51 pt->sizekn*(MSize)sizeof(lua_Number);
52 lj_mem_free(g, pt->k.gc - nkgc, sizek); 52 lj_mem_free(g, mref(pt->k, GCRef) - nkgc, sizek);
53 lj_mem_freevec(g, pt->bc, pt->sizebc, BCIns); 53 lj_mem_freevec(g, proto_bc(pt), pt->sizebc, BCIns);
54 lj_mem_freevec(g, pt->uv, pt->sizeuv, uint16_t); 54 lj_mem_freevec(g, pt->uv, pt->sizeuv, uint16_t);
55 lj_mem_freevec(g, pt->lineinfo, pt->sizelineinfo, int32_t); 55 lj_mem_freevec(g, pt->lineinfo, pt->sizelineinfo, int32_t);
56 lj_mem_freevec(g, pt->varinfo, pt->sizevarinfo, struct VarInfo); 56 lj_mem_freevec(g, pt->varinfo, pt->sizevarinfo, struct VarInfo);
diff --git a/src/lj_gc.c b/src/lj_gc.c
index 764d74a8..9f0ff2b6 100644
--- a/src/lj_gc.c
+++ b/src/lj_gc.c
@@ -254,7 +254,7 @@ static void gc_traverse_proto(global_State *g, GCproto *pt)
254 if (pt->chunkname) 254 if (pt->chunkname)
255 gc_mark_str(pt->chunkname); 255 gc_mark_str(pt->chunkname);
256 for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */ 256 for (i = -(ptrdiff_t)pt->sizekgc; i < 0; i++) /* Mark collectable consts. */
257 gc_markobj(g, gcref(pt->k.gc[i])); 257 gc_markobj(g, proto_kgc(pt, i));
258 for (i = 0; i < (ptrdiff_t)pt->sizeuvname; i++) /* Mark upvalue names. */ 258 for (i = 0; i < (ptrdiff_t)pt->sizeuvname; i++) /* Mark upvalue names. */
259 if (pt->uvname[i]) 259 if (pt->uvname[i])
260 gc_mark_str(pt->uvname[i]); 260 gc_mark_str(pt->uvname[i]);
diff --git a/src/lj_gdbjit.c b/src/lj_gdbjit.c
index 345afb51..6fb8a1d3 100644
--- a/src/lj_gdbjit.c
+++ b/src/lj_gdbjit.c
@@ -705,7 +705,10 @@ void lj_gdbjit_addtrace(jit_State *J, Trace *T, TraceNo traceno)
705 ctx.szmcode = T->szmcode; 705 ctx.szmcode = T->szmcode;
706 ctx.spadjp = CFRAME_SIZE + (MSize)(parent ? J->trace[parent]->spadjust : 0); 706 ctx.spadjp = CFRAME_SIZE + (MSize)(parent ? J->trace[parent]->spadjust : 0);
707 ctx.spadj = CFRAME_SIZE + T->spadjust; 707 ctx.spadj = CFRAME_SIZE + T->spadjust;
708 ctx.lineno = pt->lineinfo ? pt->lineinfo[startpc - pt->bc] : 0; 708 if (startpc >= proto_bc(pt))
709 ctx.lineno = pt->lineinfo ? pt->lineinfo[proto_bcpos(pt, startpc)] : 0;
710 else
711 ctx.lineno = pt->linedefined;
709 ctx.filename = strdata(pt->chunkname); 712 ctx.filename = strdata(pt->chunkname);
710 if (*ctx.filename == '@' || *ctx.filename == '=') 713 if (*ctx.filename == '@' || *ctx.filename == '=')
711 ctx.filename++; 714 ctx.filename++;
diff --git a/src/lj_obj.h b/src/lj_obj.h
index fbc1eff9..22b69c60 100644
--- a/src/lj_obj.h
+++ b/src/lj_obj.h
@@ -335,12 +335,6 @@ enum {
335 335
336/* -- Prototype object ---------------------------------------------------- */ 336/* -- Prototype object ---------------------------------------------------- */
337 337
338/* Split constant array. Collectables are below, numbers above pointer. */
339typedef union ProtoK {
340 lua_Number *n; /* Numbers. */
341 GCRef *gc; /* Collectable objects (strings/table/proto). */
342} ProtoK;
343
344#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef)) 338#define SCALE_NUM_GCO ((int32_t)sizeof(lua_Number)/sizeof(GCRef))
345#define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1)) 339#define round_nkgc(n) (((n) + SCALE_NUM_GCO-1) & ~(SCALE_NUM_GCO-1))
346 340
@@ -356,8 +350,8 @@ typedef struct GCproto {
356 uint8_t framesize; /* Fixed frame size. */ 350 uint8_t framesize; /* Fixed frame size. */
357 MSize sizebc; /* Number of bytecode instructions. */ 351 MSize sizebc; /* Number of bytecode instructions. */
358 GCRef gclist; 352 GCRef gclist;
359 ProtoK k; /* Split constant array (points to the middle). */ 353 MRef k; /* Split constant array (points to the middle). */
360 BCIns *bc; /* Array of bytecode instructions. */ 354 MRef bc; /* Array of bytecode instructions. */
361 uint16_t *uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */ 355 uint16_t *uv; /* Upvalue list. local slot|0x8000 or parent uv idx. */
362 MSize sizekgc; /* Number of collectable constants. */ 356 MSize sizekgc; /* Number of collectable constants. */
363 MSize sizekn; /* Number of lua_Number constants. */ 357 MSize sizekn; /* Number of lua_Number constants. */
@@ -383,6 +377,18 @@ typedef struct GCproto {
383#define PROTO_NO_JIT 0x10 377#define PROTO_NO_JIT 0x10
384#define PROTO_HAS_ILOOP 0x20 378#define PROTO_HAS_ILOOP 0x20
385 379
380#define proto_kgc(pt, idx) \
381 check_exp((uintptr_t)(intptr_t)(idx) >= (uintptr_t)-(intptr_t)(pt)->sizekgc, \
382 gcref(mref((pt)->k, GCRef)[(idx)]))
383#define proto_knum(pt, idx) \
384 check_exp((uintptr_t)(idx) < (pt)->sizekn, mref((pt)->k, lua_Number)[(idx)])
385#define proto_bc(pt) (mref((pt)->bc, BCIns))
386#define proto_ins(pt, pos) \
387 check_exp((uintptr_t)(pos) < (pt)->sizebc, proto_bc(pt)[(pos)])
388#define proto_insptr(pt, pos) \
389 check_exp((uintptr_t)(pos) < (pt)->sizebc, &proto_bc(pt)[(pos)])
390#define proto_bcpos(pt, pc) ((BCPos)((pc) - proto_bc(pt)))
391
386/* -- Upvalue object ------------------------------------------------------ */ 392/* -- Upvalue object ------------------------------------------------------ */
387 393
388typedef struct GCupval { 394typedef struct GCupval {
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
diff --git a/src/lj_record.c b/src/lj_record.c
index f6d13264..49e3d3b5 100644
--- a/src/lj_record.c
+++ b/src/lj_record.c
@@ -263,7 +263,7 @@ static TRef fori_arg(jit_State *J, const BCIns *pc, BCReg slot, IRType t)
263 else 263 else
264 return lj_ir_knum(J, cast_num(k)); 264 return lj_ir_knum(J, cast_num(k));
265 } else if (bc_op(ins) == BC_KNUM) { 265 } else if (bc_op(ins) == BC_KNUM) {
266 lua_Number n = J->pt->k.n[bc_d(ins)]; 266 lua_Number n = proto_knum(J->pt, bc_d(ins));
267 if (t == IRT_INT) 267 if (t == IRT_INT)
268 return lj_ir_kint(J, lj_num2int(n)); 268 return lj_ir_kint(J, lj_num2int(n));
269 else 269 else
@@ -1816,7 +1816,7 @@ void lj_record_ins(jit_State *J)
1816 case BCMnone: rb = 0; rc = bc_d(ins); break; /* Upgrade rc to 'rd'. */ 1816 case BCMnone: rb = 0; rc = bc_d(ins); break; /* Upgrade rc to 'rd'. */
1817 case BCMvar: 1817 case BCMvar:
1818 copyTV(J->L, rbv, &lbase[rb]); ix.tab = rb = getslot(J, rb); break; 1818 copyTV(J->L, rbv, &lbase[rb]); ix.tab = rb = getslot(J, rb); break;
1819 case BCMnum: { lua_Number n = J->pt->k.n[rb]; 1819 case BCMnum: { lua_Number n = proto_knum(J->pt, rb);
1820 setnumV(rbv, n); ix.tab = rb = lj_ir_knumint(J, n); } break; 1820 setnumV(rbv, n); ix.tab = rb = lj_ir_knumint(J, n); } break;
1821 default: break; /* Handled later. */ 1821 default: break; /* Handled later. */
1822 } 1822 }
@@ -1824,9 +1824,9 @@ void lj_record_ins(jit_State *J)
1824 case BCMvar: 1824 case BCMvar:
1825 copyTV(J->L, rcv, &lbase[rc]); ix.key = rc = getslot(J, rc); break; 1825 copyTV(J->L, rcv, &lbase[rc]); ix.key = rc = getslot(J, rc); break;
1826 case BCMpri: setitype(rcv, (int32_t)~rc); rc = TREF_PRI(IRT_NIL+rc); break; 1826 case BCMpri: setitype(rcv, (int32_t)~rc); rc = TREF_PRI(IRT_NIL+rc); break;
1827 case BCMnum: { lua_Number n = J->pt->k.n[rc]; 1827 case BCMnum: { lua_Number n = proto_knum(J->pt, rc);
1828 setnumV(rcv, n); ix.key = rc = lj_ir_knumint(J, n); } break; 1828 setnumV(rcv, n); ix.key = rc = lj_ir_knumint(J, n); } break;
1829 case BCMstr: { GCstr *s = strref(J->pt->k.gc[~(ptrdiff_t)rc]); 1829 case BCMstr: { GCstr *s = gco2str(proto_kgc(J->pt, ~(ptrdiff_t)rc));
1830 setstrV(J->L, rcv, s); ix.key = rc = lj_ir_kstr(J, s); } break; 1830 setstrV(J->L, rcv, s); ix.key = rc = lj_ir_kstr(J, s); } break;
1831 default: break; /* Handled later. */ 1831 default: break; /* Handled later. */
1832 } 1832 }
@@ -2018,7 +2018,7 @@ void lj_record_ins(jit_State *J)
2018 break; 2018 break;
2019 case BC_TDUP: 2019 case BC_TDUP:
2020 rc = emitir(IRT(IR_TDUP, IRT_TAB), 2020 rc = emitir(IRT(IR_TDUP, IRT_TAB),
2021 lj_ir_ktab(J, tabref(J->pt->k.gc[~(ptrdiff_t)rc])), 0); 2021 lj_ir_ktab(J, gco2tab(proto_kgc(J->pt, ~(ptrdiff_t)rc))), 0);
2022 break; 2022 break;
2023 2023
2024 /* -- Calls and vararg handling ----------------------------------------- */ 2024 /* -- Calls and vararg handling ----------------------------------------- */
@@ -2315,7 +2315,7 @@ void lj_record_setup(jit_State *J)
2315 /* Check whether we could at least potentially form an extra loop. */ 2315 /* Check whether we could at least potentially form an extra loop. */
2316 if (J->exitno == 0 && T->snap[0].nent == 0) { 2316 if (J->exitno == 0 && T->snap[0].nent == 0) {
2317 /* We can narrow a FORL for some side traces, too. */ 2317 /* We can narrow a FORL for some side traces, too. */
2318 if (J->pc > J->pt->bc && bc_op(J->pc[-1]) == BC_JFORI && 2318 if (J->pc > proto_bc(J->pt) && bc_op(J->pc[-1]) == BC_JFORI &&
2319 bc_d(J->pc[bc_j(J->pc[-1])-1]) == root) { 2319 bc_d(J->pc[bc_j(J->pc[-1])-1]) == root) {
2320 lj_snap_add(J); 2320 lj_snap_add(J);
2321 rec_setup_forl(J, J->pc-1); 2321 rec_setup_forl(J, J->pc-1);
@@ -2332,7 +2332,7 @@ void lj_record_setup(jit_State *J)
2332 rec_stop(J, TRACE_INTERP); 2332 rec_stop(J, TRACE_INTERP);
2333 } else { /* Root trace. */ 2333 } else { /* Root trace. */
2334 J->cur.root = 0; 2334 J->cur.root = 0;
2335 if (J->pc >= J->pt->bc) { /* Not a hot CALL? */ 2335 if (J->pc >= proto_bc(J->pt)) { /* Not a hot CALL? */
2336 J->cur.startins = *J->pc; 2336 J->cur.startins = *J->pc;
2337 J->pc = rec_setup_root(J); 2337 J->pc = rec_setup_root(J);
2338 /* Note: the loop instruction itself is recorded at the end and not 2338 /* Note: the loop instruction itself is recorded at the end and not
diff --git a/src/lj_trace.c b/src/lj_trace.c
index ce313df3..0bd32b1e 100644
--- a/src/lj_trace.c
+++ b/src/lj_trace.c
@@ -146,7 +146,7 @@ void lj_trace_freeproto(global_State *g, GCproto *pt)
146void lj_trace_reenableproto(GCproto *pt) 146void lj_trace_reenableproto(GCproto *pt)
147{ 147{
148 if ((pt->flags & PROTO_HAS_ILOOP)) { 148 if ((pt->flags & PROTO_HAS_ILOOP)) {
149 BCIns *bc = pt->bc; 149 BCIns *bc = proto_bc(pt);
150 BCPos i, sizebc = pt->sizebc;; 150 BCPos i, sizebc = pt->sizebc;;
151 pt->flags &= ~PROTO_HAS_ILOOP; 151 pt->flags &= ~PROTO_HAS_ILOOP;
152 for (i = 0; i < sizebc; i++) { 152 for (i = 0; i < sizebc; i++) {
@@ -323,7 +323,7 @@ static void trace_start(jit_State *J)
323 323
324 if ((J->pt->flags & PROTO_NO_JIT)) { /* JIT disabled for this proto? */ 324 if ((J->pt->flags & PROTO_NO_JIT)) { /* JIT disabled for this proto? */
325 if (J->parent == 0) { 325 if (J->parent == 0) {
326 if (J->pc >= J->pt->bc) { 326 if (J->pc >= proto_bc(J->pt)) {
327 /* Lazy bytecode patching to disable hotcount events. */ 327 /* Lazy bytecode patching to disable hotcount events. */
328 setbc_op(J->pc, (int)bc_op(*J->pc)+(int)BC_ILOOP-(int)BC_LOOP); 328 setbc_op(J->pc, (int)bc_op(*J->pc)+(int)BC_ILOOP-(int)BC_LOOP);
329 J->pt->flags |= PROTO_HAS_ILOOP; 329 J->pt->flags |= PROTO_HAS_ILOOP;
@@ -361,7 +361,7 @@ static void trace_start(jit_State *J)
361 setstrV(L, L->top++, lj_str_newlit(L, "start")); 361 setstrV(L, L->top++, lj_str_newlit(L, "start"));
362 setintV(L->top++, J->curtrace); 362 setintV(L->top++, J->curtrace);
363 setfuncV(L, L->top++, J->fn); 363 setfuncV(L, L->top++, J->fn);
364 setintV(L->top++, J->pc - J->pt->bc + 1); 364 setintV(L->top++, proto_bcpos(J->pt, J->pc) + 1);
365 if (J->parent) { 365 if (J->parent) {
366 setintV(L->top++, J->parent); 366 setintV(L->top++, J->parent);
367 setintV(L->top++, J->exitno); 367 setintV(L->top++, J->exitno);
@@ -444,7 +444,7 @@ static int trace_abort(jit_State *J)
444 setstrV(L, L->top++, lj_str_newlit(L, "abort")); 444 setstrV(L, L->top++, lj_str_newlit(L, "abort"));
445 setintV(L->top++, J->curtrace); 445 setintV(L->top++, J->curtrace);
446 setfuncV(L, L->top++, J->fn); 446 setfuncV(L, L->top++, J->fn);
447 setintV(L->top++, J->pc - J->pt->bc + 1); 447 setintV(L->top++, proto_bcpos(J->pt, J->pc) + 1);
448 copyTV(L, L->top++, restorestack(L, errobj)); 448 copyTV(L, L->top++, restorestack(L, errobj));
449 copyTV(L, L->top++, &J->errinfo); 449 copyTV(L, L->top++, &J->errinfo);
450 ); 450 );
@@ -478,7 +478,7 @@ static TValue *trace_state(lua_State *L, lua_CFunction dummy, void *ud)
478 lj_vmevent_send(L, RECORD, 478 lj_vmevent_send(L, RECORD,
479 setintV(L->top++, J->curtrace); 479 setintV(L->top++, J->curtrace);
480 setfuncV(L, L->top++, J->fn); 480 setfuncV(L, L->top++, J->fn);
481 setintV(L->top++, J->pc - J->pt->bc + 1); 481 setintV(L->top++, proto_bcpos(J->pt, J->pc) + 1);
482 setintV(L->top++, J->framedepth); 482 setintV(L->top++, J->framedepth);
483 if (bcmode_mm(bc_op(*J->pc)) == MM_call) { 483 if (bcmode_mm(bc_op(*J->pc)) == MM_call) {
484 cTValue *o = &L->base[bc_a(*J->pc)]; 484 cTValue *o = &L->base[bc_a(*J->pc)];