溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

LLVM IR字符串類型拼接方法是怎樣的

發(fā)布時(shí)間:2021-11-12 16:43:40 來(lái)源:億速云 閱讀:203 作者:柒染 欄目:大數(shù)據(jù)

本篇文章為大家展示了LLVM IR字符串類型拼接方法是怎樣的,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

IR語(yǔ)義為將字符串與整型進(jìn)行拼接操作;方法雖然笨拙,但能夠滿足目前的業(yè)務(wù)需求,今分享出來(lái)供大家交流與參考。代碼運(yùn)行環(huán)境為L(zhǎng)LVM6.0。 關(guān)鍵字: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++端進(jìn)行
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)};
//函數(shù)參數(shù),返回類型為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);
//獲取傳入的參數(shù)
	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);
	//申請(qǐng)內(nèi)存空間
	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");
		// 存儲(chǔ)字符串到內(nèi)存空間
	StoreInst *sist = builder.CreateStore(const_array_str, valueAsPtr);
	sist->setAlignment (1);	
	
	index_vector.push_back(ConstantInt::get(Type::getInt64Ty(*context), 0));
	//創(chuàng)建指向字符串存儲(chǔ)內(nèi)存空間的指針
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");
	//忽略函數(shù)名稱(可改為任意的)
	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();
	//創(chuàng)建執(zhí)行引擎
 llvm::ExecutionEngine *ee =
        llvm::EngineBuilder(std::unique_ptr<llvm::Module>(module)).setEngineKind(llvm::EngineKind::JIT).create();
 	printf("executed  .........segment........\n");
	//LLVM函數(shù)與C++函數(shù)映射
 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;
}

//編譯和運(yùn)行命令(Linux)。

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

// ./test.bc

上述內(nèi)容就是LLVM IR字符串類型拼接方法是怎樣的,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI