How does Objective-C compile?
Asked Answered
S

3

11

I'm just curious, does Objective-C compile into C code or does the Objective-C runtime work like a layer of abstraction above the program? Sorry in advance if I don't know what I'm talking about!

Seeder answered 6/3, 2014 at 19:49 Comment(1)
Yes. And no. Maybe. I've never taken it apart, but conceptually it's a C preprocessor combined with a sithload of runtime support. I think in practice they've integrated some of the functionality into the C compiler.Tolle
C
9

Compiling Objective-C into C doesn't make sense, because then it would need to parse the C code and compile it.

Objective-C compiles into machine code. Remember that the language (Objective-C, C, C++) only defines the rules to correctly write code. The compiler checks to see if your code is correct and compiles it, i.e., translates it into executable code.

Also, don't confuse Objective-C language, and the Objective-C runtime. The language defines the syntax, the runtime allows the compiled code to run (in a way it's like you say, it is a layer, but it doesn't get compiled every time with your program).

EDIT:
The runtime implements the core behavior of a computer language. The runtime contains compiled code of functions in a similar way a library does. In C, for example, when you call printf() your code is compiled into machine code and linked with the library containing the implementation of that function; what this machine code does is passing parameters to the executable code in the library.

Climate answered 6/3, 2014 at 20:1 Comment(5)
If Objective-C compiles into machine code, why does it need a runtime? Can it not run off of the processor directly?Seeder
user3318845, a runtime is not a VM. Objective-C does run directly on the processor. The runtime is more like a standard library that implements things like fast code for looking up a method. So it is more like the C standard library, just that it only concerns itself with implementing design patterns commonly needed by object-oriented software. This article might help clear things up: orangejuiceliberationfront.com/runtime-timeVernalize
"Objective-C, as any other language, compiles into machine code." And what does Java compile to?Gigue
@Gigue You're right!!!! I'll edit my answer. Thanks for pointing that out.Climate
@Climate Hi, you say compiler just compile OC to machine code directly, but in the answer, confuse me. #44561785Aura
V
16

A little history lesson:

Both C++ and Objective C originally started out as preprocessors for C. So you typed in your ObjC code, and it would effectively run a search-and-replace over the code and translate the Objective-C commands into straight C code that used a little helper library (the stuff in objc/runtime.h and similar files).

As the language started getting more complex, it was changed into a full parser that replaced/extended the parser in a C compiler with/into one specific to Objective-C. So while it would be perfectly possible to compile Objective-C into straight C, current ObjC compilers don't do it that way anymore.

Vernalize answered 6/3, 2014 at 20:14 Comment(4)
If you ever read the original book by Brad Cox you would know that this is not quite true. The dispatcher cannot be written in C, and requires a special trick.Julienne
What aspect wouldn't compile to C well? I've written several OO runtimes and implemented my own programming languages, some of them very similar to Smalltalk/ObjC, and I can't offhand think of something that's not possible. Of course, I can think of many optimizations you could add that couldn't be done in C, and current ABIs expose some of these details so you couldn't be compatible to those, but as far as making sth. that takes current ObjC source files and outputs C source files that do the same thing, I don't see why that wouldn't be possible.Vernalize
Yes, so have I and although I read the book a long time ago, I recall it rang true. The message dispatcher IIRC doesn't translate into any standard C construct and the various choices (switch, fn pointers, varargs, etc) all just fall short. In any case the compiler was a lot more than just 'search-and-replace', but it was the helper library that needed this special magic.Julienne
I suppose if objc_msgSend() is implemented as a function call like it is these days, then yes, there's really no way to forward the function parameters using straight C. That said, it is still totally possible to write a code generator that essentially pastes the contents of objc_msgSend() in the location where your method call is, and then just calls the IMP directly with the right parameters and signature.Vernalize
C
9

Compiling Objective-C into C doesn't make sense, because then it would need to parse the C code and compile it.

Objective-C compiles into machine code. Remember that the language (Objective-C, C, C++) only defines the rules to correctly write code. The compiler checks to see if your code is correct and compiles it, i.e., translates it into executable code.

Also, don't confuse Objective-C language, and the Objective-C runtime. The language defines the syntax, the runtime allows the compiled code to run (in a way it's like you say, it is a layer, but it doesn't get compiled every time with your program).

EDIT:
The runtime implements the core behavior of a computer language. The runtime contains compiled code of functions in a similar way a library does. In C, for example, when you call printf() your code is compiled into machine code and linked with the library containing the implementation of that function; what this machine code does is passing parameters to the executable code in the library.

Climate answered 6/3, 2014 at 20:1 Comment(5)
If Objective-C compiles into machine code, why does it need a runtime? Can it not run off of the processor directly?Seeder
user3318845, a runtime is not a VM. Objective-C does run directly on the processor. The runtime is more like a standard library that implements things like fast code for looking up a method. So it is more like the C standard library, just that it only concerns itself with implementing design patterns commonly needed by object-oriented software. This article might help clear things up: orangejuiceliberationfront.com/runtime-timeVernalize
"Objective-C, as any other language, compiles into machine code." And what does Java compile to?Gigue
@Gigue You're right!!!! I'll edit my answer. Thanks for pointing that out.Climate
@Climate Hi, you say compiler just compile OC to machine code directly, but in the answer, confuse me. #44561785Aura
V
0

Speaking strictly from Xcode, the code is compiled with the LLVM compiler. Here is more information about the LLVM compiler. You'll be able to find more information about how the LLVM compiler works online through simple Google searches.

Vitalize answered 6/3, 2014 at 19:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.