Subject: Re: PowerPC assembler question

From: Benjamin Herrenschmidt <[email-masked]>
Date: Mon, 10 Jun 2013 09:21:35 +1000

On Mon, 2013-06-10 at 09:01 +1000, Erik de Castro Lopo wrote:
> Hi all,
> 
> I'm trying to fix a problem in the PowerPC backend of the Glasgow
> Haskell Compiler (GHC) and have a problem with the following
> instruction form:
> 
>     lwz     30, .label - (1b)(31)
> 
> Reading the documentation I could find, I have figured out that this
> loads a 16 bit value into register 30. How it calculates that 16 bit
> value has got me somewhat flumoxed.
> 
> Anybody care to explain?

No, this loads a 32-bit value (16-bit would be lhz).

Note: It's more readable if you use the register names, ie:

	lwz	%r30, .label - (1b)(%r31)

The form of lwz is

	lwz	dest_reg, offset(address_reg)

So it will load a 32-bit value from memory at the address contained in
r31 offset by ".label - 1b" which is itself the difference between
two labels, "label", and the first "1:" label before the instruction

(gcc supports numeric labels that can be referenced with the suffix "b"
for backward and "f" for forward which are handy for small
displacements)

So for example if 1: was the base of the structure and .label a field
in the structure, it would load the 32-bit value of that field for the
structure instance starting at %r31.

In this case, this looks more like some kind of position-independent
code though.

Cheers,
Ben.