当前位置:网站首页>Lua callinfo structure, stkid structure resolution
Lua callinfo structure, stkid structure resolution
2022-06-12 13:57:00 【HyperCall】
StkId structure :
typedef union StackValue {
TValue val;
} StackValue;
typedef StackValue *StkId;
StkId Only a normal LUA Variable
CallInfo structure :
typedef struct CallInfo {
StkId func;
StkId top;
struct CallInfo *previous, *next;
union {
struct {
const Instruction *savedpc;
volatile l_signalT trap;
int nextraargs;
} l;
struct {
lua_KFunction k;
ptrdiff_t old_errfunc;
lua_KContext ctx;
} c;
} u;
union {
int funcidx;
int nyield;
struct {
unsigned short ftransfer;
unsigned short ntransfer;
} transferinfo;
} u2;
short nresults;
unsigned short callstatus;
} CallInfo;
Stack :CallInfo The main function is to record the stack reference involved in a function call . The stack space of each function is not an independent array , Instead, all functions share a common stack data structure instance ,func Is the function object associated with the call , This object is in the stack of the thread , and top Is the top of the call stack ,[func, top] This is the stack range used by this call .
Previous LUA The data structure applicable to the version is shown in the following figure ( Because no 5.3 Version of the picture ):
The function object is visible on the stack , And CallInfo.func Is the serial number of the function object in the stack , The use of the stack is limited to [func, top] in , And the serial number of the first parameter is 1 Step by step up
Call chain :Lua When calling each function , Will generate a CallInfo, And link them into a two-way linked list . By traversing the linked list , We can know the whole call chain & Call relationship ,previous and next Can make CallInfo Form a two-way linked list ,lua_State->ci The current call is recorded , This linked list can be represented by the following figure :
in addition 3 individual UNION Temporary placement of structure , Let's first look at the relevant environment generated by this structure
The function responsible for generating and filling in this structure is :luaD_call(ido.c):
void luaD_call (lua_State *L, StkId func, int nresults) {
lua_CFunction f;
retry:
switch (ttypetag(s2v(func))) {
case LUA_VCCL: /* C closure */
f = clCvalue(s2v(func))->f;
goto Cfunc;
case LUA_VLCF: /* light C function */
f = fvalue(s2v(func));
Cfunc: {
...
}
case LUA_VLCL: {
/* Lua function */
...
}
default: {
/* not a function */
checkstackGCp(L, 1, func); /* space for metamethod */
luaD_tryfuncTM(L, func); /* try to get '__call' metamethod */
goto retry; /* try again with metamethod */
}
}
}
Function body passed switch case Dispatch , As the key variable to distinguish the scheduling category :ttypetag(s2v(func))
ttypetag(s2v(func))--------->withvariant(rawtt(s2v(func)))
- func Variable is StkId type , Only one of the member variables is :TValue val
- #define s2v(o) (&(o)->val), the func The real TValue Take out
- #define rawtt(o) ((o)->tt_), the func Variable TValue Type of the member
- #define withvariant(t) ((t) & 0x3F), Associate the member type with 0x3F Operation and operation
Through analysis case You can know the value of each type , among
LUA_VCCL:/* C closure */ :0x26(0x66 & 0x3F)
LUA_VLCF:/* light C function */ :0x16(0x16 & 0x3F)
LUA_VLCL:/* Lua function */ :0x6
about C closure function :
f = clCvalue(s2v(func))->f;
#define clCvalue(o) check_exp(ttisCclosure(o), gco2ccl(val_(o).gc))
And :ttisCclosure(o): ((o)->tt_) == 0x66
And :gco2ccl(val_(o).gc): Take out func Of GCObject Member to initialize GCUnion Structure , And remove them Closure Type of cl, then cl Medium CClosure c Take out
union GCUnion {
GCObject gc; /* common header */
struct TString ts;
struct Udata u;
union Closure cl;
struct Table h;
struct Proto p;
struct lua_State th; /* thread */
struct UpVal upv;
};
and check_exp(true/false, CClosure c) == CClosure c, Therefore, the overall meaning is to initialize CClosure c,
And take one of them lua_CFunction f Members are assigned to f, And then execute Cfunc label
about light C function :
f = fvalue(s2v(func)) Expand to f = ((((&(func)->val))->value_).f);
That is, take it out directly func Medium lua_CFunction f Members are assigned to f, And then execute Cfunc function
The two above case Are assigned values f Rear steering actuator Cfunc label , The source code is as follows :
int n; /* number of returns */
CallInfo *ci;
checkstackGCp(L, LUA_MINSTACK, func); /* ensure minimum stack size */
L->ci = ci = next_ci(L);
ci->nresults = nresults;
ci->callstatus = CIST_C;
ci->top = L->top + LUA_MINSTACK;
ci->func = func;
lua_assert(ci->top <= L->stack_last);
if (L->hookmask & LUA_MASKCALL) {
int narg = cast_int(L->top - func) - 1;
luaD_hook(L, LUA_HOOKCALL, -1, 1, narg);
}
lua_unlock(L);
n = (*f)(L); /* do the actual call */
lua_lock(L);
api_checknelems(L, n);
luaD_poscall(L, ci, n);
break;
establish CallInfo After the structure, check whether the stack space is greater than LUA_MINSTACK
fill ci Structure ,L->ci = ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)), If there is nextci Then directly fill , Otherwise, a new one will be generated ci node
Fill other structural bodies & Check for stack out of bounds
Reference resources :【 thorough Lua: Call information 】 thorough Lua: Call information .
边栏推荐
- 编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
- chrome://tracing Performance analysis artifact
- Void pointer (void*) usage
- Introduction to database system (Fifth Edition) notes Chapter 1 Introduction
- [wustctf2020] selfie score query -1
- 280 weeks /2171 Take out the least number of magic beans
- Tree reconstruction (pre order + middle order or post order + middle order)
- After reading the question, you will point to offer 16 Integer power of numeric value
- 十四周作业
- SystemC time
猜你喜欢

Alibaba cloud development board haas510 sends the serial port data to the Internet of things platform

Greed issues - Egypt scores

简述CGI与FASTCGI区别

Summary of virtual box usage problems

对于跨境电商,更侧重收入的出价策略 —Google SEM

Alibaba cloud development board haas510 parses serial port JSON data and sends attributes

Dismantle and modify the advertising machine - Amateur decompression

单总线温度传感器18B20数据上云(阿里云)

编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构

Is MySQL query limit 1000,10 as fast as limit 10? How to crack deep paging
随机推荐
[wustctf2020] selfie score query -1
Fourteen week assignment
[video lesson] a full set of tutorials on the design and production of Android studio Internet of things app -- all mastered during the National Day
【SemiDrive源码分析】【X9芯片启动流程】26 - R5 SafetyOS 之 LK_INIT_LEVEL_TARGET 阶段代码流程分析(TP Drvier、Audio Server初始化)
【视频课】android studio物联网APP设计制作全套教程--国庆期间全掌握
NotePad 常用设置
编译安装基于fastcgi模式的多虚拟主机的wordpress和discuz的LAMP架构
English learning plan
Xcode debugging OpenGLES
Create a small root heap and judge the node relationship (also.C\u str() substr(),atoi(),string. Use of find())
CSDN blog points rule
List of common ACM knowledge points (to be continued)
Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
拆改廣告機---業餘解壓
阿里云开发板HaaS510解析串口JSON数据并发送属性
【mysql进阶】查询优化原理与方案(六)
【SemiDrive源码分析】【X9芯片启动流程】25 - MailBox 核间通信机制介绍(代码分析篇)之 RPMSG-IPCC RTOS & QNX篇
肝了一个月的原创小袁个人博客项目开源啦(博客基本功能都有,还包含后台管理)
Implementation of Ackermann function with simulated recursion