@@ -16,11 +16,10 @@ module Emit (
1616 varDeclGen ,
1717 literalGen ,
1818 moduleGen ,
19+ codegenTop' ,
1920 call ,
2021 toArgs ,
2122
22- x' ,
23- y' ,
2423 funcCallGen ,
2524
2625)where
@@ -46,7 +45,7 @@ import qualified LLVM.AST.Attribute as A
4645
4746import Control.Monad.State
4847import Control.Applicative
49-
48+ import JIT
5049-- | Parser ASTL to LLVM code
5150
5251
@@ -77,16 +76,16 @@ binops = Map.fromList [
7776
7877-- Arithmetic and Constants
7978fadd :: ASTL. Operand -> ASTL. Operand -> Codegen ASTL. Operand
80- fadd a b = instr $ FAdd NoFastMathFlags a b []
79+ fadd a b = instr $ ASTL. Add False False a b []
8180
8281fsub :: ASTL. Operand -> ASTL. Operand -> Codegen ASTL. Operand
83- fsub a b = instr $ FSub NoFastMathFlags a b []
82+ fsub a b = instr $ ASTL. Sub False False a b []
8483
8584fmul :: ASTL. Operand -> ASTL. Operand -> Codegen ASTL. Operand
86- fmul a b = instr $ FMul NoFastMathFlags a b []
85+ fmul a b = instr $ ASTL. Mul False False a b []
8786
8887fdiv :: ASTL. Operand -> ASTL. Operand -> Codegen ASTL. Operand
89- fdiv a b = instr $ FDiv NoFastMathFlags a b []
88+ fdiv a b = instr $ ASTL. UDiv False a b []
9089
9190externf :: ASTL. Name -> ASTL. Operand
9291externf = ConstantOperand . C. GlobalReference intL
@@ -101,42 +100,50 @@ toArgs :: [ASTL.Operand] -> [(ASTL.Operand, [A.ParameterAttribute])]
101100toArgs = map (\ x -> (x, [] ))
102101
103102
103+ liftError :: ExceptT String IO a -> IO a
104+ liftError = runExceptT >=> either fail return
104105
105106
106- y' :: Traversable t => t Func -> LLVM (t () )
107- y' fns = mapM codegenTop fns
108-
109- x' :: ASTL. Module -> LLVM a -> ASTL. Module
110- x' mod modn = runLLVM mod modn
111-
107+ -- | Actions associated with each branch of ASTL
108+ -- | Used to refer to a variable
112109
113110
114111-- withContext == Create a Context, run an action (to which it is provided), then destroy the Context.
112+ -- codegen :: ASTL.Module -> [ASTp.Func] -> IO ASTL.Module
113+ -- codegen mod fns = withContext $ \context -> -- fns = [(Function "foo " ["a", "b"] (Float 1.0)) ]
114+ -- liftError $ withModuleFromAST context newast $ \m -> do
115+ -- llstr <- moduleLLVMAssembly m
116+ -- putStrLn llstr
117+ -- return newast
118+ -- where
119+ -- modn = mapM codegenTop fns -- list of LLVM ()
120+ -- newast = runLLVM mod modn
121+
115122codegen :: ASTL. Module -> [ASTp. Func ] -> IO ASTL. Module
116- codegen mod fns = withContext $ \ context -> -- fns = [(Function "foo " ["a", "b"] (Float 1.0)) ]
123+ codegen mod fns = do
124+ res <- runJIT oldast
125+ case res of
126+ Right newast -> return newast
127+ Left err -> putStrLn err >> return oldast
128+ where
129+ modn = mapM codegenTop fns
130+ oldast = runLLVM mod modn
131+
132+ codegen' :: ASTL. Module -> [ASTp. Declaration ] -> IO ASTL. Module
133+ codegen' mod decl = withContext $ \ context ->
117134 liftError $ withModuleFromAST context newast $ \ m -> do
118135 llstr <- moduleLLVMAssembly m
119136 putStrLn llstr
120137 return newast
121138 where
122- modn = mapM codegenTop fns -- list of LLVM ()
139+ modn = mapM codegenTop' decl -- list of LLVM ()
123140 newast = runLLVM mod modn
124141
125142
126-
127-
128-
129-
130- liftError :: ExceptT String IO a -> IO a
131- liftError = runExceptT >=> either fail return
132-
133-
134-
135- -- funcGen :: ASTp.Func -> LLVM ()
136- -- funcGen x = addDef $ getDef x
137-
138- -- declarationGen :: ASTp.Declaration -> LLVM ()
139- -- declarationGen c@(ASTp.ExternDecl _ _ _) = addDef $ getExtern c
143+ codegenTop' :: ASTp. Declaration -> LLVM ()
144+ codegenTop' (ASTp. ExternDecl name fnargs retT) = do
145+ external intL name args
146+ where args = getArgList fnargs
140147
141148
142149
@@ -146,6 +153,9 @@ liftError = runExceptT >=> either fail return
146153
147154-- | Parser Module to LLVM Module
148155moduleGen :: ASTL. Module -> ASTp. Module -> IO ASTL. Module
156+ moduleGen modL (ASTp. Command (DeclarationStmt c@ (ExternDecl _ _ _))) =
157+ codegen' modL [c]
158+
149159moduleGen modL (ASTp. Method func) = codegen modL [func]
150160moduleGen modL (ASTp. Command expr) = codegen modL [rest]
151161 where rest = ASTp. Func {
0 commit comments