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