1#!/usr/bin/env python3 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 <[email protected]> 26# 27# Based on code ogiginally by: 28# Chia-I Wu <[email protected]> 29 30 31""" 32Generates the glapi_mapi_tmp.h header file from Khronos's XML file. 33""" 34 35import sys 36import xml.etree.ElementTree as etree 37 38import genCommon 39 40def _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#endif /* _GLAPI_TMP_H_ */ 61""".lstrip("\n")) 62 63 print(generate_defines(functions)) 64 if target == "gldispatch": 65 print(generate_table(functions, allFunctions)) 66 print(generate_noop_array(functions)) 67 print(generate_public_stubs(functions)) 68 print(generate_public_entries(functions)) 69 if target == "gldispatch": 70 print(generate_public_entries_table(functions)) 71 print(generate_undef_public_entries()) 72 print(generate_stub_asm_gcc(functions)) 73 74def generate_defines(functions): 75 text = r""" 76#ifdef MAPI_TMP_DEFINES 77#include "util/glheader.h" 78 79""".lstrip("\n") 80 for func in functions: 81 text += "GLAPI {f.rt} GLAPIENTRY {f.name}({f.decArgs});\n".format(f=func) 82 text += "#undef MAPI_TMP_DEFINES\n" 83 text += "#endif /* MAPI_TMP_DEFINES */\n" 84 return text 85 86def generate_table(functions, allFunctions): 87 text = "#ifdef MAPI_TMP_TABLE\n" 88 text += "#define MAPI_TABLE_NUM_STATIC %d\n" % (len(allFunctions)) 89 text += "#define MAPI_TABLE_NUM_DYNAMIC %d\n" % (genCommon.MAPI_TABLE_NUM_DYNAMIC,) 90 text += "#undef MAPI_TMP_TABLE\n" 91 text += "#endif /* MAPI_TMP_TABLE */\n" 92 return text 93 94def generate_noop_array(functions): 95 text = "#ifdef MAPI_TMP_NOOP_ARRAY\n" 96 text += "#if MESA_DEBUG\n\n" 97 98 for func in functions: 99 text += "static {f.rt} GLAPIENTRY noop{f.basename}({f.decArgs})\n".format(f=func) 100 text += "{\n" 101 if (len(func.args) > 0): 102 text += " " 103 for arg in func.args: 104 text += " (void) {a.name};".format(a=arg) 105 text += "\n" 106 text += " noop_warn(\"{f.name}\");\n".format(f=func) 107 if (func.hasReturn()): 108 text += " return ({f.rt}) 0;\n".format(f=func) 109 text += "}\n\n" 110 111 text += "const mapi_func table_noop_array[] = {\n" 112 for func in functions: 113 text += " (mapi_func) noop{f.basename},\n".format(f=func) 114 for i in range(genCommon.MAPI_TABLE_NUM_DYNAMIC - 1): 115 text += " (mapi_func) noop_generic,\n" 116 text += " (mapi_func) noop_generic\n" 117 text += "};\n\n" 118 text += "#else /* !MESA_DEBUG */\n\n" 119 text += "const mapi_func table_noop_array[] = {\n" 120 for i in range(len(functions) + genCommon.MAPI_TABLE_NUM_DYNAMIC - 1): 121 text += " (mapi_func) noop_generic,\n" 122 text += " (mapi_func) noop_generic\n" 123 124 text += "};\n\n" 125 text += "#endif /* MESA_DEBUG */\n" 126 text += "#undef MAPI_TMP_NOOP_ARRAY\n" 127 text += "#endif /* MAPI_TMP_NOOP_ARRAY */\n" 128 return text 129 130def generate_public_stubs(functions): 131 text = "#ifdef MAPI_TMP_PUBLIC_STUBS\n" 132 133 text += "static const struct mapi_stub public_stubs[] = {\n" 134 for func in functions: 135 text += " { \"%s\", %d, NULL },\n" % (func.name, func.slot) 136 text += "};\n" 137 text += "#undef MAPI_TMP_PUBLIC_STUBS\n" 138 text += "#endif /* MAPI_TMP_PUBLIC_STUBS */\n" 139 return text 140 141def generate_public_entries(functions): 142 text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n" 143 144 for func in functions: 145 retStr = ("return " if func.hasReturn() else "") 146 text += r""" 147GLAPI {f.rt} GLAPIENTRY {f.name}({f.decArgs}) 148{{ 149 const struct _glapi_table *_tbl = entry_current_get(); 150 mapi_func _func = ((const mapi_func *) _tbl)[{f.slot}]; 151 {retStr}(({f.rt} (GLAPIENTRY *)({f.decArgs})) _func)({f.callArgs}); 152}} 153 154""".lstrip("\n").format(f=func, retStr=retStr) 155 156 text += "\n" 157 text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n" 158 return text 159 160def generate_public_entries_table(functions): 161 text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n" 162 text += "static const mapi_func public_entries[] = {\n" 163 for func in functions: 164 text += " (mapi_func) %s,\n" % (func.name,) 165 text += "};\n" 166 text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n" 167 return text 168 169def generate_undef_public_entries(): 170 text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n" 171 text += "#undef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n" 172 text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n" 173 return text 174 175def generate_stub_asm_gcc(functions): 176 text = "#ifdef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n" 177 text += "__asm__(\n" 178 179 for func in functions: 180 text += 'STUB_ASM_ENTRY("%s")"\\n"\n' % (func.name,) 181 text += '"\\t"STUB_ASM_CODE("%d")"\\n"\n\n' % (func.slot,) 182 183 text += ");\n" 184 text += "#undef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n" 185 text += "#endif /* MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN */\n" 186 return text 187 188if (__name__ == "__main__"): 189 _main() 190 191