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模板定义我们会在下面进行详细讲解,今天解析出入口就到此结束啦,下节再见
阅读全文
0 0
- luaJIT字节码写入介绍(一)
- luajit笔记---编译bytecode(字节码文件)
- cocos2d-x luajit生成字节码
- 使用luajit将lua脚本编译成字节码
- 关于lua,luajit,以及lua的字节码形式
- luaJIT指令集介绍
- Peeking inside LuaJIT(窥探LuaJIT)
- luaJIT
- luajit
- Java字节码分析(一)
- 文件写入字节流
- Java字节码简单介绍
- 二进制文件读取写入(一)
- IO(一):传统IO(基于字符,字节,Socket) 与BIO,NIO,AIO 介绍
- IO(一):传统IO(基于字符,字节,Socket) 与BIO,NIO,AIO 介绍
- Java-文件读取写入整型(字节流)
- c++字节对齐(一)
- STM8 选项字节的写入
- [NOIP模拟][并查集][最短路]做运动
- SpringBoot 集成WebSocket广播模式
- Http的Session和Cookie
- 【阿里云RDS】从本地MySQL迁移到RDS for MySQL
- mysql explain-output 译文
- luaJIT字节码写入介绍(一)
- Spring cloud系列六 Ribbon的功能概述、主要组件和属性文件配置
- source insight 4乱码的问题及与keil混编的问题
- Top k Largest Numbers II
- break和continue在循环中的区别
- 1035神奇的数字9
- golang基础-互斥锁、读写锁
- Spring MVC 学习 —— <mvc:annotation-driven/>
- Program Size: Code=7044 RO-data=268 RW-data=112 ZI-data=8736解析