Przeglądaj źródła

Move bytecode offsets from lj_vm.* to generated header.

Mike Pall 16 lat temu
rodzic
commit
c4dadf1d67
11 zmienionych plików z 49 dodań i 62 usunięć
  1. 8 1
      src/Makefile
  2. 13 11
      src/Makefile.dep
  3. 16 0
      src/buildvm.c
  4. 1 2
      src/buildvm.h
  5. 0 20
      src/buildvm_asm.c
  6. 2 21
      src/buildvm_peobj.c
  7. 2 0
      src/lj_bc.c
  8. 1 0
      src/lj_bc.h
  9. 4 4
      src/lj_dispatch.c
  10. 1 3
      src/lj_vm.h
  11. 1 0
      src/msvcbuild.bat

+ 8 - 1
src/Makefile

@@ -279,7 +279,8 @@ LUAJIT_SO= libluajit.so
 LUAJIT_T= luajit
 
 ALL_T= $(LUAJIT_T) $(LUAJIT_A) $(LUAJIT_SO) $(BUILDVM_T)
-ALL_GEN= $(LJVM_S) lj_ffdef.h lj_libdef.h lj_recdef.h $(LIB_VMDEFP) lj_folddef.h
+ALL_GEN= $(LJVM_S) lj_bcdef.h lj_ffdef.h lj_libdef.h lj_recdef.h \
+	 $(LIB_VMDEFP) lj_folddef.h
 ALL_DYNGEN= buildvm_*.h
 WIN_RM= *.obj *.lib *.exp *.dll *.exe *.manifest *.pdb *.ilk
 ALL_RM= $(ALL_T) $(ALL_GEN) *.o $(WIN_RM)
@@ -365,6 +366,7 @@ distclean:	clean
 	$(Q)$(DASM) $(DASM_DISTFLAGS) $(DASM_FLAGS_X64WIN) -o buildvm_x64win.h buildvm_x86.dasc
 
 depend:
+	@test -f lj_bcdef.h || touch lj_bcdef.h
 	@test -f lj_ffdef.h || touch lj_ffdef.h
 	@test -f lj_libdef.h || touch lj_libdef.h
 	@test -f lj_recdef.h || touch lj_recdef.h
@@ -373,6 +375,7 @@ depend:
 	@test -f buildvm_x64.h || touch buildvm_x64.h
 	@test -f buildvm_x64win.h || touch buildvm_x64win.h
 	@$(HOST_CC) $(HOST_ACFLAGS) -MM *.c | sed "s|$(DASM_DIR)|\$$(DASM_DIR)|g" >Makefile.dep
+	@test -s lj_bcdef.h || $(HOST_RM) lj_bcdef.h
 	@test -s lj_ffdef.h || $(HOST_RM) lj_ffdef.h
 	@test -s lj_libdef.h || $(HOST_RM) lj_libdef.h
 	@test -s lj_recdef.h || $(HOST_RM) lj_recdef.h
@@ -409,6 +412,10 @@ $(LJVM_BOUT): $(BUILDVM_T)
 	$(E) "BUILDVM   $@"
 	$(Q)$(BUILDVM_X) -m $(LJVM_MODE) -o $@
 
+lj_bcdef.h: $(BUILDVM_T)
+	$(E) "BUILDVM   $@"
+	$(Q)$(BUILDVM_X) -m bcdef -o $@
+
 lj_ffdef.h: $(BUILDVM_T) $(LJLIB_C)
 	$(E) "BUILDVM   $@"
 	$(Q)$(BUILDVM_X) -m ffdef -o $@ $(LJLIB_C)

+ 13 - 11
src/Makefile.dep

@@ -49,7 +49,8 @@ lj_asm.o: lj_asm.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
   lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ir.h lj_jit.h lj_iropt.h \
   lj_mcode.h lj_trace.h lj_dispatch.h lj_traceerr.h lj_snap.h lj_asm.h \
   lj_vm.h lj_target.h lj_target_x86.h
-lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h
+lj_bc.o: lj_bc.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_bc.h \
+  lj_bcdef.h
 lj_ctype.o: lj_ctype.c lj_ctype.h lj_def.h lua.h luaconf.h
 lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
   lj_err.h lj_errmsg.h lj_state.h lj_frame.h lj_bc.h lj_jit.h lj_ir.h \
