| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include "ppport.h" |
| 6 |
|
| #include "uinteger_ppp.h" |
| 7 |
|
| |
| 8 |
|
| enum uint_xop_index { |
| 9 |
|
| xi_u_add, |
| 10 |
|
| xi_u_subtract, |
| 11 |
|
| xi_u_multiply, |
| 12 |
|
| xi_u_negate, |
| 13 |
|
| xi_op_count |
| 14 |
|
| }; |
| 15 |
|
| |
| 16 |
|
| #ifdef op_sibling_splice |
| 17 |
|
| # define DO_TARGMY |
| 18 |
|
| #endif |
| 19 |
|
| |
| 20 |
|
| static XOP xops[xi_op_count]; |
| 21 |
|
| |
| 22 |
|
| static void (*next_rpeepp)(pTHX_ OP *o); |
| 23 |
|
| |
| 24 |
|
| typedef OP *(*checker_type)(pTHX_ OP *o); |
| 25 |
|
| typedef OP *(*ppfunc_type)(pTHX); |
| 26 |
|
| |
| 27 |
|
| static checker_type next_add_checker; |
| 28 |
|
| static checker_type next_subtract_checker; |
| 29 |
|
| static checker_type next_multiply_checker; |
| 30 |
|
| static checker_type next_negate_checker; |
| 31 |
|
| |
| 32 |
|
| static checker_type next_sassign_checker; |
| 33 |
|
| |
| 34 |
|
| static bool |
| 35 |
0 |
| in_uinteger(pTHX) { |
| 36 |
0 |
| SV **entry = hv_fetchs(GvHV(PL_hintgv), "uinteger", 0); |
| 37 |
0 |
▶ | return entry && SvTRUE(*entry); |
Branch: 2 branches
| Branch | True | False | Total |
| return entry && SvTRUE(*entry); |
0 |
0 |
0 |
| return entry && SvTRUE(*entry); |
0 |
0 |
0 |
|
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| static OP * |
| 41 |
0 |
| integer_checker(pTHX_ OP *op, checker_type next, OP* (*ppfunc)(pTHX)) { |
| 42 |
0 |
▶ | if (in_uinteger(aTHX)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (in_uinteger(aTHX)) { |
0 |
0 |
0 |
|
| 43 |
0 |
| op->op_type = OP_CUSTOM; |
| 44 |
0 |
| op->op_ppaddr = ppfunc; |
| 45 |
|
| // newBINOP skips this if we change the opcode |
| 46 |
0 |
▶ | if (!op->op_targ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!op->op_targ) |
0 |
0 |
0 |
|
| 47 |
0 |
| op->op_targ = pad_alloc(op->op_type, SVs_PADTMP); |
| 48 |
0 |
| op = op_contextualize(op, G_SCALAR); |
| 49 |
|
| } |
| 50 |
|
| else { |
| 51 |
0 |
| op = next(aTHX_ op); |
| 52 |
|
| } |
| 53 |
0 |
| return op; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
|
| static OP * |
| 57 |
0 |
| pp_u_add(pTHX) { |
| 58 |
0 |
| SV *targ = (PL_op->op_flags & OPf_STACKED) |
| 59 |
|
| ? PL_stack_sp[-1] |
| 60 |
0 |
▶ | : PAD_SV(PL_op->op_targ); |
Branch: 1 branch
| Branch | True | False | Total |
| : PAD_SV(PL_op->op_targ); |
0 |
0 |
0 |
|
| 61 |
|
| |
| 62 |
0 |
▶ | if (rpp_try_AMAGIC_2(add_amg, AMGf_assign)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rpp_try_AMAGIC_2(add_amg, AMGf_assign)) |
0 |
0 |
0 |
|
| 63 |
0 |
| return NORMAL; |
| 64 |
|
| |
| 65 |
0 |
| SV *leftsv = PL_stack_sp[-1]; |
| 66 |
0 |
▶ | UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
0 |
0 |
0 |
| UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
0 |
0 |
0 |
|
| 67 |
0 |
| UV right = (UV)SvIV_nomg(PL_stack_sp[0]); |
| 68 |
|
| |
| 69 |
0 |
▶ | TARGu(left + right, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| TARGu(left + right, 1); |
0 |
0 |
0 |
|
| 70 |
0 |
| rpp_replace_2_1_NN(targ); |
| 71 |
0 |
| return NORMAL; |
| 72 |
|
| |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| static OP * |
| 76 |
0 |
| pp_u_subtract(pTHX) { |
| 77 |
0 |
| SV *targ = (PL_op->op_flags & OPf_STACKED) |
| 78 |
|
| ? PL_stack_sp[-1] |
| 79 |
0 |
▶ | : PAD_SV(PL_op->op_targ); |
Branch: 1 branch
| Branch | True | False | Total |
| : PAD_SV(PL_op->op_targ); |
0 |
0 |
0 |
|
| 80 |
|
| |
| 81 |
0 |
▶ | if (rpp_try_AMAGIC_2(subtr_amg, AMGf_assign)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rpp_try_AMAGIC_2(subtr_amg, AMGf_assign)) |
0 |
0 |
0 |
|
| 82 |
0 |
| return NORMAL; |
| 83 |
|
| |
| 84 |
0 |
| SV *leftsv = PL_stack_sp[-1]; |
| 85 |
0 |
▶ | UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
0 |
0 |
0 |
| UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
0 |
0 |
0 |
|
| 86 |
0 |
| UV right = (UV)SvIV_nomg(PL_stack_sp[0]); |
| 87 |
|
| |
| 88 |
0 |
▶ | TARGu(left - right, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| TARGu(left - right, 1); |
0 |
0 |
0 |
|
| 89 |
0 |
| rpp_replace_2_1_NN(targ); |
| 90 |
0 |
| return NORMAL; |
| 91 |
|
| |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| static OP * |
| 95 |
0 |
| pp_u_multiply(pTHX) { |
| 96 |
0 |
| SV *targ = (PL_op->op_flags & OPf_STACKED) |
| 97 |
|
| ? PL_stack_sp[-1] |
| 98 |
0 |
▶ | : PAD_SV(PL_op->op_targ); |
Branch: 1 branch
| Branch | True | False | Total |
| : PAD_SV(PL_op->op_targ); |
0 |
0 |
0 |
|
| 99 |
|
| |
| 100 |
0 |
▶ | if (rpp_try_AMAGIC_2(mult_amg, AMGf_assign)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rpp_try_AMAGIC_2(mult_amg, AMGf_assign)) |
0 |
0 |
0 |
|
| 101 |
0 |
| return NORMAL; |
| 102 |
|
| |
| 103 |
0 |
| SV *leftsv = PL_stack_sp[-1]; |
| 104 |
0 |
▶ | UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
0 |
0 |
0 |
| UV left = USE_LEFT(leftsv) ? (UV)SvIV_nomg(leftsv) : 0; |
0 |
0 |
0 |
|
| 105 |
0 |
| UV right = (UV)SvIV_nomg(PL_stack_sp[0]); |
| 106 |
|
| |
| 107 |
0 |
▶ | TARGu(left * right, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| TARGu(left * right, 1); |
0 |
0 |
0 |
|
| 108 |
0 |
| rpp_replace_2_1_NN(targ); |
| 109 |
0 |
| return NORMAL; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| static OP * |
| 113 |
0 |
| pp_u_negate(pTHX) { |
| 114 |
0 |
| dTARGET; |
| 115 |
0 |
▶ | if (rpp_try_AMAGIC_1(neg_amg, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rpp_try_AMAGIC_1(neg_amg, 0)) |
0 |
0 |
0 |
|
| 116 |
0 |
| return NORMAL; |
| 117 |
|
| |
| 118 |
0 |
| SV * const sv = *PL_stack_sp; |
| 119 |
|
| |
| 120 |
0 |
| UV const i = SvIV_nomg(sv); |
| 121 |
0 |
▶ | TARGu(-(UV)i, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| TARGu(-(UV)i, 1); |
0 |
0 |
0 |
|
| 122 |
0 |
▶ | if (LIKELY(targ != sv)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (LIKELY(targ != sv)) |
0 |
0 |
0 |
|
| 123 |
0 |
| rpp_replace_1_1_NN(TARG); |
| 124 |
0 |
| return NORMAL; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
|
| static ppfunc_type ppfuncs[] = |
| 128 |
|
| { |
| 129 |
|
| pp_u_add, |
| 130 |
|
| pp_u_subtract, |
| 131 |
|
| pp_u_multiply, |
| 132 |
|
| pp_u_negate |
| 133 |
|
| }; |
| 134 |
|
| |
| 135 |
|
| static OP * |
| 136 |
0 |
| add_checker(pTHX_ OP *op) { |
| 137 |
0 |
| return integer_checker(aTHX_ op, next_add_checker, pp_u_add); |
| 138 |
|
| } |
| 139 |
|
| |
| 140 |
|
| static OP * |
| 141 |
0 |
| subtract_checker(pTHX_ OP *op) { |
| 142 |
0 |
| return integer_checker(aTHX_ op, next_subtract_checker, pp_u_subtract); |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
|
| static OP * |
| 146 |
0 |
| multiply_checker(pTHX_ OP *op) { |
| 147 |
0 |
| return integer_checker(aTHX_ op, next_multiply_checker, pp_u_multiply); |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| static OP * |
| 151 |
0 |
| negate_checker(pTHX_ OP *op) { |
| 152 |
0 |
| return integer_checker(aTHX_ op, next_negate_checker, pp_u_negate); |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
|
| #ifdef XOPf_xop_dump |
| 156 |
|
| static void |
| 157 |
0 |
| my_xop_dump(pTHX_ const OP *o, struct Perl_OpDumpContext *ctx) { |
| 158 |
0 |
| Perl_opdump_printf(aTHX_ ctx, "XOPPRIVATE = (%s0x%x)", |
| 159 |
0 |
| (o->op_private & OPpTARGET_MY) ? "TARGMY," : "", |
| 160 |
0 |
▶ | (o->op_private & OPpARG4_MASK)); |
Branch: 1 branch
| Branch | True | False | Total |
| (o->op_private & OPpARG4_MASK)); |
0 |
0 |
0 |
|
| 161 |
0 |
| } |
| 162 |
|
| #endif |
| 163 |
|
| |
| 164 |
|
| static inline void |
| 165 |
0 |
| xop_register(pTHX_ enum uint_xop_index xop_index, const char *name, |
| 166 |
|
| const char *desc, U32 cls, ppfunc_type ppfunc) { |
| 167 |
0 |
| XOP *const xop = xops + xop_index; |
| 168 |
0 |
| XopENTRY_set(xop, xop_name, name); |
| 169 |
0 |
| XopENTRY_set(xop, xop_desc, desc); |
| 170 |
0 |
| XopENTRY_set(xop, xop_class, cls); |
| 171 |
|
| #ifdef XOPf_xop_dump |
| 172 |
0 |
| XopENTRY_set(xop, xop_dump, my_xop_dump); |
| 173 |
|
| #endif |
| 174 |
|
| |
| 175 |
0 |
| Perl_custom_op_register(aTHX_ ppfunc, xop); |
| 176 |
0 |
| } |
| 177 |
|
| |
| 178 |
|
| #ifdef DO_TARGMY |
| 179 |
|
| |
| 180 |
|
| static enum uint_xop_index |
| 181 |
0 |
| find_xop_index(ppfunc_type pp) { |
| 182 |
|
| int i; |
| 183 |
0 |
▶ | for (i = 0; i < xi_op_count; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < xi_op_count; ++i) { |
0 |
0 |
0 |
|
| 184 |
0 |
▶ | if (ppfuncs[i] == pp) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ppfuncs[i] == pp) |
0 |
0 |
0 |
|
| 185 |
0 |
| break; |
| 186 |
|
| } |
| 187 |
0 |
| return (enum uint_xop_index)i; |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* do the targlex optimization for our ops */ |
| 191 |
|
| static OP * |
| 192 |
0 |
| sassign_checker(pTHX_ OP *o) { |
| 193 |
0 |
| o = next_sassign_checker(aTHX_ o); |
| 194 |
|
| |
| 195 |
|
| /* adapted from S_maybe_targlex() */ |
| 196 |
0 |
| OP *const kid = cLISTOPo->op_first; |
| 197 |
|
| enum uint_xop_index i; |
| 198 |
0 |
▶ | if (kid->op_type == OP_CUSTOM |
Branch: 1 branch
| Branch | True | False | Total |
| if (kid->op_type == OP_CUSTOM |
0 |
0 |
0 |
|
| 199 |
0 |
▶ | && !(kid->op_flags & OPf_STACKED) |
Branch: 1 branch
| Branch | True | False | Total |
| && !(kid->op_flags & OPf_STACKED) |
0 |
0 |
0 |
|
| 200 |
0 |
▶ | && !(kid->op_private & OPpTARGET_MY) |
Branch: 1 branch
| Branch | True | False | Total |
| && !(kid->op_private & OPpTARGET_MY) |
0 |
0 |
0 |
|
| 201 |
0 |
▶ | && (i = find_xop_index(kid->op_ppaddr)) != xi_op_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| && (i = find_xop_index(kid->op_ppaddr)) != xi_op_count) { |
0 |
0 |
0 |
|
| 202 |
0 |
▶ | OP * const kkid = OpSIBLING(kid); |
Branch: 1 branch
| Branch | True | False | Total |
| OP * const kkid = OpSIBLING(kid); |
0 |
0 |
0 |
|
| 203 |
0 |
▶ | if (kkid && kkid->op_type == OP_PADSV) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kkid && kkid->op_type == OP_PADSV) { |
0 |
0 |
0 |
| if (kkid && kkid->op_type == OP_PADSV) { |
0 |
0 |
0 |
|
| 204 |
0 |
▶ | if (!(kkid->op_private & OPpLVAL_INTRO) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(kkid->op_private & OPpLVAL_INTRO) |
0 |
0 |
0 |
|
| 205 |
0 |
▶ | || (kkid->op_private & OPpPAD_STATE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| || (kkid->op_private & OPpPAD_STATE)) { |
0 |
0 |
0 |
|
| 206 |
0 |
| kid->op_private |= OPpTARGET_MY; |
| 207 |
0 |
| kid->op_flags = |
| 208 |
0 |
| (kid->op_flags & ~OPf_WANT) |
| 209 |
0 |
| | (o->op_flags & OPf_WANT); |
| 210 |
0 |
| kid->op_targ = kkid->op_targ; |
| 211 |
0 |
| kkid->op_targ = 0; |
| 212 |
0 |
| op_sibling_splice(o, NULL, 1, NULL); |
| 213 |
0 |
| op_free(o); |
| 214 |
0 |
| return kid; |
| 215 |
|
| } |
| 216 |
|
| } |
| 217 |
|
| } |
| 218 |
|
| |
| 219 |
0 |
| return o; |
| 220 |
|
| } |
| 221 |
|
| |
| 222 |
|
| #endif |
| 223 |
|
| |
| 224 |
|
| static void |
| 225 |
0 |
| init_ops(pTHX) { |
| 226 |
0 |
| xop_register(aTHX_ xi_u_add, "u_add", "add unsigned integers", OA_BINOP, |
| 227 |
|
| pp_u_add); |
| 228 |
|
| |
| 229 |
0 |
| xop_register(aTHX_ xi_u_subtract, "u_subtract", "subtract unsigned integers", |
| 230 |
|
| OA_BINOP, pp_u_subtract); |
| 231 |
|
| |
| 232 |
0 |
| xop_register(aTHX_ xi_u_multiply, "u_multiply", "multiply unsigned integers", |
| 233 |
|
| OA_BINOP, pp_u_multiply); |
| 234 |
|
| |
| 235 |
0 |
| xop_register(aTHX_ xi_u_negate, "u_negate", "negate unsigned integers", |
| 236 |
|
| OA_UNOP, pp_u_negate); |
| 237 |
|
| |
| 238 |
0 |
| wrap_op_checker(OP_ADD, add_checker, &next_add_checker); |
| 239 |
0 |
| wrap_op_checker(OP_SUBTRACT, subtract_checker, &next_subtract_checker); |
| 240 |
0 |
| wrap_op_checker(OP_MULTIPLY, multiply_checker, &next_multiply_checker); |
| 241 |
0 |
| wrap_op_checker(OP_NEGATE, negate_checker, &next_negate_checker); |
| 242 |
|
| |
| 243 |
|
| #ifdef DO_TARGMY |
| 244 |
0 |
| wrap_op_checker(OP_SASSIGN, sassign_checker, &next_sassign_checker); |
| 245 |
|
| #endif |
| 246 |
0 |
| } |
| 247 |
|
| |
| 248 |
|
| MODULE = uinteger PACKAGE = uinteger |
| 249 |
|
| |
| 250 |
|
| BOOT: |
| 251 |
0 |
| init_ops(aTHX); |