From a9549210a75f98f40bbbcf50fdb28b09313fee8e Mon Sep 17 00:00:00 2001 From: sudoer777 Date: Fri, 29 Sep 2023 05:17:12 -0500 Subject: [PATCH] Compile to binary executable file --- README.md | 2 +- main/Compiler/ExeGen.hs | 1 - main/Compiler/LLVMGen.hs | 21 ++++++++++++++++----- main/Main.hs | 8 +++++--- main/Types/Expr.hs | 6 ++++-- package.yaml | 5 +++-- really-bad-compiler-in-haskell.cabal | 4 ++-- stack.yaml | 2 +- stack.yaml.lock | 13 +++++++++++++ 9 files changed, 45 insertions(+), 17 deletions(-) delete mode 100644 main/Compiler/ExeGen.hs diff --git a/README.md b/README.md index a561b0c..06e499b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/main/Compiler/ExeGen.hs b/main/Compiler/ExeGen.hs deleted file mode 100644 index d35f82e..0000000 --- a/main/Compiler/ExeGen.hs +++ /dev/null @@ -1 +0,0 @@ -module Compiler.ExeGen () where diff --git a/main/Compiler/LLVMGen.hs b/main/Compiler/LLVMGen.hs index 14d0431..648abee 100644 --- a/main/Compiler/LLVMGen.hs +++ b/main/Compiler/LLVMGen.hs @@ -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) \ No newline at end of file +llvmGen :: Expr -> IO ByteString +llvmGen expr = do + let l = getLLVM expr + withContext + ( \c -> + withModuleFromAST c l moduleLLVMAssembly + ) \ No newline at end of file diff --git a/main/Main.hs b/main/Main.hs index 4b954d9..dda57c6 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -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." diff --git a/main/Types/Expr.hs b/main/Types/Expr.hs index 1736090..23e75e9 100644 --- a/main/Types/Expr.hs +++ b/main/Types/Expr.hs @@ -5,5 +5,7 @@ data Expr | Add Expr Expr | Sub Expr Expr | Mul Expr Expr - deriving (-- | Div Expr Expr - Show) \ No newline at end of file + deriving + ( -- | Div Expr Expr + Show + ) \ No newline at end of file diff --git a/package.yaml b/package.yaml index ff881a5..a8b1fbb 100644 --- a/package.yaml +++ b/package.yaml @@ -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 diff --git a/really-bad-compiler-in-haskell.cabal b/really-bad-compiler-in-haskell.cabal index fe722e3..d82f626 100644 --- a/really-bad-compiler-in-haskell.cabal +++ b/really-bad-compiler-in-haskell.cabal @@ -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 diff --git a/stack.yaml b/stack.yaml index 7070a4e..14ef8ca 100644 --- a/stack.yaml +++ b/stack.yaml @@ -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] diff --git a/stack.yaml.lock b/stack.yaml.lock index f76a1f2..62ed61b 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -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