@@ -127,14 +128,15 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
   lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_func.h \
   lj_udata.h lj_meta.h lj_state.h lj_frame.h lj_bc.h lj_trace.h lj_jit.h \
   lj_ir.h lj_dispatch.h lj_traceerr.h lj_vm.h lj_err.c lj_ctype.c \
-  lj_ctype.h lj_bc.c lj_obj.c lj_str.c lj_tab.c lj_func.c lj_udata.c \
-  lj_meta.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c luajit.h \
-  lj_vmevent.c lj_vmevent.h lj_api.c lj_parse.h lj_lex.c lj_parse.c \
-  lj_lib.c lj_lib.h lj_ir.c lj_iropt.h lj_opt_mem.c lj_opt_fold.c \
-  lj_folddef.h lj_opt_narrow.c lj_opt_dce.c lj_opt_loop.c lj_snap.h \
-  lj_mcode.c lj_mcode.h lj_snap.c lj_target.h lj_target_x86.h lj_record.c \
-  lj_ff.h lj_ffdef.h lj_record.h lj_asm.h lj_recdef.h lj_asm.c lj_trace.c \
-  lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c lualib.h \
-  lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c lib_os.c \
-  lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_init.c
+  lj_ctype.h lj_bc.c lj_bcdef.h lj_obj.c lj_str.c lj_tab.c lj_func.c \
+  lj_udata.c lj_meta.c lj_state.c lj_lex.h lj_alloc.h lj_dispatch.c \
+  luajit.h lj_vmevent.c lj_vmevent.h lj_api.c lj_parse.h lj_lex.c \
+  lj_parse.c lj_lib.c lj_lib.h lj_ir.c lj_iropt.h lj_opt_mem.c \
+  lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c lj_opt_loop.c \
+  lj_snap.h lj_mcode.c lj_mcode.h lj_snap.c lj_target.h lj_target_x86.h \
+  lj_record.c lj_ff.h lj_ffdef.h lj_record.h lj_asm.h lj_recdef.h \
+  lj_asm.c lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c \
+  lib_base.c lualib.h lj_libdef.h lib_math.c lib_string.c lib_table.c \
+  lib_io.c lib_os.c lib_package.c lib_debug.c lib_bit.c lib_jit.c \
+  lib_init.c
 luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h

+ 16 - 0
src/buildvm.c

@@ -251,6 +251,19 @@ static const char *lower(char *buf, const char *s)
   return buf;
 }
 
