11 shift if UNIVERSAL::isa($_[0], 'Imager::Expr');
21 my %default_constants =
23 # too many digits, better than too few
24 pi=>3.14159265358979323846264338327950288419716939937510582097494
28 my ($class, $opts) = @_;
30 # possibly this is a very bad idea
31 my ($type) = grep exists $expr_types{$_}, keys %$opts;
32 die "Imager::Expr: No known expression type"
34 my $self = bless {}, $expr_types{$type};
35 $self->{variables} = [ @{$opts->{variables}} ];
36 $self->{constants} = { %default_constants, %{$opts->{constants} || {}} };
37 $self->{ops} = $self->compile($opts->{$type}, $opts)
41 $self->{code} = $self->assemble()
47 my ($pack, $name) = @_;
48 $expr_types{$name} = $pack;
52 return @{$_[0]->{variables}};
60 return $_[0]->{nregs};
64 return $_[0]->{cregs};
67 my $numre = '[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?';
77 my @ops = @{$self->{ops}};
79 # this function cannot current handle code with jumps
80 return 1 if grep $_->[0] =~ /^jump/, @ops;
82 # optimization - common sub-expression elimination
83 # it's possible to fold this into the code generation - but it will wait
85 my $max_opr = $Imager::Regops::MaxOperands;
86 my $attr = \%Imager::Regops::Attr;
95 my $desc = join(",", @{$op}[0..$max_opr]);
99 my $new = $seen{$desc};
101 for my $reg (@{$op}[1..$max_opr]) {
102 $reg = $new if $reg eq $old;
108 $seen{$desc} = $op->[-1];
115 # reduce division by a constant to multiplication by a constant
116 if ($op->[0] eq 'div' && $op->[2] =~ /^r(\d+)/
117 && defined($self->{"nregs"}[$1])) {
118 my $newreg = @{$self->{"nregs"}};
119 push(@{$self->{"nregs"}}, 1.0/$self->{"nregs"}[$1]);
121 $op->[2] = 'r'.$newreg;
124 $self->{ops} = \@ops;
130 my $attr = \%Imager::Regops::Attr;
131 my $max_opr = $Imager::Regops::MaxOperands;
132 my @ops = @{$self->{ops}};
134 $op->[0] = $attr->{$op->[0]}{opcode};
135 for (@{$op}[1..$max_opr+1]) { s/^[rpj]// }
137 my $pack = $Imager::Regops::PackCode x (2+$Imager::Regops::MaxOperands);
139 return join("", ,map { pack($pack, @$_, ) } @ops);
142 # converts stack code to register code
144 my ($self, @st_ops) = @_;
147 my @vars = $self->_variables();
148 my @nregs = (0) x scalar(@vars);
150 my $attr = \%Imager::Regops::Attr;
153 my $max_opr = $Imager::Regops::MaxOperands;
154 @vars{@vars} = map { "r$_" } 0..$#vars;
159 # combining constants makes the optimization below work
160 if (exists $nregs{$_}) {
161 push(@regstack, $nregs{$_});
164 $nregs{$_} = "r".@nregs;
165 push(@regstack,"r".@nregs);
169 elsif (exists $vars{$_}) {
170 push(@regstack, $vars{$_});
172 elsif (exists $attr->{$_} && length $attr->{$_}{types}) {
173 if (@regstack < $attr->{$_}{parms}) {
174 error("Imager::transform2: stack underflow on $_");
177 my @parms = splice(@regstack, -$attr->{$_}{parms});
178 my $types = join("", map {substr($_,0,1)} @parms);
179 if ($types ne $attr->{$_}{types}) {
180 if (exists $attr->{$_.'p'} && $types eq $attr->{$_.'p'}{types}) {
184 error("Imager::transform2: Call to $_ with incorrect types");
189 if ($attr->{$_}{result} eq 'r') {
190 $result = "r".@nregs;
194 $result = "p".@cregs;
197 push(@regstack, $result);
198 push(@parms, "0") while @parms < $max_opr;
199 push(@ops, [ $_, @parms, $result ]);
200 #print "$result <- $_ @parms\n";
204 error("Imager::transform2: stack underflow with $_");
207 $names{$1} = pop(@regstack);
209 elsif (/^\@(\w+)$/) {
210 if (exists $names{$1}) {
211 push(@regstack, $names{$1});
214 error("Imager::Expr: unknown storage \@$1");
219 error("Imager::Expr: unknown operator $_");
223 if (@regstack != 1) {
224 error("stack must have only one item at end");
227 if ($regstack[0] !~ /^p/) {
228 error("you must have a color value at the top of the stack at end");
231 push(@ops, [ "ret", $regstack[0], (-1) x $max_opr ]);
233 $self->{"nregs"} = \@nregs;
234 $self->{"cregs"} = \@cregs;
241 for my $op (@{$_[0]->{ops}}) {
242 $result .= "@{$op}\n";
247 # unassembles the compiled code
250 my $code = $self->{"code"};
251 my $attr = \%Imager::Regops::Attr;
252 my @code = unpack("${Imager::Regops::PackCode}*", $code);
253 my %names = map { $attr->{$_}{opcode}, $_ } keys %Imager::Regops::Attr;
254 my @vars = $self->_variables();
257 while (my @op = splice(@code, 0, 2+$Imager::Regops::MaxOperands)) {
258 my $opcode = shift @op;
259 my $name = $names{$opcode};
261 $result .= "j$index: $name($opcode)";
262 my @types = split //, $attr->{$name}{types};
263 for my $parm (@types) {
265 $result .= " $parm$reg";
268 $result.= "($vars[$reg])";
270 elsif (defined $self->{"nregs"}[$reg]) {
271 $result .= "($self->{\"nregs\"}[$reg])";
276 $result .= " -> $attr->{$name}{result}$op[-1]"
277 if $attr->{$name}{result};
281 $result .= "unknown($opcode) @op\n";
289 package Imager::Expr::Postfix;
291 @ISA = qw(Imager::Expr);
293 Imager::Expr::Postfix->register_type('rpnexpr');
295 my %op_names = ( '+'=>'add', '-'=>'subtract', '*'=>'mult', '/' => 'div',
296 '%'=>'mod', '**'=>'pow' );
299 my ($self, $expr, $opts) = @_;
301 my @st_ops = split ' ', $expr;
304 $_ = $op_names{$_} if exists $op_names{$_};
305 $_ = $self->{constants}{$_} if exists $self->{constants}{$_};
307 return $self->stack_to_reg(@st_ops);
310 package Imager::Expr::Infix;
313 @ISA = qw(Imager::Expr);
314 use Imager::Regops qw(%Attr $MaxOperands);
317 eval "use Parse::RecDescent;";
318 __PACKAGE__->register_type('expr') if !$@;
320 # I really prefer bottom-up parsers
321 my $grammar = <<'GRAMMAR';
323 code : assigns 'return' expr
324 { $return = [ @item[1,3] ] }
326 assigns : assign(s?) { $return = [ @{$item[1]} ] }
328 assign : identifier '=' expr ';'
329 { $return = [ @item[1,3] ] }
333 relation : addition (relstuff)(s?)
336 for my $op(@{$item[2]}) { $return = [ $op->[0], $return, $op->[1] ] }
339 relstuff : relop addition { $return = [ @item[1,2] ] }
341 relop : '<=' { $return = 'le' }
342 | '<' { $return = 'lt' }
343 | '==' { $return = 'eq' }
344 | '>=' { $return = 'ge' }
345 | '>' { $return = 'gt' }
346 | '!=' { $return = 'ne' }
348 addition : multiply (addstuff)(s?)
351 # for my $op(@{$item[2]}) { $return .= " @{$op}[1,0]"; }
352 for my $op(@{$item[2]}) { $return = [ $op->[0], $return, $op->[1] ] }
354 addstuff : addop multiply { $return = [ @item[1,2] ] }
355 addop : '+' { $return = 'add' }
356 | '-' { $return = 'subtract' }
358 multiply : power mulstuff(s?)
359 { $return = $item[1];
360 # for my $op(@{$item[2]}) { $return .= " @{$op}[1,0]"; }
361 for my $op(@{$item[2]}) { $return = [ $op->[0], $return, $op->[1] ] }
364 mulstuff : mulop power { $return = [ @item[1,2] ] }
365 mulop : '*' { $return = 'mult' }
366 | '/' { $return = 'div' }
367 | '%' { $return = 'mod' }
369 power : powstuff(s?) atom
372 for my $op(reverse @{$item[1]}) { $return = [ @{$op}[1,0], $return ] }
375 powstuff : atom powop { $return = [ @item[1,2] ] }
376 powop : '**' { $return = 'pow' }
378 atom: '(' expr ')' { $return = $item[2] }
379 | '-' atom { $return = [ uminus=>$item[2] ] }
384 number : /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/
386 exprlist : expr ',' exprlist { $return = [ $item[1], @{$item[3]} ] }
387 | expr { $return = [ $item[1] ] }
389 funccall : identifier '(' exprlist ')'
390 { $return = [ $item[1], @{$item[3]} ] }
392 identifier : /[^\W\d]\w*/ { $return = $item[1] }
400 $parser = Parse::RecDescent->new($grammar);
405 my ($self, $expr, $opts) = @_;
407 $parser = Parse::RecDescent->new($grammar);
409 my $optree = $parser->code($expr);
411 $self->error("Error in $expr\n");
415 @{$self->{inputs}}{$self->_variables} = ();
416 $self->{varregs} = {};
417 @{$self->{varregs}}{$self->_variables} = map { "r$_" } 0..$self->_variables-1;
418 $self->{"nregs"} = [ (undef) x $self->_variables ];
419 $self->{"cregs"} = [];
420 $self->{"lits"} = {};
423 # generate code for the assignments
424 for my $assign (@{$optree->[0]}) {
425 my ($varname, $tree) = @$assign;
426 if (exists $self->{inputs}{$varname}) {
427 $self->error("$varname is an input - you can't assign to it");
430 $self->{varregs}{$varname} = $self->gencode($tree);
433 # generate the final result
434 my $result = $self->gencode($optree->[1]);
435 if ($result !~ /^p\d+$/) {
436 $self->error("You must return a colour value");
439 push(@{$self->{genops}}, [ 'ret', $result, (0) x $MaxOperands ])
446 return $self->{genops};
450 my ($self, $tree) = @_;
453 my ($op, @parms) = @$tree;
455 if (!exists $Attr{$op}) {
456 die "Unknown operator or function $op";
459 for my $subtree (@parms) {
460 $subtree = $self->gencode($subtree);
462 my $types = join("", map {substr($_,0,1)} @parms);
464 if (length($types) < length($Attr{$op}{types})) {
465 die "Too few parameters in call to $op";
467 if ($types ne $Attr{$op}{types}) {
468 # some alternate operators have the same name followed by p
470 if (exists $Attr{$opp} &&
471 $types eq $Attr{$opp}{types}) {
475 die "Call to $_ with incorrect types";
479 if ($Attr{$op}{result} eq 'r') {
480 $result = "r".@{$self->{nregs}};
481 push(@{$self->{nregs}}, undef);
484 $result = "p".@{$self->{cregs}};
485 push(@{$self->{cregs}}, undef);
487 push(@parms, "0") while @parms < $MaxOperands;
488 push(@{$self->{genops}}, [ $op, @parms, $result]);
491 elsif (exists $self->{varregs}{$tree}) {
492 return $self->{varregs}{$tree};
494 elsif ($tree =~ /^$numre$/ || exists $self->{constants}{$tree}) {
495 $tree = $self->{constants}{$tree} if exists $self->{constants}{$tree};
497 if (exists $self->{lits}{$tree}) {
498 return $self->{lits}{$tree};
500 my $reg = "r".@{$self->{nregs}};
501 push(@{$self->{nregs}}, $tree);
502 $self->{lits}{$tree} = $reg;
514 Imager::Expr - implements expression parsing and compilation for the
515 expression evaluation engine used by Imager::transform2()
519 my $code = Imager::Expr->new({rpnexpr=>$someexpr})
520 or die "Cannot compile $someexpr: ",Imager::Expr::error();
524 This module is used internally by the Imager::transform2() function.
525 You shouldn't have much need to use it directly, but you may want to
528 To create a new Imager::Expr object, call:
531 my $expr = Imager::Expr->new(\%options)
532 or die Imager::Expr::error();
534 You will need to set an expression value and you may set any of the
541 A hashref defining extra constants for expression parsing. The names
542 of the constants must be valid identifiers (/[^\W\d]\w*/) and the
543 values must be valid numeric constants (that Perl recognizes in
546 Imager::Expr may define it's own constants (currently just pi.)
550 A reference to an array of variable names. These are allocated
551 numeric registers starting from register zero.
555 By default you can define a 'rpnexpr' key (which emulates RPN) or
556 'expr' (an infix expression). It's also possible to write other
557 expression parsers that will use other keys. Only one expression key
560 =head2 Instance methods
562 The Imager::Expr::error() method is used to retrieve the error if the
563 expression object cannot be created.
567 Imager::Expr provides only a few simple methods meant for external use:
573 Returns the compiled code.
577 Returns a reference to the array of numeric registers.
581 Returns a reference to the array of colour registers.
583 =item $expr->dumpops()
585 Returns a string with the generated VM "machine code".
587 =item $expr->dumpcode()
589 Returns a string with the unassembled VM "machine code".
593 =head2 Creating a new parser
595 I'll write this one day.
597 Methods used by parsers:
601 =item @vars = $self->_variables()
603 A list (not a reference) of the input variables. This should be used
604 to allocate as many registers as there are variable as input
607 =item $self->error($message)
609 Set the return value of Imager::Expr::error()
611 =item @ops = $self->stack_to_reg(@stack_ops)
613 Converts marginally parsed RPN to register code.
617 =head2 Future compatibility
619 Try to avoid doing your own optimization beyond literal folding - if
620 we add some sort of jump, the existing optimizer will need to be
621 rewritten, and any optimization you perform may well be broken too
622 (well, your code generation will probably be broken anyway <sigh>).