luaJIT字节码写入介绍(一)

来源:互联网 发布:ubuntu 15.10 163源 编辑:程序博客网 时间:2024/06/06 07:22

luaJIT字节码写入介绍

这里我将对于luaJIT的字节码写入文件进行剖析介绍,来保证对于luaJIT指令集的深入了解

我们针对luaJIT中的,lj_bcwrite.c文件来进行解析

从第一步开始,根据观察我们可以发现,写入的入口方法是:

int lj_bcwrite(lua_State *L, GCproto *pt, lua_Writer writer, void *data,      int strip){  BCWriteCtx ctx;/*写入模块的上下文环境*/  int status;  ctx.L = L;  ctx.pt = pt;  ctx.wfunc = writer;/*funcproto(fn)*/  ctx.wdata = data;/*luaL_Buffer*/  ctx.strip = strip;  ctx.status = 0;  lj_str_initbuf(&ctx.sb);  status = lj_vm_cpcall(L, NULL, &ctx, cpwriter);  if (status == 0) status = ctx.status;  lj_str_freebuf(G(ctx.L), &ctx.sb);  return status;}
这里面有几个部分需要讲解:

(1)BCWriteCtx结构

typedef struct BCWriteCtx {  SBuf sb;/* Output buffer. 这里存着所有的输出字符 这个sb不是骂人啊,是string buffer*/  lua_State *L;/* Lua state. 这个就比较简单,是lua的状态虚拟机*/  GCproto *pt;/* Root prototype. GC管理*/  lua_Writer wfunc;/* Writer callback. 写入方法,但是这里输入的是  #define funcproto(fn) \    check_exp(isluafunc(fn), (GCproto *)(mref((fn)->l.pc, char)-sizeof(GCproto)))  */      void *wdata;/* Writer callback data. */  // typedef struct luaL_Buffer { 这个就是相关的输入流数据,是lua的源编写的代码  //   char *p;/* current position in buffer */  //   int lvl;  /* number of strings in the stack (level) */  //   lua_State *L;  //   char buffer[LUAL_BUFFERSIZE];  // } luaL_Buffer;  int strip;/* Strip debug info. */  int status;/* Status from writer callback. */} BCWriteCtx;

同样我们贴上SBuf的结构,方便后面理解

/* Resizable string buffer. Need this here, details in lj_str.h. */// typedef struct SBuf {//   char *buf;/* String buffer base. *///   MSize n;/* String buffer length. *///   MSize sz;/* String buffer size. */// } SBuf;


(2)lj_vm_cpcall(L, NULL, &ctx, cpwriter);

这里是我们的写入的真实入口,接下来对此进行分析

我们找到这一部分的代码:

LJ_ASMF int lj_vm_cpcall(lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp);

你敢信我就找到个定义!!!关于这一部分的讲解希望有大神能够帮我解释下

(3)cpwriter

/* Protected callback for bytecode writer. 这里处理写入包装*/static TValue *cpwriter(lua_State *L, lua_CFunction dummy, void *ud){  BCWriteCtx *ctx = (BCWriteCtx *)ud;  UNUSED(dummy);  lj_str_resizebuf(L, &ctx->sb, 1024);  /* Avoids resize for most prototypes. */  bcwrite_header(ctx);  bcwrite_proto(ctx, ctx->pt);  bcwrite_footer(ctx);  return NULL;}
这里是写入的原子操作,处理整个写入的包装,包括header、proto、footer,这里是按照dump的format进行处理的,

具体的dump模板定义我们会在下面进行详细讲解,今天解析出入口就到此结束啦,下节再见




原创粉丝点击