温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LLVM IR字符串类型拼接方法是怎样的

发布时间:2021-11-12 16:43:40 来源:亿速云 阅读:179 作者:柒染 栏目:大数据

本篇文章为大家展示了LLVM IR字符串类型拼接方法是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

IR语义为将字符串与整型进行拼接操作;方法虽然笨拙,但能够满足目前的业务需求,今分享出来供大家交流与参考。代码运行环境为LLVM6.0。 关键字:LLVM、字符串、char*、string、拼接 、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 <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <memory>
#include <string>
#include <vector>
using namespace llvm;
using namespace std;

//字符串拼接操作放在C++端进行
char* javaMethodAdd( int x, char * str) {
	
		cout << "arg x:"<< x << endl;
	string str1 = str;
	string xstr = to_string(x);
	str1+= xstr;
	char *st1 = const_cast<char *>(str1.c_str()) ;
	return st1;
}

int main() {
	llvm::LLVMContext *context = new llvm::LLVMContext();
	llvm::Module *module = new llvm::Module("test", *context);

	std::vector<llvm::Type *> arg_types = {
		llvm::Type::getInt32Ty(*context),
		llvm::Type::getInt32Ty(*context)};
//函数参数,返回类型为char指针
	llvm::FunctionType *ftype = llvm::FunctionType::get(
		llvm::Type::getInt8PtrTy(*context), arg_types,false);


	llvm::Function *func = llvm::Function::Create(
		ftype, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "my_func", module);
			func->setCallingConv(llvm::CallingConv::C);

	llvm::BasicBlock *bb = llvm::BasicBlock::(*context, "entry", func);

	llvm::IRBuilder<> builder(*context);
	builder.SetInsertPoint(bb);
//获取传入的参数
	llvm::Argument *arg0 = func->arg_begin() + 0;
	llvm::Argument *arg1 = func->arg_begin() + 1;
	
	
	//string begain------------------
	//LLVM字符串常量
Constant *const_array_str = ConstantDataArray::getString(*context, "beeeeegineeee", false);
	//申请内存空间
	AllocaInst* const_array_addr = builder.CreateAlloca(const_array_str->getType(), 
	                           ConstantExpr::getSizeOf(const_array_str->getType()),"temaddr");

const_array_addr->setAlignment (1);		   
	std::vector<Value*> index_vector;
	index_vector.push_back(ConstantInt::get(Type::getInt32Ty(*context), 0));
	
	auto valueAsPtr = builder.CreateGEP(const_array_addr, index_vector, "tmp");
		// 存储字符串到内存空间
	StoreInst *sist = builder.CreateStore(const_array_str, valueAsPtr);
	sist->setAlignment (1);	
	
	index_vector.push_back(ConstantInt::get(Type::getInt64Ty(*context), 0));
	//创建指向字符串存储内存空间的指针
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");
	//忽略函数名称(可改为任意的)
	FunctionType* double_add_double_type = TypeBuilder<char*(int,char*), false>::get(*context);
	Function* fdouble_add_double = cast<Function>(module->getOrInsertFunction("double_add_double", double_add_double_type));
	std::vector<llvm::Value*> 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();
	//创建执行引擎
 llvm::ExecutionEngine *ee =
        llvm::EngineBuilder(std::unique_ptr<llvm::Module>(module)).setEngineKind(llvm::EngineKind::JIT).create();
 	printf("executed  .........segment........\n");
	//LLVM函数与C++函数映射
 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;
}

//编译和运行命令(Linux)。

// clang++ -O3 cppName.cpp -o test.bc `llvm-config --cflags --ldflags llvm-config --libs llvm-config --system-libs  `

// ./test.bc

上述内容就是LLVM IR字符串类型拼接方法是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI