Live data from Hacker News

Can you recreate Objective-C in C?

orangejuiceliberationfront.com

21–30 of 41 posts

Re: Can you recreate Objective-C in C?

#21
This post seems to confuse "shouldn’t it be possible to recreate an Objective-C program in straight C" and "can you recreate the ObjC runtime in C". The answer to the second is no, because of the reason mentioned. You can't jump to an arbitrary function with any set of arguments and get the right calling conventions in C. But you can easily call objc_msgSend from C; you just need to cast it to the right function signature first.

The real problem is that there's also no way in C to express the ObjC metadata properly. You could in principle register all the classes/protocols/methods at run time, I suppose, but that would be slow and gross.

Re: Can you recreate Objective-C in C?

#22
post #4

Earlier quoted context omitted.

There's a lot of caching involved. And it brings a lot of benefits.

There's no inline caching is there though? And so no 'mother of all optimisations' inlining. Or is my understanding of how Objective C works mistaken? You wouldn't dream of implementing a dynamic language these days designed for performance without basic polymorphic inline caching. But Objective C seems to get by fine without it. Maybe it's not as essential as we think.

Do any statically compiled languages have inline caches? This would be a huge loss for ObjC, since it would greatly increase memory pressure by dirtying every text page.

ObjC has per-class out-of-line caches keyed by the selector (intern'd string).

It's true that there's no hope of inlining across a message send, but ObjC's C and C++ compatibility mitigates a lot of this expense. Apps routinely use C and C++ for perf critical sections.

Also some of Apple's APIs are inline C functions (NSMakeRect, etc) and others are designed to minimize dynamic dispatch. Iterating an NSArray typically requires only one message send (see the NSFastEnumeration protocol).

There's also more exotic techniques like IMP caching.

Re: Can you recreate Objective-C in C?

#23

Earlier quoted context omitted.

There’s no inlining, because determining whether inlining wpuld be safe is probably Turing-complete. However, IMP cacheing is a thing and that’s enough for most cases. For the rest there is C and (Objective-)C++.

Your comment was killed very quickly, strangely, but I though it had valid content so I revived it. The actual issue against inlining is that anybody can intercept method calls, even from places that the compiler cannot know about such as bundles loaded at runtime. So this isn’t even an issue with Turing-completeness; it’s an impossible problem to solve at compile time.

> at compile time

That's the key thing - other languages which solve this solve it dynamically.

Re: Can you recreate Objective-C in C?

#24

Earlier quoted context omitted.

Your comment was killed very quickly, strangely, but I though it had valid content so I revived it. The actual issue against inlining is that anybody can intercept method calls, even from places that the compiler cannot know about such as bundles loaded at runtime. So this isn’t even an issue with Turing-completeness; it’s an impossible problem to solve at compile time.

> at compile time That's the key thing - other languages which solve this solve it dynamically.

Yeah, that’s what a JIT does. Unfortunately that’s not something that Apple really wants to open the door to on their platforms, especially for native code.

Re: Can you recreate Objective-C in C?

#25
Ok, maybe I'm missing something, I'm not C nor Objective-C dev but...

Language is just a specification, right? Which is just a document.

Then you have to implement that language using that spec: interpreter, compiler, VM, runtime, whatever.

So I was always under impression that you could implement any language in any other language (almost; sometimes with the help of another, more low-level lang, say assembly, C etc.).

It is like saying "Java has no Tail-Call optimization (yet), hence you can't implement Scheme (which requires TCO) in Java". Surely you can! Just use trampolines or even use your own stack and calling convention.

What am I missing?

Re: Can you recreate Objective-C in C?

#26
post #25

Ok, maybe I'm missing something, I'm not C nor Objective-C dev but... Language is just a specification, right? Which is just a document. Then you have to implement that language using that spec: interpreter, compiler, VM, runtime, whatever. So I was always under impression that you could implement any language in any other language (almost; sometimes with the help of another, more low-level lang, say assembly, C etc.…

I think what you are missing is that while any Turing complete system can solve the same problems as any other, not all programming languages have the same functionality. An easy example is that some languages are typed and some are untyped. Some have objects (Objective C) and some don't (C). Can you replicate the functionality of Objective C in C. Apparently not completely.

Re: Can you recreate Objective-C in C?

#27
post #25

Ok, maybe I'm missing something, I'm not C nor Objective-C dev but... Language is just a specification, right? Which is just a document. Then you have to implement that language using that spec: interpreter, compiler, VM, runtime, whatever. So I was always under impression that you could implement any language in any other language (almost; sometimes with the help of another, more low-level lang, say assembly, C etc.…

I think what you are missing is that while any Turing complete system can solve the same problems as any other, not all programming languages have the same functionality. An easy example is that some languages are typed and some are untyped. Some have objects (Objective C) and some don't (C). Can you replicate the functionality of Objective C in C. Apparently not completely.

But that is my point: if host language doesn't have feature X, you still can implement that feature yourself, can't you?

If host language is untyped, then write your own type system.

Another example: Java has no co-routines, has no continuations, but Kotlin (written in Java) does. They compile Kotlin code with co-routines into finite state machines in Java.

Re: Can you recreate Objective-C in C?

#28
post #4

Earlier quoted context omitted.

There's a lot of caching involved. And it brings a lot of benefits.

There's no inline caching is there though? And so no 'mother of all optimisations' inlining. Or is my understanding of how Objective C works mistaken? You wouldn't dream of implementing a dynamic language these days designed for performance without basic polymorphic inline caching. But Objective C seems to get by fine without it. Maybe it's not as essential as we think.

AFAIK objc_msgSend is always called, but it's very heavily optimized and written in assembly language. Mike Ash had a great post: https://www.mikeash.com/pyblog/friday-qa-2017-06-30-dissecti...

Re: Can you recreate Objective-C in C?

#29
post #17

"C is a language that is turing-complete. So is Objective-C. So shouldn’t it be possible to recreate an Objective-C program in straight C?" Major misunderstanding about turing completeness. A language is so much richer than simply enabling you to write a map from inputs to outputs. Sure you could probably rewrite my Haskell program in Fortran in the sense that the Fortran program produces the exact same outputs for a…

Yup. To think of it another way: one way of "recreating a Foo program in Bar" would be to write a Foo compiler or interpreter in Bar, and then use that interpreter to run your original Foo program. Problem solved!

Re: Can you recreate Objective-C in C?

#30
post #21

This post seems to confuse "shouldn’t it be possible to recreate an Objective-C program in straight C" and "can you recreate the ObjC runtime in C". The answer to the second is no, because of the reason mentioned. You can't jump to an arbitrary function with any set of arguments and get the right calling conventions in C. But you can easily call objc_msgSend from C; you just need to cast it to the right function sign…

[deleted]
Post reply on HN