PPC: Add machine-specific part of FFI.
[luajit-2.0.git] / src / buildvm.c
blob122e1262b01a9b7f6eb83b6be2bcf733ab97280b
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.
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.
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_ccall.h"
27 #endif
28 #include "luajit.h"
30 #if defined(_WIN32)
31 #include <fcntl.h>
32 #include <io.h>
33 #endif
35 /* ------------------------------------------------------------------------ */
37 /* DynASM glue definitions. */
38 #define Dst ctx
39 #define Dst_DECL BuildCtx *ctx
40 #define Dst_REF (ctx->D)
41 #define DASM_CHECKS 1
43 #include "../dynasm/dasm_proto.h"
45 /* Glue macros for DynASM. */
46 static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);
48 #define DASM_EXTERN(ctx, addr, idx, type) \
49 collect_reloc(ctx, addr, idx, type)
51 /* ------------------------------------------------------------------------ */
53 /* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */
54 #define DASM_ALIGNED_WRITES 1
56 /* Embed architecture-specific DynASM encoder and backend. */
57 #if LJ_TARGET_X86
58 #include "../dynasm/dasm_x86.h"
59 #include "buildvm_x86.h"
60 #elif LJ_TARGET_X64
61 #include "../dynasm/dasm_x86.h"
62 #if LJ_ABI_WIN
63 #include "buildvm_x64win.h"
64 #else
65 #include "buildvm_x64.h"
66 #endif
67 #elif LJ_TARGET_ARM
68 #include "../dynasm/dasm_arm.h"
69 #include "buildvm_arm.h"
70 #elif LJ_TARGET_PPC
71 #include "../dynasm/dasm_ppc.h"
72 #include "buildvm_ppc.h"
73 #elif LJ_TARGET_PPCSPE
74 #include "../dynasm/dasm_ppc.h"
75 #include "buildvm_ppcspe.h"
76 #else
77 #error "No support for this architecture (yet)"
78 #endif
80 /* ------------------------------------------------------------------------ */
82 void owrite(BuildCtx *ctx, const void *ptr, size_t sz)
84 if (fwrite(ptr, 1, sz, ctx->fp) != sz) {
85 fprintf(stderr, "Error: cannot write to output file: %s\n",
86 strerror(errno));
87 exit(1);
91 /* ------------------------------------------------------------------------ */
93 /* Emit code as raw bytes. Only used for DynASM debugging. */
94 static void emit_raw(BuildCtx *ctx)
96 owrite(ctx, ctx->code, ctx->codesz);
99 /* -- Build machine code -------------------------------------------------- */
101 static const char *sym_decorate(BuildCtx *ctx,
102 const char *prefix, const char *suffix)
104 char name[256];
105 char *p;
106 #if LJ_64
107 const char *symprefix = ctx->mode == BUILD_machasm ? "_" : "";
108 #else
109 const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : "";
110 #endif
111 sprintf(name, "%s%s%s", symprefix, prefix, suffix);
112 p = strchr(name, '@');
113 if (p) {
114 if (!LJ_64 && (ctx->mode == BUILD_coffasm || ctx->mode == BUILD_peobj))
115 name[0] = '@';
116 else
117 *p = '\0';
119 p = (char *)malloc(strlen(name)+1); /* MSVC doesn't like strdup. */
120 strcpy(p, name);
121 return p;
124 #define NRELOCSYM (sizeof(extnames)/sizeof(extnames[0])-1)
126 static int relocmap[NRELOCSYM];
128 /* Collect external relocations. */
129 static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type)
131 if (ctx->nreloc >= BUILD_MAX_RELOC) {
132 fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n");
133 exit(1);
135 if (relocmap[idx] < 0) {
136 relocmap[idx] = ctx->nrelocsym;
137 ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]);
138 ctx->nrelocsym++;
140 ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code);
141 ctx->reloc[ctx->nreloc].sym = relocmap[idx];
142 ctx->reloc[ctx->nreloc].type = type;
143 ctx->nreloc++;
144 return 0; /* Encode symbol offset of 0. */
147 /* Naive insertion sort. Performance doesn't matter here. */
148 static void sym_insert(BuildCtx *ctx, int32_t ofs,
149 const char *prefix, const char *suffix)
151 ptrdiff_t i = ctx->nsym++;
152 while (i > 0) {
153 if (ctx->sym[i-1].ofs <= ofs)
154 break;
155 ctx->sym[i] = ctx->sym[i-1];
156 i--;
158 ctx->sym[i].ofs = ofs;
159 ctx->sym[i].name = sym_decorate(ctx, prefix, suffix);
162 /* Build the machine code. */
163 static int build_code(BuildCtx *ctx)
165 int status;
166 int i;
168 /* Initialize DynASM structures. */
169 ctx->nglob = GLOB__MAX;
170 ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));
171 memset(ctx->glob, 0, ctx->nglob*sizeof(void *));
172 ctx->nreloc = 0;
174 ctx->globnames = globnames;
175 ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *));
176 ctx->nrelocsym = 0;
177 for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1;
179 ctx->dasm_ident = DASM_IDENT;
180 ctx->dasm_arch = DASM_ARCH;
182 dasm_init(Dst, DASM_MAXSECTION);
183 dasm_setupglobal(Dst, ctx->glob, ctx->nglob);
184 dasm_setup(Dst, build_actionlist);
186 /* Call arch-specific backend to emit the code. */
187 ctx->npc = build_backend(ctx);
189 /* Finalize the code. */
190 (void)dasm_checkstep(Dst, -1);
191 if ((status = dasm_link(Dst, &ctx->codesz))) return status;
192 ctx->code = (uint8_t *)malloc(ctx->codesz);
193 if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;
195 /* Allocate symbol table and bytecode offsets. */
196 ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin");
197 ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym));
198 ctx->nsym = 0;
199 ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t));
201 /* Collect the opcodes (PC labels). */
202 for (i = 0; i < ctx->npc; i++) {
203 int32_t ofs = dasm_getpclabel(Dst, i);
204 if (ofs < 0) return 0x22000000|i;
205 ctx->bc_ofs[i] = ofs;
206 if ((LJ_HASJIT ||
207 !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP ||
208 i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) &&
209 (LJ_HASFFI || i != BC_KCDATA))
210 sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]);
213 /* Collect the globals (named labels). */
214 for (i = 0; i < ctx->nglob; i++) {
215 const char *gl = globnames[i];
216 int len = (int)strlen(gl);
217 if (!ctx->glob[i]) {
218 fprintf(stderr, "Error: undefined global %s\n", gl);
219 exit(2);
221 /* Skip the _Z symbols. */
222 if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z'))
223 sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code),
224 LABEL_PREFIX, globnames[i]);
227 /* Close the address range. */
228 sym_insert(ctx, (int32_t)ctx->codesz, "", "");
229 ctx->nsym--;
231 dasm_free(Dst);
233 return 0;
236 /* -- Generate VM enums --------------------------------------------------- */
238 const char *const bc_names[] = {
239 #define BCNAME(name, ma, mb, mc, mt) #name,
240 BCDEF(BCNAME)
241 #undef BCNAME
242 NULL
245 const char *const ir_names[] = {
246 #define IRNAME(name, m, m1, m2) #name,
247 IRDEF(IRNAME)
248 #undef IRNAME
249 NULL
252 const char *const irt_names[] = {
253 #define IRTNAME(name) #name,
254 IRTDEF(IRTNAME)
255 #undef IRTNAME
256 NULL
259 const char *const irfpm_names[] = {
260 #define FPMNAME(name) #name,
261 IRFPMDEF(FPMNAME)
262 #undef FPMNAME
263 NULL
266 const char *const irfield_names[] = {
267 #define FLNAME(name, ofs) #name,
268 IRFLDEF(FLNAME)
269 #undef FLNAME
270 NULL
273 const char *const ircall_names[] = {
274 #define IRCALLNAME(name, nargs, kind, type, flags) #name,
275 IRCALLDEF(IRCALLNAME)
276 #undef IRCALLNAME
277 NULL
280 static const char *const trace_errors[] = {
281 #define TREDEF(name, msg) msg,
282 #include "lj_traceerr.h"
283 NULL
286 static const char *lower(char *buf, const char *s)
288 char *p = buf;
289 while (*s) {
290 *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s;
291 s++;
293 *p = '\0';
294 return buf;
297 /* Emit C source code for bytecode-related definitions. */
298 static void emit_bcdef(BuildCtx *ctx)
300 int i;
301 fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");
302 fprintf(ctx->fp, "LJ_DATADEF const uint16_t lj_bc_ofs[] = {\n");
303 for (i = 0; i < ctx->npc; i++) {
304 if (i != 0)
305 fprintf(ctx->fp, ",\n");
306 fprintf(ctx->fp, "%d", ctx->bc_ofs[i]);
310 /* Emit VM definitions as Lua code for debug modules. */
311 static void emit_vmdef(BuildCtx *ctx)
313 char buf[80];
314 int i;
315 fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n");
316 fprintf(ctx->fp, "module(...)\n\n");
318 fprintf(ctx->fp, "bcnames = \"");
319 for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]);
320 fprintf(ctx->fp, "\"\n\n");
322 fprintf(ctx->fp, "irnames = \"");
323 for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]);
324 fprintf(ctx->fp, "\"\n\n");
326 fprintf(ctx->fp, "irfpm = { [0]=");
327 for (i = 0; irfpm_names[i]; i++)
328 fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i]));
329 fprintf(ctx->fp, "}\n\n");
331 fprintf(ctx->fp, "irfield = { [0]=");
332 for (i = 0; irfield_names[i]; i++) {
333 char *p;
334 lower(buf, irfield_names[i]);
335 p = strchr(buf, '_');
336 if (p) *p = '.';
337 fprintf(ctx->fp, "\"%s\", ", buf);
339 fprintf(ctx->fp, "}\n\n");
341 fprintf(ctx->fp, "ircall = {\n[0]=");
342 for (i = 0; ircall_names[i]; i++)
343 fprintf(ctx->fp, "\"%s\",\n", ircall_names[i]);
344 fprintf(ctx->fp, "}\n\n");
346 fprintf(ctx->fp, "traceerr = {\n[0]=");
347 for (i = 0; trace_errors[i]; i++)
348 fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]);
349 fprintf(ctx->fp, "}\n\n");
352 /* -- Argument parsing ---------------------------------------------------- */
354 /* Build mode names. */
355 static const char *const modenames[] = {
356 #define BUILDNAME(name) #name,
357 BUILDDEF(BUILDNAME)
358 #undef BUILDNAME
359 NULL
362 /* Print usage information and exit. */
363 static void usage(void)
365 int i;
366 fprintf(stderr, LUAJIT_VERSION " VM builder.\n");
367 fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n");
368 fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n");
369 fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n");
370 fprintf(stderr, "Available modes:\n");
371 for (i = 0; i < BUILD__MAX; i++)
372 fprintf(stderr, " %s\n", modenames[i]);
373 exit(1);
376 /* Parse the output mode name. */
377 static BuildMode parsemode(const char *mode)
379 int i;
380 for (i = 0; modenames[i]; i++)
381 if (!strcmp(mode, modenames[i]))
382 return (BuildMode)i;
383 usage();
384 return (BuildMode)-1;
387 /* Parse arguments. */
388 static void parseargs(BuildCtx *ctx, char **argv)
390 const char *a;
391 int i;
392 ctx->mode = (BuildMode)-1;
393 ctx->outname = "-";
394 for (i = 1; (a = argv[i]) != NULL; i++) {
395 if (a[0] != '-')
396 break;
397 switch (a[1]) {
398 case '-':
399 if (a[2]) goto err;
400 i++;
401 goto ok;
402 case '\0':
403 goto ok;
404 case 'm':
405 i++;
406 if (a[2] || argv[i] == NULL) goto err;
407 ctx->mode = parsemode(argv[i]);
408 break;
409 case 'o':
410 i++;
411 if (a[2] || argv[i] == NULL) goto err;
412 ctx->outname = argv[i];
413 break;
414 default: err:
415 usage();
416 break;
420 ctx->args = argv+i;
421 if (ctx->mode == (BuildMode)-1) goto err;
424 int main(int argc, char **argv)
426 BuildCtx ctx_;
427 BuildCtx *ctx = &ctx_;
428 int status, binmode;
430 if (sizeof(void *) != 4*LJ_32+8*LJ_64) {
431 fprintf(stderr,"Error: pointer size mismatch in cross-build.\n");
432 fprintf(stderr,"Try: make HOST_CC=\"gcc -m32\" CROSS=... TARGET=...\n\n");
433 return 1;
436 UNUSED(argc);
437 parseargs(ctx, argv);
439 if ((status = build_code(ctx))) {
440 fprintf(stderr,"Error: DASM error %08x\n", status);
441 return 1;
444 switch (ctx->mode) {
445 case BUILD_peobj:
446 case BUILD_raw:
447 binmode = 1;
448 break;
449 default:
450 binmode = 0;
451 break;
454 if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') {
455 ctx->fp = stdout;
456 #if defined(_WIN32)
457 if (binmode)
458 _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */
459 #endif
460 } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) {
461 fprintf(stderr, "Error: cannot open output file '%s': %s\n",
462 ctx->outname, strerror(errno));
463 exit(1);
466 switch (ctx->mode) {
467 case BUILD_elfasm:
468 case BUILD_coffasm:
469 case BUILD_machasm:
470 emit_asm(ctx);
471 emit_asm_debug(ctx);
472 break;
473 case BUILD_peobj:
474 emit_peobj(ctx);
475 break;
476 case BUILD_raw:
477 emit_raw(ctx);
478 break;
479 case BUILD_bcdef:
480 emit_bcdef(ctx);
481 emit_lib(ctx);
482 break;
483 case BUILD_vmdef:
484 emit_vmdef(ctx);
485 emit_lib(ctx);
486 break;
487 case BUILD_ffdef:
488 case BUILD_libdef:
489 case BUILD_recdef:
490 emit_lib(ctx);
491 break;
492 case BUILD_folddef:
493 emit_fold(ctx);
494 break;
495 default:
496 break;
499 fflush(ctx->fp);
500 if (ferror(ctx->fp)) {
501 fprintf(stderr, "Error: cannot write to output file: %s\n",
502 strerror(errno));
503 exit(1);
505 fclose(ctx->fp);
507 return 0;