1 2# (C) Copyright IBM Corporation 2005 3# All Rights Reserved. 4# 5# Permission is hereby granted, free of charge, to any person obtaining a 6# copy of this software and associated documentation files (the "Software"), 7# to deal in the Software without restriction, including without limitation 8# on the rights to use, copy, modify, merge, publish, distribute, sub 9# license, and/or sell copies of the Software, and to permit persons to whom 10# the Software is furnished to do so, subject to the following conditions: 11# 12# The above copyright notice and this permission notice (including the next 13# paragraph) shall be included in all copies or substantial portions of the 14# Software. 15# 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 19# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22# IN THE SOFTWARE. 23# 24# Authors: 25# Ian Romanick <idr@us.ibm.com> 26 27import argparse 28import copy 29 30import license 31import gl_XML, glX_XML 32 33def should_use_push(registers): 34 for [reg, offset] in registers: 35 if reg[1:4] == "xmm": 36 return 0 37 38 N = len(registers) 39 return (N & 1) != 0 40 41 42def local_size(registers): 43 # The x86-64 ABI says "the value (%rsp - 8) is always a multiple of 44 # 16 when control is transfered to the function entry point." This 45 # means that the local stack usage must be (16*N)+8 for some value 46 # of N. (16*N)+8 = (8*(2N))+8 = 8*(2N+1). As long as N is odd, we 47 # meet this requirement. 48 49 N = (len(registers) | 1) 50 return 8*N 51 52 53def save_all_regs(registers): 54 adjust_stack = 0 55 if not should_use_push(registers): 56 adjust_stack = local_size(registers) 57 print('\tsubq\t$%u, %%rsp' % (adjust_stack)) 58 59 for [reg, stack_offset] in registers: 60 save_reg( reg, stack_offset, adjust_stack ) 61 return 62 63 64def restore_all_regs(registers): 65 adjust_stack = 0 66 if not should_use_push(registers): 67 adjust_stack = local_size(registers) 68 69 temp = copy.deepcopy(registers) 70 while len(temp): 71 [reg, stack_offset] = temp.pop() 72 restore_reg(reg, stack_offset, adjust_stack) 73 74 if adjust_stack: 75 print('\taddq\t$%u, %%rsp' % (adjust_stack)) 76 return 77 78 79def save_reg(reg, offset, use_move): 80 if use_move: 81 if offset == 0: 82 print('\tmovq\t%s, (%%rsp)' % (reg)) 83 else: 84 print('\tmovq\t%s, %u(%%rsp)' % (reg, offset)) 85 else: 86 print('\tpushq\t%s' % (reg)) 87 88 return 89 90 91def restore_reg(reg, offset, use_move): 92 if use_move: 93 if offset == 0: 94 print('\tmovq\t(%%rsp), %s' % (reg)) 95 else: 96 print('\tmovq\t%u(%%rsp), %s' % (offset, reg)) 97 else: 98 print('\tpopq\t%s' % (reg)) 99 100 return 101 102 103class PrintGenericStubs(gl_XML.gl_print_base): 104 105 def __init__(self): 106 gl_XML.gl_print_base.__init__(self) 107 108 self.name = "gl_x86-64_asm.py (from Mesa)" 109 self.license = license.bsd_license_template % ("(C) Copyright IBM Corporation 2005", "IBM") 110 return 111 112 113 def get_stack_size(self, f): 114 size = 0 115 for p in f.parameterIterator(): 116 size += p.get_stack_size() 117 118 return size 119 120 121 def printRealHeader(self): 122 print("/* If we build with gcc's -fvisibility=hidden flag, we'll need to change") 123 print(" * the symbol visibility mode to 'default'.") 124 print(' */') 125 print('') 126 print('#include "x86/assyntax.h"') 127 print('') 128 print('#ifdef __GNUC__') 129 print('# pragma GCC visibility push(default)') 130 print('# define HIDDEN(x) .hidden x') 131 print('#else') 132 print('# define HIDDEN(x)') 133 print('#endif') 134 print('') 135 print('# define GL_PREFIX(n) GLNAME(CONCAT(gl,n))') 136 print('') 137 print('\t.text') 138 print('') 139 print('#ifdef USE_ELF_TLS') 140 print('') 141 print('_x86_64_get_dispatch:') 142 print('\tmovq\t_glapi_tls_Dispatch@GOTTPOFF(%rip), %rax') 143 print('\tmovq\t%fs:(%rax), %rax') 144 print('\tret') 145 print('\t.size\t_x86_64_get_dispatch, .-_x86_64_get_dispatch') 146 print('') 147 print('#elif defined(HAVE_PTHREAD)') 148 print('') 149 print('\t.extern\t_glapi_Dispatch') 150 print('\t.extern\t_gl_DispatchTSD') 151 print('\t.extern\tpthread_getspecific') 152 print('') 153 print('\t.p2align\t4,,15') 154 print('_x86_64_get_dispatch:') 155 print('\tmovq\t_gl_DispatchTSD@GOTPCREL(%rip), %rax') 156 print('\tmovl\t(%rax), %edi') 157 print('\tjmp\tpthread_getspecific@PLT') 158 print('') 159 print('#else') 160 print('') 161 print('\t.extern\t_glapi_get_dispatch') 162 print('') 163 print('#endif') 164 print('') 165 return 166 167 168 def printRealFooter(self): 169 print('') 170 print('#if defined (__ELF__) && defined (__linux__)') 171 print(' .section .note.GNU-stack,"",%progbits') 172 print('#endif') 173 return 174 175 176 def printFunction(self, f): 177 178 # The x86-64 ABI divides function parameters into a couple 179 # classes. For the OpenGL interface, the only ones that are 180 # relevant are INTEGER and SSE. Basically, the first 8 181 # GLfloat or GLdouble parameters are placed in %xmm0 - %xmm7, 182 # the first 6 non-GLfloat / non-GLdouble parameters are placed 183 # in registers listed in int_parameters. 184 # 185 # If more parameters than that are required, they are passed 186 # on the stack. Therefore, we just have to make sure that 187 # %esp hasn't changed when we jump to the actual function. 188 # Since we're jumping to the function (and not calling it), we 189 # have to make sure of that anyway! 190 191 int_parameters = ["%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"] 192 193 int_class = 0 194 sse_class = 0 195 stack_offset = 0 196 registers = [] 197 for p in f.parameterIterator(): 198 type_name = p.get_base_type_string() 199 200 if p.is_pointer() or (type_name != "GLfloat" and type_name != "GLdouble"): 201 if int_class < 6: 202 registers.append( [int_parameters[int_class], stack_offset] ) 203 int_class += 1 204 stack_offset += 8 205 else: 206 if sse_class < 8: 207 registers.append( ["%%xmm%u" % (sse_class), stack_offset] ) 208 sse_class += 1 209 stack_offset += 8 210 211 if ((int_class & 1) == 0) and (sse_class == 0): 212 registers.append( ["%rbp", 0] ) 213 214 215 name = f.dispatch_name() 216 217 print('\t.p2align\t4,,15') 218 print('\t.globl\tGL_PREFIX(%s)' % (name)) 219 print('\t.type\tGL_PREFIX(%s), @function' % (name)) 220 if not f.is_static_entry_point(f.name): 221 print('\tHIDDEN(GL_PREFIX(%s))' % (name)) 222 print('GL_PREFIX(%s):' % (name)) 223 print('#if defined(USE_ELF_TLS)') 224 print('\tcall\t_x86_64_get_dispatch@PLT') 225 print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)) 226 print('\tjmp\t*%r11') 227 print('#elif defined(HAVE_PTHREAD)') 228 229 save_all_regs(registers) 230 print('\tcall\t_x86_64_get_dispatch@PLT') 231 restore_all_regs(registers) 232 233 if f.offset == 0: 234 print('\tmovq\t(%rax), %r11') 235 else: 236 print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)) 237 238 print('\tjmp\t*%r11') 239 240 print('#else') 241 print('\tmovq\t_glapi_Dispatch(%rip), %rax') 242 print('\ttestq\t%rax, %rax') 243 print('\tje\t1f') 244 print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)) 245 print('\tjmp\t*%r11') 246 print('1:') 247 248 save_all_regs(registers) 249 print('\tcall\t_glapi_get_dispatch') 250 restore_all_regs(registers) 251 252 print('\tmovq\t%u(%%rax), %%r11' % (f.offset * 8)) 253 print('\tjmp\t*%r11') 254 print('#endif /* defined(USE_ELF_TLS) */') 255 256 print('\t.size\tGL_PREFIX(%s), .-GL_PREFIX(%s)' % (name, name)) 257 print('') 258 return 259 260 261 def printBody(self, api): 262 for f in api.functionIterateByOffset(): 263 self.printFunction(f) 264 265 266 for f in api.functionIterateByOffset(): 267 dispatch = f.dispatch_name() 268 for n in f.entry_points: 269 if n != f.name: 270 if f.is_static_entry_point(n): 271 text = '\t.globl GL_PREFIX(%s) ; .set GL_PREFIX(%s), GL_PREFIX(%s)' % (n, n, dispatch) 272 273 if f.has_different_protocol(n): 274 print('#ifndef GLX_INDIRECT_RENDERING') 275 print(text) 276 print('#endif') 277 else: 278 print(text) 279 280 return 281 282 283def _parser(): 284 """Parse arguments and return a namespace.""" 285 parser = argparse.ArgumentParser() 286 parser.add_argument('-f', 287 default='gl_API.xml', 288 dest='filename', 289 help='An XML file describing an API') 290 return parser.parse_args() 291 292 293def main(): 294 """Main file.""" 295 args = _parser() 296 printer = PrintGenericStubs() 297 api = gl_XML.parse_GL_API(args.filename, glX_XML.glx_item_factory()) 298 299 printer.Print(api) 300 301 302if __name__ == '__main__': 303 main() 304