Next: Accessing the IR via Up: The perl Intermediate Representation Previous: The ``Defined'' Example   Contents

The ``Add and Print'' Example

Figure 3.3: ``Add and Print'' Perl Program Example
This Program:
$x = 5;
$y = $x + 7;
print "RESULT: $y", "\n";

Figure 3.3 shows a more complex example. This example has variable use and assignment, as well as a print statement. The OP-tree diagram of this program is in Figure 3.4.

Figure 3.4: OP-Tree of ``Add and Print'' Example
*** Example only available as a graphic ***
(Xfig Source and PDF version of diagram available.)

This example introduces the two BINOPs (or binary OPs)--``sassign'' and ``concat''. The ``sassign'' BINOP is used for variable assignment. Note that there is a dummy ``null'' UNOP3.2 above the SVOP that is used to pass extra options to the ``gvsv'' SVOP. In this case, these options denote that the results of the ``gvsv'' OP will be used as an l-value.

Another OP that is introduced in this example is the ``pushmark'' OP. This OP refers directly to an operation on the PVM run-time stack. When the PVM evaluates the OP-tree, the PVM run-time stack is used to hold OP evaluation results as they are built during evaluation. In this example, the ``print'' LISTOP expects to use the PVM run-time stack to keep track of the arguments to be printed.

Thus, the ``print'' LISTOP first performs a ``pushmark'' to note where its arguments begin on the stack. After the PVM has evaluated the other child OPs of ``print'', ``print'' finds its mark on the stack, and prints all arguments after the mark.

Figure 3.5: Partial Stack Evaluation of ``Add and Print'' Example
OPconst(PV("RESULT: "))
STACK →* PV("\n") PV("RESULT: 12")

This behavior can be demonstrated by using perl's debugging command-line options, -Dts. Figure 3.5 shows this debugging output for the ``print'' operation. (The mark is represented in this output by an *. Note, too, that PV and NV refer to the perl internal string and number types, respectively.)

The figure shows that after the evaluation of ``print'', all arguments back to and including the mark have been consumed (and the side effect of printing those items to the standard output occurs, of course). ``print'' leaves a single item on the stack, a scalar value of ``true'' (called SV_YES internally by perl).

As that SV_YES shows, it is not only those OPs that perform a ``pushmark'' that use the run-time stack. Most OPs, when evaluated by the PVM, leave some items on the stack. In fact, one of the purposes of the ``nextstate'' OP is to clear the stack of unconsumed items.

It should be noted as well that some other OPs, such as ``concat'', use the stack for multiple items, even though they do not perform a ``pushmark''. In those cases, the OP defines a constant number of stack items it will consume. For example, BINOPs like ``concat'' always consume exactly two items.

Next: Accessing the IR via Up: The perl Intermediate Representation Previous: The ``Defined'' Example   Contents

Copyright © 2000, 2001 Bradley M. Kuhn.

Verbatim copying and distribution of this entire thesis is permitted in any medium, provided this notice is preserved.