Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / mesa / glapi / gl_x86_asm.py
1 #!/usr/bin/env python
2
3 # (C) Copyright IBM Corporation 2004, 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 license
29 import gl_XML, glX_XML
30 import sys, getopt
31
32 class PrintGenericStubs(gl_XML.gl_print_base):
33
34 def __init__(self):
35 gl_XML.gl_print_base.__init__(self)
36
37 self.name = "gl_x86_asm.py (from Mesa)"
38 self.license = license.bsd_license_template % ( \
39 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
40 (C) Copyright IBM Corporation 2004, 2005""", "BRIAN PAUL, IBM")
41 return
42
43
44 def get_stack_size(self, f):
45 size = 0
46 for p in f.parameterIterator():
47 if p.is_padding:
48 continue
49
50 size += p.get_stack_size()
51
52 return size
53
54
55 def printRealHeader(self):
56 print '#include "assyntax.h"'
57 print '#include "glapi/glapioffsets.h"'
58 print ''
59 print '#if defined(STDCALL_API)'
60 print '# if defined(USE_MGL_NAMESPACE)'
61 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(mgl,n2))'
62 print '# else'
63 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n2))'
64 print '# endif'
65 print '#else'
66 print '# if defined(USE_MGL_NAMESPACE)'
67 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(mgl,n))'
68 print '# define _glapi_Dispatch _mglapi_Dispatch'
69 print '# else'
70 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n))'
71 print '# endif'
72 print '#endif'
73 print ''
74 print '#define GL_OFFSET(x) CODEPTR(REGOFF(4 * x, EAX))'
75 print ''
76 print '#if defined(GNU_ASSEMBLER) && !defined(__DJGPP__) && !defined(__MINGW32__)'
77 print '#define GLOBL_FN(x) GLOBL x ; .type x, function'
78 print '#else'
79 print '#define GLOBL_FN(x) GLOBL x'
80 print '#endif'
81 print ''
82 print '#if defined(PTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)'
83 print '# define THREADS'
84 print '#endif'
85 print ''
86 print '#ifdef GLX_USE_TLS'
87 print ''
88 print '#ifdef GLX_X86_READONLY_TEXT'
89 print '# define CTX_INSNS MOV_L(GS:(EAX), EAX)'
90 print '#else'
91 print '# define CTX_INSNS NOP /* Pad for init_glapi_relocs() */'
92 print '#endif'
93 print ''
94 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
95 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
96 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
97 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
98 print '\tCALL(_x86_get_dispatch) ;\t\t\t\\'
99 print '\tCTX_INSNS ; \\'
100 print '\tJMP(GL_OFFSET(off))'
101 print ''
102 print '#elif defined(PTHREADS)'
103 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
104 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
105 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
106 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
107 print '\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\'
108 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\'
109 print '\tJE(1f) ;\t\t\t\t\t\\'
110 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\'
111 print '1:\tCALL(_x86_get_dispatch) ;\t\t\t\\'
112 print '\tJMP(GL_OFFSET(off))'
113 print '#elif defined(THREADS)'
114 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
115 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
116 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
117 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
118 print '\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\'
119 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\'
120 print '\tJE(1f) ;\t\t\t\t\t\\'
121 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\'
122 print '1:\tCALL(_glapi_get_dispatch) ;\t\t\t\\'
123 print '\tJMP(GL_OFFSET(off))'
124 print '#else /* Non-threaded version. */'
125 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
126 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
127 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
128 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
129 print '\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\'
130 print '\tJMP(GL_OFFSET(off))'
131 print '#endif'
132 print ''
133 print '#ifdef HAVE_ALIAS'
134 print '# define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\'
135 print '\t.globl\tGL_PREFIX(fn, fn_alt) ;\t\t\t\\'
136 print '\t.set\tGL_PREFIX(fn, fn_alt), GL_PREFIX(alias, alias_alt)'
137 print '#else'
138 print '# define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\'
139 print ' GL_STUB(fn, off, fn_alt)'
140 print '#endif'
141 print ''
142 print 'SEG_TEXT'
143 print ''
144 print '#ifdef GLX_USE_TLS'
145 print ''
146 print '\tGLOBL\tGLNAME(_x86_get_dispatch)'
147 print '\tHIDDEN(GLNAME(_x86_get_dispatch))'
148 print 'ALIGNTEXT16'
149 print 'GLNAME(_x86_get_dispatch):'
150 print '\tcall 1f'
151 print '1:\tpopl %eax'
152 print '\taddl $_GLOBAL_OFFSET_TABLE_+[.-1b], %eax'
153 print '\tmovl _glapi_tls_Dispatch@GOTNTPOFF(%eax), %eax'
154 print '\tret'
155 print ''
156 print '#elif defined(PTHREADS)'
157 print 'EXTERN GLNAME(_glapi_Dispatch)'
158 print 'EXTERN GLNAME(_gl_DispatchTSD)'
159 print 'EXTERN GLNAME(pthread_getspecific)'
160 print ''
161 print 'ALIGNTEXT16'
162 print 'GLNAME(_x86_get_dispatch):'
163 print '\tSUB_L(CONST(24), ESP)'
164 print '\tPUSH_L(GLNAME(_gl_DispatchTSD))'
165 print '\tCALL(GLNAME(pthread_getspecific))'
166 print '\tADD_L(CONST(28), ESP)'
167 print '\tRET'
168 print '#elif defined(THREADS)'
169 print 'EXTERN GLNAME(_glapi_get_dispatch)'
170 print '#endif'
171 print ''
172
173 print '#if defined( GLX_USE_TLS ) && !defined( GLX_X86_READONLY_TEXT )'
174 print '\t\t.section\twtext, "awx", @progbits'
175 print '#endif /* defined( GLX_USE_TLS ) */'
176
177 print ''
178 print '\t\tALIGNTEXT16'
179 print '\t\tGLOBL GLNAME(gl_dispatch_functions_start)'
180 print '\t\tHIDDEN(GLNAME(gl_dispatch_functions_start))'
181 print 'GLNAME(gl_dispatch_functions_start):'
182 print ''
183 return
184
185
186 def printRealFooter(self):
187 print ''
188 print '\t\tGLOBL\tGLNAME(gl_dispatch_functions_end)'
189 print '\t\tHIDDEN(GLNAME(gl_dispatch_functions_end))'
190 print '\t\tALIGNTEXT16'
191 print 'GLNAME(gl_dispatch_functions_end):'
192 print ''
193 print '#if defined(GLX_USE_TLS) && defined(__linux__)'
194 print ' .section ".note.ABI-tag", "a"'
195 print ' .p2align 2'
196 print ' .long 1f - 0f /* name length */'
197 print ' .long 3f - 2f /* data length */'
198 print ' .long 1 /* note length */'
199 print '0: .asciz "GNU" /* vendor name */'
200 print '1: .p2align 2'
201 print '2: .long 0 /* note data: the ABI tag */'
202 print ' .long 2,4,20 /* Minimum kernel version w/TLS */'
203 print '3: .p2align 2 /* pad out section */'
204 print '#endif /* GLX_USE_TLS */'
205 print ''
206 print '#if defined (__ELF__) && defined (__linux__)'
207 print ' .section .note.GNU-stack,"",%progbits'
208 print '#endif'
209 return
210
211
212 def printBody(self, api):
213 for f in api.functionIterateByOffset():
214 name = f.dispatch_name()
215 stack = self.get_stack_size(f)
216 alt = "%s@%u" % (name, stack)
217
218 print '\tGL_STUB(%s, _gloffset_%s, %s)' % (name, f.name, alt)
219
220 if not f.is_static_entry_point(f.name):
221 print '\tHIDDEN(GL_PREFIX(%s, %s))' % (name, alt)
222
223
224 for f in api.functionIterateByOffset():
225 name = f.dispatch_name()
226 stack = self.get_stack_size(f)
227 alt = "%s@%u" % (name, stack)
228
229 if f.is_static_entry_point(f.name):
230 for n in f.entry_points:
231 if n != f.name:
232 alt2 = "%s@%u" % (n, stack)
233 text = '\tGL_STUB_ALIAS(%s, _gloffset_%s, %s, %s, %s)' % (n, f.name, alt2, f.name, alt)
234
235 if f.has_different_protocol(n):
236 print '#ifndef GLX_INDIRECT_RENDERING'
237 print text
238 print '#endif'
239 else:
240 print text
241
242 return
243
244 def show_usage():
245 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys.argv[0]
246 sys.exit(1)
247
248 if __name__ == '__main__':
249 file_name = "gl_API.xml"
250 mode = "generic"
251
252 try:
253 (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
254 except Exception,e:
255 show_usage()
256
257 for (arg,val) in args:
258 if arg == '-m':
259 mode = val
260 elif arg == "-f":
261 file_name = val
262
263 if mode == "generic":
264 printer = PrintGenericStubs()
265 else:
266 print "ERROR: Invalid mode \"%s\" specified." % mode
267 show_usage()
268
269 api = gl_XML.parse_GL_API(file_name, glX_XML.glx_item_factory())
270 printer.Print(api)