| 1 |
|
| #ifndef HAMT_OPS_H |
| 2 |
|
| #define HAMT_OPS_H |
| 3 |
|
| |
| 4 |
|
| /* The function forms of the reads: hamt_map_get($m, $k) and its kin. Each is |
| 5 |
|
| * an ordinary XSUB, and where perl has call checkers (5.14 on) a call to one |
| 6 |
|
| * that is resolved at compile time is rewritten into a custom op, so the read |
| 7 |
|
| * costs no sub call. Both routes run the bodies below. */ |
| 8 |
|
| |
| 9 |
|
| #ifndef PERL_VERSION_GE |
| 10 |
|
| # define PERL_VERSION_GE(r,v,s) \ |
| 11 |
|
| (PERL_REVISION > (r) || (PERL_REVISION == (r) && \ |
| 12 |
|
| (PERL_VERSION > (v) || (PERL_VERSION == (v) && PERL_SUBVERSION >= (s))))) |
| 13 |
|
| #endif |
| 14 |
|
| |
| 15 |
|
| #if PERL_VERSION_GE(5,14,0) |
| 16 |
|
| # define HAMT_HAS_OPS 1 |
| 17 |
|
| #else |
| 18 |
|
| # define HAMT_HAS_OPS 0 |
| 19 |
|
| #endif |
| 20 |
|
| |
| 21 |
|
| /* Each function lives in, and is exported by, the class it reads. */ |
| 22 |
|
| static const char *const hamt_fn_names[] = { |
| 23 |
|
| "hamt_map_get", "hamt_map_exists", "hamt_map_count", |
| 24 |
|
| "hamt_set_has", "hamt_set_count", |
| 25 |
|
| "hamt_vector_get", "hamt_vector_count" |
| 26 |
|
| }; |
| 27 |
|
| #define HAMT_FN_COUNT 7 |
| 28 |
|
| |
| 29 |
|
| typedef struct { const char *pkg; int from, to; } hamt_fn_group; |
| 30 |
|
| static const hamt_fn_group hamt_fn_groups[3] = { |
| 31 |
|
| { "HAMT::Map", 0, 3 }, |
| 32 |
|
| { "HAMT::Set", 3, 5 }, |
| 33 |
|
| { "HAMT::Vector", 5, 7 } |
| 34 |
|
| }; |
| 35 |
|
| #define HAMT_FN_MAP 0 |
| 36 |
|
| #define HAMT_FN_SET 1 |
| 37 |
|
| #define HAMT_FN_VECTOR 2 |
| 38 |
|
| |
| 39 |
218 |
| static const char *hamt_fn_pkg(int i) { |
| 40 |
218 |
▶ | return hamt_fn_groups[i < 3 ? 0 : i < 5 ? 1 : 2].pkg; |
Branch: 2 branches
| Branch | True | False | Total |
| return hamt_fn_groups[i < 3 ? 0 : i < 5 ? 1 : 2].pkg; |
124 |
94 |
218 |
| return hamt_fn_groups[i < 3 ? 0 : i < 5 ? 1 : 2].pkg; |
61 |
63 |
124 |
|
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| /* The stored scalar itself, mortal, or a fresh undef: as the methods. */ |
| 44 |
37 |
| static SV *hamt_fn_map_get(pTHX_ SV *self, SV *key) { |
| 45 |
37 |
| hamt_map *m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "hamt_map_get", "map"); |
| 46 |
|
| hamt_key k; |
| 47 |
|
| hamt_entry *e; |
| 48 |
32 |
| hamt_key_from_sv(aTHX_ key, &k); |
| 49 |
31 |
| e = hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash); |
| 50 |
28 |
▶ | return (e && e->val) ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val)) |
Branch: 1 branch
| Branch | True | False | Total |
| return (e && e->val) ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)e->val)) |
28 |
0 |
28 |
|
| 51 |
59 |
▶ | : sv_newmortal(); |
Branch: 1 branch
| Branch | True | False | Total |
| : sv_newmortal(); |
28 |
3 |
31 |
|
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
7 |
| static SV *hamt_fn_map_exists(pTHX_ SV *self, SV *key) { |
| 55 |
7 |
| hamt_map *m = hamt_handle(aTHX_ self, &hamt_map_vtbl, "hamt_map_exists", "map"); |
| 56 |
|
| hamt_key k; |
| 57 |
6 |
| hamt_key_from_sv(aTHX_ key, &k); |
| 58 |
6 |
▶ | return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); |
5 |
1 |
6 |
|
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
8 |
| static SV *hamt_fn_set_has(pTHX_ SV *self, SV *key) { |
| 62 |
8 |
| hamt_map *m = hamt_handle(aTHX_ self, &hamt_set_vtbl, "hamt_set_has", "set"); |
| 63 |
|
| hamt_key k; |
| 64 |
7 |
| hamt_key_from_sv(aTHX_ key, &k); |
| 65 |
7 |
▶ | return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| return boolSV(hamt_map_find(m->root, k.p, (size_t)k.len, k.utf8, k.hash) != NULL); |
6 |
1 |
7 |
|
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
18 |
| static SV *hamt_fn_vector_get(pTHX_ SV *self, SV *index) { |
| 69 |
18 |
| hamt_vec *v = hamt_vec_handle(aTHX_ self, "hamt_vector_get"); |
| 70 |
|
| size_t i; |
| 71 |
17 |
| return hamt_xs_index(aTHX_ index, v->count, &i) |
| 72 |
15 |
| ? sv_2mortal(SvREFCNT_inc_simple_NN((SV *)hamt_vec_get(v, i))) |
| 73 |
32 |
▶ | : sv_newmortal(); |
Branch: 1 branch
| Branch | True | False | Total |
| : sv_newmortal(); |
15 |
2 |
17 |
|
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
14 |
| static UV hamt_fn_map_count(pTHX_ SV *self) { |
| 77 |
14 |
| return (UV)hamt_handle(aTHX_ self, &hamt_map_vtbl, "hamt_map_count", "map")->count; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
6 |
| static UV hamt_fn_set_count(pTHX_ SV *self) { |
| 81 |
6 |
| return (UV)hamt_handle(aTHX_ self, &hamt_set_vtbl, "hamt_set_count", "set")->count; |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
7 |
| static UV hamt_fn_vector_count(pTHX_ SV *self) { |
| 85 |
7 |
| return (UV)hamt_vec_handle(aTHX_ self, "hamt_vector_count")->count; |
| 86 |
|
| } |
| 87 |
|
| |
| 88 |
|
| /* Into the caller's package, as a glob assignment of a code reference, which |
| 89 |
|
| * is what marks the name imported. */ |
| 90 |
15 |
| static void hamt_fn_export(pTHX_ HV *stash, int i) { |
| 91 |
15 |
| const char *name = hamt_fn_names[i]; |
| 92 |
15 |
| CV *cv = get_cv(form("%s::%s", hamt_fn_pkg(i), name), 0); |
| 93 |
|
| SV **svp; |
| 94 |
15 |
▶ | if (!cv) croak("HAMT: %s is not built", name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cv) croak("HAMT: %s is not built", name); |
0 |
15 |
15 |
|
| 95 |
15 |
| svp = hv_fetch(stash, name, (I32)strlen(name), 1); |
| 96 |
15 |
▶ | if (!svp) croak("HAMT: cannot export %s", name); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!svp) croak("HAMT: cannot export %s", name); |
0 |
15 |
15 |
|
| 97 |
15 |
▶ | if (SvTYPE(*svp) != SVt_PVGV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(*svp) != SVt_PVGV) |
13 |
2 |
15 |
|
| 98 |
13 |
| gv_init((GV *)*svp, stash, name, (I32)strlen(name), GV_ADDMULTI); |
| 99 |
15 |
| sv_setsv(*svp, sv_2mortal(newRV_inc((SV *)cv))); |
| 100 |
15 |
| } |
| 101 |
|
| |
| 102 |
|
| /* The names asked of one class, or ":all" for every one it has. */ |
| 103 |
50 |
| static void hamt_fn_import(pTHX_ int group, SV **want, I32 n) { |
| 104 |
50 |
| const hamt_fn_group *g = &hamt_fn_groups[group]; |
| 105 |
50 |
| HV *stash = CopSTASH(PL_curcop); |
| 106 |
|
| I32 i; |
| 107 |
|
| int j; |
| 108 |
|
| |
| 109 |
58 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
12 |
46 |
58 |
|
| 110 |
12 |
| const char *w = SvPV_nolen(want[i]); |
| 111 |
12 |
▶ | if (strEQ(w, ":all")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(w, ":all")) { |
5 |
7 |
12 |
|
| 112 |
17 |
▶ | for (j = g->from; j < g->to; j++) hamt_fn_export(aTHX_ stash, j); |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = g->from; j < g->to; j++) hamt_fn_export(aTHX_ stash, j); |
12 |
5 |
17 |
|
| 113 |
5 |
| continue; |
| 114 |
|
| } |
| 115 |
18 |
▶ | for (j = g->from; j < g->to; j++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = g->from; j < g->to; j++) |
14 |
4 |
18 |
|
| 116 |
14 |
▶ | if (strEQ(w, hamt_fn_names[j])) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strEQ(w, hamt_fn_names[j])) break; |
3 |
11 |
14 |
|
| 117 |
7 |
▶ | if (j == g->to) croak("%s does not export \"%s\"", g->pkg, w); |
Branch: 1 branch
| Branch | True | False | Total |
| if (j == g->to) croak("%s does not export \"%s\"", g->pkg, w); |
4 |
3 |
7 |
|
| 118 |
3 |
| hamt_fn_export(aTHX_ stash, j); |
| 119 |
|
| } |
| 120 |
46 |
| } |
| 121 |
|
| |
| 122 |
|
| #if HAMT_HAS_OPS |
| 123 |
|
| |
| 124 |
|
| #ifndef OpHAS_SIBLING |
| 125 |
|
| # define OpHAS_SIBLING(o) ((o)->op_sibling != NULL) |
| 126 |
|
| #endif |
| 127 |
|
| #ifndef OpSIBLING |
| 128 |
|
| # define OpSIBLING(o) ((o)->op_sibling) |
| 129 |
|
| #endif |
| 130 |
|
| #ifndef OpMORESIB_set |
| 131 |
|
| # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib)) |
| 132 |
|
| #endif |
| 133 |
|
| #ifndef OpLASTSIB_set |
| 134 |
|
| # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL) |
| 135 |
|
| #endif |
| 136 |
|
| |
| 137 |
|
| /* The key may be an object that stringifies by running Perl. perl runs |
| 138 |
|
| * overloading and tie methods on a stack of their own, so this one does not |
| 139 |
|
| * move; the pointer is put back and read again all the same, which costs |
| 140 |
|
| * two moves and does not depend on that staying true. */ |
| 141 |
|
| #define HAMT_PP_KEYED(pp, fn) \ |
| 142 |
|
| static OP *pp(pTHX) { \ |
| 143 |
|
| dSP; \ |
| 144 |
|
| SV *key = POPs; \ |
| 145 |
|
| SV *self = TOPs; \ |
| 146 |
|
| SV *r; \ |
| 147 |
|
| PUTBACK; \ |
| 148 |
|
| r = fn(aTHX_ self, key); \ |
| 149 |
|
| SPAGAIN; \ |
| 150 |
|
| SETs(r); \ |
| 151 |
|
| RETURN; \ |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
|
| /* A count is written into a scalar the op owns where perl lets an extension |
| 155 |
|
| * have one, and into a temporary where it does not. pad_alloc is not |
| 156 |
|
| * exported by 5.14; 5.20 is the oldest perl this has been built on with it. */ |
| 157 |
|
| #if PERL_VERSION_GE(5,20,0) |
| 158 |
|
| # define HAMT_OP_TARG(o) ((o)->op_targ = pad_alloc(OP_CUSTOM, SVs_PADTMP)) |
| 159 |
|
| # define HAMT_PP_COUNT(pp, fn) \ |
| 160 |
|
| static OP *pp(pTHX) { \ |
| 161 |
|
| dSP; \ |
| 162 |
|
| dTARGET; \ |
| 163 |
|
| SV *self = TOPs; \ |
| 164 |
|
| sv_setuv(TARG, fn(aTHX_ self)); \ |
| 165 |
|
| SvSETMAGIC(TARG); \ |
| 166 |
|
| SETs(TARG); \ |
| 167 |
|
| RETURN; \ |
| 168 |
|
| } |
| 169 |
|
| #else |
| 170 |
|
| # define HAMT_OP_TARG(o) ((void)0) |
| 171 |
|
| # define HAMT_PP_COUNT(pp, fn) \ |
| 172 |
|
| static OP *pp(pTHX) { \ |
| 173 |
|
| dSP; \ |
| 174 |
|
| SV *self = TOPs; \ |
| 175 |
|
| SETs(sv_2mortal(newSVuv(fn(aTHX_ self)))); \ |
| 176 |
|
| RETURN; \ |
| 177 |
|
| } |
| 178 |
|
| #endif |
| 179 |
|
| |
| 180 |
31 |
| HAMT_PP_KEYED(hamt_pp_map_get, hamt_fn_map_get) |
| 181 |
4 |
| HAMT_PP_KEYED(hamt_pp_map_exists, hamt_fn_map_exists) |
| 182 |
5 |
| HAMT_PP_KEYED(hamt_pp_set_has, hamt_fn_set_has) |
| 183 |
15 |
| HAMT_PP_KEYED(hamt_pp_vector_get, hamt_fn_vector_get) |
| 184 |
12 |
▶ | HAMT_PP_COUNT(hamt_pp_map_count, hamt_fn_map_count) |
Branch: 1 branch
| Branch | True | False | Total |
| HAMT_PP_COUNT(hamt_pp_map_count, hamt_fn_map_count) |
0 |
10 |
10 |
|
| 185 |
4 |
▶ | HAMT_PP_COUNT(hamt_pp_set_count, hamt_fn_set_count) |
Branch: 1 branch
| Branch | True | False | Total |
| HAMT_PP_COUNT(hamt_pp_set_count, hamt_fn_set_count) |
0 |
3 |
3 |
|
| 186 |
5 |
▶ | HAMT_PP_COUNT(hamt_pp_vector_count, hamt_fn_vector_count) |
Branch: 1 branch
| Branch | True | False | Total |
| HAMT_PP_COUNT(hamt_pp_vector_count, hamt_fn_vector_count) |
0 |
4 |
4 |
|
| 187 |
|
| |
| 188 |
|
| /* The prototype is applied first, so the arguments are counted and in scalar |
| 189 |
|
| * context whatever they are. Then they are lifted out of the call and made |
| 190 |
|
| * the children of the op. Anything not shaped as expected is left a call. */ |
| 191 |
83 |
| static OP *hamt_ck_rewrite(pTHX_ OP *o, GV *namegv, SV *ckobj, |
| 192 |
|
| Perl_ppaddr_t pp, int nargs) { |
| 193 |
83 |
| OP *pushop, *a, *b = NULL, *cvop, *newop; |
| 194 |
|
| |
| 195 |
83 |
| o = ck_entersub_args_proto(o, namegv, ckobj); |
| 196 |
83 |
| pushop = cUNOPx(o)->op_first; |
| 197 |
83 |
▶ | if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!OpHAS_SIBLING(pushop)) pushop = cUNOPx(pushop)->op_first; |
83 |
0 |
83 |
|
| 198 |
83 |
▶ | a = OpSIBLING(pushop); |
Branch: 1 branch
| Branch | True | False | Total |
| a = OpSIBLING(pushop); |
83 |
0 |
83 |
|
| 199 |
83 |
▶ | if (!a || !OpHAS_SIBLING(a)) return o; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a || !OpHAS_SIBLING(a)) return o; |
83 |
0 |
83 |
| if (!a || !OpHAS_SIBLING(a)) return o; |
1 |
82 |
83 |
|
| 200 |
82 |
▶ | if (nargs == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nargs == 2) { |
59 |
23 |
82 |
|
| 201 |
59 |
▶ | b = OpSIBLING(a); |
Branch: 1 branch
| Branch | True | False | Total |
| b = OpSIBLING(a); |
59 |
0 |
59 |
|
| 202 |
59 |
▶ | if (!OpHAS_SIBLING(b)) return o; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!OpHAS_SIBLING(b)) return o; |
1 |
58 |
59 |
|
| 203 |
58 |
▶ | cvop = OpSIBLING(b); |
Branch: 1 branch
| Branch | True | False | Total |
| cvop = OpSIBLING(b); |
58 |
0 |
58 |
|
| 204 |
|
| } |
| 205 |
|
| else { |
| 206 |
23 |
▶ | cvop = OpSIBLING(a); |
Branch: 1 branch
| Branch | True | False | Total |
| cvop = OpSIBLING(a); |
23 |
0 |
23 |
|
| 207 |
|
| } |
| 208 |
81 |
▶ | if (OpHAS_SIBLING(cvop)) return o; |
Branch: 1 branch
| Branch | True | False | Total |
| if (OpHAS_SIBLING(cvop)) return o; |
2 |
79 |
81 |
|
| 209 |
|
| |
| 210 |
79 |
| OpMORESIB_set(pushop, cvop); |
| 211 |
79 |
▶ | if (b) OpLASTSIB_set(b, NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| if (b) OpLASTSIB_set(b, NULL); |
57 |
22 |
79 |
|
| 212 |
79 |
| OpLASTSIB_set(a, NULL); |
| 213 |
79 |
▶ | newop = b ? newBINOP(OP_NULL, 0, a, b) : newUNOP(OP_NULL, 0, a); |
Branch: 1 branch
| Branch | True | False | Total |
| newop = b ? newBINOP(OP_NULL, 0, a, b) : newUNOP(OP_NULL, 0, a); |
57 |
22 |
79 |
|
| 214 |
79 |
| newop->op_type = OP_CUSTOM; |
| 215 |
79 |
| newop->op_ppaddr = pp; |
| 216 |
79 |
▶ | if (!b) HAMT_OP_TARG(newop); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) HAMT_OP_TARG(newop); |
22 |
57 |
79 |
|
| 217 |
79 |
| op_free(o); |
| 218 |
79 |
| return newop; |
| 219 |
|
| } |
| 220 |
|
| |
| 221 |
|
| #define HAMT_CK(ck, pp, nargs) \ |
| 222 |
|
| static OP *ck(pTHX_ OP *o, GV *namegv, SV *ckobj) { \ |
| 223 |
|
| return hamt_ck_rewrite(aTHX_ o, namegv, ckobj, pp, nargs); \ |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
34 |
| HAMT_CK(hamt_ck_map_get, hamt_pp_map_get, 2) |
| 227 |
5 |
| HAMT_CK(hamt_ck_map_exists, hamt_pp_map_exists, 2) |
| 228 |
6 |
| HAMT_CK(hamt_ck_set_has, hamt_pp_set_has, 2) |
| 229 |
14 |
| HAMT_CK(hamt_ck_vector_get, hamt_pp_vector_get, 2) |
| 230 |
13 |
| HAMT_CK(hamt_ck_map_count, hamt_pp_map_count, 1) |
| 231 |
5 |
| HAMT_CK(hamt_ck_set_count, hamt_pp_set_count, 1) |
| 232 |
6 |
| HAMT_CK(hamt_ck_vector_count, hamt_pp_vector_count, 1) |
| 233 |
|
| |
| 234 |
|
| static XOP hamt_xop[HAMT_FN_COUNT]; |
| 235 |
|
| |
| 236 |
203 |
| static void hamt_ops_one(pTHX_ int i, Perl_ppaddr_t pp, Perl_call_checker ck, U32 cls) { |
| 237 |
203 |
| CV *cv = get_cv(form("%s::%s", hamt_fn_pkg(i), hamt_fn_names[i]), 0); |
| 238 |
203 |
| XopENTRY_set(&hamt_xop[i], xop_name, hamt_fn_names[i]); |
| 239 |
203 |
| XopENTRY_set(&hamt_xop[i], xop_desc, hamt_fn_names[i]); |
| 240 |
203 |
| XopENTRY_set(&hamt_xop[i], xop_class, cls); |
| 241 |
203 |
| Perl_custom_op_register(aTHX_ pp, &hamt_xop[i]); |
| 242 |
203 |
▶ | if (cv) cv_set_call_checker(cv, ck, (SV *)cv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck, (SV *)cv); |
203 |
0 |
203 |
|
| 243 |
203 |
| } |
| 244 |
|
| |
| 245 |
|
| #endif |
| 246 |
|
| |
| 247 |
|
| /* Once, after the XSUBs exist. */ |
| 248 |
29 |
| static void hamt_ops_boot(pTHX) { |
| 249 |
|
| #if HAMT_HAS_OPS |
| 250 |
29 |
| hamt_ops_one(aTHX_ 0, hamt_pp_map_get, hamt_ck_map_get, OA_BINOP); |
| 251 |
29 |
| hamt_ops_one(aTHX_ 1, hamt_pp_map_exists, hamt_ck_map_exists, OA_BINOP); |
| 252 |
29 |
| hamt_ops_one(aTHX_ 2, hamt_pp_map_count, hamt_ck_map_count, OA_UNOP); |
| 253 |
29 |
| hamt_ops_one(aTHX_ 3, hamt_pp_set_has, hamt_ck_set_has, OA_BINOP); |
| 254 |
29 |
| hamt_ops_one(aTHX_ 4, hamt_pp_set_count, hamt_ck_set_count, OA_UNOP); |
| 255 |
29 |
| hamt_ops_one(aTHX_ 5, hamt_pp_vector_get, hamt_ck_vector_get, OA_BINOP); |
| 256 |
29 |
| hamt_ops_one(aTHX_ 6, hamt_pp_vector_count, hamt_ck_vector_count, OA_UNOP); |
| 257 |
|
| #else |
| 258 |
|
| PERL_UNUSED_CONTEXT; |
| 259 |
|
| #endif |
| 260 |
29 |
| } |
| 261 |
|
| |
| 262 |
|
| #endif |