• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
32from __future__ import print_function
33
34import argparse
35
36import license
37import gl_XML, glX_XML
38
39header = """/* GLXEXT is the define used in the xserver when the GLX extension is being
40 * built.  Hijack this to determine whether this file is being built for the
41 * server or the client.
42 */
43#ifdef HAVE_DIX_CONFIG_H
44#include <dix-config.h>
45#endif
46
47#if (defined(GLXEXT) && defined(HAVE_BACKTRACE)) \\
48	|| (!defined(GLXEXT) && defined(DEBUG) && defined(HAVE_EXECINFO_H))
49#define USE_BACKTRACE
50#endif
51
52#ifdef USE_BACKTRACE
53#include <execinfo.h>
54#endif
55
56#ifndef _WIN32
57#include <dlfcn.h>
58#endif
59#include <stdlib.h>
60#include <stdio.h>
61#include <string.h>
62
63#include "main/glheader.h"
64
65#include "glapi.h"
66#include "glapitable.h"
67
68#ifdef GLXEXT
69#include "os.h"
70#endif
71
72static void
73__glapi_gentable_NoOp(void) {
74    const char *fstr = "Unknown";
75
76    /* Silence potential GCC warning for some #ifdef paths.
77     */
78    (void) fstr;
79#if defined(USE_BACKTRACE)
80#if !defined(GLXEXT)
81    if (getenv("MESA_DEBUG") || getenv("LIBGL_DEBUG"))
82#endif
83    {
84        void *frames[2];
85
86        if(backtrace(frames, 2) == 2) {
87            Dl_info info;
88            dladdr(frames[1], &info);
89            if(info.dli_sname)
90                fstr = info.dli_sname;
91        }
92
93#if !defined(GLXEXT)
94        fprintf(stderr, "Call to unimplemented API: %s\\n", fstr);
95#endif
96    }
97#endif
98#if defined(GLXEXT)
99    LogMessage(X_ERROR, "GLX: Call to unimplemented API: %s\\n", fstr);
100#endif
101}
102
103static void
104__glapi_gentable_set_remaining_noop(struct _glapi_table *disp) {
105    GLuint entries = _glapi_get_dispatch_table_size();
106    void **dispatch = (void **) disp;
107    unsigned i;
108
109    /* ISO C is annoying sometimes */
110    union {_glapi_proc p; void *v;} p;
111    p.p = __glapi_gentable_NoOp;
112
113    for(i=0; i < entries; i++)
114        if(dispatch[i] == NULL)
115            dispatch[i] = p.v;
116}
117
118"""
119
120footer = """
121struct _glapi_table *
122_glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
123    struct _glapi_table *disp = calloc(_glapi_get_dispatch_table_size(), sizeof(_glapi_proc));
124    char symboln[512];
125
126    if(!disp)
127        return NULL;
128
129    if(symbol_prefix == NULL)
130        symbol_prefix = "";
131
132    /* Note: This code relies on _glapi_table_func_names being sorted by the
133     * entry point index of each function.
134     */
135    for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) {
136        const char *name = _glapi_table_func_names[func_index];
137        void ** procp = &((void **)disp)[func_index];
138
139        snprintf(symboln, sizeof(symboln), \"%s%s\", symbol_prefix, name);
140#ifdef _WIN32
141        *procp = GetProcAddress(handle, symboln);
142#else
143        *procp = dlsym(handle, symboln);
144#endif
145    }
146    __glapi_gentable_set_remaining_noop(disp);
147
148    return disp;
149}
150
151void
152 _glapi_table_patch(struct _glapi_table *table, const char *name, void *wrapper)
153{
154   for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) {
155      if (!strcmp(_glapi_table_func_names[func_index], name)) {
156            ((void **)table)[func_index] = wrapper;
157            return;
158         }
159   }
160   fprintf(stderr, "could not patch %s in dispatch table\\n", name);
161}
162
163"""
164
165
166class PrintCode(gl_XML.gl_print_base):
167
168    def __init__(self):
169        gl_XML.gl_print_base.__init__(self)
170
171        self.name = "gl_gentable.py (from Mesa)"
172        self.license = license.bsd_license_template % ( \
173"""Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
174(C) Copyright IBM Corporation 2004, 2005
175(C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
176
177        return
178
179
180    def get_stack_size(self, f):
181        size = 0
182        for p in f.parameterIterator():
183            if p.is_padding:
184                continue
185
186            size += p.get_stack_size()
187
188        return size
189
190
191    def printRealHeader(self):
192        print(header)
193        return
194
195
196    def printRealFooter(self):
197        print(footer)
198        return
199
200
201    def printBody(self, api):
202
203        # Determine how many functions have a defined offset.
204        func_count = 0
205        for f in api.functions_by_name.values():
206            if f.offset != -1:
207                func_count += 1
208
209        # Build the mapping from offset to function name.
210        funcnames = [None] * func_count
211        for f in api.functions_by_name.values():
212            if f.offset != -1:
213                if not (funcnames[f.offset] is None):
214                    raise Exception("Function table has more than one function with same offset (offset %d, func %s)" % (f.offset, f.name))
215                funcnames[f.offset] = f.name
216
217        # Check that the table has no gaps.  We expect a function at every offset,
218        # and the code which generates the table relies on this.
219        for i in range(0, func_count):
220            if funcnames[i] is None:
221                raise Exception("Function table has no function at offset %d" % (i))
222
223        print("#define GLAPI_TABLE_COUNT %d" % func_count)
224        print("static const char * const _glapi_table_func_names[GLAPI_TABLE_COUNT] = {")
225        for i in range(0, func_count):
226            print("    /* %5d */ \"%s\"," % (i, funcnames[i]))
227        print("};")
228
229        return
230
231
232def _parser():
233    """Parse arguments and return a namespace object."""
234    parser = argparse.ArgumentParser()
235    parser.add_argument('-f',
236                        dest='filename',
237                        default='gl_API.xml',
238                        help='An XML file description of an API')
239
240    return parser.parse_args()
241
242
243def main():
244    """Main function."""
245    args = _parser()
246
247    printer = PrintCode()
248
249    api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
250    printer.Print(api)
251
252
253if __name__ == '__main__':
254    main()
255