Home | History | Annotate | Download | only in pydir
      1 def mangle(op, op_type, signed):
      2   # suffixMap gives the C++ name-mangling suffixes for a function that takes two
      3   # arguments of the given type.  The first entry is for the unsigned version of
      4   # the type, and the second entry is for the signed version.
      5   suffixMap = { 'i1': ['bb', 'bb'],
      6                 'i8': ['hh', 'aa'],
      7                 'i16': ['tt', 'ss'],
      8                 'i32': ['jj', 'ii'],
      9                 'i64': ['yy', 'xx'],
     10                 'float': ['ff', 'ff'],
     11                 'double': ['dd', 'dd'],
     12                 '<4 x i32>': ['Dv4_jS_', 'Dv4_iS_'],
     13                 '<8 x i16>': ['Dv8_tS_', 'Dv8_sS_'],
     14                 '<16 x i8>': ['Dv16_hS_', 'Dv16_aS_'],
     15                 '<4 x float>': ['Dv4_fS_', 'Dv4_fS_'],
     16               }
     17   base = 'test' + op.capitalize()
     18   return '_Z' + str(len(base)) + base + suffixMap[op_type][signed]
     19 
     20 def arith(Native, Type, Op):
     21   _TEMPLATE_ = """
     22 define internal {{native}} @{{name}}({{native}} %a, {{native}} %b) {{{{
     23   {trunc_a}
     24   {trunc_b}
     25   %result{{trunc}} = {{op}} {{type}} %a{{trunc}}, %b{{trunc}}
     26   {zext}
     27   ret {{native}} %result
     28 }}}}"""
     29 
     30   Signed = Op in {'sdiv', 'srem', 'ashr'}
     31   Name = mangle(Op, Type, Signed)
     32   # Most i1 operations are invalid for PNaCl, so map them to i32.
     33   if Type == 'i1' and (Op not in {'and', 'or', 'xor'}):
     34     Type = 'i32'
     35   x = _TEMPLATE_.format(
     36       trunc_a = '%a.trunc = trunc {native} %a to {type}' if
     37           Native != Type else '',
     38       trunc_b = '%b.trunc = trunc {native} %b to {type}' if
     39           Native != Type else '',
     40       zext = '%result = ' + ('sext' if Signed else 'zext') +
     41           ' {type} %result.trunc to {native}' if Native != Type else '')
     42   lines = x.format(native=Native, type=Type, op=Op, name=Name,
     43                    trunc='.trunc' if Native != Type else '')
     44   # Strip trailing whitespace from each line to keep git happy.
     45   print '\n'.join([line.rstrip() for line in lines.splitlines()])
     46 
     47 for op in ['add', 'sub', 'mul', 'sdiv', 'udiv', 'srem', 'urem', 'shl', 'lshr',
     48            'ashr', 'and', 'or', 'xor']:
     49   for op_type in ['i1', 'i8', 'i16', 'i32']:
     50     arith('i32', op_type, op)
     51   for op_type in ['i64', '<4 x i32>', '<8 x i16>', '<16 x i8>']:
     52     arith(op_type, op_type, op)
     53 
     54 for op in ['fadd', 'fsub', 'fmul', 'fdiv', 'frem']:
     55   for op_type in ['float', 'double', '<4 x float>']:
     56     arith(op_type, op_type, op)
     57