Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#359449 - 22/08/2013 09:34 8/16 bit compilers
sn00p
addict

Registered: 24/07/2002
Posts: 618
Loc: South London
This is something that has bugged me ever since I first had the misfortune of using a 'C' compiler for a PIC micro many years ago. Basically, they're all crap.

I tried many different makes of 'C' compiler and I could make them generate broken code rather two easily, it didn't seem to make a difference if the compiler vendor was an independent company or the chip manufacturer, I found myself having to hand hold the compiler to make sure it generated working code. I spent a lot of time double checking the resulting assembler to make sure that it was doing what it should be.

Fast forward to this week, we've replaced an off the shelf IC with an MSP running code to do the same job, in fact it's better because it can do a couple of other things and doesn't need extra transistors to handle some inversions. Great.

So I had code up and running on it, but then when I switched to release code it stopped working, so off I went into the assembler to find out why it was hanging....and there it was:

Code:
volatile int *a;

void f()
{
int *b;
....
while(a!=b)
while(a==b)
....
}


This might look odd, but it's some code to make sure that a timer isn't operating on a value so that code outside can change it. (it's not my code and at first glance it doesn't seem very elegant, but it's a solution to a slightly tricky problem).

Basically, b is a pointer to a linked list entry and a is pointer to the current entry being used by the timer. Function f changes the linked list, so you have to be careful you don't change it while in use by the timer.

Now, because a is declared as volatile the compiler should not optimise those statements, but it does, so the assembler becomes:

Code:
ne_test:
cmp &a,&b
jne ne_test

eq_test:
jmp eq_test


wtf? It's assumed because the first loop is true (i.e a does not equal b), that the second loop must be false (a equals b), but a is declared volatile, so it really shouldn't do that.

So, my question is this, how on earth do people write stuff for 8/16 bit micros with any amount of confidence that what they've written is actually what they'll get. I've not seen this on any ARM compiler that I've used (but I'm sure odd cases do pop up).

It's been at least 5 years since I first pulled my hair out trying to use one of these compilers and it seems that not a lot has changed since then.

Adrian




Edited by sn00p (22/08/2013 09:35)

Top
#359451 - 22/08/2013 10:59 Re: 8/16 bit compilers [Re: sn00p]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14484
Loc: Canada
I use compiler barriers to guarantee code correctness for the levels at which I work.

But in this case, I wonder if the pointer "a" is simply not being declared the way you want it to work? There's a difference between "the pointer is volatile" and the "data the pointer points at is volatile."

In this case, the generated compiler code appears to be assuming you care only about the "data the pointer points at" being volatile, not the pointer itself.

[EDIT}: try this instead? int *volatile a;
Or both parts volatile: int volatile * volatile a;

[EDIT2]: Reference link:
http://www.embedded.com/electronics-blog...olatile-Keyword



Edited by mlord (22/08/2013 11:07)

Top
#359452 - 22/08/2013 11:22 Re: 8/16 bit compilers [Re: mlord]
sn00p
addict

Registered: 24/07/2002
Posts: 618
Loc: South London
Bingo!

Well I never.... Proves that you really do learn something new every day!

It didn't even occur to me that you could put volatile after the asterisk!

Remind me to buy you a pint! smile

I still distrust these compilers though!

Adrian

Top