how to let the gcc compiler optimizer know that my GCC inline asm code is using stack?

how to let the gcc compiler optimizer know that my GCC inline asm code is using stack?It keeps flushing the items I pushed onto stack.

Reply to
伏虎
Loading thread data ...

I don't know if you can. If I needed to get that deep into things, I'd either allocate C variables and pass them to the assembly code, or I'd just write the whole function in assembly.

--
My liberal friends think I'm a conservative kook. 
My conservative friends think I'm a liberal kook. 
Why am I not happy that they have found common ground? 

Tim Wescott, Communications, Control, Circuits & Software 
http://www.wescottdesign.com
Reply to
Tim Wescott

I doubt that it does. Did you start the assembly as 'C' code then optimize it? That's a good way to assure that all the stack indexing is correct... but you need to gen the assembler using the same optimization level...

So when you turn the optimizing off the problem goes away? Might be worth having a separate compilation unit at different optimization level... which might be a whole other can of worms....

--
Les Cargill
Reply to
Les Cargill

is using stack?It keeps flushing the items I pushed onto stack.

Do you mean your inline assembly code is changing the stack or frame pointer, so the size of the stack is different when you exit the inline assembler sequence compared to when you entered it? If so, I don't think you can do that.

Or, do you mean that your inline assembly code is changing the values of variables the C code has already placed on the stack, and that the optimiser is (in effect) ignoring the changes made by the inline assembly sequence because it is storing values in registers? If this is the case, then making the C variables volatile should help because it will (in theory) cause the C code to re-read the variables after your inline assembler sequences has finished.

You can also use the input/output parameter lists in the inline assembler sequence to let the compiler know you are changing the value of stack variables. For example, to write r1 into the C variable called c_variable:

void my_function( void ) { volatile unsigned long c_variable;

__asm volatile \ ( \ " mov %0, r1 \n" \ : "=r" ( c_variable ) :: " \ ); }

Regards, Richard.

  • formatting link
    Designed for microcontrollers. More than 7000 downloads per month.

  • formatting link

15 interconnected trace views. An indispensable productivity tool.
Reply to
FreeRTOS info

The problem is on line 12... o_O

In other words, you need to show your code, and the final assembly code generated by gcc, and explain where things are not done as you expect.

IMHO, gcc inline asm has a very complex, error-prone syntax.

Reply to
Noob

I'm pretty sure you don't, because inline asm isn't allowed to move the stack pointer. Allowing that would wreak havoc on the compiler's own use of the stack pointer.

So don't do that, then. Seriously. Use an output constraint to inform GCC about a place where to keep that output, instead.

Reply to
Hans-Bernhard Bröker

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.