My favorites | Sign in
Project Home Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# Assembler.pm
#
# Copyright (c) 1996 Malcolm Beattie
# Copyright (c) 2008,2009,2010,2011,2012 Reini Urban
#
# You may distribute under the terms of either the GNU General Public
# License or the Artistic License, as specified in the README file.

package B::Assembler;
use Exporter;
use B qw(ppname);
use B::Asmdata qw(%insn_data @insn_name);
use Config qw(%Config);
require ByteLoader; # we just need its $VERSION

no warnings; # XXX

@ISA = qw(Exporter);
@EXPORT_OK = qw(assemble_fh newasm endasm assemble asm maxopix maxsvix);
$VERSION = '1.11';

use strict;
my %opnumber;
my ( $i, $opname );
for ( $i = 0 ; defined( $opname = ppname($i) ) ; $i++ ) {
$opnumber{$opname} = $i;
}

my ( $linenum, $errors, $out ); # global state, set up by newasm

sub error {
my $str = shift;
warn "$linenum: $str\n";
$errors++;
}

my $debug = 0;
sub debug { $debug = shift }
my $quiet = 0;
sub quiet { $quiet = shift }
my ( $maxopix, $maxsvix ) = ( 0xffffffff, 0xffffffff );
sub maxopix { $maxopix = shift }
sub maxsvix { $maxsvix = shift }

sub limcheck($$$$) {
my ( $val, $lo, $hi, $loc ) = @_;
if ( $val < $lo || $hi < $val ) {
error "argument for $loc outside [$lo, $hi]: $val";
$val = $hi;
}
return $val;
}

#
# First define all the data conversion subs to which Asmdata will refer
#

sub B::Asmdata::PUT_U8 {
my $arg = shift;
my $c = uncstring($arg);
if ( defined($c) ) {
if ( length($c) != 1 ) {
error "argument for U8 is too long: $c";
$c = substr( $c, 0, 1 );
}
}
else {
$arg = limcheck( $arg, 0, 0xff, 'U8' );
$c = chr($arg);
}
return $c;
}

sub B::Asmdata::PUT_U16 {
my $arg = limcheck( $_[0], 0, 0xffff, 'U16' );
pack( "S", $arg );
}

sub B::Asmdata::PUT_U32 {
my $arg = limcheck( $_[0], 0, 0xffffffff, 'U32' );
pack( "L", $arg );
}

sub B::Asmdata::PUT_I32 {
my $arg = limcheck( $_[0], -0x80000000, 0x7fffffff, 'I32' );
pack( "l", $arg );
}

sub B::Asmdata::PUT_NV {
sprintf( "%s\0", $_[0] );
} # "%lf" looses precision and pack('d',...)
# may not even be portable between compilers

sub B::Asmdata::PUT_objindex { # could allow names here
my $maxidx = $_[1] || 0xffffffff;
my $what = $_[2] || 'ix';
my $arg = limcheck( $_[0], 0, $maxidx, $what );
pack( "L", $arg );
}
sub B::Asmdata::PUT_svindex { B::Asmdata::PUT_objindex( @_, $maxsvix, 'svix' ) }
sub B::Asmdata::PUT_opindex { B::Asmdata::PUT_objindex( @_, $maxopix, 'opix' ) }
sub B::Asmdata::PUT_pvindex { B::Asmdata::PUT_objindex( @_, $maxsvix, 'pvix' ) }
sub B::Asmdata::PUT_hekindex { B::Asmdata::PUT_objindex( @_ ) }

sub B::Asmdata::PUT_strconst {
my $arg = shift;
my $str = uncstring($arg);
if ( !defined($str) ) {
my @callstack = caller(1);
error "bad string constant: '$arg', called from ".$callstack[3]
." line:".$callstack[2];
$str = '';
}
if ( $str =~ s/\0//g ) {
error "string constant argument contains NUL: $arg";
$str = '';
}
return $str . "\0";
}

sub B::Asmdata::PUT_pvcontents {
my $arg = shift;
error "extraneous argument: $arg" if defined $arg;
return "";
}

sub B::Asmdata::PUT_PV {
my $arg = shift;
my $str = uncstring($arg);
if ( !defined($str) ) {
error "bad string argument: $arg";
$str = '';
}
return pack( "L", length($str) ) . $str;
}

