2004-06-03 05:03:37 +00:00
|
|
|
//===-- InternalGlobalMapper.cpp - Mapping Info for Internal Globals ------===//
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2004-06-03 05:03:37 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:30:14 +00:00
|
|
|
//
|
2004-06-03 05:03:37 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// InternalGlobalMapper is a pass that helps the runtime trace optimizer map
|
|
|
|
// the names of internal GlobalValues (which may have mangled,
|
|
|
|
// unreconstructible names in the executable) to pointers. If the name mangler
|
|
|
|
// is changed at some point in the future to allow its results to be
|
|
|
|
// reconstructible (for instance, by making the type mangling symbolic instead
|
|
|
|
// of using a UniqueID) this pass should probably be phased out.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
typedef std::vector<Constant *> GVVectorTy;
|
|
|
|
|
2004-11-30 00:22:59 +00:00
|
|
|
namespace {
|
|
|
|
struct InternalGlobalMapper : public ModulePass {
|
|
|
|
bool runOnModule(Module &M);
|
|
|
|
};
|
|
|
|
}
|
2004-06-03 05:03:37 +00:00
|
|
|
|
2004-11-30 00:22:59 +00:00
|
|
|
namespace llvm {
|
|
|
|
ModulePass *createInternalGlobalMapperPass() {
|
|
|
|
return new InternalGlobalMapper();
|
|
|
|
}
|
2004-06-03 05:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void maybeAddInternalValueToVector (GVVectorTy &Vector, GlobalValue &GV){
|
|
|
|
// If it's a GlobalValue with internal linkage and a name (i.e. it's going to
|
|
|
|
// be mangled), then put the GV, casted to sbyte*, in the vector. Otherwise
|
|
|
|
// add a null.
|
|
|
|
if (GV.hasInternalLinkage () && GV.hasName ())
|
2004-09-20 04:48:05 +00:00
|
|
|
Vector.push_back(ConstantExpr::getCast(&GV,
|
|
|
|
PointerType::get(Type::SByteTy)));
|
2004-06-03 05:03:37 +00:00
|
|
|
else
|
|
|
|
Vector.push_back (ConstantPointerNull::get (PointerType::get
|
|
|
|
(Type::SByteTy)));
|
|
|
|
}
|
|
|
|
|
2004-09-20 04:48:05 +00:00
|
|
|
bool InternalGlobalMapper::runOnModule(Module &M) {
|
2004-06-03 05:03:37 +00:00
|
|
|
GVVectorTy gvvector;
|
|
|
|
|
|
|
|
// Populate the vector with internal global values and their names.
|
2005-03-15 04:54:21 +00:00
|
|
|
for (Module::global_iterator i = M.global_begin (), e = M.global_end (); i != e; ++i)
|
2004-06-03 05:03:37 +00:00
|
|
|
maybeAddInternalValueToVector (gvvector, *i);
|
2004-06-08 20:08:30 +00:00
|
|
|
// Add an extra global for _llvm_internalGlobals itself (null,
|
|
|
|
// because it's not internal)
|
|
|
|
gvvector.push_back (ConstantPointerNull::get
|
|
|
|
(PointerType::get (Type::SByteTy)));
|
2004-06-03 05:03:37 +00:00
|
|
|
for (Module::iterator i = M.begin (), e = M.end (); i != e; ++i)
|
|
|
|
maybeAddInternalValueToVector (gvvector, *i);
|
|
|
|
|
|
|
|
// Convert the vector to a constant struct of type {Size, [Size x sbyte*]}.
|
|
|
|
ArrayType *ATy = ArrayType::get (PointerType::get (Type::SByteTy),
|
|
|
|
gvvector.size ());
|
|
|
|
std::vector<const Type *> FieldTypes;
|
|
|
|
FieldTypes.push_back (Type::UIntTy);
|
|
|
|
FieldTypes.push_back (ATy);
|
|
|
|
StructType *STy = StructType::get (FieldTypes);
|
|
|
|
std::vector<Constant *> FieldValues;
|
|
|
|
FieldValues.push_back (ConstantUInt::get (Type::UIntTy, gvvector.size ()));
|
|
|
|
FieldValues.push_back (ConstantArray::get (ATy, gvvector));
|
2005-04-21 23:30:14 +00:00
|
|
|
|
2004-06-03 05:03:37 +00:00
|
|
|
// Add the constant struct to M as an external global symbol named
|
|
|
|
// "_llvm_internalGlobals".
|
|
|
|
new GlobalVariable (STy, true, GlobalValue::ExternalLinkage,
|
|
|
|
ConstantStruct::get (STy, FieldValues),
|
|
|
|
"_llvm_internalGlobals", &M);
|
|
|
|
|
|
|
|
return true; // Module was modified.
|
|
|
|
}
|