Page 163 Figure 8.5 – return, step3: *ARG = pop()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Page 163 Figure 8.5 – return, step3: *ARG = pop()

gs99
In the left column (VM command), the first command - call says:
“(calling a function f after n arguments have been pushed onto the stack)”

These are pushes done outside/before the call command.

The third command - return says:
“(from a function)”

Shouldn’t this third command say: “from a function after the return value has been pushed”?
This (return value) push is also done outside/before the return command.

If that push is not done before return starts, the current “top” value is popped:
… If the function has local variables, the latest variable value is popped.  
… If the function has 0 variables, the THAT value is popped.  

For this third step to work as expected, the return value must be pushed before return starts.
 

Reply | Threaded
Open this post in threaded view
|

Re: Page 163 Figure 8.5 – return, step3: *ARG = pop()

cadet1620
Administrator
The call description include the "after n arguments" because n is a part of the call VM command and its value affects how ARG is set.

The fact that there must always be a return value pushed on the stack is explicitly stated in gray boxed text two pages earlier.
    "Before returning, the called function must push a value onto the stack."

When you get to the compiler, you will see that this is also true for void functions (functions that have no declared return value). The void function pushes a 0 and the caller pops and discards it.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Page 163 Figure 8.5 – return, step3: *ARG = pop()

gs99
I was looking at it from the standpoint of "what's needed for these commands?"

push the arguments before call,
push the return value before return

Yes it's mentioned two pages ago, but why not repeat it here to provide a complete picture?

Anyway, I wrote it in my book as a reminder.