3 # (C) Copyright IBM Corporation 2005
6 # Permission is hereby granted, free of charge, to any person obtaining a
7 # copy of this software and associated documentation files (the "Software"),
8 # to deal in the Software without restriction, including without limitation
9 # on the rights to use, copy, modify, merge, publish, distribute, sub
10 # license, and/or sell copies of the Software, and to permit persons to whom
11 # the Software is furnished to do so, subject to the following conditions:
13 # The above copyright notice and this permission notice (including the next
14 # paragraph) shall be included in all copies or substantial portions of the
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 # Ian Romanick <idr@us.ibm.com>
32 import gl_XML
, glX_XML
34 def should_use_push(registers
):
35 for [reg
, offset
] in registers
:
43 def local_size(registers
):
44 # The x86-64 ABI says "the value (%rsp - 8) is always a multiple of
45 # 16 when control is transfered to the function entry point." This
46 # means that the local stack usage must be (16*N)+8 for some value
47 # of N. (16*N)+8 = (8*(2N))+8 = 8*(2N+1). As long as N is odd, we
48 # meet this requirement.
50 N
= (len(registers
) |
1)
54 def save_all_regs(registers
):
56 if not should_use_push(registers
):
57 adjust_stack
= local_size(registers
)
58 print '\tsubq\t$%u, %%rsp' % (adjust_stack
)
60 for [reg
, stack_offset
] in registers
:
61 save_reg( reg
, stack_offset
, adjust_stack
)
65 def restore_all_regs(registers
):
67 if not should_use_push(registers
):
68 adjust_stack
= local_size(registers
)
70 temp
= copy
.deepcopy(registers
)
72 [reg
, stack_offset
] = temp
.pop()
73 restore_reg(reg
, stack_offset
, adjust_stack
)
76 print '\taddq\t$%u, %%rsp' % (adjust_stack
)
80 def save_reg(reg
, offset
, use_move
):
83 print '\tmovq\t%s, (%%rsp)' % (reg
)
85 print '\tmovq\t%s, %u(%%rsp)' % (reg
, offset
)
87 print '\tpushq\t%s' % (reg
)
92 def restore_reg(reg
, offset
, use_move
):
95 print '\tmovq\t(%%rsp), %s' % (reg
)
97 print '\tmovq\t%u(%%rsp), %s' % (offset
, reg
)
99 print '\tpopq\t%s' % (reg
)
104 class PrintGenericStubs(gl_XML
.gl_print_base
):
107 gl_XML
.gl_print_base
.__init
__(self
)
109 self
.name
= "gl_x86-64_asm.py (from Mesa)"
110 self
.license
= license
.bsd_license_template
% ("(C) Copyright IBM Corporation 2005", "IBM")
114 def get_stack_size(self
, f
):
116 for p
in f
.parameterIterator():
117 size
+= p
.get_stack_size()
122 def printRealHeader(self
):
123 print "/* If we build with gcc's -fvisibility=hidden flag, we'll need to change"
124 print " * the symbol visibility mode to 'default'."
127 print '#include "x86/assyntax.h"'
129 print '#ifdef __GNUC__'
130 print '# pragma GCC visibility push(default)'
131 print '# define HIDDEN(x) .hidden x'
133 print '# define HIDDEN(x)'
136 print '# if defined(USE_MGL_NAMESPACE)'
137 print '# define GL_PREFIX(n) GLNAME(CONCAT(mgl,n))'
138 print '# define _glapi_Dispatch _mglapi_Dispatch'
140 print '# define GL_PREFIX(n) GLNAME(CONCAT(gl,n))'
145 print '#ifdef GLX_USE_TLS'
147 print '\t.globl _x86_64_get_get_dispatch; HIDDEN(_x86_64_get_get_dispatch)'
148 print '_x86_64_get_get_dispatch:'
149 print '\tlea\t_x86_64_get_dispatch(%rip), %rax'
152 print '\t.p2align\t4,,15'
153 print '_x86_64_get_dispatch:'
154 print '\tmovq\t_glapi_tls_Dispatch@GOTTPOFF(%rip), %rax'
155 print '\tmovq\t%fs:(%rax), %rax'
157 print '\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch'
159 print '#elif defined(HAVE_PTHREAD)'
161 print '\t.extern\t_glapi_Dispatch'
162 print '\t.extern\t_gl_DispatchTSD'
163 print '\t.extern\tpthread_getspecific'
165 print '\t.p2align\t4,,15'
166 print '_x86_64_get_dispatch:'
167 print '\tmovq\t_gl_DispatchTSD@GOTPCREL(%rip), %rax'
168 print '\tmovl\t(%rax), %edi'
169 print '\tjmp\tpthread_getspecific@PLT'
173 print '\t.extern\t_glapi_get_dispatch'
180 def printRealFooter(self
):
182 print '#if defined (__ELF__) && defined (__linux__)'
183 print ' .section .note.GNU-stack,"",%progbits'
188 def printFunction(self
, f
):
190 # The x86-64 ABI divides function parameters into a couple
191 # classes. For the OpenGL interface, the only ones that are
192 # relevant are INTEGER and SSE. Basically, the first 8
193 # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7,
194 # the first 6 non-GLfloat / non-GLdouble parameters are placed
195 # in registers listed in int_parameters.
197 # If more parameters than that are required, they are passed
198 # on the stack. Therefore, we just have to make sure that
199 # %esp hasn't changed when we jump to the actual function.
200 # Since we're jumping to the function (and not calling it), we
201 # have to make sure of that anyway!
203 int_parameters
= ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"]
209 for p
in f
.parameterIterator():
210 type_name
= p
.get_base_type_string()
212 if p
.is_pointer() or (type_name
!= "GLfloat" and type_name
!= "GLdouble"):
214 registers
.append( [int_parameters
[int_class
], stack_offset
] )
219 registers
.append( ["%%xmm%u" % (sse_class
), stack_offset
] )
223 if ((int_class
& 1) == 0) and (sse_class
== 0):
224 registers
.append( ["%rbp", 0] )
227 name
= f
.dispatch_name()
229 print '\t.p2align\t4,,15'
230 print '\t.globl\tGL_PREFIX(%s)' % (name
)
231 print '\t.type\tGL_PREFIX(%s), @function' % (name
)
232 if not f
.is_static_entry_point(f
.name
):
233 print '\tHIDDEN(GL_PREFIX(%s))' % (name
)
234 print 'GL_PREFIX(%s):' % (name
)
235 print '#if defined(GLX_USE_TLS)'
236 print '\tcall\t_x86_64_get_dispatch@PLT'
237 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
239 print '#elif defined(HAVE_PTHREAD)'
241 save_all_regs(registers
)
242 print '\tcall\t_x86_64_get_dispatch@PLT'
243 restore_all_regs(registers
)
246 print '\tmovq\t(%rax), %r11'
248 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
253 print '\tmovq\t_glapi_Dispatch(%rip), %rax'
254 print '\ttestq\t%rax, %rax'
256 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
260 save_all_regs(registers
)
261 print '\tcall\t_glapi_get_dispatch'
262 restore_all_regs(registers
)
264 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
266 print '#endif /* defined(GLX_USE_TLS) */'
268 print '\t.size\tGL_PREFIX(%s), .-GL_PREFIX(%s)' % (name
, name
)
273 def printBody(self
, api
):
274 for f
in api
.functionIterateByOffset():
275 self
.printFunction(f
)
278 for f
in api
.functionIterateByOffset():
279 dispatch
= f
.dispatch_name()
280 for n
in f
.entry_points
:
282 if f
.is_static_entry_point(n
):
283 text
= '\t.globl GL_PREFIX(%s) ; .set GL_PREFIX(%s), GL_PREFIX(%s)' % (n
, n
, dispatch
)
285 if f
.has_different_protocol(n
):
286 print '#ifndef GLX_INDIRECT_RENDERING'
296 """Parse arguments and return a namespace."""
297 parser
= argparse
.ArgumentParser()
298 parser
.add_argument('-f',
299 default
='gl_API.xml',
301 help='An XML file describing an API')
302 return parser
.parse_args()
308 printer
= PrintGenericStubs()
309 api
= gl_XML
.parse_GL_API(args
.filename
, glX_XML
.glx_item_factory())
314 if __name__
== '__main__':