1template = """/* 2 * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io> 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 FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24#ifndef _AGX_BUILDER_ 25#define _AGX_BUILDER_ 26 27#include "agx_compiler.h" 28 29static inline agx_instr * 30agx_alloc_instr(agx_builder *b, enum agx_opcode op) 31{ 32 agx_instr *I = rzalloc(b->shader, agx_instr); 33 I->op = op; 34 return I; 35} 36 37% for opcode in opcodes: 38<% 39 op = opcodes[opcode] 40 dests = op.dests 41 srcs = op.srcs 42 imms = op.imms 43 suffix = "_to" if dests > 0 else "" 44%> 45 46static inline agx_instr * 47agx_${opcode}${suffix}(agx_builder *b 48 49% for dest in range(dests): 50 , agx_index dst${dest} 51% endfor 52 53% for src in range(srcs): 54 , agx_index src${src} 55% endfor 56 57% for imm in imms: 58 , ${imm.ctype} ${imm.name} 59% endfor 60 61) { 62 agx_instr *I = agx_alloc_instr(b, AGX_OPCODE_${opcode.upper()}); 63 64% for dest in range(dests): 65 I->dest[${dest}] = dst${dest}; 66% endfor 67 68% for src in range(srcs): 69 I->src[${src}] = src${src}; 70% endfor 71 72% for imm in imms: 73 I->${imm.name} = ${imm.name}; 74% endfor 75 76 agx_builder_insert(&b->cursor, I); 77 return I; 78} 79 80% if dests == 1: 81static inline agx_index 82agx_${opcode}(agx_builder *b 83 84% if srcs == 0: 85 , unsigned size 86% endif 87 88% for src in range(srcs): 89 , agx_index src${src} 90% endfor 91 92% for imm in imms: 93 , ${imm.ctype} ${imm.name} 94% endfor 95 96) { 97<% 98 args = ["tmp"] 99 args += ["src" + str(i) for i in range(srcs)] 100 args += [imm.name for imm in imms] 101%> 102% if srcs == 0: 103 agx_index tmp = agx_temp(b->shader, agx_size_for_bits(size)); 104% else: 105 agx_index tmp = agx_temp(b->shader, src0.size); 106% endif 107 agx_${opcode}_to(b, ${", ".join(args)}); 108 return tmp; 109} 110% endif 111 112% endfor 113 114/* Convenience methods */ 115 116enum agx_bitop_table { 117 AGX_BITOP_NOT = 0x5, 118 AGX_BITOP_XOR = 0x6, 119 AGX_BITOP_AND = 0x8, 120 AGX_BITOP_MOV = 0xA, 121 AGX_BITOP_OR = 0xE 122}; 123 124#define UNOP_BITOP(name, table) \ 125 static inline agx_instr * \ 126 agx_## name ##_to(agx_builder *b, agx_index dst0, agx_index src0) \ 127 { \ 128 return agx_bitop_to(b, dst0, src0, agx_zero(), AGX_BITOP_ ## table); \ 129 } 130 131#define BINOP_BITOP(name, table) \ 132 static inline agx_instr * \ 133 agx_## name ##_to(agx_builder *b, agx_index dst0, agx_index src0, agx_index src1) \ 134 { \ 135 return agx_bitop_to(b, dst0, src0, src1, AGX_BITOP_ ## table); \ 136 } 137 138UNOP_BITOP(mov, MOV) 139UNOP_BITOP(not, NOT) 140 141BINOP_BITOP(and, AND) 142BINOP_BITOP(xor, XOR) 143BINOP_BITOP(or, OR) 144 145#undef UNOP_BITOP 146#undef BINOP_BITOP 147 148static inline agx_instr * 149agx_fmov_to(agx_builder *b, agx_index dst0, agx_index src0) 150{ 151 return agx_fadd_to(b, dst0, src0, agx_negzero()); 152} 153 154static inline agx_instr * 155agx_push_exec(agx_builder *b, unsigned n) 156{ 157 return agx_if_fcmp(b, agx_zero(), agx_zero(), n, AGX_FCOND_EQ, false); 158} 159 160static inline agx_instr * 161agx_ushr_to(agx_builder *b, agx_index dst, agx_index s0, agx_index s1) 162{ 163 return agx_bfeil_to(b, dst, agx_zero(), s0, s1, 0); 164} 165 166static inline agx_index 167agx_ushr(agx_builder *b, agx_index s0, agx_index s1) 168{ 169 agx_index tmp = agx_temp(b->shader, s0.size); 170 agx_ushr_to(b, tmp, s0, s1); 171 return tmp; 172} 173 174#endif 175""" 176 177from mako.template import Template 178from agx_opcodes import opcodes 179 180print(Template(template).render(opcodes=opcodes)) 181