x86 calling convention: should arguments passed by stack be read-only?

后端 未结 3 797
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 11:26

It seems state-of-art compilers treat arguments passed by stack as read-only. Note that in the x86 calling convention, the caller pushes arguments onto the stack and the callee

3条回答
  •  清酒与你
    2021-02-07 12:18

    The C programming language mandates that arguments are passed by value. So any modification of an argument (like an x++; as the first statement of your foo) is local to the function and does not propagate to the caller.

    Hence, a general calling convention should require copying of arguments at every call site. Calling conventions should be general enough for unknown calls, e.g. thru a function pointer!

    Of course, if you pass an address to some memory zone, the called function is free to dereference that pointer, e.g. as in

    int goo(int *x) {
        static int count;
        *x = count++;
        return count % 3;
    }
    

    BTW, you might use link-time optimizations (by compiling and linking with clang -flto -O2 or gcc -flto -O2) to perhaps enable the compiler to improve or inline some calls between translation units.

    Notice that both Clang/LLVM and GCC are free software compilers. Feel free to propose an improvement patch to them if you want to (but since both are very complex pieces of software, you'll need to work some months to make that patch).

    NB. When looking into produced assembly code, pass -fverbose-asm to your compiler!

提交回复
热议问题