Commit | Line | Data |
---|---|---|
78d46eaa NC |
1 | #!/usr/bin/perl -w |
2 | # I'm assuming that you're running this on some kind of ASCII system, but | |
d8a38289 | 3 | # it will generate EBCDIC too. (TODO) |
78d46eaa NC |
4 | use strict; |
5 | use Encode; | |
6b05cb81 | 6 | require 'regen/regen_lib.pl'; |
78d46eaa | 7 | |
a7a3cfaa TH |
8 | sub make_text { |
9 | my ($chrmap, $letter, $unpredictable, $nocsum, $size, $condition) = @_; | |
10 | my $text = " /* $letter */ $size"; | |
11 | $text .= " | PACK_SIZE_UNPREDICTABLE" if $unpredictable; | |
12 | $text .= " | PACK_SIZE_CANNOT_CSUM" if $nocsum; | |
13 | $text .= ","; | |
78d46eaa | 14 | |
a7a3cfaa | 15 | if ($condition) { |
a7a3cfaa TH |
16 | $text = "#if $condition |
17 | $text | |
18 | #else | |
19 | 0, | |
20 | #endif"; | |
21 | } | |
22 | return $text; | |
78d46eaa NC |
23 | } |
24 | ||
a7a3cfaa TH |
25 | sub 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 |
61 | my @lines = grep { |
62 | s/#.*//; | |
63 | /\S/; | |
64 | } <DATA>; | |
78d46eaa | 65 | |
a7a3cfaa | 66 | my %asciimap = map {chr $_, chr $_} 0..255; |
43fdf692 KW |
67 | |
68 | # Currently, all things generated by this on EBCDIC are alphabetics, whose | |
69 | # positions are all the same regardless of code page, so any EBCDIC encoding | |
70 | # will work; just choose one | |
a7a3cfaa | 71 | my %ebcdicmap = map {chr $_, Encode::encode("posix-bc", chr $_)} 0..255; |
78d46eaa | 72 | |
4ad176ef | 73 | my $fh = open_new('packsizetables.inc', '>', { by => $0, from => 'its data'}); |
78d46eaa | 74 | |
6b05cb81 | 75 | print $fh <<"EOC"; |
a7a3cfaa TH |
76 | #if TYPE_IS_SHRIEKING != 0x100 |
77 | ++++shriek offset should be 256 | |
78 | #endif | |
79 | ||
80 | typedef U8 packprops_t; | |
78d46eaa NC |
81 | #if 'J'-'I' == 1 |
82 | /* ASCII */ | |
a7a3cfaa | 83 | @{[make_tables (\%asciimap, @lines)]} |
78d46eaa NC |
84 | #else |
85 | /* EBCDIC (or bust) */ | |
a7a3cfaa TH |
86 | @{[make_tables (\%ebcdicmap, @lines)]} |
87 | #endif | |
78d46eaa | 88 | EOC |
78d46eaa | 89 | |
6b05cb81 NC |
90 | read_only_bottom_close_and_rename($fh); |
91 | ||
78d46eaa | 92 | __DATA__ |
f337b084 TH |
93 | #Symbol unpredictable |
94 | # nocsum size | |
78d46eaa | 95 | c char |
e2add64b | 96 | C unsigned char |
f337b084 TH |
97 | W * unsigned char |
98 | U * char | |
78d46eaa NC |
99 | s! short |
100 | s =SIZE16 | |
101 | S! unsigned short | |
102 | v =SIZE16 | |
103 | n =SIZE16 | |
104 | S =SIZE16 | |
f8e5a5db NC |
105 | v! =SIZE16 |
106 | n! =SIZE16 | |
78d46eaa NC |
107 | i int |
108 | i! int | |
109 | I unsigned int | |
110 | I! unsigned int | |
111 | j =IVSIZE | |
112 | J =UVSIZE | |
113 | l! long | |
114 | l =SIZE32 | |
115 | L! unsigned long | |
116 | V =SIZE32 | |
117 | N =SIZE32 | |
f8e5a5db NC |
118 | V! =SIZE32 |
119 | N! =SIZE32 | |
78d46eaa | 120 | L =SIZE32 |
a72c271b | 121 | p * char * |
f337b084 | 122 | w * * char |
1640b983 NC |
123 | q Quad_t IVSIZE >= 8 |
124 | Q Uquad_t IVSIZE >= 8 | |
78d46eaa NC |
125 | f float |
126 | d double | |
127 | F =NVSIZE | |
1640b983 | 128 | D =LONG_DOUBLESIZE defined(HAS_LONG_DOUBLE) && defined(USE_LONG_DOUBLE) |