how to let Inline assembly pass -O1 optimization

拈花ヽ惹草 提交于 2019-12-12 01:14:25

问题


I have following dispatch code for my user level thread library.

The code can pass GCC and runs correctly without optimization, but if I choose -O1 optimization (also higher levels), when run the code, program generates segmentation fault.

Basically the function does save context and jump to next context.

void __attribute__ ((noinline)) __lwt_dispatch(lwt_context *curr, lwt_context *next)
{
__asm__ __volatile
    (

    "mov 0xc(%ebp),%eax\n\t"
    "mov 0x4(%eax),%ecx\n\t"
    "mov (%eax),%edx\n\t"
    "mov 0x8(%ebp),%eax\n\t"
    "add $0x4,%eax\n\t"
    "mov 0x8(%ebp),%ebx\n\t"
    "push %ebp\n\t"
    "push %ebx\n\t"
    "mov %esp,(%eax)\n\t"
    "movl $return,(%ebx)\n\t"
    "mov %ecx,%esp\n\t"
    "jmp *%edx\n\t"
    "return: pop %ebx\n\t"
    "pop %ebp\n\t"
    );
}

回答1:


Thanks for help, I figured out some ways to solve it.

  1. Normally compile this function as a separate .o file then use O3 to optimize it with other files.

  2. using inline assembly is much easier and simpler than this function. Like below:

    int foo = 10, bar = 15; asm volatile("addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) ); printf("foo+bar=%d\n", foo);

  3. Another post has helped me figuring out labeling problem, see here: Labels in GCC inline assembly



来源:https://stackoverflow.com/questions/42019107/how-to-let-inline-assembly-pass-o1-optimization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!