LLVM C ++ API到底是什么 [英] What exactly is the LLVM C++ API
问题描述
我发现很难理解LLVM C ++ API.
LLVM C ++ API和LLVM IR之间有什么关系?另外,如何使用LLVM C ++ API?
I found it hard to understand the LLVM C++ API.
Is there any relationship between LLVM C++ API and LLVM IR? Also, how could one use the LLVM C++ API?
推荐答案
为简化起见,LLVM是一个用于编写编译器的C ++库.它的C ++ API是该库的用户用来实现其编译器的外部接口.
To (greatly) simplify, LLVM is a C++ library for writing compilers. Its C++ API is the external interface users of the library employ to implement their compiler.
LLVM IR与LLVM C ++ API的一部分(用于构建IR的部分)之间存在一定程度的对称性. http://llvm.org/demo/是获得这种对称感的一个很好的资源.例如,您可以编译以下C代码:
There's a degree of symmetry between LLVM IR and part of the LLVM C++ API - the part used to build IR. A very good resource for getting a feel for this symmetry is http://llvm.org/demo/. For example, you can compile this C code:
int factorial(int X) {
if (X == 0) return 1;
return X*factorial(X-1);
}
进入LLVM IR:
define i32 @factorial(i32 %X) nounwind uwtable readnone {
%1 = icmp eq i32 %X, 0
br i1 %1, label %tailrecurse._crit_edge, label %tailrecurse
tailrecurse: ; preds = %tailrecurse, %0
%X.tr2 = phi i32 [ %2, %tailrecurse ], [ %X, %0 ]
%accumulator.tr1 = phi i32 [ %3, %tailrecurse ], [ 1, %0 ]
%2 = add nsw i32 %X.tr2, -1
%3 = mul nsw i32 %X.tr2, %accumulator.tr1
%4 = icmp eq i32 %2, 0
br i1 %4, label %tailrecurse._crit_edge, label %tailrecurse
tailrecurse._crit_edge: ; preds = %tailrecurse, %0
%accumulator.tr.lcssa = phi i32 [ 1, %0 ], [ %3, %tailrecurse ]
ret i32 %accumulator.tr.lcssa
}
以及C ++ API调用(由于输出很长,因此我不会在此处粘贴它,但是您可以自己尝试).这样做,您将看到例如上述IR代码中的icmp
指令,其执行方式为:
As well as to C++ API calls (I won't paste it here because the output is long, but you can try it yourself). Doing this, you'll see, for example the icmp
instruction from the IR code above done as:
ICmpInst* int1_5 = new ICmpInst(*label_4, ICmpInst::ICMP_EQ, int32_X, const_int32_1, "");
ICmpInst
是C ++ API的一部分,用于创建icmp
指令. 程序员手册.
ICmpInst
is a class that's part of the C++ API used to create icmp
instructions. A good reference for the C++ API is the Programmer's manual.
这篇关于LLVM C ++ API到底是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!