2014-11-08 05:32:08 +08:00
|
|
|
//===- SymbolRewriter.cpp - Symbol Rewriter ---------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// SymbolRewriter is a LLVM pass which can rewrite symbols transparently within
|
|
|
|
// existing code. It is implemented as a compiler pass and is configured via a
|
|
|
|
// YAML configuration file.
|
|
|
|
//
|
|
|
|
// The YAML configuration file format is as follows:
|
|
|
|
//
|
|
|
|
// RewriteMapFile := RewriteDescriptors
|
|
|
|
// RewriteDescriptors := RewriteDescriptor | RewriteDescriptors
|
|
|
|
// RewriteDescriptor := RewriteDescriptorType ':' '{' RewriteDescriptorFields '}'
|
|
|
|
// RewriteDescriptorFields := RewriteDescriptorField | RewriteDescriptorFields
|
|
|
|
// RewriteDescriptorField := FieldIdentifier ':' FieldValue ','
|
|
|
|
// RewriteDescriptorType := Identifier
|
|
|
|
// FieldIdentifier := Identifier
|
|
|
|
// FieldValue := Identifier
|
|
|
|
// Identifier := [0-9a-zA-Z]+
|
|
|
|
//
|
|
|
|
// Currently, the following descriptor types are supported:
|
|
|
|
//
|
|
|
|
// - function: (function rewriting)
|
|
|
|
// + Source (original name of the function)
|
|
|
|
// + Target (explicit transformation)
|
|
|
|
// + Transform (pattern transformation)
|
|
|
|
// + Naked (boolean, whether the function is undecorated)
|
|
|
|
// - global variable: (external linkage global variable rewriting)
|
|
|
|
// + Source (original name of externally visible variable)
|
|
|
|
// + Target (explicit transformation)
|
|
|
|
// + Transform (pattern transformation)
|
|
|
|
// - global alias: (global alias rewriting)
|
|
|
|
// + Source (original name of the aliased name)
|
|
|
|
// + Target (explicit transformation)
|
|
|
|
// + Transform (pattern transformation)
|
|
|
|
//
|
|
|
|
// Note that source and exactly one of [Target, Transform] must be provided
|
|
|
|
//
|
|
|
|
// New rewrite descriptors can be created. Addding a new rewrite descriptor
|
|
|
|
// involves:
|
|
|
|
//
|
|
|
|
// a) extended the rewrite descriptor kind enumeration
|
|
|
|
// (<anonymous>::RewriteDescriptor::RewriteDescriptorType)
|
|
|
|
// b) implementing the new descriptor
|
|
|
|
// (c.f. <anonymous>::ExplicitRewriteFunctionDescriptor)
|
|
|
|
// c) extending the rewrite map parser
|
|
|
|
// (<anonymous>::RewriteMapParser::parseEntry)
|
|
|
|
//
|
|
|
|
// Specify to rewrite the symbols using the `-rewrite-symbols` option, and
|
|
|
|
// specify the map file to use for the rewriting via the `-rewrite-map-file`
|
|
|
|
// option.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "symbol-rewriter"
|
2016-07-26 04:52:00 +08:00
|
|
|
#include "llvm/Transforms/Utils/SymbolRewriter.h"
|
2014-11-08 05:32:08 +08:00
|
|
|
#include "llvm/Pass.h"
|
2015-03-24 02:07:13 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-02-13 18:01:29 +08:00
|
|
|
#include "llvm/IR/LegacyPassManager.h"
|
2014-11-08 05:32:08 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Regex.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
#include "llvm/Support/YAMLParser.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2015-03-09 23:50:47 +08:00
|
|
|
using namespace SymbolRewriter;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
static cl::list<std::string> RewriteMapFiles("rewrite-map-file",
|
|
|
|
cl::desc("Symbol Rewrite Map"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
2015-03-09 23:50:47 +08:00
|
|
|
static void rewriteComdat(Module &M, GlobalObject *GO,
|
|
|
|
const std::string &Source,
|
|
|
|
const std::string &Target) {
|
2015-01-28 06:57:39 +08:00
|
|
|
if (Comdat *CD = GO->getComdat()) {
|
|
|
|
auto &Comdats = M.getComdatSymbolTable();
|
|
|
|
|
|
|
|
Comdat *C = M.getOrInsertComdat(Target);
|
|
|
|
C->setSelectionKind(CD->getSelectionKind());
|
|
|
|
GO->setComdat(C);
|
|
|
|
|
|
|
|
Comdats.erase(Comdats.find(Source));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 23:50:47 +08:00
|
|
|
namespace {
|
2014-11-08 05:32:08 +08:00
|
|
|
template <RewriteDescriptor::Type DT, typename ValueType,
|
|
|
|
ValueType *(llvm::Module::*Get)(StringRef) const>
|
|
|
|
class ExplicitRewriteDescriptor : public RewriteDescriptor {
|
|
|
|
public:
|
|
|
|
const std::string Source;
|
|
|
|
const std::string Target;
|
|
|
|
|
|
|
|
ExplicitRewriteDescriptor(StringRef S, StringRef T, const bool Naked)
|
|
|
|
: RewriteDescriptor(DT), Source(Naked ? StringRef("\01" + S.str()) : S),
|
|
|
|
Target(T) {}
|
|
|
|
|
|
|
|
bool performOnModule(Module &M) override;
|
|
|
|
|
|
|
|
static bool classof(const RewriteDescriptor *RD) {
|
|
|
|
return RD->getType() == DT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <RewriteDescriptor::Type DT, typename ValueType,
|
|
|
|
ValueType *(llvm::Module::*Get)(StringRef) const>
|
|
|
|
bool ExplicitRewriteDescriptor<DT, ValueType, Get>::performOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
if (ValueType *S = (M.*Get)(Source)) {
|
2015-01-28 06:57:39 +08:00
|
|
|
if (GlobalObject *GO = dyn_cast<GlobalObject>(S))
|
|
|
|
rewriteComdat(M, GO, Source, Target);
|
|
|
|
|
2014-11-08 05:32:08 +08:00
|
|
|
if (Value *T = (M.*Get)(Target))
|
|
|
|
S->setValueName(T->getValueName());
|
|
|
|
else
|
|
|
|
S->setName(Target);
|
2015-01-28 06:57:39 +08:00
|
|
|
|
2014-11-08 05:32:08 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <RewriteDescriptor::Type DT, typename ValueType,
|
|
|
|
ValueType *(llvm::Module::*Get)(StringRef) const,
|
2015-01-06 01:56:29 +08:00
|
|
|
iterator_range<typename iplist<ValueType>::iterator>
|
|
|
|
(llvm::Module::*Iterator)()>
|
2014-11-08 05:32:08 +08:00
|
|
|
class PatternRewriteDescriptor : public RewriteDescriptor {
|
|
|
|
public:
|
|
|
|
const std::string Pattern;
|
|
|
|
const std::string Transform;
|
|
|
|
|
|
|
|
PatternRewriteDescriptor(StringRef P, StringRef T)
|
|
|
|
: RewriteDescriptor(DT), Pattern(P), Transform(T) { }
|
|
|
|
|
|
|
|
bool performOnModule(Module &M) override;
|
|
|
|
|
|
|
|
static bool classof(const RewriteDescriptor *RD) {
|
|
|
|
return RD->getType() == DT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <RewriteDescriptor::Type DT, typename ValueType,
|
|
|
|
ValueType *(llvm::Module::*Get)(StringRef) const,
|
2015-01-06 01:56:29 +08:00
|
|
|
iterator_range<typename iplist<ValueType>::iterator>
|
|
|
|
(llvm::Module::*Iterator)()>
|
2014-11-08 05:32:08 +08:00
|
|
|
bool PatternRewriteDescriptor<DT, ValueType, Get, Iterator>::
|
|
|
|
performOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (auto &C : (M.*Iterator)()) {
|
|
|
|
std::string Error;
|
|
|
|
|
|
|
|
std::string Name = Regex(Pattern).sub(Transform, C.getName(), &Error);
|
|
|
|
if (!Error.empty())
|
|
|
|
report_fatal_error("unable to transforn " + C.getName() + " in " +
|
|
|
|
M.getModuleIdentifier() + ": " + Error);
|
|
|
|
|
2015-01-28 06:57:35 +08:00
|
|
|
if (C.getName() == Name)
|
|
|
|
continue;
|
|
|
|
|
2015-01-28 06:57:39 +08:00
|
|
|
if (GlobalObject *GO = dyn_cast<GlobalObject>(&C))
|
|
|
|
rewriteComdat(M, GO, C.getName(), Name);
|
|
|
|
|
2014-11-08 05:32:08 +08:00
|
|
|
if (Value *V = (M.*Get)(Name))
|
|
|
|
C.setValueName(V->getValueName());
|
|
|
|
else
|
|
|
|
C.setName(Name);
|
|
|
|
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a rewrite for an explicitly named (function) symbol. Both the
|
|
|
|
/// source function name and target function name of the transformation are
|
|
|
|
/// explicitly spelt out.
|
2014-11-08 06:09:52 +08:00
|
|
|
typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::Function,
|
|
|
|
llvm::Function, &llvm::Module::getFunction>
|
|
|
|
ExplicitRewriteFunctionDescriptor;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
/// Represents a rewrite for an explicitly named (global variable) symbol. Both
|
|
|
|
/// the source variable name and target variable name are spelt out. This
|
|
|
|
/// applies only to module level variables.
|
2014-11-08 06:09:52 +08:00
|
|
|
typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
|
|
|
|
llvm::GlobalVariable,
|
|
|
|
&llvm::Module::getGlobalVariable>
|
|
|
|
ExplicitRewriteGlobalVariableDescriptor;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
/// Represents a rewrite for an explicitly named global alias. Both the source
|
|
|
|
/// and target name are explicitly spelt out.
|
2014-11-08 06:09:52 +08:00
|
|
|
typedef ExplicitRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
|
|
|
|
llvm::GlobalAlias,
|
|
|
|
&llvm::Module::getNamedAlias>
|
|
|
|
ExplicitRewriteNamedAliasDescriptor;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
/// Represents a rewrite for a regular expression based pattern for functions.
|
|
|
|
/// A pattern for the function name is provided and a transformation for that
|
|
|
|
/// pattern to determine the target function name create the rewrite rule.
|
2014-11-08 06:09:52 +08:00
|
|
|
typedef PatternRewriteDescriptor<RewriteDescriptor::Type::Function,
|
|
|
|
llvm::Function, &llvm::Module::getFunction,
|
|
|
|
&llvm::Module::functions>
|
|
|
|
PatternRewriteFunctionDescriptor;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
/// Represents a rewrite for a global variable based upon a matching pattern.
|
|
|
|
/// Each global variable matching the provided pattern will be transformed as
|
|
|
|
/// described in the transformation pattern for the target. Applies only to
|
|
|
|
/// module level variables.
|
2014-11-08 06:09:52 +08:00
|
|
|
typedef PatternRewriteDescriptor<RewriteDescriptor::Type::GlobalVariable,
|
|
|
|
llvm::GlobalVariable,
|
|
|
|
&llvm::Module::getGlobalVariable,
|
|
|
|
&llvm::Module::globals>
|
|
|
|
PatternRewriteGlobalVariableDescriptor;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
/// PatternRewriteNamedAliasDescriptor - represents a rewrite for global
|
|
|
|
/// aliases which match a given pattern. The provided transformation will be
|
|
|
|
/// applied to each of the matching names.
|
2014-11-08 06:09:52 +08:00
|
|
|
typedef PatternRewriteDescriptor<RewriteDescriptor::Type::NamedAlias,
|
|
|
|
llvm::GlobalAlias,
|
|
|
|
&llvm::Module::getNamedAlias,
|
|
|
|
&llvm::Module::aliases>
|
|
|
|
PatternRewriteNamedAliasDescriptor;
|
2015-03-09 23:50:47 +08:00
|
|
|
} // namespace
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
bool RewriteMapParser::parse(const std::string &MapFile,
|
|
|
|
RewriteDescriptorList *DL) {
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> Mapping =
|
|
|
|
MemoryBuffer::getFile(MapFile);
|
|
|
|
|
|
|
|
if (!Mapping)
|
|
|
|
report_fatal_error("unable to read rewrite map '" + MapFile + "': " +
|
|
|
|
Mapping.getError().message());
|
|
|
|
|
|
|
|
if (!parse(*Mapping, DL))
|
|
|
|
report_fatal_error("unable to parse rewrite map '" + MapFile + "'");
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteMapParser::parse(std::unique_ptr<MemoryBuffer> &MapFile,
|
|
|
|
RewriteDescriptorList *DL) {
|
|
|
|
SourceMgr SM;
|
|
|
|
yaml::Stream YS(MapFile->getBuffer(), SM);
|
|
|
|
|
|
|
|
for (auto &Document : YS) {
|
|
|
|
yaml::MappingNode *DescriptorList;
|
|
|
|
|
|
|
|
// ignore empty documents
|
|
|
|
if (isa<yaml::NullNode>(Document.getRoot()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DescriptorList = dyn_cast<yaml::MappingNode>(Document.getRoot());
|
|
|
|
if (!DescriptorList) {
|
|
|
|
YS.printError(Document.getRoot(), "DescriptorList node must be a map");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &Descriptor : *DescriptorList)
|
|
|
|
if (!parseEntry(YS, Descriptor, DL))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteMapParser::parseEntry(yaml::Stream &YS, yaml::KeyValueNode &Entry,
|
|
|
|
RewriteDescriptorList *DL) {
|
|
|
|
yaml::ScalarNode *Key;
|
|
|
|
yaml::MappingNode *Value;
|
|
|
|
SmallString<32> KeyStorage;
|
|
|
|
StringRef RewriteType;
|
|
|
|
|
|
|
|
Key = dyn_cast<yaml::ScalarNode>(Entry.getKey());
|
|
|
|
if (!Key) {
|
|
|
|
YS.printError(Entry.getKey(), "rewrite type must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value = dyn_cast<yaml::MappingNode>(Entry.getValue());
|
|
|
|
if (!Value) {
|
|
|
|
YS.printError(Entry.getValue(), "rewrite descriptor must be a map");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RewriteType = Key->getValue(KeyStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
if (RewriteType.equals("function"))
|
2014-11-08 05:32:08 +08:00
|
|
|
return parseRewriteFunctionDescriptor(YS, Key, Value, DL);
|
2014-11-08 08:00:50 +08:00
|
|
|
else if (RewriteType.equals("global variable"))
|
2014-11-08 05:32:08 +08:00
|
|
|
return parseRewriteGlobalVariableDescriptor(YS, Key, Value, DL);
|
2014-11-08 08:00:50 +08:00
|
|
|
else if (RewriteType.equals("global alias"))
|
2014-11-08 05:32:08 +08:00
|
|
|
return parseRewriteGlobalAliasDescriptor(YS, Key, Value, DL);
|
|
|
|
|
|
|
|
YS.printError(Entry.getKey(), "unknown rewrite type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteMapParser::
|
|
|
|
parseRewriteFunctionDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
|
|
|
|
yaml::MappingNode *Descriptor,
|
|
|
|
RewriteDescriptorList *DL) {
|
|
|
|
bool Naked = false;
|
|
|
|
std::string Source;
|
|
|
|
std::string Target;
|
|
|
|
std::string Transform;
|
|
|
|
|
|
|
|
for (auto &Field : *Descriptor) {
|
|
|
|
yaml::ScalarNode *Key;
|
|
|
|
yaml::ScalarNode *Value;
|
|
|
|
SmallString<32> KeyStorage;
|
|
|
|
SmallString<32> ValueStorage;
|
|
|
|
StringRef KeyValue;
|
|
|
|
|
|
|
|
Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
|
|
|
|
if (!Key) {
|
|
|
|
YS.printError(Field.getKey(), "descriptor key must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
|
|
|
|
if (!Value) {
|
|
|
|
YS.printError(Field.getValue(), "descriptor value must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyValue = Key->getValue(KeyStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
if (KeyValue.equals("source")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
std::string Error;
|
|
|
|
|
|
|
|
Source = Value->getValue(ValueStorage);
|
|
|
|
if (!Regex(Source).isValid(Error)) {
|
|
|
|
YS.printError(Field.getKey(), "invalid regex: " + Error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("target")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
Target = Value->getValue(ValueStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("transform")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
Transform = Value->getValue(ValueStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("naked")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
std::string Undecorated;
|
|
|
|
|
|
|
|
Undecorated = Value->getValue(ValueStorage);
|
|
|
|
Naked = StringRef(Undecorated).lower() == "true" || Undecorated == "1";
|
|
|
|
} else {
|
|
|
|
YS.printError(Field.getKey(), "unknown key for function");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Transform.empty() == Target.empty()) {
|
|
|
|
YS.printError(Descriptor,
|
|
|
|
"exactly one of transform or target must be specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO see if there is a more elegant solution to selecting the rewrite
|
|
|
|
// descriptor type
|
|
|
|
if (!Target.empty())
|
2016-07-26 02:39:08 +08:00
|
|
|
DL->push_back(llvm::make_unique<ExplicitRewriteFunctionDescriptor>(
|
|
|
|
Source, Target, Naked));
|
2014-11-08 05:32:08 +08:00
|
|
|
else
|
2016-07-26 02:10:54 +08:00
|
|
|
DL->push_back(
|
2016-07-26 02:39:08 +08:00
|
|
|
llvm::make_unique<PatternRewriteFunctionDescriptor>(Source, Transform));
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteMapParser::
|
|
|
|
parseRewriteGlobalVariableDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
|
|
|
|
yaml::MappingNode *Descriptor,
|
|
|
|
RewriteDescriptorList *DL) {
|
|
|
|
std::string Source;
|
|
|
|
std::string Target;
|
|
|
|
std::string Transform;
|
|
|
|
|
|
|
|
for (auto &Field : *Descriptor) {
|
|
|
|
yaml::ScalarNode *Key;
|
|
|
|
yaml::ScalarNode *Value;
|
|
|
|
SmallString<32> KeyStorage;
|
|
|
|
SmallString<32> ValueStorage;
|
|
|
|
StringRef KeyValue;
|
|
|
|
|
|
|
|
Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
|
|
|
|
if (!Key) {
|
|
|
|
YS.printError(Field.getKey(), "descriptor Key must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
|
|
|
|
if (!Value) {
|
|
|
|
YS.printError(Field.getValue(), "descriptor value must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyValue = Key->getValue(KeyStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
if (KeyValue.equals("source")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
std::string Error;
|
|
|
|
|
|
|
|
Source = Value->getValue(ValueStorage);
|
|
|
|
if (!Regex(Source).isValid(Error)) {
|
|
|
|
YS.printError(Field.getKey(), "invalid regex: " + Error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("target")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
Target = Value->getValue(ValueStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("transform")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
Transform = Value->getValue(ValueStorage);
|
|
|
|
} else {
|
|
|
|
YS.printError(Field.getKey(), "unknown Key for Global Variable");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Transform.empty() == Target.empty()) {
|
|
|
|
YS.printError(Descriptor,
|
|
|
|
"exactly one of transform or target must be specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Target.empty())
|
2016-07-26 02:39:08 +08:00
|
|
|
DL->push_back(llvm::make_unique<ExplicitRewriteGlobalVariableDescriptor>(
|
|
|
|
Source, Target,
|
|
|
|
/*Naked*/ false));
|
2014-11-08 05:32:08 +08:00
|
|
|
else
|
2016-07-26 02:39:08 +08:00
|
|
|
DL->push_back(llvm::make_unique<PatternRewriteGlobalVariableDescriptor>(
|
|
|
|
Source, Transform));
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteMapParser::
|
|
|
|
parseRewriteGlobalAliasDescriptor(yaml::Stream &YS, yaml::ScalarNode *K,
|
|
|
|
yaml::MappingNode *Descriptor,
|
|
|
|
RewriteDescriptorList *DL) {
|
|
|
|
std::string Source;
|
|
|
|
std::string Target;
|
|
|
|
std::string Transform;
|
|
|
|
|
|
|
|
for (auto &Field : *Descriptor) {
|
|
|
|
yaml::ScalarNode *Key;
|
|
|
|
yaml::ScalarNode *Value;
|
|
|
|
SmallString<32> KeyStorage;
|
|
|
|
SmallString<32> ValueStorage;
|
|
|
|
StringRef KeyValue;
|
|
|
|
|
|
|
|
Key = dyn_cast<yaml::ScalarNode>(Field.getKey());
|
|
|
|
if (!Key) {
|
|
|
|
YS.printError(Field.getKey(), "descriptor key must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value = dyn_cast<yaml::ScalarNode>(Field.getValue());
|
|
|
|
if (!Value) {
|
|
|
|
YS.printError(Field.getValue(), "descriptor value must be a scalar");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyValue = Key->getValue(KeyStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
if (KeyValue.equals("source")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
std::string Error;
|
|
|
|
|
|
|
|
Source = Value->getValue(ValueStorage);
|
|
|
|
if (!Regex(Source).isValid(Error)) {
|
|
|
|
YS.printError(Field.getKey(), "invalid regex: " + Error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("target")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
Target = Value->getValue(ValueStorage);
|
2014-11-08 08:00:50 +08:00
|
|
|
} else if (KeyValue.equals("transform")) {
|
2014-11-08 05:32:08 +08:00
|
|
|
Transform = Value->getValue(ValueStorage);
|
|
|
|
} else {
|
|
|
|
YS.printError(Field.getKey(), "unknown key for Global Alias");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Transform.empty() == Target.empty()) {
|
|
|
|
YS.printError(Descriptor,
|
|
|
|
"exactly one of transform or target must be specified");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Target.empty())
|
2016-07-26 02:39:08 +08:00
|
|
|
DL->push_back(llvm::make_unique<ExplicitRewriteNamedAliasDescriptor>(
|
|
|
|
Source, Target,
|
|
|
|
/*Naked*/ false));
|
2014-11-08 05:32:08 +08:00
|
|
|
else
|
2016-07-26 02:39:08 +08:00
|
|
|
DL->push_back(llvm::make_unique<PatternRewriteNamedAliasDescriptor>(
|
|
|
|
Source, Transform));
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2016-07-26 04:52:00 +08:00
|
|
|
class RewriteSymbolsLegacyPass : public ModulePass {
|
2014-11-08 05:32:08 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
RewriteSymbolsLegacyPass();
|
|
|
|
RewriteSymbolsLegacyPass(SymbolRewriter::RewriteDescriptorList &DL);
|
2014-11-08 05:32:08 +08:00
|
|
|
|
2014-11-15 03:06:36 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
private:
|
2016-07-26 04:52:00 +08:00
|
|
|
RewriteSymbolPass Impl;
|
2014-11-08 05:32:08 +08:00
|
|
|
};
|
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
char RewriteSymbolsLegacyPass::ID = 0;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass() : ModulePass(ID), Impl() {
|
|
|
|
initializeRewriteSymbolsLegacyPassPass(*PassRegistry::getPassRegistry());
|
2014-11-08 05:32:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
RewriteSymbolsLegacyPass::RewriteSymbolsLegacyPass(
|
|
|
|
SymbolRewriter::RewriteDescriptorList &DL)
|
|
|
|
: ModulePass(ID), Impl(DL) {}
|
|
|
|
|
|
|
|
bool RewriteSymbolsLegacyPass::runOnModule(Module &M) {
|
|
|
|
return Impl.runImpl(M);
|
|
|
|
}
|
2014-11-08 05:32:08 +08:00
|
|
|
}
|
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
namespace llvm {
|
|
|
|
PreservedAnalyses RewriteSymbolPass::run(Module &M, ModuleAnalysisManager &AM) {
|
|
|
|
if (!runImpl(M))
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
return PreservedAnalyses::none();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RewriteSymbolPass::runImpl(Module &M) {
|
2014-11-08 05:32:08 +08:00
|
|
|
bool Changed;
|
|
|
|
|
|
|
|
Changed = false;
|
|
|
|
for (auto &Descriptor : Descriptors)
|
2016-07-26 02:10:54 +08:00
|
|
|
Changed |= Descriptor->performOnModule(M);
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
void RewriteSymbolPass::loadAndParseMapFiles() {
|
2014-11-08 05:32:08 +08:00
|
|
|
const std::vector<std::string> MapFiles(RewriteMapFiles);
|
2016-07-26 04:52:00 +08:00
|
|
|
SymbolRewriter::RewriteMapParser Parser;
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
for (const auto &MapFile : MapFiles)
|
2016-07-26 04:52:00 +08:00
|
|
|
Parser.parse(MapFile, &Descriptors);
|
2014-11-08 05:32:08 +08:00
|
|
|
}
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2014-11-08 05:32:08 +08:00
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
INITIALIZE_PASS(RewriteSymbolsLegacyPass, "rewrite-symbols", "Rewrite Symbols",
|
|
|
|
false, false)
|
2014-11-08 05:32:08 +08:00
|
|
|
|
2016-07-26 04:52:00 +08:00
|
|
|
ModulePass *llvm::createRewriteSymbolsPass() {
|
|
|
|
return new RewriteSymbolsLegacyPass();
|
|
|
|
}
|
2014-11-08 05:32:08 +08:00
|
|
|
|
|
|
|
ModulePass *
|
|
|
|
llvm::createRewriteSymbolsPass(SymbolRewriter::RewriteDescriptorList &DL) {
|
2016-07-26 04:52:00 +08:00
|
|
|
return new RewriteSymbolsLegacyPass(DL);
|
2014-11-08 05:32:08 +08:00
|
|
|
}
|