f87065a5af2c2c7cfa29c1191e4d0c7e863ceafb
[mesa.git] / src / mesa / glapi / gl_x86_asm.py
1 #!/usr/bin/python2
2
3 # (C) Copyright IBM Corporation 2004
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""", "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 '#endif'
68 print ''
69 print '#ifndef __WIN32__'
70 print ''
71 print '#if defined(STDCALL_API)'
72 print '# if defined(USE_MGL_NAMESPACE)'
73 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(mgl,n2))'
74 print '# else'
75 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n2))'
76 print '# endif'
77 print '#else'
78 print '# if defined(USE_MGL_NAMESPACE)'
79 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(mgl,n))'
80 print '# else'
81 print '# define GL_PREFIX(n,n2) GLNAME(CONCAT(gl,n))'
82 print '# endif'
83 print '#endif'
84 print ''
85 print '#define GL_OFFSET(x) CODEPTR(REGOFF(4 * x, EAX))'
86 print ''
87 print '#if defined(GNU_ASSEMBLER) && !defined(__DJGPP__) && !defined(__MINGW32__)'
88 print '#define GLOBL_FN(x) GLOBL x ; .type x, function'
89 print '#else'
90 print '#define GLOBL_FN(x) GLOBL x'
91 print '#endif'
92 print ''
93 print '#if defined(PTHREADS) || defined(XTHREADS) || defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || defined(BEOS_THREADS)'
94 print '# define THREADS'
95 print '#endif'
96 print ''
97 print '#if defined(PTHREADS)'
98 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
99 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
100 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
101 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
102 print '\tMOV_L(CONTENT(GLNAME(_glapi_DispatchTSD)), EAX) ;\t\\'
103 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\'
104 print '\tJE(1f) ;\t\t\t\t\t\\'
105 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\'
106 print '1:\tCALL(_x86_get_dispatch) ;\t\t\t\\'
107 print '\tJMP(GL_OFFSET(off))'
108 print '#elif defined(THREADS)'
109 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
110 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
111 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
112 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
113 print '\tMOV_L(CONTENT(GLNAME(_glapi_DispatchTSD)), EAX) ;\t\\'
114 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\'
115 print '\tJE(1f) ;\t\t\t\t\t\\'
116 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\'
117 print '1:\tCALL(_glapi_get_dispatch) ;\t\t\t\\'
118 print '\tJMP(GL_OFFSET(off))'
119 print '#else /* Non-threaded version. */'
120 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\'
121 print 'ALIGNTEXT16;\t\t\t\t\t\t\\'
122 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\'
123 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\'
124 print '\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\'
125 print '\tJMP(GL_OFFSET(off))'
126 print '#endif'
127 print ''
128 print 'SEG_TEXT'
129 print ''
130 print '#ifdef PTHREADS'
131 print 'EXTERN GLNAME(_glapi_Dispatch)'
132 print 'EXTERN GLNAME(_gl_DispatchTSD)'
133 print 'EXTERN GLNAME(pthread_getspecific)'
134 print ''
135 print 'ALIGNTEXT16'
136 print 'GLNAME(_x86_get_dispatch):'
137 print '\tSUB_L(CONST(24), ESP)'
138 print '\tPUSH_L(GLNAME(_gl_DispatchTSD))'
139 print '\tCALL(GLNAME(pthread_getspecific))'
140 print '\tADD_L(CONST(28), ESP)'
141 print '\tRET'
142 print '#elif defined(THREADS)'
143 print 'EXTERN GLNAME(_glapi_get_dispatch)'
144 print '#endif'
145 print ''
146 print '\t\tALIGNTEXT16 ; GLOBL GLNAME(gl_dispatch_functions_start)'
147 print 'GLNAME(gl_dispatch_functions_start):'
148 print ''
149 return
150
151 def printRealFooter(self):
152 print ''
153 print '#endif /* __WIN32__ */'
154 return
155
156 def printFunction(self, f):
157 stack = self.get_stack_size(f)
158
159 alt = "%s@%u" % (f.name, stack)
160 print '\tGL_STUB(%s, _gloffset_%s, %s)' % (f.name, f.real_name, alt)
161 return
162
163 def show_usage():
164 print "Usage: %s [-f input_file_name] [-m output_mode]" % sys.argv[0]
165 sys.exit(1)
166
167 if __name__ == '__main__':
168 file_name = "gl_API.xml"
169 mode = "generic"
170
171 try:
172 (args, trail) = getopt.getopt(sys.argv[1:], "m:f:")
173 except Exception,e:
174 show_usage()
175
176 for (arg,val) in args:
177 if arg == '-m':
178 mode = val
179 elif arg == "-f":
180 file_name = val
181
182 if mode == "generic":
183 dh = PrintGenericStubs()
184 else:
185 print "ERROR: Invalid mode \"%s\" specified." % mode
186 show_usage()
187
188 parser = make_parser()
189 parser.setFeature(feature_namespaces, 0)
190 parser.setContentHandler(dh)
191
192 f = open(file_name)
193
194 dh.printHeader()
195 parser.parse(f)
196 dh.printFooter()