From 37dcd45432ac7d315e0404bf3205ee80476070c3 Mon Sep 17 00:00:00 2001 From: sudoer777 Date: Sun, 17 Sep 2023 23:18:55 -0500 Subject: [PATCH] Copy and paste llvm-hs-examples/basic on GitHub to LLVMGen/Expression.hs and configure stack to compile it correctly --- README.md | 15 +++++--- app/LLVMGen/Expression.hs | 51 ++++++++++++++++++++++++++++ package.yaml | 9 +++-- really-bad-compiler-in-haskell.cabal | 7 ++-- stack.yaml | 11 +++--- stack.yaml.lock | 13 +++++++ 6 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 app/LLVMGen/Expression.hs diff --git a/README.md b/README.md index 17550b8..f26cbc7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Really Bad Compiler in Haskell -A compiler written in Haskell that is really bad (I do not know what language I am going to compile yet). Built for the Introduction to Compiler Design class at The University of Texas at Dallas. +A compiler written in Haskell which can currently perform basic arithmetic using the megaparsec and llvm-hs libraries (I do not know what language I am going to compile yet). Built for the Introduction to Compiler Design class at The University of Texas at Dallas. Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell @@ -8,9 +8,15 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell - Install `ghcup` (for managing Haskell tools) and `nix` (for managing external dependencies). - Clone the repo. -- Use `ghcup` to install `stack 2.9.3`, `HLS 2.2.0.0`, and `cabal 3.6.2.0`. +- Use `ghcup` to install `stack 2.9.3`, `HLS 2.2.0.0`, `GHC 9.2.8`, and `cabal 3.6.2.0`. - Use `stack run ` to run the program (for example, `stack run example/1`). +## Known bugs + +### Building + +- If llvm_16 is in the nix store, stack will use it for some reason and the build will fail. Currently, you may need to run `nix-store --delete /nix/store/` to build it. + ## File structure - `app` - contains the compiler program @@ -25,13 +31,14 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell - https://akashagrawal.me/2017/01/19/beginners-guide-to-megaparsec.html - https://markkarpov.com/tutorial/megaparsec.html (for help writing a Haskell equation parser) - https://www.forth.com/starting-forth/1-forth-stacks-dictionary/ (for learning Forth) -- https://blog.josephmorag.com/posts/mcc0/ (Haskell LLVM tutorial) +- https://blog.josephmorag.com/posts/mcc0/ (Haskell compiler tutorial with megaparsec, llvm-hs, and nix) +- https://gh.sudoer.ch/llvm-hs/llvm-hs-examples (for help writing an llvm code generator) ### Tools - Language: Haskell - Haskell tools: GHCup, Stack, Cabal -- Libraries: megaparsec, parser-combinators, text, llvm-hs-pure, llvm-hs-pretty +- Libraries: megaparsec, parser-combinators, text, llvm-hs-pure - Dependencies: llvm, clang - IDE: VSCodium - Git platform: Forgejo diff --git a/app/LLVMGen/Expression.hs b/app/LLVMGen/Expression.hs new file mode 100644 index 0000000..0b2c4d2 --- /dev/null +++ b/app/LLVMGen/Expression.hs @@ -0,0 +1,51 @@ +module LLVMGen.Expression () where + +import qualified Objects.Expression as Expr + +import LLVM.AST +import qualified LLVM.AST as AST +import LLVM.AST.Global +import LLVM.Context +import LLVM.Module + +import Data.ByteString.Char8 as BS + +int :: Type +int = IntegerType 32 + +defAdd :: Definition +defAdd = GlobalDefinition functionDefaults + { name = Name "add" + , parameters = + ( [ Parameter int (Name "a") [] + , Parameter int (Name "b") [] ] + , False ) + , returnType = int + , basicBlocks = [body] + } + where + body = BasicBlock + (Name "entry") + [ Name "result" := + Add False -- no signed wrap + False -- no unsigned wrap + (LocalReference int (Name "a")) + (LocalReference int (Name "b")) + []] + (Do $ Ret (Just (LocalReference int (Name "result"))) []) + + +module_ :: AST.Module +module_ = defaultModule + { moduleName = "basic" + , moduleDefinitions = [defAdd] + } + + +toLLVM :: AST.Module -> IO () +toLLVM modul = withContext $ \ctx -> do + llvm <- withModuleFromAST ctx modul moduleLLVMAssembly + BS.putStrLn llvm + +main :: IO () +main = toLLVM module_ \ No newline at end of file diff --git a/package.yaml b/package.yaml index 80581b1..af19751 100644 --- a/package.yaml +++ b/package.yaml @@ -6,12 +6,14 @@ dependencies: - megaparsec >= 9.0.1 && < 10 - parser-combinators - text + - llvm-hs >= 15 && < 16 - llvm-hs-pure >= 15 && < 16 - # - llvm-hs-pretty >= 12 && < 13 -tested-with: GHC == 8.10.7 + # - llvm-hs-pretty >= 15 && < 16 + - bytestring +tested-with: GHC == 9.2.8 category: Compilers/Interpreters -ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS +ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm # library: # source-dirs: src @@ -25,3 +27,4 @@ executable: # source-dirs: test default-extensions: OverloadedStrings, LambdaCase +# flags: -fno-hs-llvm=true diff --git a/really-bad-compiler-in-haskell.cabal b/really-bad-compiler-in-haskell.cabal index cb9167b..25ecb34 100644 --- a/really-bad-compiler-in-haskell.cabal +++ b/really-bad-compiler-in-haskell.cabal @@ -11,12 +11,13 @@ author: sudoer777 maintainer: sudoer777 build-type: Simple tested-with: - GHC == 8.10.7 + GHC == 9.2.8 executable really-bad-compiler-in-haskell main-is: Main.hs other-modules: Eval.Expression + LLVMGen.Expression Objects.Expression Parser.Expression Paths_really_bad_compiler_in_haskell @@ -24,9 +25,11 @@ executable really-bad-compiler-in-haskell app default-extensions: OverloadedStrings, LambdaCase - ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS + ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm build-depends: base >=4.14.3 && <5 + , bytestring + , llvm-hs ==15.* , llvm-hs-pure ==15.* , megaparsec >=9.0.1 && <10 , parser-combinators diff --git a/stack.yaml b/stack.yaml index 0d6c2e8..f5a1103 100644 --- a/stack.yaml +++ b/stack.yaml @@ -3,14 +3,15 @@ resolver: lts-20.26 packages: - . extra-deps: - # - github: llvm-hs/llvm-hs-pretty - # commit: 655ff4d47b3a584b4c9a5863f6121b954825920c + # - github: hyunsooda/llvm-hs-pretty-15 + # commit: 79283942d1667168ecd65237667aff7fed730303 - github: llvm-hs/llvm-hs commit: 5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76 subdirs: - llvm-hs-pure + - llvm-hs nix: enable: true - packages: [llvm_16, clang_16] -# system-ghc: true -# install-ghc: false + packages: [llvm_15, clang_15, libxml2] +system-ghc: true +install-ghc: true diff --git a/stack.yaml.lock b/stack.yaml.lock index 6b809c3..0055e8d 100644 --- a/stack.yaml.lock +++ b/stack.yaml.lock @@ -17,6 +17,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