ac15b6496688ef2a77658561255613bd3a583aad
[libfirm] / ir / be / scripts / generate_machine.pl
1 #!/usr/bin/perl -w
2
3 # This script generates the data structures for the machine description.
4 # Creation: 2006/10/20
5 # $Id$
6
7 use strict;
8 use Data::Dumper;
9
10 my $specfile   = $ARGV[0];
11 my $target_dir = $ARGV[1];
12
13 our $arch;
14 our %cpu;
15 our %vliw;
16
17 # include spec file
18
19 my $return;
20
21 no strict "subs";
22 unless ($return = do $specfile) {
23         warn "couldn't parse $specfile: $@" if $@;
24         warn "couldn't do $specfile: $!"    unless defined $return;
25         warn "couldn't run $specfile"       unless $return;
26 }
27 use strict "subs";
28
29 my $target_c = $target_dir."/gen_".$arch."_machine.c";
30 my $target_h = $target_dir."/gen_".$arch."_machine.h";
31
32 # stacks for output
33 my @obst_unit_tp_defs;     # stack for execution unit type defines
34 my @obst_unit_defs;        # stack for execution unit defines
35 my @obst_init;             # stack for cpu description initialization
36 my @obst_execunits;        # stack for execunit variables
37 my @obst_execunits_header; # stack for execunit variables
38 my @obst_init_unit_types;  # stack for unit type variable init
39
40 my $bundle_size       = exists($vliw{"bundle_size"})       ? $vliw{"bundle_size"} : 3;
41 my $bundles_per_cycle = exists($vliw{"bundles_per_cycle"}) ? $vliw{"bundles_per_cycle"} : 1;
42
43 my $num_unit_types = scalar(keys(%cpu));
44 my $tmp            = uc($arch);
45 my $idx            = 0;
46 my $has_desc       = defined(%cpu);
47
48 if ($has_desc) {
49         push(@obst_unit_tp_defs, "/* enum for execution unit types */\n");
50         push(@obst_unit_tp_defs, "enum $arch\_execunit_tp_vals {\n");
51 }
52
53 foreach my $unit_type (keys(%cpu)) {
54         my $tp_name   = "$tmp\_EXECUNIT_TP_$unit_type";
55         my @cur_tp    = @{ $cpu{"$unit_type"} };
56         my $num_units = scalar(@cur_tp) - 1;
57
58         push(@obst_init_unit_types, "\t{ $num_units, ".shift(@cur_tp).", \"$unit_type\", $arch\_execution_units_$unit_type },\n");
59         push(@obst_execunits, "be_execution_unit_t $arch\_execution_units_".$unit_type."[$num_units];\n");
60         push(@obst_execunits_header, "extern be_execution_unit_t $arch\_execution_units_".$unit_type."[$num_units];\n");
61
62         push(@obst_unit_tp_defs, "\t$tp_name,\n");
63         push(@obst_init, "\n\t\t/* init of execution unit type $tp_name */\n");
64         push(@obst_init, "\t\tcur_unit_tp = &$arch\_execution_unit_types[$tp_name];\n");
65         push(@obst_init, "\t\t(void) cur_unit_tp; /* avoid warning */\n");
66
67         push(@obst_unit_defs, "/* enum for execution units of type $unit_type */\n");
68         push(@obst_unit_defs, "enum $arch\_execunit_tp_$unit_type\_vals {\n");
69
70         foreach my $unit (@cur_tp) {
71                 my $unit_name = "$tp_name\_$unit";
72
73                 push(@obst_unit_defs, "\t$unit_name,\n");
74                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].tp   = cur_unit_tp;\n");
75                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].name = \"$unit\";\n");
76         }
77         push(@obst_unit_defs, "};\n\n");
78 }
79
80 push(@obst_unit_tp_defs, "};\n\n") if ($has_desc);
81
82 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
83
84 my $creation_time = localtime(time());
85
86 print OUT<<EOF;
87 #ifndef _GEN_$tmp\_MACHINE_H_
88 #define _GEN_$tmp\_MACHINE_H_
89
90 /**
91  * Function prototypes for the machine description.
92  * DO NOT EDIT THIS FILE, your changes will be lost.
93  * Edit $specfile instead.
94  * created by: $0 $specfile $target_dir
95  * date:       $creation_time
96  */
97
98 #include "../bemachine.h"
99
100 /**
101  * Returns the $arch machine description.
102  */
103 const be_machine_t *$arch\_init_machine_description(void);
104
105 EOF
106
107 print OUT @obst_execunits_header, "\n";
108 print OUT @obst_unit_tp_defs;
109 print OUT @obst_unit_defs;
110
111 print OUT<<EOF;
112
113 #endif /* _GEN_$tmp\_MACHINE_H_ */
114
115 EOF
116
117 close(OUT);
118
119 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
120
121 $creation_time = localtime(time());
122
123 print OUT<<EOF;
124 /**
125  * Generated functions for machine description interface.
126  * DO NOT EDIT THIS FILE, your changes will be lost.
127  * Edit $specfile instead.
128  * created by: $0 $specfile $target_dir
129  * date:       $creation_time
130  */
131 #ifdef HAVE_CONFIG_H
132 #include "config.h"
133 #endif
134
135 #include "gen_$arch\_machine.h"
136
137 EOF
138
139 print OUT @obst_execunits;
140
141 print OUT<<EOF;
142
143 be_execution_unit_type_t $arch\_execution_unit_types[] = {
144 EOF
145
146 print OUT @obst_init_unit_types;
147
148 print OUT<<EOF;
149 };
150
151 be_machine_t $arch\_cpu = {
152         $bundle_size,
153         $bundles_per_cycle,
154         $num_unit_types,
155         $arch\_execution_unit_types
156 };
157
158 /**
159  * Returns the $arch machines description
160  */
161 const be_machine_t *$arch\_init_machine_description(void) {
162         static int initialized = 0;
163
164         if (! initialized) {
165                 be_execution_unit_type_t *cur_unit_tp;
166                 (void) cur_unit_tp; /* avoid warning */
167
168                 be_machine_init_dummy_unit();
169 EOF
170
171 print OUT @obst_init;
172
173 print OUT<<EOF;
174
175                 initialized = 1;
176         }
177
178         return &$arch\_cpu;
179 }
180
181 EOF
182
183 close(OUT);