Skip to content

Commit f43630f

Browse files
Buristanigormunkin
authored andcommitted
vm: introduce VM states for Lua and fast functions
This patch introduces LJ_VMST_LFUNC and LJ_VMST_FFUNC VM states separated from LJ_VMST_INERP. New VM states allow to determine the context of Lua VM execution for x86 and x64 arches. Also, LJ_VMST_C is renamed to LJ_VMST_CFUNC for naming consistence with new VM states. Also, this patch adjusts stack layout for x86 and x64 arches to save VM state for its consistency while stack unwinding when error is raised. To group all traces into the one vmstate, a special macro LJ_VMST_TRACE equal to LJ_VMST__MAX is introduced. Part of tarantool/tarantool#5442 Reviewed-by: Sergey Ostanevich <sergos@tarantool.org> Reviewed-by: Igor Munkin <imun@tarantool.org> Signed-off-by: Igor Munkin <imun@tarantool.org>
1 parent b7bc3e1 commit f43630f

File tree

11 files changed

+218
-97
lines changed

11 files changed

+218
-97
lines changed

src/lj_frame.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
127127
#define CFRAME_SIZE (16*4)
128128
#define CFRAME_SHIFT_MULTRES 0
129129
#else
130-
#define CFRAME_OFS_ERRF (15*4)
131-
#define CFRAME_OFS_NRES (14*4)
132-
#define CFRAME_OFS_PREV (13*4)
133-
#define CFRAME_OFS_L (12*4)
130+
#define CFRAME_OFS_ERRF (19*4)
131+
#define CFRAME_OFS_NRES (18*4)
132+
#define CFRAME_OFS_PREV (17*4)
133+
#define CFRAME_OFS_L (16*4)
134134
#define CFRAME_OFS_PC (6*4)
135135
#define CFRAME_OFS_MULTRES (5*4)
136-
#define CFRAME_SIZE (12*4)
136+
#define CFRAME_SIZE (16*4)
137137
#define CFRAME_SHIFT_MULTRES 0
138138
#endif
139139
#elif LJ_TARGET_X64
@@ -156,7 +156,7 @@ enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
156156
#define CFRAME_SIZE_JIT (CFRAME_SIZE + 9*16 + 4*8)
157157
#define CFRAME_SHIFT_MULTRES 0
158158
#else
159-
#define CFRAME_OFS_PREV (4*8)
159+
#define CFRAME_OFS_PREV (6*8)
160160
#if LJ_GC64
161161
#define CFRAME_OFS_PC (3*8)
162162
#define CFRAME_OFS_L (2*8)
@@ -171,9 +171,9 @@ enum { LJ_CONT_TAILCALL, LJ_CONT_FFI_CALLBACK }; /* Special continuations. */
171171
#define CFRAME_OFS_MULTRES (1*4)
172172
#endif
173173
#if LJ_NO_UNWIND
174-
#define CFRAME_SIZE (12*8)
174+
#define CFRAME_SIZE (14*8)
175175
#else
176-
#define CFRAME_SIZE (10*8)
176+
#define CFRAME_SIZE (12*8)
177177
#endif
178178
#define CFRAME_SIZE_JIT (CFRAME_SIZE + 16)
179179
#define CFRAME_SHIFT_MULTRES 0

