-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSemant.hs
More file actions
334 lines (295 loc) · 11.8 KB
/
Copy pathSemant.hs
File metadata and controls
334 lines (295 loc) · 11.8 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Microc.Semant
( checkProgram
)
where
import Microc.Ast
import Microc.Sast
import Microc.Semant.Error
import Microc.Semant.Analysis
import Microc.Utils
import qualified Data.Map as M
import Control.Monad.State
import Control.Monad.Except
import Data.Maybe ( isJust )
import Data.Text ( Text )
import Data.List ( find
, findIndex
)
type Vars = M.Map (Text, VarKind) Type
type Funcs = M.Map Text Function
type Structs = [Struct]
data Env = Env { vars :: Vars
, funcs :: Funcs
, structs :: Structs
}
type Semant = ExceptT SemantError (State Env)
checkBinds :: VarKind -> BindingLoc -> [Bind] -> Semant [Bind]
checkBinds kind loc binds = do
forM binds $ \case
Bind TyVoid name -> throwError $ IllegalBinding name Void kind loc
Bind ty name -> do
vars <- gets vars
when (M.member (name, kind) vars)
$ throwError (IllegalBinding name Duplicate kind loc)
modify $ \env -> env { vars = M.insert (name, kind) ty vars }
pure $ Bind ty name
checkFields :: Struct -> Semant Struct
checkFields s@(Struct name fields) = do
fields' <- foldM addField M.empty fields
pure $ Struct name (M.elems fields') -- this doesn't preserve ordering
where
addField acc field@(Bind t name) = case t of
TyVoid -> throwError $ IllegalBinding name Void StructField (S s)
_ -> if M.member name acc
then throwError (IllegalBinding name Duplicate StructField (S s))
else pure $ M.insert name field acc
builtIns :: Funcs
builtIns = M.fromList $ map
toFunc
[ ("printf" , [Pointer TyChar], TyVoid)
, ("printbig", [TyInt] , TyVoid)
, ("malloc" , [TyInt] , Pointer TyVoid)
, ("free" , [Pointer TyVoid], TyVoid)
]
where
toFunc (name, tys, retty) =
(name, Function retty name (map (flip Bind "x") tys) [] [])
checkExpr :: Expr -> Semant SExpr
checkExpr expr = case expr of
Literal i -> pure (TyInt, SLiteral i)
Fliteral f -> pure (TyFloat, SFliteral f)
BoolLit b -> pure (TyBool, SBoolLit b)
CharLit c -> pure (TyChar, SCharLit c)
StrLit s -> pure (Pointer TyChar, SStrLit s)
Sizeof t -> pure (TyInt, SSizeof t)
Null -> pure (Pointer TyVoid, SNull)
Noexpr -> pure (TyVoid, SNoexpr)
Id s -> do
vars <- gets vars
let foundVars = map (\kind -> M.lookup (s, kind) vars) [Local, Formal, Global]
case join $ find isJust foundVars of
Nothing -> throwError $ UndefinedSymbol s Var expr
Just ty -> pure (ty, LVal $ SId s)
Binop op lhs rhs -> do
lhs'@(t1, _) <- checkExpr lhs
rhs'@(t2, _) <- checkExpr rhs
let assertSym = unless (t1 == t2) $ throwError $ TypeError [t1] t2 (Expr expr)
checkArith = do
unless (isNumeric t1)
$ throwError (TypeError [TyInt, TyFloat] t1 (Expr expr))
pure (t1, SBinop op lhs' rhs')
checkBool = do
unless (t1 == TyBool) (throwError $ TypeError [TyBool] t1 (Expr expr))
pure (t1, SBinop op lhs' rhs')
case op of
Add ->
let sexpr = SBinop Add lhs' rhs'
in
case (t1, t2) of
(Pointer t, TyInt ) -> pure (Pointer t, sexpr)
(TyInt , Pointer t) -> pure (Pointer t, sexpr)
(TyInt , TyInt ) -> pure (TyInt, sexpr)
(TyFloat , TyFloat ) -> pure (TyFloat, sexpr)
_ ->
throwError $ TypeError [Pointer TyVoid, TyInt, TyFloat] t1 (Expr expr)
Sub ->
let sexpr = SBinop Sub lhs' rhs'
in
case (t1, t2) of
(Pointer t, TyInt ) -> pure (Pointer t, sexpr)
(TyInt , Pointer t ) -> pure (Pointer t, sexpr)
(Pointer t, Pointer t') -> if t == t'
then pure (TyInt, sexpr)
else throwError $ TypeError [Pointer t'] (Pointer t) (Expr expr)
(TyInt , TyInt ) -> pure (TyInt, sexpr)
(TyFloat, TyFloat) -> pure (TyFloat, sexpr)
_ -> throwError
$ TypeError [Pointer TyVoid, TyInt, TyFloat] t1 (Expr expr)
Mult -> assertSym >> checkArith
Div -> assertSym >> checkArith
BitAnd -> assertSym >> checkArith
BitOr -> assertSym >> checkArith
And -> assertSym >> checkBool
Or -> assertSym >> checkBool
Power -> case (t1, t2) of
(TyFloat, TyFloat) -> pure (TyFloat, SCall "llvm.pow.f64" [lhs', rhs'])
(TyFloat, TyInt ) -> pure (TyFloat, SCall "llvm.powi.f64" [lhs', rhs'])
-- Implement this case directly in llvm
(TyInt , TyInt ) -> pure (TyInt, SBinop Power lhs' rhs')
_ -> throwError $ TypeError [TyFloat, TyInt] t1 (Expr expr)
relational -> case (snd lhs', snd rhs') of
(SNull, _ ) -> checkExpr (Binop relational (Cast t1 lhs) rhs)
(_ , SNull) -> checkExpr (Binop relational lhs (Cast t1 rhs))
_ -> do
assertSym
unless (isNumeric t1)
$ throwError (TypeError [TyInt, TyFloat] t1 (Expr expr))
pure (TyBool, SBinop op lhs' rhs')
Unop op e -> do
e'@(ty, _) <- checkExpr e
case op of
Neg -> do
unless (isNumeric ty)
$ throwError (TypeError [TyInt, TyFloat] ty (Expr expr))
pure (ty, SUnop Neg e')
Not -> do
unless (ty == TyBool) $ throwError $ TypeError [TyBool] ty (Expr expr)
pure (ty, SUnop Not e')
Addr e -> do
(t, e') <- checkExpr e
case e' of
LVal l -> pure (Pointer t, SAddr l)
_ -> throwError (AddressError e)
Deref e -> do
(ty, e') <- checkExpr e
case ty of
Pointer t -> pure (t, LVal $ SDeref (ty, e'))
_ -> throwError
$ TypeError [Pointer TyVoid, Pointer TyInt, Pointer TyFloat] ty (Expr expr)
Call "printf" es -> do
es' <- mapM checkExpr es
let (formatStr, _) = head es'
unless (formatStr == Pointer TyChar)
$ throwError (TypeError [Pointer TyChar] formatStr (Expr expr))
pure (TyVoid, SCall "printf" es')
Call s es -> do
funcs <- gets funcs
case M.lookup s funcs of
Nothing -> throwError $ UndefinedSymbol s Func expr
Just f -> do
es' <- mapM checkExpr es
-- Check that the correct number of arguments was provided
let nFormals = length (formals f)
nActuals = length es
unless (nFormals == nActuals) $ throwError (ArgError nFormals nActuals expr)
-- Check that types of arguments match
forM_ (zip (map fst es') (map bindType (formals f)))
$ \(callSite, defSite) ->
unless (callSite == defSite) $ throwError $ TypeError
{ expected = [defSite]
, got = callSite
, errorLoc = Expr expr
}
pure (typ f, SCall s es')
Cast t' e -> do
e'@(t, _) <- checkExpr e
case (t', t) of
(Pointer _, Pointer _) -> pure (t', SCast t' e')
(Pointer _, TyInt ) -> pure (t', SCast t' e')
(TyInt , Pointer _) -> pure (t', SCast t' e')
(TyFloat , TyInt ) -> pure (t', SCast t' e')
_ -> throwError $ CastError t' t (Expr expr)
Access e field -> do
fieldName <- case field of
Id f -> pure f
_ -> throwError (AccessError field e)
(t, e') <- checkExpr e
lval <- case e' of
LVal l' -> pure l'
_ -> throwError (AccessError e field)
(Struct _ fields) <- case t of
TyStruct name' -> do
ss <- gets structs
case find (\(Struct n _) -> n == name') ss of
Nothing -> throwError (TypeError [TyStruct "a_struct"] t (Expr expr))
Just s -> pure s
_ -> throwError (TypeError [TyStruct "a_struct"] t (Expr expr))
f <- case findIndex (\(Bind _ f) -> f == fieldName) fields of
Nothing -> throwError (AccessError e field)
Just i -> pure i
pure (bindType (fields !! f), LVal $ SAccess lval f)
Assign lhs rhs -> do
lhs'@(t1, _) <- checkExpr lhs
rhs'@(t2, _) <- checkExpr rhs
lval <- case snd lhs' of
LVal e -> pure e
_ -> throwError $ AssignmentError lhs rhs
case snd rhs' of
SNull -> checkExpr (Assign lhs (Cast t1 rhs))
_ -> do
unless (t1 == t2) $ throwError $ TypeError [t1] t2 (Expr expr)
pure (t2, SAssign lval rhs')
where
isNumeric = \case
TyInt -> True
TyFloat -> True
TyChar -> True
Pointer _ -> True
_ -> False
checkStatement :: Function -> Statement -> Semant SStatement
checkStatement func stmt = case stmt of
Expr e -> SExpr <$> checkExpr e
If pred cons alt -> do
pred'@(ty, _) <- checkExpr pred
unless (ty == TyBool) $ throwError $ TypeError [TyBool] ty stmt
SIf pred' <$> checkStatement func cons <*> checkStatement func alt
While cond action -> do
cond'@(ty, _) <- checkExpr cond
unless (ty == TyBool) $ throwError $ TypeError [TyBool] ty stmt
action' <- checkStatement func action
pure $ SIf cond' (SDoWhile cond' action') (SBlock [])
For init cond inc action -> do
cond'@(ty, _) <- checkExpr cond
unless (ty == TyBool) $ throwError $ TypeError [TyBool] ty stmt
init' <- checkExpr init
inc' <- checkExpr inc
action' <- checkStatement func action
pure $ SBlock
[ SExpr init'
, SIf cond' (SDoWhile cond' (SBlock [action', SExpr inc'])) (SBlock [])
]
Return expr -> do
e@(ty, _) <- checkExpr expr
unless (ty == typ func) $ throwError $ TypeError [typ func] ty stmt
pure $ SReturn e
Block sl -> do
let flattened = flatten sl
unless (nothingFollowsRet flattened) $ throwError (DeadCode stmt)
SBlock <$> mapM (checkStatement func) flattened
where
flatten [] = []
flatten (Block s : ss) = flatten (s ++ ss)
flatten (s : ss) = s : flatten ss
nothingFollowsRet [] = True
nothingFollowsRet [Return _] = True
nothingFollowsRet (s : ss ) = case s of
Return _ -> False
_ -> nothingFollowsRet ss
checkFunction :: Function -> Semant SFunction
checkFunction func = do
-- add the fname to the table and check for conflicts
funcs <- gets funcs
unless (M.notMember (name func) funcs) $ throwError $ Redeclaration (name func)
-- add this func to symbol table
modify $ \env -> env { funcs = M.insert (name func) func funcs }
(formals', locals', body') <- locally $ liftM3
(,,)
(checkBinds Formal (F func) (formals func))
(checkBinds Local (F func) (locals func))
(checkStatement func (Block $ body func))
case body' of
SBlock body'' -> do
unless (typ func == TyVoid || validate (genCFG body''))
$ throwError (TypeError [typ func] TyVoid (Block $ body func))
pure $ SFunction { styp = typ func
, sname = name func
, sformals = formals'
, slocals = locals'
, sbody = SBlock body''
}
_ -> error "Internal error - block didn't become a block?"
checkProgram :: Program -> Either SemantError SProgram
checkProgram program = evalState (runExceptT (checkProgram' program)) baseEnv
where
baseEnv = Env { structs = [], vars = M.empty, funcs = builtIns }
checkProgram' :: Program -> Semant SProgram
checkProgram' (Program structs binds funcs) = do
structs' <- mapM checkFields structs
modify $ \e -> e { structs = structs' }
globals <- checkBinds Global Toplevel binds
funcs' <- mapM checkFunction funcs
case find (\f -> sname f == "main") funcs' of
Nothing -> throwError NoMain
Just _ -> pure (structs', globals, funcs')