]> git.vpit.fr Git - perl/modules/autovivification.git/blob - autovivification.xs
Factor the rv2xv recheck logic
[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 #define A_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #ifndef A_WORKAROUND_REQUIRE_PROPAGATION
17 # define A_WORKAROUND_REQUIRE_PROPAGATION !A_HAS_PERL(5, 10, 1)
18 #endif
19
20 /* --- Helpers ------------------------------------------------------------- */
21
22 #if A_WORKAROUND_REQUIRE_PROPAGATION
23
24 #define A_ENCODE_UV(B, U)   \
25  len = 0;                   \
26  while (len < sizeof(UV)) { \
27   (B)[len++] = (U) & 0xFF;  \
28   (U) >>= 8;                \
29  }
30
31 #define A_DECODE_UV(U, B)        \
32  len = sizeof(UV);               \
33  while (len > 0)                 \
34   (U) = ((U) << 8) | (B)[--len];
35
36 STATIC SV *a_tag(pTHX_ UV bits) {
37 #define a_tag(B) a_tag(aTHX_ (B))
38  SV            *hint;
39  const PERL_SI *si;
40  UV             requires = 0;
41  unsigned char  buf[sizeof(UV) * 2];
42  STRLEN         len;
43
44  for (si = PL_curstackinfo; si; si = si->si_prev) {
45   I32 cxix;
46
47   for (cxix = si->si_cxix; cxix >= 0; --cxix) {
48    const PERL_CONTEXT *cx = si->si_cxstack + cxix;
49
50    if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
51     ++requires;
52   }
53  }
54
55  A_ENCODE_UV(buf,              requires);
56  A_ENCODE_UV(buf + sizeof(UV), bits);
57  hint = newSVpvn(buf, sizeof buf);
58  SvREADONLY_on(hint);
59
60  return hint;
61 }
62
63 STATIC UV a_detag(pTHX_ const SV *hint) {
64 #define a_detag(H) a_detag(aTHX_ (H))
65  const PERL_SI *si;
66  UV             requires = 0, requires_max = 0, bits = 0;
67  unsigned char *buf;
68  STRLEN         len;
69
70  if (!(hint && SvOK(hint)))
71   return 0;
72
73  buf = SvPVX(hint);
74  A_DECODE_UV(requires_max, buf);
75
76  for (si = PL_curstackinfo; si; si = si->si_prev) {
77   I32 cxix;
78
79   for (cxix = si->si_cxix; cxix >= 0; --cxix) {
80    const PERL_CONTEXT *cx = si->si_cxstack + cxix;
81
82    if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
83                               && ++requires > requires_max)
84     return 0;
85   }
86  }
87
88  A_DECODE_UV(bits, buf + sizeof(UV));
89
90  return bits;
91 }
92
93 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
94
95 #define a_tag(B)   newSVuv(B)
96 #define a_detag(H) (((H) && SvOK(H)) ? SvUVX(H) : 0)
97
98 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
99
100 /* Used both for hints and op flags */
101 #define A_HINT_STRICT 1
102 #define A_HINT_WARN   2
103 #define A_HINT_FETCH  4
104 #define A_HINT_STORE  8
105 #define A_HINT_EXISTS 16
106 #define A_HINT_DELETE 32
107 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
108 #define A_HINT_DO     (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
109 #define A_HINT_MASK   (A_HINT_NOTIFY|A_HINT_DO)
110
111 /* Only used in op flags */
112 #define A_HINT_ROOT   64
113 #define A_HINT_DEREF  128
114
115 STATIC U32 a_hash = 0;
116
117 STATIC UV a_hint(pTHX) {
118 #define a_hint() a_hint(aTHX)
119  SV *hint;
120 #if A_HAS_PERL(5, 9, 5)
121  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
122                                        NULL,
123                                        __PACKAGE__, __PACKAGE_LEN__,
124                                        0,
125                                        a_hash);
126 #else
127  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, a_hash);
128  if (!val)
129   return 0;
130  hint = *val;
131 #endif
132  return a_detag(hint);
133 }
134
135 /* ... op => info map ...................................................... */
136
137 typedef struct {
138  OP *(*old_pp)(pTHX);
139  UV flags;
140  void *next;
141 } a_op_info;
142
143 #define PTABLE_NAME        ptable_map
144 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
145
146 #include "ptable.h"
147
148 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
149 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
150
151 STATIC ptable *a_op_map = NULL;
152
153 #ifdef USE_ITHREADS
154 STATIC perl_mutex a_op_map_mutex;
155 #endif
156
157 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
158  const a_op_info *val;
159
160 #ifdef USE_ITHREADS
161  MUTEX_LOCK(&a_op_map_mutex);
162 #endif
163
164  val = ptable_fetch(a_op_map, o);
165  if (val) {
166   *oi = *val;
167   val = oi;
168  }
169
170 #ifdef USE_ITHREADS
171  MUTEX_UNLOCK(&a_op_map_mutex);
172 #endif
173
174  return val;
175 }
176
177 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
178 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
179  a_op_info *oi;
180
181  if (!(oi = ptable_fetch(a_op_map, o))) {
182   oi = PerlMemShared_malloc(sizeof *oi);
183   ptable_map_store(a_op_map, o, oi);
184  }
185
186  oi->old_pp = old_pp;
187  oi->next   = next;
188  oi->flags  = flags;
189
190  return oi;
191 }
192
193 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
194 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
195
196 #ifdef USE_ITHREADS
197  MUTEX_LOCK(&a_op_map_mutex);
198 #endif
199
200  a_map_store_locked(o, old_pp, next, flags);
201
202 #ifdef USE_ITHREADS
203  MUTEX_UNLOCK(&a_op_map_mutex);
204 #endif
205 }
206
207 STATIC void a_map_delete(pTHX_ const OP *o) {
208 #define a_map_delete(O) a_map_delete(aTHX_ (O))
209 #ifdef USE_ITHREADS
210  MUTEX_LOCK(&a_op_map_mutex);
211 #endif
212
213  ptable_map_store(a_op_map, o, NULL);
214
215 #ifdef USE_ITHREADS
216  MUTEX_UNLOCK(&a_op_map_mutex);
217 #endif
218 }
219
220 STATIC const OP *a_map_descend(const OP *o) {
221  switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
222   case OA_BASEOP:
223   case OA_UNOP:
224   case OA_BINOP:
225   case OA_BASEOP_OR_UNOP:
226    return cUNOPo->op_first;
227   case OA_LIST:
228   case OA_LISTOP:
229    return cLISTOPo->op_last;
230  }
231
232  return NULL;
233 }
234
235 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
236 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
237  const a_op_info *roi;
238  a_op_info *oi;
239  const OP *o = root;
240
241 #ifdef USE_ITHREADS
242  MUTEX_LOCK(&a_op_map_mutex);
243 #endif
244
245  roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
246
247  while (o->op_flags & OPf_KIDS) {
248   o = a_map_descend(o);
249   if (!o)
250    break;
251   if ((oi = ptable_fetch(a_op_map, o))) {
252    oi->flags &= ~A_HINT_ROOT;
253    oi->next   = (a_op_info *) roi;
254    break;
255   }
256  }
257
258 #ifdef USE_ITHREADS
259  MUTEX_UNLOCK(&a_op_map_mutex);
260 #endif
261
262  return;
263 }
264
265 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
266  a_op_info *oi;
267  const OP *o = root;
268
269 #ifdef USE_ITHREADS
270  MUTEX_LOCK(&a_op_map_mutex);
271 #endif
272
273  flags &= ~A_HINT_ROOT;
274
275  do {
276   if ((oi = ptable_fetch(a_op_map, o)))
277    oi->flags = (oi->flags & A_HINT_ROOT) | flags;
278   if (!(o->op_flags & OPf_KIDS))
279    break;
280   o = a_map_descend(o);
281  } while (o);
282
283 #ifdef USE_ITHREADS
284  MUTEX_UNLOCK(&a_op_map_mutex);
285 #endif
286
287  return;
288 }
289
290 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
291
292 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
293  a_op_info *oi;
294
295 #ifdef USE_ITHREADS
296  MUTEX_LOCK(&a_op_map_mutex);
297 #endif
298
299  flags  &= ~A_HINT_ROOT;
300  rflags |=  A_HINT_ROOT;
301
302  oi = ptable_fetch(a_op_map, o);
303  while (!(oi->flags & A_HINT_ROOT)) {
304   oi->flags = flags;
305   oi        = oi->next;
306  }
307  oi->flags = rflags;
308
309 #ifdef USE_ITHREADS
310  MUTEX_UNLOCK(&a_op_map_mutex);
311 #endif
312
313  return;
314 }
315
316 /* ... Decide whether this expression should be autovivified or not ........ */
317
318 STATIC UV a_map_resolve(const OP *o, a_op_info *oi) {
319  UV flags = 0, rflags;
320  const OP *root;
321  a_op_info *roi = oi;
322
323  while (!(roi->flags & A_HINT_ROOT))
324   roi = roi->next;
325  if (!roi)
326   goto cancel;
327
328  rflags = roi->flags & ~A_HINT_ROOT;
329  if (!rflags)
330   goto cancel;
331
332  root = roi->next;
333  if (root->op_flags & OPf_MOD) {
334   if (rflags & A_HINT_STORE)
335    flags = (A_HINT_STORE|A_HINT_DEREF);
336  } else if (rflags & A_HINT_FETCH)
337    flags = (A_HINT_FETCH|A_HINT_DEREF);
338
339  if (!flags) {
340 cancel:
341   a_map_update_flags_bottomup(o, 0, 0);
342   return 0;
343  }
344
345  flags |= (rflags & A_HINT_NOTIFY);
346  a_map_update_flags_bottomup(o, flags, 0);
347
348  return oi->flags & A_HINT_ROOT ? 0 : flags;
349 }
350
351 /* ... Lightweight pp_defined() ............................................ */
352
353 STATIC bool a_defined(pTHX_ SV *sv) {
354 #define a_defined(S) a_defined(aTHX_ (S))
355  bool defined = FALSE;
356
357  switch (SvTYPE(sv)) {
358   case SVt_PVAV:
359    if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
360                       || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
361     defined = TRUE;
362    break;
363   case SVt_PVHV:
364    if (HvARRAY(sv) || SvGMAGICAL(sv)
365                    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
366     defined = TRUE;
367    break;
368   default:
369    defined = SvOK(sv);
370  }
371
372  return defined;
373 }
374
375 /* --- PP functions -------------------------------------------------------- */
376
377 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
378  * value, another extension might have saved our pp replacement as the ppaddr
379  * for this op, so this doesn't ensure that our function will never be called
380  * again. That's why we don't remove the op info from our map, so that it can
381  * still run correctly if required. */
382
383 /* ... pp_rv2av ............................................................ */
384
385 STATIC OP *a_pp_rv2av(pTHX) {
386  a_op_info oi;
387  UV flags;
388  dSP;
389
390  a_map_fetch(PL_op, &oi);
391  flags = oi.flags;
392
393  if (flags & A_HINT_DEREF) {
394   if (!SvOK(TOPs)) {
395    /* We always need to push an empty array to fool the pp_aelem() that comes
396     * later. */
397    SV *av;
398    POPs;
399    av = sv_2mortal((SV *) newAV());
400    PUSHs(av);
401    RETURN;
402   }
403  } else {
404   PL_op->op_ppaddr = oi.old_pp;
405  }
406
407  return CALL_FPTR(oi.old_pp)(aTHX);
408 }
409
410 /* ... pp_rv2hv ............................................................ */
411
412 STATIC OP *a_pp_rv2hv(pTHX) {
413  a_op_info oi;
414  UV flags;
415  dSP;
416
417  a_map_fetch(PL_op, &oi);
418  flags = oi.flags;
419
420  if (flags & A_HINT_DEREF) {
421   if (!SvOK(TOPs))
422    RETURN;
423  } else {
424   PL_op->op_ppaddr = oi.old_pp;
425  }
426
427  return CALL_FPTR(oi.old_pp)(aTHX);
428 }
429
430 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
431
432 STATIC OP *a_pp_deref(pTHX) {
433  a_op_info oi;
434  UV flags;
435  dSP;
436
437  a_map_fetch(PL_op, &oi);
438  flags = oi.flags;
439
440  if (flags & A_HINT_DEREF) {
441   OP *o;
442   U8 old_private;
443
444 deref:
445   old_private       = PL_op->op_private;
446   PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
447   o = CALL_FPTR(oi.old_pp)(aTHX);
448   PL_op->op_private = old_private;
449
450   if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
451    SPAGAIN;
452    if (!SvOK(TOPs)) {
453     if (flags & A_HINT_STRICT)
454      croak("Reference vivification forbidden");
455     else if (flags & A_HINT_WARN)
456       warn("Reference was vivified");
457     else /* A_HINT_STORE */
458      croak("Can't vivify reference");
459    }
460   }
461
462   return o;
463  } else if ((flags & ~A_HINT_ROOT)
464                     && (PL_op->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
465   /* Decide if the expression must autovivify or not.
466    * This branch should be called only once by expression. */
467   flags = a_map_resolve(PL_op, &oi);
468
469   /* We need the updated flags value in the deref branch. */
470   if (flags & A_HINT_DEREF)
471    goto deref;
472  }
473
474  /* This op doesn't need to skip autovivification, so restore the original
475   * state. */
476  PL_op->op_ppaddr = oi.old_pp;
477
478  return CALL_FPTR(oi.old_pp)(aTHX);
479 }
480
481 /* ... pp_root (exists,delete,keys,values) ................................. */
482
483 STATIC OP *a_pp_root_unop(pTHX) {
484  a_op_info oi;
485  dSP;
486
487  if (!a_defined(TOPs)) {
488   POPs;
489   /* Can only be reached by keys or values */
490   if (GIMME_V == G_SCALAR) {
491    dTARGET;
492    PUSHi(0);
493   }
494   RETURN;
495  }
496
497  a_map_fetch(PL_op, &oi);
498
499  return CALL_FPTR(oi.old_pp)(aTHX);
500 }
501
502 STATIC OP *a_pp_root_binop(pTHX) {
503  a_op_info oi;
504  dSP;
505
506  if (!a_defined(TOPm1s)) {
507   POPs;
508   POPs;
509   if (PL_op->op_type == OP_EXISTS)
510    RETPUSHNO;
511   else
512    RETPUSHUNDEF;
513  }
514
515  a_map_fetch(PL_op, &oi);
516
517  return CALL_FPTR(oi.old_pp)(aTHX);
518 }
519
520 /* --- Check functions ----------------------------------------------------- */
521
522 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
523 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
524  a_op_info oi;
525
526  if (o->op_type == type && o->op_ppaddr != new_pp
527                         && cUNOPo->op_first->op_type != OP_GV
528                         && a_map_fetch(o, &oi)) {
529   a_map_store(o, o->op_ppaddr, oi.next, oi.flags);
530   o->op_ppaddr = new_pp;
531  }
532
533  return;
534 }
535
536 /* ... ck_pad{any,sv} ...................................................... */
537
538 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
539  * function, but are instead manually mutated from a PADANY. This is why we set
540  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
541  * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
542  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
543  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
544  * globally. */
545
546 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
547
548 STATIC void a_pp_padsv_save(void) {
549  if (a_pp_padsv_saved)
550   return;
551
552  a_pp_padsv_saved    = PL_ppaddr[OP_PADSV];
553  PL_ppaddr[OP_PADSV] = a_pp_deref;
554 }
555
556 STATIC void a_pp_padsv_restore(OP *o) {
557  if (!a_pp_padsv_saved)
558   return;
559
560  if (o->op_ppaddr == a_pp_deref)
561   o->op_ppaddr = a_pp_padsv_saved;
562
563  PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
564  a_pp_padsv_saved    = 0;
565 }
566
567 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
568
569 STATIC OP *a_ck_padany(pTHX_ OP *o) {
570  UV hint;
571
572  a_pp_padsv_restore(o);
573
574  o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
575
576  hint = a_hint();
577  if (hint & A_HINT_DO) {
578   a_pp_padsv_save();
579   a_map_store_root(o, a_pp_padsv_saved, hint);
580  } else
581   a_map_delete(o);
582
583  return o;
584 }
585
586 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
587
588 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
589  UV hint;
590
591  a_pp_padsv_restore(o);
592
593  o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
594
595  hint = a_hint();
596  if (hint & A_HINT_DO) {
597   a_map_store_root(o, o->op_ppaddr, hint);
598   o->op_ppaddr = a_pp_deref;
599  } else
600   a_map_delete(o);
601
602  return o;
603 }
604
605 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
606
607 /* Those ops appear both at the root and inside an expression but there's no
608  * way to distinguish both situations. Worse, we can't even know if we are in a
609  * modifying context, so the expression can't be resolved yet. It will be at the
610  * first invocation of a_pp_deref() for this expression. */
611
612 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
613 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
614 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
615
616 STATIC OP *a_ck_deref(pTHX_ OP *o) {
617  OP * (*old_ck)(pTHX_ OP *o) = 0;
618  UV hint = a_hint();
619
620  switch (o->op_type) {
621   case OP_AELEM:
622    old_ck = a_old_ck_aelem;
623    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
624     a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
625    break;
626   case OP_HELEM:
627    old_ck = a_old_ck_helem;
628    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
629     a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv);
630    break;
631   case OP_RV2SV:
632    old_ck = a_old_ck_rv2sv;
633    break;
634  }
635  o = CALL_FPTR(old_ck)(aTHX_ o);
636
637  if (hint & A_HINT_DO) {
638   a_map_store_root(o, o->op_ppaddr, hint);
639   o->op_ppaddr = a_pp_deref;
640  } else
641   a_map_delete(o);
642
643  return o;
644 }
645
646 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
647
648 /* Those ops also appear both inisde and at the root, hence the caveats for
649  * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
650  * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
651  * expression. */
652
653 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
654 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
655
656 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
657  OP * (*old_ck)(pTHX_ OP *o) = 0;
658  OP * (*new_pp)(pTHX)        = 0;
659  UV hint;
660
661  switch (o->op_type) {
662   case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
663   case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv; break;
664  }
665  o = CALL_FPTR(old_ck)(aTHX_ o);
666
667  if (cUNOPo->op_first->op_type == OP_GV)
668   return o;
669
670  hint = a_hint();
671  if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
672   a_map_store_root(o, o->op_ppaddr, hint);
673   o->op_ppaddr = new_pp;
674  } else
675   a_map_delete(o);
676
677  return o;
678 }
679
680 /* ... ck_root (exists,delete,keys,values) ................................. */
681
682 /* Those ops are only found at the root of a dereferencing expression. We can
683  * then resolve at compile time if vivification must take place or not. */
684
685 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
686 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
687 STATIC OP *(*a_old_ck_keys)  (pTHX_ OP *) = 0;
688 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
689
690 STATIC OP *a_ck_root(pTHX_ OP *o) {
691  OP * (*old_ck)(pTHX_ OP *o) = 0;
692  OP * (*new_pp)(pTHX)        = 0;
693  bool enabled = FALSE;
694  UV hint = a_hint();
695
696  switch (o->op_type) {
697   case OP_EXISTS:
698    old_ck  = a_old_ck_exists;
699    new_pp  = a_pp_root_binop;
700    enabled = hint & A_HINT_EXISTS;
701    break;
702   case OP_DELETE:
703    old_ck  = a_old_ck_delete;
704    new_pp  = a_pp_root_binop;
705    enabled = hint & A_HINT_DELETE;
706    break;
707   case OP_KEYS:
708    old_ck  = a_old_ck_keys;
709    new_pp  = a_pp_root_unop;
710    enabled = hint & A_HINT_FETCH;
711    break;
712   case OP_VALUES:
713    old_ck  = a_old_ck_values;
714    new_pp  = a_pp_root_unop;
715    enabled = hint & A_HINT_FETCH;
716    break;
717  }
718  o = CALL_FPTR(old_ck)(aTHX_ o);
719
720  if (hint & A_HINT_DO) {
721   if (enabled) {
722    a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
723    a_map_store_root(o, o->op_ppaddr, hint);
724    o->op_ppaddr = new_pp;
725   } else {
726    a_map_cancel(o);
727   }
728  } else
729   a_map_delete(o);
730
731  return o;
732 }
733
734 STATIC U32 a_initialized = 0;
735
736 /* --- XS ------------------------------------------------------------------ */
737
738 MODULE = autovivification      PACKAGE = autovivification
739
740 PROTOTYPES: ENABLE
741
742 BOOT: 
743 {                                    
744  if (!a_initialized++) {
745   HV *stash;
746
747   a_op_map = ptable_new();
748 #ifdef USE_ITHREADS
749   MUTEX_INIT(&a_op_map_mutex);
750 #endif
751
752   PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
753
754   a_old_ck_padany     = PL_check[OP_PADANY];
755   PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
756   a_old_ck_padsv      = PL_check[OP_PADSV];
757   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_ck_padsv);
758
759   a_old_ck_aelem      = PL_check[OP_AELEM];
760   PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_ck_deref);
761   a_old_ck_helem      = PL_check[OP_HELEM];
762   PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_ck_deref);
763   a_old_ck_rv2sv      = PL_check[OP_RV2SV];
764   PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_ck_deref);
765
766   a_old_ck_rv2av      = PL_check[OP_RV2AV];
767   PL_check[OP_RV2AV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
768   a_old_ck_rv2hv      = PL_check[OP_RV2HV];
769   PL_check[OP_RV2HV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
770
771   a_old_ck_exists     = PL_check[OP_EXISTS];
772   PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
773   a_old_ck_delete     = PL_check[OP_DELETE];
774   PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
775   a_old_ck_keys       = PL_check[OP_KEYS];
776   PL_check[OP_KEYS]   = MEMBER_TO_FPTR(a_ck_root);
777   a_old_ck_values     = PL_check[OP_VALUES];
778   PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
779
780   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
781   newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
782   newCONSTSUB(stash, "A_HINT_WARN",   newSVuv(A_HINT_WARN));
783   newCONSTSUB(stash, "A_HINT_FETCH",  newSVuv(A_HINT_FETCH));
784   newCONSTSUB(stash, "A_HINT_STORE",  newSVuv(A_HINT_STORE));
785   newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
786   newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
787   newCONSTSUB(stash, "A_HINT_MASK",   newSVuv(A_HINT_MASK));
788  }
789 }
790
791 SV *
792 _tag(SV *hint)
793 PROTOTYPE: $
794 CODE:
795  RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
796 OUTPUT:
797  RETVAL
798
799 SV *
800 _detag(SV *tag)
801 PROTOTYPE: $
802 CODE:
803  if (!SvOK(tag))
804   XSRETURN_UNDEF;
805  RETVAL = newSVuv(a_detag(tag));
806 OUTPUT:
807  RETVAL