3 # Copyright (c) 1996 Malcolm Beattie
5 # You may distribute under the terms of either the GNU General Public
6 # License or the Artistic License, as specified in the README file.
11 use B::Asmdata qw(%insn_data @insn_name);
12 use Config qw(%Config);
13 require ByteLoader; # we just need its $VERSIOM
18 @EXPORT_OK = qw(assemble_fh newasm endasm assemble asm);
24 for ($i = 0; defined($opname = ppname($i)); $i++) {
25 $opnumber{$opname} = $i;
28 my($linenum, $errors, $out); # global state, set up by newasm
32 warn "$linenum: $str\n";
37 sub debug { $debug = shift }
40 my( $val, $lo, $hi, $loc ) = @_;
41 if( $val < $lo || $hi < $val ){
42 error "argument for $loc outside [$lo, $hi]: $val";
49 # First define all the data conversion subs to which Asmdata will refer
52 sub B::Asmdata::PUT_U8 {
54 my $c = uncstring($arg);
56 if (length($c) != 1) {
57 error "argument for U8 is too long: $c";
58 $c = substr($c, 0, 1);
61 $arg = limcheck( $arg, 0, 0xff, 'U8' );
67 sub B::Asmdata::PUT_U16 {
68 my $arg = limcheck( $_[0], 0, 0xffff, 'U16' );
71 sub B::Asmdata::PUT_U32 {
72 my $arg = limcheck( $_[0], 0, 0xffffffff, 'U32' );
75 sub B::Asmdata::PUT_I32 {
76 my $arg = limcheck( $_[0], -0x80000000, 0x7fffffff, 'I32' );
79 sub B::Asmdata::PUT_NV { sprintf("%s\0", $_[0]) } # "%lf" looses precision and pack('d',...)
80 # may not even be portable between compilers
81 sub B::Asmdata::PUT_objindex { # could allow names here
82 my $arg = limcheck( $_[0], 0, 0xffffffff, '*index' );
85 sub B::Asmdata::PUT_svindex { &B::Asmdata::PUT_objindex }
86 sub B::Asmdata::PUT_opindex { &B::Asmdata::PUT_objindex }
87 sub B::Asmdata::PUT_pvindex { &B::Asmdata::PUT_objindex }
89 sub B::Asmdata::PUT_strconst {
91 my $str = uncstring($arg);
93 error "bad string constant: $arg";
96 if ($str =~ s/\0//g) {
97 error "string constant argument contains NUL: $arg";
103 sub B::Asmdata::PUT_pvcontents {
105 error "extraneous argument: $arg" if defined $arg;
108 sub B::Asmdata::PUT_PV {
110 my $str = uncstring($arg);
111 if( ! defined($str) ){
112 error "bad string argument: $arg";
115 return pack("L", length($str)) . $str;
117 sub B::Asmdata::PUT_comment_t {
119 $arg = uncstring($arg);
120 error "bad string argument: $arg" unless defined($arg);
121 if ($arg =~ s/\n//g) {
122 error "comment argument contains linefeed: $arg";
126 sub B::Asmdata::PUT_double { sprintf("%s\0", $_[0]) } # see PUT_NV above
127 sub B::Asmdata::PUT_none {
129 error "extraneous argument: $arg" if defined $arg;
132 sub B::Asmdata::PUT_op_tr_array {
133 my @ary = split /\s*,\s*/, shift;
134 return pack "S*", @ary;
137 sub B::Asmdata::PUT_IV64 {
138 return pack "Q", shift;
141 sub B::Asmdata::PUT_IV {
142 $Config{ivsize} == 4 ? &B::Asmdata::PUT_I32 : &B::Asmdata::PUT_IV64;
145 sub B::Asmdata::PUT_PADOFFSET {
146 $Config{ptrsize} == 8 ? &B::Asmdata::PUT_IV64 : &B::Asmdata::PUT_U32;
149 sub B::Asmdata::PUT_long {
150 $Config{longsize} == 8 ? &B::Asmdata::PUT_IV64 : &B::Asmdata::PUT_U32;
153 my %unesc = (n => "\n", r => "\r", t => "\t", a => "\a",
154 b => "\b", f => "\f", v => "\013");
158 $s =~ s/^"// and $s =~ s/"$// or return undef;
159 $s =~ s/\\(\d\d\d|.)/length($1) == 3 ? chr(oct($1)) : ($unesc{$1}||$1)/eg;
165 # Comments only allowed in instructions which don't take string arguments
166 # Treat string as a single line so .* eats \n characters.
168 ^\s* # Ignore leading whitespace
170 [^"]* # A double quote '"' indicates a string argument. If we
171 # find a double quote, the match fails and we strip nothing.
173 \s*\# # Any amount of whitespace plus the comment marker...
174 .*$ # ...which carries on to end-of-string.
175 }{$1}sx; # Keep only the instruction and optional argument.
179 # create the ByteCode header: magic, archname, ByteLoader $VERSION, ivsize,
181 # nvtype is irrelevant (floats are stored as strings)
182 # byteorder is strconst not U32 because of varying size issues
187 $header .= B::Asmdata::PUT_U32(0x43424c50); # 'PLBC'
188 $header .= B::Asmdata::PUT_strconst('"' . $Config{archname}. '"');
189 $header .= B::Asmdata::PUT_strconst(qq["$ByteLoader::VERSION"]);
190 $header .= B::Asmdata::PUT_U32($Config{ivsize});
191 $header .= B::Asmdata::PUT_U32($Config{ptrsize});
195 sub parse_statement {
197 my ($insn, $arg) = $stmt =~ m{
198 ^\s* # allow (but ignore) leading whitespace
199 (.*?) # Instruction continues up until...
200 (?: # ...an optional whitespace+argument group
201 \s+ # first whitespace.
202 (.*) # The argument is all the rest (newlines included).
203 )?$ # anchor at end-of-line
206 if ($arg =~ s/^0x(?=[0-9a-fA-F]+$)//) {
208 } elsif ($arg =~ s/^0(?=[0-7]+$)//) {
210 } elsif ($arg =~ /^pp_/) {
211 $arg =~ s/\s*$//; # strip trailing whitespace
212 my $opnum = $opnumber{$arg};
213 if (defined($opnum)) {
216 error qq(No such op type "$arg");
221 return ($insn, $arg);
225 my ($insn, $arg) = @_;
226 my $data = $insn_data{$insn};
227 if (defined($data)) {
228 my ($bytecode, $putsub) = @{$data}[0, 1];
229 my $argcode = &$putsub($arg);
230 return chr($bytecode).$argcode;
232 error qq(no such instruction "$insn");
240 my $asm = newasm($out);
241 while ($line = <$fh>) {
250 die "Invalid printing routine for B::Assembler\n" unless ref $outsub eq 'CODE';
251 die <<EOD if ref $out;
252 Can't have multiple byteassembly sessions at once!
253 (perhaps you forgot an endasm()?)
256 $linenum = $errors = 0;
259 $out->(gen_header());
264 die "There were $errors assembly errors\n";
266 $linenum = $errors = $out = 0;
275 my $quotedline = $line;
276 $quotedline =~ s/\\/\\\\/g;
277 $quotedline =~ s/"/\\"/g;
278 $out->(assemble_insn("comment", qq("$quotedline")));
280 if( $line = strip_comments($line) ){
281 ($insn, $arg) = parse_statement($line);
282 $out->(assemble_insn($insn, $arg));
284 $out->(assemble_insn("nop", undef));
289 ### temporary workaround
292 return if $_[0] =~ /\s*\W/;
294 return if $_[1] eq "0" and
295 $_[0] !~ /^(?:newsvx?|av_pushx?|av_extend|xav_flags)$/;
296 return if $_[1] eq "1" and $_[0] =~ /^(?:sv_refcnt)$/;
307 B::Assembler - Assemble Perl bytecode
311 use B::Assembler qw(newasm endasm assemble);
312 newasm(\&printsub); # sets up for assembly
313 assemble($buf); # assembles one line
314 endasm(); # closes down
316 use B::Assembler qw(assemble_fh);
317 assemble_fh($fh, \&printsub); # assemble everything in $fh
321 See F<ext/B/B/Assembler.pm>.
325 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
326 Per-statement interface by Benjamin Stuhl, C<sho_pi@hotmail.com>