我试图在主函数中插入函数调用,因此当我运行生成的二进制文件时,函数将自动执行。由于我尝试“编译”的语言看起来像“脚本化”语言:
function foo () begin 3 end;
function boo () begin 4 end;
writeln (foo()+boo()) ;
writeln (8) ;
writeln (9) ;
Run Code Online (Sandbox Code Playgroud)
其中 writeln 是默认情况下可用的函数,执行二进制文件后我期望看到 7 8 9。有没有办法在主函数的 return 语句之前插入最后一个函数调用?现在我有
define i32 @main() {
entry:
ret i32 0
}
Run Code Online (Sandbox Code Playgroud)
我想要类似的东西
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
}
Run Code Online (Sandbox Code Playgroud)
手动编辑 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
}
Run Code Online (Sandbox Code Playgroud)
所以当我执行二进制文件时 - 什么也没有发生
欢迎从这里获取您的灵感
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);
Run Code Online (Sandbox Code Playgroud)