blob: 8207424efd1fd4298e823b101348b98ee0d87cfc [file] [log] [blame]
Anand Shuklae0b51422002-07-16 18:58:08 +00001//===-- EmitFunctions.cpp - interface to insert instrumentation --*- C++ -*--=//
2//
3// This inserts a global constant table with function pointers all along
4//
5//===----------------------------------------------------------------------===//
6
Anand Shuklae0b51422002-07-16 18:58:08 +00007#include "llvm/Constants.h"
8#include "llvm/DerivedTypes.h"
9#include "llvm/Constants.h"
10#include "llvm/Module.h"
11
12using std::vector;
13
Chris Lattnerf6293092002-07-23 18:06:35 +000014namespace {
15 struct EmitFunctionTable : public Pass {
16 bool run(Module &M);
17 };
18
Chris Lattnera6275cc2002-07-26 21:12:46 +000019 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
Chris Lattnerf6293092002-07-23 18:06:35 +000020}
Anand Shuklae0b51422002-07-16 18:58:08 +000021
Anand Shuklae0b51422002-07-16 18:58:08 +000022// Per Module pass for inserting function table
23bool EmitFunctionTable::run(Module &M){
24 vector<const Type*> vType;
25 vector<Constant *> vConsts;
26 for(Module::iterator MI = M.begin(), ME = M.end(); MI!=ME; ++MI)
27 if (!MI->isExternal()) {
28 ConstantPointerRef *CP = ConstantPointerRef::get(MI);
29 vType.push_back(MI->getType());
30 vConsts.push_back(CP);
31 }
32
33 StructType *sttype = StructType::get(vType);
34 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
35
36 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true, false,
37 cstruct, "llvmFunctionTable");
38 M.getGlobalList().push_back(gb);
39 return true; // Always modifies program
40}