News for hackers. Stuff that matters.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by ori_b on Saturday November 26, 2016 @03:34AM (#13042540)
I wasn't aware that there was something wrong with the old compilers. Reading the page, I'm still not aware of the motivations behind this.

What does this compiler do better/different than clang or gcc?

Re:Mulle-objc: A New Objective-C Compiler and Runtime by rweichler on Saturday November 26, 2016 @04:11AM (#13042660)
Apple's objective C runtime is not portable. There's a lot of assembly code and platform specific code. This is trying to fix that.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by eridius on Saturday November 26, 2016 @05:07AM (#13042793)
Doesn't at least objc_msgSend have to be assembly?
Re:Mulle-objc: A New Objective-C Compiler and Runtime by pjmlp on Saturday November 26, 2016 @06:57AM (#13043073)
You can do it in plain C, but it will be quite slow.

It isn't only written in Assembly, it is written in clever optimised Assembly fine tuned across several compiler releases, to minimise as much as possible the impact of dynamic dispatch.

Re:Mulle-objc: A New Objective-C Compiler and Runtime by sdegutis on Saturday November 26, 2016 @09:21AM (#13043578)
I don't remember why, but years ago I remember coming to the firm conclusion that it was impossible to write it in anything but assembly.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by ksherlock on Saturday November 26, 2016 @01:32PM (#13045155)
Don't tell that to the GCC Objective C runtime.

With the NeXT/Apple runtime,

    [object selector: arguments...]
is syntactic sugar for

    objc_msgSend(object, @selector(selector:), arguments) 
With the GCC runtime, that's syntactic sugar for:

    IMP tmp = objc_msg_lookup(object, @selector(selector:));
    tmp(object, @selector(selector:), arguments...)

No assembly necessary. I believe there are also compiler flags to assume the function won't be replaced behind your back so objc_msg_lookup can be hoisted out of a loop, etc.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by DHowett on Saturday November 26, 2016 @05:45PM (#13046391)
objc_msg_lookup is very emphatically not objc_msgSend.

Lookup operates differently from the nominally tail-calling msgSend in that it cannot support transparent forwarding (where the receiver is switched to another object before dispatch.) Since objc_msgSend is given control over the final call, the destination can be swapped if necessary.

It's not possible[1] to implement objc_msgSend in plain (portable) C, as it needs to pass on an unknowable number of arguments without damaging the ones passed in registers, the stack frame, or the optional hidden return value pointer. Any methods attempting to do so using, say, naked functions and the method's type encoding will be more fragile than a perfect argument forwarding stub written in assembly.

[1]: __builtin_apply_args, __builtin_apply and __builtin_return (https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Constructing-Ca...) notwithstanding.

Re:Mulle-objc: A New Objective-C Compiler and Runtime by groovy2shoes on Saturday November 26, 2016 @05:50PM (#13046428)
GCC's C is emphatically not "plain" (ANSI/ISO) C, but that does not mean it isn't "portable" C, given that GCC itself targets many platforms, and other compilers (clang, at least) do attempt to support GCC's dialect.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by DHowett on Saturday November 26, 2016 @05:52PM (#13046443)
I couldn't find documentation that supports Clang offering the __builtin_apply intrinsics, but their availability would make a C objc_msgSend somewhat more palatable.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by groovy2shoes on Saturday November 26, 2016 @06:10PM (#13046548)
Ah. I don't mean to be too argumentative. You've helped me to understand why objc_msgSend() could not be done in standard C, and I thank you for that :)

However, in the case of GCC's ObjC implementation, I don't see why it couldn't be implemented in GCC's dialect, thus obviating any need for assembly.

Pardon me while I reflect on (and lament) the fact that we're still stuck using such crude and ancient tools for systems programming...

Re:Mulle-objc: A New Objective-C Compiler and Runtime by lilyball on Monday November 28, 2016 @03:48PM (#13058281)
FWIW, it appears that Mulle-objc gets around this by actually changing the calling convention for Obj-C methods, so the IMP takes a single parameter (beyond self and _cmd) which is a struct that contains all of the method parameters. This way the compiler can just synthesize the struct for you, and then objc_msgSend() itself can be written in C because it just needs to pass the pointer to the struct along to the IMP rather than messing about with forwarding arbitrary arguments.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by groovy2shoes on Wednesday November 30, 2016 @11:45PM (#13077242)
Interesting. Thanks!
Re:Mulle-objc: A New Objective-C Compiler and Runtime by groovy2shoes on Saturday November 26, 2016 @05:46PM (#13046398)
I don't see anything w.r.t. objc_msgSend() that obviously prevents it from being written in C, as opposed to objc_msg_lookup(). I mean, worst case scenario, could it not just be:

    id objc_msgSend(id object, id selector, ...) {
        IMP tmp = objc_msg_lookup(object, selector);
        return tmp(object, selector, ...);
    }
(forgive me for any mistyping here, as I'm not an Obj-C programmer, and for the varargs shorthand, to keep things simple)? Especially given that GCC supports some extensions to C that could reduce the overhead a bit (tail-recursion elimination, for example), I don't see why such a function couldn't be implementable in (GCC) C.

Would you be willing to enlighten me?

Re:Mulle-objc: A New Objective-C Compiler and Runtime by ksherlock on Sunday November 27, 2016 @12:39PM (#13050280)
Conceptually, it is:

    IMP tmp = objc_msg_lookup(object, selector);
    goto *tmp;
but C can't handle that (and even with gcc computed gotos you need to preserve any argument registers and not screw up the stack).

objc_msgSend has a variable number of arguments and no way to determine how many there are.

Re:Mulle-objc: A New Objective-C Compiler and Runtime by DHowett on Saturday November 26, 2016 @05:49PM (#13046419)
It's the ... that's the hard part; I've explained a little more here: https://news.ycombinator.com/item?id=13046391.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by pjmlp on Saturday November 26, 2016 @10:06AM (#13043851)
I don't see why the naive approach to a possible implementation cannot be done in C, even if using extensions to ANSI C.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by chrisseaton on Saturday November 26, 2016 @08:07AM (#13043310)
No. Why? What's magic about assembly code that means you couldn't do the same thing in C?
Re:Mulle-objc: A New Objective-C Compiler and Runtime by ksherlock on Saturday November 26, 2016 @10:07PM (#13047469)
the 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.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by chrisseaton on Sunday November 27, 2016 @06:40AM (#13048719)
Right, 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.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by lilyball on Monday November 28, 2016 @03:50PM (#13058300)
It 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.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by jamessu on Saturday November 26, 2016 @09:58AM (#13043795)
While 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.

Re:Mulle-objc: A New Objective-C Compiler and Runtime by david-given on Saturday November 26, 2016 @12:05PM (#13044622)
My 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.

Re:Mulle-objc: A New Objective-C Compiler and Runtime by chrisseaton on Saturday November 26, 2016 @12:10PM (#13044653)
I think any real world systems C project will have undefined behaviour. I'm sure HotSpot, V8, .net etc are all totally undefined.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by ianai on Saturday November 26, 2016 @04:11AM (#13042662)
The author says to be able to write objective-C in the future. I can get that. I did like objc. He also lists some good technological goals like speed, portability, and the rest on the page.
Re:Mulle-objc: A New Objective-C Compiler and Runtime by timnic on Saturday November 26, 2016 @03:50AM (#13042599)
There is some more information available here: https://www.mulle-kybernetik.com/weblog/2015/mulle_objc_a_ne...