Re: [perl #33892] Add Interix support
[p5sagit/p5-mst-13.2.git] / genpacksizetables.pl
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)
4 use strict;
5 use Encode;
6
7 my @lines = grep {!/^#/} <DATA>;
8
9 sub addline {
10   my ($arrays, $chrmap, $letter, $arrayname, $noone, $nocsum, $size,
11       $condition) = @_;
12   my $line = "/* $letter */ $size";
13   $line .= " | PACK_SIZE_CANNOT_ONLY_ONE" if $noone;
14   $line .= " | PACK_SIZE_CANNOT_CSUM" if $nocsum;
15   $line .= ",";
16   # And then the hack
17   $line = [$condition, $line] if $condition;
18   $arrays->{$arrayname}->[ord $chrmap->{$letter}] = $line;
19   # print ord $chrmap->{$letter}, " $line\n";
20 }
21
22 sub output_tables {
23   my %arrays;
24
25   my $chrmap = shift;
26   foreach (@_) {
27     my ($letter, $shriek, $noone, $nocsum, $size, $condition)
28       = /^([A-Za-z])(!?)\t(\S*)\t(\S*)\t([^\t\n]+)(?:\t+(.*))?$/;
29     die "Can't parse '$_'" unless $size;
30
31     if (defined $condition) {
32         $condition = join " && ", map {"defined($_)"} split ' ', $condition;
33     }
34     unless ($size =~ s/^=//) {
35       $size = "sizeof($size)";
36     }
37
38     addline (\%arrays, $chrmap, $letter, $shriek ? 'shrieking' : 'normal',
39              $noone, $nocsum, $size, $condition);
40   }
41
42   my %earliest;
43   foreach my $arrayname (sort keys %arrays) {
44     my $array = $arrays{$arrayname};
45     die "No defined entries in $arrayname" unless $array->[$#$array];
46     # Find the first used entry
47     my $earliest = 0;
48     $earliest++ while (!$array->[$earliest]);
49     # Remove all the empty elements.
50     splice @$array, 0, $earliest;
51     print "unsigned char size_${arrayname}[", scalar @$array, "] = {\n";
52     my @lines;
53     foreach (@$array) {
54         # There is an assumption here that the last entry isn't conditonal
55         if (ref $_) {
56             push @lines, "#if $_->[0]", "  $_->[1]", "#else", "  0,", "#endif";
57         } else {
58             push @lines, $_ ? "  $_" : "  0,";
59         }
60     }
61     # remove the last, annoying, comma
62     die "Last entry was a conditional: '$lines[$#lines]'"
63         unless $lines[$#lines] =~ s/,$//;
64     print "$_\n" foreach @lines;
65     print "};\n";
66     $earliest{$arrayname} = $earliest;
67   }
68
69   print "struct packsize_t packsize[2] = {\n";
70
71   my @lines;
72   foreach (qw(normal shrieking)) {
73     my $array = $arrays{$_};
74     push @lines, "  {size_$_, $earliest{$_}, " . (scalar @$array) . "},";
75   }
76   # remove the last, annoying, comma
77   chop $lines[$#lines];
78   print "$_\n" foreach @lines;
79   print "};\n";
80 }
81
82 my %asciimap = (map {chr $_, chr $_} 0..255);
83 my %ebcdicmap = (map {chr $_, Encode::encode ("posix-bc", chr $_)} 0..255);
84
85 print <<'EOC';
86 #if 'J'-'I' == 1
87 /* ASCII */
88 EOC
89 output_tables (\%asciimap, @lines);
90 print <<'EOC';
91 #else
92 /* EBCDIC (or bust) */
93 EOC
94 output_tables (\%ebcdicmap, @lines);
95 print "#endif\n";
96
97 __DATA__
98 #Symbol nooone  nocsum  size
99 c                       char
100 C                       unsigned char
101 U                       char
102 s!                      short
103 s                       =SIZE16
104 S!                      unsigned short
105 v                       =SIZE16
106 n                       =SIZE16
107 S                       =SIZE16
108 v!                      =SIZE16
109 n!                      =SIZE16
110 i                       int
111 i!                      int
112 I                       unsigned int
113 I!                      unsigned int
114 j                       =IVSIZE
115 J                       =UVSIZE
116 l!                      long
117 l                       =SIZE32
118 L!                      unsigned long
119 V                       =SIZE32
120 N                       =SIZE32
121 V!                      =SIZE32
122 N!                      =SIZE32
123 L                       =SIZE32
124 p       *       *       char *
125 w               *       char
126 q                       Quad_t  HAS_QUAD
127 Q                       Uquad_t HAS_QUAD
128 f                       float
129 d                       double
130 F                       =NVSIZE
131 D                       =LONG_DOUBLESIZE        HAS_LONG_DOUBLE USE_LONG_DOUBLE