Skip to content

Commit

Permalink
Added invoke graph building
Browse files Browse the repository at this point in the history
This creates a single 'giant invoke node' that
contains all that is needed to invoke a routine.
  • Loading branch information
bdw committed Jul 8, 2014
1 parent a0fc795 commit 5071857
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/jit/compile.c
Expand Up @@ -43,6 +43,9 @@ MVMJitCode * MVM_jit_compile_graph(MVMThreadContext *tc, MVMJitGraph *jg) {
case MVM_JIT_NODE_GUARD:
MVM_jit_emit_guard(tc, jg, &node->u.guard, &state);
break;
case MVM_JIT_NODE_INVOKE:
MVM_jit_emit_invoke(tc, jg, &node->u.invoke, &state);
break;
}
node = node->next;
}
Expand Down
2 changes: 2 additions & 0 deletions src/jit/emit.h
Expand Up @@ -16,3 +16,5 @@ void MVM_jit_emit_label(MVMThreadContext *tc, MVMJitGraph *jg,
MVMJitLabel *label, dasm_State **Dst);
void MVM_jit_emit_guard(MVMThreadContext *tc, MVMJitGraph *jg,
MVMJitGuard *guard, dasm_State **Dst);
void MVM_jit_emit_invoke(MVMThreadContext *tc, MVMJitGraph *jg,
MVMJitInvoke *invoke, dasm_State **Dst);
5 changes: 5 additions & 0 deletions src/jit/emit_posix_x64.c
Expand Up @@ -1317,3 +1317,8 @@ void MVM_jit_emit_guard(MVMThreadContext *tc, MVMJitGraph *jg,
dasm_put(Dst, 1103, MVM_JIT_CTRL_DEOPT);
#line 815 "src/jit/emit_x64.dasc"
}

void MVM_jit_emit_invoke(MVMThreadContext *tc, MVMJitGraph *jg, MVMJitInvoke *invoke,
dasm_State **Dst) {
MVM_exception_throw_adhoc(tc, "JIT invoke NYI");
}
5 changes: 5 additions & 0 deletions src/jit/emit_win32_x64.c
Expand Up @@ -1262,3 +1262,8 @@ void MVM_jit_emit_guard(MVMThreadContext *tc, MVMJitGraph *jg,
dasm_put(Dst, 1069, MVM_JIT_CTRL_DEOPT);
#line 815 "src/jit/emit_x64.dasc"
}

void MVM_jit_emit_invoke(MVMThreadContext *tc, MVMJitGraph *jg, MVMJitInvoke *invoke,
dasm_State **Dst) {
MVM_exception_throw_adhoc(tc, "JIT invoke NYI");
}
5 changes: 5 additions & 0 deletions src/jit/emit_x64.dasc
Expand Up @@ -813,3 +813,8 @@ void MVM_jit_emit_guard(MVMThreadContext *tc, MVMJitGraph *jg,
| jmp ->out;
|2:
}

void MVM_jit_emit_invoke(MVMThreadContext *tc, MVMJitGraph *jg, MVMJitInvoke *invoke,
dasm_State **Dst) {
MVM_exception_throw_adhoc(tc, "JIT invoke NYI");
}
92 changes: 92 additions & 0 deletions src/jit/graph.c
Expand Up @@ -148,6 +148,95 @@ static void jgb_append_guard(MVMThreadContext *tc, JitGraphBuilder *jgb,
jgb_append_node(jgb, node);
}

