Psellos
Contemporary Development With Functional Programming

OCaml on iOS 7, Progress Report

May 31, 2014

Sorry I’ve been away, reader, but I was offered a chance to build software for a research project at a great Computer Science Department and I just couldn’t pass up the opportunity. I’ve learned a lot about node.js (no type system, but otherwise very enjoyable). I’ve talked to serious researchers in machine learning and synthetic biology. I share an elevator with the occasional robot, and work in a secret sub-basement DNA lab. In short, it’s an enthralling yet humbling environment for a guy like me.

Vaguely robotic looking Voronoi diagram

In the meantime I’ve heard from people interested in running OCaml on iOS, and I myself am still extremely interested. So I’ve started to work on updating the project to the latest versions of everything, which right now are: OCaml 4.01.0, iOS 7.1, Xcode 5.1. There are new versions of all of these coming out, but there are always new versions of everything.

The first order of business in porting OCaml to iOS is to make contact with the C and assembly toolchain, which have been changing and moving around like everything else. The latest iOS uses clang in place of gcc.

As has been the case for a while, developer tools are in a hideout deep inside the Xcode app, under a directory named Xcode.app/Contents/Developer. For the latest tools you want to look in Toolchains/XcodeDefault.xctoolchain:

Tool Location
C compiler usr/bin/clang
Assembler usr/bin/as

The good news here is that the trickiest things seem to work much as they did before. Surprisingly, my arm-as-to-ios script works without change to convert the arm.S code of OCaml 4.01.0 from Linux to iOS format. I thought writing a script was a good idea, but I didn’t expect it to work quite this well!

Here’s what it looks like:

$ HIDEOUT=/Applications/Xcode.app/Contents/Developer
$ TOOLCHAIN=$HIDEOUT/Toolchains/XcodeDefault.xctoolchain
$ CLANG=$TOOLCHAIN/usr/bin/clang
$ arm-as-to-ios arm.S > armios.S
$ $CLANG -no-integrated-as -arch armv7 -DSYS_macosx -c armios.S
$ otool -tv armios.o | head
armios.o:
(__TEXT,__text) section
_caml_call_gc:
00000000        f8dfc1e0        ldr.w   r12, [pc, #0x1e0]
00000004        f8cce000        str.w   lr, [r12]
00000008        f8dfc1dc        ldr.w   r12, [pc, #0x1dc]
0000000c        f8ccd000        str.w   sp, [r12]
00000010        ed2d0b10        vpush   {d0, d1, d2, d3, d4, d5, d6, d7}
00000014        e92d50ff        push.w  {r0, r1, r2, r3, r4, r5, r6, r7, r12, lr}
00000018        f8dfc1d0        ldr.w   r12, [pc, #0x1d0]

The only real trick here is to tell clang not to use its integrated assembler. The external assembler apparently behaves a little bit more like the previous version.

I am currently applying the patches to the new OCaml compiler sources. I’ll have more results to report soon.

The arm-as-to-ios script is described on the page Convert Linux ARM Assembly Code for iOS. I’ll revise the page for Xcode 5.1, but (as I say) the script itself seems to work as it is.

If you have any comments or encouragement, leave them below or email me at jeffsco@psellos.com.

Posted by: Jeffrey

Comments

blog comments powered by Disqus