1# 2# Copyright (C) 2020 Collabora, Ltd. 3# 4# Permission is hereby granted, free of charge, to any person obtaining a 5# copy of this software and associated documentation files (the "Software"), 6# to deal in the Software without restriction, including without limitation 7# the rights to use, copy, modify, merge, publish, distribute, sublicense, 8# and/or sell copies of the Software, and to permit persons to whom the 9# Software is furnished to do so, subject to the following conditions: 10# 11# The above copyright notice and this permission notice (including the next 12# paragraph) shall be included in all copies or substantial portions of the 13# Software. 14# 15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21# IN THE SOFTWARE. 22 23TEMPLATE = """#include <stdio.h> 24#include "compiler.h" 25 26static const char * 27bi_swizzle_as_str(enum bi_swizzle swz) 28{ 29 switch (swz) { 30 case BI_SWIZZLE_H00: return ".h00"; 31 case BI_SWIZZLE_H01: return ""; 32 case BI_SWIZZLE_H10: return ".h10"; 33 case BI_SWIZZLE_H11: return ".h11"; 34 case BI_SWIZZLE_B0000: return ".b0"; 35 case BI_SWIZZLE_B1111: return ".b1"; 36 case BI_SWIZZLE_B2222: return ".b2"; 37 case BI_SWIZZLE_B3333: return ".b3"; 38 case BI_SWIZZLE_B0011: return ".b0011"; 39 case BI_SWIZZLE_B2233: return ".b2233"; 40 case BI_SWIZZLE_B1032: return ".b1032"; 41 case BI_SWIZZLE_B3210: return ".b3210"; 42 case BI_SWIZZLE_B0022: return ".b0022"; 43 } 44 45 unreachable("Invalid swizzle"); 46} 47 48static const char * 49bir_fau_name(unsigned fau_idx) 50{ 51 const char *names[] = { 52 "zero", "lane-id", "wrap-id", "core-id", "fb-extent", 53 "atest-param", "sample-pos", "reserved", 54 "blend_descriptor_0", "blend_descriptor_1", 55 "blend_descriptor_2", "blend_descriptor_3", 56 "blend_descriptor_4", "blend_descriptor_5", 57 "blend_descriptor_6", "blend_descriptor_7", 58 "tls_ptr", "wls_ptr", "program_counter", 59 }; 60 61 assert(fau_idx < ARRAY_SIZE(names)); 62 return names[fau_idx]; 63} 64 65static const char * 66bir_passthrough_name(unsigned idx) 67{ 68 const char *names[] = { 69 "s0", "s1", "s2", "t", "fau.x", "fau.y", "t0", "t1" 70 }; 71 72 assert(idx < ARRAY_SIZE(names)); 73 return names[idx]; 74} 75 76static void 77bi_print_index(FILE *fp, bi_index index) 78{ 79 if (index.discard) 80 fputs("^", fp); 81 82 if (bi_is_null(index)) 83 fprintf(fp, "_"); 84 else if (index.type == BI_INDEX_CONSTANT) 85 fprintf(fp, "#0x%x", index.value); 86 else if (index.type == BI_INDEX_FAU && index.value >= BIR_FAU_UNIFORM) 87 fprintf(fp, "u%u", index.value & ~BIR_FAU_UNIFORM); 88 else if (index.type == BI_INDEX_FAU) 89 fprintf(fp, "%s", bir_fau_name(index.value)); 90 else if (index.type == BI_INDEX_PASS) 91 fprintf(fp, "%s", bir_passthrough_name(index.value)); 92 else if (index.type == BI_INDEX_REGISTER) 93 fprintf(fp, "r%u", index.value); 94 else if (index.type == BI_INDEX_NORMAL) 95 fprintf(fp, "%u", index.value); 96 else 97 unreachable("Invalid index"); 98 99 if (index.offset) 100 fprintf(fp, "[%u]", index.offset); 101 102 if (index.abs) 103 fputs(".abs", fp); 104 105 if (index.neg) 106 fputs(".neg", fp); 107 108 fputs(bi_swizzle_as_str(index.swizzle), fp); 109} 110 111% for mod in sorted(modifiers): 112% if len(modifiers[mod]) > 2: # otherwise just boolean 113 114UNUSED static inline const char * 115bi_${mod}_as_str(enum bi_${mod} ${mod}) 116{ 117 switch (${mod}) { 118% for i, state in enumerate(sorted(modifiers[mod])): 119% if state == "none": 120 case BI_${mod.upper()}_NONE: return ""; 121% elif state != "reserved": 122 case BI_${mod.upper()}_${state.upper()}: return ".${state.lower()}"; 123% endif 124% endfor 125 } 126 127 unreachable("Invalid ${mod}"); 128}; 129% endif 130% endfor 131 132<%def name="print_modifiers(mods, table)"> 133 % for mod in mods: 134 % if mod not in ["lane_dest"]: 135 % if len(table[mod]) > 2: 136 fputs(bi_${mod}_as_str(I->${mod}), fp); 137 % else: 138 if (I->${mod}) fputs(".${mod}", fp); 139 % endif 140 % endif 141 % endfor 142</%def> 143 144<%def name="print_source_modifiers(mods, src, table)"> 145 % for mod in mods: 146 % if mod[0:-1] not in ["lane", "lanes", "replicate", "swz", "widen", "swap", "abs", "neg", "sign", "not"]: 147 % if len(table[mod[0:-1]]) > 2: 148 fputs(bi_${mod[0:-1]}_as_str(I->${mod[0:-1]}[${src}]), fp); 149 % elif mod == "bytes2": 150 if (I->bytes2) fputs(".bytes", fp); 151 % else: 152 if (I->${mod[0:-1]}[${src}]) fputs(".${mod[0:-1]}", fp); 153 % endif 154 %endif 155 % endfor 156</%def> 157 158void 159bi_print_instr(const bi_instr *I, FILE *fp) 160{ 161 fputs(" ", fp); 162 163 bi_foreach_dest(I, d) { 164 if (d > 0) fprintf(fp, ", "); 165 166 bi_print_index(fp, I->dest[d]); 167 } 168 169 if (I->nr_dests > 0) 170 fputs(" = ", fp); 171 172 fprintf(fp, "%s", bi_opcode_props[I->op].name); 173 174 if (I->table) 175 fprintf(fp, ".table%u", I->table); 176 177 if (I->flow) 178 fprintf(fp, ".flow%u", I->flow); 179 180 if (I->op == BI_OPCODE_COLLECT_I32 || I->op == BI_OPCODE_PHI) { 181 for (unsigned i = 0; i < I->nr_srcs; ++i) { 182 if (i > 0) 183 fputs(", ", fp); 184 else 185 fputs(" ", fp); 186 187 bi_print_index(fp, I->src[i]); 188 } 189 } 190 191 switch (I->op) { 192% for opcode in ops: 193<% 194 # Extract modifiers that are not per-source 195 root_modifiers = [x for x in ops[opcode]["modifiers"] if x[-1] not in "0123"] 196%> 197 case BI_OPCODE_${opcode.replace('.', '_').upper()}: 198 ${print_modifiers(root_modifiers, modifiers)} 199 fputs(" ", fp); 200 % for src in range(src_count(ops[opcode])): 201 % if src > 0: 202 fputs(", ", fp); 203 % endif 204 bi_print_index(fp, I->src[${src}]); 205 ${print_source_modifiers([m for m in ops[opcode]["modifiers"] if m[-1] == str(src)], src, modifiers)} 206 % endfor 207 % for imm in ops[opcode]["immediates"]: 208 fprintf(fp, ", ${imm}:%u", I->${imm}); 209 % endfor 210 break; 211% endfor 212 default: 213 unreachable("Invalid opcode"); 214 } 215 216 if (I->branch_target) 217 fprintf(fp, " -> block%u", I->branch_target->index); 218 219 fputs("\\n", fp); 220 221}""" 222 223import sys 224from bifrost_isa import * 225from mako.template import Template 226 227instructions = {} 228for arg in sys.argv[1:]: 229 new_instructions = parse_instructions(arg, include_pseudo = True) 230 instructions.update(new_instructions) 231 232ir_instructions = partition_mnemonics(instructions) 233modifier_lists = order_modifiers(ir_instructions) 234 235print(Template(COPYRIGHT + TEMPLATE).render(ops = ir_instructions, modifiers = modifier_lists, src_count = src_count)) 236