Writing the garbage collector with LLVM
Garbage collection is a technique of memory management where the collector tries to reclaim the memory occupied by objects that are no longer in use. This frees the programmer from of being required to keep track of the lifetimes of heap objects.
In this recipe, we will see how to integrate LLVM into a compiler for a language that supports garbage collection. LLVM does not itself provide a garbage collector, but provides a framework for describing the garbage collector's requirements to the compiler.
Getting ready
LLVM must be built and installed.
How to do it…
We will see in the following recipe how the LLVM IR code, with garbage collection intrinsic functions, is converted to the corresponding machine assembly code:
Write the test code:
$ cat testgc.ll declare i8* @llvm_gc_allocate(i32) declare void @llvm_gc_initialize(i32) declare void @llvm.gcroot(i8**, i8*) declare void @llvm.gcwrite(i8*, i8*, i8**) define i32 @main() gc "shadow-stack" { entry: ...