Insert stuff onto lists in a sorted order.
[smatch.git] / cgcc
blob89adbed51da90f14884fe69b268b30a64e596e42
1 #!/usr/bin/perl -w
2 # -----------------------------------------------------------------------------
4 my $cc = $ENV{'REAL_CC'} || 'cc';
5 my $check = $ENV{'CHECK'} || 'sparse';
7 my $m32 = 0;
8 my $m64 = 0;
9 my $has_specs = 0;
10 my $gendeps = 0;
11 my $do_check = 0;
12 my $do_compile = 1;
13 my $verbose = 0;
15 foreach (@ARGV) {
16 # Look for a .c file. We don't want to run the checker on .o or .so files
17 # in the link run. (This simplistic check knows nothing about options
18 # with arguments, but it seems to do the job.)
19 $do_check = 1 if /^[^-].*\.c$/;
21 # Ditto for stdin.
22 $do_check = 1 if $_ eq '-';
24 $m32 = 1 if /^-m32$/;
25 $m64 = 1 if /^-m64$/;
26 $gendeps = 1 if /^-M$/;
28 if (/^-specs=(.*)$/) {
29 $check .= &add_specs ($1);
30 $has_specs = 1;
31 next;
34 if ($_ eq '-no-compile') {
35 $do_compile = 0;
36 next;
39 # If someone adds "-E", don't pre-process twice.
40 $do_compile = 0 if $_ eq '-E';
42 $verbose = 1 if $_ eq '-v';
44 my $this_arg = ' ' . &quote_arg ($_);
45 $cc .= $this_arg unless &check_only_option ($_);
46 $check .= $this_arg unless &cc_only_option ($_);
49 if ($gendeps) {
50 $do_compile = 1;
51 $do_check = 0;
54 if ($do_check) {
55 if (!$has_specs) {
56 $check .= &add_specs ('host_arch_specs');
57 $check .= &add_specs ('host_os_specs');
59 print "$check\n" if $verbose;
60 if ($do_compile) {
61 system ($check);
62 } else {
63 exec ($check);
67 if ($do_compile) {
68 print "$cc\n" if $verbose;
69 exec ($cc);
72 exit 0;
74 # -----------------------------------------------------------------------------
75 # Check if an option is for "check" only.
77 sub check_only_option {
78 my ($arg) = @_;
79 return 1 if $arg =~ /^-W(no-?)?(default-bitfield-sign|one-bit-signed-bitfield|cast-truncate|bitwise|typesign|context|undef|ptr-subtraction-blows|cast-to-as|decl|transparent-union|address-space|enum-mismatch|do-while|old-initializer|non-pointer-null|paren-string|return-void)$/;
80 return 1 if $arg =~ /^-v(no-?)?(entry|dead)$/;
81 return 0;
84 # -----------------------------------------------------------------------------
85 # Check if an option is for "cc" only.
87 sub cc_only_option {
88 my ($arg) = @_;
89 # -Wall turns on all Sparse warnings, including experimental and noisy
90 # ones. Don't include it just because a project wants to pass -Wall to cc.
91 # If you really want cgcc to run sparse with -Wall, use
92 # CHECK="sparse -Wall".
93 return 1 if $arg =~ /^-Wall$/;
94 return 0;
97 # -----------------------------------------------------------------------------
98 # Simple arg-quoting function. Just adds backslashes when needed.
100 sub quote_arg {
101 my ($arg) = @_;
102 return "''" if $arg eq '';
103 return join ('',
104 map {
105 m|^[-a-zA-Z0-9._/,=]+$| ? $_ : "\\" . $_;
106 } (split (//, $arg)));
109 # -----------------------------------------------------------------------------
111 sub integer_types {
112 my ($char,@dummy) = @_;
114 my %pow2m1 =
115 (8 => '127',
116 16 => '32767',
117 32 => '2147483647',
118 64 => '9223372036854775807',
120 my @types = (['SCHAR',''], ['SHRT',''], ['INT',''], ['LONG','L'], ['LONG_LONG','LL']);
122 my $result = " -D__CHAR_BIT__=$char";
123 while (@types) {
124 my $bits = shift @_;
125 my ($name,$suffix) = @{ shift @types };
126 die "$0: weird number of bits." unless exists $pow2m1{$bits};
127 $result .= " -D__${name}_MAX__=" . $pow2m1{$bits} . $suffix;
129 return $result;
132 # -----------------------------------------------------------------------------
134 sub float_types {
135 my ($has_inf,$has_qnan,$dec_dig,@bitsizes) = @_;
136 my $result = " -D__FLT_RADIX__=2";
137 $result .= " -D__FINITE_MATH_ONLY__=" . ($has_inf || $has_qnan ? '0' : '1');
138 $result .= " -D__DECIMAL_DIG__=$dec_dig";
140 my %constants =
141 (24 =>
143 'MIN' => '1.17549435e-38',
144 'MAX' => '3.40282347e+38',
145 'EPSILON' => '1.19209290e-7',
146 'DENORM_MIN' => '1.40129846e-45',
148 53 =>
150 'MIN' => '2.2250738585072014e-308',
151 'MAX' => '1.7976931348623157e+308',
152 'EPSILON' => '2.2204460492503131e-16',
153 'DENORM_MIN' => '4.9406564584124654e-324',
155 64 =>
157 'MIN' => '3.36210314311209350626e-4932',
158 'MAX' => '1.18973149535723176502e+4932',
159 'EPSILON' => '1.08420217248550443401e-19',
160 'DENORM_MIN' => '3.64519953188247460253e-4951',
162 113 =>
164 'MIN' => '3.36210314311209350626267781732175260e-4932',
165 'MAX' => '1.18973149535723176508575932662800702e+4932',
166 'EPSILON' => '1.92592994438723585305597794258492732e-34',
167 'DENORM_MIN' => '6.47517511943802511092443895822764655e-4966',
171 my @types = (['FLT','F'], ['DBL',''], ['LDBL','L']);
172 while (@types) {
173 my ($mant_bits,$exp_bits) = @{ shift @bitsizes };
174 my ($name,$suffix) = @{ shift @types };
176 my $h = $constants{$mant_bits};
177 die "$0: weird number of mantissa bits." unless $h;
179 my $mant_dig = int (($mant_bits - 1) * log (2) / log (10));
180 my $max_exp = 1 << ($exp_bits - 1);
181 my $min_exp = 3 - $max_exp;
182 my $max_10_exp = int ($max_exp * log (2) / log (10));
183 my $min_10_exp = -int (-$min_exp * log (2) / log (10));
185 $result .= " -D__${name}_MANT_DIG__=$mant_bits";
186 $result .= " -D__${name}_DIG__=$mant_dig";
187 $result .= " -D__${name}_MIN_EXP__='($min_exp)'";
188 $result .= " -D__${name}_MAX_EXP__=$max_exp";
189 $result .= " -D__${name}_MIN_10_EXP__='($min_10_exp)'";
190 $result .= " -D__${name}_MAX_10_EXP__=$max_10_exp";
191 $result .= " -D__${name}_HAS_INFINITY__=" . ($has_inf ? '1' : '0');
192 $result .= " -D__${name}_HAS_QUIET_NAN__=" . ($has_qnan ? '1' : '0');;
194 foreach my $inf (sort keys %$h) {
195 $result .= " -D__${name}_${inf}__=" . $h->{$inf} . $suffix;
198 return $result;
201 # -----------------------------------------------------------------------------
203 sub define_size_t {
204 my ($text) = @_;
205 # We have to undef in order to override check's internal definition.
206 return ' -U__SIZE_TYPE__ ' . &quote_arg ("-D__SIZE_TYPE__=$text");
209 # -----------------------------------------------------------------------------
211 sub add_specs {
212 my ($spec) = @_;
213 if ($spec eq 'sunos') {
214 return &add_specs ('unix') .
215 ' -D__sun__=1 -D__sun=1 -Dsun=1' .
216 ' -D__svr4__=1 -DSVR4=1' .
217 ' -D__STDC__=0' .
218 ' -D_REENTRANT' .
219 ' -D_SOLARIS_THREADS' .
220 ' -DNULL="((void *)0)"';
221 } elsif ($spec eq 'linux') {
222 return &add_specs ('unix') .
223 ' -D__linux__=1 -D__linux=1 -Dlinux=linux';
224 } elsif ($spec eq 'unix') {
225 return ' -Dunix=1 -D__unix=1 -D__unix__=1';
226 } elsif ( $spec =~ /^cygwin/) {
227 return &add_specs ('unix') .
228 ' -D__CYGWIN__=1 -D__CYGWIN32__=1' .
229 " -D'_cdecl=__attribute__((__cdecl__))'" .
230 " -D'__cdecl=__attribute__((__cdecl__))'" .
231 " -D'_stdcall=__attribute__((__stdcall__))'" .
232 " -D'__stdcall=__attribute__((__stdcall__))'" .
233 " -D'_fastcall=__attribute__((__fastcall__))'" .
234 " -D'__fastcall=__attribute__((__fastcall__))'" .
235 " -D'__declspec(x)=__attribute__((x))'";
236 } elsif ($spec eq 'i86') {
237 return (' -Di386=1 -D__i386=1 -D__i386__=1' .
238 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
239 &float_types (1, 1, 21, [24,8], [53,11], [64,15]) .
240 &define_size_t ($m64 ? "long unsigned int" : "unsigned int"));
241 } elsif ($spec eq 'sparc') {
242 return (' -Dsparc=1 -D__sparc=1 -D__sparc__=1' .
243 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
244 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
245 &define_size_t ($m64 ? "long unsigned int" : "unsigned int"));
246 } elsif ($spec eq 'x86_64') {
247 return (' -Dx86_64=1 -D__x86_64=1 -D__x86_64__=1' .
248 &integer_types (8, 16, 32, $m32 ? 32 : 64, 64) .
249 &float_types (1, 1, 33, [24,8], [53,11], [113,15]) .
250 &define_size_t ($m32 ? "unsigned int" : "long unsigned int"));
251 } elsif ($spec eq 'ppc') {
252 return (' -D__powerpc__=1 -D_BIG_ENDIAN -D_STRING_ARCH_unaligned=1' .
253 &integer_types (8, 16, 32, $m64 ? 64 : 32, 64) .
254 &float_types (1, 1, 21, [24,8], [53,11], [113,15]) .
255 &define_size_t ($m64 ? "long unsigned int" : "unsigned int"));
256 } elsif ($spec eq 'host_os_specs') {
257 my $os = `uname -s`;
258 chomp $os;
259 return &add_specs (lc $os);
260 } elsif ($spec eq 'host_arch_specs') {
261 my $arch = `uname -m`;
262 chomp $arch;
263 if ($arch =~ /^(i.?86|athlon)$/i) {
264 return &add_specs ('i86');
265 } elsif ($arch =~ /^(sun4u)$/i) {
266 return &add_specs ('sparc');
267 } elsif ($arch =~ /^(x86_64)$/i) {
268 return &add_specs ('x86_64');
269 } elsif ($arch =~ /^(ppc)$/i) {
270 return &add_specs ('ppc');
272 } else {
273 die "$0: invalid specs: $spec\n";
277 # -----------------------------------------------------------------------------