File: chex.py

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (29 lines) | stat: -rwxr-xr-x 988 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env python3
# Check HEX -- a stupid filter that allows hexadecimal literals to be checked
# for in LLVM IR FileCheck tests. It works by replacing occurrences of
# strings matching the regex /< (i[0-9]+) \s+ (0x[0-9A-Fa-f]+) >/x with the
# decimal literal equivalent that would really appear in printed LLVM IR.

import re
import sys

hex = re.compile(r"""<(i([0-9]+)\s+)0x([0-9A-Fa-f_]+)>""")


def hexReplace(match):
    # Integer type is match group 1
    ty = match.group(1)
    # Integer bit width is match group 2
    bits = int(match.group(2))
    # Hex value is match group 3
    value = int(match.group(3).replace("_", ""), base=16)
    # LLVM prints the decimal value as if it's two's-complement signed in
    # the given bitwidth, so the printed value will be negative if
    # greater than 2^(bits - 1)
    if value >= (1 << (bits - 1)):
        value -= 1 << bits
    return ty + str(value)


for line in sys.stdin:
    print(re.sub(hex, hexReplace, line), end="")