Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Unified Diff: src/IceTranslator.cpp

Issue 624663002: Introduce model of global initializers in Subzero. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Fix nits. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/IceTranslator.h ('k') | src/IceTypeConverter.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/IceTranslator.cpp
diff --git a/src/IceTranslator.cpp b/src/IceTranslator.cpp
index d11e76d2582b37ef04d9d3e752c5d725e2b084b2..e86d60a304d6155751ce836693851590270a46d4 100644
--- a/src/IceTranslator.cpp
+++ b/src/IceTranslator.cpp
@@ -29,44 +29,61 @@ using namespace Ice;
Translator::~Translator() {}
-namespace {
-void setValueName(llvm::Value *V, const char *Kind, const IceString &Prefix,
- uint32_t &NameIndex, Ostream &errs) {
- if (V->hasName()) {
- const std::string &Name(V->getName());
- if (Name.find(Prefix) == 0) {
- errs << "Warning: Default " << Kind << " prefix '" << Prefix
- << "' conflicts with name '" << Name << "'.\n";
- }
- return;
- }
- if (NameIndex == 0) {
- V->setName(Prefix);
- ++NameIndex;
- return;
- }
+IceString Translator::createUnnamedName(const IceString &Prefix, SizeT Index) {
+ if (Index == 0)
+ return Prefix;
std::string Buffer;
llvm::raw_string_ostream StrBuf(Buffer);
- StrBuf << Prefix << NameIndex;
- V->setName(StrBuf.str());
- ++NameIndex;
+ StrBuf << Prefix << Index;
+ return StrBuf.str();
+}
+
+bool Translator::checkIfUnnamedNameSafe(const IceString &Name, const char *Kind,
+ const IceString &Prefix,
+ Ostream &Stream) {
+ if (Name.find(Prefix) == 0) {
+ for (size_t i = Prefix.size(); i < Name.size(); ++i) {
+ if (!isdigit(Name[i])) {
+ return false;
+ }
+ }
+ Stream << "Warning : Default " << Kind << " prefix '" << Prefix
+ << "' potentially conflicts with name '" << Name << "'.\n";
+ return true;
+ }
+ return false;
}
-} // end of anonymous namespace
void Translator::nameUnnamedGlobalAddresses(llvm::Module *Mod) {
const IceString &GlobalPrefix = Flags.DefaultGlobalPrefix;
+ if (GlobalPrefix.empty())
+ return;
+ uint32_t NameIndex = 0;
Ostream &errs = Ctx->getStrDump();
- if (!GlobalPrefix.empty()) {
- uint32_t NameIndex = 0;
- for (auto I = Mod->global_begin(), E = Mod->global_end(); I != E; ++I)
- setValueName(I, "global", GlobalPrefix, NameIndex, errs);
+ for (auto V = Mod->global_begin(), E = Mod->global_end(); V != E; ++V) {
+ if (!V->hasName()) {
+ V->setName(createUnnamedName(GlobalPrefix, NameIndex));
+ ++NameIndex;
+ } else {
+ checkIfUnnamedNameSafe(V->getName(), "global", GlobalPrefix, errs);
+ }
}
+}
+
+void Translator::nameUnnamedFunctions(llvm::Module *Mod) {
const IceString &FunctionPrefix = Flags.DefaultFunctionPrefix;
if (FunctionPrefix.empty())
return;
uint32_t NameIndex = 0;
- for (llvm::Function &I : *Mod)
- setValueName(&I, "function", FunctionPrefix, NameIndex, errs);
+ Ostream &errs = Ctx->getStrDump();
+ for (llvm::Function &F : *Mod) {
+ if (!F.hasName()) {
+ F.setName(createUnnamedName(FunctionPrefix, NameIndex));
+ ++NameIndex;
+ } else {
+ checkIfUnnamedNameSafe(F.getName(), "function", FunctionPrefix, errs);
+ }
+ }
}
void Translator::translateFcn(Cfg *Fcn) {
@@ -93,46 +110,12 @@ void Translator::emitConstants() {
Func->getTarget()->emitConstants();
}
-void Translator::convertGlobals(llvm::Module *Mod) {
- std::unique_ptr<TargetGlobalInitLowering> GlobalLowering(
- TargetGlobalInitLowering::createLowering(Ctx->getTargetArch(), Ctx));
- for (auto I = Mod->global_begin(), E = Mod->global_end(); I != E; ++I) {
- if (!I->hasInitializer())
- continue;
- const llvm::Constant *Initializer = I->getInitializer();
- IceString Name = I->getName();
- unsigned Align = I->getAlignment();
- uint64_t NumElements = 0;
- const char *Data = NULL;
- bool IsInternal = I->hasInternalLinkage();
- bool IsConst = I->isConstant();
- bool IsZeroInitializer = false;
-
- if (const llvm::ConstantDataArray *CDA =
- llvm::dyn_cast<llvm::ConstantDataArray>(Initializer)) {
- NumElements = CDA->getNumElements();
- assert(llvm::isa<llvm::IntegerType>(CDA->getElementType()) &&
- (llvm::cast<llvm::IntegerType>(CDA->getElementType())
- ->getBitWidth() == 8));
- Data = CDA->getRawDataValues().data();
- } else if (llvm::isa<llvm::ConstantAggregateZero>(Initializer)) {
- if (const llvm::ArrayType *AT =
- llvm::dyn_cast<llvm::ArrayType>(Initializer->getType())) {
- assert(llvm::isa<llvm::IntegerType>(AT->getElementType()) &&
- (llvm::cast<llvm::IntegerType>(AT->getElementType())
- ->getBitWidth() == 8));
- NumElements = AT->getNumElements();
- IsZeroInitializer = true;
- } else {
- llvm_unreachable("Unhandled constant aggregate zero type");
- }
- } else {
- llvm_unreachable("Unhandled global initializer");
- }
-
- GlobalLowering->lower(Name, Align, IsInternal, IsConst, IsZeroInitializer,
- NumElements, Data,
- Ctx->getFlags().DisableTranslation);
+void Translator::lowerGlobals(const GlobalAddressList &GlobalAddresses) {
+ llvm::OwningPtr<Ice::TargetGlobalInitLowering> GlobalLowering(
+ Ice::TargetGlobalInitLowering::createLowering(Ctx->getTargetArch(), Ctx));
+ bool DisableTranslation = Ctx->getFlags().DisableTranslation;
+ for (const Ice::GlobalAddress *Addr : GlobalAddresses) {
+ GlobalLowering->lower(*Addr, DisableTranslation);
}
GlobalLowering.reset();
}
« no previous file with comments | « src/IceTranslator.h ('k') | src/IceTypeConverter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698