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 28import argparse 29 30import license 31import gl_XML, glX_XML 32 33class PrintGenericStubs(gl_XML.gl_print_base): 34 35 def __init__(self): 36 gl_XML.gl_print_base.__init__(self) 37 38 self.name = "gl_x86_asm.py (from Mesa)" 39 self.license = license.bsd_license_template % ( \ 40"""Copyright (C) 1999-2001 Brian Paul All Rights Reserved. 41(C) Copyright IBM Corporation 2004, 2005""", "BRIAN PAUL, IBM") 42 return 43 44 45 def get_stack_size(self, f): 46 size = 0 47 for p in f.parameterIterator(): 48 if p.is_padding: 49 continue 50 51 size += p.get_stack_size() 52 53 return size 54 55 56 def printRealHeader(self): 57 print '#include "x86/assyntax.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(__MINGW32__) && !defined(__APPLE__)' 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 '' 83 print '#ifdef GLX_USE_TLS' 84 print '' 85 print '#ifdef GLX_X86_READONLY_TEXT' 86 print '# define CTX_INSNS MOV_L(GS:(EAX), EAX)' 87 print '#else' 88 print '# define CTX_INSNS NOP /* Pad for init_glapi_relocs() */' 89 print '#endif' 90 print '' 91 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\' 92 print 'ALIGNTEXT16;\t\t\t\t\t\t\\' 93 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\' 94 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\' 95 print '\tCALL(_x86_get_dispatch) ;\t\t\t\\' 96 print '\tCTX_INSNS ; \\' 97 print '\tJMP(GL_OFFSET(off))' 98 print '' 99 print '#elif defined(HAVE_PTHREAD)' 100 print '# define GL_STUB(fn,off,fn_alt)\t\t\t\\' 101 print 'ALIGNTEXT16;\t\t\t\t\t\t\\' 102 print 'GLOBL_FN(GL_PREFIX(fn, fn_alt));\t\t\t\\' 103 print 'GL_PREFIX(fn, fn_alt):\t\t\t\t\t\\' 104 print '\tMOV_L(CONTENT(GLNAME(_glapi_Dispatch)), EAX) ;\t\\' 105 print '\tTEST_L(EAX, EAX) ;\t\t\t\t\\' 106 print '\tJE(1f) ;\t\t\t\t\t\\' 107 print '\tJMP(GL_OFFSET(off)) ;\t\t\t\t\\' 108 print '1:\tCALL(_x86_get_dispatch) ;\t\t\t\\' 109 print '\tJMP(GL_OFFSET(off))' 110 print '#else' 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_Dispatch)), 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(_glapi_get_dispatch) ;\t\t\t\\' 120 print '\tJMP(GL_OFFSET(off))' 121 print '#endif' 122 print '' 123 print '#ifdef HAVE_FUNC_ATTRIBUTE_ALIAS' 124 print '# define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\' 125 print '\t.globl\tGL_PREFIX(fn, fn_alt) ;\t\t\t\\' 126 print '\t.set\tGL_PREFIX(fn, fn_alt), GL_PREFIX(alias, alias_alt)' 127 print '#else' 128 print '# define GL_STUB_ALIAS(fn,off,fn_alt,alias,alias_alt)\t\\' 129 print ' GL_STUB(fn, off, fn_alt)' 130 print '#endif' 131 print '' 132 print 'SEG_TEXT' 133 print '' 134 print '#ifdef GLX_USE_TLS' 135 print '' 136 print '\tGLOBL\tGLNAME(_x86_get_dispatch)' 137 print '\tHIDDEN(GLNAME(_x86_get_dispatch))' 138 print 'ALIGNTEXT16' 139 print 'GLNAME(_x86_get_dispatch):' 140 print '\tcall 1f' 141 print '1:\tpopl %eax' 142 print '\taddl $_GLOBAL_OFFSET_TABLE_+[.-1b], %eax' 143 print '\tmovl _glapi_tls_Dispatch@GOTNTPOFF(%eax), %eax' 144 print '\tret' 145 print '' 146 print '#elif defined(HAVE_PTHREAD)' 147 print 'EXTERN GLNAME(_glapi_Dispatch)' 148 print 'EXTERN GLNAME(_gl_DispatchTSD)' 149 print 'EXTERN GLNAME(pthread_getspecific)' 150 print '' 151 print 'ALIGNTEXT16' 152 print 'GLNAME(_x86_get_dispatch):' 153 print '\tSUB_L(CONST(24), ESP)' 154 print '\tPUSH_L(GLNAME(_gl_DispatchTSD))' 155 print '\tCALL(GLNAME(pthread_getspecific))' 156 print '\tADD_L(CONST(28), ESP)' 157 print '\tRET' 158 print '#else' 159 print 'EXTERN GLNAME(_glapi_get_dispatch)' 160 print '#endif' 161 print '' 162 163 print '#if defined( GLX_USE_TLS ) && !defined( GLX_X86_READONLY_TEXT )' 164 print '\t\t.section\twtext, "awx", @progbits' 165 print '#endif /* defined( GLX_USE_TLS ) */' 166 167 print '' 168 print '\t\tALIGNTEXT16' 169 print '\t\tGLOBL GLNAME(gl_dispatch_functions_start)' 170 print '\t\tHIDDEN(GLNAME(gl_dispatch_functions_start))' 171 print 'GLNAME(gl_dispatch_functions_start):' 172 print '' 173 return 174 175 176 def printRealFooter(self): 177 print '' 178 print '\t\tGLOBL\tGLNAME(gl_dispatch_functions_end)' 179 print '\t\tHIDDEN(GLNAME(gl_dispatch_functions_end))' 180 print '\t\tALIGNTEXT16' 181 print 'GLNAME(gl_dispatch_functions_end):' 182 print '' 183 print '#if defined (__ELF__) && defined (__linux__)' 184 print ' .section .note.GNU-stack,"",%progbits' 185 print '#endif' 186 return 187 188 189 def printBody(self, api): 190 for f in api.functionIterateByOffset(): 191 name = f.dispatch_name() 192 stack = self.get_stack_size(f) 193 alt = "%s@%u" % (name, stack) 194 195 print '\tGL_STUB(%s, %d, %s)' % (name, f.offset, alt) 196 197 if not f.is_static_entry_point(f.name): 198 print '\tHIDDEN(GL_PREFIX(%s, %s))' % (name, alt) 199 200 201 for f in api.functionIterateByOffset(): 202 name = f.dispatch_name() 203 stack = self.get_stack_size(f) 204 alt = "%s@%u" % (name, stack) 205 206 for n in f.entry_points: 207 if f.is_static_entry_point(n): 208 if n != f.name: 209 alt2 = "%s@%u" % (n, stack) 210 text = '\tGL_STUB_ALIAS(%s, %d, %s, %s, %s)' % (n, f.offset, alt2, name, alt) 211 212 if f.has_different_protocol(n): 213 print '#ifndef GLX_INDIRECT_RENDERING' 214 print text 215 print '#endif' 216 else: 217 print text 218 219 return 220 221def _parser(): 222 parser = argparse.ArgumentParser() 223 parser.add_argument('-f', 224 dest='filename', 225 default='gl_API.xml', 226 help='An XML file describing an API.') 227 return parser.parse_args() 228 229 230def main(): 231 args = _parser() 232 printer = PrintGenericStubs() 233 234 api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory()) 235 printer.Print(api) 236 237 238if __name__ == '__main__': 239 main() 240