1
2# (C) Copyright IBM Corporation 2005
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
30import copy
31
32import license
33import gl_XML, glX_XML
34
35def should_use_push(registers):
36    for [reg, offset] in registers:
37        if reg[1:4] == "xmm":
38            return 0
39
40    N = len(registers)
41    return (N & 1) != 0
42
43
44def local_size(registers):
45    # The x86-64 ABI says "the value (%rsp - 8) is always a multiple of
46    # 16 when control is transfered to the function entry point."  This
47    # means that the local stack usage must be (16*N)+8 for some value
48    # of N.  (16*N)+8 = (8*(2N))+8 = 8*(2N+1).  As long as N is odd, we
49    # meet this requirement.
50
51    N = (len(registers) | 1)
52    return 8*N
53
54
55def save_all_regs(registers):
56    adjust_stack = 0
57    if not should_use_push(registers):
58        adjust_stack = local_size(registers)
59        print('\tsubq\t$%u, %%rsp' % (adjust_stack))
60
61    for [reg, stack_offset] in registers:
62        save_reg( reg, stack_offset, adjust_stack )
63    return
64
65
66def restore_all_regs(registers):
67    adjust_stack = 0
68    if not should_use_push(registers):
69        adjust_stack = local_size(registers)
70
71    temp = copy.deepcopy(registers)
72    while len(temp):
73        [reg, stack_offset] = temp.pop()
74        restore_reg(reg, stack_offset, adjust_stack)
75
76    if adjust_stack:
77        print('\taddq\t$%u, %%rsp' % (adjust_stack))
78    return
79
80
81def save_reg(reg, offset, use_move):
82    if use_move:
83        if offset == 0:
84            print('\tmovq\t%s, (%%rsp)' % (reg))
85        else:
86            print('\tmovq\t%s, %u(%%rsp)' % (reg, offset))
87    else:
88        print('\tpushq\t%s' % (reg))
89
90    return
91
92
93def restore_reg(reg, offset, use_move):
94    if use_move:
95        if offset == 0:
96            print('\tmovq\t(%%rsp), %s' % (reg))
97        else:
98            print('\tmovq\t%u(%%rsp), %s' % (offset, reg))
99    else:
100        print('\tpopq\t%s' % (reg))
101
102    return
103
104
105class PrintGenericStubs(gl_XML.gl_print_base):
106
107    def __init__(self):
108        gl_XML.gl_print_base.__init__(self)
109
110        self.name = "gl_x86-64_asm.py (from Mesa)"
111        self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
112        return
113
114
115    def get_stack_size(self, f):
116        size = 0
117        for p in f.parameterIterator():
118            size += p.get_stack_size()
119
120        return size
121
122
123    def printRealHeader(self):
124        print("/* If we build with gcc's -fvisibility=hidden flag, we'll need to change")
125        print(" * the symbol visibility mode to 'default'.")
126        print(' */')
127        print('')
128        print('#include "x86/assyntax.h"')
129        print('')
130        print('#ifdef __GNUC__')
131        print('#  pragma GCC visibility push(default)')
132        print('#  define HIDDEN(x) .hidden x')
133        print('#else')
134        print('#  define HIDDEN(x)')
135        print('#endif')
136        print('')
137        print('#  define GL_PREFIX(n) GLNAME(CONCAT(gl,n))')
138        print('')
139        print('\t.text')
140        print('')
141        print('#ifdef USE_ELF_TLS')
142        print('')
143        print('_x86_64_get_dispatch:')
144        print('\tmovq\t_glapi_tls_Dispatch@GOTTPOFF(%rip), %rax')
145        print('\tmovq\t%fs:(%rax), %rax')
146        print('\tret')
147        print('\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch')
148        print('')
149        print('#elif defined(HAVE_PTHREAD)')
150        print('')
151        print('\t.extern\t_glapi_Dispatch')
152        print('\t.extern\t_gl_DispatchTSD')
153        print('\t.extern\tpthread_getspecific')
154        print('')
155        print('\t.p2align\t4,,15')
156        print('_x86_64_get_dispatch:')
157        print('\tmovq\t_gl_DispatchTSD@GOTPCREL(%rip), %rax')
158        print('\tmovl\t(%rax), %edi')
159        print('\tjmp\tpthread_getspecific@PLT')
160        print('')
161        print('#else')
162        print('')
163        print('\t.extern\t_glapi_get_dispatch')
164        print('')
165        print('#endif')
166        print('')
167        return
168
169
170    def printRealFooter(self):
171        print('')
172        print('#if defined (__ELF__) && defined (__linux__)')
173        print('	.section .note.GNU-stack,"",%progbits')
174        print('#endif')
175        return
176
177
178    def printFunction(self, f):
179
180        # The x86-64 ABI divides function parameters into a couple
181        # classes.  For the OpenGL interface, the only ones that are
182        # relevant are INTEGER and SSE.  Basically, the first 8
183        # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7,
184        # the first 6 non-GLfloat / non-GLdouble parameters are placed
185        # in registers listed in int_parameters.
186        #
187        # If more parameters than that are required, they are passed
188        # on the stack.  Therefore, we just have to make sure that
189        # %esp hasn't changed when we jump to the actual function.
190        # Since we're jumping to the function (and not calling it), we
191        # have to make sure of that anyway!
192
193        int_parameters = ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"]
194
195        int_class = 0
196        sse_class = 0
197        stack_offset = 0
198        registers = []
199        for p in f.parameterIterator():
200            type_name = p.get_base_type_string()
201
202            if p.is_pointer() or (type_name != "GLfloat" and type_name != "GLdouble"):
203                if int_class < 6:
204                    registers.append( [int_parameters[int_class], stack_offset] )
205                    int_class += 1
206                    stack_offset += 8
207            else:
208                if sse_class < 8:
209                    registers.append( ["%%xmm%u" % (sse_class), stack_offset] )
210                    sse_class += 1
211                    stack_offset += 8
212
213        if ((int_class & 1) == 0) and (sse_class == 0):
214            registers.append( ["%rbp", 0] )
215
216
217        name = f.dispatch_name()
218
219        print('\t.p2align\t4,,15')
220        print('\t.globl\tGL_PREFIX(%s)' % (name))
221        print('\t.type\tGL_PREFIX(%s), @function' % (name))
222        if not f.is_static_entry_point(f.name):
223            print('\tHIDDEN(GL_PREFIX(%s))' % (name))
224        print('GL_PREFIX(%s):' % (name))
225        print('#if defined(USE_ELF_TLS)')
226        print('\tcall\t_x86_64_get_dispatch@PLT')
227        print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8))
228        print('\tjmp\t*%r11')
229        print('#elif defined(HAVE_PTHREAD)')
230
231        save_all_regs(registers)
232        print('\tcall\t_x86_64_get_dispatch@PLT')
233        restore_all_regs(registers)
234
235        if f.offset == 0:
236            print('\tmovq\t(%rax), %r11')
237        else:
238            print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8))
239
240        print('\tjmp\t*%r11')
241
242        print('#else')
243        print('\tmovq\t_glapi_Dispatch(%rip), %rax')
244        print('\ttestq\t%rax, %rax')
245        print('\tje\t1f')
246        print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8))
247        print('\tjmp\t*%r11')
248        print('1:')
249
250        save_all_regs(registers)
251        print('\tcall\t_glapi_get_dispatch')
252        restore_all_regs(registers)
253
254        print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8))
255        print('\tjmp\t*%r11')
256        print('#endif /* defined(USE_ELF_TLS) */')
257
258        print('\t.size\tGL_PREFIX(%s), .-GL_PREFIX(%s)' % (name, name))
259        print('')
260        return
261
262
263    def printBody(self, api):
264        for f in api.functionIterateByOffset():
265            self.printFunction(f)
266
267
268        for f in api.functionIterateByOffset():
269            dispatch = f.dispatch_name()
270            for n in f.entry_points:
271                if n != f.name:
272                    if f.is_static_entry_point(n):
273                        text = '\t.globl GL_PREFIX(%s) ; .set GL_PREFIX(%s), GL_PREFIX(%s)' % (n, n, dispatch)
274
275                        if f.has_different_protocol(n):
276                            print('#ifndef GLX_INDIRECT_RENDERING')
277                            print(text)
278                            print('#endif')
279                        else:
280                            print(text)
281
282        return
283
284
285def _parser():
286    """Parse arguments and return a namespace."""
287    parser = argparse.ArgumentParser()
288    parser.add_argument('-f',
289                        default='gl_API.xml',
290                        dest='filename',
291                        help='An XML file describing an API')
292    return parser.parse_args()
293
294
295def main():
296    """Main file."""
297    args = _parser()
298    printer = PrintGenericStubs()
299    api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
300
301    printer.Print(api)
302
303
304if __name__ == '__main__':
305    main()
306