@@ -1,3 +1,5 @@
1+{-# LANGUAGE PatternGuards #-}
1module IRTS.Defunctionalise(module IRTS.Defunctionalise,
3module IRTS.Defunctionalise(module IRTS.Defunctionalise,
2 module IRTS.Lang) where
4 module IRTS.Lang) where
@@ -42,8 +44,9 @@ defunctionalise nexttag defs
42 = let all = toAlist defs
44 = let all = toAlist defs
43 -- sort newcons so that EVAL and APPLY cons get sequential tags
45 -- sort newcons so that EVAL and APPLY cons get sequential tags
44 (allD, (enames, anames)) = runState (mapM (addApps defs) all) ([], [])
46 (allD, (enames, anames)) = runState (mapM (addApps defs) all) ([], [])
47+ anames' = sort anames
45 newecons = sortBy conord $ concatMap (toCons enames) (getFn all)
48 newecons = sortBy conord $ concatMap (toCons enames) (getFn all)
46− newacons = sortBy conord $ concatMap (toCons anames) (getFn all)
49+ newacons = sortBy conord $ concatMap (toConsA anames') (getFn all)
47 eval = mkEval newecons
50 eval = mkEval newecons
48 app = mkApply newacons
51 app = mkApply newacons
49 condecls = declare nexttag (newecons ++ newacons) in
52 condecls = declare nexttag (newecons ++ newacons) in
@@ -68,14 +71,14 @@ getFn xs = mapMaybe fnData xs
68-- 7 Wrap unknown applications (i.e. applications of local variables) in chains of APPLY
71-- 7 Wrap unknown applications (i.e. applications of local variables) in chains of APPLY
69-- 8 Add explicit EVAL to case, primitives, and foreign calls
72-- 8 Add explicit EVAL to case, primitives, and foreign calls
71−addApps :: LDefs -> (Name, LDecl) -> State ([Name], [Name]) (Name, DDecl)
74+addApps :: LDefs -> (Name, LDecl) -> State ([Name], [(Name, Int)]) (Name, DDecl)
72addApps defs o@(n, LConstructor _ t a)
75addApps defs o@(n, LConstructor _ t a)
73 = return (n, DConstructor n t a)
76 = return (n, DConstructor n t a)
74addApps defs (n, LFun _ _ args e)
77addApps defs (n, LFun _ _ args e)
75 = do e' <- aa args e
78 = do e' <- aa args e
76 return (n, DFun n args e')
79 return (n, DFun n args e')
78− aa :: [Name] -> LExp -> State ([Name], [Name]) DExp
81+ aa :: [Name] -> LExp -> State ([Name], [(Name, Int)]) DExp
79 aa env (LV (Glob n)) | n `elem` env = return $ DV (Glob n)
82 aa env (LV (Glob n)) | n `elem` env = return $ DV (Glob n)
80 | otherwise = aa env (LApp False (LV (Glob n)) [])
83 | otherwise = aa env (LApp False (LV (Glob n)) [])
81 aa env (LApp tc (LV (Glob n)) args)
84 aa env (LApp tc (LV (Glob n)) args)
@@ -125,7 +128,8 @@ addApps defs (n, LFun _ _ args e)
125 = return $ DApp tc n args
128 = return $ DApp tc n args
126 | length args < ar
129 | length args < ar
127 = do (ens, ans) <- get
130 = do (ens, ans) <- get
131+ let alln = map (\x -> (n, x)) [length args .. ar]
128− put (ens, nub (n : ans))
132+ put (ens, nub (alln ++ ans))
129 return $ DApp tc (mkUnderCon n (ar - length args)) args
133 return $ DApp tc (mkUnderCon n (ar - length args)) args
130 | length args > ar
134 | length args > ar
131 = return $ chainAPPLY (DApp tc n (take ar args)) (drop ar args)
135 = return $ chainAPPLY (DApp tc n (take ar args)) (drop ar args)
@@ -137,7 +141,8 @@ addApps defs (n, LFun _ _ args e)
137 return $ DApp False (mkFnCon n) args
141 return $ DApp False (mkFnCon n) args
138 | length args < ar
142 | length args < ar
139 = do (ens, ans) <- get
143 = do (ens, ans) <- get
144+ let alln = map (\x -> (n, x)) [length args .. ar]
140− put (ens, nub (n : ans))
145+ put (ens, nub (alln ++ ans))
141 return $ DApp False (mkUnderCon n (ar - length args)) args
146 return $ DApp False (mkUnderCon n (ar - length args)) args
142 | length args > ar
147 | length args > ar
143 = return $ chainAPPLY (DApp False n (take ar args)) (drop ar args)
148 = return $ chainAPPLY (DApp False n (take ar args)) (drop ar args)
@@ -187,7 +192,19 @@ toCons ns (n, i)
187 (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
192 (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
188 (dupdate tlarg
193 (dupdate tlarg
189 (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
194 (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
190− : mkApplyCase n 0 i
195+ : [] -- mkApplyCase n 0 i
197+ where dupdate tlarg x = x
199+toConsA :: [(Name, Int)] -> (Name, Int) -> [(Name, Int, EvalApply DAlt)]
201+ | Just ar <- lookup n ns
203+-- EvalCase (\tlarg ->
204+-- (DConCase (-1) (mkFnCon n) (take i (genArgs 0))
206+-- (DApp False n (map (DV . Glob) (take i (genArgs 0))))))))
207+ = mkApplyCase n ar i
191 | otherwise = []
208 | otherwise = []
192 where dupdate tlarg x = x
209 where dupdate tlarg x = x