Path: blob/21.2-virgl/src/compiler/spirv/spirv_info_c.py
4545 views
COPYRIGHT = """\1/*2* Copyright (C) 2017 Intel Corporation3*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, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*/23"""2425import argparse26import json27from sys import stdout28from mako.template import Template2930def collect_data(spirv, kind):31for x in spirv["operand_kinds"]:32if x["kind"] == kind:33operands = x34break3536# There are some duplicate values in some of the tables (thanks guys!), so37# filter them out.38seen = set()39values = []40for x in operands["enumerants"]:41if x["value"] not in seen:42seen.add(x["value"])43values.append(x["enumerant"])4445return (kind, values, operands["category"])4647def collect_opcodes(spirv):48seen = set()49values = []50for x in spirv["instructions"]:51# Handle aliases by choosing the first one in the grammar.52# E.g. OpDecorateString and OpDecorateStringGOOGLE share same opcode.53if x["opcode"] in seen:54continue55opcode = x["opcode"]56name = x["opname"]57assert name.startswith("Op")58values.append(name[2:])59seen.add(opcode)6061return ("Op", values, None)6263def parse_args():64p = argparse.ArgumentParser()65p.add_argument("json")66p.add_argument("out")67return p.parse_args()6869TEMPLATE = Template("""\70/* DO NOT EDIT - This file is generated automatically by spirv_info_c.py script */7172""" + COPYRIGHT + """\73#include "spirv_info.h"74% for kind,values,category in info:7576% if category == "BitEnum":77const char *78spirv_${kind.lower()}_to_string(Spv${kind}Mask v)79{80switch (v) {81% for name in values:82%if name != "None":83case Spv${kind}${name}Mask: return "Spv${kind}${name}";84% else:85case Spv${kind}MaskNone: return "Spv${kind}${name}";86% endif87% endfor88}8990return "unknown";91}92% else:93const char *94spirv_${kind.lower()}_to_string(Spv${kind} v)95{96switch (v) {97% for name in values:98case Spv${kind}${name}: return "Spv${kind}${name}";99% endfor100case Spv${kind}Max: break; /* silence warnings about unhandled enums. */101}102103return "unknown";104}105% endif106% endfor107""")108109if __name__ == "__main__":110pargs = parse_args()111112spirv_info = json.JSONDecoder().decode(open(pargs.json, "r").read())113114info = [115collect_data(spirv_info, "AddressingModel"),116collect_data(spirv_info, "BuiltIn"),117collect_data(spirv_info, "Capability"),118collect_data(spirv_info, "Decoration"),119collect_data(spirv_info, "Dim"),120collect_data(spirv_info, "ExecutionMode"),121collect_data(spirv_info, "ExecutionModel"),122collect_data(spirv_info, "ImageFormat"),123collect_data(spirv_info, "MemoryModel"),124collect_data(spirv_info, "StorageClass"),125collect_data(spirv_info, "ImageOperands"),126collect_data(spirv_info, "FPRoundingMode"),127collect_opcodes(spirv_info),128]129130with open(pargs.out, 'w') as f:131f.write(TEMPLATE.render(info=info))132133134