diff --git a/src/compiler/GF/Compile/GenerateBC.hs b/src/compiler/GF/Compile/GenerateBC.hs index d90e78ee7..d8edac270 100644 --- a/src/compiler/GF/Compile/GenerateBC.hs +++ b/src/compiler/GF/Compile/GenerateBC.hs @@ -33,7 +33,7 @@ compileEquations gr arity st (i:is) eqs fl bs = whilePP eqs Map.empty (cn:cns) -> let (bs1,instrs1) = compileBranch0 fl bs cn bs2 = foldl (compileBranch fl) bs1 cns bs3 = mkFail arity st fl : bs2 - in (bs3,EVAL (shiftIVal st i) RecCall : instrs1) + in (bs3,[PUSH_FRAME, EVAL (shiftIVal (st+2) i) RecCall] ++ instrs1) whilePP ((vs, PP c ps' : ps, t):eqs) cns = whilePP eqs (Map.insertWith (++) (Q c,length ps') [(vs,ps'++ps,t)] cns) whilePP ((vs, PInt n : ps, t):eqs) cns = whilePP eqs (Map.insertWith (++) (EInt n,0) [(vs,ps,t)] cns) whilePP ((vs, PString s: ps, t):eqs) cns = whilePP eqs (Map.insertWith (++) (K s,0) [(vs,ps,t)] cns) @@ -47,7 +47,7 @@ compileEquations gr arity st (i:is) eqs fl bs = whilePP eqs Map.empty (bs1,instrs1) = compileBranch0 fl1 bs cn bs2 = foldl (compileBranch fl1) bs1 cns (bs3,instrs3) = compileEquations gr arity st (i:is) eqs fl (instrs3:bs2) - in (bs3,EVAL (shiftIVal st i) RecCall : instrs1) + in (bs3,[PUSH_FRAME, EVAL (shiftIVal (st+2) i) RecCall] ++ instrs1) whilePV [] vrs = compileEquations gr arity st is vrs fl bs whilePV ((vs, PV x : ps, t):eqs) vrs = whilePV eqs (((x,i):vs,ps,t) : vrs) @@ -83,22 +83,27 @@ mkFail arity st1 (Just (st0,l)) | otherwise = [JUMP l] compileBody gr arity st vs e bs = - let (heap,bs1,is) = compileFun gr arity st vs e 0 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)] + (heap,bs1,is) = compileFun gr eval st vs e 0 bs [] in (bs1,if heap > 0 then (ALLOC heap : is) else is) -compileFun gr arity st vs (Abs _ x e) h0 bs args = +compileFun gr eval st vs (Abs _ x e) h0 bs args = let (h1,bs1,arg,is1) = compileLambda gr st vs [x] e h0 bs - (st1,is3) = pushArgs st (reverse args) - in (h1,bs1,is1++is3++[EVAL arg (if arity == 0 then (UpdateCall st st1) else (TailCall arity st st1))]) -compileFun gr arity st vs (App e1 e2) h0 bs args = + in (h1,bs1,is1++eval arg args) +compileFun gr eval st vs (App e1 e2) h0 bs args = let (h1,bs1,arg,is1) = compileArg gr st vs e2 h0 bs - (h2,bs2,is2) = compileFun gr arity st vs e1 h1 bs1 (arg:args) + (h2,bs2,is2) = compileFun gr eval st vs e1 h1 bs1 (arg:args) in (h2,bs2,is1++is2) -compileFun gr arity st vs (Q (m,id)) h0 bs args = +compileFun gr eval st vs (Q (m,id)) h0 bs args = case lookupAbsDef gr m id of Ok (_,Just _) - -> let (st1,is1) = pushArgs st (reverse args) - in (h0,bs,is1++[EVAL (GLOBAL (i2i id)) (if arity == 0 then (UpdateCall st st1) else (TailCall arity st st1))]) + -> (h0,bs,eval (GLOBAL (i2i id)) args) _ -> let Ok ty = lookupFunType gr m id (ctxt,_,_) = typeForm ty c_arity = length ctxt @@ -107,40 +112,41 @@ compileFun gr arity st vs (Q (m,id)) h0 bs args = diff = c_arity-n_args in if diff <= 0 then if n_args == 0 - then (h0,bs,[EVAL (GLOBAL (i2i id)) (if arity == 0 then (UpdateCall st st) else (TailCall arity st st))]) + then (h0,bs,eval (GLOBAL (i2i id)) []) else let h1 = h0 + 2 + n_args - in (h1,bs,PUT_CONSTR (i2i id):is1++[EVAL (HEAP h0) (if arity == 0 then (UpdateCall st st) else (TailCall arity st st))]) + 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))] - in (h1,b:bs,PUT_CLOSURE (length bs):is1++[EVAL (HEAP h0) (if arity == 0 then (UpdateCall st st) else (TailCall arity st st))]) -compileFun gr arity st vs (QC qid) h0 bs args = - compileFun gr arity st vs (Q qid) h0 bs args -compileFun gr arity st vs (Vr x) h0 bs args = - let (st1,is1) = pushArgs st (reverse args) - arg = (shiftIVal st1 . getVar vs) x - in (h0,bs,is1++[EVAL arg (if arity == 0 then (UpdateCall st st1) else (TailCall arity st st1))]) -compileFun gr arity st vs (EInt n) h0 bs _ = + 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 +compileFun gr eval st vs (Vr x) h0 bs args = + (h0,bs,eval (getVar vs x) args) +compileFun gr eval st vs (EInt n) h0 bs _ = let h1 = h0 + 2 - in (h1,bs,[PUT_LIT (LInt n), EVAL (HEAP h0) (if arity == 0 then (UpdateCall st st) else (TailCall arity st st))]) -compileFun gr arity st vs (K s) h0 bs _ = + in (h1,bs,PUT_LIT (LInt n) : eval (HEAP h0) []) +compileFun gr eval st vs (K s) h0 bs _ = let h1 = h0 + 2 - in (h1,bs,[PUT_LIT (LStr s), EVAL (HEAP h0) (if arity == 0 then (UpdateCall st st) else (TailCall arity st st))]) -compileFun gr arity st vs (EFloat d) h0 bs _ = + in (h1,bs,PUT_LIT (LStr s) : eval (HEAP h0) []) +compileFun gr eval st vs (EFloat d) h0 bs _ = let h1 = h0 + 2 - in (h1,bs,[PUT_LIT (LFlt d), EVAL (HEAP h0) (if arity == 0 then (UpdateCall st st) else (TailCall arity st st))]) -compileFun gr arity st vs (Typed e _) h0 bs args = - compileFun gr arity st vs e h0 bs args -compileFun gr arity st vs (Let (x, (_, e1)) e2) h0 bs args = + in (h1,bs,PUT_LIT (LFlt d) : eval (HEAP h0) []) +compileFun gr eval st vs (Typed e _) h0 bs args = + compileFun gr eval st vs e h0 bs args +compileFun gr eval st vs (Let (x, (_, e1)) e2) h0 bs args = let (h1,bs1,arg,is1) = compileLambda gr st vs [] e1 h0 bs - (h2,bs2,is2) = compileFun gr arity st ((x,arg):vs) e2 h1 bs1 args + (h2,bs2,is2) = compileFun gr eval st ((x,arg):vs) e2 h1 bs1 args in (h2,bs2,is1++is2) -compileFun gr arity st vs (Glue e1 e2) h0 bs args = - let (h1,bs1,arg1,is1) = compileArg gr st vs e1 h0 bs - (h2,bs2,arg2,is2) = compileArg gr st vs e2 h1 bs1 - (st1,is3) = pushArgs st [arg2,arg1] - in (h2,bs2,is1++is2++is3++[ADD]) -compileFun gr arity st vs e _ _ _ = error (show e) +compileFun gr eval st vs (Glue e1 e2) h0 bs args = + let eval' fun args = [PUSH_FRAME]++is++[EVAL fun' RecCall] + where + (st1,is) = pushArgs (st+2) (reverse args) + fun' = shiftIVal st fun + (h1,bs1,is1) = compileFun gr eval' st vs e1 h0 bs args + (h2,bs2,is2) = compileFun gr eval' st vs e2 h1 bs1 args + in (h2,bs2,is1++is2++[ADD]) +compileFun gr eval st vs e _ _ _ = error (show e) compileArg gr st vs (Q(m,id)) h0 bs = case lookupAbsDef gr m id of diff --git a/src/runtime/c/pgf/data.h b/src/runtime/c/pgf/data.h index 2b11f24da..154679ac8 100644 --- a/src/runtime/c/pgf/data.h +++ b/src/runtime/c/pgf/data.h @@ -133,12 +133,13 @@ typedef enum { PGF_INSTR_PUT_LIT = 6, PGF_INSTR_SET = 7, PGF_INSTR_SET_PAD = 8, - PGF_INSTR_PUSH = 9, - PGF_INSTR_EVAL = 10, - PGF_INSTR_DROP = 13, - PGF_INSTR_JUMP = 14, - PGF_INSTR_FAIL = 15, - PGF_INSTR_ADD = 16 + PGF_INSTR_PUSH_FRAME = 9, + PGF_INSTR_PUSH = 10, + PGF_INSTR_EVAL = 11, + PGF_INSTR_DROP = 14, + PGF_INSTR_JUMP = 15, + PGF_INSTR_FAIL = 16, + PGF_INSTR_ADD = 17, } PgfInstruction; typedef GuSeq PgfConcrs; diff --git a/src/runtime/c/pgf/jit.c b/src/runtime/c/pgf/jit.c index 0b42cc91f..dbb441454 100644 --- a/src/runtime/c/pgf/jit.c +++ b/src/runtime/c/pgf/jit.c @@ -937,6 +937,15 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr, curr_offset++; break; } + case PGF_INSTR_PUSH_FRAME: { +#ifdef PGF_JIT_DEBUG + gu_printf(out, err, "PUSH_FRAME\n"); +#endif + jit_pushr_p(JIT_VCLOS); + jit_pushr_p(JIT_FP); + jit_movr_p(JIT_FP, JIT_SP); + break; + } case PGF_INSTR_PUSH: { switch (mod) { case 0: { @@ -990,7 +999,6 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr, } case PGF_INSTR_EVAL+0: case PGF_INSTR_EVAL+2: - jit_movr_p(JIT_R2, JIT_VCLOS); case PGF_INSTR_EVAL+1: { switch (mod) { case 0: { @@ -1039,9 +1047,6 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr, #ifdef PGF_JIT_DEBUG gu_printf(out, err, "\n"); #endif - jit_pushr_p(JIT_R2); - jit_pushr_p(JIT_FP); - jit_movr_p(JIT_FP, JIT_SP); jit_callr(JIT_R0); jit_popr_p(JIT_FP); jit_popr_p(JIT_VCLOS); @@ -1072,28 +1077,15 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr, gu_printf(out, err, " update(%d,%d)\n", b, c); #endif - if (b >= 3) { - jit_stxi_p(sizeof(PgfClosure*)*(c-3), JIT_SP, JIT_FP); - jit_stxi_p(sizeof(PgfClosure*)*(c-2), JIT_SP, JIT_R2); + 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-4)-i), JIT_SP, JIT_R1); + 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-3)*sizeof(PgfClosure*)); - } else { - jit_subi_p(JIT_SP, JIT_SP, (3-b)*sizeof(PgfClosure*)); - for (size_t i = 0; i < c-b; i++) { - jit_ldxi_p(JIT_R1, JIT_SP, sizeof(PgfClosure*)*(i+(3-b))); - jit_stxi_p(sizeof(PgfClosure*)*i, JIT_SP, JIT_R1); - } - jit_stxi_p(sizeof(PgfClosure*)*(c-b), JIT_SP, JIT_FP); - jit_stxi_p(sizeof(PgfClosure*)*(c-b+1), JIT_SP, JIT_R2); + jit_addi_p(JIT_SP, JIT_SP, (b-1)*sizeof(PgfClosure*)); } - - jit_addi_p(JIT_FP, JIT_SP, sizeof(PgfClosure*)*(c-b)); jit_movi_p(JIT_R1, abstr->eval_gates->update_closure); jit_pushr_p(JIT_R1); - jit_jmpr(JIT_R0); break; } diff --git a/src/runtime/haskell/PGF/Binary.hs b/src/runtime/haskell/PGF/Binary.hs index 93214038c..3dcf9e163 100644 --- a/src/runtime/haskell/PGF/Binary.hs +++ b/src/runtime/haskell/PGF/Binary.hs @@ -144,34 +144,35 @@ instance Binary Instr where put (ALLOC n) = putWord8 12 >> put n put (PUT_CONSTR id) = putWord8 16 >> put id put (PUT_CLOSURE l) = putWord8 20 >> put l - put (PUT_LIT (LInt n)) = putWord8 24 >> put n - put (PUT_LIT (LStr s)) = putWord8 25 >> put s - put (PUT_LIT (LFlt d)) = putWord8 26 >> put d + put (PUT_LIT (LInt n)) = putWord8 24 >> put n + put (PUT_LIT (LStr s)) = putWord8 25 >> put s + put (PUT_LIT (LFlt d)) = putWord8 26 >> put d put (SET (HEAP n)) = putWord8 28 >> put n put (SET (ARG_VAR n)) = putWord8 29 >> put n put (SET (FREE_VAR n)) = putWord8 30 >> put n put (SET (GLOBAL id)) = putWord8 31 >> put id put (SET_PAD ) = putWord8 32 - put (PUSH (HEAP n)) = putWord8 36 >> put n - put (PUSH (ARG_VAR n)) = putWord8 37 >> put n - put (PUSH (FREE_VAR n)) = putWord8 38 >> put n - put (PUSH (GLOBAL id)) = putWord8 39 >> put id - put (EVAL (HEAP n) (RecCall )) = putWord8 40 >> put n - put (EVAL (ARG_VAR n) (RecCall )) = putWord8 41 >> put n - put (EVAL (FREE_VAR n) (RecCall )) = putWord8 42 >> put n - put (EVAL (GLOBAL id) (RecCall )) = putWord8 43 >> put id - put (EVAL (HEAP n) (TailCall a b c)) = putWord8 44 >> put n >> put (a,b,c) - put (EVAL (ARG_VAR n) (TailCall a b c)) = putWord8 45 >> put n >> put (a,b,c) - put (EVAL (FREE_VAR n) (TailCall a b c)) = putWord8 46 >> put n >> put (a,b,c) - put (EVAL (GLOBAL id) (TailCall a b c)) = putWord8 47 >> put id >> put (a,b,c) - put (EVAL (HEAP n) (UpdateCall b c)) = putWord8 48 >> put n >> put (b,c) - put (EVAL (ARG_VAR n) (UpdateCall b c)) = putWord8 49 >> put n >> put (b,c) - put (EVAL (FREE_VAR n) (UpdateCall b c)) = putWord8 50 >> put n >> put (b,c) - put (EVAL (GLOBAL id) (UpdateCall b c)) = putWord8 51 >> put id >> put (b,c) - put (DROP n ) = putWord8 52 >> put n - put (JUMP l ) = putWord8 56 >> put l - put (FAIL ) = putWord8 60 - put (ADD ) = putWord8 64 + put (PUSH_FRAME ) = putWord8 36 + put (PUSH (HEAP n)) = putWord8 40 >> put n + put (PUSH (ARG_VAR n)) = putWord8 41 >> put n + put (PUSH (FREE_VAR n)) = putWord8 42 >> put n + put (PUSH (GLOBAL id)) = putWord8 43 >> put id + put (EVAL (HEAP n) (RecCall )) = putWord8 44 >> put n + put (EVAL (ARG_VAR n) (RecCall )) = putWord8 45 >> put n + put (EVAL (FREE_VAR n) (RecCall )) = putWord8 46 >> put n + put (EVAL (GLOBAL id) (RecCall )) = putWord8 47 >> put id + put (EVAL (HEAP n) (TailCall a b c)) = putWord8 48 >> put n >> put (a,b,c) + put (EVAL (ARG_VAR n) (TailCall a b c)) = putWord8 49 >> put n >> put (a,b,c) + put (EVAL (FREE_VAR n) (TailCall a b c)) = putWord8 50 >> put n >> put (a,b,c) + put (EVAL (GLOBAL id) (TailCall a b c)) = putWord8 51 >> put id >> put (a,b,c) + put (EVAL (HEAP n) (UpdateCall b c)) = putWord8 52 >> put n >> put (b,c) + put (EVAL (ARG_VAR n) (UpdateCall b c)) = putWord8 53 >> put n >> put (b,c) + put (EVAL (FREE_VAR n) (UpdateCall b c)) = putWord8 54 >> put n >> put (b,c) + put (EVAL (GLOBAL id) (UpdateCall b c)) = putWord8 55 >> put id >> put (b,c) + put (DROP n ) = putWord8 56 >> put n + put (JUMP l ) = putWord8 60 >> put l + put (FAIL ) = putWord8 64 + put (ADD ) = putWord8 68 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 46f81cff9..f4aebfd92 100644 --- a/src/runtime/haskell/PGF/ByteCode.hs +++ b/src/runtime/haskell/PGF/ByteCode.hs @@ -24,6 +24,7 @@ data Instr | PUT_LIT Literal | SET IVal | SET_PAD + | PUSH_FRAME | PUSH IVal | EVAL IVal TailInfo | DROP {-# UNPACK #-} !Int @@ -59,6 +60,7 @@ ppInstr (PUT_CLOSURE l) = text "PUT_CLOSURE" <+> ppLabel l ppInstr (PUT_LIT lit ) = text "PUT_LIT " <+> ppLit lit ppInstr (SET v) = text "SET " <+> ppIVal v 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 (DROP n ) = text "DROP " <+> int n