Writing LLVM int / string input

问题描述 投票:3回答:1

我正在尝试从AST生成llvm-ir。

用于显示我添加的整数输出,

Constant *CalleeF = TheModule->getOrInsertFunction("printf",FunctionType::get(IntegerType::getInt32Ty(Context), PointerType::get(Type::getInt8Ty(Context), 0), true);`

并且在调用我写的打印函数时,

Value* PrintStmt::codegen(){
  Value* V,*val,*to_print;
  vector<Value *> ArgsV;
  for (unsigned int i = 0, e = outs.size(); i != e; ++i){
     to_print = outs[i]->codegen();
     if(outs[i]->type=="int"){
         val=Builder.CreateGlobalStringPtr("%d");
     }
  ArgsV.push_back(val);
  ArgsV.push_back(to_print);
  V =  Builder.CreateCall(CalleeF, ArgsV, "printfCall");
 }
 return V;
}

为了获得用户的输入(即scanf调用,我应该编写什么类似的代码?

c++ llvm llvm-ir
1个回答
0
投票
llvm::FunctionType *readFnType = llvm::FunctionType::get(builder.getInt32Ty(), true); llvm::Function* readfn = llvm::Function::Create(readFnType, llvm::GlobalValue::ExternalLinkage, "scanf", TheModule));

并这样称呼它

std::vector<Value*> ArgsV; // holds codegen IR for each argument
std::string StringFormat; // holds string formatting for all arguments
for(auto& arg : Args){
        if(auto v = arg->codegen()){ 
            if(v->getType()->isDoubleTy())
                StringFormat += "%lf "; // 
            else if(v->getType()->isIntegerTy())
                StringFormat += "%d ";
            ArgsV.push_back(symbolTable[arg.name]);
        }else return nullptr;
    }
ArgsV.insert(ArgsV.begin(), builder.CreateGlobalStringPtr(StringFormat));
return builder.CreateCall(TheModule->getFunction("scanf"), ArgsV, "scanfCall");

其中symbolTable是变量/自变量名称到包含变量的堆栈分配地址的Value*的映射。回想一下scanf采用

要写入的变量的地址,它说明了符号表的查找。 

也值得一提的是,这使scanf本质上不安全。您应该考虑改为使用fgetsgets函数。

© www.soinside.com 2019 - 2024. All rights reserved.