static MVMint32 jgb_consume_invoke(MVMThreadContext *tc, JitGraphBuilder *jgb,
MVMSpeshIns *ins) {
MVMJitNode *node = MVM_spesh_alloc(tc, jgb->sg, sizeof(MVMJitNode));
MVMCompUnit *cu = jgb->sg->sf->body.cu;
MVMint16 callsite_idx = ins->operands[0].callsite_idx;
MVMCallsite *cs = cu->body.callsites[callsite_idx];
MVMSpeshIns **arg_ins = MVM_spesh_alloc(tc, jgb->sg, sizeof(MVMSpeshIns*) * cs->arg_count);
MVMint16 i = 0;
MVMSpeshBB *next_bb;
MVMint32 next_label;
MVMReturnType return_type;
MVMint16 return_register;
MVMint16 code_register;
MVMint16 spesh_cand;

while (ins = ins->next) {
switch(ins->info->opcode) {
case MVM_OP_arg_i:
case MVM_OP_arg_n:
case MVM_OP_arg_s:
case MVM_OP_arg_o:
case MVM_OP_argconst_i:
case MVM_OP_argconst_n:
case MVM_OP_argconst_s:
arg_ins[i++] = ins;
case MVM_OP_sp_fastinvoke_v:
return_type = MVM_RETURN_VOID;
return_register = -1;
code_register = ins->operands[0].reg.orig;
spesh_cand = ins->operands[1].lit_i16;
break;
case MVM_OP_sp_fastinvoke_o:
return_type = MVM_RETURN_OBJ;
return_register = ins->operands[0].reg.orig;;
code_register = ins->operands[1].reg.orig;
spesh_cand = ins->operands[2].lit_i16;
break;
case MVM_OP_sp_fastinvoke_s:
return_type = MVM_RETURN_STR;
return_register = ins->operands[0].reg.orig;;
code_register = ins->operands[1].reg.orig;
spesh_cand = ins->operands[2].lit_i16;
break;
case MVM_OP_sp_fastinvoke_i:
return_type = MVM_RETURN_INT;
return_register = ins->operands[0].reg.orig;;
code_register = ins->operands[1].reg.orig;
spesh_cand = ins->operands[2].lit_i16;
break;
case MVM_OP_sp_fastinvoke_n:
return_type = MVM_RETURN_NUM;
return_register = ins->operands[0].reg.orig;;
code_register = ins->operands[1].reg.orig;
spesh_cand = ins->operands[2].lit_i16;
break;
default:
MVM_jit_log(tc, "Unexpected opcode in invoke sequence: <%s>\n",
ins->info->name);
return 0;
}
}

if (!ins || i < cs->arg_count) {
MVM_jit_log(tc, "Could not find invoke opcode or enough arguments\n");
return 0;
}
if (ins != jgb->cur_bb->last_ins || jgb->cur_bb->linear_next == NULL) {
MVM_jit_log(tc, "Invoke instruction isn't last of basic block or is last of graph\n");
return 0;
}

next_bb = jgb->cur_bb->linear_next;
next_label = get_label_name(tc, jgb, next_bb);
/* create node */
node->type = MVM_JIT_NODE_INVOKE;
node->u.invoke.callsite_idx = callsite_idx;
node->u.invoke.arg_count = cs->arg_count;
node->u.invoke.arg_ins = arg_ins;
node->u.invoke.return_type = return_type;
node->u.invoke.return_register = return_register;
node->u.invoke.code_register = code_register;
node->u.invoke.spesh_cand = spesh_cand;
node->u.invoke.next_label = next_label;
jgb_append_node(jgb, node);
/* move forward to invoke ins */
jgb->cur_ins = ins;
return 1;
}

static MVMint32 jgb_consume_ins(MVMThreadContext *tc, JitGraphBuilder *jgb,
MVMSpeshIns *ins) {
int op = ins->info->opcode;
Expand Down Expand Up @@ -285,6 +374,9 @@ static MVMint32 jgb_consume_ins(MVMThreadContext *tc, JitGraphBuilder *jgb,
case MVM_OP_sp_guardtype:
jgb_append_guard(tc, jgb, ins);
break;
case MVM_OP_prepargs: {
return jgb_consume_invoke(tc, jgb, ins);
}
default:
MVM_jit_log(tc, "Don't know how to make a graph of opcode <%s>\n",
ins->info->name);
Expand Down
15 changes: 14 additions & 1 deletion src/jit/graph.h
Expand Up @@ -86,13 +86,25 @@ struct MVMJitCallC {
MVMint16 rv_idx;
};

/* A non-final list of node types */
struct MVMJitInvoke {
MVMint16 callsite_idx;
MVMint16 arg_count;
MVMSpeshIns **arg_ins;
MVMReturnType return_type;
MVMint16 return_register;
MVMint16 code_register;
MVMint16 spesh_cand;
MVMint32 next_label;
};

/* Node types */
typedef enum {
MVM_JIT_NODE_PRIMITIVE,
MVM_JIT_NODE_CALL_C,
MVM_JIT_NODE_BRANCH,
MVM_JIT_NODE_LABEL,
MVM_JIT_NODE_GUARD,
MVM_JIT_NODE_INVOKE,
} MVMJitNodeType;

struct MVMJitNode {
Expand All @@ -104,6 +116,7 @@ struct MVMJitNode {
MVMJitBranch branch;
MVMJitLabel label;
MVMJitGuard guard;
MVMJitInvoke invoke;
} u;
};

Expand Down
2 changes: 2 additions & 0 deletions src/jit/stub.c
Expand Up @@ -31,3 +31,5 @@ void MVM_jit_emit_label(MVMThreadContext *tc, MVMJitGraph *jg,
MVMJitLabel *label, dasm_State **Dst) {}
void MVM_jit_emit_guard(MVMThreadContext *tc, MVMJitGrahp *jg,
MVMJitGuard *guard, dasm_State **Dst) {}
void MVM_jit_emit_invoke(MVMThreadContext *tc, MVMJitGraph *jg,
MVMJitInvoke *invoke, dasm_State **Dst) {}
1 change: 1 addition & 0 deletions src/types.h
Expand Up @@ -185,5 +185,6 @@ typedef struct MVMJitCallC MVMJitCallC;
typedef struct MVMJitCallArg MVMJitCallArg;
typedef struct MVMJitLabel MVMJitLabel;
typedef struct MVMJitGuard MVMJitGuard;
typedef struct MVMJitInvoke MVMJitInvoke;
typedef struct MVMJitCode MVMJitCode;

0 comments on commit 5071857

Please sign in to comment.