Compile to binary executable file

pull/21/head
Ethan Reece 2023-09-29 05:17:12 -05:00
parent c296d15f85
commit a9549210a7
Signed by: me
GPG Key ID: D3993665FF92E1C3
9 changed files with 45 additions and 17 deletions

View File

@ -45,7 +45,7 @@ I recommend using VSCodium, which is preconfigured to have syntax highlighting a
- Language: Haskell
- Haskell tools: GHCup, Stack, Cabal, GHC 9.2
- Libraries: megaparsec, parser-combinators, text, llvm-hs-pure 15, llvm-hs-pretty-15
- Libraries: megaparsec, parser-combinators, text, process, llvm-hs-pure 15, llvm-hs-pretty-15
- Dependencies: llvm 15, clang 15
- IDE: VSCodium
- Git platform: Forgejo

View File

@ -1 +0,0 @@
module Compiler.ExeGen () where

View File

@ -6,14 +6,18 @@
module Compiler.LLVMGen (llvmGen) where
import Data.Text.Lazy
-- import LLVM.Pretty
import Data.ByteString (ByteString)
import Debug.Trace
import LLVM (moduleLLVMAssembly, withModuleFromAST, writeLLVMAssemblyToFile)
import LLVM.AST hiding (function)
import LLVM.AST.Type
import LLVM.Context
import LLVM.IRBuilder.Constant
import LLVM.IRBuilder.Instruction
import LLVM.IRBuilder.Module
import LLVM.IRBuilder.Monad
import LLVM.Pretty
import Types.Expr as Expr
getLLVM :: Expr -> Module
@ -23,8 +27,10 @@ getLLVM expr =
printf <- externVarArgs "printf" [ptr] i32
function "main" [] i32 $ \_ -> mdo
numFormatStr <- globalStringPtr "%d\n" (mkName "str")
traceShow numFormatStr $ pure ()
ourExpression <- exprToLLVM expr
_ <- call (FunctionType i32 [i32] False) printf [(ConstantOperand numFormatStr, []), (ourExpression, [])]
-- _ <- call (FunctionType i32 [i32])
_ <- call (FunctionType i32 [ptr] True) printf [(ConstantOperand numFormatStr, []), (ourExpression, [])]
ret (int32 0)
exprToLLVM ::
@ -50,5 +56,10 @@ exprToLLVM (Expr.Mul a b) = mdo
primToLLVM :: Int -> Operand
primToLLVM i = int32 (fromIntegral i)
llvmGen :: Expr -> Text
llvmGen expr = ppllvm (getLLVM expr)
llvmGen :: Expr -> IO ByteString
llvmGen expr = do
let l = getLLVM expr
withContext
( \c ->
withModuleFromAST c l moduleLLVMAssembly
)

View File

@ -5,10 +5,11 @@ module Main (main) where
-- import Compiler.ExeGen
import Compiler.LLVMGen
import Data.Text.Lazy qualified as L
import Data.ByteString.Char8 qualified as B
import Data.Text.Lazy.IO qualified as T
import Parser.Expr
import System.Environment
import System.Process
import Types.Expr
getRight :: ParseResult -> Expr
@ -21,6 +22,7 @@ main = do
T.putStrLn "- Parsing file..."
let parsed = getRight (parseExpr contents)
T.putStrLn "- Generating LLVM to './a.out.ll'..."
let llvm = llvmGen parsed
writeFile "a.out.ll" (L.unpack llvm)
llvmGen parsed >>= B.writeFile "a.out.ll"
T.putStrLn "- Compiling to executable './a.out'..."
callCommand "clang a.out.ll"
T.putStrLn "- Done."

View File

@ -5,5 +5,7 @@ data Expr
| Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
deriving (-- | Div Expr Expr
Show)
deriving
( -- | Div Expr Expr
Show
)

View File

@ -6,9 +6,10 @@ dependencies:
- megaparsec >= 9.0.1 && < 10
- parser-combinators
- text
# - llvm-hs >= 15 && < 16
- process
- llvm-hs >= 15 && < 16
- llvm-hs-pure >= 15 && < 16
- llvm-hs-pretty >= 15 && < 16
# - llvm-hs-pretty >= 15 && < 16
- bytestring
tested-with: GHC == 9.2.8
category: Compilers/Interpreters

View File

@ -16,7 +16,6 @@ tested-with:
executable really-bad-compiler-in-haskell
main-is: Main.hs
other-modules:
Compiler.ExeGen
Compiler.LLVMGen
Parser.Expr
Types.Expr
@ -29,9 +28,10 @@ executable really-bad-compiler-in-haskell
build-depends:
base >=4.14.3 && <5
, bytestring
, llvm-hs-pretty ==15.*
, llvm-hs ==15.*
, llvm-hs-pure ==15.*
, megaparsec >=9.0.1 && <10
, parser-combinators
, process
, text
default-language: Haskell2010

View File

@ -9,7 +9,7 @@ extra-deps:
commit: 5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76
subdirs:
- llvm-hs-pure
# - llvm-hs
- llvm-hs
nix:
enable: true
packages: [llvm_15, clang_15, libxml2]

View File

@ -28,6 +28,19 @@ packages:
original:
subdir: llvm-hs-pure
url: https://github.com/llvm-hs/llvm-hs/archive/5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76.tar.gz
- completed:
name: llvm-hs
pantry-tree:
sha256: 21f74a6f51fae6c0a0fcf3e6620a59f341289ac743998b41ed3276fd1b7d8862
size: 12716
sha256: 526b67e2da9ce25b3856c221b6772e699a7593dbb5ba38e7ee2436349de70966
size: 9802209
subdir: llvm-hs
url: https://github.com/llvm-hs/llvm-hs/archive/5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76.tar.gz
version: 15.0.0
original:
subdir: llvm-hs
url: https://github.com/llvm-hs/llvm-hs/archive/5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76.tar.gz
snapshots:
- completed:
sha256: 5a59b2a405b3aba3c00188453be172b85893cab8ebc352b1ef58b0eae5d248a2