def to_object(b, kind, op): """convert(Complex, [real, imag]) -> PyComplex_FromDoubles(real, imag)""" arg, = op.args real = b.getfield(arg, [Const('real')]) imag = b.getfield(arg, [Const('imag')]) return b.gen_call_external("PyComplex_FromDoubles", [real, imag], op.result)
def from_ctypes_value(ctypes_value): """ Convert a ctypes value to a pykit constant """ if not is_ctypes_value(ctypes_value): assert isinstance(ctypes_value, (int, long, float)) return Const(ctypes_value) #if is_ctypes_function(ctypes_value): # restype = from_ctypes_type(ctypes_value.restype) # argtypes = [from_ctypes_type(argty) for argty in ctypes_value.argtypes] # ptr = ptrval(ctypes_value) # func_type = types.Function(restype, argtypes, varargs=False) #func_type_p = types.Pointer(func_type) #return Pointer(ptr, func_type_p) ctype = type(ctypes_value) if hashable(ctype) and ctype in ctypes_map: return Const(ctypes_value.value, from_ctypes_type(ctype)) elif is_ctypes_struct_type(ctype): names = [name for name, _ in ctype._fields_] values = [from_ctypes_value(getattr(ctypes_value, n)) for n in names] if not names: names, values = ('dummy', ), (Const(0, types.Int8)) return Struct(names, values, from_ctypes_type(ctype)) else: assert is_ctypes_pointer_type(ctype), ctype return Pointer( ctypes.cast(ctypes_value, ctypes.c_void_p).value, from_ctypes_type(ctype))
def heap_allocate(caller, builder, type, env): """ Heap allocate an object of type `type` """ phase = env['flypy.state.phase'] # TODO: implement untyped pykit builder ! # Put object on the heap: call gc.gc_alloc(nitems, type) gcmod = gc.gc_impl(env["flypy.gc.impl"]) context = env['flypy.typing.context'] # Build arguments for gc_alloc n = Const(1, ptypes.Opaque) ty = Const(type, ptypes.Opaque) context[n] = int64 context[ty] = Type[type] # Type the gc_alloc function p = caller.call(phase, gcmod.gc_alloc, [n, ty]) obj = builder.convert(ptypes.Opaque, p) # Update type context context[p] = Pointer[void] return [p, obj], obj
def test_exc_rewrite(self): func = Function("foo", [], types.Function(types.Void, (), False)) entry = func.new_block("entry") catch_block = func.new_block("catch") b = Builder(func) with b.at_front(entry): b.exc_setup([catch_block]) b.exc_throw(Const(StopIteration, types.Exception)) with b.at_front(catch_block): b.exc_catch([Const(Exception, types.Exception)]) local_exceptions.run(func, {}) self.assertNotIn('exc_throw', opcodes(func))
def _from_expr(expr, f, builder, values): if expr.opcode == 'array': result = values[expr] else: # ------------------------------------------------- # Construct args # This is purely for IR readability name = qualified_name(expr.metadata['overload'].func) args = [_from_expr(arg, f, builder, values) for arg in expr.args] args = [Const(name)] + args # ------------------------------------------------- # Construct Op result = Op("kernel", expr.dshape, args) # Copy metadata verbatim assert 'kernel' in expr.metadata assert 'overload' in expr.metadata result.add_metadata(expr.metadata) # ------------------------------------------------- # Emit Op in code stream builder.emit(result) values[expr] = result return result
def allocate_pointer_const(b, const): ty = const.type val = const.const if ty.is_pointer: value = allocate_pointer_const(b, Const(val.base, ty.base)) variable = b.alloca(ty) b.store(value, variable) return variable return const
def _handle_raise(self, op, badval, exc): "Raise an exception if retval == badval" assert badval is not None cond = self.builder.eq(types.Bool, [op, badval]) with self.builder.if_(cond): msg = op.metadata.get("exc.msg") args = [Const(msg)] if msg is not None else [] exc = self.builder.new_exc(types.Exception, [exc] + args) self.builder.exc_throw(exc)
def resolve_type(context, op): """ Resolve types for ops: - map flypy type to low-level representation type - represent stack-allocated values through pointers """ if isinstance(op, (FuncArg, Const, Op, Undef)): if op.type.is_void: return op if op not in context: raise errors.CompileError("Type for %s was lost" % (op,)) # Retrieve type type = context[op] # Remove dummy method lookups (TODO: methods as first-class citizens) if type.__class__.__name__ == 'Method': return op # TODO: Remove this # Generate low-level representation type if isinstance(type, set): assert not isinstance(type, set) ltype = compiler.representation_type(type) if isinstance(op, Const): const = op.const # Represent dummy constant structs with at least one field for LLVM if isinstance(const, Struct) and not const.values: const = Struct(['dummy'], [Const(0, ptypes.Int32)]) # Also represent stack-allocated values through pointers if ltype.is_pointer and not isinstance(const, Pointer): const = Pointer(const, ltype) op = Const(const, ltype) elif isinstance(op, Undef): op = Undef(ltype) else: op.type = ltype return op
def resolve_type(context, op): """ Resolve types for ops: - map flypy type to low-level representation type - represent stack-allocated values through pointers """ if isinstance(op, (FuncArg, Const, Op, Undef)): if op.type.is_void: return op if op not in context: raise errors.CompileError("Type for %s was lost" % (op, )) # Retrieve type type = context[op] # Remove dummy method lookups (TODO: methods as first-class citizens) if type.__class__.__name__ == 'Method': return op # TODO: Remove this # Generate low-level representation type if isinstance(type, set): assert not isinstance(type, set) ltype = compiler.representation_type(type) if isinstance(op, Const): const = op.const # Represent dummy constant structs with at least one field for LLVM if isinstance(const, Struct) and not const.values: const = Struct(['dummy'], [Const(0, ptypes.Int32)]) # Also represent stack-allocated values through pointers if ltype.is_pointer and not isinstance(const, Pointer): const = Pointer(const, ltype) op = Const(const, ltype) elif isinstance(op, Undef): op = Undef(ltype) else: op.type = ltype return op
def gen_call_external(self, fname, args, result=None): """Generate call to external function (which must be declared""" gv = self.module.get_global(fname) assert gv is not None, "Global %s not declared" % fname assert gv.type.is_function, gv assert gv.type.argtypes == [arg.type for arg in args] op = self.call(gv.type.res, [Const(fname), args]) op.result = result or op.result return op
def rewrite_overlays(func, env=None): """ Resolve overlays of constants. """ for op in func.ops: consts = collect_constants(op) new = [] for c in consts: overlain = overlay_registry.lookup_overlay(c.const) if overlain: c = Const(overlain, type=c.type) new.append(c) substitute_args(op, consts, new)
def gen_loop(self, start=None, stop=None, step=None): """ Generate a loop given start, stop, step and the index variable type. The builder's position is set to the end of the body block. Returns (condition_block, body_block, exit_block). """ self._assert_position() assert isinstance(stop, Value), "Stop should be a Constant or Operation" ty = stop.type start = start or Const(0, ty) step = step or Const(1, ty) assert start.type == ty == step.type with self.at_front(self.func.blocks[0]): var = self.alloca(types.Pointer(ty), []) prev, exit = self.splitblock('loop.exit') cond = self.func.add_block('loop.cond', after=prev) body = self.func.add_block('loop.body', after=cond) with self.at_end(prev): self.store(start, var) self.jump(cond) # Condition with self.at_front(cond): index = self.load(ty, [var]) self.store(self.add(ty, [index, step]), var) self.cbranch(self.lt(types.Bool, [index, stop]), body, exit) with self.at_end(body): self.jump(cond) self.position_at_beginning(body) return cond, body, exit
def fold(self, op, cells): m = getattr(self, 'op_' + op.opcode, None) result = None if m is not None: result = m(op, cells) if (result is not None and isconst(result) and not isinstance(result, Const)): result = Const(result, op.type) cells[result] = result if result is None: result = bottom # Undetermined value cells[op] = result return result
def op_FOR_ITER(self, inst): """ Translate a for loop to: it = getiter(iterable) try: while 1: i = next(t) ... except StopIteration: pass """ iterobj = self.peek() delta = inst.arg loopexit = self.blocks[inst.next + delta] loop_block = self.loop_stack[-1] loop_block.catch_block = loopexit # ------------------------------------------------- # Try self.insert('exc_setup', [loopexit]) self.call(next, [iterobj]) # We assume a 1-to-1 block mapping, resolve a block split in a # later pass self.insert('exc_end') # ------------------------------------------------- # Catch with self.builder.at_front(loopexit): self.insert('exc_catch', [Const(StopIteration, type=types.Exception)]) # ------------------------------------------------- # Exit # Add the loop exit at a successor to the header self.predecessors[loopexit].add(self.curblock) # Remove ourselves as a predecessor from the actual exit block, set by # SETUP_LOOP self.predecessors[loop_block.end].remove(self.prevblock)
class OpBuilder(object): """ I know how to build Operations. """ def _op(op): """Helper to create Builder methods""" def _process(self, ty, args=None, result=None, **metadata): if args is None: args = [] assert ty is not None assert isinstance(args, list), args assert not any(arg is None for arg in flatten(args)), args result = Op(op, ty, args, result) if metadata: result.add_metadata(metadata) self._insert_op(result) return result def _process_void(self, *args, **kwds): result = kwds.pop('result', None) op = _process(self, types.Void, list(args), result) if kwds: op.add_metadata(kwds) return op if ops.is_void(op): build_op = _process_void else: build_op = _process if config.op_verify: build_op = op_verifier(build_op) return build_op def _insert_op(self, op): """Implement in subclass that emits Operations""" _const = lambda val: Const(val, types.Void) # __________________________________________________________________ # IR constructors # Generated by pykit.utils._generate Sin = _const(ops.Sin) Asin = _const(ops.Asin) Sinh = _const(ops.Sinh) Asinh = _const(ops.Asinh) Cos = _const(ops.Cos) Acos = _const(ops.Acos) Cosh = _const(ops.Cosh) Acosh = _const(ops.Acosh) Tan = _const(ops.Tan) Atan = _const(ops.Atan) Atan2 = _const(ops.Atan2) Tanh = _const(ops.Tanh) Atanh = _const(ops.Atanh) Log = _const(ops.Log) Log2 = _const(ops.Log2) Log10 = _const(ops.Log10) Log1p = _const(ops.Log1p) Exp = _const(ops.Exp) Exp2 = _const(ops.Exp2) Expm1 = _const(ops.Expm1) Floor = _const(ops.Floor) Ceil = _const(ops.Ceil) Abs = _const(ops.Abs) Erfc = _const(ops.Erfc) Rint = _const(ops.Rint) Pow = _const(ops.Pow) Round = _const(ops.Round) alloca = _op(ops.alloca) load = _op(ops.load) store = _op(ops.store) map = _op(ops.map) reduce = _op(ops.reduce) filter = _op(ops.filter) scan = _op(ops.scan) zip = _op(ops.zip) allpairs = _op(ops.allpairs) flatten = _op(ops.flatten) print = _op(ops.print) concat = _op(ops.concat) length = _op(ops.length) contains = _op(ops.contains) list_append = _op(ops.list_append) list_pop = _op(ops.list_pop) set_add = _op(ops.set_add) set_remove = _op(ops.set_remove) dict_add = _op(ops.dict_add) dict_remove = _op(ops.dict_remove) dict_keys = _op(ops.dict_keys) dict_values = _op(ops.dict_values) dict_items = _op(ops.dict_items) box = _op(ops.box) unbox = _op(ops.unbox) convert = _op(ops.convert) new_list = _op(ops.new_list) new_tuple = _op(ops.new_tuple) new_dict = _op(ops.new_dict) new_set = _op(ops.new_set) new_struct = _op(ops.new_struct) new_data = _op(ops.new_data) new_exc = _op(ops.new_exc) phi = _op(ops.phi) exc_setup = _op(ops.exc_setup) exc_catch = _op(ops.exc_catch) jump = _op(ops.jump) cbranch = _op(ops.cbranch) exc_throw = _op(ops.exc_throw) ret = _op(ops.ret) function = _op(ops.function) call = _op(ops.call) call_math = _op(ops.call_math) ptradd = _op(ops.ptradd) ptrload = _op(ops.ptrload) ptrstore = _op(ops.ptrstore) ptrcast = _op(ops.ptrcast) ptr_isnull = _op(ops.ptr_isnull) getfield = _op(ops.getfield) setfield = _op(ops.setfield) getindex = _op(ops.getindex) setindex = _op(ops.setindex) getslice = _op(ops.getslice) setslice = _op(ops.setslice) slice = _op(ops.slice) add = _op(ops.add) sub = _op(ops.sub) mul = _op(ops.mul) div = _op(ops.div) mod = _op(ops.mod) lshift = _op(ops.lshift) rshift = _op(ops.rshift) bitand = _op(ops.bitand) bitor = _op(ops.bitor) bitxor = _op(ops.bitxor) invert = _op(ops.invert) not_ = _op(ops.not_) uadd = _op(ops.uadd) usub = _op(ops.usub) eq = _op(ops.eq) noteq = _op(ops.noteq) lt = _op(ops.lt) lte = _op(ops.lte) gt = _op(ops.gt) gte = _op(ops.gte) is_ = _op(ops.is_) threadpool_start = _op(ops.threadpool_start) threadpool_submit = _op(ops.threadpool_submit) threadpool_join = _op(ops.threadpool_join) threadpool_close = _op(ops.threadpool_close) thread_start = _op(ops.thread_start) thread_join = _op(ops.thread_join) check_overflow = _op(ops.check_overflow) check_error = _op(ops.check_error) addressof = _op(ops.addressof) exc_matches = _op(ops.exc_matches) store_tl_exc = _op(ops.store_tl_exc) load_tl_exc = _op(ops.load_tl_exc) gc_gotref = _op(ops.gc_gotref) gc_giveref = _op(ops.gc_giveref) gc_incref = _op(ops.gc_incref) gc_decref = _op(ops.gc_decref) gc_alloc = _op(ops.gc_alloc) gc_dealloc = _op(ops.gc_dealloc) def convert(self, type, args, result=None, buildop=convert): if type == args[0].type: return args[0] return buildop(self, type, args, result)
def visit_Constant(self, node): type = self.type_env[node.type] const = types.convert(node.value, types.resolve_typedef(type)) if isinstance(const, basestring): const = const[1:-1] # slice away quotes return Const(const)
class GeneratedBuilder(object): _const = lambda val: Const(val, types.Opaque) _insert_op = lambda self, op: None # noop #===------------------------------------------------------------------=== # Generated by pykit.utils._generate #===------------------------------------------------------------------=== Sin = _const(ops.Sin) Asin = _const(ops.Asin) Sinh = _const(ops.Sinh) Asinh = _const(ops.Asinh) Cos = _const(ops.Cos) Acos = _const(ops.Acos) Cosh = _const(ops.Cosh) Acosh = _const(ops.Acosh) Tan = _const(ops.Tan) Atan = _const(ops.Atan) Atan2 = _const(ops.Atan2) Tanh = _const(ops.Tanh) Atanh = _const(ops.Atanh) Log = _const(ops.Log) Log2 = _const(ops.Log2) Log10 = _const(ops.Log10) Log1p = _const(ops.Log1p) Exp = _const(ops.Exp) Exp2 = _const(ops.Exp2) Expm1 = _const(ops.Expm1) Floor = _const(ops.Floor) Ceil = _const(ops.Ceil) Abs = _const(ops.Abs) Erfc = _const(ops.Erfc) Rint = _const(ops.Rint) Pow = _const(ops.Pow) Round = _const(ops.Round) def alloca(self, returnType, obj0, **kwds): assert returnType is not None register = kwds.pop('result', None) op = Op('alloca', returnType, [obj0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def load(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('load', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def store(self, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('store', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def convert(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('convert', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def bitcast(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('bitcast', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def phi(self, returnType, lst0, lst1, **kwds): assert isinstance(lst0, list) assert isinstance(lst1, list) assert returnType is not None register = kwds.pop('result', None) op = Op('phi', returnType, [lst0, lst1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def exc_setup(self, lst0, **kwds): assert isinstance(lst0, list) returnType = types.Void register = kwds.pop('result', None) op = Op('exc_setup', returnType, [lst0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def exc_catch(self, lst0, **kwds): assert isinstance(lst0, list) returnType = types.Void register = kwds.pop('result', None) op = Op('exc_catch', returnType, [lst0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def jump(self, value0, **kwds): assert isinstance(value0, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('jump', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def cbranch(self, value0, value1, value2, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert isinstance(value2, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('cbranch', returnType, [value0, value1, value2], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def exc_throw(self, value0, **kwds): assert isinstance(value0, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('exc_throw', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ret(self, obj0, **kwds): returnType = types.Void register = kwds.pop('result', None) op = Op('ret', returnType, [obj0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def call(self, returnType, value0, lst0, **kwds): assert isinstance(value0, Value) assert isinstance(lst0, list) assert returnType is not None register = kwds.pop('result', None) op = Op('call', returnType, [value0, lst0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def call_math(self, returnType, obj0, lst0, **kwds): assert isinstance(lst0, list) assert returnType is not None register = kwds.pop('result', None) op = Op('call_math', returnType, [obj0, lst0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def add(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('add', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def addressof(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('addressof', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def sizeof(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('sizeof', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ptradd(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('ptradd', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ptrload(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('ptrload', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ptrstore(self, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('ptrstore', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ptrcast(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('ptrcast', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ptr_isnull(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('ptr_isnull', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ge(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('ge', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def get(self, returnType, value0, lst0, **kwds): assert isinstance(value0, Value) assert isinstance(lst0, list) assert returnType is not None register = kwds.pop('result', None) op = Op('get', returnType, [value0, lst0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def set(self, returnType, value0, value1, lst0, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert isinstance(lst0, list) assert returnType is not None register = kwds.pop('result', None) op = Op('set', returnType, [value0, value1, lst0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def getfield(self, returnType, value0, obj0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('getfield', returnType, [value0, obj0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def setfield(self, value0, obj0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('setfield', returnType, [value0, obj0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def extractfield(self, returnType, value0, obj0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('extractfield', returnType, [value0, obj0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def insertfield(self, returnType, value0, obj0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('insertfield', returnType, [value0, obj0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def shufflevector(self, returnType, value0, value1, value2, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert isinstance(value2, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('shufflevector', returnType, [value0, value1, value2], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def sub(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('sub', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def mul(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('mul', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def div(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('div', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def mod(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('mod', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def lshift(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('lshift', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def rshift(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('rshift', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def bitand(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('bitand', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def bitor(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('bitor', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def bitxor(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('bitxor', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def invert(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('invert', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def not_(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('not_', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def uadd(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('uadd', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def usub(self, returnType, value0, **kwds): assert isinstance(value0, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('usub', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def eq(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('eq', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def ne(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('ne', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def lt(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('lt', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def le(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('le', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def gt(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('gt', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def is_(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('is_', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def check_error(self, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('check_error', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def new_exc(self, returnType, value0, value1, **kwds): assert isinstance(value0, Value) assert isinstance(value1, Value) assert returnType is not None register = kwds.pop('result', None) op = Op('new_exc', returnType, [value0, value1], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op def print(self, value0, **kwds): assert isinstance(value0, Value) returnType = types.Void register = kwds.pop('result', None) op = Op('print', returnType, [value0], register, metadata=kwds) if config.op_verify: verify_op_syntax(op) self._insert_op(op) return op
def allocate_const(func, env, op, value, type): const = Const(value, ptypes.Opaque) context = env['flypy.typing.context'] context[const] = type return const
def visit_Constant(self, node): type = self.type_env[node.type] const = types.convert(node.value, type) return Const(const)
def gen_call_external(self, fname, args): gv = self.module.get_global(fname) assert gv.type.is_function, gv assert gv.type.argtypes == [arg.type for arg in args] return self.call_external(gv.type.res, [Const(fname), args])
class GeneratedBuilder(object): _const = lambda val: Const(val, types.Opaque) _insert_op = lambda self, op: None # noop