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 * Now far ahead the Road has gone,
12 * And I must follow, if I can,
13 * Pursuing it with eager feet,
14 * Until it joins some larger way
15 * Where many paths and errands meet.
16 * And whither then? I cannot say.
23 #define WORD_ALIGN sizeof(U16)
26 #define DOCATCH(o) ((CATCH_GET == TRUE) ? docatch(o) : (o))
28 static OP *docatch _((OP *o));
29 static OP *dofindlabel _((OP *o, char *label, OP **opstack, OP **oplimit));
30 static void doparseform _((SV *sv));
31 static I32 dopoptoeval _((I32 startingblock));
32 static I32 dopoptolabel _((char *label));
33 static I32 dopoptoloop _((I32 startingblock));
34 static I32 dopoptosub _((I32 startingblock));
35 static void save_lines _((AV *array, SV *sv));
36 static I32 sortcv _((SV *a, SV *b));
37 static void qsortsv _((SV **array, size_t num_elts, I32 (*fun)(SV *a, SV *b)));
38 static OP *doeval _((int gimme, OP** startop));
48 cxix = dopoptosub(cxstack_ix);
52 switch (cxstack[cxix].blk_gimme) {
69 register PMOP *pm = (PMOP*)cLOGOP->op_other;
73 MAGIC *mg = Null(MAGIC*);
77 SV *sv = SvRV(tmpstr);
79 mg = mg_find(sv, 'r');
82 regexp *re = (regexp *)mg->mg_obj;
83 ReREFCNT_dec(pm->op_pmregexp);
84 pm->op_pmregexp = ReREFCNT_inc(re);
87 t = SvPV(tmpstr, len);
89 /* Check against the last compiled regexp. */
90 if (!pm->op_pmregexp || !pm->op_pmregexp->precomp ||
91 pm->op_pmregexp->prelen != len ||
92 memNE(pm->op_pmregexp->precomp, t, len))
94 if (pm->op_pmregexp) {
95 ReREFCNT_dec(pm->op_pmregexp);
96 pm->op_pmregexp = Null(REGEXP*); /* crucial if regcomp aborts */
99 pm->op_pmflags = pm->op_pmpermflags; /* reset case sensitivity */
100 pm->op_pmregexp = pregcomp(t, t + len, pm);
104 if (!pm->op_pmregexp->prelen && curpm)
106 else if (strEQ("\\s+", pm->op_pmregexp->precomp))
107 pm->op_pmflags |= PMf_WHITE;
109 if (pm->op_pmflags & PMf_KEEP) {
110 pm->op_private &= ~OPpRUNTIME; /* no point compiling again */
111 cLOGOP->op_first->op_next = op->op_next;
119 register PMOP *pm = (PMOP*) cLOGOP->op_other;
120 register PERL_CONTEXT *cx = &cxstack[cxstack_ix];
121 register SV *dstr = cx->sb_dstr;
122 register char *s = cx->sb_s;
123 register char *m = cx->sb_m;
124 char *orig = cx->sb_orig;
125 register REGEXP *rx = cx->sb_rx;
127 rxres_restore(&cx->sb_rxres, rx);
129 if (cx->sb_iters++) {
130 if (cx->sb_iters > cx->sb_maxiters)
131 DIE("Substitution loop");
133 if (!cx->sb_rxtainted)
134 cx->sb_rxtainted = SvTAINTED(TOPs);
135 sv_catsv(dstr, POPs);
138 if (cx->sb_once || !regexec_flags(rx, s, cx->sb_strend, orig,
139 s == m, Nullsv, NULL,
140 cx->sb_safebase ? 0 : REXEC_COPY_STR))
142 SV *targ = cx->sb_targ;
143 sv_catpvn(dstr, s, cx->sb_strend - s);
145 TAINT_IF(cx->sb_rxtainted || RX_MATCH_TAINTED(rx));
147 (void)SvOOK_off(targ);
148 Safefree(SvPVX(targ));
149 SvPVX(targ) = SvPVX(dstr);
150 SvCUR_set(targ, SvCUR(dstr));
151 SvLEN_set(targ, SvLEN(dstr));
154 (void)SvPOK_only(targ);
158 PUSHs(sv_2mortal(newSViv((I32)cx->sb_iters - 1)));
159 LEAVE_SCOPE(cx->sb_oldsave);
161 RETURNOP(pm->op_next);
164 if (rx->subbase && rx->subbase != orig) {
167 cx->sb_orig = orig = rx->subbase;
169 cx->sb_strend = s + (cx->sb_strend - m);
171 cx->sb_m = m = rx->startp[0];
172 sv_catpvn(dstr, s, m-s);
173 cx->sb_s = rx->endp[0];
174 cx->sb_rxtainted |= RX_MATCH_TAINTED(rx);
175 rxres_save(&cx->sb_rxres, rx);
176 RETURNOP(pm->op_pmreplstart);
180 rxres_save(void **rsp, REGEXP *rx)
185 if (!p || p[1] < rx->nparens) {
186 i = 6 + rx->nparens * 2;
194 *p++ = (UV)rx->subbase;
195 rx->subbase = Nullch;
199 *p++ = (UV)rx->subbeg;
200 *p++ = (UV)rx->subend;
201 for (i = 0; i <= rx->nparens; ++i) {
202 *p++ = (UV)rx->startp[i];
203 *p++ = (UV)rx->endp[i];
208 rxres_restore(void **rsp, REGEXP *rx)
213 Safefree(rx->subbase);
214 rx->subbase = (char*)(*p);
219 rx->subbeg = (char*)(*p++);
220 rx->subend = (char*)(*p++);
221 for (i = 0; i <= rx->nparens; ++i) {
222 rx->startp[i] = (char*)(*p++);
223 rx->endp[i] = (char*)(*p++);
228 rxres_free(void **rsp)
233 Safefree((char*)(*p));
241 djSP; dMARK; dORIGMARK;
242 register SV *form = *++MARK;
254 bool chopspace = (strchr(chopset, ' ') != Nullch);
261 if (!SvMAGICAL(form) || !SvCOMPILED(form)) {
262 SvREADONLY_off(form);
266 SvPV_force(formtarget, len);
267 t = SvGROW(formtarget, len + SvCUR(form) + 1); /* XXX SvCUR bad */
270 /* need to jump to the next word */
271 s = f + len + WORD_ALIGN - SvCUR(form) % WORD_ALIGN;
280 case FF_LITERAL: arg = fpc[1]; name = "LITERAL"; break;
281 case FF_BLANK: arg = fpc[1]; name = "BLANK"; break;
282 case FF_SKIP: arg = fpc[1]; name = "SKIP"; break;
283 case FF_FETCH: arg = fpc[1]; name = "FETCH"; break;
284 case FF_DECIMAL: arg = fpc[1]; name = "DECIMAL"; break;
286 case FF_CHECKNL: name = "CHECKNL"; break;
287 case FF_CHECKCHOP: name = "CHECKCHOP"; break;
288 case FF_SPACE: name = "SPACE"; break;
289 case FF_HALFSPACE: name = "HALFSPACE"; break;
290 case FF_ITEM: name = "ITEM"; break;
291 case FF_CHOP: name = "CHOP"; break;
292 case FF_LINEGLOB: name = "LINEGLOB"; break;
293 case FF_NEWLINE: name = "NEWLINE"; break;
294 case FF_MORE: name = "MORE"; break;
295 case FF_LINEMARK: name = "LINEMARK"; break;
296 case FF_END: name = "END"; break;
299 PerlIO_printf(PerlIO_stderr(), "%-16s%ld\n", name, (long) arg);
301 PerlIO_printf(PerlIO_stderr(), "%-16s\n", name);
330 warn("Not enough format arguments");
335 item = s = SvPV(sv, len);
337 if (itemsize > fieldsize)
338 itemsize = fieldsize;
339 send = chophere = s + itemsize;
351 item = s = SvPV(sv, len);
353 if (itemsize <= fieldsize) {
354 send = chophere = s + itemsize;
365 itemsize = fieldsize;
366 send = chophere = s + itemsize;
367 while (s < send || (s == send && isSPACE(*s))) {
377 if (strchr(chopset, *s))
382 itemsize = chophere - item;
387 arg = fieldsize - itemsize;
396 arg = fieldsize - itemsize;
410 int ch = *t++ = *s++;
414 if ( !((*t++ = *s++) & ~31) )
424 while (*s && isSPACE(*s))
431 item = s = SvPV(sv, len);
444 SvCUR_set(formtarget, t - SvPVX(formtarget));
445 sv_catpvn(formtarget, item, itemsize);
446 SvGROW(formtarget, SvCUR(formtarget) + SvCUR(form) + 1);
447 t = SvPVX(formtarget) + SvCUR(formtarget);
452 /* If the field is marked with ^ and the value is undefined,
455 if ((arg & 512) && !SvOK(sv)) {
463 /* Formats aren't yet marked for locales, so assume "yes". */
466 sprintf(t, "%#*.*f", (int) fieldsize, (int) arg & 255, value);
468 sprintf(t, "%*.0f", (int) fieldsize, value);
475 while (t-- > linemark && *t == ' ') ;
483 if (arg) { /* repeat until fields exhausted? */
485 SvCUR_set(formtarget, t - SvPVX(formtarget));
486 lines += FmLINES(formtarget);
489 if (strnEQ(linemark, linemark - arg, arg))
490 DIE("Runaway format");
492 FmLINES(formtarget) = lines;
494 RETURNOP(cLISTOP->op_first);
505 arg = fieldsize - itemsize;
512 if (strnEQ(s," ",3)) {
513 while (s > SvPVX(formtarget) && isSPACE(s[-1]))
524 SvCUR_set(formtarget, t - SvPVX(formtarget));
525 FmLINES(formtarget) += lines;
537 if (stack_base + *markstack_ptr == SP) {
539 if (GIMME_V == G_SCALAR)
541 RETURNOP(op->op_next->op_next);
543 stack_sp = stack_base + *markstack_ptr + 1;
544 pp_pushmark(ARGS); /* push dst */
545 pp_pushmark(ARGS); /* push src */
546 ENTER; /* enter outer scope */
550 /* SAVE_DEFSV does *not* suffice here */
551 save_sptr(&THREADSV(0));
553 SAVESPTR(GvSV(defgv));
554 #endif /* USE_THREADS */
555 ENTER; /* enter inner scope */
558 src = stack_base[*markstack_ptr];
563 if (op->op_type == OP_MAPSTART)
564 pp_pushmark(ARGS); /* push top */
565 return ((LOGOP*)op->op_next)->op_other;
570 DIE("panic: mapstart"); /* uses grepstart */
576 I32 diff = (SP - stack_base) - *markstack_ptr;
584 if (diff > markstack_ptr[-1] - markstack_ptr[-2]) {
585 shift = diff - (markstack_ptr[-1] - markstack_ptr[-2]);
586 count = (SP - stack_base) - markstack_ptr[-1] + 2;
591 markstack_ptr[-1] += shift;
592 *markstack_ptr += shift;
596 dst = stack_base + (markstack_ptr[-2] += diff) - 1;
599 *dst-- = SvTEMP(TOPs) ? POPs : sv_mortalcopy(POPs);
601 LEAVE; /* exit inner scope */
604 if (markstack_ptr[-1] > *markstack_ptr) {
608 (void)POPMARK; /* pop top */
609 LEAVE; /* exit outer scope */
610 (void)POPMARK; /* pop src */
611 items = --*markstack_ptr - markstack_ptr[-1];
612 (void)POPMARK; /* pop dst */
613 SP = stack_base + POPMARK; /* pop original mark */
614 if (gimme == G_SCALAR) {
618 else if (gimme == G_ARRAY)
625 ENTER; /* enter inner scope */
628 src = stack_base[markstack_ptr[-1]];
632 RETURNOP(cLOGOP->op_other);
639 djSP; dMARK; dORIGMARK;
641 SV **myorigmark = ORIGMARK;
647 OP* nextop = op->op_next;
649 if (gimme != G_ARRAY) {
656 if (op->op_flags & OPf_STACKED) {
657 if (op->op_flags & OPf_SPECIAL) {
658 OP *kid = cLISTOP->op_first->op_sibling; /* pass pushmark */
659 kid = kUNOP->op_first; /* pass rv2gv */
660 kid = kUNOP->op_first; /* pass leave */
661 sortcop = kid->op_next;
662 stash = curcop->cop_stash;
665 cv = sv_2cv(*++MARK, &stash, &gv, 0);
666 if (!(cv && CvROOT(cv))) {
668 SV *tmpstr = sv_newmortal();
669 gv_efullname3(tmpstr, gv, Nullch);
670 if (cv && CvXSUB(cv))
671 DIE("Xsub \"%s\" called in sort", SvPVX(tmpstr));
672 DIE("Undefined sort subroutine \"%s\" called",
677 DIE("Xsub called in sort");
678 DIE("Undefined subroutine in sort");
680 DIE("Not a CODE reference in sort");
682 sortcop = CvSTART(cv);
683 SAVESPTR(CvROOT(cv)->op_ppaddr);
684 CvROOT(cv)->op_ppaddr = ppaddr[OP_NULL];
687 curpad = AvARRAY((AV*)AvARRAY(CvPADLIST(cv))[1]);
692 stash = curcop->cop_stash;
696 while (MARK < SP) { /* This may or may not shift down one here. */
698 if (*up = *++MARK) { /* Weed out nulls. */
700 if (!sortcop && !SvPOK(*up))
701 (void)sv_2pv(*up, &na);
705 max = --up - myorigmark;
710 bool oldcatch = CATCH_GET;
717 if (sortstash != stash) {
718 firstgv = gv_fetchpv("a", TRUE, SVt_PV);
719 secondgv = gv_fetchpv("b", TRUE, SVt_PV);
723 SAVESPTR(GvSV(firstgv));
724 SAVESPTR(GvSV(secondgv));
726 PUSHBLOCK(cx, CXt_NULL, stack_base);
727 if (!(op->op_flags & OPf_SPECIAL)) {
728 bool hasargs = FALSE;
729 cx->cx_type = CXt_SUB;
730 cx->blk_gimme = G_SCALAR;
733 (void)SvREFCNT_inc(cv); /* in preparation for POPSUB */
735 sortcxix = cxstack_ix;
737 qsortsv(myorigmark+1, max, sortcv);
746 MEXTEND(SP, 20); /* Can't afford stack realloc on signal. */
747 qsortsv(ORIGMARK+1, max,
748 (op->op_private & OPpLOCALE) ? sv_cmp_locale : sv_cmp);
752 stack_sp = ORIGMARK + max;
760 if (GIMME == G_ARRAY)
761 return cCONDOP->op_true;
762 return SvTRUEx(PAD_SV(op->op_targ)) ? cCONDOP->op_false : cCONDOP->op_true;
769 if (GIMME == G_ARRAY) {
770 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
774 SV *targ = PAD_SV(op->op_targ);
776 if ((op->op_private & OPpFLIP_LINENUM)
777 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
779 sv_setiv(PAD_SV(cUNOP->op_first->op_targ), 1);
780 if (op->op_flags & OPf_SPECIAL) {
788 RETURNOP(((CONDOP*)cUNOP->op_first)->op_false);
801 if (GIMME == G_ARRAY) {
807 if (SvNIOKp(left) || !SvPOKp(left) ||
808 (looks_like_number(left) && *SvPVX(left) != '0') )
813 EXTEND_MORTAL(max - i + 1);
814 EXTEND(SP, max - i + 1);
817 sv = sv_2mortal(newSViv(i++));
822 SV *final = sv_mortalcopy(right);
824 char *tmps = SvPV(final, len);
826 sv = sv_mortalcopy(left);
827 while (!SvNIOKp(sv) && SvCUR(sv) <= len &&
828 strNE(SvPVX(sv),tmps) ) {
830 sv = sv_2mortal(newSVsv(sv));
833 if (strEQ(SvPVX(sv),tmps))
839 SV *targ = PAD_SV(cUNOP->op_first->op_targ);
841 if ((op->op_private & OPpFLIP_LINENUM)
842 ? last_in_gv && SvIV(sv) == IoLINES(GvIOp(last_in_gv))
844 sv_setiv(PAD_SV(((UNOP*)cUNOP->op_first)->op_first->op_targ), 0);
845 sv_catpv(targ, "E0");
856 dopoptolabel(char *label)
860 register PERL_CONTEXT *cx;
862 for (i = cxstack_ix; i >= 0; i--) {
864 switch (cx->cx_type) {
867 warn("Exiting substitution via %s", op_name[op->op_type]);
871 warn("Exiting subroutine via %s", op_name[op->op_type]);
875 warn("Exiting eval via %s", op_name[op->op_type]);
879 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
882 if (!cx->blk_loop.label ||
883 strNE(label, cx->blk_loop.label) ) {
884 DEBUG_l(deb("(Skipping label #%ld %s)\n",
885 (long)i, cx->blk_loop.label));
888 DEBUG_l( deb("(Found label #%ld %s)\n", (long)i, label));
898 I32 gimme = block_gimme();
899 return (gimme == G_VOID) ? G_SCALAR : gimme;
908 cxix = dopoptosub(cxstack_ix);
912 switch (cxstack[cxix].blk_gimme) {
918 croak("panic: bad gimme: %d\n", cxstack[cxix].blk_gimme);
925 dopoptosub(I32 startingblock)
929 register PERL_CONTEXT *cx;
930 for (i = startingblock; i >= 0; i--) {
932 switch (cx->cx_type) {
937 DEBUG_l( deb("(Found sub #%ld)\n", (long)i));
945 dopoptoeval(I32 startingblock)
949 register PERL_CONTEXT *cx;
950 for (i = startingblock; i >= 0; i--) {
952 switch (cx->cx_type) {
956 DEBUG_l( deb("(Found eval #%ld)\n", (long)i));
964 dopoptoloop(I32 startingblock)
968 register PERL_CONTEXT *cx;
969 for (i = startingblock; i >= 0; i--) {
971 switch (cx->cx_type) {
974 warn("Exiting substitution via %s", op_name[op->op_type]);
978 warn("Exiting subroutine via %s", op_name[op->op_type]);
982 warn("Exiting eval via %s", op_name[op->op_type]);
986 warn("Exiting pseudo-block via %s", op_name[op->op_type]);
989 DEBUG_l( deb("(Found loop #%ld)\n", (long)i));
1000 register PERL_CONTEXT *cx;
1004 while (cxstack_ix > cxix) {
1005 cx = &cxstack[cxstack_ix];
1006 DEBUG_l(PerlIO_printf(Perl_debug_log, "Unwinding block %ld, type %s\n",
1007 (long) cxstack_ix, block_type[cx->cx_type]));
1008 /* Note: we don't need to restore the base context info till the end. */
1009 switch (cx->cx_type) {
1012 continue; /* not break */
1030 die_where(char *message)
1035 register PERL_CONTEXT *cx;
1041 STRLEN klen = strlen(message);
1043 svp = hv_fetch(ERRHV, message, klen, TRUE);
1046 static char prefix[] = "\t(in cleanup) ";
1048 sv_upgrade(*svp, SVt_IV);
1049 (void)SvIOK_only(*svp);
1052 SvGROW(err, SvCUR(err)+sizeof(prefix)+klen);
1053 sv_catpvn(err, prefix, sizeof(prefix)-1);
1054 sv_catpvn(err, message, klen);
1060 sv_setpv(ERRSV, message);
1062 while ((cxix = dopoptoeval(cxstack_ix)) < 0 && curstackinfo->si_prev) {
1070 if (cxix < cxstack_ix)
1074 if (cx->cx_type != CXt_EVAL) {
1075 PerlIO_printf(PerlIO_stderr(), "panic: die %s", message);
1080 if (gimme == G_SCALAR)
1081 *++newsp = &sv_undef;
1086 if (optype == OP_REQUIRE) {
1087 char* msg = SvPVx(ERRSV, na);
1088 DIE("%s", *msg ? msg : "Compilation failed in require");
1090 return pop_return();
1093 PerlIO_printf(PerlIO_stderr(), "%s",message);
1094 PerlIO_flush(PerlIO_stderr());
1103 if (SvTRUE(left) != SvTRUE(right))
1115 RETURNOP(cLOGOP->op_other);
1124 RETURNOP(cLOGOP->op_other);
1130 register I32 cxix = dopoptosub(cxstack_ix);
1131 register PERL_CONTEXT *cx;
1143 if (GIMME != G_ARRAY)
1147 if (DBsub && cxix >= 0 &&
1148 cxstack[cxix].blk_sub.cv == GvCV(DBsub))
1152 cxix = dopoptosub(cxix - 1);
1154 cx = &cxstack[cxix];
1155 if (cxstack[cxix].cx_type == CXt_SUB) {
1156 dbcxix = dopoptosub(cxix - 1);
1157 /* We expect that cxstack[dbcxix] is CXt_SUB, anyway, the
1158 field below is defined for any cx. */
1159 if (DBsub && dbcxix >= 0 && cxstack[dbcxix].blk_sub.cv == GvCV(DBsub))
1160 cx = &cxstack[dbcxix];
1163 if (GIMME != G_ARRAY) {
1164 hv = cx->blk_oldcop->cop_stash;
1169 sv_setpv(TARG, HvNAME(hv));
1175 hv = cx->blk_oldcop->cop_stash;
1179 PUSHs(sv_2mortal(newSVpv(HvNAME(hv), 0)));
1180 PUSHs(sv_2mortal(newSVpv(SvPVX(GvSV(cx->blk_oldcop->cop_filegv)), 0)));
1181 PUSHs(sv_2mortal(newSViv((I32)cx->blk_oldcop->cop_line)));
1184 if (cx->cx_type == CXt_SUB) { /* So is cxstack[dbcxix]. */
1186 gv_efullname3(sv, CvGV(cxstack[cxix].blk_sub.cv), Nullch);
1187 PUSHs(sv_2mortal(sv));
1188 PUSHs(sv_2mortal(newSViv((I32)cx->blk_sub.hasargs)));
1191 PUSHs(sv_2mortal(newSVpv("(eval)",0)));
1192 PUSHs(sv_2mortal(newSViv(0)));
1194 gimme = (I32)cx->blk_gimme;
1195 if (gimme == G_VOID)
1198 PUSHs(sv_2mortal(newSViv(gimme & G_ARRAY)));
1199 if (cx->cx_type == CXt_EVAL) {
1200 if (cx->blk_eval.old_op_type == OP_ENTEREVAL) {
1201 PUSHs(cx->blk_eval.cur_text);
1204 else if (cx->blk_eval.old_name) { /* Try blocks have old_name == 0. */
1205 /* Require, put the name. */
1206 PUSHs(sv_2mortal(newSVpv(cx->blk_eval.old_name, 0)));
1210 else if (cx->cx_type == CXt_SUB &&
1211 cx->blk_sub.hasargs &&
1212 curcop->cop_stash == debstash)
1214 AV *ary = cx->blk_sub.argarray;
1215 int off = AvARRAY(ary) - AvALLOC(ary);
1219 dbargs = GvAV(gv_AVadd(tmpgv = gv_fetchpv("DB::args", TRUE,
1222 AvREAL_off(dbargs); /* XXX Should be REIFY */
1225 if (AvMAX(dbargs) < AvFILLp(ary) + off)
1226 av_extend(dbargs, AvFILLp(ary) + off);
1227 Copy(AvALLOC(ary), AvARRAY(dbargs), AvFILLp(ary) + 1 + off, SV*);
1228 AvFILLp(dbargs) = AvFILLp(ary) + off;
1234 sortcv(SV *a, SV *b)
1237 I32 oldsaveix = savestack_ix;
1238 I32 oldscopeix = scopestack_ix;
1242 stack_sp = stack_base;
1245 if (stack_sp != stack_base + 1)
1246 croak("Sort subroutine didn't return single value");
1247 if (!SvNIOKp(*stack_sp))
1248 croak("Sort subroutine didn't return a numeric value");
1249 result = SvIV(*stack_sp);
1250 while (scopestack_ix > oldscopeix) {
1253 leave_scope(oldsaveix);
1266 sv_reset(tmps, curcop->cop_stash);
1279 TAINT_NOT; /* Each statement is presumed innocent */
1280 stack_sp = stack_base + cxstack[cxstack_ix].blk_oldsp;
1283 if (op->op_private || SvIV(DBsingle) || SvIV(DBsignal) || SvIV(DBtrace))
1287 register PERL_CONTEXT *cx;
1288 I32 gimme = G_ARRAY;
1295 DIE("No DB::DB routine defined");
1297 if (CvDEPTH(cv) >= 1 && !(debug & (1<<30))) /* don't do recursive DB::DB call */
1309 push_return(op->op_next);
1310 PUSHBLOCK(cx, CXt_SUB, SP);
1313 (void)SvREFCNT_inc(cv);
1315 curpad = AvARRAY((AV*)*av_fetch(CvPADLIST(cv),1,FALSE));
1316 RETURNOP(CvSTART(cv));
1330 register PERL_CONTEXT *cx;
1331 I32 gimme = GIMME_V;
1338 if (op->op_flags & OPf_SPECIAL)
1339 svp = save_threadsv(op->op_targ); /* per-thread variable */
1341 #endif /* USE_THREADS */
1343 svp = &curpad[op->op_targ]; /* "my" variable */
1348 (void)save_scalar(gv);
1349 svp = &GvSV(gv); /* symbol table variable */
1354 PUSHBLOCK(cx, CXt_LOOP, SP);
1355 PUSHLOOP(cx, svp, MARK);
1356 if (op->op_flags & OPf_STACKED)
1357 cx->blk_loop.iterary = (AV*)SvREFCNT_inc(POPs);
1359 cx->blk_loop.iterary = curstack;
1360 AvFILLp(curstack) = SP - stack_base;
1361 cx->blk_loop.iterix = MARK - stack_base;
1370 register PERL_CONTEXT *cx;
1371 I32 gimme = GIMME_V;
1377 PUSHBLOCK(cx, CXt_LOOP, SP);
1378 PUSHLOOP(cx, 0, SP);
1386 register PERL_CONTEXT *cx;
1387 struct block_loop cxloop;
1395 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1398 if (gimme == G_VOID)
1400 else if (gimme == G_SCALAR) {
1402 *++newsp = sv_mortalcopy(*SP);
1404 *++newsp = &sv_undef;
1408 *++newsp = sv_mortalcopy(*++mark);
1409 TAINT_NOT; /* Each item is independent */
1415 POPLOOP2(); /* Stack values are safe: release loop vars ... */
1416 curpm = newpm; /* ... and pop $1 et al */
1428 register PERL_CONTEXT *cx;
1429 struct block_sub cxsub;
1430 bool popsub2 = FALSE;
1436 if (curstackinfo->si_type == SI_SORT) {
1437 if (cxstack_ix == sortcxix || dopoptosub(cxstack_ix) <= sortcxix) {
1438 if (cxstack_ix > sortcxix)
1440 AvARRAY(curstack)[1] = *SP;
1441 stack_sp = stack_base + 1;
1446 cxix = dopoptosub(cxstack_ix);
1448 DIE("Can't return outside a subroutine");
1449 if (cxix < cxstack_ix)
1453 switch (cx->cx_type) {
1455 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1460 if (optype == OP_REQUIRE &&
1461 (MARK == SP || (gimme == G_SCALAR && !SvTRUE(*SP))) )
1463 /* Unassume the success we assumed earlier. */
1464 char *name = cx->blk_eval.old_name;
1465 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
1466 DIE("%s did not return a true value", name);
1470 DIE("panic: return");
1474 if (gimme == G_SCALAR) {
1476 *++newsp = (popsub2 && SvTEMP(*SP))
1477 ? *SP : sv_mortalcopy(*SP);
1479 *++newsp = &sv_undef;
1481 else if (gimme == G_ARRAY) {
1482 while (++MARK <= SP) {
1483 *++newsp = (popsub2 && SvTEMP(*MARK))
1484 ? *MARK : sv_mortalcopy(*MARK);
1485 TAINT_NOT; /* Each item is independent */
1490 /* Stack values are safe: */
1492 POPSUB2(); /* release CV and @_ ... */
1494 curpm = newpm; /* ... and pop $1 et al */
1497 return pop_return();
1504 register PERL_CONTEXT *cx;
1505 struct block_loop cxloop;
1506 struct block_sub cxsub;
1513 SV **mark = stack_base + cxstack[cxstack_ix].blk_oldsp;
1515 if (op->op_flags & OPf_SPECIAL) {
1516 cxix = dopoptoloop(cxstack_ix);
1518 DIE("Can't \"last\" outside a block");
1521 cxix = dopoptolabel(cPVOP->op_pv);
1523 DIE("Label not found for \"last %s\"", cPVOP->op_pv);
1525 if (cxix < cxstack_ix)
1529 switch (cx->cx_type) {
1531 POPLOOP1(cx); /* Delay POPLOOP2 until stack values are safe */
1533 nextop = cxloop.last_op->op_next;
1536 POPSUB1(cx); /* Delay POPSUB2 until stack values are safe */
1538 nextop = pop_return();
1542 nextop = pop_return();
1549 if (gimme == G_SCALAR) {
1551 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*SP))
1552 ? *SP : sv_mortalcopy(*SP);
1554 *++newsp = &sv_undef;
1556 else if (gimme == G_ARRAY) {
1557 while (++MARK <= SP) {
1558 *++newsp = ((pop2 == CXt_SUB) && SvTEMP(*MARK))
1559 ? *MARK : sv_mortalcopy(*MARK);
1560 TAINT_NOT; /* Each item is independent */
1566 /* Stack values are safe: */
1569 POPLOOP2(); /* release loop vars ... */
1573 POPSUB2(); /* release CV and @_ ... */
1576 curpm = newpm; /* ... and pop $1 et al */
1585 register PERL_CONTEXT *cx;
1588 if (op->op_flags & OPf_SPECIAL) {
1589 cxix = dopoptoloop(cxstack_ix);
1591 DIE("Can't \"next\" outside a block");
1594 cxix = dopoptolabel(cPVOP->op_pv);
1596 DIE("Label not found for \"next %s\"", cPVOP->op_pv);
1598 if (cxix < cxstack_ix)
1602 oldsave = scopestack[scopestack_ix - 1];
1603 LEAVE_SCOPE(oldsave);
1604 return cx->blk_loop.next_op;
1610 register PERL_CONTEXT *cx;
1613 if (op->op_flags & OPf_SPECIAL) {
1614 cxix = dopoptoloop(cxstack_ix);
1616 DIE("Can't \"redo\" outside a block");
1619 cxix = dopoptolabel(cPVOP->op_pv);
1621 DIE("Label not found for \"redo %s\"", cPVOP->op_pv);
1623 if (cxix < cxstack_ix)
1627 oldsave = scopestack[scopestack_ix - 1];
1628 LEAVE_SCOPE(oldsave);
1629 return cx->blk_loop.redo_op;
1632 static OP* lastgotoprobe;
1635 dofindlabel(OP *o, char *label, OP **opstack, OP **oplimit)
1639 static char too_deep[] = "Target of goto is too deeply nested";
1643 if (o->op_type == OP_LEAVE ||
1644 o->op_type == OP_SCOPE ||
1645 o->op_type == OP_LEAVELOOP ||
1646 o->op_type == OP_LEAVETRY)
1648 *ops++ = cUNOPo->op_first;
1653 if (o->op_flags & OPf_KIDS) {
1654 /* First try all the kids at this level, since that's likeliest. */
1655 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
1656 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1657 kCOP->cop_label && strEQ(kCOP->cop_label, label))
1660 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
1661 if (kid == lastgotoprobe)
1663 if ((kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE) &&
1665 (ops[-1]->op_type != OP_NEXTSTATE &&
1666 ops[-1]->op_type != OP_DBSTATE)))
1668 if (o = dofindlabel(kid, label, ops, oplimit))
1678 return pp_goto(ARGS);
1687 register PERL_CONTEXT *cx;
1688 #define GOTO_DEPTH 64
1689 OP *enterops[GOTO_DEPTH];
1691 int do_dump = (op->op_type == OP_DUMP);
1694 if (op->op_flags & OPf_STACKED) {
1697 /* This egregious kludge implements goto &subroutine */
1698 if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV) {
1700 register PERL_CONTEXT *cx;
1701 CV* cv = (CV*)SvRV(sv);
1706 if (!CvROOT(cv) && !CvXSUB(cv)) {
1708 SV *tmpstr = sv_newmortal();
1709 gv_efullname3(tmpstr, CvGV(cv), Nullch);
1710 DIE("Goto undefined subroutine &%s",SvPVX(tmpstr));
1712 DIE("Goto undefined subroutine");
1715 /* First do some returnish stuff. */
1716 cxix = dopoptosub(cxstack_ix);
1718 DIE("Can't goto subroutine outside a subroutine");
1719 if (cxix < cxstack_ix)
1722 if (cx->cx_type == CXt_EVAL && cx->blk_eval.old_op_type == OP_ENTEREVAL)
1723 DIE("Can't goto subroutine from an eval-string");
1725 if (cx->cx_type == CXt_SUB &&
1726 cx->blk_sub.hasargs) { /* put @_ back onto stack */
1727 AV* av = cx->blk_sub.argarray;
1729 items = AvFILLp(av) + 1;
1731 EXTEND(stack_sp, items); /* @_ could have been extended. */
1732 Copy(AvARRAY(av), stack_sp, items, SV*);
1735 SvREFCNT_dec(GvAV(defgv));
1736 GvAV(defgv) = cx->blk_sub.savearray;
1737 #endif /* USE_THREADS */
1741 if (cx->cx_type == CXt_SUB &&
1742 !(CvDEPTH(cx->blk_sub.cv) = cx->blk_sub.olddepth))
1743 SvREFCNT_dec(cx->blk_sub.cv);
1744 oldsave = scopestack[scopestack_ix - 1];
1745 LEAVE_SCOPE(oldsave);
1747 /* Now do some callish stuff. */
1750 if (CvOLDSTYLE(cv)) {
1751 I32 (*fp3)_((int,int,int));
1756 fp3 = (I32(*)_((int,int,int)))CvXSUB(cv);
1757 items = (*fp3)(CvXSUBANY(cv).any_i32,
1758 mark - stack_base + 1,
1760 SP = stack_base + items;
1763 stack_sp--; /* There is no cv arg. */
1764 (void)(*CvXSUB(cv))(cv);
1767 return pop_return();
1770 AV* padlist = CvPADLIST(cv);
1771 SV** svp = AvARRAY(padlist);
1772 if (cx->cx_type == CXt_EVAL) {
1773 in_eval = cx->blk_eval.old_in_eval;
1774 eval_root = cx->blk_eval.old_eval_root;
1775 cx->cx_type = CXt_SUB;
1776 cx->blk_sub.hasargs = 0;
1778 cx->blk_sub.cv = cv;
1779 cx->blk_sub.olddepth = CvDEPTH(cv);
1781 if (CvDEPTH(cv) < 2)
1782 (void)SvREFCNT_inc(cv);
1783 else { /* save temporaries on recursion? */
1784 if (CvDEPTH(cv) == 100 && dowarn)
1785 sub_crush_depth(cv);
1786 if (CvDEPTH(cv) > AvFILLp(padlist)) {
1787 AV *newpad = newAV();
1788 SV **oldpad = AvARRAY(svp[CvDEPTH(cv)-1]);
1789 I32 ix = AvFILLp((AV*)svp[1]);
1790 svp = AvARRAY(svp[0]);
1791 for ( ;ix > 0; ix--) {
1792 if (svp[ix] != &sv_undef) {
1793 char *name = SvPVX(svp[ix]);
1794 if ((SvFLAGS(svp[ix]) & SVf_FAKE)
1797 /* outer lexical or anon code */
1798 av_store(newpad, ix,
1799 SvREFCNT_inc(oldpad[ix]) );
1801 else { /* our own lexical */
1803 av_store(newpad, ix, sv = (SV*)newAV());
1804 else if (*name == '%')
1805 av_store(newpad, ix, sv = (SV*)newHV());
1807 av_store(newpad, ix, sv = NEWSV(0,0));
1812 av_store(newpad, ix, sv = NEWSV(0,0));
1816 if (cx->blk_sub.hasargs) {
1819 av_store(newpad, 0, (SV*)av);
1820 AvFLAGS(av) = AVf_REIFY;
1822 av_store(padlist, CvDEPTH(cv), (SV*)newpad);
1823 AvFILLp(padlist) = CvDEPTH(cv);
1824 svp = AvARRAY(padlist);
1828 if (!cx->blk_sub.hasargs) {
1829 AV* av = (AV*)curpad[0];
1831 items = AvFILLp(av) + 1;
1833 /* Mark is at the end of the stack. */
1835 Copy(AvARRAY(av), SP + 1, items, SV*);
1840 #endif /* USE_THREADS */
1842 curpad = AvARRAY((AV*)svp[CvDEPTH(cv)]);
1844 if (cx->blk_sub.hasargs)
1845 #endif /* USE_THREADS */
1847 AV* av = (AV*)curpad[0];
1851 cx->blk_sub.savearray = GvAV(defgv);
1852 GvAV(defgv) = (AV*)SvREFCNT_inc(av);
1853 #endif /* USE_THREADS */
1854 cx->blk_sub.argarray = av;
1857 if (items >= AvMAX(av) + 1) {
1859 if (AvARRAY(av) != ary) {
1860 AvMAX(av) += AvARRAY(av) - AvALLOC(av);
1861 SvPVX(av) = (char*)ary;
1863 if (items >= AvMAX(av) + 1) {
1864 AvMAX(av) = items - 1;
1865 Renew(ary,items+1,SV*);
1867 SvPVX(av) = (char*)ary;
1870 Copy(mark,AvARRAY(av),items,SV*);
1871 AvFILLp(av) = items - 1;
1879 if (PERLDB_SUB) { /* Checking curstash breaks DProf. */
1881 * We do not care about using sv to call CV;
1882 * it's for informational purposes only.
1884 SV *sv = GvSV(DBsub);
1887 if (PERLDB_SUB_NN) {
1888 SvIVX(sv) = (IV)cv; /* Already upgraded, saved */
1891 gv_efullname3(sv, CvGV(cv), Nullch);
1894 && (gotocv = perl_get_cv("DB::goto", FALSE)) ) {
1895 PUSHMARK( stack_sp );
1896 perl_call_sv((SV*)gotocv, G_SCALAR | G_NODEBUG);
1900 RETURNOP(CvSTART(cv));
1904 label = SvPV(sv,na);
1906 else if (op->op_flags & OPf_SPECIAL) {
1908 DIE("goto must have label");
1911 label = cPVOP->op_pv;
1913 if (label && *label) {
1920 for (ix = cxstack_ix; ix >= 0; ix--) {
1922 switch (cx->cx_type) {
1924 gotoprobe = eval_root; /* XXX not good for nested eval */
1927 gotoprobe = cx->blk_oldcop->op_sibling;
1933 gotoprobe = cx->blk_oldcop->op_sibling;
1935 gotoprobe = main_root;
1938 if (CvDEPTH(cx->blk_sub.cv)) {
1939 gotoprobe = CvROOT(cx->blk_sub.cv);
1944 DIE("Can't \"goto\" outside a block");
1948 gotoprobe = main_root;
1951 retop = dofindlabel(gotoprobe, label,
1952 enterops, enterops + GOTO_DEPTH);
1955 lastgotoprobe = gotoprobe;
1958 DIE("Can't find label %s", label);
1960 /* pop unwanted frames */
1962 if (ix < cxstack_ix) {
1969 oldsave = scopestack[scopestack_ix];
1970 LEAVE_SCOPE(oldsave);
1973 /* push wanted frames */
1975 if (*enterops && enterops[1]) {
1977 for (ix = 1; enterops[ix]; ix++) {
1979 /* Eventually we may want to stack the needed arguments
1980 * for each op. For now, we punt on the hard ones. */
1981 if (op->op_type == OP_ENTERITER)
1982 DIE("Can't \"goto\" into the middle of a foreach loop",
1984 (*op->op_ppaddr)(ARGS);
1992 if (!retop) retop = main_start;
1999 restartop = 0; /* hmm, must be GNU unexec().. */
2003 if (top_env->je_prev) {
2021 if (anum == 1 && VMSISH_EXIT)
2034 double value = SvNVx(GvSV(cCOP->cop_gv));
2035 register I32 match = I_32(value);
2038 if (((double)match) > value)
2039 --match; /* was fractional--truncate other way */
2041 match -= cCOP->uop.scop.scop_offset;
2044 else if (match > cCOP->uop.scop.scop_max)
2045 match = cCOP->uop.scop.scop_max;
2046 op = cCOP->uop.scop.scop_next[match];
2056 op = op->op_next; /* can't assume anything */
2058 match = *(SvPVx(GvSV(cCOP->cop_gv), na)) & 255;
2059 match -= cCOP->uop.scop.scop_offset;
2062 else if (match > cCOP->uop.scop.scop_max)
2063 match = cCOP->uop.scop.scop_max;
2064 op = cCOP->uop.scop.scop_next[match];
2073 save_lines(AV *array, SV *sv)
2075 register char *s = SvPVX(sv);
2076 register char *send = SvPVX(sv) + SvCUR(sv);
2078 register I32 line = 1;
2080 while (s && s < send) {
2081 SV *tmpstr = NEWSV(85,0);
2083 sv_upgrade(tmpstr, SVt_PVMG);
2084 t = strchr(s, '\n');
2090 sv_setpvn(tmpstr, s, t - s);
2091 av_store(array, line++, tmpstr);
2106 assert(CATCH_GET == TRUE);
2107 DEBUG_l(deb("Setting up local jumplevel %p, was %p\n", &cur_env, top_env));
2111 default: /* topmost level handles it */
2118 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
2134 sv_compile_2op(SV *sv, OP** startop, char *code, AV** avp)
2135 /* sv Text to convert to OP tree. */
2136 /* startop op_free() this to undo. */
2137 /* code Short string id of the caller. */
2139 dSP; /* Make POPBLOCK work. */
2142 I32 gimme = 0; /* SUSPECT - INITIALZE TO WHAT? NI-S */
2146 char tmpbuf[TYPE_DIGITS(long) + 12 + 10];
2152 /* switch to eval mode */
2154 SAVESPTR(compiling.cop_filegv);
2155 SAVEI16(compiling.cop_line);
2156 sprintf(tmpbuf, "_<(%.10s_eval %lu)", code, (unsigned long)++evalseq);
2157 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2158 compiling.cop_line = 1;
2159 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2160 deleting the eval's FILEGV from the stash before gv_check() runs
2161 (i.e. before run-time proper). To work around the coredump that
2162 ensues, we always turn GvMULTI_on for any globals that were
2163 introduced within evals. See force_ident(). GSAR 96-10-12 */
2164 safestr = savepv(tmpbuf);
2165 SAVEDELETE(defstash, safestr, strlen(safestr));
2167 #ifdef OP_IN_REGISTER
2175 op->op_type = 0; /* Avoid uninit warning. */
2176 op->op_flags = 0; /* Avoid uninit warning. */
2177 PUSHBLOCK(cx, CXt_EVAL, SP);
2178 PUSHEVAL(cx, 0, compiling.cop_filegv);
2179 rop = doeval(G_SCALAR, startop);
2183 (*startop)->op_type = OP_NULL;
2184 (*startop)->op_ppaddr = ppaddr[OP_NULL];
2186 *avp = (AV*)SvREFCNT_inc(comppad);
2188 #ifdef OP_IN_REGISTER
2194 /* With USE_THREADS, eval_owner must be held on entry to doeval */
2196 doeval(int gimme, OP** startop)
2209 /* set up a scratch pad */
2214 SAVESPTR(comppad_name);
2215 SAVEI32(comppad_name_fill);
2216 SAVEI32(min_intro_pending);
2217 SAVEI32(max_intro_pending);
2220 for (i = cxstack_ix - 1; i >= 0; i--) {
2221 PERL_CONTEXT *cx = &cxstack[i];
2222 if (cx->cx_type == CXt_EVAL)
2224 else if (cx->cx_type == CXt_SUB) {
2225 caller = cx->blk_sub.cv;
2231 compcv = (CV*)NEWSV(1104,0);
2232 sv_upgrade((SV *)compcv, SVt_PVCV);
2233 CvUNIQUE_on(compcv);
2235 CvOWNER(compcv) = 0;
2236 New(666, CvMUTEXP(compcv), 1, perl_mutex);
2237 MUTEX_INIT(CvMUTEXP(compcv));
2238 #endif /* USE_THREADS */
2241 av_push(comppad, Nullsv);
2242 curpad = AvARRAY(comppad);
2243 comppad_name = newAV();
2244 comppad_name_fill = 0;
2245 min_intro_pending = 0;
2248 av_store(comppad_name, 0, newSVpv("@_", 2));
2249 curpad[0] = (SV*)newAV();
2250 SvPADMY_on(curpad[0]); /* XXX Needed? */
2251 #endif /* USE_THREADS */
2253 comppadlist = newAV();
2254 AvREAL_off(comppadlist);
2255 av_store(comppadlist, 0, (SV*)comppad_name);
2256 av_store(comppadlist, 1, (SV*)comppad);
2257 CvPADLIST(compcv) = comppadlist;
2259 if (!saveop || saveop->op_type != OP_REQUIRE)
2260 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc(caller);
2264 /* make sure we compile in the right package */
2266 newstash = curcop->cop_stash;
2267 if (curstash != newstash) {
2269 curstash = newstash;
2273 SAVEFREESV(beginav);
2275 /* try to compile it */
2279 curcop = &compiling;
2280 curcop->cop_arybase = 0;
2282 rs = newSVpv("\n", 1);
2283 if (saveop && saveop->op_flags & OPf_SPECIAL)
2287 if (yyparse() || error_count || !eval_root) {
2291 I32 optype = 0; /* Might be reset by POPEVAL. */
2298 SP = stack_base + POPMARK; /* pop original mark */
2306 if (optype == OP_REQUIRE) {
2307 char* msg = SvPVx(ERRSV, na);
2308 DIE("%s", *msg ? msg : "Compilation failed in require");
2309 } else if (startop) {
2310 char* msg = SvPVx(ERRSV, na);
2314 croak("%sCompilation failed in regexp", (*msg ? msg : "Unknown error\n"));
2317 rs = SvREFCNT_inc(nrs);
2319 MUTEX_LOCK(&eval_mutex);
2321 COND_SIGNAL(&eval_cond);
2322 MUTEX_UNLOCK(&eval_mutex);
2323 #endif /* USE_THREADS */
2327 rs = SvREFCNT_inc(nrs);
2328 compiling.cop_line = 0;
2330 *startop = eval_root;
2331 SvREFCNT_dec(CvOUTSIDE(compcv));
2332 CvOUTSIDE(compcv) = Nullcv;
2334 SAVEFREEOP(eval_root);
2336 scalarvoid(eval_root);
2337 else if (gimme & G_ARRAY)
2342 DEBUG_x(dump_eval());
2344 /* Register with debugger: */
2345 if (PERLDB_INTER && saveop->op_type == OP_REQUIRE) {
2346 CV *cv = perl_get_cv("DB::postponed", FALSE);
2350 XPUSHs((SV*)compiling.cop_filegv);
2352 perl_call_sv((SV*)cv, G_DISCARD);
2356 /* compiled okay, so do it */
2358 CvDEPTH(compcv) = 1;
2359 SP = stack_base + POPMARK; /* pop original mark */
2360 op = saveop; /* The caller may need it. */
2362 MUTEX_LOCK(&eval_mutex);
2364 COND_SIGNAL(&eval_cond);
2365 MUTEX_UNLOCK(&eval_mutex);
2366 #endif /* USE_THREADS */
2368 RETURNOP(eval_start);
2374 register PERL_CONTEXT *cx;
2379 SV *namesv = Nullsv;
2381 I32 gimme = G_SCALAR;
2382 PerlIO *tryrsfp = 0;
2385 if (SvNIOKp(sv) && !SvPOKp(sv)) {
2386 SET_NUMERIC_STANDARD();
2387 if (atof(patchlevel) + 0.00000999 < SvNV(sv))
2388 DIE("Perl %s required--this is only version %s, stopped",
2389 SvPV(sv,na),patchlevel);
2392 name = SvPV(sv, len);
2393 if (!(name && len > 0 && *name))
2394 DIE("Null filename used");
2395 TAINT_PROPER("require");
2396 if (op->op_type == OP_REQUIRE &&
2397 (svp = hv_fetch(GvHVn(incgv), name, len, 0)) &&
2401 /* prepare to compile file */
2406 (name[1] == '.' && name[2] == '/')))
2408 || (name[0] && name[1] == ':')
2411 || (name[0] == '\\' && name[1] == '\\') /* UNC path */
2414 || (strchr(name,':') || ((*name == '[' || *name == '<') &&
2415 (isALNUM(name[1]) || strchr("$-_]>",name[1]))))
2420 tryrsfp = PerlIO_open(name,PERL_SCRIPT_MODE);
2423 AV *ar = GvAVn(incgv);
2427 if ((unixname = tounixspec(name, Nullch)) != Nullch)
2430 namesv = NEWSV(806, 0);
2431 for (i = 0; i <= AvFILL(ar); i++) {
2432 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2435 if ((unixdir = tounixpath(dir, Nullch)) == Nullch)
2437 sv_setpv(namesv, unixdir);
2438 sv_catpv(namesv, unixname);
2440 sv_setpvf(namesv, "%s/%s", dir, name);
2442 tryname = SvPVX(namesv);
2443 tryrsfp = PerlIO_open(tryname, PERL_SCRIPT_MODE);
2445 if (tryname[0] == '.' && tryname[1] == '/')
2452 SAVESPTR(compiling.cop_filegv);
2453 compiling.cop_filegv = gv_fetchfile(tryrsfp ? tryname : name);
2454 SvREFCNT_dec(namesv);
2456 if (op->op_type == OP_REQUIRE) {
2457 SV *msg = sv_2mortal(newSVpvf("Can't locate %s in @INC", name));
2458 SV *dirmsgsv = NEWSV(0, 0);
2459 AV *ar = GvAVn(incgv);
2461 if (instr(SvPVX(msg), ".h "))
2462 sv_catpv(msg, " (change .h to .ph maybe?)");
2463 if (instr(SvPVX(msg), ".ph "))
2464 sv_catpv(msg, " (did you run h2ph?)");
2465 sv_catpv(msg, " (@INC contains:");
2466 for (i = 0; i <= AvFILL(ar); i++) {
2467 char *dir = SvPVx(*av_fetch(ar, i, TRUE), na);
2468 sv_setpvf(dirmsgsv, " %s", dir);
2469 sv_catsv(msg, dirmsgsv);
2471 sv_catpvn(msg, ")", 1);
2472 SvREFCNT_dec(dirmsgsv);
2479 /* Assume success here to prevent recursive requirement. */
2480 (void)hv_store(GvHVn(incgv), name, strlen(name),
2481 newSVsv(GvSV(compiling.cop_filegv)), 0 );
2485 lex_start(sv_2mortal(newSVpv("",0)));
2487 save_aptr(&rsfp_filters);
2488 rsfp_filters = NULL;
2492 name = savepv(name);
2497 /* switch to eval mode */
2499 push_return(op->op_next);
2500 PUSHBLOCK(cx, CXt_EVAL, SP);
2501 PUSHEVAL(cx, name, compiling.cop_filegv);
2503 compiling.cop_line = 0;
2507 MUTEX_LOCK(&eval_mutex);
2508 if (eval_owner && eval_owner != thr)
2510 COND_WAIT(&eval_cond, &eval_mutex);
2512 MUTEX_UNLOCK(&eval_mutex);
2513 #endif /* USE_THREADS */
2514 return DOCATCH(doeval(G_SCALAR, NULL));
2519 return pp_require(ARGS);
2525 register PERL_CONTEXT *cx;
2527 I32 gimme = GIMME_V, was = sub_generation;
2528 char tmpbuf[TYPE_DIGITS(long) + 12];
2533 if (!SvPV(sv,len) || !len)
2535 TAINT_PROPER("eval");
2541 /* switch to eval mode */
2543 SAVESPTR(compiling.cop_filegv);
2544 sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++evalseq);
2545 compiling.cop_filegv = gv_fetchfile(tmpbuf+2);
2546 compiling.cop_line = 1;
2547 /* XXX For C<eval "...">s within BEGIN {} blocks, this ends up
2548 deleting the eval's FILEGV from the stash before gv_check() runs
2549 (i.e. before run-time proper). To work around the coredump that
2550 ensues, we always turn GvMULTI_on for any globals that were
2551 introduced within evals. See force_ident(). GSAR 96-10-12 */
2552 safestr = savepv(tmpbuf);
2553 SAVEDELETE(defstash, safestr, strlen(safestr));
2555 hints = op->op_targ;
2557 push_return(op->op_next);
2558 PUSHBLOCK(cx, CXt_EVAL, SP);
2559 PUSHEVAL(cx, 0, compiling.cop_filegv);
2561 /* prepare to compile string */
2563 if (PERLDB_LINE && curstash != debstash)
2564 save_lines(GvAV(compiling.cop_filegv), linestr);
2567 MUTEX_LOCK(&eval_mutex);
2568 if (eval_owner && eval_owner != thr)
2570 COND_WAIT(&eval_cond, &eval_mutex);
2572 MUTEX_UNLOCK(&eval_mutex);
2573 #endif /* USE_THREADS */
2574 ret = doeval(gimme, NULL);
2575 if (PERLDB_INTER && was != sub_generation /* Some subs defined here. */
2576 && ret != op->op_next) { /* Successive compilation. */
2577 strcpy(safestr, "_<(eval )"); /* Anything fake and short. */
2579 return DOCATCH(ret);
2589 register PERL_CONTEXT *cx;
2591 U8 save_flags = op -> op_flags;
2596 retop = pop_return();
2599 if (gimme == G_VOID)
2601 else if (gimme == G_SCALAR) {
2604 if (SvFLAGS(TOPs) & SVs_TEMP)
2607 *MARK = sv_mortalcopy(TOPs);
2615 /* in case LEAVE wipes old return values */
2616 for (mark = newsp + 1; mark <= SP; mark++) {
2617 if (!(SvFLAGS(*mark) & SVs_TEMP)) {
2618 *mark = sv_mortalcopy(*mark);
2619 TAINT_NOT; /* Each item is independent */
2623 curpm = newpm; /* Don't pop $1 et al till now */
2626 * Closures mentioned at top level of eval cannot be referenced
2627 * again, and their presence indirectly causes a memory leak.
2628 * (Note that the fact that compcv and friends are still set here
2629 * is, AFAIK, an accident.) --Chip
2631 if (AvFILLp(comppad_name) >= 0) {
2632 SV **svp = AvARRAY(comppad_name);
2634 for (ix = AvFILLp(comppad_name); ix >= 0; ix--) {
2636 if (sv && sv != &sv_undef && *SvPVX(sv) == '&') {
2638 svp[ix] = &sv_undef;
2642 SvREFCNT_dec(CvOUTSIDE(sv));
2643 CvOUTSIDE(sv) = Nullcv;
2656 assert(CvDEPTH(compcv) == 1);
2658 CvDEPTH(compcv) = 0;
2661 if (optype == OP_REQUIRE &&
2662 !(gimme == G_SCALAR ? SvTRUE(*SP) : SP > newsp))
2664 /* Unassume the success we assumed earlier. */
2665 char *name = cx->blk_eval.old_name;
2666 (void)hv_delete(GvHVn(incgv), name, strlen(name), G_DISCARD);
2667 retop = die("%s did not return a true value", name);
2668 /* die_where() did LEAVE, or we won't be here */
2672 if (!(save_flags & OPf_SPECIAL))
2682 register PERL_CONTEXT *cx;
2683 I32 gimme = GIMME_V;
2688 push_return(cLOGOP->op_other->op_next);
2689 PUSHBLOCK(cx, CXt_EVAL, SP);
2691 eval_root = op; /* Only needed so that goto works right. */
2696 return DOCATCH(op->op_next);
2706 register PERL_CONTEXT *cx;
2714 if (gimme == G_VOID)
2716 else if (gimme == G_SCALAR) {
2719 if (SvFLAGS(TOPs) & (SVs_PADTMP|SVs_TEMP))
2722 *MARK = sv_mortalcopy(TOPs);
2731 /* in case LEAVE wipes old return values */
2732 for (mark = newsp + 1; mark <= SP; mark++) {
2733 if (!(SvFLAGS(*mark) & (SVs_PADTMP|SVs_TEMP))) {
2734 *mark = sv_mortalcopy(*mark);
2735 TAINT_NOT; /* Each item is independent */
2739 curpm = newpm; /* Don't pop $1 et al till now */
2750 register char *s = SvPV_force(sv, len);
2751 register char *send = s + len;
2752 register char *base;
2753 register I32 skipspaces = 0;
2756 bool postspace = FALSE;
2764 croak("Null picture in formline");
2766 New(804, fops, (send - s)*3+10, U16); /* Almost certainly too long... */
2771 *fpc++ = FF_LINEMARK;
2772 noblank = repeat = FALSE;
2790 case ' ': case '\t':
2801 *fpc++ = FF_LITERAL;
2809 *fpc++ = skipspaces;
2813 *fpc++ = FF_NEWLINE;
2817 arg = fpc - linepc + 1;
2824 *fpc++ = FF_LINEMARK;
2825 noblank = repeat = FALSE;
2834 ischop = s[-1] == '^';
2840 arg = (s - base) - 1;
2842 *fpc++ = FF_LITERAL;
2851 *fpc++ = FF_LINEGLOB;
2853 else if (*s == '#' || (*s == '.' && s[1] == '#')) {
2854 arg = ischop ? 512 : 0;
2864 arg |= 256 + (s - f);
2866 *fpc++ = s - base; /* fieldsize for FETCH */
2867 *fpc++ = FF_DECIMAL;
2872 bool ismore = FALSE;
2875 while (*++s == '>') ;
2876 prespace = FF_SPACE;
2878 else if (*s == '|') {
2879 while (*++s == '|') ;
2880 prespace = FF_HALFSPACE;
2885 while (*++s == '<') ;
2888 if (*s == '.' && s[1] == '.' && s[2] == '.') {
2892 *fpc++ = s - base; /* fieldsize for FETCH */
2894 *fpc++ = ischop ? FF_CHECKCHOP : FF_CHECKNL;
2912 { /* need to jump to the next word */
2914 z = WORD_ALIGN - SvCUR(sv) % WORD_ALIGN;
2915 SvGROW(sv, SvCUR(sv) + z + arg * sizeof(U16) + 4);
2916 s = SvPVX(sv) + SvCUR(sv) + z;
2918 Copy(fops, s, arg, U16);
2920 sv_magic(sv, Nullsv, 'f', Nullch, 0);
2925 * The rest of this file was derived from source code contributed
2928 * NOTE: this code was derived from Tom Horsley's qsort replacement
2929 * and should not be confused with the original code.
2932 /* Copyright (C) Tom Horsley, 1997. All rights reserved.
2934 Permission granted to distribute under the same terms as perl which are
2937 This program is free software; you can redistribute it and/or modify
2938 it under the terms of either:
2940 a) the GNU General Public License as published by the Free
2941 Software Foundation; either version 1, or (at your option) any
2944 b) the "Artistic License" which comes with this Kit.
2946 Details on the perl license can be found in the perl source code which
2947 may be located via the www.perl.com web page.
2949 This is the most wonderfulest possible qsort I can come up with (and
2950 still be mostly portable) My (limited) tests indicate it consistently
2951 does about 20% fewer calls to compare than does the qsort in the Visual
2952 C++ library, other vendors may vary.
2954 Some of the ideas in here can be found in "Algorithms" by Sedgewick,
2955 others I invented myself (or more likely re-invented since they seemed
2956 pretty obvious once I watched the algorithm operate for a while).
2958 Most of this code was written while watching the Marlins sweep the Giants
2959 in the 1997 National League Playoffs - no Braves fans allowed to use this
2960 code (just kidding :-).
2962 I realize that if I wanted to be true to the perl tradition, the only
2963 comment in this file would be something like:
2965 ...they shuffled back towards the rear of the line. 'No, not at the
2966 rear!' the slave-driver shouted. 'Three files up. And stay there...
2968 However, I really needed to violate that tradition just so I could keep
2969 track of what happens myself, not to mention some poor fool trying to
2970 understand this years from now :-).
2973 /* ********************************************************** Configuration */
2975 #ifndef QSORT_ORDER_GUESS
2976 #define QSORT_ORDER_GUESS 2 /* Select doubling version of the netBSD trick */
2979 /* QSORT_MAX_STACK is the largest number of partitions that can be stacked up for
2980 future processing - a good max upper bound is log base 2 of memory size
2981 (32 on 32 bit machines, 64 on 64 bit machines, etc). In reality can
2982 safely be smaller than that since the program is taking up some space and
2983 most operating systems only let you grab some subset of contiguous
2984 memory (not to mention that you are normally sorting data larger than
2985 1 byte element size :-).
2987 #ifndef QSORT_MAX_STACK
2988 #define QSORT_MAX_STACK 32
2991 /* QSORT_BREAK_EVEN is the size of the largest partition we should insertion sort.
2992 Anything bigger and we use qsort. If you make this too small, the qsort
2993 will probably break (or become less efficient), because it doesn't expect
2994 the middle element of a partition to be the same as the right or left -
2995 you have been warned).
2997 #ifndef QSORT_BREAK_EVEN
2998 #define QSORT_BREAK_EVEN 6
3001 /* ************************************************************* Data Types */
3003 /* hold left and right index values of a partition waiting to be sorted (the
3004 partition includes both left and right - right is NOT one past the end or
3005 anything like that).
3007 struct partition_stack_entry {
3010 #ifdef QSORT_ORDER_GUESS
3011 int qsort_break_even;
3015 /* ******************************************************* Shorthand Macros */
3017 /* Note that these macros will be used from inside the qsort function where
3018 we happen to know that the variable 'elt_size' contains the size of an
3019 array element and the variable 'temp' points to enough space to hold a
3020 temp element and the variable 'array' points to the array being sorted
3021 and 'compare' is the pointer to the compare routine.
3023 Also note that there are very many highly architecture specific ways
3024 these might be sped up, but this is simply the most generally portable
3025 code I could think of.
3028 /* Return < 0 == 0 or > 0 as the value of elt1 is < elt2, == elt2, > elt2
3030 #define qsort_cmp(elt1, elt2) \
3031 ((*compare)(array[elt1], array[elt2]))
3033 #ifdef QSORT_ORDER_GUESS
3034 #define QSORT_NOTICE_SWAP swapped++;
3036 #define QSORT_NOTICE_SWAP
3039 /* swaps contents of array elements elt1, elt2.
3041 #define qsort_swap(elt1, elt2) \
3044 temp = array[elt1]; \
3045 array[elt1] = array[elt2]; \
3046 array[elt2] = temp; \
3049 /* rotate contents of elt1, elt2, elt3 such that elt1 gets elt2, elt2 gets
3050 elt3 and elt3 gets elt1.
3052 #define qsort_rotate(elt1, elt2, elt3) \
3055 temp = array[elt1]; \
3056 array[elt1] = array[elt2]; \
3057 array[elt2] = array[elt3]; \
3058 array[elt3] = temp; \
3061 /* ************************************************************ Debug stuff */
3068 return; /* good place to set a breakpoint */
3071 #define qsort_assert(t) (void)( (t) || (break_here(), 0) )
3074 doqsort_all_asserts(
3078 int (*compare)(const void * elt1, const void * elt2),
3079 int pc_left, int pc_right, int u_left, int u_right)
3083 qsort_assert(pc_left <= pc_right);
3084 qsort_assert(u_right < pc_left);
3085 qsort_assert(pc_right < u_left);
3086 for (i = u_right + 1; i < pc_left; ++i) {
3087 qsort_assert(qsort_cmp(i, pc_left) < 0);
3089 for (i = pc_left; i < pc_right; ++i) {
3090 qsort_assert(qsort_cmp(i, pc_right) == 0);
3092 for (i = pc_right + 1; i < u_left; ++i) {
3093 qsort_assert(qsort_cmp(pc_right, i) < 0);
3097 #define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) \
3098 doqsort_all_asserts(array, num_elts, elt_size, compare, \
3099 PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT)
3103 #define qsort_assert(t) ((void)0)
3105 #define qsort_all_asserts(PC_LEFT, PC_RIGHT, U_LEFT, U_RIGHT) ((void)0)
3109 /* ****************************************************************** qsort */
3115 I32 (*compare)(SV *a, SV *b))
3119 struct partition_stack_entry partition_stack[QSORT_MAX_STACK];
3120 int next_stack_entry = 0;
3124 #ifdef QSORT_ORDER_GUESS
3125 int qsort_break_even;
3129 /* Make sure we actually have work to do.
3131 if (num_elts <= 1) {
3135 /* Setup the initial partition definition and fall into the sorting loop
3138 part_right = (int)(num_elts - 1);
3139 #ifdef QSORT_ORDER_GUESS
3140 qsort_break_even = QSORT_BREAK_EVEN;
3142 #define qsort_break_even QSORT_BREAK_EVEN
3145 if ((part_right - part_left) >= qsort_break_even) {
3146 /* OK, this is gonna get hairy, so lets try to document all the
3147 concepts and abbreviations and variables and what they keep
3150 pc: pivot chunk - the set of array elements we accumulate in the
3151 middle of the partition, all equal in value to the original
3152 pivot element selected. The pc is defined by:
3154 pc_left - the leftmost array index of the pc
3155 pc_right - the rightmost array index of the pc
3157 we start with pc_left == pc_right and only one element
3158 in the pivot chunk (but it can grow during the scan).
3160 u: uncompared elements - the set of elements in the partition
3161 we have not yet compared to the pivot value. There are two
3162 uncompared sets during the scan - one to the left of the pc
3163 and one to the right.
3165 u_right - the rightmost index of the left side's uncompared set
3166 u_left - the leftmost index of the right side's uncompared set
3168 The leftmost index of the left sides's uncompared set
3169 doesn't need its own variable because it is always defined
3170 by the leftmost edge of the whole partition (part_left). The
3171 same goes for the rightmost edge of the right partition
3174 We know there are no uncompared elements on the left once we
3175 get u_right < part_left and no uncompared elements on the
3176 right once u_left > part_right. When both these conditions
3177 are met, we have completed the scan of the partition.
3179 Any elements which are between the pivot chunk and the
3180 uncompared elements should be less than the pivot value on
3181 the left side and greater than the pivot value on the right
3182 side (in fact, the goal of the whole algorithm is to arrange
3183 for that to be true and make the groups of less-than and
3184 greater-then elements into new partitions to sort again).
3186 As you marvel at the complexity of the code and wonder why it
3187 has to be so confusing. Consider some of the things this level
3188 of confusion brings:
3190 Once I do a compare, I squeeze every ounce of juice out of it. I
3191 never do compare calls I don't have to do, and I certainly never
3194 I also never swap any elements unless I can prove there is a
3195 good reason. Many sort algorithms will swap a known value with
3196 an uncompared value just to get things in the right place (or
3197 avoid complexity :-), but that uncompared value, once it gets
3198 compared, may then have to be swapped again. A lot of the
3199 complexity of this code is due to the fact that it never swaps
3200 anything except compared values, and it only swaps them when the
3201 compare shows they are out of position.
3203 int pc_left, pc_right;
3204 int u_right, u_left;
3208 pc_left = ((part_left + part_right) / 2);
3210 u_right = pc_left - 1;
3211 u_left = pc_right + 1;
3213 /* Qsort works best when the pivot value is also the median value
3214 in the partition (unfortunately you can't find the median value
3215 without first sorting :-), so to give the algorithm a helping
3216 hand, we pick 3 elements and sort them and use the median value
3217 of that tiny set as the pivot value.
3219 Some versions of qsort like to use the left middle and right as
3220 the 3 elements to sort so they can insure the ends of the
3221 partition will contain values which will stop the scan in the
3222 compare loop, but when you have to call an arbitrarily complex
3223 routine to do a compare, its really better to just keep track of
3224 array index values to know when you hit the edge of the
3225 partition and avoid the extra compare. An even better reason to
3226 avoid using a compare call is the fact that you can drop off the
3227 edge of the array if someone foolishly provides you with an
3228 unstable compare function that doesn't always provide consistent
3231 So, since it is simpler for us to compare the three adjacent
3232 elements in the middle of the partition, those are the ones we
3233 pick here (conveniently pointed at by u_right, pc_left, and
3234 u_left). The values of the left, center, and right elements
3235 are refered to as l c and r in the following comments.
3238 #ifdef QSORT_ORDER_GUESS
3241 s = qsort_cmp(u_right, pc_left);
3244 s = qsort_cmp(pc_left, u_left);
3245 /* if l < c, c < r - already in order - nothing to do */
3247 /* l < c, c == r - already in order, pc grows */
3249 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3251 /* l < c, c > r - need to know more */
3252 s = qsort_cmp(u_right, u_left);
3254 /* l < c, c > r, l < r - swap c & r to get ordered */
3255 qsort_swap(pc_left, u_left);
3256 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3257 } else if (s == 0) {
3258 /* l < c, c > r, l == r - swap c&r, grow pc */
3259 qsort_swap(pc_left, u_left);
3261 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3263 /* l < c, c > r, l > r - make lcr into rlc to get ordered */
3264 qsort_rotate(pc_left, u_right, u_left);
3265 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3268 } else if (s == 0) {
3270 s = qsort_cmp(pc_left, u_left);
3272 /* l == c, c < r - already in order, grow pc */
3274 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3275 } else if (s == 0) {
3276 /* l == c, c == r - already in order, grow pc both ways */
3279 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3281 /* l == c, c > r - swap l & r, grow pc */
3282 qsort_swap(u_right, u_left);
3284 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3288 s = qsort_cmp(pc_left, u_left);
3290 /* l > c, c < r - need to know more */
3291 s = qsort_cmp(u_right, u_left);
3293 /* l > c, c < r, l < r - swap l & c to get ordered */
3294 qsort_swap(u_right, pc_left);
3295 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3296 } else if (s == 0) {
3297 /* l > c, c < r, l == r - swap l & c, grow pc */
3298 qsort_swap(u_right, pc_left);
3300 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3302 /* l > c, c < r, l > r - rotate lcr into crl to order */
3303 qsort_rotate(u_right, pc_left, u_left);
3304 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3306 } else if (s == 0) {
3307 /* l > c, c == r - swap ends, grow pc */
3308 qsort_swap(u_right, u_left);
3310 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3312 /* l > c, c > r - swap ends to get in order */
3313 qsort_swap(u_right, u_left);
3314 qsort_all_asserts(pc_left, pc_right, u_left + 1, u_right - 1);
3317 /* We now know the 3 middle elements have been compared and
3318 arranged in the desired order, so we can shrink the uncompared
3323 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
3325 /* The above massive nested if was the simple part :-). We now have
3326 the middle 3 elements ordered and we need to scan through the
3327 uncompared sets on either side, swapping elements that are on
3328 the wrong side or simply shuffling equal elements around to get
3329 all equal elements into the pivot chunk.
3333 int still_work_on_left;
3334 int still_work_on_right;
3336 /* Scan the uncompared values on the left. If I find a value
3337 equal to the pivot value, move it over so it is adjacent to
3338 the pivot chunk and expand the pivot chunk. If I find a value
3339 less than the pivot value, then just leave it - its already
3340 on the correct side of the partition. If I find a greater
3341 value, then stop the scan.
3343 while (still_work_on_left = (u_right >= part_left)) {
3344 s = qsort_cmp(u_right, pc_left);
3347 } else if (s == 0) {
3349 if (pc_left != u_right) {
3350 qsort_swap(u_right, pc_left);
3356 qsort_assert(u_right < pc_left);
3357 qsort_assert(pc_left <= pc_right);
3358 qsort_assert(qsort_cmp(u_right + 1, pc_left) <= 0);
3359 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
3362 /* Do a mirror image scan of uncompared values on the right
3364 while (still_work_on_right = (u_left <= part_right)) {
3365 s = qsort_cmp(pc_right, u_left);
3368 } else if (s == 0) {
3370 if (pc_right != u_left) {
3371 qsort_swap(pc_right, u_left);
3377 qsort_assert(u_left > pc_right);
3378 qsort_assert(pc_left <= pc_right);
3379 qsort_assert(qsort_cmp(pc_right, u_left - 1) <= 0);
3380 qsort_assert(qsort_cmp(pc_left, pc_right) == 0);
3383 if (still_work_on_left) {
3384 /* I know I have a value on the left side which needs to be
3385 on the right side, but I need to know more to decide
3386 exactly the best thing to do with it.
3388 if (still_work_on_right) {
3389 /* I know I have values on both side which are out of
3390 position. This is a big win because I kill two birds
3391 with one swap (so to speak). I can advance the
3392 uncompared pointers on both sides after swapping both
3393 of them into the right place.
3395 qsort_swap(u_right, u_left);
3398 qsort_all_asserts(pc_left, pc_right, u_left, u_right);
3400 /* I have an out of position value on the left, but the
3401 right is fully scanned, so I "slide" the pivot chunk
3402 and any less-than values left one to make room for the
3403 greater value over on the right. If the out of position
3404 value is immediately adjacent to the pivot chunk (there
3405 are no less-than values), I can do that with a swap,
3406 otherwise, I have to rotate one of the less than values
3407 into the former position of the out of position value
3408 and the right end of the pivot chunk into the left end
3412 if (pc_left == u_right) {
3413 qsort_swap(u_right, pc_right);
3414 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
3416 qsort_rotate(u_right, pc_left, pc_right);
3417 qsort_all_asserts(pc_left, pc_right-1, u_left, u_right-1);
3422 } else if (still_work_on_right) {
3423 /* Mirror image of complex case above: I have an out of
3424 position value on the right, but the left is fully
3425 scanned, so I need to shuffle things around to make room
3426 for the right value on the left.
3429 if (pc_right == u_left) {
3430 qsort_swap(u_left, pc_left);
3431 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
3433 qsort_rotate(pc_right, pc_left, u_left);
3434 qsort_all_asserts(pc_left+1, pc_right, u_left+1, u_right);
3439 /* No more scanning required on either side of partition,
3440 break out of loop and figure out next set of partitions
3446 /* The elements in the pivot chunk are now in the right place. They
3447 will never move or be compared again. All I have to do is decide
3448 what to do with the stuff to the left and right of the pivot
3451 Notes on the QSORT_ORDER_GUESS ifdef code:
3453 1. If I just built these partitions without swapping any (or
3454 very many) elements, there is a chance that the elements are
3455 already ordered properly (being properly ordered will
3456 certainly result in no swapping, but the converse can't be
3459 2. A (properly written) insertion sort will run faster on
3460 already ordered data than qsort will.
3462 3. Perhaps there is some way to make a good guess about
3463 switching to an insertion sort earlier than partition size 6
3464 (for instance - we could save the partition size on the stack
3465 and increase the size each time we find we didn't swap, thus
3466 switching to insertion sort earlier for partitions with a
3467 history of not swapping).
3469 4. Naturally, if I just switch right away, it will make
3470 artificial benchmarks with pure ascending (or descending)
3471 data look really good, but is that a good reason in general?
3475 #ifdef QSORT_ORDER_GUESS
3477 #if QSORT_ORDER_GUESS == 1
3478 qsort_break_even = (part_right - part_left) + 1;
3480 #if QSORT_ORDER_GUESS == 2
3481 qsort_break_even *= 2;
3483 #if QSORT_ORDER_GUESS == 3
3484 int prev_break = qsort_break_even;
3485 qsort_break_even *= qsort_break_even;
3486 if (qsort_break_even < prev_break) {
3487 qsort_break_even = (part_right - part_left) + 1;
3491 qsort_break_even = QSORT_BREAK_EVEN;
3495 if (part_left < pc_left) {
3496 /* There are elements on the left which need more processing.
3497 Check the right as well before deciding what to do.
3499 if (pc_right < part_right) {
3500 /* We have two partitions to be sorted. Stack the biggest one
3501 and process the smallest one on the next iteration. This
3502 minimizes the stack height by insuring that any additional
3503 stack entries must come from the smallest partition which
3504 (because it is smallest) will have the fewest
3505 opportunities to generate additional stack entries.
3507 if ((part_right - pc_right) > (pc_left - part_left)) {
3508 /* stack the right partition, process the left */
3509 partition_stack[next_stack_entry].left = pc_right + 1;
3510 partition_stack[next_stack_entry].right = part_right;
3511 #ifdef QSORT_ORDER_GUESS
3512 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
3514 part_right = pc_left - 1;
3516 /* stack the left partition, process the right */
3517 partition_stack[next_stack_entry].left = part_left;
3518 partition_stack[next_stack_entry].right = pc_left - 1;
3519 #ifdef QSORT_ORDER_GUESS
3520 partition_stack[next_stack_entry].qsort_break_even = qsort_break_even;
3522 part_left = pc_right + 1;
3524 qsort_assert(next_stack_entry < QSORT_MAX_STACK);
3527 /* The elements on the left are the only remaining elements
3528 that need sorting, arrange for them to be processed as the
3531 part_right = pc_left - 1;
3533 } else if (pc_right < part_right) {
3534 /* There is only one chunk on the right to be sorted, make it
3535 the new partition and loop back around.
3537 part_left = pc_right + 1;
3539 /* This whole partition wound up in the pivot chunk, so
3540 we need to get a new partition off the stack.
3542 if (next_stack_entry == 0) {
3543 /* the stack is empty - we are done */
3547 part_left = partition_stack[next_stack_entry].left;
3548 part_right = partition_stack[next_stack_entry].right;
3549 #ifdef QSORT_ORDER_GUESS
3550 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
3554 /* This partition is too small to fool with qsort complexity, just
3555 do an ordinary insertion sort to minimize overhead.
3558 /* Assume 1st element is in right place already, and start checking
3559 at 2nd element to see where it should be inserted.
3561 for (i = part_left + 1; i <= part_right; ++i) {
3563 /* Scan (backwards - just in case 'i' is already in right place)
3564 through the elements already sorted to see if the ith element
3565 belongs ahead of one of them.
3567 for (j = i - 1; j >= part_left; --j) {
3568 if (qsort_cmp(i, j) >= 0) {
3569 /* i belongs right after j
3576 /* Looks like we really need to move some things
3580 for (k = i - 1; k >= j; --k)
3581 array[k + 1] = array[k];
3586 /* That partition is now sorted, grab the next one, or get out
3587 of the loop if there aren't any more.
3590 if (next_stack_entry == 0) {
3591 /* the stack is empty - we are done */
3595 part_left = partition_stack[next_stack_entry].left;
3596 part_right = partition_stack[next_stack_entry].right;
3597 #ifdef QSORT_ORDER_GUESS
3598 qsort_break_even = partition_stack[next_stack_entry].qsort_break_even;
3603 /* Believe it or not, the array is sorted at this point! */