| 1 |
/* |
| 2 |
|
| 3 |
texluac.w |
| 4 |
|
| 5 |
Copyright (C) 1994-2007 Lua.org, PUC-Rio. All rights reserved. |
| 6 |
Copyright 2006-2013 Taco Hoekwater <taco@@luatex.org> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining |
| 9 |
a copy of this software and associated documentation files (the |
| 10 |
"Software"), to deal in the Software without restriction, including |
| 11 |
without limitation the rights to use, copy, modify, merge, publish, |
| 12 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 13 |
permit persons to whom the Software is furnished to do so, subject to |
| 14 |
the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be |
| 17 |
included in all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 22 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 23 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 24 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 25 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 |
|
| 27 |
This file is part of LuaTeX. |
| 28 |
|
| 29 |
*/ |
| 30 |
|
| 31 |
#include <ctype.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
|
| 37 |
#define luac_c |
| 38 |
#define LUA_CORE |
| 39 |
|
| 40 |
#include "lua.h" |
| 41 |
#include "lauxlib.h" |
| 42 |
|
| 43 |
#include "ldebug.h" |
| 44 |
#include "ldo.h" |
| 45 |
#include "lfunc.h" |
| 46 |
#include "lmem.h" |
| 47 |
#include "lobject.h" |
| 48 |
#include "lopcodes.h" |
| 49 |
#include "lstring.h" |
| 50 |
#include "lundump.h" |
| 51 |
|
| 52 |
#include "lua/luatex-api.h" |
| 53 |
|
| 54 |
static void PrintFunction(const Proto* f, int full); |
| 55 |
|
| 56 |
#define luaU_print PrintFunction |
| 57 |
|
| 58 |
/*tex A fix for non-gcc compilation: */ |
| 59 |
|
| 60 |
#if !defined(__GNUC__) || (__GNUC__ < 2) |
| 61 |
# define __attribute__(x) |
| 62 |
#endif |
| 63 |
|
| 64 |
/*tex default program name */ |
| 65 |
|
| 66 |
#define PROGNAME "texluac" |
| 67 |
|
| 68 |
/*tex default output file */ |
| 69 |
|
| 70 |
#define OUTPUT PROGNAME ".out" |
| 71 |
|
| 72 |
/*tex list bytecodes? */ |
| 73 |
|
| 74 |
static int listing = 0; |
| 75 |
|
| 76 |
/*tex dump bytecodes? */ |
| 77 |
|
| 78 |
static int dumping = 1; |
| 79 |
|
| 80 |
/*tex strip debug information? */ |
| 81 |
|
| 82 |
static int stripping = 0; |
| 83 |
|
| 84 |
/*tex default output file name */ |
| 85 |
|
| 86 |
static char Output[] = { OUTPUT }; |
| 87 |
|
| 88 |
/*tex actual output file name */ |
| 89 |
|
| 90 |
static const char *output = Output; |
| 91 |
|
| 92 |
/*tex actual program name */ |
| 93 |
|
| 94 |
static const char *progname = PROGNAME; |
| 95 |
|
| 96 |
__attribute__ ((noreturn)) |
| 97 |
static void fatal(const char *message) { |
| 98 |
fprintf(stderr,"%s: %s\n",progname,message); |
| 99 |
exit(EXIT_FAILURE); |
| 100 |
} |
| 101 |
|
| 102 |
__attribute__ ((noreturn)) |
| 103 |
static void cannot(const char *what) { |
| 104 |
fprintf(stderr,"%s: cannot %s %s: %s\n",progname,what,output,strerror(errno)); |
| 105 |
exit(EXIT_FAILURE); |
| 106 |
} |
| 107 |
|
| 108 |
__attribute__ ((noreturn)) |
| 109 |
static void usage(const char* message) |
| 110 |
{ |
| 111 |
if (*message=='-') |
| 112 |
fprintf(stderr,"%s: unrecognized option " LUA_QS "\n",progname,message); |
| 113 |
else |
| 114 |
fprintf(stderr,"%s: %s\n",progname,message); |
| 115 |
fprintf(stderr, |
| 116 |
"usage: %s [options] [filenames]\n" |
| 117 |
"Available options are:\n" |
| 118 |
" -l list (use -l -l for full listing)\n" |
| 119 |
" -o name output to file " LUA_QL("name") " (default is \"%s\")\n" |
| 120 |
" -p parse only\n" |
| 121 |
" -s strip debug information\n" |
| 122 |
" -v show version information\n" |
| 123 |
" -- stop handling options\n" |
| 124 |
" - stop handling options and process stdin\n" |
| 125 |
,progname,Output); |
| 126 |
exit(EXIT_FAILURE); |
| 127 |
} |
| 128 |
|
| 129 |
#define IS(s) (strcmp(argv[i],s)==0) |
| 130 |
|
| 131 |
static int doargs(int argc, char* argv[]) |
| 132 |
{ |
| 133 |
int i; |
| 134 |
int version=0; |
| 135 |
if (argv[0]!=NULL && *argv[0]!=0) |
| 136 |
progname=argv[0]; |
| 137 |
for (i=1; i<argc; i++) { |
| 138 |
if (*argv[i]!='-') { |
| 139 |
/* end of options; keep it */ |
| 140 |
break; |
| 141 |
} else if (IS("--")) { |
| 142 |
/* end of options; skip it */ |
| 143 |
++i; |
| 144 |
if (version) ++version; |
| 145 |
break; |
| 146 |
} else if (IS("-")) { |
| 147 |
/* end of options; use stdin */ |
| 148 |
break; |
| 149 |
} else if (IS("-l")) { |
| 150 |
/* list */ |
| 151 |
++listing; |
| 152 |
} else if (IS("-o")) { |
| 153 |
/* output file */ |
| 154 |
output=argv[++i]; |
| 155 |
if (output==NULL || *output==0 || (*output=='-' && output[1]!=0)) |
| 156 |
usage(LUA_QL("-o") " needs argument"); |
| 157 |
if (IS("-")) |
| 158 |
output=NULL; |
| 159 |
} else if (IS("-p")) { |
| 160 |
/* parse only */ |
| 161 |
dumping=0; |
| 162 |
} else if (IS("-s")) { |
| 163 |
/* strip debug information */ |
| 164 |
stripping=1; |
| 165 |
} else if (IS("-v")) { |
| 166 |
/* show version */ |
| 167 |
++version; |
| 168 |
} else { |
| 169 |
/* unknown option */ |
| 170 |
usage(argv[i]); |
| 171 |
} |
| 172 |
} |
| 173 |
if (i==argc && (listing || !dumping)) { |
| 174 |
dumping=0; |
| 175 |
argv[--i]=Output; |
| 176 |
} |
| 177 |
if (version) { |
| 178 |
printf("%s\n",LUA_COPYRIGHT); |
| 179 |
if (version==argc-1) |
| 180 |
exit(EXIT_SUCCESS); |
| 181 |
} |
| 182 |
return i; |
| 183 |
} |
| 184 |
|
| 185 |
#define FUNCTION "(function()end)();" |
| 186 |
|
| 187 |
static const char* reader(lua_State *L, void *ud, size_t *size) |
| 188 |
{ |
| 189 |
UNUSED(L); |
| 190 |
if ((*(int*)ud)--) { |
| 191 |
*size=sizeof(FUNCTION)-1; |
| 192 |
return FUNCTION; |
| 193 |
} else { |
| 194 |
*size=0; |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
#define toproto(L,i) getproto(L->top+(i)) |
| 200 |
|
| 201 |
static const Proto* combine(lua_State* L, int n) |
| 202 |
{ |
| 203 |
if (n==1) { |
| 204 |
return toproto(L,-1); |
| 205 |
} else { |
| 206 |
Proto* f; |
| 207 |
int i=n; |
| 208 |
if (lua_load(L,reader,&i,"=(" PROGNAME ")",NULL)!=LUA_OK) |
| 209 |
fatal(lua_tostring(L,-1)); |
| 210 |
f=toproto(L,-1); |
| 211 |
for (i=0; i<n; i++) { |
| 212 |
f->p[i]=toproto(L,i-n-1); |
| 213 |
if (f->p[i]->sizeupvalues>0) |
| 214 |
f->p[i]->upvalues[0].instack=0; |
| 215 |
} |
| 216 |
f->sizelineinfo=0; |
| 217 |
return f; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static int writer(lua_State* L, const void* p, size_t size, void* u) |
| 222 |
{ |
| 223 |
UNUSED(L); |
| 224 |
return (fwrite(p,size,1,(FILE*)u)!=1) && (size!=0); |
| 225 |
} |
| 226 |
|
| 227 |
static int pmain(lua_State* L) |
| 228 |
{ |
| 229 |
int argc=(int)lua_tointeger(L,1); |
| 230 |
char** argv=(char**)lua_touserdata(L,2); |
| 231 |
const Proto* f; |
| 232 |
int i; |
| 233 |
if (!lua_checkstack(L,argc)) |
| 234 |
fatal("too many input files"); |
| 235 |
/*tex |
| 236 |
Open standard libraries: we need to to this to keep the symbol |
| 237 |
|luaL_openlibs|. |
| 238 |
*/ |
| 239 |
luaL_checkversion(L); |
| 240 |
/*tex stop collector during initialization */ |
| 241 |
lua_gc(L, LUA_GCSTOP, 0); |
| 242 |
/*tex open libraries */ |
| 243 |
luaL_openlibs(L); |
| 244 |
lua_gc(L, LUA_GCRESTART, 0); |
| 245 |
for (i=0; i<argc; i++) { |
| 246 |
const char* filename=IS("-") ? NULL : argv[i]; |
| 247 |
if (luaL_loadfile(L,filename)!=LUA_OK) |
| 248 |
fatal(lua_tostring(L,-1)); |
| 249 |
} |
| 250 |
f=combine(L,argc); |
| 251 |
if (listing) |
| 252 |
luaU_print(f,listing>1); |
| 253 |
if (dumping) { |
| 254 |
FILE* D= (output==NULL) ? stdout : fopen(output,"wb"); |
| 255 |
if (D==NULL) |
| 256 |
cannot("open"); |
| 257 |
lua_lock(L); |
| 258 |
luaU_dump(L,f,writer,D,stripping); |
| 259 |
lua_unlock(L); |
| 260 |
if (ferror(D)) |
| 261 |
cannot("write"); |
| 262 |
if (fclose(D)) |
| 263 |
cannot("close"); |
| 264 |
} |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
int luac_main(int ac, char *av[]) |
| 269 |
{ |
| 270 |
lua_State *L; |
| 271 |
int i = doargs(ac, av); |
| 272 |
ac -= i; |
| 273 |
av += i; |
| 274 |
if (ac <= 0) |
| 275 |
usage("no input files given"); |
| 276 |
L=luaL_newstate(); |
| 277 |
if (L == NULL) |
| 278 |
fatal("not enough memory for state"); |
| 279 |
lua_pushcfunction(L,&pmain); |
| 280 |
lua_pushinteger(L,ac); |
| 281 |
lua_pushlightuserdata(L,av); |
| 282 |
if (lua_pcall(L,2,0,0)!=LUA_OK) |
| 283 |
fatal(lua_tostring(L,-1)); |
| 284 |
lua_close(L); |
| 285 |
return EXIT_SUCCESS; |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
See Copyright Notice in |lua.h|. |
| 290 |
*/ |
| 291 |
|
| 292 |
#define VOID(p) ((const void*)(p)) |
| 293 |
|
| 294 |
#if (defined(LuajitTeX)) || (LUA_VERSION_NUM == 502) |
| 295 |
#define TSVALUE(o) rawtsvalue(o) |
| 296 |
#endif |
| 297 |
#if (LUA_VERSION_NUM == 503) |
| 298 |
#define TSVALUE(o) tsvalue(o) |
| 299 |
#endif |
| 300 |
|
| 301 |
/* if ts is a const pointer, using Lua's getstr(ts) is not correct */ |
| 302 |
#define getconststr(ts) \ |
| 303 |
check_exp(sizeof((ts)->extra), cast(const char *, (ts)) + sizeof(UTString)) |
| 304 |
|
| 305 |
|
| 306 |
static void PrintString(const TString* ts) |
| 307 |
{ |
| 308 |
const char* s=getconststr(ts); |
| 309 |
size_t i,n; |
| 310 |
#if (defined(LuajitTeX)) || (LUA_VERSION_NUM == 502) |
| 311 |
n=ts->tsv.len; |
| 312 |
#endif |
| 313 |
#if (LUA_VERSION_NUM == 503) |
| 314 |
n=(ts->tt == LUA_TSHRSTR ? ts->shrlen : ts->u.lnglen); |
| 315 |
#endif |
| 316 |
printf("%c",'"'); |
| 317 |
for (i=0; i<n; i++) { |
| 318 |
int c=(int)(unsigned char)s[i]; |
| 319 |
switch (c) { |
| 320 |
case '"': |
| 321 |
printf("\\\""); |
| 322 |
break; |
| 323 |
case '\\': |
| 324 |
printf("\\\\"); |
| 325 |
break; |
| 326 |
case '\a': |
| 327 |
printf("\\a"); |
| 328 |
break; |
| 329 |
case '\b': |
| 330 |
printf("\\b"); |
| 331 |
break; |
| 332 |
case '\f': |
| 333 |
printf("\\f"); |
| 334 |
break; |
| 335 |
case '\n': |
| 336 |
printf("\\n"); |
| 337 |
break; |
| 338 |
case '\r': |
| 339 |
printf("\\r"); |
| 340 |
break; |
| 341 |
case '\t': |
| 342 |
printf("\\t"); |
| 343 |
break; |
| 344 |
case '\v': |
| 345 |
printf("\\v"); |
| 346 |
break; |
| 347 |
default : |
| 348 |
if (isprint(c)) |
| 349 |
printf("%c",c); |
| 350 |
else |
| 351 |
printf("\\%03d",c); |
| 352 |
break; |
| 353 |
} |
| 354 |
} |
| 355 |
printf("%c",'"'); |
| 356 |
} |
| 357 |
|
| 358 |
static void PrintConstant(const Proto* f, int i) |
| 359 |
{ |
| 360 |
const TValue* o=&f->k[i]; |
| 361 |
switch (ttype(o)) { |
| 362 |
case LUA_TNIL: |
| 363 |
printf("nil"); |
| 364 |
break; |
| 365 |
case LUA_TBOOLEAN: |
| 366 |
printf(bvalue(o) ? "true" : "false"); |
| 367 |
break; |
| 368 |
case LUA_TNUMBER: |
| 369 |
printf(LUA_NUMBER_FMT,nvalue(o)); |
| 370 |
break; |
| 371 |
case LUA_TSTRING: |
| 372 |
PrintString(TSVALUE(o)); |
| 373 |
break; |
| 374 |
default: |
| 375 |
/*tex This cannot happen. */ |
| 376 |
printf("? type=%d",ttype(o)); |
| 377 |
break; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
#define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-") |
| 382 |
#define MYK(x) (-1-(x)) |
| 383 |
|
| 384 |
static void PrintCode(const Proto* f) |
| 385 |
{ |
| 386 |
const Instruction* code=f->code; |
| 387 |
int pc,n=f->sizecode; |
| 388 |
for (pc=0; pc<n; pc++) { |
| 389 |
Instruction i=code[pc]; |
| 390 |
OpCode o=GET_OPCODE(i); |
| 391 |
int a=GETARG_A(i); |
| 392 |
int b=GETARG_B(i); |
| 393 |
int c=GETARG_C(i); |
| 394 |
int ax=GETARG_Ax(i); |
| 395 |
int bx=GETARG_Bx(i); |
| 396 |
int sbx=GETARG_sBx(i); |
| 397 |
int line=getfuncline(f,pc); |
| 398 |
printf("\t%d\t",pc+1); |
| 399 |
if (line>0) |
| 400 |
printf("[%d]\t",line); |
| 401 |
else |
| 402 |
printf("[-]\t"); |
| 403 |
printf("%-9s\t",luaP_opnames[o]); |
| 404 |
switch (getOpMode(o)) { |
| 405 |
case iABC: |
| 406 |
printf("%d",a); |
| 407 |
if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b); |
| 408 |
if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c); |
| 409 |
break; |
| 410 |
case iABx: |
| 411 |
printf("%d",a); |
| 412 |
if (getBMode(o)==OpArgK) printf(" %d",MYK(bx)); |
| 413 |
if (getBMode(o)==OpArgU) printf(" %d",bx); |
| 414 |
break; |
| 415 |
case iAsBx: |
| 416 |
printf("%d %d",a,sbx); |
| 417 |
break; |
| 418 |
case iAx: |
| 419 |
printf("%d",MYK(ax)); |
| 420 |
break; |
| 421 |
} |
| 422 |
switch (o) { |
| 423 |
case OP_LOADK: |
| 424 |
printf("\t; "); PrintConstant(f,bx); |
| 425 |
break; |
| 426 |
case OP_GETUPVAL: |
| 427 |
case OP_SETUPVAL: |
| 428 |
printf("\t; %s",UPVALNAME(b)); |
| 429 |
break; |
| 430 |
case OP_GETTABUP: |
| 431 |
printf("\t; %s",UPVALNAME(b)); |
| 432 |
if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } |
| 433 |
break; |
| 434 |
case OP_SETTABUP: |
| 435 |
printf("\t; %s",UPVALNAME(a)); |
| 436 |
if (ISK(b)) { printf(" "); PrintConstant(f,INDEXK(b)); } |
| 437 |
if (ISK(c)) { printf(" "); PrintConstant(f,INDEXK(c)); } |
| 438 |
break; |
| 439 |
case OP_GETTABLE: |
| 440 |
case OP_SELF: |
| 441 |
if (ISK(c)) { printf("\t; "); PrintConstant(f,INDEXK(c)); } |
| 442 |
break; |
| 443 |
case OP_SETTABLE: |
| 444 |
case OP_ADD: |
| 445 |
case OP_SUB: |
| 446 |
case OP_MUL: |
| 447 |
case OP_DIV: |
| 448 |
case OP_POW: |
| 449 |
case OP_EQ: |
| 450 |
case OP_LT: |
| 451 |
case OP_LE: |
| 452 |
if (ISK(b) || ISK(c)) { |
| 453 |
printf("\t; "); |
| 454 |
if (ISK(b)) PrintConstant(f,INDEXK(b)); else printf("-"); |
| 455 |
printf(" "); |
| 456 |
if (ISK(c)) PrintConstant(f,INDEXK(c)); else printf("-"); |
| 457 |
} |
| 458 |
break; |
| 459 |
case OP_JMP: |
| 460 |
case OP_FORLOOP: |
| 461 |
case OP_FORPREP: |
| 462 |
case OP_TFORLOOP: |
| 463 |
printf("\t; to %d",sbx+pc+2); |
| 464 |
break; |
| 465 |
case OP_CLOSURE: |
| 466 |
printf("\t; %p",VOID(f->p[bx])); |
| 467 |
break; |
| 468 |
case OP_SETLIST: |
| 469 |
if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c); |
| 470 |
break; |
| 471 |
case OP_EXTRAARG: |
| 472 |
printf("\t; "); PrintConstant(f,ax); |
| 473 |
break; |
| 474 |
default: |
| 475 |
break; |
| 476 |
} |
| 477 |
printf("\n"); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
#define SS(x) ((x==1)?"":"s") |
| 482 |
#define S(x) (int)(x),SS(x) |
| 483 |
|
| 484 |
static void PrintHeader(const Proto* f) |
| 485 |
{ |
| 486 |
const char* s=f->source ? getstr(f->source) : "=?"; |
| 487 |
if (*s=='@' || *s=='=') |
| 488 |
s++; |
| 489 |
else if (*s==LUA_SIGNATURE[0]) |
| 490 |
s="(bstring)"; |
| 491 |
else |
| 492 |
s="(string)"; |
| 493 |
printf("\n%s <%s:%d,%d> (%d instruction%s at %p)\n", |
| 494 |
(f->linedefined==0)?"main":"function",s, |
| 495 |
f->linedefined,f->lastlinedefined, |
| 496 |
S(f->sizecode),VOID(f)); |
| 497 |
printf("%d%s param%s, %d slot%s, %d upvalue%s, ", |
| 498 |
(int)(f->numparams),f->is_vararg?"+":"",SS(f->numparams), |
| 499 |
S(f->maxstacksize),S(f->sizeupvalues)); |
| 500 |
printf("%d local%s, %d constant%s, %d function%s\n", |
| 501 |
S(f->sizelocvars),S(f->sizek),S(f->sizep)); |
| 502 |
} |
| 503 |
|
| 504 |
static void PrintDebug(const Proto* f) |
| 505 |
{ |
| 506 |
int i,n; |
| 507 |
n=f->sizek; |
| 508 |
printf("constants (%d) for %p:\n",n,VOID(f)); |
| 509 |
for (i=0; i<n; i++) { |
| 510 |
printf("\t%d\t",i+1); |
| 511 |
PrintConstant(f,i); |
| 512 |
printf("\n"); |
| 513 |
} |
| 514 |
n=f->sizelocvars; |
| 515 |
printf("locals (%d) for %p:\n",n,VOID(f)); |
| 516 |
for (i=0; i<n; i++) { |
| 517 |
printf("\t%d\t%s\t%d\t%d\n", |
| 518 |
i,getstr(f->locvars[i].varname),f->locvars[i].startpc+1,f->locvars[i].endpc+1); |
| 519 |
} |
| 520 |
n=f->sizeupvalues; |
| 521 |
printf("upvalues (%d) for %p:\n",n,VOID(f)); |
| 522 |
for (i=0; i<n; i++) { |
| 523 |
printf("\t%d\t%s\t%d\t%d\n", |
| 524 |
i,UPVALNAME(i),f->upvalues[i].instack,f->upvalues[i].idx); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
static void PrintFunction(const Proto* f, int full) |
| 529 |
{ |
| 530 |
int i,n=f->sizep; |
| 531 |
PrintHeader(f); |
| 532 |
PrintCode(f); |
| 533 |
if (full) |
| 534 |
PrintDebug(f); |
| 535 |
for (i=0; i<n; i++) |
| 536 |
PrintFunction(f->p[i],full); |
| 537 |
} |