| 1 | package Imager::Expr; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Imager::Regops; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | our $VERSION = "1.009"; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | my %expr_types; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | my $error; | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 0 | ▶ | sub error { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | shift if UNIVERSAL::isa($_[0], 'Imager::Expr'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 0 | $error = "@_"; | |||||||||||||||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||||||||||||||||
| 17 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | return $error; | |||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | # what else? | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | my %default_constants = | |||||||||||||||||||||||||||||||||||||||||
| 24 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # too many digits, better than too few | ||||||||||||||||||||||||||||||||||||||||||
| 26 | pi=>3.14159265358979323846264338327950288419716939937510582097494 | ||||||||||||||||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 31 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 31 | my ($class, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # possibly this is a very bad idea | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 31 | my ($type) = grep exists $expr_types{$_}, keys %$opts; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 31 | ▶ | die "Imager::Expr: No known expression type" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | if !defined $type; | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 31 | my $self = bless {}, $expr_types{$type}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 31 | $self->{variables} = [ @{$opts->{variables}} ]; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 31 | ▶ | $self->{constants} = { %default_constants, %{$opts->{constants} || {}} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$opts->{'constants'} || {}
MC/DC0%0/1
$opts->{'constants'} || {}
$opts->{'constants'}
Truth table50%1/2
A
A= $opts->{'constants'}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 31 | ▶ | $self->{ops} = $self->compile($opts->{$type}, $opts) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 31 | ▶ | $self->optimize() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 31 | ▶ | $self->{code} = $self->assemble() | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | or return; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 31 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 16 | ▶ | sub register_type { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
register_type: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 16 | my ($pack, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 16 | $expr_types{$name} = $pack; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | sub type_registered { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type_registered: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | my ($class, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | $expr_types{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 37 | ▶ | sub _variables { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_variables: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 37 | return @{$_[0]->{variables}}; | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 28 | ▶ | sub code { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 28 | return $_[0]->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 28 | ▶ | sub nregs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nregs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 28 | return $_[0]->{nregs}; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 28 | ▶ | sub cregs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cregs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 28 | return $_[0]->{cregs}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | my $numre = '[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?'; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | sub numre { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
numre: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | $numre; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | # optimize the code | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 31 | ▶ | sub optimize { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
optimize: called CC=12 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 31 | my @ops = @{$self->{ops}}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | # this function cannot current handle code with jumps | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 31 | ▶ | return 1 if grep $_->[0] =~ /^jump/, @ops; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | # optimization - common sub-expression elimination | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # it's possible to fold this into the code generation - but it will wait | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 30 | my $max_opr = $Imager::Regops::MaxOperands; | |||||||||||||||||||||||||||||||||||||||||
| 94 | 30 | my $attr = \%Imager::Regops::Attr; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 30 | my $foundops = 1; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 30 | ▶ | while ($foundops) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 30 | $foundops = 0; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 30 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 99 | my $index; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 30 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 30 | ▶ | while (@ops) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 188 | my $op = shift @ops; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 188 | my $desc = join(",", @{$op}[0..$max_opr]); | |||||||||||||||||||||||||||||||||||||||||
| 104 | 188 | ▶ | if ($seen{$desc}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | push(@out, @ops); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $old = $op->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $new = $seen{$desc}; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | for $op (@out) { | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | for my $reg (@{$op}[1..$max_opr]) { | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $reg = $new if $reg eq $old; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | $foundops=1; | |||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 188 | $seen{$desc} = $op->[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 117 | 188 | push(@out, $op); | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 30 | @ops = @out; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | # strength reduction | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 30 | for my $op (@ops) { | |||||||||||||||||||||||||||||||||||||||||
| 123 | # reduce division by a constant to multiplication by a constant | ||||||||||||||||||||||||||||||||||||||||||
| 124 | 188 | ▶ | if ($op->[0] eq 'div' && $op->[2] =~ /^r(\d+)/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$op->[0] eq "div" and $op->[2] =~ /^r(\d+)/ and defined $self->{"nregs"}[$1]
$op->[0] eq "div" and $op->[2] =~ /^r(\d+)/
MC/DC66%2/3
$op->[0] eq "div" and $op->[2] =~ /^r(\d+)/ and defined $self->{"nregs"}[$1]
$op->[0] eq "div"
$op->[2] =~ /^r(\d+)/
defined $self->{"nregs"}[$1]
Truth table75%3/4
A and B and C
A= $op->[0] eq "div"B= $op->[2] =~ /^r(\d+)/C= defined $self->{"nregs"}[$1]
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | && defined($self->{"nregs"}[$1])) { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 12 | my $newreg = @{$self->{"nregs"}}; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 12 | push(@{$self->{"nregs"}}, 1.0/$self->{"nregs"}[$1]); | |||||||||||||||||||||||||||||||||||||||||
| 128 | 12 | $op->[0] = 'mult'; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 12 | $op->[2] = 'r'.$newreg; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 30 | $self->{ops} = \@ops; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 30 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 31 | ▶ | sub assemble { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
assemble: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 31 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 138 | 31 | my $attr = \%Imager::Regops::Attr; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 31 | my $max_opr = $Imager::Regops::MaxOperands; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 31 | my @ops = @{$self->{ops}}; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 31 | for my $op (@ops) { | |||||||||||||||||||||||||||||||||||||||||
| 142 | 194 | $op->[0] = $attr->{$op->[0]}{opcode}; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 194 | for (@{$op}[1..$max_opr+1]) { s/^[rpj]// } | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 31 | my $pack = $Imager::Regops::PackCode x (2+$Imager::Regops::MaxOperands); | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 31 | return join("", ,map { pack($pack, @$_, ) } @ops); | |||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | # converts stack code to register code | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 29 | ▶ | sub stack_to_reg { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stack_to_reg: called CC=19 SCAR=32.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 29 | my ($self, @st_ops) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 29 | my @regstack; | |||||||||||||||||||||||||||||||||||||||||
| 154 | my %nregs; | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 29 | my @vars = $self->_variables(); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 29 | my @nregs = (0) x scalar(@vars); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 29 | my @cregs; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 29 | my $attr = \%Imager::Regops::Attr; | |||||||||||||||||||||||||||||||||||||||||
| 159 | 29 | my %vars; | |||||||||||||||||||||||||||||||||||||||||
| 160 | my %names; | ||||||||||||||||||||||||||||||||||||||||||
| 161 | 29 | my $max_opr = $Imager::Regops::MaxOperands; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 29 | @vars{@vars} = map { "r$_" } 0..$#vars; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 29 | my @ops; | |||||||||||||||||||||||||||||||||||||||||
| 165 | 29 | for (@st_ops) { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 362 | ▶ | if (/^$numre$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | # combining constants makes the optimization below work | ||||||||||||||||||||||||||||||||||||||||||
| 168 | 146 | ▶ | if (exists $nregs{$_}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 169 | 64 | push(@regstack, $nregs{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 82 | $nregs{$_} = "r".@nregs; | |||||||||||||||||||||||||||||||||||||||||
| 173 | 82 | push(@regstack,"r".@nregs); | |||||||||||||||||||||||||||||||||||||||||
| 174 | 82 | push(@nregs, $_); | |||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | elsif (exists $vars{$_}) { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | 20 | push(@regstack, $vars{$_}); | |||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||
| 180 | ▶ | elsif (exists $attr->{$_} && length $attr->{$_}{types}) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
exists $attr->{$_} and length $attr->{$_}{"types"}
MC/DC50%1/2
exists $attr->{$_} and length $attr->{$_}{"types"}
exists $attr->{$_}
length $attr->{$_}{"types"}
Truth table66%2/3
A and B
A= exists $attr->{$_}B= length $attr->{$_}{"types"}
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | 154 | ▶ | if (@regstack < $attr->{$_}{parms}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | error("Imager::transform2: stack underflow on $_"); | |||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 154 | my @parms = splice(@regstack, -$attr->{$_}{parms}); | |||||||||||||||||||||||||||||||||||||||||
| 186 | 154 | my $types = join("", map {substr($_,0,1)} @parms); | |||||||||||||||||||||||||||||||||||||||||
| 187 | 154 | ▶ | if ($types ne $attr->{$_}{types}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | 6 | ▶ | if (exists $attr->{$_.'p'} && $types eq $attr->{$_.'p'}{types}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $$attr{$_ . 'p'} and $types eq $$attr{$_ . 'p'}{"types"}
MC/DC0%0/2
exists $$attr{$_ . 'p'} and $types eq $$attr{$_ . 'p'}{"types"}
exists $$attr{$_ . 'p'}
$types eq $$attr{$_ . 'p'}{"types"}
Truth table33%1/3
A and B
A= exists $$attr{$_ . 'p'}B= $types eq $$attr{$_ . 'p'}{"types"}
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 6 | $_ .= 'p'; | |||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||
| 191 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | error("Imager::transform2: Call to $_ with incorrect types"); | |||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 154 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 197 | 154 | ▶ | if ($attr->{$_}{result} eq 'r') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 104 | $result = "r".@nregs; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 104 | push(@nregs, undef); | |||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 50 | $result = "p".@cregs; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 50 | push(@cregs, -1); | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 154 | push(@regstack, $result); | |||||||||||||||||||||||||||||||||||||||||
| 206 | 154 | ▶ | push(@parms, "0") while @parms < $max_opr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 154 | push(@ops, [ $_, @parms, $result ]); | |||||||||||||||||||||||||||||||||||||||||
| 208 | #print "$result <- $_ @parms\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||||||||
| 210 | elsif (/^!(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 211 | 13 | ▶ | if (!@regstack) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | error("Imager::transform2: stack underflow with $_"); | |||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 13 | $names{$1} = pop(@regstack); | |||||||||||||||||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||||||||||||||||
| 217 | elsif (/^\@(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 29 | ▶ | if (exists $names{$1}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 29 | push(@regstack, $names{$1}); | |||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | error("Imager::Expr: unknown storage \@$1"); | |||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | error("Imager::Expr: unknown operator $_"); | |||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 29 | ▶ | if (@regstack != 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | error("stack must have only one item at end"); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 29 | ▶ | if ($regstack[0] !~ /^p/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | error("you must have a color value at the top of the stack at end"); | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 29 | push(@ops, [ "ret", $regstack[0], (-1) x $max_opr ]); | |||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | 29 | $self->{"nregs"} = \@nregs; | |||||||||||||||||||||||||||||||||||||||||
| 242 | 29 | $self->{"cregs"} = \@cregs; | |||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||
| 244 | 29 | return \@ops; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | sub dumpops { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dumpops: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | for my $op (@{$_[0]->{ops}}) { | |||||||||||||||||||||||||||||||||||||||||
| 250 | 0 | $result .= "@{$op}\n"; | |||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | $result; | |||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | # unassembles the compiled code | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | ▶ | sub dumpcode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dumpcode: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 3 | my $code = $self->{"code"}; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 3 | my $attr = \%Imager::Regops::Attr; | |||||||||||||||||||||||||||||||||||||||||
| 260 | 3 | my @code = unpack("${Imager::Regops::PackCode}*", $code); | |||||||||||||||||||||||||||||||||||||||||
| 261 | 3 | my %names = map { $attr->{$_}{opcode}, $_ } keys %Imager::Regops::Attr; | |||||||||||||||||||||||||||||||||||||||||
| 262 | 3 | my @vars = $self->_variables(); | |||||||||||||||||||||||||||||||||||||||||
| 263 | 3 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 3 | my $index = 0; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | ▶ | while (my @op = splice(@code, 0, 2+$Imager::Regops::MaxOperands)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 15 | my $opcode = shift @op; | |||||||||||||||||||||||||||||||||||||||||
| 267 | 15 | my $name = $names{$opcode}; | |||||||||||||||||||||||||||||||||||||||||
| 268 | 15 | ▶ | if ($name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 269 | 15 | $result .= "j$index: $name($opcode)"; | |||||||||||||||||||||||||||||||||||||||||
| 270 | 15 | my @types = split //, $attr->{$name}{types}; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 15 | for my $parm (@types) { | |||||||||||||||||||||||||||||||||||||||||
| 272 | 26 | my $reg = shift @op; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 26 | $result .= " $parm$reg"; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 26 | ▶ | if ($parm eq 'r') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 23 | ▶ | if ($reg < @vars) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 6 | $result.= "($vars[$reg])"; | |||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||
| 278 | elsif (defined $self->{"nregs"}[$reg]) { | ||||||||||||||||||||||||||||||||||||||||||
| 279 | 9 | $result .= "($self->{\"nregs\"}[$reg])"; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | $result .= " -> $attr->{$name}{result}$op[-1]" | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 15 | ▶ | if $attr->{$name}{result}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 15 | $result .= "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | $result .= "unknown($opcode) @op\n"; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | 15 | ++$index; | |||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | 3 | $result; | |||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | package Imager::Expr::Postfix; | ||||||||||||||||||||||||||||||||||||||||||
| 298 | 6 | our @ISA = qw(Imager::Expr); | |||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | 6 | Imager::Expr::Postfix->register_type('rpnexpr'); | |||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 6 | my %op_names = ( '+'=>'add', '-'=>'subtract', '*'=>'mult', '/' => 'div', | |||||||||||||||||||||||||||||||||||||||||
| 303 | '%'=>'mod', '**'=>'pow' ); | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 29 | ▶ | sub compile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | 29 | my ($self, $expr, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | 29 | $expr =~ s/#.*//; # remove comments | |||||||||||||||||||||||||||||||||||||||||
| 309 | 29 | my @st_ops = split ' ', $expr; | |||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | 29 | for (@st_ops) { | |||||||||||||||||||||||||||||||||||||||||
| 312 | 362 | ▶ | $_ = $op_names{$_} if exists $op_names{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 313 | 362 | ▶ | $_ = $self->{constants}{$_} if exists $self->{constants}{$_}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||
| 315 | 29 | return $self->stack_to_reg(@st_ops); | |||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | package Imager::Expr::Infix; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 6 | our @ISA = qw(Imager::Expr); | |||||||||||||||||||||||||||||||||||||||||
| 321 | 6 | ▶ | use Imager::Regops qw(%Attr $MaxOperands); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | { | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 6 | local @INC = @INC; | |||||||||||||||||||||||||||||||||||||||||
| 325 | 6 | ▶ | pop @INC if $INC[-1] eq '.'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | 6 | ▶ | eval "use Parse::RecDescent;"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 327 | 6 | ▶ | __PACKAGE__->register_type('expr') if !$@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | # I really prefer bottom-up parsers | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 6 | my $grammar = <<'GRAMMAR'; | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | code : assigns 'return' expr | ||||||||||||||||||||||||||||||||||||||||||
| 334 | { $return = [ @item[1,3] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | assigns : assign(s?) { $return = [ @{$item[1]} ] } | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | assign : identifier '=' expr ';' | ||||||||||||||||||||||||||||||||||||||||||
| 339 | { $return = [ @item[1,3] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | expr : relation | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | relation : addition (relstuff)(s?) | ||||||||||||||||||||||||||||||||||||||||||
| 344 | { | ||||||||||||||||||||||||||||||||||||||||||
| 345 | $return = $item[1]; | ||||||||||||||||||||||||||||||||||||||||||
| 346 | for my $op(@{$item[2]}) { $return = [ $op->[0], $return, $op->[1] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | relstuff : relop addition { $return = [ @item[1,2] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||
| 352 | relop : '<=' { $return = 'le' } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | | '<' { $return = 'lt' } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | | '==' { $return = 'eq' } | ||||||||||||||||||||||||||||||||||||||||||
| 355 | | '>=' { $return = 'ge' } | ||||||||||||||||||||||||||||||||||||||||||
| 356 | | '>' { $return = 'gt' } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | | '!=' { $return = 'ne' } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | addition : multiply (addstuff)(s?) | ||||||||||||||||||||||||||||||||||||||||||
| 360 | { | ||||||||||||||||||||||||||||||||||||||||||
| 361 | $return = $item[1]; | ||||||||||||||||||||||||||||||||||||||||||
| 362 | # for my $op(@{$item[2]}) { $return .= " @{$op}[1,0]"; } | ||||||||||||||||||||||||||||||||||||||||||
| 363 | for my $op(@{$item[2]}) { $return = [ $op->[0], $return, $op->[1] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||||||||||||||||
| 366 | addstuff : addop multiply { $return = [ @item[1,2] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | addop : '+' { $return = 'add' } | ||||||||||||||||||||||||||||||||||||||||||
| 368 | | '-' { $return = 'subtract' } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | multiply : power mulstuff(s?) | ||||||||||||||||||||||||||||||||||||||||||
| 371 | { $return = $item[1]; | ||||||||||||||||||||||||||||||||||||||||||
| 372 | # for my $op(@{$item[2]}) { $return .= " @{$op}[1,0]"; } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | for my $op(@{$item[2]}) { $return = [ $op->[0], $return, $op->[1] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | mulstuff : mulop power { $return = [ @item[1,2] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | mulop : '*' { $return = 'mult' } | ||||||||||||||||||||||||||||||||||||||||||
| 379 | | '/' { $return = 'div' } | ||||||||||||||||||||||||||||||||||||||||||
| 380 | | '%' { $return = 'mod' } | ||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | power : powstuff(s?) atom | ||||||||||||||||||||||||||||||||||||||||||
| 383 | { | ||||||||||||||||||||||||||||||||||||||||||
| 384 | $return = $item[2]; | ||||||||||||||||||||||||||||||||||||||||||
| 385 | for my $op(reverse @{$item[1]}) { $return = [ @{$op}[1,0], $return ] } | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | | atom | ||||||||||||||||||||||||||||||||||||||||||
| 389 | powstuff : atom powop { $return = [ @item[1,2] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | powop : '**' { $return = 'pow' } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | atom: '(' expr ')' { $return = $item[2] } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | | '-' atom { $return = [ uminus=>$item[2] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 394 | | number | ||||||||||||||||||||||||||||||||||||||||||
| 395 | | funccall | ||||||||||||||||||||||||||||||||||||||||||
| 396 | | identifier | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | number : /[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?/ | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | exprlist : expr ',' exprlist { $return = [ $item[1], @{$item[3]} ] } | ||||||||||||||||||||||||||||||||||||||||||
| 401 | | expr { $return = [ $item[1] ] } | ||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | funccall : identifier '(' exprlist ')' | ||||||||||||||||||||||||||||||||||||||||||
| 404 | { $return = [ $item[1], @{$item[3]} ] } | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | identifier : /[^\W\d]\w*/ { $return = $item[1] } | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | GRAMMAR | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | 6 | my $parser; | |||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | sub init_parser { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init_parser: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | if (!$parser) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | $parser = Parse::RecDescent->new($grammar); | |||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | 1 | ▶ | sub compile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=7 SCAR=21.1
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | 1 | my ($self, $expr, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | ▶ | if (!$parser) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | $parser = Parse::RecDescent->new($grammar); | |||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 1 | my $optree = $parser->code($expr); | |||||||||||||||||||||||||||||||||||||||||
| 424 | 1 | ▶ | if (!$optree) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 425 | 0 | $self->error("Error in $expr\n"); | |||||||||||||||||||||||||||||||||||||||||
| 426 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 427 | } | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 1 | @{$self->{inputs}}{$self->_variables} = (); | |||||||||||||||||||||||||||||||||||||||||
| 430 | 1 | $self->{varregs} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | @{$self->{varregs}}{$self->_variables} = map { "r$_" } 0..$self->_variables-1; | |||||||||||||||||||||||||||||||||||||||||
| 432 | 1 | $self->{"nregs"} = [ (undef) x $self->_variables ]; | |||||||||||||||||||||||||||||||||||||||||
| 433 | 1 | $self->{"cregs"} = []; | |||||||||||||||||||||||||||||||||||||||||
| 434 | 1 | $self->{"lits"} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 437 | # generate code for the assignments | ||||||||||||||||||||||||||||||||||||||||||
| 438 | 1 | for my $assign (@{$optree->[0]}) { | |||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | my ($varname, $tree) = @$assign; | |||||||||||||||||||||||||||||||||||||||||
| 440 | 1 | ▶ | if (exists $self->{inputs}{$varname}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 441 | 0 | $self->error("$varname is an input - you can't assign to it"); | |||||||||||||||||||||||||||||||||||||||||
| 442 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||
| 444 | 1 | $self->{varregs}{$varname} = $self->gencode($tree); | |||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | # generate the final result | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 1 | my $result = $self->gencode($optree->[1]); | |||||||||||||||||||||||||||||||||||||||||
| 449 | 1 | ▶ | if ($result !~ /^p\d+$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 0 | $self->error("You must return a color value"); | |||||||||||||||||||||||||||||||||||||||||
| 451 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||
| 453 | 1 | push(@{$self->{genops}}, [ 'ret', $result, (0) x $MaxOperands ]) | |||||||||||||||||||||||||||||||||||||||||
| 454 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 455 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 456 | 0 | $self->error($@); | |||||||||||||||||||||||||||||||||||||||||
| 457 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | 1 | return $self->{genops}; | |||||||||||||||||||||||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | 11 | ▶ | sub gencode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
gencode: called CC=15 SCAR=28.9
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 11 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | 11 | ▶ | if (ref $tree) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 4 | my ($op, @parms) = @$tree; | |||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | 4 | ▶ | if (!exists $Attr{$op}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | die "Unknown operator or function $op"; | |||||||||||||||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||
| 473 | 4 | for my $subtree (@parms) { | |||||||||||||||||||||||||||||||||||||||||
| 474 | 9 | $subtree = $self->gencode($subtree); | |||||||||||||||||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||||||||||||||||
| 476 | 4 | my $types = join("", map {substr($_,0,1)} @parms); | |||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | 4 | ▶ | if (length($types) < length($Attr{$op}{types})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | die "Too few parameters in call to $op"; | |||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||
| 481 | 4 | ▶ | if ($types ne $Attr{$op}{types}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | # some alternate operators have the same name followed by p | ||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | my $opp = $op."p"; | |||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | ▶ | if (exists $Attr{$opp} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $Imager::Expr::Infix::Attr{$opp} and $types eq $Imager::Expr::Infix::Attr{$opp}{"types"}
MC/DC0%0/2
exists $Imager::Expr::Infix::Attr{$opp} and $types eq $Imager::Expr::Infix::Attr{$opp}{"types"}
exists $Imager::Expr::Infix::Attr{$opp}
$types eq $Imager::Expr::Infix::Attr{$opp}{"types"}
Truth table0%0/3
A and B
A= exists $Imager::Expr::Infix::Attr{$opp}B= $types eq $Imager::Expr::Infix::Attr{$opp}{"types"}
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | $types eq $Attr{$opp}{types}) { | ||||||||||||||||||||||||||||||||||||||||||
| 486 | 0 | $op = $opp; | |||||||||||||||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||||||||||||||||
| 488 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | die "Call to $_ with incorrect types"; | |||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 4 | my $result; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 4 | ▶ | if ($Attr{$op}{result} eq 'r') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 3 | $result = "r".@{$self->{nregs}}; | |||||||||||||||||||||||||||||||||||||||||
| 495 | 3 | push(@{$self->{nregs}}, undef); | |||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 498 | 1 | $result = "p".@{$self->{cregs}}; | |||||||||||||||||||||||||||||||||||||||||
| 499 | 1 | push(@{$self->{cregs}}, undef); | |||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 4 | ▶ | push(@parms, "0") while @parms < $MaxOperands; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | 4 | push(@{$self->{genops}}, [ $op, @parms, $result]); | |||||||||||||||||||||||||||||||||||||||||
| 503 | 4 | return $result; | |||||||||||||||||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||||||||||||||||
| 505 | elsif (exists $self->{varregs}{$tree}) { | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 3 | return $self->{varregs}{$tree}; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | ▶ | elsif ($tree =~ /^$numre$/ || exists $self->{constants}{$tree}) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$tree =~ /^$numre$/ or exists $self->{"constants"}{$tree}
MC/DC0%0/2
$tree =~ /^$numre$/ or exists $self->{"constants"}{$tree}
$tree =~ /^$numre$/
exists $self->{"constants"}{$tree}
Truth table66%2/3
A or B
A= $tree =~ /^$numre$/B= exists $self->{"constants"}{$tree}
| |||||||||||||||||||||||||||||||||||||||||||
| 509 | 4 | ▶ | $tree = $self->{constants}{$tree} if exists $self->{constants}{$tree}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 4 | ▶ | if (exists $self->{lits}{$tree}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 1 | return $self->{lits}{$tree}; | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | 3 | my $reg = "r".@{$self->{nregs}}; | |||||||||||||||||||||||||||||||||||||||||
| 515 | 3 | push(@{$self->{nregs}}, $tree); | |||||||||||||||||||||||||||||||||||||||||
| 516 | 3 | $self->{lits}{$tree} = $reg; | |||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | 3 | return $reg; | |||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||||||||||||||||
| 522 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||
| 524 | __END__ | ||||||||||||||||||||||||||||||||||||||||||