[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua parser question re function calls in local statement
- From: Dibyendu Majumdar <mobile@...>
- Date: Sat, 17 Jan 2015 17:37:59 +0000
On 17 January 2015 at 16:36, Dibyendu Majumdar <mobile@majumdar.org.uk> wrote:
> local i,j,k = f(), g()
>
> Initially the OP_CALL instruction has operand C set to 2 for both the
> calls, but later on the call to g() is patched so that C is set to 3
> for the call to g(). I was trying to generate special op codes to
> coerce types after each function call - but the problem is at the time
> of call to the function the parser does not know how many values to
> assign from the function call. As code for f() has already been
> generated by the time the call to g() is patched - I cannot go back
> and insert extra instructions, especially as some expressions go back
> and update specific instructions so all those anchors would be
> invalidated by any insertion of new instructions.
>
> The inefficient way seems to be to apply type coercion to i,j, and k
> after call to f() and then again to j,k after call to g() ;-(
>
> So back to the drawing board.
Looks like there is an easy solution to this:
Initially generate instructions for 1 return value for each function.
Then after the call to g() is patched, generate extra instructions for
the remaining values - as these are guaranteed to be at the end.
So for example:
local i:int, j:double = f()
Produces:
1 [1] GETTABUP 0 0 -1 ; _ENV "f"
2 [1] CALL 0 1 3
3 [1] TOINT 0 0 0
4 [1] TOFLT 1 0 0
5 [1] RETURN 0 1
And:
local i:int, j:double, k:int = f(), g()
Produces:
1 [1] GETTABUP 0 0 -1 ; _ENV "f"
2 [1] CALL 0 1 2
3 [1] TOINT 0 0 0
4 [1] GETTABUP 1 0 -2 ; _ENV "g"
5 [1] CALL 1 1 3
6 [1] TOFLT 1 0 0
7 [1] TOINT 2 0 0
8 [1] RETURN 0 1
Here TOFLT and TOINT are new instructions that perform tointeger and
tonumber on register value at A.