aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/luajit-2.0/src/buildvm.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/luajit-2.0/src/buildvm.c')
-rw-r--r--libraries/luajit-2.0/src/buildvm.c513
1 files changed, 0 insertions, 513 deletions
diff --git a/libraries/luajit-2.0/src/buildvm.c b/libraries/luajit-2.0/src/buildvm.c
deleted file mode 100644
index 43e6d88..0000000
--- a/libraries/luajit-2.0/src/buildvm.c
+++ /dev/null
@@ -1,513 +0,0 @@
1/*
2** LuaJIT VM builder.
3** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4**
5** This is a tool to build the hand-tuned assembler code required for
6** LuaJIT's bytecode interpreter. It supports a variety of output formats
7** to feed different toolchains (see usage() below).
8**
9** This tool is not particularly optimized because it's only used while
10** _building_ LuaJIT. There's no point in distributing or installing it.
11** Only the object code generated by this tool is linked into LuaJIT.
12**
13** Caveat: some memory is not free'd, error handling is lazy.
14** It's a one-shot tool -- any effort fixing this would be wasted.
15*/
16
17#include "buildvm.h"
18#include "lj_obj.h"
19#include "lj_gc.h"
20#include "lj_bc.h"
21#include "lj_ir.h"
22#include "lj_ircall.h"
23#include "lj_frame.h"
24#include "lj_dispatch.h"
25#if LJ_HASFFI
26#include "lj_ctype.h"
27#include "lj_ccall.h"
28#endif
29#include "luajit.h"
30
31#if defined(_WIN32)
32#include <fcntl.h>
33#include <io.h>
34#endif
35
36/* ------------------------------------------------------------------------ */
37
38/* DynASM glue definitions. */
39#define Dst ctx
40#define Dst_DECL BuildCtx *ctx
41#define Dst_REF (ctx->D)
42#define DASM_CHECKS 1
43
44#include "../dynasm/dasm_proto.h"
45
46/* Glue macros for DynASM. */
47static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);
48
49#define DASM_EXTERN(ctx, addr, idx, type) \
50 collect_reloc(ctx, addr, idx, type)
51
52/* ------------------------------------------------------------------------ */
53
54/* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */
55#define DASM_ALIGNED_WRITES 1
56
57/* Embed architecture-specific DynASM encoder and backend. */
58#if LJ_TARGET_X86
59#include "../dynasm/dasm_x86.h"
60#include "buildvm_x86.h"
61#elif LJ_TARGET_X64
62#include "../dynasm/dasm_x86.h"
63#if LJ_ABI_WIN
64#include "buildvm_x64win.h"
65#else
66#include "buildvm_x64.h"
67#endif
68#elif LJ_TARGET_ARM
69#include "../dynasm/dasm_arm.h"
70#include "buildvm_arm.h"
71#elif LJ_TARGET_PPC
72#include "../dynasm/dasm_ppc.h"
73#include "buildvm_ppc.h"
74#elif LJ_TARGET_PPCSPE
75#include "../dynasm/dasm_ppc.h"
76#include "buildvm_ppcspe.h"
77#elif LJ_TARGET_MIPS
78#include "../dynasm/dasm_mips.h"
79#include "buildvm_mips.h"
80#else
81#error "No support for this architecture (yet)"
82#endif
83
84/* ------------------------------------------------------------------------ */
85
86void owrite(BuildCtx *ctx, const void *ptr, size_t sz)
87{
88 if (fwrite(ptr, 1, sz, ctx->fp) != sz) {
89 fprintf(stderr, "Error: cannot write to output file: %s\n",
90 strerror(errno));
91 exit(1);
92 }
93}
94
95/* ------------------------------------------------------------------------ */
96
97/* Emit code as raw bytes. Only used for DynASM debugging. */
98static void emit_raw(BuildCtx *ctx)
99{
100 owrite(ctx, ctx->code, ctx->codesz);
101}
102
103/* -- Build machine code -------------------------------------------------- */
104
105static const char *sym_decorate(BuildCtx *ctx,
106 const char *prefix, const char *suffix)
107{
108 char name[256];
109 char *p;
110#if LJ_64
111 const char *symprefix = ctx->mode == BUILD_machasm ? "_" : "";
112#else
113 const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : "";
114#endif
115 sprintf(name, "%s%s%s", symprefix, prefix, suffix);
116 p = strchr(name, '@');
117 if (p) {
118 if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj))
119 name[0] = '@';
120 else
121 *p = '\0';
122 }
123 p = (char *)malloc(strlen(name)+1); /* MSVC doesn't like strdup. */
124 strcpy(p, name);
125 return p;
126}
127
128#define NRELOCSYM (sizeof(extnames)/sizeof(extnames[0])-1)
129
130static int relocmap[NRELOCSYM];
131
132/* Collect external relocations. */
133static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type)
134{
135 if (ctx->nreloc >= BUILD_MAX_RELOC) {
136 fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n");
137 exit(1);
138 }
139 if (relocmap[idx] < 0) {
140 relocmap[idx] = ctx->nrelocsym;
141 ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]);
142 ctx->nrelocsym++;
143 }
144 ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code);
145 ctx->reloc[ctx->nreloc].sym = relocmap[idx];
146 ctx->reloc[ctx->nreloc].type = type;
147 ctx->nreloc++;
148 return 0; /* Encode symbol offset of 0. */
149}
150
151/* Naive insertion sort. Performance doesn't matter here. */
152static void sym_insert(BuildCtx *ctx, int32_t ofs,
153 const char *prefix, const char *suffix)
154{
155 ptrdiff_t i = ctx->nsym++;
156 while (i > 0) {
157 if (ctx->sym[i-1].ofs <= ofs)
158 break;
159 ctx->sym[i] = ctx->sym[i-1];
160 i--;
161 }
162 ctx->sym[i].ofs = ofs;
163 ctx->sym[i].name = sym_decorate(ctx, prefix, suffix);
164}
165
166/* Build the machine code. */
167static int build_code(BuildCtx *ctx)
168{
169 int status;
170 int i;
171
172 /* Initialize DynASM structures. */
173 ctx->nglob = GLOB__MAX;
174 ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));
175 memset(ctx->glob, 0, ctx->nglob*sizeof(void *));
176 ctx->nreloc = 0;
177
178 ctx->globnames = globnames;
179 ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *));
180 ctx->nrelocsym = 0;
181 for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1;
182
183 ctx->dasm_ident = DASM_IDENT;
184 ctx->dasm_arch = DASM_ARCH;
185
186 dasm_init(Dst, DASM_MAXSECTION);
187 dasm_setupglobal(Dst, ctx->glob, ctx->nglob);
188 dasm_setup(Dst, build_actionlist);
189
190 /* Call arch-specific backend to emit the code. */
191 ctx->npc = build_backend(ctx);
192
193 /* Finalize the code. */
194 (void)dasm_checkstep(Dst, -1);
195 if ((status = dasm_link(Dst, &ctx->codesz))) return status;
196 ctx->code = (uint8_t *)malloc(ctx->codesz);
197 if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;
198
199 /* Allocate symbol table and bytecode offsets. */
200 ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin");
201 ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym));
202 ctx->nsym = 0;
203 ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t));
204
205 /* Collect the opcodes (PC labels). */
206 for (i = 0; i < ctx->npc; i++) {
207 int32_t ofs = dasm_getpclabel(Dst, i);
208 if (ofs < 0) return 0x22000000|i;
209 ctx->bc_ofs[i] = ofs;
210 if ((LJ_HASJIT ||
211 !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP ||
212 i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) &&
213 (LJ_HASFFI || i != BC_KCDATA))
214 sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]);
215 }
216
217 /* Collect the globals (named labels). */
218 for (i = 0; i < ctx->nglob; i++) {
219 const char *gl = globnames[i];
220 int len = (int)strlen(gl);
221 if (!ctx->glob[i]) {
222 fprintf(stderr, "Error: undefined global %s\n", gl);
223 exit(2);
224 }
225 /* Skip the _Z symbols. */
226 if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z'))
227 sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code),
228 LABEL_PREFIX, globnames[i]);
229 }
230
231 /* Close the address range. */
232 sym_insert(ctx, (int32_t)ctx->codesz, "", "");
233 ctx->nsym--;
234
235 dasm_free(Dst);
236
237 return 0;
238}
239
240/* -- Generate VM enums --------------------------------------------------- */
241
242const char *const bc_names[] = {
243#define BCNAME(name, ma, mb, mc, mt) #name,
244BCDEF(BCNAME)
245#undef BCNAME
246 NULL
247};
248
249const char *const ir_names[] = {
250#define IRNAME(name, m, m1, m2) #name,
251IRDEF(IRNAME)
252#undef IRNAME
253 NULL
254};
255
256const char *const irt_names[] = {
257#define IRTNAME(name) #name,
258IRTDEF(IRTNAME)
259#undef IRTNAME
260 NULL
261};
262
263const char *const irfpm_names[] = {
264#define FPMNAME(name) #name,
265IRFPMDEF(FPMNAME)
266#undef FPMNAME
267 NULL
268};
269
270const char *const irfield_names[] = {
271#define FLNAME(name, ofs) #name,
272IRFLDEF(FLNAME)
273#undef FLNAME
274 NULL
275};
276
277const char *const ircall_names[] = {
278#define IRCALLNAME(cond, name, nargs, kind, type, flags) #name,
279IRCALLDEF(IRCALLNAME)
280#undef IRCALLNAME
281 NULL
282};
283
284static const char *const trace_errors[] = {
285#define TREDEF(name, msg) msg,
286#include "lj_traceerr.h"
287 NULL
288};
289
290static const char *lower(char *buf, const char *s)
291{
292 char *p = buf;
293 while (*s) {
294 *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s;
295 s++;
296 }
297 *p = '\0';
298 return buf;
299}
300
301/* Emit C source code for bytecode-related definitions. */
302static void emit_bcdef(BuildCtx *ctx)
303{
304 int i;
305 fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");
306 fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n");
307 for (i = 0; i < ctx->npc; i++) {
308 if (i != 0)
309 fprintf(ctx->fp, ",\n");
310 fprintf(ctx->fp, "%d", ctx->bc_ofs[i]);
311 }
312}
313
314/* Emit VM definitions as Lua code for debug modules. */
315static void emit_vmdef(BuildCtx *ctx)
316{
317 char buf[80];
318 int i;
319 fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n");
320 fprintf(ctx->fp, "module(...)\n\n");
321
322 fprintf(ctx->fp, "bcnames = \"");
323 for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]);
324 fprintf(ctx->fp, "\"\n\n");
325
326 fprintf(ctx->fp, "irnames = \"");
327 for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]);
328 fprintf(ctx->fp, "\"\n\n");
329
330 fprintf(ctx->fp, "irfpm = { [0]=");
331 for (i = 0; irfpm_names[i]; i++)
332 fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i]));
333 fprintf(ctx->fp, "}\n\n");
334
335 fprintf(ctx->fp, "irfield = { [0]=");
336 for (i = 0; irfield_names[i]; i++) {
337 char *p;
338 lower(buf, irfield_names[i]);
339 p = strchr(buf, '_');
340 if (p) *p = '.';
341 fprintf(ctx->fp, "\"%s\", ", buf);
342 }
343 fprintf(ctx->fp, "}\n\n");
344
345 fprintf(ctx->fp, "ircall = {\n[0]=");
346 for (i = 0; ircall_names[i]; i++)
347 fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]);
348 fprintf(ctx->fp, "}\n\n");
349
350 fprintf(ctx->fp, "traceerr = {\n[0]=");
351 for (i = 0; trace_errors[i]; i++)
352 fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]);
353 fprintf(ctx->fp, "}\n\n");
354}
355
356/* -- Argument parsing ---------------------------------------------------- */
357
358/* Build mode names. */
359static const char *const modenames[] = {
360#define BUILDNAME(name) #name,
361BUILDDEF(BUILDNAME)
362#undef BUILDNAME
363 NULL
364};
365
366/* Print usage information and exit. */
367static void usage(void)
368{
369 int i;
370 fprintf(stderr, LUAJIT_VERSION " VM builder.\n");
371 fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n");
372 fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n");
373 fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n");
374 fprintf(stderr, "Available modes:\n");
375 for (i = 0; i < BUILD__MAX; i++)
376 fprintf(stderr, " %s\n", modenames[i]);
377 exit(1);
378}
379
380/* Parse the output mode name. */
381static BuildMode parsemode(const char *mode)
382{
383 int i;
384 for (i = 0; modenames[i]; i++)
385 if (!strcmp(mode, modenames[i]))
386 return (BuildMode)i;
387 usage();
388 return (BuildMode)-1;
389}
390
391/* Parse arguments. */
392static void parseargs(BuildCtx *ctx, char **argv)
393{
394 const char *a;
395 int i;
396 ctx->mode = (BuildMode)-1;
397 ctx->outname = "-";
398 for (i = 1; (a = argv[i]) != NULL; i++) {
399 if (a[0] != '-')
400 break;
401 switch (a[1]) {
402 case '-':
403 if (a[2]) goto err;
404 i++;
405 goto ok;
406 case '\0':
407 goto ok;
408 case 'm':
409 i++;
410 if (a[2] || argv[i] == NULL) goto err;
411 ctx->mode = parsemode(argv[i]);
412 break;
413 case 'o':
414 i++;
415 if (a[2] || argv[i] == NULL) goto err;
416 ctx->outname = argv[i];
417 break;
418 default: err:
419 usage();
420 break;
421 }
422 }
423ok:
424 ctx->args = argv+i;
425 if (ctx->mode == (BuildMode)-1) goto err;
426}
427
428int main(int argc, char **argv)
429{
430 BuildCtx ctx_;
431 BuildCtx *ctx = &ctx_;
432 int status, binmode;
433
434 if (sizeof(void *) != 4*LJ_32+8*LJ_64) {
435 fprintf(stderr,"Error: pointer size mismatch in cross-build.\n");
436 fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=... TARGET=...\n\n");
437 return 1;
438 }
439
440 UNUSED(argc);
441 parseargs(ctx, argv);
442
443 if ((status = build_code(ctx))) {
444 fprintf(stderr,"Error: DASM error %08x\n", status);
445 return 1;
446 }
447
448 switch (ctx->mode) {
449 case BUILD_peobj:
450 case BUILD_raw:
451 binmode = 1;
452 break;
453 default:
454 binmode = 0;
455 break;
456 }
457
458 if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') {
459 ctx->fp = stdout;
460#if defined(_WIN32)
461 if (binmode)
462 _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */
463#endif
464 } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) {
465 fprintf(stderr, "Error: cannot open output file '%s': %s\n",
466 ctx->outname, strerror(errno));
467 exit(1);
468 }
469
470 switch (ctx->mode) {
471 case BUILD_elfasm:
472 case BUILD_coffasm:
473 case BUILD_machasm:
474 emit_asm(ctx);
475 emit_asm_debug(ctx);
476 break;
477 case BUILD_peobj:
478 emit_peobj(ctx);
479 break;
480 case BUILD_raw:
481 emit_raw(ctx);
482 break;
483 case BUILD_bcdef:
484 emit_bcdef(ctx);
485 emit_lib(ctx);
486 break;
487 case BUILD_vmdef:
488 emit_vmdef(ctx);
489 emit_lib(ctx);
490 break;
491 case BUILD_ffdef:
492 case BUILD_libdef:
493 case BUILD_recdef:
494 emit_lib(ctx);
495 break;
496 case BUILD_folddef:
497 emit_fold(ctx);
498 break;
499 default:
500 break;
501 }
502
503 fflush(ctx->fp);
504 if (ferror(ctx->fp)) {
505 fprintf(stderr, "Error: cannot write to output file: %s\n",
506 strerror(errno));
507 exit(1);
508 }
509 fclose(ctx->fp);
510
511 return 0;
512}
513