]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Only remap the hint through the pointer table for threaded perls
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #ifndef NOOP
15 # define NOOP
16 #endif
17
18 #ifndef dNOOP
19 # define dNOOP
20 #endif
21
22 #ifndef Newx
23 # define Newx(v, n, c) New(0, v, n, c)
24 #endif
25
26 #ifndef SvPV_const
27 # define SvPV_const SvPV
28 #endif
29
30 #ifndef SvPV_nolen_const
31 # define SvPV_nolen_const SvPV_nolen
32 #endif
33
34 #ifndef SvPVX_const
35 # define SvPVX_const SvPVX
36 #endif
37
38 #ifndef SvREFCNT_inc_simple_NN
39 # define SvREFCNT_inc_simple_NN SvREFCNT_inc
40 #endif
41
42 #ifndef sv_catpvn_nomg
43 # define sv_catpvn_nomg sv_catpvn
44 #endif
45
46 #ifndef mPUSHp
47 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
48 #endif
49
50 #ifndef mPUSHu
51 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
52 #endif
53
54 #ifndef HvNAME_get
55 # define HvNAME_get(H) HvNAME(H)
56 #endif
57
58 #ifndef HvNAMELEN_get
59 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
60 #endif
61
62 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
63
64 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
65 # ifndef PL_lex_inwhat
66 #  define PL_lex_inwhat PL_parser->lex_inwhat
67 # endif
68 # ifndef PL_linestr
69 #  define PL_linestr PL_parser->linestr
70 # endif
71 # ifndef PL_bufptr
72 #  define PL_bufptr PL_parser->bufptr
73 # endif
74 # ifndef PL_oldbufptr
75 #  define PL_oldbufptr PL_parser->oldbufptr
76 # endif
77 #else
78 # ifndef PL_lex_inwhat
79 #  define PL_lex_inwhat PL_Ilex_inwhat
80 # endif
81 # ifndef PL_linestr
82 #  define PL_linestr PL_Ilinestr
83 # endif
84 # ifndef PL_bufptr
85 #  define PL_bufptr PL_Ibufptr
86 # endif
87 # ifndef PL_oldbufptr
88 #  define PL_oldbufptr PL_Ioldbufptr
89 # endif
90 #endif
91
92 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
93 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
94 #endif
95
96 /* ... Thread safety and multiplicity ...................................... */
97
98 #ifndef I_MULTIPLICITY
99 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
100 #  define I_MULTIPLICITY 1
101 # else
102 #  define I_MULTIPLICITY 0
103 # endif
104 #endif
105 #if I_MULTIPLICITY && !defined(tTHX)
106 # define tTHX PerlInterpreter*
107 #endif
108
109 #if I_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
110 # define I_THREADSAFE 1
111 # ifndef MY_CXT_CLONE
112 #  define MY_CXT_CLONE \
113     dMY_CXT_SV;                                                      \
114     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
115     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
116     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
117 # endif
118 #else
119 # define I_THREADSAFE 0
120 # undef  dMY_CXT
121 # define dMY_CXT      dNOOP
122 # undef  MY_CXT
123 # define MY_CXT       indirect_globaldata
124 # undef  START_MY_CXT
125 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
126 # undef  MY_CXT_INIT
127 # define MY_CXT_INIT  NOOP
128 # undef  MY_CXT_CLONE
129 # define MY_CXT_CLONE NOOP
130 #endif
131
132 /* --- Helpers ------------------------------------------------------------- */
133
134 /* ... Thread-safe hints ................................................... */
135
136 #if I_WORKAROUND_REQUIRE_PROPAGATION
137
138 typedef struct {
139  SV  *code;
140  I32  requires;
141 } indirect_hint_t;
142
143 #define I_HINT_STRUCT 1
144
145 #define I_HINT_CODE(H) ((H)->code)
146
147 #define I_HINT_FREE(H) {   \
148  indirect_hint_t *h = (H); \
149  SvREFCNT_dec(h->code);    \
150  PerlMemShared_free(h);    \
151 }
152
153 #else  /*  I_WORKAROUND_REQUIRE_PROPAGATION */
154
155 typedef SV indirect_hint_t;
156
157 #define I_HINT_STRUCT 0
158
159 #define I_HINT_CODE(H) (H)
160
161 #define I_HINT_FREE(H) SvREFCNT_dec(H);
162
163 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
164
165 #if I_THREADSAFE
166
167 #define PTABLE_NAME        ptable_hints
168 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
169
170 #define pPTBL  pTHX
171 #define pPTBL_ pTHX_
172 #define aPTBL  aTHX
173 #define aPTBL_ aTHX_
174
175 #include "ptable.h"
176
177 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
178 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
179
180 #endif /* I_THREADSAFE */
181
182 /* Define the op->str ptable here because we need to be able to clean it during
183  * thread cleanup. */
184
185 typedef struct {
186  const char *pos;
187  char       *buf;
188  STRLEN      len, size;
189  line_t      line;
190 } indirect_op_info_t;
191
192 #define PTABLE_NAME        ptable
193 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
194
195 #define pPTBL  pTHX
196 #define pPTBL_ pTHX_
197 #define aPTBL  aTHX
198 #define aPTBL_ aTHX_
199
200 #include "ptable.h"
201
202 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
203 #define ptable_clear(T)       ptable_clear(aTHX_ (T))
204 #define ptable_free(T)        ptable_free(aTHX_ (T))
205
206 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
207
208 typedef struct {
209 #if I_THREADSAFE
210  ptable     *tbl; /* It really is a ptable_hints */
211  tTHX        owner;
212 #endif
213  ptable     *map;
214  const char *linestr;
215 } my_cxt_t;
216
217 START_MY_CXT
218
219 #if I_THREADSAFE
220
221 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
222 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
223  CLONE_PARAMS  param;
224  AV           *stashes = NULL;
225  SV           *dupsv;
226
227  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
228   stashes = newAV();
229
230  param.stashes    = stashes;
231  param.flags      = 0;
232  param.proto_perl = owner;
233
234  dupsv = sv_dup(sv, &param);
235
236  if (stashes) {
237   av_undef(stashes);
238   SvREFCNT_dec(stashes);
239  }
240
241  return SvREFCNT_inc(dupsv);
242 }
243
244 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
245  my_cxt_t        *ud = ud_;
246  indirect_hint_t *h1 = ent->val;
247  indirect_hint_t *h2;
248
249  if (ud->owner == aTHX)
250   return;
251
252 #if I_HINT_STRUCT
253
254  h2           = PerlMemShared_malloc(sizeof *h2);
255  h2->code     = indirect_clone(h1->code, ud->owner);
256  SvREFCNT_inc(h2->code);
257 #if I_WORKAROUND_REQUIRE_PROPAGATION
258  h2->requires = h1->requires;
259 #endif
260
261 #else  /*  I_HINT_STRUCT */
262
263  h2 = indirect_clone(h1, ud->owner);
264  SvREFCNT_inc(h2);
265
266 #endif /* !I_HINT_STRUCT */
267
268  ptable_hints_store(ud->tbl, ent->key, h2);
269 }
270
271 STATIC void indirect_thread_cleanup(pTHX_ void *);
272
273 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
274  int *level = ud;
275
276  if (*level) {
277   *level = 0;
278   LEAVE;
279   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
280   ENTER;
281  } else {
282   dMY_CXT;
283   PerlMemShared_free(level);
284   ptable_free(MY_CXT.map);
285   ptable_hints_free(MY_CXT.tbl);
286  }
287 }
288
289 #endif /* I_THREADSAFE */
290
291 STATIC SV *indirect_tag(pTHX_ SV *value) {
292 #define indirect_tag(V) indirect_tag(aTHX_ (V))
293  indirect_hint_t *h;
294  SV *code = NULL;
295  dMY_CXT;
296
297  if (SvROK(value)) {
298   value = SvRV(value);
299   if (SvTYPE(value) >= SVt_PVCV) {
300    code = value;
301    if (CvANON(code) && !CvCLONED(code))
302     CvCLONE_on(code);
303    SvREFCNT_inc_simple_NN(code);
304   }
305  }
306
307 #if I_HINT_STRUCT
308  h = PerlMemShared_malloc(sizeof *h);
309  h->code = code;
310
311 #if I_WORKAROUND_REQUIRE_PROPAGATION
312  {
313   const PERL_SI *si;
314   I32            requires = 0;
315
316   for (si = PL_curstackinfo; si; si = si->si_prev) {
317    I32 cxix;
318
319    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
320     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
321
322     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
323      ++requires;
324    }
325   }
326
327   h->requires = requires;
328  }
329 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
330
331 #else  /*  I_HINT_STRUCT */
332  h = code;
333 #endif /* !I_HINT_STRUCT */
334
335 #if I_THREADSAFE
336  /* We only need for the key to be an unique tag for looking up the value later.
337   * Allocated memory provides convenient unique identifiers, so that's why we
338   * use the hint as the key itself. */
339  ptable_hints_store(MY_CXT.tbl, h, h);
340 #endif /* I_THREADSAFE */
341
342  return newSViv(PTR2IV(h));
343 }
344
345 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
346 #define indirect_detag(H) indirect_detag(aTHX_ (H))
347  indirect_hint_t *h;
348  dMY_CXT;
349
350  if (!(hint && SvIOK(hint)))
351   return NULL;
352
353  h = INT2PTR(indirect_hint_t *, SvIVX(hint));
354 #if I_THREADSAFE
355  h = ptable_fetch(MY_CXT.tbl, h);
356 #endif /* I_THREADSAFE */
357
358 #if I_WORKAROUND_REQUIRE_PROPAGATION
359  {
360   const PERL_SI *si;
361   I32            requires = 0;
362
363   for (si = PL_curstackinfo; si; si = si->si_prev) {
364    I32 cxix;
365
366    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
367     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
368
369     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
370                                && ++requires > h->requires)
371      return NULL;
372    }
373   }
374  }
375 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
376
377  return I_HINT_CODE(h);
378 }
379
380 STATIC U32 indirect_hash = 0;
381
382 STATIC SV *indirect_hint(pTHX) {
383 #define indirect_hint() indirect_hint(aTHX)
384  SV *hint;
385
386  if (IN_PERL_RUNTIME)
387   return NULL;
388
389 #if I_HAS_PERL(5, 9, 5)
390  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
391                                        NULL,
392                                        __PACKAGE__, __PACKAGE_LEN__,
393                                        0,
394                                        indirect_hash);
395 #else
396  {
397   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
398                                                                  indirect_hash);
399   if (!val)
400    return 0;
401   hint = *val;
402  }
403 #endif
404  return indirect_detag(hint);
405 }
406
407 /* ... op -> source position ............................................... */
408
409 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
410 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
411  indirect_op_info_t *oi;
412  const char *s;
413  STRLEN len;
414  dMY_CXT;
415
416  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
417   * In this case the linestr has temporarly changed, but the old buffer should
418   * still be alive somewhere. */
419
420  if (!PL_lex_inwhat) {
421   const char *pl_linestr = SvPVX_const(PL_linestr);
422   if (MY_CXT.linestr != pl_linestr) {
423    ptable_clear(MY_CXT.map);
424    MY_CXT.linestr = pl_linestr;
425   }
426  }
427
428  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
429   Newx(oi, 1, indirect_op_info_t);
430   ptable_store(MY_CXT.map, o, oi);
431   oi->buf  = NULL;
432   oi->size = 0;
433  }
434
435  if (sv) {
436   s = SvPV_const(sv, len);
437  } else {
438   s   = "{";
439   len = 1;
440  }
441
442  if (len > oi->size) {
443   Safefree(oi->buf);
444   Newx(oi->buf, len, char);
445   oi->size = len;
446  }
447  Copy(s, oi->buf, len, char);
448
449  oi->len  = len;
450  oi->pos  = src;
451  oi->line = line;
452 }
453
454 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
455 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
456  const indirect_op_info_t *val;
457  dMY_CXT;
458
459  if (MY_CXT.linestr != SvPVX_const(PL_linestr))
460   return NULL;
461
462  return ptable_fetch(MY_CXT.map, o);
463 }
464
465 STATIC void indirect_map_delete(pTHX_ const OP *o) {
466 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
467  dMY_CXT;
468
469  ptable_store(MY_CXT.map, o, NULL);
470 }
471
472 /* --- Check functions ----------------------------------------------------- */
473
474 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
475 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
476  STRLEN len;
477  const char *p = NULL, *r = SvPV_const(sv, len);
478
479  if (len >= 1 && *r == '$') {
480   ++r;
481   --len;
482   s = strchr(s, '$');
483   if (!s)
484    return NULL;
485  }
486
487  p = strstr(s, r);
488  while (p) {
489   p += len;
490   if (!isALNUM(*p))
491    break;
492   p = strstr(p + 1, r);
493  }
494
495  return p;
496 }
497
498 /* ... ck_const ............................................................ */
499
500 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
501
502 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
503  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
504
505  if (indirect_hint()) {
506   SV *sv = cSVOPo_sv;
507   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
508    const char *s = indirect_find(sv, PL_oldbufptr);
509    indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
510    return o;
511   }
512  }
513
514  indirect_map_delete(o);
515  return o;
516 }
517
518 /* ... ck_rv2sv ............................................................ */
519
520 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
521
522 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
523  if (indirect_hint()) {
524   OP *op = cUNOPo->op_first;
525   SV *sv;
526   const char *name = NULL, *s;
527   STRLEN len;
528   OPCODE type = (OPCODE) op->op_type;
529
530   switch (type) {
531    case OP_GV:
532    case OP_GVSV: {
533     GV *gv = cGVOPx_gv(op);
534     name = GvNAME(gv);
535     len  = GvNAMELEN(gv);
536     break;
537    }
538    default:
539     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
540      SV *nsv = cSVOPx_sv(op);
541      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
542       name = SvPV_const(nsv, len);
543     }
544   }
545   if (!name)
546    goto done;
547
548   sv = sv_2mortal(newSVpvn("$", 1));
549   sv_catpvn_nomg(sv, name, len);
550   s = indirect_find(sv, PL_oldbufptr);
551   if (!s) { /* If it failed, retry without the current stash */
552    const char *stash = HvNAME_get(PL_curstash);
553    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
554
555    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
556        || name[stashlen] != ':' || name[stashlen+1] != ':') {
557     /* Failed again ? Try to remove main */
558     stash = "main";
559     stashlen = 4;
560     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
561         || name[stashlen] != ':' || name[stashlen+1] != ':')
562      goto done;
563    }
564
565    sv_setpvn(sv, "$", 1);
566    stashlen += 2;
567    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
568    s = indirect_find(sv, PL_oldbufptr);
569    if (!s)
570     goto done;
571   }
572
573   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
574   indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
575   return o;
576  }
577
578 done:
579  o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
580
581  indirect_map_delete(o);
582  return o;
583 }
584
585 /* ... ck_padany ........................................................... */
586
587 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
588
589 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
590  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
591
592  if (indirect_hint()) {
593   SV *sv;
594   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
595
596   while (s < t && isSPACE(*s)) ++s;
597   if (*s == '$' && ++s <= t) {
598    while (s < t && isSPACE(*s)) ++s;
599    while (s < t && isSPACE(*t)) --t;
600    sv = sv_2mortal(newSVpvn("$", 1));
601    sv_catpvn_nomg(sv, s, t - s + 1);
602    indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
603    return o;
604   }
605  }
606
607  indirect_map_delete(o);
608  return o;
609 }
610
611 /* ... ck_scope ............................................................ */
612
613 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
614 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
615
616 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
617  OP *(*old_ck)(pTHX_ OP *) = 0;
618
619  switch (o->op_type) {
620   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
621   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
622  }
623  o = CALL_FPTR(old_ck)(aTHX_ o);
624
625  if (indirect_hint()) {
626   indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
627   return o;
628  }
629
630  indirect_map_delete(o);
631  return o;
632 }
633
634 /* We don't need to clean the map entries for leave ops because they can only
635  * be created by mutating from a lineseq. */
636
637 /* ... ck_method ........................................................... */
638
639 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
640
641 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
642  if (indirect_hint()) {
643   OP *op = cUNOPo->op_first;
644   const indirect_op_info_t *oi = indirect_map_fetch(op);
645   const char *s = NULL;
646   line_t line;
647   SV *sv;
648
649   if (oi && (s = oi->pos)) {
650    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
651    line = oi->line; /* Keep the old line so that we really point to the first */
652   } else {
653    sv = cSVOPx_sv(op);
654    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
655     goto done;
656    sv   = sv_mortalcopy(sv);
657    s    = indirect_find(sv, PL_oldbufptr);
658    line = CopLINE(&PL_compiling);
659   }
660
661   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
662   /* o may now be a method_named */
663
664   indirect_map_store(o, s, sv, line);
665   return o;
666  }
667
668 done:
669  o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
670
671  indirect_map_delete(o);
672  return o;
673 }
674
675 /* ... ck_entersub ......................................................... */
676
677 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
678  if (moi->pos > ooi->pos)
679   return 0;
680
681  if (moi->pos == ooi->pos)
682   return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
683
684  return 1;
685 }
686
687 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
688
689 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
690  SV *code = indirect_hint();
691
692  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
693
694  if (code) {
695   const indirect_op_info_t *moi, *ooi;
696   OP     *mop, *oop;
697   LISTOP *lop;
698
699   oop = o;
700   do {
701    lop = (LISTOP *) oop;
702    if (!(lop->op_flags & OPf_KIDS))
703     goto done;
704    oop = lop->op_first;
705   } while (oop->op_type != OP_PUSHMARK);
706   oop = oop->op_sibling;
707   mop = lop->op_last;
708
709   if (!oop)
710    goto done;
711
712   switch (oop->op_type) {
713    case OP_CONST:
714    case OP_RV2SV:
715    case OP_PADSV:
716    case OP_SCOPE:
717    case OP_LEAVE:
718     break;
719    default:
720     goto done;
721   }
722
723   if (mop->op_type == OP_METHOD)
724    mop = cUNOPx(mop)->op_first;
725   else if (mop->op_type != OP_METHOD_NAMED)
726    goto done;
727
728   moi = indirect_map_fetch(mop);
729   if (!(moi && moi->pos))
730    goto done;
731
732   ooi = indirect_map_fetch(oop);
733   if (!(ooi && ooi->pos))
734    goto done;
735
736   if (indirect_is_indirect(moi, ooi)) {
737    SV *file;
738    dSP;
739
740    ENTER;
741    SAVETMPS;
742
743 #ifdef USE_ITHREADS
744    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
745 #else
746    file = sv_mortalcopy(CopFILESV(&PL_compiling));
747 #endif
748
749    PUSHMARK(SP);
750    EXTEND(SP, 4);
751    mPUSHp(ooi->buf, ooi->len);
752    mPUSHp(moi->buf, moi->len);
753    PUSHs(file);
754    mPUSHu(moi->line);
755    PUTBACK;
756
757    call_sv(code, G_VOID);
758
759    PUTBACK;
760
761    FREETMPS;
762    LEAVE;
763   }
764  }
765
766 done:
767  return o;
768 }
769
770 STATIC U32 indirect_initialized = 0;
771
772 /* --- XS ------------------------------------------------------------------ */
773
774 MODULE = indirect      PACKAGE = indirect
775
776 PROTOTYPES: ENABLE
777
778 BOOT:
779 {
780  if (!indirect_initialized++) {
781   HV *stash;
782
783   MY_CXT_INIT;
784 #if I_THREADSAFE
785   MY_CXT.tbl     = ptable_new();
786   MY_CXT.owner   = aTHX;
787 #endif
788   MY_CXT.map     = ptable_new();
789   MY_CXT.linestr = NULL;
790
791   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
792
793   indirect_old_ck_const    = PL_check[OP_CONST];
794   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
795   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
796   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
797   indirect_old_ck_padany   = PL_check[OP_PADANY];
798   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
799   indirect_old_ck_scope    = PL_check[OP_SCOPE];
800   PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_ck_scope);
801   indirect_old_ck_lineseq  = PL_check[OP_LINESEQ];
802   PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_ck_scope);
803
804   indirect_old_ck_method   = PL_check[OP_METHOD];
805   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
806   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
807   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
808
809   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
810   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
811  }
812 }
813
814 #if I_THREADSAFE
815
816 void
817 CLONE(...)
818 PROTOTYPE: DISABLE
819 PREINIT:
820  ptable *t;
821  int    *level;
822 CODE:
823  {
824   my_cxt_t ud;
825   dMY_CXT;
826   ud.tbl   = t = ptable_new();
827   ud.owner = MY_CXT.owner;
828   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
829  }
830  {
831   MY_CXT_CLONE;
832   MY_CXT.map     = ptable_new();
833   MY_CXT.linestr = NULL;
834   MY_CXT.tbl     = t;
835   MY_CXT.owner   = aTHX;
836  }
837  {
838   level = PerlMemShared_malloc(sizeof *level);
839   *level = 1;
840   LEAVE;
841   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
842   ENTER;
843  }
844
845 #endif
846
847 SV *
848 _tag(SV *value)
849 PROTOTYPE: $
850 CODE:
851  RETVAL = indirect_tag(value);
852 OUTPUT:
853  RETVAL