src/lj_obj.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ typedef struct GCtab {
527527
/* VM states. */
528528
enum {
529529
LJ_VMST_INTERP, /* Interpreter. */
530-
LJ_VMST_C, /* C function. */
530+
LJ_VMST_LFUNC, /* Lua function. */
531+
LJ_VMST_FFUNC, /* Fast function. */
532+
LJ_VMST_CFUNC, /* C function. */
531533
LJ_VMST_GC, /* Garbage collector. */
532534
LJ_VMST_EXIT, /* Trace exit handler. */
533535
LJ_VMST_RECORD, /* Trace recorder. */
@@ -536,6 +538,13 @@ enum {
536538
LJ_VMST__MAX
537539
};
538540

541+
/*
542+
** In fact, when VM executes a trace, vmstate is set to the trace number,
543+
** but we set the boundary to group all traces in a single pseudo-vmstate.
544+
*/
545+
546+
#define LJ_VMST_TRACE (LJ_VMST__MAX)
547+
539548
#define setvmstate(g, st) ((g)->vmstate = ~LJ_VMST_##st)
540549

541550
/* Metamethods. ORDER MM */

src/lj_profile.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,10 @@ static void profile_trigger(ProfileState *ps)
157157
int st = g->vmstate;
158158
ps->vmstate = st >= 0 ? 'N' :
159159
st == ~LJ_VMST_INTERP ? 'I' :
160-
st == ~LJ_VMST_C ? 'C' :
160+
/* Stubs for profiler hooks. */
161+
st == ~LJ_VMST_LFUNC ? 'I' :
162+
st == ~LJ_VMST_FFUNC ? 'I' :
163+
st == ~LJ_VMST_CFUNC ? 'C' :
161164
st == ~LJ_VMST_GC ? 'G' : 'J';
162165
g->hookmask = (mask | HOOK_PROFILE);
163166
lj_dispatch_update(g);

src/luajit-gdb.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,14 @@ def J(g):
206206
def vm_state(g):
207207
return {
208208
i2notu32(0): 'INTERP',
209-
i2notu32(1): 'C',
210-
i2notu32(2): 'GC',
211-
i2notu32(3): 'EXIT',
212-
i2notu32(4): 'RECORD',
213-
i2notu32(5): 'OPT',
214-
i2notu32(6): 'ASM',
209+
i2notu32(1): 'LFUNC',
210+
i2notu32(2): 'FFUNC',
211+
i2notu32(3): 'CFUNC',
212+
i2notu32(4): 'GC',
213+
i2notu32(5): 'EXIT',
214+
i2notu32(6): 'RECORD',
215+
i2notu32(7): 'OPT',
216+
i2notu32(8): 'ASM',
215217
}.get(int(tou32(g['vmstate'])), 'TRACE')
216218

217219
def gc_state(g):

src/vm_arm.dasc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ static void build_subroutines(BuildCtx *ctx)
287287
|
288288
| str RB, L->base
289289
| ldr KBASE, SAVE_NRES
290-
| mv_vmstate CARG4, C
290+
| mv_vmstate CARG4, CFUNC
291291
| sub BASE, BASE, #8
292292
| subs CARG3, RC, #8
293293
| lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8
@@ -348,7 +348,7 @@ static void build_subroutines(BuildCtx *ctx)
348348
| mov CRET1, CARG2
349349
|->vm_unwind_c_eh: // Landing pad for external unwinder.
350350
| ldr L, SAVE_L
351-
| mv_vmstate CARG4, C
351+
| mv_vmstate CARG4, CFUNC
352352
| ldr GL:CARG3, L->glref
353353
| str CARG4, GL:CARG3->vmstate
354354
| str L, GL:CARG3->cur_L
@@ -4557,7 +4557,7 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop)
45574557
if (op == BC_FUNCCW) {
45584558
| ldr CARG2, CFUNC:CARG3->f
45594559
}
4560-
| mv_vmstate CARG3, C
4560+
| mv_vmstate CARG3, CFUNC
45614561
| mov CARG1, L
45624562
| bhi ->vm_growstack_c // Need to grow stack.
45634563
| st_vmstate CARG3

src/vm_arm64.dasc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static void build_subroutines(BuildCtx *ctx)
333333
|
334334
| str RB, L->base
335335
| ldrsw CARG2, SAVE_NRES // CARG2 = nresults+1.
336-
| mv_vmstate TMP0w, C
336+
| mv_vmstate TMP0w, CFUNC
337337
| sub BASE, BASE, #16
338338
| subs TMP2, RC, #8
339339
| st_vmstate TMP0w
@@ -392,7 +392,7 @@ static void build_subroutines(BuildCtx *ctx)
392392
| mov CRET1, CARG2
393393
|->vm_unwind_c_eh: // Landing pad for external unwinder.
394394
| ldr L, SAVE_L
395-
| mv_vmstate TMP0w, C
395+
| mv_vmstate TMP0w, CFUNC
396396
| ldr GL, L->glref
397397
| st_vmstate TMP0w
398398
| b ->vm_leave_unw
@@ -3895,7 +3895,7 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop)
38953895
if (op == BC_FUNCCW) {
38963896
| ldr CARG2, CFUNC:CARG3->f
38973897
}
3898-
| mv_vmstate TMP0w, C
3898+
| mv_vmstate TMP0w, CFUNC
38993899
| mov CARG1, L
39003900
| bhi ->vm_growstack_c // Need to grow stack.
39013901
| st_vmstate TMP0w

