Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 1 | //===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=// |
| 2 | // |
| 3 | // This inserts a global constant table with function pointers all along |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 7 | #include "llvm/Constants.h" |
| 8 | #include "llvm/DerivedTypes.h" |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 9 | #include "llvm/Module.h" |
Chris Lattner | c56d239 | 2003-01-14 22:39:29 +0000 | [diff] [blame] | 10 | #include "llvm/Pass.h" |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 11 | |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 12 | namespace { |
| 13 | struct EmitFunctionTable : public Pass { |
| 14 | bool run(Module &M); |
| 15 | }; |
| 16 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 17 | RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table"); |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 18 | } |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 19 | |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 20 | // Per Module pass for inserting function table |
| 21 | bool EmitFunctionTable::run(Module &M){ |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 22 | std::vector<const Type*> vType; |
| 23 | std::vector<Constant *> vConsts; |
| 24 | for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI) |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 25 | if (!MI->isExternal()) { |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 26 | vType.push_back(MI->getType()); |
Chris Lattner | de579f1 | 2003-05-22 22:00:07 +0000 | [diff] [blame] | 27 | vConsts.push_back(ConstantPointerRef::get(MI)); |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | StructType *sttype = StructType::get(vType); |
| 31 | ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts); |
| 32 | |
Chris Lattner | 4ad02e7 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 33 | GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, |
| 34 | GlobalValue::ExternalLinkage, |
Anand Shukla | e0b5142 | 2002-07-16 18:58:08 +0000 | [diff] [blame] | 35 | cstruct, "llvmFunctionTable"); |
| 36 | M.getGlobalList().push_back(gb); |
| 37 | return true; // Always modifies program |
| 38 | } |