I'm looking at some compiler output for a MIPS platform and struggling to understand how a function returns and what is allowable.
Here's a simple example:
int two_x_squared(int x)
{
return 2*x*x;
}
If I compile it with Compiler Explorer I see
two_x_squared(int):
sll $2,$4,1
mult $2,$4
mflo $2
j $31
nop
OK, no big deal here, I'm guessing j $31
jumps to the return address, and the nop
might be something required to protect against speculative execution in the pipeline.
But then I compile with XC32 under -O2
and I get
two_x_squared:
mul $4,$4,$4
j $31
sll $2,$4,1
So... the line after the j $31
gets executed after the jump?!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…