Re: Code Generation Issue
Re: Code Generation Issue
- Subject: Re: Code Generation Issue
- From: Andreas Grosam <email@hidden>
- Date: Wed, 23 Nov 2011 11:33:34 +0100
Thank you for answering!
On Nov 21, 2011, at 5:43 PM, Sean McBride wrote:
> On Tue, 15 Nov 2011 17:39:49 +0100, Andreas Grosam said:
>
>> I'm experiencing a strange runtime bug in my code that occurs only if
>> compiled with Apple LLVM compiler 3.0 and optimization flag O2 or higher
>> on Mac OS X. Code compiled with LLVM GCC 4.2 does not have this issue.
>> As far as I can see, the logic is correct.
>>
>> I usually hesitate to blame the compiler, but I can't see were I
>> possibly made an error in the source here.
>
> Since no one else has answered.... Before filing a bug, you could check the llvm/clang bugbase for something similar:
>
> <http://llvm.org/bugs/>
I checked these, searching for "switch", "code generation" but didn't found anything.
>
> Also, have you tried the following: guardmalloc, valgrind, clang static analyzer? They are all great at finding subtle bugs. Similarly, you might want to enable the flags suggested here:
>
> <http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html>
I've used the static analyser, as well as pedantic warnings - they reported no issues (as expected).
To my knowledge there is also no undefined behavior in the code.
(btw., interesting article from Chris Lattner)
Below are two simplified versions of the code which fail, and one version that works correctly:
// This version fails
template <class InputIterator, class OutputIterator>
OutputIterator encodeBase64_v1(InputIterator first, InputIterator last, OutputIterator result)
{
while (first != last)
{
++first;
if (first != last) {
++first;
if (first != last) {
// encode it into 4 bytes:
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
++first;
continue;
}
else {
// padding = 1
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
*result++ = '=';
return result; // uncomment/comment out
//break; // uncomment/comment out
}
}
else {
// padding = 2
*result++ = 'a';
*result++ = 'a';
*result++ = '=';
*result++ = '=';
return result; // uncomment/comment out
//break; // uncomment/comment out
}
} // while
return result;
}
// This version fails
template <class InputIterator, class OutputIterator>
OutputIterator encodeBase64_v2(InputIterator first, InputIterator last, OutputIterator result)
{
int padding = 0;
while (first != last)
{
++first;
if (first != last) {
++first;
if (first != last) {
// encode it into 4 bytes:
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
++first;
//continue; // uncomment/comment out
}
else {
padding = 1;
//break; // uncomment/comment out
}
}
else {
padding = 2;
//break; // uncomment/comment out
}
} // while
//assert(first == last);
if (padding == 1) {
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
*result++ = '=';
}
else if (padding == 2)
{
*result++ = 'a';
*result++ = 'a';
*result++ = '=';
*result++ = '=';
}
return result;
}
// This version seems to work
template <class InputIterator, class OutputIterator>
OutputIterator encodeBase64(InputIterator first, InputIterator last, OutputIterator result)
{
while (first != last)
{
++first;
if (first != last) {
++first;
if (first != last) {
// encode it into 4 bytes:
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
++first;
}
else {
// padding = 1
*result++ = 'a';
*result++ = 'a';
*result++ = 'a';
*result++ = '=';
}
}
else {
// padding = 2
*result++ = 'a';
*result++ = 'a';
*result++ = '=';
*result++ = '=';
}
} // while
assert(first == last);
return result;
}
Regards
Andreas _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden