mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-13 14:59:32 -06:00
a more efficient tail call by using the new TUCK instruction
This commit is contained in:
@@ -136,11 +136,12 @@ typedef enum {
|
||||
PGF_INSTR_SET_PAD = 9,
|
||||
PGF_INSTR_PUSH_FRAME = 10,
|
||||
PGF_INSTR_PUSH = 11,
|
||||
PGF_INSTR_EVAL = 12,
|
||||
PGF_INSTR_DROP = 15,
|
||||
PGF_INSTR_JUMP = 16,
|
||||
PGF_INSTR_FAIL = 17,
|
||||
PGF_INSTR_ADD = 18,
|
||||
PGF_INSTR_TUCK = 12,
|
||||
PGF_INSTR_EVAL = 13,
|
||||
PGF_INSTR_DROP = 16,
|
||||
PGF_INSTR_JUMP = 17,
|
||||
PGF_INSTR_FAIL = 18,
|
||||
PGF_INSTR_ADD = 19,
|
||||
} PgfInstruction;
|
||||
|
||||
typedef GuSeq PgfConcrs;
|
||||
|
||||
@@ -1000,6 +1000,61 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PGF_INSTR_TUCK: {
|
||||
switch (mod) {
|
||||
case 0: {
|
||||
size_t offset = pgf_read_int(rdr);
|
||||
size_t sindex = pgf_read_int(rdr);
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
gu_printf(out, err, "TUCK hp(%d) %d\n", offset, sindex);
|
||||
#endif
|
||||
|
||||
if (offset == 0) {
|
||||
jit_stxi_p(sizeof(PgfClosure*)*sindex, JIT_SP, JIT_VHEAP);
|
||||
} else {
|
||||
jit_addi_p(JIT_R0, JIT_VHEAP, offset*sizeof(void*));
|
||||
jit_stxi_p(sizeof(PgfClosure*)*sindex, JIT_SP, JIT_R0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
size_t index = pgf_read_int(rdr);
|
||||
size_t sindex = pgf_read_int(rdr);
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
gu_printf(out, err, "TUCK stk(%d) %d\n", index, sindex);
|
||||
#endif
|
||||
jit_ldxi_p(JIT_R0, JIT_SP, index*sizeof(PgfClosure*));
|
||||
jit_stxi_p(sizeof(PgfClosure*)*sindex, JIT_SP, JIT_R0);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
size_t index = pgf_read_int(rdr);
|
||||
size_t sindex = pgf_read_int(rdr);
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
gu_printf(out, err, "TUCK env(%d) %d\n", index, sindex);
|
||||
#endif
|
||||
jit_ldxi_p(JIT_R0, JIT_VCLOS, sizeof(PgfClosure)+index*sizeof(PgfClosure*));
|
||||
jit_stxi_p(sizeof(PgfClosure*)*sindex, JIT_SP, JIT_R0);
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
PgfCId id = pgf_read_cid(rdr, rdr->tmp_pool);
|
||||
size_t sindex = pgf_read_int(rdr);
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
gu_printf(out, err, "TUCK %s %d\n", id, sindex);
|
||||
#endif
|
||||
PgfCallPatch patch;
|
||||
patch.cid = id;
|
||||
patch.ref = jit_movi_p(JIT_R0, jit_forward());
|
||||
gu_buf_push(rdr->jit_state->call_patches, PgfCallPatch, patch);
|
||||
jit_stxi_p(sizeof(PgfClosure*)*sindex, JIT_SP, JIT_R0);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
gu_impossible();
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PGF_INSTR_EVAL+0:
|
||||
case PGF_INSTR_EVAL+2:
|
||||
case PGF_INSTR_EVAL+1: {
|
||||
@@ -1057,36 +1112,18 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
||||
}
|
||||
case 1: {
|
||||
size_t a = pgf_read_int(rdr);
|
||||
size_t b = pgf_read_int(rdr);
|
||||
size_t c = pgf_read_int(rdr);
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
gu_printf(out, err, " tail(%d,%d,%d)\n", a, b, c);
|
||||
gu_printf(out, err, " tail(%d)\n", a);
|
||||
#endif
|
||||
|
||||
jit_ldxi_p(JIT_R2, JIT_SP, sizeof(PgfClosure*)*(c-a-1));
|
||||
for (size_t i = 0; i < c-b; i++) {
|
||||
jit_ldxi_p(JIT_R1, JIT_SP, sizeof(PgfClosure*)*((c-b-1)-i));
|
||||
jit_stxi_p(sizeof(PgfClosure*)*((c-1)-i), JIT_SP, JIT_R1);
|
||||
}
|
||||
jit_addi_p(JIT_SP, JIT_SP, b*sizeof(PgfClosure*));
|
||||
jit_pushr_p(JIT_R2);
|
||||
jit_addi_p(JIT_SP, JIT_SP, a*sizeof(PgfClosure*));
|
||||
jit_jmpr(JIT_R0);
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
size_t b = pgf_read_int(rdr);
|
||||
size_t c = pgf_read_int(rdr);
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
gu_printf(out, err, " update(%d,%d)\n", b, c);
|
||||
gu_printf(out, err, " update\n");
|
||||
#endif
|
||||
|
||||
if (b > 1) {
|
||||
for (size_t i = 0; i < c-b; i++) {
|
||||
jit_ldxi_p(JIT_R1, JIT_SP, sizeof(PgfClosure*)*(c-b-1-i));
|
||||
jit_stxi_p(sizeof(PgfClosure*)*(c-2-i), JIT_SP, JIT_R1);
|
||||
}
|
||||
jit_addi_p(JIT_SP, JIT_SP, (b-1)*sizeof(PgfClosure*));
|
||||
}
|
||||
jit_movi_p(JIT_R1, abstr->eval_gates->update_closure);
|
||||
jit_pushr_p(JIT_R1);
|
||||
jit_jmpr(JIT_R0);
|
||||
|
||||
@@ -158,22 +158,26 @@ instance Binary Instr where
|
||||
put (PUSH (ARG_VAR n)) = putWord8 45 >> put n
|
||||
put (PUSH (FREE_VAR n)) = putWord8 46 >> put n
|
||||
put (PUSH (GLOBAL id)) = putWord8 47 >> put id
|
||||
put (EVAL (HEAP n) (RecCall )) = putWord8 48 >> put n
|
||||
put (EVAL (ARG_VAR n) (RecCall )) = putWord8 49 >> put n
|
||||
put (EVAL (FREE_VAR n) (RecCall )) = putWord8 50 >> put n
|
||||
put (EVAL (GLOBAL id) (RecCall )) = putWord8 51 >> put id
|
||||
put (EVAL (HEAP n) (TailCall a b c)) = putWord8 52 >> put n >> put (a,b,c)
|
||||
put (EVAL (ARG_VAR n) (TailCall a b c)) = putWord8 53 >> put n >> put (a,b,c)
|
||||
put (EVAL (FREE_VAR n) (TailCall a b c)) = putWord8 54 >> put n >> put (a,b,c)
|
||||
put (EVAL (GLOBAL id) (TailCall a b c)) = putWord8 55 >> put id >> put (a,b,c)
|
||||
put (EVAL (HEAP n) (UpdateCall b c)) = putWord8 56 >> put n >> put (b,c)
|
||||
put (EVAL (ARG_VAR n) (UpdateCall b c)) = putWord8 57 >> put n >> put (b,c)
|
||||
put (EVAL (FREE_VAR n) (UpdateCall b c)) = putWord8 58 >> put n >> put (b,c)
|
||||
put (EVAL (GLOBAL id) (UpdateCall b c)) = putWord8 59 >> put id >> put (b,c)
|
||||
put (DROP n ) = putWord8 60 >> put n
|
||||
put (JUMP l ) = putWord8 64 >> put l
|
||||
put (FAIL ) = putWord8 68
|
||||
put (ADD ) = putWord8 72
|
||||
put (TUCK (HEAP n) i) = putWord8 48 >> put (n,i)
|
||||
put (TUCK (ARG_VAR n) i) = putWord8 49 >> put (n,i)
|
||||
put (TUCK (FREE_VAR n) i) = putWord8 50 >> put (n,i)
|
||||
put (TUCK (GLOBAL id) i) = putWord8 51 >> put (id,i)
|
||||
put (EVAL (HEAP n) RecCall) = putWord8 52 >> put n
|
||||
put (EVAL (ARG_VAR n) RecCall) = putWord8 53 >> put n
|
||||
put (EVAL (FREE_VAR n) RecCall) = putWord8 54 >> put n
|
||||
put (EVAL (GLOBAL id) RecCall) = putWord8 55 >> put id
|
||||
put (EVAL (HEAP n) (TailCall a)) = putWord8 56 >> put n >> put a
|
||||
put (EVAL (ARG_VAR n) (TailCall a)) = putWord8 57 >> put n >> put a
|
||||
put (EVAL (FREE_VAR n) (TailCall a)) = putWord8 58 >> put n >> put a
|
||||
put (EVAL (GLOBAL id) (TailCall a)) = putWord8 59 >> put id >> put a
|
||||
put (EVAL (HEAP n) UpdateCall) = putWord8 60 >> put n
|
||||
put (EVAL (ARG_VAR n) UpdateCall) = putWord8 61 >> put n
|
||||
put (EVAL (FREE_VAR n) UpdateCall) = putWord8 62 >> put n
|
||||
put (EVAL (GLOBAL id) UpdateCall) = putWord8 63 >> put id
|
||||
put (DROP n ) = putWord8 64 >> put n
|
||||
put (JUMP l ) = putWord8 68 >> put l
|
||||
put (FAIL ) = putWord8 72
|
||||
put (ADD ) = putWord8 76
|
||||
|
||||
instance Binary Type where
|
||||
put (DTyp hypos cat exps) = put (hypos,cat,exps)
|
||||
|
||||
@@ -27,6 +27,7 @@ data Instr
|
||||
| SET_PAD
|
||||
| PUSH_FRAME
|
||||
| PUSH IVal
|
||||
| TUCK IVal {-# UNPACK #-} !Int
|
||||
| EVAL IVal TailInfo
|
||||
| DROP {-# UNPACK #-} !Int
|
||||
| JUMP {-# UNPACK #-} !CodeLabel
|
||||
@@ -38,11 +39,12 @@ data IVal
|
||||
| ARG_VAR {-# UNPACK #-} !Int
|
||||
| FREE_VAR {-# UNPACK #-} !Int
|
||||
| GLOBAL CId
|
||||
deriving Eq
|
||||
|
||||
data TailInfo
|
||||
= RecCall
|
||||
| TailCall {-# UNPACK #-} !Int {-# UNPACK #-} !Int {-# UNPACK #-} !Int
|
||||
| UpdateCall {-# UNPACK #-} !Int {-# UNPACK #-} !Int
|
||||
| TailCall {-# UNPACK #-} !Int
|
||||
| UpdateCall
|
||||
|
||||
ppLit (LStr s) = text (show s)
|
||||
ppLit (LInt n) = int n
|
||||
@@ -65,6 +67,7 @@ ppInstr (SET_PAD ) = text "SET_PAD"
|
||||
ppInstr (PUSH_FRAME ) = text "PUSH_FRAME"
|
||||
ppInstr (PUSH v) = text "PUSH " <+> ppIVal v
|
||||
ppInstr (EVAL v ti) = text "EVAL " <+> ppIVal v <+> ppTailInfo ti
|
||||
ppInstr (TUCK v n ) = text "TUCK " <+> ppIVal v <+> int n
|
||||
ppInstr (DROP n ) = text "DROP " <+> int n
|
||||
ppInstr (JUMP l ) = text "JUMP " <+> ppLabel l
|
||||
ppInstr (FAIL ) = text "FAIL"
|
||||
@@ -75,8 +78,8 @@ ppIVal (ARG_VAR n) = text "stk" <> parens (int n)
|
||||
ppIVal (FREE_VAR n) = text "env" <> parens (int n)
|
||||
ppIVal (GLOBAL id) = ppCId id
|
||||
|
||||
ppTailInfo RecCall = empty
|
||||
ppTailInfo (TailCall a b c) = text "tail" <> parens (int a <> comma <> int b <> comma <> int c)
|
||||
ppTailInfo (UpdateCall b c) = text "update" <> parens (int b <> comma <> int c)
|
||||
ppTailInfo RecCall = empty
|
||||
ppTailInfo (TailCall n) = text "tail" <> parens (int n)
|
||||
ppTailInfo UpdateCall = text "update"
|
||||
|
||||
ppLabel l = text (let s = show l in replicate (3-length s) '0' ++ s)
|
||||
|
||||
Reference in New Issue
Block a user