From b2ad3bd000efd2623078adf30a5686a987f474f9 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Tue, 7 Apr 2009 23:55:16 +0100 Subject: [PATCH] first cut at Anderson's JIT code. --- C/absmi.c | 24 +++++++++++++++++++++++- C/amasm.c | 18 ++++++++++++++++++ C/computils.c | 1 + H/YapOpcodes.h | 1 + H/absmi.h | 8 ++++++++ H/amidefs.h | 9 +++++++++ H/compile.h | 1 + H/rclause.h | 8 ++++++++ H/walkclause.h | 4 ++++ 9 files changed, 73 insertions(+), 1 deletion(-) diff --git a/C/absmi.c b/C/absmi.c index d45b0b1b6..7c0b4859a 100644 --- a/C/absmi.c +++ b/C/absmi.c @@ -8931,6 +8931,28 @@ Yap_absmi(int inp) /************************************************************************\ +* Native Code Execution * +\************************************************************************/ + + /* native_me */ + BOp(native_me, aFlp); + + if (PREG->u.aFlp.n) + EXEC_NATIVE(PREG->u.aFlp.n); + else { + PREG->u.aFlp.n++; + if (PREG->u.aFlp.n == MAX_INVOCATION) + PREG->u.aFlp.n = Yapc_Compile(PREG->u.aFlp.p); + } + + PREG = NEXTOP(PREG, aFlp); + GONext(); + + ENDBOp(); + + + +/************************************************************************\ * Basic Primitive Predicates * \************************************************************************/ @@ -11271,7 +11293,7 @@ Yap_absmi(int inp) d0 = (CELL) (f) (d0,d1); setregs(); } - if (!d0 || PREG == FAILCODE) { + if (!d0) { if (PREG != FAILCODE) PREG = PREG->u.plxxs.f; JMPNext(); diff --git a/C/amasm.c b/C/amasm.c index d2da9b60b..b1a469679 100644 --- a/C/amasm.c +++ b/C/amasm.c @@ -1606,6 +1606,21 @@ a_empty_call(clause_info *clinfo, yamop *code_p, int pass_no, struct intermedia return code_p; } +static yamop * +a_cnp(op_numbers opcode, yamop *code_p, int pass_no, struct intermediates *cip) +{ + if (pass_no) { + code_p->opc = emit_op(opcode); + code_p->u.aFlp.n = 0; + code_p->u.aFlp.native = NULL; + code_p->u.aFlp.native_next = 0; + code_p->u.aFlp.p = cip->CurrentPred; + } + GONEXT(aFlp); + return code_p; +} + + static yamop * a_pl(op_numbers opcode, PredEntry *pred, yamop *code_p, int pass_no) { @@ -3376,6 +3391,9 @@ do_pass(int pass_no, yamop **entry_codep, int assembling, int *clause_has_blobsp case cut_op: code_p = a_cut(&clinfo, code_p, pass_no, cip); break; + case native_op: + code_p = a_cnp(_native_me, code_p, pass_no, cip); + break; case cutexit_op: code_p = a_cut(&clinfo, code_p, pass_no, cip); if (cip->CurrentPred->PredFlags & LogUpdatePredFlag && diff --git a/C/computils.c b/C/computils.c index 6196111ee..2b4714398 100644 --- a/C/computils.c +++ b/C/computils.c @@ -637,6 +637,7 @@ static char *opformat[] = "unify_last_dbterm\t%w", "unify_last_longint\t%w", "unify_last_bigint\t%l", + "native_code" "function_to_var\t%v,%B", "function_to_val\t%v,%B", "function_to_0\t%B", diff --git a/H/YapOpcodes.h b/H/YapOpcodes.h index 051810a66..26ea57d6c 100644 --- a/H/YapOpcodes.h +++ b/H/YapOpcodes.h @@ -235,6 +235,7 @@ OPCODE(index_dbref ,e), OPCODE(index_blob ,e), OPCODE(index_long ,e), + OPCODE(native_me ,aFlp), OPCODE(p_atom_x ,xl), OPCODE(p_atom_y ,yl), OPCODE(p_atomic_x ,xl), diff --git a/H/absmi.h b/H/absmi.h index 24b1804fb..3e18e69a2 100644 --- a/H/absmi.h +++ b/H/absmi.h @@ -20,6 +20,14 @@ static char SccsId[] = "%W% %G%"; #endif /* SCCS */ +#if NATIVE_EXECUTION +/* just a stub */ +#else +#define EXEC_NATIVE(X) +#define MAX_INVOCATION 1024 +#define Yapc_Compile(P) 0 +#endif + /*************************************************************** * Macros for register manipulation * ***************************************************************/ diff --git a/H/amidefs.h b/H/amidefs.h index cc7fddb60..70f3b3bc2 100644 --- a/H/amidefs.h +++ b/H/amidefs.h @@ -330,6 +330,15 @@ typedef struct yami { struct yami *d; CELL next; } Otapl; + struct { + COUNT n; /* call counter */ + CPredicate native; /* native code pointer */ + struct yami *native_next; /* next instruction to execute + after native code + if the predicate was not fully compiled */ + struct pred_entry *p; /* Pointer to pred */ + CELL next; + } aFlp; /* The next two instructions are twin: they both correspond to the old ldd. */ /* The first one, aLl, handles try_logical and retry_logical, */ /* Ill handles trust_logical. */ diff --git a/H/compile.h b/H/compile.h index 67cd726f9..5e858ea31 100644 --- a/H/compile.h +++ b/H/compile.h @@ -77,6 +77,7 @@ typedef enum compiler_op { unify_last_dbterm_op, unify_last_longint_op, unify_last_bigint_op, + native_op, f_var_op, f_val_op, f_0_op, diff --git a/H/rclause.h b/H/rclause.h index 7ff1a3ebe..3ca33d3a5 100644 --- a/H/rclause.h +++ b/H/rclause.h @@ -124,6 +124,14 @@ restore_opcodes(yamop *pc) pc->u.Otapl.d = PtoOpAdjust(pc->u.Otapl.d); pc = NEXTOP(pc,Otapl); break; + /* instructions type aFlp */ + case _native_me: + pc->u.aFlp.n = ArityAdjust(pc->u.aFlp.n); + pc->u.aFlp.native = ExternalFunctionAdjust(pc->u.aFlp.native); + pc->u.aFlp.native_next = PtoOpAdjust(pc->u.aFlp.native_next); + pc->u.aFlp.native = PtoPredAdjust(pc->u.aFlp.native); + pc = NEXTOP(pc,aFlp); + break; /* instructions type c */ case _write_atom: pc->u.c.c = ConstantTermAdjust(pc->u.c.c); diff --git a/H/walkclause.h b/H/walkclause.h index a01d7e789..ccaef903b 100644 --- a/H/walkclause.h +++ b/H/walkclause.h @@ -90,6 +90,10 @@ pp = pc->u.Otapl.p; pc = NEXTOP(pc,Otapl); break; + /* instructions type aFlp */ + case _native_me: + pc = NEXTOP(pc,aFlp); + break; /* instructions type c */ case _write_atom: pc = NEXTOP(pc,c);