[PATCH] D116100: [Utils][LoongArch](5/6) Add a --endian option to extract-section.py

Lu Weining via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 21 04:02:27 PST 2021


SixWeining created this revision.
SixWeining added reviewers: myhsu, xen0n, rengolin, zixuan-wu, simoll.
SixWeining requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This a split patch of D115862 <https://reviews.llvm.org/D115862> which adds a `--bits-endian` option to
extract-section to make it possible to print bits in specified endianness.
It means that we can print instruction encoding of some targets like LoongArch
as bits[0] to bits[31] from right to left by specifing `--bits-endian little`.

Depends on D115861 <https://reviews.llvm.org/D115861>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116100

Files:
  llvm/utils/extract-section.py


Index: llvm/utils/extract-section.py
===================================================================
--- llvm/utils/extract-section.py
+++ llvm/utils/extract-section.py
@@ -59,6 +59,9 @@
             help='Print out in bits')
     arg_parser.add_argument('--byte-indicator', action='store_true',
             help='Whether to print a \'.\' every 8 bits in bits printing mode')
+    arg_parser.add_argument('--bits-endian', metavar='<little/big>', type=str,
+            choices=['little', 'big'],
+            help='Print out bits in specified endianness (little or big), default to big')
     format_group.add_argument('-h', dest='format', action='store_const', const='hex',
             help='Print out in hexadecimal')
     arg_parser.add_argument('--hex-width', metavar='<# of bytes>', type=int,
@@ -66,7 +69,7 @@
 
     arg_parser.add_argument('--help', action='help')
     arg_parser.set_defaults(format='bits', tool_path='llvm-readobj', input_file='-',
-            byte_indicator=False, hex_width=4)
+            byte_indicator=False, hex_width=4, endian='big')
     args = arg_parser.parse_args()
 
     raw_section = get_raw_section_dump(args.tool_path, args.section, args.input_file)
@@ -75,6 +78,7 @@
     for line in raw_section.splitlines(False):
         if line.startswith('Hex dump'):
             continue
+        print(line)
         parts = line.strip().split(' ')[1:]
         for part in parts[:4]:
             # exclude any non-hex dump string
@@ -82,7 +86,10 @@
                 val = int(part, 16)
                 if args.format == 'bits':
                     # divided into bytes first
-                    for byte in [(val >> off) & 0xFF for off in (24,16,8,0)]:
+                    offsets = (24,16,8,0)
+                    if args.endian == 'little':
+                        offsets = (0,8,16,24)
+                    for byte in [(val >> off) & 0xFF for off in offsets]:
                         for bit in [(byte >> off) & 1 for off in range(7, -1, -1)]:
                             results.append(str(bit))
                         if args.byte_indicator:
@@ -91,6 +98,8 @@
                     assert args.hex_width <= 4 and args.hex_width > 0
                     width_bits = args.hex_width * 8
                     offsets = [off for off in range(32 - width_bits, -1, -width_bits)]
+                    if args.endian == 'little':
+                        offsets = [off for off in range(32 - width_bits, -1, -width_bits)]
                     mask = (1 << width_bits) - 1
                     format_str = "{:0" + str(args.hex_width * 2) + "x}"
                     for word in [(val >> i) & mask for i in offsets]:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116100.395637.patch
Type: text/x-patch
Size: 2666 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211221/a3bc09c0/attachment.bin>


More information about the llvm-commits mailing list