blob: c9b8748cca3c3f3024d15370283ff74e41a81b94 [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"
Anand Shuklae0b51422002-07-16 18:58:08 +00009#include "llvm/Module.h"
Chris Lattnerc56d2392003-01-14 22:39:29 +000010#include "llvm/Pass.h"
Anand Shuklae0b51422002-07-16 18:58:08 +000011
Chris Lattnerf6293092002-07-23 18:06:35 +000012namespace {
13 struct EmitFunctionTable : public Pass {
14 bool run(Module &M);
15 };
16
Chris Lattnera6275cc2002-07-26 21:12:46 +000017 RegisterOpt<EmitFunctionTable> X("emitfuncs", "Emit a Function Table");
Chris Lattnerf6293092002-07-23 18:06:35 +000018}
Anand Shuklae0b51422002-07-16 18:58:08 +000019
Anand Shuklae0b51422002-07-16 18:58:08 +000020// Per Module pass for inserting function table
21bool EmitFunctionTable::run(Module &M){
Chris Lattnerde579f12003-05-22 22:00:07 +000022 std::vector<const Type*> vType;
23 std::vector<Constant *> vConsts;
24 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Anand Shuklae0b51422002-07-16 18:58:08 +000025 if (!MI->isExternal()) {
Anand Shuklae0b51422002-07-16 18:58:08 +000026 vType.push_back(MI->getType());
Chris Lattnerde579f12003-05-22 22:00:07 +000027 vConsts.push_back(ConstantPointerRef::get(MI));
Anand Shuklae0b51422002-07-16 18:58:08 +000028 }
29
30 StructType *sttype = StructType::get(vType);
31 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
32
Chris Lattner4ad02e72003-04-16 20:28:45 +000033 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
34 GlobalValue::ExternalLinkage,
Anand Shuklae0b51422002-07-16 18:58:08 +000035 cstruct, "llvmFunctionTable");
36 M.getGlobalList().push_back(gb);
37 return true; // Always modifies program
38}