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://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
|
||||
|
||||
|
|
|
@ -1 +1,5 @@
|
|||
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 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
|
||||
|
|
Reference in New Issue