在llvm中的PassManager中添加TargetData时出错

时间:2018-08-13 18:26:40

标签: llvm llvm-ir

我是llvm的初学者。我正在实现https://github.com/davidar/lljvm/blob/master/backend/main.cpp的main.cpp程序。当我声明PassManager PM PM.add(new DataLayout td)时,会引发错误:

error: missing template arguments before ‘PM’
  PassManager PM;

考虑到以上错误,我将命令更改为

PassManger<Module> PM
PM.add(new DataLayout td)

现在,有另一个错误:error: ‘class llvm::PassManager<llvm::Module>’ has no member named ‘add’我不知道上述更改是否正确?  这是源代码:

#include "backened.h"
#include <iostream>
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/PassManager.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/IR/DataLayout.h"
#include <llvm/Transforms/Scalar.h>
#include "llvm/IR/LegacyPassManager.h"

using namespace llvm;
using namespace std;    
static cl::opt<string> input(cl::Positional, cl::desc("Bitcode File.."),cl::Required);
static cl::opt<string> classname("classname",cl::desc("Binary name of the generated class..."));    
int main(int argc, char** argv)
{
        cl::ParseCommandLineOptions(argc, argv, "LLVM Bitcode ..");
        LLVMContext context; 

        ErrorOr<unique_ptr<MemoryBuffer>>  mb = MemoryBuffer::getFile(input);
        if(error_code ec = mb.getError())
        {
                errs() << ec.message();
                return 1;
        }    
        Expected<unique_ptr<Module>> m = parseBitcodeFile(mb->get()->getMemBufferRef(),context);
        if(error_code ec= errorToErrorCode(m.takeError()) )
        {

        PassManager<Module> PM;
        DataLayout td("e-p:32:32:32"
                 "-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64"
                 "-f32:32:32-f64:64:64");
        PM.add(new DataLayout(td));
         return 0;
}

另外,建议我是否已实施正确的方法,因为我发现从较新版本的llvm中删除了TargetData.h,而用DataLayout td代替了TargetData td

1 个答案:

答案 0 :(得分:0)

该示例中使用的PassManager已移至旧名称空间

请使用#include "llvm/IR/LegacyPassManager.h"

将变量声明更改为legacy::PassManager PM;