我正在关注 llvm 的教程,了解他们自己的简单编程语言“Kaleidoscope”,我的语言中有一个明显的功能,本教程似乎没有涵盖。我只是想像 C++ 那样将任何 double 打印到标准输出:
std::cout << 5.0;
Run Code Online (Sandbox Code Playgroud)
我的语言会做类似的事情
print(5.0);
Run Code Online (Sandbox Code Playgroud)
llvm 教程的第三章介绍了函数调用。他们使用的代码是:
Value *CallExprAST::codegen() {
// Look up the name in the global module table.
Function *CalleeF = TheModule->getFunction(Callee);
if (!CalleeF)
return ErrorV("Unknown function referenced");
// If argument mismatch error.
if (CalleeF->arg_size() != Args.size())
return ErrorV("Incorrect # arguments passed");
std::vector<Value *> ArgsV;
for (unsigned i = 0, e = Args.size(); i != e; ++i) {
ArgsV.push_back(Args[i]->codegen());
if (!ArgsV.back())
return nullptr;
}
return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
}
Run Code Online (Sandbox Code Playgroud)
如何实现codegen()特定函数调用的方法print(any fp number)?
下面是为 printf("%f", a); 生成的 llvm ir 代码 使用铿锵声。printf 签名是 int printf(const char*, ...);
@.str = private unnamed_addr constant [3 x i8] c"%f\00", align 1
; Function Attrs: nounwind uwtable
define i32 @main() #0 {
%a = alloca double, align 8
%1 = load double* %a, align 8
%2 = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x i8]* @.str, i32 0, i32 0), double %1)
ret i32 0
}
declare i32 @printf(i8*, ...) #1
Run Code Online (Sandbox Code Playgroud)
要在 codegen 中实现,您首先需要检查该函数是否已存在于模块中。如果没有,那么您需要添加声明,您可以在一次调用中完成这两项操作。
Function *CalleeF = TheModule->getOrInsertFunction("printf",
FunctionType::get(IntegerType::getInt32Ty(Context), PointerType::get(Type::getInt8Ty(Context), 0), true /* this is var arg func type*/)
);
Run Code Online (Sandbox Code Playgroud)
上面将获取或添加函数声明的句柄
declare i32 @printf(i8*, ...) #1
Run Code Online (Sandbox Code Playgroud)
然后你可以通过匹配参数调用函数。
std::vector<Value *> ArgsV;
for (unsigned i = 0, e = Args.size(); i != e; ++i)
ArgsV.push_back(Args[i]->codegen());
return Builder.CreateCall(CalleeF, ArgsV, "printfCall");
Run Code Online (Sandbox Code Playgroud)