Fix iOS build.
[luajit-2.0.git] / src / buildvm_asm.c
blob5cfa7ae81d1929998893cb7ba2cc1c9114f221bd
1 /*
2 ** LuaJIT VM builder: Assembler source code emitter.
3 ** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #include "buildvm.h"
7 #include "lj_bc.h"
9 /* ------------------------------------------------------------------------ */
11 #if LJ_TARGET_X86ORX64
12 /* Emit bytes piecewise as assembler text. */
13 static void emit_asm_bytes(BuildCtx *ctx, uint8_t *p, int n)
15 int i;
16 for (i = 0; i < n; i++) {
17 if ((i & 15) == 0)
18 fprintf(ctx->fp, "\t.byte %d", p[i]);
19 else
20 fprintf(ctx->fp, ",%d", p[i]);
21 if ((i & 15) == 15) putc('\n', ctx->fp);
23 if ((n & 15) != 0) putc('\n', ctx->fp);
26 /* Emit relocation */
27 static void emit_asm_reloc(BuildCtx *ctx, int type, const char *sym)
29 switch (ctx->mode) {
30 case BUILD_elfasm:
31 if (type)
32 fprintf(ctx->fp, "\t.long %s-.-4\n", sym);
33 else
34 fprintf(ctx->fp, "\t.long %s\n", sym);
35 break;
36 case BUILD_coffasm:
37 fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", sym);
38 if (type)
39 fprintf(ctx->fp, "\t.long %s-.-4\n", sym);
40 else
41 fprintf(ctx->fp, "\t.long %s\n", sym);
42 break;
43 default: /* BUILD_machasm for relative relocations handled below. */
44 fprintf(ctx->fp, "\t.long %s\n", sym);
45 break;
49 static const char *const jccnames[] = {
50 "jo", "jno", "jb", "jnb", "jz", "jnz", "jbe", "ja",
51 "js", "jns", "jpe", "jpo", "jl", "jge", "jle", "jg"
54 /* Emit relocation for the incredibly stupid OSX assembler. */
55 static void emit_asm_reloc_mach(BuildCtx *ctx, uint8_t *cp, int n,
56 const char *sym)
58 const char *opname = NULL;
59 if (--n < 0) goto err;
60 if (cp[n] == 0xe8) {
61 opname = "call";
62 } else if (cp[n] == 0xe9) {
63 opname = "jmp";
64 } else if (cp[n] >= 0x80 && cp[n] <= 0x8f && n > 0 && cp[n-1] == 0x0f) {
65 opname = jccnames[cp[n]-0x80];
66 n--;
67 } else {
68 err:
69 fprintf(stderr, "Error: unsupported opcode for %s symbol relocation.\n",
70 sym);
71 exit(1);
73 emit_asm_bytes(ctx, cp, n);
74 fprintf(ctx->fp, "\t%s %s\n", opname, sym);
76 #else
77 /* Emit words piecewise as assembler text. */
78 static void emit_asm_words(BuildCtx *ctx, uint8_t *p, int n)
80 int i;
81 for (i = 0; i < n; i += 4) {
82 if ((i & 15) == 0)
83 fprintf(ctx->fp, "\t.long 0x%08x", *(uint32_t *)(p+i));
84 else
85 fprintf(ctx->fp, ",0x%08x", *(uint32_t *)(p+i));
86 if ((i & 15) == 12) putc('\n', ctx->fp);
88 if ((n & 15) != 0) putc('\n', ctx->fp);
91 /* Emit relocation as part of an instruction. */
92 static void emit_asm_wordreloc(BuildCtx *ctx, uint8_t *p, int n,
93 const char *sym)
95 uint32_t ins;
96 emit_asm_words(ctx, p, n-4);
97 ins = *(uint32_t *)(p+n-4);
98 #if LJ_TARGET_ARM
99 if ((ins & 0xff000000u) == 0xfa000000u) {
100 fprintf(ctx->fp, "\tblx %s\n", sym);
101 } else if ((ins & 0x0e000000u) == 0x0a000000u) {
102 fprintf(ctx->fp, "\t%s%.2s %s\n", (ins & 0x01000000u) ? "bl" : "b",
103 "eqnecsccmiplvsvchilsgeltgtle" + 2*(ins >> 28), sym);
104 } else {
105 fprintf(stderr,
106 "Error: unsupported opcode %08x for %s symbol relocation.\n",
107 ins, sym);
108 exit(1);
110 #elif LJ_TARGET_PPC
111 if ((ins >> 26) == 16) {
112 fprintf(ctx->fp, "\t%s %d, %d, %s\n",
113 (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym);
114 } else if ((ins >> 26) == 18) {
115 fprintf(ctx->fp, "\t%s %s\n", (ins & 1) ? "bl" : "b", sym);
116 } else {
117 fprintf(stderr,
118 "Error: unsupported opcode %08x for %s symbol relocation.\n",
119 ins, sym);
120 exit(1);
122 #else
123 #error "missing relocation support for this architecture"
124 #endif
126 #endif
128 #if LJ_TARGET_ARM
129 #define ELFASM_PX "%%"
130 #else
131 #define ELFASM_PX "@"
132 #endif
134 /* Emit an assembler label. */
135 static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc)
137 switch (ctx->mode) {
138 case BUILD_elfasm:
139 fprintf(ctx->fp,
140 "\n\t.globl %s\n"
141 "\t.hidden %s\n"
142 "\t.type %s, " ELFASM_PX "%s\n"
143 "\t.size %s, %d\n"
144 "%s:\n",
145 name, name, name, isfunc ? "function" : "object", name, size, name);
146 break;
147 case BUILD_coffasm:
148 fprintf(ctx->fp, "\n\t.globl %s\n", name);
149 if (isfunc)
150 fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name);
151 fprintf(ctx->fp, "%s:\n", name);
152 break;
153 case BUILD_machasm:
154 fprintf(ctx->fp,
155 "\n\t.private_extern %s\n"
156 "%s:\n", name, name);
157 break;
158 default:
159 break;
163 /* Emit alignment. */
164 static void emit_asm_align(BuildCtx *ctx, int bits)
166 switch (ctx->mode) {
167 case BUILD_elfasm:
168 case BUILD_coffasm:
169 fprintf(ctx->fp, "\t.p2align %d\n", bits);
170 break;
171 case BUILD_machasm:
172 fprintf(ctx->fp, "\t.align %d\n", bits);
173 break;
174 default:
175 break;
179 /* ------------------------------------------------------------------------ */
181 /* Emit assembler source code. */
182 void emit_asm(BuildCtx *ctx)
184 int i, rel;
186 fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch);
187 fprintf(ctx->fp, "\t.text\n");
188 emit_asm_align(ctx, 4);
190 emit_asm_label(ctx, ctx->beginsym, 0, 0);
191 if (ctx->mode != BUILD_machasm)
192 fprintf(ctx->fp, ".Lbegin:\n");
194 #if LJ_TARGET_ARM && defined(__GNUC__) && !defined(__symbian__) && \
195 !LJ_TARGET_OSX
196 /* This should really be moved into buildvm_arm.dasc. */
197 fprintf(ctx->fp,
198 ".fnstart\n"
199 ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
200 ".pad #28\n");
201 #endif
203 for (i = rel = 0; i < ctx->nsym; i++) {
204 int32_t ofs = ctx->sym[i].ofs;
205 int32_t next = ctx->sym[i+1].ofs;
206 emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1);
207 while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) {
208 BuildReloc *r = &ctx->reloc[rel];
209 int n = r->ofs - ofs;
210 #if LJ_TARGET_X86ORX64
211 if (ctx->mode == BUILD_machasm && r->type != 0) {
212 emit_asm_reloc_mach(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
213 } else {
214 emit_asm_bytes(ctx, ctx->code+ofs, n);
215 emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);
217 ofs += n+4;
218 #else
219 emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
220 ofs += n;
221 #endif
222 rel++;
224 #if LJ_TARGET_X86ORX64
225 emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);
226 #else
227 emit_asm_words(ctx, ctx->code+ofs, next-ofs);
228 #endif
231 #if LJ_TARGET_ARM && defined(__GNUC__) && !defined(__symbian__) && \
232 !LJ_TARGET_OSX
233 fprintf(ctx->fp,
234 ".globl lj_err_unwind_arm\n"
235 ".personality lj_err_unwind_arm\n"
236 ".fnend\n");
237 #endif
239 fprintf(ctx->fp, "\n");
240 switch (ctx->mode) {
241 case BUILD_elfasm:
242 fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n");
243 #if LJ_TARGET_PPCSPE
244 /* Soft-float ABI + SPE. */
245 fprintf(ctx->fp, "\t.gnu_attribute 4, 2\n\t.gnu_attribute 8, 3\n");
246 #elif LJ_TARGET_PPC
247 /* Hard-float ABI. */
248 fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n");
249 #endif
250 /* fallthrough */
251 case BUILD_coffasm:
252 fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident);
253 break;
254 case BUILD_machasm:
255 fprintf(ctx->fp,
256 "\t.cstring\n"
257 "\t.ascii \"%s\\0\"\n", ctx->dasm_ident);
258 break;
259 default:
260 break;
262 fprintf(ctx->fp, "\n");