3 ms·
No. Why? What's magic about assembly code that means you couldn't do the same thing in C?
by chrisseaton 10y ago
No. Why? What's magic about assembly code that means you couldn't do the same thing in C?
- jamessu 10y agoWhile in this case I can't think of any specific reasons beyond performance to need to implement objc_msgSend in assembly, there are absolutely instances where assembly is the only option. Example: when performing a context switch in a kernel, assembly is required since C abstracts away architecture-specific details necessary to swap out the register set, swap the page directory pointer, etc.
- david-given 10y agoMy ObjC-fu is pretty old --- the last time I looked at it seriously was when poc was still a thing --- but my understanding is: Traditionally objc_msgSend is in assembly because it doesn't know how many and what types the message parameters are. So it can't portably preserve them while it does the computations to find out where to send the message to. This project's alternative obj_msgSend always passes parameters in a structure. That helps, because now you know that all message calls have three parameters; the destination object, the selector, and the parameter block. However... they don't always pass the return value in the message block; instead it coerces any integer types to void* and returns them in the usual way, only using an extra field in the parameter block for those values which aren't coerceable to void. This does make me kinda unhappy. I don't think* it'll be a problem, but... it's definitely pushing the limits of what C allows; round-tripping arbitrary integers through pointers is one of those things that's not technically allowed but all real-world compilers support, so, um. I'd much rather they just pass everything through the parameter block. But yeah, it's a decent enough idea, which simplifies things considerably at not a lot of performance hit. I'm kind of surprised that ObjC doesn't already do that. My main disappointment is that this is a clang variant and not a standalone preprocessor. Since poc died there isn't a working ObjC preprocessor, which means if you want to use ObjC you're limited to gcc or clang. An ObjC->C preprocessor would solve that.
- chrisseaton 10y agoI think any real world systems C project will have undefined behaviour. I'm sure HotSpot, V8, .net etc are all totally undefined.
- ksherlock 10y agothe objc_msgSend function doesn't know what it's return type is or how many arguments it has. It needs to lookup the actual function then jump to it without modifying the stack or any of the parameter registers. It simply doesn't follow the calling conventions and C can't handle it.
- chrisseaton 10y agoRight, true. I guess I'm used to working on languages where you can stick everything into a uint64, but that isn't the case with something like returned structs. if you controlled the calling convention you could do it in C.
- eridius 10y agoIt appears that controlling the calling convention is exactly what Mulle-objc is doing. It's layering a new convention on top of C calling conventions, where objc_msgSend() is given a pointer to a struct containing the arguments. This way the compiler can synthesize the struct for you, and objc_msgSend() itself can be written in vanilla C.