Generate LLVM program that returns 0 when run

pull/21/head
Ethan Reece 2023-09-22 23:59:31 -05:00
parent c0e77d22cf
commit d42de52f78
Signed by: me
GPG Key ID: D3993665FF92E1C3
8 changed files with 24 additions and 6 deletions

View File

@ -1,6 +1,6 @@
# Really Bad Compiler in Haskell # Really Bad Compiler in Haskell
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. A compiler written in Haskell which can currently perform basic arithmetic. Currently using the megaparsec and llvm-hs libraries, but I plan to reimplement parsing and llvm generation myself. 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
@ -42,5 +42,5 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell
- Dependencies: llvm, clang - Dependencies: llvm, clang
- IDE: VSCodium - IDE: VSCodium
- Git platform: Forgejo - Git platform: Forgejo
- AI: Phind - AI: Phind (GPT-4), ollama (codellama)
- Search: Kagi, Stack Overflow - Search: Kagi, Stack Overflow

View File

@ -5,6 +5,9 @@ import qualified Objects.Expression as Expr
import LLVM.AST import LLVM.AST
import qualified LLVM.AST as AST import qualified LLVM.AST as AST
import LLVM.AST.Global import LLVM.AST.Global
import LLVM.AST.Constant
import LLVM.AST.Type
import LLVM.AST.Name
import LLVM.Context import LLVM.Context
import LLVM.Module import LLVM.Module
@ -13,6 +16,21 @@ import Data.ByteString.Char8 as BS
int :: Type int :: Type
int = IntegerType 32 int = IntegerType 32
defMain :: Definition
defMain = GlobalDefinition functionDefaults
{ name = Name "main"
, parameters =
( []
, False )
, returnType = int
, basicBlocks = [body]
}
where
body = BasicBlock
(Name "entry")
[]
(Do $ Ret (Just (ConstantOperand (Int 32 0))) [])
defAdd :: Definition defAdd :: Definition
defAdd = GlobalDefinition functionDefaults defAdd = GlobalDefinition functionDefaults
{ name = Name "add" { name = Name "add"
@ -27,7 +45,7 @@ defAdd = GlobalDefinition functionDefaults
body = BasicBlock body = BasicBlock
(Name "entry") (Name "entry")
[ Name "result" := [ Name "result" :=
Add False -- no signed wrap AST.Add False -- no signed wrap
False -- no unsigned wrap False -- no unsigned wrap
(LocalReference int (Name "a")) (LocalReference int (Name "a"))
(LocalReference int (Name "b")) (LocalReference int (Name "b"))
@ -38,7 +56,7 @@ defAdd = GlobalDefinition functionDefaults
module_ :: AST.Module module_ :: AST.Module
module_ = defaultModule module_ = defaultModule
{ moduleName = "basic" { moduleName = "basic"
, moduleDefinitions = [defAdd] , moduleDefinitions = [defMain, defAdd]
} }

View File

@ -19,7 +19,7 @@ ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm
# source-dirs: src # source-dirs: src
executable: executable:
source-dirs: app source-dirs: main
main: Main.hs main: Main.hs
# tests: # tests:
# testall: # testall:

View File

@ -22,7 +22,7 @@ executable really-bad-compiler-in-haskell
Parser.Expression Parser.Expression
Paths_really_bad_compiler_in_haskell Paths_really_bad_compiler_in_haskell
hs-source-dirs: hs-source-dirs:
app main
default-extensions: default-extensions:
OverloadedStrings, LambdaCase OverloadedStrings, LambdaCase
ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm