Merge trunk version 194962 into gupc branch.
[official-gcc.git] / libgupc / gen-inline-libgupc.pl
blob32665d42959a51f8aed373d18dffa8444c5c2d16
1 #!/usr/bin/perl -w
2 use strict;
5 # usage: gen-gupc-inline-lib template.in [input-file ...]
7 # Create a file from template.in that implements selectively inline'd
8 # GUPC runtime routines.
10 # The files are read first. This processes lines with the
11 # following syntax. Note, the '//' sequence must begin in column one.
13 # The following lines are processed in the input files.
15 # //begin <name> Collect the following lines up to the matching 'end'
16 # and append them to an internal buffer named <name>
17 # Nested Begin ... end brackets are _not_ supported.
19 # //end <name> End the collection of items into the buffer named <name>.
21 # The following lines are processed in the template file.
23 # //include <name> Expands into the contents of the buffer named <name>.
25 # //inline Expands into:
26 # "__attribute__((__always_inline__)) static inline".
27 # May appear inside a begin ... end bracket.
29 # The expanded text is written to stdout.
31 my $nargs = scalar @ARGV;
32 die "missing first (template file) arg" if $nargs < 1;
33 for (@ARGV)
35 die "file not found: $_" if ! -f;
37 my $tfile = shift @ARGV;
38 open TEMPLATE, "<$tfile" or die "can't open template file: $tfile";
39 my %bufs = ();
40 my $buf;
41 my $input_file = '';
42 # Read the input files listed in @ARGV
43 while (<>)
45 if ($input_file ne $ARGV)
47 !defined ($buf)
48 or die "missing '//end ${buf}': $input_file\n";
49 $input_file = $ARGV;
51 chomp;
52 if (m{^//begin\s+(\w+)\s*$})
54 die "nested buffers not supported,"
55 . " last buffer is '$buf': $input_file\n"
56 if defined ($buf);
57 $buf = $1;
58 next;
60 elsif (m{^//end\s+(\w+)\s*$})
62 my $endbuf = $1;
63 die "no matching begin for buffer '$endbuf': $input_file\n"
64 if !defined($buf);
65 die "buffer mismatch, '$buf' != '$endbuf': $input_file\n"
66 if $buf ne $endbuf;
67 $buf = undef;
68 next;
70 if (defined ($buf))
72 $bufs{$buf} .= "$_\n";
75 !defined ($buf)
76 or die "missing '//end ${buf}': $input_file\n";
77 my $inline_attr = "__attribute__((__always_inline__))\nstatic inline";
78 for $buf (keys %bufs)
80 $bufs{$buf} =~ s{^//inline\s*$}{$inline_attr}mg;
82 while (<TEMPLATE>)
84 chomp;
85 if (m{^//include\s+(\w+)\s*$})
87 $buf = $1;
88 die "unknown buffer: $buf" if !exists ($bufs{$buf});
89 print $bufs{$buf};
90 next;
92 elsif (m{^//inline\s*$})
94 print "$inline_attr\n";
95 next;
97 print "$_\n";
99 close TEMPLATE;