3 ms·
of course there are many semantically equivalent ways to translate from C to assembly, and the optimizer may chose in surprising ways. But the point is that wh
by enriquto 8y ago
of course there are many semantically equivalent ways to translate from C to assembly, and the optimizer may chose in surprising ways. But the point is that when you write your functions, there is always an obvious assembly representation that should be in your head.
- pjmlp 8y agoExcept that representation is dependent on more than 200 possible scenarios of UB, which I doubt any human developer is able to keep on their head.
- jcranmer 8y ago> semantically equivalent Of course, the #1 biggest problem of C is that C's semantics are not what people think they are. Most notably, pointers are not integers in the C abstract machine, nor can you really enforce such a mapping without prohibiting nearly all optimizations [1]. And when you teach people to understand how processors work via C, you start to claim that C's semantics are processor semantics, which has not been the case for a very long time. [1] The underlying issue here is that the model you need for any sane optimization is that if you tell nobody that this box of data exists, then there is there no way for anyone else to refer to it. This basic fact opens up the ability to do things like move variables from stack allocations to registers, eliminate dead stores, etc. It also implies that you need some sort of pointer provenance in the semantic model to track who actually has the ability to refer to something. In practice, a semantic model for C needs to carry pointer provenance through integers as well, but that really means that C's integers also aren't mathematical integers (modulo some power of 2).
- enriquto 8y agopointers ARE ints if pointers are not integers how can you iterate through an arbitrary array using char* ?
- jcranmer 8y agoPointers are integers on most hardware, if you ignore the complexities of multiple address spaces (i.e., a feature present on virtually anything that's not a general-purpose CPU) or segmentation (i.e., the most common CPU architecture for desktop and server processors). In C, however, pointers are not integers. I've not fully built any kind of operational semantics for C, so I can't say what the full list of features you need to track for pointers is for sure, but I can take a stab at how you could describe it. A pointer consists of a tag of the object being stored in the object model (which needs to tell you what the current dynamic type of the object is and its size and alignment in addition to the actual memory contents of the object) as well as an offset of where it points inside that object. Iterating through an array would merely be incrementing (or decrementing, as the case may be) the offset field of the pointer, subject of course to the well-definedness characteristics that you can't exceed one past the end of the object or go before the beginning of the object.
- yourbandsucks 8y agoThat's... not really how it is. At compile time, the compiler knows the type of a pointer, so it can interpret struct_ptr->field and do the proper arithmetic (field is 20 bytes offset into struct). But when running, the pointers are just int64s on the stack. Check stdint.h. It's all right there. Anyone writing C 'gets' roughly what's happening and knows the shape of their structs in memory.