Contemporary Development With Functional Programming

OCaml on iOS 7, Second Progress Report

August 12, 2014

Some estimable savants and polymaths have continued to ask me gently about the status of the OCaml-on-iOS project. I’m happy to say I’ve made some progress in recent weeks, and a few days ago I built my first iOS app with OCaml 4.01.0, and ran it successfully on my iPhone (iOS 7.1.1).

Since the last report here I adapted and applied the OCaml 4.00.0 OCaml-on-iOS patches to the OCaml 4.01.0 compiler. Then I updated the xarm-build script that builds ocamlopt as an ARM cross-compiler. xarm-build is described in Compile OCaml for iOS (soon to be updated).

I got the compiler to build OK but there were two problems with it.

Portland app Portrait Upside Down screen

First, the iOS ABI sometimes needs to move a double precision value to a pair of integer registers. I’ve been representing this in the internal machine code as a standard move (Imove) operation into a double-precision register, and another move into the integer registers. However, the 4.01.0 compiler sees the first move as redundant, and when it tries to optimize it away it fails because it apparently doesn’t expect to see mixed-type move operations.

For now my fix for this is to disable the optimization when the registers have different types. Later it might be more elegant all around to use a different machine code representation for the mixed-type move. In the meantime I think it’s best to continue with the code that has been working, even if it’s clumsy.

Second, the clang runtime defines functions __divsi3 and __modsi3 for doing 32-bit div and mod operations. With the gcc runtime OCaml used __aeabi_idiv and __aeabi_idivmod. This is easy to fix just by changing the names of the functions.

Once the compiler was working I built the Portland app, which does just enough to test whether OCaml is working on the iPhone. Here is the CLI session where I built the app:

$ ocamlopt -version
$ make
/Applications/ -arch armv7 -isysroot /Applications/ -isystem ../lib/ocaml -DCAML_NAME_SPACE   -c -o wrap.o wrap.m
/Applications/ -arch armv7 -isysroot /Applications/ -isystem ../lib/ocaml -DCAML_NAME_SPACE   -c -o main.o main.m
ocamlc  -c wrapper.mli
ocamlopt  -c
ocamlc  -c wrappee.mli
ocamlopt  -c
ocamlc  -c cgAffineTransform.mli
ocamlopt  -c
ocamlc  -c nsTimer.mli
ocamlopt  -c
ocamlc  -c uiDevice.mli
ocamlopt  -c
ocamlc  -c uiLabel.mli
ocamlopt  -c
ocamlc  -c uiApplication.mli
ocamlopt  -c
ocamlopt  -c
ocamlopt  -o Portland \
            wrap.o main.o wrapper.cmx wrappee.cmx cgAffineTransform.cmx nsTimer.cmx uiDevice.cmx uiLabel.cmx uiApplication.cmx portlandappdeleg.cmx -cclib '-framework UIKit -framework Foundation' \
            -cclib -Wl,-no_pie
$ file Portland
Portland: Mach-O executable arm

It doesn’t look like much, but it’s a good feeling to see a new OCaml-on-iOS compiler working after such a long wait. Even better, when I run the app on my iPhone it works perfectly.

I want to try a few more things before releasing this version to the estimable savants and polymaths. (Likely you, reader, are one of these.) If I don’t find any serious problems, a new release will be coming soon.

If you have any comments or encouragement, leave them below or email me at

Posted by: Jeffrey


blog comments powered by Disqus