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>
28 import gl_XML
, license
29 import sys
, getopt
, copy
31 def should_use_push(registers
):
32 for [reg
, offset
] in registers
:
40 def local_size(registers
):
41 # The x86-64 ABI says "the value (%rsp - 8) is always a multiple of
42 # 16 when control is transfered to the function entry point." This
43 # means that the local stack usage must be (16*N)+8 for some value
44 # of N. (16*N)+8 = (8*(2N))+8 = 8*(2N+1). As long as N is odd, we
45 # meet this requirement.
47 N
= (len(registers
) |
1)
51 def save_all_regs(registers
):
53 if not should_use_push(registers
):
54 adjust_stack
= local_size(registers
)
55 print '\tsubq\t$%u, %%rsp' % (adjust_stack
)
57 for [reg
, stack_offset
] in registers
:
58 save_reg( reg
, stack_offset
, adjust_stack
)
62 def restore_all_regs(registers
):
64 if not should_use_push(registers
):
65 adjust_stack
= local_size(registers
)
67 temp
= copy
.deepcopy(registers
)
69 [reg
, stack_offset
] = temp
.pop()
70 restore_reg(reg
, stack_offset
, adjust_stack
)
73 print '\taddq\t$%u, %%rsp' % (adjust_stack
)
77 def save_reg(reg
, offset
, use_move
):
80 print '\tmovq\t%s, (%%rsp)' % (reg
)
82 print '\tmovq\t%s, %u(%%rsp)' % (reg
, offset
)
84 print '\tpushq\t%s' % (reg
)
89 def restore_reg(reg
, offset
, use_move
):
92 print '\tmovq\t(%%rsp), %s' % (reg
)
94 print '\tmovq\t%u(%%rsp), %s' % (offset
, reg
)
96 print '\tpopq\t%s' % (reg
)
101 class PrintGenericStubs(gl_XML
.gl_print_base
):
104 gl_XML
.gl_print_base
.__init
__(self
)
106 self
.name
= "gl_x86-64_asm.py (from Mesa)"
107 self
.license
= license
.bsd_license_template
% ("(C) Copyright IBM Corporation 2005", "IBM")
111 def get_stack_size(self
, f
):
113 for p
in f
.parameterIterator():
114 size
+= p
.get_stack_size()
119 def printRealHeader(self
):
120 print "/* If we build with gcc's -fvisibility=hidden flag, we'll need to change"
121 print " * the symbol visibility mode to 'default'."
123 print '#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 303'
124 print '# pragma GCC visibility push(default)'
125 print '# define HIDDEN(x) .hidden x'
127 print '# define HIDDEN(x)'
130 print '#if defined(PTHREADS) || defined(USE_XTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)'
131 print '# define THREADS'
136 print '#ifdef GLX_USE_TLS'
138 print '\t.globl _x86_64_get_get_dispatch; HIDDEN(_x86_64_get_get_dispatch)'
139 print '_x86_64_get_get_dispatch:'
140 print '\tlea\t_x86_64_get_dispatch(%rip), %rax'
143 print '\t.p2align\t4,,15'
144 print '_x86_64_get_dispatch:'
145 print '\tmovq\t_glapi_tls_Dispatch@GOTTPOFF(%rip), %rax'
146 print '\tmovq\t%fs:(%rax), %rax'
148 print '\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch'
150 print '#elif defined(PTHREADS)'
152 print '\t.extern\t_glapi_Dispatch'
153 print '\t.extern\t_gl_DispatchTSD'
154 print '\t.extern\tpthread_getspecific'
156 print '\t.p2align\t4,,15'
157 print '_x86_64_get_dispatch:'
158 print '\tmovq\t_gl_DispatchTSD(%rip), %rdi'
159 print '\tjmp\tpthread_getspecific@PLT'
161 print '#elif defined(THREADS)'
163 print '\t.extern\t_glapi_get_dispatch'
170 def printRealFooter(self
):
172 print '#if defined(GLX_USE_TLS) && defined(__linux__)'
173 print ' .section ".note.ABI-tag", "a"'
175 print ' .long 1f - 0f /* name length */'
176 print ' .long 3f - 2f /* data length */'
177 print ' .long 1 /* note length */'
178 print '0: .asciz "GNU" /* vendor name */'
179 print '1: .p2align 2'
180 print '2: .long 0 /* note data: the ABI tag */'
181 print ' .long 2,4,20 /* Minimum kernel version w/TLS */'
182 print '3: .p2align 2 /* pad out section */'
183 print '#endif /* GLX_USE_TLS */'
187 def printFunction(self
, f
):
189 # The x86-64 ABI divides function parameters into a couple
190 # classes. For the OpenGL interface, the only ones that are
191 # relevent are INTEGER and SSE. Basically, the first 8
192 # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7,
193 # the first 6 non-GLfloat / non-GLdouble parameters are placed
194 # in registers listed in int_parameters.
196 # If more parameters than that are required, they are passed
197 # on the stack. Therefore, we just have to make sure that
198 # %esp hasn't changed when we jump to the actual function.
199 # Since we're jumping to the function (and not calling it), we
200 # have to make sure of that anyway!
202 int_parameters
= ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"]
208 for p
in f
.parameterIterator():
209 type_name
= p
.get_base_type_string()
211 if p
.is_pointer() or (type_name
!= "GLfloat" and type_name
!= "GLdouble"):
213 registers
.append( [int_parameters
[int_class
], stack_offset
] )
218 registers
.append( ["%%xmm%u" % (sse_class
), stack_offset
] )
222 if ((int_class
& 1) == 0) and (sse_class
== 0):
223 registers
.append( ["%rbp", 0] )
226 print '\t.p2align\t4,,15'
227 print '\t.globl\tgl%s' % (f
.name
)
228 print '\t.type\tgl%s, @function' % (f
.name
)
229 print 'gl%s:' % (f
.name
)
230 print '#if defined(GLX_USE_TLS)'
231 print '\tcall\t_x86_64_get_dispatch@PLT'
232 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
234 print '#elif defined(PTHREADS)'
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)
248 print '\tmovq\t_glapi_DispatchTSD(%rip), %rax'
249 print '\ttestq\t%rax, %rax'
251 print '\tmovq\t%u(%%rax), %%r11' % (f
.offset
* 8)
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)
261 print '#endif /* defined(GLX_USE_TLS) */'
263 print '\t.size\tgl%s, .-gl%s' % (f
.name
, f
.name
)
268 def printBody(self
, api
):
269 for f
in api
.functionIterateByOffset():
270 self
.printFunction(f
)
273 for f
in api
.functionIterateByOffset():
274 for n
in f
.entry_points
:
276 print '\t.globl gl%s ; .set gl%s, gl%s' % (n
, n
, f
.name
)
281 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys
.argv
[0]
284 if __name__
== '__main__':
285 file_name
= "gl_API.xml"
289 (args
, trail
) = getopt
.getopt(sys
.argv
[1:], "m:f:")
293 for (arg
,val
) in args
:
299 if mode
== "generic":
300 printer
= PrintGenericStubs()
302 print "ERROR: Invalid mode \"%s\" specified." % mode
305 api
= gl_XML
.parse_GL_API( file_name
)