| 1 | package Imager::Expr::Assem; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Imager::Expr; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Imager::Regops; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 4 | our $VERSION = "1.004"; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 4 | our @ISA = qw(Imager::Expr); | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | __PACKAGE__->register_type('assem'); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | sub compile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=35 SCAR=42.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | my ($self, $expr, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my %nregs; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | my @vars = $self->_variables(); | |||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | my @nregs = (0) x @vars; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my @cregs; | |||||||||||||||||||||||||||||||||||||||||
| 19 | my %vars; | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | @vars{@vars} = map { "r$_" } 0..$#vars; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my %labels; | |||||||||||||||||||||||||||||||||||||||||
| 22 | my @ops; | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | my @msgs; | |||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | my $attr = \%Imager::Regops::Attr; | |||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | # initially produce [ $linenum, $result, $opcode, @parms ] | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | my $lineno = 0; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | while ($expr =~ s/^([^\n]+)(?:\n|$)//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | ++$lineno; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 10 | my $line = $1; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 10 | $line =~ s/#.*//; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 9 | for my $op (split /;/, $line) { | |||||||||||||||||||||||||||||||||||||||||
| 34 | 10 | ▶ | if (my ($name, $type) = $op =~ /^\s*var\s+([^:]+):(\S+)\s*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | ▶ | if (exists $vars{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | push(@msgs, "$lineno: duplicate variable name '$name'"); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | if ($type eq 'num' || $type eq 'n') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$type eq "num" or $type eq "n"
MC/DC50%1/2
$type eq "num" or $type eq "n"
$type eq "num"
$type eq "n"
Truth table66%2/3
A or B
A= $type eq "num"B= $type eq "n"
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | $vars{$name} = 'r'.@nregs; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | push(@nregs, undef); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | next; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | ▶ | elsif ($type eq 'pixel' || $type eq 'p' || $type eq 'c') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$type eq "pixel" or $type eq "p" or $type eq "c"
$type eq "pixel" or $type eq "p"
MC/DC0%0/3
$type eq "pixel" or $type eq "p" or $type eq "c"
$type eq "pixel"
$type eq "p"
$type eq "c"
Truth table25%1/4
A or B or C
A= $type eq "pixel"B= $type eq "p"C= $type eq "c"
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | $vars{$name} = 'p'.@cregs; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | push(@cregs, undef); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | push(@msgs, "$lineno: unknown variable type $type"); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | # any statement can have a label | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | if ($op =~ s/^\s*(\w+):\s*//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | if ($labels{$1}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | push(@msgs, | |||||||||||||||||||||||||||||||||||||||||
| 56 | "$lineno: duplicate label $1 (previous on $labels{$1}[1])"); | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 1 | $labels{$1} = [ scalar @ops, $lineno ]; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | next if $op =~ /^\s*$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | # jumps have special operand handling | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | if ($op =~ /^\s*jump\s+(\w+)\s*$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | push(@ops, [$lineno, "", "jump", $1]); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | elsif (my ($code, $reg, $targ) = | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ($op =~ /^\s*(jumpz|jumpnz)\s+(\S+)\s+(\S+)\s*$/)) { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | push(@ops, [$lineno, "", $code, $reg, $targ]); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | elsif ($op =~ /^\s*print\s+(\S+)\s*/) { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | push(@ops, [$lineno, "", 'print', $1 ]); | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | elsif ($op =~ /^\s*ret\s+(\S+)\s*/) { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | push(@ops, [$lineno, "", 'ret', $1]); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | elsif ($op =~ s/\s*(\S+)\s*=\s*(\S+)\s*$//) { | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # simple assignment | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | push(@ops, [$lineno, $1, "set", $2]); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | elsif ($op =~ s/\s*(\S+)\s*=\s*(\S+)\s*//) { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | # some normal ops finally | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | my ($result, $opcode) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | unless ($attr->{$opcode}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | push(@msgs, "$lineno: unknown operator $opcode"); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | my @oper; | |||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | ▶ | while ($op =~ s/(\S+)\s*//) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | push(@oper, $1); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | push(@ops, [$lineno, $result, $opcode, @oper]); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | push(@msgs, "$lineno: invalid statement '$op'"); | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my $max_opr = $Imager::Regops::MaxOperands; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | my $numre = $self->numre; | |||||||||||||||||||||||||||||||||||||||||
| 101 | my $trans = | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 13 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | # translate a name/number to a <type><digits> | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 13 | my ($name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 105 | $name = $self->{constants}{$name} | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 13 | ▶ | if exists $self->{constants}{$name}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 13 | ▶ | if ($vars{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | return $vars{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | elsif ($name =~ /^$numre$/) { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | $vars{$name} = 'r'.@nregs; | |||||||||||||||||||||||||||||||||||||||||
| 112 | 3 | push(@nregs, $name); | |||||||||||||||||||||||||||||||||||||||||
| 113 | 3 | return $vars{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | push(@msgs, "$lineno: undefined variable $name"); | |||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 120 | # now to translate symbols and so on | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | OP: for my $op (@ops) { | |||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | $lineno = shift @$op; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | if ($op->[1] eq 'jump') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | unless (exists $labels{$op->[2]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | push(@msgs, "$lineno: unknown label $op->[2]"); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $op = [ 'jump', "j$labels{$op->[2]}[0]", (0) x $max_opr ]; | |||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||
| 130 | elsif ($op->[1] =~ /^jump/) { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | unless (exists $labels{$op->[3]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | push(@msgs, "$lineno: unknown label $op->[2]"); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 1 | $op = [ $op->[1], $trans->($op->[2]), "j$labels{$op->[3]}[0]", | |||||||||||||||||||||||||||||||||||||||||
| 136 | (0) x ($max_opr-1) ]; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | elsif ($op->[1] eq 'print') { | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | $op = [ $op->[1], $trans->($op->[2]), (0) x $max_opr ]; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | elsif ($op->[1] eq 'ret') { | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | $op = [ 'ret', $trans->($op->[2]), (0) x $max_opr ]; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # a normal operator | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | my ($result, $name, @parms) = @$op; | |||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 4 | ▶ | if ($result =~ /^$numre$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | push(@msgs, "$lineno: target of operator cannot be a constant"); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 4 | $result = $trans->($result); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 4 | for my $parm (@parms) { | |||||||||||||||||||||||||||||||||||||||||
| 154 | 7 | $parm = $trans->($parm); | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 4 | push(@parms, (0) x ($max_opr-@parms)); | |||||||||||||||||||||||||||||||||||||||||
| 157 | 4 | $op = [ $op->[1], @parms, $result ]; | |||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | # more validation than a real assembler | ||||||||||||||||||||||||||||||||||||||||||
| 162 | # not trying to solve the halting problem... | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | if (@ops && $ops[-1][0] ne 'ret' && $ops[-1][0] ne 'jump') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
@ops and $ops[-1][0] ne "ret" and $ops[-1][0] ne "jump"
@ops and $ops[-1][0] ne "ret"
MC/DC0%0/3
@ops and $ops[-1][0] ne "ret" and $ops[-1][0] ne "jump"
@ops
$ops[-1][0] ne "ret"
$ops[-1][0] ne "jump"
Truth table25%1/4
A and B and C
A= @opsB= $ops[-1][0] ne "ret"C= $ops[-1][0] ne "jump"
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | push(@msgs, ": the last instruction must be ret or jump"); | |||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | $self->{nregs} = \@nregs; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | $self->{cregs} = \@cregs; | |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | if (@msgs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | $self->error(join("\n", @msgs)); | |||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | return \@ops; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||||||||||||||||