[darcs-devel] [Fwd: Re: patch: Pass CFLAGS to assembler]

Duncan Coutts dcoutts at gentoo.org
Wed Aug 15 17:26:53 UTC 2007


> - -------- Original Message --------
> Subject: Re: [darcs-devel] patch: Pass CFLAGS to assembler
> Date: Wed, 15 Aug 2007 13:04:26 +0200

> On Wed, Aug 15, 2007 at 11:28:55 +0100, Dave Love wrote:
> > > Thanks! This will be going in on the next round.
> > 
> > I doubt it should.  I don't understand why it should be necessary on
> > GNU/Linux (I'm pretty sure it wasn't when I was running Debian on
> > SPARC), and it isn't necessary on Solaris.  Could you explain? 

Suppose you are on a sparc system where gcc is configured to default to
the V7 ABI. Now suppose we pass -mcpu=v9 in the CFLAGS. Without
Lennart's patch this translates into passing -optc-mcpu=v9 to ghc. This
means ghc will call gcc with -mcpu=v9 when it is compiling .hc files to
assembler .S files. gcc will generate assembly output using V9
instructions. Next, ghc will call gcc again to assemble the .S file to
a .o file, but this time it will not pass -mcpu=v9 to gcc because -optc
only applies to the compilation phase, -opta is for the assembler phase.
So what happens is that gcc calls the underlying assembler (gnu gas or
some other native system assembler) which will then reject the file
since it contains V9 instructions but it was being called to target the
default V7 ABI. So it fails.

Now if we also pass -opta-mcpu=v9 then ghc calls gcc with -mcpu=v9 and
the assembler accepts the V9 instructions in the .S file.

> > Worse than that, CFLAGS are not necessarily appropriate for the assembler:
> > for instance, my Solaris installation uses Sun `as' with gcc, and that
> > won't accept `-mcpu'.  Actually, now I think about it, gas won't
> > either, and it wouldn't make sense -- the compiler back end deals with
> > instruction sets and scheduling.

That's right, the underlying assembler probably has totally different
flags. But we're not passing -mcpu to the underlying assembler that gcc
uses, we're passing them to gcc (that's what ghc's -opta does) and gcc
knows how to translate it appropriately to the underlying assembler.

The reason it's confusing is that ghc separates the compilation into two
phases with two separate invocations of gcc. Normal C build systems
would use: gcc -mcpu=v9 -c foo.c -o foo.o and gcc would take care of
calling the assembler and passing the right abi flags.

What ghc is doing however is like this:
  ghc -optc-mcpu=v9 -c Foo.hs -o Foo.o
which calls:
  gcc -mcpu=v9 -c Foo.hc -o Foo.S
  gcc          -c Foo.S  -o Foo.o
and this fails in the case that gcc defaults to the V7 ABI.

So what Lennart's patch does is change it so we do:
  ghc -optc-mcpu=v9 -opta-mcpu=v9 -c Foo.hs -o Foo.o
which calls:
  gcc -mcpu=v9 -c Foo.hc -o Foo.S
  gcc -mcpu=v9 -c Foo.S  -o Foo.o
which works.

-- 
Duncan Coutts : Gentoo Developer
email         : dcoutts at gentoo dot org



More information about the darcs-devel mailing list