Skip to content

Commit 90f4e9b

Browse files
committed
regen/embed.pl: Handle m with p flags
Prior to this commit these were illegal. This causes embed.fnc to generate macro 'Perl_foo' #defined to be macro 'foo'. If the macro name is all upper case, we instead get macro 'PERL_FOO' for 'FOO'. This could be used to easily convert existing macros into having long names should some become a name space pollution problem. This also documents in embed.fnc, under the 'm' flag discussion, how to use this instead of placing things in mathoms.c, or creating stub functions.
1 parent f8a5e26 commit 90f4e9b

File tree

4 files changed

+80
-42
lines changed

4 files changed

+80
-42
lines changed

autodoc.pl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,11 @@ ($fh, $section_name, $element_name, $docref)
18781878
|| $needs_Perl_entry
18791879
|| $cant_use_short_name)
18801880
{
1881-
$name = "Perl_$name";
1881+
# An all uppercase macro name gets an uppercase prefix.
1882+
my $perl = ($flags =~ /m/ && $name !~ /[[:lower:]]/)
1883+
? "PERL_"
1884+
: "Perl_";
1885+
$name = "$perl$name";
18821886

18831887
# We can't hide the existence of any thread context
18841888
# parameter when using the "Perl_" long form. So it must

embed.fnc

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
: real (full) name, with any appropriate thread context paramaters, thus hiding
1515
: that detail from the typical code.
1616
:
17-
: Most macros (as opposed to functions) listed here are the complete full name.
17+
: Many macros (as opposed to functions) listed here are the complete full name,
18+
: though we may want to start converting those to have full names.
1819
:
1920
: All non-static functions defined by perl need to be listed in this file.
2021
: embed.pl uses the entries here to construct:
@@ -157,8 +158,9 @@
157158
: and you know at a glance that the macro actually has documentation. It
158159
: doesn't by itself create any documentation; instead the other apidoc lines
159160
: pull in information specified by these lines. Many of the lines in this
160-
: file for macros could be pulled out of here and replaced by these lines
161-
: throughout the source. It is a goal to do that as convenience dictates.
161+
: file for macros that don't also have the 'p' flag (described below) could be
162+
: pulled out of here and replaced by these lines throughout the source. It is
163+
: a goal to do that as convenience dictates.
162164
:
163165
: The other apidoc lines either have the usage data as part of the line, or
164166
: pull in the data from this file or apidoc_defn lines.
@@ -210,10 +212,10 @@
210212
: line that begins with an '='. In particular, an '=cut' line ends that
211213
: documentation without introducing something new.
212214
:
213-
: Various macros and other elements aren't listed here in embed.fnc. They are
214-
: documented in the same manner, but since they don't have this file to get
215-
: information from, the defining lines have the syntax and meaning they do in
216-
: this file, so it can be specified:
215+
: Various macros and other elements aren't listed here in embed.fnc (though
216+
: they could be). They are documented in the same manner, but since they don't
217+
: have this file to get information from, the defining lines have the syntax
218+
: and meaning they do in this file, so it can be specified:
217219
:
218220
: =for apidoc flags|return_type|name|arg1|arg2|...|argN
219221
: =for apidoc_item flags|return_type|name|arg1|arg2|...|argN
@@ -301,14 +303,13 @@
301303
: functions flagged with this, the installation can run Configure with
302304
: the -Accflags='-DNO_MATHOMS' parameter to not even compile them.
303305
:
304-
: Sometimes the function has been subsumed by a more general one (say,
305-
: by adding a flags parameter), and a macro exists with the original
306-
: short name API, and it calls the new function, bypassing this one, and
307-
: the original 'Perl_' form is being deprecated. In this case also
308-
: specify the 'M' flag.
306+
: If the function can be implemented as a macro (that evaluates its
307+
: arguments exactly once), use the 'm' and 'p' flags together to implement
308+
: this. (See the discussion under 'm'.) Another option for this is to
309+
: use the 'M' flag.
309310
:
310-
: Without the M flag, these functions should be deprecated, and it is an
311-
: error to not also specify the 'D' flag.
311+
: Without the m or M flags, these functions should be deprecated, and it
312+
: is an error to not also specify the 'D' flag.
312313
:
313314
: The 'b' functions are normally moved to mathoms.c, but if
314315
: circumstances dictate otherwise, they can be anywhere, provided the
@@ -439,24 +440,50 @@
439440
: __attribute__always_inline__ is added
440441
:
441442
: 'm' Implemented as a macro; there is no function associated with this
442-
: name, and hence no long Perl_ or S_ name. However, if the macro name
443-
: itself begins with 'Perl_', autodoc.pl will show a thread context
444-
: parameter unless the 'T' flag is specified.
443+
: name. There is no long S_ name.
444+
:
445+
: However, you may #define the macro with a long name like 'Perl_foo',
446+
: and specify the 'p' flag. This will cause an embed.h entry to be
447+
: created that #defines 'foo' as 'Perl_foo'. This can be used to make
448+
: any macro have a long name, perhaps to avoid name collisions. If
449+
: instead you define the macro as 'PERL_FOO' (all uppercase), the
450+
: embed.h entry will use all uppercase.
451+
:
452+
: It is particularly useful tp preserve backward compatibility when a
453+
: function is converted to be a macro (remembering to not create a macro
454+
: which evaluates its parameters more than once). Most of mathoms.c
455+
: could be converted to use this facility. When there is no thread
456+
: context involved, you just do something like
457+
:
458+
: #define Perl_foo(a, b, c) Perl_bar(a, b, 0, c)
459+
:
460+
: Otherwise consider this general case where there is a series of macros
461+
: that build on the previous ones by calling something with a different
462+
: name or with an extra parameter beyond what the previous one did:
463+
:
464+
: #define Perl_foo(mTHX, a) Perl_bar1(aTHX, a)
465+
: #define Perl_bar1(mTHX, a) Perl_bar2(aTHX, a, 0)
466+
: #define Perl_bar2(mTHX, a, b) Perl_bar3(aTHX, a, b, 0)
467+
: #define Perl_bar3(mTHX, a, b, c) Perl_func(aTHX_ a, b, c, 0)
468+
:
469+
: Use the formal parameter name 'mTHX,' (which stands for "macro thread
470+
: context") as the first in each macro definition, and call the next
471+
: macro in the sequence with 'aTHX,' (Note the commas). Eventually, the
472+
: sequence will end with a function call (or else there would be no need
473+
: for thread context). For that instead call it with 'aTHX_' (with an
474+
: underscore instead of a comma).
445475
:
446476
: suppress proto.h entry (actually, not suppressed, but commented out)
447477
: suppress entry in the list of exported symbols available on all
448478
: platforms
449-
: suppress embed.h entry, as the implementation should furnish the macro
479+
: suppress embed.h entry (when no 'p' flag), as the implementation
480+
: should furnish the macro
450481
:
451482
: 'M' The implementation is furnishing its own macro instead of relying on
452483
: the automatically generated short name macro (which simply expands to
453484
: call the real name function). One reason to do this is if the
454-
: parameters need to be cast from what the caller has, or if there is a
455-
: macro that bypasses this function (whose long name is being retained
456-
: for backward compatibility for those who call it with that name). An
457-
: example is when a new function is created with an extra parameter and
458-
: a wrapper macro is added that has the old API, but calls the new one
459-
: with the exta parameter set to a default.
485+
: parameters need to be cast from what the caller has. There is less
486+
: need to do this now that 'm' and 'p' together is supported.
460487
:
461488
: This flag requires the 'p' flag to be specified, as there would be no
462489
: need to do this if the function weren't publicly accessible before.
@@ -492,8 +519,8 @@
492519
:
493520
: This is used for whatever reason to force the function to be called
494521
: with the long name. Perhaps there is a varargs issue. Use the 'M'
495-
: flag instead for wrapper macros, and legacy-only functions should
496-
: also use 'b'.
522+
: or 'm' flags instead for wrapper macros, and legacy-only functions
523+
: should also use 'b'.
497524
:
498525
: embed.h: suppress "#define foo Perl_foo"
499526
:
@@ -518,9 +545,10 @@
518545
:
519546
: proto.h: add __attribute__pure__
520547
:
521-
: 'p' Function in source code has a Perl_ prefix:
548+
: 'p' Function or macro in source code has a Perl_ prefix:
522549
:
523-
: proto.h: function is declared as Perl_foo rather than foo
550+
: proto.h: function or macro is declared as Perl_foo rather than foo
551+
: (though the entries for macros will be commented out)
524552
: embed.h: "#define foo Perl_foo" entries added
525553
:
526554
: 'R' Return value must not be ignored (also implied by 'a' and 'P' flags):

mathoms.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
/*
2323
* This file contains mathoms, various binary artifacts from previous
2424
* versions of Perl which we cannot completely remove from the core
25-
* code. There are two reasons functions should be here:
25+
* code. There is only one reason these days for functions should be here:
2626
*
2727
* 1) A function has been replaced by a macro within a minor release,
2828
* so XS modules compiled against an older release will expect to
2929
* still be able to link against the function
30-
* 2) A function Perl_foo(...) with #define foo Perl_foo(aTHX_ ...)
31-
* has been replaced by a macro, e.g. #define foo(...) foo_flags(...,0)
32-
* but XS code may still explicitly use the long form, i.e.
33-
* Perl_foo(aTHX_ ...)
30+
*
31+
* It used to be that this was the way to handle the case were a function
32+
* Perl_foo(...) had been replaced by a macro. But see the 'm' flag discussion
33+
* in embed.fnc for a better way to handle this.
3434
*
3535
* This file can't just be cleaned out periodically, because that would break
3636
* builds with -DPERL_NO_SHORT_NAMES

regen/embed.pl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@ ($$)
4949
# prefixes 'S_' or 'Perl_'
5050
my ($func, $flags) = @_;
5151

52-
return "Perl_$func" if $flags =~ /[ps]/;
52+
if ($flags =~ /[ps]/) {
53+
54+
# An all uppercase macro name gets an uppercase prefix.
55+
return ($flags =~ /m/ && $flags =~ /p/ && $func !~ /[[:lower:]]/)
56+
? "PERL_$func"
57+
: "Perl_$func";
58+
}
59+
5360
return "S_$func" if $flags =~ /[SIi]/;
5461
return $func;
5562
}
@@ -141,10 +148,6 @@ sub generate_proto_h {
141148
if ($flags =~ /S/) {
142149
die_at_end "$plain_func: m and S flags are mutually exclusive";
143150
}
144-
elsif ($flags =~ /p/ && $has_context) {
145-
die_at_end "$plain_func: m flag with p flag currently requires"
146-
. " T flag";
147-
}
148151
}
149152
else {
150153
die_at_end "$plain_func: u flag only usable with m" if $flags =~ /u/;
@@ -509,7 +512,7 @@ sub embed_h {
509512
my $inner_ind= $ind ? " " : " ";
510513
if ($flags !~ /[omM]/ or ($flags =~ /m/ && $flags =~ /p/)) {
511514
my $argc = scalar @$args;
512-
if ($flags =~ /[Tm]/) {
515+
if ($flags =~ /[T]/) {
513516
my $full_name = full_name($func, $flags);
514517
next if $full_name eq $func; # Don't output a no-op.
515518
$ret = indent_define($func, $full_name, $ind);
@@ -532,8 +535,11 @@ sub embed_h {
532535
$use_va_list ? ("__VA_ARGS__") : ());
533536
$ret = "#${ind}define $func($paramlist) ";
534537
add_indent($ret,full_name($func, $flags) . "(aTHX");
535-
$ret .= "_ " if $replacelist;
536-
$ret .= $replacelist;
538+
if ($replacelist) {
539+
$ret .= ($flags =~ /m/) ? "," : "_ ";
540+
$ret .= $replacelist;
541+
}
542+
537543
if ($flags =~ /W/) {
538544
if ($replacelist) {
539545
$ret .= " comma_aDEPTH";

0 commit comments

Comments
 (0)