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

Side by Side Diff: src/IceTargetLoweringX8632.cpp

Issue 631383003: Subzero: Fix emission of global initializers. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Remove the -disable-globals option 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 unified diff | Download patch
OLDNEW
1 //===- subzero/src/IceTargetLoweringX8632.cpp - x86-32 lowering -----------===// 1 //===- subzero/src/IceTargetLoweringX8632.cpp - x86-32 lowering -----------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file implements the TargetLoweringX8632 class, which 10 // This file implements the TargetLoweringX8632 class, which
(...skipping 4419 matching lines...) Expand 10 before | Expand all | Expand 10 after
4430 void TargetGlobalInitX8632::lower(const GlobalAddress &Global, 4430 void TargetGlobalInitX8632::lower(const GlobalAddress &Global,
4431 bool DisableTranslation) { 4431 bool DisableTranslation) {
4432 if (Ctx->isVerbose()) { 4432 if (Ctx->isVerbose()) {
4433 Global.dump(Ctx->getStrDump()); 4433 Global.dump(Ctx->getStrDump());
4434 } 4434 }
4435 4435
4436 if (DisableTranslation) 4436 if (DisableTranslation)
4437 return; 4437 return;
4438 4438
4439 Ostream &Str = Ctx->getStrEmit(); 4439 Ostream &Str = Ctx->getStrEmit();
4440 // constant:
4441 // .section .rodata,"a",@progbits
4442 // .align ALIGN
4443 // .byte ...
4444 // .size NAME, SIZE
4445
4446 // non-constant:
4447 // .data
4448 // .align ALIGN
4449 // .byte ...
4450 // .size NAME, SIZE
4451
4452 // zeroinitializer (constant):
4453 // (.section or .data as above)
4454 // .align ALIGN
4455 // .zero SIZE
4456 // .size NAME, SIZE
4457
4458 // zeroinitializer (non-constant):
4459 // (.section or .data as above)
4460 // .local NAME
4461 // .comm NAME, SIZE, ALIGN
4462 4440
4463 // TODO(kschimpf): Don't mangle name if external and uninitialized. This 4441 // TODO(kschimpf): Don't mangle name if external and uninitialized. This
4464 // will allow us to cross test relocations for references to external 4442 // will allow us to cross test relocations for references to external
4465 // global variables. 4443 // global variables.
4466 4444
4467 IceString MangledName = Ctx->mangleName(Global.getName());
4468 // Start a new section.
4469 if (Ctx->getFlags().DataSections) {
4470 Str << "\t.section\t.rodata." << MangledName << ",\"a\",@progbits\n";
4471 } else if (Global.getIsConstant()) {
4472 Str << "\t.section\t.rodata,\"a\",@progbits\n";
4473 } else {
4474 Str << "\t.type\t" << MangledName << ",@object\n";
4475 Str << "\t.data\n";
4476 }
4477
4478 Str << "\t" << (Global.getIsInternal() ? ".local" : ".global") << "\t"
4479 << MangledName << "\n";
4480
4481 const GlobalAddress::InitializerListType &Initializers = 4445 const GlobalAddress::InitializerListType &Initializers =
4482 Global.getInitializers(); 4446 Global.getInitializers();
4447 assert(Initializers.size());
4448 bool HasInitializer =
4449 !(Initializers.size() == 1 &&
4450 llvm::isa<GlobalAddress::ZeroInitializer>(Initializers[0]));
4451 bool IsConstant = Global.getIsConstant();
4452 bool IsExternal = !Global.getIsInternal();
4453 uint32_t Align = Global.getAlignment();
4454 SizeT Size = Global.getNumBytes();
4455 IceString MangledName = Ctx->mangleName(Global.getName());
4456 IceString SectionSuffix = "";
4457 if (Ctx->getFlags().DataSections)
4458 SectionSuffix = "." + MangledName;
4483 4459
4484 // Note: Handle zero initializations specially when lowering, since 4460 Str << "\t.type\t" << MangledName << ",@object\n";
4485 // we may be able to reduce object size.
4486 GlobalAddress::ZeroInitializer *SimpleZeroInit = nullptr;
4487 if (Initializers.size() == 1) {
4488 GlobalAddress::Initializer *Init = Initializers[0];
4489 if (const auto ZeroInit =
4490 llvm::dyn_cast<GlobalAddress::ZeroInitializer>(Init)) {
4491 SimpleZeroInit = ZeroInit;
4492 }
4493 }
4494 4461
4495 if (SimpleZeroInit && !Global.getIsConstant()) { 4462 if (IsConstant)
4496 // TODO(stichnot): Put the appropriate non-constant 4463 Str << "\t.section\t.rodata" << SectionSuffix << ",\"a\",@progbits\n";
4497 // zeroinitializers in a .bss section to reduce object size. 4464 else if (HasInitializer)
4498 Str << "\t.comm\t" << MangledName << ", " << Global.getNumBytes() << ", " 4465 Str << "\t.section\t.data" << SectionSuffix << ",\"aw\",@progbits\n";
4499 << Global.getAlignment() << "\n"; 4466 else if (IsExternal)
4500 // } 4467 Str << "\t.section\t.bss" << SectionSuffix << ",\"aw\",@nobits\n";
4501 } else { 4468 // No .section for non-constant + zeroinitializer + internal
4502 Str << "\t.align\t" << Global.getAlignment() << "\n"; 4469
4470 if (IsExternal)
4471 Str << "\t.globl\t" << MangledName << "\n";
4472 else if (!IsConstant && !HasInitializer)
4473 Str << "\t.local\t" << MangledName << "\n";
4474 // Internal symbols only get .local when using .comm.
4475
4476 if ((IsConstant || HasInitializer || IsExternal) && Align > 1)
4477 Str << "\t.align\t" << Align << "\n";
4478 // Alignment is part of .comm.
4479
4480 if (IsConstant || HasInitializer || IsExternal)
4503 Str << MangledName << ":\n"; 4481 Str << MangledName << ":\n";
4482 else
4483 Str << "\t.comm\t" << MangledName << "," << Size << "," << Align << "\n";
4484
4485 if (HasInitializer) {
4504 for (GlobalAddress::Initializer *Init : Initializers) { 4486 for (GlobalAddress::Initializer *Init : Initializers) {
4505 switch (Init->getKind()) { 4487 switch (Init->getKind()) {
4506 case GlobalAddress::Initializer::DataInitializerKind: { 4488 case GlobalAddress::Initializer::DataInitializerKind: {
4507 const auto Data = 4489 const auto Data =
4508 llvm::cast<GlobalAddress::DataInitializer>(Init)->getContents(); 4490 llvm::cast<GlobalAddress::DataInitializer>(Init)->getContents();
4509 for (SizeT i = 0; i < Init->getNumBytes(); ++i) { 4491 for (SizeT i = 0; i < Init->getNumBytes(); ++i) {
4510 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; 4492 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n";
4511 } 4493 }
4512 break; 4494 break;
4513 } 4495 }
(...skipping 18 matching lines...) Expand all
4532 default: { 4514 default: {
4533 std::string Buffer; 4515 std::string Buffer;
4534 llvm::raw_string_ostream StrBuf(Buffer); 4516 llvm::raw_string_ostream StrBuf(Buffer);
4535 StrBuf << "Unable to lower initializer: "; 4517 StrBuf << "Unable to lower initializer: ";
4536 Init->dump(StrBuf); 4518 Init->dump(StrBuf);
4537 llvm::report_fatal_error(StrBuf.str()); 4519 llvm::report_fatal_error(StrBuf.str());
4538 break; 4520 break;
4539 } 4521 }
4540 } 4522 }
4541 } 4523 }
4542 Str << "\t.size\t" << MangledName << ", " << Global.getNumBytes() << "\n"; 4524 } else if (IsConstant || IsExternal)
4543 } 4525 Str << "\t.zero\t" << Size << "\n";
4526 // Size is part of .comm.
4527
4528 if (IsConstant || HasInitializer || IsExternal)
4529 Str << "\t.size\t" << MangledName << ", " << Size << "\n";
4530 // Size is part of .comm.
4544 } 4531 }
4545 4532
4546 } // end of namespace Ice 4533 } // end of namespace Ice
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698