blob: 7b9810748fbc91733ab5c6e65778a6cdfa3495c7 [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;
Anand Shukla619754f2003-06-01 02:40:49 +000024 unsigned char counter = 0;
Chris Lattnerde579f12003-05-22 22:00:07 +000025 for(Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
Anand Shuklae0b51422002-07-16 18:58:08 +000026 if (!MI->isExternal()) {
Anand Shuklae0b51422002-07-16 18:58:08 +000027 vType.push_back(MI->getType());
Chris Lattnerde579f12003-05-22 22:00:07 +000028 vConsts.push_back(ConstantPointerRef::get(MI));
Anand Shukla619754f2003-06-01 02:40:49 +000029 counter++;
Anand Shuklae0b51422002-07-16 18:58:08 +000030 }
31
32 StructType *sttype = StructType::get(vType);
33 ConstantStruct *cstruct = ConstantStruct::get(sttype, vConsts);
34
Chris Lattner4ad02e72003-04-16 20:28:45 +000035 GlobalVariable *gb = new GlobalVariable(cstruct->getType(), true,
36 GlobalValue::ExternalLinkage,
Anand Shuklae0b51422002-07-16 18:58:08 +000037 cstruct, "llvmFunctionTable");
38 M.getGlobalList().push_back(gb);
Anand Shukla619754f2003-06-01 02:40:49 +000039
Chris Lattnerfa9ee732003-06-04 20:08:47 +000040 ConstantInt *cnst = ConstantSInt::get(Type::IntTy, counter);
Anand Shukla619754f2003-06-01 02:40:49 +000041 GlobalVariable *fnCount = new GlobalVariable(Type::IntTy, true,
42 GlobalValue::ExternalLinkage,
43 cnst, "llvmFunctionCount");
44 M.getGlobalList().push_back(fnCount);
Anand Shuklae0b51422002-07-16 18:58:08 +000045 return true; // Always modifies program
46}