Debian Codegraph @

src/host/buildvm.c

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