ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Faster/Faster.pm
(Generate patch)

Comparing Faster/Faster.pm (file contents):
Revision 1.21 by root, Fri Mar 10 22:39:11 2006 UTC vs.
Revision 1.34 by root, Wed Mar 15 02:32:27 2006 UTC

8 8
9 perl -MFaster ... 9 perl -MFaster ...
10 10
11=head1 DESCRIPTION 11=head1 DESCRIPTION
12 12
13This module implements a very simple-minded JIT. It works by more or less 13This module implements a very simple-minded "JIT" (or actually AIT, ahead
14translating every function it sees into a C program, compiling it and then 14of time compiler). It works by more or less translating every function it
15replacing the function by the compiled code. 15sees into a C program, compiling it and then replacing the function by the
16compiled code.
16 17
17As a result, startup times are immense, as every function might lead to a 18As a result, startup times are immense, as every function might lead to a
18full-blown compilation. 19full-blown compilation.
19 20
20The speed improvements are also not great, you can expect 20% or so on 21The speed improvements are also not great, you can expect 20% or so on
21average, for code that runs very often. 22average, for code that runs very often. The reason for this is that data
23handling is mostly being done by the same old code, it just gets called
24a bit faster. Regexes and string operations won't get faster. Airhtmetic
25doresn't become any faster. Just the operands and other stuff is put on
26the stack faster, and the opcodes themselves have a bit less overhead.
22 27
23Faster is in the early stages of development. Due to its design its 28Faster is in the early stages of development. Due to its design its
24relatively safe to use (it will either work or simply slowdown the program 29relatively safe to use (it will either work or simply slowdown the program
25immensely, but rarely cause bugs). 30immensely, but rarely cause bugs).
26 31
32More intelligent algorithms (loop optimisation, type inference) could
33improve that easily, but requires a much more elaborate presentation and
34optimiser than what is in place. There are no plans to improve Faster in
35this way, yet, but it would provide a reasonably good place to start.
36
27Usage is very easy, just C<use Faster> and every function called from then 37Usage is very easy, just C<use Faster> and every function called from then
28on will be compiled. 38on will be compiled.
29 39
30Right now, Faster will leave lots of F<*.c>, F<*.o> and F<*.so> files in 40Right now, Faster can leave lots of F<*.c> and F<*.so> files in your
31F</tmp>, and it will even create those temporary files in an insecure 41F<$FASTER_CACHEDIR> (by default F<$HOME/.perl-faster-cache>), and it will
32manner, so watch out. 42even create those temporary files in an insecure manner, so watch out.
33 43
34=over 4 44=over 4
35 45
36=cut 46=cut
37 47
38package Faster; 48package Faster;
49
50no warnings;
39 51
40use strict; 52use strict;
41use Config; 53use Config;
42use B (); 54use B ();
43#use Digest::MD5 ();
44use DynaLoader (); 55use DynaLoader ();
56use Digest::MD5 ();
57use Storable ();
58use Fcntl ();
45 59
46BEGIN { 60BEGIN {
47 our $VERSION = '0.01'; 61 our $VERSION = '0.01';
48 62
49 require XSLoader; 63 require XSLoader;
50 XSLoader::load __PACKAGE__, $VERSION; 64 XSLoader::load __PACKAGE__, $VERSION;
51} 65}
52 66
67my $CACHEDIR =
68 $ENV{FASTER_CACHE}
69 || (exists $ENV{HOME} && "$ENV{HOME}/.perl-faster-cache")
70 || do {
71 require File::Temp;
72 File::Temp::tempdir (CLEANUP => 1)
73 };
74
53my $COMPILE = "$Config{cc} -c -I$Config{archlibexp}/CORE $Config{optimize} $Config{ccflags} $Config{cccdlflags}"; 75my $COMPILE = "$Config{cc} -c -I$Config{archlibexp}/CORE $Config{optimize} $Config{ccflags} $Config{cccdlflags}";
54my $LINK = "$Config{ld} $Config{ldflags} $Config{lddlflags} $Config{ccdlflags}"; 76my $LINK = "$Config{ld} $Config{ldflags} $Config{lddlflags} $Config{ccdlflags}";
55my $LIBS = "$Config{libs}"; 77my $LIBS = "";
56my $_o = $Config{_o}; 78my $_o = $Config{_o};
57my $_so = ".so"; 79my $_so = ".so";
58 80
59# we don't need no steenking PIC on x86 81# we don't need no steenking PIC on x86
60$COMPILE =~ s/-f(?:PIC|pic)//g 82$COMPILE =~ s/-f(?:PIC|pic)//g
61 if $Config{archname} =~ /^(i[3456]86)-/; 83 if $Config{archname} =~ /^(i[3456]86)-/;
62 84
63my $opt_assert = $ENV{FASTER_DEBUG}; 85my $opt_assert = $ENV{FASTER_DEBUG} & 2;
64my $verbose = $ENV{FASTER_VERBOSE}+0; 86my $verbose = $ENV{FASTER_VERBOSE}+0;
65 87
88warn "Faster: CACHEDIR is $CACHEDIR\n" if $verbose > 2;
89
66our $source; 90our $source;
67 91
68our @ops; 92our @ops;
93our $insn;
69our $op; 94our $op;
70our $op_name; 95our $op_name;
71our @op_loop;
72our %op_regcomp; 96our %op_regcomp;
73 97
98# ops that cause immediate return to the interpreter
74my %f_unsafe = map +($_ => undef), qw( 99my %f_unsafe = map +($_ => undef), qw(
75 leavesub leavesublv return 100 leavesub leavesublv return
76 goto last redo next 101 goto last redo next
77 eval flip leaveeval entertry 102 eval flip leaveeval entertry
78 formline grepstart mapstart 103 formline grepstart mapstart
79 substcont entereval require 104 substcont entereval require
80); 105);
81 106
82# pushmark extend=0 107# ops with known stack extend behaviour
83# padsv extend=1 108# the values given are maximum values
84# padav extend=1 109my %extend = (
85# padhv extend=1 110 pushmark => 0,
86# padany extend=1 111 nextstate => 0, # might reduce the stack
87# const extend=1 112 unstack => 0,
113 enter => 0,
88 114
115 stringify => 0,
116 not => 0,
117 and => 0,
118 or => 0,
119 gvsv => 0,
120 rv2gv => 0,
121 preinc => 0,
122 predec => 0,
123 postinc => 0,
124 postdec => 0,
125 aelem => 0,
126 helem => 0,
127 qr => 1, #???
128 pushre => 1,
129 gv => 1,
130 aelemfast => 1,
131 aelem => 0,
132 padsv => 1,
133 const => 1,
134 pop => 1,
135 shift => 1,
136 eq => -1,
137 ne => -1,
138 gt => -1,
139 lt => -1,
140 ge => -1,
141 lt => -1,
142 cond_expr => -1,
143 add => -1,
144 subtract => -1,
145 multiply => -1,
146 divide => -1,
147 aassign => 0,
148 sassign => -2,
149 method => 0,
150 method_named => 1,
151);
152
153# ops that do not need an ASYNC_CHECK
89my %f_noasync = map +($_ => undef), qw( 154my %f_noasync = map +($_ => undef), qw(
90 mapstart grepstart match entereval 155 mapstart grepstart match entereval
91 enteriter entersub leaveloop 156 enteriter entersub leaveloop
92 157
93 pushmark nextstate 158 pushmark nextstate caller
94 159
95 const stub unstack 160 const stub unstack
96 last next redo seq 161 last next redo goto seq
97 padsv padav padhv padany 162 padsv padav padhv padany
98 aassign sassign orassign 163 aassign sassign orassign
99 rv2av rv2cv rv2gv rv2hv refgen 164 rv2av rv2cv rv2gv rv2hv refgen
100 gv gvsv 165 gv gvsv
101 add subtract multiply divide 166 add subtract multiply divide
102 complement cond_expr and or not 167 complement cond_expr and or not
168 bit_and bit_or bit_xor
103 defined 169 defined
104 method_named 170 method method_named bless
105 preinc postinc predec postdec 171 preinc postinc predec postdec
106 aelem aelemfast helem delete exists 172 aelem aelemfast helem delete exists
107 pushre subst list join split concat 173 pushre subst list lslice join split concat
108 length substr stringify ord 174 length substr stringify ord
109 push pop shift unshift 175 push pop shift unshift
110 eq ne gt lt ge le 176 eq ne gt lt ge le
111 regcomp regcreset regcmaybe 177 regcomp regcreset regcmaybe
112); 178);
113 179
114my %callop = ( 180my %callop = (
115 entersub => "(PL_ppaddr [OP_ENTERSUB]) (aTHX)", 181 entersub => "(PL_op->op_ppaddr) (aTHX)",
116 mapstart => "Perl_pp_grepstart (aTHX)", 182 mapstart => "Perl_pp_grepstart (aTHX)",
117); 183);
118 184
119sub callop { 185sub callop {
120 $callop{$op_name} || "Perl_pp_$op_name (aTHX)" 186 $callop{$op_name} || "Perl_pp_$op_name (aTHX)"
126} 192}
127 193
128sub out_callop { 194sub out_callop {
129 assert "nextop == (OP *)$$op"; 195 assert "nextop == (OP *)$$op";
130 $source .= " PL_op = nextop; nextop = " . (callop $op) . ";\n"; 196 $source .= " PL_op = nextop; nextop = " . (callop $op) . ";\n";
197}
198
199sub out_jump {
200 assert "nextop == (OP *)${$_[0]}L";
201 $source .= " goto op_${$_[0]};\n";
131} 202}
132 203
133sub out_cond_jump { 204sub out_cond_jump {
134 $source .= " if (nextop == (OP *)${$_[0]}L) goto op_${$_[0]};\n"; 205 $source .= " if (nextop == (OP *)${$_[0]}L) goto op_${$_[0]};\n";
135} 206}
168 239
169 out_next; 240 out_next;
170} 241}
171 242
172sub op_pushmark { 243sub op_pushmark {
173 $source .= " PUSHMARK (PL_stack_sp);\n"; 244 $source .= " faster_PUSHMARK (PL_stack_sp);\n";
174 245
175 out_next; 246 out_next;
176} 247}
177 248
178if ($Config{useithreads} ne "define") { 249if ($Config{useithreads} ne "define") {
179 # disable optimisations on ithreads 250 # disable optimisations on ithreads
180 251
181 *op_const = sub { 252 *op_const = sub {
182 $source .= " { dSP; XPUSHs ((SV *)${$op->sv}L); PUTBACK; }\n"; 253 $source .= " { dSP; PUSHs ((SV *)${$op->sv}L); PUTBACK; }\n";
254
255 $ops[0]{follows_const}++ if @ops;#d#
183 256
184 out_next; 257 out_next;
185 }; 258 };
186 259
187 *op_gv = \&op_const; 260 *op_gv = \&op_const;
207 if (!($op->flags & B::OPf_MOD)) { 280 if (!($op->flags & B::OPf_MOD)) {
208 $source .= " if (SvGMAGICAL (sv)) sv = sv_mortalcopy (sv);\n"; 281 $source .= " if (SvGMAGICAL (sv)) sv = sv_mortalcopy (sv);\n";
209 } 282 }
210 283
211 $source .= " dSP;\n"; 284 $source .= " dSP;\n";
212 $source .= " XPUSHs (sv);\n"; 285 $source .= " PUSHs (sv);\n";
213 $source .= " PUTBACK;\n"; 286 $source .= " PUTBACK;\n";
214 $source .= " }\n"; 287 $source .= " }\n";
215 288
216 out_next; 289 out_next;
217 }; 290 };
218 291
219 *op_gvsv = sub { 292 *op_gvsv = sub {
220 $source .= " {\n"; 293 $source .= " {\n";
221 $source .= " dSP;\n"; 294 $source .= " dSP;\n";
222 $source .= " EXTEND (SP, 1);\n";
223 295
224 if ($op->private & B::OPpLVAL_INTRO) { 296 if ($op->private & B::OPpLVAL_INTRO) {
225 $source .= " PUSHs (save_scalar ((GV *)${$op->sv}L));\n"; 297 $source .= " PUSHs (save_scalar ((GV *)${$op->sv}L));\n";
226 } else { 298 } else {
227 $source .= " PUSHs (GvSV ((GV *)${$op->sv}L));\n"; 299 $source .= " PUSHs (GvSV ((GV *)${$op->sv}L));\n";
287 out_next; 359 out_next;
288} 360}
289 361
290sub op_padsv { 362sub op_padsv {
291 my $flags = $op->flags; 363 my $flags = $op->flags;
292 my $targ = $op->targ; 364 my $padofs = "(PADOFFSET)" . $op->targ;
293 365
294 $source .= <<EOF; 366 $source .= <<EOF;
295 { 367 {
296 dSP; 368 dSP;
297 XPUSHs (PAD_SV ((PADOFFSET)$targ)); 369 SV *sv = PAD_SVl ($padofs);
370EOF
371
372 if (($flags & B::OPf_MOD) && ($op->private & B::OPpLVAL_INTRO)) {
373 $source .= " SAVECLEARSV (PAD_SVl ($padofs));\n";
374 $ops[0]{follows_padsv_lval_intro}++ if @ops;#d#
375 }
376
377 $source .= <<EOF;
378 PUSHs (sv);
298 PUTBACK; 379 PUTBACK;
299EOF 380EOF
300 if ($op->flags & B::OPf_MOD) { 381
301 if ($op->private & B::OPpLVAL_INTRO) { 382 if (($flags & B::OPf_MOD) && ($op->private & B::OPpDEREF)) {
302 $source .= " SAVECLEARSV (PAD_SVl ((PADOFFSET)$targ));\n"; 383 $source .= " if (!SvROK (sv)) vivify_ref (sv, " . $op->private . " & OPpDEREF);\n";
303 } elsif ($op->private & B::OPpDEREF) {
304 my $deref = $op->private & B::OPpDEREF;
305 $source .= " Perl_vivify_ref (PAD_SVl ((PADOFFSET)$targ), $deref);\n";
306 }
307 } 384 }
385 $source .= " }\n";
386
387 out_next;
388}
389
390sub op_sassign {
391 $source .= <<EOF;
392 {
393 dSP;
394 dPOPTOPssrl;
395EOF
396 $source .= " SV *temp = left; left = right; right = temp;\n"
397 if $op->private & B::OPpASSIGN_BACKWARDS;
398
399 if ($insn->{follows_padsv_lval_intro} && !($op->private & B::OPpASSIGN_BACKWARDS)) {
400 # simple assignment - the target exists, but is basically undef
401 $source .= " SvSetSV (right, left);\n";
402 } else {
403 $source .= " SvSetMagicSV (right, left);\n";
404 }
405
308 $source .= <<EOF; 406 $source .= <<EOF;
407 SETs (right);
408 PUTBACK;
309 } 409 }
310EOF 410EOF
311 411
312 out_next; 412 out_next;
313} 413}
314 414
315# pattern const+ (or general push1) 415# pattern const+ (or general push1)
316# pattern pushmark return(?)
317# pattern pushmark gv rv2av pushmark padsv+o.ä. aassign 416# pattern pushmark gv rv2av pushmark padsv+o.ä. aassign
318 417
319# pattern const method_named
320sub op_method_named { 418sub op_method_named {
419 if ($insn->{follows_const}) {
321 $source .= <<EOF; 420 $source .= <<EOF;
421 {
422 dSP;
423 static SV *last_cv;
424 static U32 last_sub_generation;
425
426 /* simple "polymorphic" inline cache */
427 if (PL_sub_generation == last_sub_generation)
428 {
429 PUSHs (last_cv);
430 PUTBACK;
431 }
432 else
433 {
434 PL_op = nextop; nextop = Perl_pp_method_named (aTHX);
435
436 SPAGAIN;
437 last_sub_generation = PL_sub_generation;
438 last_cv = TOPs;
439 }
440 }
441EOF
442 } else {
443 $source .= <<EOF;
322 { 444 {
323 static HV *last_stash; 445 static HV *last_stash;
324 static SV *last_cv; 446 static SV *last_cv;
325 static U32 last_sub_generation; 447 static U32 last_sub_generation;
326 448
333 455
334 /* simple "polymorphic" inline cache */ 456 /* simple "polymorphic" inline cache */
335 if (stash == last_stash 457 if (stash == last_stash
336 && PL_sub_generation == last_sub_generation) 458 && PL_sub_generation == last_sub_generation)
337 { 459 {
338 XPUSHs (last_cv); 460 PUSHs (last_cv);
339 PUTBACK; 461 PUTBACK;
340 } 462 }
341 else 463 else
342 { 464 {
343 PL_op = nextop; nextop = Perl_pp_method_named (aTHX); 465 PL_op = nextop; nextop = Perl_pp_method_named (aTHX);
353 /* error case usually */ 475 /* error case usually */
354 PL_op = nextop; nextop = Perl_pp_method_named (aTHX); 476 PL_op = nextop; nextop = Perl_pp_method_named (aTHX);
355 } 477 }
356 } 478 }
357EOF 479EOF
480 }
358 481
359 out_next; 482 out_next;
360} 483}
361 484
362sub op_grepstart { 485sub op_grepstart {
376} 499}
377 500
378sub out_break_op { 501sub out_break_op {
379 my ($idx) = @_; 502 my ($idx) = @_;
380 503
504 if ($op->flags & B::OPf_SPECIAL && $insn->{loop}) {
505 # common case: no label, innermost loop only
506 my $next = $insn->{loop}{loop_targ}[$idx];
381 out_callop; 507 out_callop;
382 508 out_jump $next;
383 out_cond_jump $_->[$idx] 509 } elsif (my $loop = $insn->{loop}) {
384 for reverse @op_loop; 510 # less common case: maybe break to some outer loop
385
386 $source .= " return nextop;\n"; 511 $source .= " return nextop;\n";
512 # todo: walk stack up
513 } else {
514 $source .= " return nextop;\n";
515 }
387} 516}
388 517
389sub xop_next { 518sub op_next {
390 out_break_op 0; 519 out_break_op 0;
391} 520}
392 521
393sub op_last { 522sub op_last {
394 out_break_op 1; 523 out_break_op 1;
400 529
401sub cv2c { 530sub cv2c {
402 my ($cv) = @_; 531 my ($cv) = @_;
403 532
404 local @ops; 533 local @ops;
405 local @op_loop;
406 local %op_regcomp; 534 local %op_regcomp;
407 535
408 my %opsseen; 536 my $curloop;
409 my @todo = $cv->START; 537 my @todo = $cv->START;
538 my %op_target;
539 my $numpushmark;
540 my $scope;
410 541
542 my %op_seen;
411 while (my $op = shift @todo) { 543 while (my $op = shift @todo) {
544 my $next;
412 for (; $$op; $op = $op->next) { 545 for (; $$op; $op = $next) {
413 last if $opsseen{$$op}++; 546 last if $op_seen{$$op}++;
414 push @ops, $op; 547
548 $next = $op->next;
415 549
416 my $name = $op->name; 550 my $name = $op->name;
417 my $class = B::class $op; 551 my $class = B::class $op;
418 552
553 my $insn = { op => $op };
554
555 # end of loop reached?
556 $curloop = $curloop->{loop} if $curloop && $$op == ${$curloop->{loop_targ}[1]};
557
558 # remember enclosing loop
559 $insn->{loop} = $curloop if $curloop;
560
561 push @ops, $insn;
562
563 if (exists $extend{$name}) {
564 my $extend = $extend{$name};
565 $extend = $extend->($op) if ref $extend;
566 $insn->{extend} = $extend if defined $extend;
567 }
568
569 # TODO: mark scopes similar to loops, make them comparable
570 # static cxstack(?)
419 if ($class eq "LOGOP") { 571 if ($class eq "LOGOP") {
420 unshift @todo, $op->other; # unshift vs. push saves jumps 572 push @todo, $op->other;
573 $op_target{${$op->other}}++;
421 574
422 # regcomp/o patches ops at runtime, lets expect that 575 # regcomp/o patches ops at runtime, lets expect that
576 if ($name eq "regcomp" && $op->other->pmflags & B::PMf_KEEP) {
577 $op_target{${$op->first}}++;
423 $op_regcomp{${$op->first}} = $op->next 578 $op_regcomp{${$op->first}} = $op->next;
424 if $name eq "regcomp" && $op->other->pmflags & B::PMf_KEEP; 579 }
425 580
426 } elsif ($class eq "PMOP") { 581 } elsif ($class eq "PMOP") {
582 if (${$op->pmreplstart}) {
427 unshift @todo, $op->pmreplstart if ${$op->pmreplstart}; 583 unshift @todo, $op->pmreplstart;
584 $op_target{${$op->pmreplstart}}++;
585 }
428 586
429 } elsif ($class eq "LOOP") { 587 } elsif ($class eq "LOOP") {
430 push @op_loop, [$op->nextop, $op->lastop->next, $op->redoop->next];
431 push @todo, $op->nextop, $op->lastop->next, $op->redoop->next; 588 my @targ = ($op->nextop, $op->lastop->next, $op->redoop);
589
590 unshift @todo, $next, $op->redoop, $op->nextop, $op->lastop;
591 $next = $op->redoop;
592
593 $op_target{$$_}++ for @targ;
594
595 $insn->{loop_targ} = \@targ;
596 $curloop = $insn;
597
598 } elsif ($class eq "COP") {
599 if (defined $op->label) {
600 $insn->{bblock}++;
601 $curloop->{contains_label}{$op->label}++ if $curloop; #TODO: should be within loop
602 }
603
604 } else {
605 if ($name eq "pushmark") {
606 $numpushmark++;
607 }
432 } 608 }
433 } 609 }
434 } 610 }
611
612 $_->{bblock}++ for grep $op_target{${$_->{op}}}, @ops;
435 613
436 local $source = <<EOF; 614 local $source = <<EOF;
437OP *%%%FUNC%%% (pTHX) 615OP *%%%FUNC%%% (pTHX)
438{ 616{
439 register OP *nextop = (OP *)${$ops[0]}L; 617 register OP *nextop = (OP *)${$ops[0]->{op}}L;
440EOF 618EOF
619
620 $source .= " faster_PUSHMARK_PREALLOC ($numpushmark);\n"
621 if $numpushmark;
441 622
442 while (@ops) { 623 while (@ops) {
443 $op = shift @ops; 624 $insn = shift @ops;
625
626 $op = $insn->{op};
444 $op_name = $op->name; 627 $op_name = $op->name;
445 628
629 my $class = B::class $op;
630
631 $source .= "\n/* start basic block */\n" if exists $insn->{bblock};#d#
446 $source .= "op_$$op: /* $op_name */\n"; 632 $source .= "op_$$op: /* $op_name */\n";
447 #$source .= "fprintf (stderr, \"$$op in op $op_name\\n\");\n";#d# 633 #$source .= "fprintf (stderr, \"$$op in op $op_name\\n\");\n";#d#
448 #$source .= "{ dSP; sv_dump (TOPs); }\n";#d# 634 #$source .= "{ dSP; sv_dump (TOPs); }\n";#d#
449 635
450 $source .= " PERL_ASYNC_CHECK ();\n" 636 $source .= " PERL_ASYNC_CHECK ();\n"
451 unless exists $f_noasync{$op_name}; 637 unless exists $f_noasync{$op_name};
452 638
453 if (my $can = __PACKAGE__->can ("op_$op_name")) { 639 if (my $can = __PACKAGE__->can ("op_$op_name")) {
454 # handcrafted replacement 640 # handcrafted replacement
641
642 if ($insn->{extend} > 0) {
643 # coalesce EXTENDs
644 # TODO: properly take negative preceeding and following EXTENDs into account
645 for my $i (@ops) {
646 last if exists $i->{bblock};
647 last unless exists $i->{extend};
648 my $extend = delete $i->{extend};
649 $insn->{extend} += $extend if $extend > 0;
650 }
651
652 $source .= " { dSP; EXTEND (SP, $insn->{extend}); PUTBACK; }\n"
653 if $insn->{extend} > 0;
654 }
655
455 $can->($op); 656 $can->($op);
456 657
457 } elsif (exists $f_unsafe{$op_name}) { 658 } elsif (exists $f_unsafe{$op_name}) {
458 # unsafe, return to interpreter 659 # unsafe, return to interpreter
459 assert "nextop == (OP *)$$op"; 660 assert "nextop == (OP *)$$op";
460 $source .= " return nextop;\n"; 661 $source .= " return nextop;\n";
461 662
462 } elsif ("LOGOP" eq B::class $op) { 663 } elsif ("LOGOP" eq $class) {
463 # logical operation with optionaö branch 664 # logical operation with optional branch
464 out_callop; 665 out_callop;
465 out_cond_jump $op->other; 666 out_cond_jump $op->other;
466 out_jump_next; 667 out_jump_next;
467 668
468 } elsif ("PMOP" eq B::class $op) { 669 } elsif ("PMOP" eq $class) {
469 # regex-thingy 670 # regex-thingy
470 out_callop; 671 out_callop;
471 out_cond_jump $op->pmreplroot if ${$op->pmreplroot}; 672 out_cond_jump $op->pmreplroot if $op_name ne "pushre" && ${$op->pmreplroot};
472 out_jump_next; 673 out_jump_next;
473 674
474 } else { 675 } else {
475 # normal operator, linear execution 676 # normal operator, linear execution
476 out_linear; 677 out_linear;
488 689
489 $source 690 $source
490} 691}
491 692
492my $uid = "aaaaaaa0"; 693my $uid = "aaaaaaa0";
694my %so;
493 695
494sub source2ptr { 696sub func2ptr {
495 my (@source) = @_; 697 my (@func) = @_;
496 698
497 my $stem = "/tmp/Faster-$$-" . $uid++; 699 #LOCK
700 mkdir $CACHEDIR, 0777;
701 sysopen my $meta_fh, "$CACHEDIR/meta", &Fcntl::O_RDWR | &Fcntl::O_CREAT, 0666
702 or die "$$CACHEDIR/meta: $!";
703 binmode $meta_fh, ":raw:perlio";
704 fcntl_lock fileno $meta_fh
705 or die "$CACHEDIR/meta: $!";
498 706
707 my $meta = eval { Storable::fd_retrieve $meta_fh } || { version => 1 };
708
709 for my $f (@func) {
710 $f->{func} = "F" . Digest::MD5::md5_hex ($f->{source});
711 $f->{so} = $meta->{$f->{func}};
712 }
713
714 if (grep !$_->{so}, @func) {
715 my $stem;
716
717 do {
718 $stem = "$CACHEDIR/$$-" . $uid++;
719 } while -e "$stem$_so";
720
499 open FILE, ">:raw", "$stem.c"; 721 open my $fh, ">:raw", "$stem.c";
500 print FILE <<EOF; 722 print $fh <<EOF;
501#define PERL_NO_GET_CONTEXT 723#define PERL_NO_GET_CONTEXT
724#define PERL_CORE
502 725
503#include <assert.h> 726#include <assert.h>
504 727
505#include "EXTERN.h" 728#include "EXTERN.h"
506#include "perl.h" 729#include "perl.h"
507#include "XSUB.h" 730#include "XSUB.h"
508 731
732#if 1
733# define faster_PUSHMARK_PREALLOC(count) while (PL_markstack_ptr + (count) >= PL_markstack_max) markstack_grow ()
734# define faster_PUSHMARK(p) *++PL_markstack_ptr = (p) - PL_stack_base
735#else
736# define faster_PUSHMARK_PREALLOC(count) 1
737# define faster_PUSHMARK(p) PUSHMARK(p)
738#endif
739
509#define RUNOPS_TILL(op) \\ 740#define RUNOPS_TILL(op) \\
510while (nextop != (op)) \\ 741 while (nextop != (op)) \\
511 { \\ 742 { \\
512 PERL_ASYNC_CHECK (); \\ 743 PERL_ASYNC_CHECK (); \\
513 PL_op = nextop; nextop = (PL_op->op_ppaddr)(aTHX); \\ 744 PL_op = nextop; nextop = (PL_op->op_ppaddr)(aTHX); \\
514 }
515
516EOF
517 for (@source) {
518 my $func = $uid++;
519 $_ =~ s/%%%FUNC%%%/$func/g;
520 print FILE $_;
521 $_ = $func;
522 } 745 }
523 746
524 close FILE; 747EOF
748 for my $f (grep !$_->{so}, @func) {
749 next if $f->{so} = $meta->{$f->{func}}; # some cv's alias others
750
751 warn "compiling $f->{name} to $stem$_so:$f->{func}\n" if $verbose > 1;
752 my $source = $f->{source};
753 $source =~ s/%%%FUNC%%%/$f->{func}/g;
754 print $fh $source;
755 $meta->{$f->{func}} = $f->{so} = $stem;
756 }
757
758 close $fh;
525 system "$COMPILE -o $stem$_o $stem.c"; 759 system "$COMPILE -o $stem$_o $stem.c";
526 #d#unlink "$stem.c"; 760 unlink "$stem.c" unless $ENV{FASTER_DEBUG} & 1;
527 system "$LINK -o $stem$_so $stem$_o $LIBS"; 761 system "$LINK -o $stem$_so $stem$_o $LIBS";
528 unlink "$stem$_o"; 762 unlink "$stem$_o";
763 }
529 764
765 for my $f (@func) {
766 my $stem = $f->{so};
767
530 my $so = DynaLoader::dl_load_file "$stem$_so" 768 my $so = ($so{$stem} ||= DynaLoader::dl_load_file "$stem$_so")
531 or die "$stem$_so: $!"; 769 or die "$stem$_so: $!";
532 770
533 #unlink "$stem$_so"; 771 #unlink "$stem$_so";
534 772
535 map +(DynaLoader::dl_find_symbol $so, $_), @source 773 $f->{ptr} = DynaLoader::dl_find_symbol $so, $f->{func}
774 or die "$f->{func} not found in $stem$_so: $!";
775 }
776
777 seek $meta_fh, 0, 0 or die "$CACHEDIR/meta: $!";
778 Storable::nstore_fd $meta, $meta_fh;
779 truncate $meta_fh, tell $meta_fh;
780
781 # UNLOCK (by closing $meta_fh)
536} 782}
537 783
538my %ignore; 784my %ignore;
539 785
540sub entersub { 786sub entersub {
542 788
543 my $pkg = $cv->STASH->NAME; 789 my $pkg = $cv->STASH->NAME;
544 790
545 return if $ignore{$pkg}; 791 return if $ignore{$pkg};
546 792
547 warn "compiling ", $cv->STASH->NAME, "\n" 793 warn "optimising ", $cv->STASH->NAME, "\n"
548 if $verbose; 794 if $verbose;
549 795
550 eval { 796 eval {
551 my @cv; 797 my @func;
552 my @cv_source; 798
799 push @func, {
800 cv => $cv,
801 name => "<>",
802 source => cv2c $cv,
803 };
553 804
554 # always compile the whole stash 805 # always compile the whole stash
555 my %stash = $cv->STASH->ARRAY; 806 my %stash = $cv->STASH->ARRAY;
556 while (my ($k, $v) = each %stash) { 807 while (my ($k, $v) = each %stash) {
557 $v->isa (B::GV::) 808 $v->isa (B::GV::)
560 my $cv = $v->CV; 811 my $cv = $v->CV;
561 812
562 if ($cv->isa (B::CV::) 813 if ($cv->isa (B::CV::)
563 && ${$cv->START} 814 && ${$cv->START}
564 && $cv->START->name ne "null") { 815 && $cv->START->name ne "null") {
816
565 push @cv, $cv; 817 push @func, {
818 cv => $cv,
819 name => $k,
566 push @cv_source, cv2c $cv; 820 source => cv2c $cv,
821 };
567 } 822 }
568 } 823 }
569 824
570 my @ptr = source2ptr @cv_source; 825 func2ptr @func;
571 826
572 for (0 .. $#cv) { 827 for my $f (@func) {
573 patch_cv $cv[$_], $ptr[$_]; 828 patch_cv $f->{cv}, $f->{ptr};
574 } 829 }
575 }; 830 };
576 831
577 if ($@) { 832 if ($@) {
578 $ignore{$pkg}++; 833 $ignore{$pkg}++;
593=over 4 848=over 4
594 849
595=item FASTER_VERBOSE 850=item FASTER_VERBOSE
596 851
597Faster will output more informational messages when set to values higher 852Faster will output more informational messages when set to values higher
598than C<0>. Currently, C<1> outputs which packages are being compiled. 853than C<0>. Currently, C<1> outputs which packages are being compiled, C<3>
854outputs the cache directory and C<10> outputs information on which perl
855function is compiled into which shared object.
599 856
600=item FASTER_DEBUG 857=item FASTER_DEBUG
601 858
602Add debugging code when set to values higher than C<0>. Currently, this 859Add debugging code when set to values higher than C<0>. Currently, this
603adds 1-3 C<assert>'s per perl op, to ensure that opcode order and C 860adds 1-3 C<assert>'s per perl op (FASTER_DEBUG > 1), to ensure that opcode
604execution order are compatible. 861order and C execution order are compatible.
605 862
606=item FASTER_CACHE 863=item FASTER_CACHE
607 864
608NOT YET IMPLEMENTED
609
610Set a persistent cache directory that caches compiled code 865Set a persistent cache directory that caches compiled code fragments. The
611fragments. Normally, code compiled by Faster will be deleted immediately, 866default is C<$HOME/.perl-faster-cache> if C<HOME> is set and a temporary
612and every restart will recompile everything. Setting this variable to a 867directory otherwise.
613directory makes Faster cache the generated files for re-use.
614 868
615This directory will always grow in contents, so you might need to erase it 869This directory will always grow in size, so you might need to erase it
616from time to time. 870from time to time.
617 871
618=back 872=back
619 873
620=head1 BUGS/LIMITATIONS 874=head1 BUGS/LIMITATIONS
636These constructs will force the use of the interpreter for the currently 890These constructs will force the use of the interpreter for the currently
637executed function as soon as they are being encountered during execution. 891executed function as soon as they are being encountered during execution.
638 892
639 goto 893 goto
640 next, redo (but not well-behaved last's) 894 next, redo (but not well-behaved last's)
895 labels, if used
641 eval 896 eval
642 require 897 require
643 any use of formats 898 any use of formats
644 .., ... (flipflop operators) 899 .., ... (flipflop operators)
645 900

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines