Copy and paste llvm-hs-examples/basic on GitHub to LLVMGen/Expression.hs and configure stack to compile it correctly

main
Ethan Reece 2023-09-17 23:18:55 -05:00
parent 187f0577f5
commit 37dcd45432
Signed by: me
GPG Key ID: D3993665FF92E1C3
6 changed files with 92 additions and 14 deletions

View File

@ -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 <file>` 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/<llvm16devdirectory>` 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

View File

@ -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_

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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