added support for abstract machine specification
[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);
57
58         push(@obst_init_unit_types, "\t{ $num_units, \"$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
66         push(@obst_unit_defs, "/* enum for execution units of type $unit_type */\n");
67         push(@obst_unit_defs, "enum $arch\_execunit_tp_$unit_type\_vals {\n");
68         foreach my $unit (@cur_tp) {
69                 my $unit_name = "$tp_name\_$unit";
70
71                 push(@obst_unit_defs, "\t$unit_name,\n");
72                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].tp   = cur_unit_tp;\n");
73                 push(@obst_init, "\t\t$arch\_execution_units_".$unit_type."[".$unit_name."].name = \"$unit\";\n");
74         }
75         push(@obst_unit_defs, "};\n\n");
76 }
77
78 push(@obst_unit_tp_defs, "};\n\n") if ($has_desc);
79
80 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
81
82 my $creation_time = localtime(time());
83
84 print OUT<<EOF;
85 #ifndef _GEN_$tmp\_MACHINE_H_
86 #define _GEN_$tmp\_MACHINE_H_
87
88 /**
89  * Function prototypes for the machine description.
90  * DO NOT EDIT THIS FILE, your changes will be lost.
91  * Edit $specfile instead.
92  * created by: $0 $specfile $target_dir
93  * date:       $creation_time
94  */
95
96 #include "../bemachine.h"
97
98 /**
99  * Returns the $arch machine description.
100  */
101 const be_machine_t *$arch\_init_machine_description(void);
102
103 EOF
104
105 print OUT @obst_execunits_header, "\n";
106 print OUT @obst_unit_tp_defs;
107 print OUT @obst_unit_defs;
108
109 print OUT<<EOF;
110
111 #endif /* _GEN_$tmp\_MACHINE_H_ */
112
113 EOF
114
115 close(OUT);
116
117 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
118
119 $creation_time = localtime(time());
120
121 print OUT<<EOF;
122 /**
123  * Generated functions for machine description interface.
124  * DO NOT EDIT THIS FILE, your changes will be lost.
125  * Edit $specfile instead.
126  * created by: $0 $specfile $target_dir
127  * date:       $creation_time
128  */
129 #ifdef HAVE_CONFIG_H
130 #include "config.h"
131 #endif
132
133 #include "gen_$arch\_machine.h"
134
135 EOF
136
137 print OUT @obst_execunits;
138
139 print OUT<<EOF;
140
141 be_execution_unit_type_t $arch\_execution_unit_types[] = {
142 EOF
143
144 print OUT @obst_init_unit_types;
145
146 print OUT<<EOF;
147 };
148
149 be_machine_t $arch\_cpu = {
150         $bundle_size,
151         $bundles_per_cycle,
152         $num_unit_types,
153         $arch\_execution_unit_types
154 };
155
156 /**
157  * Returns the $arch machines description
158  */
159 const be_machine_t *$arch\_init_machine_description(void) {
160         static int initialized = 0;
161
162         if (! initialized) {
163                 be_execution_unit_type_t *cur_unit_tp;
164 EOF
165
166 print OUT @obst_init;
167
168 print OUT<<EOF;
169
170                 initialized = 1;
171         }
172
173         return &$arch\_cpu;
174 }
175
176 EOF
177
178 close(OUT);