1
2# (C) Copyright IBM Corporation 2004
3# All Rights Reserved.
4#
5# Permission is hereby granted, free of charge, to any person obtaining a
6# copy of this software and associated documentation files (the "Software"),
7# to deal in the Software without restriction, including without limitation
8# on the rights to use, copy, modify, merge, publish, distribute, sub
9# license, and/or sell copies of the Software, and to permit persons to whom
10# the Software is furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice (including the next
13# paragraph) shall be included in all copies or substantial portions of the
14# Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22# IN THE SOFTWARE.
23#
24# Authors:
25#    Ian Romanick <idr@us.ibm.com>
26
27from __future__ import print_function
28
29import argparse
30
31import license
32import gl_XML, glX_XML
33
34class PrintGenericStubs(gl_XML.gl_print_base):
35    def __init__(self):
36        gl_XML.gl_print_base.__init__(self)
37        self.name = "gl_SPARC_asm.py (from Mesa)"
38        self.license = license.bsd_license_template % ( \
39"""Copyright (C) 1999-2003  Brian Paul   All Rights Reserved.
40(C) Copyright IBM Corporation 2004""", "BRIAN PAUL, IBM")
41
42
43    def printRealHeader(self):
44        print('#ifdef __arch64__')
45        print('#define GL_OFF(N)\t((N) * 8)')
46        print('#define GL_LL\t\tldx')
47        print('#define GL_TIE_LD(SYM)\t%tie_ldx(SYM)')
48        print('#define GL_STACK_SIZE\t128')
49        print('#else')
50        print('#define GL_OFF(N)\t((N) * 4)')
51        print('#define GL_LL\t\tld')
52        print('#define GL_TIE_LD(SYM)\t%tie_ld(SYM)')
53        print('#define GL_STACK_SIZE\t64')
54        print('#endif')
55        print('')
56        print('#define GLOBL_FN(x) .globl x ; .type x, @function')
57        print('#define HIDDEN(x) .hidden x')
58        print('')
59        print('\t.register %g2, #scratch')
60        print('\t.register %g3, #scratch')
61        print('')
62        print('\t.text')
63        print('')
64        print('\tGLOBL_FN(__glapi_sparc_icache_flush)')
65        print('\tHIDDEN(__glapi_sparc_icache_flush)')
66        print('\t.type\t__glapi_sparc_icache_flush, @function')
67        print('__glapi_sparc_icache_flush: /* %o0 = insn_addr */')
68        print('\tflush\t%o0')
69        print('\tretl')
70        print('\t nop')
71        print('')
72        print('\t.align\t32')
73        print('')
74        print('\t.type\t__glapi_sparc_get_pc, @function')
75        print('__glapi_sparc_get_pc:')
76        print('\tretl')
77        print('\t add\t%o7, %g2, %g2')
78        print('\t.size\t__glapi_sparc_get_pc, .-__glapi_sparc_get_pc')
79        print('')
80        print('#ifdef USE_ELF_TLS')
81        print('')
82        print('\tGLOBL_FN(__glapi_sparc_get_dispatch)')
83        print('\tHIDDEN(__glapi_sparc_get_dispatch)')
84        print('__glapi_sparc_get_dispatch:')
85        print('\tmov\t%o7, %g1')
86        print('\tsethi\t%hi(_GLOBAL_OFFSET_TABLE_-4), %g2')
87        print('\tcall\t__glapi_sparc_get_pc')
88        print('\tadd\t%g2, %lo(_GLOBAL_OFFSET_TABLE_+4), %g2')
89        print('\tmov\t%g1, %o7')
90        print('\tsethi\t%tie_hi22(_glapi_tls_Dispatch), %g1')
91        print('\tadd\t%g1, %tie_lo10(_glapi_tls_Dispatch), %g1')
92        print('\tGL_LL\t[%g2 + %g1], %g2, GL_TIE_LD(_glapi_tls_Dispatch)')
93        print('\tretl')
94        print('\t mov\t%g2, %o0')
95        print('')
96        print('\t.data')
97        print('\t.align\t32')
98        print('')
99        print('\t/* --> sethi %hi(_glapi_tls_Dispatch), %g1 */')
100        print('\t/* --> or %g1, %lo(_glapi_tls_Dispatch), %g1 */')
101        print('\tGLOBL_FN(__glapi_sparc_tls_stub)')
102        print('\tHIDDEN(__glapi_sparc_tls_stub)')
103        print('__glapi_sparc_tls_stub: /* Call offset in %g3 */')
104        print('\tmov\t%o7, %g1')
105        print('\tsethi\t%hi(_GLOBAL_OFFSET_TABLE_-4), %g2')
106        print('\tcall\t__glapi_sparc_get_pc')
107        print('\tadd\t%g2, %lo(_GLOBAL_OFFSET_TABLE_+4), %g2')
108        print('\tmov\t%g1, %o7')
109        print('\tsrl\t%g3, 10, %g3')
110        print('\tsethi\t%tie_hi22(_glapi_tls_Dispatch), %g1')
111        print('\tadd\t%g1, %tie_lo10(_glapi_tls_Dispatch), %g1')
112        print('\tGL_LL\t[%g2 + %g1], %g2, GL_TIE_LD(_glapi_tls_Dispatch)')
113        print('\tGL_LL\t[%g7+%g2], %g1')
114        print('\tGL_LL\t[%g1 + %g3], %g1')
115        print('\tjmp\t%g1')
116        print('\t nop')
117        print('\t.size\t__glapi_sparc_tls_stub, .-__glapi_sparc_tls_stub')
118        print('')
119        print('#define GL_STUB(fn, off)\t\t\t\t\\')
120        print('\tGLOBL_FN(fn);\t\t\t\t\t\\')
121        print('fn:\tba\t__glapi_sparc_tls_stub;\t\t\t\\')
122        print('\t sethi\tGL_OFF(off), %g3;\t\t\t\\')
123        print('\t.size\tfn,.-fn;')
124        print('')
125        print('#elif defined(HAVE_PTHREAD)')
126        print('')
127        print('\t/* 64-bit 0x00 --> sethi %hh(_glapi_Dispatch), %g1 */')
128        print('\t/* 64-bit 0x04 --> sethi %lm(_glapi_Dispatch), %g2 */')
129        print('\t/* 64-bit 0x08 --> or %g1, %hm(_glapi_Dispatch), %g1 */')
130        print('\t/* 64-bit 0x0c --> sllx %g1, 32, %g1 */')
131        print('\t/* 64-bit 0x10 --> add %g1, %g2, %g1 */')
132        print('\t/* 64-bit 0x14 --> ldx [%g1 + %lo(_glapi_Dispatch)], %g1 */')
133        print('')
134        print('\t/* 32-bit 0x00 --> sethi %hi(_glapi_Dispatch), %g1 */')
135        print('\t/* 32-bit 0x04 --> ld [%g1 + %lo(_glapi_Dispatch)], %g1 */')
136        print('')
137        print('\t.data')
138        print('\t.align\t32')
139        print('')
140        print('\tGLOBL_FN(__glapi_sparc_pthread_stub)')
141        print('\tHIDDEN(__glapi_sparc_pthread_stub)')
142        print('__glapi_sparc_pthread_stub: /* Call offset in %g3 */')
143        print('\tmov\t%o7, %g1')
144        print('\tsethi\t%hi(_GLOBAL_OFFSET_TABLE_-4), %g2')
145        print('\tcall\t__glapi_sparc_get_pc')
146        print('\t add\t%g2, %lo(_GLOBAL_OFFSET_TABLE_+4), %g2')
147        print('\tmov\t%g1, %o7')
148        print('\tsethi\t%hi(_glapi_Dispatch), %g1')
149        print('\tor\t%g1, %lo(_glapi_Dispatch), %g1')
150        print('\tsrl\t%g3, 10, %g3')
151        print('\tGL_LL\t[%g2+%g1], %g2')
152        print('\tGL_LL\t[%g2], %g1')
153        print('\tcmp\t%g1, 0')
154        print('\tbe\t2f')
155        print('\t nop')
156        print('1:\tGL_LL\t[%g1 + %g3], %g1')
157        print('\tjmp\t%g1')
158        print('\t nop')
159        print('2:\tsave\t%sp, GL_STACK_SIZE, %sp')
160        print('\tmov\t%g3, %l0')
161        print('\tcall\t_glapi_get_dispatch')
162        print('\t nop')
163        print('\tmov\t%o0, %g1')
164        print('\tmov\t%l0, %g3')
165        print('\tba\t1b')
166        print('\t restore %g0, %g0, %g0')
167        print('\t.size\t__glapi_sparc_pthread_stub, .-__glapi_sparc_pthread_stub')
168        print('')
169        print('#define GL_STUB(fn, off)\t\t\t\\')
170        print('\tGLOBL_FN(fn);\t\t\t\t\\')
171        print('fn:\tba\t__glapi_sparc_pthread_stub;\t\\')
172        print('\t sethi\tGL_OFF(off), %g3;\t\t\\')
173        print('\t.size\tfn,.-fn;')
174        print('')
175        print('#else /* Non-threaded version. */')
176        print('')
177        print('\t.type	__glapi_sparc_nothread_stub, @function')
178        print('__glapi_sparc_nothread_stub: /* Call offset in %g3 */')
179        print('\tmov\t%o7, %g1')
180        print('\tsethi\t%hi(_GLOBAL_OFFSET_TABLE_-4), %g2')
181        print('\tcall\t__glapi_sparc_get_pc')
182        print('\t add\t%g2, %lo(_GLOBAL_OFFSET_TABLE_+4), %g2')
183        print('\tmov\t%g1, %o7')
184        print('\tsrl\t%g3, 10, %g3')
185        print('\tsethi\t%hi(_glapi_Dispatch), %g1')
186        print('\tor\t%g1, %lo(_glapi_Dispatch), %g1')
187        print('\tGL_LL\t[%g2+%g1], %g2')
188        print('\tGL_LL\t[%g2], %g1')
189        print('\tGL_LL\t[%g1 + %g3], %g1')
190        print('\tjmp\t%g1')
191        print('\t nop')
192        print('\t.size\t__glapi_sparc_nothread_stub, .-__glapi_sparc_nothread_stub')
193        print('')
194        print('#define GL_STUB(fn, off)\t\t\t\\')
195        print('\tGLOBL_FN(fn);\t\t\t\t\\')
196        print('fn:\tba\t__glapi_sparc_nothread_stub;\t\\')
197        print('\t sethi\tGL_OFF(off), %g3;\t\t\\')
198        print('\t.size\tfn,.-fn;')
199        print('')
200        print('#endif')
201        print('')
202        print('#define GL_STUB_ALIAS(fn, alias)		\\')
203        print('	.globl	fn;				\\')
204        print('	.set	fn, alias')
205        print('')
206        print('\t.text')
207        print('\t.align\t32')
208        print('')
209        print('\t.globl\tgl_dispatch_functions_start')
210        print('\tHIDDEN(gl_dispatch_functions_start)')
211        print('gl_dispatch_functions_start:')
212        print('')
213        return
214
215    def printRealFooter(self):
216        print('')
217        print('\t.globl\tgl_dispatch_functions_end')
218        print('\tHIDDEN(gl_dispatch_functions_end)')
219        print('gl_dispatch_functions_end:')
220        return
221
222    def printBody(self, api):
223        for f in api.functionIterateByOffset():
224            name = f.dispatch_name()
225
226            print('\tGL_STUB(gl%s, %d)' % (name, f.offset))
227
228            if not f.is_static_entry_point(f.name):
229                print('\tHIDDEN(gl%s)' % (name))
230
231        for f in api.functionIterateByOffset():
232            name = f.dispatch_name()
233
234            if f.is_static_entry_point(f.name):
235                for n in f.entry_points:
236                    if n != f.name:
237                        text = '\tGL_STUB_ALIAS(gl%s, gl%s)' % (n, f.name)
238
239                        if f.has_different_protocol(n):
240                            print('#ifndef GLX_INDIRECT_RENDERING')
241                            print(text)
242                            print('#endif')
243                        else:
244                            print(text)
245
246        return
247
248
249def _parser():
250    """Parse arguments and return a namespace."""
251    parser = argparse.ArgumentParser()
252    parser.add_argument('-f',
253                        dest='filename',
254                        default='gl_API.xml',
255                        help='An XML description of an API.')
256    return parser.parse_args()
257
258
259def main():
260    """Main function."""
261    args = _parser()
262    printer = PrintGenericStubs()
263
264    api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
265    printer.Print(api)
266
267
268if __name__ == '__main__':
269    main()
270