Require Hooks::OP::Check::EntersubForCV 0.03 to make op generation work.
[p5sagit/Devel-BeginLift.git] / BeginLift.xs
CommitLineData
c0f47301 1#define PERL_CORE
2#define PERL_NO_GET_CONTEXT
3#include "EXTERN.h"
4#include "perl.h"
5#include "XSUB.h"
6#include <stdio.h>
7#include <string.h>
8
5e5bdebb 9#include "hook_op_check_entersubforcv.h"
10
c0f47301 11/* lifted from op.c */
12
13#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
14
5e5bdebb 15STATIC OP *lift_cb(pTHX_ OP *o, CV *cv, void *user_data) {
62b5f5ed 16 dSP;
c0f47301 17 SV *sv;
5e5bdebb 18 SV **stack_save;
19 OP *curop, *kid, *saved_next;
20 I32 type = o->op_type;
c0f47301 21
22 /* shamelessly lifted from fold_constants in op.c */
23
62b5f5ed 24 stack_save = SP;
25
c0f47301 26 curop = LINKLIST(o);
62b5f5ed 27
28 if (0) { /* call as macro */
29 OP *arg;
30 OP *gv;
31 /* this means the argument pushing ops are not executed, only the GV to
32 * resolve the call is, and B::OP objects will be made of all the opcodes
33 * */
34 PUSHMARK(SP); /* push a mark for the arguments */
35
36 /* push an arg for every sibling op */
37 for ( arg = curop->op_sibling; arg->op_sibling; arg = arg->op_sibling ) {
38 XPUSHs(sv_bless(newRV_inc(newSViv(PTR2IV(arg))), gv_stashpv("B::LISTOP", 0)));
39 }
40
41 /* find the last non null before the lifted entersub */
42 for ( kid = curop; kid->op_next != o; kid = kid->op_next ) {
43 if ( kid->op_type == OP_GV )
44 gv = kid;
45 }
46
47 PL_op = gv; /* make the call to our sub without evaluating the arg ops */
48 } else {
49 PL_op = curop;
50 }
51
52 /* stop right after the call */
53 saved_next = o->op_next;
54 o->op_next = NULL;
55
56 PUTBACK;
57 SAVETMPS;
c0f47301 58 CALLRUNOPS(aTHX);
62b5f5ed 59 SPAGAIN;
c0f47301 60
62b5f5ed 61 if (SP > stack_save) { /* sub returned something */
62 sv = POPs;
c0f47301 63 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
64 pad_swipe(o->op_targ, FALSE);
65 else if (SvTEMP(sv)) { /* grab mortal temp? */
66 (void)SvREFCNT_inc(sv);
67 SvTEMP_off(sv);
68 }
62b5f5ed 69
70 if (SvROK(sv) && sv_derived_from(sv, "B::OP")) {
71 OP *new = INT2PTR(OP *,SvIV((SV *)SvRV(sv)));
72 new->op_sibling = NULL;
73
74 /* FIXME this is bullshit */
75 if ( (PL_opargs[new->op_type] & OA_CLASS_MASK) != OA_SVOP ) {
76 new->op_next = saved_next;
77 } else {
78 new->op_next = new;
79 }
80
81 return new;
82 }
83
c0f47301 84 if (type == OP_RV2GV)
85 return newGVOP(OP_GV, 0, (GV*)sv);
4d07fd95 86
bd42c00a 87 if (SvTYPE(sv) == SVt_NULL) {
88 op_free(o);
89 return newOP(OP_NULL, 0);
90 }
91
c0f47301 92 return newSVOP(OP_CONST, 0, sv);
93 } else {
94 /* this bit not lifted, handles the 'sub doesn't return stuff' case
95 which fold_constants can ignore */
96 op_free(o);
97 return newOP(OP_NULL, 0);
98 }
99}
100
c0f47301 101MODULE = Devel::BeginLift PACKAGE = Devel::BeginLift
102
103PROTOTYPES: DISABLE
104
5e5bdebb 105UV
eeee00df 106setup_for_cv (class, CV *cv)
c0f47301 107 CODE:
5e5bdebb 108 RETVAL = (UV)hook_op_check_entersubforcv (cv, lift_cb, NULL);
109 OUTPUT:
110 RETVAL
c0f47301 111
112void
eeee00df 113teardown_for_cv (class, UV id)
c0f47301 114 CODE:
5e5bdebb 115 hook_op_check_entersubforcv_remove ((hook_op_check_id)id);