[LLVMdev] Implementing the ldr pseudo instruction in ARM integrated assembler

David Peixotto
Mon, 11 Nov 2013 22:26:22 -0000

I have attached an initial patch that implements the ldr pseudo. It still
needs some clean up and more tests, but I would like some feedback on the
approach I used and if there are any objections to implementing it this way.
Here is my approach:

Add a finishParse() callback to the target asm parser
  This callback is invoked when the parse has finished
  successfully. It will be used to write out constant pools
  for the ldr pseudo.

LDR pseudo implementation
  Keep a map from Section => ConstantPool
  When parsing ldr r0, =val
    parse val as an MCExpr
    get ConstantPool for current Section
    remember val in ConstantPool (at next available Offset)
    add operand to ldr that is MCExpr of (ConstantPool.Label + Offset)

  On finishParse() callback
    Write out all non-empty constant pool vals to the end of their sections

Missing features
  1. Does not convert load of small constants to mov
     (e.g. ldr r0, =0x1 => mov r0, 0x1)
  2. Does reuse constant pool entries for same constant

It currently passes the tests I shared earlier (including the error message)
except that ldr is not yet converted to mov when possible. Please let me
know if you have any issues with this method of implementation.

Thanks,
-David

-- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted
by The Linux Foundation

> -----Original Message-----
> From: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at
cs.uiuc.edu] On
> Behalf Of David Peixotto
> Sent: Friday, November 01, 2013 11:16 AM
> To: llvmdev at cs.uiuc.edu
> Subject: [LLVMdev] Implementing the ldr pseudo instruction in ARM
> integrated assembler
>
> In an earlier email[1] I proposed adding support for the ldr pseud-
> instruction to the ARM integrated assembler. After some discussion the
> overall consensus seemed to be that it was worth adding. One concern was
> that we needed to have adequate testing. I promised to provide more
> details on what the behavior should be and provide some tests before
> starting the implementation. The FileCheck-ified tests are attached to
> this email. They currently pass when using gcc to assemble the test.
>
> Here are the specific behaviors that are checked in the test:
>
> 1. Check that constants that fit into imm12 are converted to mov
>     ldr r0, =0x3
> 2. Check that large constants are converted to ldr from constant pool
>     ldr r0, =0x103
> 3. Duplicate constants should be merged to the same constant pool location
>     ldr r0, =0x103
>     ...
>     ldr r0, =0x103
> 4. A section defined in multiple pieces is merged and uses a single
> constant pool
>     For example,
>
>     .section e, "ax", %progbits
>       f6:
>         ldr r0, =0x103
>     .section f, "ax", %progbits
>       f7: add r0, r0, r0
>     .section e, "ax", %progbits
>       f8:
>         add r0, r0, r0
>         ldr r0, =0x105
>
>     should only produce one constant pool for section e.
>
> 5. Check that symbols can be loaded using ldr pseudo
>   ldr r0, =foo
>
> 6. Check that ldr pseudo can be used with expressions
>   ldr r0, =0x101+6
>   ldr r0, =bar+4
>
> 7. Check that it is used correctly in a macro
>     .macro useit_in_a_macro
>       ldr r0, =0x101
>       ldr r0, =bar
>     .endm
>     .section k, "ax", %progbits
>     f14:
>       useit_in_a_macro
>
> 8. Check that an error is issued when the constant pool would be placed
> too far away
>   ldr r0, =0x101
>   @... lots of instructions
>   Error: invalid literal constant: pool needs to be closer
>
> I have not yet looked into the code to see where/how this feature can be
> implemented. Any pointers or feedback is welcome. I've filed a Bug[2]
to
> track this issue.
>
> [1]: http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-October/066808.html
> [2]: http://llvm.org/bugs/show_bug.cgi?id=17769
>
> -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
> hosted by The Linux Foundation
>
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-PR17769-Initial-implementation-of-ldr-pseudo.patch
Type: application/octet-stream
Size: 11398 bytes
Desc: not available
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20131111/b0ffc376/attachment.obj>