Getting silly with C, part (void*)2
lcamtuf.substack.com175 points by justmarc 6 months ago
175 points by justmarc 6 months ago
C syntax is already way too rich and complex.
We need a C- ore µC:
No implicit cast except for literals and void* (explicit compile time/runtime casts), one loop statement (loop{}), no switch/enum/generic/_thread/typeof/etc, no integer promotion, only sized primitive types (u64 s32 f32 etc...), no anonymous code block, real compiler hard/compile time constant declaration, many operators have to go (--,++, a?b:c, etc)... and everything I am forgetting right now (the dangerous struct pack attribute...). But we need inline keywords for memory barriers, atomics for modern hardware architecture programming.
There is C0, a stripped-down version of C popular in academia [1]. Great for teaching because it's conceptually simple and easy to write a compiler for. But with a couple of additions (like sized primitive types) it might match what you are imagining
C really just needs if / else / while / and void functions. Function inputs should be in/out (const type* or type*).
Does Zig fit your bill?
Dunno, I should have a look though. But I have recollection of some garbage collector, wrong/right ?
I doubt that, zig is allocators land. Even stdlib datastructures required an allocators to be instanciated. Have a look at the selection of allocators: https://zig.guide/standard-library/allocators .
I had a look, zig seems to require a runtime, even small, for basic syntax support. So it seems it is not suitable.
You should be able to generate machine code without the need of any runtime, like you can with C.
I'm unsure what you're referring to here -- Zig doesn't have any runtime, it doesn't even depend on libc.
The only thing I can think of that you might be referring to is compiler-rt: if so, this is a thing in C too! It's just a small collection of implementations for operations the code generator wants to call into (e.g. memset, arithmetic for integers larger than CPU word size). Clang uses compiler-rt when compiling C code, and GCC's equivalent is libgcc. Nonetheless, Zig lets you disable it using `-fno-compiler-rt`, in which case you'll need to provide the relevant symbols yourself somehow.
This is not what I understood, there is some kind of flags required for pointers or something, which requires a data section for basic primitive types.
I'm afraid I'm not sure what you're referring to. For instance, I can build a simple Hello World in Zig using `zig build-exe`, and get a static executable, on which I can use `nm` to confirm that there aren't symbols from any kind of runtime. I can even trivially build the actual Zig compiler to a static binary.
(For context, by the way, I'm on the Zig "core team"; I'm a notable contributor to the project.)
mmmmh... basically, generates machine code which only requires a stack (which could be used by code paths not written in zig), contained in memory pages with execute and read permission only. Ofc this machine code would interact with the other machine code (written in other languages) via the architecture calling convention (the ABI/C one).