This is the mail archive of the mailing list for the Kawa project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Handling the Perl VM's "list" op with Kawa IR

My work on compiling Perl to Kawa's IR is proceeding nicely, however I hit a
minor snag and I would like some advice.


Perl's "virtual machine" has basically two components: 

   * a syntax tree of OP codes
   * a temporary array, used to hold values between one operation to the

For example, consider this  simple perl program:

   print qw/H i/, "\n";

(This says, roughly: create a list entity, of two elements, "H" and "i".
 Then, call print with the arguments of that list, and "\n").

The internal function "print" doesn't actually handle an arbitrarily deep
list, though.  It takes a list of scalar expressions.  So, to make things
work inside the Perl VM, there is another OP code, the generic "list" OP
code, that says, roughly: "Place these items on the temporary array, so a
later OP code can find them as a simple list".

A so-called "list mark" is placed in the array for the various "list"
operations to find their place.  Evaluating a plain "list" operation
basically finds the last "list mark" and removes it.

A "print" OP is a special kind of "list" operator that prints all scalar
arguments in the array and clears the array.

Thus, if you execute:

print qw/H i/, " ", qw/w o r l d/, "\n";

You get an OP tree something like this:

                          /    |      |      \
                         /    " "    list    "\n"
                       list        / | | \ \   
                       |  |        w o r l d
                       H  i

The stack goes through states that look like this (*'s represent marks):

pre-eval of "print"

pre-evaluation of list OP of qw/H i/

   * * H i

evaluation of " "

   * * H i " "

pre-evaluation of list OP of qw/W o r l d/

   * * H i " " * W o r l d

post-evaluation of list OP of qw/W o r l d/

   * * H i " " W o r l d

post-evaluation of list OP of qw/H i/

   * H i " " W o r l d

post-eval of "print"

   (Empty stack, side effect is that elements of the stack are printed in
    order from mark forward)

(You can see and examine this behavior if you have perl compiled with
 debugging and try these commands:

      $ perl -Dts -e 'print qw/H i/, " ", qw/w o r l d/, "\n";'
      $ perl -MO=Terse -e 'print qw/H i/, " ", qw/w o r l d/, "\n";'


The Kawa IR, as far as I can tell, doesn't seem to have an sort of
"temporary work space" where I can save state between expressions in this
way  (nor should it; this behavior is on of the reasons the Perl IR is so
hard to do deal with).

However, I need a way to make this behavior happen in an elegant way.

One idea I had was to make it possible for all special list OPs (such as
"print") have the option of taking Perl arrays as arguments as merely a list
of Perl Scalar expressions.  If I do this, though, it means the code for
print and all the other special list ops needs to look something like:

    print(Object args[])
        for (int ii = 0; ii < args.length; ii++ )
            if (args[ii] instanceof PerlArray) { // handle each element }
            else if (args[ii] instanceof PerlScalar) { // handle this }

Then, plain "list" OPs would simply build that PerlArray, and wrap it with a

This seemed somewhat inelegant, and it seemed like I'd be needlessly
creating PerlArray objects often.

The other idea I had was to have another expression type "ArgListExp",
similar to BeginExpr, that would simple be a container that could be
arbitrarily deep.  I could then flattened it into a simple Expression[]
right before creating the ApplyExp into one simple expression list.

(It also looks there might have been plans to do have this sort of
flattening with BeginExpr.)

Any suggestions?

Bradley M. Kuhn  -

PGP signature

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]