Added the #(internal functions) to output
[oota-llvm.git] / lib / Transforms / Instrumentation / EmitFunctions.cpp
1 //===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
2 //
3 // This inserts a global constant table with function pointers all along
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Constants.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/Module.h"
10 #include "llvm/Pass.h"
11
12 namespace {
13   struct EmitFunctionTable : public Pass {
14     bool run(Module &M);
15   };
16   
17   RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
18 }
19
20 // Per Module pass for inserting function table
21 bool EmitFunctionTable::run(Module &M){
22   std::vector<const Type*> vType;
23   std::vector<Constant *> vConsts;
24   unsigned char counter = 0;
25   for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
26     if (!MI->isExternal()) {
27       vType.push_back(MI->getType());
28       vConsts.push_back(ConstantPointerRef::get(MI));
29       counter++;
30     }
31   
32   StructType *sttype = StructType::get(vType);
33   ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
34
35   GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
36                                           GlobalValue::ExternalLinkage, 
37                                           cstruct, "llvmFunctionTable");
38   M.getGlobalList().push_back(gb);
39
40   ConstantInt *cnst = ConstantInt::get(Type::IntTy, counter); 
41   GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true, 
42                                                GlobalValue::ExternalLinkage, 
43                                                cnst, "llvmFunctionCount");
44   M.getGlobalList().push_back(fnCount);
45   return true;  // Always modifies program
46 }