The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
luac.cpp
Go to the documentation of this file.
1 /*
2 ** Lua compiler (saves bytecodes to files; also list bytecodes)
3 ** See Copyright Notice in lua.h
4 */
5 
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #define luac_c
12 #define LUA_CORE
13 
14 #include "lua.h"
15 #include "lauxlib.h"
16 
17 #include "lobject.h"
18 #include "lstate.h"
19 #include "lundump.h"
20 
21 static void PrintFunction(const Proto* f, int full);
22 #define luaU_print PrintFunction
23 
24 #define PROGNAME "luac" /* default program name */
25 #define OUTPUT PROGNAME ".out" /* default output file */
26 
27 static int listing=0; /* list bytecodes? */
28 static int dumping=1; /* dump bytecodes? */
29 static int stripping=0; /* strip debug information? */
30 static char Output[]={ OUTPUT }; /* default output file name */
31 static const char* output=Output; /* actual output file name */
32 static const char* progname=PROGNAME; /* actual program name */
33 
34 static void fatal(const char* message)
35 {
36  fprintf(stderr,"%s: %s\n",progname,message);
37  exit(EXIT_FAILURE);
38 }
39 
40 static void cannot(const char* what)
41 {
42  fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno));
43  exit(EXIT_FAILURE);
44 }
45 
46 static void usage(const char* message)
47 {
48  if (*message=='-')
49  fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message);
50  else
51  fprintf(stderr,"%s: %s\n",progname,message);
52  fprintf(stderr,
53  "usage: %s [options] [filenames]\n"
54  "Available options are:\n"
55  " -l list (use -l -l for full listing)\n"
56  " -o name output to file " LUA_QL("name") " (default is \"%s\")\n"
57  " -p parse only\n"
58  " -s strip debug information\n"
59  " -v show version information\n"
60  " -- stop handling options\n"
61  " - stop handling options and process stdin\n"
62  ,progname,Output);
63  exit(EXIT_FAILURE);
64 }
65 
66 #define IS(s) (strcmp(argv[i],s)==0)
67 
68 static int doargs(int argc, char* argv[])
69 {
70  int i;
71  int version=0;
72  if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
73  for (i=1; i<argc; i++)
74  {
75  if (*argv[i]!='-') /* end of options; keep it */
76  break;
77  else if (IS("--")) /* end of options; skip it */
78  {
79  ++i;
80  if (version) ++version;
81  break;
82  }
83  else if (IS("-")) /* end of options; use stdin */
84  break;
85  else if (IS("-l")) /* list */
86  ++listing;
87  else if (IS("-o")) /* output file */
88  {
89  output=argv[++i];
90  if (output==NULL || *output==0 || (*output=='-' && output[1]!=0))
91  usage(LUA_QL("-o") " needs argument");
92  if (IS("-")) output=NULL;
93  }
94  else if (IS("-p")) /* parse only */
95  dumping=0;
96  else if (IS("-s")) /* strip debug information */
97  stripping=1;
98  else if (IS("-v")) /* show version */
99  ++version;
100  else /* unknown option */
101  usage(argv[i]);
102  }
103  if (i==argc && (listing || !dumping))
104  {
105  dumping=0;
106  argv[--i]=Output;
107  }
108  if (version)
109  {
110  printf("%s\n",LUA_COPYRIGHT);
111  if (version==argc-1) exit(EXIT_SUCCESS);
112  }
113  return i;
114 }
115 
116 #define FUNCTION "(function()end)();"
117 
118 static const char* reader(lua_State *L, void *ud, size_t *size)
119 {
120  UNUSED(L);
121  if ((*(int*)ud)--)
122  {
123  *size=sizeof(FUNCTION)-1;
124  return FUNCTION;
125  }
126  else
127  {
128  *size=0;
129  return NULL;
130  }
131 }
132 
133 #define toproto(L,i) getproto(L->top+(i))
134 
135 static const Proto* combine(lua_State* L, int n)
136 {
137  if (n==1)
138  return toproto(L,-1);
139  else
140  {
141  Proto* f;
142  int i=n;
143  if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) fatal(lua_tostring(L,-1));
144  f=toproto(L,-1);
145  for (i=0; i<n; i++)
146  {
147  f->p[i]=toproto(L,i-n-1);
148  if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
149  }
150  f->sizelineinfo=0;
151  return f;
152  }
153 }
154 
155 static int writer(lua_State* L, const void* p, size_t size, void* u)
156 {
157  UNUSED(L);
158  return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0);
159 }
160 
161 static int pmain(lua_State* L)
162 {
163  int argc=(int)lua_tointeger(L,1);
164  char** argv=(char**)lua_touserdata(L,2);
165  const Proto* f;
166  int i;
167  if (!lua_checkstack(L,argc)) fatal("too many input files");
168  for (i=0; i<argc; i++)
169  {
170  const char* filename=IS("-") ? NULL : argv[i];
171  if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
172  }
173  f=combine(L,argc);
174  if (listing) luaU_print(f,listing>1);
175  if (dumping)
176  {
177  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
178  if (D==NULL) cannot("open");
179  lua_lock(L);
180  luaU_dump(L,f,writer,D,stripping);
181  lua_unlock(L);
182  if (ferror(D)) cannot("write");
183  if (fclose(D)) cannot("close");
184  }
185  return 0;
186 }
187 
188 int main(int argc, char* argv[])
189 {
190  lua_State* L;
191  int i=doargs(argc,argv);
192  argc-=i; argv+=i;
193  if (argc<=0) usage("no input files given");
194  L=luaL_newstate();
195  if (L==NULL) fatal("cannot create state: not enough memory");
197  lua_pushinteger(L,argc);
198  lua_pushlightuserdata(L,argv);
199  if (lua_pcall(L,2,0,0)!=LUA_OK) fatal(lua_tostring(L,-1));
200  lua_close(L);
201  return EXIT_SUCCESS;
202 }
203 
204 /*
205 ** print bytecodes
206 ** See Copyright Notice in lua.h
207 */
208 
209 #include <ctype.h>
210 #include <stdio.h>
211 
212 #define luac_c
213 #define LUA_CORE
214 
215 #include "ldebug.h"
216 #include "lobject.h"
217 #include "lopcodes.h"
218 
219 #define VOID(p) ((const void*)(p))
220 
221 static void PrintString(const TString* ts)
222 {
223  const char* s=getstr(ts);
224  size_t i,n=ts->tsv.len;
225  printf("%c",'"');
226  for (i=0; i<n; i++)
227  {
228  int c=(int)(unsigned char)s[i];
229  switch (c)
230  {
231  case '"': printf("\\\""); break;
232  case '\\': printf("\\\\"); break;
233  case '\a': printf("\\a"); break;
234  case '\b': printf("\\b"); break;
235  case '\f': printf("\\f"); break;
236  case '\n': printf("\\n"); break;
237  case '\r': printf("\\r"); break;
238  case '\t': printf("\\t"); break;
239  case '\v': printf("\\v"); break;
240  default: if (isprint(c))
241  printf("%c",c);
242  else
243  printf("\\%03d",c);
244  }
245  }
246  printf("%c",'"');
247 }
248 
249 static void PrintConstant(const Proto* f, int i)
250 {
251  const TValue* o=&f->k[i];
252  switch (ttypenv(o))
253  {
254  case LUA_TNIL:
255  printf("nil");
256  break;
257  case LUA_TBOOLEAN:
258  printf(bvalue(o) ? "true" : "false");
259  break;
260  case LUA_TNUMBER:
261  printf(LUA_NUMBER_FMT,nvalue(o));
262  break;
263  case LUA_TSTRING:
265  break;
266  default: /* cannot happen */
267  printf("? type=%d",ttype(o));
268  break;
269  }
270 }
271 
272 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
273 #define MYK(x) (-1-(x))
274 
275 static void PrintCode(const Proto* f)
276 {
277  const Instruction* code=f->code;
278  int pc,n=f->sizecode;
279  for (pc=0; pc<n; pc++)
280  {
281  Instruction i=code[pc];
282  OpCode o=GET_OPCODE(i);
283  int a=GETARG_A(i);
284  int b=GETARG_B(i);
285  int c=GETARG_C(i);
286  int ax=GETARG_Ax(i);
287  int bx=GETARG_Bx(i);
288  int sbx=GETARG_sBx(i);
289  int line=getfuncline(f,pc);
290  printf("\t%d\t",pc+1);
291  if (line>0) printf("[%d]\t",line); else printf("[-]\t");
292  printf("%-9s\t",luaP_opnames[o]);
293  switch (getOpMode(o))
294  {
295  case iABC:
296  printf("%d",a);
297  if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
298  if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
299  break;
300  case iABx:
301  printf("%d",a);
302  if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
303  if (getBMode(o)==OpArgU) printf(" %d",bx);
304  break;
305  case iAsBx:
306  printf("%d %d",a,sbx);
307  break;
308  case iAx:
309  printf("%d",MYK(ax));
310  break;
311  }
312  switch (o)
313  {
314  case OP_LOADK:
315  printf("\t; "); PrintConstant(f,bx);
316  break;
317  case OP_GETUPVAL:
318  case OP_SETUPVAL:
319  printf("\t; %s",UPVALNAME(b));
320  break;
321  case OP_GETTABUP:
322  printf("\t; %s",UPVALNAME(b));
323  if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
324  break;
325  case OP_SETTABUP:
326  printf("\t; %s",UPVALNAME(a));
327  if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); }
328  if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); }
329  break;
330  case OP_GETTABLE:
331  case OP_SELF:
332  if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); }
333  break;
334  case OP_SETTABLE:
335  case OP_ADD:
336  case OP_SUB:
337  case OP_MUL:
338  case OP_DIV:
339  case OP_POW:
340  case OP_EQ:
341  case OP_LT:
342  case OP_LE:
343  if (ISK(b) || ISK(c))
344  {
345  printf("\t; ");
346  if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-");
347  printf(" ");
348  if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-");
349  }
350  break;
351  case OP_JMP:
352  case OP_FORLOOP:
353  case OP_FORPREP:
354  case OP_TFORLOOP:
355  printf("\t; to %d",sbx+pc+2);
356  break;
357  case OP_CLOSURE:
358  printf("\t; %p",VOID(f->p[bx]));
359  break;
360  case OP_SETLIST:
361  if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
362  break;
363  case OP_EXTRAARG:
364  printf("\t; "); PrintConstant(f,ax);
365  break;
366  default:
367  break;
368  }
369  printf("\n");
370  }
371 }
372 
373 #define SS(x) ((x==1)?"":"s")
374 #define S(x) (int)(x),SS(x)
375 
376 static void PrintHeader(const Proto* f)
377 {
378  const char* s=f->source ? getstr(f->source) : "=?";
379  if (*s=='@' || *s=='=')
380  s++;
381  else if (*s==LUA_SIGNATURE[0])
382  s="(bstring)";
383  else
384  s="(string)";
385  printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n",
386  (f->linedefined==0)?"main":"function",s,
388  S(f->sizecode),VOID(f));
389  printf("%d%s param%s, %d slot%s, %d upvalue%s, ",
390  (int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams),
391  S(f->maxstacksize),S(f->sizeupvalues));
392  printf("%d local%s, %d constant%s, %d function%s\n",
393  S(f->sizelocvars),S(f->sizek),S(f->sizep));
394 }
395 
396 static void PrintDebug(const Proto* f)
397 {
398  int i,n;
399  n=f->sizek;
400  printf("constants (%d) for %p:\n",n,VOID(f));
401  for (i=0; i<n; i++)
402  {
403  printf("\t%d\t",i+1);
404  PrintConstant(f,i);
405  printf("\n");
406  }
407  n=f->sizelocvars;
408  printf("locals (%d) for %p:\n",n,VOID(f));
409  for (i=0; i<n; i++)
410  {
411  printf("\t%d\t%s\t%d\t%d\n",
412  i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1);
413  }
414  n=f->sizeupvalues;
415  printf("upvalues (%d) for %p:\n",n,VOID(f));
416  for (i=0; i<n; i++)
417  {
418  printf("\t%d\t%s\t%d\t%d\n",
419  i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx);
420  }
421 }
422 
423 static void PrintFunction(const Proto* f, int full)
424 {
425  int i,n=f->sizep;
426  PrintHeader(f);
427  PrintCode(f);
428  if (full) PrintDebug(f);
429  for (i=0; i<n; i++) PrintFunction(f->p[i],full);
430 }
static void usage(const char *message)
Definition: luac.cpp:46
LUA_API void lua_pushlightuserdata(lua_State *L, void *p)
Definition: lapi.cpp:579
TString * source
Definition: lobject.h:475
#define lua_pushcfunction(L, f)
Definition: lua.h:328
#define nvalue(o)
Definition: lobject.h:152
static void PrintCode(const Proto *f)
Definition: luac.cpp:275
static void PrintHeader(const Proto *f)
Definition: luac.cpp:376
static char Output[]
Definition: luac.cpp:30
LUALIB_API lua_State * luaL_newstate(void)
Definition: lauxlib.cpp:939
#define GETARG_sBx(i)
Definition: lopcodes.h:111
Definition: lobject.h:466
const GLfloat * c
Definition: glew.h:12741
#define MYK(x)
Definition: luac.cpp:273
static void fatal(const char *message)
Definition: luac.cpp:34
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip)
Definition: ldump.cpp:161
int sizep
Definition: lobject.h:480
static void PrintDebug(const Proto *f)
Definition: luac.cpp:396
#define getfuncline(f, pc)
Definition: ldebug.h:15
#define lua_tointeger(L, i)
Definition: lua.h:319
#define INDEXK(r)
Definition: lopcodes.h:139
#define rawtsvalue(o)
Definition: lobject.h:155
static int dumping
Definition: luac.cpp:28
Definition: lopcodes.h:31
const char * what() const
#define lua_unlock(L)
Definition: llimits.h:155
LocVar * locvars
Definition: lobject.h:472
#define GET_OPCODE(i)
Definition: lopcodes.h:88
#define VOID(p)
Definition: luac.cpp:219
Definition: lopcodes.h:31
#define LUA_SIGNATURE
Definition: lua.h:30
int sizelocvars
Definition: lobject.h:481
OpCode
Definition: lopcodes.h:164
Upvaldesc * upvalues
Definition: lobject.h:473
#define luaL_loadfile(L, f)
Definition: lauxlib.h:78
#define UPVALNAME(x)
Definition: luac.cpp:272
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
Definition: lopcodes.h:31
#define ttypenv(o)
Definition: lobject.h:126
#define lua_lock(L)
Definition: llimits.h:154
#define lua_pcall(L, n, r, f)
Definition: lua.h:258
#define LUA_TSTRING
Definition: lua.h:81
#define getstr(ts)
Definition: lobject.h:421
static int stripping
Definition: luac.cpp:29
#define GETARG_C(i)
Definition: lopcodes.h:102
#define LUA_QL(x)
Definition: luaconf.h:198
#define GETARG_A(i)
Definition: lopcodes.h:96
#define LUA_TNIL
Definition: lua.h:77
static int writer(lua_State *L, const void *p, size_t size, void *u)
Definition: luac.cpp:155
#define getBMode(m)
Definition: lopcodes.h:274
static void PrintFunction(const Proto *f, int full)
Definition: luac.cpp:423
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
LUA_API void lua_close(lua_State *L)
Definition: lstate.cpp:316
#define LUA_TNUMBER
Definition: lua.h:80
union CallInfo::@16 u
int main(int argc, char *argv[])
Definition: luac.cpp:188
int lastlinedefined
Definition: lobject.h:483
TString * varname
Definition: lobject.h:457
GLfloat GLfloat p
Definition: glew.h:12766
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
#define LUA_COPYRIGHT
Definition: lua.h:25
LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1]
Definition: lopcodes.cpp:16
Instruction * code
Definition: lobject.h:469
#define LUA_NUMBER_FMT
Definition: luaconf.h:413
struct Proto ** p
Definition: lobject.h:470
#define PROGNAME
Definition: luac.cpp:24
lu_byte maxstacksize
Definition: lobject.h:487
int sizelineinfo
Definition: lobject.h:479
#define UNUSED(x)
Definition: global.hpp:56
LUA_API void * lua_touserdata(lua_State *L, int idx)
Definition: lapi.cpp:421
static const char * output
Definition: luac.cpp:31
#define LUA_QS
Definition: luaconf.h:199
static const char * progname
Definition: luac.cpp:32
#define GETARG_B(i)
Definition: lopcodes.h:99
#define getOpMode(m)
Definition: lopcodes.h:273
lu_byte idx
Definition: lobject.h:448
lu_byte instack
Definition: lobject.h:447
LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data, const char *chunkname, const char *mode)
Definition: lapi.cpp:967
int sizek
Definition: lobject.h:477
size_t i
Definition: function.cpp:1057
#define lua_tostring(L, i)
Definition: lua.h:345
lu_byte is_vararg
Definition: lobject.h:486
struct TString::@7 tsv
int linedefined
Definition: lobject.h:482
#define SS(x)
Definition: luac.cpp:373
static const Proto * combine(lua_State *L, int n)
Definition: luac.cpp:135
static const char * reader(lua_State *L, void *ud, size_t *size)
Definition: luac.cpp:118
int sizecode
Definition: lobject.h:478
static void cannot(const char *what)
Definition: luac.cpp:40
static int doargs(int argc, char *argv[])
Definition: luac.cpp:68
GLsizeiptr size
Definition: glew.h:1649
#define getCMode(m)
Definition: lopcodes.h:275
#define ttype(o)
Definition: lobject.h:123
GLclampd n
Definition: glew.h:5903
#define LUA_OK
Definition: lua.h:44
static void PrintString(const TString *ts)
Definition: luac.cpp:221
#define bvalue(o)
Definition: lobject.h:164
#define FUNCTION
Definition: luac.cpp:116
#define ISK(x)
Definition: lopcodes.h:136
#define luaU_print
Definition: luac.cpp:22
#define toproto(L, i)
Definition: luac.cpp:133
#define IS(s)
Definition: luac.cpp:66
LUA_API int lua_checkstack(lua_State *L, int size)
Definition: lapi.cpp:86
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
int startpc
Definition: lobject.h:458
static int pmain(lua_State *L)
Definition: luac.cpp:161
int endpc
Definition: lobject.h:459
Definition: lopcodes.h:31
int sizeupvalues
Definition: lobject.h:476
static void PrintConstant(const Proto *f, int i)
Definition: luac.cpp:249
TValue * k
Definition: lobject.h:468
static int listing
Definition: luac.cpp:27
size_t len
Definition: lobject.h:415
#define GETARG_Bx(i)
Definition: lopcodes.h:105
GLdouble s
Definition: glew.h:1358
#define OUTPUT
Definition: luac.cpp:25
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
const std::string version
Definition: game_config.cpp:48
lu_int32 Instruction
Definition: llimits.h:132
#define LUA_TBOOLEAN
Definition: lua.h:78
lu_byte numparams
Definition: lobject.h:485
#define S(x)
Definition: luac.cpp:374
GLclampf f
Definition: glew.h:3024
#define GETARG_Ax(i)
Definition: lopcodes.h:108