iphone + LDC, a new ARM adventure

Dan Olson zans.is.for.cans at yahoo.com
Thu Jan 30 08:56:03 PST 2014


A small step, but over a big hurdle.  Ran D code + druntime on an
iPhone.  Nothing fancy, but initialized runtime with rt_init() and did
some dynamic array operations.  I include a snippet of silly D program I
ran at end of post.

The big hurdle has been ldc assertion failures in backend/codegen phase.
I have been studying llvm/clang code, but that takes time.  So I tried
an experiment.

If ldc2 skips the backend phase, it can generate IR (option -output-ll)
just fine for iOS target -mtriple=thumbv7-darwin.  Then question is, can
we use llc to do the backend codegen?  Like this:

  ldc2 -output-ll   [target and compile options]
  llc -filetype=obj [similar target and compile options]

It works!  Could I compile all of druntime this way? Yes!  Will it
run on an iPhone?  Yes again.

Still need to figure out the assertions but now have a work around so I
can move on with getting more D stuff (classes, exceptions, threads,
etc) working on iOS.

I also discovered that clang does a bunch of target munging, so -target
armv7-apple-darwin becomes:

  clang -cc1 -triple thumbv7-apple-ios5.0.0 -target-cpu cortex-a8

in the compile phase.  What is good for clang must be good for ldc.  It
does affect the register usage and probably the calling convention.

So when I edited druntime makefiles and used the two step build
(ldc2/llc), I used options:

ldc2 -mtriple=thumbv7-apple-ios5.0.0 -mcpu=cortex-a8 -disable-fp-elim -float-abi=soft
llc -mtriple=thumbv7-apple-ios5.0.0 -float-abi=soft -mcpu=cortex-a8 -disable-fp-elim -O3 -filetype=obj

Note: this is with llvm-svn trunk and ldc git HEAD with a small number
of hacks in ldc tree just to get things going.

Following is some code I ran.  It is kicked off by calling dfun() from
the iOS app's main() C functions.

extern (C) int dfun()
{
    puts("Hello from dfun");
    foo("foo");
    return 2014;
}

struct Foo {
    this(int x, int y) {
        this.x = x;
        this.y = y;
        printf("Foo(%d, %d)\n", x, y);
    }
    ~this() {
        puts("~Foo");
    }
    int x, y;
}

void foo(string p)
{
    string s = "I am a D function: " ~ p;
    puts(s.ptr);

    auto f = Foo(1,42);

    printf("%d\n", f.x + f.y);

    int[] ar = [42];
    scope(exit) puts("done");

    foreach (i; 0..11) {
        printf("hello %d\n", i);
        ar ~= i;
    }

    foreach (i; ar) {
        printf("ar %d\n", i);
    }
}


More information about the digitalmars-d-ldc mailing list