fix perl warning
[libfirm] / ir / be / scripts / generate_machine.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
5 #
6 # This file is part of libFirm.
7 #
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
12 #
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
16 #
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE.
20 #
21
22 # This script generates the data structures for the machine description.
23 # Creation: 2006/10/20
24 # $Id$
25
26 use strict;
27 use Data::Dumper;
28
29 my $specfile   = $ARGV[0];
30 my $target_dir = $ARGV[1];
31
32 our $arch;
33 our %cpu;
34 our %vliw;
35
36 # include spec file
37
38 my $return;
39
40 no strict "subs";
41 unless ($return = do $specfile) {
42         die "Fatal error: couldn't parse $specfile: $@" if $@;
43         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
44         die "Fatal error: couldn't run $specfile"       unless $return;
45 }
46 use strict "subs";
47
48 my $target_c = $target_dir."/gen_".$arch."_machine.c";
49 my $target_h = $target_dir."/gen_".$arch."_machine.h";
50
51 # stacks for output
52 my @obst_unit_tp_defs;     # stack for execution unit type defines
53 my @obst_unit_defs;        # stack for execution unit defines
54 my @obst_init;             # stack for cpu description initialization
55 my @obst_execunits;        # stack for execunit variables
56 my @obst_execunits_header; # stack for execunit variables
57 my @obst_init_unit_types;  # stack for unit type variable init
58
59 my $bundle_size       = exists($vliw{"bundle_size"})       ? $vliw{"bundle_size"} : 3;
60 my $bundles_per_cycle = exists($vliw{"bundles_per_cycle"}) ? $vliw{"bundles_per_cycle"} : 1;
61
62 my $tmp            = uc($arch);
63 my $idx            = 0;
64 my $has_desc       = defined(%cpu);
65 my $num_unit_types = scalar(keys(%cpu));
66
67 if ($has_desc) {
68         push(@obst_unit_tp_defs, "/* enum for execution unit types */\n");
69         push(@obst_unit_tp_defs, "enum $arch\_execunit_tp_vals {\n");
70 }
71
72 foreach my $unit_type (keys(%cpu)) {
73         my $tp_name   = "$tmp\_EXECUNIT_TP_$unit_type";
74         my @cur_tp    = @{ $cpu{"$unit_type"} };
75         my $num_units = scalar(@cur_tp) - 1;
76
77         push(@obst_init_unit_types, "\t{ $num_units, ".shift(@cur_tp).", \"$unit_type\", $arch\_execution_units_$unit_type },\n");
78         push(@obst_execunits, "be_execution_unit_t $arch\_execution_units_".$unit_type."[$num_units];\n");
79         push(@obst_execunits_header, "extern be_execution_unit_t $arch\_execution_units_".$unit_type."[$num_units];\n");
80
81         push(@obst_unit_tp_defs, "\t$tp_name,\n");
82         push(@obst_init, "\n\t\t/* init of execution unit type $tp_name */\n");
83         push(@obst_init, "\t\tcur_unit_tp = &$arch\_execution_unit_types[$tp_name];\n");
84
85         push(@obst_unit_defs, "/* enum for execution units of type $unit_type */\n");
86         push(@obst_unit_defs, "enum $arch\_execunit_tp_$unit_type\_vals {\n");
87
88         foreach my $unit (@cur_tp) {
89                 my $unit_name = "$tp_name\_$unit";
90
91                 push(@obst_unit_defs, "\t$unit_name,\n");
92                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].tp   = cur_unit_tp;\n");
93                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].name = \"$unit\";\n");
94         }
95         push(@obst_unit_defs, "};\n\n");
96 }
97
98 push(@obst_unit_tp_defs, "};\n\n") if ($has_desc);
99
100 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
101
102 my $creation_time = localtime(time());
103
104 print OUT<<EOF;
105 /**
106  * \@file
107  * \@brief     Function prototypes for the machine description.
108  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
109  *            Edit $specfile instead.
110  *            created by: $0 $specfile $target_dir
111  * \@date      $creation_time
112  */
113 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_MACHINE_H
114 #define FIRM_BE_${tmp}_GEN_${tmp}_MACHINE_H
115
116 #include "bemachine.h"
117
118 /**
119  * Returns the $arch machine description.
120  */
121 const be_machine_t *$arch\_init_machine_description(void);
122
123 EOF
124
125 print OUT @obst_execunits_header, "\n";
126 print OUT @obst_unit_tp_defs;
127 print OUT @obst_unit_defs;
128
129 print OUT<<EOF;
130
131 #endif
132
133 EOF
134
135 close(OUT);
136
137 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
138
139 $creation_time = localtime(time());
140
141 print OUT<<EOF;
142 /**
143  * \@file
144  * \@brief     Generated functions for machine description interface.
145  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
146  *            Edit $specfile instead.
147  *            created by: $0 $specfile $target_dir
148  * \@date      $creation_time
149  */
150 #include "config.h"
151
152 #include <stdlib.h>
153
154 #include "gen_$arch\_machine.h"
155
156 EOF
157
158 print OUT @obst_execunits;
159
160 if ($num_unit_types > 0) {
161 print OUT<<EOF;
162
163 static be_execution_unit_type_t $arch\_execution_unit_types[] = {
164 EOF
165 }
166
167 print OUT @obst_init_unit_types;
168
169 my $types;
170 if ($num_unit_types > 0) {
171         print OUT "};\n\n";
172         $types = "$arch\_execution_unit_types";
173 } else {
174         $types = "NULL";
175 }
176
177 print OUT<<EOF;
178 static be_machine_t $arch\_cpu = {
179         $bundle_size,
180         $bundles_per_cycle,
181         $num_unit_types,
182         $types
183 };
184
185 /**
186  * Returns the $arch machines description
187  */
188 const be_machine_t *$arch\_init_machine_description(void) {
189         static int initialized = 0;
190
191         if (! initialized) {
192                 be_execution_unit_type_t *cur_unit_tp = NULL;
193                 (void) cur_unit_tp; /* avoid warning */
194
195                 be_machine_init_dummy_unit();
196 EOF
197
198 print OUT @obst_init;
199
200 print OUT<<EOF;
201
202                 initialized = 1;
203         }
204
205         return &$arch\_cpu;
206 }
207
208 EOF
209
210 close(OUT);