2011-02-12 36 views
3

現在LLVM的AnnotationManager已經消失了(它在2.6版本中消失了,我想?),我怎樣才能獲得特定函數,全局變量和指令的註釋?LLVM AnnotationManager的現代等價物?

(例如,我曾位碼由C編譯void myFunction(__attribute__((annotate("foo"))) int var) ---給予Argument *參照本int var說法,我怎麼可能確定哪些annotate屬性附加到它?)

回答

4

要獲得註釋的特定功能,遍歷函數的入口BasicBlock找到了它的電話到@llvm.var.annotation內在的,如下:

Module *module; 

[...] 

std::string getGlobalVariableString(std::string name) 
{ 
    // assumption: the zeroth operand of a Value::GlobalVariableVal is the actual Value 
    Value *v = module->getNamedValue(name)->getOperand(0); 
    if(v->getValueID() == Value::ConstantArrayVal) 
    { 
     ConstantArray *ca = (ConstantArray *)v; 
     return ca->getAsString(); 
    } 

    return ""; 
} 

void dumpFunctionArgAnnotations(std::string funcName) 
{ 
    std::map<Value *,Argument*> mapValueToArgument; 

    Function *func = module->getFunction(funcName); 
    if(!func) 
    { 
     std::cout << "no function by that name.\n"; 
     return; 
    } 

    std::cout << funcName << "() ====================\n"; 


    // assumption: @llvm.var.annotation calls are always in the function's entry block. 
    BasicBlock *b = &func->getEntryBlock(); 


    // run through entry block first to build map of pointers to arguments 
    for(BasicBlock::iterator it = b->begin();it!=b->end();++it) 
    { 
     Instruction *inst = it; 
     if(inst->getOpcode()!=Instruction::Store) 
      continue; 

     // `store` operands: http://llvm.org/docs/LangRef.html#i_store 
     mapValueToArgument[inst->getOperand(1)] = (Argument *)inst->getOperand(0); 
    } 


    // run through entry block a second time, to associate annotations with arguments 
    for(BasicBlock::iterator it = b->begin();it!=b->end();++it) 
    { 
     Instruction *inst = it; 
     if(inst->getOpcode()!=Instruction::Call) 
      continue; 

     // assumption: Instruction::Call's operands are the function arguments, followed by the function name 
     Value *calledFunction = inst->getOperand(inst->getNumOperands()-1); 
     if(calledFunction->getName().str() != "llvm.var.annotation") 
      continue; 

     // `llvm.var.annotation` operands: http://llvm.org/docs/LangRef.html#int_var_annotation 

     Value *annotatedValue = inst->getOperand(0); 
     if(annotatedValue->getValueID() != Value::InstructionVal + Instruction::BitCast) 
      continue; 
     Argument *a = mapValueToArgument[annotatedValue->getUnderlyingObject()]; 
     if(!a) 
      continue; 

     Value *annotation = inst->getOperand(1); 
     if(annotation->getValueID() != Value::ConstantExprVal) 
      continue; 
     ConstantExpr *ce = (ConstantExpr *)annotation; 
     if(ce->getOpcode() != Instruction::GetElementPtr) 
      continue; 

     // `ConstantExpr` operands: http://llvm.org/docs/LangRef.html#constantexprs 
     Value *gv = ce->getOperand(0); 

     if(gv->getValueID() != Value::GlobalVariableVal) 
      continue; 

     std::cout << " argument " << a->getType()->getDescription() << " " << a->getName().str() 
      << " has annotation \"" << getGlobalVariableString(gv->getName().str()) << "\"\n"; 
    } 
} 
2

AnnotationManager被刪除,因爲它是沒用(它不會解決你的問題)。所有註釋都通過全局命名的'llvm.global.annotations'和註釋內在函數進行處理,您可以確定地解析並獲取所需的信息。

看看IR有一個想法,你的C代碼如何轉換成IR和什麼註釋屬性轉化爲。

+0

好吧,你需要尋找到IR的電話的功能,查看它的參數並從中獲取信息。所以,基本上,您需要「解析」用於發出註釋的一些IR構造。 –