line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Imager::Expr; |
2
|
5
|
|
|
5
|
|
114579
|
use 5.006; |
|
5
|
|
|
|
|
29
|
|
3
|
5
|
|
|
5
|
|
1730
|
use Imager::Regops; |
|
5
|
|
|
|
|
15
|
|
|
5
|
|
|
|
|
609
|
|
4
|
5
|
|
|
5
|
|
60
|
use strict; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
9499
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "1.008"; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
my %expr_types; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $error; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub error { |
13
|
0
|
0
|
|
0
|
1
|
0
|
shift if UNIVERSAL::isa($_[0], 'Imager::Expr'); |
14
|
0
|
0
|
|
|
|
0
|
if (@_) { |
15
|
0
|
|
|
|
|
0
|
$error = "@_"; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
else { |
18
|
0
|
|
|
|
|
0
|
return $error; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# what else? |
23
|
|
|
|
|
|
|
my %default_constants = |
24
|
|
|
|
|
|
|
( |
25
|
|
|
|
|
|
|
# too many digits, better than too few |
26
|
|
|
|
|
|
|
pi=>3.14159265358979323846264338327950288419716939937510582097494 |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub new { |
30
|
31
|
|
|
31
|
1
|
220
|
my ($class, $opts) = @_; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# possibly this is a very bad idea |
33
|
31
|
|
|
|
|
142
|
my ($type) = grep exists $expr_types{$_}, keys %$opts; |
34
|
31
|
50
|
|
|
|
68
|
die "Imager::Expr: No known expression type" |
35
|
|
|
|
|
|
|
if !defined $type; |
36
|
31
|
|
|
|
|
68
|
my $self = bless {}, $expr_types{$type}; |
37
|
31
|
|
|
|
|
37
|
$self->{variables} = [ @{$opts->{variables}} ]; |
|
31
|
|
|
|
|
87
|
|
38
|
31
|
50
|
|
|
|
58
|
$self->{constants} = { %default_constants, %{$opts->{constants} || {}} }; |
|
31
|
|
|
|
|
148
|
|
39
|
31
|
50
|
|
|
|
83
|
$self->{ops} = $self->compile($opts->{$type}, $opts) |
40
|
|
|
|
|
|
|
or return; |
41
|
31
|
50
|
|
|
|
84
|
$self->optimize() |
42
|
|
|
|
|
|
|
or return; |
43
|
31
|
50
|
|
|
|
85
|
$self->{code} = $self->assemble() |
44
|
|
|
|
|
|
|
or return; |
45
|
31
|
|
|
|
|
90
|
$self; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub register_type { |
49
|
13
|
|
|
13
|
1
|
37
|
my ($pack, $name) = @_; |
50
|
13
|
|
|
|
|
81
|
$expr_types{$name} = $pack; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub type_registered { |
54
|
1
|
|
|
1
|
1
|
74
|
my ($class, $name) = @_; |
55
|
|
|
|
|
|
|
|
56
|
1
|
|
|
|
|
4
|
$expr_types{$name}; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _variables { |
60
|
37
|
|
|
37
|
|
46
|
return @{$_[0]->{variables}}; |
|
37
|
|
|
|
|
95
|
|
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub code { |
64
|
28
|
|
|
28
|
1
|
66
|
return $_[0]->{code}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub nregs { |
68
|
28
|
|
|
28
|
1
|
49
|
return $_[0]->{nregs}; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub cregs { |
72
|
28
|
|
|
28
|
1
|
31671
|
return $_[0]->{cregs}; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
my $numre = '[+-]?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?'; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub numre { |
78
|
1
|
|
|
1
|
1
|
4
|
$numre; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# optimize the code |
82
|
|
|
|
|
|
|
sub optimize { |
83
|
31
|
|
|
31
|
1
|
63
|
my ($self) = @_; |
84
|
|
|
|
|
|
|
|
85
|
31
|
|
|
|
|
41
|
my @ops = @{$self->{ops}}; |
|
31
|
|
|
|
|
61
|
|
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# this function cannot current handle code with jumps |
88
|
31
|
100
|
|
|
|
149
|
return 1 if grep $_->[0] =~ /^jump/, @ops; |
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
|
|
|
|
|
45
|
my $max_opr = $Imager::Regops::MaxOperands; |
94
|
30
|
|
|
|
|
39
|
my $attr = \%Imager::Regops::Attr; |
95
|
30
|
|
|
|
|
36
|
my $foundops = 1; |
96
|
30
|
|
|
|
|
48
|
while ($foundops) { |
97
|
30
|
|
|
|
|
34
|
$foundops = 0; |
98
|
30
|
|
|
|
|
55
|
my %seen; |
99
|
|
|
|
|
|
|
my $index; |
100
|
30
|
|
|
|
|
0
|
my @out; |
101
|
30
|
|
|
|
|
49
|
while (@ops) { |
102
|
188
|
|
|
|
|
212
|
my $op = shift @ops; |
103
|
188
|
|
|
|
|
220
|
my $desc = join(",", @{$op}[0..$max_opr]); |
|
188
|
|
|
|
|
319
|
|
104
|
188
|
50
|
|
|
|
305
|
if ($seen{$desc}) { |
105
|
0
|
|
|
|
|
0
|
push(@out, @ops); |
106
|
0
|
|
|
|
|
0
|
my $old = $op->[-1]; |
107
|
0
|
|
|
|
|
0
|
my $new = $seen{$desc}; |
108
|
0
|
|
|
|
|
0
|
for $op (@out) { |
109
|
0
|
|
|
|
|
0
|
for my $reg (@{$op}[1..$max_opr]) { |
|
0
|
|
|
|
|
0
|
|
110
|
0
|
0
|
|
|
|
0
|
$reg = $new if $reg eq $old; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
} |
113
|
0
|
|
|
|
|
0
|
$foundops=1; |
114
|
0
|
|
|
|
|
0
|
last; |
115
|
|
|
|
|
|
|
} |
116
|
188
|
|
|
|
|
308
|
$seen{$desc} = $op->[-1]; |
117
|
188
|
|
|
|
|
307
|
push(@out, $op); |
118
|
|
|
|
|
|
|
} |
119
|
30
|
|
|
|
|
111
|
@ops = @out; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
# strength reduction |
122
|
30
|
|
|
|
|
49
|
for my $op (@ops) { |
123
|
|
|
|
|
|
|
# reduce division by a constant to multiplication by a constant |
124
|
188
|
100
|
66
|
|
|
371
|
if ($op->[0] eq 'div' && $op->[2] =~ /^r(\d+)/ |
|
|
|
100
|
|
|
|
|
125
|
|
|
|
|
|
|
&& defined($self->{"nregs"}[$1])) { |
126
|
12
|
|
|
|
|
16
|
my $newreg = @{$self->{"nregs"}}; |
|
12
|
|
|
|
|
19
|
|
127
|
12
|
|
|
|
|
17
|
push(@{$self->{"nregs"}}, 1.0/$self->{"nregs"}[$1]); |
|
12
|
|
|
|
|
33
|
|
128
|
12
|
|
|
|
|
20
|
$op->[0] = 'mult'; |
129
|
12
|
|
|
|
|
24
|
$op->[2] = 'r'.$newreg; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
} |
132
|
30
|
|
|
|
|
62
|
$self->{ops} = \@ops; |
133
|
30
|
|
|
|
|
81
|
1; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub assemble { |
137
|
31
|
|
|
31
|
1
|
47
|
my ($self) = @_; |
138
|
31
|
|
|
|
|
68
|
my $attr = \%Imager::Regops::Attr; |
139
|
31
|
|
|
|
|
38
|
my $max_opr = $Imager::Regops::MaxOperands; |
140
|
31
|
|
|
|
|
36
|
my @ops = @{$self->{ops}}; |
|
31
|
|
|
|
|
55
|
|
141
|
31
|
|
|
|
|
49
|
for my $op (@ops) { |
142
|
194
|
|
|
|
|
315
|
$op->[0] = $attr->{$op->[0]}{opcode}; |
143
|
194
|
|
|
|
|
236
|
for (@{$op}[1..$max_opr+1]) { s/^[rpj]// } |
|
194
|
|
|
|
|
239
|
|
|
970
|
|
|
|
|
1551
|
|
144
|
|
|
|
|
|
|
} |
145
|
31
|
|
|
|
|
61
|
my $pack = $Imager::Regops::PackCode x (2+$Imager::Regops::MaxOperands); |
146
|
|
|
|
|
|
|
|
147
|
31
|
|
|
|
|
44
|
return join("", ,map { pack($pack, @$_, ) } @ops); |
|
194
|
|
|
|
|
544
|
|
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
# converts stack code to register code |
151
|
|
|
|
|
|
|
sub stack_to_reg { |
152
|
29
|
|
|
29
|
1
|
111
|
my ($self, @st_ops) = @_; |
153
|
29
|
|
|
|
|
39
|
my @regstack; |
154
|
|
|
|
|
|
|
my %nregs; |
155
|
29
|
|
|
|
|
49
|
my @vars = $self->_variables(); |
156
|
29
|
|
|
|
|
60
|
my @nregs = (0) x scalar(@vars); |
157
|
29
|
|
|
|
|
33
|
my @cregs; |
158
|
29
|
|
|
|
|
49
|
my $attr = \%Imager::Regops::Attr; |
159
|
29
|
|
|
|
|
34
|
my %vars; |
160
|
|
|
|
|
|
|
my %names; |
161
|
29
|
|
|
|
|
33
|
my $max_opr = $Imager::Regops::MaxOperands; |
162
|
29
|
|
|
|
|
54
|
@vars{@vars} = map { "r$_" } 0..$#vars; |
|
58
|
|
|
|
|
199
|
|
163
|
|
|
|
|
|
|
|
164
|
29
|
|
|
|
|
50
|
my @ops; |
165
|
29
|
|
|
|
|
41
|
for (@st_ops) { |
166
|
362
|
100
|
66
|
|
|
1834
|
if (/^$numre$/) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# combining constants makes the optimization below work |
168
|
146
|
100
|
|
|
|
239
|
if (exists $nregs{$_}) { |
169
|
64
|
|
|
|
|
124
|
push(@regstack, $nregs{$_}); |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
else { |
172
|
82
|
|
|
|
|
199
|
$nregs{$_} = "r".@nregs; |
173
|
82
|
|
|
|
|
161
|
push(@regstack,"r".@nregs); |
174
|
82
|
|
|
|
|
142
|
push(@nregs, $_); |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
elsif (exists $vars{$_}) { |
178
|
20
|
|
|
|
|
43
|
push(@regstack, $vars{$_}); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
elsif (exists $attr->{$_} && length $attr->{$_}{types}) { |
181
|
154
|
50
|
|
|
|
283
|
if (@regstack < $attr->{$_}{parms}) { |
182
|
0
|
|
|
|
|
0
|
error("Imager::transform2: stack underflow on $_"); |
183
|
0
|
|
|
|
|
0
|
return; |
184
|
|
|
|
|
|
|
} |
185
|
154
|
|
|
|
|
262
|
my @parms = splice(@regstack, -$attr->{$_}{parms}); |
186
|
154
|
|
|
|
|
197
|
my $types = join("", map {substr($_,0,1)} @parms); |
|
307
|
|
|
|
|
554
|
|
187
|
154
|
100
|
|
|
|
293
|
if ($types ne $attr->{$_}{types}) { |
188
|
6
|
50
|
33
|
|
|
33
|
if (exists $attr->{$_.'p'} && $types eq $attr->{$_.'p'}{types}) { |
189
|
6
|
|
|
|
|
8
|
$_ .= 'p'; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
else { |
192
|
0
|
|
|
|
|
0
|
error("Imager::transform2: Call to $_ with incorrect types"); |
193
|
0
|
|
|
|
|
0
|
return; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
154
|
|
|
|
|
148
|
my $result; |
197
|
154
|
100
|
|
|
|
245
|
if ($attr->{$_}{result} eq 'r') { |
198
|
104
|
|
|
|
|
136
|
$result = "r".@nregs; |
199
|
104
|
|
|
|
|
156
|
push(@nregs, undef); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
else { |
202
|
50
|
|
|
|
|
81
|
$result = "p".@cregs; |
203
|
50
|
|
|
|
|
69
|
push(@cregs, -1); |
204
|
|
|
|
|
|
|
} |
205
|
154
|
|
|
|
|
188
|
push(@regstack, $result); |
206
|
154
|
|
|
|
|
583
|
push(@parms, "0") while @parms < $max_opr; |
207
|
154
|
|
|
|
|
495
|
push(@ops, [ $_, @parms, $result ]); |
208
|
|
|
|
|
|
|
#print "$result <- $_ @parms\n"; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
elsif (/^!(\w+)$/) { |
211
|
13
|
50
|
|
|
|
34
|
if (!@regstack) { |
212
|
0
|
|
|
|
|
0
|
error("Imager::transform2: stack underflow with $_"); |
213
|
0
|
|
|
|
|
0
|
return; |
214
|
|
|
|
|
|
|
} |
215
|
13
|
|
|
|
|
39
|
$names{$1} = pop(@regstack); |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
elsif (/^\@(\w+)$/) { |
218
|
29
|
50
|
|
|
|
54
|
if (exists $names{$1}) { |
219
|
29
|
|
|
|
|
67
|
push(@regstack, $names{$1}); |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
else { |
222
|
0
|
|
|
|
|
0
|
error("Imager::Expr: unknown storage \@$1"); |
223
|
0
|
|
|
|
|
0
|
return; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
else { |
227
|
0
|
|
|
|
|
0
|
error("Imager::Expr: unknown operator $_"); |
228
|
0
|
|
|
|
|
0
|
return; |
229
|
|
|
|
|
|
|
} |
230
|
|
|
|
|
|
|
} |
231
|
29
|
50
|
|
|
|
54
|
if (@regstack != 1) { |
232
|
0
|
|
|
|
|
0
|
error("stack must have only one item at end"); |
233
|
0
|
|
|
|
|
0
|
return; |
234
|
|
|
|
|
|
|
} |
235
|
29
|
50
|
|
|
|
81
|
if ($regstack[0] !~ /^p/) { |
236
|
0
|
|
|
|
|
0
|
error("you must have a color value at the top of the stack at end"); |
237
|
0
|
|
|
|
|
0
|
return; |
238
|
|
|
|
|
|
|
} |
239
|
29
|
|
|
|
|
78
|
push(@ops, [ "ret", $regstack[0], (-1) x $max_opr ]); |
240
|
|
|
|
|
|
|
|
241
|
29
|
|
|
|
|
65
|
$self->{"nregs"} = \@nregs; |
242
|
29
|
|
|
|
|
43
|
$self->{"cregs"} = \@cregs; |
243
|
|
|
|
|
|
|
|
244
|
29
|
|
|
|
|
173
|
return \@ops; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sub dumpops { |
248
|
0
|
|
|
0
|
1
|
0
|
my $result = ''; |
249
|
0
|
|
|
|
|
0
|
for my $op (@{$_[0]->{ops}}) { |
|
0
|
|
|
|
|
0
|
|
250
|
0
|
|
|
|
|
0
|
$result .= "@{$op}\n"; |
|
0
|
|
|
|
|
0
|
|
251
|
|
|
|
|
|
|
} |
252
|
0
|
|
|
|
|
0
|
$result; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
# unassembles the compiled code |
256
|
|
|
|
|
|
|
sub dumpcode { |
257
|
3
|
|
|
3
|
1
|
1649
|
my ($self) = @_; |
258
|
3
|
|
|
|
|
7
|
my $code = $self->{"code"}; |
259
|
3
|
|
|
|
|
8
|
my $attr = \%Imager::Regops::Attr; |
260
|
3
|
|
|
|
|
31
|
my @code = unpack("${Imager::Regops::PackCode}*", $code); |
261
|
3
|
|
|
|
|
59
|
my %names = map { $attr->{$_}{opcode}, $_ } keys %Imager::Regops::Attr; |
|
159
|
|
|
|
|
358
|
|
262
|
3
|
|
|
|
|
24
|
my @vars = $self->_variables(); |
263
|
3
|
|
|
|
|
6
|
my $result = ''; |
264
|
3
|
|
|
|
|
6
|
my $index = 0; |
265
|
3
|
|
|
|
|
29
|
while (my @op = splice(@code, 0, 2+$Imager::Regops::MaxOperands)) { |
266
|
15
|
|
|
|
|
24
|
my $opcode = shift @op; |
267
|
15
|
|
|
|
|
24
|
my $name = $names{$opcode}; |
268
|
15
|
50
|
|
|
|
23
|
if ($name) { |
269
|
15
|
|
|
|
|
32
|
$result .= "j$index: $name($opcode)"; |
270
|
15
|
|
|
|
|
36
|
my @types = split //, $attr->{$name}{types}; |
271
|
15
|
|
|
|
|
23
|
for my $parm (@types) { |
272
|
26
|
|
|
|
|
29
|
my $reg = shift @op; |
273
|
26
|
|
|
|
|
43
|
$result .= " $parm$reg"; |
274
|
26
|
100
|
|
|
|
54
|
if ($parm eq 'r') { |
275
|
23
|
100
|
|
|
|
53
|
if ($reg < @vars) { |
|
|
100
|
|
|
|
|
|
276
|
6
|
|
|
|
|
16
|
$result.= "($vars[$reg])"; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
elsif (defined $self->{"nregs"}[$reg]) { |
279
|
9
|
|
|
|
|
27
|
$result .= "($self->{\"nregs\"}[$reg])"; |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
$result .= " -> $attr->{$name}{result}$op[-1]" |
285
|
15
|
100
|
|
|
|
65
|
if $attr->{$name}{result}; |
286
|
15
|
|
|
|
|
26
|
$result .= "\n"; |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
else { |
289
|
0
|
|
|
|
|
0
|
$result .= "unknown($opcode) @op\n"; |
290
|
|
|
|
|
|
|
} |
291
|
15
|
|
|
|
|
38
|
++$index; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
3
|
|
|
|
|
30
|
$result; |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
package Imager::Expr::Postfix; |
298
|
|
|
|
|
|
|
our @ISA = qw(Imager::Expr); |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
Imager::Expr::Postfix->register_type('rpnexpr'); |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
my %op_names = ( '+'=>'add', '-'=>'subtract', '*'=>'mult', '/' => 'div', |
303
|
|
|
|
|
|
|
'%'=>'mod', '**'=>'pow' ); |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
sub compile { |
306
|
29
|
|
|
29
|
|
51
|
my ($self, $expr, $opts) = @_; |
307
|
|
|
|
|
|
|
|
308
|
29
|
|
|
|
|
68
|
$expr =~ s/#.*//; # remove comments |
309
|
29
|
|
|
|
|
105
|
my @st_ops = split ' ', $expr; |
310
|
|
|
|
|
|
|
|
311
|
29
|
|
|
|
|
50
|
for (@st_ops) { |
312
|
362
|
100
|
|
|
|
489
|
$_ = $op_names{$_} if exists $op_names{$_}; |
313
|
362
|
100
|
|
|
|
508
|
$_ = $self->{constants}{$_} if exists $self->{constants}{$_}; |
314
|
|
|
|
|
|
|
} |
315
|
29
|
|
|
|
|
60
|
return $self->stack_to_reg(@st_ops); |
316
|
|
|
|
|
|
|
} |
317
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
package Imager::Expr::Infix; |
319
|
|
|
|
|
|
|
|
320
|
|
|
|
|
|
|
our @ISA = qw(Imager::Expr); |
321
|
5
|
|
|
5
|
|
42
|
use Imager::Regops qw(%Attr $MaxOperands); |
|
5
|
|
|
|
|
40
|
|
|
5
|
|
|
|
|
4714
|
|
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
{ |
324
|
|
|
|
|
|
|
local @INC = @INC; |
325
|
|
|
|
|
|
|
pop @INC if $INC[-1] eq '.'; |
326
|
5
|
|
|
5
|
|
5234
|
eval "use Parse::RecDescent;"; |
|
5
|
|
|
|
|
168324
|
|
|
5
|
|
|
|
|
33
|
|
327
|
|
|
|
|
|
|
__PACKAGE__->register_type('expr') if !$@; |
328
|
|
|
|
|
|
|
} |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
# I really prefer bottom-up parsers |
331
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
my $parser; |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
sub init_parser { |
413
|
0
|
0
|
|
0
|
|
0
|
if (!$parser) { |
414
|
0
|
|
|
|
|
0
|
$parser = Parse::RecDescent->new($grammar); |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
sub compile { |
419
|
1
|
|
|
1
|
|
4
|
my ($self, $expr, $opts) = @_; |
420
|
1
|
50
|
|
|
|
3
|
if (!$parser) { |
421
|
1
|
|
|
|
|
5
|
$parser = Parse::RecDescent->new($grammar); |
422
|
|
|
|
|
|
|
} |
423
|
1
|
|
|
|
|
103125
|
my $optree = $parser->code($expr); |
424
|
1
|
50
|
|
|
|
61994
|
if (!$optree) { |
425
|
0
|
|
|
|
|
0
|
$self->error("Error in $expr\n"); |
426
|
0
|
|
|
|
|
0
|
return; |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
|
429
|
1
|
|
|
|
|
14
|
@{$self->{inputs}}{$self->_variables} = (); |
|
1
|
|
|
|
|
4
|
|
430
|
1
|
|
|
|
|
4
|
$self->{varregs} = {}; |
431
|
1
|
|
|
|
|
5
|
@{$self->{varregs}}{$self->_variables} = map { "r$_" } 0..$self->_variables-1; |
|
1
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
8
|
|
432
|
1
|
|
|
|
|
14
|
$self->{"nregs"} = [ (undef) x $self->_variables ]; |
433
|
1
|
|
|
|
|
4
|
$self->{"cregs"} = []; |
434
|
1
|
|
|
|
|
2
|
$self->{"lits"} = {}; |
435
|
|
|
|
|
|
|
|
436
|
1
|
|
|
|
|
2
|
eval { |
437
|
|
|
|
|
|
|
# generate code for the assignments |
438
|
1
|
|
|
|
|
2
|
for my $assign (@{$optree->[0]}) { |
|
1
|
|
|
|
|
4
|
|
439
|
1
|
|
|
|
|
3
|
my ($varname, $tree) = @$assign; |
440
|
1
|
50
|
|
|
|
8
|
if (exists $self->{inputs}{$varname}) { |
441
|
0
|
|
|
|
|
0
|
$self->error("$varname is an input - you can't assign to it"); |
442
|
0
|
|
|
|
|
0
|
return; |
443
|
|
|
|
|
|
|
} |
444
|
1
|
|
|
|
|
5
|
$self->{varregs}{$varname} = $self->gencode($tree); |
445
|
|
|
|
|
|
|
} |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
# generate the final result |
448
|
1
|
|
|
|
|
4
|
my $result = $self->gencode($optree->[1]); |
449
|
1
|
50
|
|
|
|
5
|
if ($result !~ /^p\d+$/) { |
450
|
0
|
|
|
|
|
0
|
$self->error("You must return a color value"); |
451
|
0
|
|
|
|
|
0
|
return; |
452
|
|
|
|
|
|
|
} |
453
|
1
|
|
|
|
|
3
|
push(@{$self->{genops}}, [ 'ret', $result, (0) x $MaxOperands ]) |
|
1
|
|
|
|
|
4
|
|
454
|
|
|
|
|
|
|
}; |
455
|
1
|
50
|
|
|
|
4
|
if ($@) { |
456
|
0
|
|
|
|
|
0
|
$self->error($@); |
457
|
0
|
|
|
|
|
0
|
return; |
458
|
|
|
|
|
|
|
} |
459
|
|
|
|
|
|
|
|
460
|
1
|
|
|
|
|
8
|
return $self->{genops}; |
461
|
|
|
|
|
|
|
} |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
sub gencode { |
464
|
11
|
|
|
11
|
|
15
|
my ($self, $tree) = @_; |
465
|
|
|
|
|
|
|
|
466
|
11
|
100
|
66
|
|
|
103
|
if (ref $tree) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
467
|
4
|
|
|
|
|
9
|
my ($op, @parms) = @$tree; |
468
|
|
|
|
|
|
|
|
469
|
4
|
50
|
|
|
|
13
|
if (!exists $Attr{$op}) { |
470
|
0
|
|
|
|
|
0
|
die "Unknown operator or function $op"; |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
4
|
|
|
|
|
6
|
for my $subtree (@parms) { |
474
|
9
|
|
|
|
|
21
|
$subtree = $self->gencode($subtree); |
475
|
|
|
|
|
|
|
} |
476
|
4
|
|
|
|
|
8
|
my $types = join("", map {substr($_,0,1)} @parms); |
|
9
|
|
|
|
|
19
|
|
477
|
|
|
|
|
|
|
|
478
|
4
|
50
|
|
|
|
18
|
if (length($types) < length($Attr{$op}{types})) { |
479
|
0
|
|
|
|
|
0
|
die "Too few parameters in call to $op"; |
480
|
|
|
|
|
|
|
} |
481
|
4
|
50
|
|
|
|
8
|
if ($types ne $Attr{$op}{types}) { |
482
|
|
|
|
|
|
|
# some alternate operators have the same name followed by p |
483
|
0
|
|
|
|
|
0
|
my $opp = $op."p"; |
484
|
0
|
0
|
0
|
|
|
0
|
if (exists $Attr{$opp} && |
485
|
|
|
|
|
|
|
$types eq $Attr{$opp}{types}) { |
486
|
0
|
|
|
|
|
0
|
$op = $opp; |
487
|
|
|
|
|
|
|
} |
488
|
|
|
|
|
|
|
else { |
489
|
0
|
|
|
|
|
0
|
die "Call to $_ with incorrect types"; |
490
|
|
|
|
|
|
|
} |
491
|
|
|
|
|
|
|
} |
492
|
4
|
|
|
|
|
5
|
my $result; |
493
|
4
|
100
|
|
|
|
12
|
if ($Attr{$op}{result} eq 'r') { |
494
|
3
|
|
|
|
|
4
|
$result = "r".@{$self->{nregs}}; |
|
3
|
|
|
|
|
7
|
|
495
|
3
|
|
|
|
|
4
|
push(@{$self->{nregs}}, undef); |
|
3
|
|
|
|
|
5
|
|
496
|
|
|
|
|
|
|
} |
497
|
|
|
|
|
|
|
else { |
498
|
1
|
|
|
|
|
2
|
$result = "p".@{$self->{cregs}}; |
|
1
|
|
|
|
|
2
|
|
499
|
1
|
|
|
|
|
3
|
push(@{$self->{cregs}}, undef); |
|
1
|
|
|
|
|
2
|
|
500
|
|
|
|
|
|
|
} |
501
|
4
|
|
|
|
|
15
|
push(@parms, "0") while @parms < $MaxOperands; |
502
|
4
|
|
|
|
|
6
|
push(@{$self->{genops}}, [ $op, @parms, $result]); |
|
4
|
|
|
|
|
18
|
|
503
|
4
|
|
|
|
|
13
|
return $result; |
504
|
|
|
|
|
|
|
} |
505
|
|
|
|
|
|
|
elsif (exists $self->{varregs}{$tree}) { |
506
|
3
|
|
|
|
|
7
|
return $self->{varregs}{$tree}; |
507
|
|
|
|
|
|
|
} |
508
|
|
|
|
|
|
|
elsif ($tree =~ /^$numre$/ || exists $self->{constants}{$tree}) { |
509
|
4
|
100
|
|
|
|
15
|
$tree = $self->{constants}{$tree} if exists $self->{constants}{$tree}; |
510
|
|
|
|
|
|
|
|
511
|
4
|
100
|
|
|
|
8
|
if (exists $self->{lits}{$tree}) { |
512
|
1
|
|
|
|
|
4
|
return $self->{lits}{$tree}; |
513
|
|
|
|
|
|
|
} |
514
|
3
|
|
|
|
|
4
|
my $reg = "r".@{$self->{nregs}}; |
|
3
|
|
|
|
|
7
|
|
515
|
3
|
|
|
|
|
7
|
push(@{$self->{nregs}}, $tree); |
|
3
|
|
|
|
|
7
|
|
516
|
3
|
|
|
|
|
7
|
$self->{lits}{$tree} = $reg; |
517
|
|
|
|
|
|
|
|
518
|
3
|
|
|
|
|
13
|
return $reg; |
519
|
|
|
|
|
|
|
} |
520
|
|
|
|
|
|
|
} |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
1; |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
__END__ |