def insert_const_string(self, mod, string): """Create a global string from the passed in string argument and return a void* in the GENERIC address space pointing to that string. Args: mod: LLVM module where the global string value is to be inserted. string: A Python string that will be converted to a global constant string and inserted into the module. Returns: A LLVM Constant pointing to the global string value inserted into the module. """ text = cgutils.make_bytearray(string.encode("utf-8") + b"\x00") name = "$".join(["__conststring__", self.mangler(string, ["str"])]) # Try to reuse existing global gv = mod.globals.get(name) if gv is None: # Not defined yet gv = cgutils.add_global_variable(mod, text.type, name=name, addrspace=address_space.GENERIC) gv.linkage = "internal" gv.global_constant = True gv.initializer = text # Cast to a i8* pointer charty = gv.type.pointee.element return gv.bitcast(charty.as_pointer(address_space.GENERIC))
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 codegen(context, builder, sig, args): int8ptr = ir.PointerType(ir.IntType(8)) fntype = ir.FunctionType(ir.IntType(32), [int8ptr]) fn = irutils.get_or_insert_function(builder.module, fntype, name="table_function_error") assert fn.is_declaration # msg_bytes = message.literal_value.encode('utf-8') msg_const = make_bytearray(msg_bytes + b'\0') msg_global_var = global_constant(builder.module, "table_function_error_message", msg_const) msg_ptr = builder.bitcast(msg_global_var, int8ptr) return builder.call(fn, [msg_ptr])
def declare_string(builder, value): lmod = builder.basic_block.function.module cval = cgutils.make_bytearray(value.encode("utf-8") + b"\x00") gl = cgutils.add_global_variable(lmod, cval.type, name="_str", addrspace=nvvm.ADDRSPACE_CONSTANT) gl.linkage = 'internal' gl.global_constant = True gl.initializer = cval charty = ir.IntType(8) constcharptrty = ir.PointerType(charty, nvvm.ADDRSPACE_CONSTANT) charptr = builder.bitcast(gl, constcharptrty) conv = insert_addrspace_conv(lmod, charty, nvvm.ADDRSPACE_CONSTANT) return builder.call(conv, [charptr])
def codegen(context, builder, signature, args): mgr_ptr = args[0] mgr_i8ptr = builder.bitcast(mgr_ptr, i8p) msg_bytes = msg.literal_value.encode('utf-8') msg_const = make_bytearray(msg_bytes + b'\0') msg_global_var = global_constant( builder.module, "table_function_manager_error_message", msg_const) msg_ptr = builder.bitcast(msg_global_var, i8p) fnty = ir.FunctionType(i32, [i8p, i8p]) fn = irutils.get_or_insert_function( builder.module, fnty, "TableFunctionManager_error_message") return builder.call(fn, [mgr_i8ptr, msg_ptr])
def insert_const_string(self, mod, string): """ Unlike the parent version. This returns a a pointer in the constant addrspace. """ text = cgutils.make_bytearray(string.encode("utf-8") + b"\x00") name = '$'.join(["__conststring__", itanium_mangler.mangle_identifier(string)]) # Try to reuse existing global gv = mod.globals.get(name) if gv is None: # Not defined yet gv = cgutils.add_global_variable(mod, text.type, name, addrspace=nvvm.ADDRSPACE_CONSTANT) gv.linkage = 'internal' gv.global_constant = True gv.initializer = text # Cast to a i8* pointer charty = gv.type.pointee.element return gv.bitcast(charty.as_pointer(nvvm.ADDRSPACE_CONSTANT))