Fil-C as a compiler target for managed languages
Fil-C is a dialect of C that is memory safe. Besides being safer, it also comes with an integrated garbage collector. That makes it an interesting compiler target for managed languages.
Where I’m coming from
I’d already been thinking about creating a new programming language for a long time when I started. As it got closer, I tried to anticipate what the main obstacles would be and prepare for them.
I already had knowledge of and experience with optimization and code generation, so I was comfortable with that. There’s also multiple open source backends such as LLVM to build on.
Parsing didn’t worry me either. I had already written several little parsers for toy languages, either for practise or to explore some new concept. I knew to avoid parser generators after having tried Antlr many years ago. Antlr is very good for what it is, but it’s much better to write a recursive descent parser by hand, or using a parser combinator library.
The search for a good collector
I expected garbage collection to be the biggest challenge I had to overcome. I’d read enough to know it’s not a small effort to get good performance.
The most widely used open source collector is the Boehm GC, but I had a negative impression of it going back a long time. It just seemed every program built on it was buggy and slow.
I was also aware of the Memory Pool System (MPS) and while it’s both fast and reliable, it didn’t use to have a permissive license. Nowadays it does, though.
I was delighted to discover the Perceus algorithm maybe a year before I started. Here was a solution that was easy to implement, didn’t add a complex dependency to the project, and was said to be competitive with GCs that had been tuned for years! However, I never got around to implementing it and gradually became disillusioned with it. I realized it was designed to solve problems caused by idioms in functional programming languages, such as iterating over a recursive data structure and allocating copies of every node. I think it’s better to encourage efficient idioms instead, and in most cases that means for loops and dynamic arrays, not trees.
Fil-C
When Fil-C was announced I was intrigued that it’s possible to make a memory safe C, but I overlooked the fact it comes with a garbage collector. That aspect of it doesn’t get as much attention. I guess because more people are looking for a safer C than for a C with garbage collection. But for managed languages with performance aspirations it’s actually a very good fit, if nothing else as a stepping stone.
By generating C and compiling with Fil-C I hope to get competitive performance for minimal effort. Not as fast as MPS, but close enough. The fact it was designed as a drop-in solution for legacy programs means it doesn’t require any additional work beyond generating C.
Closing words
Before I get to that though, I need to finish the front end of the compiler. My prediction that memory management would be the biggest challenge was probably false, given how much time I’ve already spent on the type system and related aspects of the front end, such as overloading.
Over the past few years I’ve also become more interested in compiling to other managed languages, mainly Javascript and Wasm. I think the initial focus on machine code was a holdover from when I started thinking about these things 20 years ago, before the web became what it is.
I already have a prototype backend for JS actually. More about that when I have time to revisit it.
Stefan