2011-02-12 2 views
3

Maintenant que AnnotationManager de LLVM est parti (il a disparu dans la version 2.6, je pense?), Comment puis-je obtenir les annotations pour des fonctions, globales et instructions spécifiques?L'équivalent moderne de LLVM AnnotationManager?

(Par exemple, j'ai compilé à partir de C code binaire void myFunction(__attribute__((annotate("foo"))) int var) --- donné une référence Argument * à cet argument int var, comment pourrais-je déterminer quels attributs annotate y sont attachés?)

Répondre

4

Pour obtenir des annotations pour une fonction spécifique, traverser la BasicBlock d'entrée de la fonction de trouver ses appels à la @llvm.var.annotation intrinsèque, comme suit:

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 a été supprimé car il était inutile (et cela ne résoudra pas votre problème). Toutes les annotations sont gérées via le global nommé 'llvm.global.annotations' et les intrinsèques d'annotation, que vous pouvez sûrement analyser et obtenir les informations dont vous aviez besoin.

Regardez dans IR pour avoir une idée, comment votre code C a été transformé en IR et quel attribut d'annotation a été transformé en.

+0

Eh bien, vous aurez besoin de se pencher sur IR pour les appels de cette fonction, examinez ses arguments et récupérez les informations. Donc, fondamentalement, vous devez "analyser" certaines constructions IR utilisées pour émettre les annotations. –