LLVM学习日志6——另一个简单例子ModuleMaker,浅读调用LLVM的API及函数

发现LLVM的调用还是看不太懂,先来学个小例子。

对了,刚发现,在llvm的examples中有各种例子的源码可以看,包括之前那个Kaleidoscope

这个ModuleMaker

照例读源码

第一句是定义context,到现在我还不知道具体有什么用,看文档说LLVMContext提供隔离,LLVM IR内存中的每个实体都属于一个context。只是看后面的定义,我猜测,就是画了个框,同在这个框中的就能愉快的玩耍。

第二句是创建一个module,取名为test,一般来说module掌管这个工程,可以包含很多函数等。

第三句是是创建一个function,看英语大概就能看懂是创建int类型,false表示不是vararg函数,但什么是vararg函数,我还不是很清楚

第四句,和第三句不同,FunctionType是定义类型,而Function就是定义函数。

下面是我的想法。我感觉是创建了一个主函数,第一个参数是返回值是一个int型,第二个是说外部链接(大概是说函数开始的位置,连接的位置),第三个是函数的名字,第四个是父类是谁,这里是module 的M(说法有点不对,意思差不多)。

第五句,创建了一个基本块。基本块的概念可以看看百度百科,使用函数BasicBlock::Create来创建的,第一个是属于哪儿的,第二个是名字,第三个也是父类,意思差不多就是这一个基本块是给函数F用的。

第六第七句一起看,value就是创建变量的(可能不对),这句就是创建了两个智障,分别指向了int常量2,3

第八句是创建加法函数,BinaryOperator::Create是创建运算的,第一个参数是运算类型,第二第三个就是输入参数,第四个不清楚是取名结果还是这个运算

第九句是把创建的加法放在基本块中

第十句是创建返回值,还不是很懂,先按照这个来吧,但感觉这个是返回一个instruction,因为ReturnInst。不知道怎么返回一个参数

第十一句就是输出,还是不懂呀

后面的比较简单,就不说了。

对了,编译的方法可以这样

clang++ -g xxx.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core native` -O3 -o xxx

#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/raw_ostream.h"

using namespace llvm;

int main() {
  LLVMContext Context;

  // Create the "module" or "program" or "translation unit" to hold the
  // function
  Module *M = new Module("test", Context);

  // Create the main function: first create the type 'int ()'
  FunctionType *FT =
    FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);

  // By passing a module as the last parameter to the Function constructor,
  // it automatically gets appended to the Module.
  Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);

  // Add a basic block to the function... again, it automatically inserts
  // because of the last argument.
  BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);

  // Get pointers to the constant integers...
  Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
  Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);

  // Create the add instruction... does not insert...
  Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
                                            "addresult");

  // explicitly insert it into the basic block...
  BB->getInstList().push_back(Add);

  // Create the return instruction and add it to the basic block
  BB->getInstList().push_back(ReturnInst::Create(Context, Add));

  // Output the bitcode file to stdout
  WriteBitcodeToFile(*M, outs());

  // Delete the module and all of its contents.
  delete M;
  return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值