Home | History | Annotate | Download | only in gen
      1 #!/usr/bin/env python
      2 
      3 # Copyright (C) 2009 Chia-I Wu <olv (at] 0xlab.org>
      4 # All Rights Reserved.
      5 #
      6 # This is based on extension_helper.py by Ian Romanick.
      7 #
      8 # Permission is hereby granted, free of charge, to any person obtaining a
      9 # copy of this software and associated documentation files (the "Software"),
     10 # to deal in the Software without restriction, including without limitation
     11 # on the rights to use, copy, modify, merge, publish, distribute, sub
     12 # license, and/or sell copies of the Software, and to permit persons to whom
     13 # the Software is furnished to do so, subject to the following conditions:
     14 #
     15 # The above copyright notice and this permission notice (including the next
     16 # paragraph) shall be included in all copies or substantial portions of the
     17 # Software.
     18 #
     19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     21 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
     22 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     24 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     25 # IN THE SOFTWARE.
     26 
     27 import argparse
     28 
     29 import license
     30 import gl_XML
     31 
     32 
     33 def get_function_spec(func):
     34     sig = ""
     35     # derive parameter signature
     36     for p in func.parameterIterator():
     37         if p.is_padding:
     38             continue
     39         # FIXME: This is a *really* ugly hack. :(
     40         tn = p.type_expr.get_base_type_node()
     41         if p.is_pointer():
     42             sig += 'p'
     43         elif tn.integer:
     44             sig += 'i'
     45         elif tn.size == 4:
     46             sig += 'f'
     47         else:
     48             sig += 'd'
     49 
     50     spec = [sig]
     51     for ent in func.entry_points:
     52         spec.append("gl" + ent)
     53 
     54     # spec is terminated by an empty string
     55     spec.append('')
     56 
     57     return spec
     58 
     59 
     60 class PrintGlRemap(gl_XML.gl_print_base):
     61     def __init__(self):
     62         gl_XML.gl_print_base.__init__(self)
     63 
     64         self.name = "remap_helper.py (from Mesa)"
     65         self.license = license.bsd_license_template % ("Copyright (C) 2009 Chia-I Wu <olv (at] 0xlab.org>", "Chia-I Wu")
     66         return
     67 
     68 
     69     def printRealHeader(self):
     70         print '#include "main/dispatch.h"'
     71         print '#include "main/remap.h"'
     72         print ''
     73         return
     74 
     75 
     76     def printBody(self, api):
     77         pool_indices = {}
     78 
     79         print '/* this is internal to remap.c */'
     80         print '#ifndef need_MESA_remap_table'
     81         print '#error Only remap.c should include this file!'
     82         print '#endif /* need_MESA_remap_table */'
     83         print ''
     84 
     85         print ''
     86         print 'static const char _mesa_function_pool[] ='
     87 
     88         # output string pool
     89         index = 0;
     90         for f in api.functionIterateAll():
     91             pool_indices[f] = index
     92 
     93             spec = get_function_spec(f)
     94 
     95             # a function has either assigned offset, fixed offset,
     96             # or no offset
     97             if f.assign_offset:
     98                 comments = "will be remapped"
     99             elif f.offset > 0:
    100                 comments = "offset %d" % f.offset
    101             else:
    102                 comments = "dynamic"
    103 
    104             print '   /* _mesa_function_pool[%d]: %s (%s) */' \
    105                             % (index, f.name, comments)
    106             for line in spec:
    107                 print '   "%s\\0"' % line
    108                 index += len(line) + 1
    109         print '   ;'
    110         print ''
    111 
    112         print '/* these functions need to be remapped */'
    113         print 'static const struct gl_function_pool_remap MESA_remap_table_functions[] = {'
    114         # output all functions that need to be remapped
    115         # iterate by offsets so that they are sorted by remap indices
    116         for f in api.functionIterateByOffset():
    117             if not f.assign_offset:
    118                 continue
    119             print '   { %5d, %s_remap_index },' \
    120                             % (pool_indices[f], f.name)
    121         print '   {    -1, -1 }'
    122         print '};'
    123         print ''
    124         return
    125 
    126 
    127 def _parser():
    128     """Parse input options and return a namsepace."""
    129     parser = argparse.ArgumentParser()
    130     parser.add_argument('-f', '--filename',
    131                         default="gl_API.xml",
    132                         metavar="input_file_name",
    133                         dest='file_name',
    134                         help="An xml description file.")
    135     return parser.parse_args()
    136 
    137 
    138 def main():
    139     """Main function."""
    140     args = _parser()
    141 
    142     api = gl_XML.parse_GL_API(args.file_name)
    143 
    144     printer = PrintGlRemap()
    145     printer.Print(api)
    146 
    147 
    148 if __name__ == '__main__':
    149     main()
    150