Heavily shorted down the emitAdd function, and added a emitInt function.
This commit is contained in:
parent
57f8b6ba5b
commit
f8d320bb6d
2 changed files with 44 additions and 43 deletions
|
|
@ -18,7 +18,7 @@ extra-source-files:
|
||||||
|
|
||||||
|
|
||||||
common warnings
|
common warnings
|
||||||
ghc-options: -Wall
|
ghc-options: -W
|
||||||
|
|
||||||
executable language
|
executable language
|
||||||
import: warnings
|
import: warnings
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,11 @@ data CodeGenerator = CodeGenerator
|
||||||
, variableCount :: Integer }
|
, variableCount :: Integer }
|
||||||
deriving Show
|
deriving Show
|
||||||
defaultCodeGenerator :: CodeGenerator
|
defaultCodeGenerator :: CodeGenerator
|
||||||
defaultCodeGenerator = CodeGenerator {instructions=[], methods=[], blocks=[], variableCount=0}
|
defaultCodeGenerator = CodeGenerator
|
||||||
|
{ instructions=[]
|
||||||
|
, methods=[]
|
||||||
|
, blocks=[]
|
||||||
|
, variableCount=0}
|
||||||
|
|
||||||
data LLVMIr = Define Type Ident Params
|
data LLVMIr = Define Type Ident Params
|
||||||
| DefineEnd
|
| DefineEnd
|
||||||
|
|
@ -96,16 +100,16 @@ increaseVarCount :: CompilerState
|
||||||
increaseVarCount = modify (\t -> t {variableCount = variableCount t + 1})
|
increaseVarCount = modify (\t -> t {variableCount = variableCount t + 1})
|
||||||
|
|
||||||
compile :: Program -> IO ()
|
compile :: Program -> IO ()
|
||||||
compile (Program e) = do
|
compile (Program prgE) = do
|
||||||
--Asp
|
--Asp
|
||||||
let s = defaultCodeGenerator {instructions = [
|
let s = defaultCodeGenerator {instructions = [
|
||||||
Comment $ printTree (Program e),
|
Comment $ printTree (Program prgE),
|
||||||
UnsafeRaw $ standardLLVMLibrary <> "\n",
|
UnsafeRaw $ standardLLVMLibrary <> "\n",
|
||||||
--UnsafeRaw "declare i32 @puts(i8* nocapture) nounwind\n",
|
--UnsafeRaw "declare i32 @puts(i8* nocapture) nounwind\n",
|
||||||
--UnsafeRaw "declare [21 x i8] @i64ToString(i64)\n",
|
--UnsafeRaw "declare [21 x i8] @i64ToString(i64)\n",
|
||||||
Define I32 (Ident "main") []
|
Define I32 (Ident "main") []
|
||||||
]}
|
]}
|
||||||
let fin = execState (go e) s
|
let fin = execState (go prgE) s
|
||||||
let ins = instructions fin
|
let ins = instructions fin
|
||||||
let var = variableCount fin
|
let var = variableCount fin
|
||||||
putStrLn $ concatMap printLLVMIr (ins ++
|
putStrLn $ concatMap printLLVMIr (ins ++
|
||||||
|
|
@ -120,47 +124,44 @@ compile (Program e) = do
|
||||||
])
|
])
|
||||||
where
|
where
|
||||||
go :: Exp -> CompilerState
|
go :: Exp -> CompilerState
|
||||||
go (EId id) = undefined
|
go (EInt int) = emitInt int
|
||||||
go (EInt int) = do
|
|
||||||
increaseVarCount
|
|
||||||
varCount <- gets variableCount
|
|
||||||
emit $ Variable $ Ident (show varCount)
|
|
||||||
emit $ Add I64 (VInteger int) (VInteger 0)
|
|
||||||
|
|
||||||
go (EApp e1 e2) = undefined
|
|
||||||
go (EAdd e1 e2) = emitAdd e1 e2
|
go (EAdd e1 e2) = emitAdd e1 e2
|
||||||
|
go (EId id) = undefined
|
||||||
|
go (EApp e1 e2) = undefined
|
||||||
go (EAbs id e) = undefined
|
go (EAbs id e) = undefined
|
||||||
|
|
||||||
--- aux functions ---
|
--- aux functions ---
|
||||||
emitAdd :: Exp -> Exp -> CompilerState
|
emitInt :: Integer -> CompilerState
|
||||||
-- instead of declaring two variables for this case,
|
emitInt i = do
|
||||||
-- we can directly pass them to add.
|
-- ideally this case should not occur if the other
|
||||||
emitAdd (EInt i1) (EInt i2) = do
|
-- emit operations are optimized
|
||||||
increaseVarCount
|
increaseVarCount
|
||||||
v1 <- gets variableCount
|
varCount <- gets variableCount
|
||||||
emit $ Variable $ Ident $ show v1
|
emit $ Variable $ Ident (show varCount)
|
||||||
emit $ Add I64 (VInteger i1) (VInteger i2)
|
emit $ Add I64 (VInteger i) (VInteger 0)
|
||||||
emitAdd (EInt i) e2 = do
|
|
||||||
go e2
|
emitAdd :: Exp -> Exp -> CompilerState
|
||||||
v2 <- gets variableCount
|
emitAdd e1 e2 = do
|
||||||
increaseVarCount
|
-- instead of declaring variables for adding ints,
|
||||||
v3 <- gets variableCount
|
-- we can directly pass them to add.
|
||||||
emit $ Variable $ Ident $ show v3
|
(v1,v2) <- case (e1, e2) of
|
||||||
emit $ Add I64 (VInteger i) (VIdent (Ident $ show v2))
|
(EInt i1, EInt i2) -> return (VInteger i1, VInteger i2)
|
||||||
emitAdd e1 (EInt i) = do
|
(EInt i, e) -> do
|
||||||
go e1
|
go e
|
||||||
v2 <- gets variableCount
|
v2 <- gets variableCount
|
||||||
increaseVarCount
|
return (VInteger i, VIdent (Ident $ show v2))
|
||||||
v3 <- gets variableCount
|
(e, EInt i) -> do
|
||||||
emit $ Variable $ Ident $ show v3
|
go e
|
||||||
emit $ Add I64 (VIdent (Ident $ show v2)) (VInteger i)
|
v2 <- gets variableCount
|
||||||
emitAdd e1 e2 = do
|
return (VIdent (Ident $ show v2), VInteger i)
|
||||||
go e1
|
(e1, e2) -> do
|
||||||
v1 <- gets variableCount
|
go e1
|
||||||
go e2
|
v1 <- gets variableCount
|
||||||
v2 <- gets variableCount
|
go e2
|
||||||
increaseVarCount
|
v2 <- gets variableCount
|
||||||
v3 <- gets variableCount
|
return (VIdent (Ident $ show v1),VIdent (Ident $ show v2))
|
||||||
emit $ Variable $ Ident $ show v3
|
increaseVarCount
|
||||||
emit $ Add I64 (VIdent (Ident $ show v1)) (VIdent (Ident $ show v2))
|
v <- gets variableCount
|
||||||
|
emit $ Variable $ Ident $ show v
|
||||||
|
emit $ Add I64 v1 v2
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue