| 1 |
#encoding=utf-8 |
| 2 |
|
| 3 |
# Copyright (C) 2021 Collabora, Ltd. |
| 4 |
# SPDX-License-Identifier: MIT |
| 5 |
|
| 6 |
import argparse |
| 7 |
import sys |
| 8 |
from valhall import valhall_parse_isa |
| 9 |
from mako.template import Template |
| 10 |
from mako import exceptions |
| 11 |
|
| 12 |
parser = argparse.ArgumentParser() |
| 13 |
parser.add_argument('--xml', required=True, help='Input ISA XML file') |
| 14 |
|
| 15 |
args = parser.parse_args() |
| 16 |
|
| 17 |
(instructions, immediates, enums, typesize, safe_name) = valhall_parse_isa(args.xml) |
| 18 |
|
| 19 |
SKIP = set([ |
| 20 |
# Extra conversions |
| 21 |
"S8_TO_S16", |
| 22 |
"S8_TO_F16", |
| 23 |
"U8_TO_U16", |
| 24 |
"U8_TO_F16", |
| 25 |
|
| 26 |
# Saturating multiplies |
| 27 |
"IMUL.s32", |
| 28 |
"IMUL.v2s16", |
| 29 |
"IMUL.v4s8", |
| 30 |
|
| 31 |
# 64-bit support |
| 32 |
"IADD.u64", |
| 33 |
"IADD.s64", |
| 34 |
"ISUB.u64", |
| 35 |
"ISUB.s64", |
| 36 |
"IMULD.u64", |
| 37 |
"IMULD.u64", |
| 38 |
"LSHIFT_AND.i64", |
| 39 |
"RSHIFT_AND.i64", |
| 40 |
"LSHIFT_OR.i64", |
| 41 |
"RSHIFT_OR.i64", |
| 42 |
"LSHIFT_XOR.i64", |
| 43 |
"RSHIFT_XOR.i64", |
| 44 |
|
| 45 |
# VAR_TEX |
| 46 |
"VAR_TEX_SINGLE", |
| 47 |
"VAR_TEX_GATHER", |
| 48 |
"VAR_TEX_GRADIENT", |
| 49 |
"VAR_TEX_DUAL", |
| 50 |
"VAR_TEX_BUF_SINGLE", |
| 51 |
"VAR_TEX_BUF_GATHER", |
| 52 |
"VAR_TEX_BUF_GRADIENT", |
| 53 |
"VAR_TEX_BUF_DUAL", |
| 54 |
|
| 55 |
# Special cased |
| 56 |
"FMA_RSCALE_N.f32", |
| 57 |
"FMA_RSCALE_LEFT.f32", |
| 58 |
"FMA_RSCALE_SCALE16.f32", |
| 59 |
|
| 60 |
# Deprecated instruction |
| 61 |
"NOT_OLD.i32", |
| 62 |
"NOT_OLD.i64", |
| 63 |
|
| 64 |
# TODO |
| 65 |
"FATAN_ASSIST.f32", |
| 66 |
"SEG_ADD.u64", |
| 67 |
"TEX_DUAL", |
| 68 |
]) |
| 69 |
|
| 70 |
template = """ |
| 71 |
#include "valhall.h" |
| 72 |
#include "bi_opcodes.h" |
| 73 |
|
| 74 |
const uint32_t valhall_immediates[32] = { |
| 75 |
% for imm in immediates: |
| 76 |
${hex(imm)}, |
| 77 |
% endfor |
| 78 |
}; |
| 79 |
|
| 80 |
<% |
| 81 |
def ibool(x): |
| 82 |
return '1' if x else '0' |
| 83 |
|
| 84 |
def hasmod(x, mod): |
| 85 |
return ibool(any([x.name == mod for x in op.modifiers])) |
| 86 |
|
| 87 |
%> |
| 88 |
const struct va_opcode_info |
| 89 |
valhall_opcodes[BI_NUM_OPCODES] = { |
| 90 |
% for op in instructions: |
| 91 |
% if op.name not in skip: |
| 92 |
<% |
| 93 |
name = op.name |
| 94 |
if name == 'BRANCHZ': |
| 95 |
name = 'BRANCHZ.i16' |
| 96 |
|
| 97 |
sr_control = 0 |
| 98 |
|
| 99 |
if len(op.staging) > 0: |
| 100 |
sr_control = op.staging[0].encoded_flags >> 6 |
| 101 |
%> |
| 102 |
[BI_OPCODE_${name.replace('.', '_').upper()}] = { |
| 103 |
.exact = ${hex(exact(op))}ULL, |
| 104 |
.srcs = { |
| 105 |
% for src in ([sr for sr in op.staging if sr.read] + op.srcs): |
| 106 |
{ |
| 107 |
.absneg = ${ibool(src.absneg)}, |
| 108 |
.swizzle = ${ibool(src.swizzle)}, |
| 109 |
.notted = ${ibool(src.notted)}, |
| 110 |
.widen = ${ibool(src.widen)}, |
| 111 |
.lanes = ${ibool(src.lanes)}, |
| 112 |
.halfswizzle = ${ibool(src.halfswizzle)}, |
| 113 |
.lane = ${ibool(src.lane)}, |
| 114 |
.combine = ${ibool(src.combine)}, |
| 115 |
% if src.size in [8, 16, 32, 64]: |
| 116 |
.size = VA_SIZE_${src.size}, |
| 117 |
% endif |
| 118 |
}, |
| 119 |
% endfor |
| 120 |
}, |
| 121 |
.type_size = ${typesize(op.name)}, |
| 122 |
.has_dest = ${ibool(len(op.dests) > 0)}, |
| 123 |
.is_signed = ${ibool(op.is_signed)}, |
| 124 |
.unit = VA_UNIT_${op.unit}, |
| 125 |
.nr_srcs = ${len(op.srcs)}, |
| 126 |
.nr_staging_srcs = ${sum([sr.read for sr in op.staging])}, |
| 127 |
.nr_staging_dests = ${sum([sr.write for sr in op.staging])}, |
| 128 |
.clamp = ${hasmod(x, 'clamp')}, |
| 129 |
.saturate = ${hasmod(x, 'saturate')}, |
| 130 |
.rhadd = ${hasmod(x, 'rhadd')}, |
| 131 |
.round_mode = ${hasmod(x, 'round_mode')}, |
| 132 |
.condition = ${hasmod(x, 'condition')}, |
| 133 |
.result_type = ${hasmod(x, 'result_type')}, |
| 134 |
.vecsize = ${hasmod(x, 'vector_size')}, |
| 135 |
.register_format = ${hasmod(x, 'register_format')}, |
| 136 |
.slot = ${hasmod(x, 'slot')}, |
| 137 |
.sr_count = ${hasmod(x, 'staging_register_count')}, |
| 138 |
.sr_write_count = ${hasmod(x, 'staging_register_write_count')}, |
| 139 |
.sr_control = ${sr_control}, |
| 140 |
}, |
| 141 |
% endif |
| 142 |
% endfor |
| 143 |
}; |
| 144 |
""" |
| 145 |
|
| 146 |
# Exact value to be ORed in to every opcode |
| 147 |
def exact_op(op): |
| 148 |
exact_op = 0 |
| 149 |
for subcode in op.opcode: |
| 150 |
exact_op |= (subcode.value << subcode.start) |
| 151 |
return exact_op |
| 152 |
|
| 153 |
try: |
| 154 |
print(Template(template).render(immediates = immediates, instructions = instructions, skip = SKIP, exact = exact_op, typesize = typesize)) |
| 155 |
except: |
| 156 |
print(exceptions.text_error_template().render()) |