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