1 /*
2 ** LuaJIT VM builder.
3 ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
4 */
5 
6 #ifndef _BUILDVM_H
7 #define _BUILDVM_H
8 
9 #include <sys/types.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <errno.h>
14 
15 #include "lj_def.h"
16 #include "lj_arch.h"
17 
18 /* Hardcoded limits. Increase as needed. */
19 #define BUILD_MAX_RELOC		200	/* Max. number of relocations. */
20 #define BUILD_MAX_FOLD		4096	/* Max. number of fold rules. */
21 
22 /* Prefix for scanned library definitions. */
23 #define LIBDEF_PREFIX		"LJLIB_"
24 
25 /* Prefix for scanned fold definitions. */
26 #define FOLDDEF_PREFIX		"LJFOLD"
27 
28 /* Prefixes for generated labels. */
29 #define LABEL_PREFIX		"lj_"
30 #define LABEL_PREFIX_BC		LABEL_PREFIX "BC_"
31 #define LABEL_PREFIX_FF		LABEL_PREFIX "ff_"
32 #define LABEL_PREFIX_CF		LABEL_PREFIX "cf_"
33 #define LABEL_PREFIX_FFH	LABEL_PREFIX "ffh_"
34 #define LABEL_PREFIX_LIBCF	LABEL_PREFIX "lib_cf_"
35 #define LABEL_PREFIX_LIBINIT	LABEL_PREFIX "lib_init_"
36 
37 /* Forward declaration. */
38 struct dasm_State;
39 
40 /* Build modes. */
41 #define BUILDDEF(_) \
42   _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \
43   _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \
44   _(folddef)
45 
46 typedef enum {
47 #define BUILDENUM(name)		BUILD_##name,
48 BUILDDEF(BUILDENUM)
49 #undef BUILDENUM
50   BUILD__MAX
51 } BuildMode;
52 
53 /* Code relocation. */
54 typedef struct BuildReloc {
55   int32_t ofs;
56   int sym;
57   int type;
58 } BuildReloc;
59 
60 typedef struct BuildSym {
61   const char *name;
62   int32_t ofs;
63 } BuildSym;
64 
65 /* Build context structure. */
66 typedef struct BuildCtx {
67   /* DynASM state pointer. Should be first member. */
68   struct dasm_State *D;
69   /* Parsed command line. */
70   BuildMode mode;
71   FILE *fp;
72   const char *outname;
73   char **args;
74   /* Code and symbols generated by DynASM. */
75   uint8_t *code;
76   size_t codesz;
77   int npc, nglob, nsym, nreloc, nrelocsym;
78   void **glob;
79   BuildSym *sym;
80   const char **relocsym;
81   int32_t *bc_ofs;
82   const char *beginsym;
83   /* Strings generated by DynASM. */
84   const char *const *globnames;
85   const char *dasm_ident;
86   const char *dasm_arch;
87   /* Relocations. */
88   BuildReloc reloc[BUILD_MAX_RELOC];
89 } BuildCtx;
90 
91 extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz);
92 extern void emit_asm(BuildCtx *ctx);
93 extern void emit_peobj(BuildCtx *ctx);
94 extern void emit_lib(BuildCtx *ctx);
95 extern void emit_fold(BuildCtx *ctx);
96 
97 extern const char *const bc_names[];
98 extern const char *const ir_names[];
99 extern const char *const irt_names[];
100 extern const char *const irfpm_names[];
101 extern const char *const irfield_names[];
102 extern const char *const ircall_names[];
103 
104 #endif
105