[PATCH] Fix implementation for Thumb ADR instruction

Mihail Popa mihail.popa at gmail.com
Mon Jul 1 05:15:05 PDT 2013


Hi Tim.

I think that my template method will actually work for all offsets. Let's
take a case study: ldr r0, [r1, #-4]
This is probably the most common offset-ed addressing mode. On ARM mode it
maps to LDRi12.
LDRi12 has an operand of type "addrmode_imm12".

Lets look at two key methods for this operand type:
1. Creation: addMemImm12OffsetOperands (ARMAsmParser.cpp:1969)
2. Decoding: DecodeAddrModeImm12Operand(ARMDisassembler:2059)

In the creation function, if you add a cout just before the first
addOperand, you will see that the actual value held by MCOperand is "-4".
Also if you look at the decoder, you will see that it reads the magnitude,
then adjusts the sign according to the U bit while treating INT32_MIN as
negative zero.

This points to the reverse of the statements above: generally offsets are
encoded in straight 2's complement arithmetic and converted to/from
encoding format.
They are not held in MCOperands in encoding format! As such the template
function should generally work.

Do you agree with this assessment?

Regarding the shifting of the immediate for ADR's PC-relative operand, I
can of course have it shifted in the MCOperand if you insist.

Please let me know.

Regards,
Mihai



On Thu, Jun 27, 2013 at 11:47 AM, Tim Northover <t.p.northover at gmail.com>wrote:

> Hi Mihail,
>
> Good to see you posting again too.
>
> > Please correct me if I'm wrong, but ARM does indeed use 2's complement
> > arithmetic! There is indeed one slight deviation in that INT32_MIN is
> > sometimes regarded as negative zero (maybe that's a relic for a distant
> > dinosaur that used direct representation?) Either way, all ARM cores
> > that I can think of use 2's complement with this little occasional quirk.
> > Am I wrong here?
>
> Ah, I see the branches do indeed use 2s complement. And the runtime
> arithmetic is too, of course. But the offset versions of load/store
> encode the immediate as a sign and distance.
>
> So after your explanation I think I'm happy to let the template go in
> at the start, with a name change to indicate what instructions it
> applies to better ("memory offset" is a valid concept for ldr/str
> too).
>
> >I believe that that answer is "straight, natural" representation,
> > but as I said, I'm not looking to fight a war
> > over this. Just adding my two pence to the discussion.
>
> It's a discussion that's been had multiple times in the past. As you
> say, probably not worth pursuing again.
>
> Cheers.
>
> Tim.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130701/fe03e0a1/attachment.html>


More information about the llvm-commits mailing list