Hi Adrian,

> On 10 Oct 2011, at 19:56, Ralph Corderoy wrote:
> > A nice description of Lua 5's implementation, including its hybrid
> > hash-table/array implementation, and using a register-based VM
> > compared to its earlier stack-based VM as Python and Perl still use.
> > http://www.jucs.org/jucs_11_7/the_implementation_of_lua/jucs_11_7_1159_1176_defigueiredo.html
>
> Niggle ;-)

You're among pedants here.  :-)

> Perl 4/5 doesn't really have a VM per se. The code is turned into a
> syntax tree that's then walked at runtime. See
> http://perldoc.perl.org/perlcompile.html.

Yes, you're right, it does walk the syntax tree and not turn it into
bytecode that's then run on the VM, like Python, I should have been more
precise.  But the operations carried out are at the level of bytecode
instructions, and they operate on a data stack, e.g. `multiply' pops the
top two items and pushes the result so I still think the evaluation is
stack-based rather than Lua's registers or Parrot.

Here's the stack contents and the operation from perl's -D option that
I've then annotated on the right.

    $ perl -Dst -e 'sub f() {my($a, $b) = @_; $a * 42 + $b} &f(9, 13)' 2>&1 |
    > expand |
    > awk '/=>/ {printf "%-44s", $0; next} 1' |
    > sed -n '/=>/s/(-e:.) *//p'
        =>                                      enter
        =>                                      nextstate
        =>                                      pushmark
        =>  *                                   const(IV(9))
        =>  *  IV(9)                            const(IV(13))
        =>  *  IV(9)  IV(13)                    gv(main::f)
        =>  *  IV(9)  IV(13)  GV()              entersub       # &f(9, 13)
        =>  IV(9)  IV(13)                       nextstate
        =>                                      pushmark
        =>  *                                   gv(main::_)
        =>  *  GV()                             rv2av
        =>  *  IV(9)  IV(13)                    pushmark
        =>  *  IV(9)  IV(13)  *                 padsv($a)
        =>  *  IV(9)  IV(13)  *  UNDEF          padsv($b)
        =>  *  IV(9)  IV(13)  *  UNDEF  UNDEF   aassign        # my($a, $b) = @_
        =>                                      nextstate
        =>                                      padsv($a)
        =>  IV(9)                               const(IV(42))
        =>  IV(9)  IV(42)                       multiply       # $a * 42
        =>  IV(378)                             padsv($b)
        =>  IV(378)  IV(13)                     add            # ... + $b
        =>  IV(391)                             leavesub
        =>  IV(391)                             leave
    $

And for completeness, Python's bytecode.

    >>> from dis import dis
    >>> def f(a, b): return a * 42 + b
    ...
    >>> def g(): f(9, 13)
    ...
    >>> dis(f)
      1           0 LOAD_FAST                0 (a)
                  3 LOAD_CONST               1 (42)
                  6 BINARY_MULTIPLY
                  7 LOAD_FAST                1 (b)
                 10 BINARY_ADD
                 11 RETURN_VALUE
    >>> dis(g)
      1           0 LOAD_GLOBAL              0 (f)
                  3 LOAD_CONST               1 (9)
                  6 LOAD_CONST               2 (13)
                  9 CALL_FUNCTION            2
                 12 POP_TOP
                 13 LOAD_CONST               0 (None)
                 16 RETURN_VALUE
    >>>

Cheers, Ralph.

--
Next meeting:  Bournemouth, Tuesday 2011-11-01 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
How to Report Bugs Effectively:  http://goo.gl/4Xue

Reply via email to