parse local i:type syntax and add dump function code in test case

Dibyendu Majumdar 9 years ago
parent 800b85233e
commit cd2fdd508c

@ -58,6 +58,7 @@ typedef struct expdesc {
/* description of active local variable */
typedef struct Vardesc {
short idx; /* variable index in stack */
int ravi_tt; /* type of variable if known */
} Vardesc;

@ -255,7 +255,8 @@
/* #define LUAI_FUNC __attribute__((visibility("hidden"))) extern */
#define LUAI_FUNC extern
#else /* }{ */
#define LUAI_FUNC LUA_API /* extern */
#define LUAI_FUNC LUA_API
/* extern */
#endif /* } */
#define LUAI_DDEC LUAI_FUNC

@ -263,7 +263,6 @@ static int codeextraarg (FuncState *fs, int a) {
return luaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
}
int luaK_codek (FuncState *fs, int reg, int k) {
if (k <= MAXARG_Bx)
return luaK_codeABx(fs, OP_LOADK, reg, k);

@ -170,8 +170,8 @@ static int registerlocalvar (LexState *ls, TString *varname) {
return fs->nlocvars++;
}
static void new_localvar (LexState *ls, TString *name) {
/* RAVI - added type tt */
static void new_localvar (LexState *ls, TString *name, int tt) {
FuncState *fs = ls->fs;
Dyndata *dyd = ls->dyd;
int reg = registerlocalvar(ls, name);
@ -180,11 +180,14 @@ static void new_localvar (LexState *ls, TString *name) {
luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
dyd->actvar.size, Vardesc, MAX_INT, "local variables");
dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg);
/* RAVI change - record type info for local variable */
dyd->actvar.arr[dyd->actvar.n - 1].ravi_tt = tt;
}
static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) {
new_localvar(ls, luaX_newstring(ls, name, sz));
/* RAVI change - add type */
new_localvar(ls, luaX_newstring(ls, name, sz), LUA_TSTRING);
}
#define new_localvarliteral(ls,v) \
@ -754,7 +757,8 @@ static void parlist (LexState *ls) {
do {
switch (ls->t.token) {
case TK_NAME: { /* param -> NAME */
new_localvar(ls, str_checkname(ls));
/* RAVI change - add type */
new_localvar(ls, str_checkname(ls), LUA_TNONE);
nparams++;
break;
}
@ -1318,7 +1322,7 @@ static void fornum (LexState *ls, TString *varname, int line) {
new_localvarliteral(ls, "(for index)");
new_localvarliteral(ls, "(for limit)");
new_localvarliteral(ls, "(for step)");
new_localvar(ls, varname);
new_localvar(ls, varname, LUA_TNONE); /* RAVI TODO - add for x: type syntax? */
checknext(ls, '=');
exp1(ls); /* initial value */
checknext(ls, ',');
@ -1345,9 +1349,9 @@ static void forlist (LexState *ls, TString *indexname) {
new_localvarliteral(ls, "(for state)");
new_localvarliteral(ls, "(for control)");
/* create declared variables */
new_localvar(ls, indexname);
new_localvar(ls, indexname, LUA_TNONE); /* RAVI TODO for name:type syntax? */
while (testnext(ls, ',')) {
new_localvar(ls, str_checkname(ls));
new_localvar(ls, str_checkname(ls), LUA_TNONE); /* RAVI change - add type */
nvars++;
}
checknext(ls, TK_IN);
@ -1428,7 +1432,8 @@ static void ifstat (LexState *ls, int line) {
static void localfunc (LexState *ls) {
expdesc b;
FuncState *fs = ls->fs;
new_localvar(ls, str_checkname(ls)); /* new local variable */
/* RAVI change - add type */
new_localvar(ls, str_checkname(ls), LUA_TFUNCTION); /* new local variable */
adjustlocalvars(ls, 1); /* enter its scope */
body(ls, &b, 0, ls->linenumber); /* function created in next register */
/* debug information will only see the variable after this point! */
@ -1442,7 +1447,20 @@ static void localstat (LexState *ls) {
int nexps;
expdesc e;
do {
new_localvar(ls, str_checkname(ls));
/* RAVI changes start */
/* local name : type = value */
TString *name = str_checkname(ls);
int tt = LUA_TNONE;
if (testnext(ls, ':')) {
TString *typename = str_checkname(ls); /* we expect a type name */
const char *str = getaddrstr(typename);
if (strcmp(str, "int") == 0)
tt = LUA_TNUMINT;
else if (strcmp(str, "double") == 0)
tt = LUA_TNUMFLT;
}
new_localvar(ls, name, tt);
/* RAVI changes end */
nvars++;
} while (testnext(ls, ','));
if (testnext(ls, '='))

@ -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;
}
Loading…
Cancel
Save