Rewrite Objective-C to C

Hello everybody,

I am new to this list, and my inexperience in compiler technology is likely to shine in my questions. Here we go...

I am interested in trying to use clang to convert Objective-C code to C, and compiling the generated C code with 3rd party compiler. I have managed to convert and compile a very simple hello application on my Mac using Apple's runtime after having tweaked the generated code. I have failed with anything more complex than the hello in one file.

I wonder:
* How realistic and feasible my approach is?
* How mature the rewrite code in clang is?
* Is there enough community interest to actually make the rewrite code complete?

Cheers,
Dado

Hi Dado,

Hi Nico,

I executed the following commands:

clang -rewrite-objc Hello.m
g++ -o Hello Hello.cpp -lobjc

First I got errors about missing types, such as id. When I added #import <objc/objc-runtime.h>, I got type conflicts. After I removed all generated code until my first #import, Hello.cpp compiled OK.

I also noticed that if I #import a header file (.h) in Objective-C code, the same file is #included in the generated C code. When the file contains Objective-C syntax, the C compiler barfs.

/Dado

Hi Dado,

This is realistic, but you will need to translate all the runtime libraries and headers also. The Objective-C rewriter only "lowers" ObjC to C, it doesn't turn (for example) Cocoa calls into MFC calls or something like that. To use it in practice, you need to be able to run the code for the frameworks you use through it to. If you're using GNUStep or something like that, it should be possible in theory.

-Chris

IIRC, it translates to C++, not C.

What problem are you trying to solve? If you want to compile ObjC code on OS X with a different compiler, then this is possible. If you want to target a different platform, then you will need to tweak the rewriter to support the GNU runtime (not difficult, and something that's on my todo list, but very low down).

If you are targeting OS X, then you must have a good reason for picking your other compiler instead of GCC or LLVM. Presumably language support isn't the reason. If it is performance, then you will have some other issues since the rewriter does not produce particularly good code. As I recall, it looks up the selectors from strings on every message send (which is very expensive) while this is usually done at module load time. If you are using GNUstep instead of Cocoa then you will find most headers are safe to use with #include instead of #import (the FSF-GCC guys tried to drop support for #import a few years ago, made everyone change their code, and then realised that it was a silly idea since Apple weren't going to drop #import in their branch and break everyone's code), but third-party frameworks may not be. One option here is to use the clang preprocessor and then the rewriter on the preprocessed output and then pass this to your other compiler.

David

Hi David,

I am trying to exploit optimizations of vendor specific compilers for an application with intensive mathematics. As I am ultimately targeting a different platform, it seems that I would have to tweak the rewriter, as well as write a new runtime because GNU runtime has license issues in commercial use when compiled with other compilers than GCC.

Thanks for your help!

/Dado