Hello,
I stumbled upon something really interesting happening to the code generated by the toolchain riscv64-unknown-elf-*.
I have something simple like:
uint32_t function (uint32_t param) {
uint32_t result = aux_func(param); /* very simple function */
asm_func(); /* extended assembly code */
return result;
}
uint32_t aux_func(uint32_t param) {
return param + 1;
}
void asm_func(void) {
__asm__ volatile ("slli a0, a0, 2" : : : );
}
The compiled assembly looks like:
patch_function_0:
slli a0, a0, 2 /* asm_function */
addi a0,a0,1 /* aux_function */
ret
So it effectively changed the order of the functions calls! (Sorry I found no good option to format the code)
One might say: “That happened because you haven’t told the compiler about your assembly changes in the clobber list”.
Correct, but I did it on purpose, because I want to change something without interference from the compiler. However the compiler changed the order of the function calls in a way that I haven’t predicted.
So, please how can I create some kind of barrier so that the first function is called before the second?
I read about FENCE and the RISC-V Memory Model, but it doesn’t look like a solution for my problem. Is there anything I can use to instruct the compiler not to re-order those instructions?
Thank you!