You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When nesting multiple function calls, instructions seem to occur in a strange and difficult to explain order, sometimes causing the return values of some of the functions to be mysteriously discarded.
Minimum Reproducible Example:
func main() returns void {
int x = add1(return1());
printf("{}", x);
}
func return1() returns int {
return 1;
}
func add1(int x) returns int {
return x + 1;
}
str w0, [sp, 0] - Variable x is declared and the initial value of zero is placed in its location on the stack, sp + 0
bl return11 - Function return1 is called
mov w9, w0 - Value returned from return1 is moved to a temporary register (w9)
mov w0, w9 - The value in w9 is moved back into w0, as it is the first and only argument passed into add1
bl add11 - Function add1 is called, passing result from return1 in
Where things get weird
bl return11 - Function return1 is called again for some reason, overwriting the return value from add1 with its own
The format string "%d" is moved into x0:
adrp x0, L1@PAGE
add x0, x0, L1@PAGEOFF
ldr w9, [sp, 0] - The value of variable x is loaded into w9. Note that x had not been updated to contain the result of the nested function call, and even if it had been, it would contain the wrong value because of the second call to return1.
The incorrect value of x is stored on the stack to be passed into printf, which is then called, printing 1 to the console:
str w9, [sp, -32]!
bl _printf
add sp, sp, 32
This must be caused by the code generation for function calls, assignments, or possibly both.
The text was updated successfully, but these errors were encountered:
When nesting multiple function calls, instructions seem to occur in a strange and difficult to explain order, sometimes causing the return values of some of the functions to be mysteriously discarded.
Minimum Reproducible Example:
Output:
Generated assembly code:
Let's break this down:
Working as expected
str w0, [sp, 0]
- Variablex
is declared and the initial value of zero is placed in its location on the stack,sp + 0
bl return11
- Functionreturn1
is calledmov w9, w0
- Value returned fromreturn1
is moved to a temporary register (w9
)mov w0, w9
- The value inw9
is moved back intow0
, as it is the first and only argument passed intoadd1
bl add11
- Functionadd1
is called, passing result fromreturn1
inWhere things get weird
bl return11
- Functionreturn1
is called again for some reason, overwriting the return value fromadd1
with its own"%d"
is moved intox0
:ldr w9, [sp, 0]
- The value of variablex
is loaded intow9
. Note thatx
had not been updated to contain the result of the nested function call, and even if it had been, it would contain the wrong value because of the second call toreturn1
.x
is stored on the stack to be passed intoprintf
, which is then called, printing1
to the console:This must be caused by the code generation for function calls, assignments, or possibly both.
The text was updated successfully, but these errors were encountered: