xref: /freebsd/contrib/lua/src/luac.c (revision a9490b81b032b43cdb3c8c76b4d01bbad9ff82c1)
18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: luac.c $
38e3e3a7aSWarner Losh ** Lua compiler (saves bytecodes to files; also lists bytecodes)
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh 
78e3e3a7aSWarner Losh #define luac_c
88e3e3a7aSWarner Losh #define LUA_CORE
98e3e3a7aSWarner Losh 
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh 
128e3e3a7aSWarner Losh #include <ctype.h>
138e3e3a7aSWarner Losh #include <errno.h>
148e3e3a7aSWarner Losh #include <stdio.h>
158e3e3a7aSWarner Losh #include <stdlib.h>
168e3e3a7aSWarner Losh #include <string.h>
178e3e3a7aSWarner Losh 
188e3e3a7aSWarner Losh #include "lua.h"
198e3e3a7aSWarner Losh #include "lauxlib.h"
208e3e3a7aSWarner Losh 
210495ed39SKyle Evans #include "ldebug.h"
228e3e3a7aSWarner Losh #include "lobject.h"
230495ed39SKyle Evans #include "lopcodes.h"
240495ed39SKyle Evans #include "lopnames.h"
258e3e3a7aSWarner Losh #include "lstate.h"
268e3e3a7aSWarner Losh #include "lundump.h"
278e3e3a7aSWarner Losh 
288e3e3a7aSWarner Losh static void PrintFunction(const Proto* f, int full);
298e3e3a7aSWarner Losh #define luaU_print	PrintFunction
308e3e3a7aSWarner Losh 
318e3e3a7aSWarner Losh #define PROGNAME	"luac"		/* default program name */
328e3e3a7aSWarner Losh #define OUTPUT		PROGNAME ".out"	/* default output file */
338e3e3a7aSWarner Losh 
348e3e3a7aSWarner Losh static int listing=0;			/* list bytecodes? */
358e3e3a7aSWarner Losh static int dumping=1;			/* dump bytecodes? */
368e3e3a7aSWarner Losh static int stripping=0;			/* strip debug information? */
378e3e3a7aSWarner Losh static char Output[]={ OUTPUT };	/* default output file name */
388e3e3a7aSWarner Losh static const char* output=Output;	/* actual output file name */
398e3e3a7aSWarner Losh static const char* progname=PROGNAME;	/* actual program name */
400495ed39SKyle Evans static TString **tmname;
418e3e3a7aSWarner Losh 
fatal(const char * message)428e3e3a7aSWarner Losh static void fatal(const char* message)
438e3e3a7aSWarner Losh {
448e3e3a7aSWarner Losh  fprintf(stderr,"%s: %s\n",progname,message);
458e3e3a7aSWarner Losh  exit(EXIT_FAILURE);
468e3e3a7aSWarner Losh }
478e3e3a7aSWarner Losh 
cannot(const char * what)488e3e3a7aSWarner Losh static void cannot(const char* what)
498e3e3a7aSWarner Losh {
508e3e3a7aSWarner Losh  fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
518e3e3a7aSWarner Losh  exit(EXIT_FAILURE);
528e3e3a7aSWarner Losh }
538e3e3a7aSWarner Losh 
usage(const char * message)548e3e3a7aSWarner Losh static void usage(const char* message)
558e3e3a7aSWarner Losh {
568e3e3a7aSWarner Losh  if (*message=='-')
578e3e3a7aSWarner Losh   fprintf(stderr,"%s: unrecognized option '%s'\n",progname,message);
588e3e3a7aSWarner Losh  else
598e3e3a7aSWarner Losh   fprintf(stderr,"%s: %s\n",progname,message);
608e3e3a7aSWarner Losh  fprintf(stderr,
618e3e3a7aSWarner Losh   "usage: %s [options] [filenames]\n"
628e3e3a7aSWarner Losh   "Available options are:\n"
638e3e3a7aSWarner Losh   "  -l       list (use -l -l for full listing)\n"
648e3e3a7aSWarner Losh   "  -o name  output to file 'name' (default is \"%s\")\n"
658e3e3a7aSWarner Losh   "  -p       parse only\n"
668e3e3a7aSWarner Losh   "  -s       strip debug information\n"
678e3e3a7aSWarner Losh   "  -v       show version information\n"
688e3e3a7aSWarner Losh   "  --       stop handling options\n"
698e3e3a7aSWarner Losh   "  -        stop handling options and process stdin\n"
708e3e3a7aSWarner Losh   ,progname,Output);
718e3e3a7aSWarner Losh  exit(EXIT_FAILURE);
728e3e3a7aSWarner Losh }
738e3e3a7aSWarner Losh 
748e3e3a7aSWarner Losh #define IS(s)	(strcmp(argv[i],s)==0)
758e3e3a7aSWarner Losh 
doargs(int argc,char * argv[])768e3e3a7aSWarner Losh static int doargs(int argc, char* argv[])
778e3e3a7aSWarner Losh {
788e3e3a7aSWarner Losh  int i;
798e3e3a7aSWarner Losh  int version=0;
808e3e3a7aSWarner Losh  if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
818e3e3a7aSWarner Losh  for (i=1; i<argc; i++)
828e3e3a7aSWarner Losh  {
838e3e3a7aSWarner Losh   if (*argv[i]!='-')			/* end of options; keep it */
848e3e3a7aSWarner Losh    break;
858e3e3a7aSWarner Losh   else if (IS("--"))			/* end of options; skip it */
868e3e3a7aSWarner Losh   {
878e3e3a7aSWarner Losh    ++i;
888e3e3a7aSWarner Losh    if (version) ++version;
898e3e3a7aSWarner Losh    break;
908e3e3a7aSWarner Losh   }
918e3e3a7aSWarner Losh   else if (IS("-"))			/* end of options; use stdin */
928e3e3a7aSWarner Losh    break;
938e3e3a7aSWarner Losh   else if (IS("-l"))			/* list */
948e3e3a7aSWarner Losh    ++listing;
958e3e3a7aSWarner Losh   else if (IS("-o"))			/* output file */
968e3e3a7aSWarner Losh   {
978e3e3a7aSWarner Losh    output=argv[++i];
988e3e3a7aSWarner Losh    if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
998e3e3a7aSWarner Losh     usage("'-o' needs argument");
1008e3e3a7aSWarner Losh    if (IS("-")) output=NULL;
1018e3e3a7aSWarner Losh   }
1028e3e3a7aSWarner Losh   else if (IS("-p"))			/* parse only */
1038e3e3a7aSWarner Losh    dumping=0;
1048e3e3a7aSWarner Losh   else if (IS("-s"))			/* strip debug information */
1058e3e3a7aSWarner Losh    stripping=1;
1068e3e3a7aSWarner Losh   else if (IS("-v"))			/* show version */
1078e3e3a7aSWarner Losh    ++version;
1088e3e3a7aSWarner Losh   else					/* unknown option */
1098e3e3a7aSWarner Losh    usage(argv[i]);
1108e3e3a7aSWarner Losh  }
1118e3e3a7aSWarner Losh  if (i==argc && (listing || !dumping))
1128e3e3a7aSWarner Losh  {
1138e3e3a7aSWarner Losh   dumping=0;
1148e3e3a7aSWarner Losh   argv[--i]=Output;
1158e3e3a7aSWarner Losh  }
1168e3e3a7aSWarner Losh  if (version)
1178e3e3a7aSWarner Losh  {
1188e3e3a7aSWarner Losh   printf("%s\n",LUA_COPYRIGHT);
1198e3e3a7aSWarner Losh   if (version==argc-1) exit(EXIT_SUCCESS);
1208e3e3a7aSWarner Losh  }
1218e3e3a7aSWarner Losh  return i;
1228e3e3a7aSWarner Losh }
1238e3e3a7aSWarner Losh 
124*a9490b81SWarner Losh #define FUNCTION "(function()end)();\n"
1258e3e3a7aSWarner Losh 
reader(lua_State * L,void * ud,size_t * size)1268e3e3a7aSWarner Losh static const char* reader(lua_State* L, void* ud, size_t* size)
1278e3e3a7aSWarner Losh {
1288e3e3a7aSWarner Losh  UNUSED(L);
1298e3e3a7aSWarner Losh  if ((*(int*)ud)--)
1308e3e3a7aSWarner Losh  {
1318e3e3a7aSWarner Losh   *size=sizeof(FUNCTION)-1;
1328e3e3a7aSWarner Losh   return FUNCTION;
1338e3e3a7aSWarner Losh  }
1348e3e3a7aSWarner Losh  else
1358e3e3a7aSWarner Losh  {
1368e3e3a7aSWarner Losh   *size=0;
1378e3e3a7aSWarner Losh   return NULL;
1388e3e3a7aSWarner Losh  }
1398e3e3a7aSWarner Losh }
1408e3e3a7aSWarner Losh 
141*a9490b81SWarner Losh #define toproto(L,i) getproto(s2v(L->top.p+(i)))
1428e3e3a7aSWarner Losh 
combine(lua_State * L,int n)1438e3e3a7aSWarner Losh static const Proto* combine(lua_State* L, int n)
1448e3e3a7aSWarner Losh {
1458e3e3a7aSWarner Losh  if (n==1)
1468e3e3a7aSWarner Losh   return toproto(L,-1);
1478e3e3a7aSWarner Losh  else
1488e3e3a7aSWarner Losh  {
1498e3e3a7aSWarner Losh   Proto* f;
1508e3e3a7aSWarner Losh   int i=n;
1518e3e3a7aSWarner Losh   if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
1528e3e3a7aSWarner Losh   f=toproto(L,-1);
1538e3e3a7aSWarner Losh   for (i=0; i<n; i++)
1548e3e3a7aSWarner Losh   {
1558e3e3a7aSWarner Losh    f->p[i]=toproto(L,i-n-1);
1568e3e3a7aSWarner Losh    if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
1578e3e3a7aSWarner Losh   }
1588e3e3a7aSWarner Losh   return f;
1598e3e3a7aSWarner Losh  }
1608e3e3a7aSWarner Losh }
1618e3e3a7aSWarner Losh 
writer(lua_State * L,const void * p,size_t size,void * u)1628e3e3a7aSWarner Losh static int writer(lua_State* L, const void* p, size_t size, void* u)
1638e3e3a7aSWarner Losh {
1648e3e3a7aSWarner Losh  UNUSED(L);
1658e3e3a7aSWarner Losh  return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
1668e3e3a7aSWarner Losh }
1678e3e3a7aSWarner Losh 
pmain(lua_State * L)1688e3e3a7aSWarner Losh static int pmain(lua_State* L)
1698e3e3a7aSWarner Losh {
1708e3e3a7aSWarner Losh  int argc=(int)lua_tointeger(L,1);
1718e3e3a7aSWarner Losh  char** argv=(char**)lua_touserdata(L,2);
1728e3e3a7aSWarner Losh  const Proto* f;
1738e3e3a7aSWarner Losh  int i;
1740495ed39SKyle Evans  tmname=G(L)->tmname;
1758e3e3a7aSWarner Losh  if (!lua_checkstack(L,argc)) fatal("too many input files");
1768e3e3a7aSWarner Losh  for (i=0; i<argc; i++)
1778e3e3a7aSWarner Losh  {
1788e3e3a7aSWarner Losh   const char* filename=IS("-") ? NULL : argv[i];
1798e3e3a7aSWarner Losh   if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
1808e3e3a7aSWarner Losh  }
1818e3e3a7aSWarner Losh  f=combine(L,argc);
1828e3e3a7aSWarner Losh  if (listing) luaU_print(f,listing>1);
1838e3e3a7aSWarner Losh  if (dumping)
1848e3e3a7aSWarner Losh  {
1858e3e3a7aSWarner Losh   FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
1868e3e3a7aSWarner Losh   if (D==NULL) cannot("open");
1878e3e3a7aSWarner Losh   lua_lock(L);
1888e3e3a7aSWarner Losh   luaU_dump(L,f,writer,D,stripping);
1898e3e3a7aSWarner Losh   lua_unlock(L);
1908e3e3a7aSWarner Losh   if (ferror(D)) cannot("write");
1918e3e3a7aSWarner Losh   if (fclose(D)) cannot("close");
1928e3e3a7aSWarner Losh  }
1938e3e3a7aSWarner Losh  return 0;
1948e3e3a7aSWarner Losh }
1958e3e3a7aSWarner Losh 
main(int argc,char * argv[])1968e3e3a7aSWarner Losh int main(int argc, char* argv[])
1978e3e3a7aSWarner Losh {
1988e3e3a7aSWarner Losh  lua_State* L;
1998e3e3a7aSWarner Losh  int i=doargs(argc,argv);
2008e3e3a7aSWarner Losh  argc-=i; argv+=i;
2018e3e3a7aSWarner Losh  if (argc<=0) usage("no input files given");
2028e3e3a7aSWarner Losh  L=luaL_newstate();
2038e3e3a7aSWarner Losh  if (L==NULL) fatal("cannot create state: not enough memory");
2048e3e3a7aSWarner Losh  lua_pushcfunction(L,&pmain);
2058e3e3a7aSWarner Losh  lua_pushinteger(L,argc);
2068e3e3a7aSWarner Losh  lua_pushlightuserdata(L,argv);
2078e3e3a7aSWarner Losh  if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
2088e3e3a7aSWarner Losh  lua_close(L);
2098e3e3a7aSWarner Losh  return EXIT_SUCCESS;
2108e3e3a7aSWarner Losh }
2118e3e3a7aSWarner Losh 
2128e3e3a7aSWarner Losh /*
2138e3e3a7aSWarner Losh ** print bytecodes
2148e3e3a7aSWarner Losh */
2158e3e3a7aSWarner Losh 
2160495ed39SKyle Evans #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
2178e3e3a7aSWarner Losh #define VOID(p) ((const void*)(p))
2180495ed39SKyle Evans #define eventname(i) (getstr(tmname[i]))
2198e3e3a7aSWarner Losh 
PrintString(const TString * ts)2208e3e3a7aSWarner Losh static void PrintString(const TString* ts)
2218e3e3a7aSWarner Losh {
2228e3e3a7aSWarner Losh  const char* s=getstr(ts);
2238e3e3a7aSWarner Losh  size_t i,n=tsslen(ts);
2240495ed39SKyle Evans  printf("\"");
2258e3e3a7aSWarner Losh  for (i=0; i<n; i++)
2268e3e3a7aSWarner Losh  {
2278e3e3a7aSWarner Losh   int c=(int)(unsigned char)s[i];
2288e3e3a7aSWarner Losh   switch (c)
2298e3e3a7aSWarner Losh   {
2300495ed39SKyle Evans    case '"':
2310495ed39SKyle Evans 	printf("\\\"");
2320495ed39SKyle Evans 	break;
2330495ed39SKyle Evans    case '\\':
2340495ed39SKyle Evans 	printf("\\\\");
2350495ed39SKyle Evans 	break;
2360495ed39SKyle Evans    case '\a':
2370495ed39SKyle Evans 	printf("\\a");
2380495ed39SKyle Evans 	break;
2390495ed39SKyle Evans    case '\b':
2400495ed39SKyle Evans 	printf("\\b");
2410495ed39SKyle Evans 	break;
2420495ed39SKyle Evans    case '\f':
2430495ed39SKyle Evans 	printf("\\f");
2440495ed39SKyle Evans 	break;
2450495ed39SKyle Evans    case '\n':
2460495ed39SKyle Evans 	printf("\\n");
2470495ed39SKyle Evans 	break;
2480495ed39SKyle Evans    case '\r':
2490495ed39SKyle Evans 	printf("\\r");
2500495ed39SKyle Evans 	break;
2510495ed39SKyle Evans    case '\t':
2520495ed39SKyle Evans 	printf("\\t");
2530495ed39SKyle Evans 	break;
2540495ed39SKyle Evans    case '\v':
2550495ed39SKyle Evans 	printf("\\v");
2560495ed39SKyle Evans 	break;
2570495ed39SKyle Evans    default:
2580495ed39SKyle Evans 	if (isprint(c)) printf("%c",c); else printf("\\%03d",c);
2590495ed39SKyle Evans 	break;
2608e3e3a7aSWarner Losh   }
2618e3e3a7aSWarner Losh  }
2620495ed39SKyle Evans  printf("\"");
2630495ed39SKyle Evans }
2640495ed39SKyle Evans 
PrintType(const Proto * f,int i)2650495ed39SKyle Evans static void PrintType(const Proto* f, int i)
2660495ed39SKyle Evans {
2670495ed39SKyle Evans  const TValue* o=&f->k[i];
2680495ed39SKyle Evans  switch (ttypetag(o))
2690495ed39SKyle Evans  {
2700495ed39SKyle Evans   case LUA_VNIL:
2710495ed39SKyle Evans 	printf("N");
2720495ed39SKyle Evans 	break;
2730495ed39SKyle Evans   case LUA_VFALSE:
2740495ed39SKyle Evans   case LUA_VTRUE:
2750495ed39SKyle Evans 	printf("B");
2760495ed39SKyle Evans 	break;
2770495ed39SKyle Evans   case LUA_VNUMFLT:
2780495ed39SKyle Evans 	printf("F");
2790495ed39SKyle Evans 	break;
2800495ed39SKyle Evans   case LUA_VNUMINT:
2810495ed39SKyle Evans 	printf("I");
2820495ed39SKyle Evans 	break;
2830495ed39SKyle Evans   case LUA_VSHRSTR:
2840495ed39SKyle Evans   case LUA_VLNGSTR:
2850495ed39SKyle Evans 	printf("S");
2860495ed39SKyle Evans 	break;
2870495ed39SKyle Evans   default:				/* cannot happen */
2880495ed39SKyle Evans 	printf("?%d",ttypetag(o));
2890495ed39SKyle Evans 	break;
2900495ed39SKyle Evans  }
2910495ed39SKyle Evans  printf("\t");
2928e3e3a7aSWarner Losh }
2938e3e3a7aSWarner Losh 
PrintConstant(const Proto * f,int i)2948e3e3a7aSWarner Losh static void PrintConstant(const Proto* f, int i)
2958e3e3a7aSWarner Losh {
2968e3e3a7aSWarner Losh  const TValue* o=&f->k[i];
2970495ed39SKyle Evans  switch (ttypetag(o))
2988e3e3a7aSWarner Losh  {
2990495ed39SKyle Evans   case LUA_VNIL:
3008e3e3a7aSWarner Losh 	printf("nil");
3018e3e3a7aSWarner Losh 	break;
3020495ed39SKyle Evans   case LUA_VFALSE:
3030495ed39SKyle Evans 	printf("false");
3048e3e3a7aSWarner Losh 	break;
3050495ed39SKyle Evans   case LUA_VTRUE:
3060495ed39SKyle Evans 	printf("true");
3070495ed39SKyle Evans 	break;
3080495ed39SKyle Evans   case LUA_VNUMFLT:
3098e3e3a7aSWarner Losh 	{
3108e3e3a7aSWarner Losh 	char buff[100];
3118e3e3a7aSWarner Losh 	sprintf(buff,LUA_NUMBER_FMT,fltvalue(o));
3128e3e3a7aSWarner Losh 	printf("%s",buff);
3138e3e3a7aSWarner Losh 	if (buff[strspn(buff,"-0123456789")]=='\0') printf(".0");
3148e3e3a7aSWarner Losh 	break;
3158e3e3a7aSWarner Losh 	}
3160495ed39SKyle Evans   case LUA_VNUMINT:
3178e3e3a7aSWarner Losh 	printf(LUA_INTEGER_FMT,ivalue(o));
3188e3e3a7aSWarner Losh 	break;
3190495ed39SKyle Evans   case LUA_VSHRSTR:
3200495ed39SKyle Evans   case LUA_VLNGSTR:
3218e3e3a7aSWarner Losh 	PrintString(tsvalue(o));
3228e3e3a7aSWarner Losh 	break;
3238e3e3a7aSWarner Losh   default:				/* cannot happen */
3240495ed39SKyle Evans 	printf("?%d",ttypetag(o));
3258e3e3a7aSWarner Losh 	break;
3268e3e3a7aSWarner Losh  }
3278e3e3a7aSWarner Losh }
3288e3e3a7aSWarner Losh 
3290495ed39SKyle Evans #define COMMENT		"\t; "
3300495ed39SKyle Evans #define EXTRAARG	GETARG_Ax(code[pc+1])
3310495ed39SKyle Evans #define EXTRAARGC	(EXTRAARG*(MAXARG_C+1))
3320495ed39SKyle Evans #define ISK		(isk ? "k" : "")
3338e3e3a7aSWarner Losh 
PrintCode(const Proto * f)3348e3e3a7aSWarner Losh static void PrintCode(const Proto* f)
3358e3e3a7aSWarner Losh {
3368e3e3a7aSWarner Losh  const Instruction* code=f->code;
3378e3e3a7aSWarner Losh  int pc,n=f->sizecode;
3388e3e3a7aSWarner Losh  for (pc=0; pc<n; pc++)
3398e3e3a7aSWarner Losh  {
3408e3e3a7aSWarner Losh   Instruction i=code[pc];
3418e3e3a7aSWarner Losh   OpCode o=GET_OPCODE(i);
3428e3e3a7aSWarner Losh   int a=GETARG_A(i);
3438e3e3a7aSWarner Losh   int b=GETARG_B(i);
3448e3e3a7aSWarner Losh   int c=GETARG_C(i);
3458e3e3a7aSWarner Losh   int ax=GETARG_Ax(i);
3468e3e3a7aSWarner Losh   int bx=GETARG_Bx(i);
3470495ed39SKyle Evans   int sb=GETARG_sB(i);
3480495ed39SKyle Evans   int sc=GETARG_sC(i);
3498e3e3a7aSWarner Losh   int sbx=GETARG_sBx(i);
3500495ed39SKyle Evans   int isk=GETARG_k(i);
3510495ed39SKyle Evans   int line=luaG_getfuncline(f,pc);
3528e3e3a7aSWarner Losh   printf("\t%d\t",pc+1);
3538e3e3a7aSWarner Losh   if (line>0) printf("[%d]\t",line); else printf("[-]\t");
3540495ed39SKyle Evans   printf("%-9s\t",opnames[o]);
3558e3e3a7aSWarner Losh   switch (o)
3568e3e3a7aSWarner Losh   {
3570495ed39SKyle Evans    case OP_MOVE:
3580495ed39SKyle Evans 	printf("%d %d",a,b);
3590495ed39SKyle Evans 	break;
3600495ed39SKyle Evans    case OP_LOADI:
3610495ed39SKyle Evans 	printf("%d %d",a,sbx);
3620495ed39SKyle Evans 	break;
3630495ed39SKyle Evans    case OP_LOADF:
3640495ed39SKyle Evans 	printf("%d %d",a,sbx);
3650495ed39SKyle Evans 	break;
3668e3e3a7aSWarner Losh    case OP_LOADK:
3670495ed39SKyle Evans 	printf("%d %d",a,bx);
3680495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,bx);
3690495ed39SKyle Evans 	break;
3700495ed39SKyle Evans    case OP_LOADKX:
3710495ed39SKyle Evans 	printf("%d",a);
3720495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,EXTRAARG);
3730495ed39SKyle Evans 	break;
3740495ed39SKyle Evans    case OP_LOADFALSE:
3750495ed39SKyle Evans 	printf("%d",a);
3760495ed39SKyle Evans 	break;
3770495ed39SKyle Evans    case OP_LFALSESKIP:
3780495ed39SKyle Evans 	printf("%d",a);
3790495ed39SKyle Evans 	break;
3800495ed39SKyle Evans    case OP_LOADTRUE:
3810495ed39SKyle Evans 	printf("%d",a);
3820495ed39SKyle Evans 	break;
3830495ed39SKyle Evans    case OP_LOADNIL:
3840495ed39SKyle Evans 	printf("%d %d",a,b);
3850495ed39SKyle Evans 	printf(COMMENT "%d out",b+1);
3868e3e3a7aSWarner Losh 	break;
3878e3e3a7aSWarner Losh    case OP_GETUPVAL:
3880495ed39SKyle Evans 	printf("%d %d",a,b);
3890495ed39SKyle Evans 	printf(COMMENT "%s",UPVALNAME(b));
3900495ed39SKyle Evans 	break;
3918e3e3a7aSWarner Losh    case OP_SETUPVAL:
3920495ed39SKyle Evans 	printf("%d %d",a,b);
3930495ed39SKyle Evans 	printf(COMMENT "%s",UPVALNAME(b));
3948e3e3a7aSWarner Losh 	break;
3958e3e3a7aSWarner Losh    case OP_GETTABUP:
3960495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
3970495ed39SKyle Evans 	printf(COMMENT "%s",UPVALNAME(b));
3980495ed39SKyle Evans 	printf(" "); PrintConstant(f,c);
3998e3e3a7aSWarner Losh 	break;
4008e3e3a7aSWarner Losh    case OP_GETTABLE:
4010495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4020495ed39SKyle Evans 	break;
4030495ed39SKyle Evans    case OP_GETI:
4040495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4050495ed39SKyle Evans 	break;
4060495ed39SKyle Evans    case OP_GETFIELD:
4070495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4080495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4090495ed39SKyle Evans 	break;
4100495ed39SKyle Evans    case OP_SETTABUP:
4110495ed39SKyle Evans 	printf("%d %d %d%s",a,b,c,ISK);
4120495ed39SKyle Evans 	printf(COMMENT "%s",UPVALNAME(a));
4130495ed39SKyle Evans 	printf(" "); PrintConstant(f,b);
4140495ed39SKyle Evans 	if (isk) { printf(" "); PrintConstant(f,c); }
4158e3e3a7aSWarner Losh 	break;
4168e3e3a7aSWarner Losh    case OP_SETTABLE:
4170495ed39SKyle Evans 	printf("%d %d %d%s",a,b,c,ISK);
4180495ed39SKyle Evans 	if (isk) { printf(COMMENT); PrintConstant(f,c); }
4190495ed39SKyle Evans 	break;
4200495ed39SKyle Evans    case OP_SETI:
4210495ed39SKyle Evans 	printf("%d %d %d%s",a,b,c,ISK);
4220495ed39SKyle Evans 	if (isk) { printf(COMMENT); PrintConstant(f,c); }
4230495ed39SKyle Evans 	break;
4240495ed39SKyle Evans    case OP_SETFIELD:
4250495ed39SKyle Evans 	printf("%d %d %d%s",a,b,c,ISK);
4260495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,b);
4270495ed39SKyle Evans 	if (isk) { printf(" "); PrintConstant(f,c); }
4280495ed39SKyle Evans 	break;
4290495ed39SKyle Evans    case OP_NEWTABLE:
4300495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4310495ed39SKyle Evans 	printf(COMMENT "%d",c+EXTRAARGC);
4320495ed39SKyle Evans 	break;
4330495ed39SKyle Evans    case OP_SELF:
4340495ed39SKyle Evans 	printf("%d %d %d%s",a,b,c,ISK);
4350495ed39SKyle Evans 	if (isk) { printf(COMMENT); PrintConstant(f,c); }
4360495ed39SKyle Evans 	break;
4370495ed39SKyle Evans    case OP_ADDI:
4380495ed39SKyle Evans 	printf("%d %d %d",a,b,sc);
4390495ed39SKyle Evans 	break;
4400495ed39SKyle Evans    case OP_ADDK:
4410495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4420495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4430495ed39SKyle Evans 	break;
4440495ed39SKyle Evans    case OP_SUBK:
4450495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4460495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4470495ed39SKyle Evans 	break;
4480495ed39SKyle Evans    case OP_MULK:
4490495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4500495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4510495ed39SKyle Evans 	break;
4520495ed39SKyle Evans    case OP_MODK:
4530495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4540495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4550495ed39SKyle Evans 	break;
4560495ed39SKyle Evans    case OP_POWK:
4570495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4580495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4590495ed39SKyle Evans 	break;
4600495ed39SKyle Evans    case OP_DIVK:
4610495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4620495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4630495ed39SKyle Evans 	break;
4640495ed39SKyle Evans    case OP_IDIVK:
4650495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4660495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4670495ed39SKyle Evans 	break;
4680495ed39SKyle Evans    case OP_BANDK:
4690495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4700495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4710495ed39SKyle Evans 	break;
4720495ed39SKyle Evans    case OP_BORK:
4730495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4740495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4750495ed39SKyle Evans 	break;
4760495ed39SKyle Evans    case OP_BXORK:
4770495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4780495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,c);
4790495ed39SKyle Evans 	break;
4800495ed39SKyle Evans    case OP_SHRI:
4810495ed39SKyle Evans 	printf("%d %d %d",a,b,sc);
4820495ed39SKyle Evans 	break;
4830495ed39SKyle Evans    case OP_SHLI:
4840495ed39SKyle Evans 	printf("%d %d %d",a,b,sc);
4850495ed39SKyle Evans 	break;
4868e3e3a7aSWarner Losh    case OP_ADD:
4870495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4880495ed39SKyle Evans 	break;
4898e3e3a7aSWarner Losh    case OP_SUB:
4900495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4910495ed39SKyle Evans 	break;
4928e3e3a7aSWarner Losh    case OP_MUL:
4930495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4940495ed39SKyle Evans 	break;
495e112e9d2SKyle Evans    case OP_MOD:
4960495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
4970495ed39SKyle Evans 	break;
4988e3e3a7aSWarner Losh    case OP_POW:
4990495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5000495ed39SKyle Evans 	break;
5018e3e3a7aSWarner Losh    case OP_DIV:
5020495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5030495ed39SKyle Evans 	break;
5048e3e3a7aSWarner Losh    case OP_IDIV:
5050495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5060495ed39SKyle Evans 	break;
5078e3e3a7aSWarner Losh    case OP_BAND:
5080495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5090495ed39SKyle Evans 	break;
5108e3e3a7aSWarner Losh    case OP_BOR:
5110495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5120495ed39SKyle Evans 	break;
5138e3e3a7aSWarner Losh    case OP_BXOR:
5140495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5150495ed39SKyle Evans 	break;
5168e3e3a7aSWarner Losh    case OP_SHL:
5170495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5180495ed39SKyle Evans 	break;
5198e3e3a7aSWarner Losh    case OP_SHR:
5200495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5210495ed39SKyle Evans 	break;
5220495ed39SKyle Evans    case OP_MMBIN:
5230495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5240495ed39SKyle Evans 	printf(COMMENT "%s",eventname(c));
5250495ed39SKyle Evans 	break;
5260495ed39SKyle Evans    case OP_MMBINI:
5270495ed39SKyle Evans 	printf("%d %d %d %d",a,sb,c,isk);
5280495ed39SKyle Evans 	printf(COMMENT "%s",eventname(c));
5290495ed39SKyle Evans 	if (isk) printf(" flip");
5300495ed39SKyle Evans 	break;
5310495ed39SKyle Evans    case OP_MMBINK:
5320495ed39SKyle Evans 	printf("%d %d %d %d",a,b,c,isk);
5330495ed39SKyle Evans 	printf(COMMENT "%s ",eventname(c)); PrintConstant(f,b);
5340495ed39SKyle Evans 	if (isk) printf(" flip");
5350495ed39SKyle Evans 	break;
5360495ed39SKyle Evans    case OP_UNM:
5370495ed39SKyle Evans 	printf("%d %d",a,b);
5380495ed39SKyle Evans 	break;
5390495ed39SKyle Evans    case OP_BNOT:
5400495ed39SKyle Evans 	printf("%d %d",a,b);
5410495ed39SKyle Evans 	break;
5420495ed39SKyle Evans    case OP_NOT:
5430495ed39SKyle Evans 	printf("%d %d",a,b);
5440495ed39SKyle Evans 	break;
5450495ed39SKyle Evans    case OP_LEN:
5460495ed39SKyle Evans 	printf("%d %d",a,b);
5470495ed39SKyle Evans 	break;
5480495ed39SKyle Evans    case OP_CONCAT:
5490495ed39SKyle Evans 	printf("%d %d",a,b);
5500495ed39SKyle Evans 	break;
5510495ed39SKyle Evans    case OP_CLOSE:
5520495ed39SKyle Evans 	printf("%d",a);
5530495ed39SKyle Evans 	break;
5540495ed39SKyle Evans    case OP_TBC:
5550495ed39SKyle Evans 	printf("%d",a);
5568e3e3a7aSWarner Losh 	break;
5578e3e3a7aSWarner Losh    case OP_JMP:
5580495ed39SKyle Evans 	printf("%d",GETARG_sJ(i));
5590495ed39SKyle Evans 	printf(COMMENT "to %d",GETARG_sJ(i)+pc+2);
5608e3e3a7aSWarner Losh 	break;
5610495ed39SKyle Evans    case OP_EQ:
5620495ed39SKyle Evans 	printf("%d %d %d",a,b,isk);
5630495ed39SKyle Evans 	break;
5640495ed39SKyle Evans    case OP_LT:
5650495ed39SKyle Evans 	printf("%d %d %d",a,b,isk);
5660495ed39SKyle Evans 	break;
5670495ed39SKyle Evans    case OP_LE:
5680495ed39SKyle Evans 	printf("%d %d %d",a,b,isk);
5690495ed39SKyle Evans 	break;
5700495ed39SKyle Evans    case OP_EQK:
5710495ed39SKyle Evans 	printf("%d %d %d",a,b,isk);
5720495ed39SKyle Evans 	printf(COMMENT); PrintConstant(f,b);
5730495ed39SKyle Evans 	break;
5740495ed39SKyle Evans    case OP_EQI:
5750495ed39SKyle Evans 	printf("%d %d %d",a,sb,isk);
5760495ed39SKyle Evans 	break;
5770495ed39SKyle Evans    case OP_LTI:
5780495ed39SKyle Evans 	printf("%d %d %d",a,sb,isk);
5790495ed39SKyle Evans 	break;
5800495ed39SKyle Evans    case OP_LEI:
5810495ed39SKyle Evans 	printf("%d %d %d",a,sb,isk);
5820495ed39SKyle Evans 	break;
5830495ed39SKyle Evans    case OP_GTI:
5840495ed39SKyle Evans 	printf("%d %d %d",a,sb,isk);
5850495ed39SKyle Evans 	break;
5860495ed39SKyle Evans    case OP_GEI:
5870495ed39SKyle Evans 	printf("%d %d %d",a,sb,isk);
5880495ed39SKyle Evans 	break;
5890495ed39SKyle Evans    case OP_TEST:
5900495ed39SKyle Evans 	printf("%d %d",a,isk);
5910495ed39SKyle Evans 	break;
5920495ed39SKyle Evans    case OP_TESTSET:
5930495ed39SKyle Evans 	printf("%d %d %d",a,b,isk);
5940495ed39SKyle Evans 	break;
5950495ed39SKyle Evans    case OP_CALL:
5960495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
5970495ed39SKyle Evans 	printf(COMMENT);
5980495ed39SKyle Evans 	if (b==0) printf("all in "); else printf("%d in ",b-1);
5990495ed39SKyle Evans 	if (c==0) printf("all out"); else printf("%d out",c-1);
6000495ed39SKyle Evans 	break;
6010495ed39SKyle Evans    case OP_TAILCALL:
6028c784bb8SWarner Losh 	printf("%d %d %d%s",a,b,c,ISK);
6030495ed39SKyle Evans 	printf(COMMENT "%d in",b-1);
6040495ed39SKyle Evans 	break;
6050495ed39SKyle Evans    case OP_RETURN:
6068c784bb8SWarner Losh 	printf("%d %d %d%s",a,b,c,ISK);
6070495ed39SKyle Evans 	printf(COMMENT);
6080495ed39SKyle Evans 	if (b==0) printf("all out"); else printf("%d out",b-1);
6090495ed39SKyle Evans 	break;
6100495ed39SKyle Evans    case OP_RETURN0:
6110495ed39SKyle Evans 	break;
6120495ed39SKyle Evans    case OP_RETURN1:
6130495ed39SKyle Evans 	printf("%d",a);
6140495ed39SKyle Evans 	break;
6150495ed39SKyle Evans    case OP_FORLOOP:
6160495ed39SKyle Evans 	printf("%d %d",a,bx);
6170495ed39SKyle Evans 	printf(COMMENT "to %d",pc-bx+2);
6180495ed39SKyle Evans 	break;
6190495ed39SKyle Evans    case OP_FORPREP:
6200495ed39SKyle Evans 	printf("%d %d",a,bx);
6218c784bb8SWarner Losh 	printf(COMMENT "exit to %d",pc+bx+3);
6220495ed39SKyle Evans 	break;
6230495ed39SKyle Evans    case OP_TFORPREP:
6240495ed39SKyle Evans 	printf("%d %d",a,bx);
6250495ed39SKyle Evans 	printf(COMMENT "to %d",pc+bx+2);
6260495ed39SKyle Evans 	break;
6270495ed39SKyle Evans    case OP_TFORCALL:
6280495ed39SKyle Evans 	printf("%d %d",a,c);
6290495ed39SKyle Evans 	break;
6300495ed39SKyle Evans    case OP_TFORLOOP:
6310495ed39SKyle Evans 	printf("%d %d",a,bx);
6320495ed39SKyle Evans 	printf(COMMENT "to %d",pc-bx+2);
6338e3e3a7aSWarner Losh 	break;
6348e3e3a7aSWarner Losh    case OP_SETLIST:
6350495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
6360495ed39SKyle Evans 	if (isk) printf(COMMENT "%d",c+EXTRAARGC);
6370495ed39SKyle Evans 	break;
6380495ed39SKyle Evans    case OP_CLOSURE:
6390495ed39SKyle Evans 	printf("%d %d",a,bx);
6400495ed39SKyle Evans 	printf(COMMENT "%p",VOID(f->p[bx]));
6410495ed39SKyle Evans 	break;
6420495ed39SKyle Evans    case OP_VARARG:
6430495ed39SKyle Evans 	printf("%d %d",a,c);
6440495ed39SKyle Evans 	printf(COMMENT);
6450495ed39SKyle Evans 	if (c==0) printf("all out"); else printf("%d out",c-1);
6460495ed39SKyle Evans 	break;
6470495ed39SKyle Evans    case OP_VARARGPREP:
6480495ed39SKyle Evans 	printf("%d",a);
6498e3e3a7aSWarner Losh 	break;
6508e3e3a7aSWarner Losh    case OP_EXTRAARG:
6510495ed39SKyle Evans 	printf("%d",ax);
6528e3e3a7aSWarner Losh 	break;
6530495ed39SKyle Evans #if 0
6548e3e3a7aSWarner Losh    default:
6550495ed39SKyle Evans 	printf("%d %d %d",a,b,c);
6560495ed39SKyle Evans 	printf(COMMENT "not handled");
6578e3e3a7aSWarner Losh 	break;
6580495ed39SKyle Evans #endif
6598e3e3a7aSWarner Losh   }
6608e3e3a7aSWarner Losh   printf("\n");
6618e3e3a7aSWarner Losh  }
6628e3e3a7aSWarner Losh }
6638e3e3a7aSWarner Losh 
6640495ed39SKyle Evans 
6658e3e3a7aSWarner Losh #define SS(x)	((x==1)?"":"s")
6668e3e3a7aSWarner Losh #define S(x)	(int)(x),SS(x)
6678e3e3a7aSWarner Losh 
PrintHeader(const Proto * f)6688e3e3a7aSWarner Losh static void PrintHeader(const Proto* f)
6698e3e3a7aSWarner Losh {
6708e3e3a7aSWarner Losh  const char* s=f->source ? getstr(f->source) : "=?";
6718e3e3a7aSWarner Losh  if (*s=='@' || *s=='=')
6728e3e3a7aSWarner Losh   s++;
6738e3e3a7aSWarner Losh  else if (*s==LUA_SIGNATURE[0])
6748e3e3a7aSWarner Losh   s="(bstring)";
6758e3e3a7aSWarner Losh  else
6768e3e3a7aSWarner Losh   s="(string)";
6778e3e3a7aSWarner Losh  printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
6788e3e3a7aSWarner Losh 	(f->linedefined==0)?"main":"function",s,
6798e3e3a7aSWarner Losh 	f->linedefined,f->lastlinedefined,
6808e3e3a7aSWarner Losh 	S(f->sizecode),VOID(f));
6818e3e3a7aSWarner Losh  printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
6828e3e3a7aSWarner Losh 	(int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
6838e3e3a7aSWarner Losh 	S(f->maxstacksize),S(f->sizeupvalues));
6848e3e3a7aSWarner Losh  printf("%d local%s, %d constant%s, %d function%s\n",
6858e3e3a7aSWarner Losh 	S(f->sizelocvars),S(f->sizek),S(f->sizep));
6868e3e3a7aSWarner Losh }
6878e3e3a7aSWarner Losh 
PrintDebug(const Proto * f)6888e3e3a7aSWarner Losh static void PrintDebug(const Proto* f)
6898e3e3a7aSWarner Losh {
6908e3e3a7aSWarner Losh  int i,n;
6918e3e3a7aSWarner Losh  n=f->sizek;
6928e3e3a7aSWarner Losh  printf("constants (%d) for %p:\n",n,VOID(f));
6938e3e3a7aSWarner Losh  for (i=0; i<n; i++)
6948e3e3a7aSWarner Losh  {
6950495ed39SKyle Evans   printf("\t%d\t",i);
6960495ed39SKyle Evans   PrintType(f,i);
6978e3e3a7aSWarner Losh   PrintConstant(f,i);
6988e3e3a7aSWarner Losh   printf("\n");
6998e3e3a7aSWarner Losh  }
7008e3e3a7aSWarner Losh  n=f->sizelocvars;
7018e3e3a7aSWarner Losh  printf("locals (%d) for %p:\n",n,VOID(f));
7028e3e3a7aSWarner Losh  for (i=0; i<n; i++)
7038e3e3a7aSWarner Losh  {
7048e3e3a7aSWarner Losh   printf("\t%d\t%s\t%d\t%d\n",
7058e3e3a7aSWarner Losh   i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
7068e3e3a7aSWarner Losh  }
7078e3e3a7aSWarner Losh  n=f->sizeupvalues;
7088e3e3a7aSWarner Losh  printf("upvalues (%d) for %p:\n",n,VOID(f));
7098e3e3a7aSWarner Losh  for (i=0; i<n; i++)
7108e3e3a7aSWarner Losh  {
7118e3e3a7aSWarner Losh   printf("\t%d\t%s\t%d\t%d\n",
7128e3e3a7aSWarner Losh   i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
7138e3e3a7aSWarner Losh  }
7148e3e3a7aSWarner Losh }
7158e3e3a7aSWarner Losh 
PrintFunction(const Proto * f,int full)7168e3e3a7aSWarner Losh static void PrintFunction(const Proto* f, int full)
7178e3e3a7aSWarner Losh {
7188e3e3a7aSWarner Losh  int i,n=f->sizep;
7198e3e3a7aSWarner Losh  PrintHeader(f);
7208e3e3a7aSWarner Losh  PrintCode(f);
7218e3e3a7aSWarner Losh  if (full) PrintDebug(f);
7228e3e3a7aSWarner Losh  for (i=0; i<n; i++) PrintFunction(f->p[i],full);
7238e3e3a7aSWarner Losh }
724