This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
nextchar() abuse misses an optimisation
[perl5.git] / op.h
1 /*    op.h
2  *
3  *    Copyright (c) 1991-2000, Larry Wall
4  *
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.
7  *
8  */
9
10 /*
11  * The fields of BASEOP are:
12  *      op_next         Pointer to next ppcode to execute after this one.
13  *                      (Top level pre-grafted op points to first op,
14  *                      but this is replaced when op is grafted in, when
15  *                      this op will point to the real next op, and the new
16  *                      parent takes over role of remembering starting op.)
17  *      op_ppaddr       Pointer to current ppcode's function.
18  *      op_type         The type of the operation.
19  *      op_flags        Flags common to all operations.  See OPf_* below.
20  *      op_private      Flags peculiar to a particular operation (BUT,
21  *                      by default, set to the number of children until
22  *                      the operation is privatized by a check routine,
23  *                      which may or may not check number of children).
24  */
25
26 typedef U32 PADOFFSET;
27 #define NOT_IN_PAD ((PADOFFSET) -1)
28
29 #ifdef DEBUGGING_OPS
30 #define OPCODE opcode
31 #else
32 #define OPCODE U16
33 #endif
34
35 #ifdef BASEOP_DEFINITION
36 #define BASEOP BASEOP_DEFINITION
37 #else
38 #define BASEOP                          \
39     OP*         op_next;                \
40     OP*         op_sibling;             \
41     OP*         (CPERLscope(*op_ppaddr))(pTHX);         \
42     PADOFFSET   op_targ;                \
43     OPCODE      op_type;                \
44     U16         op_seq;                 \
45     U8          op_flags;               \
46     U8          op_private;
47 #endif
48
49 #define OP_GIMME(op,dfl) \
50         (((op)->op_flags & OPf_WANT) == OPf_WANT_VOID   ? G_VOID   : \
51          ((op)->op_flags & OPf_WANT) == OPf_WANT_SCALAR ? G_SCALAR : \
52          ((op)->op_flags & OPf_WANT) == OPf_WANT_LIST   ? G_ARRAY   : \
53          dfl)
54
55 /*
56 =for apidoc Amn|U32|GIMME_V
57 The XSUB-writer's equivalent to Perl's C<wantarray>.  Returns C<G_VOID>,
58 C<G_SCALAR> or C<G_ARRAY> for void, scalar or list context,
59 respectively.
60
61 =for apidoc Amn|U32|GIMME
62 A backward-compatible version of C<GIMME_V> which can only return
63 C<G_SCALAR> or C<G_ARRAY>; in a void context, it returns C<G_SCALAR>.
64 Deprecated.  Use C<GIMME_V> instead.
65
66 =cut
67 */
68
69 #define GIMME_V         OP_GIMME(PL_op, block_gimme())
70
71 /* Public flags */
72
73 #define OPf_WANT        3       /* Mask for "want" bits: */
74 #define  OPf_WANT_VOID   1      /*   Want nothing */
75 #define  OPf_WANT_SCALAR 2      /*   Want single value */
76 #define  OPf_WANT_LIST   3      /*   Want list of any length */
77 #define OPf_KIDS        4       /* There is a firstborn child. */
78 #define OPf_PARENS      8       /* This operator was parenthesized. */
79                                 /*  (Or block needs explicit scope entry.) */
80 #define OPf_REF         16      /* Certified reference. */
81                                 /*  (Return container, not containee). */
82 #define OPf_MOD         32      /* Will modify (lvalue). */
83 #define OPf_STACKED     64      /* Some arg is arriving on the stack. */
84 #define OPf_SPECIAL     128     /* Do something weird for this op: */
85                                 /*  On local LVAL, don't init local value. */
86                                 /*  On OP_SORT, subroutine is inlined. */
87                                 /*  On OP_NOT, inversion was implicit. */
88                                 /*  On OP_LEAVE, don't restore curpm. */
89                                 /*  On truncate, we truncate filehandle */
90                                 /*  On control verbs, we saw no label */
91                                 /*  On flipflop, we saw ... instead of .. */
92                                 /*  On UNOPs, saw bare parens, e.g. eof(). */
93                                 /*  On OP_ENTERSUB || OP_NULL, saw a "do". */
94                                 /*  On OP_EXISTS, treat av as av, not avhv.  */
95                                 /*  On OP_(ENTER|LEAVE)EVAL, don't clear $@ */
96                                 /*  On OP_ENTERITER, loop var is per-thread */
97                                 /*  On pushre, re is /\s+/ imp. by split " " */
98                                 /*  On regcomp, "use re 'eval'" was in scope */
99
100 /* old names; don't use in new code, but don't break them, either */
101 #define OPf_LIST        OPf_WANT_LIST
102 #define OPf_KNOW        OPf_WANT
103
104 #define GIMME \
105           (PL_op->op_flags & OPf_WANT                                   \
106            ? ((PL_op->op_flags & OPf_WANT) == OPf_WANT_LIST             \
107               ? G_ARRAY                                                 \
108               : G_SCALAR)                                               \
109            : dowantarray())
110
111 /* NOTE: OP_NEXTSTATE, OP_DBSTATE, and OP_SETSTATE (i.e. COPs) carry lower
112  * bits of PL_hints in op_private */
113
114 /* Private for lvalues */
115 #define OPpLVAL_INTRO   128     /* Lvalue must be localized or lvalue sub */
116
117 /* Private for OP_LEAVE, OP_LEAVESUB, OP_LEAVESUBLV and OP_LEAVEWRITE */
118 #define OPpREFCOUNTED           64      /* op_targ carries a refcount */
119
120 /* Private for OP_AASSIGN */
121 #define OPpASSIGN_COMMON        64      /* Left & right have syms in common. */
122 #define OPpASSIGN_HASH          32      /* Assigning to possible pseudohash. */
123
124 /* Private for OP_SASSIGN */
125 #define OPpASSIGN_BACKWARDS     64      /* Left & right switched. */
126
127 /* Private for OP_MATCH and OP_SUBST{,CONST} */
128 #define OPpRUNTIME              64      /* Pattern coming in on the stack */
129
130 /* Private for OP_TRANS */
131 #define OPpTRANS_FROM_UTF       1
132 #define OPpTRANS_TO_UTF         2
133 #define OPpTRANS_IDENTICAL      4
134         /* When CU or UC, means straight latin-1 to utf-8 or vice versa */
135         /* Otherwise, IDENTICAL means the right side is the same as the left */
136 #define OPpTRANS_SQUASH         8
137 #define OPpTRANS_DELETE         16
138 #define OPpTRANS_COMPLEMENT     32
139 #define OPpTRANS_GROWS          64
140
141 /* Private for OP_REPEAT */
142 #define OPpREPEAT_DOLIST        64      /* List replication. */
143
144 /* Private for OP_LEAVELOOP */
145 #define OPpLOOP_CONTINUE        64      /* a continue block is present */
146
147 /* Private for OP_RV2?V, OP_?ELEM */
148 #define OPpDEREF                (32|64) /* Want ref to something: */
149 #define OPpDEREF_AV             32      /*   Want ref to AV. */
150 #define OPpDEREF_HV             64      /*   Want ref to HV. */
151 #define OPpDEREF_SV             (32|64) /*   Want ref to SV. */
152   /* OP_ENTERSUB only */
153 #define OPpENTERSUB_DB          16      /* Debug subroutine. */
154 #define OPpENTERSUB_HASTARG     32      /* Called from OP tree. */
155   /* OP_RV2CV only */
156 #define OPpENTERSUB_AMPER       8       /* Used & form to call. */
157 #define OPpENTERSUB_NOPAREN     128     /* bare sub call (without parens) */
158 #define OPpENTERSUB_INARGS      4       /* Lval used as arg to a sub. */
159   /* OP_GV only */
160 #define OPpEARLY_CV             32      /* foo() called before sub foo was parsed */
161   /* OP_?ELEM only */
162 #define OPpLVAL_DEFER           16      /* Defer creation of array/hash elem */
163   /* OP_RV2?V, OP_GVSV only */
164 #define OPpOUR_INTRO            16      /* Defer creation of array/hash elem */
165   /* for OP_RV2?V, lower bits carry hints (currently only HINT_STRICT_REFS) */
166
167 /* Private for OPs with TARGLEX */
168   /* (lower bits may carry MAXARG) */
169 #define OPpTARGET_MY            16      /* Target is PADMY. */
170
171 /* Private for OP_CONST */
172 #define OPpCONST_STRICT         8       /* bearword subject to strict 'subs' */
173 #define OPpCONST_ENTERED        16      /* Has been entered as symbol. */
174 #define OPpCONST_ARYBASE        32      /* Was a $[ translated to constant. */
175 #define OPpCONST_BARE           64      /* Was a bare word (filehandle?). */
176 #define OPpCONST_WARNING        128     /* Was a $^W translated to constant. */
177
178 /* Private for OP_FLIP/FLOP */
179 #define OPpFLIP_LINENUM         64      /* Range arg potentially a line num. */
180
181 /* Private for OP_LIST */
182 #define OPpLIST_GUESSED         64      /* Guessed that pushmark was needed. */
183
184 /* Private for OP_DELETE */
185 #define OPpSLICE                64      /* Operating on a list of keys */
186
187 /* Private for OP_EXISTS */
188 #define OPpEXISTS_SUB           64      /* Checking for &sub, not {} or [].  */
189
190 /* Private for OP_SORT, OP_PRTF, OP_SPRINTF, OP_FTTEXT, OP_FTBINARY, */
191 /*             string comparisons, and case changers. */
192 #define OPpLOCALE               64      /* Use locale */
193
194 /* Private for OP_SORT */
195 #define OPpSORT_NUMERIC         1       /* Optimized away { $a <=> $b } */
196 #define OPpSORT_INTEGER         2       /* Ditto while under "use integer" */
197 #define OPpSORT_REVERSE         4       /* Descending sort */
198 /* Private for OP_THREADSV */
199 #define OPpDONE_SVREF           64      /* Been through newSVREF once */
200
201 /* Private for OP_OPEN and OP_BACKTICK */
202 #define OPpOPEN_IN_RAW          16      /* binmode(F,":raw") on input fh */
203 #define OPpOPEN_IN_CRLF         32      /* binmode(F,":crlf") on input fh */
204 #define OPpOPEN_OUT_RAW         64      /* binmode(F,":raw") on output fh */
205 #define OPpOPEN_OUT_CRLF        128     /* binmode(F,":crlf") on output fh */
206
207 /* Private for OP_EXIT */
208 #define OPpEXIT_VMSISH          128     /* exit(0) vs. exit(1) vmsish mode*/
209
210 struct op {
211     BASEOP
212 };
213
214 struct unop {
215     BASEOP
216     OP *        op_first;
217 };
218
219 struct binop {
220     BASEOP
221     OP *        op_first;
222     OP *        op_last;
223 };
224
225 struct logop {
226     BASEOP
227     OP *        op_first;
228     OP *        op_other;
229 };
230
231 struct listop {
232     BASEOP
233     OP *        op_first;
234     OP *        op_last;
235     U32         op_children;
236 };
237
238 struct pmop {
239     BASEOP
240     OP *        op_first;
241     OP *        op_last;
242     U32         op_children;
243     OP *        op_pmreplroot;
244     OP *        op_pmreplstart;
245     PMOP *      op_pmnext;              /* list of all scanpats */
246     REGEXP *    op_pmregexp;            /* compiled expression */
247     U16         op_pmflags;
248     U16         op_pmpermflags;
249     U8          op_pmdynflags;
250 };
251
252 #define PMdf_USED       0x01            /* pm has been used once already */
253 #define PMdf_TAINTED    0x02            /* pm compiled from tainted pattern */
254 #define PMdf_UTF8       0x04            /* pm compiled from utf8 data */
255
256 #define PMf_RETAINT     0x0001          /* taint $1 etc. if target tainted */
257 #define PMf_ONCE        0x0002          /* use pattern only once per reset */
258 #define PMf_REVERSED    0x0004          /* Should be matched right->left */
259 #define PMf_MAYBE_CONST 0x0008          /* replacement contains variables */
260 #define PMf_SKIPWHITE   0x0010          /* skip leading whitespace for split */
261 #define PMf_WHITE       0x0020          /* pattern is \s+ */
262 #define PMf_CONST       0x0040          /* subst replacement is constant */
263 #define PMf_KEEP        0x0080          /* keep 1st runtime pattern forever */
264 #define PMf_GLOBAL      0x0100          /* pattern had a g modifier */
265 #define PMf_CONTINUE    0x0200          /* don't reset pos() if //g fails */
266 #define PMf_EVAL        0x0400          /* evaluating replacement as expr */
267 #define PMf_LOCALE      0x0800          /* use locale for character types */
268 #define PMf_MULTILINE   0x1000          /* assume multiple lines */
269 #define PMf_SINGLELINE  0x2000          /* assume single line */
270 #define PMf_FOLD        0x4000          /* case insensitivity */
271 #define PMf_EXTENDED    0x8000          /* chuck embedded whitespace */
272
273 /* mask of bits stored in regexp->reganch */
274 #define PMf_COMPILETIME (PMf_MULTILINE|PMf_SINGLELINE|PMf_LOCALE|PMf_FOLD|PMf_EXTENDED)
275
276 struct svop {
277     BASEOP
278     SV *        op_sv;
279 };
280
281 struct padop {
282     BASEOP
283     PADOFFSET   op_padix;
284 };
285
286 struct pvop {
287     BASEOP
288     char *      op_pv;
289 };
290
291 struct loop {
292     BASEOP
293     OP *        op_first;
294     OP *        op_last;
295     U32         op_children;
296     OP *        op_redoop;
297     OP *        op_nextop;
298     OP *        op_lastop;
299 };
300
301 #define cUNOPx(o)       ((UNOP*)o)
302 #define cBINOPx(o)      ((BINOP*)o)
303 #define cLISTOPx(o)     ((LISTOP*)o)
304 #define cLOGOPx(o)      ((LOGOP*)o)
305 #define cPMOPx(o)       ((PMOP*)o)
306 #define cSVOPx(o)       ((SVOP*)o)
307 #define cPADOPx(o)      ((PADOP*)o)
308 #define cPVOPx(o)       ((PVOP*)o)
309 #define cCOPx(o)        ((COP*)o)
310 #define cLOOPx(o)       ((LOOP*)o)
311
312 #define cUNOP           cUNOPx(PL_op)
313 #define cBINOP          cBINOPx(PL_op)
314 #define cLISTOP         cLISTOPx(PL_op)
315 #define cLOGOP          cLOGOPx(PL_op)
316 #define cPMOP           cPMOPx(PL_op)
317 #define cSVOP           cSVOPx(PL_op)
318 #define cPADOP          cPADOPx(PL_op)
319 #define cPVOP           cPVOPx(PL_op)
320 #define cCOP            cCOPx(PL_op)
321 #define cLOOP           cLOOPx(PL_op)
322
323 #define cUNOPo          cUNOPx(o)
324 #define cBINOPo         cBINOPx(o)
325 #define cLISTOPo        cLISTOPx(o)
326 #define cLOGOPo         cLOGOPx(o)
327 #define cPMOPo          cPMOPx(o)
328 #define cSVOPo          cSVOPx(o)
329 #define cPADOPo         cPADOPx(o)
330 #define cPVOPo          cPVOPx(o)
331 #define cCOPo           cCOPx(o)
332 #define cLOOPo          cLOOPx(o)
333
334 #define kUNOP           cUNOPx(kid)
335 #define kBINOP          cBINOPx(kid)
336 #define kLISTOP         cLISTOPx(kid)
337 #define kLOGOP          cLOGOPx(kid)
338 #define kPMOP           cPMOPx(kid)
339 #define kSVOP           cSVOPx(kid)
340 #define kPADOP          cPADOPx(kid)
341 #define kPVOP           cPVOPx(kid)
342 #define kCOP            cCOPx(kid)
343 #define kLOOP           cLOOPx(kid)
344
345
346 #ifdef USE_ITHREADS
347 #  define       cGVOPx_gv(o)    ((GV*)PL_curpad[cPADOPx(o)->op_padix])
348 #  define       IS_PADGV(v)     (v && SvTYPE(v) == SVt_PVGV && GvIN_PAD(v))
349 #  define       IS_PADCONST(v)  (v && SvREADONLY(v))
350 #  define       cSVOPx_sv(v)    (cSVOPx(v)->op_sv \
351                                  ? cSVOPx(v)->op_sv : PL_curpad[(v)->op_targ])
352 #  define       cSVOPx_svp(v)   (cSVOPx(v)->op_sv \
353                                  ? &cSVOPx(v)->op_sv : &PL_curpad[(v)->op_targ])
354 #else
355 #  define       cGVOPx_gv(o)    ((GV*)cSVOPx(o)->op_sv)
356 #  define       IS_PADGV(v)     FALSE
357 #  define       IS_PADCONST(v)  FALSE
358 #  define       cSVOPx_sv(v)    (cSVOPx(v)->op_sv)
359 #  define       cSVOPx_svp(v)   (&cSVOPx(v)->op_sv)
360 #endif
361
362 #define cGVOP_gv                cGVOPx_gv(PL_op)
363 #define cGVOPo_gv               cGVOPx_gv(o)
364 #define kGVOP_gv                cGVOPx_gv(kid)
365 #define cSVOP_sv                cSVOPx_sv(PL_op)
366 #define cSVOPo_sv               cSVOPx_sv(o)
367 #define kSVOP_sv                cSVOPx_sv(kid)
368
369 #define Nullop Null(OP*)
370
371 /* Lowest byte of PL_opargs */
372 #define OA_MARK 1
373 #define OA_FOLDCONST 2
374 #define OA_RETSCALAR 4
375 #define OA_TARGET 8
376 #define OA_RETINTEGER 16
377 #define OA_OTHERINT 32
378 #define OA_DANGEROUS 64
379 #define OA_DEFGV 128
380 #define OA_TARGLEX 256
381
382 /* The next 4 bits encode op class information */
383 #define OCSHIFT 9
384
385 #define OA_CLASS_MASK (15 << OCSHIFT)
386
387 #define OA_BASEOP (0 << OCSHIFT)
388 #define OA_UNOP (1 << OCSHIFT)
389 #define OA_BINOP (2 << OCSHIFT)
390 #define OA_LOGOP (3 << OCSHIFT)
391 #define OA_LISTOP (4 << OCSHIFT)
392 #define OA_PMOP (5 << OCSHIFT)
393 #define OA_SVOP (6 << OCSHIFT)
394 #define OA_PADOP (7 << OCSHIFT)
395 #define OA_PVOP_OR_SVOP (8 << OCSHIFT)
396 #define OA_LOOP (9 << OCSHIFT)
397 #define OA_COP (10 << OCSHIFT)
398 #define OA_BASEOP_OR_UNOP (11 << OCSHIFT)
399 #define OA_FILESTATOP (12 << OCSHIFT)
400 #define OA_LOOPEXOP (13 << OCSHIFT)
401
402 #define OASHIFT 13
403
404 /* Remaining nybbles of PL_opargs */
405 #define OA_SCALAR 1
406 #define OA_LIST 2
407 #define OA_AVREF 3
408 #define OA_HVREF 4
409 #define OA_CVREF 5
410 #define OA_FILEREF 6
411 #define OA_SCALARREF 7
412 #define OA_OPTIONAL 8
413
414 #ifdef USE_ITHREADS
415 #  define OP_REFCNT_INIT                MUTEX_INIT(&PL_op_mutex)
416 #  define OP_REFCNT_LOCK                MUTEX_LOCK(&PL_op_mutex)
417 #  define OP_REFCNT_UNLOCK              MUTEX_UNLOCK(&PL_op_mutex)
418 #  define OP_REFCNT_TERM                MUTEX_DESTROY(&PL_op_mutex)
419 #  define OpREFCNT_set(o,n)             ((o)->op_targ = (n))
420 #  define OpREFCNT_inc(o)               ((o) ? (++(o)->op_targ, (o)) : Nullop)
421 #  define OpREFCNT_dec(o)               (--(o)->op_targ)
422 #else
423 #  define OP_REFCNT_INIT                NOOP
424 #  define OP_REFCNT_LOCK                NOOP
425 #  define OP_REFCNT_UNLOCK              NOOP
426 #  define OP_REFCNT_TERM                NOOP
427 #  define OpREFCNT_set(o,n)             NOOP
428 #  define OpREFCNT_inc(o)               (o)
429 #  define OpREFCNT_dec(o)               0
430 #endif
431
432 /* flags used by Perl_load_module() */
433 #define PERL_LOADMOD_DENY               0x1
434 #define PERL_LOADMOD_NOIMPORT           0x2
435 #define PERL_LOADMOD_IMPORT_OPS         0x4