Lua5.1代码阅读(三):lcode.h/lcode.c

本文详细介绍了Lua编译器中的代码生成器lcode.c,它负责优化并生成目标二进制代码。文章深入探讨了其内部函数、宏定义、枚举类型及公共全局函数等内容,适合对Lua编译原理感兴趣的开发者阅读。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、概述

lcode.h/lcode.c是Lua的代码生成器,

用于优化和生成目标二进制代码。

lcode.c的所有导出函数只被lparser.c引用。

lcode内部的函数引用图如下:


 

二、宏

1. #define NO_JUMP (-1)

2. #define getcode(fs,e)((fs)->f->code[(e)->u.s.info])

3. #define luaK_codeAsBx(fs,o,A,sBx)luaK_codeABx(fs,o,A,(sBx)+MAXARG_sBx)

4. #define luaK_setmultret(fs,e)luaK_setreturns(fs, e, LUA_MULTRET)

5. #define lcode_c

6. #define LUA_CORE

7. #define hasjumps(e)((e)->t != (e)->f)

 

三、枚举值

1. typedef enum BinOpr {

 OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW,

 OPR_CONCAT,

 OPR_NE, OPR_EQ,

 OPR_LT, OPR_LE, OPR_GT, OPR_GE,

 OPR_AND, OPR_OR,

 OPR_NOBINOPR

} BinOpr;

2. typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr;

 

四、私有静态函数