+/* Emit C source code for bytecode-related definitions. */
+static void emit_bcdef(BuildCtx *ctx)
+{
+  int i;
+  fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");
+  fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[BC__MAX+1] = {\n  ");
+  for (i = 0; i < ctx->npc; i++) {
+    fprintf(ctx->fp, "%4d, ", ctx->sym_ofs[i]);
+    if ((i & 7) == 7) fprintf(ctx->fp, "\n  ");
+  }
+  fprintf(ctx->fp, "0\n};\n\n");
+}
+
 /* Emit VM definitions as Lua code for debug modules. */
 static void emit_vmdef(BuildCtx *ctx)
 {
@@ -418,6 +431,9 @@ int main(int argc, char **argv)
   case BUILD_raw:
     emit_raw(ctx);
     break;
+  case BUILD_bcdef:
+    emit_bcdef(ctx);
+    break;
   case BUILD_vmdef:
     emit_vmdef(ctx);
     /* fallthrough */

+ 1 - 2
src/buildvm.h

@@ -36,7 +36,6 @@
 
 /* Extra labels. */
 #define LABEL_ASM_BEGIN		LABEL_PREFIX "vm_asm_begin"
-#define LABEL_OP_OFS		LABEL_PREFIX "vm_op_ofs"
 
 /* Forward declaration. */
 struct dasm_State;
@@ -50,7 +49,7 @@ struct dasm_State;
 
 #define BUILDDEF(_) \
   _(elfasm) _(coffasm) _(machasm) BUILDDEFX(_) _(raw) \
-  _(ffdef) _(libdef) _(recdef) _(vmdef) \
+  _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \
   _(folddef)
 
 typedef enum {

+ 0 - 20
src/buildvm_asm.c

@@ -193,26 +193,6 @@ void emit_asm(BuildCtx *ctx)
     pi = ni;
   }
 
-  switch (ctx->mode) {
-  case BUILD_elfasm:
-    fprintf(ctx->fp, "\n\t.section .rodata\n");
-    break;
-  case BUILD_coffasm:
-    fprintf(ctx->fp, "\n\t.section .rdata,\"dr\"\n");
-    break;
-  case BUILD_machasm:
-    fprintf(ctx->fp, "\n\t.const\n");
-    break;
-  default:
-    break;
-  }
-  emit_asm_align(ctx, 5);
-
-  sprintf(name, "%s" LABEL_OP_OFS, symprefix);
-  emit_asm_label(ctx, name, 2*ctx->npc, 0);
-  for (i = 0; i < ctx->npc; i++)
-    fprintf(ctx->fp, "\t.short %d\n", ctx->sym_ofs[i]);
-
   fprintf(ctx->fp, "\n");
   switch (ctx->mode) {
   case BUILD_elfasm:

+ 2 - 21
src/buildvm_peobj.c

@@ -103,7 +103,6 @@ enum {
   PEOBJ_SECT_PDATA,
   PEOBJ_SECT_XDATA,
 #endif
-  PEOBJ_SECT_RDATA,
   PEOBJ_SECT_RDATA_Z,
   PEOBJ_NSECTIONS
 };
@@ -168,9 +167,6 @@ static void emit_peobj_sym_sect(BuildCtx *ctx, PEsection *pesect, int sect)
 #define emit_peobj_sym_func(ctx, name, ofs) \
   emit_peobj_sym(ctx, name, (uint32_t)(ofs), \
 		 PEOBJ_SECT_TEXT, PEOBJ_TYPE_FUNC, PEOBJ_SCL_EXTERN)
-#define emit_peobj_sym_rdata(ctx, name, ofs) \
-  emit_peobj_sym(ctx, name, (uint32_t)(ofs), \
-		 PEOBJ_SECT_RDATA, PEOBJ_TYPE_NULL, PEOBJ_SCL_EXTERN)
 
 /* Emit Windows PE object file. */
 void emit_peobj(BuildCtx *ctx)
@@ -218,12 +214,6 @@ void emit_peobj(BuildCtx *ctx)
   pesect[PEOBJ_SECT_XDATA].flags = 0x40300040;
 #endif
 
-  memcpy(pesect[PEOBJ_SECT_RDATA].name, ".rdata", sizeof(".rdata")-1);
-  pesect[PEOBJ_SECT_RDATA].ofs = sofs;
-  sofs += (pesect[PEOBJ_SECT_RDATA].size = ctx->npc*sizeof(uint16_t));
-  /* Flags: 40 = read, 30 = align4, 40 = initialized data. */
-  pesect[PEOBJ_SECT_RDATA].flags = 0x40300040;
-
   memcpy(pesect[PEOBJ_SECT_RDATA_Z].name, ".rdata$Z", sizeof(".rdata$Z")-1);
   pesect[PEOBJ_SECT_RDATA_Z].ofs = sofs;
   sofs += (pesect[PEOBJ_SECT_RDATA_Z].size = (uint32_t)strlen(ctx->dasm_ident)+1);
@@ -240,13 +230,13 @@ void emit_peobj(BuildCtx *ctx)
 
   /* Compute the size of the symbol table:
   ** @feat.00 + nsections*2
-  ** + asm_start + (nsyms-nzsym) + op_ofs
+  ** + asm_start + (nsyms-nzsym)
   ** + relocsyms
   */
   /* Skip _Z syms. */
   for (nzsym = 0; ctx->sym_ofs[ctx->perm[nzsym]] < 0; nzsym++) ;
   for (relocsyms = 0; ctx->extnames[relocsyms]; relocsyms++) ;
-  pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+(ctx->nsym-nzsym)+1 + relocsyms;
+  pehdr.nsyms = 1+PEOBJ_NSECTIONS*2 + 1+(ctx->nsym-nzsym) + relocsyms;
 #if !LJ_HASJIT
   pehdr.nsyms -= 7;
 #endif
@@ -303,12 +293,6 @@ void emit_peobj(BuildCtx *ctx)
   }
 #endif
 
-  /* Write .rdata section. */
-  for (i = 0; i < ctx->npc; i++) {
-    uint16_t pcofs = (uint16_t)ctx->sym_ofs[i];
-    owrite(ctx, &pcofs, 2);
-  }
-
   /* Write .rdata$Z section. */
   owrite(ctx, ctx->dasm_ident, strlen(ctx->dasm_ident)+1);
 
@@ -378,9 +362,6 @@ void emit_peobj(BuildCtx *ctx)
       }
     }
 
