What is the concatenation method of LLVM IR string types
This article shows you how to concatenate LLVM IR string types, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The semantics of IR is to concatenate strings and integers. Although the method is clumsy, it can meet the current business needs. The code runs in LLVM6.0. Keywords: LLVM, string, char*, string, splicing, concat, IR
# include "llvm/IR/IRBuilder.h" # include "llvm/IR/Verifier.h" # include "llvm/Support/TargetSelect.h" # include "llvm/ExecutionEngine/ExecutionEngine.h" # include "llvm/IR/LLVMContext.h" # include "llvm/IR/Module.h" # include "llvm/IR/Function.h" # include "llvm/IR/BasicBlock.h" # include "llvm/ExecutionEngine/MCJIT.h" # include "llvm/IR/TypeBuilder.h" # include # include # include # include using namespace llvm The using namespace std;// string concatenation operation is placed on the C++ side to perform char* javaMethodAdd (int x, char* str) {cout arg_begin () + 1; / / string begain- / / LLVM string constant Constant * const_array_str = ConstantDataArray::getString (* context, "beeeeegineeee", false) / / apply for memory space AllocaInst* const_array_addr = builder.CreateAlloca (const_array_str- > getType (), ConstantExpr::getSizeOf (const_array_str- > getType ()), "temaddr"); const_array_addr- > setAlignment (1); std::vector index_vector Index_vector.push_back (ConstantInt::get (Type::getInt32Ty (* context), 0); auto valueAsPtr = builder.CreateGEP (const_array_addr, index_vector, "tmp"); / / storing strings to memory StoreInst * sist = builder.CreateStore (const_array_str, valueAsPtr); sist- > setAlignment (1) Index_vector.push_back (ConstantInt::get (Type::getInt64Ty (* context), 0)); / / create a pointer to the string storage memory space auto retRes = builder.CreateInBoundsGEP (const_array_str- > getType (), const_array_addr, index_vector, "tmpstr") / / string end-- / / llvm::Value* result = builder.CreateAdd (arg0, arg1, "result"); llvm::Value* result = builder.CreateBinOp (llvm::Instruction::Add, arg0, arg1, "result") / / ignore the function name (can be changed to any) FunctionType* double_add_double_type = TypeBuilder::get (* context); Function* fdouble_add_double = cast (module- > getOrInsertFunction ("double_add_double", double_add_double_type)); std::vector args; args.push_back (result); args.push_back (retRes) Value* ret = builder.CreateCall (fdouble_add_double, args); builder.CreateRet (ret); llvm::InitializeNativeTarget (); llvm::InitializeNativeTargetAsmPrinter (); llvm::InitializeNativeTargetAsmParser (); / / create execution engine llvm::ExecutionEngine * ee = llvm::EngineBuilder (std::unique_ptr (module)) .setEngineKind (llvm::EngineKind::JIT). Create () Printf ("executed. Mapping.\ n"); / / Mapping between LLVM function and C++ function ee- > addGlobalMapping (fdouble_add_double, (char*) javaMethodAdd); ee- > finalizeObject (); char* (* add) (int, int) = (char* (*) (int, int)) ee- > getFunctionAddress ("my_func") Printf ("Result:% s\ n", add (43,10)); return 0;}
/ / compile and run the command (Linux).
/ / clang++-O3 cppName.cpp-o test.bc `llvm-config-- cflags-- ldflags llvm-config-- libs llvm-config-- system-libs `
/ /. / test.bc
The above is what the LLVM IR string type concatenation method is, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.