1/*
2** LuaJIT VM builder.
3** Copyright (C) 2005-2021 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. */
38struct dasm_State;
39
40/* Build modes. */
41#define BUILDDEF(_) \
42 _(elfasm) _(coffasm) _(machasm) _(peobj) _(raw) \
43 _(bcdef) _(ffdef) _(libdef) _(recdef) _(vmdef) \
44 _(folddef)
45
46typedef enum {
47#define BUILDENUM(name) BUILD_##name,
48BUILDDEF(BUILDENUM)
49#undef BUILDENUM
50 BUILD__MAX
51} BuildMode;
52
53/* Code relocation. */
54typedef struct BuildReloc {
55 int32_t ofs;
56 int sym;
57 int type;
58} BuildReloc;
59
60typedef struct BuildSym {
61 const char *name;
62 int32_t ofs;
63} BuildSym;
64
65/* Build context structure. */
66typedef 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 *const *extnames;
86 const char *dasm_ident;
87 const char *dasm_arch;
88 /* Relocations. */
89 BuildReloc reloc[BUILD_MAX_RELOC];
90} BuildCtx;
91
92extern void owrite(BuildCtx *ctx, const void *ptr, size_t sz);
93extern void emit_asm(BuildCtx *ctx);
94extern void emit_peobj(BuildCtx *ctx);
95extern void emit_lib(BuildCtx *ctx);
96extern void emit_fold(BuildCtx *ctx);
97
98extern const char *const bc_names[];
99extern const char *const ir_names[];
100extern const char *const irt_names[];
101extern const char *const irfpm_names[];
102extern const char *const irfield_names[];
103extern const char *const ircall_names[];
104
105#endif
106