f8f4ea0c93fad577c2a0dff2c3f0d9ad3a0abe6c
[libfirm] / ir / be / scripts / generate_machine.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2007 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 "couldn't parse $specfile: $@" if $@;
43         die "couldn't do $specfile: $!"    unless defined $return;
44         die "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 $num_unit_types = scalar(keys(%cpu));
63 my $tmp            = uc($arch);
64 my $idx            = 0;
65 my $has_desc       = defined(%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         push(@obst_init, "\t\t(void) cur_unit_tp; /* avoid warning */\n");
85
86         push(@obst_unit_defs, "/* enum for execution units of type $unit_type */\n");
87         push(@obst_unit_defs, "enum $arch\_execunit_tp_$unit_type\_vals {\n");
88
89         foreach my $unit (@cur_tp) {
90                 my $unit_name = "$tp_name\_$unit";
91
92                 push(@obst_unit_defs, "\t$unit_name,\n");
93                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].tp   = cur_unit_tp;\n");
94                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].name = \"$unit\";\n");
95         }
96         push(@obst_unit_defs, "};\n\n");
97 }
98
99 push(@obst_unit_tp_defs, "};\n\n") if ($has_desc);
100
101 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
102
103 my $creation_time = localtime(time());
104
105 print OUT<<EOF;
106 /**
107  * \@file
108  * \@brief     Function prototypes for the machine description.
109  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
110  *            Edit $specfile instead.
111  *            created by: $0 $specfile $target_dir
112  * \@date      $creation_time
113  */
114 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_MACHINE_H
115 #define FIRM_BE_${tmp}_GEN_${tmp}_MACHINE_H
116
117 #include "../bemachine.h"
118
119 /**
120  * Returns the $arch machine description.
121  */
122 const be_machine_t *$arch\_init_machine_description(void);
123
124 EOF
125
126 print OUT @obst_execunits_header, "\n";
127 print OUT @obst_unit_tp_defs;
128 print OUT @obst_unit_defs;
129
130 print OUT<<EOF;
131
132 #endif
133
134 EOF
135
136 close(OUT);
137
138 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
139
140 $creation_time = localtime(time());
141
142 print OUT<<EOF;
143 /**
144  * \@file
145  * \@brief     Generated functions for machine description interface.
146  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
147  *            Edit $specfile instead.
148  *            created by: $0 $specfile $target_dir
149  * \@date      $creation_time
150  */
151 #ifdef HAVE_CONFIG_H
152 #include "config.h"
153 #endif
154
155 #include "gen_$arch\_machine.h"
156
157 EOF
158
159 print OUT @obst_execunits;
160
161 print OUT<<EOF;
162
163 be_execution_unit_type_t $arch\_execution_unit_types[] = {
164 EOF
165
166 print OUT @obst_init_unit_types;
167
168 print OUT<<EOF;
169 };
170
171 be_machine_t $arch\_cpu = {
172         $bundle_size,
173         $bundles_per_cycle,
174         $num_unit_types,
175         $arch\_execution_unit_types
176 };
177
178 /**
179  * Returns the $arch machines description
180  */
181 const be_machine_t *$arch\_init_machine_description(void) {
182         static int initialized = 0;
183
184         if (! initialized) {
185                 be_execution_unit_type_t *cur_unit_tp;
186                 (void) cur_unit_tp; /* avoid warning */
187
188                 be_machine_init_dummy_unit();
189 EOF
190
191 print OUT @obst_init;
192
193 print OUT<<EOF;
194
195                 initialized = 1;
196         }
197
198         return &$arch\_cpu;
199 }
200
201 EOF
202
203 close(OUT);