打印LLVM::Type或者LLVM::Value的值

#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>
#include "iostream"

using namespace llvm;
/// Returns the string representation of a llvm::Value* or llvm::Type*  
template <typename T> static std::string Print(T* value_or_type) {
    std::string str;
    llvm::raw_string_ostream stream(str);
    value_or_type->print(stream);
    return str;
}

int main() {

    LLVMContext Context;

    // Create some module to put our function into it.
    std::unique_ptr<Module> Owner = make_unique<Module>("test", Context);
    Module *mod = Owner.get();

    /*
      //param numBits the bit width of the constructed APInt
      //param str the string to be interpreted
      //param radix the radix to use for the conversion
      APInt(unsigned numBits, StringRef str, uint8_t radix);

      //ConstantInt int type constant 
    */
    ConstantInt* const_int32_one = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));

    std::string result = Print(const_int32_one);
    std::cout << result << std::endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章