4 ms·
> When you write C, you should be thiking what happens in assembler, and then writing it beautifylly in C. That is a horrible, horrible idea. It gives people t
by jcranmer 8y ago
> When you write C, you should be thiking what happens in assembler, and then writing it beautifylly in C.
That is a horrible, horrible idea. It gives people the misrepresentation that C is really "just" a thin veneer over assembly language, which it most certainly has not been for at least 20 years. Moreover, it feeds the cult of C, the idea that C is the only language capable of being close to the hardware and that other languages or attempts to build system-level languages need not apply.
C has lots of flaws and faults in the language, but we're not going to be able to make any progress in fixing the language, or more likely replacing it with a better language, if we continue acting as if it is the only way to get low-level access to the hardware.
- yourbandsucks 8y agoI'd offer a different perspective: C is a simple language that obviously maps to a programmer's idea of the assembly they'd write. It biases towards lots of fixed-size stack frames and fixed-layout structs. Writing C code really does feel very close to the "bounded turing machine" metaphor that the CPU gives us. Other languages have their advantages, but C's proximity to assembler, with all the faults included, really is a great pedagogical tool.
- jcranmer 8y ago> C is a simple language that obviously maps to a programmer's idea of the assembly they'd write. It's not. That's my point. You don't need undefined behavior to get to that point, just consider what assembly you get from this code: unsigned prog_a(unsigned a) { return a * 2; } unsigned prog_b(unsigned a) { return a / 3; } What do you think you're going to get for that code? The "obvious mapping" principle for both codes suggest you're going to get a multiply and a division instruction, respectively. On x86, however, what you get is this: prog_a: leal (%rdi, %rdi), %eax ret prog_b: movl %edi, %eax movl $-1431655765, %edx mull %edx shrl %edx movl %edx, %eax ret In other words, the first program turned into a load-effective address instruction, and the latter turned into a multiply-hi instruction. Even something as simple as basic arithmetic gets turned into weird instructions, and which ones get this treatment is essentially a game of roulette based on compiler optimizations and particularly heuristics. We're not even bringing undefined behavior into play here, which is usually the major objection to C-obviously-maps-to-assembly; it's falling flat enough without it. The primary advantage C brings, what people are really touting when they tout its proximity to assembly, isn't that it's close to assembly but that it has a well-known, stable ABI for that mapping. Of course, it turns out that said ABI is actually a bad idea if you want performant modern code on most architectures (the infamous array-of-struct versus struct-of-array problem).
- nwmcsween 8y agoWho would have thought an optimizing compiler would optimize.
- fiedzia 8y agoTons of people repeating "C gets you close to hardware".
- nwmcsween 8y agoIt does though just not the way you planned, a litmus test on how close a languages is to 'the hardware' is to see how much crap a compiler frontend generates that the backend has to remove that doesn't have any positive effect on optimizations.
- fiedzia 8y agoNo, the litmus test would be "can I reason about generated native code produced by this line of C". And you can't do that today. This leaves you two options: either switch to something where you can, going lower than C to be able to control all the details, or stop worrying about, accept the fact that compilers take care of those details for you and use higher-level language.
- nwmcsween 8y agoThis is a feature of optimizing compilers and isn't what people mean when they say it maps to hardware.
- notacoward 8y agoWith optimization turned off, each line of C does map to a fairly obvious sequence of native instructions, including instructions that are actually poking at memory-mapped hardware registers. No hidden constructors/destructors, no operator overloading, no exceptions, no "built in" data types that require thousands of lines of hidden library code mapping to what the hardware actually offers. That doesn't cover things like memory ordering and barriers, but it's still a lot closer than any other language in which software is actually written (so e.g. assembler or LLVM IR don't count). I believe Rust has the same property, but only a tiny fraction as much software is written in it. Ditto for Zig and Nim. C++ has all of the paradigm-breakers mentioned above, Go has its own hidden magic mostly related to goroutines, Java doesn't even try, and anything functional doesn't even occupy the same conceptual space. As a mental exercise, try taking any random piece of generated assembler code and map it back to the corresponding higher-level-language code. I can usually do this pretty easily for C code, though switch/case statements present some challenges and heavy use of macros in any language can defeat the effort. Even calls to compiler-internal functions (e.g. memcpy-like functions for structure assignment) will be easy to interpret. Again, you can probably do the same for Rust/Nim/Zig. Attempting the same for C++ or Go will lead to all sorts of random places other than the original source line, and for just about anything else good luck even finding that generated assembler in the first place. When you can map easily back and forth between the language and CPU instructions, you're as close to the hardware as you're going to get. This is something few programmers - even in kernels and such - actually need, but C offers it and most other languages don't.