[lld] r233246 - Use arithmetic type to represent alignments (not in log2) everywhere.
Rui Ueyama
ruiu at google.com
Wed Mar 25 19:20:25 PDT 2015
Author: ruiu
Date: Wed Mar 25 21:20:25 2015
New Revision: 233246
URL: http://llvm.org/viewvc/llvm-project?rev=233246&view=rev
Log:
Use arithmetic type to represent alignments (not in log2) everywhere.
This is the final step of conversion. Now log2 numbers are removed
from everywhere!
Modified:
lld/trunk/include/lld/Core/DefinedAtom.h
lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h
lld/trunk/lib/Driver/DarwinLdDriver.cpp
lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
lld/trunk/test/mach-o/align_text.yaml
lld/trunk/test/mach-o/parse-initializers64.yaml
lld/trunk/test/mach-o/parse-literals.yaml
lld/trunk/unittests/MachOTests/MachONormalizedFileYAMLTests.cpp
Modified: lld/trunk/include/lld/Core/DefinedAtom.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/DefinedAtom.h?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/DefinedAtom.h (original)
+++ lld/trunk/include/lld/Core/DefinedAtom.h Wed Mar 25 21:20:25 2015
@@ -17,26 +17,6 @@ namespace lld {
class File;
class Reference;
-// This class represents exponents of power-of-two numbers.
-// Used to represent alignments.
-//
-// Currently we represent alignments both in log2 of a value or a
-// value itself. That is confusing. We aim to use only real values
-// only. Conversion is not easy, since both types are just arithmetic
-// types, and thus the compiler doesn't help us find places we mix
-// them. This class is to make all places where exponents are used
-// explicit.
-//
-// Once the conversion is done, this class will be removed.
-class PowerOf2 {
-public:
- PowerOf2(uint16_t v) : _v(v) {}
- bool operator==(const PowerOf2 &other) const { return _v == other._v; }
- operator uint16_t() const { return _v; }
-private:
- uint16_t _v;
-};
-
/// \brief The fundamental unit of linking.
///
/// A C function or global variable is an atom. An atom has content and
@@ -220,7 +200,7 @@ public:
struct Alignment {
Alignment(int v, int m = 0) : value(v), modulus(m) {}
- PowerOf2 value;
+ uint16_t value;
uint16_t modulus;
bool operator==(const Alignment &rhs) const {
Modified: lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h Wed Mar 25 21:20:25 2015
@@ -228,10 +228,10 @@ public:
const StringRefVector &rpaths() const { return _rpaths; }
/// Add section alignment constraint on final layout.
- void addSectionAlignment(StringRef seg, StringRef sect, PowerOf2 align2);
+ void addSectionAlignment(StringRef seg, StringRef sect, uint16_t align2);
/// Returns true if specified section had alignment constraints.
- bool sectionAligned(StringRef seg, StringRef sect, PowerOf2 &align2) const;
+ bool sectionAligned(StringRef seg, StringRef sect, uint16_t &align2) const;
StringRef dyldPath() const { return "/usr/lib/dyld"; }
@@ -312,7 +312,7 @@ private:
struct SectionAlign {
StringRef segmentName;
StringRef sectionName;
- PowerOf2 align2;
+ uint16_t align2;
};
struct OrderFileNode {
Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Wed Mar 25 21:20:25 2015
@@ -479,7 +479,7 @@ bool DarwinLdDriver::parse(int argc, con
<< alignStr << "' not a valid number\n";
return false;
}
- PowerOf2 align2 = 1 << llvm::countTrailingZeros(alignValue);
+ uint16_t align2 = 1 << llvm::countTrailingZeros(alignValue);
if (!llvm::isPowerOf2_64(alignValue)) {
diagnostics << "warning: alignment for '-sectalign "
<< segName << " " << sectName
Modified: lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp Wed Mar 25 21:20:25 2015
@@ -731,13 +731,13 @@ ArchHandler &MachOLinkingContext::archHa
void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
- PowerOf2 align2) {
+ uint16_t align2) {
SectionAlign entry = { seg, sect, align2 };
_sectAligns.push_back(entry);
}
bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
- PowerOf2 &align2) const {
+ uint16_t &align2) const {
for (const SectionAlign &entry : _sectAligns) {
if (seg.equals(entry.segmentName) && sect.equals(entry.sectionName)) {
align2 = entry.align2;
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h Wed Mar 25 21:20:25 2015
@@ -114,7 +114,7 @@ struct Section {
StringRef sectionName;
SectionType type;
SectionAttr attributes;
- PowerOf2 alignment;
+ uint16_t alignment;
Hex64 address;
ArrayRef<uint8_t> content;
Relocations relocations;
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Wed Mar 25 21:20:25 2015
@@ -58,7 +58,7 @@ struct SectionInfo {
uint32_t attributes;
uint64_t address;
uint64_t size;
- PowerOf2 alignment;
+ uint16_t alignment;
std::vector<AtomInfo> atomsAndOffsets;
uint32_t normalizedSectionIndex;
uint32_t finalSectionIndex;
@@ -69,7 +69,7 @@ SectionInfo::SectionInfo(StringRef sg, S
: segmentName(sg), sectionName(sct), type(t), attributes(attrs),
address(0), size(0), alignment(1),
normalizedSectionIndex(0), finalSectionIndex(0) {
- PowerOf2 align = 1;
+ uint16_t align = 1;
if (ctxt.sectionAligned(segmentName, sectionName, align)) {
alignment = align;
}
@@ -142,7 +142,6 @@ private:
void appendSection(SectionInfo *si, NormalizedFile &file);
uint32_t sectionIndexForAtom(const Atom *atom);
- static uint64_t alignTo(uint64_t value, PowerOf2 align2);
typedef llvm::DenseMap<const Atom*, uint32_t> AtomToIndex;
struct AtomAndIndex { const Atom *atom; uint32_t index; SymbolScope scope; };
struct AtomSorter {
@@ -453,15 +452,11 @@ void Util::organizeSections() {
}
-uint64_t Util::alignTo(uint64_t value, PowerOf2 align2) {
- return llvm::RoundUpToAlignment(value, align2);
-}
-
void Util::layoutSectionsInSegment(SegmentInfo *seg, uint64_t &addr) {
seg->address = addr;
for (SectionInfo *sect : seg->sections) {
- sect->address = alignTo(addr, sect->alignment);
+ sect->address = llvm::RoundUpToAlignment(addr, sect->alignment);
addr = sect->address + sect->size;
}
seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
@@ -485,7 +480,7 @@ void Util::layoutSectionsInTextSegment(s
// Start assigning section address starting at padded offset.
addr += (padding + hlcSize);
for (SectionInfo *sect : seg->sections) {
- sect->address = alignTo(addr, sect->alignment);
+ sect->address = llvm::RoundUpToAlignment(addr, sect->alignment);
addr = sect->address + sect->size;
}
seg->size = llvm::RoundUpToAlignment(addr - seg->address,_context.pageSize());
@@ -528,7 +523,7 @@ void Util::assignAddressesToSections(con
);
} else {
for (SectionInfo *sect : _sectionInfos) {
- sect->address = alignTo(address, sect->alignment);
+ sect->address = llvm::RoundUpToAlignment(address, sect->alignment);
address = sect->address + sect->size;
}
DEBUG_WITH_TYPE("WriterMachO-norm",
Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileYAML.cpp Wed Mar 25 21:20:25 2015
@@ -53,20 +53,6 @@ LLVM_YAML_IS_SEQUENCE_VECTOR(DataInCode)
namespace llvm {
namespace yaml {
-template <>
-struct ScalarTraits<lld::PowerOf2> {
- static void output(const lld::PowerOf2 &value, void*, raw_ostream &out) {
- out << llvm::format("%d", value);
- }
- static StringRef input(StringRef scalar, void*, lld::PowerOf2 &result) {
- uint32_t value;
- scalar.getAsInteger(10, value);
- result = 1 << value;
- return StringRef();
- }
- static bool mustQuote(StringRef) { return false; }
-};
-
// A vector of Sections is a sequence.
template<>
struct SequenceTraits< std::vector<Section> > {
@@ -290,7 +276,7 @@ struct MappingTraits<Section> {
io.mapRequired("section", sect.sectionName);
io.mapRequired("type", sect.type);
io.mapOptional("attributes", sect.attributes);
- io.mapOptional("alignment", sect.alignment, lld::PowerOf2(1));
+ io.mapOptional("alignment", sect.alignment, (uint16_t)1);
io.mapRequired("address", sect.address);
if (sect.type == llvm::MachO::S_ZEROFILL) {
// S_ZEROFILL sections use "size:" instead of "content:"
Modified: lld/trunk/test/mach-o/align_text.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/align_text.yaml?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/test/mach-o/align_text.yaml (original)
+++ lld/trunk/test/mach-o/align_text.yaml Wed Mar 25 21:20:25 2015
@@ -13,7 +13,7 @@ sections:
section: __text
type: S_REGULAR
attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS ]
- alignment: 4
+ alignment: 16
address: 0x0000000000000000
content: [ 0x90, 0x90, 0x90, 0xC3, 0xC3, 0xC3 ]
local-symbols:
Modified: lld/trunk/test/mach-o/parse-initializers64.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/parse-initializers64.yaml?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/test/mach-o/parse-initializers64.yaml (original)
+++ lld/trunk/test/mach-o/parse-initializers64.yaml Wed Mar 25 21:20:25 2015
@@ -22,7 +22,7 @@ sections:
section: __mod_init_func
type: S_MOD_INIT_FUNC_POINTERS
attributes: [ ]
- alignment: 0
+ alignment: 1
address: 0x0000000000000100
content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]
@@ -43,7 +43,7 @@ sections:
section: __mod_term_func
type: S_MOD_TERM_FUNC_POINTERS
attributes: [ ]
- alignment: 3
+ alignment: 8
address: 0x0000000000000108
content: [ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ]
relocations:
Modified: lld/trunk/test/mach-o/parse-literals.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/mach-o/parse-literals.yaml?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/test/mach-o/parse-literals.yaml (original)
+++ lld/trunk/test/mach-o/parse-literals.yaml Wed Mar 25 21:20:25 2015
@@ -14,7 +14,7 @@ sections:
section: __cstring
type: S_CSTRING_LITERALS
attributes: [ ]
- alignment: 0
+ alignment: 1
address: 0x0000000000000100
content: [ 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x00, 0x74, 0x68,
0x65, 0x72, 0x65, 0x00, 0x77, 0x6F, 0x72, 0x6C,
@@ -23,7 +23,7 @@ sections:
section: __literal4
type: S_4BYTE_LITERALS
attributes: [ ]
- alignment: 0
+ alignment: 1
address: 0x0000000000000114
content: [ 0x01, 0x02, 0x03, 0x04, 0x11, 0x12, 0x13, 0x14,
0x28, 0x29, 0x2A, 0x2B ]
@@ -31,7 +31,7 @@ sections:
section: __literal8
type: S_8BYTE_LITERALS
attributes: [ ]
- alignment: 0
+ alignment: 1
address: 0x0000000000000120
content: [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F ]
@@ -39,7 +39,7 @@ sections:
section: __literal16
type: S_16BYTE_LITERALS
attributes: [ ]
- alignment: 0
+ alignment: 1
address: 0x0000000000000130
content: [ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00 ]
@@ -47,7 +47,7 @@ sections:
section: __ustring
type: S_REGULAR
attributes: [ ]
- alignment: 0
+ alignment: 1
address: 0x0000000000000100
content: [ 0x68, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00,
0x6F, 0x00, 0x00, 0x00, 0x74, 0x00, 0x68, 0x00,
Modified: lld/trunk/unittests/MachOTests/MachONormalizedFileYAMLTests.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/MachOTests/MachONormalizedFileYAMLTests.cpp?rev=233246&r1=233245&r2=233246&view=diff
==============================================================================
--- lld/trunk/unittests/MachOTests/MachONormalizedFileYAMLTests.cpp (original)
+++ lld/trunk/unittests/MachOTests/MachONormalizedFileYAMLTests.cpp Wed Mar 25 21:20:25 2015
@@ -197,7 +197,7 @@ TEST(ObjectFileYAML, oneSection) {
" section: __text\n"
" type: S_REGULAR\n"
" attributes: [ S_ATTR_PURE_INSTRUCTIONS ]\n"
- " alignment: 1\n"
+ " alignment: 2\n"
" address: 0x12345678\n"
" content: [ 0x90, 0x90 ]\n"
"...\n");
@@ -232,7 +232,7 @@ TEST(ObjectFileYAML, hello_x86_64) {
" section: __text\n"
" type: S_REGULAR\n"
" attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS]\n"
- " alignment: 0\n"
+ " alignment: 1\n"
" address: 0x0000\n"
" content: [ 0x55, 0x48, 0x89, 0xe5, 0x48, 0x8d, 0x3d, 0x00,\n"
" 0x00, 0x00, 0x00, 0x30, 0xc0, 0xe8, 0x00, 0x00,\n"
@@ -254,7 +254,7 @@ TEST(ObjectFileYAML, hello_x86_64) {
" section: __cstring\n"
" type: S_CSTRING_LITERALS\n"
" attributes: [ ]\n"
- " alignment: 0\n"
+ " alignment: 1\n"
" address: 0x0016\n"
" content: [ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x00 ]\n"
"global-symbols:\n"
@@ -361,7 +361,7 @@ TEST(ObjectFileYAML, hello_x86) {
" section: __text\n"
" type: S_REGULAR\n"
" attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS]\n"
- " alignment: 0\n"
+ " alignment: 1\n"
" address: 0x0000\n"
" content: [ 0x55, 0x89, 0xe5, 0x83, 0xec, 0x08, 0xe8, 0x00,\n"
" 0x00, 0x00, 0x00, 0x58, 0x8d, 0x80, 0x16, 0x00,\n"
@@ -391,7 +391,7 @@ TEST(ObjectFileYAML, hello_x86) {
" section: __cstring\n"
" type: S_CSTRING_LITERALS\n"
" attributes: [ ]\n"
- " alignment: 0\n"
+ " alignment: 1\n"
" address: 0x0021\n"
" content: [ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x00 ]\n"
"global-symbols:\n"
@@ -490,7 +490,7 @@ TEST(ObjectFileYAML, hello_armv6) {
" section: __text\n"
" type: S_REGULAR\n"
" attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS]\n"
- " alignment: 2\n"
+ " alignment: 4\n"
" address: 0x0000\n"
" content: [ 0x80, 0x40, 0x2d, 0xe9, 0x10, 0x00, 0x9f, 0xe5,\n"
" 0x0d, 0x70, 0xa0, 0xe1, 0x00, 0x00, 0x8f, 0xe0,\n"
@@ -519,7 +519,7 @@ TEST(ObjectFileYAML, hello_armv6) {
" section: __cstring\n"
" type: S_CSTRING_LITERALS\n"
" attributes: [ ]\n"
- " alignment: 0\n"
+ " alignment: 1\n"
" address: 0x0020\n"
" content: [ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x00 ]\n"
"global-symbols:\n"
@@ -620,7 +620,7 @@ TEST(ObjectFileYAML, hello_armv7) {
" section: __text\n"
" type: S_REGULAR\n"
" attributes: [ S_ATTR_PURE_INSTRUCTIONS, S_ATTR_SOME_INSTRUCTIONS]\n"
- " alignment: 1\n"
+ " alignment: 2\n"
" address: 0x0000\n"
" content: [ 0x80, 0xb5, 0x40, 0xf2, 0x06, 0x00, 0x6f, 0x46,\n"
" 0xc0, 0xf2, 0x00, 0x00, 0x78, 0x44, 0xff, 0xf7,\n"
@@ -660,7 +660,7 @@ TEST(ObjectFileYAML, hello_armv7) {
" section: __cstring\n"
" type: S_CSTRING_LITERALS\n"
" attributes: [ ]\n"
- " alignment: 0\n"
+ " alignment: 1\n"
" address: 0x0016\n"
" content: [ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x0a, 0x00 ]\n"
"global-symbols:\n"
More information about the llvm-commits
mailing list