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>
29 import gl_XML
, glX_XML
30 import sys
, getopt
, copy
32 def should_use_push(registers
):
33 for [reg
, offset
] in registers
:
41 def local_size(registers
):
42 # The x86-64 ABI says "the value (%rsp - 8) is always a multiple of
43 # 16 when control is transfered to the function entry point." This
44 # means that the local stack usage must be (16*N)+8 for some value
45 # of N. (16*N)+8 = (8*(2N))+8 = 8*(2N+1). As long as N is odd, we
46 # meet this requirement.
48 N
= (len(registers
) |
1)
52 def save_all_regs(registers
):
54 if not should_use_push(registers
):
55 adjust_stack
= local_size(registers
)
56 print '\tsubq\t$%u, %%rsp' % (adjust_stack
)
58 for [reg
, stack_offset
] in registers
:
59 save_reg( reg
, stack_offset
, adjust_stack
)
63 def restore_all_regs(registers
):
65 if not should_use_push(registers
):
66 adjust_stack
= local_size(registers
)
68 temp
= copy
.deepcopy(registers
)
70 [reg
, stack_offset
] = temp
.pop()
71 restore_reg(reg
, stack_offset
, adjust_stack
)
74 print '\taddq\t$%u, %%rsp' % (adjust_stack
)
78 def save_reg(reg
, offset
, use_move
):
81 print '\tmovq\t%s, (%%rsp)' % (reg
)
83 print '\tmovq\t%s, %u(%%rsp)' % (reg
, offset
)
85 print '\tpushq\t%s' % (reg
)
90 def restore_reg(reg
, offset
, use_move
):
93 print '\tmovq\t(%%rsp), %s' % (reg
)
95 print '\tmovq\t%u(%%rsp), %s' % (offset
, reg
)
97 print '\tpopq\t%s' % (reg
)
102 class PrintGenericStubs(gl_XML
.gl_print_base
):
105 gl_XML
.gl_print_base
.__init
__(self
)
107 self
.name
= "gl_x86-64_asm.py (from Mesa)"
108 self
.license
= license
.bsd_license_template
% ("(C) Copyright IBM Corporation 2005", "IBM")
112 def get_stack_size(self
, f
):
114 for p
in f
.parameterIterator():
115 size
+= p
.get_stack_size()
120 def printRealHeader(self
):
121 print "/* If we build with gcc's -fvisibility=hidden flag, we'll need to change"
122 print " * the symbol visibility mode to 'default'."
125 print '#include "x86/assyntax.h"'
127 print '#ifdef __GNUC__'
128 print '# pragma GCC visibility push(default)'
129 print '# define HIDDEN(x) .hidden x'
131 print '# define HIDDEN(x)'
134 print '# if defined(USE_MGL_NAMESPACE)'
135 print '# define GL_PREFIX(n) GLNAME(CONCAT(mgl,n))'
136 print '# define _glapi_Dispatch _mglapi_Dispatch'
138 print '# define GL_PREFIX(n) GLNAME(CONCAT(gl,n))'
141 print '#if defined(HAVE_PTHREAD) || defined(_WIN32)'
142 print '# define THREADS'
147 print '#ifdef GLX_USE_TLS'
149 print '\t.globl _x86_64_get_get_dispatch; HIDDEN(_x86_64_get_get_dispatch)'
150 print '_x86_64_get_get_dispatch:'
151 print '\tlea\t_x86_64_get_dispatch(%rip), %rax'
154 print '\t.p2align\t4,,15'
155 print '_x86_64_get_dispatch:'
156 print '\tmovq\t_glapi_tls_Dispatch@GOTTPOFF(%rip), %rax'
157 print '\tmovq\t%fs:(%rax), %rax'
159 print '\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch'
161 print '#elif defined(HAVE_PTHREAD)'
163 print '\t.extern\t_glapi_Dispatch'
164 print '\t.extern\t_gl_DispatchTSD'
165 print '\t.extern\tpthread_getspecific'
167 print '\t.p2align\t4,,15'
168 print '_x86_64_get_dispatch:'
169 print '\tmovq\t_gl_DispatchTSD@GOTPCREL(%rip), %rax'
170 print '\tmovl\t(%rax), %edi'
171 print '\tjmp\tpthread_getspecific@PLT'
173 print '#elif defined(THREADS)'
175 print '\t.extern\t_glapi_get_dispatch'
182 def printRealFooter(self
):
184 print '#if defined (__ELF__) && defined (__linux__)'
185 print ' .section .note.GNU-stack,"",%progbits'
190 def printFunction(self
, f
):
192 # The x86-64 ABI divides function parameters into a couple
193 # classes. For the OpenGL interface, the only ones that are
194 # relevent are INTEGER and SSE. Basically, the first 8
195 # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7,
196 # the first 6 non-GLfloat / non-GLdouble parameters are placed
197 # in registers listed in int_parameters.
199 # If more parameters than that are required, they are passed
200 # on the stack. Therefore, we just have to make sure that
201 # %esp hasn't changed when we jump to the actual function.
202 # Since we're jumping to the function (and not calling it), we
203 # have to make sure of that anyway!
205 int_parameters
= ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"]
211 for p
in f
.parameterIterator():
212 type_name
= p
.get_base_type_string()
214 if p
.is_pointer() or (type_name
!= "GLfloat" and type_name
!= "GLdouble"):
216 registers
.append( [int_parameters
[int_class
], stack_offset
] )
221 registers
.append( ["%%xmm%u" % (sse_class
), stack_offset
] )
225 if ((int_class
& 1) == 0) and (sse_class
== 0):
226 registers
.append( ["%rbp", 0] )
229 name
= f
.dispatch_name()
231 print '\t.p2align\t4,,15'
232 print '\t.globl\tGL_PREFIX(%s)' % (name
)
233 print '\t.type\tGL_PREFIX(%s), @function' % (name
)
234 if not f
.is_static_entry_point(f
.name
):
235 print '\tHIDDEN(GL_PREFIX(%s))' % (name
)
236 print 'GL_PREFIX(%s):' % (name
)
237 print '#if defined(GLX_USE_TLS)'
238 print '\tcall\t_x86_64_get_dispatch@PLT'
239 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
241 print '#elif defined(HAVE_PTHREAD)'
243 save_all_regs(registers
)
244 print '\tcall\t_x86_64_get_dispatch@PLT'
245 restore_all_regs(registers
)
248 print '\tmovq\t(%rax), %r11'
250 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
255 print '\tmovq\t_glapi_Dispatch(%rip), %rax'
256 print '\ttestq\t%rax, %rax'
258 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
262 save_all_regs(registers
)
263 print '\tcall\t_glapi_get_dispatch'
264 restore_all_regs(registers
)
266 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
268 print '#endif /* defined(GLX_USE_TLS) */'
270 print '\t.size\tGL_PREFIX(%s), .-GL_PREFIX(%s)' % (name
, name
)
275 def printBody(self
, api
):
276 for f
in api
.functionIterateByOffset():
277 self
.printFunction(f
)
280 for f
in api
.functionIterateByOffset():
281 dispatch
= f
.dispatch_name()
282 for n
in f
.entry_points
:
284 if f
.is_static_entry_point(n
):
285 text
= '\t.globl GL_PREFIX(%s) ; .set GL_PREFIX(%s), GL_PREFIX(%s)' % (n
, n
, dispatch
)
287 if f
.has_different_protocol(n
):
288 print '#ifndef GLX_INDIRECT_RENDERING'
297 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys
.argv
[0]
300 if __name__
== '__main__':
301 file_name
= "gl_API.xml"
305 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "m:f:")
309 for (arg
,val
) in args
:
315 if mode
== "generic":
316 printer
= PrintGenericStubs()
318 print "ERROR: Invalid mode \"%s\" specified." % mode
321 api
= gl_XML
.parse_GL_API(file_name
, glX_XML
.glx_item_factory())