Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / gallium / drivers / swr / rasterizer / codegen / gen_llvm_types.py
1 # Copyright (C) 2014-2018 Intel Corporation. All Rights Reserved.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the "Software"),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
12 # Software.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 # IN THE SOFTWARE.
21
22 from __future__ import print_function
23 import os, sys, re
24 from gen_common import *
25 from argparse import FileType
26
27 '''
28 '''
29 def gen_llvm_type(type, name, idx, is_pointer, is_pointer_pointer, is_array, is_array_array, array_count, array_count1, is_llvm_struct, is_llvm_enum, is_llvm_pfn, output_file):
30
31 llvm_type = ''
32
33 if is_llvm_struct:
34 if is_pointer or is_pointer_pointer:
35 llvm_type = 'Type::getInt32Ty(ctx)'
36 else:
37 llvm_type = 'ArrayType::get(Type::getInt8Ty(ctx), sizeof(%s))' % type
38 elif is_llvm_enum:
39 llvm_type = 'Type::getInt32Ty(ctx)'
40 elif is_llvm_pfn:
41 llvm_type = 'PointerType::get(Type::getInt8Ty(ctx), 0)'
42 else:
43 if type == 'BYTE' or type == 'char' or type == 'uint8_t' or type == 'int8_t' or type == 'bool':
44 llvm_type = 'Type::getInt8Ty(ctx)'
45 elif type == 'UINT64' or type == 'INT64' or type == 'uint64_t' or type == 'int64_t' or type == 'gfxptr_t':
46 llvm_type = 'Type::getInt64Ty(ctx)'
47 elif type == 'UINT16' or type == 'int16_t' or type == 'uint16_t':
48 llvm_type = 'Type::getInt16Ty(ctx)'
49 elif type == 'UINT' or type == 'INT' or type == 'int' or type == 'BOOL' or type == 'uint32_t' or type == 'int32_t':
50 llvm_type = 'Type::getInt32Ty(ctx)'
51 elif type == 'float' or type == 'FLOAT':
52 llvm_type = 'Type::getFloatTy(ctx)'
53 elif type == 'double' or type == 'DOUBLE':
54 llvm_type = 'Type::getDoubleTy(ctx)'
55 elif type == 'void' or type == 'VOID':
56 llvm_type = 'Type::getInt32Ty(ctx)'
57 elif type == 'HANDLE':
58 llvm_type = 'PointerType::get(Type::getInt32Ty(ctx), 0)'
59 elif type == 'simdscalar':
60 llvm_type = 'getVectorType(Type::getFloatTy(ctx), pJitMgr->mVWidth)'
61 elif type == 'simdscalari':
62 llvm_type = 'getVectorType(Type::getInt32Ty(ctx), pJitMgr->mVWidth)'
63 elif type == 'simd16scalar':
64 llvm_type = 'getVectorType(Type::getFloatTy(ctx), 16)'
65 elif type == 'simd16scalari':
66 llvm_type = 'getVectorType(Type::getInt32Ty(ctx), 16)'
67 elif type == '__m128i':
68 llvm_type = 'getVectorType(Type::getInt32Ty(ctx), 4)'
69 elif type == 'SIMD256::Float':
70 llvm_type = 'getVectorType(Type::getFloatTy(ctx), 8)'
71 elif type == 'SIMD256::Integer':
72 llvm_type = 'getVectorType(Type::getInt32Ty(ctx), 8)'
73 elif type == 'SIMD512::Float':
74 llvm_type = 'getVectorType(Type::getFloatTy(ctx), 16)'
75 elif type == 'SIMD512::Integer':
76 llvm_type = 'getVectorType(Type::getInt32Ty(ctx), 16)'
77 elif type == 'simdvector':
78 llvm_type = 'ArrayType::get(getVectorType(Type::getFloatTy(ctx), 8), 4)'
79 elif type == 'simd16vector':
80 llvm_type = 'ArrayType::get(getVectorType(Type::getFloatTy(ctx), 16), 4)'
81 elif type == 'SIMD256::Vec4':
82 llvm_type = 'ArrayType::get(getVectorType(Type::getFloatTy(ctx), 8), 4)'
83 elif type == 'SIMD512::Vec4':
84 llvm_type = 'ArrayType::get(getVectorType(Type::getFloatTy(ctx), 16), 4)'
85 else:
86 llvm_type = 'Gen_%s(pJitMgr)' % type
87
88 if is_pointer:
89 llvm_type = 'PointerType::get(%s, 0)' % llvm_type
90
91 if is_pointer_pointer:
92 llvm_type = 'PointerType::get(%s, 0)' % llvm_type
93
94 if is_array_array:
95 llvm_type = 'ArrayType::get(ArrayType::get(%s, %s), %s)' % (llvm_type, array_count1, array_count)
96 elif is_array:
97 llvm_type = 'ArrayType::get(%s, %s)' % (llvm_type, array_count)
98
99 return {
100 'name' : name,
101 'lineNum' : idx,
102 'type' : llvm_type,
103 }
104
105 '''
106 '''
107 def gen_llvm_types(input_file, output_file):
108
109 lines = input_file.readlines()
110
111 types = []
112
113 for idx in range(len(lines)):
114 line = lines[idx].rstrip()
115
116 if 'gen_llvm_types FINI' in line:
117 break
118
119 match = re.match(r'(\s*)struct(\s*)(\w+)', line)
120 if match:
121 llvm_args = []
122
123 # Detect start of structure
124 is_fwd_decl = re.search(r';', line)
125
126 if not is_fwd_decl:
127
128 # Extract the command name
129 struct_name = match.group(3).strip()
130
131 type_entry = {
132 'name' : struct_name,
133 'lineNum' : idx+1,
134 'members' : [],
135 }
136
137 end_of_struct = False
138
139 while not end_of_struct and idx < len(lines)-1:
140 idx += 1
141 line = lines[idx].rstrip()
142
143 is_llvm_typedef = re.search(r'@llvm_typedef', line)
144 if is_llvm_typedef is not None:
145 is_llvm_typedef = True
146 continue
147 else:
148 is_llvm_typedef = False
149
150 ###########################################
151 # Is field a llvm struct? Tells script to treat type as array of bytes that is size of structure.
152 is_llvm_struct = re.search(r'@llvm_struct', line)
153
154 if is_llvm_struct is not None:
155 is_llvm_struct = True
156 else:
157 is_llvm_struct = False
158
159 ###########################################
160 # Is field the start of a function? Tells script to ignore it
161 is_llvm_func_start = re.search(r'@llvm_func_start', line)
162
163 if is_llvm_func_start is not None:
164 while not end_of_struct and idx < len(lines)-1:
165 idx += 1
166 line = lines[idx].rstrip()
167 is_llvm_func_end = re.search(r'@llvm_func_end', line)
168 if is_llvm_func_end is not None:
169 break;
170 continue
171
172 ###########################################
173 # Is field a function? Tells script to ignore it
174 is_llvm_func = re.search(r'@llvm_func', line)
175
176 if is_llvm_func is not None:
177 continue
178
179 ###########################################
180 # Is field a llvm enum? Tells script to treat type as an enum and replaced with uint32 type.
181 is_llvm_enum = re.search(r'@llvm_enum', line)
182
183 if is_llvm_enum is not None:
184 is_llvm_enum = True
185 else:
186 is_llvm_enum = False
187
188 ###########################################
189 # Is field a llvm function pointer? Tells script to treat type as an enum and replaced with uint32 type.
190 is_llvm_pfn = re.search(r'@llvm_pfn', line)
191
192 if is_llvm_pfn is not None:
193 is_llvm_pfn = True
194 else:
195 is_llvm_pfn = False
196
197 ###########################################
198 # Is field const?
199 is_const = re.search(r'\s+const\s+', line)
200
201 if is_const is not None:
202 is_const = True
203 else:
204 is_const = False
205
206 ###########################################
207 # Is field a pointer?
208 is_pointer_pointer = re.search('\*\*', line)
209
210 if is_pointer_pointer is not None:
211 is_pointer_pointer = True
212 else:
213 is_pointer_pointer = False
214
215 ###########################################
216 # Is field a pointer?
217 is_pointer = re.search('\*', line)
218
219 if is_pointer is not None:
220 is_pointer = True
221 else:
222 is_pointer = False
223
224 ###########################################
225 # Is field an array of arrays?
226 # TODO: Can add this to a list.
227 is_array_array = re.search('\[(\w*)\]\[(\w*)\]', line)
228 array_count = '0'
229 array_count1 = '0'
230
231 if is_array_array is not None:
232 array_count = is_array_array.group(1)
233 array_count1 = is_array_array.group(2)
234 is_array_array = True
235 else:
236 is_array_array = False
237
238 ###########################################
239 # Is field an array?
240 is_array = re.search('\[(\w*)\]', line)
241
242 if is_array is not None:
243 array_count = is_array.group(1)
244 is_array = True
245 else:
246 is_array = False
247
248 is_scoped = re.search('::', line)
249
250 if is_scoped is not None:
251 is_scoped = True
252 else:
253 is_scoped = False
254
255 type = None
256 name = None
257 if is_const and is_pointer:
258
259 if is_scoped:
260 field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+::)(\w+)(\s*\**\s*)(\w+)', line)
261
262 type = '%s%s' % (field_match.group(4), field_match.group(5))
263 name = field_match.group(7)
264 else:
265 field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)(\s*\**\s*)(\w+)', line)
266
267 type = field_match.group(4)
268 name = field_match.group(6)
269
270 elif is_pointer:
271 field_match = re.match(r'(\s*)(\s+)(\w+\<*\w*\>*)(\s*\**\s*)(\w+)', line)
272
273 if field_match:
274 type = field_match.group(3)
275 name = field_match.group(5)
276 elif is_const:
277 field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)(\s*)(\w+)', line)
278
279 if field_match:
280 type = field_match.group(4)
281 name = field_match.group(6)
282 else:
283 if is_scoped:
284 field_match = re.match(r'\s*(\w+\<*\w*\>*)\s*::\s*(\w+\<*\w*\>*)\s+(\w+)', line)
285
286 if field_match:
287 type = field_match.group(1) + '::' + field_match.group(2)
288 name = field_match.group(3)
289 else:
290 field_match = re.match(r'(\s*)(\w+\<*\w*\>*)(\s+)(\w+)', line)
291
292 if field_match:
293 type = field_match.group(2)
294 name = field_match.group(4)
295
296 if is_llvm_typedef is False:
297 if type is not None:
298 type_entry['members'].append(
299 gen_llvm_type(
300 type, name, idx+1, is_pointer, is_pointer_pointer, is_array, is_array_array,
301 array_count, array_count1, is_llvm_struct, is_llvm_enum, is_llvm_pfn, output_file))
302
303 # Detect end of structure
304 end_of_struct = re.match(r'(\s*)};', line)
305
306 if end_of_struct:
307 types.append(type_entry)
308
309 cur_dir = os.path.dirname(os.path.abspath(__file__))
310 template = os.path.join(cur_dir, 'templates', 'gen_llvm.hpp')
311
312 MakoTemplateWriter.to_file(
313 template,
314 output_file,
315 cmdline=sys.argv,
316 filename=os.path.basename(output_file),
317 types=types,
318 input_dir=os.path.dirname(input_file.name),
319 input_file=os.path.basename(input_file.name))
320
321 '''
322 Function which is invoked when this script is started from a command line.
323 Will present and consume a set of arguments which will tell this script how
324 to behave
325 '''
326 def main():
327
328 # Parse args...
329 parser = ArgumentParser()
330 parser.add_argument('--input', '-i', type=FileType('r'),
331 help='Path to input file containing structs', required=True)
332 parser.add_argument('--output', '-o', action='store',
333 help='Path to output file', required=True)
334 args = parser.parse_args()
335
336 final_output_dir = os.path.dirname(args.output)
337 if MakeDir(final_output_dir):
338 return 1
339
340 final_output_file = args.output
341
342 tmp_dir = MakeTmpDir('_codegen')
343 args.output = os.path.join(tmp_dir, os.path.basename(args.output))
344
345 rval = 0
346 try:
347 gen_llvm_types(args.input, args.output)
348
349 rval = CopyFileIfDifferent(args.output, final_output_file)
350 except:
351 print('ERROR: Could not generate llvm types', file=sys.stderr)
352 rval = 1
353
354 finally:
355 DeleteDirTree(tmp_dir)
356
357 return rval
358
359 if __name__ == '__main__':
360 sys.exit(main())
361 # END OF FILE