int build_code(Dst_DECL) { int ret = 0; size_t codesz = 0; uint8_t* code = NULL; int nglob = GLOB_MAX; void **glob = (void **)malloc(nglob*sizeof(void *)); memset(glob, 0, nglob*sizeof(void *)); dasm_init(Dst, DASM_MAXSECTION); dasm_setupglobal(Dst, glob, nglob); dasm_setup(Dst, build_actionlist); (void)create_backend(Dst); /* Finalize */ (void)dasm_checkstep(Dst, -1); /* sanity check */ if((ret = dasm_link(Dst, &codesz))) return ret; code = (uint8_t *)pa_malloc(codesz, true); if((ret = dasm_encode(Dst, (void *)code))) return ret; //write_raw("debug.out", code, codesz); dasm_gen_func fp = (dasm_gen_func)code; ret = fp(); printf("generated function return value = %d\n",ret); dasm_free(Dst); free(glob); free(code); return 0; }
static cfunction compile(struct jit* jit, lua_State* L, cfunction func, int ref) { struct jit_head* code; size_t codesz; int err; dasm_checkstep(jit, -1); if ((err = dasm_link(jit, &codesz)) != 0) { char buf[32]; sprintf(buf, "%x", err); luaL_error(L, "dasm_link error %s", buf); } codesz += sizeof(struct jit_head); code = (struct jit_head*) reserve_code(jit, L, codesz); code->ref = ref; code->size = codesz; compile_extern_jump(jit, L, func, code->jump); if ((err = dasm_encode(jit, code+1)) != 0) { char buf[32]; sprintf(buf, "%x", err); commit_code(jit, code, 0); luaL_error(L, "dasm_encode error %s", buf); } commit_code(jit, code, codesz); return (cfunction) (code+1); }
/* 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; }