2 # (C) Copyright IBM Corporation 2005
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:
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
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
25 # Ian Romanick <idr@us.ibm.com>
27 from __future__
import print_function
33 import gl_XML
, glX_XML
35 def should_use_push(registers
):
36 for [reg
, offset
] in registers
:
44 def 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.
51 N
= (len(registers
) |
1)
55 def save_all_regs(registers
):
57 if not should_use_push(registers
):
58 adjust_stack
= local_size(registers
)
59 print('\tsubq\t$%u, %%rsp' % (adjust_stack
))
61 for [reg
, stack_offset
] in registers
:
62 save_reg( reg
, stack_offset
, adjust_stack
)
66 def restore_all_regs(registers
):
68 if not should_use_push(registers
):
69 adjust_stack
= local_size(registers
)
71 temp
= copy
.deepcopy(registers
)
73 [reg
, stack_offset
] = temp
.pop()
74 restore_reg(reg
, stack_offset
, adjust_stack
)
77 print('\taddq\t$%u, %%rsp' % (adjust_stack
))
81 def save_reg(reg
, offset
, use_move
):
84 print('\tmovq\t%s, (%%rsp)' % (reg
))
86 print('\tmovq\t%s, %u(%%rsp)' % (reg
, offset
))
88 print('\tpushq\t%s' % (reg
))
93 def restore_reg(reg
, offset
, use_move
):
96 print('\tmovq\t(%%rsp), %s' % (reg
))
98 print('\tmovq\t%u(%%rsp), %s' % (offset
, reg
))
100 print('\tpopq\t%s' % (reg
))
105 class PrintGenericStubs(gl_XML
.gl_print_base
):
108 gl_XML
.gl_print_base
.__init
__(self
)
110 self
.name
= "gl_x86-64_asm.py (from Mesa)"
111 self
.license
= license
.bsd_license_template
% ("(C) Copyright IBM Corporation 2005", "IBM")
115 def get_stack_size(self
, f
):
117 for p
in f
.parameterIterator():
118 size
+= p
.get_stack_size()
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'.")
128 print('#include "x86/assyntax.h"')
130 print('#ifdef __GNUC__')
131 print('# pragma GCC visibility push(default)')
132 print('# define HIDDEN(x) .hidden x')
134 print('# define HIDDEN(x)')
137 print('# if defined(USE_MGL_NAMESPACE)')
138 print('# define GL_PREFIX(n) GLNAME(CONCAT(mgl,n))')
139 print('# define _glapi_Dispatch _mglapi_Dispatch')
141 print('# define GL_PREFIX(n) GLNAME(CONCAT(gl,n))')
146 print('#ifdef USE_ELF_TLS')
148 print('_x86_64_get_dispatch:')
149 print('\tmovq\t_glapi_tls_Dispatch@GOTTPOFF(%rip), %rax')
150 print('\tmovq\t%fs:(%rax), %rax')
152 print('\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch')
154 print('#elif defined(HAVE_PTHREAD)')
156 print('\t.extern\t_glapi_Dispatch')
157 print('\t.extern\t_gl_DispatchTSD')
158 print('\t.extern\tpthread_getspecific')
160 print('\t.p2align\t4,,15')
161 print('_x86_64_get_dispatch:')
162 print('\tmovq\t_gl_DispatchTSD@GOTPCREL(%rip), %rax')
163 print('\tmovl\t(%rax), %edi')
164 print('\tjmp\tpthread_getspecific@PLT')
168 print('\t.extern\t_glapi_get_dispatch')
175 def printRealFooter(self
):
177 print('#if defined (__ELF__) && defined (__linux__)')
178 print(' .section .note.GNU-stack,"",%progbits')
183 def printFunction(self
, f
):
185 # The x86-64 ABI divides function parameters into a couple
186 # classes. For the OpenGL interface, the only ones that are
187 # relevant are INTEGER and SSE. Basically, the first 8
188 # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7,
189 # the first 6 non-GLfloat / non-GLdouble parameters are placed
190 # in registers listed in int_parameters.
192 # If more parameters than that are required, they are passed
193 # on the stack. Therefore, we just have to make sure that
194 # %esp hasn't changed when we jump to the actual function.
195 # Since we're jumping to the function (and not calling it), we
196 # have to make sure of that anyway!
198 int_parameters
= ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"]
204 for p
in f
.parameterIterator():
205 type_name
= p
.get_base_type_string()
207 if p
.is_pointer() or (type_name
!= "GLfloat" and type_name
!= "GLdouble"):
209 registers
.append( [int_parameters
[int_class
], stack_offset
] )
214 registers
.append( ["%%xmm%u" % (sse_class
), stack_offset
] )
218 if ((int_class
& 1) == 0) and (sse_class
== 0):
219 registers
.append( ["%rbp", 0] )
222 name
= f
.dispatch_name()
224 print('\t.p2align\t4,,15')
225 print('\t.globl\tGL_PREFIX(%s)' % (name
))
226 print('\t.type\tGL_PREFIX(%s), @function' % (name
))
227 if not f
.is_static_entry_point(f
.name
):
228 print('\tHIDDEN(GL_PREFIX(%s))' % (name
))
229 print('GL_PREFIX(%s):' % (name
))
230 print('#if defined(USE_ELF_TLS)')
231 print('\tcall\t_x86_64_get_dispatch@PLT')
232 print('\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8))
233 print('\tjmp\t*%r11')
234 print('#elif defined(HAVE_PTHREAD)')
236 save_all_regs(registers
)
237 print('\tcall\t_x86_64_get_dispatch@PLT')
238 restore_all_regs(registers
)
241 print('\tmovq\t(%rax), %r11')
243 print('\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8))
245 print('\tjmp\t*%r11')
248 print('\tmovq\t_glapi_Dispatch(%rip), %rax')
249 print('\ttestq\t%rax, %rax')
251 print('\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8))
252 print('\tjmp\t*%r11')
255 save_all_regs(registers
)
256 print('\tcall\t_glapi_get_dispatch')
257 restore_all_regs(registers
)
259 print('\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8))
260 print('\tjmp\t*%r11')
261 print('#endif /* defined(USE_ELF_TLS) */')
263 print('\t.size\tGL_PREFIX(%s), .-GL_PREFIX(%s)' % (name
, name
))
268 def printBody(self
, api
):
269 for f
in api
.functionIterateByOffset():
270 self
.printFunction(f
)
273 for f
in api
.functionIterateByOffset():
274 dispatch
= f
.dispatch_name()
275 for n
in f
.entry_points
:
277 if f
.is_static_entry_point(n
):
278 text
= '\t.globl GL_PREFIX(%s) ; .set GL_PREFIX(%s), GL_PREFIX(%s)' % (n
, n
, dispatch
)
280 if f
.has_different_protocol(n
):
281 print('#ifndef GLX_INDIRECT_RENDERING')
291 """Parse arguments and return a namespace."""
292 parser
= argparse
.ArgumentParser()
293 parser
.add_argument('-f',
294 default
='gl_API.xml',
296 help='An XML file describing an API')
297 return parser
.parse_args()
303 printer
= PrintGenericStubs()
304 api
= gl_XML
.parse_GL_API(args
.filename
, glX_XML
.glx_item_factory())
309 if __name__
== '__main__':