def reverse_expr(n, datatype): 'b_i = a_{n-i}' def item(name): return ir.Struct_item(ir.Primitive_type(datatype), (name)) t = ir.Struct(ir.Scoped_id([("s")], ('Vector'), ''), [ir.Struct_item(ir.Primitive_type(datatype), ('m%d'%i)) for i in range(1, n+1)], '') revs = [ir.Stmt(ir.Set_struct_item(t, (ir.deref, "b"), item('m%d'%i), ir.Get_struct_item(t, (ir.deref, "a"), item('m%d'%(n-i+1))))) for i in range(1, n+1)] return revs+[ir.Stmt(ir.Return(retval(n, datatype)))]
def const_access_expr(n, datatype): def add(a, b): return ir.Plus(a, b) def item(name): return ir.Struct_item(ir.Primitive_type(datatype), (name)) t = ir.Struct(ir.Scoped_id([("s")], ('Vector'), ''), [ir.Struct_item(ir.Primitive_type(datatype), ('m%d'%i)) for i in range(1, n+1)], '') e = reduce(add, map(lambda i: ("*", ir.Get_struct_item(t, (ir.deref, "a"), item('m%d'%(i%n+1))), ir.Get_struct_item(t, (ir.deref, "b"), item('m%d'%(i%n+1)))), range(1, 129))) return ir.Stmt(ir.Return(e))
def dotproduct_expr(n, datatype): def add(a, b): return ir.Plus(a, b) def item(name): return ir.Struct_item(ir.Primitive_type(datatype), (name)) a = ir.Struct(ir.Scoped_id([("s")], ('Vector'), ''), [ir.Struct_item(ir.Primitive_type(datatype), ('m%d'%i)) for i in range(1, n+1)], '') b = a e = reduce(add, map(lambda i: ("*", ir.Get_struct_item(a, (ir.deref, "a"), 'm%d'%i), ir.Get_struct_item(b, (ir.deref, "b"), 'm%d'%i)), range(1, n+1))) return ir.Stmt(ir.Return(e))
def item(name): return ir.Struct_item(ir.Primitive_type(datatype), (name))
def lower_ir(symbol_table, sidl_term, header=None, struct_suffix='__data', enum_suffix='__enum', lower_scoped_ids=True, qualify_names=True, qualify_enums=True, raw_ior_arrays=False, wrap_rarrays=False): """ FIXME!! can we merge this with convert_arg?? lower SIDL types into IR The idea is that no Chapel-specific code is in this function. It should provide a generic translation from SIDL -> IR. @param lower_scoped_ids This is a broken design, but right now the Chapel code generator accepts some sidl node types such as array, rarray, and class. If False, then these types will not be lowered. @param qualify_names If \c True, enum values will get prefixed with the full qualified name of the enum. """ def low(sidl_term): return lower_ir(symbol_table, sidl_term, header, struct_suffix, enum_suffix, lower_scoped_ids, qualify_names, qualify_enums, raw_ior_arrays, wrap_rarrays) # print 'low(',sidl_term, ')' array_prefix = '/* IOR */ ' if raw_ior_arrays else '' with match(sidl_term): if (sidlir.arg, Attrs, Mode, (sidlir.scoped_id, _, _, _), Name): lowtype = low(sidl_term[3]) if lowtype[0] == ir.struct and Mode == ir.in_: # struct arguments are passed as pointer, regardless of mode # unless they are a return value lowtype = ir.Pointer_type(lowtype) return (ir.arg, Attrs, Mode, lowtype, Name) elif (sidlir.arg, Attrs, Mode, Typ, Name): return (ir.arg, Attrs, Mode, low(Typ), Name) elif (sidlir.scoped_id, Prefix, Name, Ext): return low(symbol_table[sidl_term][1]) elif (sidlir.void): return ir.pt_void elif (ir.void_ptr): return ir.void_ptr elif (sidlir.primitive_type, sidlir.opaque): return ir.Pointer_type(ir.pt_void) elif (sidlir.primitive_type, sidlir.string): return sidl_term #ir.const_str elif (sidlir.primitive_type, Type): return ir.Primitive_type(Type) elif (sidlir.enum, Name, Enumerators, DocComment): # in the IOR enums are represented as int64 if qualify_enums: es = lower_ir( SymbolTable(symbol_table, symbol_table.prefix + [Name]), Enumerators, header, struct_suffix, enum_suffix, lower_scoped_ids, qualify_names, qualify_enums, raw_ior_arrays, wrap_rarrays) return ir.Enum( qual_name(symbol_table, sidl_term[1]) + enum_suffix, es, DocComment) else: return ir.Enum(sidl_term[1], low(Enumerators), DocComment) elif (sidlir.enumerator, Name): if qualify_enums: return ir.Enumerator(qual_name(symbol_table, Name)) else: return sidl_term elif (sidlir.enumerator_value, Name, Val): if qualify_enums: return ir.Enumerator_value(qual_name(symbol_table, Name), Val) else: return sidl_term elif (sidlir.struct, (sidlir.scoped_id, Prefix, Name, Ext), Items, DocComment): # a nested Struct return ir.Struct( qual_id(sidl_term[1]) + struct_suffix, low(Items), '') elif (sidlir.struct, Name, Items, DocComment): return ir.Struct( qual_name(symbol_table, Name) + struct_suffix, low(Items), '') elif (sidlir.struct_item, Type, Name): if Type[0] == sidlir.scoped_id: t = symbol_table[Type][1] if t[0] == sidlir.class_ or t[0] == sidlir.interface: if header: header.genh(ir.Import(qual_id(t[1]) + '_IOR')) t = ir_object_type(t[1][1], t[1][2]) elif t[0] == sidlir.struct or t[0] == sidlir.enum: return (ir.struct_item, low(t), Name) return (ir.struct_item, t, Name) return (ir.struct_item, low(Type), Name) # elif (sidlir.rarray, Scalar_type, Dimension, Extents): # # Future optimization: # # Direct-call version (r-array IOR) # # return ir.Pointer_type(lower_type_ir(symbol_table, Scalar_type)) # FIXME # # SIDL IOR version # return ir.Typedef_type('sidl_%s__array'%Scalar_type[1]) elif (sidlir.rarray, Scalar_type, Dimension, Extents): if wrap_rarrays: return ir.Pointer_type( ir.Struct( '%ssidl_%s__array' % (array_prefix, Scalar_type[1]), [], '')) else: return ir.Rarray(low(Scalar_type), Dimension, Extents) elif (sidlir.array, [], [], []): #if not lower_scoped_ids: return sidl_term #return ir.Pointer_type(ir.pt_void) return ir.Pointer_type(ir.Struct('sidl__array', [], '')) elif (sidlir.array, Scalar_type, Dimension, Orientation): #if not lower_scoped_ids: return sidl_term if Scalar_type[0] == ir.scoped_id: # All object arrays are called sidl_interface__array t = 'interface' if header: header.genh(ir.Import('sidl_BaseInterface_IOR')) else: t = Scalar_type[1] if header: header.genh(ir.Import('sidl_' + t + '_IOR')) return ir.Pointer_type( ir.Struct('%ssidl_%s__array' % (array_prefix, t), [], '')) elif (sidlir.class_, ScopedId, _, _, _, _, _): if not lower_scoped_ids: return ScopedId else: return ir_object_type(ScopedId[1], ScopedId[2]) elif (sidlir.interface, ScopedId, _, _, _, _): if not lower_scoped_ids: return ScopedId return ir_object_type(ScopedId[1], ScopedId[2]) elif (Terms): if (isinstance(Terms, list)): return map(low, Terms) else: raise Exception("lower_ir: Not implemented: " + str(sidl_term)) else: raise Exception("match error")