1bf215546Sopenharmony_ci
2bf215546Sopenharmony_ci# (C) Copyright IBM Corporation 2004, 2005
3bf215546Sopenharmony_ci# All Rights Reserved.
4bf215546Sopenharmony_ci#
5bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci# on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci# license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci# the Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci#
12bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci# Software.
15bf215546Sopenharmony_ci#
16bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22bf215546Sopenharmony_ci# IN THE SOFTWARE.
23bf215546Sopenharmony_ci#
24bf215546Sopenharmony_ci# Authors:
25bf215546Sopenharmony_ci#    Ian Romanick <idr@us.ibm.com>
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ciimport argparse
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ciimport license
30bf215546Sopenharmony_ciimport gl_XML, glX_XML
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ciclass PrintGenericStubs(gl_XML.gl_print_base):
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci    def __init__(self):
35bf215546Sopenharmony_ci        gl_XML.gl_print_base.__init__(self)
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci        self.name = "gl_x86_asm.py (from Mesa)"
38bf215546Sopenharmony_ci        self.license = license.bsd_license_template % ( \
39bf215546Sopenharmony_ci"""Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
40bf215546Sopenharmony_ci(C) Copyright IBM Corporation 2004, 2005""", "BRIAN PAUL, IBM")
41bf215546Sopenharmony_ci        return
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci    def get_stack_size(self, f):
45bf215546Sopenharmony_ci        size = 0
46bf215546Sopenharmony_ci        for p in f.parameterIterator():
47bf215546Sopenharmony_ci            if p.is_padding:
48bf215546Sopenharmony_ci                continue
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci            size += p.get_stack_size()
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci        return size
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci    def printRealHeader(self):
56bf215546Sopenharmony_ci        print('#include "x86/assyntax.h"')
57bf215546Sopenharmony_ci        print('')
58bf215546Sopenharmony_ci        print('#if defined(STDCALL_API)')
59bf215546Sopenharmony_ci        print('#  define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n2))')
60bf215546Sopenharmony_ci        print('#else')
61bf215546Sopenharmony_ci        print('#  define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n))')
62bf215546Sopenharmony_ci        print('#endif')
63bf215546Sopenharmony_ci        print('')
64bf215546Sopenharmony_ci        print('#define GL_OFFSET(x) CODEPTR(REGOFF(4 * x, EAX))')
65bf215546Sopenharmony_ci        print('')
66bf215546Sopenharmony_ci        print('#if defined(GNU_ASSEMBLER) && !defined(__MINGW32__) && !defined(__APPLE__)')
67bf215546Sopenharmony_ci        print('#define GLOBL_FN(x) GLOBL x ; .type x, @function')
68bf215546Sopenharmony_ci        print('#else')
69bf215546Sopenharmony_ci        print('#define GLOBL_FN(x) GLOBL x')
70bf215546Sopenharmony_ci        print('#endif')
71bf215546Sopenharmony_ci        print('')
72bf215546Sopenharmony_ci        print('')
73bf215546Sopenharmony_ci        print('#ifdef REALLY_INITIAL_EXEC')
74bf215546Sopenharmony_ci        print('')
75bf215546Sopenharmony_ci        print('#ifdef GLX_X86_READONLY_TEXT')
76bf215546Sopenharmony_ci        print('# define CTX_INSNS MOV_L(GS:(EAX), EAX)')
77bf215546Sopenharmony_ci        print('#else')
78bf215546Sopenharmony_ci        print('# define CTX_INSNS NOP /* Pad for init_glapi_relocs() */')
79bf215546Sopenharmony_ci        print('#endif')
80bf215546Sopenharmony_ci        print('')
81bf215546Sopenharmony_ci        print('#  define GL_STUB(fn,off,fn_alt)\t\t\t\\')
82bf215546Sopenharmony_ci        print('ALIGNTEXT16;\t\t\t\t\t\t\\')
83bf215546Sopenharmony_ci        print('GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\')
84bf215546Sopenharmony_ci        print('GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\')
85bf215546Sopenharmony_ci        print('\tCALL(_x86_get_dispatch) ;\t\t\t\\')
86bf215546Sopenharmony_ci        print('\tCTX_INSNS ;					\\')
87bf215546Sopenharmony_ci        print('\tJMP(GL_OFFSET(off))')
88bf215546Sopenharmony_ci        print('')
89bf215546Sopenharmony_ci        print('#else')
90bf215546Sopenharmony_ci        print('#  define GL_STUB(fn,off,fn_alt)\t\t\t\\')
91bf215546Sopenharmony_ci        print('ALIGNTEXT16;\t\t\t\t\t\t\\')
92bf215546Sopenharmony_ci        print('GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\')
93bf215546Sopenharmony_ci        print('GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\')
94bf215546Sopenharmony_ci        print('\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\')
95bf215546Sopenharmony_ci        print('\tTEST_L(EAX, EAX) ;\t\t\t\t\\')
96bf215546Sopenharmony_ci        print('\tJE(1f) ;\t\t\t\t\t\\')
97bf215546Sopenharmony_ci        print('\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\')
98bf215546Sopenharmony_ci        print('1:\tCALL(_glapi_get_dispatch) ;\t\t\t\\')
99bf215546Sopenharmony_ci        print('\tJMP(GL_OFFSET(off))')
100bf215546Sopenharmony_ci        print('#endif')
101bf215546Sopenharmony_ci        print('')
102bf215546Sopenharmony_ci        print('#ifdef HAVE_FUNC_ATTRIBUTE_ALIAS')
103bf215546Sopenharmony_ci        print('#  define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\')
104bf215546Sopenharmony_ci        print('\t.globl\tGL_PREFIX(fn, fn_alt) ;\t\t\t\\')
105bf215546Sopenharmony_ci        print('\t.set\tGL_PREFIX(fn, fn_alt), GL_PREFIX(alias, alias_alt)')
106bf215546Sopenharmony_ci        print('#else')
107bf215546Sopenharmony_ci        print('#  define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\')
108bf215546Sopenharmony_ci        print('    GL_STUB(fn, off, fn_alt)')
109bf215546Sopenharmony_ci        print('#endif')
110bf215546Sopenharmony_ci        print('')
111bf215546Sopenharmony_ci        print('SEG_TEXT')
112bf215546Sopenharmony_ci        print('')
113bf215546Sopenharmony_ci        print('#ifdef REALLY_INITIAL_EXEC')
114bf215546Sopenharmony_ci        print('')
115bf215546Sopenharmony_ci        print('\tGLOBL\tGLNAME(_x86_get_dispatch)')
116bf215546Sopenharmony_ci        print('\tHIDDEN(GLNAME(_x86_get_dispatch))')
117bf215546Sopenharmony_ci        print('ALIGNTEXT16')
118bf215546Sopenharmony_ci        print('GLNAME(_x86_get_dispatch):')
119bf215546Sopenharmony_ci        print('\tcall	1f')
120bf215546Sopenharmony_ci        print('1:\tpopl	%eax')
121bf215546Sopenharmony_ci        print('\taddl	$_GLOBAL_OFFSET_TABLE_+[.-1b], %eax')
122bf215546Sopenharmony_ci        print('\tmovl	_glapi_tls_Dispatch@GOTNTPOFF(%eax), %eax')
123bf215546Sopenharmony_ci        print('\tret')
124bf215546Sopenharmony_ci        print('')
125bf215546Sopenharmony_ci        print('#else')
126bf215546Sopenharmony_ci        print('EXTERN GLNAME(_glapi_Dispatch)')
127bf215546Sopenharmony_ci        print('EXTERN GLNAME(_glapi_get_dispatch)')
128bf215546Sopenharmony_ci        print('#endif')
129bf215546Sopenharmony_ci        print('')
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci        print('#if !defined( GLX_X86_READONLY_TEXT )')
132bf215546Sopenharmony_ci        print('\t\t.section\twtext, "awx", @progbits')
133bf215546Sopenharmony_ci        print('#endif /* !defined( GLX_X86_READONLY_TEXT ) */')
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci        print('')
136bf215546Sopenharmony_ci        print('\t\tALIGNTEXT16')
137bf215546Sopenharmony_ci        print('\t\tGLOBL GLNAME(gl_dispatch_functions_start)')
138bf215546Sopenharmony_ci        print('\t\tHIDDEN(GLNAME(gl_dispatch_functions_start))')
139bf215546Sopenharmony_ci        print('GLNAME(gl_dispatch_functions_start):')
140bf215546Sopenharmony_ci        print('')
141bf215546Sopenharmony_ci        return
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci    def printRealFooter(self):
145bf215546Sopenharmony_ci        print('')
146bf215546Sopenharmony_ci        print('\t\tGLOBL\tGLNAME(gl_dispatch_functions_end)')
147bf215546Sopenharmony_ci        print('\t\tHIDDEN(GLNAME(gl_dispatch_functions_end))')
148bf215546Sopenharmony_ci        print('\t\tALIGNTEXT16')
149bf215546Sopenharmony_ci        print('GLNAME(gl_dispatch_functions_end):')
150bf215546Sopenharmony_ci        print('')
151bf215546Sopenharmony_ci        print('#if defined (__ELF__) && defined (__linux__)')
152bf215546Sopenharmony_ci        print('	.section .note.GNU-stack,"",%progbits')
153bf215546Sopenharmony_ci        print('#endif')
154bf215546Sopenharmony_ci        return
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_ci    def printBody(self, api):
158bf215546Sopenharmony_ci        for f in api.functionIterateByOffset():
159bf215546Sopenharmony_ci            name = f.dispatch_name()
160bf215546Sopenharmony_ci            stack = self.get_stack_size(f)
161bf215546Sopenharmony_ci            alt = "%s@%u" % (name, stack)
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci            print('\tGL_STUB(%s, %d, %s)' % (name, f.offset, alt))
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci            if not f.is_static_entry_point(f.name):
166bf215546Sopenharmony_ci                print('\tHIDDEN(GL_PREFIX(%s, %s))' % (name, alt))
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci        for f in api.functionIterateByOffset():
170bf215546Sopenharmony_ci            name = f.dispatch_name()
171bf215546Sopenharmony_ci            stack = self.get_stack_size(f)
172bf215546Sopenharmony_ci            alt = "%s@%u" % (name, stack)
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci            for n in f.entry_points:
175bf215546Sopenharmony_ci                if f.is_static_entry_point(n):
176bf215546Sopenharmony_ci                    if n != f.name:
177bf215546Sopenharmony_ci                        alt2 = "%s@%u" % (n, stack)
178bf215546Sopenharmony_ci                        text = '\tGL_STUB_ALIAS(%s, %d, %s, %s, %s)' % (n, f.offset, alt2, name, alt)
179bf215546Sopenharmony_ci
180bf215546Sopenharmony_ci                        if f.has_different_protocol(n):
181bf215546Sopenharmony_ci                            print('#ifndef GLX_INDIRECT_RENDERING')
182bf215546Sopenharmony_ci                            print(text)
183bf215546Sopenharmony_ci                            print('#endif')
184bf215546Sopenharmony_ci                        else:
185bf215546Sopenharmony_ci                            print(text)
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci        return
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_cidef _parser():
190bf215546Sopenharmony_ci    parser = argparse.ArgumentParser()
191bf215546Sopenharmony_ci    parser.add_argument('-f',
192bf215546Sopenharmony_ci                        dest='filename',
193bf215546Sopenharmony_ci                        default='gl_API.xml',
194bf215546Sopenharmony_ci                        help='An XML file describing an API.')
195bf215546Sopenharmony_ci    return parser.parse_args()
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_cidef main():
199bf215546Sopenharmony_ci    args = _parser()
200bf215546Sopenharmony_ci    printer = PrintGenericStubs()
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci    api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
203bf215546Sopenharmony_ci    printer.Print(api)
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci
206bf215546Sopenharmony_ciif __name__ == '__main__':
207bf215546Sopenharmony_ci    main()
208