4 ms·
You use a compiler to abstract away the CPU. Because of that, there is no “run as written”. Simple example: if you declare a structure that is 14-bytes in size
by Someone 5y ago
You use a compiler to abstract away the CPU. Because of that, there is no “run as written”.
Simple example: if you declare a structure that is 14-bytes in size, declare variables foo and bar of that type, and then write
foo = bar;
C must behave as if 14 bytes are copied from the memory where bar is stored to the memory where foo is stored, wit two observable states: foo either has its old contents or the new ones. Chances are very high that your CPU cannot do that.
The compiler will instead do something different. It might call memcpy, it might copy 8 bytes first, then 4 more, then the last 2, it might copy the first 8, then the last 8 (copying the two center bytes twice), etc.
Similarly, if you declare an int named i and do
i += 1;
There’s no guarantee that will translate to a single instruction (what if your CPU has 8-bit registers, but your compiler chose to have 16-bit ints?). Even if it does, does “as written” mean an increase instruction or an addition? Your CPU might have neither, but only subtraction and negation.
Also, a literal interpretation of “as written” requires spilling the result of every statement back to memory; a strict literal interpretation even forbids using registers (if you don’t write “copy i into a register, add 1, write the result back to i”, the compiler can’t do that if it has to run your code “as written”)
But yes, it would be awesome if somehow the tooling or the compiler could report some of these things in an easy to visualize not-super-verbose way.
Unfortunately, that isn’t possible. Modern languages and their compilers are just too far abstracted away from modern hardware.
- overgard 5y ago> You use a compiler to abstract away the CPU. Because of that, there is no “run as written”. Um, no. I use a compiler to generate code that would otherwise be very tedious to write. I do not expect it in any way to abstract away the CPU. I do not want it to abstract away the CPU. If I did I would write Java or python. I'm using C++. I am very aware of the CPU. What I'm not aware of is whatever silly transformations the optimizer thinks it can do to my carefully crafted program because I didn't read page 783 of the spec.
- gizmo686 5y agoYou want a macro assembler. C is not a macro assembler. C was never supposed to be a macro assembler. It was designed as a cross platform language, which fundamentally means you are programming to an abstract machine, which necessarily means that you cannot have an macro assembler [0]. I can, however, see an argument that people want a C like language which is a macro assembler. I am lost again when people start asking for a C++ like macro assembler. The features that C++ brings over C are not concepts that exist on any mainstream CPU. [0] Unless you decide to build a concrete machine (either in bare metal, or as a VM).
- co_pilates 5y agoHave any good sources of this topic of macro assemblers? I'm interested in reading about this type of thing. I'll search Google, but you can't always find good sources there today.
- gizmo686 5y agoUnfourtuantly not. The only one I have worked with was really just a normal assembler with a big library of in house macros.
- lokedhs 5y agoI searched a bit, and found a large set of pages with information on the topic in the context of the 360 architecture. I'd say this architecture is unique in that it is still to this day used to write high level software. Put "360 assembler macros" in your search engine and you'll find lots of information. This is a good place to start: https://www.ibm.com/docs/en/zos/2.1.0?topic=language-macro-instructions https://www.ibm.com/docs/en/zos/2.1.0?topic=language-macro-i...
- pjmlp 5y agoYeah, plenty of books at archive.org. "Assembly language for the PC" https://archive.org/details/assemblylanguage00soch https://archive.org/details/assemblylanguage00soch "TASM Reference" https://archive.org/details/bitsavers_borlandturemblerVersion4.0QuickReferenceMar94_5680156 https://archive.org/details/bitsavers_borlandturemblerVersio... "Structured Assembler Language For IBM Microcomputers" https://archive.org/details/structured-assembler-language-for-ibm-microcomputers https://archive.org/details/structured-assembler-language-fo... Or just go to MASM documentation, you will find plenty of directives that look like an high level language https://docs.microsoft.com/en-us/cpp/assembler/masm/directives-reference?view=msvc-160 https://docs.microsoft.com/en-us/cpp/assembler/masm/directiv...
- kaba0 5y agoFeel free to examine the resulting machine code. Godbolt is a really valuable tool.
- brundolf 5y agoSorry to break it to you: https://m-cacm.acm.org/magazines/2018/7/229036-c-is-not-a-low-level-language/fulltext https://m-cacm.acm.org/magazines/2018/7/229036-c-is-not-a-lo...
- aaronmdjones 5y agoEven x86 assembly is not a low-level language: https://blog.erratasec.com/2015/03/x86-is-high-level-language.html https://blog.erratasec.com/2015/03/x86-is-high-level-languag...
- pjmlp 5y agoNot only x86 assembly, microcoded CPUs has always been quite common on CISC designs, hence why 1960's papers tend to refer to Assembly as bytecode.
- overgard 5y agoI've read that and it would take a book to explain whats wrong with it.
- pajko 5y ago> I do not expect it in any way to abstract away the CPU C was especially designed to do that! https://stackoverflow.com/questions/53100198/what-is-the-precise-relationship-between-c-and-its-abstract-machine https://stackoverflow.com/questions/53100198/what-is-the-pre...
- Dylan16807 5y agoBeing caught in the middle of an instruction, like all your examples, is much less surprising than when something never happens at all. > Also, a literal interpretation of “as written” requires spilling the result of every statement back to memory; a strict literal interpretation even forbids using registers (if you don’t write “copy i into a register, add 1, write the result back to i”, the compiler can’t do that if it has to run your code “as written”) If you don't make a pointer to a variable then it's allowed to be in a register, isn't it? There's even a 'register' keyword, however obsolete it may be.
- terminalcommand 5y agoI thought compilers ignored those words when optimizations are turned on, compilers reorganize and rewrite your code. Using an interpreter seems to be the best option to abstract away the CPU. Write the interpreter once, get enough eyeballs onto it, test it 'till death and use it for predictability. Even if you write assembly these days, there is all sorts of ambiguity around. Even the underlying hardware we use can fail. I used to believe that computers did as they're told, they could do no mistakes, all mistakes were human-made. Now I see that there is no such thing as error-free. We are basically living on chance, much is uncertain. I trust my eyes and ears more and more instead of the machine.
- Someone 5y agoI think it’s only less surprising because it happens more often, so that ‘everybody’ knows about it, not because it is inherently less strange. As examples of merging, if you do s = sin(x) c = cos(x) would you find it surprising if that generated a single sincos instruction if the CPU has one? If you do typedef struct s { char a; char b; } s c; […] c.a = 3; c.b = 6; do you find it surprising if the compiler generates a single instruction that is equivalent to (exact code will vary according to the CPU architecture, and this may need a further cast to avoid undefined behavior): *(short *)(&c) = 0x0603;
- phendrenad2 5y agoNo "run as written"? Tell that to interpreted C++.