Advertisement
Guest User

testing

a guest
Jul 25th, 2013
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include "llvm/Pass.h"                                  
  2. #include "llvm/IR/Module.h"
  3. #include "llvm/IR/Function.h"
  4. #include "llvm/Support/raw_ostream.h"
  5. #include "llvm/IR/Type.h"
  6. #include "llvm/IR/DerivedTypes.h"
  7. #include "llvm/IR/Instructions.h"
  8. #include "llvm/IR/Instruction.h"
  9. #include "llvm/IR/IRBuilder.h"
  10. #include "llvm/IR/User.h"
  11. #include "llvm/IR/Value.h"
  12. #include "llvm/IR/Constant.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "llvm/Support/InstIterator.h"
  15. #include "sstream"
  16. #include "string"
  17. #include "list"
  18. #include "vector"
  19. using namespace llvm;
  20. using namespace std;
  21.  
  22. namespace
  23. {
  24.     struct testing : public ModulePass
  25.     {
  26.             static char ID;  
  27.             Function *hook;
  28.         LLVMContext* Context;      
  29.         int rtrn;
  30.         string expr,lhsvar;    
  31.         list<string> oprnd;    
  32.         list<string>::iterator oprnditer;
  33.             testing() : ModulePass(ID) {}
  34.  
  35.         int abc(Instruction *vi)
  36.         {
  37.             stringstream opcode,operands;
  38.             if(isa<LoadInst>(*vi) || isa<AllocaInst>(*vi))
  39.             {
  40.                 if(isa<LoadInst>(*vi))
  41.                 {                  
  42.                     operands << vi->getOperand(0);
  43.                     expr+=operands.str()+"_";
  44.                     oprnd.push_back(operands.str());
  45.                 }
  46.                 errs() << "\t\t" << *vi << "\n";               
  47.                 return 0;
  48.             }
  49.             opcode << vi->getOpcode();
  50.             expr+=opcode.str()+"_";
  51.             errs() << "\t\t" << *vi << "\n";
  52.             for(User::op_iterator i = vi->op_begin(); i != vi->op_end(); ++i)
  53.             {
  54.                 if(Instruction *ti = dyn_cast<Instruction>(*i))
  55.                     rtrn=testing::abc(ti);
  56.             }
  57.             return 1;
  58.         }
  59.        
  60.             virtual bool runOnModule(Module &M)
  61.             {      
  62.             for(Module::iterator F = M.begin(); F != M.end(); ++F)
  63.             {
  64.                 for(Function::iterator BB = F->begin(); BB != F->end(); ++BB)
  65.                 {
  66.                     vector<Instruction*> worklist;
  67.                     vector<Instruction*>::iterator worklistiter;
  68.                     for(inst_iterator I = inst_begin(F); I != inst_end(F); ++I)
  69.                     {
  70.                                 worklist.push_back(&*I);
  71.                         }
  72.                     for(worklistiter = worklist.begin(); worklistiter != worklist.end(); ++worklistiter)
  73.                     {
  74.                         Instruction* instr = *worklistiter;
  75.                         expr="";
  76.                         oprnd.clear();
  77.                         errs() << "use: " <<*instr << "\n";
  78.                         for (User::op_iterator i = instr->op_begin(); i != instr->op_end(); ++i)
  79.                         {
  80.                             if(Instruction *vi = dyn_cast<Instruction>(*i))
  81.                             {  
  82.                                 //errs() << "\t\t" << *vi << "\n"; //<< "\t\t" << instr->getOpcode()
  83.                                 if(isa<StoreInst>(*instr))
  84.                                 {
  85.                                     if(!isa<AllocaInst>(*vi))
  86.                                     {
  87.                                         rtrn=testing::abc(vi);
  88.                                         errs() << "expr: " << expr << "\n";
  89.                                         errs() << "operands: ";
  90.                                         for(oprnditer=oprnd.begin();oprnditer!=oprnd.end();++oprnditer)
  91.                                             errs() << *oprnditer << " ";
  92.                                     }
  93.                                     else
  94.                                     {
  95.                                         stringstream lhs;
  96.                                         lhs << instr->getOperand(1);
  97.                                         lhsvar=lhs.str();
  98.                                         errs() << "\n\t\t" << *vi << "\n";
  99.                                         errs() << "LHS variable: " << lhsvar << "\n\n";
  100.                                     }
  101.                                 }
  102.                             }
  103.                         }
  104.                         if(isa<StoreInst>(*instr))
  105.                         {
  106.                             //need to call a external function named  //hashtable(string), which will take "expr" which has been computed above as an arument.
  107.                         }
  108.                     }
  109.                 }
  110.             }
  111.             return false;
  112.         }
  113.     };
  114. }
  115. char testing::ID = 0;
  116. static RegisterPass<testing> X("testing", "test function exist", false, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement