3 * Copyright (C) 2017 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
28 from sys
import stdout
29 from mako
.template
import Template
31 def find_result_types(spirv
):
33 for inst
in spirv
['instructions']:
34 # Handle aliases by choosing the first one in the grammar.
35 if inst
['opcode'] in seen
:
37 seen
.add(inst
['opcode'])
41 if 'operands' not in inst
:
46 for idx
, arg
in enumerate(inst
['operands']):
47 if arg
['kind'] == 'IdResult':
49 elif arg
['kind'] == 'IdResultType':
50 res_type_arg_idx
= idx
52 if res_type_arg_idx
>= 0:
53 assert res_arg_idx
>= 0
54 elif res_arg_idx
>= 0:
61 assert name
.startswith('OpType') or name
in untyped_insts
63 if res_arg_idx
>= 0 or res_type_arg_idx
>= 0:
64 yield (name
, res_arg_idx
, res_type_arg_idx
)
66 TEMPLATE
= Template(COPYRIGHT
+ """\
68 /* DO NOT EDIT - This file is generated automatically by the
69 * vtn_gather_types_c.py script
72 #include "vtn_private.h"
79 static struct type_args
80 result_type_args_for_opcode(SpvOp opcode)
83 % for opcode in opcodes:
84 case Spv${opcode[0]}: return (struct type_args){ ${opcode[1]}, ${opcode[2]} };
86 default: return (struct type_args){ -1, -1 };
91 vtn_set_instruction_result_type(struct vtn_builder *b, SpvOp opcode,
92 const uint32_t *w, unsigned count)
94 struct type_args args = result_type_args_for_opcode(opcode);
96 if (args.res_idx >= 0 && args.res_type_idx >= 0) {
97 struct vtn_value *val = vtn_untyped_value(b, w[1 + args.res_idx]);
98 val->type = vtn_value(b, w[1 + args.res_type_idx],
99 vtn_value_type_type)->type;
107 if __name__
== "__main__":
108 p
= argparse
.ArgumentParser()
109 p
.add_argument("json")
110 p
.add_argument("out")
111 args
= p
.parse_args()
113 spirv_info
= json
.JSONDecoder().decode(open(args
.json
, "r").read())
115 opcodes
= list(find_result_types(spirv_info
))
118 with
open(args
.out
, 'w') as f
:
119 f
.write(TEMPLATE
.render(opcodes
=opcodes
))
121 # In the even there's an error this imports some helpers from mako
122 # to print a useful stack trace and prints it, then exits with
123 # status 1, if python is run with debug; otherwise it just raises
127 from mako
import exceptions
128 sys
.stderr
.write(exceptions
.text_error_template().render() + '\n')