Skip to search.
msp430 · TI MSP430 microcontroller users group

Group Information

  • Members: 5304
  • Category: Hardware
  • Founded: Oct 13, 2000
  • Language: English
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Messages

  Messages Help
Advanced
Bug in IAR for MSP430   Message List  
Reply Message #46648 of 50764 |
I've come across a bug in IAR (lastest version, latest patches).
I have reduced it down to a simple one-line function.

It appears at every optimization level except "None".

I have emailed John Regehr (see his papers on C compiler correctness) and Nigel
Jones (http://embeddedgurus.com/stack-overflow/) who concurred that this is a
compiler bug and not some C language ambiguity. I have contacted IAR
support/development but they don't acknowledge it is a bug.

I hope that the IAR gentleman (Anders) who posts in this forum can help to get
this resolved. This is a very common construct and I don't know if it affects
other IAR target platforms.

I found this since I tend to compile my applications with multiple compilers to
ensure that they always behave identically.

Buggy function
--------------------------------------------
void iar_buggy_func(unsigned char ch)
{
/* Some simple steps to initialze dma omitted.
* But occurs even if they are ommitted */
/* ..... */
DMA1SZ = (uint16_t) &ch;
/* .... */
/* Other steps omitted to wait for dma completion */
}
---------------------------------------------
It occurs with any volatile hardware register as in
----------------------------------------
unsigned char volatile * volatile v3;
void iar_buggy_func3(unsigned char ch)
{
v3 = &ch;
}
---------------------------------------------------
What is the Bug?
Note that the address of the parameter is taken.
The compiler should put this on the stack or some place in memory and then
assign the volatile hardware register this address.

The assembly generated by the above function is
------------------------------
iar_buggy_func3:
004080 8321 decd.w SP
v3 = &ch;
004082 4182 1102 mov.w SP,&v3
004086 5321 incd.w SP
004088 4130 ret
------------------------------
Note, that stack space is allocated at the beginning of the
function and released at the end of the function. But the value
of the parameter ch is not stored on the stack.
The volatile register is set to the SP (stack pointer), but
the paramter "ch" is NOT stored at this memory location.

The bug does not occur if
1) Optimization level "None"
2) Another function call f() or even intrinsic function like nop()
is present within the function.
The correct assembly should be
-------------------
iar_buggy_func:
00403E 124C push.b R12
__no_operation();
004040 4303 nop
DMA1SA = (uint16_t)src;
004042 4182 01EA mov.w SP,&DMA1SA
}
004046 5321 incd.w SP
004048 4130 ret
-----------------------------------------------
Note that in this case, the "push" instruction stores the parameter value
(passed in R12) onto the stack.

=====================================================
I've contacted IAR support, but they have blown me off by saying things such as:
1) It may be a glitch that may be fixed in the next release.
2) High optimization may not produce the code intended
3) Providing me with workarounds for this specific function.
and here is the response from IAR development.

>>>>> IAR development response ----------------------------
To condense the problem, we have a function that looks like the following:

volatile unsigned char * v;
void iar_buggy_func(unsigned char ch)
{
v = & ch;
}

In the report you correctly noted that the value of "ch" was never written to
the stack.

When it comes to "volatile", the basic rule is that anything that has some kind
of side-effect or could be accessed by the underlying hardware should be
declared to be "volatile".

In this case, when writing to "v", both "v" and "ch" is accessed by the
hardware. Hence, both should be declared "volatile".

As "ch" is a parameter, it is possible to assign it to a local volatile variable
before the assignment, for example:

volatile unsigned char * v;
void iar_buggy_func(unsigned char ch)
{
volatile unsigned char ch2 = ch;
v = & ch2;
}
>>>> Done IAR development response -----------------------
This is not correct. It doesn't matter if ch is volatile or not.
The correct assignment must happen.

--------------------------

----------------------------------------------------------
Thanks very much.
Ratish Punnoose



-------------------------------------------





Thu Sep 23, 2010 9:15 pm

ratishpunnoose
Offline Offline
Send Email Send Email

Message #46648 of 50764 |
Expand Messages Author Sort by Date

I've come across a bug in IAR (lastest version, latest patches). I have reduced it down to a simple one-line function. It appears at every optimization level...
ratishpunnoose Offline Send Email Sep 23, 2010
11:15 pm

I would say that the bug is that the function iar_buggy_func3 is taking the address of an argument and then assigning that address to a global variable, which...
Preston Gurd
rpgurd Offline Send Email
Sep 24, 2010
5:01 am

Yes, that is correct. That was just for illustration. For actual use, you would have to have additional code at the end to ensure that the function is done...
ratishpunnoose Offline Send Email Sep 24, 2010
5:28 am

Hello, ... By "that variable" you mean 'ch'? But there is no other use of 'ch' nor can there be any modification to it that the compiler knows about because ...
Paul Curtis
paul_l_curtis Offline Send Email
Sep 24, 2010
8:11 am

See Paul (CPaul?) therein lies the problem, you're diluting your Vitamin C with sugar. Al...
OneStone
onestone_apc Offline Send Email
Sep 24, 2010
8:56 am

