1 2# (C) Copyright IBM Corporation 2004, 2005 3# (C) Copyright Apple Inc. 2011 4# Copyright (C) 2015 Intel Corporation 5# All Rights Reserved. 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# on the rights to use, copy, modify, merge, publish, distribute, sub 11# license, and/or sell copies of the Software, and to permit persons to whom 12# the Software is furnished to do so, subject to the following conditions: 13# 14# The above copyright notice and this permission notice (including the next 15# paragraph) shall be included in all copies or substantial portions of the 16# Software. 17# 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 21# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 24# IN THE SOFTWARE. 25# 26# Authors: 27# Jeremy Huddleston <jeremyhu@apple.com> 28# 29# Based on code ogiginally by: 30# Ian Romanick <idr@us.ibm.com> 31 32import argparse 33 34import license 35import gl_XML, glX_XML 36 37header = """/* GLXEXT is the define used in the xserver when the GLX extension is being 38 * built. Hijack this to determine whether this file is being built for the 39 * server or the client. 40 */ 41#ifdef HAVE_DIX_CONFIG_H 42#include <dix-config.h> 43#endif 44 45#if (defined(GLXEXT) && defined(HAVE_BACKTRACE)) \\ 46 || (!defined(GLXEXT) && defined(DEBUG) && !defined(__CYGWIN__) && !defined(__MINGW32__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)) 47#define USE_BACKTRACE 48#endif 49 50#ifdef USE_BACKTRACE 51#include <execinfo.h> 52#endif 53 54#ifndef _WIN32 55#include <dlfcn.h> 56#endif 57#include <stdlib.h> 58#include <stdio.h> 59#include <string.h> 60 61#include "main/glheader.h" 62 63#include "glapi.h" 64#include "glapitable.h" 65 66#ifdef GLXEXT 67#include "os.h" 68#endif 69 70static void 71__glapi_gentable_NoOp(void) { 72 const char *fstr = "Unknown"; 73 74 /* Silence potential GCC warning for some #ifdef paths. 75 */ 76 (void) fstr; 77#if defined(USE_BACKTRACE) 78#if !defined(GLXEXT) 79 if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG")) 80#endif 81 { 82 void *frames[2]; 83 84 if(backtrace(frames, 2) == 2) { 85 Dl_info info; 86 dladdr(frames[1], &info); 87 if(info.dli_sname) 88 fstr = info.dli_sname; 89 } 90 91#if !defined(GLXEXT) 92 fprintf(stderr, "Call to unimplemented API: %s\\n", fstr); 93#endif 94 } 95#endif 96#if defined(GLXEXT) 97 LogMessage(X_ERROR, "GLX: Call to unimplemented API: %s\\n", fstr); 98#endif 99} 100 101static void 102__glapi_gentable_set_remaining_noop(struct _glapi_table *disp) { 103 GLuint entries = _glapi_get_dispatch_table_size(); 104 void **dispatch = (void **) disp; 105 unsigned i; 106 107 /* ISO C is annoying sometimes */ 108 union {_glapi_proc p; void *v;} p; 109 p.p = __glapi_gentable_NoOp; 110 111 for(i=0; i < entries; i++) 112 if(dispatch[i] == NULL) 113 dispatch[i] = p.v; 114} 115 116""" 117 118footer = """ 119struct _glapi_table * 120_glapi_create_table_from_handle(void *handle, const char *symbol_prefix) { 121 struct _glapi_table *disp = calloc(_glapi_get_dispatch_table_size(), sizeof(_glapi_proc)); 122 char symboln[512]; 123 124 if(!disp) 125 return NULL; 126 127 if(symbol_prefix == NULL) 128 symbol_prefix = ""; 129 130 /* Note: This code relies on _glapi_table_func_names being sorted by the 131 * entry point index of each function. 132 */ 133 for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) { 134 const char *name = _glapi_table_func_names[func_index]; 135 void ** procp = &((void **)disp)[func_index]; 136 137 snprintf(symboln, sizeof(symboln), \"%s%s\", symbol_prefix, name); 138#ifdef _WIN32 139 *procp = GetProcAddress(handle, symboln); 140#else 141 *procp = dlsym(handle, symboln); 142#endif 143 } 144 __glapi_gentable_set_remaining_noop(disp); 145 146 return disp; 147} 148 149void 150 _glapi_table_patch(struct _glapi_table *table, const char *name, void *wrapper) 151{ 152 for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) { 153 if (!strcmp(_glapi_table_func_names[func_index], name)) { 154 ((void **)table)[func_index] = wrapper; 155 return; 156 } 157 } 158 fprintf(stderr, "could not patch %s in dispatch table\\n", name); 159} 160 161""" 162 163 164class PrintCode(gl_XML.gl_print_base): 165 166 def __init__(self): 167 gl_XML.gl_print_base.__init__(self) 168 169 self.name = "gl_gentable.py (from Mesa)" 170 self.license = license.bsd_license_template % ( \ 171"""Copyright (C) 1999-2001 Brian Paul All Rights Reserved. 172(C) Copyright IBM Corporation 2004, 2005 173(C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM") 174 175 return 176 177 178 def get_stack_size(self, f): 179 size = 0 180 for p in f.parameterIterator(): 181 if p.is_padding: 182 continue 183 184 size += p.get_stack_size() 185 186 return size 187 188 189 def printRealHeader(self): 190 print header 191 return 192 193 194 def printRealFooter(self): 195 print footer 196 return 197 198 199 def printBody(self, api): 200 201 # Determine how many functions have a defined offset. 202 func_count = 0 203 for f in api.functions_by_name.itervalues(): 204 if f.offset != -1: 205 func_count += 1 206 207 # Build the mapping from offset to function name. 208 funcnames = [None] * func_count 209 for f in api.functions_by_name.itervalues(): 210 if f.offset != -1: 211 if not (funcnames[f.offset] is None): 212 raise Exception("Function table has more than one function with same offset (offset %d, func %s)" % (f.offset, f.name)) 213 funcnames[f.offset] = f.name 214 215 # Check that the table has no gaps. We expect a function at every offset, 216 # and the code which generates the table relies on this. 217 for i in xrange(0, func_count): 218 if funcnames[i] is None: 219 raise Exception("Function table has no function at offset %d" % (i)) 220 221 print "#define GLAPI_TABLE_COUNT %d" % func_count 222 print "static const char * const _glapi_table_func_names[GLAPI_TABLE_COUNT] = {" 223 for i in xrange(0, func_count): 224 print " /* %5d */ \"%s\"," % (i, funcnames[i]) 225 print "};" 226 227 return 228 229 230def _parser(): 231 """Parse arguments and return a namespace object.""" 232 parser = argparse.ArgumentParser() 233 parser.add_argument('-f', 234 dest='filename', 235 default='gl_API.xml', 236 help='An XML file description of an API') 237 238 return parser.parse_args() 239 240 241def main(): 242 """Main function.""" 243 args = _parser() 244 245 printer = PrintCode() 246 247 api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory()) 248 printer.Print(api) 249 250 251if __name__ == '__main__': 252 main() 253