luaJIT字节码写入介绍(一)

本文主要介绍luaJIT字节码的写入过程,通过分析lj_bcwrite.c文件,探讨BCWriteCtx结构和SBuf的作用,并揭示luaJIT中字节码写入的起点—— lj_vm_cpcall()函数调用。后续章节将进一步解析关键函数cpwriter及dump模板定义。
部署运行你感兴趣的模型镜像

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模板定义我们会在下面进行详细讲解,今天解析出入口就到此结束啦,下节再见




您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值