2 # Copyright (c) 2017 Google Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 """Generates language headers from a JSON grammar file"""
17 from __future__ import print_function
25 def make_path_to_file(f):
26 """Makes all ancestor directories to the given file, if they
30 f: The file whose ancestor directories are to be created.
32 dir = os.path.dirname(os.path.abspath(f))
36 if e.errno == errno.EEXIST and os.path.isdir(dir):
42 """The grammar for an extended instruction set"""
44 def __init__(self, name, copyright, instructions, operand_kinds, version = None, revision = None):
46 self.copyright = copyright
47 self.instructions = instructions
48 self.operand_kinds = operand_kinds
49 self.version = version
50 self.revision = revision
54 """A language-specific generator"""
57 self.upper_case_initial = re.compile('^[A-Z]')
60 def comment_prefix(self):
63 def namespace_prefix(self):
66 def uses_guards(self):
69 def cpp_guard_preamble(self):
72 def cpp_guard_postamble(self):
75 def enum_value(self, prefix, name, value):
76 if self.upper_case_initial.match(name):
81 return " {}{} = {},".format(prefix, use_name, value)
83 def generate(self, grammar):
84 """Returns a string that is the language-specific header for the given grammar"""
88 parts.extend(["{}{}".format(self.comment_prefix(), f) for f in grammar.copyright])
91 guard = 'SPIRV_EXTINST_{}_H_'.format(grammar.name)
93 parts.append('#ifndef {}'.format(guard))
94 parts.append('#define {}'.format(guard))
97 parts.append(self.cpp_guard_preamble())
100 parts.append(self.const_definition(grammar.name, 'Version', grammar.version))
102 if grammar.revision is not None:
103 parts.append(self.const_definition(grammar.name, 'Revision', grammar.revision))
107 if grammar.instructions:
108 parts.append(self.enum_prefix(grammar.name, 'Instructions'))
109 for inst in grammar.instructions:
110 parts.append(self.enum_value(grammar.name, inst['opname'], inst['opcode']))
111 parts.append(self.enum_end(grammar.name, 'Instructions'))
114 if grammar.operand_kinds:
115 for kind in grammar.operand_kinds:
116 parts.append(self.enum_prefix(grammar.name, kind['kind']))
117 for e in kind['enumerants']:
118 parts.append(self.enum_value(grammar.name, e['enumerant'], e['value']))
119 parts.append(self.enum_end(grammar.name, kind['kind']))
122 parts.append(self.cpp_guard_postamble())
125 parts.append('#endif // {}'.format(guard))
127 return '\n'.join(parts)
130 class CLikeGenerator(LangGenerator):
131 def uses_guards(self):
134 def comment_prefix(self):
137 def const_definition(self, prefix, var, value):
138 # Use an anonymous enum. Don't use a static const int variable because
139 # that can bloat binary size.
140 return 'enum {0} {1}{2} = {3}, {1}{2}_BitWidthPadding = 0x7fffffff {4};'.format(
141 '{', prefix, var, value, '}')
143 def enum_prefix(self, prefix, name):
144 return 'enum {}{} {}'.format(prefix, name, '{')
146 def enum_end(self, prefix, enum):
147 return ' {}{}Max = 0x7ffffff\n{};\n'.format(prefix, enum, '}')
149 def cpp_guard_preamble(self):
150 return '#ifdef __cplusplus\nextern "C" {\n#endif\n'
152 def cpp_guard_postamble(self):
153 return '#ifdef __cplusplus\n}\n#endif\n'
156 class CGenerator(CLikeGenerator):
162 parser = argparse.ArgumentParser(description='Generate language headers from a JSON grammar')
164 parser.add_argument('--extinst-name',
165 type=str, required=True,
166 help='The name to use in tokens')
167 parser.add_argument('--extinst-grammar', metavar='<path>',
168 type=str, required=True,
169 help='input JSON grammar file for extended instruction set')
170 parser.add_argument('--extinst-output-base', metavar='<path>',
171 type=str, required=True,
172 help='Basename of the language-specific output file.')
173 args = parser.parse_args()
175 with open(args.extinst_grammar) as json_file:
176 grammar_json = json.loads(json_file.read())
177 grammar = ExtInstGrammar(name = args.extinst_name,
178 copyright = grammar_json['copyright'],
179 instructions = grammar_json['instructions'],
180 operand_kinds = grammar_json['operand_kinds'],
181 version = grammar_json['version'],
182 revision = grammar_json['revision'])
183 make_path_to_file(args.extinst_output_base)
184 print(CGenerator().generate(grammar), file=open(args.extinst_output_base + '.h', 'w'))
187 if __name__ == '__main__':