1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3bf215546Sopenharmony_ci * Copyright (C) 2019-2020 Collabora, Ltd.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "agx_compiler.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_cistatic void
28bf215546Sopenharmony_ciagx_print_sized(char prefix, unsigned value, enum agx_size size, FILE *fp)
29bf215546Sopenharmony_ci{
30bf215546Sopenharmony_ci   switch (size) {
31bf215546Sopenharmony_ci   case AGX_SIZE_16:
32bf215546Sopenharmony_ci      fprintf(fp, "%c%u%c", prefix, value >> 1, (value & 1) ? 'h' : 'l');
33bf215546Sopenharmony_ci      return;
34bf215546Sopenharmony_ci   case AGX_SIZE_32:
35bf215546Sopenharmony_ci      assert((value & 1) == 0);
36bf215546Sopenharmony_ci      fprintf(fp, "%c%u", prefix, value >> 1);
37bf215546Sopenharmony_ci      return;
38bf215546Sopenharmony_ci   case AGX_SIZE_64:
39bf215546Sopenharmony_ci      assert((value & 1) == 0);
40bf215546Sopenharmony_ci      fprintf(fp, "%c%u:%c%u", prefix, value >> 1,
41bf215546Sopenharmony_ci            prefix, (value >> 1) + 1);
42bf215546Sopenharmony_ci      return;
43bf215546Sopenharmony_ci   }
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_ci   unreachable("Invalid size");
46bf215546Sopenharmony_ci}
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistatic void
49bf215546Sopenharmony_ciagx_print_index(agx_index index, FILE *fp)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci   switch (index.type) {
52bf215546Sopenharmony_ci   case AGX_INDEX_NULL:
53bf215546Sopenharmony_ci      fprintf(fp, "_");
54bf215546Sopenharmony_ci      return;
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci   case AGX_INDEX_NORMAL:
57bf215546Sopenharmony_ci      if (index.cache)
58bf215546Sopenharmony_ci         fprintf(fp, "$");
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci      if (index.discard)
61bf215546Sopenharmony_ci         fprintf(fp, "`");
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci      if (index.kill)
64bf215546Sopenharmony_ci         fprintf(fp, "*");
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci      fprintf(fp, "%u", index.value);
67bf215546Sopenharmony_ci      break;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci   case AGX_INDEX_IMMEDIATE:
70bf215546Sopenharmony_ci      fprintf(fp, "#%u", index.value);
71bf215546Sopenharmony_ci      break;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci   case AGX_INDEX_UNIFORM:
74bf215546Sopenharmony_ci      agx_print_sized('u', index.value, index.size, fp);
75bf215546Sopenharmony_ci      break;
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci   case AGX_INDEX_REGISTER:
78bf215546Sopenharmony_ci      agx_print_sized('r', index.value, index.size, fp);
79bf215546Sopenharmony_ci      break;
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   default:
82bf215546Sopenharmony_ci      unreachable("Invalid index type");
83bf215546Sopenharmony_ci   }
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   /* Print length suffixes if not implied */
86bf215546Sopenharmony_ci   if (index.type == AGX_INDEX_NORMAL || index.type == AGX_INDEX_IMMEDIATE) {
87bf215546Sopenharmony_ci      if (index.size == AGX_SIZE_16)
88bf215546Sopenharmony_ci         fprintf(fp, "h");
89bf215546Sopenharmony_ci      else if (index.size == AGX_SIZE_64)
90bf215546Sopenharmony_ci         fprintf(fp, "d");
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   if (index.abs)
94bf215546Sopenharmony_ci      fprintf(fp, ".abs");
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci   if (index.neg)
97bf215546Sopenharmony_ci      fprintf(fp, ".neg");
98bf215546Sopenharmony_ci}
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_civoid
101bf215546Sopenharmony_ciagx_print_instr(agx_instr *I, FILE *fp)
102bf215546Sopenharmony_ci{
103bf215546Sopenharmony_ci   assert(I->op < AGX_NUM_OPCODES);
104bf215546Sopenharmony_ci   struct agx_opcode_info info = agx_opcodes_info[I->op];
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci   fprintf(fp, "   %s", info.name);
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci   if (I->saturate)
109bf215546Sopenharmony_ci      fprintf(fp, ".sat");
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci   if (I->last)
112bf215546Sopenharmony_ci      fprintf(fp, ".last");
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   fprintf(fp, " ");
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   bool print_comma = false;
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   for (unsigned d = 0; d < info.nr_dests; ++d) {
119bf215546Sopenharmony_ci      if (print_comma)
120bf215546Sopenharmony_ci         fprintf(fp, ", ");
121bf215546Sopenharmony_ci      else
122bf215546Sopenharmony_ci         print_comma = true;
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci      agx_print_index(I->dest[d], fp);
125bf215546Sopenharmony_ci   }
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   for (unsigned s = 0; s < I->nr_srcs; ++s) {
128bf215546Sopenharmony_ci      if (print_comma)
129bf215546Sopenharmony_ci         fprintf(fp, ", ");
130bf215546Sopenharmony_ci      else
131bf215546Sopenharmony_ci         print_comma = true;
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci      agx_print_index(I->src[s], fp);
134bf215546Sopenharmony_ci   }
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci   if (I->mask) {
137bf215546Sopenharmony_ci      fprintf(fp, ", ");
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci      for (unsigned i = 0; i < 4; ++i) {
140bf215546Sopenharmony_ci         if (I->mask & (1 << i))
141bf215546Sopenharmony_ci            fprintf(fp, "%c", "xyzw"[i]);
142bf215546Sopenharmony_ci      }
143bf215546Sopenharmony_ci   }
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci   /* TODO: Do better for enums, truth tables, etc */
146bf215546Sopenharmony_ci   if (info.immediates) {
147bf215546Sopenharmony_ci      if (print_comma)
148bf215546Sopenharmony_ci         fprintf(fp, ", ");
149bf215546Sopenharmony_ci      else
150bf215546Sopenharmony_ci         print_comma = true;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci      fprintf(fp, "#%X", I->imm);
153bf215546Sopenharmony_ci   }
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   if (info.immediates & AGX_IMMEDIATE_DIM) {
156bf215546Sopenharmony_ci      if (print_comma)
157bf215546Sopenharmony_ci         fprintf(fp, ", ");
158bf215546Sopenharmony_ci      else
159bf215546Sopenharmony_ci         print_comma = true;
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_ci      fprintf(fp, "dim %u", I->dim); // TODO enumify
162bf215546Sopenharmony_ci   }
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   if (info.immediates & AGX_IMMEDIATE_SCOREBOARD) {
165bf215546Sopenharmony_ci      if (print_comma)
166bf215546Sopenharmony_ci         fprintf(fp, ", ");
167bf215546Sopenharmony_ci      else
168bf215546Sopenharmony_ci         print_comma = true;
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci      fprintf(fp, "slot %u", I->scoreboard);
171bf215546Sopenharmony_ci   }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   if (info.immediates & AGX_IMMEDIATE_NEST) {
174bf215546Sopenharmony_ci      if (print_comma)
175bf215546Sopenharmony_ci         fprintf(fp, ", ");
176bf215546Sopenharmony_ci      else
177bf215546Sopenharmony_ci         print_comma = true;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci      fprintf(fp, "n=%u", I->nest);
180bf215546Sopenharmony_ci   }
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ci   if ((info.immediates & AGX_IMMEDIATE_INVERT_COND) && I->invert_cond) {
183bf215546Sopenharmony_ci      if (print_comma)
184bf215546Sopenharmony_ci         fprintf(fp, ", ");
185bf215546Sopenharmony_ci      else
186bf215546Sopenharmony_ci         print_comma = true;
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci      fprintf(fp, "inv");
189bf215546Sopenharmony_ci   }
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci   fprintf(fp, "\n");
192bf215546Sopenharmony_ci}
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_civoid
195bf215546Sopenharmony_ciagx_print_block(agx_block *block, FILE *fp)
196bf215546Sopenharmony_ci{
197bf215546Sopenharmony_ci   fprintf(fp, "block%u {\n", block->index);
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci   agx_foreach_instr_in_block(block, ins)
200bf215546Sopenharmony_ci      agx_print_instr(ins, fp);
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   fprintf(fp, "}");
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   if (block->successors[0]) {
205bf215546Sopenharmony_ci      fprintf(fp, " -> ");
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci      agx_foreach_successor(block, succ)
208bf215546Sopenharmony_ci         fprintf(fp, "block%u ", succ->index);
209bf215546Sopenharmony_ci   }
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   if (block->predecessors.size) {
212bf215546Sopenharmony_ci      fprintf(fp, " from");
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci      agx_foreach_predecessor(block, pred)
215bf215546Sopenharmony_ci         fprintf(fp, " block%u", (*pred)->index);
216bf215546Sopenharmony_ci   }
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   fprintf(fp, "\n\n");
219bf215546Sopenharmony_ci}
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_civoid
222bf215546Sopenharmony_ciagx_print_shader(agx_context *ctx, FILE *fp)
223bf215546Sopenharmony_ci{
224bf215546Sopenharmony_ci   agx_foreach_block(ctx, block)
225bf215546Sopenharmony_ci      agx_print_block(block, fp);
226bf215546Sopenharmony_ci}
227