<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Mar 31, 2014 at 10:18 AM, David Majnemer <span dir="ltr"><<a href="mailto:david.majnemer@gmail.com" target="_blank">david.majnemer@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: majnemer<br>
Date: Mon Mar 31 12:18:53 2014<br>
New Revision: 205225<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=205225&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=205225&view=rev</a><br>
Log:<br>
MS ABI: mangleStringLiteral shouldn't rely on the host's endianness<br>
<br>
No test case is needed, the one in-tree is sufficient.  The build-bot<br>
never emailed me because something else had upset it.<br>
<br>
Modified:<br>
    cfe/trunk/lib/AST/MicrosoftMangle.cpp<br>
<br>
Modified: cfe/trunk/lib/AST/MicrosoftMangle.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=205225&r1=205224&r2=205225&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=205225&r1=205224&r2=205225&view=diff</a><br>

==============================================================================<br>
--- cfe/trunk/lib/AST/MicrosoftMangle.cpp (original)<br>
+++ cfe/trunk/lib/AST/MicrosoftMangle.cpp Mon Mar 31 12:18:53 2014<br>
@@ -2385,9 +2385,39 @@ void MicrosoftMangleContextImpl::mangleS<br>
     }<br>
   };<br>
<br>
+  auto GetLittleEndianByte = [&Mangler, &SL](unsigned Index) {<br></blockquote><div><br></div><div>Lambda nub question: can you capture SL by value since it never changes?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

+    unsigned CharByteWidth = SL->getCharByteWidth();<br>
+    uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);<br>
+    if (CharByteWidth == 1) {<br>
+      return static_cast<char>(CodeUnit);<br>
+    } else if (CharByteWidth == 2) {<br>
+      if (Index % 2)<br>
+        return static_cast<char>((CodeUnit >> 8) & 0xff);<br>
+      else<br>
+        return static_cast<char>(CodeUnit & 0xff);<br>
+    } else {<br>
+      llvm_unreachable("unsupported CharByteWidth");<br>
+    }<br>
+  };<br>
+<br>
+  auto GetBigEndianByte = [&Mangler, &SL](unsigned Index) {<br>
+    unsigned CharByteWidth = SL->getCharByteWidth();<br>
+    uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);<br>
+    if (CharByteWidth == 1) {<br>
+      return static_cast<char>(CodeUnit);<br>
+    } else if (CharByteWidth == 2) {<br>
+      if (Index % 2)<br>
+        return static_cast<char>(CodeUnit & 0xff);<br>
+      else<br>
+        return static_cast<char>((CodeUnit >> 8) & 0xff);<br>
+    } else {<br>
+      llvm_unreachable("unsupported CharByteWidth");<br>
+    }<br>
+  };<br>
+<br></blockquote><div><br></div><div>This feels overly complex, but I haven't come up with any brilliant ideas for simplifying it.  The best I can come up with is getting the bytes, copying them, and endian swapping them appropriately.</div>
<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
   // CRC all the bytes of the StringLiteral.<br>
-  for (char Byte : SL->getBytes())<br>
-    UpdateCRC(Byte);<br>
+  for (unsigned I = 0, E = SL->getByteLength(); I != E; ++I)<br>
+    UpdateCRC(GetLittleEndianByte(I));<br>
<br>
   // The NUL terminator byte(s) were not present earlier,<br>
   // we need to manually process those bytes into the CRC.<br>
@@ -2462,25 +2492,17 @@ void MicrosoftMangleContextImpl::mangleS<br>
     }<br>
   };<br>
<br>
-  auto MangleChar = [&Mangler, &MangleByte, &SL](uint32_t CodeUnit) {<br>
-    if (SL->getCharByteWidth() == 1) {<br>
-      MangleByte(static_cast<char>(CodeUnit));<br>
-    } else if (SL->getCharByteWidth() == 2) {<br>
-      MangleByte(static_cast<char>((CodeUnit >> 16) & 0xff));<br>
-      MangleByte(static_cast<char>(CodeUnit & 0xff));<br>
-    } else {<br>
-      llvm_unreachable("unsupported CharByteWidth");<br>
-    }<br>
-  };<br>
-<br>
   // Enforce our 32 character max.<br>
   unsigned NumCharsToMangle = std::min(32U, SL->getLength());<br>
-  for (unsigned i = 0; i < NumCharsToMangle; ++i)<br>
-    MangleChar(SL->getCodeUnit(i));<br>
+  for (unsigned I = 0, E = NumCharsToMangle * SL->getCharByteWidth(); I != E;<br>
+       ++I)<br>
+    MangleByte(GetBigEndianByte(I));<br>
<br>
   // Encode the NUL terminator if there is room.<br>
   if (NumCharsToMangle < 32)<br>
-    MangleChar(0);<br>
+    for (unsigned NullTerminator = 0; NullTerminator < SL->getCharByteWidth();<br>
+         ++NullTerminator)<br>
+      MangleByte(0);<br>
<br>
   Mangler.getStream() << '@';<br>
 }<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div></div>