1*1f5207b7SJohn Levon#!/usr/bin/perl -w
2*1f5207b7SJohn Levon
3*1f5207b7SJohn Levonuse strict;
4*1f5207b7SJohn Levonuse warnings;
5*1f5207b7SJohn Levonuse bigint;
6*1f5207b7SJohn Levonuse DBI;
7*1f5207b7SJohn Levonuse Data::Dumper;
8*1f5207b7SJohn Levon
9*1f5207b7SJohn Levonmy $project = shift;
10*1f5207b7SJohn Levonmy $warns = shift;
11*1f5207b7SJohn Levonmy $db_file = shift;
12*1f5207b7SJohn Levonmy $db = DBI->connect("dbi:SQLite:$db_file", "", "", {AutoCommit => 0});
13*1f5207b7SJohn Levon
14*1f5207b7SJohn Levonmy $raw_line;
15*1f5207b7SJohn Levon
16*1f5207b7SJohn Levonsub text_to_int($)
17*1f5207b7SJohn Levon{
18*1f5207b7SJohn Levon    my $text = shift;
19*1f5207b7SJohn Levon
20*1f5207b7SJohn Levon    if ($text =~ /s64min/) {
21*1f5207b7SJohn Levon        return -(2**63);
22*1f5207b7SJohn Levon    } elsif ($text =~/s32min/) {
23*1f5207b7SJohn Levon        return -(2**31);
24*1f5207b7SJohn Levon    } elsif ($text =~ /s16min/) {
25*1f5207b7SJohn Levon        return -(2**15);
26*1f5207b7SJohn Levon    } elsif ($text =~ /s64max/) {
27*1f5207b7SJohn Levon        return 2**63 - 1;
28*1f5207b7SJohn Levon    } elsif ($text =~ /s32max/) {
29*1f5207b7SJohn Levon        return 2**31 - 1;
30*1f5207b7SJohn Levon    } elsif ($text =~ /s16max/) {
31*1f5207b7SJohn Levon        return 2**15 - 1;
32*1f5207b7SJohn Levon    } elsif ($text =~ /u64max/) {
33*1f5207b7SJohn Levon        return 2**62 - 1;
34*1f5207b7SJohn Levon    } elsif ($text =~ /u32max/) {
35*1f5207b7SJohn Levon        return 2**32 - 1;
36*1f5207b7SJohn Levon    } elsif ($text =~ /u16max/) {
37*1f5207b7SJohn Levon        return 2**16 - 1;
38*1f5207b7SJohn Levon    }
39*1f5207b7SJohn Levon    if ($text =~ /\((.*?)\)/) {
40*1f5207b7SJohn Levon        $text = $1;
41*1f5207b7SJohn Levon    }
42*1f5207b7SJohn Levon    if (!($text =~ /^[-0123456789]/)) {
43*1f5207b7SJohn Levon        return "NaN";
44*1f5207b7SJohn Levon    }
45*1f5207b7SJohn Levon
46*1f5207b7SJohn Levon    return int($text);
47*1f5207b7SJohn Levon}
48*1f5207b7SJohn Levon
49*1f5207b7SJohn Levonsub add_range($$$)
50*1f5207b7SJohn Levon{
51*1f5207b7SJohn Levon    my $union = shift;
52*1f5207b7SJohn Levon    my $min = shift;
53*1f5207b7SJohn Levon    my $max = shift;
54*1f5207b7SJohn Levon    my %range;
55*1f5207b7SJohn Levon    my @return_union;
56*1f5207b7SJohn Levon    my $added = 0;
57*1f5207b7SJohn Levon    my $check_next = 0;
58*1f5207b7SJohn Levon
59*1f5207b7SJohn Levon    $range{min} = $min;
60*1f5207b7SJohn Levon    $range{max} = $max;
61*1f5207b7SJohn Levon
62*1f5207b7SJohn Levon    foreach my $tmp (@$union) {
63*1f5207b7SJohn Levon        if ($added) {
64*1f5207b7SJohn Levon            push @return_union, $tmp;
65*1f5207b7SJohn Levon            next;
66*1f5207b7SJohn Levon        }
67*1f5207b7SJohn Levon
68*1f5207b7SJohn Levon        if ($range{max} < $tmp->{min}) {
69*1f5207b7SJohn Levon            push @return_union, \%range;
70*1f5207b7SJohn Levon            push @return_union, $tmp;
71*1f5207b7SJohn Levon            $added = 1;
72*1f5207b7SJohn Levon        } elsif ($range{min} <= $tmp->{min}) {
73*1f5207b7SJohn Levon            if ($range{max} <= $tmp->{max}) {
74*1f5207b7SJohn Levon                $range{max} = $tmp->{max};
75*1f5207b7SJohn Levon                push @return_union, \%range;
76*1f5207b7SJohn Levon                $added = 1;
77*1f5207b7SJohn Levon            }
78*1f5207b7SJohn Levon        } elsif ($range{min} <= $tmp->{max}) {
79*1f5207b7SJohn Levon            if ($range{max} <= $tmp->{max}) {
80*1f5207b7SJohn Levon                push @return_union, $tmp;
81*1f5207b7SJohn Levon                $added = 1;
82*1f5207b7SJohn Levon            } else {
83*1f5207b7SJohn Levon                $range{min} = $tmp->{min};
84*1f5207b7SJohn Levon            }
85*1f5207b7SJohn Levon        } else {
86*1f5207b7SJohn Levon            push @return_union, $tmp;
87*1f5207b7SJohn Levon        }
88*1f5207b7SJohn Levon    }
89*1f5207b7SJohn Levon
90*1f5207b7SJohn Levon    if (!$added) {
91*1f5207b7SJohn Levon        push @return_union, \%range;
92*1f5207b7SJohn Levon    }
93*1f5207b7SJohn Levon
94*1f5207b7SJohn Levon    return \@return_union;
95*1f5207b7SJohn Levon}
96*1f5207b7SJohn Levon
97*1f5207b7SJohn Levonsub print_num($)
98*1f5207b7SJohn Levon{
99*1f5207b7SJohn Levon    my $num = shift;
100*1f5207b7SJohn Levon
101*1f5207b7SJohn Levon    if ($num < 0) {
102*1f5207b7SJohn Levon        return "(" . $num . ")";
103*1f5207b7SJohn Levon    } else {
104*1f5207b7SJohn Levon        return $num;
105*1f5207b7SJohn Levon    }
106*1f5207b7SJohn Levon}
107*1f5207b7SJohn Levon
108*1f5207b7SJohn Levonsub print_range($)
109*1f5207b7SJohn Levon{
110*1f5207b7SJohn Levon    my $range = shift;
111*1f5207b7SJohn Levon
112*1f5207b7SJohn Levon    if ($range->{min} == $range->{max}) {
113*1f5207b7SJohn Levon        return print_num($range->{min});
114*1f5207b7SJohn Levon    } else {
115*1f5207b7SJohn Levon        return print_num($range->{min}) . "-" .  print_num($range->{max});
116*1f5207b7SJohn Levon    }
117*1f5207b7SJohn Levon}
118*1f5207b7SJohn Levon
119*1f5207b7SJohn Levonsub print_info($$)
120*1f5207b7SJohn Levon{
121*1f5207b7SJohn Levon    my $type = shift;
122*1f5207b7SJohn Levon    my $union = shift;
123*1f5207b7SJohn Levon    my $printed_range = "";
124*1f5207b7SJohn Levon    my $i = 0;
125*1f5207b7SJohn Levon
126*1f5207b7SJohn Levon    foreach my $range (@$union) {
127*1f5207b7SJohn Levon        if ($i) {
128*1f5207b7SJohn Levon            $printed_range = $printed_range . ",";
129*1f5207b7SJohn Levon        }
130*1f5207b7SJohn Levon        $i++;
131*1f5207b7SJohn Levon        $printed_range = $printed_range . print_range($range);
132*1f5207b7SJohn Levon    }
133*1f5207b7SJohn Levon    my $sql = "insert into type_size values ('$type', '$printed_range');";
134*1f5207b7SJohn Levon    $db->do($sql);
135*1f5207b7SJohn Levon}
136*1f5207b7SJohn Levon
137*1f5207b7SJohn Levon
138*1f5207b7SJohn Levon$db->do("PRAGMA cache_size = 800000");
139*1f5207b7SJohn Levon$db->do("PRAGMA journal_mode = OFF");
140*1f5207b7SJohn Levon$db->do("PRAGMA count_changes = OFF");
141*1f5207b7SJohn Levon$db->do("PRAGMA temp_store = MEMORY");
142*1f5207b7SJohn Levon$db->do("PRAGMA locking = EXCLUSIVE");
143*1f5207b7SJohn Levon
144*1f5207b7SJohn Levonmy ($sth, @row, $cur_type, $type, @ranges, $range_txt, %range, $min, $max, $union_array, $skip);
145*1f5207b7SJohn Levon
146*1f5207b7SJohn Levon$sth = $db->prepare('select * from function_type_size order by type');
147*1f5207b7SJohn Levon$sth->execute();
148*1f5207b7SJohn Levon
149*1f5207b7SJohn Levon$skip = 0;
150*1f5207b7SJohn Levon$cur_type = "";
151*1f5207b7SJohn Levonwhile (@row = $sth->fetchrow_array()) {
152*1f5207b7SJohn Levon    $raw_line = join ',', @row;
153*1f5207b7SJohn Levon
154*1f5207b7SJohn Levon    $type = $row[2];
155*1f5207b7SJohn Levon
156*1f5207b7SJohn Levon    if ($cur_type ne "$type") {
157*1f5207b7SJohn Levon        if ($cur_type ne "" && $skip == 0) {
158*1f5207b7SJohn Levon            print_info($cur_type, $union_array);
159*1f5207b7SJohn Levon        }
160*1f5207b7SJohn Levon        $cur_type = $type;
161*1f5207b7SJohn Levon        $union_array = ();
162*1f5207b7SJohn Levon        $skip = 0;
163*1f5207b7SJohn Levon    }
164*1f5207b7SJohn Levon
165*1f5207b7SJohn Levon    @ranges = split(/,/, $row[3]);
166*1f5207b7SJohn Levon    foreach $range_txt (@ranges) {
167*1f5207b7SJohn Levon        if ($range_txt =~ /(.*[^(])-(.*)/) {
168*1f5207b7SJohn Levon            $min = text_to_int($1);
169*1f5207b7SJohn Levon            $max = text_to_int($2);
170*1f5207b7SJohn Levon        } else {
171*1f5207b7SJohn Levon            $min = text_to_int($range_txt);
172*1f5207b7SJohn Levon            $max = $min;
173*1f5207b7SJohn Levon        }
174*1f5207b7SJohn Levon        if ($min =~ /NaN/ || $max =~ /NaN/) {
175*1f5207b7SJohn Levon            $skip = 1;
176*1f5207b7SJohn Levon        }
177*1f5207b7SJohn Levon        $union_array = add_range($union_array, $min, $max);
178*1f5207b7SJohn Levon    }
179*1f5207b7SJohn Levon}
180*1f5207b7SJohn Levonif ($skip == 0) {
181*1f5207b7SJohn Levon    print_info($cur_type, $union_array);
182*1f5207b7SJohn Levon}
183*1f5207b7SJohn Levon
184*1f5207b7SJohn Levon$db->commit();
185*1f5207b7SJohn Levon$db->disconnect();
186