1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
|
|
5 | typedef CV *B__CV; |
|
|
6 | |
5 | static OP *(*old_entersub)(pTHX); |
7 | static OP *(*old_entersub)(pTHX); |
6 | |
|
|
7 | static void |
|
|
8 | traverse (OP *op) |
|
|
9 | { |
|
|
10 | } |
|
|
11 | |
|
|
12 | static void |
|
|
13 | analyze (CV *cv) |
|
|
14 | { |
|
|
15 | OP *op = CvSTART (cv); |
|
|
16 | |
|
|
17 | traverse (op); |
|
|
18 | } |
|
|
19 | |
8 | |
20 | // this is, of course, a slower entersub |
9 | // this is, of course, a slower entersub |
21 | static OP * |
10 | static OP * |
22 | faster_entersub (pTHX) |
11 | faster_entersub (pTHX) |
23 | { |
12 | { |
24 | dSP; |
13 | dSP; |
25 | dTOPss; |
14 | dTOPss; |
26 | |
15 | |
27 | if (SvTYPE (sv) == SVt_PVGV) |
16 | if (SvTYPE (sv) == SVt_PVGV) |
28 | sv = GvCV (sv); |
17 | sv = (SV *)GvCV (sv); |
29 | |
18 | |
30 | if (sv) |
19 | if (sv) |
31 | { |
20 | { |
32 | sv_dump (sv); |
|
|
33 | // only once for now |
21 | // only once for now |
34 | PL_op->op_ppaddr = old_entersub; |
22 | PL_op->op_ppaddr = old_entersub; |
35 | |
23 | |
36 | // only simple cv calls for now |
24 | // only simple cv calls for now |
|
|
25 | if (!PL_perldb && !PL_tainting |
37 | if (!PL_perldb && SvTYPE (sv) == SVt_PVCV && !CvXSUB (sv)) |
26 | && SvTYPE (sv) == SVt_PVCV && !CvXSUB (sv) |
|
|
27 | && CvSTART (sv) // must exist |
|
|
28 | && CvSTART (sv)->op_type != OP_NULL) // shield against compiling an already-compiled op |
38 | { |
29 | { |
39 | sv = newSViv (PTR2IV (sv)); |
30 | SV *bsv = newSViv (PTR2IV (sv)); |
|
|
31 | |
40 | ENTER; |
32 | ENTER; |
41 | SAVETMPS; |
33 | SAVETMPS; |
42 | PUSHMARK (SP); |
34 | PUSHMARK (SP); |
43 | // emulate B::CV typemap entry we don't have |
35 | // emulate B::CV typemap entry we don't have |
44 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (sv), gv_stashpv ("B::CV", 1)))); |
36 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (bsv), gv_stashpv ("B::CV", 1)))); |
45 | PUTBACK; |
37 | PUTBACK; |
46 | call_pv ("Faster::entersub", G_VOID|G_DISCARD); |
38 | call_pv ("Faster::entersub", G_VOID|G_DISCARD|G_EVAL); |
47 | SPAGAIN; |
39 | SPAGAIN; |
48 | FREETMPS; |
40 | FREETMPS; |
49 | LEAVE; |
41 | LEAVE; |
50 | } |
42 | } |
51 | } |
43 | } |
… | |
… | |
53 | return old_entersub (aTHX); |
45 | return old_entersub (aTHX); |
54 | } |
46 | } |
55 | |
47 | |
56 | MODULE = Faster PACKAGE = Faster |
48 | MODULE = Faster PACKAGE = Faster |
57 | |
49 | |
|
|
50 | PROTOTYPES: ENABLE |
|
|
51 | |
|
|
52 | IV |
|
|
53 | ppaddr (int optype) |
|
|
54 | CODE: |
|
|
55 | RETVAL = optype == OP_ENTERSUB |
|
|
56 | ? (IV)old_entersub |
|
|
57 | : (IV)PL_ppaddr [optype]; |
|
|
58 | OUTPUT: |
|
|
59 | RETVAL |
|
|
60 | |
58 | void |
61 | void |
59 | hook_entersub () |
62 | hook_entersub () |
60 | CODE: |
63 | CODE: |
61 | old_entersub = PL_ppaddr [OP_ENTERSUB]; |
64 | old_entersub = PL_ppaddr [OP_ENTERSUB]; |
62 | PL_ppaddr [OP_ENTERSUB] = faster_entersub; |
65 | PL_ppaddr [OP_ENTERSUB] = faster_entersub; |
63 | |
66 | |
|
|
67 | void |
|
|
68 | patch_cv (B::CV cv, void *ptr) |
|
|
69 | CODE: |
|
|
70 | { |
|
|
71 | OP *op; |
64 | |
72 | |
|
|
73 | if (!ptr) |
|
|
74 | croak ("NULL not allowed as code address for patch_cv"); |
|
|
75 | |
|
|
76 | NewOp (0, op, 1, OP); |
|
|
77 | |
|
|
78 | op->op_sibling = CvSTART (cv); |
|
|
79 | op->op_type = OP_NULL; |
|
|
80 | op->op_ppaddr = ptr; |
|
|
81 | |
|
|
82 | CvSTART (cv) = op; |
|
|
83 | } |
|
|
84 | |
|
|
85 | |