C++ LLVM将函数调用插入到另一个函数中
我试图在主函数中插入函数调用,所以当我运行生成的二进制文件时,函数将自动执行。由于我尝试“编译”的语言看起来像是“脚本化”语言:C++ LLVM将函数调用插入到另一个函数中,c++,llvm,C++,Llvm,我试图在主函数中插入函数调用,所以当我运行生成的二进制文件时,函数将自动执行。由于我尝试“编译”的语言看起来像是“脚本化”语言: function foo () begin 3 end; function boo () begin 4 end; writeln (foo()+boo()) ; writeln (8) ; writeln (9) ; 其中writeln是一个默认可用的函数,在执行binary之后,我希望看到7 8 9。有没有办法在主函数的返回语句之前插入最后一个函数调用? 现在
function foo () begin 3 end;
function boo () begin 4 end;
writeln (foo()+boo()) ;
writeln (8) ;
writeln (9) ;
其中writeln是一个默认可用的函数,在执行binary之后,我希望看到7 8 9。有没有办法在主函数的返回语句之前插入最后一个函数调用?
现在我有
define i32 @main() {
entry:
ret i32 0
}
我想要一些像这样的东西
define i32 @main() {
entry:
%calltmp = call double @writeln(double 7.000000e+00)
%calltmp = call double @writeln(double 8.000000e+00)
%calltmp = call double @writeln(double 9.000000e+00)
ret i32 0
}
手动编辑IR文件并在以后编译它是可行的,但我想在代码的codegen部分中这样做
编辑
我现在产生的是
define double @__anon_expr() {
entry:
%main = call double @writeln(double 3.000000e+00)
ret double %main
}
define i32 @main() {
entry:
ret i32 0
}
因此,当我执行二进制时,什么也不会发生请随意从这里获取灵感
Type * returnType = Type::getInt32Ty(TheContext);
std::vector<Type *> argTypes;
FunctionType * functionType = FunctionType::get(returnType, argTypes, false);
Function * function = Function::Create(functionType, Function::ExternalLinkage, "main", TheModule.get());
BasicBlock * BB = BasicBlock::Create(TheContext, "entry", function);
Builder.SetInsertPoint(BB);
vector<Value *> args;
args.push_back(ConstantFP::get(TheContext, APFloat(4.0)));
Builder.CreateCall(getFunction("writeln"), args, "call");
Value * returnValue = Builder.getInt32(0);
Builder.CreateRet(returnValue);
Type*returnType=Type::getInt32Ty(上下文);
std::向量argtype;
FunctionType*FunctionType=FunctionType::get(returnType,argTypes,false);
Function*Function=Function::Create(functionType,Function::ExternalLinkage,“main”,TheModule.get());
BasicBlock*BB=BasicBlock::Create(上下文“entry”,函数);
生成器设置插入点(BB);
向量args;
参数push_back(ConstantFP::get(context,APFloat(4.0));
CreateCall(getFunction(“writeln”),args,“call”);
Value*returnValue=Builder.getInt32(0);
Builder.CreateRet(返回值);
这与c++
有什么关系?@super我正在用c++编写llvm的前端,不太清楚您想做什么。如果它是你正在生成的IR,你可以做任何你喜欢的事情。如果你想修改一些现有的IR-那可能不是你应该做的。如果您仍然坚持,那么只需编写一个函数传递并使用opt
@SK logic运行它,我正在生成IR,但我不知道如何在主函数中添加函数调用。现在看起来像这样的“define double@u anon_expr(){entry:%main=call double@writeln(double 3.000000e+00)ret double%main}define i32@main(){entry:ret i32 0}”,所以什么都不执行