re PR libgomp/25877 (team.c:269: warning: implicit declaration of function 'alloca')
[gcc.git] / libgomp / mkomp_h.pl
1 #!/usr/bin/perl -w
2 # Copyright (C) 2005 Free Software Foundation, Inc.
3 # Contributed by Richard Henderson <rth@redhat.com>.
4 #
5 # This file is part of the GNU OpenMP Library (libgomp).
6 #
7 # Libgomp is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation; either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15 # more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with libgomp; see the file COPYING.LIB. If not, write to the
19 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 # MA 02110-1301, USA.
21 #
22 # As a special exception, if you link this library with other files, some
23 # of which are compiled with GCC, to produce an executable, this library
24 # does not by itself cause the resulting executable to be covered by the
25 # GNU General Public License. This exception does not however invalidate
26 # any other reasons why the executable file might be covered by the GNU
27 # General Public License.
28
29 # This file fills in the blanks for omp.h.in, computing the size and
30 # alignment of the lock types that we export. We do this to avoid
31 # polluting the namespace with e.g. pthreads declarations.
32
33 $COMPILE = $ARGV[0];
34 $INFILE = $ARGV[1];
35 $OUTFILE = $ARGV[2];
36
37 $HEADER = "#include \"omp-lock.h\"\n";
38
39 # configure might put libtool specific options into $COMPILE.
40 $COMPILE =~ s/ -Wc,/ /g;
41
42 # Close stderr in order to discard compiler errors. Which we expect apleanty.
43 close STDERR;
44
45 # Return true if the boolean expression evaluates true at compile-time.
46 sub compile_test {
47 my $expr = shift;
48
49 open GCC, "|$COMPILE -fsyntax-only -xc -";
50 print GCC $HEADER;
51 print GCC "char test[($expr) ? 1 : -1];\n";
52 return close GCC;
53 }
54
55 # Return a number guaranteed to be larger than the integer epression.
56 sub upper_bound {
57 use integer;
58 my $expr = shift;
59 my $max = 9;
60
61 while (compile_test("($expr) >= $max")) {
62 $max = $max * 2;
63 }
64
65 return $max;
66 }
67
68 # Return an exact number for the integer expression.
69 sub binary_search {
70 use integer;
71 my $expr = shift;
72 my $low = 1;
73 my $high = upper_bound($expr);
74
75 while ($low < $high) {
76 my $mid = ($high + $low + 1) / 2;
77 if (compile_test("($expr) >= $mid")) {
78 $low = $mid;
79 } else {
80 $high = $mid - 1;
81 }
82 }
83
84 return $low;
85 }
86
87 # Apply OP to TYPE, where OP is either sizeof or __alignof.
88 sub resolve {
89 my $op = shift;
90 my $type = shift;
91
92 return binary_search("$op($type)");
93 }
94
95 # Find all the constants we need.
96 $sizeof_omp_lock_t = resolve ("sizeof", "omp_lock_t");
97 $alignof_omp_lock_t = resolve ("__alignof", "omp_lock_t");
98 $sizeof_omp_nest_lock_t = resolve ("sizeof", "omp_nest_lock_t");
99 $alignof_omp_nest_lock_t = resolve ("__alignof", "omp_nest_lock_t");
100 $omp_lock_kind = $sizeof_omp_lock_t;
101 $omp_nest_lock_kind = $sizeof_omp_nest_lock_t;
102 if ($sizeof_omp_lock_t >= 8 || $alignof_omp_lock_t > $sizeof_omp_lock_t) {
103 $omp_lock_kind = 8;
104 }
105 if ($sizeof_omp_nest_lock_t >= 8 || $alignof_omp_nest_lock_t > $sizeof_omp_nest_lock_t) {
106 $omp_nest_lock_kind = 8;
107 }
108
109 # Edit the input template into the output.
110 open IN, "<", $INFILE;
111 open OUT, ">", $OUTFILE;
112 while (<IN>) {
113 s/OMP_LOCK_SIZE/$sizeof_omp_lock_t/o;
114 s/OMP_LOCK_ALIGN/$alignof_omp_lock_t/o;
115 s/OMP_NEST_LOCK_SIZE/$sizeof_omp_nest_lock_t/o;
116 s/OMP_NEST_LOCK_ALIGN/$alignof_omp_nest_lock_t/o;
117 s/OMP_LOCK_KIND/$omp_lock_kind/o;
118 s/OMP_NEST_LOCK_KIND/$omp_nest_lock_kind/o;
119 print OUT;
120 }
121
122 close OUT;