def pack_value(self, builder, ty, value, ptr): """Pack data for array storage """ if isinstance(ty, types.Record): pdata = cgutils.get_record_data(builder, value) databuf = builder.load(pdata) casted = builder.bitcast(ptr, Type.pointer(databuf.type)) builder.store(databuf, casted) return if ty == types.boolean: value = cgutils.as_bool_byte(builder, value) assert value.type == ptr.type.pointee builder.store(value, ptr)
def from_native_value(self, val, typ): if typ == types.pyobject: return val elif typ == types.boolean: longval = self.builder.zext(val, self.long) return self.bool_from_long(longval) elif typ in types.unsigned_domain: ullval = self.builder.zext(val, self.ulonglong) return self.long_from_ulonglong(ullval) elif typ in types.signed_domain: ival = self.builder.sext(val, self.longlong) return self.long_from_longlong(ival) elif typ == types.float32: dbval = self.builder.fpext(val, self.double) return self.float_from_double(dbval) elif typ == types.float64: return self.float_from_double(val) elif typ == types.complex128: cmplxcls = self.context.make_complex(typ) cval = cmplxcls(self.context, self.builder, value=val) return self.complex_from_doubles(cval.real, cval.imag) elif typ == types.complex64: cmplxcls = self.context.make_complex(typ) cval = cmplxcls(self.context, self.builder, value=val) freal = self.context.cast(self.builder, cval.real, types.float32, types.float64) fimag = self.context.cast(self.builder, cval.imag, types.float32, types.float64) return self.complex_from_doubles(freal, fimag) elif isinstance(typ, types.NPDatetime): return self.create_np_datetime(val, typ.unit_code) elif isinstance(typ, types.NPTimedelta): return self.create_np_timedelta(val, typ.unit_code) elif typ == types.none: ret = self.make_none() return ret elif isinstance(typ, types.Optional): return self.from_native_return(val, typ.type) elif isinstance(typ, types.Array): return self.from_native_array(typ, val) elif isinstance(typ, types.Record): # Note we will create a copy of the record # This is the only safe way. pdata = cgutils.get_record_data(self.builder, val) size = Constant.int(Type.int(), pdata.type.pointee.count) ptr = self.builder.bitcast(pdata, Type.pointer(Type.int(8))) # Note: this will only work for CPU mode # The following requires access to python object dtype_addr = Constant.int(self.py_ssize_t, id(typ.dtype)) dtypeobj = dtype_addr.inttoptr(self.pyobj) return self.recreate_record(ptr, size, dtypeobj) elif isinstance(typ, (types.Tuple, types.UniTuple)): return self.from_tuple(typ, val) raise NotImplementedError(typ)