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