From d42de52f78524942f49bcf231f6114ebf5e63315 Mon Sep 17 00:00:00 2001 From: sudoer777 Date: Fri, 22 Sep 2023 23:59:31 -0500 Subject: [PATCH] Generate LLVM program that returns 0 when run --- README.md | 4 ++-- {app => main}/Eval/Expression.hs | 0 {app => main}/LLVMGen/Expression.hs | 22 ++++++++++++++++++++-- {app => main}/Main.hs | 0 {app => main}/Objects/Expression.hs | 0 {app => main}/Parser/Expression.hs | 0 package.yaml | 2 +- really-bad-compiler-in-haskell.cabal | 2 +- 8 files changed, 24 insertions(+), 6 deletions(-) rename {app => main}/Eval/Expression.hs (100%) rename {app => main}/LLVMGen/Expression.hs (68%) rename {app => main}/Main.hs (100%) rename {app => main}/Objects/Expression.hs (100%) rename {app => main}/Parser/Expression.hs (100%) diff --git a/README.md b/README.md index f26cbc7..ff890c3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 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 @@ -42,5 +42,5 @@ Main repo: https://git.sudoer.ch/me/really-bad-compiler-in-haskell - Dependencies: llvm, clang - IDE: VSCodium - Git platform: Forgejo -- AI: Phind +- AI: Phind (GPT-4), ollama (codellama) - Search: Kagi, Stack Overflow diff --git a/app/Eval/Expression.hs b/main/Eval/Expression.hs similarity index 100% rename from app/Eval/Expression.hs rename to main/Eval/Expression.hs diff --git a/app/LLVMGen/Expression.hs b/main/LLVMGen/Expression.hs similarity index 68% rename from app/LLVMGen/Expression.hs rename to main/LLVMGen/Expression.hs index e501bd0..f3a0d28 100644 --- a/app/LLVMGen/Expression.hs +++ b/main/LLVMGen/Expression.hs @@ -5,6 +5,9 @@ import qualified Objects.Expression as Expr import LLVM.AST import qualified LLVM.AST as AST import LLVM.AST.Global +import LLVM.AST.Constant +import LLVM.AST.Type +import LLVM.AST.Name import LLVM.Context import LLVM.Module @@ -13,6 +16,21 @@ import Data.ByteString.Char8 as BS int :: Type 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 = GlobalDefinition functionDefaults { name = Name "add" @@ -27,7 +45,7 @@ defAdd = GlobalDefinition functionDefaults body = BasicBlock (Name "entry") [ Name "result" := - Add False -- no signed wrap + AST.Add False -- no signed wrap False -- no unsigned wrap (LocalReference int (Name "a")) (LocalReference int (Name "b")) @@ -38,7 +56,7 @@ defAdd = GlobalDefinition functionDefaults module_ :: AST.Module module_ = defaultModule { moduleName = "basic" - , moduleDefinitions = [defAdd] + , moduleDefinitions = [defMain, defAdd] } diff --git a/app/Main.hs b/main/Main.hs similarity index 100% rename from app/Main.hs rename to main/Main.hs diff --git a/app/Objects/Expression.hs b/main/Objects/Expression.hs similarity index 100% rename from app/Objects/Expression.hs rename to main/Objects/Expression.hs diff --git a/app/Parser/Expression.hs b/main/Parser/Expression.hs similarity index 100% rename from app/Parser/Expression.hs rename to main/Parser/Expression.hs diff --git a/package.yaml b/package.yaml index af19751..edb5381 100644 --- a/package.yaml +++ b/package.yaml @@ -19,7 +19,7 @@ ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm # source-dirs: src executable: - source-dirs: app + source-dirs: main main: Main.hs # tests: # testall: diff --git a/really-bad-compiler-in-haskell.cabal b/really-bad-compiler-in-haskell.cabal index 25ecb34..fc851e2 100644 --- a/really-bad-compiler-in-haskell.cabal +++ b/really-bad-compiler-in-haskell.cabal @@ -22,7 +22,7 @@ executable really-bad-compiler-in-haskell Parser.Expression Paths_really_bad_compiler_in_haskell hs-source-dirs: - app + main default-extensions: OverloadedStrings, LambdaCase ghc-options: -threaded -Wall -j8 +RTS -A64M -RTS -fllvm