17ec681f3Smrgtemplate = """/*
27ec681f3Smrg * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io>
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#ifndef _AGX_BUILDER_
257ec681f3Smrg#define _AGX_BUILDER_
267ec681f3Smrg
277ec681f3Smrg#include "agx_compiler.h"
287ec681f3Smrg
297ec681f3Smrgstatic inline agx_instr *
307ec681f3Smrgagx_alloc_instr(agx_builder *b, enum agx_opcode op)
317ec681f3Smrg{
327ec681f3Smrg   agx_instr *I = rzalloc(b->shader, agx_instr);
337ec681f3Smrg   I->op = op;
347ec681f3Smrg   return I;
357ec681f3Smrg}
367ec681f3Smrg
377ec681f3Smrg% for opcode in opcodes:
387ec681f3Smrg<%
397ec681f3Smrg   op = opcodes[opcode]
407ec681f3Smrg   dests = op.dests
417ec681f3Smrg   srcs = op.srcs
427ec681f3Smrg   imms = op.imms
437ec681f3Smrg   suffix = "_to" if dests > 0 else ""
447ec681f3Smrg%>
457ec681f3Smrg
467ec681f3Smrgstatic inline agx_instr *
477ec681f3Smrgagx_${opcode}${suffix}(agx_builder *b
487ec681f3Smrg
497ec681f3Smrg% for dest in range(dests):
507ec681f3Smrg   , agx_index dst${dest}
517ec681f3Smrg% endfor
527ec681f3Smrg
537ec681f3Smrg% for src in range(srcs):
547ec681f3Smrg   , agx_index src${src}
557ec681f3Smrg% endfor
567ec681f3Smrg
577ec681f3Smrg% for imm in imms:
587ec681f3Smrg   , ${imm.ctype} ${imm.name}
597ec681f3Smrg% endfor
607ec681f3Smrg
617ec681f3Smrg) {
627ec681f3Smrg   agx_instr *I = agx_alloc_instr(b, AGX_OPCODE_${opcode.upper()});
637ec681f3Smrg
647ec681f3Smrg% for dest in range(dests):
657ec681f3Smrg   I->dest[${dest}] = dst${dest};
667ec681f3Smrg% endfor
677ec681f3Smrg
687ec681f3Smrg% for src in range(srcs):
697ec681f3Smrg   I->src[${src}] = src${src};
707ec681f3Smrg% endfor
717ec681f3Smrg
727ec681f3Smrg% for imm in imms:
737ec681f3Smrg   I->${imm.name} = ${imm.name};
747ec681f3Smrg% endfor
757ec681f3Smrg
767ec681f3Smrg   agx_builder_insert(&b->cursor, I);
777ec681f3Smrg   return I;
787ec681f3Smrg}
797ec681f3Smrg
807ec681f3Smrg% if dests == 1:
817ec681f3Smrgstatic inline agx_index
827ec681f3Smrgagx_${opcode}(agx_builder *b
837ec681f3Smrg
847ec681f3Smrg% if srcs == 0:
857ec681f3Smrg   , unsigned size
867ec681f3Smrg% endif
877ec681f3Smrg
887ec681f3Smrg% for src in range(srcs):
897ec681f3Smrg   , agx_index src${src}
907ec681f3Smrg% endfor
917ec681f3Smrg
927ec681f3Smrg% for imm in imms:
937ec681f3Smrg   , ${imm.ctype} ${imm.name}
947ec681f3Smrg% endfor
957ec681f3Smrg
967ec681f3Smrg) {
977ec681f3Smrg<%
987ec681f3Smrg   args = ["tmp"]
997ec681f3Smrg   args += ["src" + str(i) for i in range(srcs)]
1007ec681f3Smrg   args += [imm.name for imm in imms]
1017ec681f3Smrg%>
1027ec681f3Smrg% if srcs == 0:
1037ec681f3Smrg   agx_index tmp = agx_temp(b->shader, agx_size_for_bits(size));
1047ec681f3Smrg% else:
1057ec681f3Smrg   agx_index tmp = agx_temp(b->shader, src0.size);
1067ec681f3Smrg% endif
1077ec681f3Smrg   agx_${opcode}_to(b, ${", ".join(args)});
1087ec681f3Smrg   return tmp;
1097ec681f3Smrg}
1107ec681f3Smrg% endif
1117ec681f3Smrg
1127ec681f3Smrg% endfor
1137ec681f3Smrg
1147ec681f3Smrg/* Convenience methods */
1157ec681f3Smrg
1167ec681f3Smrgenum agx_bitop_table {
1177ec681f3Smrg   AGX_BITOP_NOT = 0x5,
1187ec681f3Smrg   AGX_BITOP_XOR = 0x6,
1197ec681f3Smrg   AGX_BITOP_AND = 0x8,
1207ec681f3Smrg   AGX_BITOP_MOV = 0xA,
1217ec681f3Smrg   AGX_BITOP_OR  = 0xE
1227ec681f3Smrg};
1237ec681f3Smrg
1247ec681f3Smrg#define UNOP_BITOP(name, table) \
1257ec681f3Smrg   static inline agx_instr * \
1267ec681f3Smrg   agx_## name ##_to(agx_builder *b, agx_index dst0, agx_index src0) \
1277ec681f3Smrg   { \
1287ec681f3Smrg      return agx_bitop_to(b, dst0, src0, agx_zero(), AGX_BITOP_ ## table); \
1297ec681f3Smrg   }
1307ec681f3Smrg
1317ec681f3Smrg#define BINOP_BITOP(name, table) \
1327ec681f3Smrg   static inline agx_instr * \
1337ec681f3Smrg   agx_## name ##_to(agx_builder *b, agx_index dst0, agx_index src0, agx_index src1) \
1347ec681f3Smrg   { \
1357ec681f3Smrg      return agx_bitop_to(b, dst0, src0, src1, AGX_BITOP_ ## table); \
1367ec681f3Smrg   }
1377ec681f3Smrg
1387ec681f3SmrgUNOP_BITOP(mov, MOV)
1397ec681f3SmrgUNOP_BITOP(not, NOT)
1407ec681f3Smrg
1417ec681f3SmrgBINOP_BITOP(and, AND)
1427ec681f3SmrgBINOP_BITOP(xor, XOR)
1437ec681f3SmrgBINOP_BITOP(or, OR)
1447ec681f3Smrg
1457ec681f3Smrg#undef UNOP_BITOP
1467ec681f3Smrg#undef BINOP_BITOP
1477ec681f3Smrg
1487ec681f3Smrgstatic inline agx_instr *
1497ec681f3Smrgagx_fmov_to(agx_builder *b, agx_index dst0, agx_index src0)
1507ec681f3Smrg{
1517ec681f3Smrg   return agx_fadd_to(b, dst0, src0, agx_negzero());
1527ec681f3Smrg}
1537ec681f3Smrg
1547ec681f3Smrgstatic inline agx_instr *
1557ec681f3Smrgagx_push_exec(agx_builder *b, unsigned n)
1567ec681f3Smrg{
1577ec681f3Smrg   return agx_if_fcmp(b, agx_zero(), agx_zero(), n, AGX_FCOND_EQ, false);
1587ec681f3Smrg}
1597ec681f3Smrg
1607ec681f3Smrgstatic inline agx_instr *
1617ec681f3Smrgagx_ushr_to(agx_builder *b, agx_index dst, agx_index s0, agx_index s1)
1627ec681f3Smrg{
1637ec681f3Smrg    return agx_bfeil_to(b, dst, agx_zero(), s0, s1, 0);
1647ec681f3Smrg}
1657ec681f3Smrg
1667ec681f3Smrgstatic inline agx_index
1677ec681f3Smrgagx_ushr(agx_builder *b, agx_index s0, agx_index s1)
1687ec681f3Smrg{
1697ec681f3Smrg    agx_index tmp = agx_temp(b->shader, s0.size);
1707ec681f3Smrg    agx_ushr_to(b, tmp, s0, s1);
1717ec681f3Smrg    return tmp;
1727ec681f3Smrg}
1737ec681f3Smrg
1747ec681f3Smrg#endif
1757ec681f3Smrg"""
1767ec681f3Smrg
1777ec681f3Smrgfrom mako.template import Template
1787ec681f3Smrgfrom agx_opcodes import opcodes
1797ec681f3Smrg
1807ec681f3Smrgprint(Template(template).render(opcodes=opcodes))
181