Getting started with clang

Hi,

I'm just getting started with clang and having some problems. I'm trying to build a compiler to run on x8_64 and gen code for the powerpc. My goal is to make some changes to the ppc code generator, but I'm not off to a very good start.

I have the latest source tree of llvm and clang from subversion and followed the build instructions. This definitely produces a working native compiler as seen here:

$ clang hello.c
$ ./a.out
Hello, world

However, when I try the '-arch ppc' option, it still only produces x86_64 code:

$ rm a.out
$ clang -arch ppc hello.c
clang: warning: argument unused during compilation: '-arch ppc'
$ ./a.out
Hello, world

I am also seeing different output than that shown in the DriverInternals.html document that comes with clang. In the "Driver Stages" section, there is an example I followed and get very different result from the doc:

Trying to get identical output, I broke hello world into 2 source files. It's trivial source; main() just calls hello() in f_hello.c. I know they compile:

$ rm a.out
[rwuest@garion hello]$ clang main.c f_hello.c
[rwuest@garion hello]$ ./a.out
Hello, world

However, the print-phase option with arch statements has no "bind-arch in the output (as indicated in the DriverInternals.html doc):

$ clang -ccc-print-phases -c -arch i386 -arch x86_64 main.c f_hello.c
0: input, "main.c", c
1: preprocessor, {0}, cpp-output
2: compiler, {1}, assembler
3: assembler, {2}, object
4: input, "f_hello.c", c
5: preprocessor, {4}, cpp-output
6: compiler, {5}, assembler
7: assembler, {6}, object

Have I missed something?

Robert

I'm just getting started with clang and having some problems. I'm
trying to build a compiler to run on x8_64 and gen code for the
powerpc.

The normal clang can do that just fine.

This definitely produces a working native compiler as seen here:

$ clang hello.c
$ ./a.out
Hello, world

However, when I try the '-arch ppc' option, it still only produces
x86_64 code:

You sure? See below... Try the -S option, seeing is believing.

$ rm a.out
$ clang -arch ppc hello.c
clang: warning: argument unused during compilation: '-arch ppc'
$ ./a.out
Hello, world

Have I missed something?

$ cat >t.c
int main() {
   printf("Hello World!\n");
}
mrs $ clang t.c -w
mrs $ a.out
Hello World!
$ file a.out
a.out: Mach-O 64-bit executable x86_64
mrs $ clang -arch ppc t.c -w
mrs $ a.out
Hello World!
mrs $ file a.out
a.out: Mach-O executable ppc
mrs $ clang -arch ppc t.c -w -o - -S
  .machine ppc
  .section __TEXT,__textcoal_nt,coalesced,pure_instructions
  .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
  .section __TEXT,__text,regular,pure_instructions

  .globl _main
  .align 4
_main:
  mflr r0
  stw r31, 20(r1)
  stw r0, 8(r1)
  stwu r1, -64(r1)
  mr r31, r1
  bl "L1$pb"
"L1$pb":
  mflr r2
  li r3, 0
  addis r2, r2, ha16(L_.str-"L1$pb")
  stw r3, 60(r31)
  la r2, lo16(L_.str-"L1$pb")(r2)
  mr r3, r2
  bl L_printf$stub
  mr r2, r3
  lwz r2, 60(r31)
  mr r3, r2
  addi r1, r1, 64
  lwz r0, 8(r1)
  lwz r31, 20(r1)
  mtlr r0
  blr
  .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
  .align 4
L_printf$stub:
  .indirect_symbol _printf
  mflr r0
  bcl 20,31,L_printf$stub$tmp
L_printf$stub$tmp:
  mflr r11
  addis r11,r11,ha16(L_printf$lazy_ptr-L_printf$stub$tmp)
  mtlr r0
  lwzu r12,lo16(L_printf$lazy_ptr-L_printf$stub$tmp)(r11)
  mtctr r12
  bctr
  .section __DATA,__la_symbol_ptr,lazy_symbol_pointers
L_printf$lazy_ptr:
  .indirect_symbol _printf
  .long dyld_stub_binding_helper

  .subsections_via_symbols
  .section __TEXT,__cstring,cstring_literals
L_.str:
  .asciz "Hello World!\n"

Hi Robert,

The problem you are seeing is that -arch is not supported on
non-Darwin platforms, and the driver just ignores it.

-arch implies more than just a way to target a different architecture.
My long term goal is to add a '-triple' option which will be the user
level option for targeting a separate architecture, portably. You can
see my basic plan at:
  llvm.org/PR4127

For now, although I hate to recommend this, it is possible to use the
'-ccc-host-triple' to do what you want:

This does the trick. At least for assembly output. Thanks.