1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

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

Discussion in 'Embedded' started by ä¼è™Ž, Oct 8, 2012.

  1. ä¼è™Ž

    ä¼è™Ž Guest

    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.
     
    ä¼è™Ž, Oct 8, 2012
    #1
    1. Advertisements

  2. ä¼è™Ž

    Tim Wescott Guest

    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
     
    Tim Wescott, Oct 8, 2012
    #2
    1. Advertisements

  3. ä¼è™Ž

    Les Cargill Guest

    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, Oct 8, 2012
    #3
  4. 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.

    + http://www.FreeRTOS.org
    Designed for microcontrollers. More than 7000 downloads per month.

    + http://www.FreeRTOS.org/trace
    15 interconnected trace views. An indispensable productivity tool.
     
    FreeRTOS info, Oct 8, 2012
    #4
  5. ä¼è™Ž

    Noob Guest

    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.
     
    Noob, Oct 8, 2012
    #5
  6. 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.
     
    Hans-Bernhard Bröker, Oct 8, 2012
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.