]> git.vpit.fr Git - perl/modules/autovivification.git/blob - autovivification.xs
Make a_map_fetch() a simple macro around ptable_fetch() in the non-threaded case
[perl/modules/autovivification.git] / autovivification.xs
1 /* This file is part of the autovivification Perl module.
2  * See http://search.cpan.org/dist/autovivification/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "autovivification"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #ifndef HvNAME_get
15 # define HvNAME_get(H) HvNAME(H)
16 #endif
17
18 #ifndef HvNAMELEN_get
19 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
20 #endif
21
22 #define A_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
23
24 #undef ENTERn
25 #if defined(ENTER_with_name) && !A_HAS_PERL(5, 11, 4)
26 # define ENTERn(N) ENTER_with_name(N)
27 #else
28 # define ENTERn(N) ENTER
29 #endif
30
31 #undef LEAVEn
32 #if defined(LEAVE_with_name) && !A_HAS_PERL(5, 11, 4)
33 # define LEAVEn(N) LEAVE_with_name(N)
34 #else
35 # define LEAVEn(N) LEAVE
36 #endif
37
38 #ifndef A_WORKAROUND_REQUIRE_PROPAGATION
39 # define A_WORKAROUND_REQUIRE_PROPAGATION !A_HAS_PERL(5, 10, 1)
40 #endif
41
42 /* ... Thread safety and multiplicity ...................................... */
43
44 /* Always safe when the workaround isn't needed */
45 #if !A_WORKAROUND_REQUIRE_PROPAGATION
46 # undef A_FORKSAFE
47 # define A_FORKSAFE 1
48 /* Otherwise, safe unless Makefile.PL says it's Win32 */
49 #elif !defined(A_FORKSAFE)
50 # define A_FORKSAFE 1
51 #endif
52
53 #ifndef A_MULTIPLICITY
54 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
55 #  define A_MULTIPLICITY 1
56 # else
57 #  define A_MULTIPLICITY 0
58 # endif
59 #endif
60 #if A_MULTIPLICITY && !defined(tTHX)
61 # define tTHX PerlInterpreter*
62 #endif
63
64 #if A_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))
65 # define A_THREADSAFE 1
66 # ifndef MY_CXT_CLONE
67 #  define MY_CXT_CLONE \
68     dMY_CXT_SV;                                                      \
69     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
70     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
71     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
72 # endif
73 #else
74 # define A_THREADSAFE 0
75 # undef  dMY_CXT
76 # define dMY_CXT      dNOOP
77 # undef  MY_CXT
78 # define MY_CXT       a_globaldata
79 # undef  START_MY_CXT
80 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
81 # undef  MY_CXT_INIT
82 # define MY_CXT_INIT  NOOP
83 # undef  MY_CXT_CLONE
84 # define MY_CXT_CLONE NOOP
85 #endif
86
87 /* --- Helpers ------------------------------------------------------------- */
88
89 /* ... Thread-safe hints ................................................... */
90
91 #if A_WORKAROUND_REQUIRE_PROPAGATION
92
93 typedef struct {
94  U32 bits;
95  IV  require_tag;
96 } a_hint_t;
97
98 #define A_HINT_FREE(H) PerlMemShared_free(H)
99
100 #if A_THREADSAFE
101
102 #define PTABLE_NAME        ptable_hints
103 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
104
105 #define pPTBL  pTHX
106 #define pPTBL_ pTHX_
107 #define aPTBL  aTHX
108 #define aPTBL_ aTHX_
109
110 #include "ptable.h"
111
112 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
113 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
114
115 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
116
117 typedef struct {
118  ptable *tbl;   /* It really is a ptable_hints */
119  tTHX    owner;
120 } my_cxt_t;
121
122 START_MY_CXT
123
124 STATIC SV *a_clone(pTHX_ SV *sv, tTHX owner) {
125 #define a_clone(S, O) a_clone(aTHX_ (S), (O))
126  CLONE_PARAMS  param;
127  AV           *stashes = NULL;
128  SV           *dupsv;
129
130  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
131   stashes = newAV();
132
133  param.stashes    = stashes;
134  param.flags      = 0;
135  param.proto_perl = owner;
136
137  dupsv = sv_dup(sv, &param);
138
139  if (stashes) {
140   av_undef(stashes);
141   SvREFCNT_dec(stashes);
142  }
143
144  return SvREFCNT_inc(dupsv);
145 }
146
147 STATIC void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
148  my_cxt_t *ud = ud_;
149  a_hint_t *h1 = ent->val;
150  a_hint_t *h2;
151
152  if (ud->owner == aTHX)
153   return;
154
155  h2              = PerlMemShared_malloc(sizeof *h2);
156  h2->bits        = h1->bits;
157  h2->require_tag = PTR2IV(a_clone(INT2PTR(SV *, h1->require_tag), ud->owner));
158
159  ptable_hints_store(ud->tbl, ent->key, h2);
160 }
161
162 #include "reap.h"
163
164 STATIC void a_thread_cleanup(pTHX_ void *ud) {
165  dMY_CXT;
166
167  ptable_hints_free(MY_CXT.tbl);
168 }
169
170 #endif /* A_THREADSAFE */
171
172 STATIC IV a_require_tag(pTHX) {
173 #define a_require_tag() a_require_tag(aTHX)
174  const CV *cv, *outside;
175
176  cv = PL_compcv;
177
178  if (!cv) {
179   /* If for some reason the pragma is operational at run-time, try to discover
180    * the current cv in use. */
181   const PERL_SI *si;
182
183   for (si = PL_curstackinfo; si; si = si->si_prev) {
184    I32 cxix;
185
186    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
187     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
188
189     switch (CxTYPE(cx)) {
190      case CXt_SUB:
191      case CXt_FORMAT:
192       /* The propagation workaround is only needed up to 5.10.0 and at that
193        * time format and sub contexts were still identical. And even later the
194        * cv members offsets should have been kept the same. */
195       cv = cx->blk_sub.cv;
196       goto get_enclosing_cv;
197      case CXt_EVAL:
198       cv = cx->blk_eval.cv;
199       goto get_enclosing_cv;
200      default:
201       break;
202     }
203    }
204   }
205
206   cv = PL_main_cv;
207  }
208
209 get_enclosing_cv:
210  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
211   cv = outside;
212
213  return PTR2IV(cv);
214 }
215
216 STATIC SV *a_tag(pTHX_ UV bits) {
217 #define a_tag(B) a_tag(aTHX_ (B))
218  a_hint_t *h;
219  dMY_CXT;
220
221  h              = PerlMemShared_malloc(sizeof *h);
222  h->bits        = bits;
223  h->require_tag = a_require_tag();
224
225 #if A_THREADSAFE
226  /* We only need for the key to be an unique tag for looking up the value later.
227   * Allocated memory provides convenient unique identifiers, so that's why we
228   * use the hint as the key itself. */
229  ptable_hints_store(MY_CXT.tbl, h, h);
230 #endif /* A_THREADSAFE */
231
232  return newSViv(PTR2IV(h));
233 }
234
235 STATIC UV a_detag(pTHX_ const SV *hint) {
236 #define a_detag(H) a_detag(aTHX_ (H))
237  a_hint_t *h;
238  dMY_CXT;
239
240  if (!(hint && SvIOK(hint)))
241   return 0;
242
243  h = INT2PTR(a_hint_t *, SvIVX(hint));
244 #if A_THREADSAFE
245  h = ptable_fetch(MY_CXT.tbl, h);
246 #endif /* A_THREADSAFE */
247
248  if (a_require_tag() != h->require_tag)
249   return 0;
250
251  return h->bits;
252 }
253
254 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
255
256 #define a_tag(B)   newSVuv(B)
257 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
258  * from a copy. */
259 #define a_detag(H) \
260  ((H)              \
261   ? (SvIOK(H)      \
262      ? SvUVX(H)    \
263      : (SvPOK(H)   \
264         ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
265         : 0        \
266        )           \
267      )             \
268   : 0)
269
270 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
271
272 /* Used both for hints and op flags */
273 #define A_HINT_STRICT 1
274 #define A_HINT_WARN   2
275 #define A_HINT_FETCH  4
276 #define A_HINT_STORE  8
277 #define A_HINT_EXISTS 16
278 #define A_HINT_DELETE 32
279 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
280 #define A_HINT_DO     (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
281 #define A_HINT_MASK   (A_HINT_NOTIFY|A_HINT_DO)
282
283 /* Only used in op flags */
284 #define A_HINT_ROOT   64
285 #define A_HINT_DEREF  128
286
287 STATIC U32 a_hash = 0;
288
289 STATIC UV a_hint(pTHX) {
290 #define a_hint() a_hint(aTHX)
291  SV *hint;
292 #if A_HAS_PERL(5, 9, 5)
293  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
294                                        NULL,
295                                        __PACKAGE__, __PACKAGE_LEN__,
296                                        0,
297                                        a_hash);
298 #else
299  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, a_hash);
300  if (!val)
301   return 0;
302  hint = *val;
303 #endif
304  return a_detag(hint);
305 }
306
307 /* ... op => info map ...................................................... */
308
309 typedef struct {
310  OP *(*old_pp)(pTHX);
311  UV flags;
312  void *next;
313 } a_op_info;
314
315 #define PTABLE_NAME        ptable_map
316 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
317
318 #include "ptable.h"
319
320 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
321 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
322
323 STATIC ptable *a_op_map = NULL;
324
325 #ifdef USE_ITHREADS
326
327 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
328
329 STATIC perl_mutex a_op_map_mutex;
330
331 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
332  const a_op_info *val;
333
334  MUTEX_LOCK(&a_op_map_mutex);
335
336  val = ptable_fetch(a_op_map, o);
337  if (val) {
338   *oi = *val;
339   val = oi;
340  }
341
342  MUTEX_UNLOCK(&a_op_map_mutex);
343
344  return val;
345 }
346
347 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
348
349 #else /* USE_ITHREADS */
350
351 #define dA_MAP_THX dNOOP
352
353 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
354
355 #endif /* !USE_ITHREADS */
356
357 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
358 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
359  a_op_info *oi;
360
361  if (!(oi = ptable_fetch(a_op_map, o))) {
362   oi = PerlMemShared_malloc(sizeof *oi);
363   ptable_map_store(a_op_map, o, oi);
364  }
365
366  oi->old_pp = old_pp;
367  oi->next   = next;
368  oi->flags  = flags;
369
370  return oi;
371 }
372
373 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
374 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
375
376 #ifdef USE_ITHREADS
377  MUTEX_LOCK(&a_op_map_mutex);
378 #endif
379
380  a_map_store_locked(o, old_pp, next, flags);
381
382 #ifdef USE_ITHREADS
383  MUTEX_UNLOCK(&a_op_map_mutex);
384 #endif
385 }
386
387 STATIC void a_map_delete(pTHX_ const OP *o) {
388 #define a_map_delete(O) a_map_delete(aTHX_ (O))
389 #ifdef USE_ITHREADS
390  MUTEX_LOCK(&a_op_map_mutex);
391 #endif
392
393  ptable_map_store(a_op_map, o, NULL);
394
395 #ifdef USE_ITHREADS
396  MUTEX_UNLOCK(&a_op_map_mutex);
397 #endif
398 }
399
400 STATIC const OP *a_map_descend(const OP *o) {
401  switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
402   case OA_BASEOP:
403   case OA_UNOP:
404   case OA_BINOP:
405   case OA_BASEOP_OR_UNOP:
406    return cUNOPo->op_first;
407   case OA_LIST:
408   case OA_LISTOP:
409    return cLISTOPo->op_last;
410  }
411
412  return NULL;
413 }
414
415 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
416 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
417  const a_op_info *roi;
418  a_op_info *oi;
419  const OP *o = root;
420
421 #ifdef USE_ITHREADS
422  MUTEX_LOCK(&a_op_map_mutex);
423 #endif
424
425  roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
426
427  while (o->op_flags & OPf_KIDS) {
428   o = a_map_descend(o);
429   if (!o)
430    break;
431   if ((oi = ptable_fetch(a_op_map, o))) {
432    oi->flags &= ~A_HINT_ROOT;
433    oi->next   = (a_op_info *) roi;
434    break;
435   }
436  }
437
438 #ifdef USE_ITHREADS
439  MUTEX_UNLOCK(&a_op_map_mutex);
440 #endif
441
442  return;
443 }
444
445 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
446  a_op_info *oi;
447  const OP *o = root;
448
449 #ifdef USE_ITHREADS
450  MUTEX_LOCK(&a_op_map_mutex);
451 #endif
452
453  flags &= ~A_HINT_ROOT;
454
455  do {
456   if ((oi = ptable_fetch(a_op_map, o)))
457    oi->flags = (oi->flags & A_HINT_ROOT) | flags;
458   if (!(o->op_flags & OPf_KIDS))
459    break;
460   o = a_map_descend(o);
461  } while (o);
462
463 #ifdef USE_ITHREADS
464  MUTEX_UNLOCK(&a_op_map_mutex);
465 #endif
466
467  return;
468 }
469
470 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
471
472 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
473  a_op_info *oi;
474
475 #ifdef USE_ITHREADS
476  MUTEX_LOCK(&a_op_map_mutex);
477 #endif
478
479  flags  &= ~A_HINT_ROOT;
480  rflags |=  A_HINT_ROOT;
481
482  oi = ptable_fetch(a_op_map, o);
483  while (!(oi->flags & A_HINT_ROOT)) {
484   oi->flags = flags;
485   oi        = oi->next;
486  }
487  oi->flags = rflags;
488
489 #ifdef USE_ITHREADS
490  MUTEX_UNLOCK(&a_op_map_mutex);
491 #endif
492
493  return;
494 }
495
496 /* ... Decide whether this expression should be autovivified or not ........ */
497
498 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
499  UV flags = 0, rflags;
500  const OP *root;
501  const a_op_info *roi = oi;
502
503  while (!(roi->flags & A_HINT_ROOT))
504   roi = roi->next;
505  if (!roi)
506   goto cancel;
507
508  rflags = roi->flags & ~A_HINT_ROOT;
509  if (!rflags)
510   goto cancel;
511
512  root = roi->next;
513  if (root->op_flags & OPf_MOD) {
514   if (rflags & A_HINT_STORE)
515    flags = (A_HINT_STORE|A_HINT_DEREF);
516  } else if (rflags & A_HINT_FETCH)
517    flags = (A_HINT_FETCH|A_HINT_DEREF);
518
519  if (!flags) {
520 cancel:
521   a_map_update_flags_bottomup(o, 0, 0);
522   return 0;
523  }
524
525  flags |= (rflags & A_HINT_NOTIFY);
526  a_map_update_flags_bottomup(o, flags, 0);
527
528  return oi->flags & A_HINT_ROOT ? 0 : flags;
529 }
530
531 /* ... Inspired from pp_defined() .......................................... */
532
533 STATIC int a_undef(pTHX_ SV *sv) {
534 #define a_undef(S) a_undef(aTHX_ (S))
535  switch (SvTYPE(sv)) {
536   case SVt_NULL:
537    return 1;
538   case SVt_PVAV:
539    if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
540                       || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
541     return 0;
542    break;
543   case SVt_PVHV:
544    if (HvARRAY(sv) || SvGMAGICAL(sv)
545                    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
546     return 0;
547    break;
548   default:
549    SvGETMAGIC(sv);
550    if (SvOK(sv))
551     return 0;
552  }
553
554  return 1;
555 }
556
557 /* --- PP functions -------------------------------------------------------- */
558
559 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
560  * value, another extension might have saved our pp replacement as the ppaddr
561  * for this op, so this doesn't ensure that our function will never be called
562  * again. That's why we don't remove the op info from our map, so that it can
563  * still run correctly if required. */
564
565 /* ... pp_rv2av ............................................................ */
566
567 STATIC OP *a_pp_rv2av(pTHX) {
568  dA_MAP_THX;
569  const a_op_info *oi;
570  dSP;
571
572  oi = a_map_fetch(PL_op);
573
574  if (oi->flags & A_HINT_DEREF) {
575   if (a_undef(TOPs)) {
576    /* We always need to push an empty array to fool the pp_aelem() that comes
577     * later. */
578    SV *av;
579    POPs;
580    av = sv_2mortal((SV *) newAV());
581    PUSHs(av);
582    RETURN;
583   }
584  } else {
585   PL_op->op_ppaddr = oi->old_pp;
586  }
587
588  return CALL_FPTR(oi->old_pp)(aTHX);
589 }
590
591 /* ... pp_rv2hv ............................................................ */
592
593 STATIC OP *a_pp_rv2hv_simple(pTHX) {
594  dA_MAP_THX;
595  const a_op_info *oi;
596  dSP;
597
598  oi = a_map_fetch(PL_op);
599
600  if (oi->flags & A_HINT_DEREF) {
601   if (a_undef(TOPs))
602    RETURN;
603  } else {
604   PL_op->op_ppaddr = oi->old_pp;
605  }
606
607  return CALL_FPTR(oi->old_pp)(aTHX);
608 }
609
610 STATIC OP *a_pp_rv2hv(pTHX) {
611  dA_MAP_THX;
612  const a_op_info *oi;
613  dSP;
614
615  oi = a_map_fetch(PL_op);
616
617  if (oi->flags & A_HINT_DEREF) {
618   if (a_undef(TOPs)) {
619    SV *hv;
620    POPs;
621    hv = sv_2mortal((SV *) newHV());
622    PUSHs(hv);
623    RETURN;
624   }
625  } else {
626   PL_op->op_ppaddr = oi->old_pp;
627  }
628
629  return CALL_FPTR(oi->old_pp)(aTHX);
630 }
631
632 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
633
634 STATIC OP *a_pp_deref(pTHX) {
635  dA_MAP_THX;
636  const a_op_info *oi;
637  UV flags;
638  dSP;
639
640  oi = a_map_fetch(PL_op);
641  flags = oi->flags;
642
643  if (flags & A_HINT_DEREF) {
644   OP *o;
645   U8 old_private;
646
647 deref:
648   old_private       = PL_op->op_private;
649   PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
650   o = CALL_FPTR(oi->old_pp)(aTHX);
651   PL_op->op_private = old_private;
652
653   if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
654    SPAGAIN;
655    if (a_undef(TOPs)) {
656     if (flags & A_HINT_STRICT)
657      croak("Reference vivification forbidden");
658     else if (flags & A_HINT_WARN)
659       warn("Reference was vivified");
660     else /* A_HINT_STORE */
661      croak("Can't vivify reference");
662    }
663   }
664
665   return o;
666  } else if ((flags & ~A_HINT_ROOT)
667                     && (PL_op->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
668   /* Decide if the expression must autovivify or not.
669    * This branch should be called only once by expression. */
670   flags = a_map_resolve(PL_op, oi);
671
672   /* We need the updated flags value in the deref branch. */
673   if (flags & A_HINT_DEREF)
674    goto deref;
675  }
676
677  /* This op doesn't need to skip autovivification, so restore the original
678   * state. */
679  PL_op->op_ppaddr = oi->old_pp;
680
681  return CALL_FPTR(oi->old_pp)(aTHX);
682 }
683
684 /* ... pp_root (exists,delete,keys,values) ................................. */
685
686 STATIC OP *a_pp_root_unop(pTHX) {
687  dSP;
688
689  if (a_undef(TOPs)) {
690   POPs;
691   /* Can only be reached by keys or values */
692   if (GIMME_V == G_SCALAR) {
693    dTARGET;
694    PUSHi(0);
695   }
696   RETURN;
697  }
698
699  {
700   dA_MAP_THX;
701   const a_op_info *oi = a_map_fetch(PL_op);
702   return CALL_FPTR(oi->old_pp)(aTHX);
703  }
704 }
705
706 STATIC OP *a_pp_root_binop(pTHX) {
707  dSP;
708
709  if (a_undef(TOPm1s)) {
710   POPs;
711   POPs;
712   if (PL_op->op_type == OP_EXISTS)
713    RETPUSHNO;
714   else
715    RETPUSHUNDEF;
716  }
717
718  {
719   dA_MAP_THX;
720   const a_op_info *oi = a_map_fetch(PL_op);
721   return CALL_FPTR(oi->old_pp)(aTHX);
722  }
723 }
724
725 /* --- Check functions ----------------------------------------------------- */
726
727 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
728 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
729
730  if (o->op_type == type && o->op_ppaddr != new_pp
731                         && cUNOPo->op_first->op_type != OP_GV) {
732   dA_MAP_THX;
733   const a_op_info *oi = a_map_fetch(o);
734   if (oi) {
735    a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
736    o->op_ppaddr = new_pp;
737   }
738  }
739
740  return;
741 }
742
743 /* ... ck_pad{any,sv} ...................................................... */
744
745 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
746  * function, but are instead manually mutated from a PADANY. This is why we set
747  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
748  * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
749  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
750  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
751  * globally. */
752
753 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
754
755 STATIC void a_pp_padsv_save(void) {
756  if (a_pp_padsv_saved)
757   return;
758
759  a_pp_padsv_saved    = PL_ppaddr[OP_PADSV];
760  PL_ppaddr[OP_PADSV] = a_pp_deref;
761 }
762
763 STATIC void a_pp_padsv_restore(OP *o) {
764  if (!a_pp_padsv_saved)
765   return;
766
767  if (o->op_ppaddr == a_pp_deref)
768   o->op_ppaddr = a_pp_padsv_saved;
769
770  PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
771  a_pp_padsv_saved    = 0;
772 }
773
774 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
775
776 STATIC OP *a_ck_padany(pTHX_ OP *o) {
777  UV hint;
778
779  a_pp_padsv_restore(o);
780
781  o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
782
783  hint = a_hint();
784  if (hint & A_HINT_DO) {
785   a_pp_padsv_save();
786   a_map_store_root(o, a_pp_padsv_saved, hint);
787  } else
788   a_map_delete(o);
789
790  return o;
791 }
792
793 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
794
795 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
796  UV hint;
797
798  a_pp_padsv_restore(o);
799
800  o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
801
802  hint = a_hint();
803  if (hint & A_HINT_DO) {
804   a_map_store_root(o, o->op_ppaddr, hint);
805   o->op_ppaddr = a_pp_deref;
806  } else
807   a_map_delete(o);
808
809  return o;
810 }
811
812 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
813
814 /* Those ops appear both at the root and inside an expression but there's no
815  * way to distinguish both situations. Worse, we can't even know if we are in a
816  * modifying context, so the expression can't be resolved yet. It will be at the
817  * first invocation of a_pp_deref() for this expression. */
818
819 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
820 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
821 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
822
823 STATIC OP *a_ck_deref(pTHX_ OP *o) {
824  OP * (*old_ck)(pTHX_ OP *o) = 0;
825  UV hint = a_hint();
826
827  switch (o->op_type) {
828   case OP_AELEM:
829    old_ck = a_old_ck_aelem;
830    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
831     a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
832    break;
833   case OP_HELEM:
834    old_ck = a_old_ck_helem;
835    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
836     a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
837    break;
838   case OP_RV2SV:
839    old_ck = a_old_ck_rv2sv;
840    break;
841  }
842  o = CALL_FPTR(old_ck)(aTHX_ o);
843
844  if (hint & A_HINT_DO) {
845   a_map_store_root(o, o->op_ppaddr, hint);
846   o->op_ppaddr = a_pp_deref;
847  } else
848   a_map_delete(o);
849
850  return o;
851 }
852
853 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
854
855 /* Those ops also appear both inisde and at the root, hence the caveats for
856  * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
857  * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
858  * expression. */
859
860 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
861 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
862
863 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
864  OP * (*old_ck)(pTHX_ OP *o) = 0;
865  OP * (*new_pp)(pTHX)        = 0;
866  UV hint;
867
868  switch (o->op_type) {
869   case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
870   case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
871  }
872  o = CALL_FPTR(old_ck)(aTHX_ o);
873
874  if (cUNOPo->op_first->op_type == OP_GV)
875   return o;
876
877  hint = a_hint();
878  if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
879   a_map_store_root(o, o->op_ppaddr, hint);
880   o->op_ppaddr = new_pp;
881  } else
882   a_map_delete(o);
883
884  return o;
885 }
886
887 /* ... ck_xslice (aslice,hslice) ........................................... */
888
889 /* I think those are only found at the root, but there's nothing that really
890  * prevent them to be inside the expression too. We only need to update the
891  * root so that the rest of the expression will see the right context when
892  * resolving. That's why we don't replace the ppaddr. */
893
894 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
895 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
896
897 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
898  OP * (*old_ck)(pTHX_ OP *o) = 0;
899  UV hint = a_hint();
900
901  switch (o->op_type) {
902   case OP_ASLICE:
903    old_ck = a_old_ck_aslice;
904    break;
905   case OP_HSLICE:
906    old_ck = a_old_ck_hslice;
907    if (hint & A_HINT_DO)
908     a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
909    break;
910  }
911  o = CALL_FPTR(old_ck)(aTHX_ o);
912
913  if (hint & A_HINT_DO) {
914   a_map_store_root(o, 0, hint);
915  } else
916   a_map_delete(o);
917
918  return o;
919 }
920
921 /* ... ck_root (exists,delete,keys,values) ................................. */
922
923 /* Those ops are only found at the root of a dereferencing expression. We can
924  * then resolve at compile time if vivification must take place or not. */
925
926 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
927 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
928 STATIC OP *(*a_old_ck_keys)  (pTHX_ OP *) = 0;
929 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
930
931 STATIC OP *a_ck_root(pTHX_ OP *o) {
932  OP * (*old_ck)(pTHX_ OP *o) = 0;
933  OP * (*new_pp)(pTHX)        = 0;
934  bool enabled = FALSE;
935  UV hint = a_hint();
936
937  switch (o->op_type) {
938   case OP_EXISTS:
939    old_ck  = a_old_ck_exists;
940    new_pp  = a_pp_root_binop;
941    enabled = hint & A_HINT_EXISTS;
942    break;
943   case OP_DELETE:
944    old_ck  = a_old_ck_delete;
945    new_pp  = a_pp_root_binop;
946    enabled = hint & A_HINT_DELETE;
947    break;
948   case OP_KEYS:
949    old_ck  = a_old_ck_keys;
950    new_pp  = a_pp_root_unop;
951    enabled = hint & A_HINT_FETCH;
952    break;
953   case OP_VALUES:
954    old_ck  = a_old_ck_values;
955    new_pp  = a_pp_root_unop;
956    enabled = hint & A_HINT_FETCH;
957    break;
958  }
959  o = CALL_FPTR(old_ck)(aTHX_ o);
960
961  if (hint & A_HINT_DO) {
962   if (enabled) {
963    a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
964    a_map_store_root(o, o->op_ppaddr, hint);
965    o->op_ppaddr = new_pp;
966   } else {
967    a_map_cancel(o);
968   }
969  } else
970   a_map_delete(o);
971
972  return o;
973 }
974
975 STATIC U32 a_initialized = 0;
976
977 STATIC void a_teardown(pTHX_ void *root) {
978
979  if (!a_initialized)
980   return;
981
982 #if A_MULTIPLICITY
983  if (aTHX != root)
984   return;
985 #endif
986
987 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
988  {
989   dMY_CXT;
990   ptable_hints_free(MY_CXT.tbl);
991  }
992 #endif
993
994  PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_old_ck_padany);
995  a_old_ck_padany     = 0;
996  PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_old_ck_padsv);
997  a_old_ck_padsv      = 0;
998
999  PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_old_ck_aelem);
1000  a_old_ck_aelem      = 0;
1001  PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_old_ck_helem);
1002  a_old_ck_helem      = 0;
1003  PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_old_ck_rv2sv);
1004  a_old_ck_rv2sv      = 0;
1005
1006  PL_check[OP_RV2AV]  = MEMBER_TO_FPTR(a_old_ck_rv2av);
1007  a_old_ck_rv2av      = 0;
1008  PL_check[OP_RV2HV]  = MEMBER_TO_FPTR(a_old_ck_rv2hv);
1009  a_old_ck_rv2hv      = 0;
1010
1011  PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_old_ck_aslice);
1012  a_old_ck_aslice     = 0;
1013  PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_old_ck_hslice);
1014  a_old_ck_hslice     = 0;
1015
1016  PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_old_ck_exists);
1017  a_old_ck_exists     = 0;
1018  PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_old_ck_delete);
1019  a_old_ck_delete     = 0;
1020  PL_check[OP_KEYS]   = MEMBER_TO_FPTR(a_old_ck_keys);
1021  a_old_ck_keys       = 0;
1022  PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_old_ck_values);
1023  a_old_ck_values     = 0;
1024
1025  if (a_pp_padsv_saved) {
1026   PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
1027   a_pp_padsv_saved    = 0;
1028  }
1029
1030  a_initialized = 0;
1031 }
1032
1033 STATIC void a_setup(pTHX) {
1034 #define a_setup() a_setup(aTHX)
1035  if (a_initialized)
1036   return;
1037
1038 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1039  {
1040   MY_CXT_INIT;
1041   MY_CXT.tbl   = ptable_new();
1042   MY_CXT.owner = aTHX;
1043  }
1044 #endif
1045
1046  a_old_ck_padany     = PL_check[OP_PADANY];
1047  PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
1048  a_old_ck_padsv      = PL_check[OP_PADSV];
1049  PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_ck_padsv);
1050
1051  a_old_ck_aelem      = PL_check[OP_AELEM];
1052  PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_ck_deref);
1053  a_old_ck_helem      = PL_check[OP_HELEM];
1054  PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_ck_deref);
1055  a_old_ck_rv2sv      = PL_check[OP_RV2SV];
1056  PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_ck_deref);
1057
1058  a_old_ck_rv2av      = PL_check[OP_RV2AV];
1059  PL_check[OP_RV2AV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
1060  a_old_ck_rv2hv      = PL_check[OP_RV2HV];
1061  PL_check[OP_RV2HV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
1062
1063  a_old_ck_aslice     = PL_check[OP_ASLICE];
1064  PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1065  a_old_ck_hslice     = PL_check[OP_HSLICE];
1066  PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1067
1068  a_old_ck_exists     = PL_check[OP_EXISTS];
1069  PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
1070  a_old_ck_delete     = PL_check[OP_DELETE];
1071  PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
1072  a_old_ck_keys       = PL_check[OP_KEYS];
1073  PL_check[OP_KEYS]   = MEMBER_TO_FPTR(a_ck_root);
1074  a_old_ck_values     = PL_check[OP_VALUES];
1075  PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
1076
1077 #if A_MULTIPLICITY
1078  call_atexit(a_teardown, aTHX);
1079 #else
1080  call_atexit(a_teardown, NULL);
1081 #endif
1082
1083  a_initialized = 1;
1084 }
1085
1086 STATIC U32 a_booted = 0;
1087
1088 /* --- XS ------------------------------------------------------------------ */
1089
1090 MODULE = autovivification      PACKAGE = autovivification
1091
1092 PROTOTYPES: ENABLE
1093
1094 BOOT: 
1095 {                                    
1096  if (!a_booted++) {
1097   HV *stash;
1098
1099   a_op_map = ptable_new();
1100 #ifdef USE_ITHREADS
1101   MUTEX_INIT(&a_op_map_mutex);
1102 #endif
1103
1104   PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1105
1106   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1107   newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1108   newCONSTSUB(stash, "A_HINT_WARN",   newSVuv(A_HINT_WARN));
1109   newCONSTSUB(stash, "A_HINT_FETCH",  newSVuv(A_HINT_FETCH));
1110   newCONSTSUB(stash, "A_HINT_STORE",  newSVuv(A_HINT_STORE));
1111   newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1112   newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1113   newCONSTSUB(stash, "A_HINT_MASK",   newSVuv(A_HINT_MASK));
1114   newCONSTSUB(stash, "A_THREADSAFE",  newSVuv(A_THREADSAFE));
1115   newCONSTSUB(stash, "A_FORKSAFE",    newSVuv(A_FORKSAFE));
1116  }
1117
1118  a_setup();
1119 }
1120
1121 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1122
1123 void
1124 CLONE(...)
1125 PROTOTYPE: DISABLE
1126 PREINIT:
1127  ptable *t;
1128 PPCODE:
1129  {
1130   my_cxt_t ud;
1131   dMY_CXT;
1132   ud.tbl   = t = ptable_new();
1133   ud.owner = MY_CXT.owner;
1134   ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1135  }
1136  {
1137   MY_CXT_CLONE;
1138   MY_CXT.tbl   = t;
1139   MY_CXT.owner = aTHX;
1140  }
1141  reap(3, a_thread_cleanup, NULL);
1142  XSRETURN(0);
1143
1144 #endif
1145
1146 SV *
1147 _tag(SV *hint)
1148 PROTOTYPE: $
1149 CODE:
1150  RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1151 OUTPUT:
1152  RETVAL
1153
1154 SV *
1155 _detag(SV *tag)
1156 PROTOTYPE: $
1157 CODE:
1158  if (!SvOK(tag))
1159   XSRETURN_UNDEF;
1160  RETVAL = newSVuv(a_detag(tag));
1161 OUTPUT:
1162  RETVAL