4 ms·
I think it would be more interesting to compile to LLVM IR, so you don't have to deal with C. Part of the appeal of Wasm is that it's a modern system that doesn
by IshKebab 3y ago
I think it would be more interesting to compile to LLVM IR, so you don't have to deal with C. Part of the appeal of Wasm is that it's a modern system that doesn't have to deal with 70s nonsense.
- shermantanktop 3y agoC is not a large language, and a transpiler can choose to generate code that only uses a subset of the features of the language, and can choose to be consistent about safety mechanisms that the underlying language doesn't guarantee. When you say "70s nonsense" are there specific C features you are concerned about? I would think that transpilation can just avoid bad practices like passing around void * pointers and then casting them optimistically, or even the use of char * for strings in favor of a bounds-checked alternative.
- lmm 3y agoC can't represent lots of information that would be useful, e.g. while C99 has a limited "restrict" function, you can't express any more complex detail of "these things can only alias under these circumstances". If the hardware supports doing anything useful with sum types (e.g. atomic operations), there is no way to expose that in the C model. C specifies a concept of trap representations but anything you would actually do with them is undefined behaviour, so you can't write code that e.g. safely errors out when someone tries to use an uninitialized pointer. Even for arithmetic on standard hardware, the ability to fully express even quite basic things is limited - you can't write code that e.g. permits signed overflow to occur and handles it if it does. You effectively can't use any floating point rounding modes except the most broken one, at least not if your code ever uses threads. I'm sure there's more.
- turbolent 3y agoYou do not have to deal with the generated C, simply consider it the IR. The main benefit of generating C over LLVM IR is portability: C is supported by far more systems than LLVM can target. For example, it enables porting Rust applications to Mac OS 9 (https://twitter.com/turbolent/status/1617231570573873152 https://twitter.com/turbolent/status/1617231570573873152), or porting Python to all sorts of operating systems and CPUs (https://twitter.com/turbolent/status/1621992945745547264 https://twitter.com/turbolent/status/1621992945745547264). The main "goal" of w2c2 so far has been allowing to port applications and libraries to as many systems as possible. For more information, see the README of w2c2.
- IshKebab 3y agoYou do have to deal with the generated C though. Unless you just generate it and throw it away?
- brundolf 3y agoThat would 1. Put more onus for optimization on the converter 2. Mean you can't target as many platforms
- connicpu 3y agoTo be fair for point 1, if you have LLVM IR you can just run the full LLVM optimizer suite over it. But overall LLVM IR is a poor target because it constantly changes with every LLVM release.
- brundolf 3y agoLLVM does some optimizations, but compilers that target it normally do their own optimizations before generating the IR, because they know more than LLVM does about the higher-level source language and what can be done with it So eg, you may generate LLVM IR directly and only get LLVM's optimizations, or you may generate C and compile it with Clang and get Clang's optimizations + LLVM's optimizations You could always implement your own pre-LLVM optimizations in your LLVM IR generator, but as I think we all know, that's a huge amount of extra work (which is the OP's point)
- logicchains 3y ago>I think it would be more interesting to compile to LLVM IR C is a stable format that's backwards compatible for decades; LLVM IR changes with every LLM release. Unnecessarily tying stuff to a LLVM version is a nightmare waiting to happen.
- kaba0 3y agoObligatory ‘C is not a low-level language’ blog post. Standard C doesn’t specify any specifically low-level detail, no cache, no vector instructions, nothing.
- remexre 3y agoBut we're not expecting that the produced C (or LLVM IR) has already had target-specific optimization applied, so as long as the C compiler is an optimizing compiler, that's not a problem?
- flohofwoe 3y agoLLVM IR is a moving target, and AFAIK actually also target platform specific because of things like different per-CPU/per-platform ABIs. C on the other hand is standardized, stable, ABI agnostic and compiles on pretty much anything that has a CPU.