1 |
#include "EXTERN.h" |
2 |
#include "perl.h" |
3 |
#include "XSUB.h" |
4 |
|
5 |
typedef CV *B__CV; |
6 |
|
7 |
static OP *(*old_entersub)(pTHX); |
8 |
|
9 |
// this is, of course, a slower entersub |
10 |
static OP * |
11 |
faster_entersub (pTHX) |
12 |
{ |
13 |
dSP; |
14 |
dTOPss; |
15 |
|
16 |
if (SvTYPE (sv) == SVt_PVGV) |
17 |
sv = (SV *)GvCV (sv); |
18 |
|
19 |
if (sv) |
20 |
{ |
21 |
// only once for now |
22 |
PL_op->op_ppaddr = old_entersub; |
23 |
|
24 |
// only simple cv calls for now |
25 |
if (!PL_perldb && !PL_tainting |
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 |
29 |
{ |
30 |
SV *bsv = newSViv (PTR2IV (sv)); |
31 |
|
32 |
ENTER; |
33 |
SAVETMPS; |
34 |
PUSHMARK (SP); |
35 |
// emulate B::CV typemap entry we don't have |
36 |
XPUSHs (sv_2mortal (sv_bless (newRV_noinc (bsv), gv_stashpv ("B::CV", 1)))); |
37 |
PUTBACK; |
38 |
call_pv ("Faster::entersub", G_VOID|G_DISCARD|G_EVAL); |
39 |
SPAGAIN; |
40 |
FREETMPS; |
41 |
LEAVE; |
42 |
} |
43 |
} |
44 |
|
45 |
return old_entersub (aTHX); |
46 |
} |
47 |
|
48 |
MODULE = Faster PACKAGE = Faster |
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 |
|
61 |
void |
62 |
hook_entersub () |
63 |
CODE: |
64 |
old_entersub = PL_ppaddr [OP_ENTERSUB]; |
65 |
PL_ppaddr [OP_ENTERSUB] = faster_entersub; |
66 |
|
67 |
void |
68 |
patch_cv (B::CV cv, void *ptr) |
69 |
CODE: |
70 |
{ |
71 |
OP *op; |
72 |
|
73 |
NewOp (0, op, 1, OP); |
74 |
|
75 |
op->op_sibling = CvSTART (cv); |
76 |
op->op_type = OP_NULL; |
77 |
op->op_ppaddr = ptr; |
78 |
|
79 |
CvSTART (cv) = op; |
80 |
} |
81 |
|
82 |
|