diff --git a/src/compiler/GF/Compile/GenerateBC.hs b/src/compiler/GF/Compile/GenerateBC.hs index 7050abd55..44e28ec1c 100644 --- a/src/compiler/GF/Compile/GenerateBC.hs +++ b/src/compiler/GF/Compile/GenerateBC.hs @@ -7,6 +7,7 @@ import PGF(CId,utf8CId) import PGF.Internal(CodeLabel,Instr(..),IVal(..),TailInfo(..),Literal(..)) import qualified Data.Map as Map import Data.List(nub,mapAccumL) +import Data.Maybe(fromMaybe) generateByteCode :: SourceGrammar -> Int -> [L Equation] -> [[Instr]] generateByteCode gr arity eqs = @@ -88,10 +89,9 @@ compileBody gr arity st vs e bs = let eval fun args | arity == 0 = let (st1,is) = pushArgs (st+2) (reverse args) fun' = shiftIVal st1 fun - in [PUSH_FRAME]++is++[EVAL fun' (UpdateCall st st1)] - | otherwise = let (st1,is) = pushArgs st (reverse args) - fun' = shiftIVal st1 fun - in is++[EVAL fun' (TailCall arity st st1)] + in [PUSH_FRAME]++is++[EVAL fun' UpdateCall] + | otherwise = let (st1,fun',is) = tuckArgs arity st fun args + in is++[EVAL fun' (TailCall (st1-length args-1))] (heap,bs1,is) = compileFun gr eval st vs e 0 bs [] in (bs1,if heap > 0 then (ALLOC heap : is) else is) @@ -119,7 +119,13 @@ compileFun gr eval st vs (Q (m,id)) h0 bs args = in (h1,bs,PUT_CONSTR (i2i id):is1++eval (HEAP h0) []) else let h1 = h0 + 1 + n_args is2 = [SET (FREE_VAR i) | i <- [0..n_args-1]] ++ [SET (ARG_VAR (i+1)) | i <- [0..diff-1]] - b = CHECK_ARGS diff : ALLOC (c_arity+2) : PUT_CONSTR (i2i id) : is2 ++ [EVAL (HEAP h0) (TailCall diff (diff+1) (diff+1))] + b = CHECK_ARGS diff : + ALLOC (c_arity+2) : + PUT_CONSTR (i2i id) : + is2 ++ + TUCK (ARG_VAR 0) diff : + EVAL (HEAP h0) (TailCall diff) : + [] in (h1,b:bs,PUT_CLOSURE (length bs):is1++eval (HEAP h0) []) compileFun gr eval st vs (QC qid) h0 bs args = compileFun gr eval st vs (Q qid) h0 bs args @@ -159,7 +165,13 @@ compileArg gr st vs (Q(m,id)) h0 bs = in if c_arity == 0 then (h0,bs,GLOBAL (i2i id),[]) else let is2 = [SET (ARG_VAR (i+1)) | i <- [0..c_arity-1]] - b = CHECK_ARGS c_arity : ALLOC (c_arity+2) : PUT_CONSTR (i2i id) : is2 ++ [EVAL (HEAP h0) (TailCall c_arity (c_arity+1) (c_arity+1))] + b = CHECK_ARGS c_arity : + ALLOC (c_arity+2) : + PUT_CONSTR (i2i id) : + is2 ++ + TUCK (ARG_VAR 0) c_arity : + EVAL (HEAP h0) (TailCall c_arity) : + [] h1 = h0 + 2 in (h1,b:bs,HEAP h0,[PUT_CLOSURE (length bs),SET_PAD]) compileArg gr st vs (QC qid) h0 bs = @@ -206,7 +218,13 @@ compileArg gr st vs e h0 bs = in (h2,bs1,HEAP h1,is1 ++ (PUT_CONSTR (i2i id) : is2)) else let h2 = h1 + 1 + n_args is2 = [SET (FREE_VAR i) | i <- [0..n_args-1]] ++ [SET (ARG_VAR (i+1)) | i <- [0..diff-1]] - b = CHECK_ARGS diff : ALLOC (c_arity+2) : PUT_CONSTR (i2i id) : is2 ++ [EVAL (HEAP h0) (TailCall diff (diff+1) (diff+1))] + b = CHECK_ARGS diff : + ALLOC (c_arity+2) : + PUT_CONSTR (i2i id) : + is2 ++ + TUCK (ARG_VAR 0) diff : + EVAL (HEAP h0) (TailCall diff) : + [] in (h2,b:bs1,HEAP h1,is1 ++ (PUT_CLOSURE (length bs):is2)) Nothing -> compileLambda gr st vs [] e h0 bs @@ -241,6 +259,28 @@ pushArgs st [] = (st,[]) pushArgs st (arg:args) = let (st1,is) = pushArgs (st+1) args in (st1, PUSH (shiftIVal st arg) : is) +tuckArgs arity st fun args = (st2,shiftIVal st2 fun',is1++is2) + where + (st2,fun',is2) = tucks st1 0 fun tas + (st1,is1) = pushArgs st pas + (tas,pas) = splitAt st args' + args' = reverse (ARG_VAR arity : args) + + tucks st i fun [] = (st,fun,[]) + tucks st i fun (arg:args) + | arg == ARG_VAR i = tucks st (i+1) fun args + | otherwise = case save st (ARG_VAR i) (fun:args) of + Just (fun:args) -> let (st1,fun',is) = tucks (st+1) (i+1) fun args + in (st1, fun', PUSH (ARG_VAR (st-i-1)) : + TUCK (shiftIVal (st+1) arg) (st-i) : is) + Nothing -> let (st1,fun',is) = tucks st (i+1) fun args + in (st1, fun', TUCK (shiftIVal st arg) (st-i-1) : is) + + save st arg0 [] = Nothing + save st arg0 (arg:args) + | arg0 == arg = Just (ARG_VAR st1 : fromMaybe args (save st arg0 args)) + | otherwise = fmap (arg :) (save st arg0 args) + setArgs st [] = [] setArgs st (arg:args) = SET (shiftIVal st arg) : setArgs st args diff --git a/src/runtime/c/pgf/data.h b/src/runtime/c/pgf/data.h index 864cf9b27..79c8dcc31 100644 --- a/src/runtime/c/pgf/data.h +++ b/src/runtime/c/pgf/data.h @@ -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; diff --git a/src/runtime/c/pgf/jit.c b/src/runtime/c/pgf/jit.c index 9886c648a..60f8d28f6 100644 --- a/src/runtime/c/pgf/jit.c +++ b/src/runtime/c/pgf/jit.c @@ -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); diff --git a/src/runtime/haskell/PGF/Binary.hs b/src/runtime/haskell/PGF/Binary.hs index 87946a7cb..6f63e63c0 100644 --- a/src/runtime/haskell/PGF/Binary.hs +++ b/src/runtime/haskell/PGF/Binary.hs @@ -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) diff --git a/src/runtime/haskell/PGF/ByteCode.hs b/src/runtime/haskell/PGF/ByteCode.hs index 6cf7daffd..9ac072378 100644 --- a/src/runtime/haskell/PGF/ByteCode.hs +++ b/src/runtime/haskell/PGF/ByteCode.hs @@ -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)