This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlop: nits
[perl5.git] / genpacksizetables.pl
CommitLineData
78d46eaa
NC
1#!/usr/bin/perl -w
2# I'm assuming that you're running this on some kind of ASCII system, but
3# it will generate EDCDIC too. (TODO)
4use strict;
5use Encode;
6
a7a3cfaa
TH
7sub make_text {
8 my ($chrmap, $letter, $unpredictable, $nocsum, $size, $condition) = @_;
9 my $text = " /* $letter */ $size";
10 $text .= " | PACK_SIZE_UNPREDICTABLE" if $unpredictable;
11 $text .= " | PACK_SIZE_CANNOT_CSUM" if $nocsum;
12 $text .= ",";
78d46eaa 13
a7a3cfaa
TH
14 if ($condition) {
15 $condition = join " && ", map {"defined($_)"} split ' ', $condition;
16 $text = "#if $condition
17$text
18#else
19 0,
20#endif";
21 }
22 return $text;
78d46eaa
NC
23}
24
a7a3cfaa
TH
25sub make_tables {
26 my %arrays;
78d46eaa 27
a7a3cfaa
TH
28 my $chrmap = shift;
29 foreach (@_) {
30 my ($letter, $shriek, $unpredictable, $nocsum, $size, $condition) =
31 /^([A-Za-z])(!?)\t(\S*)\t(\S*)\t([^\t\n]+)(?:\t+(.*))?$/ or
32 die "Can't parse '$_'";
78d46eaa 33
a7a3cfaa 34 $size = "sizeof($size)" unless $size =~ s/^=//;
78d46eaa 35
a7a3cfaa
TH
36 $arrays{$shriek ? 'shrieking' : 'normal'}{ord $chrmap->{$letter}} =
37 make_text($chrmap, $letter,
38 $unpredictable, $nocsum, $size, $condition);
80a13697 39 }
a7a3cfaa 40
f43a9a31 41 my $text = "STATIC const packprops_t packprops[512] = {\n";
a7a3cfaa
TH
42 foreach my $arrayname (qw(normal shrieking)) {
43 my $array = $arrays{$arrayname} ||
44 die "No defined entries in $arrayname";
45 $text .= " /* $arrayname */\n";
46 for my $ch (0..255) {
47 $text .= $array->{$ch} || " 0,";
48 $text .= "\n";
49 }
7212898e 50 }
a7a3cfaa
TH
51 # Join "0," entries together
52 1 while $text =~ s/\b0,\s*\n\s*0,/0, 0,/g;
53 # But split them up again if the sequence gets too long
54 $text =~ s/((?:\b0, ){15}0,) /$1\n /g;
55 # Clean up final ,
56 $text =~ s/,$//;
57 $text .= "};";
58 return $text;
59}
78d46eaa 60
a7a3cfaa
TH
61my @lines = grep {
62 s/#.*//;
63 /\S/;
64} <DATA>;
78d46eaa 65
a7a3cfaa
TH
66my %asciimap = map {chr $_, chr $_} 0..255;
67my %ebcdicmap = map {chr $_, Encode::encode("posix-bc", chr $_)} 0..255;
78d46eaa 68
a7a3cfaa
TH
69print <<"EOC";
70/* These tables are regenerated by genpacksizetables.pl (and then hand pasted
71 in). You're unlikely ever to need to regenerate them. */
78d46eaa 72
a7a3cfaa
TH
73#if TYPE_IS_SHRIEKING != 0x100
74 ++++shriek offset should be 256
75#endif
76
77typedef U8 packprops_t;
78d46eaa
NC
78#if 'J'-'I' == 1
79/* ASCII */
a7a3cfaa 80@{[make_tables (\%asciimap, @lines)]}
78d46eaa
NC
81#else
82/* EBCDIC (or bust) */
a7a3cfaa
TH
83@{[make_tables (\%ebcdicmap, @lines)]}
84#endif
78d46eaa 85EOC
78d46eaa
NC
86
87__DATA__
f337b084
TH
88#Symbol unpredictable
89# nocsum size
78d46eaa 90c char
f337b084
TH
91C * unsigned char
92W * unsigned char
93U * char
78d46eaa
NC
94s! short
95s =SIZE16
96S! unsigned short
97v =SIZE16
98n =SIZE16
99S =SIZE16
7212898e
NC
100v! =SIZE16 PERL_PACK_CAN_SHRIEKSIGN
101n! =SIZE16 PERL_PACK_CAN_SHRIEKSIGN
78d46eaa
NC
102i int
103i! int
104I unsigned int
105I! unsigned int
106j =IVSIZE
107J =UVSIZE
108l! long
109l =SIZE32
110L! unsigned long
111V =SIZE32
112N =SIZE32
7212898e
NC
113V! =SIZE32 PERL_PACK_CAN_SHRIEKSIGN
114N! =SIZE32 PERL_PACK_CAN_SHRIEKSIGN
78d46eaa 115L =SIZE32
a72c271b 116p * char *
f337b084 117w * * char
80a13697
NC
118q Quad_t HAS_QUAD
119Q Uquad_t HAS_QUAD
78d46eaa
NC
120f float
121d double
122F =NVSIZE
80a13697 123D =LONG_DOUBLESIZE HAS_LONG_DOUBLE USE_LONG_DOUBLE