Switch from cElementTree to ElementTree.
[mesa.git] / src / mapi / new / gen_gldispatch_mapi.py
1 #!/usr/bin/env python
2
3 # Copyright (C) 2010 LunarG Inc.
4 # (C) Copyright 2015, NVIDIA CORPORATION.
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 # and/or sell copies of the Software, and to permit persons to whom the
11 # Software is furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
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.
23 #
24 # Authors:
25 # Kyle Brenneman <kbrenneman@nvidia.com>
26 #
27 # Based on code ogiginally by:
28 # Chia-I Wu <olv@lunarg.com>
29
30
31 """
32 Generates the glapi_mapi_tmp.h header file from Khronos's XML file.
33 """
34
35 import sys
36 import xml.etree.ElementTree as etree
37
38 import genCommon
39
40 def _main():
41 target = sys.argv[1]
42 xmlFiles = sys.argv[2:]
43
44 roots = [ etree.parse(filename).getroot() for filename in xmlFiles ]
45 allFunctions = genCommon.getFunctionsFromRoots(roots)
46
47 names = genCommon.getExportNamesFromRoots(target, roots)
48 functions = [f for f in allFunctions if(f.name in names)]
49
50 if (target in ("gl", "gldispatch")):
51 assert(len(functions) == len(allFunctions))
52 assert(all(functions[i] == allFunctions[i] for i in range(len(functions))))
53 assert(all(functions[i].slot == i for i in range(len(functions))))
54
55 print(r"""
56 /* This file is automatically generated by mapi_abi.py. Do not modify. */
57
58 #ifndef _GLAPI_TMP_H_
59 #define _GLAPI_TMP_H_
60 typedef int GLclampx;
61 #endif /* _GLAPI_TMP_H_ */
62 """.lstrip("\n"))
63
64 print(generate_defines(functions))
65 if target == "gldispatch":
66 print(generate_table(functions, allFunctions))
67 print(generate_noop_array(functions))
68 print(generate_public_stubs(functions))
69 print(generate_public_entries(functions))
70 if target == "gldispatch":
71 print(generate_public_entries_table(functions))
72 print(generate_undef_public_entries())
73 print(generate_stub_asm_gcc(functions))
74
75 def generate_defines(functions):
76 text = r"""
77 #ifdef MAPI_TMP_DEFINES
78 #define GL_GLEXT_PROTOTYPES
79 #include "GL/gl.h"
80 #include "GL/glext.h"
81
82 """.lstrip("\n")
83 for func in functions:
84 text += "GLAPI {f.rt} APIENTRY {f.name}({f.decArgs});\n".format(f=func)
85 text += "#undef MAPI_TMP_DEFINES\n"
86 text += "#endif /* MAPI_TMP_DEFINES */\n"
87 return text
88
89 def generate_table(functions, allFunctions):
90 text = "#ifdef MAPI_TMP_TABLE\n"
91 text += "#define MAPI_TABLE_NUM_STATIC %d\n" % (len(allFunctions))
92 text += "#define MAPI_TABLE_NUM_DYNAMIC %d\n" % (genCommon.MAPI_TABLE_NUM_DYNAMIC,)
93 text += "#undef MAPI_TMP_TABLE\n"
94 text += "#endif /* MAPI_TMP_TABLE */\n"
95 return text
96
97 def generate_noop_array(functions):
98 text = "#ifdef MAPI_TMP_NOOP_ARRAY\n"
99 text += "#ifdef DEBUG\n\n"
100
101 for func in functions:
102 text += "static {f.rt} APIENTRY noop{f.basename}({f.decArgs})\n".format(f=func)
103 text += "{\n"
104 if (len(func.args) > 0):
105 text += " "
106 for arg in func.args:
107 text += " (void) {a.name};".format(a=arg)
108 text += "\n"
109 text += " noop_warn(\"{f.name}\");\n".format(f=func)
110 if (func.hasReturn()):
111 text += " return ({f.rt}) 0;\n".format(f=func)
112 text += "}\n\n"
113
114 text += "const mapi_func table_noop_array[] = {\n"
115 for func in functions:
116 text += " (mapi_func) noop{f.basename},\n".format(f=func)
117 for i in range(genCommon.MAPI_TABLE_NUM_DYNAMIC - 1):
118 text += " (mapi_func) noop_generic,\n"
119 text += " (mapi_func) noop_generic\n"
120 text += "};\n\n"
121 text += "#else /* DEBUG */\n\n"
122 text += "const mapi_func table_noop_array[] = {\n"
123 for i in range(len(functions) + genCommon.MAPI_TABLE_NUM_DYNAMIC - 1):
124 text += " (mapi_func) noop_generic,\n"
125 text += " (mapi_func) noop_generic\n"
126
127 text += "};\n\n"
128 text += "#endif /* DEBUG */\n"
129 text += "#undef MAPI_TMP_NOOP_ARRAY\n"
130 text += "#endif /* MAPI_TMP_NOOP_ARRAY */\n"
131 return text
132
133 def generate_public_stubs(functions):
134 text = "#ifdef MAPI_TMP_PUBLIC_STUBS\n"
135
136 text += "static const struct mapi_stub public_stubs[] = {\n"
137 for func in functions:
138 text += " { \"%s\", %d, NULL },\n" % (func.name, func.slot)
139 text += "};\n"
140 text += "#undef MAPI_TMP_PUBLIC_STUBS\n"
141 text += "#endif /* MAPI_TMP_PUBLIC_STUBS */\n"
142 return text
143
144 def generate_public_entries(functions):
145 text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
146
147 for func in functions:
148 retStr = ("return " if func.hasReturn() else "")
149 text += r"""
150 GLAPI {f.rt} APIENTRY {f.name}({f.decArgs})
151 {{
152 const struct _glapi_table *_tbl = entry_current_get();
153 mapi_func _func = ((const mapi_func *) _tbl)[{f.slot}];
154 {retStr}(({f.rt} (APIENTRY *)({f.decArgs})) _func)({f.callArgs});
155 }}
156
157 """.lstrip("\n").format(f=func, retStr=retStr)
158
159 text += "\n"
160 text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"
161 return text
162
163 def generate_public_entries_table(functions):
164 text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
165 text += "static const mapi_func public_entries[] = {\n"
166 for func in functions:
167 text += " (mapi_func) %s,\n" % (func.name,)
168 text += "};\n"
169 text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"
170 return text
171
172 def generate_undef_public_entries():
173 text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
174 text += "#undef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"
175 text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"
176 return text
177
178 def generate_stub_asm_gcc(functions):
179 text = "#ifdef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n"
180 text += "__asm__(\n"
181
182 for func in functions:
183 text += 'STUB_ASM_ENTRY("%s")"\\n"\n' % (func.name,)
184 text += '"\\t"STUB_ASM_CODE("%d")"\\n"\n\n' % (func.slot,)
185
186 text += ");\n"
187 text += "#undef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n"
188 text += "#endif /* MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN */\n"
189 return text
190
191 if (__name__ == "__main__"):
192 _main()
193