Path: blob/21.2-virgl/src/panfrost/bifrost/bi_printer.c.py
4564 views
#1# Copyright (C) 2020 Collabora, Ltd.2#3# Permission is hereby granted, free of charge, to any person obtaining a4# copy of this software and associated documentation files (the "Software"),5# to deal in the Software without restriction, including without limitation6# the rights to use, copy, modify, merge, publish, distribute, sublicense,7# and/or sell copies of the Software, and to permit persons to whom the8# Software is furnished to do so, subject to the following conditions:9#10# The above copyright notice and this permission notice (including the next11# paragraph) shall be included in all copies or substantial portions of the12# Software.13#14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20# IN THE SOFTWARE.2122TEMPLATE = """#include <stdio.h>23#include "compiler.h"2425static const char *26bi_swizzle_as_str(enum bi_swizzle swz)27{28switch (swz) {29case BI_SWIZZLE_H00: return ".h00";30case BI_SWIZZLE_H01: return "";31case BI_SWIZZLE_H10: return ".h10";32case BI_SWIZZLE_H11: return ".h11";33case BI_SWIZZLE_B0000: return ".b0";34case BI_SWIZZLE_B1111: return ".b1";35case BI_SWIZZLE_B2222: return ".b2";36case BI_SWIZZLE_B3333: return ".b3";37case BI_SWIZZLE_B0011: return ".b0011";38case BI_SWIZZLE_B2233: return ".b2233";39case BI_SWIZZLE_B1032: return ".b1032";40case BI_SWIZZLE_B3210: return ".b3210";41case BI_SWIZZLE_B0022: return ".b0022";42}4344unreachable("Invalid swizzle");45}4647static const char *48bir_fau_name(unsigned fau_idx)49{50const char *names[] = {51"zero", "lane-id", "wrap-id", "core-id", "fb-extent",52"atest-param", "sample-pos", "reserved",53"blend_descriptor_0", "blend_descriptor_1",54"blend_descriptor_2", "blend_descriptor_3",55"blend_descriptor_4", "blend_descriptor_5",56"blend_descriptor_6", "blend_descriptor_7",57};5859assert(fau_idx < ARRAY_SIZE(names));60return names[fau_idx];61}6263static const char *64bir_passthrough_name(unsigned idx)65{66const char *names[] = {67"s0", "s1", "s2", "t", "fau.x", "fau.y", "t0", "t1"68};6970assert(idx < ARRAY_SIZE(names));71return names[idx];72}7374static void75bi_print_index(FILE *fp, bi_index index)76{77if (bi_is_null(index))78fprintf(fp, "_");79else if (index.type == BI_INDEX_CONSTANT)80fprintf(fp, "#0x%x", index.value);81else if (index.type == BI_INDEX_FAU && index.value >= BIR_FAU_UNIFORM)82fprintf(fp, "u%u", index.value & ~BIR_FAU_UNIFORM);83else if (index.type == BI_INDEX_FAU)84fprintf(fp, "%s", bir_fau_name(index.value));85else if (index.type == BI_INDEX_PASS)86fprintf(fp, "%s", bir_passthrough_name(index.value));87else if (index.type == BI_INDEX_REGISTER)88fprintf(fp, "br%u", index.value);89else if (index.type == BI_INDEX_NORMAL && index.reg)90fprintf(fp, "r%u", index.value);91else if (index.type == BI_INDEX_NORMAL)92fprintf(fp, "%u", index.value);93else94unreachable("Invalid index");9596if (index.offset)97fprintf(fp, "[%u]", index.offset);9899if (index.abs)100fputs(".abs", fp);101102if (index.neg)103fputs(".neg", fp);104105fputs(bi_swizzle_as_str(index.swizzle), fp);106}107108% for mod in sorted(modifiers):109% if len(modifiers[mod]) > 2: # otherwise just boolean110111static inline const char *112bi_${mod}_as_str(enum bi_${mod} ${mod})113{114switch (${mod}) {115% for i, state in enumerate(sorted(modifiers[mod])):116% if state == "none":117case BI_${mod.upper()}_NONE: return "";118% elif state != "reserved":119case BI_${mod.upper()}_${state.upper()}: return ".${state.lower()}";120% endif121% endfor122}123124unreachable("Invalid ${mod}");125};126% endif127% endfor128129<%def name="print_modifiers(mods, table)">130% for mod in mods:131% if mod not in ["lane_dest"]:132% if len(table[mod]) > 2:133fputs(bi_${mod}_as_str(I->${mod}), fp);134% else:135if (I->${mod}) fputs(".${mod}", fp);136% endif137% endif138% endfor139</%def>140141<%def name="print_source_modifiers(mods, src, table)">142% for mod in mods:143% if mod[0:-1] not in ["lane", "lanes", "replicate", "swz", "widen", "swap", "abs", "neg", "sign", "not"]:144% if len(table[mod[0:-1]]) > 2:145fputs(bi_${mod[0:-1]}_as_str(I->${mod[0:-1]}[${src}]), fp);146% elif mod == "bytes2":147if (I->bytes2) fputs(".bytes", fp);148% else:149if (I->${mod[0:-1]}[${src}]) fputs(".${mod[0:-1]}", fp);150% endif151%endif152% endfor153</%def>154155void156bi_print_instr(bi_instr *I, FILE *fp)157{158bi_foreach_dest(I, d) {159if (bi_is_null(I->dest[d])) break;160if (d > 0) fprintf(fp, ", ");161162bi_print_index(fp, I->dest[d]);163}164165fprintf(fp, " = %s", bi_opcode_props[I->op].name);166167if (I->table)168fprintf(fp, ".%s", bi_table_as_str(I->table));169170switch (I->op) {171% for opcode in ops:172<%173# Extract modifiers that are not per-source174root_modifiers = [x for x in ops[opcode]["modifiers"] if x[-1] not in "0123"]175%>176case BI_OPCODE_${opcode.replace('.', '_').upper()}:177${print_modifiers(root_modifiers, modifiers)}178fputs(" ", fp);179% for src in range(src_count(ops[opcode])):180% if src > 0:181fputs(", ", fp);182% endif183bi_print_index(fp, I->src[${src}]);184${print_source_modifiers([m for m in ops[opcode]["modifiers"] if m[-1] == str(src)], src, modifiers)}185% endfor186% for imm in ops[opcode]["immediates"]:187fprintf(fp, ", ${imm}:%u", I->${imm});188% endfor189break;190% endfor191default:192unreachable("Invalid opcode");193}194195if (I->branch_target)196fprintf(fp, " -> block%u", I->branch_target->base.name);197198fputs("\\n", fp);199200}"""201202import sys203from bifrost_isa import *204from mako.template import Template205206instructions = parse_instructions(sys.argv[1], include_pseudo = True)207ir_instructions = partition_mnemonics(instructions)208modifier_lists = order_modifiers(ir_instructions)209210print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = modifier_lists, src_count = src_count))211212213