added WITH_ILP switch
[libfirm] / ir / be / scripts / generate_new_opcodes.pl
1 #!/usr/bin/perl -w
2
3 # This script generates the C code which creates the irop's and
4 # their coresponding node constructors for all operations in a given spec
5 # so they can be used as normal firm nodes.
6 # Creation: 2005/10/19
7 # $Id$
8
9 use strict;
10 use Data::Dumper;
11
12 my $specfile   = $ARGV[0];
13 my $target_dir = $ARGV[1];
14 my $state      = 1;
15 my $cur_op     = "";
16 my $line_nr    = 0;
17
18 our $arch;
19 our %nodes;
20
21 # include spec file
22
23 my $return;
24
25 no strict "subs";
26 unless ($return = do $specfile) {
27         warn "couldn't parse $specfile: $@" if $@;
28         warn "couldn't do $specfile: $!"    unless defined $return;
29         warn "couldn't run $specfile"       unless $return;
30 }
31 use strict "subs";
32
33 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
34 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
35
36 #print Dumper(%nodes);
37
38 # create c code file from specs
39
40 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
41 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
42 my @obst_constructor; # stack for node constructor functions
43 my @obst_new_irop;    # stack for the new_ir_op calls
44 my @obst_header;      # stack for function prototypes
45 my @obst_is_archirn;  # stack for the is_$arch_irn() function
46 my @obst_cmp_attr;    # stack for the compare attribute functions
47 my $orig_op;
48 my $arity;
49 my $cmp_attr_func;
50 my $temp;
51 my $n_opcodes = 2;    # we have two additional border opcodes (lowest/highest)
52
53 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
54
55 foreach my $op (keys(%nodes)) {
56         my %n = %{ $nodes{"$op"} };
57
58         # determine arity from in requirements
59         $arity = 0;
60         if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
61                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
62         }
63
64         $orig_op = $op;
65         $op      = $arch."_".$op;
66         $temp    = "";
67
68         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
69         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
70         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
71
72         push(@obst_is_archirn, "is_$op(node)");
73
74         push(@obst_header, "ir_op *get_op_$op(void);\n");
75         push(@obst_header, "int is_$op(const ir_node *n);\n");
76
77         $cmp_attr_func = 0;
78         # create compare attribute function if needed
79         if (exists($n{"cmp_attr"})) {
80                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
81                 push(@obst_cmp_attr, "  $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
82                 push(@obst_cmp_attr, "  $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
83                 push(@obst_cmp_attr, $n{"cmp_attr"});
84                 push(@obst_cmp_attr, "}\n\n");
85
86                 $cmp_attr_func = 1;
87         }
88
89         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
90                 # we explicitly skip the constructor if the specification entry says NONE
91         }
92         else {
93                 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
94                 $n{"comment"} =~ s/^"|"$//g;    # remove "
95                 $n{"comment"} = "/* ".$n{"comment"}." */\n";
96                 push(@obst_constructor, $n{"comment"});
97
98                 # create constructor head
99                 my $complete_args = "";
100                 my $arg_names     = "";
101                 $temp             = "";
102
103                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
104                 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
105                         if ($arity !~ /^\d+$/) {
106                                 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
107                                 next;
108                         }
109                         for (my $i = 1; $i <= $arity; $i++) {
110                                 $complete_args .= ", ir_node *op".$i;
111                                 $arg_names     .= ", op".$i;
112                         }
113                         $complete_args .= ", ir_mode *mode";
114                         $arg_names     .= ", mode";
115                 }
116                 else { # user defined args
117                         for my $href (@{ $n{"args"} }) {
118                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
119                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
120                                 $arg_names      .= ", ".$href->{"name"};
121                         }
122                 }
123                 $complete_args = substr($complete_args, 2);
124                 $temp .= ", $complete_args)";
125                 push(@obst_constructor, $temp." {\n");
126                 push(@obst_header, $temp.";\n");
127
128                 # emit constructor code
129                 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
130                         if ($arity !~ /^\d+$/) {
131                                 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
132                                 next;
133                         }
134
135                         $temp  = "  ir_node *res;\n";
136                         $temp .= "  ir_node *in[$arity];\n" if ($arity > 0);
137                         $temp .= "  int flags = 0;\n";
138
139                         undef my $in_req_var;
140                         undef my $out_req_var;
141
142                         # set up static variables for requirements and registers
143                         if (exists($n{"reg_req"})) {
144                                 my %req = %{ $n{"reg_req"} };
145                                 my $idx;
146
147                                 undef my @in;
148                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
149                                 undef my @out;
150                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
151
152                                 if (@in) {
153                                         $in_req_var = "_in_req_$op";
154                                         $temp .= "  static const $arch\_register_req_t *".$in_req_var."[] =\n  {\n";
155                                         for ($idx = 0; $idx <= $#in; $idx++) {
156                                                 $temp .= "    ".$op."_reg_req_in_".$idx.",\n";
157                                         }
158                                         $temp .= "  };\n";
159                                 }
160
161                                 if (@out) {
162                                         $out_req_var = "_out_req_$op";
163
164                                         $temp .= "  static const $arch\_register_req_t *".$out_req_var."[] =\n  {\n";
165                                         for ($idx = 0; $idx <= $#out; $idx++) {
166                                                 $temp .= "    ".$op."_reg_req_out_".$idx.",\n";
167                                         }
168                                         $temp .= "  };\n";
169                                 }
170                         }
171
172                         $temp .= "\n";
173                         $temp .= "  if (!op_$op) {\n";
174                         $temp .= "    assert(0);\n";
175                         $temp .= "    return NULL;\n";
176                         $temp .= "  }\n\n";
177                         for (my $i = 1; $i <= $arity; $i++) {
178                                 $temp .= "  in[".($i - 1)."] = op".$i.";\n";
179                         }
180                         $temp .= "  res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
181                         $temp .= "  res = optimize_node(res);\n";
182                         $temp .= "  irn_vrfy_irg(res, irg);\n\n";
183
184                         # set flags
185                         if (exists($n{"irn_flags"})) {
186                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
187                                         if ($flag eq "R") {
188                                                 $temp .= "  flags |= arch_irn_flags_rematerializable;   /* op can be easily recalulated */\n";
189                                         }
190                                         elsif ($flag eq "N") {
191                                                 $temp .= "  flags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
192                                         }
193                                         elsif ($flag eq "I") {
194                                                 $temp .= "  flags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
195                                         }
196                                 }
197                         }
198
199                         my $in_param;
200                         my $out_param;
201                         # allocate memory and set pointer to register requirements
202                         if (exists($n{"reg_req"})) {
203                                 my %req = %{ $n{"reg_req"} };
204
205                                 undef my @in;
206                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
207                                 undef my @out;
208                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
209
210                                 if (@in) {
211                                         $in_param = $in_req_var;
212                                 }
213                                 else {
214                                         $in_param = "NULL";
215                                 }
216
217                                 if (@out) {
218                                         $out_param = $out_req_var.", ".($#out + 1);
219                                 }
220                                 else {
221                                         $out_param = "NULL, 0";
222                                 }
223                         }
224                         $temp .= "\n  /* init node attributes */\n";
225                         $temp .= "  init_$arch\_attributes(res, flags, $in_param, $out_param);\n";
226
227                         $temp .= "\n  return res;\n";
228
229                         push(@obst_constructor, $temp);
230                 }
231                 else { # user defined constructor
232                         push(@obst_constructor, $n{"rd_constructor"});
233                 }
234
235                 # close constructor function
236                 push(@obst_constructor, "}\n\n");
237
238         } # constructor creation
239
240         # set default values for state and flags if not given
241         $n{"state"}    = "floats" if (! exists($n{"state"}));
242         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
243
244         push(@obst_new_irop, "\n  memset(&ops, 0, sizeof(ops));\n");
245         push(@obst_new_irop, "  ops.dump_node     = dump_node_$arch;\n");
246
247         if ($cmp_attr_func) {
248                 push(@obst_new_irop, "  ops.node_cmp_attr = cmp_attr_$op;\n");
249         }
250
251         $n_opcodes++;
252         $temp  = "  op_$op = new_ir_op(cur_opcode++, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
253         $temp .= ", ".translate_arity($arity).", 0, sizeof($arch\_attr_t), &ops);\n";
254         push(@obst_new_irop, $temp);
255 }
256
257 # emit the code
258
259 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
260
261 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
262 print OUT @obst_cmp_attr;
263 print OUT "\n";
264 print OUT @obst_opvar;
265 print OUT "\n";
266 print OUT @obst_get_opvar;
267 print OUT "\n";
268
269 print OUT<<ENDOFISIRN;
270
271 static opcode $arch\_opcode_start = -1;
272 static opcode $arch\_opcode_end   = -1;
273
274 int is_$arch\_irn(const ir_node *node) {
275   opcode opc = get_irn_opcode(node);
276
277   assert($arch\_opcode_start > 0 && "missing opcode init");
278   assert($arch\_opcode_end > 0 && "missing opcode init");
279
280   if (opc > $arch\_opcode_start && opc < $arch\_opcode_end)
281     return 1;
282
283   return 0;
284 }
285
286 ENDOFISIRN
287
288 print OUT @obst_constructor;
289
290 print OUT<<ENDOFMAIN;
291 /**
292  * Creates the $arch specific firm operations
293  * needed for the assembler irgs.
294  */
295 void $arch\_create_opcodes(void) {
296 #define N   irop_flag_none
297 #define L   irop_flag_labeled
298 #define C   irop_flag_commutative
299 #define X   irop_flag_cfopcode
300 #define I   irop_flag_ip_cfopcode
301 #define F   irop_flag_fragile
302 #define Y   irop_flag_forking
303 #define H   irop_flag_highlevel
304 #define c   irop_flag_constlike
305 #define K   irop_flag_keep
306
307   ir_op_ops ops;
308   int cur_opcode = get_next_ir_opcodes($n_opcodes);
309
310   $arch\_opcode_start = cur_opcode++;
311
312 ENDOFMAIN
313
314 print OUT @obst_new_irop;
315 print OUT "\n  $arch\_opcode_end = cur_opcode;\n";
316 print OUT "}\n";
317
318 close(OUT);
319
320 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
321
322 print OUT "int is_$arch\_irn(const ir_node *node);\n";
323 print OUT @obst_header;
324
325 close(OUT);
326
327 ###
328 # Translates numeric arity into string constant.
329 ###
330 sub translate_arity {
331         my $arity = shift;
332
333         if ($arity =~ /^\d+$/) {
334                 if    ($arity == 0) {
335                         return "oparity_zero";
336                 }
337                 elsif ($arity == 1) {
338                         return "oparity_unary";
339                 }
340                 elsif ($arity == 2) {
341                         return "oparity_binary";
342                 }
343                 elsif ($arity == 3) {
344                         return "oparity_trinary";
345                 }
346                 else {
347                         return "$arity";
348                 }
349         }
350         else {
351                 return "oparity_".$arity;
352         }
353 }