sub B::Asmdata::PUT_comment_t {
my $arg = shift;
$arg = uncstring($arg);
error "bad string argument: $arg" unless defined($arg);
if ( $arg =~ s/\n//g ) {
error "comment argument contains linefeed: $arg";
}
return $arg . "\n";
}
sub B::Asmdata::PUT_double { sprintf( "%s\0", $_[0] ) } # see PUT_NV above

sub B::Asmdata::PUT_none {
my $arg = shift;
error "extraneous argument: $arg" if defined $arg;
return "";
}

sub B::Asmdata::PUT_op_tr_array {
my @ary = split /\s*,\s*/, shift;
return pack "S*", @ary;
}

sub B::Asmdata::PUT_IV64 {
return pack "Q", shift;
}

sub B::Asmdata::PUT_IV {
$Config{ivsize} == 4 ? &B::Asmdata::PUT_I32 : &B::Asmdata::PUT_IV64;
}

sub B::Asmdata::PUT_PADOFFSET {
$Config{ptrsize} == 8 ? &B::Asmdata::PUT_IV64 : &B::Asmdata::PUT_U32;
}

sub B::Asmdata::PUT_long {
$Config{longsize} == 8 ? &B::Asmdata::PUT_IV64 : &B::Asmdata::PUT_U32;
}

sub B::Asmdata::PUT_svtype {
$Config{longsize} == 8 ? &B::Asmdata::PUT_IV64 : &B::Asmdata::PUT_U32;
}

sub B::Asmdata::PUT_pmflags {
return ($] < 5.013) ? B::Asmdata::PUT_U16(@_) : B::Asmdata::PUT_U32(@_);
}

my %unesc = (
n => "\n",
r => "\r",
t => "\t",
a => "\a",
b => "\b",
f => "\f",
v => "\013"
);

sub uncstring {
my $s = shift;
$s =~ s/^"// and $s =~ s/"$// or return undef;
$s =~ s/\\(\d\d\d|.)/length($1) == 3 ? chr(oct($1)) : ($unesc{$1}||$1)/eg;
return $s;
}

sub strip_comments {
my $stmt = shift;

# Comments only allowed in instructions which don't take string arguments
# Treat string as a single line so .* eats \n characters.
my $line = $stmt;
$stmt =~ s{
^\s* # Ignore leading whitespace
(
[^"]* # A double quote '"' indicates a string argument. If we
# find a double quote, the match fails and we strip nothing.
)
\s*\# # Any amount of whitespace plus the comment marker...
\s*(.*)$ # ...which carries on to end-of-string.
}{$1}sx; # Keep only the instruction and optional argument.
return ($stmt) if $line eq $stmt;

$stmt =~ m{
^\s*
(
[^"]*
)
\s*\#
\s*(.*)$
}sx; # Keep only the instruction and optional argument.
my ( $line, $comment ) = ( $1, $2 );

# $line =~ s/\t$// if $comment;
return ( $line, $comment );
}

# create the ByteCode header:
# magic, archname, ByteLoader $VERSION, ivsize, ptrsize, longsize, byteorder,
# archflag, perlversion
# byteorder is strconst, not U32 because of varying size issues (?)
# archflag: bit 1: useithreads, bit 2: multiplicity
# perlversion for the bytecode translation.

sub gen_header {
my $header = gen_header_hash();
my $string = "";
$string .= B::Asmdata::PUT_U32( $header->{magic} );
$string .= B::Asmdata::PUT_strconst( '"' . $header->{archname} . '"' );
$string .= B::Asmdata::PUT_strconst( '"' . $header->{blversion} . '"' );
$string .= B::Asmdata::PUT_U32( $header->{ivsize} );
$string .= B::Asmdata::PUT_U32( $header->{ptrsize} );
if ( exists $header->{longsize} ) {
$string .= B::Asmdata::PUT_U32( $header->{longsize} );
}
$string .= B::Asmdata::PUT_strconst( sprintf(qq["0x%s"], $header->{byteorder} ));
if ( exists $header->{archflag} ) {
$string .= B::Asmdata::PUT_U16( $header->{archflag} );
}
if ( exists $header->{perlversion} ) {
$string .= B::Asmdata::PUT_strconst( '"' . $header->{perlversion} . '"');
}
$string;
}

# Calculate the ByteCode header values:
# magic, archname, ByteLoader $VERSION, ivsize, ptrsize, longsize, byteorder
# archflag, perlversion
# nvtype is irrelevant (floats are stored as strings)
# byteorder is strconst, not U32 because of varying size issues (?)
# archflag: bit 1: useithreads, bit 2: multiplicity
# perlversion for the bytecode translation.

sub gen_header_hash {
my $header = {};
my $blversion = "$ByteLoader::VERSION";
#if ($] < 5.009 and $blversion eq '0.06_01') {
# $blversion = '0.06';# fake the old backwards compatible version
#}
$header->{magic} = 0x43424c50;
$header->{archname} = $Config{archname};
$header->{blversion} = $blversion;
$header->{ivsize} = $Config{ivsize};
$header->{ptrsize} = $Config{ptrsize};
if ( $blversion ge "0.06_03" ) {
$header->{longsize} = $Config{longsize};
}
my $byteorder = $Config{byteorder};
if ($] < 5.007) {
# until 5.6 the $Config{byteorder} was dependent on ivsize, which was wrong. we need longsize.
my $t = $Config{ivtype};
my $s = $Config{longsize};
my $f = $t eq 'long' ? 'L!' : $s == 8 ? 'Q': 'I';
if ($s == 4 || $s == 8) {
my $i = 0;
foreach my $c (reverse(2..$s)) { $i |= ord($c); $i <<= 8 }
$i |= ord(1);
$byteorder = join('', unpack('a'x$s, pack($f, $i)));
} else {
$byteorder = '?'x$s;
}
}
$header->{byteorder} = $byteorder;
if ( $blversion ge "0.06_05" ) {
my $archflag = 0;
$archflag += 1 if $Config{useithreads};
$archflag += 2 if $Config{usemultiplicity};
$header->{archflag} = $archflag;
}
if ( $blversion ge "0.06_06" ) {
$header->{perlversion} = $];
}
$header;
}

sub parse_statement {
my $stmt = shift;
my ( $insn, $arg ) = $stmt =~ m{
^\s* # allow (but ignore) leading whitespace
(.*?) # Ignore -S op groups. Instruction continues up until...
(?: # ...an optional whitespace+argument group
\s+ # first whitespace.
(.*) # The argument is all the rest (newlines included).
)?$ # anchor at end-of-line
}sx;
if ( defined($arg) ) {
if ( $arg =~ s/^0x(?=[0-9a-fA-F]+$)// ) {
$arg = hex($arg);
}
elsif ( $arg =~ s/^0(?=[0-7]+$)// ) {
$arg = oct($arg);
}
elsif ( $arg =~ /^pp_/ ) {
$arg =~ s/\s*$//; # strip trailing whitespace
my $opnum = $opnumber{$arg};
if ( defined($opnum) ) {
$arg = $opnum;
}
else {
# TODO: ignore [op] from O=Bytecode,-S
error qq(No such op type "$arg");
$arg = 0;
}
}
}
return ( $insn, $arg );
}

sub assemble_insn {
my ( $insn, $arg ) = @_;
my $data = $insn_data{$insn};
if ( defined($data) ) {
my ( $bytecode, $putsub ) = @{$data}[ 0, 1 ];
error qq(unsupported instruction "$insn") unless $putsub;
return "" unless $putsub;
my $argcode = &$putsub($arg);
return chr($bytecode) . $argcode;
}
else {
error qq(no such instruction "$insn");
return "";
}
}

sub assemble_fh {
my ( $fh, $out ) = @_;
my $line;
my $asm = newasm($out);
while ( $line = <$fh> ) {
assemble($line);
}
endasm();
}

sub newasm {
my ($outsub) = @_;

die "Invalid printing routine for B::Assembler\n"
unless ref $outsub eq 'CODE';
die <<EOD if ref $out;
Can't have multiple byteassembly sessions at once!
(perhaps you forgot an endasm()?)
EOD

$linenum = $errors = 0;
$out = $outsub;

$out->( gen_header() );
}

sub endasm {
if ($errors) {
die "There were $errors assembly errors\n";
}
$linenum = $errors = $out = 0;
}

