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 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 make_keywords(self, kws): strings = [] stringtype = Type.pointer(Type.int(8)) for k in kws: strings.append(self.make_const_string(k)) strings.append(Constant.null(stringtype)) kwlist = Constant.array(stringtype, strings) kwlist = cgutils.global_constant(self.module, ".kwlist", kwlist) return Constant.bitcast(kwlist, Type.pointer(stringtype))
def insert_const_string(self, mod, string): stringtype = GENERIC_POINTER text = Constant.stringz(string) name = ".const.%s" % string for gv in mod.global_variables: if gv.name == name and gv.type.pointee == text.type: break else: gv = cgutils.global_constant(mod, name, text) gv.linkage = lc.LINKAGE_INTERNAL return Constant.bitcast(gv, stringtype)
def insert_const_string(self, mod, string): """ Unlike the parent version. This returns a a pointer in the constant addrspace. """ text = Constant.stringz(string) name = '.'.join(["__conststring__", self.mangle_name(string)]) # Try to reuse existing global gv = mod.globals.get(name) if gv is None: # Not defined yet gv = mod.add_global_variable(text.type, name=name, addrspace=nvvm.ADDRSPACE_CONSTANT) gv.linkage = LINKAGE_INTERNAL gv.global_constant = True gv.initializer = text # Cast to a i8* pointer charty = gv.type.pointee.element return Constant.bitcast(gv, charty.as_pointer(nvvm.ADDRSPACE_CONSTANT))