def bsort_expr(n, datatype): def assign(var, val): return ir.Stmt((ir.assignment, var, val)) def item(name): return ir.Struct_item(ir.Primitive_type(datatype), (name)) t = ir.Struct(ir.Scoped_id([("s")], ('Vector'), ''), [item('m%d'%i) for i in range(1, n+1)], '') a = ir.Arg([], ir.in_, t, 'a') b = ir.Arg([], ir.inout, t, 'b') copy = [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'%i)))) for i in range(1, n+1)] sort = [(ir.var_decl, ir.pt_bool, ("swapped")), (ir.var_decl, ir.pt_int, ("tmp")), (ir.do_while, ("swapped"), # if A[i-1] > A[i] [assign(("swapped"), ir.Bool(ir.false))]+ [[(ir.if_, (ir.Infix_expr(ir.gt, ir.Get_struct_item(t, (ir.deref, "b"), item('m%d'%(i-1))), ir.Get_struct_item(t, (ir.deref, "b"), item('m%d'%i)))), # swap( A[i-1], A[i] ) # swapped = true [assign(("tmp"), ir.Get_struct_item(t, (ir.deref, "b"), item('m%d'%i))), ir.Stmt(ir.Set_struct_item(t, (ir.deref, "b"), item('m%d'%i), ir.Get_struct_item(t, (ir.deref, "b"), item('m%d'%(i-1))))), ir.Stmt(ir.Set_struct_item(t, (ir.deref, "b"), item('m%d'%(i-1)), ("tmp"))), assign(("swapped"), ir.Bool(ir.true))])] for i in range(2, n+1)])] return copy+sort+[ir.Stmt(ir.Return(retval(n, datatype)))]
def vcall(name, args, ci): """ \return the IR for a non-static Babel virtual method call """ try: cdecl = ci.epv.find_method(name) epv_type = ci.epv.get_type() epv = ir.Get_struct_item(ci.obj, ir.Deref(args[0]), ir.Struct_item(epv_type, 'd_epv')) except: if False: # FIXME no_contracts and no_hooks: return ir.Call('_'.join(ci.co.qualified_name + [name]), args) else: cdecl = ci.epv.find_static_method(name) epv_type = ci.epv.get_sepv_type() epv = '_getSEPV()' # this is part of an ugly hack to make sure that self is # dereferenced as self->d_object (by setting attr of self to the # unused value of 'pure') if ci.co.is_interface() and args: _, attrs, type_, id_, arguments, doc = cdecl _, attrs0, mode0, type0, name0 = arguments[0] arguments = [ir.Arg([ir.pure], mode0, type0, name0)] + arguments[1:] cdecl = ir.Fn_decl(attrs, type_, id_, arguments, doc) return ir.Call( ir.Deref( ir.Get_struct_item( epv_type, ir.Deref(epv), ir.Struct_item(ir.Pointer_type(cdecl), 'f_' + name))), args)
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 build_function_call(ci, cdecl, static): ''' Build an IR expression that consists of the EPV lookup for a possibly virtual function call using Babel IOR. ''' #if final: # final : Final methods are the opposite of virtual. While # they may still be inherited by child classes, they # cannot be overridden. # static call #The problem with this is that e.g. C++ symbols usere different names #We should modify Babel to generate __attribute__ ((weak, alias ("__entry"))) # callee = '_'.join(['impl']+symbol_table.prefix+[ci.epv.name,Name]) if static: # static : Static methods are sometimes called "class # methods" because they are part of a class, but do not # depend on an object instance. In non-OO languages, this # means that the typical first argument of an instance is # removed. In OO languages, these are mapped directly to # an Java or C++ static method. epv_type = ci.epv.get_type() obj_type = ci.obj callee = ir.Get_struct_item( epv_type, ir.Deref(ir.Call('_getSEPV', [])), ir.Struct_item(ir.Pointer_type(cdecl), 'f_' + ir.fn_decl_id(cdecl))) else: # dynamic virtual method call epv_type = ci.epv.get_type() obj_type = ci.obj callee = ir.Deref( ir.Get_struct_item( epv_type, ir.Deref( ir.Get_struct_item(obj_type, ir.Deref('self'), ir.Struct_item(epv_type, 'd_epv'))), ir.Struct_item(ir.Pointer_type(cdecl), 'f_' + ir.fn_decl_id(cdecl)))) return callee
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)))]