3 * Copyright (c) 1991-1997, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "It's a big house this, and very peculiar. Always a bit more to discover,
12 * and no knowing what you'll find around a corner. And Elves, sir!" --Samwise
19 * The compiler on Concurrent CX/UX systems has a subtle bug which only
20 * seems to show up when compiling pp.c - it generates the wrong double
21 * precision constant value for (double)UV_MAX when used inline in the body
22 * of the code below, so this makes a static variable up front (which the
23 * compiler seems to get correct) and uses it in place of UV_MAX below.
25 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
26 static double UV_MAX_cxux = ((double)UV_MAX);
30 * Types used in bitwise operations.
32 * Normally we'd just use IV and UV. However, some hardware and
33 * software combinations (e.g. Alpha and current OSF/1) don't have a
34 * floating-point type to use for NV that has adequate bits to fully
35 * hold an IV/UV. (In other words, sizeof(long) == sizeof(double).)
37 * It just so happens that "int" is the right size almost everywhere.
43 * Mask used after bitwise operations.
45 * There is at least one realm (Cray word machines) that doesn't
46 * have an integral type (except char) small enough to be represented
47 * in a double without loss; that is, it has no 32-bit type.
49 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
51 # define BW_MASK ((1 << BW_BITS) - 1)
52 # define BW_SIGN (1 << (BW_BITS - 1))
53 # define BWi(i) (((i) & BW_SIGN) ? ((i) | ~BW_MASK) : ((i) & BW_MASK))
54 # define BWu(u) ((u) & BW_MASK)
61 * Offset for integer pack/unpack.
63 * On architectures where I16 and I32 aren't really 16 and 32 bits,
64 * which for now are all Crays, pack and unpack have to play games.
68 * These values are required for portability of pack() output.
69 * If they're not right on your machine, then pack() and unpack()
70 * wouldn't work right anyway; you'll need to apply the Cray hack.
71 * (I'd like to check them with #if, but you can't use sizeof() in
77 #if BYTEORDER > 0xFFFF && defined(_CRAY) && !defined(_CRAYMPP)
78 # if BYTEORDER == 0x12345678
79 # define OFF16(p) (char*)(p)
80 # define OFF32(p) (char*)(p)
82 # if BYTEORDER == 0x87654321
83 # define OFF16(p) ((char*)(p) + (sizeof(U16) - SIZE16))
84 # define OFF32(p) ((char*)(p) + (sizeof(U32) - SIZE32))
86 }}}} bad cray byte order
89 # define COPY16(s,p) (*(p) = 0, Copy(s, OFF16(p), SIZE16, char))
90 # define COPY32(s,p) (*(p) = 0, Copy(s, OFF32(p), SIZE32, char))
91 # define CAT16(sv,p) sv_catpvn(sv, OFF16(p), SIZE16)
92 # define CAT32(sv,p) sv_catpvn(sv, OFF32(p), SIZE32)
94 # define COPY16(s,p) Copy(s, p, SIZE16, char)
95 # define COPY32(s,p) Copy(s, p, SIZE32, char)
96 # define CAT16(sv,p) sv_catpvn(sv, (char*)(p), SIZE16)
97 # define CAT32(sv,p) sv_catpvn(sv, (char*)(p), SIZE32)
100 static void doencodes _((SV* sv, char* s, I32 len));
101 static SV* refto _((SV* sv));
102 static U32 seed _((void));
104 static bool srand_called = FALSE;
106 /* variations on pp_null */
108 #ifdef DONT_DECLARE_STD
113 extern pid_t getpid (void);
119 if (GIMME_V == G_SCALAR)
134 if (op->op_private & OPpLVAL_INTRO)
135 SAVECLEARSV(curpad[op->op_targ]);
137 if (op->op_flags & OPf_REF) {
141 if (GIMME == G_ARRAY) {
142 I32 maxarg = AvFILL((AV*)TARG) + 1;
144 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
148 SV* sv = sv_newmortal();
149 I32 maxarg = AvFILL((AV*)TARG) + 1;
150 sv_setiv(sv, maxarg);
162 if (op->op_private & OPpLVAL_INTRO)
163 SAVECLEARSV(curpad[op->op_targ]);
164 if (op->op_flags & OPf_REF)
167 if (gimme == G_ARRAY) {
168 RETURNOP(do_kv(ARGS));
170 else if (gimme == G_SCALAR) {
171 SV* sv = sv_newmortal();
172 if (HvFILL((HV*)TARG))
173 sv_setpvf(sv, "%ld/%ld",
174 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
184 DIE("NOT IMPL LINE %d",__LINE__);
196 if (SvTYPE(sv) == SVt_PVIO) {
197 GV *gv = (GV*) sv_newmortal();
198 gv_init(gv, 0, "", 0, 0);
199 GvIOp(gv) = (IO *)sv;
200 (void)SvREFCNT_inc(sv);
202 } else if (SvTYPE(sv) != SVt_PVGV)
203 DIE("Not a GLOB reference");
206 if (SvTYPE(sv) != SVt_PVGV) {
209 if (SvGMAGICAL(sv)) {
215 if (op->op_flags & OPf_REF ||
216 op->op_private & HINT_STRICT_REFS)
217 DIE(no_usym, "a symbol");
223 if (op->op_private & HINT_STRICT_REFS)
224 DIE(no_symref, sym, "a symbol");
225 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
228 if (op->op_private & OPpLVAL_INTRO)
229 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
241 switch (SvTYPE(sv)) {
245 DIE("Not a SCALAR reference");
252 if (SvTYPE(gv) != SVt_PVGV) {
253 if (SvGMAGICAL(sv)) {
259 if (op->op_flags & OPf_REF ||
260 op->op_private & HINT_STRICT_REFS)
261 DIE(no_usym, "a SCALAR");
267 if (op->op_private & HINT_STRICT_REFS)
268 DIE(no_symref, sym, "a SCALAR");
269 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
273 if (op->op_flags & OPf_MOD) {
274 if (op->op_private & OPpLVAL_INTRO)
275 sv = save_scalar((GV*)TOPs);
276 else if (op->op_private & OPpDEREF)
277 vivify_ref(sv, op->op_private & OPpDEREF);
287 SV *sv = AvARYLEN(av);
289 AvARYLEN(av) = sv = NEWSV(0,0);
290 sv_upgrade(sv, SVt_IV);
291 sv_magic(sv, (SV*)av, '#', Nullch, 0);
299 djSP; dTARGET; dPOPss;
301 if (op->op_flags & OPf_MOD) {
302 if (SvTYPE(TARG) < SVt_PVLV) {
303 sv_upgrade(TARG, SVt_PVLV);
304 sv_magic(TARG, Nullsv, '.', Nullch, 0);
309 PUSHs(TARG); /* no SvSETMAGIC */
315 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
316 mg = mg_find(sv, 'g');
317 if (mg && mg->mg_len >= 0) {
318 PUSHi(mg->mg_len + curcop->cop_arybase);
332 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
333 /* (But not in defined().) */
334 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
337 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
354 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
356 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
364 CV* cv = (CV*)curpad[op->op_targ];
366 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
382 if (GIMME != G_ARRAY) {
386 EXTEND_MORTAL(SP - MARK);
388 *MARK = refto(*MARK);
397 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
400 if (!(sv = LvTARG(sv)))
403 else if (SvPADTMP(sv))
407 (void)SvREFCNT_inc(sv);
410 sv_upgrade(rv, SVt_RV);
424 if (sv && SvGMAGICAL(sv))
427 if (!sv || !SvROK(sv))
431 pv = sv_reftype(sv,TRUE);
432 PUSHp(pv, strlen(pv));
442 stash = curcop->cop_stash;
444 stash = gv_stashsv(POPs, TRUE);
446 (void)sv_bless(TOPs, stash);
463 switch (elem ? *elem : '\0')
466 if (strEQ(elem, "ARRAY"))
470 if (strEQ(elem, "CODE"))
471 ref = (SV*)GvCVu(gv);
474 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
475 ref = (SV*)GvIOp(gv);
478 if (strEQ(elem, "GLOB"))
482 if (strEQ(elem, "HASH"))
486 if (strEQ(elem, "IO"))
487 ref = (SV*)GvIOp(gv);
490 if (strEQ(elem, "NAME"))
491 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
494 if (strEQ(elem, "PACKAGE"))
495 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
498 if (strEQ(elem, "SCALAR"))
512 /* Pattern matching */
517 register UNOP *unop = cUNOP;
518 register unsigned char *s;
521 register I32 *sfirst;
525 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
526 PMOP *pm = (PMOP *)unop->op_first;
527 SV *rv = sv_newmortal();
528 sv = newSVrv(rv, "Regexp");
529 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
533 if (sv == lastscream) {
539 SvSCREAM_off(lastscream);
540 SvREFCNT_dec(lastscream);
542 lastscream = SvREFCNT_inc(sv);
545 s = (unsigned char*)(SvPV(sv, len));
549 if (pos > maxscream) {
551 maxscream = pos + 80;
552 New(301, screamfirst, 256, I32);
553 New(302, screamnext, maxscream, I32);
556 maxscream = pos + pos / 4;
557 Renew(screamnext, maxscream, I32);
561 sfirst = screamfirst;
564 if (!sfirst || !snext)
565 DIE("do_study: out of memory");
567 for (ch = 256; ch; --ch)
574 snext[pos] = sfirst[ch] - pos;
581 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
590 if (op->op_flags & OPf_STACKED)
596 TARG = sv_newmortal();
597 PUSHi(do_trans(sv, op));
601 /* Lvalue operators. */
613 djSP; dMARK; dTARGET;
623 SETi(do_chomp(TOPs));
629 djSP; dMARK; dTARGET;
630 register I32 count = 0;
633 count += do_chomp(POPs);
644 if (!sv || !SvANY(sv))
646 switch (SvTYPE(sv)) {
648 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
652 if (HvARRAY(sv) || SvGMAGICAL(sv))
656 if (CvROOT(sv) || CvXSUB(sv))
673 if (!op->op_private) {
682 if (SvTHINKFIRST(sv)) {
689 switch (SvTYPE(sv)) {
699 if (cv_const_sv((CV*)sv))
700 warn("Constant subroutine %s undefined",
701 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
704 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
706 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
710 sv_setsv(sv, &sv_undef);
713 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
716 SvPV_set(sv, Nullch);
729 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
731 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
732 SvIVX(TOPs) != IV_MIN)
735 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
746 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
748 sv_setsv(TARG, TOPs);
749 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
750 SvIVX(TOPs) != IV_MAX)
753 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
767 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
769 sv_setsv(TARG, TOPs);
770 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
771 SvIVX(TOPs) != IV_MIN)
774 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
783 /* Ordinary operators. */
787 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
790 SETn( pow( left, right) );
797 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
800 SETn( left * right );
807 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
812 DIE("Illegal division by zero");
814 /* insure that 20./5. == 4. */
817 if ((double)I_V(left) == left &&
818 (double)I_V(right) == right &&
819 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
822 value = left / right;
826 value = left / right;
835 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
843 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
845 right = (right_neg = (i < 0)) ? -i : i;
849 right = U_V((right_neg = (n < 0)) ? -n : n);
852 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
854 left = (left_neg = (i < 0)) ? -i : i;
858 left = U_V((left_neg = (n < 0)) ? -n : n);
862 DIE("Illegal modulus zero");
865 if ((left_neg != right_neg) && ans)
868 /* XXX may warn: unary minus operator applied to unsigned type */
869 /* could change -foo to be (~foo)+1 instead */
870 if (ans <= ~((UV)IV_MAX)+1)
871 sv_setiv(TARG, ~ans+1);
873 sv_setnv(TARG, -(double)ans);
884 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
886 register I32 count = POPi;
887 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
889 I32 items = SP - MARK;
901 repeatcpy((char*)(MARK + items), (char*)MARK,
902 items * sizeof(SV*), count - 1);
908 else { /* Note: mark already snarfed by pp_list */
913 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
914 if (SvREADONLY(tmpstr) && curcop != &compiling)
915 DIE("Can't x= to readonly value");
919 SvSetSV(TARG, tmpstr);
920 SvPV_force(TARG, len);
925 SvGROW(TARG, (count * len) + 1);
926 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
927 SvCUR(TARG) *= count;
931 (void)SvPOK_only(TARG);
940 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
943 SETn( left - right );
950 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
953 if (op->op_private & HINT_INTEGER) {
969 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
972 if (op->op_private & HINT_INTEGER) {
988 djSP; tryAMAGICbinSET(lt,0);
991 SETs(boolSV(TOPn < value));
998 djSP; tryAMAGICbinSET(gt,0);
1001 SETs(boolSV(TOPn > value));
1008 djSP; tryAMAGICbinSET(le,0);
1011 SETs(boolSV(TOPn <= value));
1018 djSP; tryAMAGICbinSET(ge,0);
1021 SETs(boolSV(TOPn >= value));
1028 djSP; tryAMAGICbinSET(ne,0);
1031 SETs(boolSV(TOPn != value));
1038 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1045 else if (left < right)
1047 else if (left > right)
1060 djSP; tryAMAGICbinSET(slt,0);
1063 int cmp = ((op->op_private & OPpLOCALE)
1064 ? sv_cmp_locale(left, right)
1065 : sv_cmp(left, right));
1066 SETs(boolSV(cmp < 0));
1073 djSP; tryAMAGICbinSET(sgt,0);
1076 int cmp = ((op->op_private & OPpLOCALE)
1077 ? sv_cmp_locale(left, right)
1078 : sv_cmp(left, right));
1079 SETs(boolSV(cmp > 0));
1086 djSP; tryAMAGICbinSET(sle,0);
1089 int cmp = ((op->op_private & OPpLOCALE)
1090 ? sv_cmp_locale(left, right)
1091 : sv_cmp(left, right));
1092 SETs(boolSV(cmp <= 0));
1099 djSP; tryAMAGICbinSET(sge,0);
1102 int cmp = ((op->op_private & OPpLOCALE)
1103 ? sv_cmp_locale(left, right)
1104 : sv_cmp(left, right));
1105 SETs(boolSV(cmp >= 0));
1112 djSP; tryAMAGICbinSET(seq,0);
1115 SETs(boolSV(sv_eq(left, right)));
1122 djSP; tryAMAGICbinSET(sne,0);
1125 SETs(boolSV(!sv_eq(left, right)));
1132 djSP; dTARGET; tryAMAGICbin(scmp,0);
1135 int cmp = ((op->op_private & OPpLOCALE)
1136 ? sv_cmp_locale(left, right)
1137 : sv_cmp(left, right));
1145 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1148 if (SvNIOKp(left) || SvNIOKp(right)) {
1149 if (op->op_private & HINT_INTEGER) {
1150 IBW value = SvIV(left) & SvIV(right);
1154 UBW value = SvUV(left) & SvUV(right);
1159 do_vop(op->op_type, TARG, left, right);
1168 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1171 if (SvNIOKp(left) || SvNIOKp(right)) {
1172 if (op->op_private & HINT_INTEGER) {
1173 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1177 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1182 do_vop(op->op_type, TARG, left, right);
1191 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1194 if (SvNIOKp(left) || SvNIOKp(right)) {
1195 if (op->op_private & HINT_INTEGER) {
1196 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1200 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1205 do_vop(op->op_type, TARG, left, right);
1214 djSP; dTARGET; tryAMAGICun(neg);
1219 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1221 else if (SvNIOKp(sv))
1223 else if (SvPOKp(sv)) {
1225 char *s = SvPV(sv, len);
1226 if (isIDFIRST(*s)) {
1227 sv_setpvn(TARG, "-", 1);
1230 else if (*s == '+' || *s == '-') {
1232 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1235 sv_setnv(TARG, -SvNV(sv));
1247 djSP; tryAMAGICunSET(not);
1248 #endif /* OVERLOAD */
1249 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1255 djSP; dTARGET; tryAMAGICun(compl);
1259 if (op->op_private & HINT_INTEGER) {
1260 IBW value = ~SvIV(sv);
1264 UBW value = ~SvUV(sv);
1269 register char *tmps;
1270 register long *tmpl;
1275 tmps = SvPV_force(TARG, len);
1278 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1281 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1285 for ( ; anum > 0; anum--, tmps++)
1294 /* integer versions of some of the above */
1298 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1301 SETi( left * right );
1308 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1312 DIE("Illegal division by zero");
1313 value = POPi / value;
1321 djSP; dATARGET; tryAMAGICbin(mod,opASSIGN);
1325 DIE("Illegal modulus zero");
1326 SETi( left % right );
1333 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1336 SETi( left + right );
1343 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1346 SETi( left - right );
1353 djSP; tryAMAGICbinSET(lt,0);
1356 SETs(boolSV(left < right));
1363 djSP; tryAMAGICbinSET(gt,0);
1366 SETs(boolSV(left > right));
1373 djSP; tryAMAGICbinSET(le,0);
1376 SETs(boolSV(left <= right));
1383 djSP; tryAMAGICbinSET(ge,0);
1386 SETs(boolSV(left >= right));
1393 djSP; tryAMAGICbinSET(eq,0);
1396 SETs(boolSV(left == right));
1403 djSP; tryAMAGICbinSET(ne,0);
1406 SETs(boolSV(left != right));
1413 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1420 else if (left < right)
1431 djSP; dTARGET; tryAMAGICun(neg);
1436 /* High falutin' math. */
1440 djSP; dTARGET; tryAMAGICbin(atan2,0);
1443 SETn(atan2(left, right));
1450 djSP; dTARGET; tryAMAGICun(sin);
1462 djSP; dTARGET; tryAMAGICun(cos);
1482 if (!srand_called) {
1483 (void)srand((unsigned)seed());
1484 srand_called = TRUE;
1487 value = rand() * value / 2147483648.0;
1490 value = rand() * value / 65536.0;
1493 value = rand() * value / 32768.0;
1495 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1511 (void)srand((unsigned)anum);
1512 srand_called = TRUE;
1521 * This is really just a quick hack which grabs various garbage
1522 * values. It really should be a real hash algorithm which
1523 * spreads the effect of every input bit onto every output bit,
1524 * if someone who knows about such tings would bother to write it.
1525 * Might be a good idea to add that function to CORE as well.
1526 * No numbers below come from careful analysis or anyting here,
1527 * except they are primes and SEED_C1 > 1E6 to get a full-width
1528 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1529 * probably be bigger too.
1532 # define SEED_C1 1000003
1533 #define SEED_C4 73819
1535 # define SEED_C1 25747
1536 #define SEED_C4 20639
1540 #define SEED_C5 26107
1545 # include <starlet.h>
1546 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1547 * in 100-ns units, typically incremented ever 10 ms. */
1548 unsigned int when[2];
1549 _ckvmssts(sys$gettim(when));
1550 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1552 # ifdef HAS_GETTIMEOFDAY
1553 struct timeval when;
1554 gettimeofday(&when,(struct timezone *) 0);
1555 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1559 u = (U32)SEED_C1 * when;
1562 u += SEED_C3 * (U32)getpid();
1563 u += SEED_C4 * (U32)(UV)stack_sp;
1564 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1565 u += SEED_C5 * (U32)(UV)&when;
1572 djSP; dTARGET; tryAMAGICun(exp);
1584 djSP; dTARGET; tryAMAGICun(log);
1589 SET_NUMERIC_STANDARD();
1590 DIE("Can't take log of %g", value);
1600 djSP; dTARGET; tryAMAGICun(sqrt);
1605 SET_NUMERIC_STANDARD();
1606 DIE("Can't take sqrt of %g", value);
1608 value = sqrt(value);
1618 double value = TOPn;
1621 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1627 (void)modf(value, &value);
1629 (void)modf(-value, &value);
1644 djSP; dTARGET; tryAMAGICun(abs);
1646 double value = TOPn;
1649 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1650 (iv = SvIVX(TOPs)) != IV_MIN) {
1671 XPUSHu(scan_hex(tmps, 99, &argtype));
1683 while (*tmps && isSPACE(*tmps))
1688 value = scan_hex(++tmps, 99, &argtype);
1690 value = scan_oct(tmps, 99, &argtype);
1700 SETi( sv_len(TOPs) );
1713 I32 lvalue = op->op_flags & OPf_MOD;
1715 I32 arybase = curcop->cop_arybase;
1721 tmps = SvPV(sv, curlen);
1722 if (pos >= arybase) {
1740 else if (len >= 0) {
1742 if (rem > (I32)curlen)
1756 if (dowarn || lvalue)
1757 warn("substr outside of string");
1762 sv_setpvn(TARG, tmps, rem);
1763 if (lvalue) { /* it's an lvalue! */
1764 if (!SvGMAGICAL(sv)) {
1768 warn("Attempt to use reference as lvalue in substr");
1770 if (SvOK(sv)) /* is it defined ? */
1771 (void)SvPOK_only(sv);
1773 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1776 if (SvTYPE(TARG) < SVt_PVLV) {
1777 sv_upgrade(TARG, SVt_PVLV);
1778 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1783 LvTARGOFF(TARG) = pos;
1784 LvTARGLEN(TARG) = rem;
1787 PUSHs(TARG); /* avoid SvSETMAGIC here */
1794 register I32 size = POPi;
1795 register I32 offset = POPi;
1796 register SV *src = POPs;
1797 I32 lvalue = op->op_flags & OPf_MOD;
1799 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1800 unsigned long retnum;
1803 offset *= size; /* turn into bit offset */
1804 len = (offset + size + 7) / 8;
1805 if (offset < 0 || size < 1)
1808 if (lvalue) { /* it's an lvalue! */
1809 if (SvTYPE(TARG) < SVt_PVLV) {
1810 sv_upgrade(TARG, SVt_PVLV);
1811 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1816 LvTARGOFF(TARG) = offset;
1817 LvTARGLEN(TARG) = size;
1825 if (offset >= srclen)
1828 retnum = (unsigned long) s[offset] << 8;
1830 else if (size == 32) {
1831 if (offset >= srclen)
1833 else if (offset + 1 >= srclen)
1834 retnum = (unsigned long) s[offset] << 24;
1835 else if (offset + 2 >= srclen)
1836 retnum = ((unsigned long) s[offset] << 24) +
1837 ((unsigned long) s[offset + 1] << 16);
1839 retnum = ((unsigned long) s[offset] << 24) +
1840 ((unsigned long) s[offset + 1] << 16) +
1841 (s[offset + 2] << 8);
1846 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1851 else if (size == 16)
1852 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1853 else if (size == 32)
1854 retnum = ((unsigned long) s[offset] << 24) +
1855 ((unsigned long) s[offset + 1] << 16) +
1856 (s[offset + 2] << 8) + s[offset+3];
1860 sv_setiv(TARG, (IV)retnum);
1875 I32 arybase = curcop->cop_arybase;
1880 offset = POPi - arybase;
1883 tmps = SvPV(big, biglen);
1886 else if (offset > biglen)
1888 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1889 (unsigned char*)tmps + biglen, little)))
1890 retval = -1 + arybase;
1892 retval = tmps2 - tmps + arybase;
1909 I32 arybase = curcop->cop_arybase;
1915 tmps2 = SvPV(little, llen);
1916 tmps = SvPV(big, blen);
1920 offset = SvIV(offstr) - arybase + llen;
1923 else if (offset > blen)
1925 if (!(tmps2 = rninstr(tmps, tmps + offset,
1926 tmps2, tmps2 + llen)))
1927 retval = -1 + arybase;
1929 retval = tmps2 - tmps + arybase;
1936 djSP; dMARK; dORIGMARK; dTARGET;
1937 #ifdef USE_LOCALE_NUMERIC
1938 if (op->op_private & OPpLOCALE)
1939 SET_NUMERIC_LOCAL();
1941 SET_NUMERIC_STANDARD();
1943 do_sprintf(TARG, SP-MARK, MARK+1);
1944 TAINT_IF(SvTAINTED(TARG));
1958 value = (I32) (*tmps & 255);
1963 value = (I32) (anum & 255);
1974 (void)SvUPGRADE(TARG,SVt_PV);
1980 (void)SvPOK_only(TARG);
1987 djSP; dTARGET; dPOPTOPssrl;
1989 char *tmps = SvPV(left, na);
1991 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
1993 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
1997 "The crypt() function is unimplemented due to excessive paranoia.");
2009 if (!SvPADTMP(sv)) {
2015 s = SvPV_force(sv, na);
2017 if (op->op_private & OPpLOCALE) {
2020 *s = toUPPER_LC(*s);
2035 if (!SvPADTMP(sv)) {
2041 s = SvPV_force(sv, na);
2043 if (op->op_private & OPpLOCALE) {
2046 *s = toLOWER_LC(*s);
2063 if (!SvPADTMP(sv)) {
2070 s = SvPV_force(sv, len);
2072 register char *send = s + len;
2074 if (op->op_private & OPpLOCALE) {
2077 for (; s < send; s++)
2078 *s = toUPPER_LC(*s);
2081 for (; s < send; s++)
2095 if (!SvPADTMP(sv)) {
2102 s = SvPV_force(sv, len);
2104 register char *send = s + len;
2106 if (op->op_private & OPpLOCALE) {
2109 for (; s < send; s++)
2110 *s = toLOWER_LC(*s);
2113 for (; s < send; s++)
2125 register char *s = SvPV(sv,len);
2129 (void)SvUPGRADE(TARG, SVt_PV);
2130 SvGROW(TARG, (len * 2) + 1);
2138 SvCUR_set(TARG, d - SvPVX(TARG));
2139 (void)SvPOK_only(TARG);
2142 sv_setpvn(TARG, s, len);
2151 djSP; dMARK; dORIGMARK;
2153 register AV* av = (AV*)POPs;
2154 register I32 lval = op->op_flags & OPf_MOD;
2155 I32 arybase = curcop->cop_arybase;
2158 if (SvTYPE(av) == SVt_PVAV) {
2159 if (lval && op->op_private & OPpLVAL_INTRO) {
2161 for (svp = mark + 1; svp <= sp; svp++) {
2166 if (max > AvMAX(av))
2169 while (++MARK <= SP) {
2170 elem = SvIVx(*MARK);
2174 svp = av_fetch(av, elem, lval);
2176 if (!svp || *svp == &sv_undef)
2177 DIE(no_aelem, elem);
2178 if (op->op_private & OPpLVAL_INTRO)
2181 *MARK = svp ? *svp : &sv_undef;
2184 if (GIMME != G_ARRAY) {
2192 /* Associative arrays. */
2197 HV *hash = (HV*)POPs;
2199 I32 gimme = GIMME_V;
2200 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2203 /* might clobber stack_sp */
2204 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2209 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2210 if (gimme == G_ARRAY) {
2212 /* might clobber stack_sp */
2213 sv_setsv(TARG, realhv ?
2214 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2219 else if (gimme == G_SCALAR)
2238 I32 gimme = GIMME_V;
2239 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2243 if (op->op_private & OPpSLICE) {
2247 hvtype = SvTYPE(hv);
2248 while (++MARK <= SP) {
2249 if (hvtype == SVt_PVHV)
2250 sv = hv_delete_ent(hv, *MARK, discard, 0);
2251 else if (hvtype == SVt_PVAV)
2252 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2254 DIE("Not a HASH reference");
2255 *MARK = sv ? sv : &sv_undef;
2259 else if (gimme == G_SCALAR) {
2268 if (SvTYPE(hv) == SVt_PVHV)
2269 sv = hv_delete_ent(hv, keysv, discard, 0);
2270 else if (SvTYPE(hv) == SVt_PVAV)
2271 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2273 DIE("Not a HASH reference");
2287 if (SvTYPE(hv) == SVt_PVHV) {
2288 if (hv_exists_ent(hv, tmpsv, 0))
2290 } else if (SvTYPE(hv) == SVt_PVAV) {
2291 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2294 DIE("Not a HASH reference");
2301 djSP; dMARK; dORIGMARK;
2303 register HV *hv = (HV*)POPs;
2304 register I32 lval = op->op_flags & OPf_MOD;
2305 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2307 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2308 while (++MARK <= SP) {
2312 he = hv_fetch_ent(hv, keysv, lval, 0);
2313 svp = he ? &HeVAL(he) : 0;
2315 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2318 if (!he || HeVAL(he) == &sv_undef)
2319 DIE(no_helem, SvPV(keysv, na));
2320 if (op->op_private & OPpLVAL_INTRO)
2321 save_svref(&HeVAL(he));
2323 *MARK = he ? HeVAL(he) : &sv_undef;
2326 if (GIMME != G_ARRAY) {
2334 /* List operators. */
2339 if (GIMME != G_ARRAY) {
2341 *MARK = *SP; /* unwanted list, return last item */
2352 SV **lastrelem = stack_sp;
2353 SV **lastlelem = stack_base + POPMARK;
2354 SV **firstlelem = stack_base + POPMARK + 1;
2355 register SV **firstrelem = lastlelem + 1;
2356 I32 arybase = curcop->cop_arybase;
2357 I32 lval = op->op_flags & OPf_MOD;
2358 I32 is_something_there = lval;
2360 register I32 max = lastrelem - lastlelem;
2361 register SV **lelem;
2364 if (GIMME != G_ARRAY) {
2365 ix = SvIVx(*lastlelem);
2370 if (ix < 0 || ix >= max)
2371 *firstlelem = &sv_undef;
2373 *firstlelem = firstrelem[ix];
2379 SP = firstlelem - 1;
2383 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2389 else if (!(*lelem = firstrelem[ix]))
2394 if (ix >= max || !(*lelem = firstrelem[ix]))
2397 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2398 is_something_there = TRUE;
2400 if (is_something_there)
2403 SP = firstlelem - 1;
2409 djSP; dMARK; dORIGMARK;
2410 I32 items = SP - MARK;
2411 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2412 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2419 djSP; dMARK; dORIGMARK;
2420 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2424 SV *val = NEWSV(46, 0);
2426 sv_setsv(val, *++MARK);
2428 warn("Odd number of elements in hash list");
2429 (void)hv_store_ent(hv,key,val,0);
2438 djSP; dMARK; dORIGMARK;
2439 register AV *ary = (AV*)*++MARK;
2443 register I32 offset;
2444 register I32 length;
2453 offset = i = SvIVx(*MARK);
2455 offset += AvFILL(ary) + 1;
2457 offset -= curcop->cop_arybase;
2461 length = SvIVx(*MARK++);
2466 length = AvMAX(ary) + 1; /* close enough to infinity */
2470 length = AvMAX(ary) + 1;
2472 if (offset > AvFILL(ary) + 1)
2473 offset = AvFILL(ary) + 1;
2474 after = AvFILL(ary) + 1 - (offset + length);
2475 if (after < 0) { /* not that much array */
2476 length += after; /* offset+length now in array */
2482 /* At this point, MARK .. SP-1 is our new LIST */
2485 diff = newlen - length;
2486 if (newlen && !AvREAL(ary)) {
2490 assert(AvREAL(ary)); /* would leak, so croak */
2493 if (diff < 0) { /* shrinking the area */
2495 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2496 Copy(MARK, tmparyval, newlen, SV*);
2499 MARK = ORIGMARK + 1;
2500 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2501 MEXTEND(MARK, length);
2502 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2504 EXTEND_MORTAL(length);
2505 for (i = length, dst = MARK; i; i--) {
2506 if (!SvIMMORTAL(*dst))
2507 sv_2mortal(*dst); /* free them eventualy */
2514 *MARK = AvARRAY(ary)[offset+length-1];
2516 if (!SvIMMORTAL(*MARK))
2518 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2519 SvREFCNT_dec(*dst++); /* free them now */
2522 AvFILL(ary) += diff;
2524 /* pull up or down? */
2526 if (offset < after) { /* easier to pull up */
2527 if (offset) { /* esp. if nothing to pull */
2528 src = &AvARRAY(ary)[offset-1];
2529 dst = src - diff; /* diff is negative */
2530 for (i = offset; i > 0; i--) /* can't trust Copy */
2534 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2538 if (after) { /* anything to pull down? */
2539 src = AvARRAY(ary) + offset + length;
2540 dst = src + diff; /* diff is negative */
2541 Move(src, dst, after, SV*);
2543 dst = &AvARRAY(ary)[AvFILL(ary)+1];
2544 /* avoid later double free */
2548 dst[--i] = &sv_undef;
2551 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2553 *dst = NEWSV(46, 0);
2554 sv_setsv(*dst++, *src++);
2556 Safefree(tmparyval);
2559 else { /* no, expanding (or same) */
2561 New(452, tmparyval, length, SV*); /* so remember deletion */
2562 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2565 if (diff > 0) { /* expanding */
2567 /* push up or down? */
2569 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2573 Move(src, dst, offset, SV*);
2575 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2577 AvFILL(ary) += diff;
2580 if (AvFILL(ary) + diff >= AvMAX(ary)) /* oh, well */
2581 av_extend(ary, AvFILL(ary) + diff);
2582 AvFILL(ary) += diff;
2585 dst = AvARRAY(ary) + AvFILL(ary);
2587 for (i = after; i; i--) {
2594 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2595 *dst = NEWSV(46, 0);
2596 sv_setsv(*dst++, *src++);
2598 MARK = ORIGMARK + 1;
2599 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2601 Copy(tmparyval, MARK, length, SV*);
2603 EXTEND_MORTAL(length);
2604 for (i = length, dst = MARK; i; i--) {
2605 if (!SvIMMORTAL(*dst))
2606 sv_2mortal(*dst); /* free them eventualy */
2610 Safefree(tmparyval);
2614 else if (length--) {
2615 *MARK = tmparyval[length];
2617 if (!SvIMMORTAL(*MARK))
2619 while (length-- > 0)
2620 SvREFCNT_dec(tmparyval[length]);
2622 Safefree(tmparyval);
2633 djSP; dMARK; dORIGMARK; dTARGET;
2634 register AV *ary = (AV*)*++MARK;
2635 register SV *sv = &sv_undef;
2637 for (++MARK; MARK <= SP; MARK++) {
2640 sv_setsv(sv, *MARK);
2644 PUSHi( AvFILL(ary) + 1 );
2652 SV *sv = av_pop(av);
2653 if (!SvIMMORTAL(sv) && AvREAL(av))
2654 (void)sv_2mortal(sv);
2663 SV *sv = av_shift(av);
2667 if (!SvIMMORTAL(sv) && AvREAL(av))
2668 (void)sv_2mortal(sv);
2675 djSP; dMARK; dORIGMARK; dTARGET;
2676 register AV *ary = (AV*)*++MARK;
2680 av_unshift(ary, SP - MARK);
2683 sv_setsv(sv, *++MARK);
2684 (void)av_store(ary, i++, sv);
2688 PUSHi( AvFILL(ary) + 1 );
2698 if (GIMME == G_ARRAY) {
2709 register char *down;
2715 do_join(TARG, &sv_no, MARK, SP);
2717 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2718 up = SvPV_force(TARG, len);
2720 down = SvPVX(TARG) + len - 1;
2726 (void)SvPOK_only(TARG);
2735 mul128(SV *sv, U8 m)
2738 char *s = SvPV(sv, len);
2742 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2743 SV *New = newSVpv("0000000000", 10);
2746 SvREFCNT_dec(sv); /* free old sv */
2751 while (!*t) /* trailing '\0'? */
2754 i = ((*t - '0') << 7) + m;
2755 *(t--) = '0' + (i % 10);
2761 /* Explosives and implosives. */
2768 I32 gimme = GIMME_V;
2772 register char *pat = SvPV(left, llen);
2773 register char *s = SvPV(right, rlen);
2774 char *strend = s + rlen;
2776 register char *patend = pat + llen;
2781 /* These must not be in registers: */
2792 unsigned Quad_t auquad;
2798 register U32 culong;
2800 static char* bitcount = 0;
2803 if (gimme != G_ARRAY) { /* arrange to do first one only */
2805 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2806 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2808 while (isDIGIT(*patend) || *patend == '*')
2814 while (pat < patend) {
2816 datumtype = *pat++ & 0xFF;
2817 if (isSPACE(datumtype))
2821 else if (*pat == '*') {
2822 len = strend - strbeg; /* long enough */
2825 else if (isDIGIT(*pat)) {
2827 while (isDIGIT(*pat))
2828 len = (len * 10) + (*pat++ - '0');
2831 len = (datumtype != '@');
2834 croak("Invalid type in unpack: '%c'", (int)datumtype);
2835 case ',': /* grandfather in commas but with a warning */
2836 if (commas++ == 0 && dowarn)
2837 warn("Invalid type in unpack: '%c'", (int)datumtype);
2840 if (len == 1 && pat[-1] != '1')
2849 if (len > strend - strbeg)
2850 DIE("@ outside of string");
2854 if (len > s - strbeg)
2855 DIE("X outside of string");
2859 if (len > strend - s)
2860 DIE("x outside of string");
2865 if (len > strend - s)
2868 goto uchar_checksum;
2869 sv = NEWSV(35, len);
2870 sv_setpvn(sv, s, len);
2872 if (datumtype == 'A') {
2873 aptr = s; /* borrow register */
2874 s = SvPVX(sv) + len - 1;
2875 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2878 SvCUR_set(sv, s - SvPVX(sv));
2879 s = aptr; /* unborrow register */
2881 XPUSHs(sv_2mortal(sv));
2885 if (pat[-1] == '*' || len > (strend - s) * 8)
2886 len = (strend - s) * 8;
2889 Newz(601, bitcount, 256, char);
2890 for (bits = 1; bits < 256; bits++) {
2891 if (bits & 1) bitcount[bits]++;
2892 if (bits & 2) bitcount[bits]++;
2893 if (bits & 4) bitcount[bits]++;
2894 if (bits & 8) bitcount[bits]++;
2895 if (bits & 16) bitcount[bits]++;
2896 if (bits & 32) bitcount[bits]++;
2897 if (bits & 64) bitcount[bits]++;
2898 if (bits & 128) bitcount[bits]++;
2902 culong += bitcount[*(unsigned char*)s++];
2907 if (datumtype == 'b') {
2909 if (bits & 1) culong++;
2915 if (bits & 128) culong++;
2922 sv = NEWSV(35, len + 1);
2925 aptr = pat; /* borrow register */
2927 if (datumtype == 'b') {
2929 for (len = 0; len < aint; len++) {
2930 if (len & 7) /*SUPPRESS 595*/
2934 *pat++ = '0' + (bits & 1);
2939 for (len = 0; len < aint; len++) {
2944 *pat++ = '0' + ((bits & 128) != 0);
2948 pat = aptr; /* unborrow register */
2949 XPUSHs(sv_2mortal(sv));
2953 if (pat[-1] == '*' || len > (strend - s) * 2)
2954 len = (strend - s) * 2;
2955 sv = NEWSV(35, len + 1);
2958 aptr = pat; /* borrow register */
2960 if (datumtype == 'h') {
2962 for (len = 0; len < aint; len++) {
2967 *pat++ = hexdigit[bits & 15];
2972 for (len = 0; len < aint; len++) {
2977 *pat++ = hexdigit[(bits >> 4) & 15];
2981 pat = aptr; /* unborrow register */
2982 XPUSHs(sv_2mortal(sv));
2985 if (len > strend - s)
2990 if (aint >= 128) /* fake up signed chars */
3000 if (aint >= 128) /* fake up signed chars */
3003 sv_setiv(sv, (IV)aint);
3004 PUSHs(sv_2mortal(sv));
3009 if (len > strend - s)
3024 sv_setiv(sv, (IV)auint);
3025 PUSHs(sv_2mortal(sv));
3030 along = (strend - s) / SIZE16;
3047 sv_setiv(sv, (IV)ashort);
3048 PUSHs(sv_2mortal(sv));
3055 along = (strend - s) / SIZE16;
3060 COPY16(s, &aushort);
3063 if (datumtype == 'n')
3064 aushort = ntohs(aushort);
3067 if (datumtype == 'v')
3068 aushort = vtohs(aushort);
3077 COPY16(s, &aushort);
3081 if (datumtype == 'n')
3082 aushort = ntohs(aushort);
3085 if (datumtype == 'v')
3086 aushort = vtohs(aushort);
3088 sv_setiv(sv, (IV)aushort);
3089 PUSHs(sv_2mortal(sv));
3094 along = (strend - s) / sizeof(int);
3099 Copy(s, &aint, 1, int);
3102 cdouble += (double)aint;
3111 Copy(s, &aint, 1, int);
3114 sv_setiv(sv, (IV)aint);
3115 PUSHs(sv_2mortal(sv));
3120 along = (strend - s) / sizeof(unsigned int);
3125 Copy(s, &auint, 1, unsigned int);
3126 s += sizeof(unsigned int);
3128 cdouble += (double)auint;
3137 Copy(s, &auint, 1, unsigned int);
3138 s += sizeof(unsigned int);
3140 sv_setuv(sv, (UV)auint);
3141 PUSHs(sv_2mortal(sv));
3146 along = (strend - s) / SIZE32;
3154 cdouble += (double)along;
3166 sv_setiv(sv, (IV)along);
3167 PUSHs(sv_2mortal(sv));
3174 along = (strend - s) / SIZE32;
3182 if (datumtype == 'N')
3183 aulong = ntohl(aulong);
3186 if (datumtype == 'V')
3187 aulong = vtohl(aulong);
3190 cdouble += (double)aulong;
3202 if (datumtype == 'N')
3203 aulong = ntohl(aulong);
3206 if (datumtype == 'V')
3207 aulong = vtohl(aulong);
3210 sv_setuv(sv, (UV)aulong);
3211 PUSHs(sv_2mortal(sv));
3216 along = (strend - s) / sizeof(char*);
3222 if (sizeof(char*) > strend - s)
3225 Copy(s, &aptr, 1, char*);
3231 PUSHs(sv_2mortal(sv));
3241 while ((len > 0) && (s < strend)) {
3242 auv = (auv << 7) | (*s & 0x7f);
3243 if (!(*s++ & 0x80)) {
3247 PUSHs(sv_2mortal(sv));
3251 else if (++bytes >= sizeof(UV)) { /* promote to string */
3254 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3255 while (s < strend) {
3256 sv = mul128(sv, *s & 0x7f);
3257 if (!(*s++ & 0x80)) {
3266 PUSHs(sv_2mortal(sv));
3271 if ((s >= strend) && bytes)
3272 croak("Unterminated compressed integer");
3277 if (sizeof(char*) > strend - s)
3280 Copy(s, &aptr, 1, char*);
3285 sv_setpvn(sv, aptr, len);
3286 PUSHs(sv_2mortal(sv));
3293 if (s + sizeof(Quad_t) > strend)
3296 Copy(s, &aquad, 1, Quad_t);
3297 s += sizeof(Quad_t);
3300 if (aquad >= IV_MIN && aquad <= IV_MAX)
3301 sv_setiv(sv, (IV)aquad);
3303 sv_setnv(sv, (double)aquad);
3304 PUSHs(sv_2mortal(sv));
3311 if (s + sizeof(unsigned Quad_t) > strend)
3314 Copy(s, &auquad, 1, unsigned Quad_t);
3315 s += sizeof(unsigned Quad_t);
3318 if (aquad <= UV_MAX)
3319 sv_setuv(sv, (UV)auquad);
3321 sv_setnv(sv, (double)auquad);
3322 PUSHs(sv_2mortal(sv));
3326 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3329 along = (strend - s) / sizeof(float);
3334 Copy(s, &afloat, 1, float);
3343 Copy(s, &afloat, 1, float);
3346 sv_setnv(sv, (double)afloat);
3347 PUSHs(sv_2mortal(sv));
3353 along = (strend - s) / sizeof(double);
3358 Copy(s, &adouble, 1, double);
3359 s += sizeof(double);
3367 Copy(s, &adouble, 1, double);
3368 s += sizeof(double);
3370 sv_setnv(sv, (double)adouble);
3371 PUSHs(sv_2mortal(sv));
3376 along = (strend - s) * 3 / 4;
3377 sv = NEWSV(42, along);
3380 while (s < strend && *s > ' ' && *s < 'a') {
3385 len = (*s++ - ' ') & 077;
3387 if (s < strend && *s >= ' ')
3388 a = (*s++ - ' ') & 077;
3391 if (s < strend && *s >= ' ')
3392 b = (*s++ - ' ') & 077;
3395 if (s < strend && *s >= ' ')
3396 c = (*s++ - ' ') & 077;
3399 if (s < strend && *s >= ' ')
3400 d = (*s++ - ' ') & 077;
3403 hunk[0] = (a << 2) | (b >> 4);
3404 hunk[1] = (b << 4) | (c >> 2);
3405 hunk[2] = (c << 6) | d;
3406 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3411 else if (s[1] == '\n') /* possible checksum byte */
3414 XPUSHs(sv_2mortal(sv));
3419 if (strchr("fFdD", datumtype) ||
3420 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3424 while (checksum >= 16) {
3428 while (checksum >= 4) {
3434 along = (1 << checksum) - 1;
3435 while (cdouble < 0.0)
3437 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3438 sv_setnv(sv, cdouble);
3441 if (checksum < 32) {
3442 aulong = (1 << checksum) - 1;
3445 sv_setuv(sv, (UV)culong);
3447 XPUSHs(sv_2mortal(sv));
3451 if (sp == oldsp && gimme == G_SCALAR)
3457 doencodes(register SV *sv, register char *s, register I32 len)
3462 sv_catpvn(sv, hunk, 1);
3465 hunk[0] = ' ' + (077 & (*s >> 2));
3466 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3467 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3468 hunk[3] = ' ' + (077 & (s[2] & 077));
3469 sv_catpvn(sv, hunk, 4);
3473 for (s = SvPVX(sv); *s; s++) {
3477 sv_catpvn(sv, "\n", 1);
3481 is_an_int(char *s, STRLEN l)
3483 SV *result = newSVpv("", l);
3484 char *result_c = SvPV(result, na); /* convenience */
3485 char *out = result_c;
3495 SvREFCNT_dec(result);
3518 SvREFCNT_dec(result);
3524 SvCUR_set(result, out - result_c);
3529 div128(SV *pnum, bool *done)
3530 /* must be '\0' terminated */
3534 char *s = SvPV(pnum, len);
3543 i = m * 10 + (*t - '0');
3545 r = (i >> 7); /* r < 10 */
3552 SvCUR_set(pnum, (STRLEN) (t - s));
3559 djSP; dMARK; dORIGMARK; dTARGET;
3560 register SV *cat = TARG;
3563 register char *pat = SvPVx(*++MARK, fromlen);
3564 register char *patend = pat + fromlen;
3569 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3570 static char *space10 = " ";
3572 /* These must not be in registers: */
3581 unsigned Quad_t auquad;
3590 sv_setpvn(cat, "", 0);
3591 while (pat < patend) {
3592 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3593 datumtype = *pat++ & 0xFF;
3594 if (isSPACE(datumtype))
3597 len = strchr("@Xxu", datumtype) ? 0 : items;
3600 else if (isDIGIT(*pat)) {
3602 while (isDIGIT(*pat))
3603 len = (len * 10) + (*pat++ - '0');
3609 croak("Invalid type in pack: '%c'", (int)datumtype);
3610 case ',': /* grandfather in commas but with a warning */
3611 if (commas++ == 0 && dowarn)
3612 warn("Invalid type in pack: '%c'", (int)datumtype);
3615 DIE("%% may only be used in unpack");
3626 if (SvCUR(cat) < len)
3627 DIE("X outside of string");
3634 sv_catpvn(cat, null10, 10);
3637 sv_catpvn(cat, null10, len);
3642 aptr = SvPV(fromstr, fromlen);
3646 sv_catpvn(cat, aptr, len);
3648 sv_catpvn(cat, aptr, fromlen);
3650 if (datumtype == 'A') {
3652 sv_catpvn(cat, space10, 10);
3655 sv_catpvn(cat, space10, len);
3659 sv_catpvn(cat, null10, 10);
3662 sv_catpvn(cat, null10, len);
3669 char *savepat = pat;
3674 aptr = SvPV(fromstr, fromlen);
3679 SvCUR(cat) += (len+7)/8;
3680 SvGROW(cat, SvCUR(cat) + 1);
3681 aptr = SvPVX(cat) + aint;
3686 if (datumtype == 'B') {
3687 for (len = 0; len++ < aint;) {
3688 items |= *pat++ & 1;
3692 *aptr++ = items & 0xff;
3698 for (len = 0; len++ < aint;) {
3704 *aptr++ = items & 0xff;
3710 if (datumtype == 'B')
3711 items <<= 7 - (aint & 7);
3713 items >>= 7 - (aint & 7);
3714 *aptr++ = items & 0xff;
3716 pat = SvPVX(cat) + SvCUR(cat);
3727 char *savepat = pat;
3732 aptr = SvPV(fromstr, fromlen);
3737 SvCUR(cat) += (len+1)/2;
3738 SvGROW(cat, SvCUR(cat) + 1);
3739 aptr = SvPVX(cat) + aint;
3744 if (datumtype == 'H') {
3745 for (len = 0; len++ < aint;) {
3747 items |= ((*pat++ & 15) + 9) & 15;
3749 items |= *pat++ & 15;
3753 *aptr++ = items & 0xff;
3759 for (len = 0; len++ < aint;) {
3761 items |= (((*pat++ & 15) + 9) & 15) << 4;
3763 items |= (*pat++ & 15) << 4;
3767 *aptr++ = items & 0xff;
3773 *aptr++ = items & 0xff;
3774 pat = SvPVX(cat) + SvCUR(cat);
3786 aint = SvIV(fromstr);
3788 sv_catpvn(cat, &achar, sizeof(char));
3791 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3796 afloat = (float)SvNV(fromstr);
3797 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3804 adouble = (double)SvNV(fromstr);
3805 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3811 ashort = (I16)SvIV(fromstr);
3813 ashort = htons(ashort);
3815 CAT16(cat, &ashort);
3821 ashort = (I16)SvIV(fromstr);
3823 ashort = htovs(ashort);
3825 CAT16(cat, &ashort);
3832 ashort = (I16)SvIV(fromstr);
3833 CAT16(cat, &ashort);
3839 auint = SvUV(fromstr);
3840 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3846 adouble = floor(SvNV(fromstr));
3849 croak("Cannot compress negative numbers");
3855 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3856 adouble <= UV_MAX_cxux
3863 char buf[1 + sizeof(UV)];
3864 char *in = buf + sizeof(buf);
3865 UV auv = U_V(adouble);;
3868 *--in = (auv & 0x7f) | 0x80;
3871 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3872 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3874 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3875 char *from, *result, *in;
3880 /* Copy string and check for compliance */
3881 from = SvPV(fromstr, len);
3882 if ((norm = is_an_int(from, len)) == NULL)
3883 croak("can compress only unsigned integer");
3885 New('w', result, len, char);
3889 *--in = div128(norm, &done) | 0x80;
3890 result[len - 1] &= 0x7F; /* clear continue bit */
3891 sv_catpvn(cat, in, (result + len) - in);
3893 SvREFCNT_dec(norm); /* free norm */
3895 else if (SvNOKp(fromstr)) {
3896 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3897 char *in = buf + sizeof(buf);
3900 double next = floor(adouble / 128);
3901 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3902 if (--in < buf) /* this cannot happen ;-) */
3903 croak ("Cannot compress integer");
3905 } while (adouble > 0);
3906 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3907 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3910 croak("Cannot compress non integer");
3916 aint = SvIV(fromstr);
3917 sv_catpvn(cat, (char*)&aint, sizeof(int));
3923 aulong = SvUV(fromstr);
3925 aulong = htonl(aulong);
3927 CAT32(cat, &aulong);
3933 aulong = SvUV(fromstr);
3935 aulong = htovl(aulong);
3937 CAT32(cat, &aulong);
3943 aulong = SvUV(fromstr);
3944 CAT32(cat, &aulong);
3950 along = SvIV(fromstr);
3958 auquad = (unsigned Quad_t)SvIV(fromstr);
3959 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
3965 aquad = (Quad_t)SvIV(fromstr);
3966 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
3969 #endif /* HAS_QUAD */
3971 len = 1; /* assume SV is correct length */
3976 if (fromstr == &sv_undef)
3979 /* XXX better yet, could spirit away the string to
3980 * a safe spot and hang on to it until the result
3981 * of pack() (and all copies of the result) are
3984 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
3985 warn("Attempt to pack pointer to temporary value");
3986 if (SvPOK(fromstr) || SvNIOK(fromstr))
3987 aptr = SvPV(fromstr,na);
3989 aptr = SvPV_force(fromstr,na);
3991 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
3996 aptr = SvPV(fromstr, fromlen);
3997 SvGROW(cat, fromlen * 4 / 3);
4002 while (fromlen > 0) {
4009 doencodes(cat, aptr, todo);
4027 register I32 limit = POPi; /* note, negative is forever */
4030 register char *s = SvPV(sv, len);
4031 char *strend = s + len;
4033 register REGEXP *rx;
4037 I32 maxiters = (strend - s) + 10;
4040 I32 origlimit = limit;
4043 AV *oldstack = curstack;
4044 I32 gimme = GIMME_V;
4045 I32 oldsave = savestack_ix;
4048 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4053 DIE("panic: do_split");
4054 rx = pm->op_pmregexp;
4056 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4057 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4059 if (pm->op_pmreplroot)
4060 ary = GvAVn((GV*)pm->op_pmreplroot);
4061 else if (gimme != G_ARRAY)
4063 ary = (AV*)curpad[0];
4066 #endif /* USE_THREADS */
4069 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4073 for (i = AvFILL(ary); i >= 0; i--)
4074 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4078 /* temporarily switch stacks */
4079 SWITCHSTACK(curstack, ary);
4081 base = SP - stack_base;
4083 if (pm->op_pmflags & PMf_SKIPWHITE) {
4084 if (pm->op_pmflags & PMf_LOCALE) {
4085 while (isSPACE_LC(*s))
4093 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4095 multiline = pm->op_pmflags & PMf_MULTILINE;
4099 limit = maxiters + 2;
4100 if (pm->op_pmflags & PMf_WHITE) {
4103 while (m < strend &&
4104 !((pm->op_pmflags & PMf_LOCALE)
4105 ? isSPACE_LC(*m) : isSPACE(*m)))
4110 dstr = NEWSV(30, m-s);
4111 sv_setpvn(dstr, s, m-s);
4117 while (s < strend &&
4118 ((pm->op_pmflags & PMf_LOCALE)
4119 ? isSPACE_LC(*s) : isSPACE(*s)))
4123 else if (strEQ("^", rx->precomp)) {
4126 for (m = s; m < strend && *m != '\n'; m++) ;
4130 dstr = NEWSV(30, m-s);
4131 sv_setpvn(dstr, s, m-s);
4138 else if (rx->check_substr && !rx->nparens
4139 && (rx->reganch & ROPT_CHECK_ALL)
4140 && !(rx->reganch & ROPT_ANCH)) {
4141 i = SvCUR(rx->check_substr);
4142 if (i == 1 && !SvTAIL(rx->check_substr)) {
4143 i = *SvPVX(rx->check_substr);
4146 for (m = s; m < strend && *m != i; m++) ;
4149 dstr = NEWSV(30, m-s);
4150 sv_setpvn(dstr, s, m-s);
4159 while (s < strend && --limit &&
4160 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4161 rx->check_substr)) )
4164 dstr = NEWSV(31, m-s);
4165 sv_setpvn(dstr, s, m-s);
4174 maxiters += (strend - s) * rx->nparens;
4175 while (s < strend && --limit &&
4176 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4178 TAINT_IF(RX_MATCH_TAINTED(rx));
4180 && rx->subbase != orig) {
4185 strend = s + (strend - m);
4188 dstr = NEWSV(32, m-s);
4189 sv_setpvn(dstr, s, m-s);
4194 for (i = 1; i <= rx->nparens; i++) {
4198 dstr = NEWSV(33, m-s);
4199 sv_setpvn(dstr, s, m-s);
4202 dstr = NEWSV(33, 0);
4211 LEAVE_SCOPE(oldsave);
4212 iters = (SP - stack_base) - base;
4213 if (iters > maxiters)
4216 /* keep field after final delim? */
4217 if (s < strend || (iters && origlimit)) {
4218 dstr = NEWSV(34, strend-s);
4219 sv_setpvn(dstr, s, strend-s);
4225 else if (!origlimit) {
4226 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4230 SWITCHSTACK(ary, oldstack);
4231 if (SvSMAGICAL(ary)) {
4236 if (gimme == G_ARRAY) {
4238 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4244 if (gimme == G_ARRAY)
4247 if (iters || !pm->op_pmreplroot) {
4257 unlock_condpair(void *svv)
4260 MAGIC *mg = mg_find((SV*)svv, 'm');
4263 croak("panic: unlock_condpair unlocking non-mutex");
4264 MUTEX_LOCK(MgMUTEXP(mg));
4265 if (MgOWNER(mg) != thr)
4266 croak("panic: unlock_condpair unlocking mutex that we don't own");
4268 COND_SIGNAL(MgOWNERCONDP(mg));
4269 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4270 (unsigned long)thr, (unsigned long)svv);)
4271 MUTEX_UNLOCK(MgMUTEXP(mg));
4273 #endif /* USE_THREADS */
4286 mg = condpair_magic(sv);
4287 MUTEX_LOCK(MgMUTEXP(mg));
4288 if (MgOWNER(mg) == thr)
4289 MUTEX_UNLOCK(MgMUTEXP(mg));
4292 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4294 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4295 (unsigned long)thr, (unsigned long)sv);)
4296 MUTEX_UNLOCK(MgMUTEXP(mg));
4297 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4298 save_destructor(unlock_condpair, sv);
4300 #endif /* USE_THREADS */
4301 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4302 || SvTYPE(retsv) == SVt_PVCV) {
4303 retsv = refto(retsv);
4314 if (op->op_private & OPpLVAL_INTRO)
4315 PUSHs(*save_threadsv(op->op_targ));
4317 PUSHs(THREADSV(op->op_targ));
4320 DIE("tried to access per-thread data in non-threaded perl");
4321 #endif /* USE_THREADS */