Sherif AbdelFadil
Sherif AbdelFadil

Reputation: 96

How can I resolve RISC-V assembly pseudo instructions to true RISC-V instructions?

I need to build large RISC-V assembly programs (.a/.as/.S files) using a specific toolchain that doesn't support pseudo-instructions. (Synopsys' ASIP designer tool creates a custom assembler for a customized CPU / instruction set.)

The programs assemble fine with GCC (which uses GAS), but the assembler that I need to use does not recognize pseudo-instructions. It only works on programs written only using actual RISC-V machine instructions.

Is there a tool or a GCC or GAS option that I can use to resolve all the pseudo instructions to true RISC-V instructions, without changing the rest of the program?

Upvotes: 4

Views: 2625

Answers (1)

Capybara
Capybara

Reputation: 1473

I am not sure I completely understand your question. But I think that maybe a combination of using a script and objdump or just manually swapping sections of your code may work.

Let's say we have a test.S file with the following content (those are all pseudo-instructions):

start:
    nop
    li a2, 42
    mv a3, a4
    not a5, a6
    neg a7, a0
    beqz t0, 1f
    negw t1, t2
    sext.w t4, t5
    seqz t6, a0
    snez a1, a2
    sltz a3, a4
    sgtz a5, a6
1:
    ret

Using gcc we can compile it into an object using this command: gcc -c test.S -o test.

Now, using the objdump tool we can print the instructions of the object: objdump -d test.

test:     file format elf64-littleriscv


Disassembly of section .text:

0000000000000000 <.L1^B1-0x2c>:
   0:   0001                    nop
   2:   02a00613                li      a2,42
   6:   86ba                    mv      a3,a4
   8:   fff84793                not     a5,a6
   c:   40a008b3                neg     a7,a0
  10:   00028e63                beqz    t0,2c <.L1^B1>
  14:   4070033b                negw    t1,t2
  18:   000f0e9b                sext.w  t4,t5
  1c:   00153f93                seqz    t6,a0
  20:   00c035b3                snez    a1,a2
  24:   000726b3                sltz    a3,a4
  28:   010027b3                sgtz    a5,a6

000000000000002c <.L1^B1>:
  2c:   8082                    ret

objdump prints the content using pseudo-instructions. However, you can use the -M flag to make objdump not to use pseudo-instructions: objdump -d -M no-aliases test.

The result is this:

test:     file format elf64-littleriscv


Disassembly of section .text:

0000000000000000 <.L1^B1-0x2c>:
   0:   0001                    c.addi  zero,0
   2:   02a00613                addi    a2,zero,42
   6:   86ba                    c.mv    a3,a4
   8:   fff84793                xori    a5,a6,-1
   c:   40a008b3                sub     a7,zero,a0
  10:   00028e63                beq     t0,zero,2c <.L1^B1>
  14:   4070033b                subw    t1,zero,t2
  18:   000f0e9b                addiw   t4,t5,0
  1c:   00153f93                sltiu   t6,a0,1
  20:   00c035b3                sltu    a1,zero,a2
  24:   000726b3                slt     a3,a4,zero
  28:   010027b3                slt     a5,zero,a6

000000000000002c <.L1^B1>:
  2c:   8082                    c.jr    ra

You can extract all the instructions from your object files with this.

Upvotes: 4

Related Questions