0

I am trying to change the endianity of multibyte data(no larger than generic type) stored on dwarf stack. I have gone through dwarfv5 DW_OP_xxx operators but not able to find any operator to swap bytes. although i am able to do it using list of operations like shift/mask/bitwise-and but that creates a near about 25 DW_OPs for each time i want to make that changes.

I am aware of dwarf_subroutines, do anybody know if we have any single(or two) dwarf operator(s) to swap the bytes of top of the dwarf stack value. (or any enhancement issues for DWARFv5) or any non-standard extensions.

Thanks in advance.

Chirag Patel
  • 1,141
  • 1
  • 11
  • 23

1 Answers1

0

The DWARF stack machine has bitwise operations - shifts, AND, OR.

A byte swap for a two byte value would be, in regular notation:

(a >> 8) | (a << 8)

If translated to the stack machine language, assuming that a is on the stack top in the beginning, this might go:

              #a
dup           #a a
const1u 8     #8 a a
swap          #a 8 a
shr           #a>>8 a
swap          #a a>>8
const1u 8     #8 a a>>8
swap          #a 8 a>>8
shl           #a<<8 a>>8
or            #Bingo!

For a 4 byte constant, it would be more involved. You'd have to isolate bytes using AND, shift them into their right positions, and finally OR them together.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • Thanks, i am currently doing the same and have opened a dwarf enhancement request for addition of single operator DW_OP_byte_swap to accomplice that. issue 191107.1: http://dwarfstd.org/ShowIssue.php?issue=191107.1 – Chirag Patel Jan 10 '20 at 18:09