Skip to content

Commit

Permalink
Create MVMJitCode structure
Browse files Browse the repository at this point in the history
This is a single structure that contains the function pointer
as well as the size of the fragment, the number of locals, and
the 'magic bytecode' which is now constant.
  • Loading branch information
bdw committed Jul 1, 2014
1 parent 4ca7c7c commit ad5044c
Show file tree
Hide file tree
Showing 13 changed files with 683 additions and 674 deletions.
4 changes: 2 additions & 2 deletions src/core/frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ void MVM_frame_invoke(MVMThreadContext *tc, MVMStaticFrame *static_frame,
else {
/* In the post-specialize phase; can safely used the code. */
frame = allocate_frame(tc, static_frame_body, chosen_cand);
if (chosen_cand->jitcode_size) {
frame->effective_bytecode = chosen_cand->jit_bytecode;
if (chosen_cand->jitcode) {
frame->effective_bytecode = chosen_cand->jitcode->bytecode;
}
else {
frame->effective_bytecode = chosen_cand->bytecode;
Expand Down
3 changes: 1 addition & 2 deletions src/core/interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4667,8 +4667,7 @@ void MVM_interp_run(MVMThreadContext *tc, void (*initial_invoke)(MVMThreadContex
if (tc->cur_frame->spesh_cand->jitcode == NULL) {
MVM_exception_throw_adhoc(tc, "Try to enter NULL jitcode");
}
MVM_jit_enter(tc, tc->cur_frame,
tc->cur_frame->spesh_cand->jitcode);
MVM_jit_enter_code(tc, cu, tc->cur_frame->spesh_cand->jitcode);
if (MVM_frame_try_return(tc) == 0)
goto return_label;
goto NEXT;
Expand Down
32 changes: 19 additions & 13 deletions src/jit/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
#include "platform/mmap.h"
#include "emit.h"

static const MVMuint16 MAGIC_BYTECODE[] = { MVM_OP_sp_jit_enter, 0 };

MVMJitCode MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *jg,
size_t *codesize_out) {
MVMJitCode * MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *jg) {
dasm_State *state;
void * memory;
size_t codesize;
/* Space for globals */
MVMint32 num_globals = MVM_jit_num_globals();
void ** dasm_globals = malloc(num_globals * sizeof(void*));
MVMJitIns * ins = jg->first_ins;
MVMJitCode * code;

MVM_jit_log(tc, "Starting compilation\n");

Expand Down Expand Up @@ -53,28 +54,33 @@ MVMJitCode MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *jg,
dasm_encode(&state, memory);
/* protect memory from being overwritten */
MVM_platform_set_page_mode(memory, codesize, MVM_PAGE_READ|MVM_PAGE_EXEC);
*codesize_out = codesize;
/* clear up the assembler */
dasm_free(&state);
free(dasm_globals);

MVM_jit_log(tc, "Bytecode size: %d\n", codesize);
/* Create code segment */
code = malloc(sizeof(MVMJitCode));
code->func_ptr = (MVMJitFunc)memory;
code->size = codesize;
code->sf = jg->spesh->sf;
code->num_locals = jg->spesh->num_locals;
code->bytecode = (MVMuint8*)MAGIC_BYTECODE;

if (tc->instance->jit_bytecode_dir) {
MVM_jit_log_bytecode(tc, memory, codesize);
MVM_jit_log_bytecode(tc, code);
}
if (tc->instance->jit_log_fh)
fflush(tc->instance->jit_log_fh);
return (MVMJitCode)memory;
return code;
}


MVMuint8 * MVM_jit_magic_bytecode(MVMThreadContext *tc) {
MVMuint16 magic_bytecode[] = { MVM_OP_sp_jit_enter, 0 };
MVMuint8 *mbc = malloc(sizeof(magic_bytecode));
return memcpy(mbc, magic_bytecode, sizeof(magic_bytecode));
void MVM_jit_destroy_code(MVMThreadContext *tc, MVMJitCode *code) {
MVM_platform_free_pages(code->func_ptr, code->size);
free(code);
}

void MVM_jit_enter(MVMThreadContext *tc, MVMFrame *f, MVMJitCode code) {
/* this should do something intelligent, of course :-) */
code(tc, f, NULL);
void MVM_jit_enter_code(MVMThreadContext *tc, MVMCompUnit *cu,
MVMJitCode *code) {
code->func_ptr(tc, cu, NULL);
}
17 changes: 14 additions & 3 deletions src/jit/compile.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
MVMJitCode MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *graph, size_t *codesize_out);
MVMuint8* MVM_jit_magic_bytecode(MVMThreadContext *tc);
void MVM_enter_jit(MVMThreadContext *tc, MVMFrame *frame, MVMJitCode jitcode);


struct MVMJitCode {
MVMJitFunc func_ptr;
size_t size;
MVMuint8 *bytecode;
MVMint16 num_locals;
MVMStaticFrame *sf;
};

MVMJitCode* MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *graph);
void MVM_jit_destroy_code(MVMThreadContext *tc, MVMJitCode *code);
void MVM_jit_enter_code(MVMThreadContext *tc, MVMCompUnit *cu,
MVMJitCode * code);
Loading

0 comments on commit ad5044c

Please sign in to comment.