summaryrefslogtreecommitdiff
path: root/src/buildvm_asm.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildvm_asm.c')
-rw-r--r--src/buildvm_asm.c67
1 files changed, 13 insertions, 54 deletions
diff --git a/src/buildvm_asm.c b/src/buildvm_asm.c
index b135b864..1898f0d5 100644
--- a/src/buildvm_asm.c
+++ b/src/buildvm_asm.c
@@ -123,74 +123,33 @@ static void emit_asm_align(BuildCtx *ctx, int bits)
123/* Emit assembler source code. */ 123/* Emit assembler source code. */
124void emit_asm(BuildCtx *ctx) 124void emit_asm(BuildCtx *ctx)
125{ 125{
126 char name[80]; 126 int i, rel;
127 int32_t prev;
128 int i, pi, rel;
129#if LJ_64
130 const char *symprefix = ctx->mode == BUILD_machasm ? "_" : "";
131 int keepfc = 0;
132#else
133 const char *symprefix = ctx->mode != BUILD_elfasm ? "_" : "";
134 /* Keep fastcall suffix for COFF on WIN32. */
135 int keepfc = (ctx->mode == BUILD_coffasm);
136#endif
137 127
138 fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch); 128 fprintf(ctx->fp, "\t.file \"buildvm_%s.dasc\"\n", ctx->dasm_arch);
139 fprintf(ctx->fp, "\t.text\n"); 129 fprintf(ctx->fp, "\t.text\n");
140 emit_asm_align(ctx, 4); 130 emit_asm_align(ctx, 4);
141 131
142 sprintf(name, "%s" LABEL_ASM_BEGIN, symprefix); 132 emit_asm_label(ctx, ctx->beginsym, 0, 0);
143 emit_asm_label(ctx, name, 0, 0);
144 if (ctx->mode != BUILD_machasm) 133 if (ctx->mode != BUILD_machasm)
145 fprintf(ctx->fp, ".Lbegin:\n"); 134 fprintf(ctx->fp, ".Lbegin:\n");
146 135
147 i = 0; 136 for (i = rel = 0; i < ctx->nsym; i++) {
148 do { 137 int32_t ofs = ctx->sym[i].ofs;
149 pi = ctx->perm[i++]; 138 int32_t next = ctx->sym[i+1].ofs;
150 prev = ctx->sym_ofs[pi]; 139 emit_asm_label(ctx, ctx->sym[i].name, next - ofs, 1);
151 } while (prev < 0); /* Skip the _Z symbols. */ 140 while (rel < ctx->nreloc && ctx->reloc[rel].ofs < next) {
152
153 for (rel = 0; i <= ctx->nsym; i++) {
154 int ni = ctx->perm[i];
155 int32_t next = ctx->sym_ofs[ni];
156 int size = (int)(next - prev);
157 int32_t stop = next;
158 if (pi >= ctx->npc) {
159 char *p;
160 sprintf(name, "%s" LABEL_PREFIX "%s", symprefix,
161 ctx->globnames[pi-ctx->npc]);
162 p = strchr(name, '@');
163 if (p) { if (keepfc) name[0] = '@'; else *p = '\0'; }
164 emit_asm_label(ctx, name, size, 1);
165#if LJ_HASJIT
166 } else {
167#else
168 } else if (!(pi == BC_JFORI || pi == BC_JFORL || pi == BC_JITERL ||
169 pi == BC_JLOOP || pi == BC_IFORL || pi == BC_IITERL ||
170 pi == BC_ILOOP)) {
171#endif
172 sprintf(name, "%s" LABEL_PREFIX_BC "%s", symprefix, bc_names[pi]);
173 emit_asm_label(ctx, name, size, 1);
174 }
175 while (rel < ctx->nreloc && ctx->reloc[rel].ofs < stop) {
176 BuildReloc *r = &ctx->reloc[rel]; 141 BuildReloc *r = &ctx->reloc[rel];
177 int n = r->ofs - prev; 142 int n = r->ofs - ofs;
178 char *p;
179 sprintf(name, "%s%s", symprefix, ctx->extnames[r->sym]);
180 p = strchr(name, '@');
181 if (p) { if (keepfc) name[0] = '@'; else *p = '\0'; }
182 if (ctx->mode == BUILD_machasm && r->type != 0) { 143 if (ctx->mode == BUILD_machasm && r->type != 0) {
183 emit_asm_reloc_mach(ctx, ctx->code+prev, n, name); 144 emit_asm_reloc_mach(ctx, ctx->code+ofs, n, ctx->relocsym[r->sym]);
184 } else { 145 } else {
185 emit_asm_bytes(ctx, ctx->code+prev, n); 146 emit_asm_bytes(ctx, ctx->code+ofs, n);
186 emit_asm_reloc(ctx, r->type, name); 147 emit_asm_reloc(ctx, r->type, ctx->relocsym[r->sym]);
187 } 148 }
188 prev += n+4; 149 ofs += n+4;
189 rel++; 150 rel++;
190 } 151 }
191 emit_asm_bytes(ctx, ctx->code+prev, stop-prev); 152 emit_asm_bytes(ctx, ctx->code+ofs, next-ofs);
192 prev = next;
193 pi = ni;
194 } 153 }
195 154
196 fprintf(ctx->fp, "\n"); 155 fprintf(ctx->fp, "\n");