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