|
|
|
@ -1,14 +1,19 @@
|
|
|
|
|
#define lvm_c
|
|
|
|
|
#define LUA_CORE
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
// FIXME a hack
|
|
|
|
|
#define LUA_BUILD_AS_DLL
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
#include "lauxlib.h"
|
|
|
|
|
#include "lualib.h"
|
|
|
|
|
|
|
|
|
|
#include "ldebug.h"
|
|
|
|
|
#include "ldo.h"
|
|
|
|
@ -22,7 +27,236 @@
|
|
|
|
|
#include "ltm.h"
|
|
|
|
|
#include "lvm.h"
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#define VOID(p) ((const void*)(p))
|
|
|
|
|
|
|
|
|
|
static void PrintString(const TString* ts)
|
|
|
|
|
{
|
|
|
|
|
const char* s = getstr(ts);
|
|
|
|
|
size_t i, n = ts->len;
|
|
|
|
|
printf("%c", '"');
|
|
|
|
|
for (i = 0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
int c = (int)(unsigned char)s[i];
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case '"': printf("\\\""); break;
|
|
|
|
|
case '\\': printf("\\\\"); break;
|
|
|
|
|
case '\a': printf("\\a"); break;
|
|
|
|
|
case '\b': printf("\\b"); break;
|
|
|
|
|
case '\f': printf("\\f"); break;
|
|
|
|
|
case '\n': printf("\\n"); break;
|
|
|
|
|
case '\r': printf("\\r"); break;
|
|
|
|
|
case '\t': printf("\\t"); break;
|
|
|
|
|
case '\v': printf("\\v"); break;
|
|
|
|
|
default: if (isprint(c))
|
|
|
|
|
printf("%c", c);
|
|
|
|
|
else
|
|
|
|
|
printf("\\%03d", c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
printf("%c", '"');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void PrintConstant(const Proto* f, int i)
|
|
|
|
|
{
|
|
|
|
|
const TValue* o = &f->k[i];
|
|
|
|
|
switch (ttype(o))
|
|
|
|
|
{
|
|
|
|
|
case LUA_TNIL:
|
|
|
|
|
printf("nil");
|
|
|
|
|
break;
|
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
|
printf(bvalue(o) ? "true" : "false");
|
|
|
|
|
break;
|
|
|
|
|
case LUA_TNUMFLT:
|
|
|
|
|
printf(LUA_NUMBER_FMT, fltvalue(o));
|
|
|
|
|
break;
|
|
|
|
|
case LUA_TNUMINT:
|
|
|
|
|
printf(LUA_INTEGER_FMT, ivalue(o));
|
|
|
|
|
break;
|
|
|
|
|
case LUA_TSHRSTR: case LUA_TLNGSTR:
|
|
|
|
|
PrintString(tsvalue(o));
|
|
|
|
|
break;
|
|
|
|
|
default: /* cannot happen */
|
|
|
|
|
printf("? type=%d", ttype(o));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
|
|
|
|
|
#define MYK(x) (-1-(x))
|
|
|
|
|
|
|
|
|
|
static void PrintCode(const Proto* f)
|
|
|
|
|
{
|
|
|
|
|
const Instruction* code = f->code;
|
|
|
|
|
int pc, n = f->sizecode;
|
|
|
|
|
for (pc = 0; pc<n; pc++)
|
|
|
|
|
{
|
|
|
|
|
Instruction i = code[pc];
|
|
|
|
|
OpCode o = GET_OPCODE(i);
|
|
|
|
|
int a = GETARG_A(i);
|
|
|
|
|
int b = GETARG_B(i);
|
|
|
|
|
int c = GETARG_C(i);
|
|
|
|
|
int ax = GETARG_Ax(i);
|
|
|
|
|
int bx = GETARG_Bx(i);
|
|
|
|
|
int sbx = GETARG_sBx(i);
|
|
|
|
|
int line = getfuncline(f, pc);
|
|
|
|
|
printf("\t%d\t", pc + 1);
|
|
|
|
|
if (line>0) printf("[%d]\t", line); else printf("[-]\t");
|
|
|
|
|
printf("%-9s\t", luaP_opnames[o]);
|
|
|
|
|
switch (getOpMode(o))
|
|
|
|
|
{
|
|
|
|
|
case iABC:
|
|
|
|
|
printf("%d", a);
|
|
|
|
|
if (getBMode(o) != OpArgN) printf(" %d", ISK(b) ? (MYK(INDEXK(b))) : b);
|
|
|
|
|
if (getCMode(o) != OpArgN) printf(" %d", ISK(c) ? (MYK(INDEXK(c))) : c);
|
|
|
|
|
break;
|
|
|
|
|
case iABx:
|
|
|
|
|
printf("%d", a);
|
|
|
|
|
if (getBMode(o) == OpArgK) printf(" %d", MYK(bx));
|
|
|
|
|
if (getBMode(o) == OpArgU) printf(" %d", bx);
|
|
|
|
|
break;
|
|
|
|
|
case iAsBx:
|
|
|
|
|
printf("%d %d", a, sbx);
|
|
|
|
|
break;
|
|
|
|
|
case iAx:
|
|
|
|
|
printf("%d", MYK(ax));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
switch (o)
|
|
|
|
|
{
|
|
|
|
|
case OP_LOADK:
|
|
|
|
|
printf("\t; "); PrintConstant(f, bx);
|
|
|
|
|
break;
|
|
|
|
|
case OP_GETUPVAL:
|
|
|
|
|
case OP_SETUPVAL:
|
|
|
|
|
printf("\t; %s", UPVALNAME(b));
|
|
|
|
|
break;
|
|
|
|
|
case OP_GETTABUP:
|
|
|
|
|
printf("\t; %s", UPVALNAME(b));
|
|
|
|
|
if (ISK(c)) { printf(" "); PrintConstant(f, INDEXK(c)); }
|
|
|
|
|
break;
|
|
|
|
|
case OP_SETTABUP:
|
|
|
|
|
printf("\t; %s", UPVALNAME(a));
|
|
|
|
|
if (ISK(b)) { printf(" "); PrintConstant(f, INDEXK(b)); }
|
|
|
|
|
if (ISK(c)) { printf(" "); PrintConstant(f, INDEXK(c)); }
|
|
|
|
|
break;
|
|
|
|
|
case OP_GETTABLE:
|
|
|
|
|
case OP_SELF:
|
|
|
|
|
if (ISK(c)) { printf("\t; "); PrintConstant(f, INDEXK(c)); }
|
|
|
|
|
break;
|
|
|
|
|
case OP_SETTABLE:
|
|
|
|
|
case OP_ADD:
|
|
|
|
|
case OP_SUB:
|
|
|
|
|
case OP_MUL:
|
|
|
|
|
case OP_POW:
|
|
|
|
|
case OP_DIV:
|
|
|
|
|
case OP_IDIV:
|
|
|
|
|
case OP_BAND:
|
|
|
|
|
case OP_BOR:
|
|
|
|
|
case OP_BXOR:
|
|
|
|
|
case OP_SHL:
|
|
|
|
|
case OP_SHR:
|
|
|
|
|
case OP_EQ:
|
|
|
|
|
case OP_LT:
|
|
|
|
|
case OP_LE:
|
|
|
|
|
if (ISK(b) || ISK(c))
|
|
|
|
|
{
|
|
|
|
|
printf("\t; ");
|
|
|
|
|
if (ISK(b)) PrintConstant(f, INDEXK(b)); else printf("-");
|
|
|
|
|
printf(" ");
|
|
|
|
|
if (ISK(c)) PrintConstant(f, INDEXK(c)); else printf("-");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case OP_JMP:
|
|
|
|
|
case OP_FORLOOP:
|
|
|
|
|
case OP_FORPREP:
|
|
|
|
|
case OP_TFORLOOP:
|
|
|
|
|
printf("\t; to %d", sbx + pc + 2);
|
|
|
|
|
break;
|
|
|
|
|
case OP_CLOSURE:
|
|
|
|
|
printf("\t; %p", VOID(f->p[bx]));
|
|
|
|
|
break;
|
|
|
|
|
case OP_SETLIST:
|
|
|
|
|
if (c == 0) printf("\t; %d", (int)code[++pc]); else printf("\t; %d", c);
|
|
|
|
|
break;
|
|
|
|
|
case OP_EXTRAARG:
|
|
|
|
|
printf("\t; "); PrintConstant(f, ax);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SS(x) ((x==1)?"":"s")
|
|
|
|
|
#define S(x) (int)(x),SS(x)
|
|
|
|
|
|
|
|
|
|
static void PrintHeader(const Proto* f)
|
|
|
|
|
{
|
|
|
|
|
const char* s = f->source ? getstr(f->source) : "=?";
|
|
|
|
|
if (*s == '@' || *s == '=')
|
|
|
|
|
s++;
|
|
|
|
|
else if (*s == LUA_SIGNATURE[0])
|
|
|
|
|
s = "(bstring)";
|
|
|
|
|
else
|
|
|
|
|
s = "(string)";
|
|
|
|
|
printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
|
|
|
|
|
(f->linedefined == 0) ? "main" : "function", s,
|
|
|
|
|
f->linedefined, f->lastlinedefined,
|
|
|
|
|
S(f->sizecode), VOID(f));
|
|
|
|
|
printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
|
|
|
|
|
(int)(f->numparams), f->is_vararg ? "+" : "", SS(f->numparams),
|
|
|
|
|
S(f->maxstacksize), S(f->sizeupvalues));
|
|
|
|
|
printf("%d local%s, %d constant%s, %d function%s\n",
|
|
|
|
|
S(f->sizelocvars), S(f->sizek), S(f->sizep));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void PrintDebug(const Proto* f)
|
|
|
|
|
{
|
|
|
|
|
int i, n;
|
|
|
|
|
n = f->sizek;
|
|
|
|
|
printf("constants (%d) for %p:\n", n, VOID(f));
|
|
|
|
|
for (i = 0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("\t%d\t", i + 1);
|
|
|
|
|
PrintConstant(f, i);
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
n = f->sizelocvars;
|
|
|
|
|
printf("locals (%d) for %p:\n", n, VOID(f));
|
|
|
|
|
for (i = 0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("\t%d\t%s\t%d\t%d\n",
|
|
|
|
|
i, getstr(f->locvars[i].varname), f->locvars[i].startpc + 1, f->locvars[i].endpc + 1);
|
|
|
|
|
}
|
|
|
|
|
n = f->sizeupvalues;
|
|
|
|
|
printf("upvalues (%d) for %p:\n", n, VOID(f));
|
|
|
|
|
for (i = 0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
printf("\t%d\t%s\t%d\t%d\n",
|
|
|
|
|
i, UPVALNAME(i), f->upvalues[i].instack, f->upvalues[i].idx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void PrintFunction(const Proto* f, int full)
|
|
|
|
|
{
|
|
|
|
|
int i, n = f->sizep;
|
|
|
|
|
PrintHeader(f);
|
|
|
|
|
PrintCode(f);
|
|
|
|
|
if (full) PrintDebug(f);
|
|
|
|
|
for (i = 0; i<n; i++) PrintFunction(f->p[i], full);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define toproto(L,i) getproto(L->top+(i))
|
|
|
|
|
|
|
|
|
|
static void DumpFunction(lua_State *L)
|
|
|
|
|
{
|
|
|
|
|
Proto* f;
|
|
|
|
|
f = toproto(L, -1);
|
|
|
|
|
PrintFunction(f, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
lua_State *L;
|
|
|
|
@ -220,6 +454,19 @@ static int test_return0()
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int test_luacomp1()
|
|
|
|
|
{
|
|
|
|
|
int rc = 0;
|
|
|
|
|
lua_State *L;
|
|
|
|
|
L = luaL_newstate();
|
|
|
|
|
const char *code = "local i:int = 5; return i";
|
|
|
|
|
if (luaL_loadbuffer(L, code, strlen(code), "testfunc") != 0)
|
|
|
|
|
rc = 1;
|
|
|
|
|
DumpFunction(L);
|
|
|
|
|
lua_close(L);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(const char *argv[])
|
|
|
|
|
{
|
|
|
|
@ -229,7 +476,7 @@ int main(const char *argv[])
|
|
|
|
|
failures += test_unmf();
|
|
|
|
|
failures += test_binintop(OP_RAVI_ADDIIRR, 6, 7, 13);
|
|
|
|
|
failures += test_binintop(OP_RAVI_MULIIRR, 6, 7, 42);
|
|
|
|
|
|
|
|
|
|
failures += test_luacomp1();
|
|
|
|
|
|
|
|
|
|
return failures ? 1 : 0;
|
|
|
|
|
}
|