This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
inline.h: Change fcn name prefix from S_ to Perl_
[perl5.git] / perl.h
1 /*    perl.h
2  *
3  *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
4  *    2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 #ifndef H_PERL
12 #define H_PERL 1
13
14 #ifdef PERL_FOR_X2P
15 /*
16  * This file is being used for x2p stuff.
17  * Above symbol is defined via -D in 'x2p/Makefile.SH'
18  * Decouple x2p stuff from some of perls more extreme eccentricities.
19  */
20 #undef MULTIPLICITY
21 #undef USE_STDIO
22 #define USE_STDIO
23 #endif /* PERL_FOR_X2P */
24
25 #ifdef PERL_MICRO
26 #   include "uconfig.h"
27 #else
28 #   include "config.h"
29 #endif
30
31 /* this is used for functions which take a depth trailing
32  * argument under debugging */
33 #ifdef DEBUGGING
34 #define _pDEPTH ,U32 depth
35 #define _aDEPTH ,depth
36 #else
37 #define _pDEPTH
38 #define _aDEPTH
39 #endif
40
41 /* NOTE 1: that with gcc -std=c89 the __STDC_VERSION__ is *not* defined
42  * because the __STDC_VERSION__ became a thing only with C90.  Therefore,
43  * with gcc, HAS_C99 will never become true as long as we use -std=c89.
44
45  * NOTE 2: headers lie.  Do not expect that if HAS_C99 gets to be true,
46  * all the C99 features are there and are correct. */
47 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
48     defined(_STDC_C99) || defined(__c99)
49 #  define HAS_C99 1
50 #endif
51
52 /* See L<perlguts/"The Perl API"> for detailed notes on
53  * PERL_IMPLICIT_CONTEXT and PERL_IMPLICIT_SYS */
54
55 /* XXX NOTE that from here --> to <-- the same logic is
56  * repeated in makedef.pl, so be certain to update
57  * both places when editing. */
58
59 #ifdef USE_ITHREADS
60 #  if !defined(MULTIPLICITY)
61 #    define MULTIPLICITY
62 #  endif
63 #endif
64
65 #ifdef PERL_GLOBAL_STRUCT_PRIVATE
66 #  ifndef PERL_GLOBAL_STRUCT
67 #    define PERL_GLOBAL_STRUCT
68 #  endif
69 #endif
70
71 #ifdef PERL_GLOBAL_STRUCT
72 #  ifndef MULTIPLICITY
73 #    define MULTIPLICITY
74 #  endif
75 #endif
76
77 #ifdef MULTIPLICITY
78 #  ifndef PERL_IMPLICIT_CONTEXT
79 #    define PERL_IMPLICIT_CONTEXT
80 #  endif
81 #endif
82
83 /* undef WIN32 when building on Cygwin (for libwin32) - gph */
84 #ifdef __CYGWIN__
85 #   undef WIN32
86 #   undef _WIN32
87 #endif
88
89 #if defined(__SYMBIAN32__) || (defined(__VC32__) && defined(WINS))
90 #   ifndef SYMBIAN
91 #       define SYMBIAN
92 #   endif
93 #endif
94
95 #ifdef __SYMBIAN32__
96 #  include "symbian/symbian_proto.h"
97 #endif
98
99 /* Any stack-challenged places.  The limit varies (and often
100  * is configurable), but using more than a kilobyte of stack
101  * is usually dubious in these systems. */
102 #if defined(__SYMBIAN32__)
103 /* Symbian: need to work around the SDK features. *
104  * On WINS: MS VC5 generates calls to _chkstk,         *
105  * if a "large" stack frame is allocated.              *
106  * gcc on MARM does not generate calls like these.     */
107 #   define USE_HEAP_INSTEAD_OF_STACK
108 #endif
109
110 /* Use the reentrant APIs like localtime_r and getpwent_r */
111 /* Win32 has naturally threadsafe libraries, no need to use any _r variants.
112  * XXX KEEP makedef.pl copy of this code in sync */
113 #if defined(USE_ITHREADS) && !defined(USE_REENTRANT_API) && !defined(NETWARE) && !defined(WIN32)
114 #   define USE_REENTRANT_API
115 #endif
116
117 /* <--- here ends the logic shared by perl.h and makedef.pl */
118
119 /* Microsoft Visual C++ 6.0 needs special treatment in numerous places */
120 #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1200 && _MSC_VER < 1300
121 #  define USING_MSVC6
122 #endif
123
124 #undef START_EXTERN_C
125 #undef END_EXTERN_C
126 #undef EXTERN_C
127 #ifdef __cplusplus
128 #  define START_EXTERN_C extern "C" {
129 #  define END_EXTERN_C }
130 #  define EXTERN_C extern "C"
131 #else
132 #  define START_EXTERN_C
133 #  define END_EXTERN_C
134 #  define EXTERN_C extern
135 #endif
136
137 /* Fallback definitions in case we don't have definitions from config.h.
138    This should only matter for systems that don't use Configure and
139    haven't been modified to define PERL_STATIC_INLINE yet.
140 */
141 #if !defined(PERL_STATIC_INLINE)
142 #  ifdef HAS_STATIC_INLINE
143 #    define PERL_STATIC_INLINE static inline
144 #  else
145 #    define PERL_STATIC_INLINE static
146 #  endif
147 #endif
148
149 #if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GET_VARS)
150 #    ifdef PERL_GLOBAL_STRUCT_PRIVATE
151        EXTERN_C struct perl_vars* Perl_GetVarsPrivate();
152 #      define PERL_GET_VARS() Perl_GetVarsPrivate() /* see miniperlmain.c */
153 #    else
154 #      define PERL_GET_VARS() PL_VarsPtr
155 #    endif
156 #endif
157
158 /* this used to be off by default, now its on, see perlio.h */
159 #define PERLIO_FUNCS_CONST
160
161 #define pVAR    struct perl_vars* my_vars PERL_UNUSED_DECL
162
163 #ifdef PERL_GLOBAL_STRUCT
164 #  define dVAR          pVAR    = (struct perl_vars*)PERL_GET_VARS()
165 #else
166 #  define dVAR          dNOOP
167 #endif
168
169 #ifdef PERL_IMPLICIT_CONTEXT
170 #  ifndef MULTIPLICITY
171 #    define MULTIPLICITY
172 #  endif
173 #  define tTHX  PerlInterpreter*
174 #  define pTHX  tTHX my_perl PERL_UNUSED_DECL
175 #  define aTHX  my_perl
176 #  define aTHXa(a) aTHX = (tTHX)a
177 #  ifdef PERL_GLOBAL_STRUCT
178 #    define dTHXa(a)    dVAR; pTHX = (tTHX)a
179 #  else
180 #    define dTHXa(a)    pTHX = (tTHX)a
181 #  endif
182 #  ifdef PERL_GLOBAL_STRUCT
183 #    define dTHX                dVAR; pTHX = PERL_GET_THX
184 #  else
185 #    define dTHX                pTHX = PERL_GET_THX
186 #  endif
187 #  define pTHX_         pTHX,
188 #  define aTHX_         aTHX,
189 #  define pTHX_1        2
190 #  define pTHX_2        3
191 #  define pTHX_3        4
192 #  define pTHX_4        5
193 #  define pTHX_5        6
194 #  define pTHX_6        7
195 #  define pTHX_7        8
196 #  define pTHX_8        9
197 #  define pTHX_9        10
198 #  define pTHX_12       13
199 #  if defined(DEBUGGING) && !defined(PERL_TRACK_MEMPOOL)
200 #    define PERL_TRACK_MEMPOOL
201 #  endif
202 #else
203 #  undef PERL_TRACK_MEMPOOL
204 #endif
205
206 #ifdef DEBUGGING
207 #  define dTHX_DEBUGGING dTHX
208 #else
209 #  define dTHX_DEBUGGING dNOOP
210 #endif
211
212 #define STATIC static
213
214 #ifndef PERL_CORE
215 /* Do not use these macros. They were part of PERL_OBJECT, which was an
216  * implementation of multiplicity using C++ objects. They have been left
217  * here solely for the sake of XS code which has incorrectly
218  * cargo-culted them.
219  */
220 #define CPERLscope(x) x
221 #define CPERLarg void
222 #define CPERLarg_
223 #define _CPERLarg
224 #define PERL_OBJECT_THIS
225 #define _PERL_OBJECT_THIS
226 #define PERL_OBJECT_THIS_
227 #define CALL_FPTR(fptr) (*fptr)
228 #define MEMBER_TO_FPTR(name) name
229 #endif /* !PERL_CORE */
230
231 #define CALLRUNOPS  PL_runops
232
233 #define CALLREGCOMP(sv, flags) Perl_pregcomp(aTHX_ (sv),(flags))
234
235 #define CALLREGCOMP_ENG(prog, sv, flags) (prog)->comp(aTHX_ sv, flags)
236 #define CALLREGEXEC(prog,stringarg,strend,strbeg,minend,sv,data,flags) \
237     RX_ENGINE(prog)->exec(aTHX_ (prog),(stringarg),(strend), \
238         (strbeg),(minend),(sv),(data),(flags))
239 #define CALLREG_INTUIT_START(prog,sv,strbeg,strpos,strend,flags,data) \
240     RX_ENGINE(prog)->intuit(aTHX_ (prog), (sv), (strbeg), (strpos), \
241         (strend),(flags),(data))
242 #define CALLREG_INTUIT_STRING(prog) \
243     RX_ENGINE(prog)->checkstr(aTHX_ (prog))
244
245 #define CALLREGFREE(prog) \
246     Perl_pregfree(aTHX_ (prog))
247
248 #define CALLREGFREE_PVT(prog) \
249     if(prog && RX_ENGINE(prog)) RX_ENGINE(prog)->rxfree(aTHX_ (prog))
250
251 #define CALLREG_NUMBUF_FETCH(rx,paren,usesv)                                \
252     RX_ENGINE(rx)->numbered_buff_FETCH(aTHX_ (rx),(paren),(usesv))
253
254 #define CALLREG_NUMBUF_STORE(rx,paren,value) \
255     RX_ENGINE(rx)->numbered_buff_STORE(aTHX_ (rx),(paren),(value))
256
257 #define CALLREG_NUMBUF_LENGTH(rx,sv,paren)                              \
258     RX_ENGINE(rx)->numbered_buff_LENGTH(aTHX_ (rx),(sv),(paren))
259
260 #define CALLREG_NAMED_BUFF_FETCH(rx, key, flags) \
261     RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), NULL, ((flags) | RXapif_FETCH))
262
263 #define CALLREG_NAMED_BUFF_STORE(rx, key, value, flags) \
264     RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), (value), ((flags) | RXapif_STORE))
265
266 #define CALLREG_NAMED_BUFF_DELETE(rx, key, flags) \
267     RX_ENGINE(rx)->named_buff(aTHX_ (rx),(key), NULL, ((flags) | RXapif_DELETE))
268
269 #define CALLREG_NAMED_BUFF_CLEAR(rx, flags) \
270     RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, ((flags) | RXapif_CLEAR))
271
272 #define CALLREG_NAMED_BUFF_EXISTS(rx, key, flags) \
273     RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), NULL, ((flags) | RXapif_EXISTS))
274
275 #define CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags) \
276     RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx), NULL, ((flags) | RXapif_FIRSTKEY))
277
278 #define CALLREG_NAMED_BUFF_NEXTKEY(rx, lastkey, flags) \
279     RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx), (lastkey), ((flags) | RXapif_NEXTKEY))
280
281 #define CALLREG_NAMED_BUFF_SCALAR(rx, flags) \
282     RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, ((flags) | RXapif_SCALAR))
283
284 #define CALLREG_NAMED_BUFF_COUNT(rx) \
285     RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, RXapif_REGNAMES_COUNT)
286
287 #define CALLREG_NAMED_BUFF_ALL(rx, flags) \
288     RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, flags)
289
290 #define CALLREG_PACKAGE(rx) \
291     RX_ENGINE(rx)->qr_package(aTHX_ (rx))
292
293 #if defined(USE_ITHREADS)
294 #define CALLREGDUPE(prog,param) \
295     Perl_re_dup(aTHX_ (prog),(param))
296
297 #define CALLREGDUPE_PVT(prog,param) \
298     (prog ? RX_ENGINE(prog)->dupe(aTHX_ (prog),(param)) \
299           : (REGEXP *)NULL)
300 #endif
301
302
303
304
305
306 /*
307  * Because of backward compatibility reasons the PERL_UNUSED_DECL
308  * cannot be changed from postfix to PERL_UNUSED_DECL(x).  Sigh.
309  *
310  * Note that there are C compilers such as MetroWerks CodeWarrior
311  * which do not have an "inlined" way (like the gcc __attribute__) of
312  * marking unused variables (they need e.g. a #pragma) and therefore
313  * cpp macros like PERL_UNUSED_DECL cannot work for this purpose, even
314  * if it were PERL_UNUSED_DECL(x), which it cannot be (see above).
315  *
316  */
317
318 #if defined(__SYMBIAN32__) && defined(__GNUC__)
319 #  ifdef __cplusplus
320 #    define PERL_UNUSED_DECL
321 #  else
322 #    define PERL_UNUSED_DECL __attribute__((unused))
323 #  endif
324 #endif
325
326 #ifndef PERL_UNUSED_DECL
327 #  if defined(HASATTRIBUTE_UNUSED) && (!defined(__cplusplus) || __GNUC__ >= 4)
328 #    define PERL_UNUSED_DECL __attribute__unused__
329 #  else
330 #    define PERL_UNUSED_DECL
331 #  endif
332 #endif
333
334 /* gcc -Wall:
335  * for silencing unused variables that are actually used most of the time,
336  * but we cannot quite get rid of, such as "ax" in PPCODE+noargs xsubs,
337  * or variables/arguments that are used only in certain configurations.
338  */
339 #ifndef PERL_UNUSED_ARG
340 #  define PERL_UNUSED_ARG(x) ((void)sizeof(x))
341 #endif
342 #ifndef PERL_UNUSED_VAR
343 #  define PERL_UNUSED_VAR(x) ((void)sizeof(x))
344 #endif
345
346 #if defined(USE_ITHREADS) || defined(PERL_GLOBAL_STRUCT)
347 #  define PERL_UNUSED_CONTEXT PERL_UNUSED_ARG(my_perl)
348 #else
349 #  define PERL_UNUSED_CONTEXT
350 #endif
351
352 /* gcc (-ansi) -pedantic doesn't allow gcc statement expressions,
353  * g++ allows them but seems to have problems with them
354  * (insane errors ensue).
355  * g++ does not give insane errors now (RMB 2008-01-30, gcc 4.2.2).
356  */
357 #if defined(PERL_GCC_PEDANTIC) || \
358     (defined(__GNUC__) && defined(__cplusplus) && \
359         ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2))))
360 #  ifndef PERL_GCC_BRACE_GROUPS_FORBIDDEN
361 #    define PERL_GCC_BRACE_GROUPS_FORBIDDEN
362 #  endif
363 #endif
364
365 /* Use PERL_UNUSED_RESULT() to suppress the warnings about unused results
366  * of function calls, e.g. PERL_UNUSED_RESULT(foo(a, b)).
367  *
368  * The main reason for this is that the combination of gcc -Wunused-result
369  * (part of -Wall) and the __attribute__((warn_unused_result)) cannot
370  * be silenced with casting to void.  This causes trouble when the system
371  * header files use the attribute.
372  *
373  * Use PERL_UNUSED_RESULT sparingly, though, since usually the warning
374  * is there for a good reason: you might lose success/failure information,
375  * or leak resources, or changes in resources.
376  *
377  * But sometimes you just want to ignore the return value, e.g. on
378  * codepaths soon ending up in abort, or in "best effort" attempts,
379  * or in situations where there is no good way to handle failures.
380  *
381  * Sometimes PERL_UNUSED_RESULT might not be the most natural way:
382  * another possibility is that you can capture the return value
383  * and use PERL_UNUSED_VAR on that.
384  *
385  * The __typeof__() is used instead of typeof() since typeof() is not
386  * available under strict C89, and because of compilers masquerading
387  * as gcc (clang and icc), we want exactly the gcc extension
388  * __typeof__ and nothing else.
389  */
390 #ifndef PERL_UNUSED_RESULT
391 #  if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
392 #    define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
393 #  else
394 #    define PERL_UNUSED_RESULT(v) ((void)(v))
395 #  endif
396 #endif
397
398 /* on gcc (and clang), specify that a warning should be temporarily
399  * ignored; e.g.
400  *
401  *    GCC_DIAG_IGNORE_DECL(-Wmultichar);
402  *    char b = 'ab';
403  *    GCC_DIAG_RESTORE_DECL;
404  *
405  * based on http://dbp-consulting.com/tutorials/SuppressingGCCWarnings.html
406  *
407  * Note that "pragma GCC diagnostic push/pop" was added in GCC 4.6, Mar 2011;
408  * clang only pretends to be GCC 4.2, but still supports push/pop.
409  *
410  * Note on usage: all macros must be used at a place where a declaration
411  * or statement can occur, i.e., not in the middle of an expression.
412  * *_DIAG_IGNORE() and *_DIAG_RESTORE can be used in any such place, but
413  * must be used without a following semicolon.  *_DIAG_IGNORE_DECL() and
414  * *_DIAG_RESTORE_DECL must be used with a following semicolon, and behave
415  * syntactically as declarations (like dNOOP).  *_DIAG_IGNORE_STMT()
416  * and *_DIAG_RESTORE_STMT must be used with a following semicolon,
417  * and behave syntactically as statements (like NOOP).
418  *
419  */
420
421 #if defined(__clang__) || defined(__clang) || \
422        (defined( __GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406)
423 #  define GCC_DIAG_PRAGMA(x) _Pragma (#x)
424 /* clang has "clang diagnostic" pragmas, but also understands gcc. */
425 #  define GCC_DIAG_IGNORE(x) _Pragma("GCC diagnostic push") \
426                              GCC_DIAG_PRAGMA(GCC diagnostic ignored #x)
427 #  define GCC_DIAG_RESTORE   _Pragma("GCC diagnostic pop")
428 #else
429 #  define GCC_DIAG_IGNORE(w)
430 #  define GCC_DIAG_RESTORE
431 #endif
432 #define GCC_DIAG_IGNORE_DECL(x) GCC_DIAG_IGNORE(x) dNOOP
433 #define GCC_DIAG_RESTORE_DECL GCC_DIAG_RESTORE dNOOP
434 #define GCC_DIAG_IGNORE_STMT(x) GCC_DIAG_IGNORE(x) NOOP
435 #define GCC_DIAG_RESTORE_STMT GCC_DIAG_RESTORE NOOP
436 /* for clang specific pragmas */
437 #if defined(__clang__) || defined(__clang)
438 #  define CLANG_DIAG_PRAGMA(x) _Pragma (#x)
439 #  define CLANG_DIAG_IGNORE(x) _Pragma("clang diagnostic push") \
440                                CLANG_DIAG_PRAGMA(clang diagnostic ignored #x)
441 #  define CLANG_DIAG_RESTORE   _Pragma("clang diagnostic pop")
442 #else
443 #  define CLANG_DIAG_IGNORE(w)
444 #  define CLANG_DIAG_RESTORE
445 #endif
446 #define CLANG_DIAG_IGNORE_DECL(x) CLANG_DIAG_IGNORE(x) dNOOP
447 #define CLANG_DIAG_RESTORE_DECL CLANG_DIAG_RESTORE dNOOP
448 #define CLANG_DIAG_IGNORE_STMT(x) CLANG_DIAG_IGNORE(x) NOOP
449 #define CLANG_DIAG_RESTORE_STMT CLANG_DIAG_RESTORE NOOP
450
451 #if defined(_MSC_VER) && (_MSC_VER >= 1300)
452 #  define MSVC_DIAG_IGNORE(x) __pragma(warning(push)) \
453                               __pragma(warning(disable : x))
454 #  define MSVC_DIAG_RESTORE   __pragma(warning(pop))
455 #else
456 #  define MSVC_DIAG_IGNORE(x)
457 #  define MSVC_DIAG_RESTORE
458 #endif
459 #define MSVC_DIAG_IGNORE_DECL(x) MSVC_DIAG_IGNORE(x) dNOOP
460 #define MSVC_DIAG_RESTORE_DECL MSVC_DIAG_RESTORE dNOOP
461 #define MSVC_DIAG_IGNORE_STMT(x) MSVC_DIAG_IGNORE(x) NOOP
462 #define MSVC_DIAG_RESTORE_STMT MSVC_DIAG_RESTORE NOOP
463
464 #define NOOP /*EMPTY*/(void)0
465 #define dNOOP struct Perl___notused_struct
466
467 #ifndef pTHX
468 /* Don't bother defining tTHX ; using it outside
469  * code guarded by PERL_IMPLICIT_CONTEXT is an error.
470  */
471 #  define pTHX          void
472 #  define pTHX_
473 #  define aTHX
474 #  define aTHX_
475 #  define aTHXa(a)      NOOP
476 #  define dTHXa(a)      dNOOP
477 #  define dTHX          dNOOP
478 #  define pTHX_1        1       
479 #  define pTHX_2        2
480 #  define pTHX_3        3
481 #  define pTHX_4        4
482 #  define pTHX_5        5
483 #  define pTHX_6        6
484 #  define pTHX_7        7
485 #  define pTHX_8        8
486 #  define pTHX_9        9
487 #  define pTHX_12       12
488 #endif
489
490 #ifndef dVAR
491 #  define dVAR          dNOOP
492 #endif
493
494 /* these are only defined for compatibility; should not be used internally */
495 #if !defined(pTHXo) && !defined(PERL_CORE)
496 #  define pTHXo         pTHX
497 #  define pTHXo_        pTHX_
498 #  define aTHXo         aTHX
499 #  define aTHXo_        aTHX_
500 #  define dTHXo         dTHX
501 #  define dTHXoa(x)     dTHXa(x)
502 #endif
503
504 #ifndef pTHXx
505 #  define pTHXx         PerlInterpreter *my_perl
506 #  define pTHXx_        pTHXx,
507 #  define aTHXx         my_perl
508 #  define aTHXx_        aTHXx,
509 #  define dTHXx         dTHX
510 #endif
511
512 /* Under PERL_IMPLICIT_SYS (used in Windows for fork emulation)
513  * PerlIO_foo() expands to PL_StdIO->pFOO(PL_StdIO, ...).
514  * dTHXs is therefore needed for all functions using PerlIO_foo(). */
515 #ifdef PERL_IMPLICIT_SYS
516 #  ifdef PERL_GLOBAL_STRUCT_PRIVATE
517 #    define dTHXs               dVAR; dTHX
518 #  else
519 #    define dTHXs               dTHX
520 #  endif
521 #else
522 #  ifdef PERL_GLOBAL_STRUCT_PRIVATE
523 #    define dTHXs               dVAR
524 #  else
525 #    define dTHXs               dNOOP
526 #  endif
527 #endif
528
529 #if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN) && !defined(__cplusplus)
530 #  ifndef PERL_USE_GCC_BRACE_GROUPS
531 #    define PERL_USE_GCC_BRACE_GROUPS
532 #  endif
533 #endif
534
535 /*
536 =head1 Miscellaneous Functions
537
538 =for apidoc AmnUu|void|STMT_START
539
540  STMT_START { statements; } STMT_END;
541
542 can be used as a single statement, as in
543
544  if (x) STMT_START { ... } STMT_END; else ...
545
546 These are often used in macro definitions.  Note that you can't return a value
547 out of them.
548
549 =for apidoc AmnUhu|void|STMT_END
550
551 =cut
552
553  Trying to select a version that gives no warnings...
554 */
555 #if !(defined(STMT_START) && defined(STMT_END))
556 # ifdef PERL_USE_GCC_BRACE_GROUPS
557 #   define STMT_START   (void)( /* gcc supports "({ STATEMENTS; })" */
558 #   define STMT_END     )
559 # else
560 #   define STMT_START   do
561 #   define STMT_END     while (0)
562 # endif
563 #endif
564
565 #ifndef BYTEORDER  /* Should never happen -- byteorder is in config.h */
566 #   define BYTEORDER 0x1234
567 #endif
568
569 /* Overall memory policy? */
570 #ifndef CONSERVATIVE
571 #   define LIBERAL 1
572 #endif
573
574 #if 'A' == 65 && 'I' == 73 && 'J' == 74 && 'Z' == 90
575 #define ASCIIish
576 #else
577 #undef  ASCIIish
578 #endif
579
580 /*
581  * The following contortions are brought to you on behalf of all the
582  * standards, semi-standards, de facto standards, not-so-de-facto standards
583  * of the world, as well as all the other botches anyone ever thought of.
584  * The basic theory is that if we work hard enough here, the rest of the
585  * code can be a lot prettier.  Well, so much for theory.  Sorry, Henry...
586  */
587
588 /* define this once if either system, instead of cluttering up the src */
589 #if defined(MSDOS) || defined(WIN32) || defined(NETWARE)
590 #define DOSISH 1
591 #endif
592
593 /* These exist only for back-compat with XS modules. */
594 #ifndef PERL_CORE
595 #define VOL volatile
596 #define CAN_PROTOTYPE
597 #define _(args) args
598 #define I_LIMITS
599 #define I_STDARG
600 #define STANDARD_C
601 #endif
602
603 /* By compiling a perl with -DNO_TAINT_SUPPORT or -DSILENT_NO_TAINT_SUPPORT,
604  * you get a perl without taint support, but doubtlessly with a lesser
605  * degree of support. Do not do so unless you know exactly what it means
606  * technically, have a good reason to do so, and know exactly how the
607  * perl will be used. perls with -DSILENT_NO_TAINT_SUPPORT are considered
608  * a potential security risk due to flat out ignoring the security-relevant
609  * taint flags. This being said, a perl without taint support compiled in
610  * has marginal run-time performance benefits.
611  * SILENT_NO_TAINT_SUPPORT implies NO_TAINT_SUPPORT.
612  * SILENT_NO_TAINT_SUPPORT is the same as NO_TAINT_SUPPORT except it
613  * silently ignores -t/-T instead of throwing an exception.
614  *
615  * DANGER! Using NO_TAINT_SUPPORT or SILENT_NO_TAINT_SUPPORT
616  *         voids your nonexistent warranty!
617  */
618 #if defined(SILENT_NO_TAINT_SUPPORT) && !defined(NO_TAINT_SUPPORT)
619 #  define NO_TAINT_SUPPORT 1
620 #endif
621
622 /* NO_TAINT_SUPPORT can be set to transform virtually all taint-related
623  * operations into no-ops for a very modest speed-up. Enable only if you
624  * know what you're doing: tests and CPAN modules' tests are bound to fail.
625  */
626 #ifdef NO_TAINT_SUPPORT
627 #   define TAINT                NOOP
628 #   define TAINT_NOT            NOOP
629 #   define TAINT_IF(c)          NOOP
630 #   define TAINT_ENV()          NOOP
631 #   define TAINT_PROPER(s)      NOOP
632 #   define TAINT_set(s)         NOOP
633 #   define TAINT_get            0
634 #   define TAINTING_get         0
635 #   define TAINTING_set(s)      NOOP
636 #   define TAINT_WARN_get       0
637 #   define TAINT_WARN_set(s)    NOOP
638 #else
639     /* Set to tainted if we are running under tainting mode */
640 #   define TAINT                (PL_tainted = PL_tainting)
641
642 #   define TAINT_NOT    (PL_tainted = FALSE)        /* Untaint */
643 #   define TAINT_IF(c)  if (UNLIKELY(c)) { TAINT; } /* Conditionally taint */
644 #   define TAINT_ENV()  if (UNLIKELY(PL_tainting)) { taint_env(); }
645                                 /* croak or warn if tainting */
646 #   define TAINT_PROPER(s)      if (UNLIKELY(PL_tainting)) {                \
647                                     taint_proper(NULL, s);                  \
648                                 }
649 #   define TAINT_set(s)         (PL_tainted = (s))
650 #   define TAINT_get            (PL_tainted)    /* Is something tainted? */
651 #   define TAINTING_get         (PL_tainting)   /* Is taint checking enabled? */
652 #   define TAINTING_set(s)      (PL_tainting = (s))
653 #   define TAINT_WARN_get       (PL_taint_warn) /* FALSE => tainting violations
654                                                             are fatal
655                                                    TRUE =>  they're just
656                                                             warnings */
657 #   define TAINT_WARN_set(s)    (PL_taint_warn = (s))
658 #endif
659
660 /* flags used internally only within pp_subst and pp_substcont */
661 #ifdef PERL_CORE
662 #  define SUBST_TAINT_STR      1        /* string tainted */
663 #  define SUBST_TAINT_PAT      2        /* pattern tainted */
664 #  define SUBST_TAINT_REPL     4        /* replacement tainted */
665 #  define SUBST_TAINT_RETAINT  8        /* use re'taint' in scope */
666 #  define SUBST_TAINT_BOOLRET 16        /* return is boolean (don't taint) */
667 #endif
668
669 /* XXX All process group stuff is handled in pp_sys.c.  Should these
670    defines move there?  If so, I could simplify this a lot. --AD  9/96.
671 */
672 /* Process group stuff changed from traditional BSD to POSIX.
673    perlfunc.pod documents the traditional BSD-style syntax, so we'll
674    try to preserve that, if possible.
675 */
676 #ifdef HAS_SETPGID
677 #  define BSD_SETPGRP(pid, pgrp)        setpgid((pid), (pgrp))
678 #elif defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
679 #  define BSD_SETPGRP(pid, pgrp)        setpgrp((pid), (pgrp))
680 #elif defined(HAS_SETPGRP2)
681 #  define BSD_SETPGRP(pid, pgrp)        setpgrp2((pid), (pgrp))
682 #endif
683 #if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
684 #  define HAS_SETPGRP  /* Well, effectively it does . . . */
685 #endif
686
687 /* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
688     our life easier :-) so we'll try it.
689 */
690 #ifdef HAS_GETPGID
691 #  define BSD_GETPGRP(pid)              getpgid((pid))
692 #elif defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
693 #  define BSD_GETPGRP(pid)              getpgrp((pid))
694 #elif defined(HAS_GETPGRP2)
695 #  define BSD_GETPGRP(pid)              getpgrp2((pid))
696 #endif
697 #if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
698 #  define HAS_GETPGRP  /* Well, effectively it does . . . */
699 #endif
700
701 /* These are not exact synonyms, since setpgrp() and getpgrp() may
702    have different behaviors, but perl.h used to define USE_BSDPGRP
703    (prior to 5.003_05) so some extension might depend on it.
704 */
705 #if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
706 #  ifndef USE_BSDPGRP
707 #    define USE_BSDPGRP
708 #  endif
709 #endif
710
711 /* HP-UX 10.X CMA (Common Multithreaded Architecture) insists that
712    pthread.h must be included before all other header files.
713 */
714 #if defined(USE_ITHREADS) && defined(PTHREAD_H_FIRST) && defined(I_PTHREAD)
715 #  include <pthread.h>
716 #endif
717
718 #include <sys/types.h>
719
720 # include <stdarg.h>
721
722 #ifdef I_STDINT
723 # include <stdint.h>
724 #endif
725
726 #include <ctype.h>
727 #include <float.h>
728 #include <limits.h>
729
730 #ifdef METHOD   /* Defined by OSF/1 v3.0 by ctype.h */
731 #undef METHOD
732 #endif
733
734 #ifdef PERL_MICRO
735 #   define NO_LOCALE
736 #endif
737
738 #ifdef I_LOCALE
739 #   include <locale.h>
740 #endif
741
742 #ifdef I_XLOCALE
743 #   include <xlocale.h>
744 #endif
745
746 /* If not forbidden, we enable locale handling if either 1) the POSIX 2008
747  * functions are available, or 2) just the setlocale() function.  This logic is
748  * repeated in t/loc_tools.pl and makedef.pl;  The three should be kept in
749  * sync. */
750 #if   ! defined(NO_LOCALE)
751
752 #  if ! defined(NO_POSIX_2008_LOCALE)           \
753    &&   defined(HAS_NEWLOCALE)                  \
754    &&   defined(HAS_USELOCALE)                  \
755    &&   defined(HAS_DUPLOCALE)                  \
756    &&   defined(HAS_FREELOCALE)                 \
757    &&   defined(LC_ALL_MASK)
758
759     /* For simplicity, the code is written to assume that any platform advanced
760      * enough to have the Posix 2008 locale functions has LC_ALL.  The final
761      * test above makes sure that assumption is valid */
762
763 #    define HAS_POSIX_2008_LOCALE
764 #    define USE_LOCALE
765 #  elif defined(HAS_SETLOCALE)
766 #    define USE_LOCALE
767 #  endif
768 #endif
769
770 #ifdef USE_LOCALE
771 #   define HAS_SKIP_LOCALE_INIT /* Solely for XS code to test for this
772                                    #define */
773 #   if !defined(NO_LOCALE_COLLATE) && defined(LC_COLLATE) \
774        && defined(HAS_STRXFRM)
775 #       define USE_LOCALE_COLLATE
776 #   endif
777 #   if !defined(NO_LOCALE_CTYPE) && defined(LC_CTYPE)
778 #       define USE_LOCALE_CTYPE
779 #   endif
780 #   if !defined(NO_LOCALE_NUMERIC) && defined(LC_NUMERIC)
781 #       define USE_LOCALE_NUMERIC
782 #   endif
783 #   if !defined(NO_LOCALE_MESSAGES) && defined(LC_MESSAGES)
784 #       define USE_LOCALE_MESSAGES
785 #   endif
786 #   if !defined(NO_LOCALE_MONETARY) && defined(LC_MONETARY)
787 #       define USE_LOCALE_MONETARY
788 #   endif
789 #   if !defined(NO_LOCALE_TIME) && defined(LC_TIME)
790 #       define USE_LOCALE_TIME
791 #   endif
792 #   if !defined(NO_LOCALE_ADDRESS) && defined(LC_ADDRESS)
793 #       define USE_LOCALE_ADDRESS
794 #   endif
795 #   if !defined(NO_LOCALE_IDENTIFICATION) && defined(LC_IDENTIFICATION)
796 #       define USE_LOCALE_IDENTIFICATION
797 #   endif
798 #   if !defined(NO_LOCALE_MEASUREMENT) && defined(LC_MEASUREMENT)
799 #       define USE_LOCALE_MEASUREMENT
800 #   endif
801 #   if !defined(NO_LOCALE_PAPER) && defined(LC_PAPER)
802 #       define USE_LOCALE_PAPER
803 #   endif
804 #   if !defined(NO_LOCALE_TELEPHONE) && defined(LC_TELEPHONE)
805 #       define USE_LOCALE_TELEPHONE
806 #   endif
807
808 /* XXX The next few defines are unfortunately duplicated in makedef.pl, and
809  * changes here MUST also be made there */
810
811 #  if ! defined(HAS_SETLOCALE) && defined(HAS_POSIX_2008_LOCALE)
812 #      define USE_POSIX_2008_LOCALE
813 #      ifndef USE_THREAD_SAFE_LOCALE
814 #        define USE_THREAD_SAFE_LOCALE
815 #      endif
816                                    /* If compiled with
817                                     * -DUSE_THREAD_SAFE_LOCALE, will do so even
818                                     * on unthreaded builds */
819 #  elif   (defined(USE_ITHREADS) || defined(USE_THREAD_SAFE_LOCALE))         \
820        && (    defined(HAS_POSIX_2008_LOCALE)                                \
821            || (defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400))     \
822        && ! defined(NO_THREAD_SAFE_LOCALE)
823 #    ifndef USE_THREAD_SAFE_LOCALE
824 #      define USE_THREAD_SAFE_LOCALE
825 #    endif
826 #    ifdef HAS_POSIX_2008_LOCALE
827 #      define USE_POSIX_2008_LOCALE
828 #    endif
829 #  endif
830 #endif
831
832 /*  Microsoft documentation reads in the change log for VS 2015:
833  *     "The localeconv function declared in locale.h now works correctly when
834  *     per-thread locale is enabled. In previous versions of the library, this
835  *     function would return the lconv data for the global locale, not the
836  *     thread's locale."
837  */
838 #if defined(WIN32) && defined(USE_THREAD_SAFE_LOCALE) && _MSC_VER < 1900
839 #  define TS_W32_BROKEN_LOCALECONV
840 #endif
841
842 #include <setjmp.h>
843
844 #ifdef I_SYS_PARAM
845 #   ifdef PARAM_NEEDS_TYPES
846 #       include <sys/types.h>
847 #   endif
848 #   include <sys/param.h>
849 #endif
850
851 /* On BSD-derived systems, <sys/param.h> defines BSD to a year-month
852    value something like 199306.  This may be useful if no more-specific
853    feature test is available.
854 */
855 #if defined(BSD)
856 #   ifndef BSDish
857 #       define BSDish
858 #   endif
859 #endif
860
861 /* Use all the "standard" definitions */
862 #include <stdlib.h>
863
864 /* If this causes problems, set i_unistd=undef in the hint file.  */
865 #ifdef I_UNISTD
866 #    if defined(__amigaos4__)
867 #        ifdef I_NETINET_IN
868 #            include <netinet/in.h>
869 #        endif
870 #   endif
871 #   include <unistd.h>
872 #   if defined(__amigaos4__)
873 /* Under AmigaOS 4 newlib.library provides an environ.  However using
874  * it doesn't give us enough control over inheritance of variables by
875  * subshells etc. so replace with custom version based on abc-shell
876  * code. */
877 extern char **myenviron;
878 #       undef environ
879 #       define environ myenviron
880 #   endif
881 #endif
882
883 /* for WCOREDUMP */
884 #ifdef I_SYS_WAIT
885 #   include <sys/wait.h>
886 #endif
887
888 #ifdef __SYMBIAN32__
889 #   undef _SC_ARG_MAX /* Symbian has _SC_ARG_MAX but no sysconf() */
890 #endif
891
892 #if defined(HAS_SYSCALL) && !defined(HAS_SYSCALL_PROTO)
893 EXTERN_C int syscall(int, ...);
894 #endif
895
896 #if defined(HAS_USLEEP) && !defined(HAS_USLEEP_PROTO)
897 EXTERN_C int usleep(unsigned int);
898 #endif
899
900 /* macros for correct constant construction.  These are in C99 <stdint.h>
901  * (so they will not be available in strict C89 mode), but they are nice, so
902  * let's define them if necessary. */
903 #ifndef UINT16_C
904 #  if INTSIZE >= 2
905 #    define UINT16_C(x) ((U16_TYPE)x##U)
906 #  else
907 #    define UINT16_C(x) ((U16_TYPE)x##UL)
908 #  endif
909 #endif
910
911 #ifndef UINT32_C
912 #  if INTSIZE >= 4
913 #    define UINT32_C(x) ((U32_TYPE)x##U)
914 #  else
915 #    define UINT32_C(x) ((U32_TYPE)x##UL)
916 #  endif
917 #endif
918
919 #ifdef I_STDINT
920     typedef intmax_t  PERL_INTMAX_T;
921     typedef uintmax_t PERL_UINTMAX_T;
922 #endif
923
924 /* N.B.  We use QUADKIND here instead of HAS_QUAD here, because that doesn't
925  * actually mean what it has always been documented to mean (see RT #119753)
926  * and is explicitly turned off outside of core with dire warnings about
927  * removing the undef. */
928
929 #if defined(QUADKIND)
930 #  undef PeRl_INT64_C
931 #  undef PeRl_UINT64_C
932 /* Prefer the native integer types (int and long) over long long
933  * (which is not C89) and Win32-specific __int64. */
934 #  if QUADKIND == QUAD_IS_INT && INTSIZE == 8
935 #    define PeRl_INT64_C(c)     (c)
936 #    define PeRl_UINT64_C(c)    CAT2(c,U)
937 #  endif
938 #  if QUADKIND == QUAD_IS_LONG && LONGSIZE == 8
939 #    define PeRl_INT64_C(c)     CAT2(c,L)
940 #    define PeRl_UINT64_C(c)    CAT2(c,UL)
941 #  endif
942 #  if QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LONG_LONG)
943 #    define PeRl_INT64_C(c)     CAT2(c,LL)
944 #    define PeRl_UINT64_C(c)    CAT2(c,ULL)
945 #  endif
946 #  if QUADKIND == QUAD_IS___INT64
947 #    define PeRl_INT64_C(c)     CAT2(c,I64)
948 #    define PeRl_UINT64_C(c)    CAT2(c,UI64)
949 #  endif
950 #  ifndef PeRl_INT64_C
951 #    define PeRl_INT64_C(c)     ((I64)(c)) /* last resort */
952 #    define PeRl_UINT64_C(c)    ((U64TYPE)(c))
953 #  endif
954 /* In OS X the INT64_C/UINT64_C are defined with LL/ULL, which will
955  * not fly with C89-pedantic gcc, so let's undefine them first so that
956  * we can redefine them with our native integer preferring versions. */
957 #  if defined(PERL_DARWIN) && defined(PERL_GCC_PEDANTIC)
958 #    undef INT64_C
959 #    undef UINT64_C
960 #  endif
961 #  ifndef INT64_C
962 #    define INT64_C(c) PeRl_INT64_C(c)
963 #  endif
964 #  ifndef UINT64_C
965 #    define UINT64_C(c) PeRl_UINT64_C(c)
966 #  endif
967
968 #  ifndef I_STDINT
969     typedef I64TYPE PERL_INTMAX_T;
970     typedef U64TYPE PERL_UINTMAX_T;
971 #  endif
972 #  ifndef INTMAX_C
973 #    define INTMAX_C(c) INT64_C(c)
974 #  endif
975 #  ifndef UINTMAX_C
976 #    define UINTMAX_C(c) UINT64_C(c)
977 #  endif
978
979 #else  /* below QUADKIND is undefined */
980
981 /* Perl doesn't work on 16 bit systems, so must be 32 bit */
982 #  ifndef I_STDINT
983     typedef I32TYPE PERL_INTMAX_T;
984     typedef U32TYPE PERL_UINTMAX_T;
985 #  endif
986 #  ifndef INTMAX_C
987 #    define INTMAX_C(c) INT32_C(c)
988 #  endif
989 #  ifndef UINTMAX_C
990 #    define UINTMAX_C(c) UINT32_C(c)
991 #  endif
992
993 #endif  /* no QUADKIND */
994
995 #ifdef PERL_CORE
996
997 /* byte-swapping functions for big-/little-endian conversion */
998 # define _swab_16_(x) ((U16)( \
999          (((U16)(x) & UINT16_C(0x00ff)) << 8) | \
1000          (((U16)(x) & UINT16_C(0xff00)) >> 8) ))
1001
1002 # define _swab_32_(x) ((U32)( \
1003          (((U32)(x) & UINT32_C(0x000000ff)) << 24) | \
1004          (((U32)(x) & UINT32_C(0x0000ff00)) <<  8) | \
1005          (((U32)(x) & UINT32_C(0x00ff0000)) >>  8) | \
1006          (((U32)(x) & UINT32_C(0xff000000)) >> 24) ))
1007
1008 # ifdef HAS_QUAD
1009 #  define _swab_64_(x) ((U64)( \
1010           (((U64)(x) & UINT64_C(0x00000000000000ff)) << 56) | \
1011           (((U64)(x) & UINT64_C(0x000000000000ff00)) << 40) | \
1012           (((U64)(x) & UINT64_C(0x0000000000ff0000)) << 24) | \
1013           (((U64)(x) & UINT64_C(0x00000000ff000000)) <<  8) | \
1014           (((U64)(x) & UINT64_C(0x000000ff00000000)) >>  8) | \
1015           (((U64)(x) & UINT64_C(0x0000ff0000000000)) >> 24) | \
1016           (((U64)(x) & UINT64_C(0x00ff000000000000)) >> 40) | \
1017           (((U64)(x) & UINT64_C(0xff00000000000000)) >> 56) ))
1018 # endif
1019
1020 /* The old value was hard coded at 1008. (4096-16) seems to be a bit faster,
1021    at least on FreeBSD.  YMMV, so experiment.  */
1022 #ifndef PERL_ARENA_SIZE
1023 #define PERL_ARENA_SIZE 4080
1024 #endif
1025
1026 /* Maximum level of recursion */
1027 #ifndef PERL_SUB_DEPTH_WARN
1028 #define PERL_SUB_DEPTH_WARN 100
1029 #endif
1030
1031 #endif /* PERL_CORE */
1032
1033 /* Maximum number of args that may be passed to an OP_MULTICONCAT op.
1034  * It determines the size of local arrays in S_maybe_multiconcat() and
1035  * pp_multiconcat().
1036  */
1037 #define PERL_MULTICONCAT_MAXARG 64
1038
1039 /* The indexes of fields of a multiconcat aux struct.
1040  * The fixed fields are followed by nargs+1 const segment lengths,
1041  * and if utf8 and non-utf8 differ, a second nargs+1 set for utf8.
1042  */
1043
1044 #define PERL_MULTICONCAT_IX_NARGS     0 /* number of arguments */
1045 #define PERL_MULTICONCAT_IX_PLAIN_PV  1 /* non-utf8 constant string */
1046 #define PERL_MULTICONCAT_IX_PLAIN_LEN 2 /* non-utf8 constant string length */
1047 #define PERL_MULTICONCAT_IX_UTF8_PV   3 /* utf8 constant string */
1048 #define PERL_MULTICONCAT_IX_UTF8_LEN  4 /* utf8 constant string length */
1049 #define PERL_MULTICONCAT_IX_LENGTHS   5 /* first of nargs+1 const segment lens */
1050 #define PERL_MULTICONCAT_HEADER_SIZE 5 /* The number of fields of a
1051                                            multiconcat header */
1052
1053 /* We no longer default to creating a new SV for GvSV.
1054    Do this before embed.  */
1055 #ifndef PERL_CREATE_GVSV
1056 #  ifndef PERL_DONT_CREATE_GVSV
1057 #    define PERL_DONT_CREATE_GVSV
1058 #  endif
1059 #endif
1060
1061 #if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
1062 #define PERL_USES_PL_PIDSTATUS
1063 #endif
1064
1065 #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(__SYMBIAN32__)
1066 #define PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1067 #endif
1068
1069 #define MEM_SIZE Size_t
1070
1071 /* Round all values passed to malloc up, by default to a multiple of
1072    sizeof(size_t)
1073 */
1074 #ifndef PERL_STRLEN_ROUNDUP_QUANTUM
1075 #define PERL_STRLEN_ROUNDUP_QUANTUM Size_t_size
1076 #endif
1077
1078 /* sv_grow() will expand strings by at least a certain percentage of
1079    the previously *used* length to avoid excessive calls to realloc().
1080    The default is 25% of the current length.
1081 */
1082 #ifndef PERL_STRLEN_EXPAND_SHIFT
1083 #  define PERL_STRLEN_EXPAND_SHIFT 2
1084 #endif
1085
1086 /* This use of offsetof() requires /Zc:offsetof- for VS2017 (and presumably
1087  * onwards) when building Socket.xs, but we can just use a different definition
1088  * for STRUCT_OFFSET instead. */
1089 #if defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1910
1090 #  define STRUCT_OFFSET(s,m)  (Size_t)(&(((s *)0)->m))
1091 #else
1092 #  include <stddef.h>
1093 #  define STRUCT_OFFSET(s,m)  offsetof(s,m)
1094 #endif
1095
1096 /* ptrdiff_t is C11, so undef it under pedantic builds.  (Actually it is
1097  * in C89, but apparently there are platforms where it doesn't exist.  See
1098  * thread beginning at http://nntp.perl.org/group/perl.perl5.porters/251541.)
1099  * */
1100 #ifdef PERL_GCC_PEDANTIC
1101 #   undef HAS_PTRDIFF_T
1102 #endif
1103
1104 #ifdef HAS_PTRDIFF_T
1105 #  define Ptrdiff_t ptrdiff_t
1106 #else
1107 #  define Ptrdiff_t SSize_t
1108 #endif
1109
1110 #ifndef __SYMBIAN32__
1111 #  include <string.h>
1112 #endif
1113
1114 /* This comes after <stdlib.h> so we don't try to change the standard
1115  * library prototypes; we'll use our own in proto.h instead. */
1116
1117 #ifdef MYMALLOC
1118 #  ifdef PERL_POLLUTE_MALLOC
1119 #   ifndef PERL_EXTMALLOC_DEF
1120 #    define Perl_malloc         malloc
1121 #    define Perl_calloc         calloc
1122 #    define Perl_realloc        realloc
1123 #    define Perl_mfree          free
1124 #   endif
1125 #  else
1126 #    define EMBEDMYMALLOC       /* for compatibility */
1127 #  endif
1128
1129 #  define safemalloc  Perl_malloc
1130 #  define safecalloc  Perl_calloc
1131 #  define saferealloc Perl_realloc
1132 #  define safefree    Perl_mfree
1133 #  define CHECK_MALLOC_TOO_LATE_FOR_(code)      STMT_START {            \
1134         if (!TAINTING_get && MallocCfg_ptr[MallocCfg_cfg_env_read])     \
1135                 code;                                                   \
1136     } STMT_END
1137 #  define CHECK_MALLOC_TOO_LATE_FOR(ch)                         \
1138         CHECK_MALLOC_TOO_LATE_FOR_(MALLOC_TOO_LATE_FOR(ch))
1139 #  define panic_write2(s)               write(2, s, strlen(s))
1140 #  define CHECK_MALLOC_TAINT(newval)                            \
1141         CHECK_MALLOC_TOO_LATE_FOR_(                             \
1142                 if (newval) {                                   \
1143                   PERL_UNUSED_RESULT(panic_write2("panic: tainting with $ENV{PERL_MALLOC_OPT}\n"));\
1144                   exit(1); })
1145 #  define MALLOC_CHECK_TAINT(argc,argv,env)     STMT_START {    \
1146         if (doing_taint(argc,argv,env)) {                       \
1147                 MallocCfg_ptr[MallocCfg_skip_cfg_env] = 1;      \
1148     }} STMT_END;
1149 #else  /* MYMALLOC */
1150 #  define safemalloc  safesysmalloc
1151 #  define safecalloc  safesyscalloc
1152 #  define saferealloc safesysrealloc
1153 #  define safefree    safesysfree
1154 #  define CHECK_MALLOC_TOO_LATE_FOR(ch)         ((void)0)
1155 #  define CHECK_MALLOC_TAINT(newval)            ((void)0)
1156 #  define MALLOC_CHECK_TAINT(argc,argv,env)
1157 #endif /* MYMALLOC */
1158
1159 /* diag_listed_as: "-T" is on the #! line, it must also be used on the command line */
1160 #define TOO_LATE_FOR_(ch,what)  Perl_croak(aTHX_ "\"-%c\" is on the #! line, it must also be used on the command line%s", (char)(ch), what)
1161 #define TOO_LATE_FOR(ch)        TOO_LATE_FOR_(ch, "")
1162 #define MALLOC_TOO_LATE_FOR(ch) TOO_LATE_FOR_(ch, " with $ENV{PERL_MALLOC_OPT}")
1163 #define MALLOC_CHECK_TAINT2(argc,argv)  MALLOC_CHECK_TAINT(argc,argv,NULL)
1164
1165 #ifndef memzero
1166 #   define memzero(d,l) memset(d,0,l)
1167 #endif
1168
1169 #ifdef I_NETINET_IN
1170 #   include <netinet/in.h>
1171 #endif
1172
1173 #ifdef I_ARPA_INET
1174 #   include <arpa/inet.h>
1175 #endif
1176
1177 #ifdef I_SYS_STAT
1178 #   include <sys/stat.h>
1179 #endif
1180
1181 /* Microsoft VC's sys/stat.h defines all S_Ixxx macros except S_IFIFO.
1182    This definition should ideally go into win32/win32.h, but S_IFIFO is
1183    used later here in perl.h before win32/win32.h is being included. */
1184 #if !defined(S_IFIFO) && defined(_S_IFIFO)
1185 #   define S_IFIFO _S_IFIFO
1186 #endif
1187
1188 /* The stat macros for Unisoft System V/88 (and derivatives
1189    like UTekV) are broken, sometimes giving false positives.  Undefine
1190    them here and let the code below set them to proper values.
1191
1192    The ghs macro stands for GreenHills Software C-1.8.5 which
1193    is the C compiler for sysV88 and the various derivatives.
1194    This header file bug is corrected in gcc-2.5.8 and later versions.
1195    --Kaveh Ghazi (ghazi@noc.rutgers.edu) 10/3/94.  */
1196
1197 #if defined(m88k) && defined(ghs)
1198 #   undef S_ISDIR
1199 #   undef S_ISCHR
1200 #   undef S_ISBLK
1201 #   undef S_ISREG
1202 #   undef S_ISFIFO
1203 #   undef S_ISLNK
1204 #endif
1205
1206 #include <time.h>
1207
1208 #ifdef I_SYS_TIME
1209 #   ifdef I_SYS_TIME_KERNEL
1210 #       define KERNEL
1211 #   endif
1212 #   include <sys/time.h>
1213 #   ifdef I_SYS_TIME_KERNEL
1214 #       undef KERNEL
1215 #   endif
1216 #endif
1217
1218 #if defined(HAS_TIMES) && defined(I_SYS_TIMES)
1219 #    include <sys/times.h>
1220 #endif
1221
1222 #include <errno.h>
1223
1224 #if defined(WIN32) && defined(PERL_IMPLICIT_SYS)
1225 #  define WIN32SCK_IS_STDSCK            /* don't pull in custom wsock layer */
1226 #endif
1227
1228 #if defined(HAS_SOCKET) && !defined(WIN32) /* WIN32 handles sockets via win32.h */
1229 # include <sys/socket.h>
1230 # if defined(USE_SOCKS) && defined(I_SOCKS)
1231 #   if !defined(INCLUDE_PROTOTYPES)
1232 #       define INCLUDE_PROTOTYPES /* for <socks.h> */
1233 #       define PERL_SOCKS_NEED_PROTOTYPES
1234 #   endif
1235 #   include <socks.h>
1236 #   ifdef PERL_SOCKS_NEED_PROTOTYPES /* keep cpp space clean */
1237 #       undef INCLUDE_PROTOTYPES
1238 #       undef PERL_SOCKS_NEED_PROTOTYPES
1239 #   endif
1240 # endif
1241 # ifdef I_NETDB
1242 #  ifdef NETWARE
1243 #   include<stdio.h>
1244 #  endif
1245 #  include <netdb.h>
1246 # endif
1247 # ifndef ENOTSOCK
1248 #  ifdef I_NET_ERRNO
1249 #   include <net/errno.h>
1250 #  endif
1251 # endif
1252 #endif
1253
1254 /* sockatmark() is so new (2001) that many places might have it hidden
1255  * behind some -D_BLAH_BLAH_SOURCE guard.  The __THROW magic is required
1256  * e.g. in Gentoo, see http://bugs.gentoo.org/show_bug.cgi?id=12605 */
1257 #if defined(HAS_SOCKATMARK) && !defined(HAS_SOCKATMARK_PROTO)
1258 # if defined(__THROW) && defined(__GLIBC__)
1259 int sockatmark(int) __THROW;
1260 # else
1261 int sockatmark(int);
1262 # endif
1263 #endif
1264
1265 #if defined(__osf__) && defined(__cplusplus) && !defined(_XOPEN_SOURCE_EXTENDED) /* Tru64 "cxx" (C++), see hints/dec_osf.sh for why the _XOPEN_SOURCE_EXTENDED cannot be defined. */
1266 EXTERN_C int fchdir(int);
1267 EXTERN_C int flock(int, int);
1268 EXTERN_C int fseeko(FILE *, off_t, int);
1269 EXTERN_C off_t ftello(FILE *);
1270 #endif
1271
1272 #if defined(__SUNPRO_CC) /* SUNWspro CC (C++) */
1273 EXTERN_C char *crypt(const char *, const char *);
1274 #endif
1275
1276 #if defined(__cplusplus) && defined(__CYGWIN__)
1277 EXTERN_C char *crypt(const char *, const char *);
1278 #endif
1279
1280 /*
1281 =head1 Errno
1282
1283 =for apidoc m|void|SETERRNO|int errcode|int vmserrcode
1284
1285 Set C<errno>, and on VMS set C<vaxc$errno>.
1286
1287 =for apidoc mn|void|dSAVEDERRNO
1288
1289 Declare variables needed to save C<errno> and any operating system
1290 specific error number.
1291
1292 =for apidoc mn|void|dSAVE_ERRNO
1293
1294 Declare variables needed to save C<errno> and any operating system
1295 specific error number, and save them for optional later restoration
1296 by C<RESTORE_ERRNO>.
1297
1298 =for apidoc mn|void|SAVE_ERRNO
1299
1300 Save C<errno> and any operating system specific error number for
1301 optional later restoration by C<RESTORE_ERRNO>.  Requires
1302 C<dSAVEDERRNO> or C<dSAVE_ERRNO> in scope.
1303
1304 =for apidoc mn|void|RESTORE_ERRNO
1305
1306 Restore C<errno> and any operating system specific error number that
1307 was saved by C<dSAVE_ERRNO> or C<RESTORE_ERRNO>.
1308
1309 =cut
1310 */
1311
1312 #ifdef SETERRNO
1313 # undef SETERRNO  /* SOCKS might have defined this */
1314 #endif
1315
1316 #ifdef VMS
1317 #   define SETERRNO(errcode,vmserrcode) \
1318         STMT_START {                    \
1319             set_errno(errcode);         \
1320             set_vaxc_errno(vmserrcode); \
1321         } STMT_END
1322 #   define dSAVEDERRNO    int saved_errno; unsigned saved_vms_errno
1323 #   define dSAVE_ERRNO    int saved_errno = errno; unsigned saved_vms_errno = vaxc$errno
1324 #   define SAVE_ERRNO     ( saved_errno = errno, saved_vms_errno = vaxc$errno )
1325 #   define RESTORE_ERRNO  SETERRNO(saved_errno, saved_vms_errno)
1326
1327 #   define LIB_INVARG           LIB$_INVARG
1328 #   define RMS_DIR              RMS$_DIR
1329 #   define RMS_FAC              RMS$_FAC
1330 #   define RMS_FEX              RMS$_FEX
1331 #   define RMS_FNF              RMS$_FNF
1332 #   define RMS_IFI              RMS$_IFI
1333 #   define RMS_ISI              RMS$_ISI
1334 #   define RMS_PRV              RMS$_PRV
1335 #   define SS_ACCVIO            SS$_ACCVIO
1336 #   define SS_DEVOFFLINE        SS$_DEVOFFLINE
1337 #   define SS_IVCHAN            SS$_IVCHAN
1338 #   define SS_NORMAL            SS$_NORMAL
1339 #   define SS_NOPRIV            SS$_NOPRIV
1340 #   define SS_BUFFEROVF         SS$_BUFFEROVF
1341 #else
1342 #   define LIB_INVARG           0
1343 #   define RMS_DIR              0
1344 #   define RMS_FAC              0
1345 #   define RMS_FEX              0
1346 #   define RMS_FNF              0
1347 #   define RMS_IFI              0
1348 #   define RMS_ISI              0
1349 #   define RMS_PRV              0
1350 #   define SS_ACCVIO            0
1351 #   define SS_DEVOFFLINE        0
1352 #   define SS_IVCHAN            0
1353 #   define SS_NORMAL            0
1354 #   define SS_NOPRIV            0
1355 #   define SS_BUFFEROVF         0
1356 #endif
1357
1358 #ifdef WIN32
1359 #   define dSAVEDERRNO  int saved_errno; DWORD saved_win32_errno
1360 #   define dSAVE_ERRNO  int saved_errno = errno; DWORD saved_win32_errno = GetLastError()
1361 #   define SAVE_ERRNO   ( saved_errno = errno, saved_win32_errno = GetLastError() )
1362 #   define RESTORE_ERRNO ( errno = saved_errno, SetLastError(saved_win32_errno) )
1363 #endif
1364
1365 #ifdef OS2
1366 #   define dSAVEDERRNO  int saved_errno; unsigned long saved_os2_errno
1367 #   define dSAVE_ERRNO  int saved_errno = errno; unsigned long saved_os2_errno = Perl_rc
1368 #   define SAVE_ERRNO   ( saved_errno = errno, saved_os2_errno = Perl_rc )
1369 #   define RESTORE_ERRNO ( errno = saved_errno, Perl_rc = saved_os2_errno )
1370 #endif
1371
1372 #ifndef SETERRNO
1373 #   define SETERRNO(errcode,vmserrcode) (errno = (errcode))
1374 #endif
1375
1376 #ifndef dSAVEDERRNO
1377 #   define dSAVEDERRNO    int saved_errno
1378 #   define dSAVE_ERRNO    int saved_errno = errno
1379 #   define SAVE_ERRNO     (saved_errno = errno)
1380 #   define RESTORE_ERRNO  (errno = saved_errno)
1381 #endif
1382
1383 /*
1384 =head1 Warning and Dieing
1385
1386 =for apidoc Amn|SV *|ERRSV
1387
1388 Returns the SV for C<$@>, creating it if needed.
1389
1390 =for apidoc Am|void|CLEAR_ERRSV
1391
1392 Clear the contents of C<$@>, setting it to the empty string.
1393
1394 This replaces any read-only SV with a fresh SV and removes any magic.
1395
1396 =for apidoc Am|void|SANE_ERRSV
1397
1398 Clean up ERRSV so we can safely set it.
1399
1400 This replaces any read-only SV with a fresh writable copy and removes
1401 any magic.
1402
1403 =cut
1404 */
1405
1406 #define ERRSV GvSVn(PL_errgv)
1407
1408 /* contains inlined gv_add_by_type */
1409 #define CLEAR_ERRSV() STMT_START {                                      \
1410     SV ** const svp = &GvSV(PL_errgv);                                  \
1411     if (!*svp) {                                                        \
1412         *svp = newSVpvs("");                                            \
1413     } else if (SvREADONLY(*svp)) {                                      \
1414         SvREFCNT_dec_NN(*svp);                                          \
1415         *svp = newSVpvs("");                                            \
1416     } else {                                                            \
1417         SV *const errsv = *svp;                                         \
1418         SvPVCLEAR(errsv);                                               \
1419         SvPOK_only(errsv);                                              \
1420         if (SvMAGICAL(errsv)) {                                         \
1421             mg_free(errsv);                                             \
1422         }                                                               \
1423     }                                                                   \
1424     } STMT_END
1425
1426 /* contains inlined gv_add_by_type */
1427 #define SANE_ERRSV() STMT_START {                                       \
1428     SV ** const svp = &GvSV(PL_errgv);                                  \
1429     if (!*svp) {                                                        \
1430         *svp = newSVpvs("");                                            \
1431     } else if (SvREADONLY(*svp)) {                                      \
1432         SV *dupsv = newSVsv(*svp);                                      \
1433         SvREFCNT_dec_NN(*svp);                                          \
1434         *svp = dupsv;                                                   \
1435     } else {                                                            \
1436         SV *const errsv = *svp;                                         \
1437         if (SvMAGICAL(errsv)) {                                         \
1438             mg_free(errsv);                                             \
1439         }                                                               \
1440     }                                                                   \
1441     } STMT_END
1442
1443
1444 #ifdef PERL_CORE
1445 # define DEFSV (0 + GvSVn(PL_defgv))
1446 # define DEFSV_set(sv) \
1447     (SvREFCNT_dec(GvSV(PL_defgv)), GvSV(PL_defgv) = SvREFCNT_inc(sv))
1448 # define SAVE_DEFSV                \
1449     (                               \
1450         save_gp(PL_defgv, 0),        \
1451         GvINTRO_off(PL_defgv),        \
1452         SAVEGENERICSV(GvSV(PL_defgv)), \
1453         GvSV(PL_defgv) = NULL           \
1454     )
1455 #else
1456 # define DEFSV GvSVn(PL_defgv)
1457 # define DEFSV_set(sv) (GvSV(PL_defgv) = (sv))
1458 # define SAVE_DEFSV SAVESPTR(GvSV(PL_defgv))
1459 #endif
1460
1461 #ifndef errno
1462         extern int errno;     /* ANSI allows errno to be an lvalue expr.
1463                                * For example in multithreaded environments
1464                                * something like this might happen:
1465                                * extern int *_errno(void);
1466                                * #define errno (*_errno()) */
1467 #endif
1468
1469 #define UNKNOWN_ERRNO_MSG "(unknown)"
1470
1471 #if VMS
1472 #define Strerror(e) strerror((e), vaxc$errno)
1473 #else
1474 #define Strerror(e) strerror(e)
1475 #endif
1476
1477 #ifdef I_SYS_IOCTL
1478 #   ifndef _IOCTL_
1479 #       include <sys/ioctl.h>
1480 #   endif
1481 #endif
1482
1483 #if defined(mc300) || defined(mc500) || defined(mc700) || defined(mc6000)
1484 #   ifdef HAS_SOCKETPAIR
1485 #       undef HAS_SOCKETPAIR
1486 #   endif
1487 #   ifdef I_NDBM
1488 #       undef I_NDBM
1489 #   endif
1490 #endif
1491
1492 #ifndef HAS_SOCKETPAIR
1493 #   ifdef HAS_SOCKET
1494 #       define socketpair Perl_my_socketpair
1495 #   endif
1496 #endif
1497
1498 #if INTSIZE == 2
1499 #   define htoni htons
1500 #   define ntohi ntohs
1501 #else
1502 #   define htoni htonl
1503 #   define ntohi ntohl
1504 #endif
1505
1506 /* Configure already sets Direntry_t */
1507 #if defined(I_DIRENT)
1508 #  include <dirent.h>
1509 #elif defined(I_SYS_NDIR)
1510 #  include <sys/ndir.h>
1511 #elif defined(I_SYS_DIR)
1512 #  include <sys/dir.h>
1513 #endif
1514
1515 /*
1516  * The following gobbledygook brought to you on behalf of __STDC__.
1517  * (I could just use #ifndef __STDC__, but this is more bulletproof
1518  * in the face of half-implementations.)
1519  */
1520
1521 #if defined(I_SYSMODE)
1522 #include <sys/mode.h>
1523 #endif
1524
1525 #ifndef S_IFMT
1526 #   ifdef _S_IFMT
1527 #       define S_IFMT _S_IFMT
1528 #   else
1529 #       define S_IFMT 0170000
1530 #   endif
1531 #endif
1532
1533 #ifndef S_ISDIR
1534 #   define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
1535 #endif
1536
1537 #ifndef S_ISCHR
1538 #   define S_ISCHR(m) ((m & S_IFMT) == S_IFCHR)
1539 #endif
1540
1541 #ifndef S_ISBLK
1542 #   ifdef S_IFBLK
1543 #       define S_ISBLK(m) ((m & S_IFMT) == S_IFBLK)
1544 #   else
1545 #       define S_ISBLK(m) (0)
1546 #   endif
1547 #endif
1548
1549 #ifndef S_ISREG
1550 #   define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
1551 #endif
1552
1553 #ifndef S_ISFIFO
1554 #   ifdef S_IFIFO
1555 #       define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
1556 #   else
1557 #       define S_ISFIFO(m) (0)
1558 #   endif
1559 #endif
1560
1561 #ifndef S_ISLNK
1562 #  ifdef _S_ISLNK
1563 #    define S_ISLNK(m) _S_ISLNK(m)
1564 #  elif defined(_S_IFLNK)
1565 #    define S_ISLNK(m) ((m & S_IFMT) == _S_IFLNK)
1566 #  elif defined(S_IFLNK)
1567 #    define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
1568 #  else
1569 #    define S_ISLNK(m) (0)
1570 #  endif
1571 #endif
1572
1573 #ifndef S_ISSOCK
1574 #  ifdef _S_ISSOCK
1575 #    define S_ISSOCK(m) _S_ISSOCK(m)
1576 #  elif defined(_S_IFSOCK)
1577 #    define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
1578 #  elif defined(S_IFSOCK)
1579 #    define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
1580 #  else
1581 #    define S_ISSOCK(m) (0)
1582 #  endif
1583 #endif
1584
1585 #ifndef S_IRUSR
1586 #   ifdef S_IREAD
1587 #       define S_IRUSR S_IREAD
1588 #       define S_IWUSR S_IWRITE
1589 #       define S_IXUSR S_IEXEC
1590 #   else
1591 #       define S_IRUSR 0400
1592 #       define S_IWUSR 0200
1593 #       define S_IXUSR 0100
1594 #   endif
1595 #endif
1596
1597 #ifndef S_IRGRP
1598 #   ifdef S_IRUSR
1599 #       define S_IRGRP (S_IRUSR>>3)
1600 #       define S_IWGRP (S_IWUSR>>3)
1601 #       define S_IXGRP (S_IXUSR>>3)
1602 #   else
1603 #       define S_IRGRP 0040
1604 #       define S_IWGRP 0020
1605 #       define S_IXGRP 0010
1606 #   endif
1607 #endif
1608
1609 #ifndef S_IROTH
1610 #   ifdef S_IRUSR
1611 #       define S_IROTH (S_IRUSR>>6)
1612 #       define S_IWOTH (S_IWUSR>>6)
1613 #       define S_IXOTH (S_IXUSR>>6)
1614 #   else
1615 #       define S_IROTH 0040
1616 #       define S_IWOTH 0020
1617 #       define S_IXOTH 0010
1618 #   endif
1619 #endif
1620
1621 #ifndef S_ISUID
1622 #   define S_ISUID 04000
1623 #endif
1624
1625 #ifndef S_ISGID
1626 #   define S_ISGID 02000
1627 #endif
1628
1629 #ifndef S_IRWXU
1630 #   define S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
1631 #endif
1632
1633 #ifndef S_IRWXG
1634 #   define S_IRWXG (S_IRGRP|S_IWGRP|S_IXGRP)
1635 #endif
1636
1637 #ifndef S_IRWXO
1638 #   define S_IRWXO (S_IROTH|S_IWOTH|S_IXOTH)
1639 #endif
1640
1641 /* Haiku R1 seems to define S_IREAD and S_IWRITE in <posix/fcntl.h>
1642  * which would get included through <sys/file.h >, but that is 3000
1643  * lines in the future.  --jhi */
1644
1645 #if !defined(S_IREAD) && !defined(__HAIKU__)
1646 #   define S_IREAD S_IRUSR
1647 #endif
1648
1649 #if !defined(S_IWRITE) && !defined(__HAIKU__)
1650 #   define S_IWRITE S_IWUSR
1651 #endif
1652
1653 #ifndef S_IEXEC
1654 #   define S_IEXEC S_IXUSR
1655 #endif
1656
1657 #if defined(cray) || defined(gould) || defined(i860) || defined(pyr)
1658 #   define SLOPPYDIVIDE
1659 #endif
1660
1661 #ifdef UV
1662 #undef UV
1663 #endif
1664
1665 /* This used to be conditionally defined based on whether we had a sprintf()
1666  * that correctly returns the string length (as required by C89), but we no
1667  * longer need that. XS modules can (and do) use this name, so it must remain
1668  * a part of the API that's visible to modules.
1669
1670 =head1 Miscellaneous Functions
1671
1672 =for apidoc ATmD|int|my_sprintf|NN char *buffer|NN const char *pat|...
1673
1674 Do NOT use this due to the possibility of overflowing C<buffer>.  Instead use
1675 my_snprintf()
1676
1677 =cut
1678 */
1679 #define my_sprintf sprintf
1680
1681 /*
1682  * If we have v?snprintf() and the C99 variadic macros, we can just
1683  * use just the v?snprintf().  It is nice to try to trap the buffer
1684  * overflow, however, so if we are DEBUGGING, and we cannot use the
1685  * gcc statement expressions, then use the function wrappers which try
1686  * to trap the overflow.  If we can use the gcc statement expressions,
1687  * we can try that even with the version that uses the C99 variadic
1688  * macros.
1689  */
1690
1691 /* Note that we do not check against snprintf()/vsnprintf() returning
1692  * negative values because that is non-standard behaviour and we use
1693  * snprintf/vsnprintf only iff HAS_VSNPRINTF has been defined, and
1694  * that should be true only if the snprintf()/vsnprintf() are true
1695  * to the standard. */
1696
1697 #define PERL_SNPRINTF_CHECK(len, max, api) STMT_START { if ((max) > 0 && (Size_t)len > (max)) Perl_croak_nocontext("panic: %s buffer overflow", STRINGIFY(api)); } STMT_END
1698
1699 #ifdef USE_QUADMATH
1700 #  define my_snprintf Perl_my_snprintf
1701 #  define PERL_MY_SNPRINTF_GUARDED
1702 #elif defined(HAS_SNPRINTF) && defined(HAS_C99_VARIADIC_MACROS) && !(defined(DEBUGGING) && !defined(PERL_USE_GCC_BRACE_GROUPS)) && !defined(PERL_GCC_PEDANTIC)
1703 #  ifdef PERL_USE_GCC_BRACE_GROUPS
1704 #      define my_snprintf(buffer, max, ...) ({ int len = snprintf(buffer, max, __VA_ARGS__); PERL_SNPRINTF_CHECK(len, max, snprintf); len; })
1705 #      define PERL_MY_SNPRINTF_GUARDED
1706 #  else
1707 #    define my_snprintf(buffer, max, ...) snprintf(buffer, max, __VA_ARGS__)
1708 #  endif
1709 #else
1710 #  define my_snprintf  Perl_my_snprintf
1711 #  define PERL_MY_SNPRINTF_GUARDED
1712 #endif
1713
1714 /* There is no quadmath_vsnprintf, and therefore my_vsnprintf()
1715  * dies if called under USE_QUADMATH. */
1716 #if defined(HAS_VSNPRINTF) && defined(HAS_C99_VARIADIC_MACROS) && !(defined(DEBUGGING) && !defined(PERL_USE_GCC_BRACE_GROUPS)) && !defined(PERL_GCC_PEDANTIC)
1717 #  ifdef PERL_USE_GCC_BRACE_GROUPS
1718 #      define my_vsnprintf(buffer, max, ...) ({ int len = vsnprintf(buffer, max, __VA_ARGS__); PERL_SNPRINTF_CHECK(len, max, vsnprintf); len; })
1719 #      define PERL_MY_VSNPRINTF_GUARDED
1720 #  else
1721 #    define my_vsnprintf(buffer, max, ...) vsnprintf(buffer, max, __VA_ARGS__)
1722 #  endif
1723 #else
1724 #  define my_vsnprintf Perl_my_vsnprintf
1725 #  define PERL_MY_VSNPRINTF_GUARDED
1726 #endif
1727
1728 /* You will definitely need to use the PERL_MY_SNPRINTF_POST_GUARD()
1729  * or PERL_MY_VSNPRINTF_POST_GUARD() if you otherwise decide to ignore
1730  * the result of my_snprintf() or my_vsnprintf().  (No, you should not
1731  * completely ignore it: otherwise you cannot know whether your output
1732  * was too long.)
1733  *
1734  * int len = my_sprintf(buf, max, ...);
1735  * PERL_MY_SNPRINTF_POST_GUARD(len, max);
1736  *
1737  * The trick is that in certain platforms [a] the my_sprintf() already
1738  * contains the sanity check, while in certain platforms [b] it needs
1739  * to be done as a separate step.  The POST_GUARD is that step-- in [a]
1740  * platforms the POST_GUARD actually does nothing since the check has
1741  * already been done.  Watch out for the max being the same in both calls.
1742  *
1743  * If you actually use the snprintf/vsnprintf return value already,
1744  * you assumedly are checking its validity somehow.  But you can
1745  * insert the POST_GUARD() also in that case. */
1746
1747 #ifndef PERL_MY_SNPRINTF_GUARDED
1748 #  define PERL_MY_SNPRINTF_POST_GUARD(len, max) PERL_SNPRINTF_CHECK(len, max, snprintf)
1749 #else
1750 #  define PERL_MY_SNPRINTF_POST_GUARD(len, max) PERL_UNUSED_VAR(len)
1751 #endif
1752
1753 #ifndef  PERL_MY_VSNPRINTF_GUARDED
1754 #  define PERL_MY_VSNPRINTF_POST_GUARD(len, max) PERL_SNPRINTF_CHECK(len, max, vsnprintf)
1755 #else
1756 #  define PERL_MY_VSNPRINTF_POST_GUARD(len, max) PERL_UNUSED_VAR(len)
1757 #endif
1758
1759 #ifdef HAS_STRLCAT
1760 #  define my_strlcat    strlcat
1761 #endif
1762
1763 #if defined(PERL_CORE) || defined(PERL_EXT)
1764 #  ifdef HAS_MEMRCHR
1765 #    define my_memrchr  memrchr
1766 #  else
1767 #    define my_memrchr  S_my_memrchr
1768 #  endif
1769 #endif
1770
1771 #ifdef HAS_STRLCPY
1772 #  define my_strlcpy    strlcpy
1773 #endif
1774
1775 #ifdef HAS_STRNLEN
1776 #  define my_strnlen    strnlen
1777 #endif
1778
1779 /*
1780     The IV type is supposed to be long enough to hold any integral
1781     value or a pointer.
1782     --Andy Dougherty    August 1996
1783 */
1784
1785 typedef IVTYPE IV;
1786 typedef UVTYPE UV;
1787
1788 #if defined(USE_64_BIT_INT) && defined(HAS_QUAD)
1789 #  if QUADKIND == QUAD_IS_INT64_T && defined(INT64_MAX)
1790 #    define IV_MAX INT64_MAX
1791 #    define IV_MIN INT64_MIN
1792 #    define UV_MAX UINT64_MAX
1793 #    ifndef UINT64_MIN
1794 #      define UINT64_MIN 0
1795 #    endif
1796 #    define UV_MIN UINT64_MIN
1797 #  else
1798 #    define IV_MAX PERL_QUAD_MAX
1799 #    define IV_MIN PERL_QUAD_MIN
1800 #    define UV_MAX PERL_UQUAD_MAX
1801 #    define UV_MIN PERL_UQUAD_MIN
1802 #  endif
1803 #  define IV_IS_QUAD
1804 #  define UV_IS_QUAD
1805 #else
1806 #  if defined(INT32_MAX) && IVSIZE == 4
1807 #    define IV_MAX INT32_MAX
1808 #    define IV_MIN INT32_MIN
1809 #    ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
1810 #        define UV_MAX UINT32_MAX
1811 #    else
1812 #        define UV_MAX 4294967295U
1813 #    endif
1814 #    ifndef UINT32_MIN
1815 #      define UINT32_MIN 0
1816 #    endif
1817 #    define UV_MIN UINT32_MIN
1818 #  else
1819 #    define IV_MAX PERL_LONG_MAX
1820 #    define IV_MIN PERL_LONG_MIN
1821 #    define UV_MAX PERL_ULONG_MAX
1822 #    define UV_MIN PERL_ULONG_MIN
1823 #  endif
1824 #  if IVSIZE == 8
1825 #    define IV_IS_QUAD
1826 #    define UV_IS_QUAD
1827 #    ifndef HAS_QUAD
1828 #      define HAS_QUAD
1829 #    endif
1830 #  else
1831 #    undef IV_IS_QUAD
1832 #    undef UV_IS_QUAD
1833 #if !defined(PERL_CORE) || defined(USING_MSVC6)
1834 /* We think that removing this decade-old undef this will cause too much
1835    breakage on CPAN for too little gain. (See RT #119753)
1836    However, we do need HAS_QUAD in the core for use by the drand48 code,
1837    but not for Win32 VC6 because it has poor __int64 support. */
1838 #    undef HAS_QUAD
1839 #endif
1840 #  endif
1841 #endif
1842
1843 #define Size_t_MAX (~(Size_t)0)
1844 #define SSize_t_MAX (SSize_t)(~(Size_t)0 >> 1)
1845
1846 #define IV_DIG (BIT_DIGITS(IVSIZE * 8))
1847 #define UV_DIG (BIT_DIGITS(UVSIZE * 8))
1848
1849 #ifndef NO_PERL_PRESERVE_IVUV
1850 #define PERL_PRESERVE_IVUV      /* We like our integers to stay integers. */
1851 #endif
1852
1853 /*
1854  *  The macros INT2PTR and NUM2PTR are (despite their names)
1855  *  bi-directional: they will convert int/float to or from pointers.
1856  *  However the conversion to int/float are named explicitly:
1857  *  PTR2IV, PTR2UV, PTR2NV.
1858  *
1859  *  For int conversions we do not need two casts if pointers are
1860  *  the same size as IV and UV.   Otherwise we need an explicit
1861  *  cast (PTRV) to avoid compiler warnings.
1862  */
1863 #if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
1864 #  define PTRV                  UV
1865 #  define INT2PTR(any,d)        (any)(d)
1866 #elif PTRSIZE == LONGSIZE
1867 #  define PTRV                  unsigned long
1868 #  define PTR2ul(p)             (unsigned long)(p)
1869 #else
1870 #  define PTRV                  unsigned
1871 #endif
1872
1873 #ifndef INT2PTR
1874 #  define INT2PTR(any,d)        (any)(PTRV)(d)
1875 #endif
1876
1877 #ifndef PTR2ul
1878 #  define PTR2ul(p)     INT2PTR(unsigned long,p)        
1879 #endif
1880
1881 #define NUM2PTR(any,d)  (any)(PTRV)(d)
1882 #define PTR2IV(p)       INT2PTR(IV,p)
1883 #define PTR2UV(p)       INT2PTR(UV,p)
1884 #define PTR2NV(p)       NUM2PTR(NV,p)
1885 #define PTR2nat(p)      (PTRV)(p)       /* pointer to integer of PTRSIZE */
1886
1887 /* According to strict ANSI C89 one cannot freely cast between
1888  * data pointers and function (code) pointers.  There are at least
1889  * two ways around this.  One (used below) is to do two casts,
1890  * first the other pointer to an (unsigned) integer, and then
1891  * the integer to the other pointer.  The other way would be
1892  * to use unions to "overlay" the pointers.  For an example of
1893  * the latter technique, see union dirpu in struct xpvio in sv.h.
1894  * The only feasible use is probably temporarily storing
1895  * function pointers in a data pointer (such as a void pointer). */
1896
1897 #define DPTR2FPTR(t,p) ((t)PTR2nat(p))  /* data pointer to function pointer */
1898 #define FPTR2DPTR(t,p) ((t)PTR2nat(p))  /* function pointer to data pointer */
1899
1900 #ifdef USE_LONG_DOUBLE
1901 #  if LONG_DOUBLESIZE == DOUBLESIZE
1902 #    define LONG_DOUBLE_EQUALS_DOUBLE
1903 #    undef USE_LONG_DOUBLE /* Ouch! */
1904 #  endif
1905 #endif
1906
1907 /* The following is all to get LDBL_DIG, in order to pick a nice
1908    default value for printing floating point numbers in Gconvert.
1909    (see config.h)
1910 */
1911 #ifndef HAS_LDBL_DIG
1912 #  if LONG_DOUBLESIZE == 10
1913 #    define LDBL_DIG 18 /* assume IEEE */
1914 #  elif LONG_DOUBLESIZE == 12
1915 #    define LDBL_DIG 18 /* gcc? */
1916 #  elif LONG_DOUBLESIZE == 16
1917 #    define LDBL_DIG 33 /* assume IEEE */
1918 #  elif LONG_DOUBLESIZE == DOUBLESIZE
1919 #    define LDBL_DIG DBL_DIG /* bummer */
1920 #  endif
1921 #endif
1922
1923 typedef NVTYPE NV;
1924
1925 #ifdef I_IEEEFP
1926 #   include <ieeefp.h>
1927 #endif
1928
1929 #ifdef USING_MSVC6
1930 /* VC6 has broken NaN semantics: NaN == NaN returns true instead of false,
1931  * and for example NaN < IV_MIN. */
1932 #  define NAN_COMPARE_BROKEN
1933 #endif
1934 #if defined(__DECC) && defined(__osf__)
1935 /* Also Tru64 cc has broken NaN comparisons. */
1936 #  define NAN_COMPARE_BROKEN
1937 #endif
1938 #if defined(__sgi)
1939 #  define NAN_COMPARE_BROKEN
1940 #endif
1941
1942 #ifdef USE_LONG_DOUBLE
1943 #   ifdef I_SUNMATH
1944 #       include <sunmath.h>
1945 #   endif
1946 #   if defined(LDBL_DIG)
1947 #       define NV_DIG LDBL_DIG
1948 #       ifdef LDBL_MANT_DIG
1949 #           define NV_MANT_DIG LDBL_MANT_DIG
1950 #       endif
1951 #       ifdef LDBL_MIN
1952 #           define NV_MIN LDBL_MIN
1953 #       endif
1954 #       ifdef LDBL_MAX
1955 #           define NV_MAX LDBL_MAX
1956 #       endif
1957 #       ifdef LDBL_MIN_EXP
1958 #           define NV_MIN_EXP LDBL_MIN_EXP
1959 #       endif
1960 #       ifdef LDBL_MAX_EXP
1961 #           define NV_MAX_EXP LDBL_MAX_EXP
1962 #       endif
1963 #       ifdef LDBL_MIN_10_EXP
1964 #           define NV_MIN_10_EXP LDBL_MIN_10_EXP
1965 #       endif
1966 #       ifdef LDBL_MAX_10_EXP
1967 #           define NV_MAX_10_EXP LDBL_MAX_10_EXP
1968 #       endif
1969 #       ifdef LDBL_EPSILON
1970 #           define NV_EPSILON LDBL_EPSILON
1971 #       endif
1972 #       ifdef LDBL_MAX
1973 #           define NV_MAX LDBL_MAX
1974 /* Having LDBL_MAX doesn't necessarily mean that we have LDBL_MIN... -Allen */
1975 #       elif defined(HUGE_VALL)
1976 #           define NV_MAX HUGE_VALL
1977 #       endif
1978 #   endif
1979 #   if defined(HAS_SQRTL)
1980 #       define Perl_acos acosl
1981 #       define Perl_asin asinl
1982 #       define Perl_atan atanl
1983 #       define Perl_atan2 atan2l
1984 #       define Perl_ceil ceill
1985 #       define Perl_cos cosl
1986 #       define Perl_cosh coshl
1987 #       define Perl_exp expl
1988 /* no Perl_fabs, but there's PERL_ABS */
1989 #       define Perl_floor floorl
1990 #       define Perl_fmod fmodl
1991 #       define Perl_log logl
1992 #       define Perl_log10 log10l
1993 #       define Perl_pow powl
1994 #       define Perl_sin sinl
1995 #       define Perl_sinh sinhl
1996 #       define Perl_sqrt sqrtl
1997 #       define Perl_tan tanl
1998 #       define Perl_tanh tanhl
1999 #   endif
2000 /* e.g. libsunmath doesn't have modfl and frexpl as of mid-March 2000 */
2001 #   ifndef Perl_modf
2002 #       ifdef HAS_MODFL
2003 #           define Perl_modf(x,y) modfl(x,y)
2004 /* eg glibc 2.2 series seems to provide modfl on ppc and arm, but has no
2005    prototype in <math.h> */
2006 #           ifndef HAS_MODFL_PROTO
2007 EXTERN_C long double modfl(long double, long double *);
2008 #           endif
2009 #       elif (defined(HAS_TRUNCL) || defined(HAS_AINTL)) && defined(HAS_COPYSIGNL)
2010         extern long double Perl_my_modfl(long double x, long double *ip);
2011 #           define Perl_modf(x,y) Perl_my_modfl(x,y)
2012 #       endif
2013 #   endif
2014 #   ifndef Perl_frexp
2015 #       ifdef HAS_FREXPL
2016 #           define Perl_frexp(x,y) frexpl(x,y)
2017 #       elif defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
2018 extern long double Perl_my_frexpl(long double x, int *e);
2019 #           define Perl_frexp(x,y) Perl_my_frexpl(x,y)
2020 #       endif
2021 #   endif
2022 #   ifndef Perl_ldexp
2023 #       ifdef HAS_LDEXPL
2024 #           define Perl_ldexp(x, y) ldexpl(x,y)
2025 #       elif defined(HAS_SCALBNL) && FLT_RADIX == 2
2026 #           define Perl_ldexp(x,y) scalbnl(x,y)
2027 #       endif
2028 #   endif
2029 #   ifndef Perl_isnan
2030 #       if defined(HAS_ISNANL) && !(defined(isnan) && defined(HAS_C99))
2031 #           define Perl_isnan(x) isnanl(x)
2032 #       elif defined(__sgi) && defined(__c99)  /* XXX Configure test needed */
2033 #           define Perl_isnan(x) isnan(x)
2034 #       endif
2035 #   endif
2036 #   ifndef Perl_isinf
2037 #       if defined(HAS_ISINFL) && !(defined(isinf) && defined(HAS_C99))
2038 #           define Perl_isinf(x) isinfl(x)
2039 #       elif defined(__sgi) && defined(__c99)  /* XXX Configure test needed */
2040 #           define Perl_isinf(x) isinf(x)
2041 #       elif defined(LDBL_MAX) && !defined(NAN_COMPARE_BROKEN)
2042 #           define Perl_isinf(x) ((x) > LDBL_MAX || (x) < -LDBL_MAX)
2043 #       endif
2044 #   endif
2045 #   ifndef Perl_isfinite
2046 #       define Perl_isfinite(x) Perl_isfinitel(x)
2047 #   endif
2048 #elif defined(USE_QUADMATH) && defined(I_QUADMATH)
2049 #   include <quadmath.h>
2050 #   define NV_DIG FLT128_DIG
2051 #   define NV_MANT_DIG FLT128_MANT_DIG
2052 #   define NV_MIN FLT128_MIN
2053 #   define NV_MAX FLT128_MAX
2054 #   define NV_MIN_EXP FLT128_MIN_EXP
2055 #   define NV_MAX_EXP FLT128_MAX_EXP
2056 #   define NV_EPSILON FLT128_EPSILON
2057 #   define NV_MIN_10_EXP FLT128_MIN_10_EXP
2058 #   define NV_MAX_10_EXP FLT128_MAX_10_EXP
2059 #   define Perl_acos acosq
2060 #   define Perl_asin asinq
2061 #   define Perl_atan atanq
2062 #   define Perl_atan2 atan2q
2063 #   define Perl_ceil ceilq
2064 #   define Perl_cos cosq
2065 #   define Perl_cosh coshq
2066 #   define Perl_exp expq
2067 /* no Perl_fabs, but there's PERL_ABS */
2068 #   define Perl_floor floorq
2069 #   define Perl_fmod fmodq
2070 #   define Perl_log logq
2071 #   define Perl_log10 log10q
2072 #   define Perl_signbit signbitq
2073 #   define Perl_pow powq
2074 #   define Perl_sin sinq
2075 #   define Perl_sinh sinhq
2076 #   define Perl_sqrt sqrtq
2077 #   define Perl_tan tanq
2078 #   define Perl_tanh tanhq
2079 #   define Perl_modf(x,y) modfq(x,y)
2080 #   define Perl_frexp(x,y) frexpq(x,y)
2081 #   define Perl_ldexp(x, y) ldexpq(x,y)
2082 #   define Perl_isinf(x) isinfq(x)
2083 #   define Perl_isnan(x) isnanq(x)
2084 #   define Perl_isfinite(x) !(isnanq(x) || isinfq(x))
2085 #   define Perl_fp_class(x) ((x) == 0.0Q ? 0 : isinfq(x) ? 3 : isnanq(x) ? 4 : PERL_ABS(x) < FLT128_MIN ? 2 : 1)
2086 #   define Perl_fp_class_inf(x)    (Perl_fp_class(x) == 3)
2087 #   define Perl_fp_class_nan(x)    (Perl_fp_class(x) == 4)
2088 #   define Perl_fp_class_norm(x)   (Perl_fp_class(x) == 1)
2089 #   define Perl_fp_class_denorm(x) (Perl_fp_class(x) == 2)
2090 #   define Perl_fp_class_zero(x)   (Perl_fp_class(x) == 0)
2091 #else
2092 #   define NV_DIG DBL_DIG
2093 #   define NV_MANT_DIG DBL_MANT_DIG
2094 #   define NV_MIN DBL_MIN
2095 #   define NV_MAX DBL_MAX
2096 #   define NV_MIN_EXP DBL_MIN_EXP
2097 #   define NV_MAX_EXP DBL_MAX_EXP
2098 #   define NV_MIN_10_EXP DBL_MIN_10_EXP
2099 #   define NV_MAX_10_EXP DBL_MAX_10_EXP
2100 #   define NV_EPSILON DBL_EPSILON
2101 #   define NV_MAX DBL_MAX
2102 #   define NV_MIN DBL_MIN
2103
2104 /* These math interfaces are C89. */
2105 #   define Perl_acos acos
2106 #   define Perl_asin asin
2107 #   define Perl_atan atan
2108 #   define Perl_atan2 atan2
2109 #   define Perl_ceil ceil
2110 #   define Perl_cos cos
2111 #   define Perl_cosh cosh
2112 #   define Perl_exp exp
2113 /* no Perl_fabs, but there's PERL_ABS */
2114 #   define Perl_floor floor
2115 #   define Perl_fmod fmod
2116 #   define Perl_log log
2117 #   define Perl_log10 log10
2118 #   define Perl_pow pow
2119 #   define Perl_sin sin
2120 #   define Perl_sinh sinh
2121 #   define Perl_sqrt sqrt
2122 #   define Perl_tan tan
2123 #   define Perl_tanh tanh
2124
2125 #   define Perl_modf(x,y) modf(x,y)
2126 #   define Perl_frexp(x,y) frexp(x,y)
2127 #   define Perl_ldexp(x,y) ldexp(x,y)
2128
2129 #   ifndef Perl_isnan
2130 #       ifdef HAS_ISNAN
2131 #           define Perl_isnan(x) isnan(x)
2132 #       endif
2133 #   endif
2134 #   ifndef Perl_isinf
2135 #       if defined(HAS_ISINF)
2136 #           define Perl_isinf(x) isinf(x)
2137 #       elif defined(DBL_MAX) && !defined(NAN_COMPARE_BROKEN)
2138 #           define Perl_isinf(x) ((x) > DBL_MAX || (x) < -DBL_MAX)
2139 #       endif
2140 #   endif
2141 #   ifndef Perl_isfinite
2142 #     ifdef HAS_ISFINITE
2143 #       define Perl_isfinite(x) isfinite(x)
2144 #     elif defined(HAS_FINITE)
2145 #       define Perl_isfinite(x) finite(x)
2146 #     endif
2147 #   endif
2148 #endif
2149
2150 /* fpclassify(): C99.  It is supposed to be a macro that switches on
2151 * the sizeof() of its argument, so there's no need for e.g. fpclassifyl().*/
2152 #if !defined(Perl_fp_class) && defined(HAS_FPCLASSIFY)
2153 #    include <math.h>
2154 #    if defined(FP_INFINITE) && defined(FP_NAN)
2155 #        define Perl_fp_class(x)        fpclassify(x)
2156 #        define Perl_fp_class_inf(x)    (Perl_fp_class(x)==FP_INFINITE)
2157 #        define Perl_fp_class_nan(x)    (Perl_fp_class(x)==FP_NAN)
2158 #        define Perl_fp_class_norm(x)   (Perl_fp_class(x)==FP_NORMAL)
2159 #        define Perl_fp_class_denorm(x) (Perl_fp_class(x)==FP_SUBNORMAL)
2160 #        define Perl_fp_class_zero(x)   (Perl_fp_class(x)==FP_ZERO)
2161 #    elif defined(FP_PLUS_INF) && defined(FP_QNAN)
2162 /* Some versions of HP-UX (10.20) have (only) fpclassify() but which is
2163  * actually not the C99 fpclassify, with its own set of return defines. */
2164 #        define Perl_fp_class(x)        fpclassify(x)
2165 #        define Perl_fp_class_pinf(x)   (Perl_fp_class(x)==FP_PLUS_INF)
2166 #        define Perl_fp_class_ninf(x)   (Perl_fp_class(x)==FP_MINUS_INF)
2167 #        define Perl_fp_class_snan(x)   (Perl_fp_class(x)==FP_SNAN)
2168 #        define Perl_fp_class_qnan(x)   (Perl_fp_class(x)==FP_QNAN)
2169 #        define Perl_fp_class_pnorm(x)  (Perl_fp_class(x)==FP_PLUS_NORM)
2170 #        define Perl_fp_class_nnorm(x)  (Perl_fp_class(x)==FP_MINUS_NORM)
2171 #        define Perl_fp_class_pdenorm(x)        (Perl_fp_class(x)==FP_PLUS_DENORM)
2172 #        define Perl_fp_class_ndenorm(x)        (Perl_fp_class(x)==FP_MINUS_DENORM)
2173 #        define Perl_fp_class_pzero(x)  (Perl_fp_class(x)==FP_PLUS_ZERO)
2174 #        define Perl_fp_class_nzero(x)  (Perl_fp_class(x)==FP_MINUS_ZERO)
2175 #    else
2176 #        undef Perl_fp_class /* Unknown set of defines */
2177 #    endif
2178 #endif
2179
2180 /* fp_classify(): Legacy: VMS, maybe Unicos? The values, however,
2181  * are identical to the C99 fpclassify(). */
2182 #if !defined(Perl_fp_class) && defined(HAS_FP_CLASSIFY)
2183 #    include <math.h>
2184 #    ifdef __VMS
2185      /* FP_INFINITE and others are here rather than in math.h as C99 stipulates */
2186 #        include <fp.h>
2187      /* oh, and the isnormal macro has a typo in it! */
2188 #    undef isnormal
2189 #    define isnormal(x) Perl_fp_class_norm(x)
2190 #    endif
2191 #    if defined(FP_INFINITE) && defined(FP_NAN)
2192 #        define Perl_fp_class(x)        fp_classify(x)
2193 #        define Perl_fp_class_inf(x)    (Perl_fp_class(x)==FP_INFINITE)
2194 #        define Perl_fp_class_nan(x)    (Perl_fp_class(x)==FP_NAN)
2195 #        define Perl_fp_class_norm(x)   (Perl_fp_class(x)==FP_NORMAL)
2196 #        define Perl_fp_class_denorm(x) (Perl_fp_class(x)==FP_SUBNORMAL)
2197 #        define Perl_fp_class_zero(x)   (Perl_fp_class(x)==FP_ZERO)
2198 #    else
2199 #        undef Perl_fp_class /* Unknown set of defines */
2200 #    endif
2201 #endif
2202
2203 /* Feel free to check with me for the SGI manpages, SGI testing,
2204  * etcetera, if you want to try getting this to work with IRIX.
2205  *
2206  * - Allen <allens@cpan.org> */
2207
2208 /* fpclass(): SysV, at least Solaris and some versions of IRIX. */
2209 #if !defined(Perl_fp_class) && (defined(HAS_FPCLASS)||defined(HAS_FPCLASSL))
2210 /* Solaris and IRIX have fpclass/fpclassl, but they are using
2211  * an enum typedef, not cpp symbols, and Configure doesn't detect that.
2212  * Define some symbols also as cpp symbols so we can detect them. */
2213 #    if defined(__sun) || defined(__sgi) /* XXX Configure test instead */
2214 #     define FP_PINF FP_PINF
2215 #     define FP_QNAN FP_QNAN
2216 #    endif
2217 #    include <math.h>
2218 #    ifdef I_IEEFP
2219 #        include <ieeefp.h>
2220 #    endif
2221 #    ifdef I_FP
2222 #        include <fp.h>
2223 #    endif
2224 #    if defined(USE_LONG_DOUBLE) && defined(HAS_FPCLASSL)
2225 #        define Perl_fp_class(x)        fpclassl(x)
2226 #    else
2227 #        define Perl_fp_class(x)        fpclass(x)
2228 #    endif
2229 #    if defined(FP_CLASS_PINF) && defined(FP_CLASS_SNAN)
2230 #        define Perl_fp_class_snan(x)   (Perl_fp_class(x)==FP_CLASS_SNAN)
2231 #        define Perl_fp_class_qnan(x)   (Perl_fp_class(x)==FP_CLASS_QNAN)
2232 #        define Perl_fp_class_ninf(x)   (Perl_fp_class(x)==FP_CLASS_NINF)
2233 #        define Perl_fp_class_pinf(x)   (Perl_fp_class(x)==FP_CLASS_PINF)
2234 #        define Perl_fp_class_nnorm(x)  (Perl_fp_class(x)==FP_CLASS_NNORM)
2235 #        define Perl_fp_class_pnorm(x)  (Perl_fp_class(x)==FP_CLASS_PNORM)
2236 #        define Perl_fp_class_ndenorm(x)        (Perl_fp_class(x)==FP_CLASS_NDENORM)
2237 #        define Perl_fp_class_pdenorm(x)        (Perl_fp_class(x)==FP_CLASS_PDENORM)
2238 #        define Perl_fp_class_nzero(x)  (Perl_fp_class(x)==FP_CLASS_NZERO)
2239 #        define Perl_fp_class_pzero(x)  (Perl_fp_class(x)==FP_CLASS_PZERO)
2240 #    elif defined(FP_PINF) && defined(FP_QNAN)
2241 #        define Perl_fp_class_snan(x)   (Perl_fp_class(x)==FP_SNAN)
2242 #        define Perl_fp_class_qnan(x)   (Perl_fp_class(x)==FP_QNAN)
2243 #        define Perl_fp_class_ninf(x)   (Perl_fp_class(x)==FP_NINF)
2244 #        define Perl_fp_class_pinf(x)   (Perl_fp_class(x)==FP_PINF)
2245 #        define Perl_fp_class_nnorm(x)  (Perl_fp_class(x)==FP_NNORM)
2246 #        define Perl_fp_class_pnorm(x)  (Perl_fp_class(x)==FP_PNORM)
2247 #        define Perl_fp_class_ndenorm(x)        (Perl_fp_class(x)==FP_NDENORM)
2248 #        define Perl_fp_class_pdenorm(x)        (Perl_fp_class(x)==FP_PDENORM)
2249 #        define Perl_fp_class_nzero(x)  (Perl_fp_class(x)==FP_NZERO)
2250 #        define Perl_fp_class_pzero(x)  (Perl_fp_class(x)==FP_PZERO)
2251 #    else
2252 #        undef Perl_fp_class /* Unknown set of defines */
2253 #    endif
2254 #endif
2255
2256 /* fp_class(): Legacy: at least Tru64, some versions of IRIX. */
2257 #if !defined(Perl_fp_class) && (defined(HAS_FP_CLASS)||defined(HAS_FP_CLASSL))
2258 #    include <math.h>
2259 #    if !defined(FP_SNAN) && defined(I_FP_CLASS)
2260 #        include <fp_class.h>
2261 #    endif
2262 #    if defined(FP_POS_INF) && defined(FP_QNAN)
2263 #        ifdef __sgi /* XXX Configure test instead */
2264 #            ifdef USE_LONG_DOUBLE
2265 #                define Perl_fp_class(x)        fp_class_l(x)
2266 #            else
2267 #                define Perl_fp_class(x)        fp_class_d(x)
2268 #            endif
2269 #        else
2270 #            if defined(USE_LONG_DOUBLE) && defined(HAS_FP_CLASSL)
2271 #                define Perl_fp_class(x)        fp_classl(x)
2272 #            else
2273 #                define Perl_fp_class(x)        fp_class(x)
2274 #            endif
2275 #        endif
2276 #        if defined(FP_POS_INF) && defined(FP_QNAN)
2277 #            define Perl_fp_class_snan(x)       (Perl_fp_class(x)==FP_SNAN)
2278 #            define Perl_fp_class_qnan(x)       (Perl_fp_class(x)==FP_QNAN)
2279 #            define Perl_fp_class_ninf(x)       (Perl_fp_class(x)==FP_NEG_INF)
2280 #            define Perl_fp_class_pinf(x)       (Perl_fp_class(x)==FP_POS_INF)
2281 #            define Perl_fp_class_nnorm(x)      (Perl_fp_class(x)==FP_NEG_NORM)
2282 #            define Perl_fp_class_pnorm(x)      (Perl_fp_class(x)==FP_POS_NORM)
2283 #            define Perl_fp_class_ndenorm(x)    (Perl_fp_class(x)==FP_NEG_DENORM)
2284 #            define Perl_fp_class_pdenorm(x)    (Perl_fp_class(x)==FP_POS_DENORM)
2285 #            define Perl_fp_class_nzero(x)      (Perl_fp_class(x)==FP_NEG_ZERO)
2286 #            define Perl_fp_class_pzero(x)      (Perl_fp_class(x)==FP_POS_ZERO)
2287 #        else
2288 #            undef Perl_fp_class /* Unknown set of defines */
2289 #        endif
2290 #    endif
2291 #endif
2292
2293 /* class(), _class(): Legacy: AIX. */
2294 #if !defined(Perl_fp_class) && defined(HAS_CLASS)
2295 #    include <math.h>
2296 #    if defined(FP_PLUS_NORM) && defined(FP_PLUS_INF)
2297 #        ifndef _cplusplus
2298 #            define Perl_fp_class(x)    class(x)
2299 #        else
2300 #            define Perl_fp_class(x)    _class(x)
2301 #        endif
2302 #        if defined(FP_PLUS_INF) && defined(FP_NANQ)
2303 #            define Perl_fp_class_snan(x)       (Perl_fp_class(x)==FP_NANS)
2304 #            define Perl_fp_class_qnan(x)       (Perl_fp_class(x)==FP_NANQ)
2305 #            define Perl_fp_class_ninf(x)       (Perl_fp_class(x)==FP_MINUS_INF)
2306 #            define Perl_fp_class_pinf(x)       (Perl_fp_class(x)==FP_PLUS_INF)
2307 #            define Perl_fp_class_nnorm(x)      (Perl_fp_class(x)==FP_MINUS_NORM)
2308 #            define Perl_fp_class_pnorm(x)      (Perl_fp_class(x)==FP_PLUS_NORM)
2309 #            define Perl_fp_class_ndenorm(x)    (Perl_fp_class(x)==FP_MINUS_DENORM)
2310 #            define Perl_fp_class_pdenorm(x)    (Perl_fp_class(x)==FP_PLUS_DENORM)
2311 #            define Perl_fp_class_nzero(x)      (Perl_fp_class(x)==FP_MINUS_ZERO)
2312 #            define Perl_fp_class_pzero(x)      (Perl_fp_class(x)==FP_PLUS_ZERO)
2313 #        else
2314 #            undef Perl_fp_class /* Unknown set of defines */
2315 #        endif
2316 #    endif
2317 #endif
2318
2319 /* Win32: _fpclass(), _isnan(), _finite(). */
2320 #ifdef _MSC_VER
2321 #  ifndef Perl_isnan
2322 #    define Perl_isnan(x) _isnan(x)
2323 #  endif
2324 #  ifndef Perl_isfinite
2325 #    define Perl_isfinite(x) _finite(x)
2326 #  endif
2327 #  ifndef Perl_fp_class_snan
2328 /* No simple way to #define Perl_fp_class because _fpclass()
2329  * returns a set of bits. */
2330 #    define Perl_fp_class_snan(x) (_fpclass(x) & _FPCLASS_SNAN)
2331 #    define Perl_fp_class_qnan(x) (_fpclass(x) & _FPCLASS_QNAN)
2332 #    define Perl_fp_class_nan(x) (_fpclass(x) & (_FPCLASS_SNAN|_FPCLASS_QNAN))
2333 #    define Perl_fp_class_ninf(x) (_fpclass(x) & _FPCLASS_NINF))
2334 #    define Perl_fp_class_pinf(x) (_fpclass(x) & _FPCLASS_PINF))
2335 #    define Perl_fp_class_inf(x) (_fpclass(x) & (_FPCLASS_NINF|_FPCLASS_PINF))
2336 #    define Perl_fp_class_nnorm(x) (_fpclass(x) & _FPCLASS_NN)
2337 #    define Perl_fp_class_pnorm(x) (_fpclass(x) & _FPCLASS_PN)
2338 #    define Perl_fp_class_norm(x) (_fpclass(x) & (_FPCLASS_NN|_FPCLASS_PN))
2339 #    define Perl_fp_class_ndenorm(x) (_fpclass(x) & _FPCLASS_ND)
2340 #    define Perl_fp_class_pdenorm(x) (_fpclass(x) & _FPCLASS_PD)
2341 #    define Perl_fp_class_denorm(x) (_fpclass(x) & (_FPCLASS_ND|_FPCLASS_PD))
2342 #    define Perl_fp_class_nzero(x) (_fpclass(x) & _FPCLASS_NZ)
2343 #    define Perl_fp_class_pzero(x) (_fpclass(x) & _FPCLASS_PZ)
2344 #    define Perl_fp_class_zero(x) (_fpclass(x) & (_FPCLASS_NZ|_FPCLASS_PZ))
2345 #  endif
2346 #endif
2347
2348 #if !defined(Perl_fp_class_inf) && \
2349   defined(Perl_fp_class_pinf) && defined(Perl_fp_class_ninf)
2350 #  define Perl_fp_class_inf(x) \
2351     (Perl_fp_class_pinf(x) || Perl_fp_class_ninf(x))
2352 #endif
2353
2354 #if !defined(Perl_fp_class_nan) && \
2355   defined(Perl_fp_class_snan) && defined(Perl_fp_class_qnan)
2356 #  define Perl_fp_class_nan(x) \
2357     (Perl_fp_class_snan(x) || Perl_fp_class_qnan(x))
2358 #endif
2359
2360 #if !defined(Perl_fp_class_zero) && \
2361   defined(Perl_fp_class_pzero) && defined(Perl_fp_class_nzero)
2362 #  define Perl_fp_class_zero(x) \
2363     (Perl_fp_class_pzero(x) || Perl_fp_class_nzero(x))
2364 #endif
2365
2366 #if !defined(Perl_fp_class_norm) && \
2367   defined(Perl_fp_class_pnorm) && defined(Perl_fp_class_nnorm)
2368 #  define Perl_fp_class_norm(x) \
2369     (Perl_fp_class_pnorm(x) || Perl_fp_class_nnorm(x))
2370 #endif
2371
2372 #if !defined(Perl_fp_class_denorm) && \
2373   defined(Perl_fp_class_pdenorm) && defined(Perl_fp_class_ndenorm)
2374 #  define Perl_fp_class_denorm(x) \
2375     (Perl_fp_class_pdenorm(x) || Perl_fp_class_ndenorm(x))
2376 #endif
2377
2378 #ifndef Perl_isnan
2379 #   ifdef Perl_fp_class_nan
2380 #       define Perl_isnan(x) Perl_fp_class_nan(x)
2381 #   elif defined(HAS_UNORDERED)
2382 #       define Perl_isnan(x) unordered((x), 0.0)
2383 #   else
2384 #       define Perl_isnan(x) ((x)!=(x))
2385 #   endif
2386 #endif
2387
2388 #ifndef Perl_isinf
2389 #   ifdef Perl_fp_class_inf
2390 #       define Perl_isinf(x) Perl_fp_class_inf(x)
2391 #   endif
2392 #endif
2393
2394 #ifndef Perl_isfinite
2395 #   if defined(HAS_ISFINITE) && !defined(isfinite)
2396 #     define Perl_isfinite(x) isfinite((double)(x))
2397 #   elif defined(HAS_FINITE)
2398 #       define Perl_isfinite(x) finite((double)(x))
2399 #   elif defined(Perl_fp_class_finite)
2400 #     define Perl_isfinite(x) Perl_fp_class_finite(x)
2401 #   else
2402 /* For the infinities the multiplication returns nan,
2403  * for the nan the multiplication also returns nan,
2404  * for everything else (that is, finite) zero should be returned. */
2405 #     define Perl_isfinite(x) (((x) * 0) == 0)
2406 #   endif
2407 #endif
2408
2409 #ifndef Perl_isinf
2410 #   if defined(Perl_isfinite) && defined(Perl_isnan)
2411 #       define Perl_isinf(x) !(Perl_isfinite(x)||Perl_isnan(x))
2412 #   endif
2413 #endif
2414
2415 /* We need Perl_isfinitel (ends with ell) (if available) even when
2416  * not USE_LONG_DOUBLE because the printf code (sv_catpvfn_flags)
2417  * needs that. */
2418 #if defined(HAS_LONG_DOUBLE) && !defined(Perl_isfinitel)
2419 /* If isfinite() is a macro and looks like we have C99,
2420  * we assume it's the type-aware C99 isfinite(). */
2421 #    if defined(HAS_ISFINITE) && defined(isfinite) && defined(HAS_C99)
2422 #        define Perl_isfinitel(x) isfinite(x)
2423 #    elif defined(HAS_ISFINITEL)
2424 #        define Perl_isfinitel(x) isfinitel(x)
2425 #    elif defined(HAS_FINITEL)
2426 #        define Perl_isfinitel(x) finitel(x)
2427 #    elif defined(HAS_INFL) && defined(HAS_NANL)
2428 #        define Perl_isfinitel(x) !(isinfl(x)||isnanl(x))
2429 #    else
2430 #        define Perl_isfinitel(x) ((x) * 0 == 0)  /* See Perl_isfinite. */
2431 #    endif
2432 #endif
2433
2434 /* The default is to use Perl's own atof() implementation (in numeric.c).
2435  * Usually that is the one to use but for some platforms (e.g. UNICOS)
2436  * it is however best to use the native implementation of atof.
2437  * You can experiment with using your native one by -DUSE_PERL_ATOF=0.
2438  * Some good tests to try out with either setting are t/base/num.t,
2439  * t/op/numconvert.t, and t/op/pack.t. Note that if using long doubles
2440  * you may need to be using a different function than atof! */
2441
2442 #ifndef USE_PERL_ATOF
2443 #   ifndef _UNICOS
2444 #       define USE_PERL_ATOF
2445 #   endif
2446 #else
2447 #   if USE_PERL_ATOF == 0
2448 #       undef USE_PERL_ATOF
2449 #   endif
2450 #endif
2451
2452 #ifdef USE_PERL_ATOF
2453 #   define Perl_atof(s) Perl_my_atof(s)
2454 #   define Perl_atof2(s, n) Perl_my_atof3(aTHX_ (s), &(n), 0)
2455 #else
2456 #   define Perl_atof(s) (NV)atof(s)
2457 #   define Perl_atof2(s, n) ((n) = atof(s))
2458 #endif
2459 #define my_atof2(a,b) my_atof3(a,b,0)
2460
2461 /*
2462  * CHAR_MIN and CHAR_MAX are not included here, as the (char) type may be
2463  * ambiguous. It may be equivalent to (signed char) or (unsigned char)
2464  * depending on local options. Until Configure detects this (or at least
2465  * detects whether the "signed" keyword is available) the CHAR ranges
2466  * will not be included. UCHAR functions normally.
2467  *                                                           - kja
2468  */
2469
2470 #define PERL_UCHAR_MIN ((unsigned char)0)
2471 #define PERL_UCHAR_MAX ((unsigned char)UCHAR_MAX)
2472
2473 #define PERL_USHORT_MIN ((unsigned short)0)
2474 #define PERL_USHORT_MAX ((unsigned short)USHRT_MAX)
2475
2476 #define PERL_SHORT_MAX ((short)SHRT_MAX)
2477 #define PERL_SHORT_MIN ((short)SHRT_MIN)
2478
2479 #define PERL_UINT_MAX ((unsigned int)UINT_MAX)
2480 #define PERL_UINT_MIN ((unsigned int)0)
2481
2482 #define PERL_INT_MAX ((int)INT_MAX)
2483 #define PERL_INT_MIN ((int)INT_MIN)
2484
2485 #define PERL_ULONG_MAX ((unsigned long)ULONG_MAX)
2486 #define PERL_ULONG_MIN ((unsigned long)0L)
2487
2488 #define PERL_LONG_MAX ((long)LONG_MAX)
2489 #define PERL_LONG_MIN ((long)LONG_MIN)
2490
2491 #ifdef UV_IS_QUAD
2492 #    define PERL_UQUAD_MAX      (~(UV)0)
2493 #    define PERL_UQUAD_MIN      ((UV)0)
2494 #    define PERL_QUAD_MAX       ((IV) (PERL_UQUAD_MAX >> 1))
2495 #    define PERL_QUAD_MIN       (-PERL_QUAD_MAX - ((3 & -1) == 3))
2496 #endif
2497
2498 /*
2499 =head1 Numeric functions
2500
2501 =for apidoc AmnUh||PERL_INT_MIN
2502 =for apidoc AmnUh||PERL_LONG_MAX
2503 =for apidoc AmnUh||PERL_LONG_MIN
2504 =for apidoc AmnUh||PERL_QUAD_MAX
2505 =for apidoc AmnUh||PERL_SHORT_MAX
2506 =for apidoc AmnUh||PERL_SHORT_MIN
2507 =for apidoc AmnUh||PERL_UCHAR_MAX
2508 =for apidoc AmnUh||PERL_UCHAR_MIN
2509 =for apidoc AmnUh||PERL_UINT_MAX
2510 =for apidoc AmnUh||PERL_ULONG_MAX
2511 =for apidoc AmnUh||PERL_ULONG_MIN
2512 =for apidoc AmnUh||PERL_UQUAD_MAX
2513 =for apidoc AmnUh||PERL_UQUAD_MIN
2514 =for apidoc AmnUh||PERL_USHORT_MAX
2515 =for apidoc AmnUh||PERL_USHORT_MIN
2516 =for apidoc AmnUh||PERL_QUAD_MIN
2517 =for apidoc AmnU||PERL_INT_MAX
2518 This and
2519 C<PERL_INT_MIN>,
2520 C<PERL_LONG_MAX>,
2521 C<PERL_LONG_MIN>,
2522 C<PERL_QUAD_MAX>,
2523 C<PERL_SHORT_MAX>,
2524 C<PERL_SHORT_MIN>,
2525 C<PERL_UCHAR_MAX>,
2526 C<PERL_UCHAR_MIN>,
2527 C<PERL_UINT_MAX>,
2528 C<PERL_ULONG_MAX>,
2529 C<PERL_ULONG_MIN>,
2530 C<PERL_UQUAD_MAX>,
2531 C<PERL_UQUAD_MIN>,
2532 C<PERL_USHORT_MAX>,
2533 C<PERL_USHORT_MIN>,
2534 C<PERL_QUAD_MIN>
2535 give the largest and smallest number representable in the current
2536 platform in variables of the corresponding types.
2537
2538 For signed types, the smallest representable number is the most negative
2539 number, the one furthest away from zero.
2540
2541 For C99 and later compilers, these correspond to things like C<INT_MAX>, which
2542 are available to the C code.  But these constants, furnished by Perl,
2543 allow code compiled on earlier compilers to portably have access to the same
2544 constants.
2545
2546 =cut
2547
2548 */
2549
2550 typedef MEM_SIZE STRLEN;
2551
2552 typedef struct op OP;
2553 typedef struct cop COP;
2554 typedef struct unop UNOP;
2555 typedef struct unop_aux UNOP_AUX;
2556 typedef struct binop BINOP;
2557 typedef struct listop LISTOP;
2558 typedef struct logop LOGOP;
2559 typedef struct pmop PMOP;
2560 typedef struct svop SVOP;
2561 typedef struct padop PADOP;
2562 typedef struct pvop PVOP;
2563 typedef struct loop LOOP;
2564 typedef struct methop METHOP;
2565
2566 #ifdef PERL_CORE
2567 typedef struct opslab OPSLAB;
2568 typedef struct opslot OPSLOT;
2569 #endif
2570
2571 typedef struct block_hooks BHK;
2572 typedef struct custom_op XOP;
2573
2574 typedef struct interpreter PerlInterpreter;
2575
2576 /* SGI's <sys/sema.h> has struct sv */
2577 #if defined(__sgi)
2578 #   define STRUCT_SV perl_sv
2579 #else
2580 #   define STRUCT_SV sv
2581 #endif
2582 typedef struct STRUCT_SV SV;
2583 typedef struct av AV;
2584 typedef struct hv HV;
2585 typedef struct cv CV;
2586 typedef struct p5rx REGEXP;
2587 typedef struct gp GP;
2588 typedef struct gv GV;
2589 typedef struct io IO;
2590 typedef struct context PERL_CONTEXT;
2591 typedef struct block BLOCK;
2592
2593 typedef struct magic MAGIC;
2594 typedef struct xpv XPV;
2595 typedef struct xpviv XPVIV;
2596 typedef struct xpvuv XPVUV;
2597 typedef struct xpvnv XPVNV;
2598 typedef struct xpvmg XPVMG;
2599 typedef struct xpvlv XPVLV;
2600 typedef struct xpvinvlist XINVLIST;
2601 typedef struct xpvav XPVAV;
2602 typedef struct xpvhv XPVHV;
2603 typedef struct xpvgv XPVGV;
2604 typedef struct xpvcv XPVCV;
2605 typedef struct xpvbm XPVBM;
2606 typedef struct xpvfm XPVFM;
2607 typedef struct xpvio XPVIO;
2608 typedef struct mgvtbl MGVTBL;
2609 typedef union any ANY;
2610 typedef struct ptr_tbl_ent PTR_TBL_ENT_t;
2611 typedef struct ptr_tbl PTR_TBL_t;
2612 typedef struct clone_params CLONE_PARAMS;
2613
2614 /* a pad is currently just an AV; but that might change,
2615  * so hide the type.  */
2616 typedef struct padlist PADLIST;
2617 typedef AV PAD;
2618 typedef struct padnamelist PADNAMELIST;
2619 typedef struct padname PADNAME;
2620
2621 /* always enable PERL_OP_PARENT  */
2622 #if !defined(PERL_OP_PARENT)
2623 #  define PERL_OP_PARENT
2624 #endif
2625
2626 /* enable PERL_COPY_ON_WRITE by default */
2627 #if !defined(PERL_COPY_ON_WRITE) && !defined(PERL_NO_COW)
2628 #  define PERL_COPY_ON_WRITE
2629 #endif
2630
2631 #ifdef PERL_COPY_ON_WRITE
2632 #  define PERL_ANY_COW
2633 #else
2634 # define PERL_SAWAMPERSAND
2635 #endif
2636
2637 #if defined(PERL_DEBUG_READONLY_OPS) && !defined(USE_ITHREADS)
2638 # error PERL_DEBUG_READONLY_OPS only works with ithreads
2639 #endif
2640
2641 #include "handy.h"
2642 #include "charclass_invlists.h"
2643
2644 #if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_RAWIO)
2645 #   if LSEEKSIZE == 8 && !defined(USE_64_BIT_RAWIO)
2646 #       define USE_64_BIT_RAWIO /* implicit */
2647 #   endif
2648 #endif
2649
2650 /* Notice the use of HAS_FSEEKO: now we are obligated to always use
2651  * fseeko/ftello if possible.  Don't go #defining ftell to ftello yourself,
2652  * however, because operating systems like to do that themself. */
2653 #ifndef FSEEKSIZE
2654 #   ifdef HAS_FSEEKO
2655 #       define FSEEKSIZE LSEEKSIZE
2656 #   else
2657 #       define FSEEKSIZE LONGSIZE
2658 #   endif
2659 #endif
2660
2661 #if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_STDIO)
2662 #   if FSEEKSIZE == 8 && !defined(USE_64_BIT_STDIO)
2663 #       define USE_64_BIT_STDIO /* implicit */
2664 #   endif
2665 #endif
2666
2667 #ifdef USE_64_BIT_RAWIO
2668 #   ifdef HAS_OFF64_T
2669 #       undef Off_t
2670 #       define Off_t off64_t
2671 #       undef LSEEKSIZE
2672 #       define LSEEKSIZE 8
2673 #   endif
2674 /* Most 64-bit environments have defines like _LARGEFILE_SOURCE that
2675  * will trigger defines like the ones below.  Some 64-bit environments,
2676  * however, do not.  Therefore we have to explicitly mix and match. */
2677 #   if defined(USE_OPEN64)
2678 #       define open open64
2679 #   endif
2680 #   if defined(USE_LSEEK64)
2681 #       define lseek lseek64
2682 #   else
2683 #       if defined(USE_LLSEEK)
2684 #           define lseek llseek
2685 #       endif
2686 #   endif
2687 #   if defined(USE_STAT64)
2688 #       define stat stat64
2689 #   endif
2690 #   if defined(USE_FSTAT64)
2691 #       define fstat fstat64
2692 #   endif
2693 #   if defined(USE_LSTAT64)
2694 #       define lstat lstat64
2695 #   endif
2696 #   if defined(USE_FLOCK64)
2697 #       define flock flock64
2698 #   endif
2699 #   if defined(USE_LOCKF64)
2700 #       define lockf lockf64
2701 #   endif
2702 #   if defined(USE_FCNTL64)
2703 #       define fcntl fcntl64
2704 #   endif
2705 #   if defined(USE_TRUNCATE64)
2706 #       define truncate truncate64
2707 #   endif
2708 #   if defined(USE_FTRUNCATE64)
2709 #       define ftruncate ftruncate64
2710 #   endif
2711 #endif
2712
2713 #ifdef USE_64_BIT_STDIO
2714 #   ifdef HAS_FPOS64_T
2715 #       undef Fpos_t
2716 #       define Fpos_t fpos64_t
2717 #   endif
2718 /* Most 64-bit environments have defines like _LARGEFILE_SOURCE that
2719  * will trigger defines like the ones below.  Some 64-bit environments,
2720  * however, do not. */
2721 #   if defined(USE_FOPEN64)
2722 #       define fopen fopen64
2723 #   endif
2724 #   if defined(USE_FSEEK64)
2725 #       define fseek fseek64 /* don't do fseeko here, see perlio.c */
2726 #   endif
2727 #   if defined(USE_FTELL64)
2728 #       define ftell ftell64 /* don't do ftello here, see perlio.c */
2729 #   endif
2730 #   if defined(USE_FSETPOS64)
2731 #       define fsetpos fsetpos64
2732 #   endif
2733 #   if defined(USE_FGETPOS64)
2734 #       define fgetpos fgetpos64
2735 #   endif
2736 #   if defined(USE_TMPFILE64)
2737 #       define tmpfile tmpfile64
2738 #   endif
2739 #   if defined(USE_FREOPEN64)
2740 #       define freopen freopen64
2741 #   endif
2742 #endif
2743
2744 #if defined(OS2)
2745 #  include "iperlsys.h"
2746 #endif
2747
2748 #ifdef DOSISH
2749 #   if defined(OS2)
2750 #       include "os2ish.h"
2751 #   else
2752 #       include "dosish.h"
2753 #   endif
2754 #elif defined(VMS)
2755 #   include "vmsish.h"
2756 #elif defined(PLAN9)
2757 #   include "./plan9/plan9ish.h"
2758 #elif defined(__VOS__)
2759 #   ifdef __GNUC__
2760 #     include "./vos/vosish.h"
2761 #   else
2762 #     include "vos/vosish.h"
2763 #   endif
2764 #elif defined(__SYMBIAN32__)
2765 #   include "symbian/symbianish.h"
2766 #elif defined(__HAIKU__)
2767 #   include "haiku/haikuish.h"
2768 #else
2769 #   include "unixish.h"
2770 #endif
2771
2772 #ifdef __amigaos4__
2773 #    include "amigaos.h"
2774 #    undef FD_CLOEXEC /* a lie in AmigaOS */
2775 #endif
2776
2777 /* NSIG logic from Configure --> */
2778 #ifndef NSIG
2779 #  ifdef _NSIG
2780 #    define NSIG (_NSIG)
2781 #  elif defined(SIGMAX)
2782 #    define NSIG (SIGMAX+1)
2783 #  elif defined(SIG_MAX)
2784 #    define NSIG (SIG_MAX+1)
2785 #  elif defined(_SIG_MAX)
2786 #    define NSIG (_SIG_MAX+1)
2787 #  elif defined(MAXSIG)
2788 #    define NSIG (MAXSIG+1)
2789 #  elif defined(MAX_SIG)
2790 #    define NSIG (MAX_SIG+1)
2791 #  elif defined(SIGARRAYSIZE)
2792 #    define NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */
2793 #  elif defined(_sys_nsig)
2794 #    define NSIG (_sys_nsig) /* Solaris 2.5 */
2795 #  else
2796      /* Default to some arbitrary number that's big enough to get most
2797       * of the common signals.  */
2798 #    define NSIG 50
2799 #  endif
2800 #endif
2801 /* <-- NSIG logic from Configure */
2802
2803 #ifndef NO_ENVIRON_ARRAY
2804 #  define USE_ENVIRON_ARRAY
2805 #endif
2806
2807 /*
2808  * initialise to avoid floating-point exceptions from overflow, etc
2809  */
2810 #ifndef PERL_FPU_INIT
2811 #  ifdef HAS_FPSETMASK
2812 #    if HAS_FLOATINGPOINT_H
2813 #      include <floatingpoint.h>
2814 #    endif
2815 /* Some operating systems have this as a macro, which in turn expands to a comma
2816    expression, and the last sub-expression is something that gets calculated,
2817    and then they have the gall to warn that a value computed is not used. Hence
2818    cast to void.  */
2819 #    define PERL_FPU_INIT (void)fpsetmask(0)
2820 #  elif defined(SIGFPE) && defined(SIG_IGN) && !defined(PERL_MICRO)
2821 #    define PERL_FPU_INIT       PL_sigfpe_saved = (Sighandler_t) signal(SIGFPE, SIG_IGN)
2822 #    define PERL_FPU_PRE_EXEC   { Sigsave_t xfpe; rsignal_save(SIGFPE, PL_sigfpe_saved, &xfpe);
2823 #    define PERL_FPU_POST_EXEC    rsignal_restore(SIGFPE, &xfpe); }
2824 #  else
2825 #    define PERL_FPU_INIT
2826 #  endif
2827 #endif
2828 #ifndef PERL_FPU_PRE_EXEC
2829 #  define PERL_FPU_PRE_EXEC   {
2830 #  define PERL_FPU_POST_EXEC  }
2831 #endif
2832
2833 /* In Tru64 the cc -ieee enables the IEEE math but disables traps.
2834  * We need to reenable the "invalid" trap because otherwise generation
2835  * of NaN values leaves the IEEE fp flags in bad state, leaving any further
2836  * fp ops behaving strangely (Inf + 1 resulting in zero, for example). */
2837 #ifdef __osf__
2838 #  include <machine/fpu.h>
2839 #  define PERL_SYS_FPU_INIT \
2840      STMT_START { \
2841          ieee_set_fp_control(IEEE_TRAP_ENABLE_INV); \
2842          signal(SIGFPE, SIG_IGN); \
2843      } STMT_END
2844 #endif
2845 /* In IRIX the default for Flush to Zero bit is true,
2846  * which means that results going below the minimum of normal
2847  * floating points go to zero, instead of going denormal/subnormal.
2848  * This is unlike almost any other system running Perl, so let's clear it.
2849  * [perl #123767] IRIX64 blead (ddce084a) opbasic/arith.t failure, originally
2850  * [perl #120426] small numbers shouldn't round to zero if they have extra floating digits
2851  *
2852  * XXX The flush-to-zero behaviour should be a Configure scan.
2853  * To change the behaviour usually requires some system-specific
2854  * incantation, though, like the below. */
2855 #ifdef __sgi
2856 #  include <sys/fpu.h>
2857 #  define PERL_SYS_FPU_INIT \
2858      STMT_START { \
2859          union fpc_csr csr; \
2860          csr.fc_word = get_fpc_csr(); \
2861          csr.fc_struct.flush = 0; \
2862          set_fpc_csr(csr.fc_word); \
2863      } STMT_END
2864 #endif
2865
2866 #ifndef PERL_SYS_FPU_INIT
2867 #  define PERL_SYS_FPU_INIT NOOP
2868 #endif
2869
2870 #ifndef PERL_SYS_INIT3_BODY
2871 #  define PERL_SYS_INIT3_BODY(argvp,argcp,envp) PERL_SYS_INIT_BODY(argvp,argcp)
2872 #endif
2873
2874 /*
2875 =head1 Miscellaneous Functions
2876
2877 =for apidoc Am|void|PERL_SYS_INIT|int *argc|char*** argv
2878 Provides system-specific tune up of the C runtime environment necessary to
2879 run Perl interpreters.  This should be called only once, before creating
2880 any Perl interpreters.
2881
2882 =for apidoc Am|void|PERL_SYS_INIT3|int *argc|char*** argv|char*** env
2883 Provides system-specific tune up of the C runtime environment necessary to
2884 run Perl interpreters.  This should be called only once, before creating
2885 any Perl interpreters.
2886
2887 =for apidoc Am|void|PERL_SYS_TERM|
2888 Provides system-specific clean up of the C runtime environment after
2889 running Perl interpreters.  This should be called only once, after
2890 freeing any remaining Perl interpreters.
2891
2892 =cut
2893  */
2894
2895 #define PERL_SYS_INIT(argc, argv)       Perl_sys_init(argc, argv)
2896 #define PERL_SYS_INIT3(argc, argv, env) Perl_sys_init3(argc, argv, env)
2897 #define PERL_SYS_TERM()                 Perl_sys_term()
2898
2899 #ifndef PERL_WRITE_MSG_TO_CONSOLE
2900 #  define PERL_WRITE_MSG_TO_CONSOLE(io, msg, len) PerlIO_write(io, msg, len)
2901 #endif
2902
2903 #ifndef MAXPATHLEN
2904 #  ifdef PATH_MAX
2905 #    ifdef _POSIX_PATH_MAX
2906 #       if PATH_MAX > _POSIX_PATH_MAX
2907 /* POSIX 1990 (and pre) was ambiguous about whether PATH_MAX
2908  * included the null byte or not.  Later amendments of POSIX,
2909  * XPG4, the Austin Group, and the Single UNIX Specification
2910  * all explicitly include the null byte in the PATH_MAX.
2911  * Ditto for _POSIX_PATH_MAX. */
2912 #         define MAXPATHLEN PATH_MAX
2913 #       else
2914 #         define MAXPATHLEN _POSIX_PATH_MAX
2915 #       endif
2916 #    else
2917 #      define MAXPATHLEN (PATH_MAX+1)
2918 #    endif
2919 #  else
2920 #    define MAXPATHLEN 1024     /* Err on the large side. */
2921 #  endif
2922 #endif
2923
2924 /* In case Configure was not used (we are using a "canned config"
2925  * such as Win32, or a cross-compilation setup, for example) try going
2926  * by the gcc major and minor versions.  One useful URL is
2927  * http://www.ohse.de/uwe/articles/gcc-attributes.html,
2928  * but contrary to this information warn_unused_result seems
2929  * not to be in gcc 3.3.5, at least. --jhi
2930  * Also, when building extensions with an installed perl, this allows
2931  * the user to upgrade gcc and get the right attributes, rather than
2932  * relying on the list generated at Configure time.  --AD
2933  * Set these up now otherwise we get confused when some of the <*thread.h>
2934  * includes below indirectly pull in <perlio.h> (which needs to know if we
2935  * have HASATTRIBUTE_FORMAT).
2936  */
2937
2938 #ifndef PERL_MICRO
2939 #if defined __GNUC__ && !defined(__INTEL_COMPILER)
2940 #  if __GNUC__ == 3 && __GNUC_MINOR__ >= 1 || __GNUC__ > 3 /* 3.1 -> */
2941 #    define HASATTRIBUTE_DEPRECATED
2942 #  endif
2943 #  if __GNUC__ >= 3 /* 3.0 -> */ /* XXX Verify this version */
2944 #    define HASATTRIBUTE_FORMAT
2945 #    if defined __MINGW32__
2946 #      define PRINTF_FORMAT_NULL_OK
2947 #    endif
2948 #  endif
2949 #  if __GNUC__ >= 3 /* 3.0 -> */
2950 #    define HASATTRIBUTE_MALLOC
2951 #  endif
2952 #  if __GNUC__ == 3 && __GNUC_MINOR__ >= 3 || __GNUC__ > 3 /* 3.3 -> */
2953 #    define HASATTRIBUTE_NONNULL
2954 #  endif
2955 #  if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 || __GNUC__ > 2 /* 2.5 -> */
2956 #    define HASATTRIBUTE_NORETURN
2957 #  endif
2958 #  if __GNUC__ >= 3 /* gcc 3.0 -> */
2959 #    define HASATTRIBUTE_PURE
2960 #  endif
2961 #  if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 || __GNUC__ > 3 /* 3.4 -> */
2962 #    define HASATTRIBUTE_UNUSED
2963 #  endif
2964 #  if __GNUC__ == 3 && __GNUC_MINOR__ == 3 && !defined(__cplusplus)
2965 #    define HASATTRIBUTE_UNUSED /* gcc-3.3, but not g++-3.3. */
2966 #  endif
2967 #  if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 || __GNUC__ > 3 /* 3.4 -> */
2968 #    define HASATTRIBUTE_WARN_UNUSED_RESULT
2969 #  endif
2970 #endif
2971 #endif /* #ifndef PERL_MICRO */
2972
2973 /* USE_5005THREADS needs to be after unixish.h as <pthread.h> includes
2974  * <sys/signal.h> which defines NSIG - which will stop inclusion of <signal.h>
2975  * this results in many functions being undeclared which bothers C++
2976  * May make sense to have threads after "*ish.h" anyway
2977  */
2978
2979 /* clang Thread Safety Analysis/Annotations/Attributes
2980  * http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
2981  *
2982  * Available since clang 3.6-ish (appeared in 3.4, but shaky still in 3.5).
2983  * Apple XCode hijacks __clang_major__ and __clang_minor__
2984  * (6.1 means really clang 3.6), so needs extra hijinks
2985  * (could probably also test the contents of __apple_build_version__).
2986  */
2987 #if defined(USE_ITHREADS) && defined(I_PTHREAD) && \
2988     defined(__clang__) && \
2989     !defined(PERL_GLOBAL_STRUCT) && \
2990     !defined(PERL_GLOBAL_STRUCT_PRIVATE) && \
2991     !defined(SWIG) && \
2992   ((!defined(__apple_build_version__) &&               \
2993     ((__clang_major__ == 3 && __clang_minor__ >= 6) || \
2994      (__clang_major__ >= 4))) || \
2995    (defined(__apple_build_version__) &&                \
2996     ((__clang_major__ == 6 && __clang_minor__ >= 1) || \
2997      (__clang_major__ >= 7))))
2998 #  define PERL_TSA__(x)   __attribute__((x))
2999 #  define PERL_TSA_ACTIVE
3000 #else
3001 #  define PERL_TSA__(x)   /* No TSA, make TSA attributes no-ops. */
3002 #  undef PERL_TSA_ACTIVE
3003 #endif
3004
3005 /* PERL_TSA_CAPABILITY() is used to annotate typedefs.
3006  * typedef old_type PERL_TSA_CAPABILITY("mutex") new_type;
3007  */
3008 #define PERL_TSA_CAPABILITY(x) \
3009     PERL_TSA__(capability(x))
3010
3011 /* In the below examples the mutex must be lexically visible, usually
3012  * either as global variables, or as function arguments. */
3013
3014 /* PERL_TSA_GUARDED_BY() is used to annotate global variables.
3015  *
3016  * Foo foo PERL_TSA_GUARDED_BY(mutex);
3017  */
3018 #define PERL_TSA_GUARDED_BY(x) \
3019     PERL_TSA__(guarded_by(x))
3020
3021 /* PERL_TSA_PT_GUARDED_BY() is used to annotate global pointers.
3022  * The data _behind_ the pointer is guarded.
3023  *
3024  * Foo* ptr PERL_TSA_PT_GUARDED_BY(mutex);
3025  */
3026 #define PERL_TSA_PT_GUARDED_BY(x) \
3027     PERL_TSA__(pt_guarded_by(x))
3028
3029 /* PERL_TSA_REQUIRES() is used to annotate functions.
3030  * The caller MUST hold the resource when calling the function.
3031  *
3032  * void Foo() PERL_TSA_REQUIRES(mutex);
3033  */
3034 #define PERL_TSA_REQUIRES(x) \
3035     PERL_TSA__(requires_capability(x))
3036
3037 /* PERL_TSA_EXCLUDES() is used to annotate functions.
3038  * The caller MUST NOT hold resource when calling the function.
3039  *
3040  * EXCLUDES should be used when the function first acquires
3041  * the resource and then releases it.  Use to avoid deadlock.
3042  *
3043  * void Foo() PERL_TSA_EXCLUDES(mutex);
3044  */
3045 #define PERL_TSA_EXCLUDES(x) \
3046     PERL_TSA__(locks_excluded(x))
3047
3048 /* PERL_TSA_ACQUIRE() is used to annotate functions.
3049  * The caller MUST NOT hold the resource when calling the function,
3050  * and the function will acquire the resource.
3051  *
3052  * void Foo() PERL_TSA_ACQUIRE(mutex);
3053  */
3054 #define PERL_TSA_ACQUIRE(x) \
3055     PERL_TSA__(acquire_capability(x))
3056
3057 /* PERL_TSA_RELEASE() is used to annotate functions.
3058  * The caller MUST hold the resource when calling the function,
3059  * and the function will release the resource.
3060  *
3061  * void Foo() PERL_TSA_RELEASE(mutex);
3062  */
3063 #define PERL_TSA_RELEASE(x) \
3064     PERL_TSA__(release_capability(x))
3065
3066 /* PERL_TSA_NO_TSA is used to annotate functions.
3067  * Used when being intentionally unsafe, or when the code is too
3068  * complicated for the analysis.  Use sparingly.
3069  *
3070  * void Foo() PERL_TSA_NO_TSA;
3071  */
3072 #define PERL_TSA_NO_TSA \
3073     PERL_TSA__(no_thread_safety_analysis)
3074
3075 /* There are more annotations/attributes available, see the clang
3076  * documentation for details. */
3077
3078 #if defined(USE_ITHREADS)
3079 #  ifdef NETWARE
3080 #    include <nw5thread.h>
3081 #  elif defined(WIN32)
3082 #    include <win32thread.h>
3083 #  elif defined(OS2)
3084 #    include "os2thread.h"
3085 #  elif defined(I_MACH_CTHREADS)
3086 #    include <mach/cthreads.h>
3087 typedef cthread_t       perl_os_thread;
3088 typedef mutex_t         perl_mutex;
3089 typedef condition_t     perl_cond;
3090 typedef void *          perl_key;
3091 #  elif defined(I_PTHREAD) /* Posix threads */
3092 #    include <pthread.h>
3093 typedef pthread_t       perl_os_thread;
3094 typedef pthread_mutex_t PERL_TSA_CAPABILITY("mutex") perl_mutex;
3095 typedef pthread_cond_t  perl_cond;
3096 typedef pthread_key_t   perl_key;
3097 #  endif
3098 #endif /* USE_ITHREADS */
3099
3100 #ifdef PERL_TSA_ACTIVE
3101 /* Since most pthread mutex interfaces have not been annotated, we
3102  * need to have these wrappers. The NO_TSA annotation is quite ugly
3103  * but it cannot be avoided in plain C, unlike in C++, where one could
3104  * e.g. use ACQUIRE() with no arg on a mutex lock method.
3105  *
3106  * The bodies of these wrappers are in util.c
3107  *
3108  * TODO: however, some platforms are starting to get these clang
3109  * thread safety annotations for pthreads, for example FreeBSD.
3110  * Do we need a way to a bypass these wrappers? */
3111 EXTERN_C int perl_tsa_mutex_lock(perl_mutex* mutex)
3112   PERL_TSA_ACQUIRE(*mutex)
3113   PERL_TSA_NO_TSA;
3114 EXTERN_C int perl_tsa_mutex_unlock(perl_mutex* mutex)
3115   PERL_TSA_RELEASE(*mutex)
3116   PERL_TSA_NO_TSA;
3117 #endif
3118
3119 #if defined(WIN32)
3120 #  include "win32.h"
3121 #endif
3122
3123 #ifdef NETWARE
3124 #  include "netware.h"
3125 #endif
3126
3127 #define STATUS_UNIX     PL_statusvalue
3128 #ifdef VMS
3129 #   define STATUS_NATIVE        PL_statusvalue_vms
3130 /*
3131  * vaxc$errno is only guaranteed to be valid if errno == EVMSERR, otherwise
3132  * its contents can not be trusted.  Unfortunately, Perl seems to check
3133  * it on exit, so it when PL_statusvalue_vms is updated, vaxc$errno should
3134  * be updated also.
3135  */
3136 #  include <stsdef.h>
3137 #  include <ssdef.h>
3138 /* Presume this because if VMS changes it, it will require a new
3139  * set of APIs for waiting on children for binary compatibility.
3140  */
3141 #  define child_offset_bits (8)
3142 #  ifndef C_FAC_POSIX
3143 #  define C_FAC_POSIX 0x35A000
3144 #  endif
3145
3146 /*  STATUS_EXIT - validates and returns a NATIVE exit status code for the
3147  * platform from the existing UNIX or Native status values.
3148  */
3149
3150 #   define STATUS_EXIT \
3151         (((I32)PL_statusvalue_vms == -1 ? SS$_ABORT : PL_statusvalue_vms) | \
3152            (VMSISH_HUSHED ? STS$M_INHIB_MSG : 0))
3153
3154
3155 /* STATUS_NATIVE_CHILD_SET - Calculate UNIX status that matches the child
3156  * exit code and shifts the UNIX value over the correct number of bits to
3157  * be a child status.  Usually the number of bits is 8, but that could be
3158  * platform dependent.  The NATIVE status code is presumed to have either
3159  * from a child process.
3160  */
3161
3162 /* This is complicated.  The child processes return a true native VMS
3163    status which must be saved.  But there is an assumption in Perl that
3164    the UNIX child status has some relationship to errno values, so
3165    Perl tries to translate it to text in some of the tests.
3166    In order to get the string translation correct, for the error, errno
3167    must be EVMSERR, but that generates a different text message
3168    than what the test programs are expecting.  So an errno value must
3169    be derived from the native status value when an error occurs.
3170    That will hide the true native status message.  With this version of
3171    perl, the true native child status can always be retrieved so that
3172    is not a problem.  But in this case, Pl_statusvalue and errno may
3173    have different values in them.
3174  */
3175
3176 #   define STATUS_NATIVE_CHILD_SET(n) \
3177         STMT_START {                                                    \
3178             I32 evalue = (I32)n;                                        \
3179             if (evalue == EVMSERR) {                                    \
3180               PL_statusvalue_vms = vaxc$errno;                          \
3181               PL_statusvalue = evalue;                                  \
3182             } else {                                                    \
3183               PL_statusvalue_vms = evalue;                              \
3184               if (evalue == -1) {                                       \
3185                 PL_statusvalue = -1;                                    \
3186                 PL_statusvalue_vms = SS$_ABORT; /* Should not happen */ \
3187               } else                                                    \
3188                 PL_statusvalue = Perl_vms_status_to_unix(evalue, 1);    \
3189               set_vaxc_errno(evalue);                                   \
3190               if ((PL_statusvalue_vms & C_FAC_POSIX) == C_FAC_POSIX)    \
3191                   set_errno(EVMSERR);                                   \
3192               else set_errno(Perl_vms_status_to_unix(evalue, 0));       \
3193               PL_statusvalue = PL_statusvalue << child_offset_bits;     \
3194             }                                                           \
3195         } STMT_END
3196
3197 #   ifdef VMSISH_STATUS
3198 #       define STATUS_CURRENT   (VMSISH_STATUS ? STATUS_NATIVE : STATUS_UNIX)
3199 #   else
3200 #       define STATUS_CURRENT   STATUS_UNIX
3201 #   endif
3202
3203   /* STATUS_UNIX_SET - takes a UNIX/POSIX errno value and attempts to update
3204    * the NATIVE status to an equivalent value.  Can not be used to translate
3205    * exit code values as exit code values are not guaranteed to have any
3206    * relationship at all to errno values.
3207    * This is used when Perl is forcing errno to have a specific value.
3208    */
3209 #   define STATUS_UNIX_SET(n)                           \
3210         STMT_START {                                    \
3211             I32 evalue = (I32)n;                        \
3212             PL_statusvalue = evalue;                    \
3213             if (PL_statusvalue != -1) {                 \
3214                 if (PL_statusvalue != EVMSERR) {        \
3215                   PL_statusvalue &= 0xFFFF;             \
3216                   if (MY_POSIX_EXIT)                    \
3217                     PL_statusvalue_vms=PL_statusvalue ? SS$_ABORT : SS$_NORMAL;\
3218                   else PL_statusvalue_vms = Perl_unix_status_to_vms(evalue); \
3219                 }                                       \
3220                 else {                                  \
3221                   PL_statusvalue_vms = vaxc$errno;      \
3222                 }                                       \
3223             }                                           \
3224             else PL_statusvalue_vms = SS$_ABORT;        \
3225             set_vaxc_errno(PL_statusvalue_vms);         \
3226         } STMT_END
3227
3228   /* STATUS_UNIX_EXIT_SET - Takes a UNIX/POSIX exit code and sets
3229    * the NATIVE error status based on it.
3230    *
3231    * When in the default mode to comply with the Perl VMS documentation,
3232    * 0 is a success and any other code sets the NATIVE status to a failure
3233    * code of SS$_ABORT.
3234    *
3235    * In the new POSIX EXIT mode, native status will be set so that the
3236    * actual exit code will can be retrieved by the calling program or
3237    * shell.
3238    *
3239    * If the exit code is not clearly a UNIX parent or child exit status,
3240    * it will be passed through as a VMS status.
3241    */
3242
3243 #   define STATUS_UNIX_EXIT_SET(n)                      \
3244         STMT_START {                                    \
3245             I32 evalue = (I32)n;                        \
3246             PL_statusvalue = evalue;                    \
3247             if (MY_POSIX_EXIT) { \
3248               if (evalue <= 0xFF00) {           \
3249                   if (evalue > 0xFF)                    \
3250                     evalue = (evalue >> child_offset_bits) & 0xFF; \
3251                   PL_statusvalue_vms =          \
3252                     (C_FAC_POSIX | (evalue << 3 ) |     \
3253                     ((evalue == 1) ? (STS$K_ERROR | STS$M_INHIB_MSG) : 1)); \
3254               } else /* forgive them Perl, for they have sinned */ \
3255                 PL_statusvalue_vms = evalue; \
3256             } else { \
3257               if (evalue == 0)                  \
3258                 PL_statusvalue_vms = SS$_NORMAL;        \
3259               else if (evalue <= 0xFF00) \
3260                 PL_statusvalue_vms = SS$_ABORT; \
3261               else { /* forgive them Perl, for they have sinned */ \
3262                   if (evalue != EVMSERR) PL_statusvalue_vms = evalue; \
3263                   else PL_statusvalue_vms = vaxc$errno; \
3264                   /* And obviously used a VMS status value instead of UNIX */ \
3265                   PL_statusvalue = EVMSERR;             \
3266               } \
3267               set_vaxc_errno(PL_statusvalue_vms);       \
3268             }                                           \
3269         } STMT_END
3270
3271
3272   /* STATUS_EXIT_SET - Takes a NATIVE/UNIX/POSIX exit code
3273    * and sets the NATIVE error status based on it.  This special case
3274    * is needed to maintain compatibility with past VMS behavior.
3275    *
3276    * In the default mode on VMS, this number is passed through as
3277    * both the NATIVE and UNIX status.  Which makes it different
3278    * that the STATUS_UNIX_EXIT_SET.
3279    *
3280    * In the new POSIX EXIT mode, native status will be set so that the
3281    * actual exit code will can be retrieved by the calling program or
3282    * shell.
3283    *
3284    * A POSIX exit code is from 0 to 255.  If the exit code is higher
3285    * than this, it needs to be assumed that it is a VMS exit code and
3286    * passed through.
3287    */
3288
3289 #   define STATUS_EXIT_SET(n)                           \
3290         STMT_START {                                    \
3291             I32 evalue = (I32)n;                        \
3292             PL_statusvalue = evalue;                    \
3293             if (MY_POSIX_EXIT)                          \
3294                 if (evalue > 255) PL_statusvalue_vms = evalue; else {   \
3295                   PL_statusvalue_vms = \
3296                     (C_FAC_POSIX | (evalue << 3 ) |     \
3297                      ((evalue == 1) ? (STS$K_ERROR | STS$M_INHIB_MSG) : 1));} \
3298             else                                        \
3299                 PL_statusvalue_vms = evalue ? evalue : SS$_NORMAL; \
3300             set_vaxc_errno(PL_statusvalue_vms);         \
3301         } STMT_END
3302
3303
3304  /* This macro forces a success status */
3305 #   define STATUS_ALL_SUCCESS   \
3306         (PL_statusvalue = 0, PL_statusvalue_vms = SS$_NORMAL)
3307
3308  /* This macro forces a failure status */
3309 #   define STATUS_ALL_FAILURE   (PL_statusvalue = 1, \
3310      vaxc$errno = PL_statusvalue_vms = MY_POSIX_EXIT ? \
3311         (C_FAC_POSIX | (1 << 3) | STS$K_ERROR | STS$M_INHIB_MSG) : SS$_ABORT)
3312
3313 #elif defined(__amigaos4__)
3314  /* A somewhat experimental attempt to simulate posix return code values */
3315 #   define STATUS_NATIVE        PL_statusvalue_posix
3316 #   define STATUS_NATIVE_CHILD_SET(n)                      \
3317         STMT_START {                                       \
3318             PL_statusvalue_posix = (n);                    \
3319             if (PL_statusvalue_posix < 0) {                \
3320                 PL_statusvalue = -1;                       \
3321             }                                              \
3322             else {                                         \
3323                 PL_statusvalue = n << 8;                   \
3324             }                                              \
3325         } STMT_END
3326 #   define STATUS_UNIX_SET(n)           \
3327         STMT_START {                    \
3328             PL_statusvalue = (n);               \
3329             if (PL_statusvalue != -1)   \
3330                 PL_statusvalue &= 0xFFFF;       \
3331         } STMT_END
3332 #   define STATUS_UNIX_EXIT_SET(n) STATUS_UNIX_SET(n)
3333 #   define STATUS_EXIT_SET(n) STATUS_UNIX_SET(n)
3334 #   define STATUS_CURRENT STATUS_UNIX
3335 #   define STATUS_EXIT STATUS_UNIX
3336 #   define STATUS_ALL_SUCCESS   (PL_statusvalue = 0, PL_statusvalue_posix = 0)
3337 #   define STATUS_ALL_FAILURE   (PL_statusvalue = 1, PL_statusvalue_posix = 1)
3338
3339 #else
3340 #   define STATUS_NATIVE        PL_statusvalue_posix
3341 #   if defined(WCOREDUMP)
3342 #       define STATUS_NATIVE_CHILD_SET(n)                  \
3343             STMT_START {                                   \
3344                 PL_statusvalue_posix = (n);                \
3345                 if (PL_statusvalue_posix == -1)            \
3346                     PL_statusvalue = -1;                   \
3347                 else {                                     \
3348                     PL_statusvalue =                       \
3349                         (WIFEXITED(PL_statusvalue_posix) ? (WEXITSTATUS(PL_statusvalue_posix) << 8) : 0) |  \
3350                         (WIFSIGNALED(PL_statusvalue_posix) ? (WTERMSIG(PL_statusvalue_posix) & 0x7F) : 0) | \
3351                         (WIFSIGNALED(PL_statusvalue_posix) && WCOREDUMP(PL_statusvalue_posix) ? 0x80 : 0);  \
3352                 }                                          \
3353             } STMT_END
3354 #   elif defined(WIFEXITED)
3355 #       define STATUS_NATIVE_CHILD_SET(n)                  \
3356             STMT_START {                                   \
3357                 PL_statusvalue_posix = (n);                \
3358                 if (PL_statusvalue_posix == -1)            \
3359                     PL_statusvalue = -1;                   \
3360                 else {                                     \
3361                     PL_statusvalue =                       \
3362                         (WIFEXITED(PL_statusvalue_posix) ? (WEXITSTATUS(PL_statusvalue_posix) << 8) : 0) |  \
3363                         (WIFSIGNALED(PL_statusvalue_posix) ? (WTERMSIG(PL_statusvalue_posix) & 0x7F) : 0);  \
3364                 }                                          \
3365             } STMT_END
3366 #   else
3367 #       define STATUS_NATIVE_CHILD_SET(n)                  \
3368             STMT_START {                                   \
3369                 PL_statusvalue_posix = (n);                \
3370                 if (PL_statusvalue_posix == -1)            \
3371                     PL_statusvalue = -1;                   \
3372                 else {                                     \
3373                     PL_statusvalue =                       \
3374                         PL_statusvalue_posix & 0xFFFF;     \
3375                 }                                          \
3376             } STMT_END
3377 #   endif
3378 #   define STATUS_UNIX_SET(n)           \
3379         STMT_START {                    \
3380             PL_statusvalue = (n);               \
3381             if (PL_statusvalue != -1)   \
3382                 PL_statusvalue &= 0xFFFF;       \
3383         } STMT_END
3384 #   define STATUS_UNIX_EXIT_SET(n) STATUS_UNIX_SET(n)
3385 #   define STATUS_EXIT_SET(n) STATUS_UNIX_SET(n)
3386 #   define STATUS_CURRENT STATUS_UNIX
3387 #   define STATUS_EXIT STATUS_UNIX
3388 #   define STATUS_ALL_SUCCESS   (PL_statusvalue = 0, PL_statusvalue_posix = 0)
3389 #   define STATUS_ALL_FAILURE   (PL_statusvalue = 1, PL_statusvalue_posix = 1)
3390 #endif
3391
3392 /* flags in PL_exit_flags for nature of exit() */
3393 #define PERL_EXIT_EXPECTED      0x01
3394 #define PERL_EXIT_DESTRUCT_END  0x02  /* Run END in perl_destruct */
3395 #define PERL_EXIT_WARN          0x04  /* Warn if Perl_my_exit() or Perl_my_failure_exit() called */
3396 #define PERL_EXIT_ABORT         0x08  /* Call abort() if Perl_my_exit() or Perl_my_failure_exit() called */
3397
3398 #ifndef PERL_CORE
3399 /* format to use for version numbers in file/directory names */
3400 /* XXX move to Configure? */
3401 /* This was only ever used for the current version, and that can be done at
3402    compile time, as PERL_FS_VERSION, so should we just delete it?  */
3403 #  ifndef PERL_FS_VER_FMT
3404 #    define PERL_FS_VER_FMT     "%d.%d.%d"
3405 #  endif
3406 #endif
3407
3408 #ifndef PERL_FS_VERSION
3409 #  define PERL_FS_VERSION       PERL_VERSION_STRING
3410 #endif
3411
3412 /* This defines a way to flush all output buffers.  This may be a
3413  * performance issue, so we allow people to disable it.  Also, if
3414  * we are using stdio, there are broken implementations of fflush(NULL)
3415  * out there, Solaris being the most prominent.
3416  */
3417 #ifndef PERL_FLUSHALL_FOR_CHILD
3418 # if defined(USE_PERLIO) || defined(FFLUSH_NULL)
3419 #  define PERL_FLUSHALL_FOR_CHILD       PerlIO_flush((PerlIO*)NULL)
3420 # elif defined(FFLUSH_ALL)
3421 #  define PERL_FLUSHALL_FOR_CHILD       my_fflush_all()
3422 # else
3423 #  define PERL_FLUSHALL_FOR_CHILD       NOOP
3424 # endif
3425 #endif
3426
3427 #ifndef PERL_WAIT_FOR_CHILDREN
3428 #  define PERL_WAIT_FOR_CHILDREN        NOOP
3429 #endif
3430
3431 /* the traditional thread-unsafe notion of "current interpreter". */
3432 #ifndef PERL_SET_INTERP
3433 #  define PERL_SET_INTERP(i)            (PL_curinterp = (PerlInterpreter*)(i))
3434 #endif
3435
3436 #ifndef PERL_GET_INTERP
3437 #  define PERL_GET_INTERP               (PL_curinterp)
3438 #endif
3439
3440 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_GET_THX)
3441 #  ifdef MULTIPLICITY
3442 #    define PERL_GET_THX                ((PerlInterpreter *)PERL_GET_CONTEXT)
3443 #  endif
3444 #  define PERL_SET_THX(t)               PERL_SET_CONTEXT(t)
3445 #endif
3446
3447 /*
3448     This replaces the previous %_ "hack" by the "%p" hacks.
3449     All that is required is that the perl source does not
3450     use "%-p" or "%-<number>p" or "%<number>p" formats.
3451     These formats will still work in perl code.
3452     See comments in sv.c for further details.
3453
3454     Robin Barker 2005-07-14
3455
3456     No longer use %1p for VDf = %vd.  RMB 2007-10-19
3457 */
3458
3459 #ifndef SVf_
3460 #  define SVf_(n) "-" STRINGIFY(n) "p"
3461 #endif
3462
3463 #ifndef SVf
3464 #  define SVf "-p"
3465 #endif
3466
3467 #ifndef SVf32
3468 #  define SVf32 SVf_(32)
3469 #endif
3470
3471 #ifndef SVf256
3472 #  define SVf256 SVf_(256)
3473 #endif
3474
3475 #define SVfARG(p) ((void*)(p))
3476
3477 #ifndef HEKf
3478 #  define HEKf "2p"
3479 #endif
3480
3481 /* Not ideal, but we cannot easily include a number in an already-numeric
3482  * format sequence. */
3483 #ifndef HEKf256
3484 #  define HEKf256 "3p"
3485 #endif
3486
3487 #define HEKfARG(p) ((void*)(p))
3488
3489 /* Takes three arguments: is_utf8, length, str */
3490 #ifndef UTF8f
3491 #  define UTF8f "d%" UVuf "%4p"
3492 #endif
3493 #define UTF8fARG(u,l,p) (int)cBOOL(u), (UV)(l), (void*)(p)
3494
3495 #define PNf UTF8f
3496 #define PNfARG(pn) (int)1, (UV)PadnameLEN(pn), (void *)PadnamePV(pn)
3497
3498 #ifdef PERL_CORE
3499 /* not used; but needed for backward compatibility with XS code? - RMB */
3500 #  undef UVf
3501 #elif !defined(UVf)
3502 #  define UVf UVuf
3503 #endif
3504
3505 #ifdef HASATTRIBUTE_DEPRECATED
3506 #  define __attribute__deprecated__         __attribute__((deprecated))
3507 #endif
3508 #ifdef HASATTRIBUTE_FORMAT
3509 #  define __attribute__format__(x,y,z)      __attribute__((format(x,y,z)))
3510 #endif
3511 #ifdef HASATTRIBUTE_MALLOC
3512 #  define __attribute__malloc__             __attribute__((__malloc__))
3513 #endif
3514 #ifdef HASATTRIBUTE_NONNULL
3515 #  define __attribute__nonnull__(a)         __attribute__((nonnull(a)))
3516 #endif
3517 #ifdef HASATTRIBUTE_NORETURN
3518 #  define __attribute__noreturn__           __attribute__((noreturn))
3519 #endif
3520 #ifdef HASATTRIBUTE_PURE
3521 #  define __attribute__pure__               __attribute__((pure))
3522 #endif
3523 #ifdef HASATTRIBUTE_UNUSED
3524 #  define __attribute__unused__             __attribute__((unused))
3525 #endif
3526 #ifdef HASATTRIBUTE_WARN_UNUSED_RESULT
3527 #  define __attribute__warn_unused_result__ __attribute__((warn_unused_result))
3528 #endif
3529
3530 /* If we haven't defined the attributes yet, define them to blank. */
3531 #ifndef __attribute__deprecated__
3532 #  define __attribute__deprecated__
3533 #endif
3534 #ifndef __attribute__format__
3535 #  define __attribute__format__(x,y,z)
3536 #endif
3537 #ifndef __attribute__malloc__
3538 #  define __attribute__malloc__
3539 #endif
3540 #ifndef __attribute__nonnull__
3541 #  define __attribute__nonnull__(a)
3542 #endif
3543 #ifndef __attribute__noreturn__
3544 #  define __attribute__noreturn__
3545 #endif
3546 #ifndef __attribute__pure__
3547 #  define __attribute__pure__
3548 #endif
3549 #ifndef __attribute__unused__
3550 #  define __attribute__unused__
3551 #endif
3552 #ifndef __attribute__warn_unused_result__
3553 #  define __attribute__warn_unused_result__
3554 #endif
3555
3556 #if !defined(DEBUGGING) && !defined(NDEBUG)
3557 #  define NDEBUG 1
3558 #endif
3559 #include <assert.h>
3560
3561 /* For functions that are marked as __attribute__noreturn__, it's not
3562    appropriate to call return.  In either case, include the lint directive.
3563  */
3564 #ifdef HASATTRIBUTE_NORETURN
3565 #  define NORETURN_FUNCTION_END NOT_REACHED;
3566 #else
3567 #  define NORETURN_FUNCTION_END NOT_REACHED; return 0
3568 #endif
3569
3570 /* Some OS warn on NULL format to printf */
3571 #ifdef PRINTF_FORMAT_NULL_OK
3572 #  define __attribute__format__null_ok__(x,y,z)  __attribute__format__(x,y,z)
3573 #else
3574 #  define __attribute__format__null_ok__(x,y,z)
3575 #endif
3576
3577 #ifdef HAS_BUILTIN_EXPECT
3578 #  define EXPECT(expr,val)                  __builtin_expect(expr,val)
3579 #else
3580 #  define EXPECT(expr,val)                  (expr)
3581 #endif
3582
3583 /*
3584 =head1 Miscellaneous Functions
3585
3586 =for apidoc AmU|bool|LIKELY|const bool expr
3587
3588 Returns the input unchanged, but at the same time it gives a branch prediction
3589 hint to the compiler that this condition is likely to be true.
3590
3591 =for apidoc AmU|bool|UNLIKELY|const bool expr
3592
3593 Returns the input unchanged, but at the same time it gives a branch prediction
3594 hint to the compiler that this condition is likely to be false.
3595
3596 =cut
3597 */
3598 #define LIKELY(cond)                        EXPECT(cBOOL(cond),TRUE)
3599 #define UNLIKELY(cond)                      EXPECT(cBOOL(cond),FALSE)
3600
3601 #ifdef HAS_BUILTIN_CHOOSE_EXPR
3602 /* placeholder */
3603 #endif
3604
3605 /* STATIC_ASSERT_DECL/STATIC_ASSERT_STMT are like assert(), but for compile
3606    time invariants. That is, their argument must be a constant expression that
3607    can be verified by the compiler. This expression can contain anything that's
3608    known to the compiler, e.g. #define constants, enums, or sizeof (...). If
3609    the expression evaluates to 0, compilation fails.
3610    Because they generate no runtime code (i.e.  their use is "free"), they're
3611    always active, even under non-DEBUGGING builds.
3612    STATIC_ASSERT_DECL expands to a declaration and is suitable for use at
3613    file scope (outside of any function).
3614    STATIC_ASSERT_STMT expands to a statement and is suitable for use inside a
3615    function.
3616 */
3617 #if (defined(static_assert) || (defined(__cplusplus) && __cplusplus >= 201103L)) && (!defined(__IBMC__) || __IBMC__ >= 1210)
3618 /* static_assert is a macro defined in <assert.h> in C11 or a compiler
3619    builtin in C++11.  But IBM XL C V11 does not support _Static_assert, no
3620    matter what <assert.h> says.
3621 */
3622 #  define STATIC_ASSERT_DECL(COND) static_assert(COND, #COND)
3623 #else
3624 /* We use a bit-field instead of an array because gcc accepts
3625    'typedef char x[n]' where n is not a compile-time constant.
3626    We want to enforce constantness.
3627 */
3628 #  define STATIC_ASSERT_2(COND, SUFFIX) \
3629     typedef struct { \
3630         unsigned int _static_assertion_failed_##SUFFIX : (COND) ? 1 : -1; \
3631     } _static_assertion_failed_##SUFFIX PERL_UNUSED_DECL
3632 #  define STATIC_ASSERT_1(COND, SUFFIX) STATIC_ASSERT_2(COND, SUFFIX)
3633 #  define STATIC_ASSERT_DECL(COND)    STATIC_ASSERT_1(COND, __LINE__)
3634 #endif
3635 /* We need this wrapper even in C11 because 'case X: static_assert(...);' is an
3636    error (static_assert is a declaration, and only statements can have labels).
3637 */
3638 #define STATIC_ASSERT_STMT(COND)      STMT_START { STATIC_ASSERT_DECL(COND); } STMT_END
3639
3640 #ifndef __has_builtin
3641 #  define __has_builtin(x) 0 /* not a clang style compiler */
3642 #endif
3643
3644 /* ASSUME is like assert(), but it has a benefit in a release build. It is a
3645    hint to a compiler about a statement of fact in a function call free
3646    expression, which allows the compiler to generate better machine code.
3647    In a debug build, ASSUME(x) is a synonym for assert(x). ASSUME(0) means
3648    the control path is unreachable. In a for loop, ASSUME can be used to hint
3649    that a loop will run at least X times. ASSUME is based off MSVC's __assume
3650    intrinsic function, see its documents for more details.
3651 */
3652
3653 #ifndef DEBUGGING
3654 #  if __has_builtin(__builtin_unreachable) \
3655      || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5 || __GNUC__ > 4) /* 4.5 -> */
3656 #    define ASSUME(x) ((x) ? (void) 0 : __builtin_unreachable())
3657 #  elif defined(_MSC_VER)
3658 #    define ASSUME(x) __assume(x)
3659 #  elif defined(__ARMCC_VERSION) /* untested */
3660 #    define ASSUME(x) __promise(x)
3661 #  else
3662 /* a random compiler might define assert to its own special optimization token
3663    so pass it through to C lib as a last resort */
3664 #    define ASSUME(x) assert(x)
3665 #  endif
3666 #else
3667 #  define ASSUME(x) assert(x)
3668 #endif
3669
3670 #if defined(__sun)      /* ASSUME() generates warnings on Solaris */
3671 #  define NOT_REACHED
3672 #elif defined(DEBUGGING) && (__has_builtin(__builtin_unreachable) \
3673      || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5 || __GNUC__ > 4)) /* 4.5 -> */
3674 #  define NOT_REACHED STMT_START { ASSUME(!"UNREACHABLE"); __builtin_unreachable(); } STMT_END
3675 #else
3676 #  define NOT_REACHED ASSUME(!"UNREACHABLE")
3677 #endif
3678
3679 /* Some unistd.h's give a prototype for pause() even though
3680    HAS_PAUSE ends up undefined.  This causes the #define
3681    below to be rejected by the compiler.  Sigh.
3682 */
3683 #ifdef HAS_PAUSE
3684 #define Pause   pause
3685 #else
3686 #define Pause() sleep((32767<<16)+32767)
3687 #endif
3688
3689 #ifndef IOCPARM_LEN
3690 #   ifdef IOCPARM_MASK
3691         /* on BSDish systems we're safe */
3692 #       define IOCPARM_LEN(x)  (((x) >> 16) & IOCPARM_MASK)
3693 #   elif defined(_IOC_SIZE) && defined(__GLIBC__)
3694         /* on Linux systems we're safe; except when we're not [perl #38223] */
3695 #       define IOCPARM_LEN(x) (_IOC_SIZE(x) < 256 ? 256 : _IOC_SIZE(x))
3696 #   else
3697         /* otherwise guess at what's safe */
3698 #       define IOCPARM_LEN(x)   256
3699 #   endif
3700 #endif
3701
3702 #if defined(__CYGWIN__)
3703 /* USEMYBINMODE
3704  *   This symbol, if defined, indicates that the program should
3705  *   use the routine my_binmode(FILE *fp, char iotype, int mode) to insure
3706  *   that a file is in "binary" mode -- that is, that no translation
3707  *   of bytes occurs on read or write operations.
3708  */
3709 #  define USEMYBINMODE /**/
3710 #  include <io.h> /* for setmode() prototype */
3711 #  define my_binmode(fp, iotype, mode) \
3712             cBOOL(PerlLIO_setmode(fileno(fp), mode) != -1)
3713 #endif
3714
3715 #ifdef __CYGWIN__
3716 void init_os_extras(void);
3717 #endif
3718
3719 #ifdef UNION_ANY_DEFINITION
3720 UNION_ANY_DEFINITION;
3721 #else
3722 union any {
3723     void*       any_ptr;
3724     SV*         any_sv;
3725     SV**        any_svp;
3726     GV*         any_gv;
3727     AV*         any_av;
3728     HV*         any_hv;
3729     OP*         any_op;
3730     char*       any_pv;
3731     char**      any_pvp;
3732     I32         any_i32;
3733     U32         any_u32;
3734     IV          any_iv;
3735     UV          any_uv;
3736     long        any_long;
3737     bool        any_bool;
3738     void        (*any_dptr) (void*);
3739     void        (*any_dxptr) (pTHX_ void*);
3740 };
3741 #endif
3742
3743 typedef I32 (*filter_t) (pTHX_ int, SV *, int);
3744
3745 #define FILTER_READ(idx, sv, len)  filter_read(idx, sv, len)
3746 #define FILTER_DATA(idx) \
3747             (PL_parser ? AvARRAY(PL_parser->rsfp_filters)[idx] : NULL)
3748 #define FILTER_ISREADER(idx) \
3749             (PL_parser && PL_parser->rsfp_filters \
3750                 && idx >= AvFILLp(PL_parser->rsfp_filters))
3751 #define PERL_FILTER_EXISTS(i) \
3752             (PL_parser && PL_parser->rsfp_filters \
3753                 && (i) <= av_tindex(PL_parser->rsfp_filters))
3754
3755 #if defined(_AIX) && !defined(_AIX43)
3756 #if defined(USE_REENTRANT) || defined(_REENTRANT) || defined(_THREAD_SAFE)
3757 /* We cannot include <crypt.h> to get the struct crypt_data
3758  * because of setkey prototype problems when threading */
3759 typedef        struct crypt_data {     /* straight from /usr/include/crypt.h */
3760     /* From OSF, Not needed in AIX
3761        char C[28], D[28];
3762     */
3763     char E[48];
3764     char KS[16][48];
3765     char block[66];
3766     char iobuf[16];
3767 } CRYPTD;
3768 #endif /* threading */
3769 #endif /* AIX */
3770
3771 #ifndef PERL_CALLCONV
3772 #  ifdef __cplusplus
3773 #    define PERL_CALLCONV extern "C"
3774 #  else
3775 #    define PERL_CALLCONV
3776 #  endif
3777 #endif
3778 #ifndef PERL_CALLCONV_NO_RET
3779 #    define PERL_CALLCONV_NO_RET PERL_CALLCONV
3780 #endif
3781
3782 /* PERL_STATIC_NO_RET is supposed to be equivalent to STATIC on builds that
3783    dont have a noreturn as a declaration specifier
3784 */
3785 #ifndef PERL_STATIC_NO_RET
3786 #  define PERL_STATIC_NO_RET STATIC
3787 #endif
3788 /* PERL_STATIC_NO_RET is supposed to be equivalent to PERL_STATIC_INLINE on
3789    builds that dont have a noreturn as a declaration specifier
3790 */
3791 #ifndef PERL_STATIC_INLINE_NO_RET
3792 #  define PERL_STATIC_INLINE_NO_RET PERL_STATIC_INLINE
3793 #endif
3794
3795 #if !defined(OS2)
3796 #  include "iperlsys.h"
3797 #endif
3798
3799 #ifdef __LIBCATAMOUNT__
3800 #undef HAS_PASSWD  /* unixish.h but not unixish enough. */
3801 #undef HAS_GROUP
3802 #define FAKE_BIT_BUCKET
3803 #endif
3804
3805 /* [perl #22371] Algorimic Complexity Attack on Perl 5.6.1, 5.8.0.
3806  * Note that the USE_HASH_SEED and similar defines are *NOT* defined by
3807  * Configure, despite their names being similar to other defines like
3808  * USE_ITHREADS.  Configure in fact knows nothing about the randomised
3809  * hashes.  Therefore to enable/disable the hash randomisation defines
3810  * use the Configure -Accflags=... instead. */
3811 #if !defined(NO_HASH_SEED) && !defined(USE_HASH_SEED)
3812 #  define USE_HASH_SEED
3813 #endif
3814
3815 #include "perly.h"
3816
3817
3818 /* macros to define bit-fields in structs. */
3819 #ifndef PERL_BITFIELD8
3820 #  define PERL_BITFIELD8 unsigned
3821 #endif
3822 #ifndef PERL_BITFIELD16
3823 #  define PERL_BITFIELD16 unsigned
3824 #endif
3825 #ifndef PERL_BITFIELD32
3826 #  define PERL_BITFIELD32 unsigned
3827 #endif
3828
3829 #include "sv.h"
3830 #include "regexp.h"
3831 #include "util.h"
3832 #include "form.h"
3833 #include "gv.h"
3834 #include "pad.h"
3835 #include "cv.h"
3836 #include "opnames.h"
3837 #include "op.h"
3838 #include "hv.h"
3839 #include "cop.h"
3840 #include "av.h"
3841 #include "mg.h"
3842 #include "scope.h"
3843 #include "warnings.h"
3844 #include "utf8.h"
3845
3846 /* these would be in doio.h if there was such a file */
3847 #define my_stat()  my_stat_flags(SV_GMAGIC)
3848 #define my_lstat() my_lstat_flags(SV_GMAGIC)
3849
3850 /* defined in sv.c, but also used in [ach]v.c */
3851 #undef _XPV_HEAD
3852 #undef _XPVMG_HEAD
3853 #undef _XPVCV_COMMON
3854
3855 #include "parser.h"
3856
3857 typedef struct magic_state MGS; /* struct magic_state defined in mg.c */
3858
3859 #if defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C)
3860
3861 /* These have to be predeclared, as they are used in proto.h which is #included
3862  * before their definitions in regcomp.h. */
3863
3864 struct scan_data_t;
3865 typedef struct regnode_charclass regnode_charclass;
3866
3867 /* A hopefully less confusing name.  The sub-classes are all Posix classes only
3868  * used under /l matching */
3869 typedef struct regnode_charclass_posixl regnode_charclass_class;
3870 typedef struct regnode_charclass_posixl regnode_charclass_posixl;
3871
3872 typedef struct regnode_ssc regnode_ssc;
3873 typedef struct RExC_state_t RExC_state_t;
3874 struct _reg_trie_data;
3875
3876 #endif
3877
3878 struct ptr_tbl_ent {
3879     struct ptr_tbl_ent*         next;
3880     const void*                 oldval;
3881     void*                       newval;
3882 };
3883
3884 struct ptr_tbl {
3885     struct ptr_tbl_ent**        tbl_ary;
3886     UV                          tbl_max;
3887     UV                          tbl_items;
3888     struct ptr_tbl_arena        *tbl_arena;
3889     struct ptr_tbl_ent          *tbl_arena_next;
3890     struct ptr_tbl_ent          *tbl_arena_end;
3891 };
3892
3893 #if defined(htonl) && !defined(HAS_HTONL)
3894 #define HAS_HTONL
3895 #endif
3896 #if defined(htons) && !defined(HAS_HTONS)
3897 #define HAS_HTONS
3898 #endif
3899 #if defined(ntohl) && !defined(HAS_NTOHL)
3900 #define HAS_NTOHL
3901 #endif
3902 #if defined(ntohs) && !defined(HAS_NTOHS)
3903 #define HAS_NTOHS
3904 #endif
3905 #ifndef HAS_HTONL
3906 #define HAS_HTONS
3907 #define HAS_HTONL
3908 #define HAS_NTOHS
3909 #define HAS_NTOHL
3910 #  if (BYTEORDER & 0xffff) == 0x4321
3911 /* Big endian system, so ntohl, ntohs, htonl and htons do not need to
3912    re-order their values. However, to behave identically to the alternative
3913    implementations, they should truncate to the correct size.  */
3914 #    define ntohl(x)    ((x)&0xFFFFFFFF)
3915 #    define htonl(x)    ntohl(x)
3916 #    define ntohs(x)    ((x)&0xFFFF)
3917 #    define htons(x)    ntohs(x)
3918 #  elif BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
3919
3920 /* Note that we can't straight out declare our own htonl and htons because
3921    the Win32 build process forcibly undefines HAS_HTONL etc for its miniperl,
3922    to avoid the overhead of initialising the socket subsystem, but the headers
3923    that *declare* the various functions are still seen. If we declare our own
3924    htonl etc they will clash with the declarations in the Win32 headers.  */
3925
3926 PERL_STATIC_INLINE U32
3927 my_swap32(const U32 x) {
3928     return ((x & 0xFF) << 24) | ((x >> 24) & 0xFF)      
3929         | ((x & 0x0000FF00) << 8) | ((x & 0x00FF0000) >> 8);
3930 }
3931
3932 PERL_STATIC_INLINE U16
3933 my_swap16(const U16 x) {
3934     return ((x & 0xFF) << 8) | ((x >> 8) & 0xFF);
3935 }
3936
3937 #    define htonl(x)    my_swap32(x)
3938 #    define ntohl(x)    my_swap32(x)
3939 #    define ntohs(x)    my_swap16(x)
3940 #    define htons(x)    my_swap16(x)
3941 #  else
3942 #    error "Unsupported byteorder"
3943 /* The C pre-processor doesn't let us return the value of BYTEORDER as part of
3944    the error message. Please check the value of the macro BYTEORDER, as defined
3945    in config.h. The values of BYTEORDER we expect are
3946
3947             big endian  little endian
3948    32 bit       0x4321  0x1234
3949    64 bit   0x87654321  0x12345678
3950
3951    If you have a system with a different byte order, please see
3952    pod/perlhack.pod for how to submit a patch to add supporting code.
3953 */
3954 #  endif
3955 #endif
3956
3957 /*
3958  * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
3959  * -DWS
3960  */
3961 #if BYTEORDER == 0x1234 || BYTEORDER == 0x12345678
3962 /* Little endian system, so vtohl, vtohs, htovl and htovs do not need to
3963    re-order their values. However, to behave identically to the alternative
3964    implementations, they should truncate to the correct size.  */
3965 #  define vtohl(x)      ((x)&0xFFFFFFFF)
3966 #  define vtohs(x)      ((x)&0xFFFF)
3967 #  define htovl(x)      vtohl(x)
3968 #  define htovs(x)      vtohs(x)
3969 #elif BYTEORDER == 0x4321 || BYTEORDER == 0x87654321
3970 #  define vtohl(x)      ((((x)&0xFF)<<24)       \
3971                         +(((x)>>24)&0xFF)       \
3972                         +(((x)&0x0000FF00)<<8)  \
3973                         +(((x)&0x00FF0000)>>8)  )
3974 #  define vtohs(x)      ((((x)&0xFF)<<8) + (((x)>>8)&0xFF))
3975 #  define htovl(x)      vtohl(x)
3976 #  define htovs(x)      vtohs(x)
3977 #else
3978 #  error "Unsupported byteorder"
3979 /* If you have need for current perl on PDP-11 or similar, and can help test
3980    that blead keeps working on a mixed-endian system, then see
3981    pod/perlhack.pod for how to submit patches to things working again.  */
3982 #endif
3983
3984 /* *MAX Plus 1. A floating point value.
3985    Hopefully expressed in a way that dodgy floating point can't mess up.
3986    >> 2 rather than 1, so that value is safely less than I32_MAX after 1
3987    is added to it
3988    May find that some broken compiler will want the value cast to I32.
3989    [after the shift, as signed >> may not be as secure as unsigned >>]
3990 */
3991 #define I32_MAX_P1 (2.0 * (1 + (((U32)I32_MAX) >> 1)))
3992 #define U32_MAX_P1 (4.0 * (1 + ((U32_MAX) >> 2)))
3993 /* For compilers that can't correctly cast NVs over 0x7FFFFFFF (or
3994    0x7FFFFFFFFFFFFFFF) to an unsigned integer. In the future, sizeof(UV)
3995    may be greater than sizeof(IV), so don't assume that half max UV is max IV.
3996 */
3997 #define U32_MAX_P1_HALF (2.0 * (1 + ((U32_MAX) >> 2)))
3998
3999 #define UV_MAX_P1 (4.0 * (1 + ((UV_MAX) >> 2)))
4000 #define IV_MAX_P1 (2.0 * (1 + (((UV)IV_MAX) >> 1)))
4001 #define UV_MAX_P1_HALF (2.0 * (1 + ((UV_MAX) >> 2)))
4002
4003 /* This may look like unnecessary jumping through hoops, but converting
4004    out of range floating point values to integers *is* undefined behaviour,
4005    and it is starting to bite.
4006 */
4007 #ifndef CAST_INLINE
4008 #define I_32(what) (cast_i32((NV)(what)))
4009 #define U_32(what) (cast_ulong((NV)(what)))
4010 #define I_V(what) (cast_iv((NV)(what)))
4011 #define U_V(what) (cast_uv((NV)(what)))
4012 #else
4013 #define I_32(n) ((n) < I32_MAX_P1 ? ((n) < I32_MIN ? I32_MIN : (I32) (n)) \
4014                   : ((n) < U32_MAX_P1 ? (I32)(U32) (n) \
4015                      : ((n) > 0 ? (I32) U32_MAX : 0 /* NaN */)))
4016 #define U_32(n) ((n) < 0.0 ? ((n) < I32_MIN ? (UV) I32_MIN : (U32)(I32) (n)) \
4017                   : ((n) < U32_MAX_P1 ? (U32) (n) \
4018                      : ((n) > 0 ? U32_MAX : 0 /* NaN */)))
4019 #define I_V(n) (LIKELY((n) < IV_MAX_P1) ? (UNLIKELY((n) < IV_MIN) ? IV_MIN : (IV) (n)) \
4020                   : (LIKELY((n) < UV_MAX_P1) ? (IV)(UV) (n) \
4021                      : ((n) > 0 ? (IV)UV_MAX : 0 /* NaN */)))
4022 #define U_V(n) ((n) < 0.0 ? (UNLIKELY((n) < IV_MIN) ? (UV) IV_MIN : (UV)(IV) (n)) \
4023                   : (LIKELY((n) < UV_MAX_P1) ? (UV) (n) \
4024                      : ((n) > 0 ? UV_MAX : 0 /* NaN */)))
4025 #endif
4026
4027 #define U_S(what) ((U16)U_32(what))
4028 #define U_I(what) ((unsigned int)U_32(what))
4029 #define U_L(what) U_32(what)
4030
4031 #ifdef HAS_SIGNBIT
4032 #  ifndef Perl_signbit
4033 #    define Perl_signbit signbit
4034 #  endif
4035 #endif
4036
4037 /* These do not care about the fractional part, only about the range. */
4038 #define NV_WITHIN_IV(nv) (I_V(nv) >= IV_MIN && I_V(nv) <= IV_MAX)
4039 #define NV_WITHIN_UV(nv) ((nv)>=0.0 && U_V(nv) >= UV_MIN && U_V(nv) <= UV_MAX)
4040
4041 /* Used with UV/IV arguments: */
4042                                         /* XXXX: need to speed it up */
4043 #define CLUMP_2UV(iv)   ((iv) < 0 ? 0 : (UV)(iv))
4044 #define CLUMP_2IV(uv)   ((uv) > (UV)IV_MAX ? IV_MAX : (IV)(uv))
4045
4046 #ifndef MAXSYSFD
4047 #   define MAXSYSFD 2
4048 #endif
4049
4050 #ifndef __cplusplus
4051 #if !(defined(WIN32) || defined(SYMBIAN))
4052 Uid_t getuid (void);
4053 Uid_t geteuid (void);
4054 Gid_t getgid (void);
4055 Gid_t getegid (void);
4056 #endif
4057 #endif
4058
4059 #ifndef Perl_debug_log
4060 #  define Perl_debug_log        PerlIO_stderr()
4061 #endif
4062
4063 #ifndef Perl_error_log
4064 #  define Perl_error_log        (PL_stderrgv                    \
4065                                  && isGV(PL_stderrgv)           \
4066                                  && GvIOp(PL_stderrgv)          \
4067                                  && IoOFP(GvIOp(PL_stderrgv))   \
4068                                  ? IoOFP(GvIOp(PL_stderrgv))    \
4069                                  : PerlIO_stderr())
4070 #endif
4071
4072
4073 #define DEBUG_p_FLAG            0x00000001 /*      1 */
4074 #define DEBUG_s_FLAG            0x00000002 /*      2 */
4075 #define DEBUG_l_FLAG            0x00000004 /*      4 */
4076 #define DEBUG_t_FLAG            0x00000008 /*      8 */
4077 #define DEBUG_o_FLAG            0x00000010 /*     16 */
4078 #define DEBUG_c_FLAG            0x00000020 /*     32 */
4079 #define DEBUG_P_FLAG            0x00000040 /*     64 */
4080 #define DEBUG_m_FLAG            0x00000080 /*    128 */
4081 #define DEBUG_f_FLAG            0x00000100 /*    256 */
4082 #define DEBUG_r_FLAG            0x00000200 /*    512 */
4083 #define DEBUG_x_FLAG            0x00000400 /*   1024 */
4084 #define DEBUG_u_FLAG            0x00000800 /*   2048 */
4085 /* U is reserved for Unofficial, exploratory hacking */
4086 #define DEBUG_U_FLAG            0x00001000 /*   4096 */
4087 /* spare                                        8192 */
4088 #define DEBUG_X_FLAG            0x00004000 /*  16384 */
4089 #define DEBUG_D_FLAG            0x00008000 /*  32768 */
4090 #define DEBUG_S_FLAG            0x00010000 /*  65536 */
4091 #define DEBUG_T_FLAG            0x00020000 /* 131072 */
4092 #define DEBUG_R_FLAG            0x00040000 /* 262144 */
4093 #define DEBUG_J_FLAG            0x00080000 /* 524288 */
4094 #define DEBUG_v_FLAG            0x00100000 /*1048576 */
4095 #define DEBUG_C_FLAG            0x00200000 /*2097152 */
4096 #define DEBUG_A_FLAG            0x00400000 /*4194304 */
4097 #define DEBUG_q_FLAG            0x00800000 /*8388608 */
4098 #define DEBUG_M_FLAG            0x01000000 /*16777216*/
4099 #define DEBUG_B_FLAG            0x02000000 /*33554432*/
4100 #define DEBUG_L_FLAG            0x04000000 /*67108864*/
4101 #define DEBUG_i_FLAG            0x08000000 /*134217728*/
4102 #define DEBUG_MASK              0x0FFFEFFF /* mask of all the standard flags */
4103
4104 #define DEBUG_DB_RECURSE_FLAG   0x40000000
4105 #define DEBUG_TOP_FLAG          0x80000000 /* -D was given --> PL_debug |= FLAG */
4106
4107 #  define DEBUG_p_TEST_ UNLIKELY(PL_debug & DEBUG_p_FLAG)
4108 #  define DEBUG_s_TEST_ UNLIKELY(PL_debug & DEBUG_s_FLAG)
4109 #  define DEBUG_l_TEST_ UNLIKELY(PL_debug & DEBUG_l_FLAG)
4110 #  define DEBUG_t_TEST_ UNLIKELY(PL_debug & DEBUG_t_FLAG)
4111 #  define DEBUG_o_TEST_ UNLIKELY(PL_debug & DEBUG_o_FLAG)
4112 #  define DEBUG_c_TEST_ UNLIKELY(PL_debug & DEBUG_c_FLAG)
4113 #  define DEBUG_P_TEST_ UNLIKELY(PL_debug & DEBUG_P_FLAG)
4114 #  define DEBUG_m_TEST_ UNLIKELY(PL_debug & DEBUG_m_FLAG)
4115 #  define DEBUG_f_TEST_ UNLIKELY(PL_debug & DEBUG_f_FLAG)
4116 #  define DEBUG_r_TEST_ UNLIKELY(PL_debug & DEBUG_r_FLAG)
4117 #  define DEBUG_x_TEST_ UNLIKELY(PL_debug & DEBUG_x_FLAG)
4118 #  define DEBUG_u_TEST_ UNLIKELY(PL_debug & DEBUG_u_FLAG)
4119 #  define DEBUG_U_TEST_ UNLIKELY(PL_debug & DEBUG_U_FLAG)
4120 #  define DEBUG_X_TEST_ UNLIKELY(PL_debug & DEBUG_X_FLAG)
4121 #  define DEBUG_D_TEST_ UNLIKELY(PL_debug & DEBUG_D_FLAG)
4122 #  define DEBUG_S_TEST_ UNLIKELY(PL_debug & DEBUG_S_FLAG)
4123 #  define DEBUG_T_TEST_ UNLIKELY(PL_debug & DEBUG_T_FLAG)
4124 #  define DEBUG_R_TEST_ UNLIKELY(PL_debug & DEBUG_R_FLAG)
4125 #  define DEBUG_J_TEST_ UNLIKELY(PL_debug & DEBUG_J_FLAG)
4126 #  define DEBUG_v_TEST_ UNLIKELY(PL_debug & DEBUG_v_FLAG)
4127 #  define DEBUG_C_TEST_ UNLIKELY(PL_debug & DEBUG_C_FLAG)
4128 #  define DEBUG_A_TEST_ UNLIKELY(PL_debug & DEBUG_A_FLAG)
4129 #  define DEBUG_q_TEST_ UNLIKELY(PL_debug & DEBUG_q_FLAG)
4130 #  define DEBUG_M_TEST_ UNLIKELY(PL_debug & DEBUG_M_FLAG)
4131 #  define DEBUG_B_TEST_ UNLIKELY(PL_debug & DEBUG_B_FLAG)
4132 #  define DEBUG_L_TEST_ UNLIKELY(PL_debug & DEBUG_L_FLAG)
4133 #  define DEBUG_i_TEST_ UNLIKELY(PL_debug & DEBUG_i_FLAG)
4134 #  define DEBUG_Xv_TEST_ (DEBUG_X_TEST_ && DEBUG_v_TEST_)
4135 #  define DEBUG_Uv_TEST_ (DEBUG_U_TEST_ && DEBUG_v_TEST_)
4136 #  define DEBUG_Pv_TEST_ (DEBUG_P_TEST_ && DEBUG_v_TEST_)
4137 #  define DEBUG_Lv_TEST_ (DEBUG_L_TEST_ && DEBUG_v_TEST_)
4138
4139 #ifdef DEBUGGING
4140
4141 #  define DEBUG_p_TEST DEBUG_p_TEST_
4142 #  define DEBUG_s_TEST DEBUG_s_TEST_
4143 #  define DEBUG_l_TEST DEBUG_l_TEST_
4144 #  define DEBUG_t_TEST DEBUG_t_TEST_
4145 #  define DEBUG_o_TEST DEBUG_o_TEST_
4146 #  define DEBUG_c_TEST DEBUG_c_TEST_
4147 #  define DEBUG_P_TEST DEBUG_P_TEST_
4148 #  define DEBUG_m_TEST DEBUG_m_TEST_
4149 #  define DEBUG_f_TEST DEBUG_f_TEST_
4150 #  define DEBUG_r_TEST DEBUG_r_TEST_
4151 #  define DEBUG_x_TEST DEBUG_x_TEST_
4152 #  define DEBUG_u_TEST DEBUG_u_TEST_
4153 #  define DEBUG_U_TEST DEBUG_U_TEST_
4154 #  define DEBUG_X_TEST DEBUG_X_TEST_
4155 #  define DEBUG_D_TEST DEBUG_D_TEST_
4156 #  define DEBUG_S_TEST DEBUG_S_TEST_
4157 #  define DEBUG_T_TEST DEBUG_T_TEST_
4158 #  define DEBUG_R_TEST DEBUG_R_TEST_
4159 #  define DEBUG_J_TEST DEBUG_J_TEST_
4160 #  define DEBUG_v_TEST DEBUG_v_TEST_
4161 #  define DEBUG_C_TEST DEBUG_C_TEST_
4162 #  define DEBUG_A_TEST DEBUG_A_TEST_
4163 #  define DEBUG_q_TEST DEBUG_q_TEST_
4164 #  define DEBUG_M_TEST DEBUG_M_TEST_
4165 #  define DEBUG_B_TEST DEBUG_B_TEST_
4166 #  define DEBUG_L_TEST DEBUG_L_TEST_
4167 #  define DEBUG_i_TEST DEBUG_i_TEST_
4168 #  define DEBUG_Xv_TEST DEBUG_Xv_TEST_
4169 #  define DEBUG_Uv_TEST DEBUG_Uv_TEST_
4170 #  define DEBUG_Pv_TEST DEBUG_Pv_TEST_
4171 #  define DEBUG_Lv_TEST DEBUG_Lv_TEST_
4172
4173 #  define PERL_DEB(a)                  a
4174 #  define PERL_DEB2(a,b)               a
4175 #  define PERL_DEBUG(a) if (PL_debug)  a
4176 #  define DEBUG_p(a) if (DEBUG_p_TEST) a
4177 #  define DEBUG_s(a) if (DEBUG_s_TEST) a
4178 #  define DEBUG_l(a) if (DEBUG_l_TEST) a
4179 #  define DEBUG_t(a) if (DEBUG_t_TEST) a
4180 #  define DEBUG_o(a) if (DEBUG_o_TEST) a
4181 #  define DEBUG_c(a) if (DEBUG_c_TEST) a
4182 #  define DEBUG_P(a) if (DEBUG_P_TEST) a
4183
4184      /* Temporarily turn off memory debugging in case the a
4185       * does memory allocation, either directly or indirectly. */
4186 #  define DEBUG_m(a)  \
4187     STMT_START {                                                        \
4188         if (PERL_GET_INTERP) {                                          \
4189                                 dTHX;                                   \
4190                                 if (DEBUG_m_TEST) {                     \
4191                                     PL_debug &= ~DEBUG_m_FLAG;          \
4192                                     a;                                  \
4193                                     PL_debug |= DEBUG_m_FLAG;           \
4194                                 }                                       \
4195                               }                                         \
4196     } STMT_END
4197
4198 #  define DEBUG__(t, a)                                                 \
4199         STMT_START {                                                    \
4200                 if (t) STMT_START {a;} STMT_END;                        \
4201         } STMT_END
4202
4203 #  define DEBUG_f(a) DEBUG__(DEBUG_f_TEST, a)
4204
4205 #  ifndef PERL_EXT_RE_BUILD
4206 #    define DEBUG_r(a) DEBUG__(DEBUG_r_TEST, a)
4207 #  else
4208 #    define DEBUG_r(a) STMT_START {a;} STMT_END
4209 #  endif /* PERL_EXT_RE_BUILD */
4210
4211 #  define DEBUG_x(a) DEBUG__(DEBUG_x_TEST, a)
4212 #  define DEBUG_u(a) DEBUG__(DEBUG_u_TEST, a)
4213 #  define DEBUG_U(a) DEBUG__(DEBUG_U_TEST, a)
4214 #  define DEBUG_X(a) DEBUG__(DEBUG_X_TEST, a)
4215 #  define DEBUG_D(a) DEBUG__(DEBUG_D_TEST, a)
4216 #  define DEBUG_Xv(a) DEBUG__(DEBUG_Xv_TEST, a)
4217 #  define DEBUG_Uv(a) DEBUG__(DEBUG_Uv_TEST, a)
4218 #  define DEBUG_Pv(a) DEBUG__(DEBUG_Pv_TEST, a)
4219 #  define DEBUG_Lv(a) DEBUG__(DEBUG_Lv_TEST, a)
4220
4221 #  define DEBUG_S(a) DEBUG__(DEBUG_S_TEST, a)
4222 #  define DEBUG_T(a) DEBUG__(DEBUG_T_TEST, a)
4223 #  define DEBUG_R(a) DEBUG__(DEBUG_R_TEST, a)
4224 #  define DEBUG_v(a) DEBUG__(DEBUG_v_TEST, a)
4225 #  define DEBUG_C(a) DEBUG__(DEBUG_C_TEST, a)
4226 #  define DEBUG_A(a) DEBUG__(DEBUG_A_TEST, a)
4227 #  define DEBUG_q(a) DEBUG__(DEBUG_q_TEST, a)
4228 #  define DEBUG_M(a) DEBUG__(DEBUG_M_TEST, a)
4229 #  define DEBUG_B(a) DEBUG__(DEBUG_B_TEST, a)
4230 #  define DEBUG_L(a) DEBUG__(DEBUG_L_TEST, a)
4231 #  define DEBUG_i(a) DEBUG__(DEBUG_i_TEST, a)
4232
4233 #else /* ! DEBUGGING below */
4234
4235 #  define DEBUG_p_TEST (0)
4236 #  define DEBUG_s_TEST (0)
4237 #  define DEBUG_l_TEST (0)
4238 #  define DEBUG_t_TEST (0)
4239 #  define DEBUG_o_TEST (0)
4240 #  define DEBUG_c_TEST (0)
4241 #  define DEBUG_P_TEST (0)
4242 #  define DEBUG_m_TEST (0)
4243 #  define DEBUG_f_TEST (0)
4244 #  define DEBUG_r_TEST (0)
4245 #  define DEBUG_x_TEST (0)
4246 #  define DEBUG_u_TEST (0)
4247 #  define DEBUG_U_TEST (0)
4248 #  define DEBUG_X_TEST (0)
4249 #  define DEBUG_D_TEST (0)
4250 #  define DEBUG_S_TEST (0)
4251 #  define DEBUG_T_TEST (0)
4252 #  define DEBUG_R_TEST (0)
4253 #  define DEBUG_J_TEST (0)
4254 #  define DEBUG_v_TEST (0)
4255 #  define DEBUG_C_TEST (0)
4256 #  define DEBUG_A_TEST (0)
4257 #  define DEBUG_q_TEST (0)
4258 #  define DEBUG_M_TEST (0)
4259 #  define DEBUG_B_TEST (0)
4260 #  define DEBUG_L_TEST (0)
4261 #  define DEBUG_i_TEST (0)
4262 #  define DEBUG_Xv_TEST (0)
4263 #  define DEBUG_Uv_TEST (0)
4264 #  define DEBUG_Pv_TEST (0)
4265 #  define DEBUG_Lv_TEST (0)
4266
4267 #  define PERL_DEB(a)
4268 #  define PERL_DEB2(a,b)               b
4269 #  define PERL_DEBUG(a)
4270 #  define DEBUG_p(a)
4271 #  define DEBUG_s(a)
4272 #  define DEBUG_l(a)
4273 #  define DEBUG_t(a)
4274 #  define DEBUG_o(a)
4275 #  define DEBUG_c(a)
4276 #  define DEBUG_P(a)
4277 #  define DEBUG_m(a)
4278 #  define DEBUG_f(a)
4279 #  define DEBUG_r(a)
4280 #  define DEBUG_x(a)
4281 #  define DEBUG_u(a)
4282 #  define DEBUG_U(a)
4283 #  define DEBUG_X(a)
4284 #  define DEBUG_D(a)
4285 #  define DEBUG_S(a)
4286 #  define DEBUG_T(a)
4287 #  define DEBUG_R(a)
4288 #  define DEBUG_v(a)
4289 #  define DEBUG_C(a)
4290 #  define DEBUG_A(a)
4291 #  define DEBUG_q(a)
4292 #  define DEBUG_M(a)
4293 #  define DEBUG_B(a)
4294 #  define DEBUG_L(a)
4295 #  define DEBUG_i(a)
4296 #  define DEBUG_Xv(a)
4297 #  define DEBUG_Uv(a)
4298 #  define DEBUG_Pv(a)
4299 #  define DEBUG_Lv(a)
4300 #endif /* DEBUGGING */
4301
4302
4303 #define DEBUG_SCOPE(where) \
4304     DEBUG_l( \
4305     Perl_deb(aTHX_ "%s scope %ld (savestack=%ld) at %s:%d\n",   \
4306                     where, (long)PL_scopestack_ix, (long)PL_savestack_ix, \
4307                     __FILE__, __LINE__));
4308
4309 /* Keep the old croak based assert for those who want it, and as a fallback if
4310    the platform is so heretically non-ANSI that it can't assert.  */
4311
4312 #define Perl_assert(what)       PERL_DEB2(                              \
4313         ((what) ? ((void) 0) :                                          \
4314             (Perl_croak_nocontext("Assertion %s failed: file \"" __FILE__ \
4315                         "\", line %d", STRINGIFY(what), __LINE__),      \
4316              (void) 0)), ((void)0))
4317
4318 /* assert() gets defined if DEBUGGING.
4319  * If no DEBUGGING, the <assert.h> has not been included. */
4320 #ifndef assert
4321 #  define assert(what)  Perl_assert(what)
4322 #endif
4323 #ifdef DEBUGGING
4324 #  define assert_(what) assert(what),
4325 #else
4326 #  define assert_(what)
4327 #endif
4328
4329 struct ufuncs {
4330     I32 (*uf_val)(pTHX_ IV, SV*);
4331     I32 (*uf_set)(pTHX_ IV, SV*);
4332     IV uf_index;
4333 };
4334
4335 /* In pre-5.7-Perls the PERL_MAGIC_uvar magic didn't get the thread context.
4336  * XS code wanting to be backward compatible can do something
4337  * like the following:
4338
4339 #ifndef PERL_MG_UFUNC
4340 #define PERL_MG_UFUNC(name,ix,sv) I32 name(IV ix, SV *sv)
4341 #endif
4342
4343 static PERL_MG_UFUNC(foo_get, index, val)
4344 {
4345     sv_setsv(val, ...);
4346     return TRUE;
4347 }
4348
4349 -- Doug MacEachern
4350
4351 */
4352
4353 #ifndef PERL_MG_UFUNC
4354 #define PERL_MG_UFUNC(name,ix,sv) I32 name(pTHX_ IV ix, SV *sv)
4355 #endif
4356
4357 #include <math.h>
4358 #ifdef __VMS
4359      /* isfinite and others are here rather than in math.h as C99 stipulates */
4360 #    include <fp.h>
4361 #endif
4362
4363 #ifndef __cplusplus
4364 #  if !defined(WIN32) && !defined(VMS)
4365 #ifndef crypt
4366 char *crypt (const char*, const char*);
4367 #endif
4368 #  endif /* !WIN32 */
4369 #  ifndef WIN32
4370 #    ifndef getlogin
4371 char *getlogin (void);
4372 #    endif
4373 #  endif /* !WIN32 */
4374 #endif /* !__cplusplus */
4375
4376 /* Fixme on VMS.  This needs to be a run-time, not build time options */
4377 /* Also rename() is affected by this */
4378 #ifdef UNLINK_ALL_VERSIONS /* Currently only makes sense for VMS */
4379 #define UNLINK unlnk
4380 I32 unlnk (pTHX_ const char*);
4381 #else
4382 #define UNLINK PerlLIO_unlink
4383 #endif
4384
4385 /* some versions of glibc are missing the setresuid() proto */
4386 #if defined(HAS_SETRESUID) && !defined(HAS_SETRESUID_PROTO)
4387 int setresuid(uid_t ruid, uid_t euid, uid_t suid);
4388 #endif
4389 /* some versions of glibc are missing the setresgid() proto */
4390 #if defined(HAS_SETRESGID) && !defined(HAS_SETRESGID_PROTO)
4391 int setresgid(gid_t rgid, gid_t egid, gid_t sgid);
4392 #endif
4393
4394 #ifndef HAS_SETREUID
4395 #  ifdef HAS_SETRESUID
4396 #    define setreuid(r,e) setresuid(r,e,(Uid_t)-1)
4397 #    define HAS_SETREUID
4398 #  endif
4399 #endif
4400 #ifndef HAS_SETREGID
4401 #  ifdef HAS_SETRESGID
4402 #    define setregid(r,e) setresgid(r,e,(Gid_t)-1)
4403 #    define HAS_SETREGID
4404 #  endif
4405 #endif
4406
4407 /* Sighandler_t defined in iperlsys.h */
4408
4409 #ifdef HAS_SIGACTION
4410 typedef struct sigaction Sigsave_t;
4411 #else
4412 typedef Sighandler_t Sigsave_t;
4413 #endif
4414
4415 #define SCAN_DEF 0
4416 #define SCAN_TR 1
4417 #define SCAN_REPL 2
4418
4419 #ifdef DEBUGGING
4420 # ifndef register
4421 #  define register
4422 # endif
4423 # define RUNOPS_DEFAULT Perl_runops_debug
4424 #else
4425 # define RUNOPS_DEFAULT Perl_runops_standard
4426 #endif
4427
4428 #if defined(USE_PERLIO)
4429 EXTERN_C void PerlIO_teardown(void);
4430 # ifdef USE_ITHREADS
4431 #  define PERLIO_INIT MUTEX_INIT(&PL_perlio_mutex)
4432 #  define PERLIO_TERM                           \
4433         STMT_START {                            \
4434                 PerlIO_teardown();              \
4435                 MUTEX_DESTROY(&PL_perlio_mutex);\
4436         } STMT_END
4437 # else
4438 #  define PERLIO_INIT
4439 #  define PERLIO_TERM   PerlIO_teardown()
4440 # endif
4441 #else
4442 #  define PERLIO_INIT
4443 #  define PERLIO_TERM
4444 #endif
4445
4446 #ifdef MYMALLOC
4447 #  ifdef MUTEX_INIT_CALLS_MALLOC
4448 #    define MALLOC_INIT                                 \
4449         STMT_START {                                    \
4450                 PL_malloc_mutex = NULL;                 \
4451                 MUTEX_INIT(&PL_malloc_mutex);           \
4452         } STMT_END
4453 #    define MALLOC_TERM                                 \
4454         STMT_START {                                    \
4455                 perl_mutex tmp = PL_malloc_mutex;       \
4456                 PL_malloc_mutex = NULL;                 \
4457                 MUTEX_DESTROY(&tmp);                    \
4458         } STMT_END
4459 #  else
4460 #    define MALLOC_INIT MUTEX_INIT(&PL_malloc_mutex)
4461 #    define MALLOC_TERM MUTEX_DESTROY(&PL_malloc_mutex)
4462 #  endif
4463 #else
4464 #  define MALLOC_INIT
4465 #  define MALLOC_TERM
4466 #endif
4467
4468 #if defined(PERL_IMPLICIT_CONTEXT)
4469
4470 struct perl_memory_debug_header;
4471 struct perl_memory_debug_header {
4472   tTHX  interpreter;
4473 #  if defined(PERL_POISON) || defined(PERL_DEBUG_READONLY_COW)
4474   MEM_SIZE size;
4475 #  endif
4476   struct perl_memory_debug_header *prev;
4477   struct perl_memory_debug_header *next;
4478 #  ifdef PERL_DEBUG_READONLY_COW
4479   bool readonly;
4480 #  endif
4481 };
4482
4483 #elif defined(PERL_DEBUG_READONLY_COW)
4484
4485 struct perl_memory_debug_header;
4486 struct perl_memory_debug_header {
4487   MEM_SIZE size;
4488 };
4489
4490 #endif
4491
4492 #if defined (PERL_TRACK_MEMPOOL) || defined (PERL_DEBUG_READONLY_COW)
4493
4494 #  define PERL_MEMORY_DEBUG_HEADER_SIZE \
4495         (sizeof(struct perl_memory_debug_header) + \
4496         (MEM_ALIGNBYTES - sizeof(struct perl_memory_debug_header) \
4497          %MEM_ALIGNBYTES) % MEM_ALIGNBYTES)
4498
4499 #else
4500 #  define PERL_MEMORY_DEBUG_HEADER_SIZE 0
4501 #endif
4502
4503 #ifdef PERL_TRACK_MEMPOOL
4504 # ifdef PERL_DEBUG_READONLY_COW
4505 #  define INIT_TRACK_MEMPOOL(header, interp)                    \
4506         STMT_START {                                            \
4507                 (header).interpreter = (interp);                \
4508                 (header).prev = (header).next = &(header);      \
4509                 (header).readonly = 0;                          \
4510         } STMT_END
4511 # else
4512 #  define INIT_TRACK_MEMPOOL(header, interp)                    \
4513         STMT_START {                                            \
4514                 (header).interpreter = (interp);                \
4515                 (header).prev = (header).next = &(header);      \
4516         } STMT_END
4517 # endif
4518 # else
4519 #  define INIT_TRACK_MEMPOOL(header, interp)
4520 #endif
4521
4522 #ifdef I_MALLOCMALLOC
4523 /* Needed for malloc_size(), malloc_good_size() on some systems */
4524 #  include <malloc/malloc.h>
4525 #endif
4526
4527 #ifdef MYMALLOC
4528 #  define Perl_safesysmalloc_size(where)        Perl_malloced_size(where)
4529 #else
4530 #  if defined(HAS_MALLOC_SIZE) && !defined(PERL_DEBUG_READONLY_COW)
4531 #    ifdef PERL_TRACK_MEMPOOL
4532 #       define Perl_safesysmalloc_size(where)                   \
4533             (malloc_size(((char *)(where)) - PERL_MEMORY_DEBUG_HEADER_SIZE) - PERL_MEMORY_DEBUG_HEADER_SIZE)
4534 #    else
4535 #       define Perl_safesysmalloc_size(where) malloc_size(where)
4536 #    endif
4537 #  endif
4538 #  ifdef HAS_MALLOC_GOOD_SIZE
4539 #    ifdef PERL_TRACK_MEMPOOL
4540 #       define Perl_malloc_good_size(how_much)                  \
4541             (malloc_good_size((how_much) + PERL_MEMORY_DEBUG_HEADER_SIZE) - PERL_MEMORY_DEBUG_HEADER_SIZE)
4542 #    else
4543 #       define Perl_malloc_good_size(how_much) malloc_good_size(how_much)
4544 #    endif
4545 #  else
4546 /* Having this as the identity operation makes some code simpler.  */
4547 #       define Perl_malloc_good_size(how_much)  (how_much)
4548 #  endif
4549 #endif
4550
4551 typedef int (*runops_proc_t)(pTHX);
4552 typedef void (*share_proc_t) (pTHX_ SV *sv);
4553 typedef int  (*thrhook_proc_t) (pTHX);
4554 typedef OP* (*PPADDR_t[]) (pTHX);
4555 typedef bool (*destroyable_proc_t) (pTHX_ SV *sv);
4556 typedef void (*despatch_signals_proc_t) (pTHX);
4557
4558 #if defined(__DYNAMIC__) && defined(PERL_DARWIN) && defined(PERL_CORE)
4559 #  include <crt_externs.h>      /* for the env array */
4560 #  define environ (*_NSGetEnviron())
4561 #elif defined(USE_ENVIRON_ARRAY) && !defined(environ)
4562    /* VMS and some other platforms don't use the environ array */
4563 EXTERN_C char **environ;  /* environment variables supplied via exec */
4564 #endif
4565
4566 #define PERL_PATCHLEVEL_H_IMPLICIT
4567 #include "patchlevel.h"
4568 #undef PERL_PATCHLEVEL_H_IMPLICIT
4569
4570 #define PERL_VERSION_STRING     STRINGIFY(PERL_REVISION) "." \
4571                                 STRINGIFY(PERL_VERSION) "." \
4572                                 STRINGIFY(PERL_SUBVERSION)
4573
4574 #define PERL_API_VERSION_STRING STRINGIFY(PERL_API_REVISION) "." \
4575                                 STRINGIFY(PERL_API_VERSION) "." \
4576                                 STRINGIFY(PERL_API_SUBVERSION)
4577
4578 START_EXTERN_C
4579
4580 /* handy constants */
4581 EXTCONST char PL_warn_uninit[]
4582   INIT("Use of uninitialized value%s%s%s");
4583 EXTCONST char PL_warn_uninit_sv[]
4584   INIT("Use of uninitialized value%" SVf "%s%s");
4585 EXTCONST char PL_warn_nosemi[]
4586   INIT("Semicolon seems to be missing");
4587 EXTCONST char PL_warn_reserved[]
4588   INIT("Unquoted string \"%s\" may clash with future reserved word");
4589 EXTCONST char PL_warn_nl[]
4590   INIT("Unsuccessful %s on filename containing newline");
4591 EXTCONST char PL_no_wrongref[]
4592   INIT("Can't use %s ref as %s ref");
4593 /* The core no longer needs this here. If you require the string constant,
4594    please inline a copy into your own code.  */
4595 EXTCONST char PL_no_symref[] __attribute__deprecated__
4596   INIT("Can't use string (\"%.32s\") as %s ref while \"strict refs\" in use");
4597 EXTCONST char PL_no_symref_sv[]
4598   INIT("Can't use string (\"%" SVf32 "\"%s) as %s ref while \"strict refs\" in use");
4599
4600 EXTCONST char PL_no_usym[]
4601   INIT("Can't use an undefined value as %s reference");
4602 EXTCONST char PL_no_aelem[]
4603   INIT("Modification of non-creatable array value attempted, subscript %d");
4604 EXTCONST char PL_no_helem_sv[]
4605   INIT("Modification of non-creatable hash value attempted, subscript \"%" SVf "\"");
4606 EXTCONST char PL_no_modify[]
4607   INIT("Modification of a read-only value attempted");
4608 EXTCONST char PL_no_mem[sizeof("Out of memory!\n")]
4609   INIT("Out of memory!\n");
4610 EXTCONST char PL_no_security[]
4611   INIT("Insecure dependency in %s%s");
4612 EXTCONST char PL_no_sock_func[]
4613   INIT("Unsupported socket function \"%s\" called");
4614 EXTCONST char PL_no_dir_func[]
4615   INIT("Unsupported directory function \"%s\" called");
4616 EXTCONST char PL_no_func[]
4617   INIT("The %s function is unimplemented");
4618 EXTCONST char PL_no_myglob[]
4619   INIT("\"%s\" %se %s can't be in a package");
4620 EXTCONST char PL_no_localize_ref[]
4621   INIT("Can't localize through a reference");
4622 EXTCONST char PL_memory_wrap[]
4623   INIT("panic: memory wrap");
4624
4625 EXTCONST char PL_Yes[]
4626   INIT("1");
4627 EXTCONST char PL_No[]
4628   INIT("");
4629 EXTCONST char PL_Zero[]
4630   INIT("0");
4631 EXTCONST char PL_hexdigit[]
4632   INIT("0123456789abcdef0123456789ABCDEF");
4633
4634 EXTCONST STRLEN PL_WARN_ALL
4635   INIT(0);
4636 EXTCONST STRLEN PL_WARN_NONE
4637   INIT(0);
4638
4639 /* This is constant on most architectures, a global on OS/2 */
4640 #ifndef OS2
4641 EXTCONST char PL_sh_path[]
4642   INIT(SH_PATH); /* full path of shell */
4643 #endif
4644
4645 #ifdef CSH
4646 EXTCONST char PL_cshname[]
4647   INIT(CSH);
4648 #  define PL_cshlen     (sizeof(CSH "") - 1)
4649 #endif
4650
4651 /* These are baked at compile time into any shared perl library.
4652    In future releases this will allow us in main() to sanity test the
4653    library we're linking against.  */
4654
4655 EXTCONST U8 PL_revision
4656   INIT(PERL_REVISION);
4657 EXTCONST U8 PL_version
4658   INIT(PERL_VERSION);
4659 EXTCONST U8 PL_subversion
4660   INIT(PERL_SUBVERSION);
4661
4662 EXTCONST char PL_uuemap[65]
4663   INIT("`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_");
4664
4665 /* a special string address whose value is "isa", but which perl knows
4666  * to treat as if it were really "DOES" when printing the method name in
4667  *  the "Can't call method '%s'" error message */
4668 EXTCONST char PL_isa_DOES[]
4669   INIT("isa");
4670
4671 #ifdef DOINIT
4672 EXTCONST char PL_uudmap[256] =
4673 #  ifdef PERL_MICRO
4674 #    include "uuudmap.h"
4675 #  else
4676 #    include "uudmap.h"
4677 #  endif
4678 ;
4679 EXTCONST char PL_bitcount[256] =
4680 #  ifdef PERL_MICRO
4681 #    include "ubitcount.h"
4682 #else
4683 #    include "bitcount.h"
4684 #  endif
4685 ;
4686 EXTCONST char* const PL_sig_name[] = { SIG_NAME };
4687 EXTCONST int         PL_sig_num[]  = { SIG_NUM };
4688 #else
4689 EXTCONST char PL_uudmap[256];
4690 EXTCONST char PL_bitcount[256];
4691 EXTCONST char* const PL_sig_name[];
4692 EXTCONST int         PL_sig_num[];
4693 #endif
4694
4695 /* fast conversion and case folding tables.  The folding tables complement the
4696  * fold, so that 'a' maps to 'A' and 'A' maps to 'a', ignoring more complicated
4697  * folds such as outside the range or to multiple characters. */
4698
4699 #ifdef DOINIT
4700 #ifndef EBCDIC
4701
4702 /* The EBCDIC fold table depends on the code page, and hence is found in
4703  * utfebcdic.h */
4704
4705 EXTCONST  unsigned char PL_fold[] = {
4706         0,      1,      2,      3,      4,      5,      6,      7,
4707         8,      9,      10,     11,     12,     13,     14,     15,
4708         16,     17,     18,     19,     20,     21,     22,     23,
4709         24,     25,     26,     27,     28,     29,     30,     31,
4710         32,     33,     34,     35,     36,     37,     38,     39,
4711         40,     41,     42,     43,     44,     45,     46,     47,
4712         48,     49,     50,     51,     52,     53,     54,     55,
4713         56,     57,     58,     59,     60,     61,     62,     63,
4714         64,     'a',    'b',    'c',    'd',    'e',    'f',    'g',
4715         'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
4716         'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
4717         'x',    'y',    'z',    91,     92,     93,     94,     95,
4718         96,     'A',    'B',    'C',    'D',    'E',    'F',    'G',
4719         'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
4720         'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
4721         'X',    'Y',    'Z',    123,    124,    125,    126,    127,
4722         128,    129,    130,    131,    132,    133,    134,    135,
4723         136,    137,    138,    139,    140,    141,    142,    143,
4724         144,    145,    146,    147,    148,    149,    150,    151,
4725         152,    153,    154,    155,    156,    157,    158,    159,
4726         160,    161,    162,    163,    164,    165,    166,    167,
4727         168,    169,    170,    171,    172,    173,    174,    175,
4728         176,    177,    178,    179,    180,    181,    182,    183,
4729         184,    185,    186,    187,    188,    189,    190,    191,
4730         192,    193,    194,    195,    196,    197,    198,    199,
4731         200,    201,    202,    203,    204,    205,    206,    207,
4732         208,    209,    210,    211,    212,    213,    214,    215,
4733         216,    217,    218,    219,    220,    221,    222,    223,    
4734         224,    225,    226,    227,    228,    229,    230,    231,
4735         232,    233,    234,    235,    236,    237,    238,    239,
4736         240,    241,    242,    243,    244,    245,    246,    247,
4737         248,    249,    250,    251,    252,    253,    254,    255
4738 };
4739 EXTCONST  unsigned char PL_fold_latin1[] = {
4740     /* Full latin1 complement folding, except for three problematic code points:
4741      *  Micro sign (181 = 0xB5) and y with diearesis (255 = 0xFF) have their
4742      *  fold complements outside the Latin1 range, so can't match something
4743      *  that isn't in utf8.
4744      *  German lower case sharp s (223 = 0xDF) folds to two characters, 'ss',
4745      *  not one, so can't be represented in this table.
4746      *
4747      * All have to be specially handled */
4748         0,      1,      2,      3,      4,      5,      6,      7,
4749         8,      9,      10,     11,     12,     13,     14,     15,
4750         16,     17,     18,     19,     20,     21,     22,     23,
4751         24,     25,     26,     27,     28,     29,     30,     31,
4752         32,     33,     34,     35,     36,     37,     38,     39,
4753         40,     41,     42,     43,     44,     45,     46,     47,
4754         48,     49,     50,     51,     52,     53,     54,     55,
4755         56,     57,     58,     59,     60,     61,     62,     63,
4756         64,     'a',    'b',    'c',    'd',    'e',    'f',    'g',
4757         'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
4758         'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
4759         'x',    'y',    'z',    91,     92,     93,     94,     95,
4760         96,     'A',    'B',    'C',    'D',    'E',    'F',    'G',
4761         'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
4762         'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
4763         'X',    'Y',    'Z',    123,    124,    125,    126,    127,
4764         128,    129,    130,    131,    132,    133,    134,    135,
4765         136,    137,    138,    139,    140,    141,    142,    143,
4766         144,    145,    146,    147,    148,    149,    150,    151,
4767         152,    153,    154,    155,    156,    157,    158,    159,
4768         160,    161,    162,    163,    164,    165,    166,    167,
4769         168,    169,    170,    171,    172,    173,    174,    175,
4770         176,    177,    178,    179,    180,    181 /*micro */, 182,    183,
4771         184,    185,    186,    187,    188,    189,    190,    191,
4772         192+32, 193+32, 194+32, 195+32, 196+32, 197+32, 198+32, 199+32,
4773         200+32, 201+32, 202+32, 203+32, 204+32, 205+32, 206+32, 207+32,
4774         208+32, 209+32, 210+32, 211+32, 212+32, 213+32, 214+32, 215,
4775         216+32, 217+32, 218+32, 219+32, 220+32, 221+32, 222+32, 223 /* ss */,
4776         224-32, 225-32, 226-32, 227-32, 228-32, 229-32, 230-32, 231-32,
4777         232-32, 233-32, 234-32, 235-32, 236-32, 237-32, 238-32, 239-32,
4778         240-32, 241-32, 242-32, 243-32, 244-32, 245-32, 246-32, 247,
4779         248-32, 249-32, 250-32, 251-32, 252-32, 253-32, 254-32,
4780         255 /* y with diaeresis */
4781 };
4782
4783 /* If these tables are accessed through ebcdic, the access will be converted to
4784  * latin1 first */
4785 EXTCONST  unsigned char PL_latin1_lc[] = {  /* lowercasing */
4786         0,      1,      2,      3,      4,      5,      6,      7,
4787         8,      9,      10,     11,     12,     13,     14,     15,
4788         16,     17,     18,     19,     20,     21,     22,     23,
4789         24,     25,     26,     27,     28,     29,     30,     31,
4790         32,     33,     34,     35,     36,     37,     38,     39,
4791         40,     41,     42,     43,     44,     45,     46,     47,
4792         48,     49,     50,     51,     52,     53,     54,     55,
4793         56,     57,     58,     59,     60,     61,     62,     63,
4794         64,     'a',    'b',    'c',    'd',    'e',    'f',    'g',
4795         'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
4796         'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
4797         'x',    'y',    'z',    91,     92,     93,     94,     95,
4798         96,     97,     98,     99,     100,    101,    102,    103,
4799         104,    105,    106,    107,    108,    109,    110,    111,
4800         112,    113,    114,    115,    116,    117,    118,    119,
4801         120,    121,    122,    123,    124,    125,    126,    127,
4802         128,    129,    130,    131,    132,    133,    134,    135,
4803         136,    137,    138,    139,    140,    141,    142,    143,
4804         144,    145,    146,    147,    148,    149,    150,    151,
4805         152,    153,    154,    155,    156,    157,    158,    159,
4806         160,    161,    162,    163,    164,    165,    166,    167,
4807         168,    169,    170,    171,    172,    173,    174,    175,
4808         176,    177,    178,    179,    180,    181,    182,    183,
4809         184,    185,    186,    187,    188,    189,    190,    191,
4810         192+32, 193+32, 194+32, 195+32, 196+32, 197+32, 198+32, 199+32,
4811         200+32, 201+32, 202+32, 203+32, 204+32, 205+32, 206+32, 207+32,
4812         208+32, 209+32, 210+32, 211+32, 212+32, 213+32, 214+32, 215,
4813         216+32, 217+32, 218+32, 219+32, 220+32, 221+32, 222+32, 223,    
4814         224,    225,    226,    227,    228,    229,    230,    231,
4815         232,    233,    234,    235,    236,    237,    238,    239,
4816         240,    241,    242,    243,    244,    245,    246,    247,
4817         248,    249,    250,    251,    252,    253,    254,    255
4818 };
4819
4820 /* upper and title case of latin1 characters, modified so that the three tricky
4821  * ones are mapped to 255 (which is one of the three) */
4822 EXTCONST  unsigned char PL_mod_latin1_uc[] = {
4823         0,      1,      2,      3,      4,      5,      6,      7,
4824         8,      9,      10,     11,     12,     13,     14,     15,
4825         16,     17,     18,     19,     20,     21,     22,     23,
4826         24,     25,     26,     27,     28,     29,     30,     31,
4827         32,     33,     34,     35,     36,     37,     38,     39,
4828         40,     41,     42,     43,     44,     45,     46,     47,
4829         48,     49,     50,     51,     52,     53,     54,     55,
4830         56,     57,     58,     59,     60,     61,     62,     63,
4831         64,     65,     66,     67,     68,     69,     70,     71,
4832         72,     73,     74,     75,     76,     77,     78,     79,
4833         80,     81,     82,     83,     84,     85,     86,     87,
4834         88,     89,     90,     91,     92,     93,     94,     95,
4835         96,     'A',    'B',    'C',    'D',    'E',    'F',    'G',
4836         'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
4837         'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
4838         'X',    'Y',    'Z',    123,    124,    125,    126,    127,
4839         128,    129,    130,    131,    132,    133,    134,    135,
4840         136,    137,    138,    139,    140,    141,    142,    143,
4841         144,    145,    146,    147,    148,    149,    150,    151,
4842         152,    153,    154,    155,    156,    157,    158,    159,
4843         160,    161,    162,    163,    164,    165,    166,    167,
4844         168,    169,    170,    171,    172,    173,    174,    175,
4845         176,    177,    178,    179,    180,    255 /*micro*/,  182,    183,
4846         184,    185,    186,    187,    188,    189,    190,    191,
4847         192,    193,    194,    195,    196,    197,    198,    199,
4848         200,    201,    202,    203,    204,    205,    206,    207,
4849         208,    209,    210,    211,    212,    213,    214,    215,
4850         216,    217,    218,    219,    220,    221,    222,
4851 #if    UNICODE_MAJOR_VERSION > 2                                        \
4852    || (UNICODE_MAJOR_VERSION == 2 && UNICODE_DOT_VERSION >= 1           \
4853                                   && UNICODE_DOT_DOT_VERSION >= 8)
4854                                                                 255 /*sharp s*/,
4855 #else   /* uc(sharp s) is 'sharp s' itself in early unicode */
4856                                                                 223,
4857 #endif
4858         224-32, 225-32, 226-32, 227-32, 228-32, 229-32, 230-32, 231-32,
4859         232-32, 233-32, 234-32, 235-32, 236-32, 237-32, 238-32, 239-32,
4860         240-32, 241-32, 242-32, 243-32, 244-32, 245-32, 246-32, 247,
4861         248-32, 249-32, 250-32, 251-32, 252-32, 253-32, 254-32, 255
4862 };
4863 #endif  /* !EBCDIC, but still in DOINIT */
4864 #else   /* ! DOINIT */
4865 #   ifndef EBCDIC
4866 EXTCONST unsigned char PL_fold[];
4867 EXTCONST unsigned char PL_fold_latin1[];
4868 EXTCONST unsigned char PL_mod_latin1_uc[];
4869 EXTCONST unsigned char PL_latin1_lc[];
4870 #   endif
4871 #endif
4872
4873 #ifndef PERL_GLOBAL_STRUCT /* or perlvars.h */
4874 #ifdef DOINIT
4875 EXT unsigned char PL_fold_locale[256] = { /* Unfortunately not EXTCONST. */
4876         0,      1,      2,      3,      4,      5,      6,      7,
4877         8,      9,      10,     11,     12,     13,     14,     15,
4878         16,     17,     18,     19,     20,     21,     22,     23,
4879         24,     25,     26,     27,     28,     29,     30,     31,
4880         32,     33,     34,     35,     36,     37,     38,     39,
4881         40,     41,     42,     43,     44,     45,     46,     47,
4882         48,     49,     50,     51,     52,     53,     54,     55,
4883         56,     57,     58,     59,     60,     61,     62,     63,
4884         64,     'a',    'b',    'c',    'd',    'e',    'f',    'g',
4885         'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
4886         'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
4887         'x',    'y',    'z',    91,     92,     93,     94,     95,
4888         96,     'A',    'B',    'C',    'D',    'E',    'F',    'G',
4889         'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
4890         'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
4891         'X',    'Y',    'Z',    123,    124,    125,    126,    127,
4892         128,    129,    130,    131,    132,    133,    134,    135,
4893         136,    137,    138,    139,    140,    141,    142,    143,
4894         144,    145,    146,    147,    148,    149,    150,    151,
4895         152,    153,    154,    155,    156,    157,    158,    159,
4896         160,    161,    162,    163,    164,    165,    166,    167,
4897         168,    169,    170,    171,    172,    173,    174,    175,
4898         176,    177,    178,    179,    180,    181,    182,    183,
4899         184,    185,    186,    187,    188,    189,    190,    191,
4900         192,    193,    194,    195,    196,    197,    198,    199,
4901         200,    201,    202,    203,    204,    205,    206,    207,
4902         208,    209,    210,    211,    212,    213,    214,    215,
4903         216,    217,    218,    219,    220,    221,    222,    223,    
4904         224,    225,    226,    227,    228,    229,    230,    231,
4905         232,    233,    234,    235,    236,    237,    238,    239,
4906         240,    241,    242,    243,    244,    245,    246,    247,
4907         248,    249,    250,    251,    252,    253,    254,    255
4908 };
4909 #else
4910 EXT unsigned char PL_fold_locale[256]; /* Unfortunately not EXTCONST. */
4911 #endif
4912 #endif /* !PERL_GLOBAL_STRUCT */
4913
4914 #ifdef DOINIT
4915 #ifdef EBCDIC
4916 EXTCONST unsigned char PL_freq[] = {/* EBCDIC frequencies for mixed English/C */
4917     1,      2,      84,     151,    154,    155,    156,    157,
4918     165,    246,    250,    3,      158,    7,      18,     29,
4919     40,     51,     62,     73,     85,     96,     107,    118,
4920     129,    140,    147,    148,    149,    150,    152,    153,
4921     255,      6,      8,      9,     10,     11,     12,     13,
4922      14,     15,     24,     25,     26,     27,     28,    226,
4923      29,     30,     31,     32,     33,     43,     44,     45,
4924      46,     47,     48,     49,     50,     76,     77,     78,
4925      79,     80,     81,     82,     83,     84,     85,     86,
4926      87,     94,     95,    234,    181,    233,    187,    190,
4927     180,     96,     97,     98,     99,    100,    101,    102,
4928     104,    112,    182,    174,    236,    232,    229,    103,
4929     228,    226,    114,    115,    116,    117,    118,    119,
4930     120,    121,    122,    235,    176,    230,    194,    162,
4931     130,    131,    132,    133,    134,    135,    136,    137,
4932     138,    139,    201,    205,    163,    217,    220,    224,
4933     5,      248,    227,    244,    242,    255,    241,    231,
4934     240,    253,    16,     197,    19,     20,     21,     187,
4935     23,     169,    210,    245,    237,    249,    247,    239,
4936     168,    252,    34,     196,    36,     37,     38,     39,
4937     41,     42,     251,    254,    238,    223,    221,    213,
4938     225,    177,    52,     53,     54,     55,     56,     57,
4939     58,     59,     60,     61,     63,     64,     65,     66,
4940     67,     68,     69,     70,     71,     72,     74,     75,
4941     205,    208,    186,    202,    200,    218,    198,    179,
4942     178,    214,    88,     89,     90,     91,     92,     93,
4943     217,    166,    170,    207,    199,    209,    206,    204,
4944     160,    212,    105,    106,    108,    109,    110,    111,
4945     203,    113,    216,    215,    192,    175,    193,    243,
4946     172,    161,    123,    124,    125,    126,    127,    128,
4947     222,    219,    211,    195,    188,    193,    185,    184,
4948     191,    183,    141,    142,    143,    144,    145,    146
4949 };
4950 #else  /* ascii rather than ebcdic */
4951 EXTCONST unsigned char PL_freq[] = {    /* letter frequencies for mixed English/C */
4952         1,      2,      84,     151,    154,    155,    156,    157,
4953         165,    246,    250,    3,      158,    7,      18,     29,
4954         40,     51,     62,     73,     85,     96,     107,    118,
4955         129,    140,    147,    148,    149,    150,    152,    153,
4956         255,    182,    224,    205,    174,    176,    180,    217,
4957         233,    232,    236,    187,    235,    228,    234,    226,
4958         222,    219,    211,    195,    188,    193,    185,    184,
4959         191,    183,    201,    229,    181,    220,    194,    162,
4960         163,    208,    186,    202,    200,    218,    198,    179,
4961         178,    214,    166,    170,    207,    199,    209,    206,
4962         204,    160,    212,    216,    215,    192,    175,    173,
4963         243,    172,    161,    190,    203,    189,    164,    230,
4964         167,    248,    227,    244,    242,    255,    241,    231,
4965         240,    253,    169,    210,    245,    237,    249,    247,
4966         239,    168,    252,    251,    254,    238,    223,    221,
4967         213,    225,    177,    197,    171,    196,    159,    4,
4968         5,      6,      8,      9,      10,     11,     12,     13,
4969         14,     15,     16,     17,     19,     20,     21,     22,
4970         23,     24,     25,     26,     27,     28,     30,     31,
4971         32,     33,     34,     35,     36,     37,     38,     39,
4972         41,     42,     43,     44,     45,     46,     47,     48,
4973         49,     50,     52,     53,     54,     55,     56,     57,
4974         58,     59,     60,     61,     63,     64,     65,     66,
4975         67,     68,     69,     70,     71,     72,     74,     75,
4976         76,     77,     78,     79,     80,     81,     82,     83,
4977         86,     87,     88,     89,     90,     91,     92,     93,
4978         94,     95,     97,     98,     99,     100,    101,    102,
4979         103,    104,    105,    106,    108,    109,    110,    111,
4980         112,    113,    114,    115,    116,    117,    119,    120,
4981         121,    122,    123,    124,    125,    126,    127,    128,
4982         130,    131,    132,    133,    134,    135,    136,    137,
4983         138,    139,    141,    142,    143,    144,    145,    146
4984 };
4985 #endif
4986 #else
4987 EXTCONST unsigned char PL_freq[];
4988 #endif
4989
4990 /* Although only used for debugging, these constants must be available in
4991  * non-debugging builds too, since they're used in ext/re/re_exec.c,
4992  * which has DEBUGGING enabled always */
4993 #ifdef DOINIT
4994 EXTCONST char* const PL_block_type[] = {
4995         "NULL",
4996         "WHEN",
4997         "BLOCK",
4998         "GIVEN",
4999         "LOOP_ARY",
5000         "LOOP_LAZYSV",
5001         "LOOP_LAZYIV",
5002         "LOOP_LIST",
5003         "LOOP_PLAIN",
5004         "SUB",
5005         "FORMAT",
5006         "EVAL",
5007         "SUBST"
5008 };
5009 #else
5010 EXTCONST char* PL_block_type[];
5011 #endif
5012
5013 /* These are all the compile time options that affect binary compatibility.
5014    Other compile time options that are binary compatible are in perl.c
5015    (in S_Internals_V()). Both are combined for the output of perl -V
5016    However, this string will be embedded in any shared perl library, which will
5017    allow us add a comparison check in perlmain.c in the near future.  */
5018 #ifdef DOINIT
5019 EXTCONST char PL_bincompat_options[] =
5020 #  ifdef DEBUG_LEAKING_SCALARS
5021                              " DEBUG_LEAKING_SCALARS"
5022 #  endif
5023 #  ifdef DEBUG_LEAKING_SCALARS_FORK_DUMP
5024                              " DEBUG_LEAKING_SCALARS_FORK_DUMP"
5025 #  endif
5026 #  ifdef FCRYPT
5027                              " FCRYPT"
5028 #  endif
5029 #  ifdef HAS_TIMES
5030                              " HAS_TIMES"
5031 #  endif
5032 #  ifdef HAVE_INTERP_INTERN
5033                              " HAVE_INTERP_INTERN"
5034 #  endif
5035 #  ifdef MULTIPLICITY
5036                              " MULTIPLICITY"
5037 #  endif
5038 #  ifdef MYMALLOC
5039                              " MYMALLOC"
5040 #  endif
5041 #  ifdef PERLIO_LAYERS
5042                              " PERLIO_LAYERS"
5043 #  endif
5044 #  ifdef PERL_DEBUG_READONLY_COW
5045                              " PERL_DEBUG_READONLY_COW"
5046 #  endif
5047 #  ifdef PERL_DEBUG_READONLY_OPS
5048                              " PERL_DEBUG_READONLY_OPS"
5049 #  endif
5050 #  ifdef PERL_GLOBAL_STRUCT
5051                              " PERL_GLOBAL_STRUCT"
5052 #  endif
5053 #  ifdef PERL_GLOBAL_STRUCT_PRIVATE
5054                              " PERL_GLOBAL_STRUCT_PRIVATE"
5055 #  endif
5056 #  ifdef PERL_IMPLICIT_CONTEXT
5057                              " PERL_IMPLICIT_CONTEXT"
5058 #  endif
5059 #  ifdef PERL_IMPLICIT_SYS
5060                              " PERL_IMPLICIT_SYS"
5061 #  endif
5062 #  ifdef PERL_MICRO
5063                              " PERL_MICRO"
5064 #  endif
5065 #  ifdef PERL_NEED_APPCTX
5066                              " PERL_NEED_APPCTX"
5067 #  endif
5068 #  ifdef PERL_NEED_TIMESBASE
5069                              " PERL_NEED_TIMESBASE"
5070 #  endif
5071 #  ifdef PERL_POISON
5072                              " PERL_POISON"
5073 #  endif
5074 #  ifdef PERL_SAWAMPERSAND
5075                              " PERL_SAWAMPERSAND"
5076 #  endif
5077 #  ifdef PERL_TRACK_MEMPOOL
5078                              " PERL_TRACK_MEMPOOL"
5079 #  endif
5080 #  ifdef PERL_USES_PL_PIDSTATUS
5081                              " PERL_USES_PL_PIDSTATUS"
5082 #  endif
5083 #  ifdef USE_64_BIT_ALL
5084                              " USE_64_BIT_ALL"
5085 #  endif
5086 #  ifdef USE_64_BIT_INT
5087                              " USE_64_BIT_INT"
5088 #  endif
5089 #  ifdef USE_IEEE
5090                              " USE_IEEE"
5091 #  endif
5092 #  ifdef USE_ITHREADS
5093                              " USE_ITHREADS"
5094 #  endif
5095 #  ifdef USE_LARGE_FILES
5096                              " USE_LARGE_FILES"
5097 #  endif
5098 #  ifdef USE_LOCALE_COLLATE
5099                              " USE_LOCALE_COLLATE"
5100 #  endif
5101 #  ifdef USE_LOCALE_NUMERIC
5102                              " USE_LOCALE_NUMERIC"
5103 #  endif
5104 #  ifdef USE_LOCALE_TIME
5105                              " USE_LOCALE_TIME"
5106 #  endif
5107 #  ifdef USE_LONG_DOUBLE
5108                              " USE_LONG_DOUBLE"
5109 #  endif
5110 #  ifdef USE_PERLIO
5111                              " USE_PERLIO"
5112 #  endif
5113 #  ifdef USE_QUADMATH
5114                              " USE_QUADMATH"
5115 #  endif
5116 #  ifdef USE_REENTRANT_API
5117                              " USE_REENTRANT_API"
5118 #  endif
5119 #  ifdef USE_SOCKS
5120                              " USE_SOCKS"
5121 #  endif
5122 #  ifdef VMS_DO_SOCKETS
5123                              " VMS_DO_SOCKETS"
5124 #  endif
5125 #  ifdef VMS_SHORTEN_LONG_SYMBOLS
5126                              " VMS_SHORTEN_LONG_SYMBOLS"
5127 #  endif
5128 #  ifdef VMS_WE_ARE_CASE_SENSITIVE
5129                              " VMS_SYMBOL_CASE_AS_IS"
5130 #  endif
5131   "";
5132 #else
5133 EXTCONST char PL_bincompat_options[];
5134 #endif
5135
5136 #ifndef PERL_SET_PHASE
5137 #  define PERL_SET_PHASE(new_phase) \
5138     PERL_DTRACE_PROBE_PHASE(new_phase); \
5139     PL_phase = new_phase;
5140 #endif
5141
5142 /* The interpreter phases. If these ever change, PL_phase_names right below will
5143  * need to be updated accordingly. */
5144 enum perl_phase {
5145     PERL_PHASE_CONSTRUCT        = 0,
5146     PERL_PHASE_START            = 1,
5147     PERL_PHASE_CHECK            = 2,
5148     PERL_PHASE_INIT             = 3,
5149     PERL_PHASE_RUN              = 4,
5150     PERL_PHASE_END              = 5,
5151     PERL_PHASE_DESTRUCT         = 6
5152 };
5153
5154 #ifdef DOINIT
5155 EXTCONST char *const PL_phase_names[] = {
5156     "CONSTRUCT",
5157     "START",
5158     "CHECK",
5159     "INIT",
5160     "RUN",
5161     "END",
5162     "DESTRUCT"
5163 };
5164 #else
5165 EXTCONST char *const PL_phase_names[];
5166 #endif
5167
5168 #ifndef PERL_CORE
5169 /* Do not use this macro. It only exists for extensions that rely on PL_dirty
5170  * instead of using the newer PL_phase, which provides everything PL_dirty
5171  * provided, and more. */
5172 #  define PL_dirty cBOOL(PL_phase == PERL_PHASE_DESTRUCT)
5173
5174 #  define PL_amagic_generation PL_na
5175 #  define PL_encoding ((SV *)NULL)
5176 #endif /* !PERL_CORE */
5177
5178 #define PL_hints PL_compiling.cop_hints
5179 #define PL_maxo  MAXO
5180
5181 END_EXTERN_C
5182
5183 /*****************************************************************************/
5184 /* This lexer/parser stuff is currently global since yacc is hard to reenter */
5185 /*****************************************************************************/
5186 /* XXX This needs to be revisited, since BEGIN makes yacc re-enter... */
5187
5188 #ifdef __Lynx__
5189 /* LynxOS defines these in scsi.h which is included via ioctl.h */
5190 #ifdef FORMAT
5191 #undef FORMAT
5192 #endif
5193 #ifdef SPACE
5194 #undef SPACE
5195 #endif
5196 #endif
5197
5198 #define LEX_NOTPARSING          11      /* borrowed from toke.c */
5199
5200 typedef enum {
5201     XOPERATOR,
5202     XTERM,
5203     XREF,
5204     XSTATE,
5205     XBLOCK,
5206     XATTRBLOCK, /* next token should be an attribute or block */
5207     XATTRTERM,  /* next token should be an attribute, or block in a term */
5208     XTERMBLOCK,
5209     XBLOCKTERM,
5210     XPOSTDEREF,
5211     XTERMORDORDOR /* evil hack */
5212     /* update exp_name[] in toke.c if adding to this enum */
5213 } expectation;
5214
5215 #define KEY_sigvar 0xFFFF /* fake keyword representing a signature var */
5216
5217 /* Hints are now stored in a dedicated U32, so the bottom 8 bits are no longer
5218    special and there is no need for HINT_PRIVATE_MASK for COPs
5219    However, bitops store HINT_INTEGER in their op_private.
5220
5221     NOTE: The typical module using these has the bit value hard-coded, so don't
5222     blindly change the values of these.
5223
5224    If we run out of bits, the 2 locale ones could be combined.  The PARTIAL one
5225    is for "use locale 'FOO'" which excludes some categories.  It requires going
5226    to %^H to find out which are in and which are out.  This could be extended
5227    for the normal case of a plain HINT_LOCALE, so that %^H would be used for
5228    any locale form. */
5229 #define HINT_INTEGER            0x00000001 /* integer pragma */
5230 #define HINT_STRICT_REFS        0x00000002 /* strict pragma */
5231 #define HINT_LOCALE             0x00000004 /* locale pragma */
5232 #define HINT_BYTES              0x00000008 /* bytes pragma */
5233 #define HINT_LOCALE_PARTIAL     0x00000010 /* locale, but a subset of categories */
5234
5235 #define HINT_EXPLICIT_STRICT_REFS       0x00000020 /* strict.pm */
5236 #define HINT_EXPLICIT_STRICT_SUBS       0x00000040 /* strict.pm */
5237 #define HINT_EXPLICIT_STRICT_VARS       0x00000080 /* strict.pm */
5238
5239 #define HINT_BLOCK_SCOPE        0x00000100
5240 #define HINT_STRICT_SUBS        0x00000200 /* strict pragma */
5241 #define HINT_STRICT_VARS        0x00000400 /* strict pragma */
5242 #define HINT_UNI_8_BIT          0x00000800 /* unicode_strings feature */
5243
5244 /* The HINT_NEW_* constants are used by the overload pragma */
5245 #define HINT_NEW_INTEGER        0x00001000
5246 #define HINT_NEW_FLOAT          0x00002000
5247 #define HINT_NEW_BINARY         0x00004000
5248 #define HINT_NEW_STRING         0x00008000
5249 #define HINT_NEW_RE             0x00010000
5250 #define HINT_LOCALIZE_HH        0x00020000 /* %^H needs to be copied */
5251 #define HINT_LEXICAL_IO_IN      0x00040000 /* ${^OPEN} is set for input */
5252 #define HINT_LEXICAL_IO_OUT     0x00080000 /* ${^OPEN} is set for output */
5253
5254 #define HINT_RE_TAINT           0x00100000 /* re pragma */
5255 #define HINT_RE_EVAL            0x00200000 /* re pragma */
5256
5257 #define HINT_FILETEST_ACCESS    0x00400000 /* filetest pragma */
5258 #define HINT_UTF8               0x00800000 /* utf8 pragma */
5259
5260 #define HINT_NO_AMAGIC          0x01000000 /* overloading pragma */
5261
5262 #define HINT_RE_FLAGS           0x02000000 /* re '/xism' pragma */
5263
5264 #define HINT_FEATURE_MASK       0x1c000000 /* 3 bits for feature bundles */
5265
5266                                 /* Note: Used for HINT_M_VMSISH_*,
5267                                    currently defined by vms/vmsish.h:
5268                                 0x40000000
5269                                 0x80000000
5270                                  */
5271
5272 /* The following are stored in $^H{sort}, not in PL_hints */
5273 #define HINT_SORT_STABLE        0x00000100 /* sort styles */
5274 #define HINT_SORT_UNSTABLE      0x00000200
5275
5276 /* flags for PL_sawampersand */
5277
5278 #define SAWAMPERSAND_LEFT       1   /* saw $` */
5279 #define SAWAMPERSAND_MIDDLE     2   /* saw $& */
5280 #define SAWAMPERSAND_RIGHT      4   /* saw $' */
5281
5282 #ifndef PERL_SAWAMPERSAND
5283 # define PL_sawampersand \
5284         (SAWAMPERSAND_LEFT|SAWAMPERSAND_MIDDLE|SAWAMPERSAND_RIGHT)
5285 #endif
5286
5287 /* Used for debugvar magic */
5288 #define DBVARMG_SINGLE  0
5289 #define DBVARMG_TRACE   1
5290 #define DBVARMG_SIGNAL  2
5291 #define DBVARMG_COUNT   3
5292
5293 #define PL_DBsingle_iv  (PL_DBcontrol[DBVARMG_SINGLE])
5294 #define PL_DBtrace_iv   (PL_DBcontrol[DBVARMG_TRACE])
5295 #define PL_DBsignal_iv  (PL_DBcontrol[DBVARMG_SIGNAL])
5296
5297 /* Various states of the input record separator SV (rs) */
5298 #define RsSNARF(sv)   (! SvOK(sv))
5299 #define RsSIMPLE(sv)  (SvOK(sv) && (! SvPOK(sv) || SvCUR(sv)))
5300 #define RsPARA(sv)    (SvPOK(sv) && ! SvCUR(sv))
5301 #define RsRECORD(sv)  (SvROK(sv) && (SvIV(SvRV(sv)) > 0))
5302
5303 /* A struct for keeping various DEBUGGING related stuff,
5304  * neatly packed.  Currently only scratch variables for
5305  * constructing debug output are included.  Needed always,
5306  * not just when DEBUGGING, though, because of the re extension. c*/
5307 struct perl_debug_pad {
5308   SV pad[3];
5309 };
5310
5311 #define PERL_DEBUG_PAD(i)       &(PL_debug_pad.pad[i])
5312 #define PERL_DEBUG_PAD_ZERO(i)  (SvPVX(PERL_DEBUG_PAD(i))[0] = 0, \
5313         (((XPV*) SvANY(PERL_DEBUG_PAD(i)))->xpv_cur = 0), \
5314         PERL_DEBUG_PAD(i))
5315
5316 /* Enable variables which are pointers to functions */
5317 typedef void (*peep_t)(pTHX_ OP* o);
5318 typedef regexp* (*regcomp_t) (pTHX_ char* exp, char* xend, PMOP* pm);
5319 typedef I32     (*regexec_t) (pTHX_ regexp* prog, char* stringarg,
5320                                       char* strend, char* strbeg, I32 minend,
5321                                       SV* screamer, void* data, U32 flags);
5322 typedef char*   (*re_intuit_start_t) (pTHX_ regexp *prog, SV *sv,
5323                                                 char *strpos, char *strend,
5324                                                 U32 flags,
5325                                                 re_scream_pos_data *d);
5326 typedef SV*     (*re_intuit_string_t) (pTHX_ regexp *prog);
5327 typedef void    (*regfree_t) (pTHX_ struct regexp* r);
5328 typedef regexp* (*regdupe_t) (pTHX_ const regexp* r, CLONE_PARAMS *param);
5329 typedef I32     (*re_fold_t)(const char *, char const *, I32);
5330
5331 typedef void (*DESTRUCTORFUNC_NOCONTEXT_t) (void*);
5332 typedef void (*DESTRUCTORFUNC_t) (pTHX_ void*);
5333 typedef void (*SVFUNC_t) (pTHX_ SV* const);
5334 typedef I32  (*SVCOMPARE_t) (pTHX_ SV* const, SV* const);
5335 typedef void (*XSINIT_t) (pTHX);
5336 typedef void (*ATEXIT_t) (pTHX_ void*);
5337 typedef void (*XSUBADDR_t) (pTHX_ CV *);
5338
5339 typedef OP* (*Perl_ppaddr_t)(pTHX);
5340 typedef OP* (*Perl_check_t) (pTHX_ OP*);
5341 typedef void(*Perl_ophook_t)(pTHX_ OP*);
5342 typedef int (*Perl_keyword_plugin_t)(pTHX_ char*, STRLEN, OP**);
5343 typedef void(*Perl_cpeep_t)(pTHX_ OP *, OP *);
5344
5345 typedef void(*globhook_t)(pTHX);
5346
5347 #define KEYWORD_PLUGIN_DECLINE 0
5348 #define KEYWORD_PLUGIN_STMT    1
5349 #define KEYWORD_PLUGIN_EXPR    2
5350
5351 /* Interpreter exitlist entry */
5352 typedef struct exitlistentry {
5353     void (*fn) (pTHX_ void*);
5354     void *ptr;
5355 } PerlExitListEntry;
5356
5357 /* if you only have signal() and it resets on each signal, FAKE_PERSISTENT_SIGNAL_HANDLERS fixes */
5358 /* These have to be before perlvars.h */
5359 #if !defined(HAS_SIGACTION) && defined(VMS)
5360 #  define  FAKE_PERSISTENT_SIGNAL_HANDLERS
5361 #endif
5362 /* if we're doing kill() with sys$sigprc on VMS, FAKE_DEFAULT_SIGNAL_HANDLERS */
5363 #if defined(KILL_BY_SIGPRC)
5364 #  define  FAKE_DEFAULT_SIGNAL_HANDLERS
5365 #endif
5366
5367 #if !defined(MULTIPLICITY)
5368
5369 struct interpreter {
5370     char broiled;
5371 };
5372
5373 #else
5374
5375 /* If we have multiple interpreters define a struct
5376    holding variables which must be per-interpreter
5377    If we don't have threads anything that would have
5378    be per-thread is per-interpreter.
5379 */
5380
5381 /* Set up PERLVAR macros for populating structs */
5382 #  define PERLVAR(prefix,var,type) type prefix##var;
5383
5384 /* 'var' is an array of length 'n' */
5385 #  define PERLVARA(prefix,var,n,type) type prefix##var[n];
5386
5387 /* initialize 'var' to init' */
5388 #  define PERLVARI(prefix,var,type,init) type prefix##var;
5389
5390 /* like PERLVARI, but make 'var' a const */
5391 #  define PERLVARIC(prefix,var,type,init) type prefix##var;
5392
5393 struct interpreter {
5394 #  include "intrpvar.h"
5395 };
5396
5397 EXTCONST U16 PL_interp_size
5398   INIT(sizeof(struct interpreter));
5399
5400 #  define PERL_INTERPRETER_SIZE_UPTO_MEMBER(member)                     \
5401     STRUCT_OFFSET(struct interpreter, member) +                         \
5402     sizeof(((struct interpreter*)0)->member)
5403
5404 /* This will be useful for subsequent releases, because this has to be the
5405    same in your libperl as in main(), else you have a mismatch and must abort.
5406 */
5407 EXTCONST U16 PL_interp_size_5_18_0
5408   INIT(PERL_INTERPRETER_SIZE_UPTO_MEMBER(PERL_LAST_5_18_0_INTERP_MEMBER));
5409
5410
5411 #  ifdef PERL_GLOBAL_STRUCT
5412 /* MULTIPLICITY is automatically defined when PERL_GLOBAL_STRUCT is defined,
5413    hence it's safe and sane to nest this within #ifdef MULTIPLICITY  */
5414
5415 struct perl_vars {
5416 #    include "perlvars.h"
5417 };
5418
5419 EXTCONST U16 PL_global_struct_size
5420   INIT(sizeof(struct perl_vars));
5421
5422 #    ifdef PERL_CORE
5423 #      ifndef PERL_GLOBAL_STRUCT_PRIVATE
5424 EXT struct perl_vars PL_Vars;
5425 EXT struct perl_vars *PL_VarsPtr INIT(&PL_Vars);
5426 #        undef PERL_GET_VARS
5427 #        define PERL_GET_VARS() PL_VarsPtr
5428 #      endif /* !PERL_GLOBAL_STRUCT_PRIVATE */
5429 #    else /* PERL_CORE */
5430 #      if !defined(__GNUC__) || !defined(WIN32)
5431 EXT
5432 #      endif /* WIN32 */
5433 struct perl_vars *PL_VarsPtr;
5434 #      define PL_Vars (*((PL_VarsPtr) \
5435                        ? PL_VarsPtr : (PL_VarsPtr = Perl_GetVars(aTHX))))
5436 #    endif /* PERL_CORE */
5437 #  endif /* PERL_GLOBAL_STRUCT */
5438
5439 /* Done with PERLVAR macros for now ... */
5440 #  undef PERLVAR
5441 #  undef PERLVARA
5442 #  undef PERLVARI
5443 #  undef PERLVARIC
5444
5445 #endif /* MULTIPLICITY */
5446
5447 struct tempsym; /* defined in pp_pack.c */
5448
5449 #include "thread.h"
5450 #include "pp.h"
5451
5452 #undef PERL_CKDEF
5453 #undef PERL_PPDEF
5454 #define PERL_CKDEF(s)   PERL_CALLCONV OP *s (pTHX_ OP *o);
5455 #define PERL_PPDEF(s)   PERL_CALLCONV OP *s (pTHX);
5456
5457 #ifdef MYMALLOC
5458 #  include "malloc_ctl.h"
5459 #endif
5460
5461 /*
5462  * This provides a layer of functions and macros to ensure extensions will
5463  * get to use the same RTL functions as the core.
5464  */
5465 #if defined(WIN32)
5466 #  include "win32iop.h"
5467 #endif
5468
5469
5470 #include "proto.h"
5471
5472 /* this has structure inits, so it cannot be included before here */
5473 #include "opcode.h"
5474
5475 /* The following must follow proto.h as #defines mess up syntax */
5476
5477 #if !defined(PERL_FOR_X2P)
5478 #  include "embedvar.h"
5479 #endif
5480
5481 /* Now include all the 'global' variables
5482  * If we don't have threads or multiple interpreters
5483  * these include variables that would have been their struct-s
5484  */
5485
5486 #define PERLVAR(prefix,var,type) EXT type PL_##var;
5487 #define PERLVARA(prefix,var,n,type) EXT type PL_##var[n];
5488 #define PERLVARI(prefix,var,type,init) EXT type  PL_##var INIT(init);
5489 #define PERLVARIC(prefix,var,type,init) EXTCONST type PL_##var INIT(init);
5490
5491 #if !defined(MULTIPLICITY)
5492 START_EXTERN_C
5493 #  include "intrpvar.h"
5494 END_EXTERN_C
5495 #  define PL_sv_yes   (PL_sv_immortals[0])
5496 #  define PL_sv_undef (PL_sv_immortals[1])
5497 #  define PL_sv_no    (PL_sv_immortals[2])
5498 #  define PL_sv_zero  (PL_sv_immortals[3])
5499 #endif
5500
5501 #ifdef PERL_CORE
5502 /* All core uses now exterminated. Ensure no zombies can return:  */
5503 #  undef PL_na
5504 #endif
5505
5506 /* Now all the config stuff is setup we can include embed.h
5507    In particular, need the relevant *ish file included already, as it may
5508    define HAVE_INTERP_INTERN  */
5509 #include "embed.h"
5510
5511 #ifndef PERL_GLOBAL_STRUCT
5512 START_EXTERN_C
5513
5514 #  include "perlvars.h"
5515
5516 END_EXTERN_C
5517 #endif
5518
5519 #undef PERLVAR
5520 #undef PERLVARA
5521 #undef PERLVARI
5522 #undef PERLVARIC
5523
5524 #if !defined(MULTIPLICITY)
5525 /* Set up PERLVAR macros for populating structs */
5526 #  define PERLVAR(prefix,var,type) type prefix##var;
5527 /* 'var' is an array of length 'n' */
5528 #  define PERLVARA(prefix,var,n,type) type prefix##var[n];
5529 /* initialize 'var' to init' */
5530 #  define PERLVARI(prefix,var,type,init) type prefix##var;
5531 /* like PERLVARI, but make 'var' a const */
5532 #  define PERLVARIC(prefix,var,type,init) type prefix##var;
5533
5534 /* this is never instantiated, is it just used for sizeof(struct PerlHandShakeInterpreter) */
5535 struct PerlHandShakeInterpreter {
5536 #  include "intrpvar.h"
5537 };
5538 #  undef PERLVAR
5539 #  undef PERLVARA
5540 #  undef PERLVARI
5541 #  undef PERLVARIC
5542 #endif
5543
5544 START_EXTERN_C
5545
5546 /* dummy variables that hold pointers to both runops functions, thus forcing
5547  * them *both* to get linked in (useful for Peek.xs, debugging etc) */
5548
5549 EXTCONST runops_proc_t PL_runops_std
5550   INIT(Perl_runops_standard);
5551 EXTCONST runops_proc_t PL_runops_dbg
5552   INIT(Perl_runops_debug);
5553
5554 #define EXT_MGVTBL EXTCONST MGVTBL
5555
5556 #define PERL_MAGIC_READONLY_ACCEPTABLE 0x40
5557 #define PERL_MAGIC_VALUE_MAGIC 0x80
5558 #define PERL_MAGIC_VTABLE_MASK 0x3F
5559 #define PERL_MAGIC_TYPE_READONLY_ACCEPTABLE(t) \
5560     (PL_magic_data[(U8)(t)] & PERL_MAGIC_READONLY_ACCEPTABLE)
5561 #define PERL_MAGIC_TYPE_IS_VALUE_MAGIC(t) \
5562     (PL_magic_data[(U8)(t)] & PERL_MAGIC_VALUE_MAGIC)
5563
5564 #include "mg_vtable.h"
5565
5566 #ifdef DOINIT
5567 EXTCONST U8 PL_magic_data[256] =
5568 #  ifdef PERL_MICRO
5569 #    include "umg_data.h"
5570 #  else
5571 #    include "mg_data.h"
5572 #  endif
5573 ;
5574 #else
5575 EXTCONST U8 PL_magic_data[256];
5576 #endif
5577
5578 #ifdef DOINIT
5579                         /* NL IV NV PV INV PI PN MG RX GV LV AV HV CV FM IO */
5580 EXTCONST bool
5581 PL_valid_types_IVX[]    = { 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 };
5582 EXTCONST bool
5583 PL_valid_types_NVX[]    = { 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 };
5584 EXTCONST bool
5585 PL_valid_types_PVX[]    = { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1 };
5586 EXTCONST bool
5587 PL_valid_types_RV[]     = { 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 };
5588 EXTCONST bool
5589 PL_valid_types_IV_set[] = { 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1 };
5590 EXTCONST bool
5591 PL_valid_types_NV_set[] = { 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
5592
5593 #else
5594
5595 EXTCONST bool PL_valid_types_IVX[];
5596 EXTCONST bool PL_valid_types_NVX[];
5597 EXTCONST bool PL_valid_types_PVX[];
5598 EXTCONST bool PL_valid_types_RV[];
5599 EXTCONST bool PL_valid_types_IV_set[];
5600 EXTCONST bool PL_valid_types_NV_set[];
5601
5602 #endif
5603
5604 /* In C99 we could use designated (named field) union initializers.
5605  * In C89 we need to initialize the member declared first.
5606  * In C++ we need extern C initializers.
5607  *
5608  * With the U8_NV version you will want to have inner braces,
5609  * while with the NV_U8 use just the NV. */
5610
5611 #define INFNAN_U8_NV_DECL EXTCONST union { U8 u8[NVSIZE]; NV nv; }
5612 #define INFNAN_NV_U8_DECL EXTCONST union { NV nv; U8 u8[NVSIZE]; }
5613
5614 /* if these never got defined, they need defaults */
5615 #ifndef PERL_SET_CONTEXT
5616 #  define PERL_SET_CONTEXT(i)           PERL_SET_INTERP(i)
5617 #endif
5618
5619 #ifndef PERL_GET_CONTEXT
5620 #  define PERL_GET_CONTEXT              PERL_GET_INTERP
5621 #endif
5622
5623 #ifndef PERL_GET_THX
5624 #  define PERL_GET_THX                  ((void*)NULL)
5625 #endif
5626
5627 #ifndef PERL_SET_THX
5628 #  define PERL_SET_THX(t)               NOOP
5629 #endif
5630
5631 #ifndef EBCDIC
5632
5633 /* The tables below are adapted from
5634  * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/, which requires this copyright
5635  * notice:
5636
5637 Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
5638
5639 Permission is hereby granted, free of charge, to any person obtaining a copy of
5640 this software and associated documentation files (the "Software"), to deal in
5641 the Software without restriction, including without limitation the rights to
5642 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
5643 of the Software, and to permit persons to whom the Software is furnished to do
5644 so, subject to the following conditions:
5645
5646 The above copyright notice and this permission notice shall be included in all
5647 copies or substantial portions of the Software.
5648
5649 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5650 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
5651 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
5652 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
5653 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
5654 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
5655 SOFTWARE.
5656
5657 */
5658
5659 #  ifdef DOINIT
5660 #    if 0       /* This is the original table given in
5661                    http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ */
5662 static U8 utf8d_C9[] = {
5663   /* The first part of the table maps bytes to character classes that
5664    * to reduce the size of the transition table and create bitmasks. */
5665    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /*-1F*/
5666    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /*-3F*/
5667    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /*-5F*/
5668    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /*-7F*/
5669    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,  9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, /*-9F*/
5670    7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, /*-BF*/
5671    8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2,  2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, /*-DF*/
5672   10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, /*-FF*/
5673
5674   /* The second part is a transition table that maps a combination
5675    * of a state of the automaton and a character class to a state. */
5676    0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
5677   12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
5678   12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
5679   12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
5680   12,36,12,12,12,12,12,12,12,12,12,12
5681 };
5682
5683 #    endif
5684
5685 /* This is a version of the above table customized for Perl that doesn't
5686  * exclude surrogates and accepts start bytes up through FD (FE on 64-bit
5687  * machines).  The classes have been renumbered so that the patterns are more
5688  * evident in the table.  The class numbers for start bytes are constrained so
5689  * that they can be used as a shift count for masking off the leading one bits.
5690  * It would make the code simpler if start byte FF could also be handled, but
5691  * doing so would mean adding nodes for each of continuation bytes 6-12
5692  * remaining, and two more nodes for overlong detection (a total of 9), and
5693  * there is room only for 4 more nodes unless we make the array U16 instead of
5694  * U8.
5695  *
5696  * The classes are
5697  *      00-7F           0
5698  *      80-81           7   Not legal immediately after start bytes E0 F0 F8 FC
5699  *                          FE
5700  *      82-83           8   Not legal immediately after start bytes E0 F0 F8 FC
5701  *      84-87           9   Not legal immediately after start bytes E0 F0 F8
5702  *      88-8F          10   Not legal immediately after start bytes E0 F0
5703  *      90-9F          11   Not legal immediately after start byte E0
5704  *      A0-BF          12
5705  *      C0,C1           1
5706  *      C2-DF           2
5707  *      E0             13
5708  *      E1-EF           3
5709  *      F0             14
5710  *      F1-F7           4
5711  *      F8             15
5712  *      F9-FB           5
5713  *      FC             16
5714  *      FD              6
5715  *      FE             17  (or 1 on 32-bit machines, since it overflows)
5716  *      FF              1
5717  */
5718
5719 EXTCONST U8 PL_extended_utf8_dfa_tab[] = {
5720     /* The first part of the table maps bytes to character classes to reduce
5721      * the size of the transition table and create bitmasks. */
5722    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*00-0F*/
5723    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*10-1F*/
5724    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*20-2F*/
5725    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*30-3F*/
5726    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*40-4F*/
5727    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*50-5F*/
5728    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*60-6F*/
5729    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*70-7F*/
5730    7, 7, 8, 8, 9, 9, 9, 9,10,10,10,10,10,10,10,10, /*80-8F*/
5731   11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, /*90-9F*/
5732   12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, /*A0-AF*/
5733   12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, /*B0-BF*/
5734    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /*C0-CF*/
5735    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /*D0-DF*/
5736   13, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /*E0-EF*/
5737   14, 4, 4, 4, 4, 4, 4, 4,15, 5, 5, 5,16, 6,       /*F0-FD*/
5738 #    ifdef UV_IS_QUAD
5739                                             17,    /*FE*/
5740 #    else
5741                                              1,    /*FE*/
5742 #    endif
5743                                                 1, /*FF*/
5744
5745 /* The second part is a transition table that maps a combination
5746  * of a state of the automaton and a character class to a new state, called a
5747  * node.  The nodes are:
5748  * N0     The initial state, and final accepting one.
5749  * N1     Any one continuation byte (80-BF) left.  This is transitioned to
5750  *        immediately when the start byte indicates a two-byte sequence
5751  * N2     Any two continuation bytes left.
5752  * N3     Any three continuation bytes left.
5753  * N4     Any four continuation bytes left.
5754  * N5     Any five continuation bytes left.
5755  * N6     Start byte is E0.  Continuation bytes 80-9F are illegal (overlong);
5756  *        the other continuations transition to N1
5757  * N7     Start byte is F0.  Continuation bytes 80-8F are illegal (overlong);
5758  *        the other continuations transition to N2
5759  * N8     Start byte is F8.  Continuation bytes 80-87 are illegal (overlong);
5760  *        the other continuations transition to N3
5761  * N9     Start byte is FC.  Continuation bytes 80-83 are illegal (overlong);
5762  *        the other continuations transition to N4
5763  * N10    Start byte is FE.  Continuation bytes 80-81 are illegal (overlong);
5764  *        the other continuations transition to N5
5765  * 1      Reject.  All transitions not mentioned above (except the single
5766  *        byte ones (as they are always legal) are to this state.
5767  */
5768
5769 #    define NUM_CLASSES 18
5770 #    define N0 0
5771 #    define N1 ((N0)   + NUM_CLASSES)
5772 #    define N2 ((N1)   + NUM_CLASSES)
5773 #    define N3 ((N2)   + NUM_CLASSES)
5774 #    define N4 ((N3)   + NUM_CLASSES)
5775 #    define N5 ((N4)   + NUM_CLASSES)
5776 #    define N6 ((N5)   + NUM_CLASSES)
5777 #    define N7 ((N6)   + NUM_CLASSES)
5778 #    define N8 ((N7)   + NUM_CLASSES)
5779 #    define N9 ((N8)   + NUM_CLASSES)
5780 #    define N10 ((N9)  + NUM_CLASSES)
5781
5782 /*Class: 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17  */
5783 /*N0*/   0, 1,N1,N2,N3,N4,N5, 1, 1, 1, 1, 1, 1,N6,N7,N8,N9,N10,
5784 /*N1*/   1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
5785 /*N2*/   1, 1, 1, 1, 1, 1, 1,N1,N1,N1,N1,N1,N1, 1, 1, 1, 1, 1,
5786 /*N3*/   1, 1, 1, 1, 1, 1, 1,N2,N2,N2,N2,N2,N2, 1, 1, 1, 1, 1,
5787 /*N4*/   1, 1, 1, 1, 1, 1, 1,N3,N3,N3,N3,N3,N3, 1, 1, 1, 1, 1,
5788 /*N5*/   1, 1, 1, 1, 1, 1, 1,N4,N4,N4,N4,N4,N4, 1, 1, 1, 1, 1,
5789
5790 /*N6*/   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,N1, 1, 1, 1, 1, 1,
5791 /*N7*/   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,N2,N2, 1, 1, 1, 1, 1,
5792 /*N8*/   1, 1, 1, 1, 1, 1, 1, 1, 1, 1,N3,N3,N3, 1, 1, 1, 1, 1,
5793 /*N9*/   1, 1, 1, 1, 1, 1, 1, 1, 1,N4,N4,N4,N4, 1, 1, 1, 1, 1,
5794 /*N10*/  1, 1, 1, 1, 1, 1, 1, 1,N5,N5,N5,N5,N5, 1, 1, 1, 1, 1,
5795 };
5796
5797 /* And below is a version of the above table that accepts only strict UTF-8.
5798  * Hence no surrogates nor non-characters, nor non-Unicode.  Thus, if the input
5799  * passes this dfa, it will be for a well-formed, non-problematic code point
5800  * that can be returned immediately.
5801  *
5802  * The "Implementation details" portion of
5803  * http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ shows how
5804  * the first portion of the table maps each possible byte into a character
5805  * class.  And that the classes for those bytes which are start bytes have been
5806  * carefully chosen so they serve as well to be used as a shift value to mask
5807  * off the leading 1 bits of the start byte.  Unfortunately the addition of
5808  * being able to distinguish non-characters makes this not fully work.  This is
5809  * because, now, the start bytes E1-EF have to be broken into 3 classes instead
5810  * of 2:
5811  *  1) ED because it could be a surrogate
5812  *  2) EF because it could be a non-character
5813  *  3) the rest, which can never evaluate to a problematic code point.
5814  *
5815  * Each of E1-EF has three leading 1 bits, then a 0.  That means we could use a
5816  * shift (and hence class number) of either 3 or 4 to get a mask that works.
5817  * But that only allows two categories, and we need three.  khw made the
5818  * decision to therefore treat the ED start byte as an error, so that the dfa
5819  * drops out immediately for that.  In the dfa, classes 3 and 4 are used to
5820  * distinguish EF vs the rest.  Then special code is used to deal with ED,
5821  * that's executed only when the dfa drops out.  The code points started by ED
5822  * are half surrogates, and half hangul syllables.  This means that 2048 of the
5823  * the hangul syllables (about 18%) take longer than all other non-problematic
5824  * code points to handle.
5825  *
5826  * The changes to handle non-characters requires the addition of states and
5827  * classes to the dfa.  (See the section on "Mapping bytes to character
5828  * classes" in the linked-to document for further explanation of the original
5829  * dfa.)
5830  *
5831  * The classes are
5832  *      00-7F           0
5833  *      80-8E           9
5834  *      8F             10
5835  *      90-9E          11
5836  *      9F             12
5837  *      A0-AE          13
5838  *      AF             14
5839  *      B0-B6          15
5840  *      B7             16
5841  *      B8-BD          15
5842  *      BE             17
5843  *      BF             18
5844  *      C0,C1           1
5845  *      C2-DF           2
5846  *      E0              7
5847  *      E1-EC           3
5848  *      ED              1
5849  *      EE              3
5850  *      EF              4
5851  *      F0              8
5852  *      F1-F3           6  (6 bits can be stripped)
5853  *      F4              5  (only 5 can be stripped)
5854  *      F5-FF           1
5855  */
5856
5857 EXTCONST U8 PL_strict_utf8_dfa_tab[] = {
5858     /* The first part of the table maps bytes to character classes to reduce
5859      * the size of the transition table and create bitmasks. */
5860    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*00-0F*/
5861    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*10-1F*/
5862    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*20-2F*/
5863    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*30-3F*/
5864    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*40-4F*/
5865    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*50-5F*/
5866    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*60-6F*/
5867    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*70-7F*/
5868    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,10, /*80-8F*/
5869   11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12, /*90-9F*/
5870   13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14, /*A0-AF*/
5871   15,15,15,15,15,15,15,16,15,15,15,15,15,15,17,18, /*B0-BF*/
5872    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /*C0-CF*/
5873    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /*D0-DF*/
5874    7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 3, 4, /*E0-EF*/
5875    8, 6, 6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*F0-FF*/
5876
5877 /* The second part is a transition table that maps a combination
5878  * of a state of the automaton and a character class to a new state, called a
5879  * node.  The nodes are:
5880  * N0     The initial state, and final accepting one.
5881  * N1     Any one continuation byte (80-BF) left.  This is transitioned to
5882  *        immediately when the start byte indicates a two-byte sequence
5883  * N2     Any two continuation bytes left.
5884  * N3     Start byte is E0.  Continuation bytes 80-9F are illegal (overlong);
5885  *        the other continuations transition to state N1
5886  * N4     Start byte is EF.  Continuation byte B7 transitions to N8; BF to N9;
5887  *        the other continuations transitions to N1
5888  * N5     Start byte is F0.  Continuation bytes 80-8F are illegal (overlong);
5889  *        [9AB]F transition to N10; the other continuations to N2.
5890  * N6     Start byte is F[123].  Continuation bytes [89AB]F transition
5891  *        to N10; the other continuations to N2.
5892  * N7     Start byte is F4.  Continuation bytes 90-BF are illegal
5893  *        (non-unicode); 8F transitions to N10; the other continuations to N2
5894  * N8     Initial sequence is EF B7.  Continuation bytes 90-AF are illegal
5895  *        (non-characters); the other continuations transition to N0.
5896  * N9     Initial sequence is EF BF.  Continuation bytes BE and BF are illegal
5897  *        (non-characters); the other continuations transition to N0.
5898  * N10    Initial sequence is one of: F0 [9-B]F; F[123] [8-B]F; or F4 8F.
5899  *        Continuation byte BF transitions to N11; the other continuations to
5900  *        N1
5901  * N11    Initial sequence is the two bytes given in N10 followed by BF.
5902  *        Continuation bytes BE and BF are illegal (non-characters); the other
5903  *        continuations transition to N0.
5904  * 1      Reject.  All transitions not mentioned above (except the single
5905  *        byte ones (as they are always legal) are to this state.
5906  */
5907
5908 #    undef N0
5909 #    undef N1
5910 #    undef N2
5911 #    undef N3
5912 #    undef N4
5913 #    undef N5
5914 #    undef N6
5915 #    undef N7
5916 #    undef N8
5917 #    undef N9
5918 #    undef NUM_CLASSES
5919 #    define NUM_CLASSES 19
5920 #    define N0 0
5921 #    define N1  ((N0)  + NUM_CLASSES)
5922 #    define N2  ((N1)  + NUM_CLASSES)
5923 #    define N3  ((N2)  + NUM_CLASSES)
5924 #    define N4  ((N3)  + NUM_CLASSES)
5925 #    define N5  ((N4)  + NUM_CLASSES)
5926 #    define N6  ((N5)  + NUM_CLASSES)
5927 #    define N7  ((N6)  + NUM_CLASSES)
5928 #    define N8  ((N7)  + NUM_CLASSES)
5929 #    define N9  ((N8)  + NUM_CLASSES)
5930 #    define N10 ((N9)  + NUM_CLASSES)
5931 #    define N11 ((N10) + NUM_CLASSES)
5932
5933 /*Class: 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18 */
5934 /*N0*/   0,  1, N1, N2, N4, N7, N6, N3, N5,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
5935 /*N1*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
5936 /*N2*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N1, N1, N1, N1, N1, N1, N1, N1, N1, N1,
5937
5938 /*N3*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, N1, N1, N1, N1, N1, N1,
5939 /*N4*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N1, N1, N1, N1, N1, N1, N1, N8, N1, N9,
5940 /*N5*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, N2,N10, N2,N10, N2, N2, N2,N10,
5941 /*N6*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N2,N10, N2,N10, N2,N10, N2, N2, N2,N10,
5942 /*N7*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N2,N10,  1,  1,  1,  1,  1,  1,  1,  1,
5943 /*N8*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  1,  1,  1,  1,  0,  0,  0,  0,
5944 /*N9*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,
5945 /*N10*/  1,  1,  1,  1,  1,  1,  1,  1,  1, N1, N1, N1, N1, N1, N1, N1, N1, N1,N11,
5946 /*N11*/  1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,
5947 };
5948
5949 /* And below is yet another version of the above tables that accepts only UTF-8
5950  * as defined by Corregidum #9.  Hence no surrogates nor non-Unicode, but
5951  * it allows non-characters.  This is isomorphic to the original table
5952  * in http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
5953  *
5954  * The classes are
5955  *      00-7F           0
5956  *      80-8F           9
5957  *      90-9F          10
5958  *      A0-BF          11
5959  *      C0,C1           1
5960  *      C2-DF           2
5961  *      E0              7
5962  *      E1-EC           3
5963  *      ED              4
5964  *      EE-EF           3
5965  *      F0              8
5966  *      F1-F3           6  (6 bits can be stripped)
5967  *      F4              5  (only 5 can be stripped)
5968  *      F5-FF           1
5969  */
5970
5971 EXTCONST U8 PL_c9_utf8_dfa_tab[] = {
5972     /* The first part of the table maps bytes to character classes to reduce
5973      * the size of the transition table and create bitmasks. */
5974    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*00-0F*/
5975    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*10-1F*/
5976    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*20-2F*/
5977    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*30-3F*/
5978    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*40-4F*/
5979    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*50-5F*/
5980    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*60-6F*/
5981    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /*70-7F*/
5982    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, /*80-8F*/
5983   10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, /*90-9F*/
5984   11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, /*A0-AF*/
5985   11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, /*B0-BF*/
5986    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /*C0-CF*/
5987    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /*D0-DF*/
5988    7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 3, /*E0-EF*/
5989    8, 6, 6, 6, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /*F0-FF*/
5990
5991 /* The second part is a transition table that maps a combination
5992  * of a state of the automaton and a character class to a new state, called a
5993  * node.  The nodes are:
5994  * N0     The initial state, and final accepting one.
5995  * N1     Any one continuation byte (80-BF) left.  This is transitioned to
5996  *        immediately when the start byte indicates a two-byte sequence
5997  * N2     Any two continuation bytes left.
5998  * N3     Any three continuation bytes left.
5999  * N4     Start byte is E0.  Continuation bytes 80-9F are illegal (overlong);
6000  *        the other continuations transition to state N1
6001  * N5     Start byte is ED.  Continuation bytes A0-BF all lead to surrogates,
6002  *        so are illegal.  The other continuations transition to state N1.
6003  * N6     Start byte is F0.  Continuation bytes 80-8F are illegal (overlong);
6004  *        the other continuations transition to N2
6005  * N7     Start byte is F4.  Continuation bytes 90-BF are illegal
6006  *        (non-unicode); the other continuations transition to N2
6007  * 1      Reject.  All transitions not mentioned above (except the single
6008  *        byte ones (as they are always legal) are to this state.
6009  */
6010
6011 #    undef N0
6012 #    undef N1
6013 #    undef N2
6014 #    undef N3
6015 #    undef N4
6016 #    undef N5
6017 #    undef N6
6018 #    undef N7
6019 #    undef NUM_CLASSES
6020 #    define NUM_CLASSES 12
6021 #    define N0 0
6022 #    define N1  ((N0)  + NUM_CLASSES)
6023 #    define N2  ((N1)  + NUM_CLASSES)
6024 #    define N3  ((N2)  + NUM_CLASSES)
6025 #    define N4  ((N3)  + NUM_CLASSES)
6026 #    define N5  ((N4)  + NUM_CLASSES)
6027 #    define N6  ((N5)  + NUM_CLASSES)
6028 #    define N7  ((N6)  + NUM_CLASSES)
6029
6030 /*Class: 0   1   2   3   4   5   6   7   8   9  10  11 */
6031 /*N0*/   0,  1, N1, N2, N5, N7, N3, N4, N6,  1,  1,  1,
6032 /*N1*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  0,  0,  0,
6033 /*N2*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N1, N1, N1,
6034 /*N3*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N2, N2, N2,
6035
6036 /*N4*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1, N1,
6037 /*N5*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N1, N1,  1,
6038 /*N6*/   1,  1,  1,  1,  1,  1,  1,  1,  1,  1, N2, N2,
6039 /*N7*/   1,  1,  1,  1,  1,  1,  1,  1,  1, N2,  1,  1,
6040 };
6041
6042 #  else     /* End of is DOINIT */
6043
6044 EXTCONST U8 PL_extended_utf8_dfa_tab[];
6045 EXTCONST U8 PL_strict_utf8_dfa_tab[];
6046 EXTCONST U8 PL_c9_utf8_dfa_tab[];
6047
6048 #  endif
6049 #endif    /* end of isn't EBCDIC */
6050
6051 #ifndef PERL_NO_INLINE_FUNCTIONS
6052 /* Static inline funcs that depend on includes and declarations above.
6053    Some of these reference functions in the perl object files, and some
6054    compilers aren't smart enough to eliminate unused static inline
6055    functions, so including this file in source code can cause link errors
6056    even if the source code uses none of the functions. Hence including these
6057    can be be suppressed by setting PERL_NO_INLINE_FUNCTIONS. Doing this will
6058    (obviously) result in unworkable XS code, but allows simple probing code
6059    to continue to work, because it permits tests to include the perl headers
6060    for definitions without creating a link dependency on the perl library
6061    (which may not exist yet).
6062 */
6063
6064 #  include "inline.h"
6065 #endif
6066
6067 #include "overload.h"
6068
6069 END_EXTERN_C
6070
6071 struct am_table {
6072   U8 flags;
6073   U8 fallback;
6074   U16 spare;
6075   U32 was_ok_sub;
6076   CV* table[NofAMmeth];
6077 };
6078 struct am_table_short {
6079   U8 flags;
6080   U8 fallback;
6081   U16 spare;
6082   U32 was_ok_sub;
6083 };
6084 typedef struct am_table AMT;
6085 typedef struct am_table_short AMTS;
6086
6087 #define AMGfallNEVER    1
6088 #define AMGfallNO       2
6089 #define AMGfallYES      3
6090
6091 #define AMTf_AMAGIC             1
6092 #define AMT_AMAGIC(amt)         ((amt)->flags & AMTf_AMAGIC)
6093 #define AMT_AMAGIC_on(amt)      ((amt)->flags |= AMTf_AMAGIC)
6094 #define AMT_AMAGIC_off(amt)     ((amt)->flags &= ~AMTf_AMAGIC)
6095
6096 #define StashHANDLER(stash,meth)        gv_handler((stash),CAT2(meth,_amg))
6097
6098 /*
6099  * some compilers like to redefine cos et alia as faster
6100  * (and less accurate?) versions called F_cos et cetera (Quidquid
6101  * latine dictum sit, altum viditur.)  This trick collides with
6102  * the Perl overloading (amg).  The following #defines fool both.
6103  */
6104
6105 #ifdef _FASTMATH
6106 #   ifdef atan2
6107 #       define F_atan2_amg  atan2_amg
6108 #   endif
6109 #   ifdef cos
6110 #       define F_cos_amg    cos_amg
6111 #   endif
6112 #   ifdef exp
6113 #       define F_exp_amg    exp_amg
6114 #   endif
6115 #   ifdef log
6116 #       define F_log_amg    log_amg
6117 #   endif
6118 #   ifdef pow
6119 #       define F_pow_amg    pow_amg
6120 #   endif
6121 #   ifdef sin
6122 #       define F_sin_amg    sin_amg
6123 #   endif
6124 #   ifdef sqrt
6125 #       define F_sqrt_amg   sqrt_amg
6126 #   endif
6127 #endif /* _FASTMATH */
6128
6129 #define PERLDB_ALL              (PERLDBf_SUB    | PERLDBf_LINE  |       \
6130                                  PERLDBf_NOOPT  | PERLDBf_INTER |       \
6131                                  PERLDBf_SUBLINE| PERLDBf_SINGLE|       \
6132                                  PERLDBf_NAMEEVAL| PERLDBf_NAMEANON |   \
6133                                  PERLDBf_SAVESRC)
6134                                         /* No _NONAME, _GOTO */
6135 #define PERLDBf_SUB             0x01    /* Debug sub enter/exit */
6136 #define PERLDBf_LINE            0x02    /* Keep line # */
6137 #define PERLDBf_NOOPT           0x04    /* Switch off optimizations */
6138 #define PERLDBf_INTER           0x08    /* Preserve more data for
6139                                            later inspections  */
6140 #define PERLDBf_SUBLINE         0x10    /* Keep subr source lines */
6141 #define PERLDBf_SINGLE          0x20    /* Start with single-step on */
6142 #define PERLDBf_NONAME          0x40    /* For _SUB: no name of the subr */
6143 #define PERLDBf_GOTO            0x80    /* Report goto: call DB::goto */
6144 #define PERLDBf_NAMEEVAL        0x100   /* Informative names for evals */
6145 #define PERLDBf_NAMEANON        0x200   /* Informative names for anon subs */
6146 #define PERLDBf_SAVESRC         0x400   /* Save source lines into @{"_<$filename"} */
6147 #define PERLDBf_SAVESRC_NOSUBS  0x800   /* Including evals that generate no subroutines */
6148 #define PERLDBf_SAVESRC_INVALID 0x1000  /* Save source that did not compile */
6149
6150 #define PERLDB_SUB              (PL_perldb & PERLDBf_SUB)
6151 #define PERLDB_LINE             (PL_perldb & PERLDBf_LINE)
6152 #define PERLDB_NOOPT            (PL_perldb & PERLDBf_NOOPT)
6153 #define PERLDB_INTER            (PL_perldb & PERLDBf_INTER)
6154 #define PERLDB_SUBLINE          (PL_perldb & PERLDBf_SUBLINE)
6155 #define PERLDB_SINGLE           (PL_perldb & PERLDBf_SINGLE)
6156 #define PERLDB_SUB_NN           (PL_perldb & PERLDBf_NONAME)
6157 #define PERLDB_GOTO             (PL_perldb & PERLDBf_GOTO)
6158 #define PERLDB_NAMEEVAL         (PL_perldb & PERLDBf_NAMEEVAL)
6159 #define PERLDB_NAMEANON         (PL_perldb & PERLDBf_NAMEANON)
6160 #define PERLDB_SAVESRC          (PL_perldb & PERLDBf_SAVESRC)
6161 #define PERLDB_SAVESRC_NOSUBS   (PL_perldb & PERLDBf_SAVESRC_NOSUBS)
6162 #define PERLDB_SAVESRC_INVALID  (PL_perldb & PERLDBf_SAVESRC_INVALID)
6163
6164 #define PERLDB_LINE_OR_SAVESRC (PL_perldb & (PERLDBf_LINE | PERLDBf_SAVESRC))
6165
6166 #ifdef USE_ITHREADS
6167 #  define KEYWORD_PLUGIN_MUTEX_INIT    MUTEX_INIT(&PL_keyword_plugin_mutex)
6168 #  define KEYWORD_PLUGIN_MUTEX_LOCK    MUTEX_LOCK(&PL_keyword_plugin_mutex)
6169 #  define KEYWORD_PLUGIN_MUTEX_UNLOCK  MUTEX_UNLOCK(&PL_keyword_plugin_mutex)
6170 #  define KEYWORD_PLUGIN_MUTEX_TERM    MUTEX_DESTROY(&PL_keyword_plugin_mutex)
6171 #  define USER_PROP_MUTEX_INIT    MUTEX_INIT(&PL_user_prop_mutex)
6172 #  define USER_PROP_MUTEX_LOCK    MUTEX_LOCK(&PL_user_prop_mutex)
6173 #  define USER_PROP_MUTEX_UNLOCK  MUTEX_UNLOCK(&PL_user_prop_mutex)
6174 #  define USER_PROP_MUTEX_TERM    MUTEX_DESTROY(&PL_user_prop_mutex)
6175 #else
6176 #  define KEYWORD_PLUGIN_MUTEX_INIT    NOOP
6177 #  define KEYWORD_PLUGIN_MUTEX_LOCK    NOOP
6178 #  define KEYWORD_PLUGIN_MUTEX_UNLOCK  NOOP
6179 #  define KEYWORD_PLUGIN_MUTEX_TERM    NOOP
6180 #  define USER_PROP_MUTEX_INIT    NOOP
6181 #  define USER_PROP_MUTEX_LOCK    NOOP
6182 #  define USER_PROP_MUTEX_UNLOCK  NOOP
6183 #  define USER_PROP_MUTEX_TERM    NOOP
6184 #endif
6185
6186 #ifdef USE_LOCALE /* These locale things are all subject to change */
6187
6188    /* Returns TRUE if the plain locale pragma without a parameter is in effect.
6189     * */
6190 #  define IN_LOCALE_RUNTIME     (PL_curcop                                  \
6191                               && CopHINTS_get(PL_curcop) & HINT_LOCALE)
6192
6193    /* Returns TRUE if either form of the locale pragma is in effect */
6194 #  define IN_SOME_LOCALE_FORM_RUNTIME                                       \
6195         cBOOL(CopHINTS_get(PL_curcop) & (HINT_LOCALE|HINT_LOCALE_PARTIAL))
6196
6197 #  define IN_LOCALE_COMPILETIME cBOOL(PL_hints & HINT_LOCALE)
6198 #  define IN_SOME_LOCALE_FORM_COMPILETIME                                   \
6199                         cBOOL(PL_hints & (HINT_LOCALE|HINT_LOCALE_PARTIAL))
6200
6201 /*
6202 =head1 Locale-related functions and macros
6203
6204 =for apidoc Amn|bool|IN_LOCALE
6205
6206 Evaluates to TRUE if the plain locale pragma without a parameter (S<C<use
6207 locale>>) is in effect.
6208
6209 =for apidoc Amn|bool|IN_LOCALE_COMPILETIME
6210
6211 Evaluates to TRUE if, when compiling a perl program (including an C<eval>) if
6212 the plain locale pragma without a parameter (S<C<use locale>>) is in effect.
6213
6214 =for apidoc Amn|bool|IN_LOCALE_RUNTIME
6215
6216 Evaluates to TRUE if, when executing a perl program (including an C<eval>) if
6217 the plain locale pragma without a parameter (S<C<use locale>>) is in effect.
6218
6219 =cut
6220 */
6221
6222 #  define IN_LOCALE                                                         \
6223         (IN_PERL_COMPILETIME ? IN_LOCALE_COMPILETIME : IN_LOCALE_RUNTIME)
6224 #  define IN_SOME_LOCALE_FORM                                               \
6225                     (IN_PERL_COMPILETIME ? IN_SOME_LOCALE_FORM_COMPILETIME  \
6226                                          : IN_SOME_LOCALE_FORM_RUNTIME)
6227
6228 #  define IN_LC_ALL_COMPILETIME   IN_LOCALE_COMPILETIME
6229 #  define IN_LC_ALL_RUNTIME       IN_LOCALE_RUNTIME
6230
6231 #  define IN_LC_PARTIAL_COMPILETIME   cBOOL(PL_hints & HINT_LOCALE_PARTIAL)
6232 #  define IN_LC_PARTIAL_RUNTIME                                             \
6233               (PL_curcop && CopHINTS_get(PL_curcop) & HINT_LOCALE_PARTIAL)
6234
6235 #  define IN_LC_COMPILETIME(category)                                       \
6236        (       IN_LC_ALL_COMPILETIME                                        \
6237         || (   IN_LC_PARTIAL_COMPILETIME                                    \
6238             && Perl__is_in_locale_category(aTHX_ TRUE, (category))))
6239 #  define IN_LC_RUNTIME(category)                                           \
6240       (IN_LC_ALL_RUNTIME || (IN_LC_PARTIAL_RUNTIME                          \
6241                  && Perl__is_in_locale_category(aTHX_ FALSE, (category))))
6242 #  define IN_LC(category)  \
6243                     (IN_LC_COMPILETIME(category) || IN_LC_RUNTIME(category))
6244
6245 #  if defined (PERL_CORE) || defined (PERL_IN_XSUB_RE)
6246
6247      /* This internal macro should be called from places that operate under
6248       * locale rules.  If there is a problem with the current locale that
6249       * hasn't been raised yet, it will output a warning this time.  Because
6250       * this will so rarely  be true, there is no point to optimize for time;
6251       * instead it makes sense to minimize space used and do all the work in
6252       * the rarely called function */
6253 #    ifdef USE_LOCALE_CTYPE
6254 #      define _CHECK_AND_WARN_PROBLEMATIC_LOCALE                              \
6255                 STMT_START {                                                  \
6256                     if (UNLIKELY(PL_warn_locale)) {                           \
6257                         Perl__warn_problematic_locale();                      \
6258                     }                                                         \
6259                 }  STMT_END
6260 #    else
6261 #      define _CHECK_AND_WARN_PROBLEMATIC_LOCALE
6262 #    endif
6263
6264
6265      /* These two internal macros are called when a warning should be raised,
6266       * and will do so if enabled.  The first takes a single code point
6267       * argument; the 2nd, is a pointer to the first byte of the UTF-8 encoded
6268       * string, and an end position which it won't try to read past */
6269 #    define _CHECK_AND_OUTPUT_WIDE_LOCALE_CP_MSG(cp)                        \
6270         STMT_START {                                                        \
6271             if (! PL_in_utf8_CTYPE_locale && ckWARN(WARN_LOCALE)) {         \
6272                 Perl_warner(aTHX_ packWARN(WARN_LOCALE),                    \
6273                                        "Wide character (U+%" UVXf ") in %s",\
6274                                        (UV) cp, OP_DESC(PL_op));            \
6275             }                                                               \
6276         }  STMT_END
6277
6278 #    define _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(s, send)                 \
6279         STMT_START { /* Check if to warn before doing the conversion work */\
6280             if (! PL_in_utf8_CTYPE_locale && ckWARN(WARN_LOCALE)) {         \
6281                 UV cp = utf8_to_uvchr_buf((U8 *) (s), (U8 *) (send), NULL); \
6282                 Perl_warner(aTHX_ packWARN(WARN_LOCALE),                    \
6283                     "Wide character (U+%" UVXf ") in %s",                   \
6284                     (cp == 0)                                               \
6285                      ? UNICODE_REPLACEMENT                                  \
6286                      : (UV) cp,                                             \
6287                     OP_DESC(PL_op));                                        \
6288             }                                                               \
6289         }  STMT_END
6290
6291 #  endif   /* PERL_CORE or PERL_IN_XSUB_RE */
6292 #else   /* No locale usage */
6293 #  define IN_LOCALE_RUNTIME                0
6294 #  define IN_SOME_LOCALE_FORM_RUNTIME      0
6295 #  define IN_LOCALE_COMPILETIME            0
6296 #  define IN_SOME_LOCALE_FORM_COMPILETIME  0
6297 #  define IN_LOCALE                        0
6298 #  define IN_SOME_LOCALE_FORM              0
6299 #  define IN_LC_ALL_COMPILETIME            0
6300 #  define IN_LC_ALL_RUNTIME                0
6301 #  define IN_LC_PARTIAL_COMPILETIME        0
6302 #  define IN_LC_PARTIAL_RUNTIME            0
6303 #  define IN_LC_COMPILETIME(category)      0
6304 #  define IN_LC_RUNTIME(category)          0
6305 #  define IN_LC(category)                  0
6306 #  define _CHECK_AND_WARN_PROBLEMATIC_LOCALE
6307 #  define _CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(s, send)
6308 #  define _CHECK_AND_OUTPUT_WIDE_LOCALE_CP_MSG(c)
6309 #endif
6310
6311
6312 /* Locale/thread synchronization macros.  These aren't needed if using
6313  * thread-safe locale operations, except if something is broken */
6314 #if    defined(USE_LOCALE)                                                  \
6315  &&    defined(USE_ITHREADS)                                                \
6316  && (! defined(USE_THREAD_SAFE_LOCALE) || defined(TS_W32_BROKEN_LOCALECONV))
6317
6318 /* We have a locale object holding the 'C' locale for Posix 2008 */
6319 #  ifndef USE_POSIX_2008_LOCALE
6320 #    define _LOCALE_TERM_POSIX_2008  NOOP
6321 #  else
6322 #    define _LOCALE_TERM_POSIX_2008                                         \
6323                     STMT_START {                                            \
6324                         if (PL_C_locale_obj) {                              \
6325                             /* Make sure we aren't using the locale         \
6326                              * space we are about to free */                \
6327                             uselocale(LC_GLOBAL_LOCALE);                    \
6328                             freelocale(PL_C_locale_obj);                    \
6329                             PL_C_locale_obj = (locale_t) NULL;              \
6330                         }                                                   \
6331                     } STMT_END
6332 #  endif
6333
6334 /* This is used as a generic lock for locale operations.  For example this is
6335  * used when calling nl_langinfo() so that another thread won't zap the
6336  * contents of its buffer before it gets saved; and it's called when changing
6337  * the locale of LC_MESSAGES.  On some systems the latter can cause the
6338  * nl_langinfo buffer to be zapped under a race condition.
6339  *
6340  * If combined with LC_NUMERIC_LOCK, calls to this and its corresponding unlock
6341  * should be contained entirely within the locked portion of LC_NUMERIC.  This
6342  * mutex should be used only in very short sections of code, while
6343  * LC_NUMERIC_LOCK may span more operations.  By always following this
6344  * convention, deadlock should be impossible.  But if necessary, the two
6345  * mutexes could be combined.
6346  *
6347  * Actually, the two macros just below with the '_V' suffixes are used in just
6348  * a few places where there is a broken localeconv(), but otherwise things are
6349  * thread safe, and hence don't need locking.  Just below LOCALE_LOCK and
6350  * LOCALE_UNLOCK are defined in terms of these for use everywhere else */
6351 #  define LOCALE_LOCK_V                                                     \
6352         STMT_START {                                                        \
6353             DEBUG_Lv(PerlIO_printf(Perl_debug_log,                          \
6354                     "%s: %d: locking locale\n", __FILE__, __LINE__));       \
6355             MUTEX_LOCK(&PL_locale_mutex);                                   \
6356         } STMT_END
6357 #  define LOCALE_UNLOCK_V                                                   \
6358         STMT_START {                                                        \
6359             DEBUG_Lv(PerlIO_printf(Perl_debug_log,                          \
6360                    "%s: %d: unlocking locale\n", __FILE__, __LINE__));      \
6361             MUTEX_UNLOCK(&PL_locale_mutex);                                 \
6362         } STMT_END
6363
6364 /* On windows, we just need the mutex for LOCALE_LOCK */
6365 #  ifdef TS_W32_BROKEN_LOCALECONV
6366 #    define LOCALE_LOCK     NOOP
6367 #    define LOCALE_UNLOCK   NOOP
6368 #    define LOCALE_INIT     MUTEX_INIT(&PL_locale_mutex);
6369 #    define LOCALE_TERM     MUTEX_DESTROY(&PL_locale_mutex)
6370 #    define LC_NUMERIC_LOCK(cond)
6371 #    define LC_NUMERIC_UNLOCK
6372 #  else
6373 #    define LOCALE_LOCK     LOCALE_LOCK_V
6374 #    define LOCALE_UNLOCK   LOCALE_UNLOCK_V
6375
6376      /* We also need to lock LC_NUMERIC for non-windows (hence Posix 2008)
6377       * systems */
6378 #    define LOCALE_INIT          STMT_START {                               \
6379                                     MUTEX_INIT(&PL_locale_mutex);           \
6380                                     MUTEX_INIT(&PL_lc_numeric_mutex);       \
6381                                 } STMT_END
6382
6383 #    define LOCALE_TERM         STMT_START {                                \
6384                                     MUTEX_DESTROY(&PL_locale_mutex);        \
6385                                     MUTEX_DESTROY(&PL_lc_numeric_mutex);    \
6386                                     _LOCALE_TERM_POSIX_2008;                \
6387                                 } STMT_END
6388
6389     /* This mutex is used to create critical sections where we want the
6390      * LC_NUMERIC locale to be locked into either the C (standard) locale, or
6391      * the underlying locale, so that other threads interrupting this one don't
6392      * change it to the wrong state before we've had a chance to complete our
6393      * operation.  It can stay locked over an entire printf operation, for
6394      * example.  And so is made distinct from the LOCALE_LOCK mutex.
6395      *
6396      * This simulates kind of a general semaphore.  The current thread will
6397      * lock the mutex if the per-thread variable is zero, and then increments
6398      * that variable.  Each corresponding UNLOCK decrements the variable until
6399      * it is 0, at which point it actually unlocks the mutex.  Since the
6400      * variable is per-thread, there is no race with other threads.
6401      *
6402      * The single argument is a condition to test for, and if true, to panic,
6403      * as this would be an attempt to complement the LC_NUMERIC state, and
6404      * we're not supposed to because it's locked.
6405      *
6406      * Clang improperly gives warnings for this, if not silenced:
6407      * https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#conditional-locks
6408      * */
6409 #    define LC_NUMERIC_LOCK(cond_to_panic_if_already_locked)                \
6410         CLANG_DIAG_IGNORE(-Wthread-safety)                                  \
6411         STMT_START {                                                        \
6412             if (PL_lc_numeric_mutex_depth <= 0) {                           \
6413                 MUTEX_LOCK(&PL_lc_numeric_mutex);                           \
6414                 PL_lc_numeric_mutex_depth = 1;                              \
6415                 DEBUG_Lv(PerlIO_printf(Perl_debug_log,                      \
6416                          "%s: %d: locking lc_numeric; depth=1\n",           \
6417                          __FILE__, __LINE__));                              \
6418             }                                                               \
6419             else {                                                          \
6420                 PL_lc_numeric_mutex_depth++;                                \
6421                 DEBUG_Lv(PerlIO_printf(Perl_debug_log,                      \
6422                         "%s: %d: avoided lc_numeric_lock; new depth=%d\n",  \
6423                         __FILE__, __LINE__, PL_lc_numeric_mutex_depth));    \
6424                 if (cond_to_panic_if_already_locked) {                      \
6425                     Perl_croak_nocontext("panic: %s: %d: Trying to change"  \
6426                                          " LC_NUMERIC incompatibly",        \
6427                                          __FILE__, __LINE__);               \
6428                 }                                                           \
6429             }                                                               \
6430         } STMT_END
6431
6432 #    define LC_NUMERIC_UNLOCK                                               \
6433         STMT_START {                                                        \
6434             if (PL_lc_numeric_mutex_depth <= 1) {                           \
6435                 MUTEX_UNLOCK(&PL_lc_numeric_mutex);                         \
6436                 PL_lc_numeric_mutex_depth = 0;                              \
6437                 DEBUG_Lv(PerlIO_printf(Perl_debug_log,                      \
6438                          "%s: %d: unlocking lc_numeric; depth=0\n",         \
6439                          __FILE__, __LINE__));                              \
6440             }                                                               \
6441             else {                                                          \
6442                 PL_lc_numeric_mutex_depth--;                                \
6443                 DEBUG_Lv(PerlIO_printf(Perl_debug_log,                      \
6444                         "%s: %d: avoided lc_numeric_unlock; new depth=%d\n",\
6445                         __FILE__, __LINE__, PL_lc_numeric_mutex_depth));    \
6446             }                                                               \
6447         } STMT_END                                                          \
6448         CLANG_DIAG_RESTORE
6449
6450 #  endif    /* End of needs locking LC_NUMERIC */
6451 #else   /* Below is no locale sync needed */
6452 #  define LOCALE_INIT
6453 #  define LOCALE_LOCK
6454 #  define LOCALE_LOCK_V
6455 #  define LOCALE_UNLOCK
6456 #  define LOCALE_UNLOCK_V
6457 #  define LC_NUMERIC_LOCK(cond)
6458 #  define LC_NUMERIC_UNLOCK
6459 #  define LOCALE_TERM
6460 #endif
6461
6462 #ifdef USE_LOCALE_NUMERIC
6463
6464 /* These macros are for toggling between the underlying locale (UNDERLYING or
6465  * LOCAL) and the C locale (STANDARD).  (Actually we don't have to use the C
6466  * locale if the underlying locale is indistinguishable from it in the numeric
6467  * operations used by Perl, namely the decimal point, and even the thousands
6468  * separator.)
6469
6470 =head1 Locale-related functions and macros
6471
6472 =for apidoc Amn|void|DECLARATION_FOR_LC_NUMERIC_MANIPULATION
6473
6474 This macro should be used as a statement.  It declares a private variable
6475 (whose name begins with an underscore) that is needed by the other macros in
6476 this section.  Failing to include this correctly should lead to a syntax error.
6477 For compatibility with C89 C compilers it should be placed in a block before
6478 any executable statements.
6479
6480 =for apidoc Am|void|STORE_LC_NUMERIC_FORCE_TO_UNDERLYING
6481
6482 This is used by XS code that that is C<LC_NUMERIC> locale-aware to force the
6483 locale for category C<LC_NUMERIC> to be what perl thinks is the current
6484 underlying locale.  (The perl interpreter could be wrong about what the
6485 underlying locale actually is if some C or XS code has called the C library
6486 function L<setlocale(3)> behind its back; calling L</sync_locale> before calling
6487 this macro will update perl's records.)
6488
6489 A call to L</DECLARATION_FOR_LC_NUMERIC_MANIPULATION> must have been made to
6490 declare at compile time a private variable used by this macro.  This macro
6491 should be called as a single statement, not an expression, but with an empty
6492 argument list, like this:
6493
6494  {
6495     DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
6496      ...
6497     STORE_LC_NUMERIC_FORCE_TO_UNDERLYING();
6498      ...
6499     RESTORE_LC_NUMERIC();
6500      ...
6501  }
6502
6503 The private variable is used to save the current locale state, so
6504 that the requisite matching call to L</RESTORE_LC_NUMERIC> can restore it.
6505
6506 On threaded perls not operating with thread-safe functionality, this macro uses
6507 a mutex to force a critical section.  Therefore the matching RESTORE should be
6508 close by, and guaranteed to be called.
6509
6510 =for apidoc Am|void|STORE_LC_NUMERIC_SET_TO_NEEDED
6511
6512 This is used to help wrap XS or C code that is C<LC_NUMERIC> locale-aware.
6513 This locale category is generally kept set to a locale where the decimal radix
6514 character is a dot, and the separator between groups of digits is empty.  This
6515 is because most XS code that reads floating point numbers is expecting them to
6516 have this syntax.
6517
6518 This macro makes sure the current C<LC_NUMERIC> state is set properly, to be
6519 aware of locale if the call to the XS or C code from the Perl program is
6520 from within the scope of a S<C<use locale>>; or to ignore locale if the call is
6521 instead from outside such scope.
6522
6523 This macro is the start of wrapping the C or XS code; the wrap ending is done
6524 by calling the L</RESTORE_LC_NUMERIC> macro after the operation.  Otherwise
6525 the state can be changed that will adversely affect other XS code.
6526
6527 A call to L</DECLARATION_FOR_LC_NUMERIC_MANIPULATION> must have been made to
6528 declare at compile time a private variable used by this macro.  This macro
6529 should be called as a single statement, not an expression, but with an empty
6530 argument list, like this:
6531
6532  {
6533     DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
6534      ...
6535     STORE_LC_NUMERIC_SET_TO_NEEDED();
6536      ...
6537     RESTORE_LC_NUMERIC();
6538      ...
6539  }
6540
6541 On threaded perls not operating with thread-safe functionality, this macro uses
6542 a mutex to force a critical section.  Therefore the matching RESTORE should be
6543 close by, and guaranteed to be called; see L</WITH_LC_NUMERIC_SET_TO_NEEDED>
6544 for a more contained way to ensure that.
6545
6546 =for apidoc Am|void|STORE_LC_NUMERIC_SET_TO_NEEDED_IN|bool in_lc_numeric
6547
6548 Same as L</STORE_LC_NUMERIC_SET_TO_NEEDED> with in_lc_numeric provided
6549 as the precalculated value of C<IN_LC(LC_NUMERIC)>. It is the caller's
6550 responsibility to ensure that the status of C<PL_compiling> and C<PL_hints>
6551 cannot have changed since the precalculation.
6552
6553 =for apidoc Am|void|RESTORE_LC_NUMERIC
6554
6555 This is used in conjunction with one of the macros
6556 L</STORE_LC_NUMERIC_SET_TO_NEEDED>
6557 and L</STORE_LC_NUMERIC_FORCE_TO_UNDERLYING> to properly restore the
6558 C<LC_NUMERIC> state.
6559
6560 A call to L</DECLARATION_FOR_LC_NUMERIC_MANIPULATION> must have been made to
6561 declare at compile time a private variable used by this macro and the two
6562 C<STORE> ones.  This macro should be called as a single statement, not an
6563 expression, but with an empty argument list, like this:
6564
6565  {
6566     DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
6567      ...
6568     RESTORE_LC_NUMERIC();
6569      ...
6570  }
6571
6572 =for apidoc Am|void|WITH_LC_NUMERIC_SET_TO_NEEDED|block
6573
6574 This macro invokes the supplied statement or block within the context
6575 of a L</STORE_LC_NUMERIC_SET_TO_NEEDED> .. L</RESTORE_LC_NUMERIC> pair
6576 if required, so eg:
6577
6578   WITH_LC_NUMERIC_SET_TO_NEEDED(
6579     SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis)
6580   );
6581
6582 is equivalent to:
6583
6584   {
6585 #ifdef USE_LOCALE_NUMERIC
6586     DECLARATION_FOR_LC_NUMERIC_MANIPULATION;
6587     STORE_LC_NUMERIC_SET_TO_NEEDED();
6588 #endif
6589     SNPRINTF_G(fv, ebuf, sizeof(ebuf), precis);
6590 #ifdef USE_LOCALE_NUMERIC
6591     RESTORE_LC_NUMERIC();
6592 #endif
6593   }
6594
6595 =for apidoc Am|void|WITH_LC_NUMERIC_SET_TO_NEEDED_IN|bool in_lc_numeric|block
6596
6597 Same as L</WITH_LC_NUMERIC_SET_TO_NEEDED> with in_lc_numeric provided
6598 as the precalculated value of C<IN_LC(LC_NUMERIC)>. It is the caller's
6599 responsibility to ensure that the status of C<PL_compiling> and C<PL_hints>
6600 cannot have changed since the precalculation.
6601
6602 =cut
6603
6604 */
6605
6606 /* If the underlying numeric locale has a non-dot decimal point or has a
6607  * non-empty floating point thousands separator, the current locale is instead
6608  * generally kept in the C locale instead of that underlying locale.  The
6609  * current status is known by looking at two words.  One is non-zero if the
6610  * current numeric locale is the standard C/POSIX one or is indistinguishable
6611  * from C.  The other is non-zero if the current locale is the underlying
6612  * locale.  Both can be non-zero if, as often happens, the underlying locale is
6613  * C or indistinguishable from it.
6614  *
6615  * khw believes the reason for the variables instead of the bits in a single
6616  * word is to avoid having to have masking instructions. */
6617
6618 #  define _NOT_IN_NUMERIC_STANDARD (! PL_numeric_standard)
6619
6620 /* We can lock the category to stay in the C locale, making requests to the
6621  * contrary be noops, in the dynamic scope by setting PL_numeric_standard to 2.
6622  * */
6623 #  define _NOT_IN_NUMERIC_UNDERLYING                                        \
6624                     (! PL_numeric_underlying && PL_numeric_standard < 2)
6625
6626 #  define DECLARATION_FOR_LC_NUMERIC_MANIPULATION                           \
6627     void (*_restore_LC_NUMERIC_function)(pTHX) = NULL
6628
6629 #  define STORE_LC_NUMERIC_SET_TO_NEEDED_IN(in)                             \
6630         STMT_START {                                                        \
6631             bool _in_lc_numeric = (in);                                     \
6632             LC_NUMERIC_LOCK(                                                \
6633                     (   (  _in_lc_numeric && _NOT_IN_NUMERIC_UNDERLYING)    \
6634                      || (! _in_lc_numeric && _NOT_IN_NUMERIC_STANDARD)));   \
6635             if (_in_lc_numeric) {                                           \
6636                 if (_NOT_IN_NUMERIC_UNDERLYING) {                           \
6637                     Perl_set_numeric_underlying(aTHX);                      \
6638                     _restore_LC_NUMERIC_function                            \
6639                                             = &Perl_set_numeric_standard;   \
6640                 }                                                           \
6641             }                                                               \
6642             else {                                                          \
6643                 if (_NOT_IN_NUMERIC_STANDARD) {                             \
6644                     Perl_set_numeric_standard(aTHX);                        \
6645                     _restore_LC_NUMERIC_function                            \
6646                                             = &Perl_set_numeric_underlying; \
6647                 }                                                           \
6648             }                                                               \
6649         } STMT_END
6650
6651 #  define STORE_LC_NUMERIC_SET_TO_NEEDED() \
6652         STORE_LC_NUMERIC_SET_TO_NEEDED_IN(IN_LC(LC_NUMERIC))
6653
6654 #  define RESTORE_LC_NUMERIC()                                              \
6655         STMT_START {                                                        \
6656             if (_restore_LC_NUMERIC_function) {                             \
6657                 _restore_LC_NUMERIC_function(aTHX);                         \
6658             }                                                               \
6659             LC_NUMERIC_UNLOCK;                                              \
6660         } STMT_END
6661
6662 /* The next two macros set unconditionally.  These should be rarely used, and
6663  * only after being sure that this is what is needed */
6664 #  define SET_NUMERIC_STANDARD()                                            \
6665         STMT_START {                                                        \
6666             DEBUG_Lv(PerlIO_printf(Perl_debug_log,                          \
6667                                "%s: %d: lc_numeric standard=%d\n",          \
6668                                 __FILE__, __LINE__, PL_numeric_standard));  \
6669             Perl_set_numeric_standard(aTHX);                                \
6670             DEBUG_Lv(PerlIO_printf(Perl_debug_log,                          \
6671                                  "%s: %d: lc_numeric standard=%d\n",        \
6672                                  __FILE__, __LINE__, PL_numeric_standard)); \
6673         } STMT_END
6674
6675 #  define SET_NUMERIC_UNDERLYING()                                          \
6676         STMT_START {                                                        \
6677             if (_NOT_IN_NUMERIC_UNDERLYING) {                               \
6678                 Perl_set_numeric_underlying(aTHX);                          \
6679             }                                                               \
6680         } STMT_END
6681
6682 /* The rest of these LC_NUMERIC macros toggle to one or the other state, with
6683  * the RESTORE_foo ones called to switch back, but only if need be */
6684 #  define STORE_LC_NUMERIC_SET_STANDARD()                                   \
6685         STMT_START {                                                        \
6686             LC_NUMERIC_LOCK(_NOT_IN_NUMERIC_STANDARD);                      \
6687             if (_NOT_IN_NUMERIC_STANDARD) {                                 \
6688                 _restore_LC_NUMERIC_function = &Perl_set_numeric_underlying;\
6689                 Perl_set_numeric_standard(aTHX);                            \
6690             }                                                               \
6691         } STMT_END
6692
6693 /* Rarely, we want to change to the underlying locale even outside of 'use
6694  * locale'.  This is principally in the POSIX:: functions */
6695 #  define STORE_LC_NUMERIC_FORCE_TO_UNDERLYING()                            \
6696         STMT_START {                                                        \
6697             LC_NUMERIC_LOCK(_NOT_IN_NUMERIC_UNDERLYING);                    \
6698             if (_NOT_IN_NUMERIC_UNDERLYING) {                               \
6699                 Perl_set_numeric_underlying(aTHX);                          \
6700                 _restore_LC_NUMERIC_function = &Perl_set_numeric_standard;  \
6701             }                                                               \
6702         } STMT_END
6703
6704 /* Lock/unlock to the C locale until unlock is called.  This needs to be
6705  * recursively callable.  [perl #128207] */
6706 #  define LOCK_LC_NUMERIC_STANDARD()                                        \
6707         STMT_START {                                                        \
6708             DEBUG_Lv(PerlIO_printf(Perl_debug_log,                          \
6709                       "%s: %d: lock lc_numeric_standard: new depth=%d\n",   \
6710                       __FILE__, __LINE__, PL_numeric_standard + 1));        \
6711             __ASSERT_(PL_numeric_standard)                                  \
6712             PL_numeric_standard++;                                          \
6713         } STMT_END
6714
6715 #  define UNLOCK_LC_NUMERIC_STANDARD()                                      \
6716         STMT_START {                                                        \
6717             if (PL_numeric_standard > 1) {                                  \
6718                 PL_numeric_standard--;                                      \
6719             }                                                               \
6720             else {                                                          \
6721                 assert(0);                                                  \
6722             }                                                               \
6723             DEBUG_Lv(PerlIO_printf(Perl_debug_log,                          \
6724             "%s: %d: lc_numeric_standard decrement lock, new depth=%d\n",   \
6725             __FILE__, __LINE__, PL_numeric_standard));                      \
6726         } STMT_END
6727
6728 #  define WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric, block)            \
6729         STMT_START {                                                        \
6730             DECLARATION_FOR_LC_NUMERIC_MANIPULATION;                        \
6731             STORE_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric);               \
6732             block;                                                          \
6733             RESTORE_LC_NUMERIC();                                           \
6734         } STMT_END;
6735
6736 #  define WITH_LC_NUMERIC_SET_TO_NEEDED(block) \
6737         WITH_LC_NUMERIC_SET_TO_NEEDED_IN(IN_LC(LC_NUMERIC), block)
6738
6739 #else /* !USE_LOCALE_NUMERIC */
6740
6741 #  define SET_NUMERIC_STANDARD()
6742 #  define SET_NUMERIC_UNDERLYING()
6743 #  define IS_NUMERIC_RADIX(a, b)                (0)
6744 #  define DECLARATION_FOR_LC_NUMERIC_MANIPULATION
6745 #  define STORE_LC_NUMERIC_SET_STANDARD()
6746 #  define STORE_LC_NUMERIC_FORCE_TO_UNDERLYING()
6747 #  define STORE_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric)
6748 #  define STORE_LC_NUMERIC_SET_TO_NEEDED()
6749 #  define RESTORE_LC_NUMERIC()
6750 #  define LOCK_LC_NUMERIC_STANDARD()
6751 #  define UNLOCK_LC_NUMERIC_STANDARD()
6752 #  define WITH_LC_NUMERIC_SET_TO_NEEDED_IN(in_lc_numeric, block) \
6753     STMT_START { block; } STMT_END
6754 #  define WITH_LC_NUMERIC_SET_TO_NEEDED(block) \
6755     STMT_START { block; } STMT_END
6756
6757 #endif /* !USE_LOCALE_NUMERIC */
6758
6759 #define Atof                            my_atof
6760
6761 /*
6762
6763 =head1 Numeric functions
6764
6765 =for apidoc AmTR|NV|Strtod|NN const char * const s|NULLOK char ** e
6766
6767 This is a synonym for L</my_strtod>.
6768
6769 =for apidoc AmTR|NV|Strtol|NN const char * const s|NULLOK char ** e|int base
6770
6771 Platform and configuration independent C<strtol>.  This expands to the
6772 appropriate C<strotol>-like function based on the platform and F<Configure>
6773 options>.  For example it could expand to C<strtoll> or C<strtoq> instead of
6774 C<strtol>.
6775
6776 =for apidoc AmTR|NV|Strtoul|NN const char * const s|NULLOK char ** e|int base
6777
6778 Platform and configuration independent C<strtoul>.  This expands to the
6779 appropriate C<strotoul>-like function based on the platform and F<Configure>
6780 options>.  For example it could expand to C<strtoull> or C<strtouq> instead of
6781 C<strtoul>.
6782
6783 =cut
6784
6785 */
6786
6787 #define Strtod                          my_strtod
6788
6789 #if    defined(HAS_STRTOD)                                          \
6790    ||  defined(USE_QUADMATH)                                        \
6791    || (defined(HAS_STRTOLD) && defined(HAS_LONG_DOUBLE)             \
6792                             && defined(USE_LONG_DOUBLE))
6793 #  define Perl_strtod   Strtod
6794 #endif
6795
6796 #if !defined(Strtol) && defined(USE_64_BIT_INT) && defined(IV_IS_QUAD) && \
6797         (QUADKIND == QUAD_IS_LONG_LONG || QUADKIND == QUAD_IS___INT64)
6798 #    ifdef __hpux
6799 #        define strtoll __strtoll       /* secret handshake */
6800 #    endif
6801 #    if defined(WIN64) && defined(_MSC_VER)
6802 #        define strtoll _strtoi64       /* secret handshake */
6803 #    endif
6804 #   if !defined(Strtol) && defined(HAS_STRTOLL)
6805 #       define Strtol   strtoll
6806 #   endif
6807 #    if !defined(Strtol) && defined(HAS_STRTOQ)
6808 #       define Strtol   strtoq
6809 #    endif
6810 /* is there atoq() anywhere? */
6811 #endif
6812 #if !defined(Strtol) && defined(HAS_STRTOL)
6813 #   define Strtol       strtol
6814 #endif
6815 #ifndef Atol
6816 /* It would be more fashionable to use Strtol() to define atol()
6817  * (as is done for Atoul(), see below) but for backward compatibility
6818  * we just assume atol(). */
6819 #   if defined(USE_64_BIT_INT) && defined(IV_IS_QUAD) && defined(HAS_ATOLL) && \
6820         (QUADKIND == QUAD_IS_LONG_LONG || QUADKIND == QUAD_IS___INT64)
6821 #    ifdef WIN64
6822 #       define atoll    _atoi64         /* secret handshake */
6823 #    endif
6824 #       define Atol     atoll
6825 #   else
6826 #       define Atol     atol
6827 #   endif
6828 #endif
6829
6830 #if !defined(Strtoul) && defined(USE_64_BIT_INT) && defined(UV_IS_QUAD) && \
6831         (QUADKIND == QUAD_IS_LONG_LONG || QUADKIND == QUAD_IS___INT64)
6832 #    ifdef __hpux
6833 #        define strtoull __strtoull     /* secret handshake */
6834 #    endif
6835 #    if defined(WIN64) && defined(_MSC_VER)
6836 #        define strtoull _strtoui64     /* secret handshake */
6837 #    endif
6838 #    if !defined(Strtoul) && defined(HAS_STRTOULL)
6839 #       define Strtoul  strtoull
6840 #    endif
6841 #    if !defined(Strtoul) && defined(HAS_STRTOUQ)
6842 #       define Strtoul  strtouq
6843 #    endif
6844 /* is there atouq() anywhere? */
6845 #endif
6846 #if !defined(Strtoul) && defined(HAS_STRTOUL)
6847 #   define Strtoul      strtoul
6848 #endif
6849 #if !defined(Strtoul) && defined(HAS_STRTOL) /* Last resort. */
6850 #   define Strtoul(s, e, b)     strchr((s), '-') ? ULONG_MAX : (unsigned long)strtol((s), (e), (b))
6851 #endif
6852 #ifndef Atoul
6853 #   define Atoul(s)     Strtoul(s, NULL, 10)
6854 #endif
6855
6856 #ifndef PERL_SCRIPT_MODE
6857 #define PERL_SCRIPT_MODE "r"
6858 #endif
6859
6860 /* not used. Kept as a NOOP for backcompat */
6861 #define PERL_STACK_OVERFLOW_CHECK()  NOOP
6862
6863 /*
6864  * Some nonpreemptive operating systems find it convenient to
6865  * check for asynchronous conditions after each op execution.
6866  * Keep this check simple, or it may slow down execution
6867  * massively.
6868  */
6869
6870 #ifndef PERL_MICRO
6871 #       ifndef PERL_ASYNC_CHECK
6872 #               define PERL_ASYNC_CHECK() if (UNLIKELY(PL_sig_pending)) PL_signalhook(aTHX)
6873 #       endif
6874 #endif
6875
6876 #ifndef PERL_ASYNC_CHECK
6877 #   define PERL_ASYNC_CHECK()  NOOP
6878 #endif
6879
6880 /*
6881  * On some operating systems, a memory allocation may succeed,
6882  * but put the process too close to the system's comfort limit.
6883  * In this case, PERL_ALLOC_CHECK frees the pointer and sets
6884  * it to NULL.
6885  */
6886 #ifndef PERL_ALLOC_CHECK
6887 #define PERL_ALLOC_CHECK(p)  NOOP
6888 #endif
6889
6890 #ifdef HAS_SEM
6891 #   include <sys/ipc.h>
6892 #   include <sys/sem.h>
6893 #   ifndef HAS_UNION_SEMUN      /* Provide the union semun. */
6894     union semun {
6895         int             val;
6896         struct semid_ds *buf;
6897         unsigned short  *array;
6898     };
6899 #   endif
6900 #   ifdef USE_SEMCTL_SEMUN
6901 #       ifdef IRIX32_SEMUN_BROKEN_BY_GCC
6902             union gccbug_semun {
6903                 int             val;
6904                 struct semid_ds *buf;
6905                 unsigned short  *array;
6906                 char            __dummy[5];
6907             };
6908 #           define semun gccbug_semun
6909 #       endif
6910 #       define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun)
6911 #   elif defined(USE_SEMCTL_SEMID_DS)
6912 #           ifdef EXTRA_F_IN_SEMUN_BUF
6913 #               define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun.buff)
6914 #           else
6915 #               define Semctl(id, num, cmd, semun) semctl(id, num, cmd, semun.buf)
6916 #           endif
6917 #   endif
6918 #endif
6919
6920 /*
6921  * Boilerplate macros for initializing and accessing interpreter-local
6922  * data from C.  All statics in extensions should be reworked to use
6923  * this, if you want to make the extension thread-safe.  See
6924  * ext/XS/APItest/APItest.xs for an example of the use of these macros,
6925  * and perlxs.pod for more.
6926  *
6927  * Code that uses these macros is responsible for the following:
6928  * 1. #define MY_CXT_KEY to a unique string, e.g.
6929  *    "DynaLoader::_guts" XS_VERSION
6930  *    XXX in the current implementation, this string is ignored.
6931  * 2. Declare a typedef named my_cxt_t that is a structure that contains
6932  *    all the data that needs to be interpreter-local.
6933  * 3. Use the START_MY_CXT macro after the declaration of my_cxt_t.
6934  * 4. Use the MY_CXT_INIT macro such that it is called exactly once
6935  *    (typically put in the BOOT: section).
6936  * 5. Use the members of the my_cxt_t structure everywhere as
6937  *    MY_CXT.member.
6938  * 6. Use the dMY_CXT macro (a declaration) in all the functions that
6939  *    access MY_CXT.
6940  */
6941
6942 #if defined(PERL_IMPLICIT_CONTEXT)
6943
6944 /* START_MY_CXT must appear in all extensions that define a my_cxt_t structure,
6945  * right after the definition (i.e. at file scope).  The non-threads
6946  * case below uses it to declare the data as static. */
6947 #  ifdef PERL_GLOBAL_STRUCT_PRIVATE
6948 #    define START_MY_CXT
6949 #    define MY_CXT_INDEX Perl_my_cxt_index(aTHX_ MY_CXT_KEY)
6950 #    define MY_CXT_INIT_ARG MY_CXT_KEY
6951 #  else
6952 #    define START_MY_CXT static int my_cxt_index = -1;
6953 #    define MY_CXT_INDEX my_cxt_index
6954 #    define MY_CXT_INIT_ARG &my_cxt_index
6955 #  endif /* #ifdef PERL_GLOBAL_STRUCT_PRIVATE */
6956
6957 /* Creates and zeroes the per-interpreter data.
6958  * (We allocate my_cxtp in a Perl SV so that it will be released when
6959  * the interpreter goes away.) */
6960 #  define MY_CXT_INIT \
6961         my_cxt_t *my_cxtp = \
6962             (my_cxt_t*)Perl_my_cxt_init(aTHX_ MY_CXT_INIT_ARG, sizeof(my_cxt_t)); \
6963         PERL_UNUSED_VAR(my_cxtp)
6964 #  define MY_CXT_INIT_INTERP(my_perl) \
6965         my_cxt_t *my_cxtp = \
6966             (my_cxt_t*)Perl_my_cxt_init(my_perl, MY_CXT_INIT_ARG, sizeof(my_cxt_t)); \
6967         PERL_UNUSED_VAR(my_cxtp)
6968
6969 /* This declaration should be used within all functions that use the
6970  * interpreter-local data. */
6971 #  define dMY_CXT       \
6972         my_cxt_t *my_cxtp = (my_cxt_t *)PL_my_cxt_list[MY_CXT_INDEX]
6973 #  define dMY_CXT_INTERP(my_perl)       \
6974         my_cxt_t *my_cxtp = (my_cxt_t *)(my_perl)->Imy_cxt_list[MY_CXT_INDEX]
6975
6976 /* Clones the per-interpreter data. */
6977 #  define MY_CXT_CLONE \
6978         my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1));\
6979         void * old_my_cxtp = PL_my_cxt_list[MY_CXT_INDEX];              \
6980         PL_my_cxt_list[MY_CXT_INDEX] = my_cxtp;                         \
6981         Copy(old_my_cxtp, my_cxtp, 1, my_cxt_t);
6982
6983
6984
6985 /* This macro must be used to access members of the my_cxt_t structure.
6986  * e.g. MY_CXT.some_data */
6987 #  define MY_CXT                (*my_cxtp)
6988
6989 /* Judicious use of these macros can reduce the number of times dMY_CXT
6990  * is used.  Use is similar to pTHX, aTHX etc. */
6991 #  define pMY_CXT       my_cxt_t *my_cxtp
6992 #  define pMY_CXT_      pMY_CXT,
6993 #  define _pMY_CXT      ,pMY_CXT
6994 #  define aMY_CXT       my_cxtp
6995 #  define aMY_CXT_      aMY_CXT,
6996 #  define _aMY_CXT      ,aMY_CXT
6997
6998 #else /* PERL_IMPLICIT_CONTEXT */
6999
7000 #  define START_MY_CXT          static my_cxt_t my_cxt;
7001 #  define dMY_CXT_SV            dNOOP
7002 #  define dMY_CXT               dNOOP
7003 #  define dMY_CXT_INTERP(my_perl) dNOOP
7004 #  define MY_CXT_INIT           NOOP
7005 #  define MY_CXT_CLONE          NOOP
7006 #  define MY_CXT                my_cxt
7007
7008 #  define pMY_CXT               void
7009 #  define pMY_CXT_
7010 #  define _pMY_CXT
7011 #  define aMY_CXT
7012 #  define aMY_CXT_
7013 #  define _aMY_CXT
7014
7015 #endif /* !defined(PERL_IMPLICIT_CONTEXT) */
7016
7017 #ifdef I_FCNTL
7018 #  include <fcntl.h>
7019 #endif
7020
7021 #ifdef __Lynx__
7022 #  include <fcntl.h>
7023 #endif
7024
7025 #ifdef __amigaos4__
7026 #  undef FD_CLOEXEC /* a lie in AmigaOS */
7027 #endif
7028
7029 #ifdef I_SYS_FILE
7030 #  include <sys/file.h>
7031 #endif
7032
7033 #if defined(HAS_FLOCK) && !defined(HAS_FLOCK_PROTO)
7034 int flock(int fd, int op);
7035 #endif
7036
7037 #ifndef O_RDONLY
7038 /* Assume UNIX defaults */
7039 #    define O_RDONLY    0000
7040 #    define O_WRONLY    0001
7041 #    define O_RDWR      0002
7042 #    define O_CREAT     0100
7043 #endif
7044
7045 #ifndef O_BINARY
7046 #  define O_BINARY 0
7047 #endif
7048
7049 #ifndef O_TEXT
7050 #  define O_TEXT 0
7051 #endif
7052
7053 #if O_TEXT != O_BINARY
7054     /* If you have different O_TEXT and O_BINARY and you are a CRLF shop,
7055      * that is, you are somehow DOSish. */
7056 #   if defined(__HAIKU__) || defined(__VOS__) || defined(__CYGWIN__)
7057     /* Haiku has O_TEXT != O_BINARY but O_TEXT and O_BINARY have no effect;
7058      * Haiku is always UNIXoid (LF), not DOSish (CRLF). */
7059     /* VOS has O_TEXT != O_BINARY, and they have effect,
7060      * but VOS always uses LF, never CRLF. */
7061     /* If you have O_TEXT different from your O_BINARY but you still are
7062      * not a CRLF shop. */
7063 #       undef PERLIO_USING_CRLF
7064 #   else
7065     /* If you really are DOSish. */
7066 #      define PERLIO_USING_CRLF 1
7067 #   endif
7068 #endif
7069
7070 #ifdef I_LIBUTIL
7071 #   include <libutil.h>         /* setproctitle() in some FreeBSDs */
7072 #endif
7073
7074 #ifndef EXEC_ARGV_CAST
7075 #define EXEC_ARGV_CAST(x) (char **)x
7076 #endif
7077
7078 #define IS_NUMBER_IN_UV               0x01 /* number within UV range (maybe not
7079                                               int).  value returned in pointed-
7080                                               to UV */
7081 #define IS_NUMBER_GREATER_THAN_UV_MAX 0x02 /* pointed to UV undefined */
7082 #define IS_NUMBER_NOT_INT             0x04 /* saw . or E notation or infnan */
7083 #define IS_NUMBER_NEG                 0x08 /* leading minus sign */
7084 #define IS_NUMBER_INFINITY            0x10 /* this is big */
7085 #define IS_NUMBER_NAN                 0x20 /* this is not */
7086 #define IS_NUMBER_TRAILING            0x40 /* number has trailing trash */
7087
7088 /*
7089 =head1 Numeric functions
7090
7091 =for apidoc AmdR|bool|GROK_NUMERIC_RADIX|NN const char **sp|NN const char *send
7092
7093 A synonym for L</grok_numeric_radix>
7094
7095 =cut
7096 */
7097 #define GROK_NUMERIC_RADIX(sp, send) grok_numeric_radix(sp, send)
7098
7099 /* Input flags: */
7100 #define PERL_SCAN_ALLOW_UNDERSCORES   0x01 /* grok_??? accept _ in numbers */
7101 #define PERL_SCAN_DISALLOW_PREFIX     0x02 /* grok_??? reject 0x in hex etc */
7102 #define PERL_SCAN_SILENT_ILLDIGIT     0x04 /* grok_??? not warn about illegal digits */
7103 #define PERL_SCAN_SILENT_NON_PORTABLE 0x08 /* grok_??? not warn about very large
7104                                               numbers which are <= UV_MAX */
7105 #define PERL_SCAN_TRAILING            0x10 /* grok_number_flags() allow trailing
7106                                               and set IS_NUMBER_TRAILING */
7107
7108 /* Output flags: */
7109 #define PERL_SCAN_GREATER_THAN_UV_MAX 0x02 /* should this merge with above? */
7110
7111 /* to let user control profiling */
7112 #ifdef PERL_GPROF_CONTROL
7113 extern void moncontrol(int);
7114 #define PERL_GPROF_MONCONTROL(x) moncontrol(x)
7115 #else
7116 #define PERL_GPROF_MONCONTROL(x)
7117 #endif
7118
7119 /* ISO 6429 NEL - C1 control NExt Line */
7120 /* See http://www.unicode.org/unicode/reports/tr13/ */
7121 #define NEXT_LINE_CHAR  NEXT_LINE_NATIVE
7122
7123 #ifndef PIPESOCK_MODE
7124 #  define PIPESOCK_MODE
7125 #endif
7126
7127 #ifndef SOCKET_OPEN_MODE
7128 #  define SOCKET_OPEN_MODE      PIPESOCK_MODE
7129 #endif
7130
7131 #ifndef PIPE_OPEN_MODE
7132 #  define PIPE_OPEN_MODE        PIPESOCK_MODE
7133 #endif
7134
7135 #define PERL_MAGIC_UTF8_CACHESIZE       2
7136
7137 #define PERL_UNICODE_STDIN_FLAG                 0x0001
7138 #define PERL_UNICODE_STDOUT_FLAG                0x0002
7139 #define PERL_UNICODE_STDERR_FLAG                0x0004
7140 #define PERL_UNICODE_IN_FLAG                    0x0008
7141 #define PERL_UNICODE_OUT_FLAG                   0x0010
7142 #define PERL_UNICODE_ARGV_FLAG                  0x0020
7143 #define PERL_UNICODE_LOCALE_FLAG                0x0040
7144 #define PERL_UNICODE_WIDESYSCALLS_FLAG          0x0080 /* for Sarathy */
7145 #define PERL_UNICODE_UTF8CACHEASSERT_FLAG       0x0100
7146
7147 #define PERL_UNICODE_STD_FLAG           \
7148         (PERL_UNICODE_STDIN_FLAG        | \
7149          PERL_UNICODE_STDOUT_FLAG       | \
7150          PERL_UNICODE_STDERR_FLAG)
7151
7152 #define PERL_UNICODE_INOUT_FLAG         \
7153         (PERL_UNICODE_IN_FLAG   | \
7154          PERL_UNICODE_OUT_FLAG)
7155
7156 #define PERL_UNICODE_DEFAULT_FLAGS      \
7157         (PERL_UNICODE_STD_FLAG          | \
7158          PERL_UNICODE_INOUT_FLAG        | \
7159          PERL_UNICODE_LOCALE_FLAG)
7160
7161 #define PERL_UNICODE_ALL_FLAGS                  0x01ff
7162
7163 #define PERL_UNICODE_STDIN                      'I'
7164 #define PERL_UNICODE_STDOUT                     'O'
7165 #define PERL_UNICODE_STDERR                     'E'
7166 #define PERL_UNICODE_STD                        'S'
7167 #define PERL_UNICODE_IN                         'i'
7168 #define PERL_UNICODE_OUT                        'o'
7169 #define PERL_UNICODE_INOUT                      'D'
7170 #define PERL_UNICODE_ARGV                       'A'
7171 #define PERL_UNICODE_LOCALE                     'L'
7172 #define PERL_UNICODE_WIDESYSCALLS               'W'
7173 #define PERL_UNICODE_UTF8CACHEASSERT            'a'
7174
7175 #define PERL_SIGNALS_UNSAFE_FLAG        0x0001
7176
7177 /*
7178 =head1 Numeric functions
7179
7180 =for apidoc Am|int|PERL_ABS|int
7181
7182 Typeless C<abs> or C<fabs>, I<etc>.  (The usage below indicates it is for
7183 integers, but it works for any type.)  Use instead of these, since the C
7184 library ones force their argument to be what it is expecting, potentially
7185 leading to disaster.  But also beware that this evaluates its argument twice,
7186 so no C<x++>.
7187
7188 =cut
7189 */
7190
7191 #define PERL_ABS(x) ((x) < 0 ? -(x) : (x))
7192
7193 #if defined(__DECC) && defined(__osf__)
7194 #pragma message disable (mainparm) /* Perl uses the envp in main(). */
7195 #endif
7196
7197 #define do_open(g, n, l, a, rm, rp, sf) \
7198         do_openn(g, n, l, a, rm, rp, sf, (SV **) NULL, 0)
7199 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
7200 #  define do_exec(cmd)                  do_exec3(cmd,0,0)
7201 #endif
7202 #ifdef OS2
7203 #  define do_aexec                      Perl_do_aexec
7204 #else
7205 #  define do_aexec(really, mark,sp)     do_aexec5(really, mark, sp, 0, 0)
7206 #endif
7207
7208 /* check embedded \0 characters in pathnames passed to syscalls,
7209    but allow one ending \0 */
7210 #define IS_SAFE_SYSCALL(p, len, what, op_name) (Perl_is_safe_syscall(aTHX_ (p), (len), (what), (op_name)))
7211
7212 #define IS_SAFE_PATHNAME(p, len, op_name) IS_SAFE_SYSCALL((p), (len), "pathname", (op_name))
7213
7214 #if defined(OEMVS) || defined(__amigaos4__)
7215 #define NO_ENV_ARRAY_IN_MAIN
7216 #endif
7217
7218 /* These are used by Perl_pv_escape() and Perl_pv_pretty()
7219  * are here so that they are available throughout the core
7220  * NOTE that even though some are for _escape and some for _pretty
7221  * there must not be any clashes as the flags from _pretty are
7222  * passed straight through to _escape.
7223  */
7224
7225 #define PERL_PV_ESCAPE_QUOTE        0x000001
7226 #define PERL_PV_PRETTY_QUOTE        PERL_PV_ESCAPE_QUOTE
7227
7228 #define PERL_PV_PRETTY_ELLIPSES     0x000002
7229 #define PERL_PV_PRETTY_LTGT         0x000004
7230 #define PERL_PV_PRETTY_EXACTSIZE    0x000008
7231
7232 #define PERL_PV_ESCAPE_UNI          0x000100
7233 #define PERL_PV_ESCAPE_UNI_DETECT   0x000200
7234 #define PERL_PV_ESCAPE_NONASCII     0x000400
7235 #define PERL_PV_ESCAPE_FIRSTCHAR    0x000800
7236
7237 #define PERL_PV_ESCAPE_ALL            0x001000
7238 #define PERL_PV_ESCAPE_NOBACKSLASH  0x002000
7239 #define PERL_PV_ESCAPE_NOCLEAR      0x004000
7240 #define PERL_PV_PRETTY_NOCLEAR      PERL_PV_ESCAPE_NOCLEAR
7241 #define PERL_PV_ESCAPE_RE           0x008000
7242
7243 #define PERL_PV_ESCAPE_DWIM         0x010000
7244
7245
7246 /* used by pv_display in dump.c*/
7247 #define PERL_PV_PRETTY_DUMP  PERL_PV_PRETTY_ELLIPSES|PERL_PV_PRETTY_QUOTE
7248 #define PERL_PV_PRETTY_REGPROP PERL_PV_PRETTY_ELLIPSES|PERL_PV_PRETTY_LTGT|PERL_PV_ESCAPE_RE|PERL_PV_ESCAPE_NONASCII
7249
7250 #if DOUBLEKIND == DOUBLE_IS_VAX_F_FLOAT || \
7251     DOUBLEKIND == DOUBLE_IS_VAX_D_FLOAT || \
7252     DOUBLEKIND == DOUBLE_IS_VAX_G_FLOAT
7253 #  define DOUBLE_IS_VAX_FLOAT
7254 #else
7255 #  define DOUBLE_IS_IEEE_FORMAT
7256 #endif
7257
7258 #if DOUBLEKIND == DOUBLE_IS_IEEE_754_32_BIT_LITTLE_ENDIAN || \
7259     DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_LITTLE_ENDIAN || \
7260     DOUBLEKIND == DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
7261 #  define DOUBLE_LITTLE_ENDIAN
7262 #endif
7263
7264 #if DOUBLEKIND == DOUBLE_IS_IEEE_754_32_BIT_BIG_ENDIAN || \
7265     DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_BIG_ENDIAN || \
7266     DOUBLEKIND == DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
7267 #  define DOUBLE_BIG_ENDIAN
7268 #endif
7269
7270 #if DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE || \
7271     DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
7272 #  define DOUBLE_MIX_ENDIAN
7273 #endif
7274
7275 /* The VAX fp formats are neither consistently little-endian nor
7276  * big-endian, and neither are they really IEEE-mixed endian like
7277  * the mixed-endian ARM IEEE formats (with swapped bytes).
7278  * Ultimately, the VAX format came from the PDP-11.
7279  *
7280  * The ordering of the parts in VAX floats is quite vexing.
7281  * In the below the fraction_n are the mantissa bits.
7282  *
7283  * The fraction_1 is the most significant (numbering as by DEC/Digital),
7284  * while the rightmost bit in each fraction is the least significant:
7285  * in other words, big-endian bit order within the fractions.
7286  *
7287  * The fraction segments themselves would be big-endianly, except that
7288  * within 32 bit segments the less significant half comes first, the more
7289  * significant after, except that in the format H (used for long doubles)
7290  * the first fraction segment is alone, because the exponent is wider.
7291  * This means for example that both the most and the least significant
7292  * bits can be in the middle of the floats, not at either end.
7293  *
7294  * References:
7295  * http://nssdc.gsfc.nasa.gov/nssdc/formats/VAXFloatingPoint.htm
7296  * http://www.quadibloc.com/comp/cp0201.htm
7297  * http://h71000.www7.hp.com/doc/82final/6443/6443pro_028.html
7298  * (somebody at HP should be fired for the URLs)
7299  *
7300  * F   fraction_2:16 sign:1 exp:8  fraction_1:7
7301  *     (exponent bias 128, hidden first one-bit)
7302  *
7303  * D   fraction_2:16 sign:1 exp:8  fraction_1:7
7304  *     fraction_4:16               fraction_3:16
7305  *     (exponent bias 128, hidden first one-bit)
7306  *
7307  * G   fraction_2:16 sign:1 exp:11 fraction_1:4
7308  *     fraction_4:16               fraction_3:16
7309  *     (exponent bias 1024, hidden first one-bit)
7310  *
7311  * H   fraction_1:16 sign:1 exp:15
7312  *     fraction_3:16               fraction_2:16
7313  *     fraction_5:16               fraction_4:16
7314  *     fraction_7:16               fraction_6:16
7315  *     (exponent bias 16384, hidden first one-bit)
7316  *     (available only on VAX, and only on Fortran?)
7317  *
7318  * The formats S, T and X are available on the Alpha (and Itanium,
7319  * also known as I64/IA64) and are equivalent with the IEEE-754 formats
7320  * binary32, binary64, and binary128 (commonly: float, double, long double).
7321  *
7322  * S   sign:1 exp:8 mantissa:23
7323  *     (exponent bias 127, hidden first one-bit)
7324  *
7325  * T   sign:1 exp:11 mantissa:52
7326  *     (exponent bias 1022, hidden first one-bit)
7327  *
7328  * X   sign:1 exp:15 mantissa:112
7329  *     (exponent bias 16382, hidden first one-bit)
7330  *
7331  */
7332
7333 #ifdef DOUBLE_IS_VAX_FLOAT
7334 #  define DOUBLE_VAX_ENDIAN
7335 #endif
7336
7337 #ifdef DOUBLE_IS_IEEE_FORMAT
7338 /* All the basic IEEE formats have the implicit bit,
7339  * except for the x86 80-bit extended formats, which will undef this.
7340  * Also note that the IEEE 754 subnormals (formerly known as denormals)
7341  * do not have the implicit bit of one. */
7342 #  define NV_IMPLICIT_BIT
7343 #endif
7344
7345 #if defined(LONG_DOUBLEKIND) && LONG_DOUBLEKIND != LONG_DOUBLE_IS_DOUBLE
7346
7347 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN || \
7348       LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN || \
7349       LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_LE
7350 #    define LONGDOUBLE_LITTLE_ENDIAN
7351 #  endif
7352
7353 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN || \
7354       LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN || \
7355       LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE
7356 #    define LONGDOUBLE_BIG_ENDIAN
7357 #  endif
7358
7359 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_BE || \
7360       LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_LE
7361 #    define LONGDOUBLE_MIX_ENDIAN
7362 #  endif
7363
7364 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN || \
7365       LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
7366 #    define LONGDOUBLE_X86_80_BIT
7367 #    ifdef USE_LONG_DOUBLE
7368 #      undef NV_IMPLICIT_BIT
7369 #      define NV_X86_80_BIT
7370 #    endif
7371 #  endif
7372
7373 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_LE || \
7374       LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE || \
7375       LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_BE || \
7376       LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_LE
7377 #    define LONGDOUBLE_DOUBLEDOUBLE
7378 #  endif
7379
7380 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_VAX_H_FLOAT
7381 #    define LONGDOUBLE_VAX_ENDIAN
7382 #  endif
7383
7384 #endif /* LONG_DOUBLEKIND */
7385
7386 #ifdef USE_QUADMATH /* assume quadmath endianness == native double endianness */
7387 #  if defined(DOUBLE_LITTLE_ENDIAN)
7388 #    define NV_LITTLE_ENDIAN
7389 #  elif defined(DOUBLE_BIG_ENDIAN)
7390 #    define NV_BIG_ENDIAN
7391 #  elif defined(DOUBLE_MIX_ENDIAN) /* stretch */
7392 #    define NV_MIX_ENDIAN
7393 #  endif
7394 #elif NVSIZE == DOUBLESIZE
7395 #  ifdef DOUBLE_LITTLE_ENDIAN
7396 #    define NV_LITTLE_ENDIAN
7397 #  endif
7398 #  ifdef DOUBLE_BIG_ENDIAN
7399 #    define NV_BIG_ENDIAN
7400 #  endif
7401 #  ifdef DOUBLE_MIX_ENDIAN
7402 #    define NV_MIX_ENDIAN
7403 #  endif
7404 #  ifdef DOUBLE_VAX_ENDIAN
7405 #    define NV_VAX_ENDIAN
7406 #  endif
7407 #elif NVSIZE == LONG_DOUBLESIZE
7408 #  ifdef LONGDOUBLE_LITTLE_ENDIAN
7409 #    define NV_LITTLE_ENDIAN
7410 #  endif
7411 #  ifdef LONGDOUBLE_BIG_ENDIAN
7412 #    define NV_BIG_ENDIAN
7413 #  endif
7414 #  ifdef LONGDOUBLE_MIX_ENDIAN
7415 #    define NV_MIX_ENDIAN
7416 #  endif
7417 #  ifdef LONGDOUBLE_VAX_ENDIAN
7418 #    define NV_VAX_ENDIAN
7419 #  endif
7420 #endif
7421
7422 /* We have somehow managed not to define the denormal/subnormal
7423  * detection.
7424  *
7425  * This may happen if the compiler doesn't expose the C99 math like
7426  * the fpclassify() without some special switches.  Perl tries to
7427  * stay C89, so for example -std=c99 is not an option.
7428  *
7429  * The Perl_isinf() and Perl_isnan() should have been defined even if
7430  * the C99 isinf() and isnan() are unavailable, and the NV_MIN becomes
7431  * from the C89 DBL_MIN or moral equivalent. */
7432 #if !defined(Perl_fp_class_denorm) && defined(Perl_isinf) && defined(Perl_isnan) && defined(NV_MIN)
7433 #  define Perl_fp_class_denorm(x) ((x) != 0.0 && !Perl_isinf(x) && !Perl_isnan(x) && PERL_ABS(x) < NV_MIN)
7434 #endif
7435
7436 /* This is not a great fallback: subnormals tests will fail,
7437  * but at least Perl will link and 99.999% of tests will work. */
7438 #if !defined(Perl_fp_class_denorm)
7439 #  define Perl_fp_class_denorm(x) FALSE
7440 #endif
7441
7442 #ifdef DOUBLE_IS_IEEE_FORMAT
7443 #  define DOUBLE_HAS_INF
7444 #  define DOUBLE_HAS_NAN
7445 #endif
7446
7447 #ifdef DOUBLE_HAS_NAN
7448
7449 START_EXTERN_C
7450
7451 #ifdef DOINIT
7452
7453 /* PL_inf and PL_nan initialization.
7454  *
7455  * For inf and nan initialization the ultimate fallback is dividing
7456  * one or zero by zero: however, some compilers will warn or even fail
7457  * on divide-by-zero, but hopefully something earlier will work.
7458  *
7459  * If you are thinking of using HUGE_VAL for infinity, or using
7460  * <math.h> functions to generate NV_INF (e.g. exp(1e9), log(-1.0)),
7461  * stop.  Neither will work portably: HUGE_VAL can be just DBL_MAX,
7462  * and the math functions might be just generating DBL_MAX, or even zero.
7463  *
7464  * Also, do NOT try doing NV_NAN based on NV_INF and trying (NV_INF-NV_INF).
7465  * Though logically correct, some compilers (like Visual C 2003)
7466  * falsely misoptimize that to zero (x-x is always zero, right?)
7467  *
7468  * Finally, note that not all floating point formats define Inf (or NaN).
7469  * For the infinity a large number may be used instead.  Operations that
7470  * under the IEEE floating point would return Inf or NaN may return
7471  * either large numbers (positive or negative), or they may cause
7472  * a floating point exception or some other fault.
7473  */
7474
7475 /* The quadmath literals are anon structs which -Wc++-compat doesn't like. */
7476 GCC_DIAG_IGNORE_DECL(-Wc++-compat);
7477
7478 #  ifdef USE_QUADMATH
7479 /* Cannot use HUGE_VALQ for PL_inf because not a compile-time
7480  * constant. */
7481 INFNAN_NV_U8_DECL PL_inf = { 1.0Q/0.0Q };
7482 #  elif NVSIZE == LONG_DOUBLESIZE && defined(LONGDBLINFBYTES)
7483 INFNAN_U8_NV_DECL PL_inf = { { LONGDBLINFBYTES } };
7484 #  elif NVSIZE == DOUBLESIZE && defined(DOUBLEINFBYTES)
7485 INFNAN_U8_NV_DECL PL_inf = { { DOUBLEINFBYTES } };
7486 #  else
7487 #    if NVSIZE == LONG_DOUBLESIZE && defined(USE_LONG_DOUBLE)
7488 #      if defined(LDBL_INFINITY)
7489 INFNAN_NV_U8_DECL PL_inf = { LDBL_INFINITY };
7490 #      elif defined(LDBL_INF)
7491 INFNAN_NV_U8_DECL PL_inf = { LDBL_INF };
7492 #      elif defined(INFINITY)
7493 INFNAN_NV_U8_DECL PL_inf = { (NV)INFINITY };
7494 #      elif defined(INF)
7495 INFNAN_NV_U8_DECL PL_inf = { (NV)INF };
7496 #      else
7497 INFNAN_NV_U8_DECL PL_inf = { 1.0L/0.0L }; /* keep last */
7498 #      endif
7499 #    else
7500 #      if defined(DBL_INFINITY)
7501 INFNAN_NV_U8_DECL PL_inf = { DBL_INFINITY };
7502 #      elif defined(DBL_INF)
7503 INFNAN_NV_U8_DECL PL_inf = { DBL_INF };
7504 #      elif defined(INFINITY) /* C99 */
7505 INFNAN_NV_U8_DECL PL_inf = { (NV)INFINITY };
7506 #      elif defined(INF)
7507 INFNAN_NV_U8_DECL PL_inf = { (NV)INF };
7508 #      else
7509 INFNAN_NV_U8_DECL PL_inf = { 1.0/0.0 }; /* keep last */
7510 #      endif
7511 #    endif
7512 #  endif
7513
7514 #  ifdef USE_QUADMATH
7515 /* Cannot use nanq("0") for PL_nan because not a compile-time
7516  * constant. */
7517 INFNAN_NV_U8_DECL PL_nan = { 0.0Q/0.0Q };
7518 #  elif NVSIZE == LONG_DOUBLESIZE && defined(LONGDBLNANBYTES)
7519 INFNAN_U8_NV_DECL PL_nan = { { LONGDBLNANBYTES } };
7520 #  elif NVSIZE == DOUBLESIZE && defined(DOUBLENANBYTES)
7521 INFNAN_U8_NV_DECL PL_nan = { { DOUBLENANBYTES } };
7522 #  else
7523 #    if NVSIZE == LONG_DOUBLESIZE && defined(USE_LONG_DOUBLE)
7524 #      if defined(LDBL_NAN)
7525 INFNAN_NV_U8_DECL PL_nan = { LDBL_NAN };
7526 #      elif defined(LDBL_QNAN)
7527 INFNAN_NV_U8_DECL PL_nan = { LDBL_QNAN };
7528 #      elif defined(NAN)
7529 INFNAN_NV_U8_DECL PL_nan = { (NV)NAN };
7530 #      else
7531 INFNAN_NV_U8_DECL PL_nan = { 0.0L/0.0L }; /* keep last */
7532 #      endif
7533 #    else
7534 #      if defined(DBL_NAN)
7535 INFNAN_NV_U8_DECL PL_nan = { DBL_NAN };
7536 #      elif defined(DBL_QNAN)
7537 INFNAN_NV_U8_DECL PL_nan = { DBL_QNAN };
7538 #      elif defined(NAN) /* C99 */
7539 INFNAN_NV_U8_DECL PL_nan = { (NV)NAN };
7540 #      else
7541 INFNAN_NV_U8_DECL PL_nan = { 0.0/0.0 }; /* keep last */
7542 #      endif
7543 #    endif
7544 #  endif
7545
7546 GCC_DIAG_RESTORE_DECL;
7547
7548 #else
7549
7550 INFNAN_NV_U8_DECL PL_inf;
7551 INFNAN_NV_U8_DECL PL_nan;
7552
7553 #endif
7554
7555 END_EXTERN_C
7556
7557 /* If you have not defined NV_INF/NV_NAN (like for example win32/win32.h),
7558  * we will define NV_INF/NV_NAN as the nv part of the global const
7559  * PL_inf/PL_nan.  Note, however, that the preexisting NV_INF/NV_NAN
7560  * might not be a compile-time constant, in which case it cannot be
7561  * used to initialize PL_inf/PL_nan above. */
7562 #ifndef NV_INF
7563 #  define NV_INF PL_inf.nv
7564 #endif
7565 #ifndef NV_NAN
7566 #  define NV_NAN PL_nan.nv
7567 #endif
7568
7569 /* NaNs (not-a-numbers) can carry payload bits, in addition to
7570  * "nan-ness".  Part of the payload is the quiet/signaling bit.
7571  * To back up a bit (harhar):
7572  *
7573  * For IEEE 754 64-bit formats [1]:
7574  *
7575  * s 000 (mantissa all-zero)  zero
7576  * s 000 (mantissa non-zero)  subnormals (denormals)
7577  * s 001 ... 7fe              normals
7578  * s 7ff q                    nan
7579  *
7580  * For IEEE 754 128-bit formats:
7581  *
7582  * s 0000 (mantissa all-zero)  zero
7583  * s 0000 (mantissa non-zero)  subnormals (denormals)
7584  * s 0001 ... 7ffe             normals
7585  * s 7fff q                    nan
7586  *
7587  * [1] this looks like big-endian, but applies equally to little-endian.
7588  *
7589  * s = Sign bit.  Yes, zeros and nans can have negative sign,
7590  *     the interpretation is application-specific.
7591  *
7592  * q = Quietness bit, the interpretation is platform-specific.
7593  *     Most platforms have the most significant bit being one
7594  *     meaning quiet, but some (older mips, hppa) have the msb
7595  *     being one meaning signaling.  Note that the above means
7596  *     that on most platforms there cannot be signaling nan with
7597  *     zero payload because that is identical with infinity;
7598  *     while conversely on older mips/hppa there cannot be a quiet nan
7599  *     because that is identical with infinity.
7600  *
7601  *     Moreover, whether there is any behavioral difference
7602  *     between quiet and signaling NaNs, depends on the platform.
7603  *
7604  * x86 80-bit extended precision is different, the mantissa bits:
7605  *
7606  * 63 62 61   30387+    pre-387    visual c
7607  * --------   ----      --------   --------
7608  *  0  0  0   invalid   infinity
7609  *  0  0  1   invalid   snan
7610  *  0  1  0   invalid   snan
7611  *  0  1  1   invalid   snan
7612  *  1  0  0   infinity  snan        1.#INF
7613  *  1  0  1   snan                  1.#SNAN
7614  *  1  1  0   qnan                 -1.#IND  (x86 chooses this to negative)
7615  *  1  1  1   qnan                  1.#QNAN
7616  *
7617  * This means that in this format there are 61 bits available
7618  * for the nan payload.
7619  *
7620  * Note that the 32-bit x86 ABI cannot do signaling nans: the x87
7621  * simply cannot preserve the bit.  You can either use the 80-bit
7622  * extended precision (long double, -Duselongdouble), or use x86-64.
7623  *
7624  * In all platforms, the payload bytes (and bits, some of them are
7625  * often in a partial byte) themselves can be either all zero (x86),
7626  * all one (sparc or mips), or a mixture: in IEEE 754 128-bit double
7627  * or in a double-double, the first half of the payload can follow the
7628  * native double, while in the second half the payload can be all
7629  * zeros.  (Therefore the mask for payload bits is not necessarily
7630  * identical to bit complement of the NaN.)  Another way of putting
7631  * this: the payload for the default NaN might not be zero.
7632  *
7633  * For the x86 80-bit long doubles, the trailing bytes (the 80 bits
7634  * being 'packaged' in either 12 or 16 bytes) can be whatever random
7635  * garbage.
7636  *
7637  * Furthermore, the semantics of the sign bit on NaNs are platform-specific.
7638  * On normal floats, the sign bit being on means negative.  But this may,
7639  * or may not, be reverted on NaNs: in other words, the default NaN might
7640  * have the sign bit on, and therefore look like negative if you look
7641  * at it at the bit level.
7642  *
7643  * NaN payloads are not propagated even on copies, or in arithmetics.
7644  * They *might* be, according to some rules, on your particular
7645  * cpu/os/compiler/libraries, but no guarantees.
7646  *
7647  * To summarize, on most platforms, and for 64-bit doubles
7648  * (using big-endian ordering here):
7649  *
7650  * [7FF8000000000000..7FFFFFFFFFFFFFFF] quiet
7651  * [FFF8000000000000..FFFFFFFFFFFFFFFF] quiet
7652  * [7FF0000000000001..7FF7FFFFFFFFFFFF] signaling
7653  * [FFF0000000000001..FFF7FFFFFFFFFFFF] signaling
7654  *
7655  * The C99 nan() is supposed to generate *quiet* NaNs.
7656  *
7657  * Note the asymmetry:
7658  * The 7FF0000000000000 is positive infinity,
7659  * the FFF0000000000000 is negative infinity.
7660  */
7661
7662 /* NVMANTBITS is the number of _real_ mantissa bits in an NV.
7663  * For the standard IEEE 754 fp this number is usually one less that
7664  * *DBL_MANT_DIG because of the implicit (aka hidden) bit, which isn't
7665  * real.  For the 80-bit extended precision formats (x86*), the number
7666  * of mantissa bits... depends. For normal floats, it's 64.  But for
7667  * the inf/nan, it's different (zero for inf, 61 for nan).
7668  * NVMANTBITS works for normal floats. */
7669
7670 /* We do not want to include the quiet/signaling bit. */
7671 #define NV_NAN_BITS (NVMANTBITS - 1)
7672
7673 #if defined(USE_LONG_DOUBLE) && NVSIZE > DOUBLESIZE
7674 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
7675 #    define NV_NAN_QS_BYTE_OFFSET 13
7676 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
7677 #    define NV_NAN_QS_BYTE_OFFSET 2
7678 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
7679 #    define NV_NAN_QS_BYTE_OFFSET 7
7680 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
7681 #    define NV_NAN_QS_BYTE_OFFSET 2
7682 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_LE
7683 #    define NV_NAN_QS_BYTE_OFFSET 13
7684 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE
7685 #    define NV_NAN_QS_BYTE_OFFSET 1
7686 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_BE
7687 #    define NV_NAN_QS_BYTE_OFFSET 9
7688 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_LE
7689 #    define NV_NAN_QS_BYTE_OFFSET 6
7690 #  else
7691 #    error "Unexpected long double format"
7692 #  endif
7693 #else
7694 #  ifdef USE_QUADMATH
7695 #    ifdef NV_LITTLE_ENDIAN
7696 #      define NV_NAN_QS_BYTE_OFFSET 13
7697 #    elif defined(NV_BIG_ENDIAN)
7698 #      define NV_NAN_QS_BYTE_OFFSET 2
7699 #    else
7700 #      error "Unexpected quadmath format"
7701 #    endif
7702 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_32_BIT_LITTLE_ENDIAN
7703 #    define NV_NAN_QS_BYTE_OFFSET 2
7704 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_32_BIT_BIG_ENDIAN
7705 #    define NV_NAN_QS_BYTE_OFFSET 1
7706 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_LITTLE_ENDIAN
7707 #    define NV_NAN_QS_BYTE_OFFSET 6
7708 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_BIG_ENDIAN
7709 #    define NV_NAN_QS_BYTE_OFFSET 1
7710 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
7711 #    define NV_NAN_QS_BYTE_OFFSET 13
7712 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
7713 #    define NV_NAN_QS_BYTE_OFFSET 2
7714 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
7715 #    define NV_NAN_QS_BYTE_OFFSET 2 /* bytes 4 5 6 7 0 1 2 3 (MSB 7) */
7716 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
7717 #    define NV_NAN_QS_BYTE_OFFSET 5 /* bytes 3 2 1 0 7 6 5 4 (MSB 7) */
7718 #  else
7719 /* For example the VAX formats should never
7720  * get here because they do not have NaN. */
7721 #    error "Unexpected double format"
7722 #  endif
7723 #endif
7724 /* NV_NAN_QS_BYTE is the byte to test for the quiet/signaling */
7725 #define NV_NAN_QS_BYTE(nvp) (((U8*)(nvp))[NV_NAN_QS_BYTE_OFFSET])
7726 /* NV_NAN_QS_BIT is the bit to test in the NV_NAN_QS_BYTE_OFFSET
7727  * for the quiet/signaling */
7728 #if defined(USE_LONG_DOUBLE) && \
7729   (LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN || \
7730    LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN)
7731 #  define NV_NAN_QS_BIT_SHIFT 6 /* 0x40 */
7732 #elif defined(USE_LONG_DOUBLE) && \
7733   (LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_LE || \
7734    LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE || \
7735    LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_BE || \
7736    LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_LE)
7737 #  define NV_NAN_QS_BIT_SHIFT 3 /* 0x08, but not via NV_NAN_BITS */
7738 #else
7739 #  define NV_NAN_QS_BIT_SHIFT ((NV_NAN_BITS) % 8) /* usually 3, or 0x08 */
7740 #endif
7741 #define NV_NAN_QS_BIT (1 << (NV_NAN_QS_BIT_SHIFT))
7742 /* NV_NAN_QS_BIT_OFFSET is the bit offset from the beginning of a NV
7743  * (bytes ordered big-endianly) for the quiet/signaling bit
7744  * for the quiet/signaling */
7745 #define NV_NAN_QS_BIT_OFFSET \
7746     (8 * (NV_NAN_QS_BYTE_OFFSET) + (NV_NAN_QS_BIT_SHIFT))
7747 /* NV_NAN_QS_QUIET (always defined) is true if the NV_NAN_QS_QS_BIT being
7748  * on indicates quiet NaN.  NV_NAN_QS_SIGNALING (also always defined)
7749  * is true if the NV_NAN_QS_BIT being on indicates signaling NaN. */
7750 #define NV_NAN_QS_QUIET \
7751     ((NV_NAN_QS_BYTE(PL_nan.u8) & NV_NAN_QS_BIT) == NV_NAN_QS_BIT)
7752 #define NV_NAN_QS_SIGNALING (!(NV_NAN_QS_QUIET))
7753 #define NV_NAN_QS_TEST(nvp) (NV_NAN_QS_BYTE(nvp) & NV_NAN_QS_BIT)
7754 /* NV_NAN_IS_QUIET() returns true if the NV behind nvp is a NaN,
7755  * whether it is a quiet NaN, NV_NAN_IS_SIGNALING() if a signaling NaN.
7756  * Note however that these do not check whether the nvp is a NaN. */
7757 #define NV_NAN_IS_QUIET(nvp) \
7758     (NV_NAN_QS_TEST(nvp) == (NV_NAN_QS_QUIET ? NV_NAN_QS_BIT : 0))
7759 #define NV_NAN_IS_SIGNALING(nvp) \
7760     (NV_NAN_QS_TEST(nvp) == (NV_NAN_QS_QUIET ? 0 : NV_NAN_QS_BIT))
7761 #define NV_NAN_SET_QUIET(nvp) \
7762     (NV_NAN_QS_QUIET ? \
7763      (NV_NAN_QS_BYTE(nvp) |= NV_NAN_QS_BIT) : \
7764      (NV_NAN_QS_BYTE(nvp) &= ~NV_NAN_QS_BIT))
7765 #define NV_NAN_SET_SIGNALING(nvp) \
7766     (NV_NAN_QS_QUIET ? \
7767      (NV_NAN_QS_BYTE(nvp) &= ~NV_NAN_QS_BIT) : \
7768      (NV_NAN_QS_BYTE(nvp) |= NV_NAN_QS_BIT))
7769 #define NV_NAN_QS_XOR(nvp) (NV_NAN_QS_BYTE(nvp) ^= NV_NAN_QS_BIT)
7770
7771 /* NV_NAN_PAYLOAD_MASK: masking the nan payload bits.
7772  *
7773  * NV_NAN_PAYLOAD_PERM: permuting the nan payload bytes.
7774  * 0xFF means "don't go here".*/
7775
7776 /* Shorthands to avoid typoses. */
7777 #define NV_NAN_PAYLOAD_MASK_SKIP_EIGHT \
7778   0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
7779 #define NV_NAN_PAYLOAD_PERM_SKIP_EIGHT \
7780   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
7781 #define NV_NAN_PAYLOAD_PERM_0_TO_7 \
7782   0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
7783 #define NV_NAN_PAYLOAD_PERM_7_TO_0 \
7784   0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0
7785 #define NV_NAN_PAYLOAD_MASK_IEEE_754_128_LE \
7786   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
7787   0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00
7788 #define NV_NAN_PAYLOAD_PERM_IEEE_754_128_LE \
7789   NV_NAN_PAYLOAD_PERM_0_TO_7, \
7790   0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xFF, 0xFF
7791 #define NV_NAN_PAYLOAD_MASK_IEEE_754_128_BE \
7792   0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, \
7793   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
7794 #define NV_NAN_PAYLOAD_PERM_IEEE_754_128_BE \
7795   0xFF, 0xFF, 0xd, 0xc, 0xb, 0xa, 0x9, 0x8, \
7796   NV_NAN_PAYLOAD_PERM_7_TO_0
7797 #define NV_NAN_PAYLOAD_MASK_IEEE_754_64_LE \
7798   0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00
7799 #define NV_NAN_PAYLOAD_PERM_IEEE_754_64_LE \
7800   0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0xFF
7801 #define NV_NAN_PAYLOAD_MASK_IEEE_754_64_BE \
7802   0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
7803 #define NV_NAN_PAYLOAD_PERM_IEEE_754_64_BE \
7804   0xFF, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x0
7805
7806 #if defined(USE_LONG_DOUBLE) && NVSIZE > DOUBLESIZE
7807 #  if LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
7808 #    define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_128_LE
7809 #    define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_128_LE
7810 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
7811 #    define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_128_BE
7812 #    define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_128_BE
7813 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_LITTLE_ENDIAN
7814 #    if LONG_DOUBLESIZE == 10
7815 #      define NV_NAN_PAYLOAD_MASK \
7816          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, \
7817          0x00, 0x00
7818 #      define NV_NAN_PAYLOAD_PERM \
7819          NV_NAN_PAYLOAD_PERM_0_TO_7, 0xFF, 0xFF
7820 #    elif LONG_DOUBLESIZE == 12
7821 #      define NV_NAN_PAYLOAD_MASK \
7822          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, \
7823          0x00, 0x00, 0x00, 0x00
7824 #      define NV_NAN_PAYLOAD_PERM \
7825          NV_NAN_PAYLOAD_PERM_0_TO_7, 0xFF, 0xFF, 0xFF, 0xFF
7826 #    elif LONG_DOUBLESIZE == 16
7827 #      define NV_NAN_PAYLOAD_MASK \
7828          0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, \
7829          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7830 #      define NV_NAN_PAYLOAD_PERM \
7831          NV_NAN_PAYLOAD_PERM_0_TO_7, \
7832          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
7833 #    else
7834 #      error "Unexpected x86 80-bit little-endian long double format"
7835 #    endif
7836 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_X86_80_BIT_BIG_ENDIAN
7837 #    if LONG_DOUBLESIZE == 10
7838 #      define NV_NAN_PAYLOAD_MASK \
7839          0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, \
7840          0xff, 0xff
7841 #      define NV_NAN_PAYLOAD_PERM \
7842          NV_NAN_PAYLOAD_PERM_7_TO_0, 0xFF, 0xFF
7843 #    elif LONG_DOUBLESIZE == 12
7844 #      define NV_NAN_PAYLOAD_MASK \
7845          0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, \
7846          0xff, 0xff, 0x00, 0x00
7847 #      define NV_NAN_PAYLOAD_PERM \
7848          NV_NAN_PAYLOAD_PERM_7_TO_0, 0xFF, 0xFF, 0xFF, 0xFF
7849 #    elif LONG_DOUBLESIZE == 16
7850 #      define NV_NAN_PAYLOAD_MASK \
7851          0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, \
7852          0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
7853 #      define NV_NAN_PAYLOAD_PERM \
7854          NV_NAN_PAYLOAD_PERM_7_TO_0, \
7855          0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
7856 #    else
7857 #      error "Unexpected x86 80-bit big-endian long double format"
7858 #    endif
7859 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_LE
7860 /* For double-double we assume only the first double (in LE or BE terms)
7861  * is used for NaN. */
7862 #    define NV_NAN_PAYLOAD_MASK \
7863        NV_NAN_PAYLOAD_MASK_SKIP_EIGHT, NV_NAN_PAYLOAD_MASK_IEEE_754_64_LE
7864 #    define NV_NAN_PAYLOAD_PERM \
7865        NV_NAN_PAYLOAD_PERM_SKIP_EIGHT, NV_NAN_PAYLOAD_PERM_IEEE_754_64_LE
7866 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_BE
7867 #    define NV_NAN_PAYLOAD_MASK \
7868        NV_NAN_PAYLOAD_MASK_IEEE_754_64_BE
7869 #    define NV_NAN_PAYLOAD_PERM \
7870        NV_NAN_PAYLOAD_PERM_IEEE_754_64_BE
7871 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_LE_BE
7872 #    define NV_NAN_PAYLOAD_MASK \
7873        NV_NAN_PAYLOAD_MASK_IEEE_754_64_LE
7874 #    define NV_NAN_PAYLOAD_PERM \
7875        NV_NAN_PAYLOAD_PERM_IEEE_754_64_LE
7876 #  elif LONG_DOUBLEKIND == LONG_DOUBLE_IS_DOUBLEDOUBLE_128_BIT_BE_LE
7877 #    define NV_NAN_PAYLOAD_MASK \
7878        NV_NAN_PAYLOAD_MASK_SKIP_EIGHT, NV_NAN_PAYLOAD_MASK_IEEE_754_64_BE
7879 #    define NV_NAN_PAYLOAD_PERM \
7880        NV_NAN_PAYLOAD_PERM_SKIP_EIGHT, NV_NAN_PAYLOAD_PERM_IEEE_754_64_BE
7881 #  else
7882 #    error "Unexpected long double format"
7883 #  endif
7884 #else
7885 #  ifdef USE_QUADMATH /* quadmath is not long double */
7886 #    ifdef NV_LITTLE_ENDIAN
7887 #      define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_128_LE
7888 #      define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_128_LE
7889 #    elif defined(NV_BIG_ENDIAN)
7890 #      define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_128_BE
7891 #      define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_128_BE
7892 #    else
7893 #      error "Unexpected quadmath format"
7894 #    endif
7895 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_32_BIT_LITTLE_ENDIAN
7896 #    define NV_NAN_PAYLOAD_MASK 0xff, 0xff, 0x07, 0x00
7897 #    define NV_NAN_PAYLOAD_PERM 0x0, 0x1, 0x2, 0xFF
7898 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_32_BIT_BIG_ENDIAN
7899 #    define NV_NAN_PAYLOAD_MASK 0x00, 0x07, 0xff, 0xff
7900 #    define NV_NAN_PAYLOAD_PERM 0xFF, 0x2, 0x1, 0x0
7901 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_LITTLE_ENDIAN
7902 #    define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_64_LE
7903 #    define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_64_LE
7904 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_BIG_ENDIAN
7905 #    define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_64_BE
7906 #    define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_64_BE
7907 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_128_BIT_LITTLE_ENDIAN
7908 #    define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_128_LE
7909 #    define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_128_LE
7910 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_128_BIT_BIG_ENDIAN
7911 #    define NV_NAN_PAYLOAD_MASK NV_NAN_PAYLOAD_MASK_IEEE_754_128_BE
7912 #    define NV_NAN_PAYLOAD_PERM NV_NAN_PAYLOAD_PERM_IEEE_754_128_BE
7913 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_LE_BE
7914 #    define NV_NAN_PAYLOAD_MASK 0xff, 0xff, 0x07, 0x00, 0xff, 0xff, 0xff, 0xff
7915 #    define NV_NAN_PAYLOAD_PERM 0x4, 0x5, 0x6, 0xFF, 0x0, 0x1, 0x2, 0x3
7916 #  elif DOUBLEKIND == DOUBLE_IS_IEEE_754_64_BIT_MIXED_ENDIAN_BE_LE
7917 #    define NV_NAN_PAYLOAD_MASK 0xff, 0xff, 0xff, 0xff, 0x00, 0x07, 0xff, 0xff
7918 #    define NV_NAN_PAYLOAD_PERM 0x3, 0x2, 0x1, 0x0, 0xFF, 0x6, 0x5, 0x4
7919 #  else
7920 #    error "Unexpected double format"
7921 #  endif
7922 #endif
7923
7924 #endif /* DOUBLE_HAS_NAN */
7925
7926
7927 /*
7928
7929    (KEEP THIS LAST IN perl.h!)
7930
7931    Mention
7932
7933    NV_PRESERVES_UV
7934
7935    HAS_MKSTEMP
7936    HAS_MKSTEMPS
7937    HAS_MKDTEMP
7938
7939    HAS_GETCWD
7940
7941    HAS_MMAP
7942    HAS_MPROTECT
7943    HAS_MSYNC
7944    HAS_MADVISE
7945    HAS_MUNMAP
7946    I_SYSMMAN
7947    Mmap_t
7948
7949    NVef
7950    NVff
7951    NVgf
7952
7953    HAS_UALARM
7954    HAS_USLEEP
7955
7956    HAS_SETITIMER
7957    HAS_GETITIMER
7958
7959    HAS_SENDMSG
7960    HAS_RECVMSG
7961    HAS_READV
7962    HAS_WRITEV
7963    I_SYSUIO
7964    HAS_STRUCT_MSGHDR
7965    HAS_STRUCT_CMSGHDR
7966
7967    HAS_NL_LANGINFO
7968
7969    HAS_DIRFD
7970
7971    so that Configure picks them up.
7972
7973    (KEEP THIS LAST IN perl.h!)
7974
7975 */
7976
7977 #endif /* Include guard */
7978
7979 /*
7980  * ex: set ts=8 sts=4 sw=4 et:
7981  */