Advertisement

C++ Common Runtime

Started by August 12, 2018 03:43 PM
14 comments, last by Bregma 6 years, 6 months ago

Hey everyone,

I was fiddling these days with Raspberry Pi and emulation software to run good old retro games from my childhood and came across the question why there isn't a common runtime for C++ code. I avoid the L (for language) because I know a lot about the .NET CLR and it's general assembly model.

The idea for a C++ common runtime is to allow precompiled code to run everywhere, on whatever archcitecture like C# does. Why isn't there something like that so we won't need emulation software for our 20 year old games? To prevent the discussion for platform dependent code and C++ optimizations, those are done by the C# CLR too and STL is also similar to the Framework code so bases are there.

What are your thoughts?

Hi!

Technically, it is be possible to write something like that, you can also find something on that line if you look around the internet. You can even compile c++ to javascript to run your application in a browser with Emscripten! In practice this is not really needed or wanted in most cases. A common runtime or a virtual machine would always imply some performance cost (you either need to interpret an intermediate byte code, or run it through a just-in-time compiler), which is kind of undesired for a performance oriented language. It is also relatively maneageble to compile c++ code to native binary code for virtually every platform in existence, as long as it is written keeping portability in mind.

Cheers!

Advertisement
11 hours ago, GenusPongo said:

It is also relatively maneageble to compile c++ code to native binary code for virtually every platform in existence, as long as it is written keeping portability in mind

Sure, you could drive the multiplatform road and recompile for every architecture but this isn't real portable, it is just writing multiplatform code. You can't compile x64 assembly and hope it runs on ARM too ;)

What I mean is a really portable compile once and run everywhere solution. Performance suffering depends in my opinion. If you ship something that is compiled to an instruction set that has to be interpreted again then you're right but shipping as code that could be "finally compiled" to the platform it should run at just now then chances are given that performance is as well or nearly as well as if it was natively compiled for exactly that platform.

Again, it depends on the exact implementation of the Common Runtime but doing incremental compile at startup isn't that problem if it could be a system similar to Steam for example where you could run a game even if it wasn't completely finished downloading from steam. So compiling the minimum of code you need to start the program and asynchronously push remaining code into execution memory frequently until anything is loaded correctly could give certain value here.

I don't want to build something as this yet but was corious to know about the possibility and if (at perfect circumstances) the system could be realized there would be an audience for that

Microsoft made Managed C++, followed by C++/CLR, followed by C++/CX, which are all basically this idea, using their existing CLR ecosystem. 

CERN has a 'C++ as a scripting language' project, which is a runtime interpreter (no JIT) for a subset of C++. 

As mentioned above, you can also compile C++ to JS / asm.js / Web Assembly and then run it via a JS interpreter / JITter, such as V8. If I was going to try and get something like this running, I'd probably just try to glue existing stuff together like that :)

Google also did a safe native code for Web thing (native client? Salt and Pepper?), which I think has been abandoned now, but which allowed cross platform C++ IIRC. 

However, if you want a WORA environment, you probably should just use a nicer language in the first place. C++ is a very flawed language, so if building something fresh, you could just pick a fresh language too! A lot old old software probably isn't portable / standards compliant anyway, so they would need to be ported to your new runtime anyway... 

I think the closest thing we could have is a IL code with a JIT compiler, which is what .NET implements already. 

What about if we just distribute the LLVM IR code, and make the target machine compile it to x86, ARM, or whatever before it runs. The compile time would take quite a while though. (This is a similar idea to web assembly I guess).

http://9tawan.net/en/

Just use Dosbox or some other emulator for your retro games and you are done.

If you want to program something new, use Java (or any Language running on JRE), C# (or any language running on .NET) or some portable scripting language.

Advertisement
8 hours ago, mr_tawan said:

What about if we just distribute the LLVM IR code, and make the target machine compile it to x86, ARM, or whatever before it runs.

Apple effectively does this today. They started requiring you to submit LLVM IR alongside your compiled application a little while back, which allows them to recompile/re-optimise the binary whenever they release a device with a new chip/architecture (or even just develop new IR optimisation passes).

No need to run the compilation on the client if the app store backend can run it once for each model of client hardware...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

In my opinion, the real strength of C and/or C++ is that they are low enough level to allow you maximum control over behavior and code generation. This for example allows you to do platform specific optimizations and use stuff like intrinsics to squeeze out the last drops of performance. If you are not going to do these things (which would effectively be pointless if you want a platform independent IL type thing) then I'm not sure why you wouldn't just use something that was designed with in mind instead of C++. 

1 hour ago, DvDmanDT said:

In my opinion, the real strength of C and/or C++ is that they are low enough level to allow you maximum control over behavior and code generation.

That isn't strictly the case in the modern day.

Clang explicitly complies C/C++ down to a portable intermediate psuedo-"assembly language", which can either be interpreted, or more commonly, passed to a backend for native code generation. GCC uses Gimple to similar effect. Most other compilers that I am aware of internals of, operate on a similar model (although the IR format isn't typically publicly documented).

Hell, these days one quite often cross-compiles C/C++ to WebASM (Unity and Unreal's web players both work this way). WebASM is typically executed in the browser, but folks have implemented native execution on a wide variety of hardware...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

2 minutes ago, swiftcoder said:

That isn't strictly the case in the modern day.

Clang explicitly complies C/C++ down to a portable intermediate psuedo-"assembly language", which can either be interpreted, or more commonly, passed to a backend for native code generation. GCC uses Gimple to similar effect. Most other compilers that I am aware of internals of, operate on a similar model (although the IR format isn't typically publicly documented).

Hell, these days one quite often cross-compiles C/C++ to WebASM (Unity and Unreal's web players both work this way). WebASM is typically executed in the browser, but folks have implemented native execution on a wide variety of hardware...

You're right. I worded my point incredibly bad. I wasn't really talking about the exact output in terms of instructions, but rather the exact output in terms of behavior. You can optimize your data structures to fit a particular architecture with respect to things like alignment and cache behaviors and what not. You obviously don't have to do that, and it may be difficult when writing code intended to be portable, but I'd argue that if you don't, then you are missing out on what I consider the biggest strength of C++ and might as well use some other language/tech that was designed with the use case in mind (completely ignoring existing code that is. It's obviously a lot more complicated and a lot more factors involved).

This topic is closed to new replies.

Advertisement