Add TLS support to libGL and, by virtue of using glthread.h and GL_CALL, all
[mesa.git] / src / mesa / glapi / gl_x86_asm.py
1 #!/usr/bin/python2
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 from xml.sax import saxutils
29 from xml.sax import make_parser
30 from xml.sax.handler import feature_namespaces
31
32 import gl_XML
33 import license
34 import sys, getopt
35
36 class PrintGenericStubs(gl_XML.FilterGLAPISpecBase):
37 name = "gl_x86_asm.py (from Mesa)"
38
39 def __init__(self):
40 gl_XML.FilterGLAPISpecBase.__init__(self)
41 self.license = license.bsd_license_template % ( \
42 """Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
43 (C) Copyright IBM Corporation 2004, 2005""", "BRIAN PAUL, IBM")
44
45
46 def get_stack_size(self, f):
47 size = 0
48 for p in f.parameterIterator():
49 t = p.p_type
50
51 if p.is_array() or t.size != 8:
52 size += 4
53 else:
54 size += 8
55
56 return size
57
58 def printRealHeader(self):
59 print '#include "assyntax.h"'
60 print '#include "glapioffsets.h"'
61 print ''
62 print "/* If we build with gcc's -fvisibility=hidden flag, we'll need to change"
63 print "* the symbol visibility mode to 'default'."
64 print '*/'
65 print '#if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 303'
66 print '# pragma GCC visibility push(default)'
67 print '# define HIDDEN(x) .hidden x'
68 print '#else'
69 print '# define HIDDEN(x)'
70 print '#endif'
71 print ''
72 print '#ifndef __WIN32__'
73 print ''
74 print '#if defined(STDCALL_API)'
75 print '# if defined(USE_MGL_NAMESPACE)'
76 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(mgl,n2))'
77 print '# else'
78 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n2))'
79 print '# endif'
80 print '#else'
81 print '# if defined(USE_MGL_NAMESPACE)'
82 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(mgl,n))'
83 print '# else'
84 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n))'
85 print '# endif'
86 print '#endif'
87 print ''
88 print '#define GL_OFFSET(x) CODEPTR(REGOFF(4 * x, EAX))'
89 print ''
90 print '#if defined(GNU_ASSEMBLER) && !defined(__DJGPP__) && !defined(__MINGW32__)'
91 print '#define GLOBL_FN(x) GLOBL x ; .type x, function'
92 print '#else'
93 print '#define GLOBL_FN(x) GLOBL x'
94 print '#endif'
95 print ''
96 print '#if defined(PTHREADS) || defined(XTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)'
97 print '# define THREADS'
98 print '#endif'
99 print ''
100 print '#ifdef GLX_USE_TLS'
101 print ''
102 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
103 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
104 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
105 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
106 print '\tCALL(_x86_get_dispatch) ;\t\t\t\\'
107 print '\tNOP ;\t\t\t\t\t\t\\'
108 print '\tJMP(GL_OFFSET(off))'
109 print ''
110 print '#elif defined(PTHREADS)'
111 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
112 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
113 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
114 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
115 print '\tMOV_L(CONTENT(GLNAME(_glapi_DispatchTSD)), EAX) ;\t\\'
116 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\'
117 print '\tJE(1f) ;\t\t\t\t\t\\'
118 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\'
119 print '1:\tCALL(_x86_get_dispatch) ;\t\t\t\\'
120 print '\tJMP(GL_OFFSET(off))'
121 print '#elif defined(THREADS)'
122 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
123 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
124 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
125 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
126 print '\tMOV_L(CONTENT(GLNAME(_glapi_DispatchTSD)), EAX) ;\t\\'
127 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\'
128 print '\tJE(1f) ;\t\t\t\t\t\\'
129 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\'
130 print '1:\tCALL(_glapi_get_dispatch) ;\t\t\t\\'
131 print '\tJMP(GL_OFFSET(off))'
132 print '#else /* Non-threaded version. */'
133 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
134 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
135 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
136 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
137 print '\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\'
138 print '\tJMP(GL_OFFSET(off))'
139 print '#endif'
140 print ''
141 print '#ifdef HAVE_ALIAS'
142 print '# define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\'
143 print '\t.globl\tGL_PREFIX(fn, fn_alt) ;\t\t\t\\'
144 print '\t.set\tGL_PREFIX(fn, fn_alt), GL_PREFIX(alias, alias_alt)'
145 print '#else'
146 print '# define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\'
147 print ' GL_STUB(fn, off, fn_alt)'
148 print '#endif'
149 print ''
150 print 'SEG_TEXT'
151 print ''
152 print '#ifdef GLX_USE_TLS'
153 print ''
154 print '\tGLOBL\tGLNAME(_x86_get_dispatch)'
155 print '\tHIDDEN(GLNAME(_x86_get_dispatch))'
156 print 'ALIGNTEXT16'
157 print 'GLNAME(_x86_get_dispatch):'
158 print '\tmovl\t%gs:_glapi_tls_Dispatch@NTPOFF, %eax'
159 print '\tret'
160 print ''
161 print '#elif defined(PTHREADS)'
162 print 'EXTERN GLNAME(_glapi_Dispatch)'
163 print 'EXTERN GLNAME(_gl_DispatchTSD)'
164 print 'EXTERN GLNAME(pthread_getspecific)'
165 print ''
166 print 'ALIGNTEXT16'
167 print 'GLNAME(_x86_get_dispatch):'
168 print '\tSUB_L(CONST(24), ESP)'
169 print '\tPUSH_L(GLNAME(_gl_DispatchTSD))'
170 print '\tCALL(GLNAME(pthread_getspecific))'
171 print '\tADD_L(CONST(28), ESP)'
172 print '\tRET'
173 print '#elif defined(THREADS)'
174 print 'EXTERN GLNAME(_glapi_get_dispatch)'
175 print '#endif'
176 print ''
177
178 print '#if defined( GLX_USE_TLS )'
179 print '\t\t.section\twtext, "awx", @progbits'
180 print '#endif /* defined( GLX_USE_TLS ) */'
181
182 print ''
183 print '\t\tALIGNTEXT16'
184 print '\t\tGLOBL GLNAME(gl_dispatch_functions_start)'
185 print '\t\tHIDDEN(GLNAME(gl_dispatch_functions_start))'
186 print 'GLNAME(gl_dispatch_functions_start):'
187 print ''
188 return
189
190 def printRealFooter(self):
191 print ''
192 print '\t\tGLOBL\tGLNAME(gl_dispatch_functions_end)'
193 print '\t\tHIDDEN(GLNAME(gl_dispatch_functions_end))'
194 print '\t\tALIGNTEXT16'
195 print 'GLNAME(gl_dispatch_functions_end):'
196 print ''
197 print '#if defined(GLX_USE_TLS) && defined(__linux__)'
198 print ' .section ".note.ABI-tag", "a"'
199 print ' .p2align 2'
200 print ' .long 1f - 0f /* name length */'
201 print ' .long 3f - 2f /* data length */'
202 print ' .long 1 /* note length */'
203 print '0: .asciz "GNU" /* vendor name */'
204 print '1: .p2align 2'
205 print '2: .long 0 /* note data: the ABI tag */'
206 print ' .long 2,4,20 /* Minimum kernel version w/TLS */'
207 print '3: .p2align 2 /* pad out section */'
208 print '#endif /* GLX_USE_TLS */'
209 print ''
210 print '#endif /* __WIN32__ */'
211 return
212
213 def printFunction(self, f):
214 stack = self.get_stack_size(f)
215
216 alt = "%s@%u" % (f.name, stack)
217 if f.fn_alias == None:
218 print '\tGL_STUB(%s, _gloffset_%s, %s)' % (f.name, f.real_name, alt)
219 else:
220 alias_alt = "%s@%u" % (f.real_name, stack)
221 print '\tGL_STUB_ALIAS(%s, _gloffset_%s, %s, %s, %s)' % \
222 (f.name, f.real_name, alt, f.real_name, alias_alt)
223 return
224
225 def show_usage():
226 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys.argv[0]
227 sys.exit(1)
228
229 if __name__ == '__main__':
230 file_name = "gl_API.xml"
231 mode = "generic"
232
233 try:
234 (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
235 except Exception,e:
236 show_usage()
237
238 for (arg,val) in args:
239 if arg == '-m':
240 mode = val
241 elif arg == "-f":
242 file_name = val
243
244 if mode == "generic":
245 dh = PrintGenericStubs()
246 else:
247 print "ERROR: Invalid mode \"%s\" specified." % mode
248 show_usage()
249
250 parser = make_parser()
251 parser.setFeature(feature_namespaces, 0)
252 parser.setContentHandler(dh)
253
254 f = open(file_name)
255
256 dh.printHeader()
257 parser.parse(f)
258 dh.printFooter()