Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 //===- subzero/src/IceTargetLowering.cpp - Basic lowering implementation --===// | 1 //===- subzero/src/IceTargetLowering.cpp - Basic lowering implementation --===// |
| 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 skeleton of the TargetLowering class, | 10 // This file implements the skeleton of the TargetLowering class, |
| 11 // specifically invoking the appropriate lowering method for a given | 11 // specifically invoking the appropriate lowering method for a given |
| 12 // instruction kind and driving global register allocation. It also | 12 // instruction kind and driving global register allocation. It also |
| 13 // implements the non-deleted instruction iteration in | 13 // implements the non-deleted instruction iteration in |
| 14 // LoweringContext. | 14 // LoweringContext. |
| 15 // | 15 // |
| 16 //===----------------------------------------------------------------------===// | 16 //===----------------------------------------------------------------------===// |
| 17 | 17 |
| 18 #include <sstream> | |
|
Jim Stichnoth
2015/06/16 17:03:25
http://llvm.org/docs/CodingStandards.html#use-raw-
John
2015/06/16 17:30:55
no longer relevant here, but Done for IceELFObject
| |
| 19 | |
| 18 #include "IceAssemblerARM32.h" | 20 #include "IceAssemblerARM32.h" |
| 19 #include "IceAssemblerX8632.h" | 21 #include "IceAssemblerX8632.h" |
| 20 #include "assembler_mips32.h" | 22 #include "assembler_mips32.h" |
| 21 #include "IceCfg.h" // setError() | 23 #include "IceCfg.h" // setError() |
| 22 #include "IceCfgNode.h" | 24 #include "IceCfgNode.h" |
| 23 #include "IceGlobalInits.h" | 25 #include "IceGlobalInits.h" |
| 24 #include "IceOperand.h" | 26 #include "IceOperand.h" |
| 25 #include "IceRegAlloc.h" | 27 #include "IceRegAlloc.h" |
| 26 #include "IceTargetLowering.h" | 28 #include "IceTargetLowering.h" |
| 27 #include "IceTargetLoweringARM32.h" | 29 #include "IceTargetLoweringARM32.h" |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 439 #define SUBZERO_TARGET(X) \ | 441 #define SUBZERO_TARGET(X) \ |
| 440 if (Target == Target_##X) \ | 442 if (Target == Target_##X) \ |
| 441 return TargetData##X::create(Ctx); | 443 return TargetData##X::create(Ctx); |
| 442 #include "llvm/Config/SZTargets.def" | 444 #include "llvm/Config/SZTargets.def" |
| 443 | 445 |
| 444 llvm::report_fatal_error("Unsupported target data lowering"); | 446 llvm::report_fatal_error("Unsupported target data lowering"); |
| 445 } | 447 } |
| 446 | 448 |
| 447 TargetDataLowering::~TargetDataLowering() {} | 449 TargetDataLowering::~TargetDataLowering() {} |
| 448 | 450 |
| 449 void TargetDataLowering::emitGlobal(const VariableDeclaration &Var) { | 451 namespace { |
| 452 | |
| 453 IceString dataSectionSuffix(const IceString &SectionSuffix, | |
|
Jim Stichnoth
2015/06/16 17:03:24
I realize that -fdata-sections hasn't been impleme
John
2015/06/16 17:30:55
Well, with -fdata-sections we don't need the Secti
| |
| 454 const IceString &MangledVarName, | |
| 455 const bool DataSections) { | |
| 456 if (SectionSuffix.empty() && !DataSections) { | |
| 457 // Early optimizing the simple case. If no suffix is needed then we should | |
| 458 // avoid constructing the ostringstream below. | |
| 459 return ""; | |
| 460 } | |
| 461 | |
| 462 std::ostringstream MangledName; | |
| 463 | |
| 464 if (!SectionSuffix.empty()) { | |
| 465 MangledName << "." << SectionSuffix; | |
| 466 } | |
| 467 | |
| 468 if (DataSections) { | |
| 469 MangledName << "." << MangledVarName; | |
| 470 } | |
| 471 | |
| 472 return MangledName.str(); | |
| 473 } | |
| 474 | |
| 475 } // end of anonymous namespace | |
| 476 | |
| 477 void TargetDataLowering::emitGlobal(const VariableDeclaration &Var, | |
| 478 const IceString &SectionSuffix) { | |
| 450 if (!ALLOW_DUMP) | 479 if (!ALLOW_DUMP) |
| 451 return; | 480 return; |
| 452 | 481 |
| 453 // If external and not initialized, this must be a cross test. | 482 // If external and not initialized, this must be a cross test. |
| 454 // Don't generate a declaration for such cases. | 483 // Don't generate a declaration for such cases. |
| 455 bool IsExternal = Var.isExternal() || Ctx->getFlags().getDisableInternal(); | 484 const bool IsExternal = |
| 485 Var.isExternal() || Ctx->getFlags().getDisableInternal(); | |
| 456 if (IsExternal && !Var.hasInitializer()) | 486 if (IsExternal && !Var.hasInitializer()) |
| 457 return; | 487 return; |
| 458 | 488 |
| 459 Ostream &Str = Ctx->getStrEmit(); | 489 Ostream &Str = Ctx->getStrEmit(); |
| 460 const VariableDeclaration::InitializerListType &Initializers = | 490 const bool HasNonzeroInitializer = Var.hasNonzeroInitializer(); |
| 461 Var.getInitializers(); | 491 const bool IsConstant = Var.getIsConstant(); |
| 462 bool HasNonzeroInitializer = Var.hasNonzeroInitializer(); | 492 const SizeT Size = Var.getNumBytes(); |
| 463 bool IsConstant = Var.getIsConstant(); | 493 const IceString MangledName = Var.mangleName(Ctx); |
| 464 uint32_t Align = Var.getAlignment(); | |
| 465 SizeT Size = Var.getNumBytes(); | |
| 466 IceString MangledName = Var.mangleName(Ctx); | |
| 467 IceString SectionSuffix = ""; | |
| 468 if (Ctx->getFlags().getDataSections()) | |
| 469 SectionSuffix = "." + MangledName; | |
| 470 | 494 |
| 471 Str << "\t.type\t" << MangledName << ",%object\n"; | 495 Str << "\t.type\t" << MangledName << ",%object\n"; |
| 472 | 496 |
| 497 const bool UseDataSections = Ctx->getFlags().getDataSections(); | |
| 498 const IceString Suffix = | |
| 499 dataSectionSuffix(SectionSuffix, MangledName, UseDataSections); | |
| 473 if (IsConstant) | 500 if (IsConstant) |
| 474 Str << "\t.section\t.rodata" << SectionSuffix << ",\"a\",%progbits\n"; | 501 Str << "\t.section\t.rodata" << Suffix << ",\"a\",%progbits\n"; |
| 475 else if (HasNonzeroInitializer) | 502 else if (HasNonzeroInitializer) |
| 476 Str << "\t.section\t.data" << SectionSuffix << ",\"aw\",%progbits\n"; | 503 Str << "\t.section\t.data" << Suffix << ",\"aw\",%progbits\n"; |
| 477 else | 504 else |
| 478 Str << "\t.section\t.bss" << SectionSuffix << ",\"aw\",%nobits\n"; | 505 Str << "\t.section\t.bss" << Suffix << ",\"aw\",%nobits\n"; |
| 479 | 506 |
| 480 if (IsExternal) | 507 if (IsExternal) |
| 481 Str << "\t.globl\t" << MangledName << "\n"; | 508 Str << "\t.globl\t" << MangledName << "\n"; |
| 482 | 509 |
| 510 const uint32_t Align = Var.getAlignment(); | |
| 483 if (Align > 1) { | 511 if (Align > 1) { |
| 484 assert(llvm::isPowerOf2_32(Align)); | 512 assert(llvm::isPowerOf2_32(Align)); |
| 485 // Use the .p2align directive, since the .align N directive can either | 513 // Use the .p2align directive, since the .align N directive can either |
| 486 // interpret N as bytes, or power of 2 bytes, depending on the target. | 514 // interpret N as bytes, or power of 2 bytes, depending on the target. |
| 487 Str << "\t.p2align\t" << llvm::Log2_32(Align) << "\n"; | 515 Str << "\t.p2align\t" << llvm::Log2_32(Align) << "\n"; |
| 488 } | 516 } |
| 489 | 517 |
| 490 Str << MangledName << ":\n"; | 518 Str << MangledName << ":\n"; |
| 491 | 519 |
| 492 if (HasNonzeroInitializer) { | 520 if (HasNonzeroInitializer) { |
| 493 for (VariableDeclaration::Initializer *Init : Initializers) { | 521 for (VariableDeclaration::Initializer *Init : Var.getInitializers()) { |
| 494 switch (Init->getKind()) { | 522 switch (Init->getKind()) { |
| 495 case VariableDeclaration::Initializer::DataInitializerKind: { | 523 case VariableDeclaration::Initializer::DataInitializerKind: { |
| 496 const auto Data = llvm::cast<VariableDeclaration::DataInitializer>(Init) | 524 const auto &Data = llvm::cast<VariableDeclaration::DataInitializer>( |
| 497 ->getContents(); | 525 Init)->getContents(); |
| 498 for (SizeT i = 0; i < Init->getNumBytes(); ++i) { | 526 for (SizeT i = 0; i < Init->getNumBytes(); ++i) { |
| 499 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; | 527 Str << "\t.byte\t" << (((unsigned)Data[i]) & 0xff) << "\n"; |
| 500 } | 528 } |
| 501 break; | 529 break; |
| 502 } | 530 } |
| 503 case VariableDeclaration::Initializer::ZeroInitializerKind: | 531 case VariableDeclaration::Initializer::ZeroInitializerKind: |
| 504 Str << "\t.zero\t" << Init->getNumBytes() << "\n"; | 532 Str << "\t.zero\t" << Init->getNumBytes() << "\n"; |
| 505 break; | 533 break; |
| 506 case VariableDeclaration::Initializer::RelocInitializerKind: { | 534 case VariableDeclaration::Initializer::RelocInitializerKind: { |
| 507 const auto Reloc = | 535 const auto *Reloc = |
| 508 llvm::cast<VariableDeclaration::RelocInitializer>(Init); | 536 llvm::cast<VariableDeclaration::RelocInitializer>(Init); |
| 509 Str << "\t" << getEmit32Directive() << "\t"; | 537 Str << "\t" << getEmit32Directive() << "\t"; |
| 510 Str << Reloc->getDeclaration()->mangleName(Ctx); | 538 Str << Reloc->getDeclaration()->mangleName(Ctx); |
| 511 if (RelocOffsetT Offset = Reloc->getOffset()) { | 539 if (RelocOffsetT Offset = Reloc->getOffset()) { |
| 512 if (Offset >= 0 || (Offset == INT32_MIN)) | 540 if (Offset >= 0 || (Offset == INT32_MIN)) |
| 513 Str << " + " << Offset; | 541 Str << " + " << Offset; |
| 514 else | 542 else |
| 515 Str << " - " << -Offset; | 543 Str << " - " << -Offset; |
| 516 } | 544 } |
| 517 Str << "\n"; | 545 Str << "\n"; |
| 518 break; | 546 break; |
| 519 } | 547 } |
| 520 } | 548 } |
| 521 } | 549 } |
| 522 } else | 550 } else { |
| 523 // NOTE: for non-constant zero initializers, this is BSS (no bits), | 551 // NOTE: for non-constant zero initializers, this is BSS (no bits), |
| 524 // so an ELF writer would not write to the file, and only track | 552 // so an ELF writer would not write to the file, and only track |
| 525 // virtual offsets, but the .s writer still needs this .zero and | 553 // virtual offsets, but the .s writer still needs this .zero and |
| 526 // cannot simply use the .size to advance offsets. | 554 // cannot simply use the .size to advance offsets. |
| 527 Str << "\t.zero\t" << Size << "\n"; | 555 Str << "\t.zero\t" << Size << "\n"; |
| 556 } | |
| 528 | 557 |
| 529 Str << "\t.size\t" << MangledName << ", " << Size << "\n"; | 558 Str << "\t.size\t" << MangledName << ", " << Size << "\n"; |
| 530 } | 559 } |
| 531 | 560 |
| 532 std::unique_ptr<TargetHeaderLowering> | 561 std::unique_ptr<TargetHeaderLowering> |
| 533 TargetHeaderLowering::createLowering(GlobalContext *Ctx) { | 562 TargetHeaderLowering::createLowering(GlobalContext *Ctx) { |
| 534 TargetArch Target = Ctx->getFlags().getTargetArch(); | 563 TargetArch Target = Ctx->getFlags().getTargetArch(); |
| 535 #define SUBZERO_TARGET(X) \ | 564 #define SUBZERO_TARGET(X) \ |
| 536 if (Target == Target_##X) \ | 565 if (Target == Target_##X) \ |
| 537 return TargetHeader##X::create(Ctx); | 566 return TargetHeader##X::create(Ctx); |
| 538 #include "llvm/Config/SZTargets.def" | 567 #include "llvm/Config/SZTargets.def" |
| 539 | 568 |
| 540 llvm::report_fatal_error("Unsupported target header lowering"); | 569 llvm::report_fatal_error("Unsupported target header lowering"); |
| 541 } | 570 } |
| 542 | 571 |
| 543 TargetHeaderLowering::~TargetHeaderLowering() {} | 572 TargetHeaderLowering::~TargetHeaderLowering() {} |
| 544 | 573 |
| 545 } // end of namespace Ice | 574 } // end of namespace Ice |
| OLD | NEW |