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