Best Practice For Function Declarations and Calls in my VMWriter

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

Best Practice For Function Declarations and Calls in my VMWriter

jrd
I have encountered two conceptual programming issues that I would appreciate some clarity on:

1) When implementing VMWriter function declaration routine (i.e., writeFunction), one needs to know the number of local variables (i.e., nLocals) that a given function will subsequently utilize.  However, at the point in the source code when a function's declaration-related tokens are being parsed, the function's local variables have not yet been encountered/are NOT yet declared.  Consequently, the local variables are also not yet included in the function's Subroutine Scope Table that's in the process of being defined.  Therefore, although I've been generally able to handle my VM code generation as my source file is being parsed (also as I was able to handle my XML writer), this case requires me to somehow delay and write the function declaration code generation AFTER the local variables declarations are parsed and analyzed (and the function's Subroutine Scope Table is completed) - in order to determine the value of nLocals.  Is that accurate?

2) When implementing VMWriter function invocation routine (i.e., writeCall) as part of a "do statement," one needs to know the number of arguments (i.e., nArgs) that have been previously pushed onto the stack for the given function.  However, the number of arguments for a given function are included in a Subroutine Scope Table that was created and destroyed at the point in the source code when the function was initially declared/not at the point where the function is being called.  So, therefore, in order to deduce the number of arguments required for a given function at the moment that it's being called, I am referring to its accompanying parameter list and counting the number variables that are passed into the function.  Is this the most efficient/best way of handling?

Any observations/clarification would be helpful.  Thx.
Reply | Threaded
Open this post in threaded view
|

Re: Best Practice For Function Declarations and Calls in my VMWriter

cadet1620
Administrator
jrd wrote
I have encountered two conceptual programming issues that I would appreciate some clarity on:

1) When implementing VMWriter function declaration routine (i.e., writeFunction), one needs to know the number of local variables (i.e., nLocals) that a given function will subsequently utilize.  However, at the point in the source code when a function's declaration-related tokens are being parsed, the function's local variables have not yet been encountered/are NOT yet declared.  
This is correct. You need to delay generation of the "function" command until after the local variables are parsed.
2) When implementing VMWriter function invocation routine (i.e., writeCall) as part of a "do statement," one needs to know the number of arguments (i.e., nArgs) that have been previously pushed onto the stack for the given function.  However, the number of arguments for a given function are included in a Subroutine Scope Table that was created and destroyed at the point in the source code when the function was initially declared...
The function that you are calling my not be in the source file that you are compiling, so you have no choice but to count the number of arguments that are being pushed:
    do Foo.bar(x, y, z);

My compiler's CompileExpressionList() routine returns the number of expressions that were compiled.

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

Re: Best Practice For Function Declarations and Calls in my VMWriter

jrd
Mark:

Thank you.  This explanation/information very helpful.

- JRD