blob: 99bd50470800879beba7ac4afcae3d1dfcbc352c [file] [log] [blame]
Chia-I Wufb2559d2014-08-01 11:19:52 +08001#!/usr/bin/env python3
Chia-I Wu701f3f62014-09-02 08:32:09 +08002#
3# XGL
4#
5# Copyright (C) 2014 LunarG, Inc.
6#
7# Permission is hereby granted, free of charge, to any person obtaining a
8# copy of this software and associated documentation files (the "Software"),
9# to deal in the Software without restriction, including without limitation
10# the rights to use, copy, modify, merge, publish, distribute, sublicense,
11# and/or sell copies of the Software, and to permit persons to whom the
12# Software is furnished to do so, subject to the following conditions:
13#
14# The above copyright notice and this permission notice shall be included
15# in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23# DEALINGS IN THE SOFTWARE.
24#
25# Authors:
26# Chia-I Wu <olv@lunarg.com>
Chia-I Wufb2559d2014-08-01 11:19:52 +080027
28import sys
29
30import xgl
31
32class Subcommand(object):
33 def __init__(self, argv):
34 self.argv = argv
Chia-I Wuc4f24e82015-01-01 08:46:31 +080035 self.headers = xgl.headers
36 self.protos = xgl.protos
Chia-I Wufb2559d2014-08-01 11:19:52 +080037
38 def run(self):
Chia-I Wufb2559d2014-08-01 11:19:52 +080039 print(self.generate())
40
41 def generate(self):
42 copyright = self.generate_copyright()
43 header = self.generate_header()
44 body = self.generate_body()
45 footer = self.generate_footer()
46
47 contents = []
48 if copyright:
49 contents.append(copyright)
50 if header:
51 contents.append(header)
52 if body:
53 contents.append(body)
54 if footer:
55 contents.append(footer)
56
57 return "\n\n".join(contents)
58
59 def generate_copyright(self):
60 return """/* THIS FILE IS GENERATED. DO NOT EDIT. */
61
62/*
63 * XGL
64 *
65 * Copyright (C) 2014 LunarG, Inc.
66 *
67 * Permission is hereby granted, free of charge, to any person obtaining a
68 * copy of this software and associated documentation files (the "Software"),
69 * to deal in the Software without restriction, including without limitation
70 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
71 * and/or sell copies of the Software, and to permit persons to whom the
72 * Software is furnished to do so, subject to the following conditions:
73 *
74 * The above copyright notice and this permission notice shall be included
75 * in all copies or substantial portions of the Software.
76 *
77 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
80 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
82 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
83 * DEALINGS IN THE SOFTWARE.
84 */"""
85
86 def generate_header(self):
Chia-I Wu6bdf0192014-09-13 13:36:06 +080087 return "\n".join(["#include <" + h + ">" for h in self.headers])
Chia-I Wufb2559d2014-08-01 11:19:52 +080088
89 def generate_body(self):
90 pass
91
92 def generate_footer(self):
93 pass
94
Chia-I Wu2e4f5302015-01-02 00:21:24 +080095class LoaderSubcommand(Subcommand):
96 def generate_header(self):
97 return "#include \"loader.h\""
98
99 def _generate_loader_dispatch_entrypoints(self, qual=""):
Chia-I Wu19300602014-08-04 08:03:57 +0800100 if qual:
101 qual += " "
102
Chia-I Wudac3e492014-08-02 23:49:43 +0800103 funcs = []
104 for proto in self.protos:
Tobin Ehlisea7fe0b2014-11-12 11:47:07 -0700105 if not xgl.is_dispatchable(proto):
106 continue
107 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
108 stmt = "(*disp)->%s" % proto.c_call()
Chia-I Wu2e4f5302015-01-02 00:21:24 +0800109 if proto.name == "CreateDevice":
Tobin Ehlisea7fe0b2014-11-12 11:47:07 -0700110 funcs.append("%s%s\n"
111 "{\n"
Jon Ashburn613b0c12014-11-17 10:17:37 -0700112 " loader_activate_layers(%s, %s);\n"
Tobin Ehlisea7fe0b2014-11-12 11:47:07 -0700113 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
Jon Ashburne05c8c62014-12-03 14:30:48 -0700114 " const XGL_LAYER_DISPATCH_TABLE **disp =\n"
115 " (const XGL_LAYER_DISPATCH_TABLE **) wrapped_obj->baseObject;\n"
Tobin Ehlisea7fe0b2014-11-12 11:47:07 -0700116 " %s = wrapped_obj->nextObject;\n"
Jon Ashburne05c8c62014-12-03 14:30:48 -0700117 " XGL_RESULT res = %s;\n"
118 " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n"
119 " return res;\n"
120 "}" % (qual, decl, proto.params[0].name, proto.params[1].name,
121 proto.params[0].name, proto.params[0].name, stmt,
122 proto.params[-1].name))
123 elif xgl.does_function_create_object(proto.name) and qual == "LOADER_EXPORT ":
124 funcs.append("%s%s\n"
125 "{\n"
126 " const XGL_LAYER_DISPATCH_TABLE **disp =\n"
127 " (const XGL_LAYER_DISPATCH_TABLE **) %s;\n"
128 " XGL_RESULT res = %s;\n"
129 " *(const XGL_LAYER_DISPATCH_TABLE **) (*%s) = *disp;\n"
130 " return res;\n"
131 "}" % (qual, decl, proto.params[0].name, stmt, proto.params[-1].name))
Chia-I Wu2e4f5302015-01-02 00:21:24 +0800132 elif proto.name == "GetMultiGpuCompatibility":
Jon Ashburne05c8c62014-12-03 14:30:48 -0700133 funcs.append("%s%s\n"
134 "{\n"
135 " XGL_BASE_LAYER_OBJECT* wrapped_obj0 = (XGL_BASE_LAYER_OBJECT*)%s;\n"
136 " XGL_BASE_LAYER_OBJECT* wrapped_obj1 = (XGL_BASE_LAYER_OBJECT*)%s;\n"
137 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
138 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj0->baseObject;\n"
139 " %s = wrapped_obj0->nextObject;\n"
140 " %s = wrapped_obj1->nextObject;\n"
141 " return %s;\n"
142 "}" % (qual, decl, proto.params[0].name, proto.params[1].name,
143 proto.params[0].name, proto.params[1].name, stmt))
Chia-I Wu2e4f5302015-01-02 00:21:24 +0800144 elif proto.params[0].ty != "XGL_PHYSICAL_GPU":
Jon Ashburne05c8c62014-12-03 14:30:48 -0700145 if proto.ret != "XGL_VOID":
146 stmt = "return " + stmt
Tobin Ehlisea7fe0b2014-11-12 11:47:07 -0700147 funcs.append("%s%s\n"
148 "{\n"
149 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
150 " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n"
151 " %s;\n"
152 "}" % (qual, decl, proto.params[0].name, stmt))
153 else:
Jon Ashburne05c8c62014-12-03 14:30:48 -0700154 if proto.ret != "XGL_VOID":
155 stmt = "return " + stmt
Tobin Ehlisea7fe0b2014-11-12 11:47:07 -0700156 funcs.append("%s%s\n"
157 "{\n"
158 " XGL_BASE_LAYER_OBJECT* wrapped_obj = (XGL_BASE_LAYER_OBJECT*)%s;\n"
159 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
160 " (const XGL_LAYER_DISPATCH_TABLE * const *) wrapped_obj->baseObject;\n"
161 " %s = wrapped_obj->nextObject;\n"
162 " %s;\n"
163 "}" % (qual, decl, proto.params[0].name, proto.params[0].name, stmt))
Chia-I Wudac3e492014-08-02 23:49:43 +0800164
165 return "\n\n".join(funcs)
166
Chia-I Wufb2559d2014-08-01 11:19:52 +0800167 def generate_body(self):
Chia-I Wu2e4f5302015-01-02 00:21:24 +0800168 body = [self._generate_loader_dispatch_entrypoints("LOADER_EXPORT")]
Jon Ashburnd38bfb12014-10-14 19:15:22 -0600169
170 return "\n\n".join(body)
171
Chia-I Wu03537f72014-08-03 09:55:18 +0800172class IcdDispatchEntrypointsSubcommand(Subcommand):
Chia-I Wu03537f72014-08-03 09:55:18 +0800173 def generate_header(self):
Chia-I Wu19300602014-08-04 08:03:57 +0800174 return "#include \"icd.h\""
Chia-I Wu03537f72014-08-03 09:55:18 +0800175
Chia-I Wu2e4f5302015-01-02 00:21:24 +0800176 def _generate_icd_dispatch_entrypoints(self, qual=""):
177 if qual:
178 qual += " "
179
180 funcs = []
181 for proto in self.protos:
182 if not xgl.is_dispatchable(proto):
183 continue
184
185 decl = proto.c_func(prefix="xgl", attr="XGLAPI")
186 stmt = "(*disp)->%s" % proto.c_call()
187 if proto.ret != "XGL_VOID":
188 stmt = "return " + stmt
189
190 funcs.append("%s%s\n"
191 "{\n"
192 " const XGL_LAYER_DISPATCH_TABLE * const *disp =\n"
193 " (const XGL_LAYER_DISPATCH_TABLE * const *) %s;\n"
194 " %s;\n"
195 "}" % (qual, decl, proto.params[0].name, stmt))
196
197 return "\n\n".join(funcs)
198
Chia-I Wu03537f72014-08-03 09:55:18 +0800199 def generate_body(self):
Chia-I Wu2e4f5302015-01-02 00:21:24 +0800200 return self._generate_icd_dispatch_entrypoints("ICD_EXPORT")
Chia-I Wu03537f72014-08-03 09:55:18 +0800201
Chia-I Wudf3c4322014-08-04 10:08:08 +0800202class IcdDispatchDummyImplSubcommand(Subcommand):
203 def run(self):
204 if len(self.argv) != 1:
205 print("IcdDispatchDummyImplSubcommand: <prefix> unspecified")
206 return
207
208 self.prefix = self.argv[0]
209
210 super().run()
211
212 def generate_header(self):
213 return "#include \"icd.h\""
214
215 def _generate_stub_decl(self, proto):
216 plist = []
217 for param in proto.params:
218 idx = param.ty.find("[")
219 if idx < 0:
220 idx = len(param.ty)
221
222 pad = 44 - idx
223 if pad <= 0:
224 pad = 1
225
226 plist.append(" %s%s%s%s" % (param.ty[:idx],
227 " " * pad, param.name, param.ty[idx:]))
228
229 return "%s XGLAPI %s%s(\n%s)" % (proto.ret, self.prefix,
230 proto.name, ",\n".join(plist))
231
232 def _generate_stubs(self):
233 stubs = []
234 for proto in self.protos:
235 if not xgl.is_dispatchable(proto):
236 continue
237
238 decl = self._generate_stub_decl(proto)
239 if proto.ret != "XGL_VOID":
240 stmt = " return XGL_ERROR_UNAVAILABLE;\n"
241 else:
242 stmt = ""
243
244 stubs.append("static %s\n{\n%s}" % (decl, stmt))
245
246 return "\n\n".join(stubs)
247
248
249 def _generate_tables(self):
250 initializer = []
251 for proto in self.protos:
252 prefix = self.prefix if xgl.is_dispatchable(proto) else "xgl"
253 initializer.append(".%s = %s%s" %
254 (proto.name, prefix, proto.name))
255
Jon Ashburn8c05b7a2014-12-03 11:13:40 -0700256 return """const XGL_LAYER_DISPATCH_TABLE %s_normal_dispatch_table = {
Chia-I Wudf3c4322014-08-04 10:08:08 +0800257 %s,
258};
259
Jon Ashburn8c05b7a2014-12-03 11:13:40 -0700260const XGL_LAYER_DISPATCH_TABLE %s_debug_dispatch_table = {
Chia-I Wudf3c4322014-08-04 10:08:08 +0800261 %s,
262};""" % (self.prefix, ",\n ".join(initializer),
263 self.prefix, ",\n ".join(initializer))
264
265 def generate_body(self):
266 body = [self._generate_stubs(),
267 self._generate_tables()]
268
269 return "\n\n".join(body)
270
Chia-I Wufb2559d2014-08-01 11:19:52 +0800271def main():
272 subcommands = {
Chia-I Wufb2559d2014-08-01 11:19:52 +0800273 "loader": LoaderSubcommand,
Chia-I Wu03537f72014-08-03 09:55:18 +0800274 "icd-dispatch-entrypoints": IcdDispatchEntrypointsSubcommand,
Chia-I Wudf3c4322014-08-04 10:08:08 +0800275 "icd-dispatch-dummy-impl": IcdDispatchDummyImplSubcommand,
Chia-I Wufb2559d2014-08-01 11:19:52 +0800276 }
277
278 if len(sys.argv) < 2 or sys.argv[1] not in subcommands:
279 print("Usage: %s <subcommand> [options]" % sys.argv[0])
280 print
281 print("Available sucommands are: %s" % " ".join(subcommands))
282 exit(1)
283
284 subcmd = subcommands[sys.argv[1]](sys.argv[2:])
285 subcmd.run()
286
287if __name__ == "__main__":
288 main()