Add greater than and less than for integers
parent
6e2031f715
commit
a75b0df9bc
|
@ -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://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)
|
- 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)
|
- 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
|
### Tools
|
||||||
|
|
||||||
|
|
|
@ -1 +1,5 @@
|
||||||
printInt(5*(3-2)+-4-4);
|
printInt(5*(3-2)+-4-4);
|
||||||
|
printBool(true);
|
||||||
|
printBool(false);
|
||||||
|
printBool(5 * 3 > 5 + 9);
|
||||||
|
printBool(5*(3-2)+-4-4 < -3);
|
|
@ -16,7 +16,9 @@ import Data.String.Conversions
|
||||||
import Data.Text
|
import Data.Text
|
||||||
import LLVM (moduleLLVMAssembly, withModuleFromAST)
|
import LLVM (moduleLLVMAssembly, withModuleFromAST)
|
||||||
import LLVM.AST hiding (function)
|
import LLVM.AST hiding (function)
|
||||||
|
import LLVM.AST.IntegerPredicate (IntegerPredicate (SGE, SGT, SLE, SLT))
|
||||||
import LLVM.AST.Type
|
import LLVM.AST.Type
|
||||||
|
import LLVM.AST.Type qualified as AST
|
||||||
import LLVM.Context
|
import LLVM.Context
|
||||||
import LLVM.IRBuilder.Constant
|
import LLVM.IRBuilder.Constant
|
||||||
import LLVM.IRBuilder.Instruction
|
import LLVM.IRBuilder.Instruction
|
||||||
|
@ -87,6 +89,13 @@ statementToLLVM (T.PrintInt e) = mdo
|
||||||
formatStr <- getString "%d\n"
|
formatStr <- getString "%d\n"
|
||||||
_ <- call (FunctionType i32 [ptr] True) printf [(formatStr, []), (val, [])]
|
_ <- call (FunctionType i32 [ptr] True) printf [(formatStr, []), (val, [])]
|
||||||
pure 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 ::
|
intExprToLLVM ::
|
||||||
( MonadIRBuilder m,
|
( MonadIRBuilder m,
|
||||||
|
@ -95,7 +104,7 @@ intExprToLLVM ::
|
||||||
) =>
|
) =>
|
||||||
T.Int ->
|
T.Int ->
|
||||||
m Operand
|
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
|
intExprToLLVM (T.IntArith T.Add a b) = mdo
|
||||||
lhs <- intExprToLLVM a
|
lhs <- intExprToLLVM a
|
||||||
rhs <- intExprToLLVM b
|
rhs <- intExprToLLVM b
|
||||||
|
@ -113,8 +122,31 @@ intExprToLLVM (T.IntArith T.Div a b) = mdo
|
||||||
rhs <- intExprToLLVM b
|
rhs <- intExprToLLVM b
|
||||||
sdiv lhs rhs
|
sdiv lhs rhs
|
||||||
|
|
||||||
primToLLVM :: Int -> Operand
|
boolExprToLLVM ::
|
||||||
primToLLVM i = int32 $ fromIntegral i
|
( 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 :: [T.Statement] -> IO ByteString
|
||||||
llvmGen expr = do
|
llvmGen expr = do
|
||||||
|
|
Reference in New Issue