| 1 |
#!/usr/bin/python |
| 2 |
import sys |
| 3 |
import xml.etree.ElementTree as xml |
| 4 |
|
| 5 |
SKIPZERO = True |
| 6 |
COLORS = True |
| 7 |
|
| 8 |
""" |
| 9 |
A script used to filter intel intrinsics XML and print out instructions |
| 10 |
related with them |
| 11 |
In case of problems with ansi colors on Windows, switch COLORS to False |
| 12 |
Also, it uses \b character hack to "truncate" strings |
| 13 |
(while printing \b moves cursor one character back, so it appears as truncated) |
| 14 |
|
| 15 |
Usage: ./intel-helper.py TECH? |
| 16 |
|
| 17 |
Examples: |
| 18 |
./intel-helper.py AVX2 # filters avx2 instructions |
| 19 |
./intel-helper.py # prints all intrinsics |
| 20 |
|
| 21 |
Setting SKIPZERO means this script won't print intrinsics with no assembly |
| 22 |
instructions related with them. |
| 23 |
This may happen because the XML may not be 100% completed, but also most of |
| 24 |
casts just change type and do not touch the bits |
| 25 |
|
| 26 |
Link to the XML: |
| 27 |
https://software.intel.com/sites/landingpage/IntrinsicsGuide/files/data-3.5.0.xml |
| 28 |
(beware, for a text file, its huge) |
| 29 |
""" |
| 30 |
|
| 31 |
CBLUE = '' |
| 32 |
CGREEN = '' |
| 33 |
CEND = '' |
| 34 |
CBEIGE = '' |
| 35 |
|
| 36 |
if COLORS: |
| 37 |
CBLUE = '\033[94m' |
| 38 |
CGREEN = '\033[92m' |
| 39 |
CEND = '\x1b[0m' |
| 40 |
CBEIGE = '\33[36m' |
| 41 |
|
| 42 |
tech = '' |
| 43 |
filename = '' |
| 44 |
if len(sys.argv) >= 2: |
| 45 |
filename = sys.argv[1] |
| 46 |
else: |
| 47 |
print("Not enough arguments") |
| 48 |
exit(1) |
| 49 |
|
| 50 |
def doc_helper(args, tree): |
| 51 |
tech = '' |
| 52 |
if len(args) != 0: |
| 53 |
tech = args[0] |
| 54 |
|
| 55 |
for child in tree.getroot(): |
| 56 |
if len(tech) != 0 and child.attrib["tech"] != tech: |
| 57 |
continue |
| 58 |
|
| 59 |
name = child.attrib["name"] |
| 60 |
|
| 61 |
instructions = '' |
| 62 |
for instruction in child.findall("instruction"): |
| 63 |
iname = instruction.attrib["name"].lower() |
| 64 |
iform = instruction.get("form") |
| 65 |
|
| 66 |
if iform is None: |
| 67 |
instructions += f"`{iname}`, " |
| 68 |
else: |
| 69 |
instructions += f"`{iname} {iform}`, " |
| 70 |
|
| 71 |
print(f"/// * **Intrinsic:** [`{name}`]") |
| 72 |
print(f"/// * **Assembly:** {instructions[:-2]}", end='\n\n') |
| 73 |
|
| 74 |
def pretty_print_intrinsics_and_instructions(args, tree): |
| 75 |
tech = '' |
| 76 |
if len(args) != 0: |
| 77 |
tech = args[0] |
| 78 |
|
| 79 |
for child in tree.getroot(): |
| 80 |
if len(tech) != 0 and child.attrib["tech"] != tech: |
| 81 |
continue |
| 82 |
|
| 83 |
instructions = child.findall("instruction") |
| 84 |
instr_no = len(instructions) |
| 85 |
|
| 86 |
intrinsic_name = child.attrib["name"] |
| 87 |
intrinsic_name += '(' |
| 88 |
for arg in child.findall("parameter"): |
| 89 |
typ = arg.attrib["type"] |
| 90 |
name = arg.attrib.get("varname", "\b") |
| 91 |
intrinsic_name += f"{CBEIGE}{typ}{CBLUE} {name}, " |
| 92 |
intrinsic_name = intrinsic_name[:-2] + ')' |
| 93 |
|
| 94 |
if instr_no == 0 and SKIPZERO: |
| 95 |
continue |
| 96 |
elif instr_no == 1: |
| 97 |
print(f"{CBLUE}{intrinsic_name}{CEND}", end=': ') |
| 98 |
else: |
| 99 |
print(f"{CBLUE}{intrinsic_name}{CEND}") |
| 100 |
|
| 101 |
for instr in instructions: |
| 102 |
attribs = instr.attrib |
| 103 |
name = attribs["name"].lower() |
| 104 |
form = attribs.get("form", "\b") |
| 105 |
print(f"{CGREEN}{name}{CEND} {form}") |
| 106 |
|
| 107 |
doc_helper(sys.argv[2:], xml.parse(filename)) |
| 108 |
#pretty_print_intrinsics_and_instructions(sys.argv[2:], xml.parse(filename)) |
| 109 |
|