src/vm_mips.dasc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ static void build_subroutines(BuildCtx *ctx)
403403
|
404404
| addiu TMP1, RD, -8
405405
| sw TMP2, L->base
406-
| li_vmstate C
406+
| li_vmstate CFUNC
407407
| lw TMP2, SAVE_NRES
408408
| addiu BASE, BASE, -8
409409
| st_vmstate
@@ -473,7 +473,7 @@ static void build_subroutines(BuildCtx *ctx)
473473
| move CRET1, CARG2
474474
|->vm_unwind_c_eh: // Landing pad for external unwinder.
475475
| lw L, SAVE_L
476-
| li TMP0, ~LJ_VMST_C
476+
| li TMP0, ~LJ_VMST_CFUNC
477477
| lw GL:TMP1, L->glref
478478
| b ->vm_leave_unw
479479
|. sw TMP0, GL:TMP1->vmstate
@@ -5213,7 +5213,7 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop)
52135213
| sw BASE, L->base
52145214
| sltu AT, TMP2, TMP1
52155215
| sw RC, L->top
5216-
| li_vmstate C
5216+
| li_vmstate CFUNC
52175217
if (op == BC_FUNCCW) {
52185218
| lw CARG2, CFUNC:RB->f
52195219
}

src/vm_mips64.dasc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ static void build_subroutines(BuildCtx *ctx)
453453
|
454454
| addiu TMP1, RD, -8
455455
| sd TMP2, L->base
456-
| li_vmstate C
456+
| li_vmstate CFUNC
457457
| lw TMP2, SAVE_NRES
458458
| daddiu BASE, BASE, -16
459459
| st_vmstate
@@ -528,7 +528,7 @@ static void build_subroutines(BuildCtx *ctx)
528528
| move CRET1, CARG2
529529
|->vm_unwind_c_eh: // Landing pad for external unwinder.
530530
| ld L, SAVE_L
531-
| li TMP0, ~LJ_VMST_C
531+
| li TMP0, ~LJ_VMST_CFUNC
532532
| ld GL:TMP1, L->glref
533533
| b ->vm_leave_unw
534534
|. sw TMP0, GL:TMP1->vmstate
@@ -5428,7 +5428,7 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop)
54285428
| sd BASE, L->base
54295429
| sltu AT, TMP2, TMP1
54305430
| sd RC, L->top
5431-
| li_vmstate C
5431+
| li_vmstate CFUNC
54325432
if (op == BC_FUNCCW) {
54335433
| ld CARG2, CFUNC:RB->f
54345434
}

src/vm_ppc.dasc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static void build_subroutines(BuildCtx *ctx)
553553
| // TMP0 = PC & FRAME_TYPE
554554
| cmpwi TMP0, FRAME_C
555555
| rlwinm TMP2, PC, 0, 0, 28
556-
| li_vmstate C
556+
| li_vmstate CFUNC
557557
| sub TMP2, BASE, TMP2 // TMP2 = previous base.
558558
| bney ->vm_returnp
559559
|
@@ -639,7 +639,7 @@ static void build_subroutines(BuildCtx *ctx)
639639
|->vm_unwind_c_eh: // Landing pad for external unwinder.
640640
| lwz L, SAVE_L
641641
| .toc ld TOCREG, SAVE_TOC
642-
| li TMP0, ~LJ_VMST_C
642+
| li TMP0, ~LJ_VMST_CFUNC
643643
| lwz GL:TMP1, L->glref
644644
| stw TMP0, GL:TMP1->vmstate
645645
| b ->vm_leave_unw
@@ -5853,7 +5853,7 @@ static void build_ins(BuildCtx *ctx, BCOp op, int defop)
58535853
| stp BASE, L->base
58545854
| cmplw TMP1, TMP2
58555855
| stp RC, L->top
5856-
| li_vmstate C
5856+
| li_vmstate CFUNC
58575857
|.if TOC
58585858
| mtctr TMP3
58595859
|.else

0 commit comments

Comments
 (0)