... S'okay, my elderberry wine is now ready to go into the demijohns. I can shift my daily sugar intake from breakfast to after dinner. :-) -- Paul Curtis,...
Paul Curtis
paul_l_curtis Offline Send Email
Sep 24, 2010
9:06 am

Strange place to keep your wine, I used to wear mine way back when in the Army to keep warm. Al...
OneStone
onestone_apc Offline Send Email
Sep 24, 2010
4:17 pm

On Fri, 24 Sep 2010 17:17:30 +0100, OneStone <onestone@...> ... Wow, you used to wear your wine to keep it warm? Did the army know you were doing...
Paul Curtis
paul_l_curtis Offline Send Email
Sep 24, 2010
4:37 pm

Absolutely, chateau plonk '74, we used to sit and contemplate it for a while until it was well mulled over, then pour it down our demijohns to keep warm. Al...
OneStone
onestone_apc Offline Send Email
Sep 24, 2010
4:40 pm

I know I am off the topic again. I speak and write in Chinese. I was told that there are lots of automated translators that translate English into Chinese and...
old_cow_yellow Offline Send Email Sep 24, 2010
4:31 pm

I mistyped the function. It should have been DMA1SA, which for the DMA controller is the address in memory of the source to transfer. Here is the context. ...
ratishpunnoose Offline Send Email Sep 24, 2010
4:07 pm

Hi, ... @address; ... I think you should write this correctly. I think this should work for you: void really_send_this(unsigned char ch) { volatile unsigned...
Paul Curtis
paul_l_curtis Offline Send Email
Sep 24, 2010
4:26 pm

... Hi! Over the past week we've had this discussion privately, via our support department, but I'm more that willing to discuss is here as well. I would base...
Anders Lindgren
rdogs_maint Offline Send Email
Sep 27, 2010
8:59 am

I'm not a C expert by a long shot, but IMO Anders has nailed it extremely well with his code examples. It also highlights my personal belief in general wrt the...
microbit@...
forum_microbit Offline Send Email
Sep 27, 2010
3:49 pm

Anders, Thank you very much for the detailed reply. I had some mixed messages from initial support which made me think that they did not quite understand what...
ratishpunnoose Offline Send Email Sep 27, 2010
4:24 pm

On Mon, 27 Sep 2010 17:24:31 +0100, ratishpunnoose <ratish@...> ... Whilst we're slapping each other on the back, I might as well add that the original...
Paul Curtis
paul_l_curtis Offline Send Email
Sep 27, 2010
4:31 pm

Hi Ratish! ... You're welcome! ... I typically don't get into the loop until after a few mails have been exchanged, so this might have been the case. However,...
Anders Lindgren
rdogs_maint Offline Send Email
Sep 28, 2010
6:51 am

Anders, Thank you for your illuminating discussion. I can see that "not doing this" would add to the grief of the compiler writer.... but can I take it a...
david collier
dexdynedgc Offline Send Email
Oct 1, 2010
2:02 pm

... C is a language with a standard (actually, several standards...). The standards are not perfect, but they are not bad, and they form a contract between...
davidbrown563 Offline Send Email Oct 1, 2010
6:02 pm

... See that's the thing.... with the optimisation turned off, the code DID what he wanted. It was the optimisation process that created code that DIDN'T. You...
david collier
dexdynedgc Offline Send Email
Oct 5, 2010
3:41 pm

David, ... Sorry, this is plain wrong. The original was a pointer to volatile, not a volatile pointer. Volatile pointers are not common--and the because the ...
Paul Curtis
paul_l_curtis Offline Send Email
Oct 5, 2010
3:48 pm

... Yes I agree Paul, it is plain wrong, for the code he originally posted. What he was trying to do was to declare the address register in the DMA controller...
david collier
dexdynedgc Offline Send Email
Oct 6, 2010
7:29 pm

... The code is perfectly good, legal C code, and the compiler generates appropriate assembly to execute that C code in an efficient way. How is the compiler...
davidbrown563 Offline Send Email Oct 7, 2010
11:27 am

The one thing I never really got about this ongoing thread, was the use of the pointer (well, lack thereof :-). The pointer (DMA address) was actually never...
Microbit_Ubuntu
forum_microbit Offline Send Email
Oct 9, 2010
4:08 am

... If code stops working when optimisation is turned on or off, then either the compiler is broken, or the original code was broken. In this case, the...
davidbrown563 Offline Send Email Oct 6, 2010
8:00 am

Hi David, Just to drag it all up again (:-), I was glancing at part of this ... Indeed interesting question - and surely a big dilemma for compiler designers ?...
Microbit_Ubuntu
forum_microbit Offline Send Email
Oct 12, 2010
9:38 pm

... Hi Kris, I've got to say warnings, all of them on, are just fine. But then, I'm a box jockey, my day job. Our product runs some quarter million lines of ...
Dan Bloomquist
lakeweb Offline Send Email
Oct 13, 2010
6:48 am

Hi David! ... You're welcome! ... When it comes to the hole on the stack, it might be possible to optimize it away as well. The IAR compiler typically doesn't...
Anders Lindgren
rdogs_maint Offline Send Email
Oct 5, 2010
8:45 am
Advanced

Copyright © 2010 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines NEW - Help