/* Naive insertion sort. Performance doesn't matter here. */ static void sym_insert(BuildCtx *ctx, int32_t ofs, const char *prefix, const char *suffix) { ptrdiff_t i = ctx->nsym++; while (i > 0) { if (ctx->sym[i-1].ofs <= ofs) break; ctx->sym[i] = ctx->sym[i-1]; i--; } ctx->sym[i].ofs = ofs; ctx->sym[i].name = sym_decorate(ctx, prefix, suffix); }
/* Collect external relocations. */ static int collect_reloc(BuildCtx *ctx, uint8_t *addr, int idx, int type) { if (ctx->nreloc >= BUILD_MAX_RELOC) { fprintf(stderr, "Error: too many relocations, increase BUILD_MAX_RELOC.\n"); exit(1); } if (relocmap[idx] < 0) { relocmap[idx] = ctx->nrelocsym; ctx->relocsym[ctx->nrelocsym] = sym_decorate(ctx, "", extnames[idx]); ctx->nrelocsym++; } ctx->reloc[ctx->nreloc].ofs = (int32_t)(addr - ctx->code); ctx->reloc[ctx->nreloc].sym = relocmap[idx]; ctx->reloc[ctx->nreloc].type = type; ctx->nreloc++; return 0; /* Encode symbol offset of 0. */ }
/* Build the machine code. */ static int build_code(BuildCtx *ctx) { int status; int i; /* Initialize DynASM structures. */ ctx->nglob = GLOB__MAX; ctx->glob = (void **)malloc(ctx->nglob*sizeof(void *)); memset(ctx->glob, 0, ctx->nglob*sizeof(void *)); ctx->nreloc = 0; ctx->globnames = globnames; ctx->relocsym = (const char **)malloc(NRELOCSYM*sizeof(const char *)); ctx->nrelocsym = 0; for (i = 0; i < (int)NRELOCSYM; i++) relocmap[i] = -1; ctx->dasm_ident = DASM_IDENT; ctx->dasm_arch = DASM_ARCH; dasm_init(Dst, DASM_MAXSECTION); dasm_setupglobal(Dst, ctx->glob, ctx->nglob); dasm_setup(Dst, build_actionlist); /* Call arch-specific backend to emit the code. */ ctx->npc = build_backend(ctx); /* Finalize the code. */ (void)dasm_checkstep(Dst, -1); if ((status = dasm_link(Dst, &ctx->codesz))) return status; ctx->code = (uint8_t *)malloc(ctx->codesz); if ((status = dasm_encode(Dst, (void *)ctx->code))) return status; /* Allocate symbol table and bytecode offsets. */ ctx->beginsym = sym_decorate(ctx, "", LABEL_PREFIX "vm_asm_begin"); ctx->sym = (BuildSym *)malloc((ctx->npc+ctx->nglob+1)*sizeof(BuildSym)); ctx->nsym = 0; ctx->bc_ofs = (int32_t *)malloc(ctx->npc*sizeof(int32_t)); /* Collect the opcodes (PC labels). */ for (i = 0; i < ctx->npc; i++) { int32_t ofs = dasm_getpclabel(Dst, i); if (ofs < 0) return 0x22000000|i; ctx->bc_ofs[i] = ofs; if ((LJ_HASJIT || !(i == BC_JFORI || i == BC_JFORL || i == BC_JITERL || i == BC_JLOOP || i == BC_IFORL || i == BC_IITERL || i == BC_ILOOP)) && (LJ_HASFFI || i != BC_KCDATA)) sym_insert(ctx, ofs, LABEL_PREFIX_BC, bc_names[i]); } /* Collect the globals (named labels). */ for (i = 0; i < ctx->nglob; i++) { const char *gl = globnames[i]; int len = (int)strlen(gl); if (!ctx->glob[i]) { fprintf(stderr, "Error: undefined global %s\n", gl); exit(2); } /* Skip the _Z symbols. */ if (!(len >= 2 && gl[len-2] == '_' && gl[len-1] == 'Z')) sym_insert(ctx, (int32_t)((uint8_t *)(ctx->glob[i]) - ctx->code), LABEL_PREFIX, globnames[i]); } /* Close the address range. */ sym_insert(ctx, (int32_t)ctx->codesz, "", ""); ctx->nsym--; dasm_free(Dst); return 0; }