| 1 |
# Copyright 2016 Intel Corporation |
| 2 |
# Copyright 2016 Broadcom |
| 3 |
# Copyright 2020 Collabora, Ltd. |
| 4 |
# Copyright 2025 Valve Corporation |
| 5 |
# SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
from mako.template import Template |
| 8 |
from mako import exceptions |
| 9 |
from isa import isa |
| 10 |
|
| 11 |
# XXX: deduplicate |
| 12 |
def to_alphanum(name): |
| 13 |
substitutions = { |
| 14 |
' ': '_', |
| 15 |
'/': '_', |
| 16 |
'[': '', |
| 17 |
']': '', |
| 18 |
'(': '', |
| 19 |
')': '', |
| 20 |
'-': '_', |
| 21 |
':': '', |
| 22 |
'.': '', |
| 23 |
',': '', |
| 24 |
'=': '', |
| 25 |
'>': '', |
| 26 |
'#': '', |
| 27 |
'&': '', |
| 28 |
'*': '', |
| 29 |
'"': '', |
| 30 |
'+': '', |
| 31 |
'\'': '', |
| 32 |
} |
| 33 |
|
| 34 |
for i, j in substitutions.items(): |
| 35 |
name = name.replace(i, j) |
| 36 |
|
| 37 |
return name |
| 38 |
|
| 39 |
def safe_name(name): |
| 40 |
name = to_alphanum(name) |
| 41 |
if not name[0].isalpha(): |
| 42 |
name = '_' + name |
| 43 |
|
| 44 |
return name.lower() |
| 45 |
|
| 46 |
def chunk(x): |
| 47 |
chunks = [hex((x >> (32 * i)) & ((1 << 32) - 1)) for i in range(4)] |
| 48 |
while len(chunks) > 1 and chunks[-1] == '0x0': |
| 49 |
chunks.pop() |
| 50 |
|
| 51 |
return f'{{{", ".join(chunks)}}}' |
| 52 |
|
| 53 |
def instr_is_trivial(I): |
| 54 |
return len(I.dests + I.sources + I.immediates + I.modifiers) == 0 |
| 55 |
|
| 56 |
def extract_bits(x, bitset="r"): |
| 57 |
parts = [] |
| 58 |
n = 0 |
| 59 |
|
| 60 |
for r in x.bits.ranges: |
| 61 |
parts.append(f'bits({bitset}, {r.start}, {r.size}, {n})') |
| 62 |
n += r.size |
| 63 |
|
| 64 |
return ' | '.join(parts) |
| 65 |
|
| 66 |
template = """ |
| 67 |
#include "disasm-internal.h" |
| 68 |
|
| 69 |
% for en in isa.enums: |
| 70 |
UNUSED static const char *enum_${safe_name(isa.enums[en].kind)}[] = { |
| 71 |
% for n, v in isa.enums[en].values.items(): |
| 72 |
[${n}] = "${v.display or ''}", |
| 73 |
% endfor |
| 74 |
}; |
| 75 |
% endfor |
| 76 |
|
| 77 |
% for k, enc in isa.encodings.items(): |
| 78 |
static void |
| 79 |
print_${k}(struct disasm_ctx *ctx, BITSET_WORD *orig, uint64_t v, bool source) |
| 80 |
{ |
| 81 |
BITSET_WORD r[] = { v, v >> 32 }; |
| 82 |
% for i, case in enumerate(enc.cases): |
| 83 |
${"} else " if i > 0 else ""}if ((v & ${hex(case.exact.mask)}ull) == ${hex(case.exact.value)}ull) { |
| 84 |
% for mod in case.modifiers: |
| 85 |
% if mod.name == 'hint': |
| 86 |
if (${extract_bits(mod)} == 0) { |
| 87 |
fprintf(ctx->fp, "# missing hint"); |
| 88 |
} |
| 89 |
|
| 90 |
% endif |
| 91 |
% endfor |
| 92 |
_print_operand(ctx, (struct operand_desc){ |
| 93 |
% if case.kind is not None: |
| 94 |
.kind = KIND_${case.kind.name.upper()}, |
| 95 |
% if case.kind.kind == 'signed': |
| 96 |
.value = util_sign_extend(${extract_bits(case.kind)} << ${case.kind.shift}, ${case.kind.bits.size()}), |
| 97 |
% else: |
| 98 |
.value = ${extract_bits(case.kind)} << ${case.kind.shift}, |
| 99 |
% endif |
| 100 |
% endif |
| 101 |
% for mod in case.modifiers: |
| 102 |
% if mod.name == 'mask': |
| 103 |
.count = util_bitcount(${extract_bits(mod)}), |
| 104 |
% elif mod.name == 'count': |
| 105 |
.count = (${extract_bits(mod)}) ?: 4, |
| 106 |
% elif mod.name == 'zx': |
| 107 |
.sx = ${extract_bits(mod)} ^ 1, |
| 108 |
% else: |
| 109 |
.${mod.name} = ${extract_bits(mod)}, |
| 110 |
% endif |
| 111 |
% endfor |
| 112 |
% for prop, v in case.properties.items(): |
| 113 |
.${prop} = ${v}, |
| 114 |
% endfor |
| 115 |
.optional = source, |
| 116 |
}); |
| 117 |
% endfor |
| 118 |
} else { |
| 119 |
fprintf(ctx->fp, "# XXX: Invalid value 0x%"PRIx64" for ${k}", v); |
| 120 |
ctx->error = true; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
% endfor |
| 125 |
|
| 126 |
% for k, instr in isa.instrs.items(): |
| 127 |
% if not instr_is_trivial(instr): |
| 128 |
static void |
| 129 |
print_${instr.name}(struct disasm_ctx *ctx, BITSET_WORD *r) |
| 130 |
{ |
| 131 |
% for i, op in enumerate(instr.dests): |
| 132 |
print_${op.kind}(ctx, r, ${extract_bits(op)}, false); |
| 133 |
% endfor |
| 134 |
% for i, op in enumerate(instr.sources): |
| 135 |
print_${op.kind}(ctx, r, ${extract_bits(op)}, true); |
| 136 |
% endfor |
| 137 |
% for imm in instr.immediates: |
| 138 |
print_immediate(ctx, ${'true' if imm.kind == 'signed' else 'false'}, ${extract_bits(imm)}); |
| 139 |
% endfor |
| 140 |
% for mod in instr.modifiers: |
| 141 |
% if mod.name != 'length': |
| 142 |
% if mod.kind in isa.enums: |
| 143 |
print_enum(ctx, enum_${mod.kind}, ${extract_bits(mod)}); |
| 144 |
% else: |
| 145 |
print_modifier(ctx, "${mod.display}", ${extract_bits(mod)}); |
| 146 |
% endif |
| 147 |
% endif |
| 148 |
% endfor |
| 149 |
} |
| 150 |
|
| 151 |
% endif |
| 152 |
% endfor |
| 153 |
struct disasm_spec decodings[] = { |
| 154 |
% for k, instr in isa.instrs.items(): |
| 155 |
{ "${instr.display if instr_is_trivial(instr) else f'{instr.display:<8}'}", ${'NULL' if instr_is_trivial(instr) else f'print_{instr.name}'}, ${instr.length_bit}, ${instr.length[0]}, ${instr.length[-1]}, ${chunk(instr.exact.mask)}, ${chunk(instr.exact.value)}, ${chunk(instr.mask())} }, |
| 156 |
% endfor |
| 157 |
}; |
| 158 |
|
| 159 |
signed |
| 160 |
agx2_disassemble_instr(BITSET_WORD *code, FILE *fp, unsigned offset, bool verbose) |
| 161 |
{ |
| 162 |
return _disassemble_instr(code, fp, decodings, ARRAY_SIZE(decodings), offset, verbose); |
| 163 |
}""" |
| 164 |
|
| 165 |
try: |
| 166 |
print(Template(template).render(isa = isa, safe_name = safe_name, chunk = chunk, instr_is_trivial = instr_is_trivial, extract_bits = extract_bits)) |
| 167 |
except: |
| 168 |
print(exceptions.text_error_template().render()) |