-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPufCompiler.hs
200 lines (167 loc) · 7 KB
/
PufCompiler.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
module PufCompiler {-(compileAST)-} where
import PufAST
import PufCompilerTypes
import Control.Monad.State
compileAST :: AST String -> String
compileAST ast = pprint.elimSpaghetti.instListToList $ codegen ast
instListToList :: InstList -> [InstList]
instListToList (Cat il1 il2) = instListToList il1 ++ instListToList il2
instListToList i = [i]
pprint :: [InstList] -> String
pprint = unlines.map printInstruction
elimSpaghetti :: [InstList] -> [InstList]
elimSpaghetti (Mkclos a:Jump b:rest)
= let (body,(_:end)) = break (== Label b) rest
in Mkclos a : elimSpaghetti (end ++ body)
elimSpaghetti (Mkfunval a:Jump b:rest)
= let (body,(_:end)) = break (== Label b) rest
in Mkfunval a : elimSpaghetti (end ++ body)
elimSpaghetti (x:xs) = x:elimSpaghetti xs
elimSpaghetti [] = []
codegen ast = evalState (codeV ast initialEnvironment 0) initialState <> Halt
getNextLabel = do (CompilerState l) <- get
put (CompilerState (l+1))
return l
getvar :: String -> Environment -> Int -> Instruction
getvar name e sd = case envGet name e of
(Local,i) -> Pushloc (sd-i)
(Global,i) -> Pushglob i
codeB :: AST String -> Environment -> Int -> State CompilerState InstList
codeV :: AST String -> Environment -> Int -> State CompilerState InstList
codeC :: AST String -> Environment -> Int -> State CompilerState InstList
codeB (Num i) _ _ = return (Loadc i)
codeB (App (Prim op) e) p sd
= do exp <- codeB e p sd
return (exp <> builtinInst op)
codeB (App (App (Prim op) e1) e2) p sd
= do exp1 <- codeB e1 p sd
exp2 <- codeB e2 p (sd+1)
return (exp1 <> exp2 <> builtinInst op)
codeB (Cond cond e1 e2) p sd
= do cxp <- codeB cond p sd
la <- getNextLabel
ex1 <- codeB e1 p sd
lb <- getNextLabel
ex2 <- codeB e2 p sd
return (cxp <> Jumpz la <> ex1 <> Jump lb <> Label la
<> ex2 <> Label lb)
codeB e p sd = do exp <- codeV e p sd
return $ exp <> Getbasic
codeV (Num i) _ _ = return (Loadc i <> Mkbasic)
codeV (Var a) e sd = return (getvar a e sd <> Eval)
codeV ex@(App (Prim _) _) p sd
= do exp <- codeB ex p sd
return (exp <> Mkbasic)
codeV ex@(App (App (Prim _) _) _) p sd
= do exp <- codeB ex p sd
return (exp <> Mkbasic)
codeV (Cond cond e1 e2) p sd
= do cxp <- codeB cond p sd
la <- getNextLabel
ex1 <- codeV e1 p sd
lb <- getNextLabel
ex2 <- codeV e2 p sd
return (cxp <> Jumpz la <> ex1 <> Jump lb <> Label la
<> ex2 <> Label lb)
codeV fn@(Fun _ _) p sd
= do let z = free(fn)
(x,e) = getLambdaSplit fn
g = length z
k = length x
vars = zipWith (\x i -> getvar x p (sd+i)) z [0..(g-1)]
p' = (foldl envAdd initialEnvironment
(zipWith (\x i -> (x,(Local,-i))) x [0..(k-1)]))
`envUnion`
(foldl envAdd initialEnvironment
(zipWith (\z j -> (z,(Global,j))) z [0..(g-1)]))
la <- getNextLabel
exp <- codeV e p' 0
lb <- getNextLabel
return ( (if null vars then Mkvec g else foldl1 Cat vars <> Mkvec g)
<> Mkfunval la <> Jump lb <> Label la <> Targ k <> exp
<> Return k <> Label lb)
where getLambdaSplit (Fun n e) = let (a,b) = getLambdaSplit e
in (n:a,b)
getLambdaSplit e = ([],e)
codeV ap@(App _ _) p sd
= do let (e':es) = apps ap
e = reverse es
m = length e
la <- getNextLabel
exps <- zipWithM (\e i -> codeC e p (sd + i)) e [3..(m+2)]
ex <- codeV e' p (sd + m + 3)
return $ Mark la <> foldl1 Cat exps <> ex <> Apply <> Label la
where apps (App ap@(App _ _) e) = apps ap++[e]
apps (App e1 e2) = e1:e2:[]
apps _ = []
codeV (Let [Tdecl ys e1] e0) p sd
= do let k = length ys
p' = foldl envAdd p (
zipWith (\y i -> (y,(Local,sd+i))) ys
[0..(k-1)])
ex1 <- codeV e1 p sd
ex0 <- codeV e0 p' (sd+k-1)
return $ ex1 <> Getvec k <> ex0 <> Slide k
codeV (Let ldecls e0) p sd
= do let n = length ldecls
ps = scanl envUnion p (zipWith
(\l j -> case l of
Sdecl n _ -> envSingle n (Local,sd+j)
Tdecl ns _ -> undefined
) ldecls [1..(n)])
exps <- zipWithM
(\d (env,i) -> case d of
Sdecl _ e -> codeC e env (sd+i)
Tdecl _ e -> codeC e env (sd+i)
) ldecls (zip ps [0..(n-1)])
ex <- codeV e0 (last ps) (sd+n)
return $ foldl1 Cat exps <> ex <> Slide n
codeV (Rec rdecls e0) p sd
= do let n = length rdecls
p' = foldl envAdd p (map (\((Rdecl name _),i) ->
(name,(Local,sd+i))) (zip rdecls [1..n]))
exps <- forM rdecls (\(Rdecl name ei) ->
codeC ei p' (sd+n))
exp0 <- codeV e0 p' (sd+n)
return $
foldl Cat (Alloc n)
(zipWith (\e i -> e <> Rewrite i) exps [n,(n-1)..1])
<> exp0 <> Slide n
codeV (Tuple es) p sd
= do let k = length es
exprs = zipWithM (\e i -> codeC e p (sd+i)) es [0..k]
exs <- exprs
return $ foldl1 Cat exs <> Mkvec k
codeV (Select j e) p sd
= do ex <- codeV e p sd
return $ ex <> Get j <> Eval
codeV (PufAST.Nil) _ _ = return PufCompilerTypes.Nil
codeV (PufAST.Cons e1 e2) p sd
= do ex1 <- codeC e1 p sd
ex2 <- codeC e2 p (sd+1)
return $ ex1 <> ex2 <> PufCompilerTypes.Cons
codeV (Case e0 e1 h t e2) p sd
= do let p' = envUnion p (envSingle h (Local,sd+1)) `envUnion`
envSingle t (Local,sd+2)
ex0 <- codeV e0 p sd
la <- getNextLabel
ex1 <- codeV e1 p sd
lb <- getNextLabel
ex2 <- codeV e2 p' (sd+2)
return $ ex0 <> Tlist la <> ex1 <> Jump lb <> Label la
<> ex2 <> Slide 2 <> Label lb
codeC (b@(Num _)) p sd = codeV b p sd
codeC (fn@(Fun _ _)) p sd = codeV fn p sd
codeC e p sd = do let z = free(e)
g = length z
iRange = [0..(g-1)]
vars = map (\(x,i) -> getvar x p (sd+i)) (zip z iRange)
p' = foldl envAdd initialEnvironment (
zipWith (\n i -> (n,(Global,i))) z iRange )
la <- getNextLabel
exp <- codeV e p' 0
lb <- getNextLabel
return ((if null vars then Mkvec g
else foldl1 Cat vars <> Mkvec g)
<> Mkclos la <> Jump lb <> Label la
<> exp <> Update <> Label lb)