]> git.vpit.fr Git - perl/modules/autovivification.git/blob - autovivification.xs
Handle array and hash slices
[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_simple(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 STATIC OP *a_pp_rv2hv(pTHX) {
431  a_op_info oi;
432  UV flags;
433  dSP;
434
435  a_map_fetch(PL_op, &oi);
436  flags = oi.flags;
437
438  if (flags & A_HINT_DEREF) {
439   if (!SvOK(TOPs)) {
440    SV *hv;
441    POPs;
442    hv = sv_2mortal((SV *) newHV());
443    PUSHs(hv);
444    RETURN;
445   }
446  } else {
447   PL_op->op_ppaddr = oi.old_pp;
448  }
449
450  return CALL_FPTR(oi.old_pp)(aTHX);
451 }
452
453 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
454
455 STATIC OP *a_pp_deref(pTHX) {
456  a_op_info oi;
457  UV flags;
458  dSP;
459
460  a_map_fetch(PL_op, &oi);
461  flags = oi.flags;
462
463  if (flags & A_HINT_DEREF) {
464   OP *o;
465   U8 old_private;
466
467 deref:
468   old_private       = PL_op->op_private;
469   PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
470   o = CALL_FPTR(oi.old_pp)(aTHX);
471   PL_op->op_private = old_private;
472
473   if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
474    SPAGAIN;
475    if (!SvOK(TOPs)) {
476     if (flags & A_HINT_STRICT)
477      croak("Reference vivification forbidden");
478     else if (flags & A_HINT_WARN)
479       warn("Reference was vivified");
480     else /* A_HINT_STORE */
481      croak("Can't vivify reference");
482    }
483   }
484
485   return o;
486  } else if ((flags & ~A_HINT_ROOT)
487                     && (PL_op->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
488   /* Decide if the expression must autovivify or not.
489    * This branch should be called only once by expression. */
490   flags = a_map_resolve(PL_op, &oi);
491
492   /* We need the updated flags value in the deref branch. */
493   if (flags & A_HINT_DEREF)
494    goto deref;
495  }
496
497  /* This op doesn't need to skip autovivification, so restore the original
498   * state. */
499  PL_op->op_ppaddr = oi.old_pp;
500
501  return CALL_FPTR(oi.old_pp)(aTHX);
502 }
503
504 /* ... pp_root (exists,delete,keys,values) ................................. */
505
506 STATIC OP *a_pp_root_unop(pTHX) {
507  a_op_info oi;
508  dSP;
509
510  if (!a_defined(TOPs)) {
511   POPs;
512   /* Can only be reached by keys or values */
513   if (GIMME_V == G_SCALAR) {
514    dTARGET;
515    PUSHi(0);
516   }
517   RETURN;
518  }
519
520  a_map_fetch(PL_op, &oi);
521
522  return CALL_FPTR(oi.old_pp)(aTHX);
523 }
524
525 STATIC OP *a_pp_root_binop(pTHX) {
526  a_op_info oi;
527  dSP;
528
529  if (!a_defined(TOPm1s)) {
530   POPs;
531   POPs;
532   if (PL_op->op_type == OP_EXISTS)
533    RETPUSHNO;
534   else
535    RETPUSHUNDEF;
536  }
537
538  a_map_fetch(PL_op, &oi);
539
540  return CALL_FPTR(oi.old_pp)(aTHX);
541 }
542
543 /* --- Check functions ----------------------------------------------------- */
544
545 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
546 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
547  a_op_info oi;
548
549  if (o->op_type == type && o->op_ppaddr != new_pp
550                         && cUNOPo->op_first->op_type != OP_GV
551                         && a_map_fetch(o, &oi)) {
552   a_map_store(o, o->op_ppaddr, oi.next, oi.flags);
553   o->op_ppaddr = new_pp;
554  }
555
556  return;
557 }
558
559 /* ... ck_pad{any,sv} ...................................................... */
560
561 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
562  * function, but are instead manually mutated from a PADANY. This is why we set
563  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
564  * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
565  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
566  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
567  * globally. */
568
569 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
570
571 STATIC void a_pp_padsv_save(void) {
572  if (a_pp_padsv_saved)
573   return;
574
575  a_pp_padsv_saved    = PL_ppaddr[OP_PADSV];
576  PL_ppaddr[OP_PADSV] = a_pp_deref;
577 }
578
579 STATIC void a_pp_padsv_restore(OP *o) {
580  if (!a_pp_padsv_saved)
581   return;
582
583  if (o->op_ppaddr == a_pp_deref)
584   o->op_ppaddr = a_pp_padsv_saved;
585
586  PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
587  a_pp_padsv_saved    = 0;
588 }
589
590 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
591
592 STATIC OP *a_ck_padany(pTHX_ OP *o) {
593  UV hint;
594
595  a_pp_padsv_restore(o);
596
597  o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
598
599  hint = a_hint();
600  if (hint & A_HINT_DO) {
601   a_pp_padsv_save();
602   a_map_store_root(o, a_pp_padsv_saved, hint);
603  } else
604   a_map_delete(o);
605
606  return o;
607 }
608
609 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
610
611 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
612  UV hint;
613
614  a_pp_padsv_restore(o);
615
616  o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
617
618  hint = a_hint();
619  if (hint & A_HINT_DO) {
620   a_map_store_root(o, o->op_ppaddr, hint);
621   o->op_ppaddr = a_pp_deref;
622  } else
623   a_map_delete(o);
624
625  return o;
626 }
627
628 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
629
630 /* Those ops appear both at the root and inside an expression but there's no
631  * way to distinguish both situations. Worse, we can't even know if we are in a
632  * modifying context, so the expression can't be resolved yet. It will be at the
633  * first invocation of a_pp_deref() for this expression. */
634
635 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
636 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
637 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
638
639 STATIC OP *a_ck_deref(pTHX_ OP *o) {
640  OP * (*old_ck)(pTHX_ OP *o) = 0;
641  UV hint = a_hint();
642
643  switch (o->op_type) {
644   case OP_AELEM:
645    old_ck = a_old_ck_aelem;
646    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
647     a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
648    break;
649   case OP_HELEM:
650    old_ck = a_old_ck_helem;
651    if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
652     a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
653    break;
654   case OP_RV2SV:
655    old_ck = a_old_ck_rv2sv;
656    break;
657  }
658  o = CALL_FPTR(old_ck)(aTHX_ o);
659
660  if (hint & A_HINT_DO) {
661   a_map_store_root(o, o->op_ppaddr, hint);
662   o->op_ppaddr = a_pp_deref;
663  } else
664   a_map_delete(o);
665
666  return o;
667 }
668
669 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
670
671 /* Those ops also appear both inisde and at the root, hence the caveats for
672  * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
673  * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
674  * expression. */
675
676 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
677 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
678
679 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
680  OP * (*old_ck)(pTHX_ OP *o) = 0;
681  OP * (*new_pp)(pTHX)        = 0;
682  UV hint;
683
684  switch (o->op_type) {
685   case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
686   case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
687  }
688  o = CALL_FPTR(old_ck)(aTHX_ o);
689
690  if (cUNOPo->op_first->op_type == OP_GV)
691   return o;
692
693  hint = a_hint();
694  if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
695   a_map_store_root(o, o->op_ppaddr, hint);
696   o->op_ppaddr = new_pp;
697  } else
698   a_map_delete(o);
699
700  return o;
701 }
702
703 /* ... ck_xslice (aslice,hslice) ........................................... */
704
705 /* I think those are only found at the root, but there's nothing that really
706  * prevent them to be inside the expression too. We only need to update the
707  * root so that the rest of the expression will see the right context when
708  * resolving. That's why we don't replace the ppaddr. */
709
710 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
711 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
712
713 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
714  OP * (*old_ck)(pTHX_ OP *o) = 0;
715  UV hint = a_hint();
716
717  switch (o->op_type) {
718   case OP_ASLICE:
719    old_ck = a_old_ck_aslice;
720    break;
721   case OP_HSLICE:
722    old_ck = a_old_ck_hslice;
723    if (hint & A_HINT_DO)
724     a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
725    break;
726  }
727  o = CALL_FPTR(old_ck)(aTHX_ o);
728
729  if (hint & A_HINT_DO) {
730   a_map_store_root(o, 0, hint);
731  } else
732   a_map_delete(o);
733
734  return o;
735 }
736
737 /* ... ck_root (exists,delete,keys,values) ................................. */
738
739 /* Those ops are only found at the root of a dereferencing expression. We can
740  * then resolve at compile time if vivification must take place or not. */
741
742 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
743 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
744 STATIC OP *(*a_old_ck_keys)  (pTHX_ OP *) = 0;
745 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
746
747 STATIC OP *a_ck_root(pTHX_ OP *o) {
748  OP * (*old_ck)(pTHX_ OP *o) = 0;
749  OP * (*new_pp)(pTHX)        = 0;
750  bool enabled = FALSE;
751  UV hint = a_hint();
752
753  switch (o->op_type) {
754   case OP_EXISTS:
755    old_ck  = a_old_ck_exists;
756    new_pp  = a_pp_root_binop;
757    enabled = hint & A_HINT_EXISTS;
758    break;
759   case OP_DELETE:
760    old_ck  = a_old_ck_delete;
761    new_pp  = a_pp_root_binop;
762    enabled = hint & A_HINT_DELETE;
763    break;
764   case OP_KEYS:
765    old_ck  = a_old_ck_keys;
766    new_pp  = a_pp_root_unop;
767    enabled = hint & A_HINT_FETCH;
768    break;
769   case OP_VALUES:
770    old_ck  = a_old_ck_values;
771    new_pp  = a_pp_root_unop;
772    enabled = hint & A_HINT_FETCH;
773    break;
774  }
775  o = CALL_FPTR(old_ck)(aTHX_ o);
776
777  if (hint & A_HINT_DO) {
778   if (enabled) {
779    a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
780    a_map_store_root(o, o->op_ppaddr, hint);
781    o->op_ppaddr = new_pp;
782   } else {
783    a_map_cancel(o);
784   }
785  } else
786   a_map_delete(o);
787
788  return o;
789 }
790
791 STATIC U32 a_initialized = 0;
792
793 /* --- XS ------------------------------------------------------------------ */
794
795 MODULE = autovivification      PACKAGE = autovivification
796
797 PROTOTYPES: ENABLE
798
799 BOOT: 
800 {                                    
801  if (!a_initialized++) {
802   HV *stash;
803
804   a_op_map = ptable_new();
805 #ifdef USE_ITHREADS
806   MUTEX_INIT(&a_op_map_mutex);
807 #endif
808
809   PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
810
811   a_old_ck_padany     = PL_check[OP_PADANY];
812   PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
813   a_old_ck_padsv      = PL_check[OP_PADSV];
814   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_ck_padsv);
815
816   a_old_ck_aelem      = PL_check[OP_AELEM];
817   PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_ck_deref);
818   a_old_ck_helem      = PL_check[OP_HELEM];
819   PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_ck_deref);
820   a_old_ck_rv2sv      = PL_check[OP_RV2SV];
821   PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_ck_deref);
822
823   a_old_ck_rv2av      = PL_check[OP_RV2AV];
824   PL_check[OP_RV2AV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
825   a_old_ck_rv2hv      = PL_check[OP_RV2HV];
826   PL_check[OP_RV2HV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
827
828   a_old_ck_aslice     = PL_check[OP_ASLICE];
829   PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_ck_xslice);
830   a_old_ck_hslice     = PL_check[OP_HSLICE];
831   PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_ck_xslice);
832
833   a_old_ck_exists     = PL_check[OP_EXISTS];
834   PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
835   a_old_ck_delete     = PL_check[OP_DELETE];
836   PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
837   a_old_ck_keys       = PL_check[OP_KEYS];
838   PL_check[OP_KEYS]   = MEMBER_TO_FPTR(a_ck_root);
839   a_old_ck_values     = PL_check[OP_VALUES];
840   PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
841
842   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
843   newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
844   newCONSTSUB(stash, "A_HINT_WARN",   newSVuv(A_HINT_WARN));
845   newCONSTSUB(stash, "A_HINT_FETCH",  newSVuv(A_HINT_FETCH));
846   newCONSTSUB(stash, "A_HINT_STORE",  newSVuv(A_HINT_STORE));
847   newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
848   newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
849   newCONSTSUB(stash, "A_HINT_MASK",   newSVuv(A_HINT_MASK));
850  }
851 }
852
853 SV *
854 _tag(SV *hint)
855 PROTOTYPE: $
856 CODE:
857  RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
858 OUTPUT:
859  RETVAL
860
861 SV *
862 _detag(SV *tag)
863 PROTOTYPE: $
864 CODE:
865  if (!SvOK(tag))
866   XSRETURN_UNDEF;
867  RETVAL = newSVuv(a_detag(tag));
868 OUTPUT:
869  RETVAL