Late last year, we talked about Bismuth, a virtual machine being developed by Eniko Fox, one of the developers of the awesome game Kitsune Tails. Part of a operating systems development side project, Bismuth is a VM (think Java Virtual Machine, not VMware) on top of Fox’ custom kernel, designed specifically to run programs in a sandbox. The first article detailed the origins of Bismuth, and the second article delved into memory safety, sandboxing, and more.
We’re a few months down the line now, and Fox recently published another article in the series, this time explaining how a hello world-program works in Bismuth.
This is the third in a series of posts about a virtual machine I’m developing as a hobby project called Bismuth. I’ve talked a lot about Bismuth, mostly on social media, but I don’t think I’ve done a good job at communicating how you go from some code to a program in this VM. In this post I aim to rectify that by walking you through the entire life cycle of a hello world Bismuth program, from the highest level to the lowest.
↫ Eniko Fox
There’s a ton of detail here, and at the end you’ll have a pretty solid grip on how Bismuth works.
Have a look at the hex editor, in which is a binary of a bismuth program.
https://enikofox.com/media/posts/17/responsive/bismuth-binary-2xl.png
The directory there is called
D:\Programming\C-Sharp\BismuthVM\bin\Debug\net8.0\prog.b…
That means the BismthVM is written in C# and when it is running it is running on a .NET VM. :-/
So running a Bismuth-program on the BismuthVM on the .NET VM.
But the HelloWorld-binary is with 156 bytes really a lot of smaller, then a .NET binary, which includes a MZ DOS-header and a Windows-PE header,
But a Hello World program in Java have also only a 418 bytes small *.class binary.
As far as I know from the article series, Bismuth is written in C. Might just be the directory structure is some default thing or just doesn’t match reality.
I actually addressed this in my blog post. The VM is written in C, but the compiler that compiles from text IR to C as well as to binary IR is for the time being written in C#, because dealing with a whole bunch of string manipulation in C is too painful to do for something that’s supposed to be a hobby project. So no, the VM isn’t running in a VM.
Eniko,
Granted C# has some very nice abstractions, you don’t have to explain it’s benefits to me 🙂
Strings are not all that painful to do in C because you can get used to the boilerplate code. The C syntax leaves something to be desired though, is there a reason you didn’t use C++ instead? I tend to avoid several C++ features, like dynamic typing and polymorphism, even templates I use sparingly. However I always miss OOP when programing in C. C can kind of mimic it, but it’s weak at creating foundations with strong encapsulations and abstractions. Personally given then choice I would choose C++ over C every time. Obviously C/C++ aren’t as safe as managed languages that automatically handle memory management, but there are plenty of decent string classes for C++ that make strings easy. I use my own in a lot of my projects.
I didn’t use C++ because I don’t like writing it.
@Alfman
> I tend to avoid several C++ features, like dynamic typing and polymorphism, even templates I use sparingly
I agree completely and this is why I rarely reach for C++ myself. With something like C#, generics, async, custom iterators, yield return, anonymous lambda functions, and other advanced features are easy and safe to use and so why not (I do not use dynamic typing much–maybe reflection).
I don’t love much of the C++ standard library either. As you say, there are lots of custom string classes but that becomes part of the problem as it is easy to end up with more than one of them in your project. The fact that it is so common to rewrite low-level features in C++ is a failure of the standard library. People do not feel compelled to do that in C# where a string is a string and a CustomClass<> will work with it.
That said, the C++ that SerentiyOS created is quite nice. The way they do error handing, even redefining main, is well designed, as are their memory management constructs. And their string types are very clever (especially memory wise). They have basically created a full soup to nuts SDK for C++. It should be used in other projects.
All only my opinions of course.
Then again, the guy who created most of the Serenity SDK now wants to move to Swift. What does that tell us?
@Alfman
It sounds like your C++ style is basically “C++ the Good Parts” (Javascript reference).
If the choice is simply C vs C++, I agree that C++ has a number of nice features. If you are writing your own code from scratch, you can avoid the bits of C++ you do not like and just have a “better C” which was the original vision for C++ I think.
Eniko,
Not that you owe me an answer, but why do you prefer C? As mentioned, I’m not a fan of all of C++ features, but they are all optional so there’s not really a con to just using the good features like classes and OOP. The problem with C code is that it always turns into a global mess especially scaling up to massive teams. Modern languages all have solutions, including C#. I think the only reason C doesn’t have better scoping mechanisms is because they weren’t experienced with large code bases when it was invented. C could have been a much better language if they had the benefit of hindsight, but of course it’s not their fault they didn’t… they were breaking in new ground!
LeFantome,
I do relatively little programming in C#, but it’s one of my favorite languages because I find the features are well thought out and it tackles complexity fairly well.
I appreciate this point. I don’t know it much to do with C# as a language or if it’s more about learning from the mistakes so many made before them? Older languages like C/C++/perl seem to suffer more from not-invented-here syndrome (ie perl has a standard string, but a bewildering number of date/time classes). The built in language features get created and released to the wild without fleshing out the rest of the framework. C# on the other hand copied a mature java framework, which probably helped establish a stronger framework standard from day 1.
I am not familiar with the reference, but it sounds like a reasonable summation of the way I use C/C++ 🙂
Not only large complex projects, but even simple things like Arduino microcontroller programming. C++ OOP classes are just perfect for logical abstractions and organizing code and writing concise programs without it feeling like a mess of functions like in C.
@Alfman
Apologies that I never got back to this.
What I do not like about C++ is the complexity. But that is a charge I could also level at C# which I like.
And while I can say I like the simplicity of C, I also know that all the undefined behaviour makes it much less simple than it seems.
I suppose my real issue is that I do not like the complexity of other people’s code written in C++. And many projects write so many custom classes that it seems that every C++ project has its own bespoke standard library. I praise SerenityOS but it re-invents absolutely everything.
I like the idea of “C++, the good parts” but it seems that every C++ programmer chooses a different subset for that.
I really don’t quite get what this project is all about and what’s the revolutionary concept behind it.
There isn’t one. I’m just one person working on a VM as a hobby project, not doing anything revolutionary. I mean, maybe the C transpiler is kind of unique?
@Eniko I think this is a great project, please keep us updated.
Thanks! I think I’ve got a few more blog posts in me 😀