1. static int isnumeral(expdesc *e) {

2. static int condjump (FuncState *fs, OpCode op, int A, int B, int C) {

3. static void fixjump (FuncState *fs, int pc, int dest) {

4. static int getjump (FuncState *fs, int pc) {

5. static Instruction *getjumpcontrol (FuncState *fs, int pc) {

6. static int need_value (FuncState *fs, int list) {

7. static int patchtestreg (FuncState *fs, int node, int reg) {

8. static void removevalues (FuncState *fs, int list) {

9. static void patchlistaux (FuncState *fs, int list, int vtarget, int reg, int dtarget) {

10. static void dischargejpc (FuncState *fs) {

11. static void freereg (FuncState *fs, int reg) {

12. static void freeexp (FuncState *fs, expdesc *e) {

13. static int addk (FuncState *fs, TValue *k, TValue *v) {

14. static int boolK (FuncState *fs, int b) {

15. static int nilK (FuncState *fs) {

16. static int code_label (FuncState *fs, int A, int b, int jump) {

17. static void discharge2reg (FuncState *fs, expdesc *e, int reg) {

18. static void discharge2anyreg (FuncState *fs, expdesc *e) {

19. static void exp2reg (FuncState *fs, expdesc *e, int reg) {

20. static void invertjump (FuncState *fs, expdesc *e) {

21. static int jumponcond (FuncState *fs, expdesc *e, int cond) {

22. static void luaK_goiffalse (FuncState *fs, expdesc *e) {

23. static void codenot (FuncState *fs, expdesc *e) {

24. static int constfolding (OpCode op, expdesc *e1, expdesc *e2) {

25. static void codearith (FuncState *fs, OpCode op, expdesc *e1, expdesc *e2) {

26. static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, expdesc *e2) {

27. static int luaK_code (FuncState *fs, Instruction i, int line) {

 

五、公共全局函数

1. void luaK_nil (FuncState *fs, int from, int n) {

2. int luaK_jump (FuncState *fs) {

3. void luaK_ret (FuncState *fs, int first, int nret) {

4. int luaK_getlabel (FuncState *fs) {

5. void luaK_patchlist (FuncState *fs, int list, int target) {

6. void luaK_patchtohere (FuncState *fs, int list) {

7. void luaK_concat (FuncState *fs, int *l1, int l2) {

8. void luaK_checkstack (FuncState *fs, int n) {

9. void luaK_reserveregs (FuncState *fs, int n) {

10. int luaK_stringK (FuncState *fs, TString *s) {

11. int luaK_numberK (FuncState *fs, lua_Number r) {

12. void luaK_setreturns (FuncState *fs, expdesc *e, int nresults) {

13. void luaK_setoneret (FuncState *fs, expdesc *e) {

14. void luaK_dischargevars (FuncState *fs, expdesc *e) {

15. void luaK_exp2nextreg (FuncState *fs, expdesc *e) {

16. int luaK_exp2anyreg (FuncState *fs, expdesc *e) {

17. void luaK_exp2val (FuncState *fs, expdesc *e) {

18. int luaK_exp2RK (FuncState *fs, expdesc *e) {

19. void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) {

20. void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {

21. void luaK_goiftrue (FuncState *fs, expdesc *e) {

22. void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) {

23. void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) {

24. void luaK_infix (FuncState *fs, BinOpr op, expdesc *v) {

25. void luaK_posfix (FuncState *fs, BinOpr op, expdesc *e1, expdesc *e2) {

26. void luaK_fixline (FuncState *fs, int line) {

27. int luaK_codeABC (FuncState *fs, OpCode o, int a, int b, int c) {

28. int luaK_codeABx (FuncState *fs, OpCode o, int a, unsigned int bc) {

29. void luaK_setlist (FuncState *fs, int base, int nelems, int tostore) {

 

六、值得观察的代码

1. luaK_code

可以在此处设置断点,

观察Lua编译器是何时生成Instruction代码,

以及如何将其保存到Proto(函数原型)结构体的code数组中。

2. TODO:

大多数公共全局函数或全局宏(luaK_*)都有生成OP_*指令的能力。

<think>好的,用户遇到了Redis编译时缺少libhiredis.a、liblua.a和libjemalloc.a文件的问题。我需要先分析这些依赖库的作用以及它们缺失的可能原因。 首先,libhiredis是Redis的C客户端库,liblua与Redis的Lua脚本支持有关,而libjemalloc是内存分配器,通常Redis推荐使用它来提升性能。这些库在编译Redis时可能需要先被正确安装或编译。 接下来,用户可能在编译Redis时没有正确安装这些依赖,或者编译选项配置有问题。例如,某些系统可能需要手动安装这些库,或者使用特定的make参数来启用它们。例如,使用make MALLOC=libc可能会绕过jemalloc的问题,但可能影响性能。 然后,我需要回忆常见的解决方案步骤。首先,检查系统是否安装了这些库的开发包,比如在Ubuntu下可能是libhiredis-dev、liblua5.1-dev和libjemalloc-dev。如果缺少,安装它们。其次,确认Redis的编译配置是否正确,比如是否有指定正确的路径。另外,有时候可能需要从源代码编译这些依赖库,并确保它们的位置在编译器能找到的地方。 还要考虑用户是否在正确的目录下执行编译,或者是否清理了之前的编译文件。比如,执行make distclean后再重新配置和编译。此外,查看Redis的README或INSTALL文档是否有特定说明。 用户可能已经尝试过基本的make命令,但遇到了错误。需要指导他们逐步检查依赖安装,调整make参数,并处理可能的环境问题。同时,需要提醒用户注意使用非jemalloc内存分配器可能带来的影响,以及如何验证解决方案是否有效。 最后,确保回答结构清晰,分步骤说明,并引用相关的参考资料,特别是用户提供的引用中提到的Jedis连接问题,虽然和编译错误不同,但可能涉及环境配置的共通点,比如正确安装Redis服务端组件。</think>关于Redis编译时缺少依赖库的解决方案分析如下: ### 一、问题原因分析 这个依赖库在Redis编译过程中承担关键作用: - $libhiredis.a$:Redis官方C语言客户端核心库 - $liblua.a$:Lua脚本引擎支持库 - $libjemalloc.a$:高性能内存分配器(Redis推荐使用) 常见报错场景: ```bash error: missing libhiredis.a make[1]: *** [server] Error 1 ``` ### 二、分步解决方案 #### 第一步:安装基础依赖 ```bash # Ubuntu/Debian sudo apt-get install build-essential libhiredis-dev liblua5.1-dev libjemalloc-dev # CentOS/RHEL sudo yum install gcc make hiredis-devel lua-devel jemalloc-devel ``` #### 第二步:配置编译参数 ```bash # 进入Redis源码目录 cd redis-6.2.6 # 清理旧编译文件 make distclean # 指定内存分配器 make MALLOC=jemalloc # 或显式指定所有依赖路径 make \ USE_JEMALLOC=yes \ JEMALLOC_LIB=/usr/lib/x86_64-linux-gnu/ \ HIREDIS_LIB=/usr/lib/x86_64-linux-gnu/ \ LUA_LIB=/usr/lib/x86_64-linux-gnu/ ``` #### 第步:验证依赖路径 通过`ldconfig -p`命令检查库文件是否存在: ```bash ldconfig -p | grep libhiredis ldconfig -p | grep liblua ldconfig -p | grep libjemalloc ``` ### 、特殊情况处理 如果系统仓库没有提供预编译包,需要从源码编译: $$ \begin{aligned} &\text{编译libhiredis:} \\ &git clone https://github.com/redis/hiredis.git \\ &cd hiredis \&\& make \&\& sudo make install \\[2ex] &\text{编译Lua 5.1:} \\ &wget http://www.lua.org/ftp/lua-5.1.5.tar.gz \\ &tar zxf lua-5.1.5.tar.gz \&\& cd lua-5.1.5 \\ &make linux \&\& sudo make install \\[2ex] &\text{编译jemalloc:} \\ &git clone https://github.com/jemalloc/jemalloc.git \\ &cd jemalloc \&\& ./autogen.sh \&\& make \&\& sudo make install \end{aligned} $$ ### 四、环境配置建议 1. 设置动态库路径: ```bash export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH ``` 2. 更新链接库缓存: ```bash sudo ldconfig ``` ### 五、验证编译结果 成功编译后应生成关键文件: ```bash $ find . -name "redis-server" ./src/redis-server $ ldd src/redis-server | grep jemalloc libjemalloc.so.2 => /usr/lib/x86_64-linux-gnu/libjemalloc.so.2 ``` ### 注意事项 1. 使用`make MALLOC=libc`可跳过jemalloc,但会降低性能[^3] 2. 确保gcc版本>=4.9(通过`gcc -v`验证) 3. 32位系统需要添加`make 32bit`参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值