All the magic about handling parameters in Parrot, all the slurpy params, flat args, named args, and optional params are all sorted out in the function Parrot_process_args. This function is called most often from parrot_pass_args and parrot_pass_args_fromc. These, in turn, are called from some very interesting places:
1) It gets called from the get_params opcode
2) Called from the set_returns opcodes
3) It gets called from inside the generated code of C-defined methods
4) a handful of other places such as in exception handlers called from certain places.
Parrot_process_args depends on certain values from the callers context, such as Keys (that I discussed last time) that can store references to registers instead of their values directly. This can cause a problem because of the various invocation paths and their handling of contexts in different ways:
1) Parrot_PCCINVOKE and Parrot_pcc_invoke_* functions create a new context to store passed-in params, and then invoke their sub objects.
2) The invoke vtable of a Sub creates a new context for itself
3) The generated C code of a C-defined method creates a new context for itself (NCI PMCs do not create one when invoked, so this is the workaround for that)
So if we are going through a Parrot_pcc_invoke_* call, we're going to be creating two contexts for almost every call: One context created in Parrot_pcc_invoke_* to hold the passed-in params, and one context created in the Sub or METHOD itself. Now when we finally do call Parrot_process_args, it can be either 1 or two levels below the context of the caller, which causes all sorts of problems when it comes to register references. I've conceived of an idea to store the callers context somewhere (possibly inside the CallSignature PMC somewhere) to handle these corner cases, but that might be a job for much much later in the refactoring process.
I've tried to resolve these references in Parrot_pcc_build_sig_object_from_varargs, but every time I do it seems to create hangs or crashes that I don't quite understand yet. I plan to get to the bottom of that soon.