| 1 |
|
| /* You may distribute under the terms of either the GNU General Public License |
| 2 |
|
| * or the Artistic License (the same terms as Perl itself) |
| 3 |
|
| * |
| 4 |
|
| * (C) Paul Evans, 2021-2023 -- leonerd@leonerd.org.uk |
| 5 |
|
| */ |
| 6 |
|
| #define PERL_NO_GET_CONTEXT |
| 7 |
|
| |
| 8 |
|
| #include "EXTERN.h" |
| 9 |
|
| #include "perl.h" |
| 10 |
|
| #include "XSUB.h" |
| 11 |
|
| |
| 12 |
|
| #include "XSParseKeyword.h" |
| 13 |
|
| #include "XSParseInfix.h" |
| 14 |
|
| |
| 15 |
|
| #define HAVE_PERL_VERSION(R, V, S) \ |
| 16 |
|
| (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S)))))) |
| 17 |
|
| |
| 18 |
|
| #include "hax/perl-backcompat.c.inc" |
| 19 |
|
| |
| 20 |
|
| #if HAVE_PERL_VERSION(5,32,0) |
| 21 |
|
| # define HAVE_OP_ISA |
| 22 |
|
| #endif |
| 23 |
|
| |
| 24 |
|
| #if HAVE_PERL_VERSION(5,18,0) |
| 25 |
|
| # define HAVE_BOOL_SvIV_please_nomg |
| 26 |
|
| #endif |
| 27 |
|
| |
| 28 |
|
| #if HAVE_PERL_VERSION(5,35,9) |
| 29 |
|
| # define HAVE_SV_NUMEQ_FLAGS |
| 30 |
|
| #endif |
| 31 |
|
| |
| 32 |
|
| #include "dispatchop.h" |
| 33 |
|
| |
| 34 |
|
| #ifndef HAVE_SV_NUMEQ_FLAGS |
| 35 |
|
| /* We'd like to call Perl_do_ncmp, except that isn't an exported API function |
| 36 |
|
| * Here's a near-copy of it for num-equality testing purposes */ |
| 37 |
|
| #define do_numeq(left, right) S_do_numeq(aTHX_ left, right) |
| 38 |
|
| static bool S_do_numeq(pTHX_ SV *left, SV *right) |
| 39 |
|
| { |
| 40 |
|
| #ifndef HAVE_BOOL_SvIV_please_nomg |
| 41 |
|
| /* Before perl 5.18, SvIV_please_nomg() was void-returning */ |
| 42 |
|
| SvIV_please_nomg(left); |
| 43 |
|
| SvIV_please_nomg(right); |
| 44 |
|
| #endif |
| 45 |
|
| |
| 46 |
|
| if( |
| 47 |
|
| #ifdef HAVE_BOOL_SvIV_please_nomg |
| 48 |
|
| SvIV_please_nomg(right) && SvIV_please_nomg(left) |
| 49 |
|
| #else |
| 50 |
|
| SvIOK(left) && SvIOK(right) |
| 51 |
|
| #endif |
| 52 |
|
| ) { |
| 53 |
|
| /* Compare as integers */ |
| 54 |
|
| switch((SvUOK(left) ? 1 : 0) | (SvUOK(right) ? 2 : 0)) { |
| 55 |
|
| case 0: /* IV == IV */ |
| 56 |
|
| return SvIVX(left) == SvIVX(right); |
| 57 |
|
| |
| 58 |
|
| case 1: /* UV == IV */ |
| 59 |
|
| { |
| 60 |
|
| const IV riv = SvUVX(right); |
| 61 |
|
| if(riv < 0) |
| 62 |
|
| return 0; |
| 63 |
|
| return (SvUVX(left) == riv); |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| case 2: /* IV == UV */ |
| 67 |
|
| { |
| 68 |
|
| const IV liv = SvUVX(left); |
| 69 |
|
| if(liv < 0) |
| 70 |
|
| return 0; |
| 71 |
|
| return (liv == SvUVX(right)); |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
|
| case 3: /* UV == UV */ |
| 75 |
|
| return SvUVX(left) == SvUVX(right); |
| 76 |
|
| } |
| 77 |
|
| } |
| 78 |
|
| else { |
| 79 |
|
| /* Compare NVs */ |
| 80 |
|
| NV const rnv = SvNV_nomg(right); |
| 81 |
|
| NV const lnv = SvNV_nomg(left); |
| 82 |
|
| |
| 83 |
|
| return lnv == rnv; |
| 84 |
|
| } |
| 85 |
|
| } |
| 86 |
|
| #endif |
| 87 |
|
| |
| 88 |
|
| #define newPADSVOP(type, flags, padix) MY_newPADSVOP(aTHX_ type, flags, padix) |
| 89 |
103 |
| static OP *MY_newPADSVOP(pTHX_ I32 type, I32 flags, PADOFFSET padix) |
| 90 |
|
| { |
| 91 |
103 |
| OP *op = newOP(type, flags); |
| 92 |
103 |
| op->op_targ = padix; |
| 93 |
103 |
| return op; |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
4 |
| static OP *pp_dispatch_numeq(pTHX) |
| 97 |
|
| { |
| 98 |
4 |
| dDISPATCH; |
| 99 |
4 |
| dTARGET; |
| 100 |
|
| int idx; |
| 101 |
|
| |
| 102 |
4 |
▶ | bool has_magic = SvAMAGIC(TARG); |
Branch: 3 branches
| Branch | True | False | Total |
| bool has_magic = SvAMAGIC(TARG); |
0 |
4 |
4 |
| bool has_magic = SvAMAGIC(TARG); |
0 |
0 |
0 |
| bool has_magic = SvAMAGIC(TARG); |
0 |
0 |
0 |
|
| 103 |
|
| |
| 104 |
8 |
▶ | for(idx = 0; idx < n_cases; idx++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(idx = 0; idx < n_cases; idx++) { |
8 |
0 |
8 |
|
| 105 |
8 |
| SV *val = values[idx]; |
| 106 |
|
| |
| 107 |
|
| SV *ret; |
| 108 |
8 |
| if(has_magic && |
| 109 |
0 |
| (ret = amagic_call(TARG, val, eq_amg, 0))) { |
| 110 |
0 |
▶ | if(SvTRUE(ret)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvTRUE(ret)) |
0 |
0 |
0 |
|
| 111 |
0 |
| return dispatch[idx]; |
| 112 |
|
| } |
| 113 |
|
| #ifdef HAVE_SV_NUMEQ_FLAGS |
| 114 |
8 |
▶ | else if(sv_numeq_flags(TARG, val, SV_SKIP_OVERLOAD)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(sv_numeq_flags(TARG, val, SV_SKIP_OVERLOAD)) |
4 |
4 |
8 |
|
| 115 |
|
| #else |
| 116 |
|
| /* stolen from core's pp_hot.c / pp_eq() */ |
| 117 |
|
| else if((SvIOK_notUV(TARG) && SvIOK_notUV(val)) ? |
| 118 |
|
| SvIVX(TARG) == SvIVX(val) : (do_numeq(TARG, val))) |
| 119 |
|
| #endif |
| 120 |
4 |
| return dispatch[idx]; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
0 |
| return cDISPATCHOP->op_other; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
1000012 |
| static OP *pp_dispatch_streq(pTHX) |
| 127 |
|
| { |
| 128 |
1000012 |
| dDISPATCH; |
| 129 |
1000012 |
| dTARGET; |
| 130 |
|
| int idx; |
| 131 |
|
| |
| 132 |
1000012 |
▶ | bool has_magic = SvAMAGIC(TARG); |
Branch: 3 branches
| Branch | True | False | Total |
| bool has_magic = SvAMAGIC(TARG); |
2 |
1000010 |
1000012 |
| bool has_magic = SvAMAGIC(TARG); |
2 |
0 |
2 |
| bool has_magic = SvAMAGIC(TARG); |
2 |
0 |
2 |
|
| 133 |
|
| |
| 134 |
10000059 |
▶ | for(idx = 0; idx < n_cases; idx++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(idx = 0; idx < n_cases; idx++) { |
9000057 |
1000002 |
10000059 |
|
| 135 |
9000057 |
| SV *val = values[idx]; |
| 136 |
|
| |
| 137 |
|
| SV *ret; |
| 138 |
9000060 |
| if(has_magic && |
| 139 |
3 |
| (ret = amagic_call(TARG, val, seq_amg, 0))) { |
| 140 |
3 |
▶ | if(SvTRUE(ret)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvTRUE(ret)) |
1 |
2 |
3 |
|
| 141 |
1 |
| return dispatch[idx]; |
| 142 |
|
| } |
| 143 |
9000054 |
▶ | else if(sv_eq(TARG, val)) |
Branch: 1 branch
| Branch | True | False | Total |
| else if(sv_eq(TARG, val)) |
9 |
9000045 |
9000054 |
|
| 144 |
9 |
| return dispatch[idx]; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
1000002 |
| return cDISPATCHOP->op_other; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| #ifdef HAVE_OP_ISA |
| 151 |
1 |
| static OP *pp_dispatch_isa(pTHX) |
| 152 |
|
| { |
| 153 |
1 |
| dDISPATCH; |
| 154 |
1 |
| dTARGET; |
| 155 |
|
| int idx; |
| 156 |
|
| |
| 157 |
1 |
▶ | for(idx = 0; idx < n_cases; idx++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(idx = 0; idx < n_cases; idx++) |
1 |
0 |
1 |
|
| 158 |
1 |
▶ | if(sv_isa_sv(TARG, values[idx])) |
Branch: 1 branch
| Branch | True | False | Total |
| if(sv_isa_sv(TARG, values[idx])) |
1 |
0 |
1 |
|
| 159 |
1 |
| return dispatch[idx]; |
| 160 |
|
| |
| 161 |
0 |
| return cDISPATCHOP->op_other; |
| 162 |
|
| } |
| 163 |
|
| #endif |
| 164 |
|
| |
| 165 |
|
| struct MatchCaseBlock { |
| 166 |
|
| int n_cases; |
| 167 |
|
| struct MatchCase { |
| 168 |
|
| bool is_if; |
| 169 |
|
| OP *expr; |
| 170 |
|
| } *cases; |
| 171 |
|
| |
| 172 |
|
| OP *op; |
| 173 |
|
| }; |
| 174 |
|
| |
| 175 |
67 |
| static OP *build_cases_nondispatch(pTHX_ XSParseInfixInfo *matchinfo, PADOFFSET padix, struct MatchCaseBlock *block, OP *elseop) |
| 176 |
|
| { |
| 177 |
67 |
| size_t n_cases = block->n_cases; |
| 178 |
|
| |
| 179 |
|
| assert(n_cases); |
| 180 |
|
| |
| 181 |
67 |
| OP *testop = NULL; |
| 182 |
|
| |
| 183 |
|
| U32 i; |
| 184 |
137 |
▶ | for(i = 0; i < n_cases; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < n_cases; i++) { |
70 |
67 |
137 |
|
| 185 |
70 |
| bool is_if = block->cases[i].is_if; |
| 186 |
70 |
| OP *caseop = block->cases[i].expr; |
| 187 |
|
| |
| 188 |
|
| OP *thistestop; |
| 189 |
|
| |
| 190 |
70 |
▶ | if(is_if) |
Branch: 1 branch
| Branch | True | False | Total |
| if(is_if) |
2 |
68 |
70 |
|
| 191 |
2 |
| thistestop = caseop; |
| 192 |
68 |
| else switch(matchinfo->opcode) { |
| 193 |
|
| #ifdef HAVE_OP_ISA |
| 194 |
62 |
| case OP_ISA: |
| 195 |
|
| #endif |
| 196 |
|
| case OP_SEQ: |
| 197 |
|
| case OP_EQ: |
| 198 |
62 |
| thistestop = newBINOP(matchinfo->opcode, 0, |
| 199 |
|
| newPADSVOP(OP_PADSV, 0, padix), caseop); |
| 200 |
62 |
| break; |
| 201 |
|
| |
| 202 |
6 |
| case OP_MATCH: |
| 203 |
6 |
▶ | if(caseop->op_type != OP_MATCH || cPMOPx(caseop)->op_first) |
Branch: 2 branches
| Branch | True | False | Total |
| if(caseop->op_type != OP_MATCH || cPMOPx(caseop)->op_first) |
6 |
0 |
6 |
| if(caseop->op_type != OP_MATCH || cPMOPx(caseop)->op_first) |
0 |
6 |
6 |
|
| 204 |
0 |
| croak("Expected a regexp match"); |
| 205 |
6 |
| thistestop = caseop; |
| 206 |
|
| #if HAVE_PERL_VERSION(5,22,0) |
| 207 |
6 |
| thistestop->op_targ = padix; |
| 208 |
|
| #else |
| 209 |
|
| cPMOPx(thistestop)->op_first = newPADSVOP(OP_PADSV, 0, padix); |
| 210 |
|
| thistestop->op_flags |= OPf_KIDS|OPf_STACKED; |
| 211 |
|
| #endif |
| 212 |
6 |
| break; |
| 213 |
0 |
| case OP_CUSTOM: |
| 214 |
0 |
| thistestop = xs_parse_infix_new_op(matchinfo, 0, |
| 215 |
|
| newPADSVOP(OP_PADSV, 0, padix), caseop); |
| 216 |
0 |
| break; |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
70 |
▶ | if(testop) |
Branch: 1 branch
| Branch | True | False | Total |
| if(testop) |
3 |
67 |
70 |
|
| 220 |
3 |
| testop = newLOGOP(OP_OR, 0, testop, thistestop); |
| 221 |
|
| else |
| 222 |
67 |
| testop = thistestop; |
| 223 |
|
| } |
| 224 |
|
| |
| 225 |
|
| assert(testop); |
| 226 |
|
| |
| 227 |
67 |
▶ | if(elseop) |
Branch: 1 branch
| Branch | True | False | Total |
| if(elseop) |
43 |
24 |
67 |
|
| 228 |
43 |
| return newCONDOP(0, testop, block->op, elseop); |
| 229 |
|
| else |
| 230 |
24 |
| return newLOGOP(OP_AND, 0, testop, block->op); |
| 231 |
|
| } |
| 232 |
|
| |
| 233 |
9 |
| static OP *build_cases_dispatch(pTHX_ OPCODE matchtype, PADOFFSET padix, size_t n_cases, struct MatchCaseBlock *blocks, OP *elseop) |
| 234 |
|
| { |
| 235 |
|
| assert(n_cases); |
| 236 |
|
| assert(matchtype != OP_MATCH); |
| 237 |
|
| |
| 238 |
|
| U32 blocki; |
| 239 |
|
| |
| 240 |
9 |
| ENTER; |
| 241 |
|
| |
| 242 |
9 |
| SV *valuessv = newSV(n_cases * sizeof(SV *)); |
| 243 |
9 |
| SV *dispatchsv = newSV(n_cases * sizeof(OP *)); |
| 244 |
9 |
| SAVEFREESV(valuessv); |
| 245 |
9 |
| SAVEFREESV(dispatchsv); |
| 246 |
|
| |
| 247 |
9 |
| SV **values = (SV **)SvPVX(valuessv); |
| 248 |
9 |
| OP **dispatch = (OP **)SvPVX(dispatchsv); |
| 249 |
|
| |
| 250 |
9 |
| DISPATCHOP *o = alloc_DISPATCHOP(); |
| 251 |
9 |
| o->op_type = OP_CUSTOM; |
| 252 |
9 |
| o->op_targ = padix; |
| 253 |
|
| |
| 254 |
9 |
| switch(matchtype) { |
| 255 |
|
| #ifdef HAVE_OP_ISA |
| 256 |
1 |
| case OP_ISA: o->op_ppaddr = &pp_dispatch_isa; break; |
| 257 |
|
| #endif |
| 258 |
3 |
| case OP_SEQ: o->op_ppaddr = &pp_dispatch_streq; break; |
| 259 |
5 |
| case OP_EQ: o->op_ppaddr = &pp_dispatch_numeq; break; |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
9 |
| o->op_first = NULL; |
| 263 |
|
| |
| 264 |
9 |
| o->n_cases = n_cases; |
| 265 |
9 |
| o->values = values; |
| 266 |
9 |
| o->dispatch = dispatch; |
| 267 |
|
| |
| 268 |
9 |
| OP *retop = newUNOP(OP_NULL, 0, (OP *)o); |
| 269 |
|
| |
| 270 |
9 |
| U32 idx = 0; |
| 271 |
9 |
| blocki = 0; |
| 272 |
41 |
▶ | while(n_cases) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(n_cases) { |
32 |
9 |
41 |
|
| 273 |
32 |
| struct MatchCaseBlock *block = &blocks[blocki]; |
| 274 |
|
| |
| 275 |
32 |
| U32 this_n_cases = block->n_cases; |
| 276 |
|
| |
| 277 |
32 |
| OP *blockop = block->op; |
| 278 |
32 |
▶ | OP *blockstart = LINKLIST(blockop); |
Branch: 1 branch
| Branch | True | False | Total |
| OP *blockstart = LINKLIST(blockop); |
0 |
32 |
32 |
|
| 279 |
32 |
| blockop->op_next = retop; |
| 280 |
|
| |
| 281 |
32 |
| n_cases -= this_n_cases; |
| 282 |
|
| |
| 283 |
64 |
▶ | for(U32 casei = 0; casei < this_n_cases; casei++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(U32 casei = 0; casei < this_n_cases; casei++) { |
32 |
32 |
64 |
|
| 284 |
32 |
| bool is_if = block->cases[casei].is_if; |
| 285 |
32 |
| OP *caseop = block->cases[casei].expr; |
| 286 |
|
| |
| 287 |
32 |
▶ | if(is_if) |
Branch: 1 branch
| Branch | True | False | Total |
| if(is_if) |
0 |
32 |
32 |
|
| 288 |
0 |
| croak("TODO: case if dispatch"); |
| 289 |
|
| |
| 290 |
|
| assert(caseop->op_type == OP_CONST); |
| 291 |
32 |
| values[idx] = SvREFCNT_inc(cSVOPx(caseop)->op_sv); |
| 292 |
32 |
| op_free(caseop); |
| 293 |
|
| |
| 294 |
32 |
| dispatch[idx] = blockstart; |
| 295 |
|
| |
| 296 |
32 |
| idx++; |
| 297 |
|
| } |
| 298 |
|
| |
| 299 |
|
| /* TODO: link chain of siblings */ |
| 300 |
|
| |
| 301 |
32 |
| blocki++; |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
9 |
▶ | if(elseop) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(elseop) { |
7 |
2 |
9 |
|
| 305 |
7 |
▶ | o->op_other = LINKLIST(elseop); |
Branch: 1 branch
| Branch | True | False | Total |
| o->op_other = LINKLIST(elseop); |
1 |
6 |
7 |
|
| 306 |
7 |
| elseop->op_next = retop; |
| 307 |
|
| /* TODO: sibling linkage */ |
| 308 |
|
| } |
| 309 |
|
| else { |
| 310 |
2 |
| o->op_other = retop; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| /* Steal the SV buffers */ |
| 314 |
9 |
| SvPVX(valuessv) = NULL; SvLEN(valuessv) = 0; |
| 315 |
9 |
| SvPVX(dispatchsv) = NULL; SvLEN(dispatchsv) = 0; |
| 316 |
|
| |
| 317 |
9 |
| LEAVE; |
| 318 |
|
| |
| 319 |
9 |
| return retop; |
| 320 |
|
| } |
| 321 |
|
| |
| 322 |
41 |
| static int build_match(pTHX_ OP **out, XSParseKeywordPiece *args[], size_t nargs, void *hookdata) |
| 323 |
|
| { |
| 324 |
|
| /* args: |
| 325 |
|
| * [0]: topic expression |
| 326 |
|
| * [1]: match type |
| 327 |
|
| * [2]: count of blocks |
| 328 |
|
| * [3]: count of case exprs = $N |
| 329 |
|
| * [4,5...]: $N * [if, case expr]s |
| 330 |
|
| * []: block |
| 331 |
|
| * [LAST]: default case if present |
| 332 |
|
| */ |
| 333 |
41 |
| U32 argi = 0; |
| 334 |
|
| |
| 335 |
41 |
| OP *topic = args[argi++]->op; |
| 336 |
41 |
| XSParseInfixInfo *matchinfo = args[argi++]->infix; |
| 337 |
41 |
| int n_blocks = args[argi++]->i; |
| 338 |
|
| |
| 339 |
|
| /* Extract the raw args into a better data structure we can work with */ |
| 340 |
|
| struct MatchCaseBlock *blocks; |
| 341 |
|
| |
| 342 |
41 |
| Newx(blocks, n_blocks, struct MatchCaseBlock); |
| 343 |
41 |
| SAVEFREEPV(blocks); |
| 344 |
|
| |
| 345 |
|
| int blocki; |
| 346 |
140 |
▶ | for(blocki = 0; blocki < n_blocks; blocki++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(blocki = 0; blocki < n_blocks; blocki++) { |
99 |
41 |
140 |
|
| 347 |
99 |
| struct MatchCaseBlock *block = &blocks[blocki]; |
| 348 |
|
| |
| 349 |
99 |
| int n_cases = args[argi++]->i; |
| 350 |
|
| |
| 351 |
99 |
| block->n_cases = n_cases; |
| 352 |
|
| |
| 353 |
99 |
| Newx(block->cases, n_cases, struct MatchCase); |
| 354 |
99 |
| SAVEFREEPV(block->cases); |
| 355 |
|
| |
| 356 |
201 |
▶ | for(int i = 0; i < n_cases; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i = 0; i < n_cases; i++) { |
102 |
99 |
201 |
|
| 357 |
102 |
| block->cases[i].is_if = args[argi++]->i; |
| 358 |
102 |
| block->cases[i].expr = args[argi++]->op; |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
99 |
| block->op = args[argi++]->op; |
| 362 |
|
| } |
| 363 |
|
| |
| 364 |
41 |
| bool has_default = args[argi]->i; |
| 365 |
41 |
| OP *o = NULL; |
| 366 |
41 |
▶ | if(has_default) |
Branch: 1 branch
| Branch | True | False | Total |
| if(has_default) |
15 |
26 |
41 |
|
| 367 |
15 |
| o = args[argi + 1]->op; |
| 368 |
|
| |
| 369 |
41 |
| bool use_dispatch = hv_fetchs(GvHV(PL_hintgv), "Syntax::Keyword::Match/experimental(dispatch)", 0); |
| 370 |
|
| |
| 371 |
|
| /* The name is totally meaningless and never used, but if we don't set a |
| 372 |
|
| * name and instead use pad_alloc(SVs_PADTMP) then the peephole optimiser |
| 373 |
|
| * for aassign will crash |
| 374 |
|
| */ |
| 375 |
41 |
| PADOFFSET padix = pad_add_name_pvs("$(Syntax::Keyword::Match/topic)", 0, NULL, NULL); |
| 376 |
41 |
| intro_my(); |
| 377 |
|
| |
| 378 |
41 |
| OP *startop = newBINOP(OP_SASSIGN, 0, |
| 379 |
|
| topic, newPADSVOP(OP_PADSV, OPf_MOD|OPf_REF|(OPpLVAL_INTRO << 8), padix)); |
| 380 |
41 |
| PL_hints |= HINT_BLOCK_SCOPE; /* ensures that op_scope() creates a full ENTER+LEAVE pair */ |
| 381 |
|
| |
| 382 |
41 |
| int n_dispatch = 0; |
| 383 |
|
| |
| 384 |
41 |
| blocki = n_blocks-1; |
| 385 |
|
| |
| 386 |
|
| /* Roll up the blocks backwards, from end to beginning */ |
| 387 |
140 |
▶ | while(blocki >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(blocki >= 0) { |
99 |
41 |
140 |
|
| 388 |
99 |
| struct MatchCaseBlock *block = &blocks[blocki--]; |
| 389 |
|
| |
| 390 |
99 |
| int n_cases = block->n_cases; |
| 391 |
|
| |
| 392 |
|
| /* perl expects a strict optree, where each block appears exactly once. |
| 393 |
|
| * We can't reüse the block between dispatch and non-dispatch ops, so |
| 394 |
|
| * we'll have to decide which strategy to use here |
| 395 |
|
| */ |
| 396 |
99 |
| bool this_block_dispatch = use_dispatch; |
| 397 |
|
| |
| 398 |
201 |
▶ | for(U32 casei = 0; casei < n_cases; casei++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(U32 casei = 0; casei < n_cases; casei++) { |
102 |
99 |
201 |
|
| 399 |
102 |
▶ | if(block->cases[casei].is_if) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(block->cases[casei].is_if) { |
2 |
100 |
102 |
|
| 400 |
2 |
| this_block_dispatch = false; |
| 401 |
2 |
| continue; |
| 402 |
|
| } |
| 403 |
|
| |
| 404 |
|
| /* TODO: forbid the , operator in the case label */ |
| 405 |
100 |
| OP *caseop = block->cases[casei].expr; |
| 406 |
|
| |
| 407 |
100 |
| switch(matchinfo->opcode) { |
| 408 |
|
| #ifdef HAVE_OP_ISA |
| 409 |
9 |
| case OP_ISA: |
| 410 |
|
| /* bareword class names are permitted */ |
| 411 |
9 |
▶ | if(caseop->op_type == OP_CONST && caseop->op_private & OPpCONST_BARE) |
Branch: 2 branches
| Branch | True | False | Total |
| if(caseop->op_type == OP_CONST && caseop->op_private & OPpCONST_BARE) |
9 |
0 |
9 |
| if(caseop->op_type == OP_CONST && caseop->op_private & OPpCONST_BARE) |
9 |
0 |
9 |
|
| 412 |
9 |
| caseop->op_private &= ~(OPpCONST_BARE|OPpCONST_STRICT); |
| 413 |
|
| /* FALLTHROUGH */ |
| 414 |
|
| #endif |
| 415 |
|
| case OP_SEQ: |
| 416 |
|
| case OP_EQ: |
| 417 |
94 |
▶ | if(use_dispatch && caseop->op_type == OP_CONST) |
Branch: 2 branches
| Branch | True | False | Total |
| if(use_dispatch && caseop->op_type == OP_CONST) |
33 |
61 |
94 |
| if(use_dispatch && caseop->op_type == OP_CONST) |
32 |
1 |
33 |
|
| 418 |
32 |
| continue; |
| 419 |
|
| |
| 420 |
|
| /* FALLTHROUGH */ |
| 421 |
|
| case OP_MATCH: |
| 422 |
|
| case OP_CUSTOM: |
| 423 |
68 |
| this_block_dispatch = false; |
| 424 |
68 |
| break; |
| 425 |
|
| } |
| 426 |
|
| } |
| 427 |
|
| |
| 428 |
99 |
▶ | if(this_block_dispatch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(this_block_dispatch) { |
32 |
67 |
99 |
|
| 429 |
32 |
| n_dispatch += n_cases; |
| 430 |
32 |
| continue; |
| 431 |
|
| } |
| 432 |
|
| |
| 433 |
67 |
▶ | if(n_dispatch) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(n_dispatch) { |
1 |
66 |
67 |
|
| 434 |
1 |
| o = build_cases_dispatch(aTHX_ matchinfo->opcode, padix, |
| 435 |
|
| n_dispatch, block + 1, o); |
| 436 |
1 |
| n_dispatch = 0; |
| 437 |
|
| } |
| 438 |
|
| |
| 439 |
67 |
| o = build_cases_nondispatch(aTHX_ matchinfo, padix, block, o); |
| 440 |
|
| } |
| 441 |
|
| |
| 442 |
41 |
▶ | if(n_dispatch) |
Branch: 1 branch
| Branch | True | False | Total |
| if(n_dispatch) |
8 |
33 |
41 |
|
| 443 |
8 |
| o = build_cases_dispatch(aTHX_ matchinfo->opcode, padix, |
| 444 |
|
| n_dispatch, blocks, o); |
| 445 |
|
| |
| 446 |
41 |
| *out = newLISTOP(OP_LINESEQ, 0, startop, o); |
| 447 |
|
| |
| 448 |
41 |
| return KEYWORD_PLUGIN_STMT; |
| 449 |
|
| } |
| 450 |
|
| |
| 451 |
|
| static const struct XSParseKeywordHooks hooks_match = { |
| 452 |
|
| .flags = XPK_FLAG_BLOCKSCOPE, |
| 453 |
|
| .permit_hintkey = "Syntax::Keyword::Match/match", |
| 454 |
|
| |
| 455 |
|
| .pieces = (const struct XSParseKeywordPieceType []){ |
| 456 |
|
| XPK_PARENS( /* ( EXPR : OP ) */ |
| 457 |
|
| XPK_TERMEXPR_SCALARCTX, |
| 458 |
|
| XPK_COLON, |
| 459 |
|
| XPK_INFIX_MATCH_NOSMART |
| 460 |
|
| ), |
| 461 |
|
| XPK_INTRO_MY, |
| 462 |
|
| XPK_BRACES( /* { blocks... } */ |
| 463 |
|
| XPK_REPEATED( /* case (EXPR) {BLOCK} */ |
| 464 |
|
| XPK_COMMALIST( |
| 465 |
|
| XPK_KEYWORD("case"), |
| 466 |
|
| XPK_OPTIONAL( XPK_KEYWORD("if") ), |
| 467 |
|
| XPK_PARENS( XPK_TERMEXPR_SCALARCTX ) |
| 468 |
|
| ), |
| 469 |
|
| XPK_BLOCK |
| 470 |
|
| ), |
| 471 |
|
| XPK_OPTIONAL( /* default { ... } */ |
| 472 |
|
| XPK_KEYWORD("default"), |
| 473 |
|
| XPK_BLOCK |
| 474 |
|
| ) |
| 475 |
|
| ), |
| 476 |
|
| 0, |
| 477 |
|
| }, |
| 478 |
|
| .build = &build_match, |
| 479 |
|
| }; |
| 480 |
|
| |
| 481 |
|
| #ifndef HAVE_OP_ISA |
| 482 |
|
| #include "hax/newOP_CUSTOM.c.inc" |
| 483 |
|
| |
| 484 |
|
| /* Can't use sv_isa_sv() because that was only added in 5.32 */ |
| 485 |
|
| static bool S_sv_isa_sv(pTHX_ SV *sv, SV *namesv) |
| 486 |
|
| { |
| 487 |
|
| if(!SvROK(sv) || !SvOBJECT(SvRV(sv))) |
| 488 |
|
| return FALSE; |
| 489 |
|
| |
| 490 |
|
| /* Also can't use GV_NOUNIVERSAL here because that also only turned up in 5.32 */ |
| 491 |
|
| GV *isagv = gv_fetchmeth_pvn(SvSTASH(SvRV(sv)), "isa", 3, -1, 0); |
| 492 |
|
| /* This probably finds UNIVERSAL::isa; if so we can avoid it and just do it |
| 493 |
|
| * directly ourselves by calling sv_derived_from_sv() |
| 494 |
|
| */ |
| 495 |
|
| if(isagv && !strEQ(HvNAME(GvSTASH(isagv)), "UNIVERSAL")) { |
| 496 |
|
| dSP; |
| 497 |
|
| CV *isacv = isGV(isagv) ? GvCV(isagv) : MUTABLE_CV(isagv); |
| 498 |
|
| |
| 499 |
|
| PUTBACK; |
| 500 |
|
| |
| 501 |
|
| ENTER; |
| 502 |
|
| SAVETMPS; |
| 503 |
|
| |
| 504 |
|
| EXTEND(SP, 2); |
| 505 |
|
| PUSHMARK(SP); |
| 506 |
|
| PUSHs(sv); |
| 507 |
|
| PUSHs(namesv); |
| 508 |
|
| PUTBACK; |
| 509 |
|
| |
| 510 |
|
| call_sv((SV *)isacv, G_SCALAR); |
| 511 |
|
| |
| 512 |
|
| SPAGAIN; |
| 513 |
|
| SV *retsv = POPs; |
| 514 |
|
| bool ret = SvTRUE(retsv); |
| 515 |
|
| PUTBACK; |
| 516 |
|
| |
| 517 |
|
| FREETMPS; |
| 518 |
|
| LEAVE; |
| 519 |
|
| |
| 520 |
|
| return ret; |
| 521 |
|
| } |
| 522 |
|
| |
| 523 |
|
| return sv_derived_from_sv(sv, namesv, 0); |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
|
| static OP *pp_isa(pTHX) |
| 527 |
|
| { |
| 528 |
|
| dSP; |
| 529 |
|
| SV *rhs = POPs; |
| 530 |
|
| SV *lhs = TOPs; |
| 531 |
|
| |
| 532 |
|
| SETs(boolSV(S_sv_isa_sv(aTHX_ lhs, rhs))); |
| 533 |
|
| RETURN; |
| 534 |
|
| } |
| 535 |
|
| |
| 536 |
|
| static OP *newop_isa(pTHX_ U32 flags, OP *lhs, OP *rhs, SV **parsedata, void *hookdata) |
| 537 |
|
| { |
| 538 |
|
| /* Avoid strictness failure on bareword RHS */ |
| 539 |
|
| if(rhs->op_type == OP_CONST && rhs->op_private & OPpCONST_BARE) |
| 540 |
|
| rhs->op_private &= ~(OPpCONST_BARE|OPpCONST_STRICT); |
| 541 |
|
| |
| 542 |
|
| return newBINOP_CUSTOM(&pp_isa, flags, lhs, rhs); |
| 543 |
|
| } |
| 544 |
|
| |
| 545 |
|
| static const struct XSParseInfixHooks hooks_isa = { |
| 546 |
|
| .flags = 0, |
| 547 |
|
| .cls = XPI_CLS_ISA, |
| 548 |
|
| |
| 549 |
|
| .new_op = &newop_isa, |
| 550 |
|
| }; |
| 551 |
|
| #endif |
| 552 |
|
| |
| 553 |
|
| MODULE = Syntax::Keyword::Match PACKAGE = Syntax::Keyword::Match |
| 554 |
|
| |
| 555 |
|
| BOOT: |
| 556 |
18 |
| boot_xs_parse_keyword(0.36); |
| 557 |
18 |
| boot_xs_parse_infix(0); |
| 558 |
|
| |
| 559 |
18 |
| register_xs_parse_keyword("match", &hooks_match, NULL); |
| 560 |
|
| #ifndef HAVE_OP_ISA |
| 561 |
|
| register_xs_parse_infix("isa", &hooks_isa, NULL); |
| 562 |
|
| #endif |