lua 1.0 源码分析 -- 1 lua 的虚拟指令

  lua的解释器拿到 lua 编写的源码,首先进行解析,就是进行词法分析和语法分析,将源码转换成 lua 的指令集,然后执行这个指令集。

  

  lua 源码:

  

function f(val)
    return val;
end

function main()
    local i = 1;
    local j = 2;
    local b = i + f(2);
    --local b = i + j;

    print("retval = "..b);

    return b;
end

   调用 main 的指令集分析:

  

 in while  opcode = [CALLFUNC]. -- main
 in while  opcode = [NOP].
 in while  opcode = [SETFUNCTION].
 in while  opcode = [ADJUST].
 in while  opcode = [NOP].
 in while  opcode = [SETLINE].
 in while  opcode = [PUSH1].  -- i 
 in while  opcode = [SETLINE].
 in while  opcode = [PUSH2].  -- j
 in while  opcode = [SETLINE].  -- local b = i + f(2)
 in while  opcode = [PUSHLOCAL0]. -- i
 in while  opcode = [PUSHGLOBAL]. -- f
 in while  opcode = [PUSHMARK].
 in while  opcode = [PUSH2].    -- val
 in while  opcode = [CALLFUNC]. -- f()
 in while  opcode = [NOP].
 in while  opcode = [SETFUNCTION].
 in while  opcode = [ADJUST].
 in while  opcode = [NOP].
 in while  opcode = [SETLINE].
 in while  opcode = [PUSHLOCAL0]. -- val 
 in while  opcode = [RESET].
 in while  opcode = [RETCODE].
 in while  opcode = [SETLINE].
 in while  opcode = [ADJUST].
 in while  opcode = [ADDOP].  -- local b = i + f(2)
 in while  opcode = [SETLINE].
 in while  opcode = [NOP].
 in while  opcode = [PUSHGLOBAL]. --  print
 in while  opcode = [PUSHMARK].
 in while  opcode = [PUSHSTRING]. -- retval =
 in while  opcode = [PUSHLOCAL2]. -- b
 in while  opcode = [CONCOP].
 in while  opcode = [CALLFUNC]. -- print()
retval = 3
 in while  opcode = [ADJUST].
 in while  opcode = [SETLINE].
 in while  opcode = [PUSHLOCAL2].
 in while  opcode = [RESET].
 in while  opcode = [RETCODE].
 in while  opcode = [HALT].

 PS:

  函数就是一个入口地址,入口是通向将要执行的指令集。想下 汇编 中的 call,ret。lua 中的虚拟指令类似,只不过入口中的指令集是 lua 自己的虚拟指令,不是像 c 那样的机器指令。

转载于:https://www.cnblogs.com/ashen/p/11547044.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值