xref: /aosp_15_r20/external/mesa3d/src/mapi/glapi/gen/gl_gentable.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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 <[email protected]>
28#
29# Based on code ogiginally by:
30#    Ian Romanick <[email protected]>
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#ifndef _WIN32
46#include <dlfcn.h>
47#endif
48#include <stdlib.h>
49#include <stdio.h>
50#include <string.h>
51
52#include "glapi.h"
53#include "glapitable.h"
54
55#ifdef GLXEXT
56#include "os.h"
57#endif
58
59static void
60__glapi_gentable_NoOp(void) {
61#if defined(GLXEXT)
62    LogMessage(X_ERROR, "GLX: Call to unimplemented API: Unknown\\n");
63#else
64    fprintf(stderr, "Call to unimplemented API: Unknown\\n");
65#endif
66}
67
68static void
69__glapi_gentable_set_remaining_noop(struct _glapi_table *disp) {
70    GLuint entries = _glapi_get_dispatch_table_size();
71    void **dispatch = (void **) disp;
72    unsigned i;
73
74    /* ISO C is annoying sometimes */
75    union {_glapi_proc p; void *v;} p;
76    p.p = __glapi_gentable_NoOp;
77
78    for(i=0; i < entries; i++)
79        if(dispatch[i] == NULL)
80            dispatch[i] = p.v;
81}
82
83"""
84
85footer = """
86#if defined(GLX_USE_APPLEGL) || defined(GLX_USE_WINDOWSGL)
87struct _glapi_table *
88_glapi_create_table_from_handle(void *handle, const char *symbol_prefix) {
89    struct _glapi_table *disp = calloc(_glapi_get_dispatch_table_size(), sizeof(_glapi_proc));
90    char symboln[512];
91
92    if(!disp)
93        return NULL;
94
95    if(symbol_prefix == NULL)
96        symbol_prefix = "";
97
98    /* Note: This code relies on _glapi_table_func_names being sorted by the
99     * entry point index of each function.
100     */
101    for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) {
102        const char *name = _glapi_table_func_names[func_index];
103        void ** procp = &((void **)disp)[func_index];
104
105        snprintf(symboln, sizeof(symboln), \"%s%s\", symbol_prefix, name);
106#ifdef _WIN32
107        *procp = GetProcAddress(handle, symboln);
108#else
109        *procp = dlsym(handle, symboln);
110#endif
111    }
112    __glapi_gentable_set_remaining_noop(disp);
113
114    return disp;
115}
116
117void
118 _glapi_table_patch(struct _glapi_table *table, const char *name, void *wrapper)
119{
120   for (int func_index = 0; func_index < GLAPI_TABLE_COUNT; ++func_index) {
121      if (!strcmp(_glapi_table_func_names[func_index], name)) {
122            ((void **)table)[func_index] = wrapper;
123            return;
124         }
125   }
126   fprintf(stderr, "could not patch %s in dispatch table\\n", name);
127}
128#endif
129"""
130
131
132class PrintCode(gl_XML.gl_print_base):
133
134    def __init__(self):
135        gl_XML.gl_print_base.__init__(self)
136
137        self.name = "gl_gentable.py (from Mesa)"
138        self.license = license.bsd_license_template % ( \
139"""Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
140(C) Copyright IBM Corporation 2004, 2005
141(C) Copyright Apple Inc 2011""", "BRIAN PAUL, IBM")
142
143        return
144
145
146    def get_stack_size(self, f):
147        size = 0
148        for p in f.parameterIterator():
149            if p.is_padding:
150                continue
151
152            size += p.get_stack_size()
153
154        return size
155
156
157    def printRealHeader(self):
158        print(header)
159        return
160
161
162    def printRealFooter(self):
163        print(footer)
164        return
165
166
167    def printBody(self, api):
168
169        # Determine how many functions have a defined offset.
170        func_count = 0
171        for f in api.functions_by_name.values():
172            if f.offset != -1:
173                func_count += 1
174
175        # Build the mapping from offset to function name.
176        funcnames = [None] * func_count
177        for f in api.functions_by_name.values():
178            if f.offset != -1:
179                if not (funcnames[f.offset] is None):
180                    raise Exception("Function table has more than one function with same offset (offset %d, func %s)" % (f.offset, f.name))
181                funcnames[f.offset] = f.name
182
183        # Check that the table has no gaps.  We expect a function at every offset,
184        # and the code which generates the table relies on this.
185        for i in range(0, func_count):
186            if funcnames[i] is None:
187                raise Exception("Function table has no function at offset %d" % (i))
188
189        print("#define GLAPI_TABLE_COUNT %d" % func_count)
190        print("static const char * const _glapi_table_func_names[GLAPI_TABLE_COUNT] = {")
191        for i in range(0, func_count):
192            print("    /* %5d */ \"%s\"," % (i, funcnames[i]))
193        print("};")
194
195        return
196
197
198def _parser():
199    """Parse arguments and return a namespace object."""
200    parser = argparse.ArgumentParser()
201    parser.add_argument('-f',
202                        dest='filename',
203                        default='gl_API.xml',
204                        help='An XML file description of an API')
205
206    return parser.parse_args()
207
208
209def main():
210    """Main function."""
211    args = _parser()
212
213    printer = PrintCode()
214
215    api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
216    printer.Print(api)
217
218
219if __name__ == '__main__':
220    main()
221