@@ -23,17 +23,16 @@ data Bytecode = BAtom BAtom
23 | BTailApp BAtom [BAtom]
23 | BTailApp BAtom [BAtom]
24 | BLazy BAtom [BAtom]
24 | BLazy BAtom [BAtom]
25 | BLet Local Bytecode Bytecode
25 | BLet Local Bytecode Bytecode
26− | BEval Local Bytecode
27 | BFCall String CType [(BAtom, CType)]
26 | BFCall String CType [(BAtom, CType)]
28 | BCon Tag [BAtom]
27 | BCon Tag [BAtom]
29 | BCase Local [BAlt]
28 | BCase Local [BAlt]
30 | BPrimOp SPrim [BAtom]
29 | BPrimOp SPrim [BAtom]
31 | BError String
30 | BError String
32− | GetArgs [Name] Bytecode -- get function arguments
31+ | BGetArgs [Name] Bytecode -- get function arguments
33− | Reserve Int Bytecode -- reserve stack space, clear on exit
32+ | BReserve Int Bytecode -- reserve stack space, clear on exit
34 deriving Show
33 deriving Show
36−data BAlt = BConCase Tag [Name] Bytecode
35+data BAlt = BConCase Tag [Name] Int Bytecode
37 | BConstCase Const Bytecode
36 | BConstCase Const Bytecode
38 | BDefaultCase Bytecode
37 | BDefaultCase Bytecode
39 deriving Show
38 deriving Show
@@ -41,12 +40,12 @@ data BAlt = BConCase Tag [Name] Bytecode
41bcdefs :: [(Name, SCDef)] -> [(Name, Bytecode)]
40bcdefs :: [(Name, SCDef)] -> [(Name, Bytecode)]
42bcdefs = map (\ (n, s) -> (n, bc s))
41bcdefs = map (\ (n, s) -> (n, bc s))
44−bc (SCDef args max c) = GetArgs (map fst args) (bcExp max (length args) c)
43+bc (SCDef args max c) = BGetArgs (map fst args) (bcExp max (length args) c)
46bcExp v arity x
45bcExp v arity x
47− = let (code, max) = runState (bc' True x) v
46+ = let (code, max) = runState (bc' True arity x) v
48 space = max - arity in
47 space = max - arity in
49− if (space > 0) then Reserve space code else code
48+ if (space > 0) then BReserve space code else code
51 ref i = do s <- get
50 ref i = do s <- get
52 when (i > s) $ put i
51 when (i > s) $ put i
@@ -54,34 +53,37 @@ bcExp v arity x
54 put (s + 1)
53 put (s + 1)
57− bc' :: Bool -> SCExp -> State Int Bytecode
56+ bc' :: Bool -> Int -> SCExp -> State Int Bytecode
58− bc' tl (SRef n) = return $ BAtom (BP n)
57+ bc' tl d (SRef n) = return $ BAtom (BP n)
59− bc' tl (SLoc i) = do ref i; return $ BAtom (BL i)
58+ bc' tl d (SLoc i) = do ref i; return $ BAtom (BL i)
60− bc' tl (SApp f args)
59+ bc' tl d (SApp f args)
61− = do f' <- bc' False f
60+ = do f' <- bc' False d f
62− args' <- mapM (bc' False) args
61+ args' <- mapM (bc' False d) args
63 let bapp = if tl then BTailApp else BApp
62 let bapp = if tl then BTailApp else BApp
64 case f' of
63 case f' of
65 BAtom r -> mkApp (\x -> bapp r x) args' []
64 BAtom r -> mkApp (\x -> bapp r x) args' []
66 bc -> do v <- next
65 bc -> do v <- next
67 mkApp (\x -> BLet v bc (bapp (BL v) x)) args' []
66 mkApp (\x -> BLet v bc (bapp (BL v) x)) args' []
68− bc' tl (SLazyApp f args) = do args' <- mapM (bc' False) args
67+ bc' tl d (SLazyApp f args) = do args' <- mapM (bc' False d) args
69− let bapp = if tl then BTailApp else BApp
68+ let bapp = if tl then BTailApp else BApp
70− mkApp (\x -> bapp (BP f) x) args' []
69+ mkApp (\x -> bapp (BP f) x) args' []
70+ bc' tl d (SLet n val sc) = do v' <- bc' False d val
71+ sc' <- bc' False (d + 1) sc
72+ return $ BLet d v' sc'
71− bc' tl (SCon t args) = do args' <- mapM (bc' False) args
73+ bc' tl d (SCon t args) = do args' <- mapM (bc' False d) args
72− mkApp (\x -> BCon t x) args' []
74+ mkApp (\x -> BCon t x) args' []
73− bc' tl (SFCall c t args) = do args' <- mapM (bc' False) (map fst args)
75+ bc' tl d (SFCall c t args) = do args' <- mapM (bc' False d) (map fst args)
74− mkFApp c t (zip args' (map snd args)) []
76+ mkFApp c t (zip args' (map snd args)) []
75− bc' tl (SConst c) = return $ BAtom (BC c)
77+ bc' tl d (SConst c) = return $ BAtom (BC c)
76− bc' tl (SError s) = return $ BError s
78+ bc' tl d (SError s) = return $ BError s
77− bc' tl (SCase e alts) = do e' <- bc' False e
79+ bc' tl d (SCase e alts) = do e' <- bc' False d e
78− alts' <- mapM (bcAlt tl) alts
80+ alts' <- mapM (bcAlt tl d) alts
79− case e' of
81+ case e' of
80− BAtom (BL i) -> return $ BCase i alts'
82+ BAtom (BL i) -> return $ BCase i alts'
81− bc -> do v <- next
83+ bc -> do v <- next
82− return $ BLet v bc (BCase v alts')
84+ return $ BLet v bc (BCase v alts')
83− bc' tl (SPrimOp p args) = do args' <- mapM (bc' tl) args
85+ bc' tl d (SPrimOp p args) = do args' <- mapM (bc' tl d) args
84− mkApp (\x -> BPrimOp p x) args' []
86+ mkApp (\x -> BPrimOp p x) args' []
86 mkApp ap [] locs = return $ ap locs
88 mkApp ap [] locs = return $ ap locs
87 mkApp ap (BAtom (BL i) : as) locs = mkApp ap as (locs ++ [BL i])
89 mkApp ap (BAtom (BL i) : as) locs = mkApp ap as (locs ++ [BL i])
@@ -89,16 +91,18 @@ bcExp v arity x
89 app <- mkApp ap as (locs ++ [BL v])
91 app <- mkApp ap as (locs ++ [BL v])
90 return $ BLet v a app
92 return $ BLet v a app
92− bcAlt tl (SConCase t args e) = do e' <- bc' tl e
94+ bcAlt tl d (SConCase t args e) = do e' <- bc' tl (d + length args) e
93− return $ BConCase t args e'
95+ return $ BConCase t args d e'
94− bcAlt tl (SConstCase i e) = do e' <- bc' tl e
96+ bcAlt tl d (SConstCase i e) = do e' <- bc' tl d e
95− return $ BConstCase i e'
97+ return $ BConstCase i e'
96− bcAlt tl (SDefaultCase e) = do e' <- bc' tl e
98+ bcAlt tl d (SDefaultCase e) = do e' <- bc' tl d e
97− return $ BDefaultCase e'
99+ return $ BDefaultCase e'
99 mkFApp c ty [] locs = return $ BFCall c ty locs
101 mkFApp c ty [] locs = return $ BFCall c ty locs
100− mkFApp c ty ((BAtom (BL i), t) : as) locs = mkFApp c ty as (locs ++ [(BL i, t)])
102+ mkFApp c ty ((BAtom (BL i), t) : as) locs
103+ = mkFApp c ty as (locs ++ [(BL i, t)])
101− mkFApp c ty ((a, t) : as) locs = do v <- next
104+ mkFApp c ty ((a, t) : as) locs
102− app <- mkFApp c ty as (locs ++ [(BL v, t)])
106+ app <- mkFApp c ty as (locs ++ [(BL v, t)])
103− return $ BLet v a app
107+ return $ BLet v a app