NAME ^

PAST::Compiler - PAST Compiler

DESCRIPTION ^

PAST::Compiler implements a basic compiler for PAST nodes. By default PAST::Compiler transforms a PAST tree into POST.

Signature Flags ^

Throughout the compiler PAST uses a number of 1-character "flags" to indicate allowable register types and conversions. This helps the compiler generate more efficient code and know what sorts of conversions are allowed (or desired). The basic flags are:

    P,S,I,N   PMC, string, int, or num register
    s         string register or constant
    i         int register or constant
    n         num register or constant
    r         any register result
    v         void (no result)
    *         any result type except void
    +         PMC, int register, num register, or numeric constant
    ~         PMC, string register, or string constant
    :         argument (same as '*'), possibly with :named or :flat

These flags are used to describe signatures and desired return types for various operations. For example, if an opcode is specified with a signature of I~P*, then the opcode places its result in an int register, its first child is coerced into some sort of string value, its second child is coerced into a PMC register, and the third and subsequent children can return any value type.

Compiler methods ^

to_post(node [, 'option'=>option, ...])

Compile the abstract syntax tree given by past into POST.

escape(str)

Return str as a PIR constant string.

unique([STR fmt])

Generate a unique number that can be used as an identifier. If fmt is provided, then it will be used as a prefix to the unique number.

uniquereg(rtype)

Generate a unique register based on rtype, where rtype is one of the signature flags described above.

coerce(post, rtype)

Return a POST tree that coerces the result of post to have a return value compatible with rtype. rtype can also be a specific register, in which case the result of post is forced into that register (with conversions as needed).

post_children(node [, 'signature'=>signature] )

Return the POST representation of evaluating all of node's children in sequence. The signature option is a string of flags as described in "Signature Flags" above. Since we're just evaluating children nodes, the first character of signature (return value type) is ignored. Thus a signature of v~P* says that the first child needs to be something in string context, the second child should be a PMC, and the third and subsequent children can be any value they wish.

Methods on PAST::Node arguments ^

The methods below are used to transform PAST nodes into their POST equivalents.

Defaults

as_post(node) (General)

Return a POST representation of node. Note that post is a multimethod based on the type of its first argument, this is the method that is called when no other methods match.

as_post(Any)

This is the "fallback" method for any unrecognized node types. We use this to throw a more useful exception in case any non-PAST nodes make it into the tree.

as_post(Undef)

Return an empty POST node that can be used to hold a (PMC) result.

as_post(String class)

Generate POST to create a new object of type class. This is typically invoked by the various vivification methods below (e.g., in a PAST::Var node to default a variable to a given type).

as_post(PAST::Node node)

Return the POST representation of executing node's children in sequence. The result of the final child is used as the result of this node.

N.B.: This method is also the one that is invoked for converting nodes of type PAST::Stmts.

PAST::Block

as_post(PAST::Block node)

Return the POST representation of a PAST::Block.

PAST::Op

as_post(PAST::Op node)

Return the POST representation of a PAST::Op node. Normally this is handled by redispatching to a method corresponding to the node's "pasttype" attribute.

pirop(PAST::Op node)

Return the POST representation of a PAST::Op node with a 'pasttype' of 'pirop'.

call(PAST::Op node)

Return the POST representation of a PAST::Op node for calling a sub.

callmethod(PAST::Op node)

Return the POST representation of a PAST::Op node to invoke a method on a PMC.

if(PAST::Op node)

unless(PAST::Op node)

Return the POST representation of PAST::Op nodes with a 'pasttype' of if/unless.

while(PAST::Op node)

until(PAST::Op node)

Return the POST representation of a while or until loop.

repeat_while(PAST::Op node)

repeat_until(PAST::Op node)

Return the POST representation of a repeat_while or repeat_until loop.

for(PAST::Op node)

Return the POST representation of the for loop given by node.

list(PAST::Op node)

Build a list from the children. The type of list constructed is determined by the returns attribute, which defaults to ResizablePMCArray if not set.

return(PAST::Op node)

Generate a return exception, using the first child (if any) as a return value.

try(PAST::Op node)

Return the POST representation of a PAST::Op node with a 'pasttype' of bind. The first child is the code to be surrounded by an exception handler, the second child (if any) is the code to process the handler.

chain(PAST::Op node)

A short-circuiting chain of operations. In a sequence of nodes with pasttype 'chain', the right operand of a node serves as the left operand of its parent. Each node is evaluated only once, and the first false result short-circuits the chain. In other words, $x < $y < $z is true only if $x < $y and $y < $z, but $y only gets evaluated once.

def_or(PAST::Op node)

The short-circuiting default operator (e.g., Perl 6's infix:<//>). Returns its first child if its defined, otherwise it evaluates and returns the second child. (N.B.: This particular pasttype is a candidate for being refactored out using thunks of some sort.)

xor(PAST::Op node)

A short-circuiting exclusive-or operation. Each child is evaluated, if exactly one child evaluates to true then its value is returned, otherwise return Undef. Short-circuits with Undef as soon as a second child is found that evaluates as true.

bind(PAST::Op node)

Return the POST representation of a PAST::Op node with a 'pasttype' of bind.

copy(PAST::Op node)

Implement a 'copy' assignment (at least until we get the 'copy' opcode -- see RT#47828).

inline(PAST::Op node)

Return the POST representation of a PAST::Op node with a 'pasttype' of inline.

PAST::Var

as_post(PAST::Block node)

Return the POST representation of a PAST::Var. Generally we redispatch to an appropriate handler based on the node's 'scope' attribute.

PAST::Val

as_post(PAST::Val node [, 'rtype'=>rtype])

Return the POST representation of the constant value given by node. The rtype parameter advises the method whether the value may be returned directly as a PIR constant or needs to have a PMC generated containing the constant value.

AUTHOR ^

Patrick Michaud <pmichaud@pobox.com> is the author and maintainer. Please send patches and suggestions to the Parrot porters or Perl 6 compilers mailing lists.

HISTORY ^

2006-11-20 Patrick Michaud added first draft of POD documentation. 2006-11-27 Significant refactor into separate modules.

COPYRIGHT ^

Copyright (C) 2006-2008, The Perl Foundation.


parrot