### interface via whole line, and optional comments
sub assemble {
my ($line) = @_;
my ( $insn, $arg, $comment );
$linenum++;
chomp $line;
$line =~ s/\cM$//;
if ($debug) {
my $quotedline = $line;
$quotedline =~ s/\\/\\\\/g;
$quotedline =~ s/"/\\"/g;
$out->( assemble_insn( "comment", qq("$quotedline") ) );
}
( $line, $comment ) = strip_comments($line);
if ($line) {
( $insn, $arg ) = parse_statement($line);
if ($debug and !$comment and $insn =~ /_flags/) {
$comment = sprintf("0x%x", $arg);
}
$out->( assemble_insn( $insn, $arg, $comment ) );
if ($debug) {
$out->( assemble_insn( "nop", undef ) );
}
}
elsif ( $debug and $comment ) {
$out->( assemble_insn( "nop", undef, $comment ) );
}
}

### temporary workaround
### interface via 2-3 args

sub asm ($;$$) {
return if $_[0] =~ /\s*\W/;
if ( defined $_[1] ) {
return
if $_[1] eq "0"
and $_[0] !~ /^(?:ldsv|stsv|newsvx?|newpadlx|av_pushx?|av_extend|xav_flags)$/;
return if $_[1] eq "1" and $]>5.007 and $_[0] =~ /^(?:sv_refcnt)$/;
}
my ( $insn, $arg, $comment ) = @_;
if ($] < 5.007) {
if ($insn eq "newsvx") {
$arg = $arg & 0xff; # sv not SVt_NULL
$insn = "newsv";
# XXX but this needs stsv tix-1 also
} elsif ($insn eq "newopx") {
$insn = "newop";
} elsif ($insn eq "av_pushx") {
$insn = "av_push";
} elsif ($insn eq "ldspecsvx") {
$insn = "ldspecsv";
} elsif ($insn eq "gv_stashpvx") {
$insn = "gv_stashpv";
} elsif ($insn eq "gv_fetchpvx") {
$insn = "gv_fetchpv";
} elsif ($insn eq "main_cv") {
return;
}
}
$out->( assemble_insn( $insn, $arg, $comment ) );
$linenum++;

# assemble "@_";
}

1;

__END__

=head1 NAME

B::Assembler - Assemble Perl bytecode

=head1 SYNOPSIS

perl -MO=Bytecode,-S,-omy.asm my.pl
assemble my.asm > my.plc

use B::Assembler qw(newasm endasm assemble);
newasm(\&printsub); # sets up for assembly
assemble($buf); # assembles one line
asm(opcode, arg, [comment]);
endasm(); # closes down

use B::Assembler qw(assemble_fh);
assemble_fh($fh, \&printsub); # assemble everything in $fh

=head1 DESCRIPTION

B::Bytecode helper module.

=head1 AUTHORS

Malcolm Beattie C<MICB at cpan.org> I<(1996, retired)>,
Per-statement interface by Benjamin Stuhl C<sho_pi@hotmail.com>,
Reini Urban C<perl-compiler@googlegroups.com> I(2008-)

=cut

# Local Variables:
# mode: cperl
# cperl-indent-level: 2
# fill-column: 100
# End:
# vim: expandtab shiftwidth=2:

Change log

3f20007a3a0f by Reini Urban <rur...@cpanel.net> on Nov 2, 2012   Diff
BC: Do store av_pushx 0, as ix=0
represents Nullsv

This fixes tests 9 and 10
Go to: 
Project members, sign in to write a code review

Older revisions

1d28c216fcc8 by Reini Urban <rur...@cpanel.net> on Nov 2, 2012   Diff
BC: PADLIST support

add -DP to print PAD indices and types
to STDERR

...
2cb0016ce423 by Reini Urban <rur...@cpanel.net> on Oct 31, 2012   Diff
BC: rename newpadl to newpadlx
87d023fbfd10 by Reini Urban <rur...@cpanel.net> on Oct 31, 2012   Diff
BC: more PADLIST + PAD work

rename padl_new to newpadl
fix Assembler (1.11) not to skip
newpadl with arg = 0
...
All revisions of this file

File info

Size: 12760 bytes, 496 lines
Powered by Google Project Hosting