i want to implement a common lisp subset using llvm for fun.
This requires the use of a garbage collector. I read the docs, but many things are still unclear to me.
One of the important things to recognize about LLVM’s GC support is that it only provides facilities which must be provided by the compiler back end. There are many other necessary facilities which must be provided by a complete language implementation. For the parts of the system where it doesn’t need to be involved, LLVM simply gets out of your way.
- how are collectors supposed to find all living objects? there is llvm.gcroot for marking objects on the stack,but how do collectors crawl heap objects? I did not see a way to provide custom mark functions. Are collectors supposed to rely on the type informations provided by llvm?
Registering roots (e.g. globals) is a facility your runtime should provide. You could use a module initializer to call into the runtime and accomplish this.
Traversing the object graph is also outside of LLVM’s scope. Your code would need to emit whatever metadata is necessary for it to correctly traverse the graph at runtime.
- what about gcreading and gcwriting objects of a different type than i8*?
Simply bitcast to/from i8*. If you don’t need write barriers, you would be advised to ignore gcread and gcwrite entirely, since currently they will only pessimize your code as compared to direct loads and stores.
- No Intrinsic for allocating gc memory?
Again, that’s a feature of the runtime. Simply write a function to do so. For efficiency, your compiler may want to inline part of allocation if you’re using a bumpptr allocator.
- When passing a gc managed pointer to a c function,how should that function access the pointer? directly?
Generally speaking, yes, directly. If you are using a moving collector and need to pin or copy an object, LLVM neither helps nor hinders your efforts.
there seem to be functions for that in the runtime/ source directory,but it also seems to be meant for the llvm c frontend? (and not for use in an extra c library)
I’m not sure what you mean here.
- some collectors update pointers upon read/write access, can the llvm gc api provide this?
gcread easily allows the pointer loaded from the derived pointer to be updated when it is read. Updating the object pointer from gcread or gcwrite may be possible, but is not straightforward within the framework.