1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package Imager::Expr::Assem;
use strict;
use Imager::Expr;
use Imager::Regops;
use vars qw($VERSION);
$VERSION = "1.002";
use vars qw(@ISA);
@ISA = qw(Imager::Expr);
__PACKAGE__->register_type('assem');
sub compile {
my ($self, $expr, $opts) = @_;
my %nregs;
my @vars = $self->_variables();
my @nregs = (0) x @vars;
my @cregs;
my %vars;
@vars{@vars} = map { "r$_" } 0..$#vars;
my %labels;
my @ops;
my @msgs;
my $attr = \%Imager::Regops::Attr;
# initially produce [ $linenum, $result, $opcode, @parms ]
my $lineno = 0;
while ($expr =~ s/^([^\n]+)(?:\n|$)//) {
++$lineno;
my $line = $1;
$line =~ s/#.*//;
next if $line =~ /^\s*$/;
for my $op (split /;/, $line) {
if (my ($name, $type) = $op =~ /^\s*var\s+([^:]+):(\S+)\s*$/) {
if (exists $vars{$name}) {
push(@msgs, "$lineno: duplicate variable name '$name'");
next;
}
if ($type eq 'num' || $type eq 'n') {
$vars{$name} = 'r'.@nregs;
push(@nregs, undef);
next;
}
elsif ($type eq 'pixel' || $type eq 'p' || $type eq 'c') {
$vars{$name} = 'p'.@cregs;
push(@cregs, undef);
next;
}
push(@msgs, "$lineno: unknown variable type $type");
next;
}
# any statement can have a label
if ($op =~ s/^\s*(\w+):\s*//) {
if ($labels{$1}) {
push(@msgs,
"$lineno: duplicate label $1 (previous on $labels{$1}[1])");
next;
}
$labels{$1} = [ scalar @ops, $lineno ];
}
next if $op =~ /^\s*$/;
# jumps have special operand handling
if ($op =~ /^\s*jump\s+(\w+)\s*$/) {
push(@ops, [$lineno, "", "jump", $1]);
}
elsif (my ($code, $reg, $targ) =
($op =~ /^\s*(jumpz|jumpnz)\s+(\S+)\s+(\S+)\s*$/)) {
push(@ops, [$lineno, "", $code, $reg, $targ]);
}
elsif ($op =~ /^\s*print\s+(\S+)\s*/) {
push(@ops, [$lineno, "", 'print', $1 ]);
}
elsif ($op =~ /^\s*ret\s+(\S+)\s*/) {
push(@ops, [$lineno, "", 'ret', $1]);
}
elsif ($op =~ s/\s*(\S+)\s*=\s*(\S+)\s*$//) {
# simple assignment
push(@ops, [$lineno, $1, "set", $2]);
}
elsif ($op =~ s/\s*(\S+)\s*=\s*(\S+)\s*//) {
# some normal ops finally
my ($result, $opcode) = ($1, $2);
unless ($attr->{$opcode}) {
push(@msgs, "$lineno: unknown operator $opcode");
next;
}
my @oper;
while ($op =~ s/(\S+)\s*//) {
push(@oper, $1);
}
push(@ops, [$lineno, $result, $opcode, @oper]);
}
else {
push(@msgs, "$lineno: invalid statement '$op'");
}
}
}
my $max_opr = $Imager::Regops::MaxOperands;
my $numre = $self->numre;
my $trans =
sub {
# translate a name/number to a <type><digits>
my ($name) = @_;
$name = $self->{constants}{$name}
if exists $self->{constants}{$name};
if ($vars{$name}) {
return $vars{$name};
}
elsif ($name =~ /^$numre$/) {
$vars{$name} = 'r'.@nregs;
push(@nregs, $name);
return $vars{$name};
}
else {
push(@msgs, "$lineno: undefined variable $name");
return '';
}
};
# now to translate symbols and so on
OP: for my $op (@ops) {
$lineno = shift @$op;
if ($op->[1] eq 'jump') {
unless (exists $labels{$op->[2]}) {
push(@msgs, "$lineno: unknown label $op->[2]");
next;
}
$op = [ 'jump', "j$labels{$op->[2]}[0]", (0) x $max_opr ];
}
elsif ($op->[1] =~ /^jump/) {
unless (exists $labels{$op->[3]}) {
push(@msgs, "$lineno: unknown label $op->[2]");
next;
}
$op = [ $op->[1], $trans->($op->[2]), "j$labels{$op->[3]}[0]",
(0) x ($max_opr-1) ];
}
elsif ($op->[1] eq 'print') {
$op = [ $op->[1], $trans->($op->[2]), (0) x $max_opr ];
}
elsif ($op->[1] eq 'ret') {
$op = [ 'ret', $trans->($op->[2]), (0) x $max_opr ];
}
else {
# a normal operator
my ($result, $name, @parms) = @$op;
if ($result =~ /^$numre$/) {
push(@msgs, "$lineno: target of operator cannot be a constant");
next;
}
$result = $trans->($result);
for my $parm (@parms) {
$parm = $trans->($parm);
}
push(@parms, (0) x ($max_opr-@parms));
$op = [ $op->[1], @parms, $result ];
}
}
# more validation than a real assembler
# not trying to solve the halting problem...
if (@ops && $ops[-1][0] ne 'ret' && $ops[-1][0] ne 'jump') {
push(@msgs, ": the last instruction must be ret or jump");
}
$self->{nregs} = \@nregs;
$self->{cregs} = \@cregs;
if (@msgs) {
$self->error(join("\n", @msgs));
return 0;
}
return \@ops;
}
1;
__END__
=head1 NAME
Imager::Expr::Assem - an assembler for producing code for the Imager
register machine
=head1 SYNOPSIS
use Imager::Expr::Assem;
my $expr = Imager::Expr->new(assem=>'...', ...)
=head1 DESCRIPTION
This module is a simple Imager::Expr compiler that compiles a
low-level language that has a nearly 1-to-1 relationship to the
internal representation used for compiled regmach code.
=head2 Syntax
Each line can contain multiple statements separated by semi-colons.
Anything after '#' in a line is ignored.
Types of statements:
=over 4
=item variable definition
=over 4
C<var> I<name>:I<type>
=back
defines variable I<name> to have I<type>, which can be any of 'n' or
'num' for a numeric type or 'pixel', 'p' or 'c' for a pixel or color
type.
Variable names cannot include whitespace.
=item operators
Operators can be split into 3 basic types, those that have a result
value, those that don't and the null operator, eg. jump has no value.
The format for operators that return a value is typically:
=over 4
I<result> = I<operator> I<operand> ...
=back
and for those that don't return a value:
=over 4
I<operator> I<operand>
=back
where operator is any valid regmach operator, result is any variable
defined with C<var>, and operands are variables, constants or
literals, or for jump operators, labels.
The set operator can be simplified to:
=over 4
I<result> = I<operator>
=back
All operators maybe preceded by a label, which is any non-whitespace
text immediately followed by a colon (':').
=back
=head1 BUGS
Note that the current optimizer may produce incorrect optimization for
your code, fortunately the optimizer will disable itself if you
include any jump operator in your code. A single jump to anywhere
after your final ret operator can be used to disable the optimizer
without slowing down your code.
There's currently no high-level code generation that can generate code
with loops or real conditions.
=head1 SEE ALSO
Imager(3), transform.perl, regmach.c
=head1 AUTHOR
Tony Cook <tony@develop-help.com>
=cut
|