CONSOLE: Handle P64, GPR64, TOC* and PPE (PS3) in PPC interpreter.
[luajit-2.0/celess22.git] / src / host / buildvm_asm.c
blob08264c8b5462c948e07bb718536e81c014c138f6
1 /*
2 ** LuaJIT VM builder: Assembler source code emitter.
3 ** Copyright (C) 2005-2012 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 || LJ_TARGET_PPCSPE
111 #if LJ_TARGET_PS3
112 #define TOCPREFIX "."
113 #else
114 #define TOCPREFIX ""
115 #endif
116 if ((ins >> 26) == 16) {
117 fprintf(ctx->fp, "\t%s %d, %d, " TOCPREFIX "%s\n",
118 (ins & 1) ? "bcl" : "bc", (ins >> 21) & 31, (ins >> 16) & 31, sym);
119 } else if ((ins >> 26) == 18) {
120 fprintf(ctx->fp, "\t%s " TOCPREFIX "%s\n", (ins & 1) ? "bl" : "b", sym);
121 } else {
122 fprintf(stderr,
123 "Error: unsupported opcode %08x for %s symbol relocation.\n",
124 ins, sym);
125 exit(1);
127 #elif LJ_TARGET_MIPS
128 fprintf(stderr,
129 "Error: unsupported opcode %08x for %s symbol relocation.\n",
130 ins, sym);
131 exit(1);
132 #else
133 #error "missing relocation support for this architecture"
134 #endif
136 #endif
138 #if LJ_TARGET_ARM
139 #define ELFASM_PX "%%"
140 #else
141 #define ELFASM_PX "@"
142 #endif
144 /* Emit an assembler label. */
145 static void emit_asm_label(BuildCtx *ctx, const char *name, int size, int isfunc)
147 switch (ctx->mode) {
148 case BUILD_elfasm:
149 #if LJ_TARGET_PS3
150 if (!strncmp(name, "lj_vm_", 6)) {
151 fprintf(ctx->fp,
152 "\n\t.globl %s\n"
153 "\n\t.section \".opd\",\"aw\"\n"
154 "%s:\n"
155 "\t.long .%s,.TOC.@tocbase32\n"
156 "\t.size %s,8\n"
157 "\t.previous\n"
158 "\t.globl .%s\n"
159 "\t.hidden .%s\n"
160 "\t.type .%s, " ELFASM_PX "function\n"
161 "\t.size .%s, %d\n"
162 ".%s:\n",
163 name, name, name, name, name, name, name, name, size, name);
164 break;
166 #endif
167 fprintf(ctx->fp,
168 "\n\t.globl %s\n"
169 "\t.hidden %s\n"
170 "\t.type %s, " ELFASM_PX "%s\n"
171 "\t.size %s, %d\n"
172 "%s:\n",
173 name, name, name, isfunc ? "function" : "object", name, size, name);
174 break;
175 case BUILD_coffasm:
176 fprintf(ctx->fp, "\n\t.globl %s\n", name);
177 if (isfunc)
178 fprintf(ctx->fp, "\t.def %s; .scl 3; .type 32; .endef\n", name);
179 fprintf(ctx->fp, "%s:\n", name);
180 break;
181 case BUILD_machasm:
182 fprintf(ctx->fp,
183 "\n\t.private_extern %s\n"
184 "%s:\n", name, name);
185 break;
186 default:
187 break;
191 /* Emit alignment. */
192 static void emit_asm_align(BuildCtx *ctx, int bits)
194 switch (ctx->mode) {
195 case BUILD_elfasm:
196 case BUILD_coffasm:
197 fprintf(ctx->fp, "\t.p2align %d\n", bits);
198 break;
199 case BUILD_machasm:
200 fprintf(ctx->fp, "\t.align %d\n", bits);
201 break;
202 default:
203 break;
207 /* ------------------------------------------------------------------------ */
209 /* Emit assembler source code. */
210 void emit_asm(BuildCtx *ctx)
212 int i, rel;
214 fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch);
215 fprintf(ctx->fp, "\t.text\n");
216 emit_asm_align(ctx, 4);
218 emit_asm_label(ctx, ctx->beginsym, 0, 0);
219 if (ctx->mode != BUILD_machasm)
220 fprintf(ctx->fp, ".Lbegin:\n");
222 #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND
223 /* This should really be moved into buildvm_arm.dasc. */
224 fprintf(ctx->fp,
225 ".fnstart\n"
226 ".save {r4, r5, r6, r7, r8, r9, r10, r11, lr}\n"
227 ".pad #28\n");
228 #endif
229 #if LJ_TARGET_MIPS
230 fprintf(ctx->fp, ".set nomips16\n.abicalls\n.set noreorder\n.set nomacro\n");
231 #endif
233 for (i = rel = 0; i < ctx->nsym; i++) {
234 int32_t ofs = ctx->sym[i].ofs;
235 int32_t next = ctx->sym[i+1].ofs;
236 #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND && LJ_HASFFI
237 if (!strcmp(ctx->sym[i].name, "lj_vm_ffi_call"))
238 fprintf(ctx->fp,
239 ".globl lj_err_unwind_arm\n"
240 ".personality lj_err_unwind_arm\n"
241 ".fnend\n"
242 ".fnstart\n"
243 ".save {r4, r5, r11, lr}\n"
244 ".setfp r11, sp\n");
245 #endif
246 emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1);
247 while (rel < ctx->nreloc && ctx->reloc[rel].ofs <= next) {
248 BuildReloc *r = &ctx->reloc[rel];
249 int n = r->ofs - ofs;
250 #if LJ_TARGET_X86ORX64
251 if (ctx->mode == BUILD_machasm && r->type != 0) {
252 emit_asm_reloc_mach(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
253 } else {
254 emit_asm_bytes(ctx, ctx->code+ofs, n);
255 emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);
257 ofs += n+4;
258 #else
259 emit_asm_wordreloc(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
260 ofs += n;
261 #endif
262 rel++;
264 #if LJ_TARGET_X86ORX64
265 emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);
266 #else
267 emit_asm_words(ctx, ctx->code+ofs, next-ofs);
268 #endif
271 #if LJ_TARGET_ARM && defined(__GNUC__) && !LJ_NO_UNWIND
272 fprintf(ctx->fp,
273 #if !LJ_HASFFI
274 ".globl lj_err_unwind_arm\n"
275 ".personality lj_err_unwind_arm\n"
276 #endif
277 ".fnend\n");
278 #endif
280 fprintf(ctx->fp, "\n");
281 switch (ctx->mode) {
282 case BUILD_elfasm:
283 #if !LJ_TARGET_PS3
284 fprintf(ctx->fp, "\t.section .note.GNU-stack,\"\"," ELFASM_PX "progbits\n");
285 #endif
286 #if LJ_TARGET_PPCSPE
287 /* Soft-float ABI + SPE. */
288 fprintf(ctx->fp, "\t.gnu_attribute 4, 2\n\t.gnu_attribute 8, 3\n");
289 #elif LJ_TARGET_PPC && !LJ_TARGET_PS3
290 /* Hard-float ABI. */
291 fprintf(ctx->fp, "\t.gnu_attribute 4, 1\n");
292 #endif
293 /* fallthrough */
294 case BUILD_coffasm:
295 fprintf(ctx->fp, "\t.ident \"%s\"\n", ctx->dasm_ident);
296 break;
297 case BUILD_machasm:
298 fprintf(ctx->fp,
299 "\t.cstring\n"
300 "\t.ascii \"%s\\0\"\n", ctx->dasm_ident);
301 break;
302 default:
303 break;
305 fprintf(ctx->fp, "\n");