1b8e80941SmrgCOPYRIGHT = """\
2b8e80941Smrg/*
3b8e80941Smrg * Copyright (C) 2017 Intel Corporation
4b8e80941Smrg *
5b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
7b8e80941Smrg * to deal in the Software without restriction, including without limitation
8b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
10b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
11b8e80941Smrg *
12b8e80941Smrg * The above copyright notice and this permission notice (including the next
13b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
14b8e80941Smrg * Software.
15b8e80941Smrg *
16b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22b8e80941Smrg * DEALINGS IN THE SOFTWARE.
23b8e80941Smrg */
24b8e80941Smrg"""
25b8e80941Smrg
26b8e80941Smrgimport argparse
27b8e80941Smrgimport json
28b8e80941Smrgfrom sys import stdout
29b8e80941Smrgfrom mako.template import Template
30b8e80941Smrg
31b8e80941Smrgdef collect_data(spirv, kind):
32b8e80941Smrg    for x in spirv["operand_kinds"]:
33b8e80941Smrg        if x["kind"] == kind:
34b8e80941Smrg            operands = x
35b8e80941Smrg            break
36b8e80941Smrg
37b8e80941Smrg    # There are some duplicate values in some of the tables (thanks guys!), so
38b8e80941Smrg    # filter them out.
39b8e80941Smrg    seen = set()
40b8e80941Smrg    values = []
41b8e80941Smrg    for x in operands["enumerants"]:
42b8e80941Smrg        if x["value"] not in seen:
43b8e80941Smrg            seen.add(x["value"])
44b8e80941Smrg            values.append(x["enumerant"])
45b8e80941Smrg
46b8e80941Smrg    return (kind, values)
47b8e80941Smrg
48b8e80941Smrgdef collect_opcodes(spirv):
49b8e80941Smrg    values = []
50b8e80941Smrg    for x in spirv["instructions"]:
51b8e80941Smrg        name = x["opname"]
52b8e80941Smrg        assert name.startswith("Op")
53b8e80941Smrg        values.append(name[2:])
54b8e80941Smrg
55b8e80941Smrg    return ("Op", values)
56b8e80941Smrg
57b8e80941Smrgdef parse_args():
58b8e80941Smrg    p = argparse.ArgumentParser()
59b8e80941Smrg    p.add_argument("json")
60b8e80941Smrg    p.add_argument("out")
61b8e80941Smrg    return p.parse_args()
62b8e80941Smrg
63b8e80941SmrgTEMPLATE  = Template("""\
64b8e80941Smrg/* DO NOT EDIT - This file is generated automatically by spirv_info_c.py script */
65b8e80941Smrg
66b8e80941Smrg""" + COPYRIGHT + """\
67b8e80941Smrg#include "spirv_info.h"
68b8e80941Smrg% for kind,values in info:
69b8e80941Smrg
70b8e80941Smrgconst char *
71b8e80941Smrgspirv_${kind.lower()}_to_string(Spv${kind} v)
72b8e80941Smrg{
73b8e80941Smrg   switch (v) {
74b8e80941Smrg    % for name in values:
75b8e80941Smrg   case Spv${kind}${name}: return "Spv${kind}${name}";
76b8e80941Smrg    % endfor
77b8e80941Smrg   case Spv${kind}Max: break; /* silence warnings about unhandled enums. */
78b8e80941Smrg   }
79b8e80941Smrg
80b8e80941Smrg   return "unknown";
81b8e80941Smrg}
82b8e80941Smrg% endfor
83b8e80941Smrg""")
84b8e80941Smrg
85b8e80941Smrgif __name__ == "__main__":
86b8e80941Smrg    pargs = parse_args()
87b8e80941Smrg
88b8e80941Smrg    spirv_info = json.JSONDecoder().decode(open(pargs.json, "r").read())
89b8e80941Smrg
90b8e80941Smrg    info = [
91b8e80941Smrg        collect_data(spirv_info, "AddressingModel"),
92b8e80941Smrg        collect_data(spirv_info, "BuiltIn"),
93b8e80941Smrg        collect_data(spirv_info, "Capability"),
94b8e80941Smrg        collect_data(spirv_info, "Decoration"),
95b8e80941Smrg        collect_data(spirv_info, "Dim"),
96b8e80941Smrg        collect_data(spirv_info, "ExecutionMode"),
97b8e80941Smrg        collect_data(spirv_info, "ExecutionModel"),
98b8e80941Smrg        collect_data(spirv_info, "ImageFormat"),
99b8e80941Smrg        collect_data(spirv_info, "StorageClass"),
100b8e80941Smrg        collect_opcodes(spirv_info),
101b8e80941Smrg    ]
102b8e80941Smrg
103b8e80941Smrg    with open(pargs.out, 'w') as f:
104b8e80941Smrg        f.write(TEMPLATE.render(info=info))
105