I’ve been experimenting and put together a couple of interesting things.
Geeky tech discussion incoming…
Runtime Environment
Not complete, but I’ve made a good start on a runtime environment for a strongly typed, garbage collected language:
-
A type system that supports native types (bool, integers, floats etc…), strings, typed arrays and typed dictionaries (still need to add user types, aka classes)
-
A single threaded, incremental garbage collector that supports global roots, stack frames and pinned objects.
-
The GC is non-compacting, non-relocating and just manages memory over the top of a regular memory heap (eg: malloc/free).
-
The underlying heap is pluggable so I can use Cantabile’s internal non-blocking heap manager on the audio thread.
-
All the internal structures used by the GC are O(1)
for insert/delete
-
A complete GC cycle is typically O(n)
where n is the number of allocated objects.
-
Both the mark and sweep phases of the GC are incremental so the entire cost of the garbage collection can be amortized over a long period of time (instead of having to cause a long GC induced, glitch provoking pause).
-
The incremental aspect of the GC is tunable in that it can be told how much work to do each cycle, but I need to come up with an algorithm to tune this as it runs.
-
The GC algorithm is a tri-color garbage collector with write barriers.
-
The entire runtime/GC is written in good old plain C (not even C++). While I wouldn’t want to develop full time with it, C is such an elegantly simple but powerful language that I really enjoy working with when going low level.
All that’s to say, it seems like a garbage collected language running on the audio thread should be possible.
Language Compiler
Although the runtime sounds cool, it is a complete pain to use. Even just writing some simple tests for it was painful.
So I’ve started on a compiler for a C# inspired scripting language. By C# inspired I mean a language with basically the same syntax, but more easily hosted and more aimed at scripting (without requirement for namespaces/classes, without a huge class library but with support for top-level statements). The language will be strictly single threaded with the GC running on the same thread.
So far, I’ve built a tokenizer, a parser and set of classes to represent the abstract tree - mostly copied from an existing, abandoned project.
Next to do is the semantic analysis phase which is where the bulk of the work will be.
Code generation will be done by generating C code and pushing it through Tiny C Compiler for on-the-fly just-in-time in-memory machine code generation. Fun!
The compiler is being developed in C# and will be hostable directly in Cantabile.
Future
What’s clear from the above is there’s a lot of work to be done to build this - but it’s the kind of project I find really interesting and challenging.
So, I’m going to continue playing with this as a side project (so as to not distract from continued Cantabile development) and if it ends up something useful then good, if not then I’m sure I’ll have learned something along the way.