Integrated the garbage collector.

This commit is contained in:
Samuel Hammersberg 2023-03-30 12:31:03 +02:00
parent bbe0d77a19
commit b3525db7fd
5 changed files with 21 additions and 12 deletions

View file

@ -135,7 +135,7 @@ defaultStart =
, UnsafeRaw "declare i32 @printf(ptr noalias nocapture, ...)\n"
, UnsafeRaw "declare i32 @exit(i32 noundef)\n"
, UnsafeRaw "declare ptr @malloc(i32 noundef)\n"
, UnsafeRaw "declare void @_ZN2GC4Heap4initEv()\n"
, UnsafeRaw "declare void @_ZN2GC4Heap5allocEm()\n"
, UnsafeRaw "declare void @_ZN2GC4Heap7disposeEv()\n"
, UnsafeRaw "declare external void @cheap_init()\n"
, UnsafeRaw "declare external ptr @cheap_alloc(i64)\n"
, UnsafeRaw "declare external void @cheap_dispose()\n"
]

View file

@ -127,12 +127,13 @@ compileScs (MIR.DData (MIR.Data typ ts) : xs) = do
compileScs xs
firstMainContent :: [LLVMIr]
firstMainContent = []
firstMainContent = [UnsafeRaw "call void @cheap_init()\n"]
lastMainContent :: LLVMValue -> [LLVMIr]
lastMainContent var =
[ UnsafeRaw $
"call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef " <> toIr var <> ")\n"
, UnsafeRaw "call void @cheap_dispose()\n"
, Ret I64 (VInteger 0)
]
@ -169,6 +170,7 @@ emitECased t e cases = do
-- crashLbl <- TIR.Ident . ("crash_" <>) . show <$> getNewLabel
-- emit $ Label crashLbl
emit . UnsafeRaw $ "call i32 (ptr, ...) @printf(ptr noundef @.non_exhaustive_patterns, i64 noundef 6, i64 noundef 6)\n"
emit . UnsafeRaw $ "call void @cheap_dispose()\n"
emit . UnsafeRaw $ "call i32 @exit(i32 noundef 1)\n"
mapM_ (const increaseVarCount) [0 .. 1]
emit $ Br label

View file

@ -225,7 +225,7 @@ llvmIrToString = go 0
(Alloca t) -> unwords ["alloca", toIr t, "\n"]
(Malloca t) ->
concat
[ "call ptr @malloc(i32 ", show t, ")\n"]
[ "call ptr @cheap_alloc(i64 ", show t, ")\n"]
(Store t1 val t2 (Ident id2)) ->
concat
[ "store ", toIr t1, " ", toIr val

View file

@ -15,8 +15,15 @@ compileClang =
readCreateProcess . shell $
unwords
[ "clang++" -- , "-Lsrc/GC/lib/", "-l:libgcoll.a"
, "-fno-exceptions -x"
, "ir"
, "-fno-rtti"
, "src/GC/lib/cheap.cpp"
, "src/GC/lib/event.cpp"
, "src/GC/lib/heap.cpp"
, "src/GC/lib/profiler.cpp"
, "-Wall -Wextra -g -std=gnu++20 -stdlib=libstdc++"
, "-Isrc/GC/include"
, "-x"
, "ir" -- , "-Lsrc/GC/lib -l:gcoll.a"
, "-o"
, "output/hello_world"
, "-"

View file

@ -1,9 +1,9 @@
-- main = head (Cons (sum (repeat 10 5)) Nil);
main = head (Cons (sum (repeat 5 9223372036854775807)) Nil); --9223372036854775807
main = case (bind (fmap (\s . s + 1) (Just 5)) (\s . pure (s + 10))) of {
Just a => a ;
Nothing => minusOne ;
};
-- main = case (bind (fmap (\s . s + 1) (Just 5)) (\s . pure (s + 10))) of {
-- Just a => a ;
-- Nothing => minusOne ;
-- };
---- MAYBE MONAD ----
data Maybe () where {