Skip to content

Clang Clang Clang – We have a Compiler!

November 7, 2014

I’m delighted to say that the Clang C++ compiler is now working on my macbook to compile for the “leg” machine (“leg” is a subset of the ARM architecture and it’s the basis of this tutorial).  I haven’t really accomplished anything beyond getting the existing code to build and I had a lot of help from the presenter but it’s a start.

I verified the compiler with a simple example

int foo(int a, int b) {
    int result = a + b;   // r0 + r1
    return result;        // r0
}

At first I was taken aback because the output code was quite clunky looking:

	.file	"/Users/billrowe/llvmleg34wclang/llvm-leg-leg_for_release_34/docs/Codeplay/examples/ex1/ex1.c"
	.text
	.globl	foo
	.type	foo,@function
foo:
	sub sp, sp, #20
	str r0, [sp, #16]
	str r1, [sp, #12]
	ldr r2, [sp, #16]
	add r2, r2, r1
	str r2, [sp, #8]
	str r0, [sp, #4]
	mov r0, r2
	str r1, [sp]
	add sp, sp, #20
	bx lr
.tmp0:
	.size	foo, .tmp0-foo

	.ident	"clang version 3.4.2 "
}

but re-compiling with the -O3 option to turn on optimization gave a delightfully minimal result

	.file	"/Users/billrowe/llvmleg34wclang/llvm-leg-leg_for_release_34/docs/Codeplay/examples/ex1/ex1.c"
	.text
	.globl	foo
	.type	foo,@function
foo:
	add r0, r1, r0
	bx lr
.tmp0:
	.size	foo, .tmp0-foo

	.ident	"clang version 3.4.2 "

For my records, I

  • downloaded-as-zip the directory llvm-leg-leg_for_release_34 from https://github.com/frasercrmck/llvm-leg/tree/leg_for_release_34#
  • unzipped that into ~/llvmleg34wclang/llvm-leg-leg_for_release_34
  • downloaded-as-zip the directory llvm-leg-leg_for_release_34 from https://github.com/frasercrmck/clang-leg/tree/leg_for_release_34
  • unzipped that into ~/llvmleg34wclang/llvm-leg-leg_for_release_34/tools and renamed it clang
  • made directory ~/llvmleg34wclang/build
  • added cmake’s directory to the path with PATH=/Applications/CMake.app/Contents/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin
  • in the build directory ran cmake -G “Unix Makefiles” -DCMAKE_BUILD_TYPE:STRING=DEBUG -DLLVM_TARGETS_TO_BUILD:STRING=LEG ../llvm-leg-leg_for_release_34
  • ran make
  • ran ./bin/clang -cc1 -triple leg-unknown-unknown -S -o ex1.s /Users/billrowe/llvmleg34wclang/llvm-leg-leg_for_release_34/docs/Codeplay/examples/ex1/ex1.c to get the unoptimized output
  • and ran ./bin/clang -cc1 -triple leg-unknown-unknown -S -O3 -o ex1.s /Users/billrowe/llvmleg34wclang/llvm-leg-leg_for_release_34/docs/Codeplay/examples/ex1/ex1.c to get the optimized output

Thanks again to Pierre-Andre Saulais of Codeplay Software for helping me get this working on my aged macbook.

Advertisements

From → LLVM

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: