def insert_const_bytes(self, mod, bytes, name=None): """ Insert constant *byte* (a `bytes` object) into module *mod*. """ stringtype = GENERIC_POINTER name = ".bytes.%s" % (name or hash(bytes)) text = cgutils.make_bytearray(bytes) gv = self.insert_unique_const(mod, name, text) return Constant.bitcast(gv, stringtype)
def insert_const_string(self, mod, string): """ Insert constant *string* (a str object) into module *mod*. """ stringtype = GENERIC_POINTER name = ".const.%s" % string text = cgutils.make_bytearray(string.encode("utf-8") + b"\x00") gv = self.insert_unique_const(mod, name, text) return Constant.bitcast(gv, stringtype)
def serialize_object(self, obj): """ Serialize the given object in the bitcode, and return it as a pointer to a {i8* data, i32 length}, structure constant (suitable for passing to unserialize()). """ try: gv = self.module.__serialized[obj] except KeyError: # First make the array constant data = pickle.dumps(obj, protocol=-1) name = ".const.pickledata.%s" % (id(obj)) bdata = cgutils.make_bytearray(data) arr = self.context.insert_unique_const(self.module, name, bdata) # Then populate the structure constant struct = ir.Constant.literal_struct([ arr.bitcast(self.voidptr), ir.Constant(ir.IntType(32), arr.type.pointee.count), ]) name = ".const.picklebuf.%s" % (id(obj)) gv = self.context.insert_unique_const(self.module, name, struct) # Make the id() (and hence the name) unique while populating the module. self.module.__serialized[obj] = gv return gv