-    emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA);
-    emit_peobj_sym_rdata(ctx, PEOBJ_SYM_PREFIX LABEL_OP_OFS, 0);
-
     emit_peobj_sym_sect(ctx, pesect, PEOBJ_SECT_RDATA_Z);
 
     if (strtab)

+ 2 - 0
src/lj_bc.c

@@ -15,3 +15,5 @@ BCDEF(BCMODE)
   0
 };
 
+#include "lj_bcdef.h"
+

+ 1 - 0
src/lj_bc.h

@@ -231,5 +231,6 @@ typedef enum {
   (BCM##ma|(BCM##mb<<3)|(BCM##mc<<7)|(MM_##mm<<11)),
 
 LJ_DATA const uint16_t lj_bc_mode[BC__MAX+1];
+LJ_DATA const uint16_t lj_bc_ofs[BC__MAX+1];
 
 #endif

+ 4 - 4
src/lj_dispatch.c

@@ -27,7 +27,7 @@ void lj_dispatch_init(GG_State *GG)
   uint32_t i;
   ASMFunction *disp = GG->dispatch;
   for (i = 0; i < BC__MAX; i++)
-    disp[GG_DISP_STATIC+i] = disp[i] = makeasmfunc(lj_vm_op_ofs[i]);
+    disp[GG_DISP_STATIC+i] = disp[i] = makeasmfunc(lj_bc_ofs[i]);
   /* The JIT engine is off by default. luaopen_jit() turns it on. */
   disp[BC_FORL] = disp[BC_IFORL];
   disp[BC_ITERL] = disp[BC_IITERL];
@@ -61,9 +61,9 @@ void lj_dispatch_update(global_State *g)
     ASMFunction f_forl, f_iterl, f_loop;
     g->dispatchmode = mode;
     if ((mode & 5) == 1) {  /* Hotcount if JIT is on, but not when recording. */
-      f_forl = makeasmfunc(lj_vm_op_ofs[BC_FORL]);
-      f_iterl = makeasmfunc(lj_vm_op_ofs[BC_ITERL]);
-      f_loop = makeasmfunc(lj_vm_op_ofs[BC_LOOP]);
+      f_forl = makeasmfunc(lj_bc_ofs[BC_FORL]);
+      f_iterl = makeasmfunc(lj_bc_ofs[BC_ITERL]);
+      f_loop = makeasmfunc(lj_bc_ofs[BC_LOOP]);
     } else {  /* Otherwise use the non-hotcounting instructions. */
       f_forl = disp[GG_DISP_STATIC+BC_IFORL];
       f_iterl = disp[GG_DISP_STATIC+BC_IITERL];

+ 1 - 3
src/lj_vm.h

@@ -61,9 +61,7 @@ LJ_ASMF void lj_cont_hook(void);  /* Continue from hook yield. */
 /* Start of the ASM code. */
 LJ_ASMF char lj_vm_asm_begin[];
 
-/* Opcode handler offsets, relative to lj_vm_asm_begin. */
-LJ_ASMF const uint16_t lj_vm_op_ofs[];
-
+/* Bytecode offsets are relative to lj_vm_asm_begin. */
 #define makeasmfunc(ofs)	((ASMFunction)(lj_vm_asm_begin + (ofs)))
 
 #endif

+ 1 - 0
src/msvcbuild.bat

@@ -32,6 +32,7 @@ if exist buildvm.exe.manifest^
   %LJMT% -manifest buildvm.exe.manifest -outputresource:buildvm.exe
 
 buildvm -m peobj -o lj_vm.obj
+buildvm -m bcdef -o lj_bcdef.h
 buildvm -m ffdef -o lj_ffdef.h %ALL_LIB%
 buildvm -m libdef -o lj_libdef.h %ALL_LIB%
 buildvm -m recdef -o lj_recdef.h %ALL_LIB%