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)
101 static void doencodes _((SV* sv, char* s, I32 len));
102 static SV* refto _((SV* sv));
103 static U32 seed _((void));
106 static bool srand_called = FALSE;
108 /* variations on pp_null */
114 /* XXX I can't imagine anyone who doesn't have this actually _needs_
115 it, since pid_t is an integral type.
118 #ifdef NEED_GETPID_PROTO
119 extern Pid_t getpid (void);
125 if (GIMME_V == G_SCALAR)
140 if (op->op_private & OPpLVAL_INTRO)
141 SAVECLEARSV(curpad[op->op_targ]);
143 if (op->op_flags & OPf_REF) {
147 if (GIMME == G_ARRAY) {
148 I32 maxarg = AvFILL((AV*)TARG) + 1;
150 if (SvMAGICAL(TARG)) {
152 for (i=0; i < maxarg; i++) {
153 SV **svp = av_fetch((AV*)TARG, i, FALSE);
154 SP[i+1] = (svp) ? *svp : &sv_undef;
158 Copy(AvARRAY((AV*)TARG), SP+1, maxarg, SV*);
163 SV* sv = sv_newmortal();
164 I32 maxarg = AvFILL((AV*)TARG) + 1;
165 sv_setiv(sv, maxarg);
177 if (op->op_private & OPpLVAL_INTRO)
178 SAVECLEARSV(curpad[op->op_targ]);
179 if (op->op_flags & OPf_REF)
182 if (gimme == G_ARRAY) {
183 RETURNOP(do_kv(ARGS));
185 else if (gimme == G_SCALAR) {
186 SV* sv = sv_newmortal();
187 if (HvFILL((HV*)TARG))
188 sv_setpvf(sv, "%ld/%ld",
189 (long)HvFILL((HV*)TARG), (long)HvMAX((HV*)TARG) + 1);
199 DIE("NOT IMPL LINE %d",__LINE__);
211 if (SvTYPE(sv) == SVt_PVIO) {
212 GV *gv = (GV*) sv_newmortal();
213 gv_init(gv, 0, "", 0, 0);
214 GvIOp(gv) = (IO *)sv;
215 (void)SvREFCNT_inc(sv);
217 } else if (SvTYPE(sv) != SVt_PVGV)
218 DIE("Not a GLOB reference");
221 if (SvTYPE(sv) != SVt_PVGV) {
224 if (SvGMAGICAL(sv)) {
230 if (op->op_flags & OPf_REF ||
231 op->op_private & HINT_STRICT_REFS)
232 DIE(no_usym, "a symbol");
238 if (op->op_private & HINT_STRICT_REFS)
239 DIE(no_symref, sym, "a symbol");
240 sv = (SV*)gv_fetchpv(sym, TRUE, SVt_PVGV);
243 if (op->op_private & OPpLVAL_INTRO)
244 save_gp((GV*)sv, !(op->op_flags & OPf_SPECIAL));
256 switch (SvTYPE(sv)) {
260 DIE("Not a SCALAR reference");
267 if (SvTYPE(gv) != SVt_PVGV) {
268 if (SvGMAGICAL(sv)) {
274 if (op->op_flags & OPf_REF ||
275 op->op_private & HINT_STRICT_REFS)
276 DIE(no_usym, "a SCALAR");
282 if (op->op_private & HINT_STRICT_REFS)
283 DIE(no_symref, sym, "a SCALAR");
284 gv = (GV*)gv_fetchpv(sym, TRUE, SVt_PV);
288 if (op->op_flags & OPf_MOD) {
289 if (op->op_private & OPpLVAL_INTRO)
290 sv = save_scalar((GV*)TOPs);
291 else if (op->op_private & OPpDEREF)
292 vivify_ref(sv, op->op_private & OPpDEREF);
302 SV *sv = AvARYLEN(av);
304 AvARYLEN(av) = sv = NEWSV(0,0);
305 sv_upgrade(sv, SVt_IV);
306 sv_magic(sv, (SV*)av, '#', Nullch, 0);
314 djSP; dTARGET; dPOPss;
316 if (op->op_flags & OPf_MOD) {
317 if (SvTYPE(TARG) < SVt_PVLV) {
318 sv_upgrade(TARG, SVt_PVLV);
319 sv_magic(TARG, Nullsv, '.', Nullch, 0);
324 PUSHs(TARG); /* no SvSETMAGIC */
330 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)) {
331 mg = mg_find(sv, 'g');
332 if (mg && mg->mg_len >= 0) {
333 PUSHi(mg->mg_len + curcop->cop_arybase);
347 /* We usually try to add a non-existent subroutine in case of AUTOLOAD. */
348 /* (But not in defined().) */
349 CV *cv = sv_2cv(TOPs, &stash, &gv, !(op->op_flags & OPf_SPECIAL));
352 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
369 if (SvPOK(TOPs) && SvCUR(TOPs) >= 7) {
370 char *s = SvPVX(TOPs);
371 if (strnEQ(s, "CORE::", 6)) {
374 code = keyword(s + 6, SvCUR(TOPs) - 6);
375 if (code < 0) { /* Overridable. */
376 #define MAX_ARGS_OP ((sizeof(I32) - 1) * 2)
377 int i = 0, n = 0, seen_question = 0;
379 char str[ MAX_ARGS_OP * 2 + 2 ]; /* One ';', one '\0' */
381 while (i < MAXO) { /* The slow way. */
382 if (strEQ(s + 6, op_name[i]) || strEQ(s + 6, op_desc[i]))
386 goto nonesuch; /* Should not happen... */
388 oa = opargs[i] >> OASHIFT;
390 if (oa & OA_OPTIONAL) {
393 } else if (seen_question)
394 goto set; /* XXXX system, exec */
395 if ((oa & (OA_OPTIONAL - 1)) >= OA_AVREF
396 && (oa & (OA_OPTIONAL - 1)) <= OA_HVREF) {
399 /* What to do with R ((un)tie, tied, (sys)read, recv)? */
400 str[n++] = ("?$@@%&*$")[oa & (OA_OPTIONAL - 1)];
404 ret = sv_2mortal(newSVpv(str, n - 1));
405 } else if (code) /* Non-Overridable */
407 else { /* None such */
409 croak("Cannot find an opnumber for \"%s\"", s+6);
413 cv = sv_2cv(TOPs, &stash, &gv, FALSE);
415 ret = sv_2mortal(newSVpv(SvPVX(cv), SvCUR(cv)));
424 CV* cv = (CV*)curpad[op->op_targ];
426 cv = (CV*)sv_2mortal((SV*)cv_clone(cv));
442 if (GIMME != G_ARRAY) {
446 EXTEND_MORTAL(SP - MARK);
448 *MARK = refto(*MARK);
457 if (SvTYPE(sv) == SVt_PVLV && LvTYPE(sv) == 'y') {
460 if (!(sv = LvTARG(sv)))
463 else if (SvPADTMP(sv))
467 (void)SvREFCNT_inc(sv);
470 sv_upgrade(rv, SVt_RV);
484 if (sv && SvGMAGICAL(sv))
487 if (!sv || !SvROK(sv))
491 pv = sv_reftype(sv,TRUE);
492 PUSHp(pv, strlen(pv));
502 stash = curcop->cop_stash;
504 stash = gv_stashsv(POPs, TRUE);
506 (void)sv_bless(TOPs, stash);
523 switch (elem ? *elem : '\0')
526 if (strEQ(elem, "ARRAY"))
527 tmpRef = (SV*)GvAV(gv);
530 if (strEQ(elem, "CODE"))
531 tmpRef = (SV*)GvCVu(gv);
534 if (strEQ(elem, "FILEHANDLE")) /* XXX deprecate in 5.005 */
535 tmpRef = (SV*)GvIOp(gv);
538 if (strEQ(elem, "GLOB"))
542 if (strEQ(elem, "HASH"))
543 tmpRef = (SV*)GvHV(gv);
546 if (strEQ(elem, "IO"))
547 tmpRef = (SV*)GvIOp(gv);
550 if (strEQ(elem, "NAME"))
551 sv = newSVpv(GvNAME(gv), GvNAMELEN(gv));
554 if (strEQ(elem, "PACKAGE"))
555 sv = newSVpv(HvNAME(GvSTASH(gv)), 0);
558 if (strEQ(elem, "SCALAR"))
572 /* Pattern matching */
577 register UNOP *unop = cUNOP;
578 register unsigned char *s;
581 register I32 *sfirst;
585 if(unop->op_first && unop->op_first->op_type == OP_PUSHRE) {
586 PMOP *pm = (PMOP *)unop->op_first;
587 SV *rv = sv_newmortal();
588 sv = newSVrv(rv, "Regexp");
589 sv_magic(sv,(SV*)ReREFCNT_inc(pm->op_pmregexp),'r',0,0);
593 if (sv == lastscream) {
599 SvSCREAM_off(lastscream);
600 SvREFCNT_dec(lastscream);
602 lastscream = SvREFCNT_inc(sv);
605 s = (unsigned char*)(SvPV(sv, len));
609 if (pos > maxscream) {
611 maxscream = pos + 80;
612 New(301, screamfirst, 256, I32);
613 New(302, screamnext, maxscream, I32);
616 maxscream = pos + pos / 4;
617 Renew(screamnext, maxscream, I32);
621 sfirst = screamfirst;
624 if (!sfirst || !snext)
625 DIE("do_study: out of memory");
627 for (ch = 256; ch; --ch)
634 snext[pos] = sfirst[ch] - pos;
641 sv_magic(sv, Nullsv, 'g', Nullch, 0); /* piggyback on m//g magic */
650 if (op->op_flags & OPf_STACKED)
656 TARG = sv_newmortal();
657 PUSHi(do_trans(sv, op));
661 /* Lvalue operators. */
673 djSP; dMARK; dTARGET;
683 SETi(do_chomp(TOPs));
689 djSP; dMARK; dTARGET;
690 register I32 count = 0;
693 count += do_chomp(POPs);
704 if (!sv || !SvANY(sv))
706 switch (SvTYPE(sv)) {
708 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv))
712 if (HvARRAY(sv) || SvGMAGICAL(sv))
716 if (CvROOT(sv) || CvXSUB(sv))
733 if (!op->op_private) {
742 if (SvTHINKFIRST(sv)) {
749 switch (SvTYPE(sv)) {
759 if (cv_const_sv((CV*)sv))
760 warn("Constant subroutine %s undefined",
761 CvANON((CV*)sv) ? "(anonymous)" : GvENAME(CvGV((CV*)sv)));
764 { GV* gv = (GV*)SvREFCNT_inc(CvGV((CV*)sv));
766 CvGV((CV*)sv) = gv; } /* let user-undef'd sub keep its identity */
770 sv_setsv(sv, &sv_undef);
773 if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) {
776 SvPV_set(sv, Nullch);
789 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
791 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
792 SvIVX(TOPs) != IV_MIN)
795 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
806 if (SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
808 sv_setsv(TARG, TOPs);
809 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
810 SvIVX(TOPs) != IV_MAX)
813 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
827 if(SvREADONLY(TOPs) || SvTYPE(TOPs) > SVt_PVLV)
829 sv_setsv(TARG, TOPs);
830 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs) &&
831 SvIVX(TOPs) != IV_MIN)
834 SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
843 /* Ordinary operators. */
847 djSP; dATARGET; tryAMAGICbin(pow,opASSIGN);
850 SETn( pow( left, right) );
857 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
860 SETn( left * right );
867 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
872 DIE("Illegal division by zero");
874 /* insure that 20./5. == 4. */
877 if ((double)I_V(left) == left &&
878 (double)I_V(right) == right &&
879 (k = I_V(left)/I_V(right))*I_V(right) == I_V(left)) {
882 value = left / right;
886 value = left / right;
895 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
903 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
905 right = (right_neg = (i < 0)) ? -i : i;
909 right = U_V((right_neg = (n < 0)) ? -n : n);
912 if (SvIOK(TOPs) && !SvNOK(TOPs) && !SvPOK(TOPs)) {
914 left = (left_neg = (i < 0)) ? -i : i;
918 left = U_V((left_neg = (n < 0)) ? -n : n);
922 DIE("Illegal modulus zero");
925 if ((left_neg != right_neg) && ans)
928 /* XXX may warn: unary minus operator applied to unsigned type */
929 /* could change -foo to be (~foo)+1 instead */
930 if (ans <= ~((UV)IV_MAX)+1)
931 sv_setiv(TARG, ~ans+1);
933 sv_setnv(TARG, -(double)ans);
944 djSP; dATARGET; tryAMAGICbin(repeat,opASSIGN);
946 register I32 count = POPi;
947 if (GIMME == G_ARRAY && op->op_private & OPpREPEAT_DOLIST) {
949 I32 items = SP - MARK;
961 repeatcpy((char*)(MARK + items), (char*)MARK,
962 items * sizeof(SV*), count - 1);
968 else { /* Note: mark already snarfed by pp_list */
973 if (TARG == tmpstr && SvTHINKFIRST(tmpstr)) {
974 if (SvREADONLY(tmpstr) && curcop != &compiling)
975 DIE("Can't x= to readonly value");
979 SvSetSV(TARG, tmpstr);
980 SvPV_force(TARG, len);
985 SvGROW(TARG, (count * len) + 1);
986 repeatcpy(SvPVX(TARG) + len, SvPVX(TARG), len, count - 1);
987 SvCUR(TARG) *= count;
991 (void)SvPOK_only(TARG);
1000 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1003 SETn( left - right );
1010 djSP; dATARGET; tryAMAGICbin(lshift,opASSIGN);
1013 if (op->op_private & HINT_INTEGER) {
1015 i = BWi(i) << shift;
1029 djSP; dATARGET; tryAMAGICbin(rshift,opASSIGN);
1032 if (op->op_private & HINT_INTEGER) {
1034 i = BWi(i) >> shift;
1048 djSP; tryAMAGICbinSET(lt,0);
1051 SETs(boolSV(TOPn < value));
1058 djSP; tryAMAGICbinSET(gt,0);
1061 SETs(boolSV(TOPn > value));
1068 djSP; tryAMAGICbinSET(le,0);
1071 SETs(boolSV(TOPn <= value));
1078 djSP; tryAMAGICbinSET(ge,0);
1081 SETs(boolSV(TOPn >= value));
1088 djSP; tryAMAGICbinSET(ne,0);
1091 SETs(boolSV(TOPn != value));
1098 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1105 else if (left < right)
1107 else if (left > right)
1120 djSP; tryAMAGICbinSET(slt,0);
1123 int cmp = ((op->op_private & OPpLOCALE)
1124 ? sv_cmp_locale(left, right)
1125 : sv_cmp(left, right));
1126 SETs(boolSV(cmp < 0));
1133 djSP; tryAMAGICbinSET(sgt,0);
1136 int cmp = ((op->op_private & OPpLOCALE)
1137 ? sv_cmp_locale(left, right)
1138 : sv_cmp(left, right));
1139 SETs(boolSV(cmp > 0));
1146 djSP; tryAMAGICbinSET(sle,0);
1149 int cmp = ((op->op_private & OPpLOCALE)
1150 ? sv_cmp_locale(left, right)
1151 : sv_cmp(left, right));
1152 SETs(boolSV(cmp <= 0));
1159 djSP; tryAMAGICbinSET(sge,0);
1162 int cmp = ((op->op_private & OPpLOCALE)
1163 ? sv_cmp_locale(left, right)
1164 : sv_cmp(left, right));
1165 SETs(boolSV(cmp >= 0));
1172 djSP; tryAMAGICbinSET(seq,0);
1175 SETs(boolSV(sv_eq(left, right)));
1182 djSP; tryAMAGICbinSET(sne,0);
1185 SETs(boolSV(!sv_eq(left, right)));
1192 djSP; dTARGET; tryAMAGICbin(scmp,0);
1195 int cmp = ((op->op_private & OPpLOCALE)
1196 ? sv_cmp_locale(left, right)
1197 : sv_cmp(left, right));
1205 djSP; dATARGET; tryAMAGICbin(band,opASSIGN);
1208 if (SvNIOKp(left) || SvNIOKp(right)) {
1209 if (op->op_private & HINT_INTEGER) {
1210 IBW value = SvIV(left) & SvIV(right);
1214 UBW value = SvUV(left) & SvUV(right);
1219 do_vop(op->op_type, TARG, left, right);
1228 djSP; dATARGET; tryAMAGICbin(bxor,opASSIGN);
1231 if (SvNIOKp(left) || SvNIOKp(right)) {
1232 if (op->op_private & HINT_INTEGER) {
1233 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) ^ SvIV(right);
1237 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) ^ SvUV(right);
1242 do_vop(op->op_type, TARG, left, right);
1251 djSP; dATARGET; tryAMAGICbin(bor,opASSIGN);
1254 if (SvNIOKp(left) || SvNIOKp(right)) {
1255 if (op->op_private & HINT_INTEGER) {
1256 IBW value = (USE_LEFT(left) ? SvIV(left) : 0) | SvIV(right);
1260 UBW value = (USE_LEFT(left) ? SvUV(left) : 0) | SvUV(right);
1265 do_vop(op->op_type, TARG, left, right);
1274 djSP; dTARGET; tryAMAGICun(neg);
1279 if (SvIOKp(sv) && !SvNOKp(sv) && !SvPOKp(sv) && SvIVX(sv) != IV_MIN)
1281 else if (SvNIOKp(sv))
1283 else if (SvPOKp(sv)) {
1285 char *s = SvPV(sv, len);
1286 if (isIDFIRST(*s)) {
1287 sv_setpvn(TARG, "-", 1);
1290 else if (*s == '+' || *s == '-') {
1292 *SvPV_force(TARG, len) = *s == '-' ? '+' : '-';
1295 sv_setnv(TARG, -SvNV(sv));
1307 djSP; tryAMAGICunSET(not);
1308 #endif /* OVERLOAD */
1309 *stack_sp = boolSV(!SvTRUE(*stack_sp));
1315 djSP; dTARGET; tryAMAGICun(compl);
1319 if (op->op_private & HINT_INTEGER) {
1320 IBW value = ~SvIV(sv);
1324 UBW value = ~SvUV(sv);
1329 register char *tmps;
1330 register long *tmpl;
1335 tmps = SvPV_force(TARG, len);
1338 for ( ; anum && (unsigned long)tmps % sizeof(long); anum--, tmps++)
1341 for ( ; anum >= sizeof(long); anum -= sizeof(long), tmpl++)
1345 for ( ; anum > 0; anum--, tmps++)
1354 /* integer versions of some of the above */
1358 djSP; dATARGET; tryAMAGICbin(mult,opASSIGN);
1361 SETi( left * right );
1368 djSP; dATARGET; tryAMAGICbin(div,opASSIGN);
1372 DIE("Illegal division by zero");
1373 value = POPi / value;
1381 djSP; dATARGET; tryAMAGICbin(modulo,opASSIGN);
1385 DIE("Illegal modulus zero");
1386 SETi( left % right );
1393 djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
1396 SETi( left + right );
1403 djSP; dATARGET; tryAMAGICbin(subtr,opASSIGN);
1406 SETi( left - right );
1413 djSP; tryAMAGICbinSET(lt,0);
1416 SETs(boolSV(left < right));
1423 djSP; tryAMAGICbinSET(gt,0);
1426 SETs(boolSV(left > right));
1433 djSP; tryAMAGICbinSET(le,0);
1436 SETs(boolSV(left <= right));
1443 djSP; tryAMAGICbinSET(ge,0);
1446 SETs(boolSV(left >= right));
1453 djSP; tryAMAGICbinSET(eq,0);
1456 SETs(boolSV(left == right));
1463 djSP; tryAMAGICbinSET(ne,0);
1466 SETs(boolSV(left != right));
1473 djSP; dTARGET; tryAMAGICbin(ncmp,0);
1480 else if (left < right)
1491 djSP; dTARGET; tryAMAGICun(neg);
1496 /* High falutin' math. */
1500 djSP; dTARGET; tryAMAGICbin(atan2,0);
1503 SETn(atan2(left, right));
1510 djSP; dTARGET; tryAMAGICun(sin);
1522 djSP; dTARGET; tryAMAGICun(cos);
1542 if (!srand_called) {
1543 (void)srand((unsigned)seed());
1544 srand_called = TRUE;
1547 value = rand() * value / 2147483648.0;
1550 value = rand() * value / 65536.0;
1553 value = rand() * value / 32768.0;
1555 value = rand() * value / (double)(((unsigned long)1) << RANDBITS);
1571 (void)srand((unsigned)anum);
1572 srand_called = TRUE;
1581 * This is really just a quick hack which grabs various garbage
1582 * values. It really should be a real hash algorithm which
1583 * spreads the effect of every input bit onto every output bit,
1584 * if someone who knows about such tings would bother to write it.
1585 * Might be a good idea to add that function to CORE as well.
1586 * No numbers below come from careful analysis or anyting here,
1587 * except they are primes and SEED_C1 > 1E6 to get a full-width
1588 * value from (tv_sec * SEED_C1 + tv_usec). The multipliers should
1589 * probably be bigger too.
1592 # define SEED_C1 1000003
1593 #define SEED_C4 73819
1595 # define SEED_C1 25747
1596 #define SEED_C4 20639
1600 #define SEED_C5 26107
1605 # include <starlet.h>
1606 /* when[] = (low 32 bits, high 32 bits) of time since epoch
1607 * in 100-ns units, typically incremented ever 10 ms. */
1608 unsigned int when[2];
1609 _ckvmssts(sys$gettim(when));
1610 u = (U32)SEED_C1 * when[0] + (U32)SEED_C2 * when[1];
1612 # ifdef HAS_GETTIMEOFDAY
1613 struct timeval when;
1614 gettimeofday(&when,(struct timezone *) 0);
1615 u = (U32)SEED_C1 * when.tv_sec + (U32)SEED_C2 * when.tv_usec;
1619 u = (U32)SEED_C1 * when;
1622 u += SEED_C3 * (U32)getpid();
1623 u += SEED_C4 * (U32)(UV)stack_sp;
1624 #ifndef PLAN9 /* XXX Plan9 assembler chokes on this; fix needed */
1625 u += SEED_C5 * (U32)(UV)&when;
1632 djSP; dTARGET; tryAMAGICun(exp);
1644 djSP; dTARGET; tryAMAGICun(log);
1649 SET_NUMERIC_STANDARD();
1650 DIE("Can't take log of %g", value);
1660 djSP; dTARGET; tryAMAGICun(sqrt);
1665 SET_NUMERIC_STANDARD();
1666 DIE("Can't take sqrt of %g", value);
1668 value = sqrt(value);
1678 double value = TOPn;
1681 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs)) {
1687 (void)modf(value, &value);
1689 (void)modf(-value, &value);
1704 djSP; dTARGET; tryAMAGICun(abs);
1706 double value = TOPn;
1709 if (SvIOKp(TOPs) && !SvNOKp(TOPs) && !SvPOKp(TOPs) &&
1710 (iv = SvIVX(TOPs)) != IV_MIN) {
1731 XPUSHu(scan_hex(tmps, 99, &argtype));
1743 while (*tmps && isSPACE(*tmps))
1748 value = scan_hex(++tmps, 99, &argtype);
1750 value = scan_oct(tmps, 99, &argtype);
1760 SETi( sv_len(TOPs) );
1773 I32 lvalue = op->op_flags & OPf_MOD;
1775 I32 arybase = curcop->cop_arybase;
1781 tmps = SvPV(sv, curlen);
1782 if (pos >= arybase) {
1800 else if (len >= 0) {
1802 if (rem > (I32)curlen)
1816 if (dowarn || lvalue)
1817 warn("substr outside of string");
1822 sv_setpvn(TARG, tmps, rem);
1823 if (lvalue) { /* it's an lvalue! */
1824 if (!SvGMAGICAL(sv)) {
1828 warn("Attempt to use reference as lvalue in substr");
1830 if (SvOK(sv)) /* is it defined ? */
1831 (void)SvPOK_only(sv);
1833 sv_setpvn(sv,"",0); /* avoid lexical reincarnation */
1836 if (SvTYPE(TARG) < SVt_PVLV) {
1837 sv_upgrade(TARG, SVt_PVLV);
1838 sv_magic(TARG, Nullsv, 'x', Nullch, 0);
1843 LvTARGOFF(TARG) = pos;
1844 LvTARGLEN(TARG) = rem;
1847 PUSHs(TARG); /* avoid SvSETMAGIC here */
1854 register I32 size = POPi;
1855 register I32 offset = POPi;
1856 register SV *src = POPs;
1857 I32 lvalue = op->op_flags & OPf_MOD;
1859 unsigned char *s = (unsigned char*)SvPV(src, srclen);
1860 unsigned long retnum;
1863 offset *= size; /* turn into bit offset */
1864 len = (offset + size + 7) / 8;
1865 if (offset < 0 || size < 1)
1868 if (lvalue) { /* it's an lvalue! */
1869 if (SvTYPE(TARG) < SVt_PVLV) {
1870 sv_upgrade(TARG, SVt_PVLV);
1871 sv_magic(TARG, Nullsv, 'v', Nullch, 0);
1876 LvTARGOFF(TARG) = offset;
1877 LvTARGLEN(TARG) = size;
1885 if (offset >= srclen)
1888 retnum = (unsigned long) s[offset] << 8;
1890 else if (size == 32) {
1891 if (offset >= srclen)
1893 else if (offset + 1 >= srclen)
1894 retnum = (unsigned long) s[offset] << 24;
1895 else if (offset + 2 >= srclen)
1896 retnum = ((unsigned long) s[offset] << 24) +
1897 ((unsigned long) s[offset + 1] << 16);
1899 retnum = ((unsigned long) s[offset] << 24) +
1900 ((unsigned long) s[offset + 1] << 16) +
1901 (s[offset + 2] << 8);
1906 retnum = (s[offset >> 3] >> (offset & 7)) & ((1 << size) - 1);
1911 else if (size == 16)
1912 retnum = ((unsigned long) s[offset] << 8) + s[offset+1];
1913 else if (size == 32)
1914 retnum = ((unsigned long) s[offset] << 24) +
1915 ((unsigned long) s[offset + 1] << 16) +
1916 (s[offset + 2] << 8) + s[offset+3];
1920 sv_setuv(TARG, (UV)retnum);
1935 I32 arybase = curcop->cop_arybase;
1940 offset = POPi - arybase;
1943 tmps = SvPV(big, biglen);
1946 else if (offset > biglen)
1948 if (!(tmps2 = fbm_instr((unsigned char*)tmps + offset,
1949 (unsigned char*)tmps + biglen, little)))
1950 retval = -1 + arybase;
1952 retval = tmps2 - tmps + arybase;
1969 I32 arybase = curcop->cop_arybase;
1975 tmps2 = SvPV(little, llen);
1976 tmps = SvPV(big, blen);
1980 offset = SvIV(offstr) - arybase + llen;
1983 else if (offset > blen)
1985 if (!(tmps2 = rninstr(tmps, tmps + offset,
1986 tmps2, tmps2 + llen)))
1987 retval = -1 + arybase;
1989 retval = tmps2 - tmps + arybase;
1996 djSP; dMARK; dORIGMARK; dTARGET;
1997 #ifdef USE_LOCALE_NUMERIC
1998 if (op->op_private & OPpLOCALE)
1999 SET_NUMERIC_LOCAL();
2001 SET_NUMERIC_STANDARD();
2003 do_sprintf(TARG, SP-MARK, MARK+1);
2004 TAINT_IF(SvTAINTED(TARG));
2018 value = (I32) (*tmps & 255);
2023 value = (I32) (anum & 255);
2034 (void)SvUPGRADE(TARG,SVt_PV);
2040 (void)SvPOK_only(TARG);
2047 djSP; dTARGET; dPOPTOPssrl;
2049 char *tmps = SvPV(left, na);
2051 sv_setpv(TARG, fcrypt(tmps, SvPV(right, na)));
2053 sv_setpv(TARG, crypt(tmps, SvPV(right, na)));
2057 "The crypt() function is unimplemented due to excessive paranoia.");
2069 if (!SvPADTMP(sv)) {
2075 s = SvPV_force(sv, na);
2077 if (op->op_private & OPpLOCALE) {
2080 *s = toUPPER_LC(*s);
2095 if (!SvPADTMP(sv)) {
2101 s = SvPV_force(sv, na);
2103 if (op->op_private & OPpLOCALE) {
2106 *s = toLOWER_LC(*s);
2123 if (!SvPADTMP(sv)) {
2130 s = SvPV_force(sv, len);
2132 register char *send = s + len;
2134 if (op->op_private & OPpLOCALE) {
2137 for (; s < send; s++)
2138 *s = toUPPER_LC(*s);
2141 for (; s < send; s++)
2155 if (!SvPADTMP(sv)) {
2162 s = SvPV_force(sv, len);
2164 register char *send = s + len;
2166 if (op->op_private & OPpLOCALE) {
2169 for (; s < send; s++)
2170 *s = toLOWER_LC(*s);
2173 for (; s < send; s++)
2185 register char *s = SvPV(sv,len);
2189 (void)SvUPGRADE(TARG, SVt_PV);
2190 SvGROW(TARG, (len * 2) + 1);
2198 SvCUR_set(TARG, d - SvPVX(TARG));
2199 (void)SvPOK_only(TARG);
2202 sv_setpvn(TARG, s, len);
2211 djSP; dMARK; dORIGMARK;
2213 register AV* av = (AV*)POPs;
2214 register I32 lval = op->op_flags & OPf_MOD;
2215 I32 arybase = curcop->cop_arybase;
2218 if (SvTYPE(av) == SVt_PVAV) {
2219 if (lval && op->op_private & OPpLVAL_INTRO) {
2221 for (svp = MARK + 1; svp <= SP; svp++) {
2226 if (max > AvMAX(av))
2229 while (++MARK <= SP) {
2230 elem = SvIVx(*MARK);
2234 svp = av_fetch(av, elem, lval);
2236 if (!svp || *svp == &sv_undef)
2237 DIE(no_aelem, elem);
2238 if (op->op_private & OPpLVAL_INTRO)
2239 save_aelem(av, elem, svp);
2241 *MARK = svp ? *svp : &sv_undef;
2244 if (GIMME != G_ARRAY) {
2252 /* Associative arrays. */
2257 HV *hash = (HV*)POPs;
2259 I32 gimme = GIMME_V;
2260 I32 realhv = (SvTYPE(hash) == SVt_PVHV);
2263 /* might clobber stack_sp */
2264 entry = realhv ? hv_iternext(hash) : avhv_iternext((AV*)hash);
2269 PUSHs(hv_iterkeysv(entry)); /* won't clobber stack_sp */
2270 if (gimme == G_ARRAY) {
2272 /* might clobber stack_sp */
2273 sv_setsv(TARG, realhv ?
2274 hv_iterval(hash, entry) : avhv_iterval((AV*)hash, entry));
2279 else if (gimme == G_SCALAR)
2298 I32 gimme = GIMME_V;
2299 I32 discard = (gimme == G_VOID) ? G_DISCARD : 0;
2303 if (op->op_private & OPpSLICE) {
2307 hvtype = SvTYPE(hv);
2308 while (++MARK <= SP) {
2309 if (hvtype == SVt_PVHV)
2310 sv = hv_delete_ent(hv, *MARK, discard, 0);
2311 else if (hvtype == SVt_PVAV)
2312 sv = avhv_delete_ent((AV*)hv, *MARK, discard, 0);
2314 DIE("Not a HASH reference");
2315 *MARK = sv ? sv : &sv_undef;
2319 else if (gimme == G_SCALAR) {
2328 if (SvTYPE(hv) == SVt_PVHV)
2329 sv = hv_delete_ent(hv, keysv, discard, 0);
2330 else if (SvTYPE(hv) == SVt_PVAV)
2331 sv = avhv_delete_ent((AV*)hv, keysv, discard, 0);
2333 DIE("Not a HASH reference");
2347 if (SvTYPE(hv) == SVt_PVHV) {
2348 if (hv_exists_ent(hv, tmpsv, 0))
2350 } else if (SvTYPE(hv) == SVt_PVAV) {
2351 if (avhv_exists_ent((AV*)hv, tmpsv, 0))
2354 DIE("Not a HASH reference");
2361 djSP; dMARK; dORIGMARK;
2363 register HV *hv = (HV*)POPs;
2364 register I32 lval = op->op_flags & OPf_MOD;
2365 I32 realhv = (SvTYPE(hv) == SVt_PVHV);
2367 if (realhv || SvTYPE(hv) == SVt_PVAV) {
2368 while (++MARK <= SP) {
2372 he = hv_fetch_ent(hv, keysv, lval, 0);
2373 svp = he ? &HeVAL(he) : 0;
2375 svp = avhv_fetch_ent((AV*)hv, keysv, lval, 0);
2378 if (!he || HeVAL(he) == &sv_undef)
2379 DIE(no_helem, SvPV(keysv, na));
2380 if (op->op_private & OPpLVAL_INTRO)
2381 save_helem(hv, keysv, &HeVAL(he));
2383 *MARK = he ? HeVAL(he) : &sv_undef;
2386 if (GIMME != G_ARRAY) {
2394 /* List operators. */
2399 if (GIMME != G_ARRAY) {
2401 *MARK = *SP; /* unwanted list, return last item */
2412 SV **lastrelem = stack_sp;
2413 SV **lastlelem = stack_base + POPMARK;
2414 SV **firstlelem = stack_base + POPMARK + 1;
2415 register SV **firstrelem = lastlelem + 1;
2416 I32 arybase = curcop->cop_arybase;
2417 I32 lval = op->op_flags & OPf_MOD;
2418 I32 is_something_there = lval;
2420 register I32 max = lastrelem - lastlelem;
2421 register SV **lelem;
2424 if (GIMME != G_ARRAY) {
2425 ix = SvIVx(*lastlelem);
2430 if (ix < 0 || ix >= max)
2431 *firstlelem = &sv_undef;
2433 *firstlelem = firstrelem[ix];
2439 SP = firstlelem - 1;
2443 for (lelem = firstlelem; lelem <= lastlelem; lelem++) {
2449 else if (!(*lelem = firstrelem[ix]))
2454 if (ix >= max || !(*lelem = firstrelem[ix]))
2457 if (!is_something_there && (SvOK(*lelem) || SvGMAGICAL(*lelem)))
2458 is_something_there = TRUE;
2460 if (is_something_there)
2463 SP = firstlelem - 1;
2469 djSP; dMARK; dORIGMARK;
2470 I32 items = SP - MARK;
2471 SV *av = sv_2mortal((SV*)av_make(items, MARK+1));
2472 SP = ORIGMARK; /* av_make() might realloc stack_sp */
2479 djSP; dMARK; dORIGMARK;
2480 HV* hv = (HV*)sv_2mortal((SV*)newHV());
2484 SV *val = NEWSV(46, 0);
2486 sv_setsv(val, *++MARK);
2488 warn("Odd number of elements in hash list");
2489 (void)hv_store_ent(hv,key,val,0);
2498 djSP; dMARK; dORIGMARK;
2499 register AV *ary = (AV*)*++MARK;
2503 register I32 offset;
2504 register I32 length;
2511 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2512 *MARK-- = mg->mg_obj;
2516 perl_call_method("SPLICE",GIMME_V);
2525 offset = i = SvIVx(*MARK);
2527 offset += AvFILLp(ary) + 1;
2529 offset -= curcop->cop_arybase;
2533 length = SvIVx(*MARK++);
2538 length = AvMAX(ary) + 1; /* close enough to infinity */
2542 length = AvMAX(ary) + 1;
2544 if (offset > AvFILLp(ary) + 1)
2545 offset = AvFILLp(ary) + 1;
2546 after = AvFILLp(ary) + 1 - (offset + length);
2547 if (after < 0) { /* not that much array */
2548 length += after; /* offset+length now in array */
2554 /* At this point, MARK .. SP-1 is our new LIST */
2557 diff = newlen - length;
2558 if (newlen && !AvREAL(ary)) {
2562 assert(AvREAL(ary)); /* would leak, so croak */
2565 if (diff < 0) { /* shrinking the area */
2567 New(451, tmparyval, newlen, SV*); /* so remember insertion */
2568 Copy(MARK, tmparyval, newlen, SV*);
2571 MARK = ORIGMARK + 1;
2572 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2573 MEXTEND(MARK, length);
2574 Copy(AvARRAY(ary)+offset, MARK, length, SV*);
2576 EXTEND_MORTAL(length);
2577 for (i = length, dst = MARK; i; i--) {
2578 if (!SvIMMORTAL(*dst))
2579 sv_2mortal(*dst); /* free them eventualy */
2586 *MARK = AvARRAY(ary)[offset+length-1];
2588 if (!SvIMMORTAL(*MARK))
2590 for (i = length - 1, dst = &AvARRAY(ary)[offset]; i > 0; i--)
2591 SvREFCNT_dec(*dst++); /* free them now */
2594 AvFILLp(ary) += diff;
2596 /* pull up or down? */
2598 if (offset < after) { /* easier to pull up */
2599 if (offset) { /* esp. if nothing to pull */
2600 src = &AvARRAY(ary)[offset-1];
2601 dst = src - diff; /* diff is negative */
2602 for (i = offset; i > 0; i--) /* can't trust Copy */
2606 SvPVX(ary) = (char*)(AvARRAY(ary) - diff); /* diff is negative */
2610 if (after) { /* anything to pull down? */
2611 src = AvARRAY(ary) + offset + length;
2612 dst = src + diff; /* diff is negative */
2613 Move(src, dst, after, SV*);
2615 dst = &AvARRAY(ary)[AvFILLp(ary)+1];
2616 /* avoid later double free */
2620 dst[--i] = &sv_undef;
2623 for (src = tmparyval, dst = AvARRAY(ary) + offset;
2625 *dst = NEWSV(46, 0);
2626 sv_setsv(*dst++, *src++);
2628 Safefree(tmparyval);
2631 else { /* no, expanding (or same) */
2633 New(452, tmparyval, length, SV*); /* so remember deletion */
2634 Copy(AvARRAY(ary)+offset, tmparyval, length, SV*);
2637 if (diff > 0) { /* expanding */
2639 /* push up or down? */
2641 if (offset < after && diff <= AvARRAY(ary) - AvALLOC(ary)) {
2645 Move(src, dst, offset, SV*);
2647 SvPVX(ary) = (char*)(AvARRAY(ary) - diff);/* diff is positive */
2649 AvFILLp(ary) += diff;
2652 if (AvFILLp(ary) + diff >= AvMAX(ary)) /* oh, well */
2653 av_extend(ary, AvFILLp(ary) + diff);
2654 AvFILLp(ary) += diff;
2657 dst = AvARRAY(ary) + AvFILLp(ary);
2659 for (i = after; i; i--) {
2666 for (src = MARK, dst = AvARRAY(ary) + offset; newlen; newlen--) {
2667 *dst = NEWSV(46, 0);
2668 sv_setsv(*dst++, *src++);
2670 MARK = ORIGMARK + 1;
2671 if (GIMME == G_ARRAY) { /* copy return vals to stack */
2673 Copy(tmparyval, MARK, length, SV*);
2675 EXTEND_MORTAL(length);
2676 for (i = length, dst = MARK; i; i--) {
2677 if (!SvIMMORTAL(*dst))
2678 sv_2mortal(*dst); /* free them eventualy */
2682 Safefree(tmparyval);
2686 else if (length--) {
2687 *MARK = tmparyval[length];
2689 if (!SvIMMORTAL(*MARK))
2691 while (length-- > 0)
2692 SvREFCNT_dec(tmparyval[length]);
2694 Safefree(tmparyval);
2705 djSP; dMARK; dORIGMARK; dTARGET;
2706 register AV *ary = (AV*)*++MARK;
2707 register SV *sv = &sv_undef;
2710 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2711 *MARK-- = mg->mg_obj;
2715 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
2720 /* Why no pre-extend of ary here ? */
2721 for (++MARK; MARK <= SP; MARK++) {
2724 sv_setsv(sv, *MARK);
2729 PUSHi( AvFILL(ary) + 1 );
2737 SV *sv = av_pop(av);
2738 if (!SvIMMORTAL(sv) && AvREAL(av))
2739 (void)sv_2mortal(sv);
2748 SV *sv = av_shift(av);
2752 if (!SvIMMORTAL(sv) && AvREAL(av))
2753 (void)sv_2mortal(sv);
2760 djSP; dMARK; dORIGMARK; dTARGET;
2761 register AV *ary = (AV*)*++MARK;
2766 if (SvRMAGICAL(ary) && (mg = mg_find((SV*)ary,'P'))) {
2767 *MARK-- = mg->mg_obj;
2771 perl_call_method("UNSHIFT",G_SCALAR|G_DISCARD);
2776 av_unshift(ary, SP - MARK);
2779 sv_setsv(sv, *++MARK);
2780 (void)av_store(ary, i++, sv);
2784 PUSHi( AvFILL(ary) + 1 );
2794 if (GIMME == G_ARRAY) {
2805 register char *down;
2811 do_join(TARG, &sv_no, MARK, SP);
2813 sv_setsv(TARG, (SP > MARK) ? *SP : DEFSV);
2814 up = SvPV_force(TARG, len);
2816 down = SvPVX(TARG) + len - 1;
2822 (void)SvPOK_only(TARG);
2831 mul128(SV *sv, U8 m)
2834 char *s = SvPV(sv, len);
2838 if (!strnEQ(s, "0000", 4)) { /* need to grow sv */
2839 SV *New = newSVpv("0000000000", 10);
2842 SvREFCNT_dec(sv); /* free old sv */
2847 while (!*t) /* trailing '\0'? */
2850 i = ((*t - '0') << 7) + m;
2851 *(t--) = '0' + (i % 10);
2857 /* Explosives and implosives. */
2864 I32 gimme = GIMME_V;
2868 register char *pat = SvPV(left, llen);
2869 register char *s = SvPV(right, rlen);
2870 char *strend = s + rlen;
2872 register char *patend = pat + llen;
2877 /* These must not be in registers: */
2888 unsigned Quad_t auquad;
2894 register U32 culong;
2896 static char* bitcount = 0;
2899 if (gimme != G_ARRAY) { /* arrange to do first one only */
2901 for (patend = pat; !isALPHA(*patend) || *patend == 'x'; patend++) ;
2902 if (strchr("aAbBhHP", *patend) || *pat == '%') {
2904 while (isDIGIT(*patend) || *patend == '*')
2910 while (pat < patend) {
2912 datumtype = *pat++ & 0xFF;
2913 if (isSPACE(datumtype))
2917 else if (*pat == '*') {
2918 len = strend - strbeg; /* long enough */
2921 else if (isDIGIT(*pat)) {
2923 while (isDIGIT(*pat))
2924 len = (len * 10) + (*pat++ - '0');
2927 len = (datumtype != '@');
2930 croak("Invalid type in unpack: '%c'", (int)datumtype);
2931 case ',': /* grandfather in commas but with a warning */
2932 if (commas++ == 0 && dowarn)
2933 warn("Invalid type in unpack: '%c'", (int)datumtype);
2936 if (len == 1 && pat[-1] != '1')
2945 if (len > strend - strbeg)
2946 DIE("@ outside of string");
2950 if (len > s - strbeg)
2951 DIE("X outside of string");
2955 if (len > strend - s)
2956 DIE("x outside of string");
2961 if (len > strend - s)
2964 goto uchar_checksum;
2965 sv = NEWSV(35, len);
2966 sv_setpvn(sv, s, len);
2968 if (datumtype == 'A') {
2969 aptr = s; /* borrow register */
2970 s = SvPVX(sv) + len - 1;
2971 while (s >= SvPVX(sv) && (!*s || isSPACE(*s)))
2974 SvCUR_set(sv, s - SvPVX(sv));
2975 s = aptr; /* unborrow register */
2977 XPUSHs(sv_2mortal(sv));
2981 if (pat[-1] == '*' || len > (strend - s) * 8)
2982 len = (strend - s) * 8;
2985 Newz(601, bitcount, 256, char);
2986 for (bits = 1; bits < 256; bits++) {
2987 if (bits & 1) bitcount[bits]++;
2988 if (bits & 2) bitcount[bits]++;
2989 if (bits & 4) bitcount[bits]++;
2990 if (bits & 8) bitcount[bits]++;
2991 if (bits & 16) bitcount[bits]++;
2992 if (bits & 32) bitcount[bits]++;
2993 if (bits & 64) bitcount[bits]++;
2994 if (bits & 128) bitcount[bits]++;
2998 culong += bitcount[*(unsigned char*)s++];
3003 if (datumtype == 'b') {
3005 if (bits & 1) culong++;
3011 if (bits & 128) culong++;
3018 sv = NEWSV(35, len + 1);
3021 aptr = pat; /* borrow register */
3023 if (datumtype == 'b') {
3025 for (len = 0; len < aint; len++) {
3026 if (len & 7) /*SUPPRESS 595*/
3030 *pat++ = '0' + (bits & 1);
3035 for (len = 0; len < aint; len++) {
3040 *pat++ = '0' + ((bits & 128) != 0);
3044 pat = aptr; /* unborrow register */
3045 XPUSHs(sv_2mortal(sv));
3049 if (pat[-1] == '*' || len > (strend - s) * 2)
3050 len = (strend - s) * 2;
3051 sv = NEWSV(35, len + 1);
3054 aptr = pat; /* borrow register */
3056 if (datumtype == 'h') {
3058 for (len = 0; len < aint; len++) {
3063 *pat++ = hexdigit[bits & 15];
3068 for (len = 0; len < aint; len++) {
3073 *pat++ = hexdigit[(bits >> 4) & 15];
3077 pat = aptr; /* unborrow register */
3078 XPUSHs(sv_2mortal(sv));
3081 if (len > strend - s)
3086 if (aint >= 128) /* fake up signed chars */
3096 if (aint >= 128) /* fake up signed chars */
3099 sv_setiv(sv, (IV)aint);
3100 PUSHs(sv_2mortal(sv));
3105 if (len > strend - s)
3120 sv_setiv(sv, (IV)auint);
3121 PUSHs(sv_2mortal(sv));
3126 along = (strend - s) / SIZE16;
3143 sv_setiv(sv, (IV)ashort);
3144 PUSHs(sv_2mortal(sv));
3151 along = (strend - s) / SIZE16;
3156 COPY16(s, &aushort);
3159 if (datumtype == 'n')
3160 aushort = PerlSock_ntohs(aushort);
3163 if (datumtype == 'v')
3164 aushort = vtohs(aushort);
3173 COPY16(s, &aushort);
3177 if (datumtype == 'n')
3178 aushort = PerlSock_ntohs(aushort);
3181 if (datumtype == 'v')
3182 aushort = vtohs(aushort);
3184 sv_setiv(sv, (IV)aushort);
3185 PUSHs(sv_2mortal(sv));
3190 along = (strend - s) / sizeof(int);
3195 Copy(s, &aint, 1, int);
3198 cdouble += (double)aint;
3207 Copy(s, &aint, 1, int);
3210 sv_setiv(sv, (IV)aint);
3211 PUSHs(sv_2mortal(sv));
3216 along = (strend - s) / sizeof(unsigned int);
3221 Copy(s, &auint, 1, unsigned int);
3222 s += sizeof(unsigned int);
3224 cdouble += (double)auint;
3233 Copy(s, &auint, 1, unsigned int);
3234 s += sizeof(unsigned int);
3236 sv_setuv(sv, (UV)auint);
3237 PUSHs(sv_2mortal(sv));
3242 along = (strend - s) / SIZE32;
3250 cdouble += (double)along;
3262 sv_setiv(sv, (IV)along);
3263 PUSHs(sv_2mortal(sv));
3270 along = (strend - s) / SIZE32;
3278 if (datumtype == 'N')
3279 aulong = PerlSock_ntohl(aulong);
3282 if (datumtype == 'V')
3283 aulong = vtohl(aulong);
3286 cdouble += (double)aulong;
3298 if (datumtype == 'N')
3299 aulong = PerlSock_ntohl(aulong);
3302 if (datumtype == 'V')
3303 aulong = vtohl(aulong);
3306 sv_setuv(sv, (UV)aulong);
3307 PUSHs(sv_2mortal(sv));
3312 along = (strend - s) / sizeof(char*);
3318 if (sizeof(char*) > strend - s)
3321 Copy(s, &aptr, 1, char*);
3327 PUSHs(sv_2mortal(sv));
3337 while ((len > 0) && (s < strend)) {
3338 auv = (auv << 7) | (*s & 0x7f);
3339 if (!(*s++ & 0x80)) {
3343 PUSHs(sv_2mortal(sv));
3347 else if (++bytes >= sizeof(UV)) { /* promote to string */
3350 sv = newSVpvf("%.*Vu", (int)TYPE_DIGITS(UV), auv);
3351 while (s < strend) {
3352 sv = mul128(sv, *s & 0x7f);
3353 if (!(*s++ & 0x80)) {
3362 PUSHs(sv_2mortal(sv));
3367 if ((s >= strend) && bytes)
3368 croak("Unterminated compressed integer");
3373 if (sizeof(char*) > strend - s)
3376 Copy(s, &aptr, 1, char*);
3381 sv_setpvn(sv, aptr, len);
3382 PUSHs(sv_2mortal(sv));
3389 if (s + sizeof(Quad_t) > strend)
3392 Copy(s, &aquad, 1, Quad_t);
3393 s += sizeof(Quad_t);
3396 if (aquad >= IV_MIN && aquad <= IV_MAX)
3397 sv_setiv(sv, (IV)aquad);
3399 sv_setnv(sv, (double)aquad);
3400 PUSHs(sv_2mortal(sv));
3407 if (s + sizeof(unsigned Quad_t) > strend)
3410 Copy(s, &auquad, 1, unsigned Quad_t);
3411 s += sizeof(unsigned Quad_t);
3414 if (aquad <= UV_MAX)
3415 sv_setuv(sv, (UV)auquad);
3417 sv_setnv(sv, (double)auquad);
3418 PUSHs(sv_2mortal(sv));
3422 /* float and double added gnb@melba.bby.oz.au 22/11/89 */
3425 along = (strend - s) / sizeof(float);
3430 Copy(s, &afloat, 1, float);
3439 Copy(s, &afloat, 1, float);
3442 sv_setnv(sv, (double)afloat);
3443 PUSHs(sv_2mortal(sv));
3449 along = (strend - s) / sizeof(double);
3454 Copy(s, &adouble, 1, double);
3455 s += sizeof(double);
3463 Copy(s, &adouble, 1, double);
3464 s += sizeof(double);
3466 sv_setnv(sv, (double)adouble);
3467 PUSHs(sv_2mortal(sv));
3472 along = (strend - s) * 3 / 4;
3473 sv = NEWSV(42, along);
3476 while (s < strend && *s > ' ' && *s < 'a') {
3481 len = (*s++ - ' ') & 077;
3483 if (s < strend && *s >= ' ')
3484 a = (*s++ - ' ') & 077;
3487 if (s < strend && *s >= ' ')
3488 b = (*s++ - ' ') & 077;
3491 if (s < strend && *s >= ' ')
3492 c = (*s++ - ' ') & 077;
3495 if (s < strend && *s >= ' ')
3496 d = (*s++ - ' ') & 077;
3499 hunk[0] = (a << 2) | (b >> 4);
3500 hunk[1] = (b << 4) | (c >> 2);
3501 hunk[2] = (c << 6) | d;
3502 sv_catpvn(sv, hunk, (len > 3) ? 3 : len);
3507 else if (s[1] == '\n') /* possible checksum byte */
3510 XPUSHs(sv_2mortal(sv));
3515 if (strchr("fFdD", datumtype) ||
3516 (checksum > 32 && strchr("iIlLN", datumtype)) ) {
3520 while (checksum >= 16) {
3524 while (checksum >= 4) {
3530 along = (1 << checksum) - 1;
3531 while (cdouble < 0.0)
3533 cdouble = modf(cdouble / adouble, &trouble) * adouble;
3534 sv_setnv(sv, cdouble);
3537 if (checksum < 32) {
3538 aulong = (1 << checksum) - 1;
3541 sv_setuv(sv, (UV)culong);
3543 XPUSHs(sv_2mortal(sv));
3547 if (SP == oldsp && gimme == G_SCALAR)
3553 doencodes(register SV *sv, register char *s, register I32 len)
3558 sv_catpvn(sv, hunk, 1);
3561 hunk[0] = ' ' + (077 & (*s >> 2));
3562 hunk[1] = ' ' + (077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017)));
3563 hunk[2] = ' ' + (077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03)));
3564 hunk[3] = ' ' + (077 & (s[2] & 077));
3565 sv_catpvn(sv, hunk, 4);
3569 for (s = SvPVX(sv); *s; s++) {
3573 sv_catpvn(sv, "\n", 1);
3577 is_an_int(char *s, STRLEN l)
3579 SV *result = newSVpv("", l);
3580 char *result_c = SvPV(result, na); /* convenience */
3581 char *out = result_c;
3591 SvREFCNT_dec(result);
3614 SvREFCNT_dec(result);
3620 SvCUR_set(result, out - result_c);
3625 div128(SV *pnum, bool *done)
3626 /* must be '\0' terminated */
3630 char *s = SvPV(pnum, len);
3639 i = m * 10 + (*t - '0');
3641 r = (i >> 7); /* r < 10 */
3648 SvCUR_set(pnum, (STRLEN) (t - s));
3655 djSP; dMARK; dORIGMARK; dTARGET;
3656 register SV *cat = TARG;
3659 register char *pat = SvPVx(*++MARK, fromlen);
3660 register char *patend = pat + fromlen;
3665 static char null10[] = {0,0,0,0,0,0,0,0,0,0};
3666 static char *space10 = " ";
3668 /* These must not be in registers: */
3677 unsigned Quad_t auquad;
3686 sv_setpvn(cat, "", 0);
3687 while (pat < patend) {
3688 #define NEXTFROM (items-- > 0 ? *MARK++ : &sv_no)
3689 datumtype = *pat++ & 0xFF;
3690 if (isSPACE(datumtype))
3693 len = strchr("@Xxu", datumtype) ? 0 : items;
3696 else if (isDIGIT(*pat)) {
3698 while (isDIGIT(*pat))
3699 len = (len * 10) + (*pat++ - '0');
3705 croak("Invalid type in pack: '%c'", (int)datumtype);
3706 case ',': /* grandfather in commas but with a warning */
3707 if (commas++ == 0 && dowarn)
3708 warn("Invalid type in pack: '%c'", (int)datumtype);
3711 DIE("%% may only be used in unpack");
3722 if (SvCUR(cat) < len)
3723 DIE("X outside of string");
3730 sv_catpvn(cat, null10, 10);
3733 sv_catpvn(cat, null10, len);
3738 aptr = SvPV(fromstr, fromlen);
3742 sv_catpvn(cat, aptr, len);
3744 sv_catpvn(cat, aptr, fromlen);
3746 if (datumtype == 'A') {
3748 sv_catpvn(cat, space10, 10);
3751 sv_catpvn(cat, space10, len);
3755 sv_catpvn(cat, null10, 10);
3758 sv_catpvn(cat, null10, len);
3765 char *savepat = pat;
3770 aptr = SvPV(fromstr, fromlen);
3775 SvCUR(cat) += (len+7)/8;
3776 SvGROW(cat, SvCUR(cat) + 1);
3777 aptr = SvPVX(cat) + aint;
3782 if (datumtype == 'B') {
3783 for (len = 0; len++ < aint;) {
3784 items |= *pat++ & 1;
3788 *aptr++ = items & 0xff;
3794 for (len = 0; len++ < aint;) {
3800 *aptr++ = items & 0xff;
3806 if (datumtype == 'B')
3807 items <<= 7 - (aint & 7);
3809 items >>= 7 - (aint & 7);
3810 *aptr++ = items & 0xff;
3812 pat = SvPVX(cat) + SvCUR(cat);
3823 char *savepat = pat;
3828 aptr = SvPV(fromstr, fromlen);
3833 SvCUR(cat) += (len+1)/2;
3834 SvGROW(cat, SvCUR(cat) + 1);
3835 aptr = SvPVX(cat) + aint;
3840 if (datumtype == 'H') {
3841 for (len = 0; len++ < aint;) {
3843 items |= ((*pat++ & 15) + 9) & 15;
3845 items |= *pat++ & 15;
3849 *aptr++ = items & 0xff;
3855 for (len = 0; len++ < aint;) {
3857 items |= (((*pat++ & 15) + 9) & 15) << 4;
3859 items |= (*pat++ & 15) << 4;
3863 *aptr++ = items & 0xff;
3869 *aptr++ = items & 0xff;
3870 pat = SvPVX(cat) + SvCUR(cat);
3882 aint = SvIV(fromstr);
3884 sv_catpvn(cat, &achar, sizeof(char));
3887 /* Float and double added by gnb@melba.bby.oz.au 22/11/89 */
3892 afloat = (float)SvNV(fromstr);
3893 sv_catpvn(cat, (char *)&afloat, sizeof (float));
3900 adouble = (double)SvNV(fromstr);
3901 sv_catpvn(cat, (char *)&adouble, sizeof (double));
3907 ashort = (I16)SvIV(fromstr);
3909 ashort = PerlSock_htons(ashort);
3911 CAT16(cat, &ashort);
3917 ashort = (I16)SvIV(fromstr);
3919 ashort = htovs(ashort);
3921 CAT16(cat, &ashort);
3928 ashort = (I16)SvIV(fromstr);
3929 CAT16(cat, &ashort);
3935 auint = SvUV(fromstr);
3936 sv_catpvn(cat, (char*)&auint, sizeof(unsigned int));
3942 adouble = floor(SvNV(fromstr));
3945 croak("Cannot compress negative numbers");
3951 #ifdef CXUX_BROKEN_CONSTANT_CONVERT
3952 adouble <= UV_MAX_cxux
3959 char buf[1 + sizeof(UV)];
3960 char *in = buf + sizeof(buf);
3961 UV auv = U_V(adouble);;
3964 *--in = (auv & 0x7f) | 0x80;
3967 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
3968 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
3970 else if (SvPOKp(fromstr)) { /* decimal string arithmetics */
3971 char *from, *result, *in;
3976 /* Copy string and check for compliance */
3977 from = SvPV(fromstr, len);
3978 if ((norm = is_an_int(from, len)) == NULL)
3979 croak("can compress only unsigned integer");
3981 New('w', result, len, char);
3985 *--in = div128(norm, &done) | 0x80;
3986 result[len - 1] &= 0x7F; /* clear continue bit */
3987 sv_catpvn(cat, in, (result + len) - in);
3989 SvREFCNT_dec(norm); /* free norm */
3991 else if (SvNOKp(fromstr)) {
3992 char buf[sizeof(double) * 2]; /* 8/7 <= 2 */
3993 char *in = buf + sizeof(buf);
3996 double next = floor(adouble / 128);
3997 *--in = (unsigned char)(adouble - (next * 128)) | 0x80;
3998 if (--in < buf) /* this cannot happen ;-) */
3999 croak ("Cannot compress integer");
4001 } while (adouble > 0);
4002 buf[sizeof(buf) - 1] &= 0x7f; /* clear continue bit */
4003 sv_catpvn(cat, in, (buf + sizeof(buf)) - in);
4006 croak("Cannot compress non integer");
4012 aint = SvIV(fromstr);
4013 sv_catpvn(cat, (char*)&aint, sizeof(int));
4019 aulong = SvUV(fromstr);
4021 aulong = PerlSock_htonl(aulong);
4023 CAT32(cat, &aulong);
4029 aulong = SvUV(fromstr);
4031 aulong = htovl(aulong);
4033 CAT32(cat, &aulong);
4039 aulong = SvUV(fromstr);
4040 CAT32(cat, &aulong);
4046 along = SvIV(fromstr);
4054 auquad = (unsigned Quad_t)SvIV(fromstr);
4055 sv_catpvn(cat, (char*)&auquad, sizeof(unsigned Quad_t));
4061 aquad = (Quad_t)SvIV(fromstr);
4062 sv_catpvn(cat, (char*)&aquad, sizeof(Quad_t));
4065 #endif /* HAS_QUAD */
4067 len = 1; /* assume SV is correct length */
4072 if (fromstr == &sv_undef)
4075 /* XXX better yet, could spirit away the string to
4076 * a safe spot and hang on to it until the result
4077 * of pack() (and all copies of the result) are
4080 if (dowarn && (SvTEMP(fromstr) || SvPADTMP(fromstr)))
4081 warn("Attempt to pack pointer to temporary value");
4082 if (SvPOK(fromstr) || SvNIOK(fromstr))
4083 aptr = SvPV(fromstr,na);
4085 aptr = SvPV_force(fromstr,na);
4087 sv_catpvn(cat, (char*)&aptr, sizeof(char*));
4092 aptr = SvPV(fromstr, fromlen);
4093 SvGROW(cat, fromlen * 4 / 3);
4098 while (fromlen > 0) {
4105 doencodes(cat, aptr, todo);
4124 register I32 limit = POPi; /* note, negative is forever */
4127 register char *s = SvPV(sv, len);
4128 char *strend = s + len;
4130 register REGEXP *rx;
4134 I32 maxiters = (strend - s) + 10;
4137 I32 origlimit = limit;
4140 AV *oldstack = curstack;
4141 I32 gimme = GIMME_V;
4142 I32 oldsave = savestack_ix;
4143 I32 make_mortal = 1;
4144 MAGIC *mg = (MAGIC *) NULL;
4147 Copy(&LvTARGOFF(POPs), &pm, 1, PMOP*);
4152 DIE("panic: do_split");
4153 rx = pm->op_pmregexp;
4155 TAINT_IF((pm->op_pmflags & PMf_LOCALE) &&
4156 (pm->op_pmflags & (PMf_WHITE | PMf_SKIPWHITE)));
4158 if (pm->op_pmreplroot)
4159 ary = GvAVn((GV*)pm->op_pmreplroot);
4160 else if (gimme != G_ARRAY)
4162 ary = (AV*)curpad[0];
4165 #endif /* USE_THREADS */
4168 if (ary && (gimme != G_ARRAY || (pm->op_pmflags & PMf_ONCE))) {
4174 if (SvRMAGICAL(ary) && (mg = mg_find((SV *) ary, 'P'))) {
4181 for (i = AvFILLp(ary); i >= 0; i--)
4182 AvARRAY(ary)[i] = &sv_undef; /* don't free mere refs */
4184 /* temporarily switch stacks */
4185 SWITCHSTACK(curstack, ary);
4189 base = SP - stack_base;
4191 if (pm->op_pmflags & PMf_SKIPWHITE) {
4192 if (pm->op_pmflags & PMf_LOCALE) {
4193 while (isSPACE_LC(*s))
4201 if (pm->op_pmflags & (PMf_MULTILINE|PMf_SINGLELINE)) {
4203 multiline = pm->op_pmflags & PMf_MULTILINE;
4207 limit = maxiters + 2;
4208 if (pm->op_pmflags & PMf_WHITE) {
4211 while (m < strend &&
4212 !((pm->op_pmflags & PMf_LOCALE)
4213 ? isSPACE_LC(*m) : isSPACE(*m)))
4218 dstr = NEWSV(30, m-s);
4219 sv_setpvn(dstr, s, m-s);
4225 while (s < strend &&
4226 ((pm->op_pmflags & PMf_LOCALE)
4227 ? isSPACE_LC(*s) : isSPACE(*s)))
4231 else if (strEQ("^", rx->precomp)) {
4234 for (m = s; m < strend && *m != '\n'; m++) ;
4238 dstr = NEWSV(30, m-s);
4239 sv_setpvn(dstr, s, m-s);
4246 else if (rx->check_substr && !rx->nparens
4247 && (rx->reganch & ROPT_CHECK_ALL)
4248 && !(rx->reganch & ROPT_ANCH)) {
4249 i = SvCUR(rx->check_substr);
4250 if (i == 1 && !SvTAIL(rx->check_substr)) {
4251 i = *SvPVX(rx->check_substr);
4254 for (m = s; m < strend && *m != i; m++) ;
4257 dstr = NEWSV(30, m-s);
4258 sv_setpvn(dstr, s, m-s);
4267 while (s < strend && --limit &&
4268 (m=fbm_instr((unsigned char*)s, (unsigned char*)strend,
4269 rx->check_substr)) )
4272 dstr = NEWSV(31, m-s);
4273 sv_setpvn(dstr, s, m-s);
4282 maxiters += (strend - s) * rx->nparens;
4283 while (s < strend && --limit &&
4284 regexec_flags(rx, s, strend, orig, 1, Nullsv, NULL, 0))
4286 TAINT_IF(RX_MATCH_TAINTED(rx));
4288 && rx->subbase != orig) {
4293 strend = s + (strend - m);
4296 dstr = NEWSV(32, m-s);
4297 sv_setpvn(dstr, s, m-s);
4302 for (i = 1; i <= rx->nparens; i++) {
4306 dstr = NEWSV(33, m-s);
4307 sv_setpvn(dstr, s, m-s);
4310 dstr = NEWSV(33, 0);
4320 LEAVE_SCOPE(oldsave);
4321 iters = (SP - stack_base) - base;
4322 if (iters > maxiters)
4325 /* keep field after final delim? */
4326 if (s < strend || (iters && origlimit)) {
4327 dstr = NEWSV(34, strend-s);
4328 sv_setpvn(dstr, s, strend-s);
4334 else if (!origlimit) {
4335 while (iters > 0 && (!TOPs || !SvANY(TOPs) || SvCUR(TOPs) == 0))
4341 SWITCHSTACK(ary, oldstack);
4342 if (SvSMAGICAL(ary)) {
4347 if (gimme == G_ARRAY) {
4349 Copy(AvARRAY(ary), SP + 1, iters, SV*);
4357 perl_call_method("PUSH",G_SCALAR|G_DISCARD);
4360 if (gimme == G_ARRAY) {
4361 /* EXTEND should not be needed - we just popped them */
4363 for (i=0; i < iters; i++) {
4364 SV **svp = av_fetch(ary, i, FALSE);
4365 PUSHs((svp) ? *svp : &sv_undef);
4372 if (gimme == G_ARRAY)
4375 if (iters || !pm->op_pmreplroot) {
4385 unlock_condpair(void *svv)
4388 MAGIC *mg = mg_find((SV*)svv, 'm');
4391 croak("panic: unlock_condpair unlocking non-mutex");
4392 MUTEX_LOCK(MgMUTEXP(mg));
4393 if (MgOWNER(mg) != thr)
4394 croak("panic: unlock_condpair unlocking mutex that we don't own");
4396 COND_SIGNAL(MgOWNERCONDP(mg));
4397 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: unlock 0x%lx\n",
4398 (unsigned long)thr, (unsigned long)svv);)
4399 MUTEX_UNLOCK(MgMUTEXP(mg));
4401 #endif /* USE_THREADS */
4414 mg = condpair_magic(sv);
4415 MUTEX_LOCK(MgMUTEXP(mg));
4416 if (MgOWNER(mg) == thr)
4417 MUTEX_UNLOCK(MgMUTEXP(mg));
4420 COND_WAIT(MgOWNERCONDP(mg), MgMUTEXP(mg));
4422 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "0x%lx: pp_lock lock 0x%lx\n",
4423 (unsigned long)thr, (unsigned long)sv);)
4424 MUTEX_UNLOCK(MgMUTEXP(mg));
4425 SvREFCNT_inc(sv); /* keep alive until magic_mutexfree */
4426 save_destructor(unlock_condpair, sv);
4428 #endif /* USE_THREADS */
4429 if (SvTYPE(retsv) == SVt_PVAV || SvTYPE(retsv) == SVt_PVHV
4430 || SvTYPE(retsv) == SVt_PVCV) {
4431 retsv = refto(retsv);
4442 if (op->op_private & OPpLVAL_INTRO)
4443 PUSHs(*save_threadsv(op->op_targ));
4445 PUSHs(THREADSV(op->op_targ));
4448 DIE("tried to access per-thread data in non-threaded perl");
4449 #endif /* USE_THREADS */