Hello,

(* I'm trying a lot of things out, to figure out each part of the Lua language. This is Yet Another Question , this time concerning "missing" arguments *)

I'm currently figuring out how "missing arguments" should be handled. Suppose there is a function foo, that takes 4 parameters. In Lua it is allowed not to pass all arguments (or even none), but then the argument is just /null/ or in Lua terms /nil/. However, it should not be a NULL PMC, but it should be something like PerlUndef (but later on it should be replaced by LuaNil PMC). For that to work, there must be some extra code that checks on NULL PMCs. I figured out how it could be done. It can be seen in the following code snippet.

.sub _main
.local pmc p
.local pmc q
.local pmc r
.local pmc s p = new .PerlInt
q = new .PerlInt
r = new .PerlInt
s = new .PerlInt p = 1
q = 2
r = 3
s = 4


_f()
_f(p)
_f(p, q)
_f(p, q, r)
_f(p, q, r, s) _f(p, q, r)
_f(p, q)
_f(p)
_f()


   end
.end

.sub _f non_prototyped
# fixed parameters
.param pmc a
.param pmc b
.param pmc c
.param pmc d
# check on arguments being NULL
isnull a, L1
isnull b, L2
isnull c, L3
isnull d, L4
branch L5
L1:
a = new .PerlInt # later on this should be LuaNil, but this is for testing
a = 0
L2: b = new .PerlInt
b = 0
L3:
c = new .PerlInt
c = 0
L4:
d = new .PerlInt
d = 0
L5: # body
print "a = "
print a
print "; b = "
print b
print "; c = "
print c
print "; d = "
print d
print "\n"
.end


When I run this program, I get the following output:

a = 0; b = 0; c = 0; d = 0   <= ok!
a = 1; b = 0; c = 0; d = 0   <= ok!
a = 1; b = 2; c = 0; d = 0   <= ok!
a = 1; b = 2; c = 3; d = 0   <= ok!
a = 1; b = 2; c = 3; d = 4   <= ok!
a = 1; b = 2; c = 3; d = 4   <= huh?
a = 1; b = 2; c = 3; d = 4   <= huh?
a = 1; b = 2; c = 3; d = 4   <= huh?
a = 1; b = 2; c = 3; d = 4   <= huh?

The first 5 lines are ok, I expected those. I'm wondered by the last 4 lines.
The arguments are still being passed to the function, while it is clear I didn't call the function
with those arguments (see above: _f(), _f(p), etc.)


I translated the thing to PASM, and that explained a lot: the arguments from previous calls are
still in P5, P6 etc.


This can easily be solved by nulling these registers, but that seems a bit strange to me.
But on the other hand, it may be that if I want this functionality (as described) I should take care of it myself (thus nulling the PMC argument register before making a new call).


Anyway, my question is: is this normal behaviour of PIR/IMCC? Should I be aware of this fact and
generating extra code to null out argument registers before making a call to a function?


Regards
Klaas-Jan



Reply via email to