|
@@ -1,5 +1,5 @@
|
|
/*
|
|
/*
|
|
-** $Id: lcode.c,v 1.2 2000/03/03 12:33:59 roberto Exp roberto $
|
|
|
|
|
|
+** $Id: lcode.c,v 1.3 2000/03/03 14:58:26 roberto Exp roberto $
|
|
** Code generator for Lua
|
|
** Code generator for Lua
|
|
** See Copyright Notice in lua.h
|
|
** See Copyright Notice in lua.h
|
|
*/
|
|
*/
|
|
@@ -20,12 +20,10 @@ void luaK_error (LexState *ls, const char *msg) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-static Instruction *last_i (FuncState *fs) {
|
|
|
|
- static Instruction dummy = SET_OPCODE(0, ENDCODE);
|
|
|
|
- if (fs->last_pc < 0)
|
|
|
|
- return &dummy;
|
|
|
|
- else
|
|
|
|
- return &fs->f->code[fs->last_pc];
|
|
|
|
|
|
+static Instruction *last_i (LexState *ls, expdesc *v) {
|
|
|
|
+ FuncState *fs = ls->fs;
|
|
|
|
+ int last_pc = (v->info != NOJUMPS) ? v->info : fs->pc-1;
|
|
|
|
+ return &fs->f->code[last_pc];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -37,58 +35,64 @@ int luaK_primitivecode (LexState *ls, Instruction i) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+static void luaK_minus (LexState *ls, expdesc *v) {
|
|
|
|
+ Instruction *last = last_i(ls, v);
|
|
|
|
+ switch(GET_OPCODE(*last)) {
|
|
|
|
+ case PUSHINT: *last = SETARG_S(*last, -GETARG_S(*last)); return;
|
|
|
|
+ case PUSHNUM: *last = SET_OPCODE(*last, PUSHNEGNUM); return;
|
|
|
|
+ case PUSHNEGNUM: *last = SET_OPCODE(*last, PUSHNUM); return;
|
|
|
|
+ default: luaK_primitivecode(ls, CREATE_0(MINUSOP));
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
-int luaK_code (LexState *ls, Instruction i) {
|
|
|
|
- FuncState *fs = ls->fs;
|
|
|
|
- Instruction *last = last_i(fs);
|
|
|
|
- switch (GET_OPCODE(i)) {
|
|
|
|
-
|
|
|
|
- case MINUSOP:
|
|
|
|
- switch(GET_OPCODE(*last)) {
|
|
|
|
- case PUSHINT: *last = SETARG_S(*last, -GETARG_S(*last)); break;
|
|
|
|
- case PUSHNUM: *last = SET_OPCODE(*last, PUSHNEGNUM); break;
|
|
|
|
- case PUSHNEGNUM: *last = SET_OPCODE(*last, PUSHNUM); break;
|
|
|
|
- default: fs->last_pc = luaK_primitivecode(ls, i);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
|
|
|
|
- case GETTABLE:
|
|
|
|
- switch(GET_OPCODE(*last)) {
|
|
|
|
- case PUSHSTRING: *last = SET_OPCODE(*last, GETDOTTED); break;
|
|
|
|
- default: fs->last_pc = luaK_primitivecode(ls, i);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
|
|
+static void luaK_gettable (LexState *ls, expdesc *v) {
|
|
|
|
+ Instruction *last = last_i(ls, v);
|
|
|
|
+ luaK_deltastack(ls, -1);
|
|
|
|
+ switch(GET_OPCODE(*last)) {
|
|
|
|
+ case PUSHSTRING: *last = SET_OPCODE(*last, GETDOTTED); break;
|
|
|
|
+ default: luaK_primitivecode(ls, CREATE_0(GETTABLE));
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
- case RETCODE:
|
|
|
|
- switch(GET_OPCODE(*last)) {
|
|
|
|
- case CALL:
|
|
|
|
- *last = SET_OPCODE(*last, TAILCALL);
|
|
|
|
- *last = SETARG_B(*last, GETARG_U(i));
|
|
|
|
- break;
|
|
|
|
- default: fs->last_pc = luaK_primitivecode(ls, i);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
|
|
|
|
- case ADDOP:
|
|
|
|
- switch(GET_OPCODE(*last)) {
|
|
|
|
- case PUSHINT: *last = SET_OPCODE(*last, ADDI); break;
|
|
|
|
- default: fs->last_pc = luaK_primitivecode(ls, i);
|
|
|
|
- }
|
|
|
|
- break;
|
|
|
|
|
|
+static void luaK_add (LexState *ls, expdesc *v) {
|
|
|
|
+ Instruction *last = last_i(ls, v);
|
|
|
|
+ luaK_deltastack(ls, -1);
|
|
|
|
+ switch(GET_OPCODE(*last)) {
|
|
|
|
+ case PUSHINT: *last = SET_OPCODE(*last, ADDI); break;
|
|
|
|
+ default: luaK_primitivecode(ls, CREATE_0(ADDOP));
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
|
|
- case SUBOP:
|
|
|
|
- switch(GET_OPCODE(*last)) {
|
|
|
|
- case PUSHINT:
|
|
|
|
- *last = SET_OPCODE(*last, ADDI);
|
|
|
|
- *last = SETARG_S(*last, -GETARG_S(*last));
|
|
|
|
- break;
|
|
|
|
- default: fs->last_pc = luaK_primitivecode(ls, i);
|
|
|
|
- }
|
|
|
|
|
|
+static void luaK_sub (LexState *ls, expdesc *v) {
|
|
|
|
+ Instruction *last = last_i(ls, v);
|
|
|
|
+ luaK_deltastack(ls, -1);
|
|
|
|
+ switch(GET_OPCODE(*last)) {
|
|
|
|
+ case PUSHINT:
|
|
|
|
+ *last = SET_OPCODE(*last, ADDI);
|
|
|
|
+ *last = SETARG_S(*last, -GETARG_S(*last));
|
|
break;
|
|
break;
|
|
|
|
+ default: luaK_primitivecode(ls, CREATE_0(SUBOP));
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
|
|
- default: fs->last_pc = luaK_primitivecode(ls, i);
|
|
|
|
|
|
+
|
|
|
|
+void luaK_retcode (LexState *ls, int nlocals, listdesc *e) {
|
|
|
|
+ if (e->n > 0 && luaK_iscall(ls, e->info)) {
|
|
|
|
+ Instruction *last = &ls->fs->f->code[ls->fs->pc-1];
|
|
|
|
+ *last = SET_OPCODE(*last, TAILCALL);
|
|
|
|
+ *last = SETARG_B(*last, nlocals);
|
|
}
|
|
}
|
|
- return fs->last_pc;
|
|
|
|
|
|
+ else
|
|
|
|
+ luaK_U(ls, RETCODE, nlocals, 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int luaK_code (LexState *ls, Instruction i, int delta) {
|
|
|
|
+ luaK_deltastack(ls, delta);
|
|
|
|
+ return luaK_primitivecode(ls, i);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -97,7 +101,6 @@ void luaK_fixjump (LexState *ls, int pc, int dest) {
|
|
Instruction *jmp = &fs->f->code[pc];
|
|
Instruction *jmp = &fs->f->code[pc];
|
|
/* jump is relative to position following jump instruction */
|
|
/* jump is relative to position following jump instruction */
|
|
*jmp = SETARG_S(*jmp, dest-(pc+1));
|
|
*jmp = SETARG_S(*jmp, dest-(pc+1));
|
|
- fs->last_pc = pc;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -112,38 +115,8 @@ void luaK_deltastack (LexState *ls, int delta) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-static int aux_code (LexState *ls, OpCode op, Instruction i, int delta) {
|
|
|
|
- luaK_deltastack(ls, delta);
|
|
|
|
- return luaK_code(ls, SET_OPCODE(i, op));
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-int luaK_0 (LexState *ls, OpCode op, int delta) {
|
|
|
|
- return aux_code(ls, op, 0, delta);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-int luaK_U (LexState *ls, OpCode op, int u, int delta) {
|
|
|
|
- Instruction i = SETARG_U(0, u);
|
|
|
|
- return aux_code(ls, op, i, delta);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-int luaK_S (LexState *ls, OpCode op, int s, int delta) {
|
|
|
|
- Instruction i = SETARG_S(0, s);
|
|
|
|
- return aux_code(ls, op, i, delta);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-int luaK_AB (LexState *ls, OpCode op, int a, int b, int delta) {
|
|
|
|
- Instruction i = SETARG_A(0, a);
|
|
|
|
- i = SETARG_B(i, b);
|
|
|
|
- return aux_code(ls, op, i, delta);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-int luaK_kstr (LexState *ls, int c) {
|
|
|
|
- return luaK_U(ls, PUSHSTRING, c, 1);
|
|
|
|
|
|
+void luaK_kstr (LexState *ls, int c) {
|
|
|
|
+ luaK_U(ls, PUSHSTRING, c, 1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -166,37 +139,40 @@ static int real_constant (LexState *ls, real r) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-int luaK_number (LexState *ls, real f) {
|
|
|
|
|
|
+void luaK_number (LexState *ls, real f) {
|
|
if (f <= (real)MAXARG_S && (int)f == f)
|
|
if (f <= (real)MAXARG_S && (int)f == f)
|
|
- return luaK_S(ls, PUSHINT, (int)f, 1); /* f has a short integer value */
|
|
|
|
|
|
+ luaK_S(ls, PUSHINT, (int)f, 1); /* f has a short integer value */
|
|
else
|
|
else
|
|
- return luaK_U(ls, PUSHNUM, real_constant(ls, f), 1);
|
|
|
|
|
|
+ luaK_U(ls, PUSHNUM, real_constant(ls, f), 1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-int luaK_adjuststack (LexState *ls, int n) {
|
|
|
|
|
|
+void luaK_adjuststack (LexState *ls, int n) {
|
|
if (n > 0)
|
|
if (n > 0)
|
|
- return luaK_U(ls, POP, n, -n);
|
|
|
|
|
|
+ luaK_U(ls, POP, n, -n);
|
|
else if (n < 0)
|
|
else if (n < 0)
|
|
- return luaK_U(ls, PUSHNIL, (-n)-1, -n);
|
|
|
|
- else return 0;
|
|
|
|
|
|
+ luaK_U(ls, PUSHNIL, (-n)-1, -n);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-int luaK_iscall (LexState *ls, int pc) {
|
|
|
|
- return (GET_OPCODE(ls->fs->f->code[pc]) == CALL);
|
|
|
|
|
|
+int luaK_iscall (LexState *ls, int hasjumps) {
|
|
|
|
+ if (hasjumps) return 0; /* a call cannot have internal jumps */
|
|
|
|
+ else /* check whether last instruction is a function call */
|
|
|
|
+ return (GET_OPCODE(ls->fs->f->code[ls->fs->pc-1]) == CALL);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-void luaK_setcallreturns (LexState *ls, int pc, int nresults) {
|
|
|
|
- if (luaK_iscall(ls, pc)) { /* expression is a function call? */
|
|
|
|
- Instruction *i = &ls->fs->f->code[pc];
|
|
|
|
- int old_nresults = GETARG_B(*i);
|
|
|
|
- if (old_nresults != MULT_RET)
|
|
|
|
- luaK_deltastack(ls, -old_nresults); /* pop old nresults */
|
|
|
|
- *i = SETARG_B(*i, nresults); /* set nresults */
|
|
|
|
- if (nresults != MULT_RET)
|
|
|
|
- luaK_deltastack(ls, nresults); /* push results */
|
|
|
|
|
|
+void luaK_setcallreturns (LexState *ls, int hasjumps, int nresults) {
|
|
|
|
+ if (!hasjumps) { /* if `hasjumps' cannot be a function call */
|
|
|
|
+ Instruction *i = &ls->fs->f->code[ls->fs->pc-1];
|
|
|
|
+ if (GET_OPCODE(*i) == CALL) { /* expression is a function call? */
|
|
|
|
+ int old_nresults = GETARG_B(*i);
|
|
|
|
+ if (old_nresults != MULT_RET)
|
|
|
|
+ luaK_deltastack(ls, -old_nresults); /* pop old nresults */
|
|
|
|
+ *i = SETARG_B(*i, nresults); /* set nresults */
|
|
|
|
+ if (nresults != MULT_RET)
|
|
|
|
+ luaK_deltastack(ls, nresults); /* push results */
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -209,20 +185,21 @@ static void assertglobal (LexState *ls, int index) {
|
|
void luaK_2stack (LexState *ls, expdesc *var) {
|
|
void luaK_2stack (LexState *ls, expdesc *var) {
|
|
switch (var->k) {
|
|
switch (var->k) {
|
|
case VLOCAL:
|
|
case VLOCAL:
|
|
- var->info = luaK_U(ls, PUSHLOCAL, var->info, 1);
|
|
|
|
|
|
+ luaK_U(ls, PUSHLOCAL, var->info, 1);
|
|
break;
|
|
break;
|
|
case VGLOBAL:
|
|
case VGLOBAL:
|
|
|
|
+ luaK_U(ls, GETGLOBAL, var->info, 1);
|
|
assertglobal(ls, var->info); /* make sure that there is a global */
|
|
assertglobal(ls, var->info); /* make sure that there is a global */
|
|
- var->info = luaK_U(ls, GETGLOBAL, var->info, 1);
|
|
|
|
break;
|
|
break;
|
|
case VINDEXED:
|
|
case VINDEXED:
|
|
- var->info = luaK_0(ls, GETTABLE, -1);
|
|
|
|
|
|
+ luaK_gettable(ls, var);
|
|
break;
|
|
break;
|
|
case VEXP:
|
|
case VEXP:
|
|
luaK_setcallreturns(ls, var->info, 1); /* call must return 1 value */
|
|
luaK_setcallreturns(ls, var->info, 1); /* call must return 1 value */
|
|
- break;
|
|
|
|
|
|
+ return; /* does not change var->info */
|
|
}
|
|
}
|
|
var->k = VEXP;
|
|
var->k = VEXP;
|
|
|
|
+ var->info = NOJUMPS;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -246,10 +223,9 @@ void luaK_storevar (LexState *ls, const expdesc *var) {
|
|
|
|
|
|
void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
|
void luaK_prefix (LexState *ls, int op, expdesc *v) {
|
|
luaK_2stack(ls, v);
|
|
luaK_2stack(ls, v);
|
|
- if (op == '-')
|
|
|
|
- v->info = luaK_0(ls, MINUSOP, 0);
|
|
|
|
- else
|
|
|
|
- v->info = luaK_0(ls, NOTOP, 0);
|
|
|
|
|
|
+ if (op == '-') luaK_minus(ls, v);
|
|
|
|
+ else luaK_0(ls, NOTOP, 0);
|
|
|
|
+ v->info = NOJUMPS;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -267,19 +243,20 @@ void luaK_posfix (LexState *ls, int op, expdesc *v1, expdesc *v2) {
|
|
switch (op) {
|
|
switch (op) {
|
|
case AND: case OR:
|
|
case AND: case OR:
|
|
luaK_fixjump(ls, v1->info, ls->fs->pc);
|
|
luaK_fixjump(ls, v1->info, ls->fs->pc);
|
|
- break;
|
|
|
|
- case '+': v1->info = luaK_0(ls, ADDOP, -1); break;
|
|
|
|
- case '-': v1->info = luaK_0(ls, SUBOP, -1); break;
|
|
|
|
- case '*': v1->info = luaK_0(ls, MULTOP, -1); break;
|
|
|
|
- case '/': v1->info = luaK_0(ls, DIVOP, -1); break;
|
|
|
|
- case '^': v1->info = luaK_0(ls, POWOP, -1); break;
|
|
|
|
- case CONC: v1->info = luaK_0(ls, CONCOP, -1); break;
|
|
|
|
- case EQ: v1->info = luaK_0(ls, EQOP, -1); break;
|
|
|
|
- case NE: v1->info = luaK_0(ls, NEQOP, -1); break;
|
|
|
|
- case '>': v1->info = luaK_0(ls, GTOP, -1); break;
|
|
|
|
- case '<': v1->info = luaK_0(ls, LTOP, -1); break;
|
|
|
|
- case GE: v1->info = luaK_0(ls, GEOP, -1); break;
|
|
|
|
- case LE: v1->info = luaK_0(ls, LEOP, -1); break;
|
|
|
|
|
|
+ return; /* keep v1->info != NOJUMPS */
|
|
|
|
+ case '+': luaK_add(ls, v2); break;
|
|
|
|
+ case '-': luaK_sub(ls, v2); break;
|
|
|
|
+ case '*': luaK_0(ls, MULTOP, -1); break;
|
|
|
|
+ case '/': luaK_0(ls, DIVOP, -1); break;
|
|
|
|
+ case '^': luaK_0(ls, POWOP, -1); break;
|
|
|
|
+ case CONC: luaK_0(ls, CONCOP, -1); break;
|
|
|
|
+ case EQ: luaK_0(ls, EQOP, -1); break;
|
|
|
|
+ case NE: luaK_0(ls, NEQOP, -1); break;
|
|
|
|
+ case '>': luaK_0(ls, GTOP, -1); break;
|
|
|
|
+ case '<': luaK_0(ls, LTOP, -1); break;
|
|
|
|
+ case GE: luaK_0(ls, GEOP, -1); break;
|
|
|
|
+ case LE: luaK_0(ls, LEOP, -1); break;
|
|
}
|
|
}
|
|
|
|
+ v1->info = NOJUMPS;
|
|
}
|
|
}
|
|
|
|
|