Path: blob/21.2-virgl/src/mapi/new/gen_gldispatch_mapi.py
4560 views
#!/usr/bin/env python12# Copyright (C) 2010 LunarG Inc.3# (C) Copyright 2015, NVIDIA CORPORATION.4#5# Permission is hereby granted, free of charge, to any person obtaining a6# copy of this software and associated documentation files (the "Software"),7# to deal in the Software without restriction, including without limitation8# the rights to use, copy, modify, merge, publish, distribute, sublicense,9# and/or sell copies of the Software, and to permit persons to whom the10# Software is furnished to do so, subject to the following conditions:11#12# The above copyright notice and this permission notice shall be included13# in all copies or substantial portions of the Software.14#15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21# DEALINGS IN THE SOFTWARE.22#23# Authors:24# Kyle Brenneman <[email protected]>25#26# Based on code ogiginally by:27# Chia-I Wu <[email protected]>282930"""31Generates the glapi_mapi_tmp.h header file from Khronos's XML file.32"""3334import sys35import xml.etree.ElementTree as etree3637import genCommon3839def _main():40target = sys.argv[1]41xmlFiles = sys.argv[2:]4243roots = [ etree.parse(filename).getroot() for filename in xmlFiles ]44allFunctions = genCommon.getFunctionsFromRoots(roots)4546names = genCommon.getExportNamesFromRoots(target, roots)47functions = [f for f in allFunctions if(f.name in names)]4849if (target in ("gl", "gldispatch")):50assert(len(functions) == len(allFunctions))51assert(all(functions[i] == allFunctions[i] for i in range(len(functions))))52assert(all(functions[i].slot == i for i in range(len(functions))))5354print(r"""55/* This file is automatically generated by mapi_abi.py. Do not modify. */5657#ifndef _GLAPI_TMP_H_58#define _GLAPI_TMP_H_59typedef int GLclampx;60#endif /* _GLAPI_TMP_H_ */61""".lstrip("\n"))6263print(generate_defines(functions))64if target == "gldispatch":65print(generate_table(functions, allFunctions))66print(generate_noop_array(functions))67print(generate_public_stubs(functions))68print(generate_public_entries(functions))69if target == "gldispatch":70print(generate_public_entries_table(functions))71print(generate_undef_public_entries())72print(generate_stub_asm_gcc(functions))7374def generate_defines(functions):75text = r"""76#ifdef MAPI_TMP_DEFINES77#define GL_GLEXT_PROTOTYPES78#include "GL/gl.h"79#include "GL/glext.h"8081""".lstrip("\n")82for func in functions:83text += "GLAPI {f.rt} APIENTRY {f.name}({f.decArgs});\n".format(f=func)84text += "#undef MAPI_TMP_DEFINES\n"85text += "#endif /* MAPI_TMP_DEFINES */\n"86return text8788def generate_table(functions, allFunctions):89text = "#ifdef MAPI_TMP_TABLE\n"90text += "#define MAPI_TABLE_NUM_STATIC %d\n" % (len(allFunctions))91text += "#define MAPI_TABLE_NUM_DYNAMIC %d\n" % (genCommon.MAPI_TABLE_NUM_DYNAMIC,)92text += "#undef MAPI_TMP_TABLE\n"93text += "#endif /* MAPI_TMP_TABLE */\n"94return text9596def generate_noop_array(functions):97text = "#ifdef MAPI_TMP_NOOP_ARRAY\n"98text += "#ifdef DEBUG\n\n"99100for func in functions:101text += "static {f.rt} APIENTRY noop{f.basename}({f.decArgs})\n".format(f=func)102text += "{\n"103if (len(func.args) > 0):104text += " "105for arg in func.args:106text += " (void) {a.name};".format(a=arg)107text += "\n"108text += " noop_warn(\"{f.name}\");\n".format(f=func)109if (func.hasReturn()):110text += " return ({f.rt}) 0;\n".format(f=func)111text += "}\n\n"112113text += "const mapi_func table_noop_array[] = {\n"114for func in functions:115text += " (mapi_func) noop{f.basename},\n".format(f=func)116for i in range(genCommon.MAPI_TABLE_NUM_DYNAMIC - 1):117text += " (mapi_func) noop_generic,\n"118text += " (mapi_func) noop_generic\n"119text += "};\n\n"120text += "#else /* DEBUG */\n\n"121text += "const mapi_func table_noop_array[] = {\n"122for i in range(len(functions) + genCommon.MAPI_TABLE_NUM_DYNAMIC - 1):123text += " (mapi_func) noop_generic,\n"124text += " (mapi_func) noop_generic\n"125126text += "};\n\n"127text += "#endif /* DEBUG */\n"128text += "#undef MAPI_TMP_NOOP_ARRAY\n"129text += "#endif /* MAPI_TMP_NOOP_ARRAY */\n"130return text131132def generate_public_stubs(functions):133text = "#ifdef MAPI_TMP_PUBLIC_STUBS\n"134135text += "static const struct mapi_stub public_stubs[] = {\n"136for func in functions:137text += " { \"%s\", %d, NULL },\n" % (func.name, func.slot)138text += "};\n"139text += "#undef MAPI_TMP_PUBLIC_STUBS\n"140text += "#endif /* MAPI_TMP_PUBLIC_STUBS */\n"141return text142143def generate_public_entries(functions):144text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"145146for func in functions:147retStr = ("return " if func.hasReturn() else "")148text += r"""149GLAPI {f.rt} APIENTRY {f.name}({f.decArgs})150{{151const struct _glapi_table *_tbl = entry_current_get();152mapi_func _func = ((const mapi_func *) _tbl)[{f.slot}];153{retStr}(({f.rt} (APIENTRY *)({f.decArgs})) _func)({f.callArgs});154}}155156""".lstrip("\n").format(f=func, retStr=retStr)157158text += "\n"159text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"160return text161162def generate_public_entries_table(functions):163text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"164text += "static const mapi_func public_entries[] = {\n"165for func in functions:166text += " (mapi_func) %s,\n" % (func.name,)167text += "};\n"168text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"169return text170171def generate_undef_public_entries():172text = "#ifdef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"173text += "#undef MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN\n"174text += "#endif /* MAPI_TMP_PUBLIC_ENTRIES_NO_HIDDEN */\n"175return text176177def generate_stub_asm_gcc(functions):178text = "#ifdef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n"179text += "__asm__(\n"180181for func in functions:182text += 'STUB_ASM_ENTRY("%s")"\\n"\n' % (func.name,)183text += '"\\t"STUB_ASM_CODE("%d")"\\n"\n\n' % (func.slot,)184185text += ");\n"186text += "#undef MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN\n"187text += "#endif /* MAPI_TMP_STUB_ASM_GCC_NO_HIDDEN */\n"188return text189190if (__name__ == "__main__"):191_main()192193194195