Copy and paste llvm-hs-examples/basic on GitHub to LLVMGen/Expression.hs and configure stack to compile it correctly
parent
187f0577f5
commit
37dcd45432
15
README.md
15
README.md
|
@ -1,6 +1,6 @@
|
||||||
# Really Bad Compiler in Haskell
|
# 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
|
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).
|
- Install `ghcup` (for managing Haskell tools) and `nix` (for managing external dependencies).
|
||||||
- Clone the repo.
|
- 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`).
|
- 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
|
## File structure
|
||||||
|
|
||||||
- `app` - contains the compiler program
|
- `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://akashagrawal.me/2017/01/19/beginners-guide-to-megaparsec.html
|
||||||
- https://markkarpov.com/tutorial/megaparsec.html (for help writing a Haskell equation parser)
|
- 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://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
|
### Tools
|
||||||
|
|
||||||
- Language: Haskell
|
- Language: Haskell
|
||||||
- Haskell tools: GHCup, Stack, Cabal
|
- 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
|
- Dependencies: llvm, clang
|
||||||
- IDE: VSCodium
|
- IDE: VSCodium
|
||||||
- Git platform: Forgejo
|
- Git platform: Forgejo
|
||||||
|
|
|
@ -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_
|
|
@ -6,12 +6,14 @@ dependencies:
|
||||||
- megaparsec >= 9.0.1 && < 10
|
- megaparsec >= 9.0.1 && < 10
|
||||||
- parser-combinators
|
- parser-combinators
|
||||||
- text
|
- text
|
||||||
|
- llvm-hs >= 15 && < 16
|
||||||
- llvm-hs-pure >= 15 && < 16
|
- llvm-hs-pure >= 15 && < 16
|
||||||
# - llvm-hs-pretty >= 12 && < 13
|
# - llvm-hs-pretty >= 15 && < 16
|
||||||
tested-with: GHC == 8.10.7
|
- bytestring
|
||||||
|
tested-with: GHC == 9.2.8
|
||||||
category: Compilers/Interpreters
|
category: Compilers/Interpreters
|
||||||
|
|
||||||
ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS
|
ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm
|
||||||
|
|
||||||
# library:
|
# library:
|
||||||
# source-dirs: src
|
# source-dirs: src
|
||||||
|
@ -25,3 +27,4 @@ executable:
|
||||||
# source-dirs: test
|
# source-dirs: test
|
||||||
|
|
||||||
default-extensions: OverloadedStrings, LambdaCase
|
default-extensions: OverloadedStrings, LambdaCase
|
||||||
|
# flags: -fno-hs-llvm=true
|
||||||
|
|
|
@ -11,12 +11,13 @@ author: sudoer777
|
||||||
maintainer: sudoer777
|
maintainer: sudoer777
|
||||||
build-type: Simple
|
build-type: Simple
|
||||||
tested-with:
|
tested-with:
|
||||||
GHC == 8.10.7
|
GHC == 9.2.8
|
||||||
|
|
||||||
executable really-bad-compiler-in-haskell
|
executable really-bad-compiler-in-haskell
|
||||||
main-is: Main.hs
|
main-is: Main.hs
|
||||||
other-modules:
|
other-modules:
|
||||||
Eval.Expression
|
Eval.Expression
|
||||||
|
LLVMGen.Expression
|
||||||
Objects.Expression
|
Objects.Expression
|
||||||
Parser.Expression
|
Parser.Expression
|
||||||
Paths_really_bad_compiler_in_haskell
|
Paths_really_bad_compiler_in_haskell
|
||||||
|
@ -24,9 +25,11 @@ executable really-bad-compiler-in-haskell
|
||||||
app
|
app
|
||||||
default-extensions:
|
default-extensions:
|
||||||
OverloadedStrings, LambdaCase
|
OverloadedStrings, LambdaCase
|
||||||
ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS
|
ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm
|
||||||
build-depends:
|
build-depends:
|
||||||
base >=4.14.3 && <5
|
base >=4.14.3 && <5
|
||||||
|
, bytestring
|
||||||
|
, llvm-hs ==15.*
|
||||||
, llvm-hs-pure ==15.*
|
, llvm-hs-pure ==15.*
|
||||||
, megaparsec >=9.0.1 && <10
|
, megaparsec >=9.0.1 && <10
|
||||||
, parser-combinators
|
, parser-combinators
|
||||||
|
|
11
stack.yaml
11
stack.yaml
|
@ -3,14 +3,15 @@ resolver: lts-20.26
|
||||||
packages:
|
packages:
|
||||||
- .
|
- .
|
||||||
extra-deps:
|
extra-deps:
|
||||||
# - github: llvm-hs/llvm-hs-pretty
|
# - github: hyunsooda/llvm-hs-pretty-15
|
||||||
# commit: 655ff4d47b3a584b4c9a5863f6121b954825920c
|
# commit: 79283942d1667168ecd65237667aff7fed730303
|
||||||
- github: llvm-hs/llvm-hs
|
- github: llvm-hs/llvm-hs
|
||||||
commit: 5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76
|
commit: 5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76
|
||||||
subdirs:
|
subdirs:
|
||||||
- llvm-hs-pure
|
- llvm-hs-pure
|
||||||
|
- llvm-hs
|
||||||
nix:
|
nix:
|
||||||
enable: true
|
enable: true
|
||||||
packages: [llvm_16, clang_16]
|
packages: [llvm_15, clang_15, libxml2]
|
||||||
# system-ghc: true
|
system-ghc: true
|
||||||
# install-ghc: false
|
install-ghc: true
|
||||||
|
|
|
@ -17,6 +17,19 @@ packages:
|
||||||
original:
|
original:
|
||||||
subdir: llvm-hs-pure
|
subdir: llvm-hs-pure
|
||||||
url: https://github.com/llvm-hs/llvm-hs/archive/5bca2c1a2a3aa98ecfb19181e7a5ebbf3e212b76.tar.gz
|
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:
|
snapshots:
|
||||||
- completed:
|
- completed:
|
||||||
sha256: 5a59b2a405b3aba3c00188453be172b85893cab8ebc352b1ef58b0eae5d248a2
|
sha256: 5a59b2a405b3aba3c00188453be172b85893cab8ebc352b1ef58b0eae5d248a2
|
||||||
|
|
Reference in New Issue