2 # Copyright (C) 2001-2008, Parrot Foundation.
7 Parrot::Op - Parrot Operation
15 C<Parrot::Op> represents a Parrot operation (op, for short), as read
16 from an ops file via C<Parrot::OpsFile>, or perhaps even generated by
17 some other means. It is the Perl equivalent of the C<op_info_t> C
18 C<struct> defined in F<include/parrot/op.h>.
22 Ops are either I<auto> or I<manual>. Manual ops are responsible for
23 having explicit next-op C<RETURN()> statements, while auto ops can count
24 on an automatically generated next-op to be appended to the op body.
26 Note that F<tools/build/ops2c.pl> supplies either 'inline' or 'function'
27 as the op's type, depending on whether the C<inline> keyword is present
28 in the op definition. This has the effect of causing all ops to be
33 Note that argument 0 is considered to be the op itself, with arguments
34 1..9 being the arguments passed to the op.
36 Op argument direction and type are represented by short one or two letter
41 i The argument is incoming
42 o The argument is outgoing
43 io The argument is both incoming and outgoing
47 i The argument is an integer register index.
48 n The argument is a number register index.
49 p The argument is a PMC register index.
50 s The argument is a string register index.
51 ic The argument is an integer constant (in-line).
52 nc The argument is a number constant index.
53 pc The argument is a PMC constant index.
54 sc The argument is a string constant index.
55 kc The argument is a key constant index.
56 ki The argument is a key integer register index.
57 kic The argument is a key integer constant (in-line).
70 =item C<new($code, $type, $name, $args, $argdirs, $labels, $flags)>
72 Allocates a new bodyless op. A body must be provided eventually for the
75 C<$code> is the integer identifier for the op.
77 C<$type> is the type of op (see the note on op types above).
79 C<$name> is the name of the op.
81 C<$args> is a reference to an array of argument type descriptors.
83 C<$argdirs> is a reference to an array of argument direction
84 descriptors. Element I<x> is the direction of argument C<< $args->[I<x>]
87 C<$labels> is a reference to an array of boolean values indicating
88 whether each argument direction was prefixed by 'C<label>'.
90 C<$flags> is a hash reference containing zero or more I<hints> or
97 my ( $code, $type, $name, $args, $argdirs, $labels, $flags ) = @_;
104 ARGDIRS
=> [@
$argdirs],
105 LABELS
=> [@
$labels],
111 return bless $self, $class;
116 =head2 Instance Methods
129 return $self->{CODE
};
134 The type of the op, either 'inline' or 'function'.
141 return $self->{TYPE
};
146 The (short or root) name of the op.
153 return $self->{NAME
};
158 For argumentless ops, it's the same as C<name()>. For ops with
159 arguments, an underscore followed by underscore-separated argument types
160 are appended to the name.
166 my $name = $self->name;
167 my @arg_types = $self->arg_types;
169 $name .= "_" . join( "_", @arg_types ) if @arg_types;
176 The same as C<full_name()>, but with 'C<Parrot_>' prefixed.
181 my ( $self, $trans ) = @_;
183 return $trans->prefix . $self->full_name;
188 Returns the types of the op's arguments.
195 return @
{ $self->{ARGS
} };
198 =item C<arg_type($index)>
200 Returns the type of the op's argument at C<$index>.
207 return $self->{ARGS
}[shift];
212 Returns the directions of the op's arguments.
219 return @
{ $self->{ARGDIRS
} };
231 return @
{ $self->{LABELS
} };
234 =item C<flags(@flags)>
238 Sets/gets the op's flags. This returns a hash reference, whose keys are any
239 flags (passed as ":flag") specified for the op.
247 $self->{FLAGS
} = shift;
250 return $self->{FLAGS
};
253 =item C<arg_dir($index)>
255 Returns the direction of the op's argument at C<$index>.
262 return $self->{ARGDIRS
}[shift];
269 Sets/gets the op's code body.
277 $self->{BODY
} = shift;
280 return $self->{BODY
};
287 Sets/gets a string containing one or more C<op_jump_t> values joined with
288 C<|> (see F<include/parrot/op.h>). This indicates if and how an op
297 $self->{JUMP
} = shift;
300 return $self->{JUMP
};
305 For manual ops, C<full_body()> is the same as C<body()>. For auto ops
306 this method adds a final C<goto NEXT()> line to the code to represent
307 the auto-computed return value. See the note on op types above.
313 my $body = $self->body;
315 $body .= sprintf( " {{+=%d}};\n", $self->size ) if $self->type eq 'auto';
320 # Called from rewrite_body() to perform the actual substitutions.
326 s/{{\@([^{]*?)}}/ $trans->access_arg($self->arg_type($1 - 1), $1, $self); /me;
328 s/{{=0,=([^{]*?)}}/ $trans->restart_address($1) . "; {{=0}}"; /me;
329 s/{{=0,\+=([^{]*?)}}/ $trans->restart_offset($1) . "; {{=0}}"; /me;
330 s/{{=0,-=([^{]*?)}}/ $trans->restart_offset(-$1) . "; {{=0}}"; /me;
332 s/{{\+=([^{]*?)}}/ $trans->goto_offset($1); /me;
333 s/{{-=([^{]*?)}}/ $trans->goto_offset(-$1); /me;
334 s/{{=([^*][^{]*?)}}/ $trans->goto_address($1); /me;
336 s/{{\^(-?\d+)}}/ $1 /me;
337 s/{{\^\+([^{]*?)}}/ $trans->expr_offset($1); /me;
338 s/{{\^-([^{]*?)}}/ $trans->expr_offset(-$1); /me;
339 s/{{\^([^{]*?)}}/ $trans->expr_address($1); /me;
344 =item C<rewrite_body($body, $trans)>
346 Performs the various macro substitutions using the specified transform,
347 correctly handling nested substitions, and repeating over the whole string
348 until no more substitutions can be made.
350 C<VTABLE_> macros are enforced by converting C<<< I<< x >>->vtable->I<<
351 method >> >>> to C<VTABLE_I<method>>.
356 my ( $self, $body, $trans ) = @_;
364 )->vtable->\s
*(\w
+)\
(
368 my $new_body = $self->_substitute( $body, $trans );
370 last if $body eq $new_body;
378 =item C<source($trans)>
380 Returns the L<C<full_body()>> of the op with substitutions made by
381 C<$trans> (a subclass of C<Parrot::OpTrans>).
386 my ( $self, $trans ) = @_;
388 my $flags = $self->flags;
390 if (exists($$flags{pic
})
391 && !( ref($trans) eq 'Parrot::OpTrans::CGP' || ref($trans) eq 'Parrot::OpTrans::CSwitch' ) )
393 return qq{PANIC
(interp
, "How did you do that");\n};
396 my $prelude = $trans->can( 'add_body_prelude' )
397 ?
$trans->add_body_prelude()
400 return $self->rewrite_body( $prelude . $self->full_body, $trans );
405 Returns the op's number of arguments. Note that this also includes
406 the op itself as one argument.
413 return scalar( $self->arg_types + 1 );
422 =item C<Parrot::OpsFile>
424 =item C<Parrot::OpTrans>
426 =item F<tools/build/ops2c.pl>
428 =item F<tools/build/ops2pm.pl>
430 =item F<tools/build/pbc2c.pl>
436 Author: Gregor N. Purdy E<lt>gregor@focusresearch.comE<gt>
444 # cperl-indent-level: 4
447 # vim: expandtab shiftwidth=4: