Life So Short, the Craft So Long to Learn

OCaml 4.00.0 Working on iOS

July 31, 2012

I’ve been having a great time modifying OCaml 4.00.0 to work on iOS, and today I was able to build the Portland example app with my modified compiler, and run it on an iPod Touch. It seems to be running normally (knock on wood), so things are looking very good. Before releasing it I want to test some more demanding apps, and also clean up a few loose ends.

I call the compiler OCamlXARM because it runs on a Mac, and cross-compiles for ARM-based iOS devices. It requires Apple’s Xcode, which contains the necessary cross-compilation toolchain and the Cocoa Touch libraries. Right now my test version builds for armv7/Thumb, which means the generated apps only run on moderately recent iOS devices (including all iPads). I don’t foresee any problem creating a version of OCamlXARM for armv6/ARM, which will support all the historical iOS devices. It should also be possible to generate code for armv7/ARM. It should just be a matter of working through the details.

If you’re interested in trying a prerelease, send me an email. I recommend this for the adventurous only. The less adventurous can try version 1.0.15 of OCamlXARM, which is based on OCaml 3.10.2 (admittedly, a rather old OCaml release).

The newly revamped ARM code generation in OCaml 4 is the work of Benedikt Meurer of the University of Siegen. My modifications just add support for iOS, while leaving the existing Linux support in place. For any brothers and sisters working at low levels, here are the changes I’ve made so far.

  • The runtime assembly language file arm.S needs to be translated into proper form for the Apple assembler. Rather than doing this by hand, I wrote a Python script named arm-as-to-ios that does the translation. In fact, its output works both with Linux and Apple assemblers. For those interested, I’ve written a blog post about arm-as-to-ios.

  • Make the same modifications to the code emitted by the OCaml compiler itself. The native code OCaml compiler works by emitting assembly code and then passing it to an external assembler.

  • Revamp code emitted for jump tables. The Apple assembler disagrees about the meaning of the jump table code emitted by the stock compiler, and the resulting code crashes. The new emitted code is intended to work for both Linux and Apple assemblers.

  • Add support for Apple’s slightly different linkage to C. In essence, floating values are passed and returned in pairs of integer registers rather than in floating registers.

I’ll test more thoroughly, then I’ll release the OCamlXARM patches and binary. The new version will be OCamlXARM 3.1. I’m looking forward to trying out the new OCaml language features, and maybe doing some timing tests to see if armv7/Thumb code is faster than the armv6/ARM code generated by the previous OCamlXARM release.

If you have comments or questions, please leave them below, or email me at

Posted by: Jeffrey


blog comments powered by Disqus