Path: blob/21.2-virgl/src/asahi/compiler/agx_print.c
4564 views
/*1* Copyright (C) 2021 Alyssa Rosenzweig <[email protected]>2* Copyright (C) 2019-2020 Collabora, Ltd.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* 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 THE21* SOFTWARE.22*/2324#include "agx_compiler.h"2526static void27agx_print_sized(char prefix, unsigned value, enum agx_size size, FILE *fp)28{29switch (size) {30case AGX_SIZE_16:31fprintf(fp, "%c%u%c", prefix, value >> 1, (value & 1) ? 'h' : 'l');32return;33case AGX_SIZE_32:34assert((value & 1) == 0);35fprintf(fp, "%c%u", prefix, value >> 1);36return;37case AGX_SIZE_64:38assert((value & 1) == 0);39fprintf(fp, "%c%u:%c%u", prefix, value >> 1,40prefix, (value >> 1) + 1);41return;42}4344unreachable("Invalid size");45}4647static void48agx_print_index(agx_index index, FILE *fp)49{50switch (index.type) {51case AGX_INDEX_NULL:52fprintf(fp, "_");53return;5455case AGX_INDEX_NORMAL:56if (index.cache)57fprintf(fp, "$");5859if (index.discard)60fprintf(fp, "`");6162if (index.kill)63fprintf(fp, "*");6465fprintf(fp, "%u", index.value);66break;6768case AGX_INDEX_IMMEDIATE:69fprintf(fp, "#%u", index.value);70break;7172case AGX_INDEX_UNIFORM:73agx_print_sized('u', index.value, index.size, fp);74break;7576case AGX_INDEX_REGISTER:77agx_print_sized('r', index.value, index.size, fp);78break;7980default:81unreachable("Invalid index type");82}8384/* Print length suffixes if not implied */85if (index.type == AGX_INDEX_NORMAL || index.type == AGX_INDEX_IMMEDIATE) {86if (index.size == AGX_SIZE_16)87fprintf(fp, "h");88else if (index.size == AGX_SIZE_64)89fprintf(fp, "d");90}9192if (index.abs)93fprintf(fp, ".abs");9495if (index.neg)96fprintf(fp, ".neg");97}9899void100agx_print_instr(agx_instr *I, FILE *fp)101{102assert(I->op < AGX_NUM_OPCODES);103struct agx_opcode_info info = agx_opcodes_info[I->op];104105fprintf(fp, " %s", info.name);106107if (I->saturate)108fprintf(fp, ".sat");109110if (I->last)111fprintf(fp, ".last");112113fprintf(fp, " ");114115bool print_comma = false;116117for (unsigned d = 0; d < info.nr_dests; ++d) {118if (print_comma)119fprintf(fp, ", ");120else121print_comma = true;122123agx_print_index(I->dest[d], fp);124}125126for (unsigned s = 0; s < info.nr_srcs; ++s) {127if (print_comma)128fprintf(fp, ", ");129else130print_comma = true;131132agx_print_index(I->src[s], fp);133}134135if (I->mask) {136fprintf(fp, ", ");137138for (unsigned i = 0; i < 4; ++i) {139if (I->mask & (1 << i))140fprintf(fp, "%c", "xyzw"[i]);141}142}143144/* TODO: Do better for enums, truth tables, etc */145if (info.immediates) {146if (print_comma)147fprintf(fp, ", ");148else149print_comma = true;150151fprintf(fp, "#%X", I->imm);152}153154if (info.immediates & AGX_IMMEDIATE_DIM) {155if (print_comma)156fprintf(fp, ", ");157else158print_comma = true;159160fprintf(fp, "dim %u", I->dim); // TODO enumify161}162163if (info.immediates & AGX_IMMEDIATE_SCOREBOARD) {164if (print_comma)165fprintf(fp, ", ");166else167print_comma = true;168169fprintf(fp, "slot %u", I->scoreboard);170}171172if (info.immediates & AGX_IMMEDIATE_NEST) {173if (print_comma)174fprintf(fp, ", ");175else176print_comma = true;177178fprintf(fp, "n=%u", I->nest);179}180181if ((info.immediates & AGX_IMMEDIATE_INVERT_COND) && I->invert_cond) {182if (print_comma)183fprintf(fp, ", ");184else185print_comma = true;186187fprintf(fp, "inv");188}189190fprintf(fp, "\n");191}192193void194agx_print_block(agx_block *block, FILE *fp)195{196fprintf(fp, "block%u {\n", block->name);197198agx_foreach_instr_in_block(block, ins)199agx_print_instr(ins, fp);200201fprintf(fp, "}");202203if (block->successors[0]) {204fprintf(fp, " -> ");205206agx_foreach_successor(block, succ)207fprintf(fp, "block%u ", succ->name);208}209210if (block->predecessors->entries) {211fprintf(fp, " from");212213agx_foreach_predecessor(block, pred)214fprintf(fp, " block%u", pred->name);215}216217fprintf(fp, "\n\n");218}219220void221agx_print_shader(agx_context *ctx, FILE *fp)222{223agx_foreach_block(ctx, block)224agx_print_block(block, fp);225}226227228