[lld] r233232 - Add a scaffolding to merge alignment representations.

Rui Ueyama ruiu at google.com
Wed Mar 25 17:10:50 PDT 2015


Author: ruiu
Date: Wed Mar 25 19:10:50 2015
New Revision: 233232

URL: http://llvm.org/viewvc/llvm-project?rev=233232&view=rev
Log:
Add a scaffolding to merge alignment representations.

We are using log2 values and values themselves to represent alignments.
For example, alignment 8 is sometimes represented as 3 (8 == 2^3).
We want to stop using log2 values.

Because both types are regular arithmetic types, we cannot get help from
a compiler to find places we mix two representations. That makes this
merging work surprisingly hard because if I make a mistake, I'll just get
wrong results at runtime (Yay types!). In this patch, I introduced
a class to represents power-of-two values, which is basically an alias
for an integer type.

Once the migration is done, the class will be removed.

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/MachONormalizedFileFromAtoms.cpp

Modified: lld/trunk/include/lld/Core/DefinedAtom.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/DefinedAtom.h?rev=233232&r1=233231&r2=233232&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/DefinedAtom.h (original)
+++ lld/trunk/include/lld/Core/DefinedAtom.h Wed Mar 25 19:10:50 2015
@@ -17,6 +17,25 @@ 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) {}
+  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
@@ -198,11 +217,9 @@ public:
   };
 
   struct Alignment {
-    Alignment(int p2, int m = 0)
-      : powerOf2(p2)
-      , modulus(m) {}
+    Alignment(int p2, int m = 0) : powerOf2(p2), modulus(m) {}
 
-    uint16_t powerOf2;
+    PowerOf2 powerOf2;
     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=233232&r1=233231&r2=233232&view=diff
==============================================================================
--- lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h (original)
+++ lld/trunk/include/lld/ReaderWriter/MachOLinkingContext.h Wed Mar 25 19:10:50 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, uint8_t align2);
+  void addSectionAlignment(StringRef seg, StringRef sect, PowerOf2 align2);
 
   /// Returns true if specified section had alignment constraints.
-  bool sectionAligned(StringRef seg, StringRef sect, uint8_t &align2) const;
+  bool sectionAligned(StringRef seg, StringRef sect, PowerOf2 &align2) const;
 
   StringRef dyldPath() const { return "/usr/lib/dyld"; }
 

Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=233232&r1=233231&r2=233232&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Wed Mar 25 19:10:50 2015
@@ -479,7 +479,7 @@ bool DarwinLdDriver::parse(int argc, con
                   << alignStr << "' not a valid number\n";
       return false;
     }
-    uint8_t align2 = llvm::countTrailingZeros(alignValue);
+    PowerOf2 align2 = 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=233232&r1=233231&r2=233232&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachOLinkingContext.cpp Wed Mar 25 19:10:50 2015
@@ -731,7 +731,7 @@ ArchHandler &MachOLinkingContext::archHa
 
 
 void MachOLinkingContext::addSectionAlignment(StringRef seg, StringRef sect,
-                                                               uint8_t align2) {
+                                              PowerOf2 align2) {
   SectionAlign entry;
   entry.segmentName = seg;
   entry.sectionName = sect;
@@ -740,7 +740,7 @@ void MachOLinkingContext::addSectionAlig
 }
 
 bool MachOLinkingContext::sectionAligned(StringRef seg, StringRef sect,
-                                                        uint8_t &align2) const {
+                                         PowerOf2 &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/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=233232&r1=233231&r2=233232&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Wed Mar 25 19:10:50 2015
@@ -58,7 +58,7 @@ struct SectionInfo {
   uint32_t                  attributes;
   uint64_t                  address;
   uint64_t                  size;
-  uint32_t                  alignment;
+  PowerOf2                  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(0),
                  normalizedSectionIndex(0), finalSectionIndex(0) {
-  uint8_t align;
+  PowerOf2 align(0);
   if (ctxt.sectionAligned(segmentName, sectionName, align)) {
     alignment = align;
   }
@@ -142,7 +142,7 @@ private:
   void         appendSection(SectionInfo *si, NormalizedFile &file);
   uint32_t     sectionIndexForAtom(const Atom *atom);
 
-  static uint64_t alignTo(uint64_t value, uint8_t align2);
+  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,7 +453,7 @@ void Util::organizeSections() {
 
 }
 
-uint64_t Util::alignTo(uint64_t value, uint8_t align2) {
+uint64_t Util::alignTo(uint64_t value, PowerOf2 align2) {
   return llvm::RoundUpToAlignment(value, 1 << align2);
 }
 
@@ -1235,4 +1235,3 @@ normalizedFromAtoms(const lld::File &ato
 } // namespace normalized
 } // namespace mach_o
 } // namespace lld
-





More information about the llvm-commits mailing list