summaryrefslogtreecommitdiff
path: root/src/buildvm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildvm.c')
-rw-r--r--src/buildvm.c438
1 files changed, 438 insertions, 0 deletions
diff --git a/src/buildvm.c b/src/buildvm.c
new file mode 100644
index 00000000..b3738db4
--- /dev/null
+++ b/src/buildvm.c
@@ -0,0 +1,438 @@
1/*
2** LuaJIT VM builder.
3** Copyright (C) 2005-2009 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 "lua.h"
18#include "luajit.h"
19
20#ifdef LUA_USE_WIN
21#include <fcntl.h>
22#include <io.h>
23#endif
24
25#include "lj_obj.h"
26#include "lj_gc.h"
27#include "lj_bc.h"
28#include "lj_ir.h"
29#include "lj_frame.h"
30#include "lj_dispatch.h"
31#include "lj_target.h"
32
33#include "buildvm.h"
34
35/* ------------------------------------------------------------------------ */
36
37/* DynASM glue definitions. */
38#define Dst ctx
39#define Dst_DECL BuildCtx *ctx
40#define Dst_REF (ctx->D)
41
42#include "../dynasm/dasm_proto.h"
43
44/* Glue macros for DynASM. */
45#define DASM_M_GROW(ctx, t, p, sz, need) \
46 do { \
47 size_t _sz = (sz), _need = (need); \
48 if (_sz < _need) { \
49 if (_sz < 16) _sz = 16; \
50 while (_sz < _need) _sz += _sz; \
51 (p) = (t *)realloc((p), _sz); \
52 if ((p) == NULL) exit(1); \
53 (sz) = _sz; \
54 } \
55 } while(0)
56
57#define DASM_M_FREE(ctx, p, sz) free(p)
58
59static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type);
60
61#define DASM_EXTERN(ctx, addr, idx, type) \
62 collect_reloc(ctx, addr, idx, type)
63
64/* ------------------------------------------------------------------------ */
65
66/* Avoid trouble if cross-compiling for an x86 target. Speed doesn't matter. */
67#define DASM_ALIGNED_WRITES 1
68
69/* Embed architecture-specific DynASM encoder and backend. */
70#if LJ_TARGET_X86
71#include "../dynasm/dasm_x86.h"
72#include "buildvm_x86.h"
73#else
74#error "No support for this architecture (yet)"
75#endif
76
77/* ------------------------------------------------------------------------ */
78
79void owrite(BuildCtx *ctx, const void *ptr, size_t sz)
80{
81 if (fwrite(ptr, 1, sz, ctx->fp) != sz) {
82 fprintf(stderr, "Error: cannot write to output file: %s\n",
83 strerror(errno));
84 exit(1);
85 }
86}
87
88/* ------------------------------------------------------------------------ */
89
90/* Emit code as raw bytes. Only used for DynASM debugging. */
91static void emit_raw(BuildCtx *ctx)
92{
93 owrite(ctx, ctx->code, ctx->codesz);
94}
95
96/* -- Build machine code -------------------------------------------------- */
97
98/* Collect external relocations. */
99static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type)
100{
101 if (ctx->nreloc >= BUILD_MAX_RELOC) {
102 fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n");
103 exit(1);
104 }
105 ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code);
106 ctx->reloc[ctx->nreloc].sym = idx;
107 ctx->reloc[ctx->nreloc].type = type;
108 ctx->nreloc++;
109 return 0; /* Encode symbol offset of 0. */
110}
111
112/* Naive insertion sort. Performance doesn't matter here. */
113static void perm_insert(int *perm, int32_t *ofs, int i)
114{
115 perm[i] = i;
116 while (i > 0) {
117 int a = perm[i-1];
118 int b = perm[i];
119 if (ofs[a] <= ofs[b]) break;
120 perm[i] = a;
121 perm[i-1] = b;
122 i--;
123 }
124}
125
126/* Build the machine code. */
127static int build_code(BuildCtx *ctx)
128{
129 int status;
130 int i, j;
131
132 /* Initialize DynASM structures. */
133 ctx->nglob = GLOB__MAX;
134 ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *));
135 memset(ctx->glob, 0, ctx->nglob*sizeof(void *));
136 ctx->nreloc = 0;
137
138 ctx->extnames = extnames;
139 ctx->globnames = globnames;
140
141 ctx->dasm_ident = DASM_IDENT;
142 ctx->dasm_arch = DASM_ARCH;
143
144 dasm_init(Dst, DASM_MAXSECTION);
145 dasm_setupglobal(Dst, ctx->glob, ctx->nglob);
146 dasm_setup(Dst, build_actionlist);
147
148 /* Call arch-specific backend to emit the code. */
149 ctx->npc = build_backend(ctx);
150
151 /* Finalize the code. */
152 (void)dasm_checkstep(Dst, DASM_SECTION_CODE);
153 if ((status = dasm_link(Dst, &ctx->codesz))) return status;
154 ctx->code = (uint8_t *)malloc(ctx->codesz);
155 if ((status = dasm_encode(Dst, (void *)ctx->code))) return status;
156
157 /* Allocate the symbol offset and permutation tables. */
158 ctx->nsym = ctx->npc + ctx->nglob;
159 ctx->perm = (int *)malloc((ctx->nsym+1)*sizeof(int *));
160 ctx->sym_ofs = (int32_t *)malloc((ctx->nsym+1)*sizeof(int32_t));
161
162 /* Collect the opcodes (PC labels). */
163 for (i = 0; i < ctx->npc; i++) {
164 int32_t n = dasm_getpclabel(Dst, i);
165 if (n < 0) return 0x22000000|i;
166 ctx->sym_ofs[i] = n;
167 perm_insert(ctx->perm, ctx->sym_ofs, i);
168 }
169
170 /* Collect the globals (named labels). */
171 for (j = 0; j < ctx->nglob; j++, i++) {
172 const char *gl = globnames[j];
173 int len = (int)strlen(gl);
174 if (!ctx->glob[j]) {
175 fprintf(stderr, "Error: undefined global %s\n", gl);
176 exit(2);
177 }
178 if (len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z')
179 ctx->sym_ofs[i] = -1; /* Skip the _Z symbols. */
180 else
181 ctx->sym_ofs[i] = (int32_t)((uint8_t *)(ctx->glob[j]) - ctx->code);
182 perm_insert(ctx->perm, ctx->sym_ofs, i);
183 }
184
185 /* Close the address range. */
186 ctx->sym_ofs[i] = (int32_t)ctx->codesz;
187 perm_insert(ctx->perm, ctx->sym_ofs, i);
188
189 dasm_free(Dst);
190
191 return 0;
192}
193
194/* -- Generate VM enums --------------------------------------------------- */
195
196const char *const bc_names[] = {
197#define BCNAME(name, ma, mb, mc, mt) #name,
198BCDEF(BCNAME)
199#undef BCNAME
200 NULL
201};
202
203const char *const ir_names[] = {
204#define IRNAME(name, m, m1, m2) #name,
205IRDEF(IRNAME)
206#undef IRNAME
207 NULL
208};
209
210const char *const irfpm_names[] = {
211#define FPMNAME(name) #name,
212IRFPMDEF(FPMNAME)
213#undef FPMNAME
214 NULL
215};
216
217const char *const irfield_names[] = {
218#define FLNAME(name, type, field) #name,
219IRFLDEF(FLNAME)
220#undef FLNAME
221 NULL
222};
223
224static const char *const trace_errors[] = {
225#define TREDEF(name, msg) msg,
226#include "lj_traceerr.h"
227 NULL
228};
229
230static const char *lower(char *buf, const char *s)
231{
232 char *p = buf;
233 while (*s) {
234 *p++ = (*s >= 'A' && *s <= 'Z') ? *s+0x20 : *s;
235 s++;
236 }
237 *p = '\0';
238 return buf;
239}
240
241/* Emit VM definitions as Lua code for debug modules. */
242static void emit_vmdef(BuildCtx *ctx)
243{
244 char buf[80];
245 int i;
246 fprintf(ctx->fp, "-- This is a generated file. DO NOT EDIT!\n\n");
247 fprintf(ctx->fp, "module(...)\n\n");
248
249 fprintf(ctx->fp, "bcnames = \"");
250 for (i = 0; bc_names[i]; i++) fprintf(ctx->fp, "%-6s", bc_names[i]);
251 fprintf(ctx->fp, "\"\n\n");
252
253 fprintf(ctx->fp, "irnames = \"");
254 for (i = 0; ir_names[i]; i++) fprintf(ctx->fp, "%-6s", ir_names[i]);
255 fprintf(ctx->fp, "\"\n\n");
256
257 fprintf(ctx->fp, "irfpm = { [0]=");
258 for (i = 0; irfpm_names[i]; i++)
259 fprintf(ctx->fp, "\"%s\", ", lower(buf, irfpm_names[i]));
260 fprintf(ctx->fp, "}\n\n");
261
262 fprintf(ctx->fp, "irfield = { [0]=");
263 for (i = 0; irfield_names[i]; i++) {
264 char *p;
265 lower(buf, irfield_names[i]);
266 p = strchr(buf, '_');
267 if (p) *p = '.';
268 fprintf(ctx->fp, "\"%s\", ", buf);
269 }
270 fprintf(ctx->fp, "}\n\n");
271
272 fprintf(ctx->fp, "traceerr = {\n[0]=");
273 for (i = 0; trace_errors[i]; i++)
274 fprintf(ctx->fp, "\"%s\",\n", trace_errors[i]);
275 fprintf(ctx->fp, "}\n\n");
276}
277
278/* -- Argument parsing ---------------------------------------------------- */
279
280/* Build mode names. */
281static const char *const modenames[] = {
282#define BUILDNAME(name) #name,
283BUILDDEF(BUILDNAME)
284#undef BUILDNAME
285 NULL
286};
287
288/* Print usage information and exit. */
289static void usage(void)
290{
291 int i;
292 fprintf(stderr, LUAJIT_VERSION " VM builder.\n");
293 fprintf(stderr, LUAJIT_COPYRIGHT ", " LUAJIT_URL "\n");
294 fprintf(stderr, "Target architecture: " LJ_ARCH_NAME "\n\n");
295 fprintf(stderr, "Usage: buildvm -m mode [-o outfile] [infiles...]\n\n");
296 fprintf(stderr, "Available modes:\n");
297 for (i = 0; i < BUILD__MAX; i++)
298 fprintf(stderr, " %s\n", modenames[i]);
299 exit(1);
300}
301
302/* Parse the output mode name. */
303static BuildMode parsemode(const char *mode)
304{
305 int i;
306 for (i = 0; modenames[i]; i++)
307 if (!strcmp(mode, modenames[i]))
308 return (BuildMode)i;
309 usage();
310 return (BuildMode)-1;
311}
312
313/* Parse arguments. */
314static void parseargs(BuildCtx *ctx, char **argv)
315{
316 const char *a;
317 int i;
318 ctx->mode = (BuildMode)-1;
319 ctx->outname = "-";
320 for (i = 1; (a = argv[i]) != NULL; i++) {
321 if (a[0] != '-')
322 break;
323 switch (a[1]) {
324 case '-':
325 if (a[2]) goto err;
326 i++;
327 goto ok;
328 case '\0':
329 goto ok;
330 case 'm':
331 i++;
332 if (a[2] || argv[i] == NULL) goto err;
333 ctx->mode = parsemode(argv[i]);
334 break;
335 case 'o':
336 i++;
337 if (a[2] || argv[i] == NULL) goto err;
338 ctx->outname = argv[i];
339 break;
340 default: err:
341 usage();
342 break;
343 }
344 }
345ok:
346 ctx->args = argv+i;
347 if (ctx->mode == (BuildMode)-1) goto err;
348}
349
350int main(int argc, char **argv)
351{
352 BuildCtx ctx_;
353 BuildCtx *ctx = &ctx_;
354 int status, binmode;
355
356 UNUSED(argc);
357 parseargs(ctx, argv);
358
359 if ((status = build_code(ctx))) {
360 fprintf(stderr,"Error: DASM error %08x\n", status);
361 return 1;
362 }
363
364 switch (ctx->mode) {
365#if LJ_TARGET_X86ORX64
366 case BUILD_peobj:
367#endif
368 case BUILD_raw:
369 binmode = 1;
370 break;
371 default:
372 binmode = 0;
373 break;
374 }
375
376 if (ctx->outname[0] == '-' && ctx->outname[1] == '\0') {
377 ctx->fp = stdout;
378#ifdef LUA_USE_WIN
379 if (binmode)
380 _setmode(_fileno(stdout), _O_BINARY); /* Yuck. */
381#endif
382 } else if (!(ctx->fp = fopen(ctx->outname, binmode ? "wb" : "w"))) {
383 fprintf(stderr, "Error: cannot open output file '%s': %s\n",
384 ctx->outname, strerror(errno));
385 exit(1);
386 }
387
388 switch (ctx->mode) {
389 case BUILD_asm:
390#if defined(__ELF__)
391 ctx->mode = BUILD_elfasm;
392#elif defined(__MACH__)
393 ctx->mode = BUILD_machasm;
394#else
395 fprintf(stderr,"Error: auto-guessing the system assembler failed\n");
396 return 1;
397#endif
398 /* fallthrough */
399 case BUILD_elfasm:
400 case BUILD_coffasm:
401 case BUILD_machasm:
402 emit_asm(ctx);
403 emit_asm_debug(ctx);
404 break;
405#if LJ_TARGET_X86ORX64
406 case BUILD_peobj:
407 emit_peobj(ctx);
408 break;
409#endif
410 case BUILD_raw:
411 emit_raw(ctx);
412 break;
413 case BUILD_vmdef:
414 emit_vmdef(ctx);
415 /* fallthrough */
416 case BUILD_ffdef:
417 case BUILD_libdef:
418 case BUILD_recdef:
419 emit_lib(ctx);
420 break;
421 case BUILD_folddef:
422 emit_fold(ctx);
423 break;
424 default:
425 break;
426 }
427
428 fflush(ctx->fp);
429 if (ferror(ctx->fp)) {
430 fprintf(stderr, "Error: cannot write to output file: %s\n",
431 strerror(errno));
432 exit(1);
433 }
434 fclose(ctx->fp);
435
436 return 0;
437}
438