r205225 - MS ABI: mangleStringLiteral shouldn't rely on the host's endianness

David Majnemer david.majnemer at gmail.com
Mon Mar 31 14:54:30 PDT 2014


I managed to simplify it a little in r205250.
On Mon Mar 31 2014 at 10:46:53 AM, Reid Kleckner <rnk at google.com> wrote:

> On Mon, Mar 31, 2014 at 10:18 AM, David Majnemer <david.majnemer at gmail.com
> > wrote:
>
>> Author: majnemer
>> Date: Mon Mar 31 12:18:53 2014
>> New Revision: 205225
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=205225&view=rev
>> Log:
>> MS ABI: mangleStringLiteral shouldn't rely on the host's endianness
>>
>> No test case is needed, the one in-tree is sufficient.  The build-bot
>> never emailed me because something else had upset it.
>>
>> Modified:
>>     cfe/trunk/lib/AST/MicrosoftMangle.cpp
>>
>> Modified: cfe/trunk/lib/AST/MicrosoftMangle.cpp
>> URL:
>> http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/MicrosoftMangle.cpp?rev=205225&r1=205224&r2=205225&view=diff
>>
>> ==============================================================================
>> --- cfe/trunk/lib/AST/MicrosoftMangle.cpp (original)
>> +++ cfe/trunk/lib/AST/MicrosoftMangle.cpp Mon Mar 31 12:18:53 2014
>> @@ -2385,9 +2385,39 @@ void MicrosoftMangleContextImpl::mangleS
>>      }
>>    };
>>
>> +  auto GetLittleEndianByte = [&Mangler, &SL](unsigned Index) {
>>
>
> Lambda nub question: can you capture SL by value since it never changes?
>
>
>> +    unsigned CharByteWidth = SL->getCharByteWidth();
>> +    uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);
>> +    if (CharByteWidth == 1) {
>> +      return static_cast<char>(CodeUnit);
>> +    } else if (CharByteWidth == 2) {
>> +      if (Index % 2)
>> +        return static_cast<char>((CodeUnit >> 8) & 0xff);
>> +      else
>> +        return static_cast<char>(CodeUnit & 0xff);
>> +    } else {
>> +      llvm_unreachable("unsupported CharByteWidth");
>> +    }
>> +  };
>> +
>> +  auto GetBigEndianByte = [&Mangler, &SL](unsigned Index) {
>> +    unsigned CharByteWidth = SL->getCharByteWidth();
>> +    uint32_t CodeUnit = SL->getCodeUnit(Index / CharByteWidth);
>> +    if (CharByteWidth == 1) {
>> +      return static_cast<char>(CodeUnit);
>> +    } else if (CharByteWidth == 2) {
>> +      if (Index % 2)
>> +        return static_cast<char>(CodeUnit & 0xff);
>> +      else
>> +        return static_cast<char>((CodeUnit >> 8) & 0xff);
>> +    } else {
>> +      llvm_unreachable("unsupported CharByteWidth");
>> +    }
>> +  };
>> +
>>
>
> 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.
>
>
>>    // CRC all the bytes of the StringLiteral.
>> -  for (char Byte : SL->getBytes())
>> -    UpdateCRC(Byte);
>> +  for (unsigned I = 0, E = SL->getByteLength(); I != E; ++I)
>> +    UpdateCRC(GetLittleEndianByte(I));
>>
>>    // The NUL terminator byte(s) were not present earlier,
>>    // we need to manually process those bytes into the CRC.
>> @@ -2462,25 +2492,17 @@ void MicrosoftMangleContextImpl::mangleS
>>      }
>>    };
>>
>> -  auto MangleChar = [&Mangler, &MangleByte, &SL](uint32_t CodeUnit) {
>> -    if (SL->getCharByteWidth() == 1) {
>> -      MangleByte(static_cast<char>(CodeUnit));
>> -    } else if (SL->getCharByteWidth() == 2) {
>> -      MangleByte(static_cast<char>((CodeUnit >> 16) & 0xff));
>> -      MangleByte(static_cast<char>(CodeUnit & 0xff));
>> -    } else {
>> -      llvm_unreachable("unsupported CharByteWidth");
>> -    }
>> -  };
>> -
>>    // Enforce our 32 character max.
>>    unsigned NumCharsToMangle = std::min(32U, SL->getLength());
>> -  for (unsigned i = 0; i < NumCharsToMangle; ++i)
>> -    MangleChar(SL->getCodeUnit(i));
>> +  for (unsigned I = 0, E = NumCharsToMangle * SL->getCharByteWidth(); I
>> != E;
>> +       ++I)
>> +    MangleByte(GetBigEndianByte(I));
>>
>>    // Encode the NUL terminator if there is room.
>>    if (NumCharsToMangle < 32)
>> -    MangleChar(0);
>> +    for (unsigned NullTerminator = 0; NullTerminator <
>> SL->getCharByteWidth();
>> +         ++NullTerminator)
>> +      MangleByte(0);
>>
>>    Mangler.getStream() << '@';
>>  }
>>
>>
>> _______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140331/6af012fd/attachment.html>


More information about the cfe-commits mailing list