glapi: gl_table.py: remove unused variable 'es'
[mesa.git] / src / mapi / glapi / gen / gl_x86-64_asm.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2005
4 # All Rights Reserved.
5 #
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:
12 #
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
15 # Software.
16 #
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
23 # IN THE SOFTWARE.
24 #
25 # Authors:
26 # Ian Romanick <idr@us.ibm.com>
27
28 import argparse
29 import copy
30
31 import license
32 import gl_XML, glX_XML
33
34 def should_use_push(registers):
35 for [reg, offset] in registers:
36 if reg[1:4] == "xmm":
37 return 0
38
39 N = len(registers)
40 return (N & 1) != 0
41
42
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.
49
50 N = (len(registers) | 1)
51 return 8*N
52
53
54 def save_all_regs(registers):
55 adjust_stack = 0
56 if not should_use_push(registers):
57 adjust_stack = local_size(registers)
58 print '\tsubq\t$%u, %%rsp' % (adjust_stack)
59
60 for [reg, stack_offset] in registers:
61 save_reg( reg, stack_offset, adjust_stack )
62 return
63
64
65 def restore_all_regs(registers):
66 adjust_stack = 0
67 if not should_use_push(registers):
68 adjust_stack = local_size(registers)
69
70 temp = copy.deepcopy(registers)
71 while len(temp):
72 [reg, stack_offset] = temp.pop()
73 restore_reg(reg, stack_offset, adjust_stack)
74
75 if adjust_stack:
76 print '\taddq\t$%u, %%rsp' % (adjust_stack)
77 return
78
79
80 def save_reg(reg, offset, use_move):
81 if use_move:
82 if offset == 0:
83 print '\tmovq\t%s, (%%rsp)' % (reg)
84 else:
85 print '\tmovq\t%s, %u(%%rsp)' % (reg, offset)
86 else:
87 print '\tpushq\t%s' % (reg)
88
89 return
90
91
92 def restore_reg(reg, offset, use_move):
93 if use_move:
94 if offset == 0:
95 print '\tmovq\t(%%rsp), %s' % (reg)
96 else:
97 print '\tmovq\t%u(%%rsp), %s' % (offset, reg)
98 else:
99 print '\tpopq\t%s' % (reg)
100
101 return
102
103
104 class PrintGenericStubs(gl_XML.gl_print_base):
105
106 def __init__(self):
107 gl_XML.gl_print_base.__init__(self)
108
109 self.name = "gl_x86-64_asm.py (from Mesa)"
110 self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM")
111 return
112
113
114 def get_stack_size(self, f):
115 size = 0
116 for p in f.parameterIterator():
117 size += p.get_stack_size()
118
119 return size
120
121
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'."
125 print ' */'
126 print ''
127 print '#include "x86/assyntax.h"'
128 print ''
129 print '#ifdef __GNUC__'
130 print '# pragma GCC visibility push(default)'
131 print '# define HIDDEN(x) .hidden x'
132 print '#else'
133 print '# define HIDDEN(x)'
134 print '#endif'
135 print ''
136 print '# if defined(USE_MGL_NAMESPACE)'
137 print '# define GL_PREFIX(n) GLNAME(CONCAT(mgl,n))'
138 print '# define _glapi_Dispatch _mglapi_Dispatch'
139 print '# else'
140 print '# define GL_PREFIX(n) GLNAME(CONCAT(gl,n))'
141 print '# endif'
142 print ''
143 print '\t.text'
144 print ''
145 print '#ifdef GLX_USE_TLS'
146 print ''
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'
150 print '\tret'
151 print ''
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'
156 print '\tret'
157 print '\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch'
158 print ''
159 print '#elif defined(HAVE_PTHREAD)'
160 print ''
161 print '\t.extern\t_glapi_Dispatch'
162 print '\t.extern\t_gl_DispatchTSD'
163 print '\t.extern\tpthread_getspecific'
164 print ''
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'
170 print ''
171 print '#else'
172 print ''
173 print '\t.extern\t_glapi_get_dispatch'
174 print ''
175 print '#endif'
176 print ''
177 return
178
179
180 def printRealFooter(self):
181 print ''
182 print '#if defined (__ELF__) && defined (__linux__)'
183 print ' .section .note.GNU-stack,"",%progbits'
184 print '#endif'
185 return
186
187
188 def printFunction(self, f):
189
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.
196 #
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!
202
203 int_parameters = ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"]
204
205 int_class = 0
206 sse_class = 0
207 stack_offset = 0
208 registers = []
209 for p in f.parameterIterator():
210 type_name = p.get_base_type_string()
211
212 if p.is_pointer() or (type_name != "GLfloat" and type_name != "GLdouble"):
213 if int_class < 6:
214 registers.append( [int_parameters[int_class], stack_offset] )
215 int_class += 1
216 stack_offset += 8
217 else:
218 if sse_class < 8:
219 registers.append( ["%%xmm%u" % (sse_class), stack_offset] )
220 sse_class += 1
221 stack_offset += 8
222
223 if ((int_class & 1) == 0) and (sse_class == 0):
224 registers.append( ["%rbp", 0] )
225
226
227 name = f.dispatch_name()
228
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)
238 print '\tjmp\t*%r11'
239 print '#elif defined(HAVE_PTHREAD)'
240
241 save_all_regs(registers)
242 print '\tcall\t_x86_64_get_dispatch@PLT'
243 restore_all_regs(registers)
244
245 if f.offset == 0:
246 print '\tmovq\t(%rax), %r11'
247 else:
248 print '\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)
249
250 print '\tjmp\t*%r11'
251
252 print '#else'
253 print '\tmovq\t_glapi_Dispatch(%rip), %rax'
254 print '\ttestq\t%rax, %rax'
255 print '\tje\t1f'
256 print '\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)
257 print '\tjmp\t*%r11'
258 print '1:'
259
260 save_all_regs(registers)
261 print '\tcall\t_glapi_get_dispatch'
262 restore_all_regs(registers)
263
264 print '\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)
265 print '\tjmp\t*%r11'
266 print '#endif /* defined(GLX_USE_TLS) */'
267
268 print '\t.size\tGL_PREFIX(%s), .-GL_PREFIX(%s)' % (name, name)
269 print ''
270 return
271
272
273 def printBody(self, api):
274 for f in api.functionIterateByOffset():
275 self.printFunction(f)
276
277
278 for f in api.functionIterateByOffset():
279 dispatch = f.dispatch_name()
280 for n in f.entry_points:
281 if n != f.name:
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)
284
285 if f.has_different_protocol(n):
286 print '#ifndef GLX_INDIRECT_RENDERING'
287 print text
288 print '#endif'
289 else:
290 print text
291
292 return
293
294
295 def _parser():
296 """Parse arguments and return a namespace."""
297 parser = argparse.ArgumentParser()
298 parser.add_argument('-f',
299 default='gl_API.xml',
300 dest='filename',
301 help='An XML file describing an API')
302 return parser.parse_args()
303
304
305 def main():
306 """Main file."""
307 args = _parser()
308 printer = PrintGenericStubs()
309 api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory())
310
311 printer.Print(api)
312
313
314 if __name__ == '__main__':
315 main()