diff --git a/README.md b/README.md index 170d18f..36b09d7 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a - https://9to5tutorial.com/homebrew-compiler-made-with-haskell-llvm-configuration (for help using llvm-hs-pure) - https://blog.ocharles.org.uk/blog/posts/2012-12-17-24-days-of-hackage-optparse-applicative.html (for help parsing command line arguments with optparse-applicative) - http://learnyouahaskell.com/making-our-own-types-and-typeclasses (for help defining types) +- https://llvm.org/docs/LangRef.html (LLVM documentation) +- https://hackage.haskell.org/package/llvm-hs-pure-9.0.0/docs/ (llvm-hs documentation) ### Tools diff --git a/example/1.hear b/example/1.hear index a948a80..44fe560 100644 --- a/example/1.hear +++ b/example/1.hear @@ -1 +1,5 @@ -printInt(5*(3-2)+-4-4); \ No newline at end of file +printInt(5*(3-2)+-4-4); +printBool(true); +printBool(false); +printBool(5 * 3 > 5 + 9); +printBool(5*(3-2)+-4-4 < -3); \ No newline at end of file diff --git a/src/Main/LLVMGen.hs b/src/Main/LLVMGen.hs index 65306f2..ecb8d65 100644 --- a/src/Main/LLVMGen.hs +++ b/src/Main/LLVMGen.hs @@ -16,7 +16,9 @@ import Data.String.Conversions import Data.Text import LLVM (moduleLLVMAssembly, withModuleFromAST) import LLVM.AST hiding (function) +import LLVM.AST.IntegerPredicate (IntegerPredicate (SGE, SGT, SLE, SLT)) import LLVM.AST.Type +import LLVM.AST.Type qualified as AST import LLVM.Context import LLVM.IRBuilder.Constant import LLVM.IRBuilder.Instruction @@ -87,6 +89,13 @@ statementToLLVM (T.PrintInt e) = mdo formatStr <- getString "%d\n" _ <- call (FunctionType i32 [ptr] True) printf [(formatStr, []), (val, [])] pure val +statementToLLVM (T.PrintBool e) = mdo + val <- boolExprToLLVM e + val32 <- zext val i32 + printf <- getOperand "printf" + formatStr <- getString "%d\n" + _ <- call (FunctionType i32 [ptr] True) printf [(formatStr, []), (val32, [])] + pure val intExprToLLVM :: ( MonadIRBuilder m, @@ -95,7 +104,7 @@ intExprToLLVM :: ) => T.Int -> m Operand -intExprToLLVM (T.Int prim) = pure $ primToLLVM prim +intExprToLLVM (T.Int prim) = pure $ int32 $ fromIntegral prim intExprToLLVM (T.IntArith T.Add a b) = mdo lhs <- intExprToLLVM a rhs <- intExprToLLVM b @@ -113,8 +122,31 @@ intExprToLLVM (T.IntArith T.Div a b) = mdo rhs <- intExprToLLVM b sdiv lhs rhs -primToLLVM :: Int -> Operand -primToLLVM i = int32 $ fromIntegral i +boolExprToLLVM :: + ( MonadIRBuilder m, + MonadModuleBuilder m, + MonadState Env m + ) => + T.Bool -> + m Operand +boolExprToLLVM (T.Bool prim) = + if prim then pure $ bit 1 else pure $ bit 0 +boolExprToLLVM (T.IntOrdCmp T.GT a b) = mdo + lhs <- intExprToLLVM a + rhs <- intExprToLLVM b + icmp SGT lhs rhs +boolExprToLLVM (T.IntOrdCmp T.GTE a b) = mdo + lhs <- intExprToLLVM a + rhs <- intExprToLLVM b + icmp SGE lhs rhs +boolExprToLLVM (T.IntOrdCmp T.LT a b) = mdo + lhs <- intExprToLLVM a + rhs <- intExprToLLVM b + icmp SLT lhs rhs +boolExprToLLVM (T.IntOrdCmp T.LTE a b) = mdo + lhs <- intExprToLLVM a + rhs <- intExprToLLVM b + icmp SLE lhs rhs llvmGen :: [T.Statement] -> IO ByteString llvmGen expr = do