summaryrefslogtreecommitdiff
path: root/src/lua51/lcode.c
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2020-09-27 20:31:53 +0800
committerchai <chaifix@163.com>2020-09-27 20:31:53 +0800
commit63cb4fbbb961da133c68865845eaf22d9b876700 (patch)
tree42be163db598df2cf1c11d329c3e5843db4faa0c /src/lua51/lcode.c
parent2dfa15a926f06137f2ba6afcce2e3c1d23300100 (diff)
*misc
Diffstat (limited to 'src/lua51/lcode.c')
-rw-r--r--src/lua51/lcode.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/lua51/lcode.c b/src/lua51/lcode.c
index 8f25b06..640f107 100644
--- a/src/lua51/lcode.c
+++ b/src/lua51/lcode.c
@@ -56,11 +56,12 @@ void luaK_nil (FuncState *fs, int from, int n) {
}
+//c 生成jump指令
int luaK_jump (FuncState *fs) {
int jpc = fs->jpc; /* save list of jumps to here */
int j;
fs->jpc = NO_JUMP;
- j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP);
+ j = luaK_codeAsBx(fs, OP_JMP, 0, NO_JUMP); // 生成OP_JUMP指令
luaK_concat(fs, &j, jpc); /* keep them on hold */
return j;
}
@@ -196,6 +197,7 @@ void luaK_concat (FuncState *fs, int *l1, int l2) {
}
+//c
void luaK_checkstack (FuncState *fs, int n) {
int newstack = fs->freereg + n;
if (newstack > fs->f->maxstacksize) {
@@ -344,7 +346,7 @@ static int code_label (FuncState *fs, int A, int b, int jump) {
return luaK_codeABC(fs, OP_LOADBOOL, A, b, jump);
}
-
+//c! 根据不同的表达式类型,比如NIL,TRUE,NUMBER等生成对应的字节码
static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
luaK_dischargevars(fs, e);
switch (e->k) {
@@ -360,7 +362,7 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) {
luaK_codeABx(fs, OP_LOADK, reg, e->u.s.info);
break;
}
- case VKNUM: {
+ case VKNUM: { // 将常量设置到寄存器
luaK_codeABx(fs, OP_LOADK, reg, luaK_numberK(fs, e->u.nval));
break;
}
@@ -416,10 +418,11 @@ static void exp2reg (FuncState *fs, expdesc *e, int reg) {
}
+//c! 根据expdesc结构生成字节码
void luaK_exp2nextreg (FuncState *fs, expdesc *e) {
- luaK_dischargevars(fs, e);
- freeexp(fs, e);
- luaK_reserveregs(fs, 1);
+ luaK_dischargevars(fs, e); // 根据变量的作用域来决定这个变量是否需要重定向,即VNONRELOC或VRELOC
+ freeexp(fs, e); // 如果不需要重定向,释放expdesc寄存器
+ luaK_reserveregs(fs, 1); //申请一个寄存器
exp2reg(fs, e, fs->freereg - 1);
}
@@ -791,7 +794,7 @@ void luaK_fixline (FuncState *fs, int line) {
}
-//c 指令生成
+//c 指令生成的唯一入口
static int luaK_code (FuncState *fs, Instruction i, int line) {
Proto *f = fs->f;
dischargejpc(fs); /* `pc' will change */