fixed generation of default dummy unit
[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 $additional_opcodes;
20 our %nodes;
21 our %cpu;
22
23 # include spec file
24
25 my $return;
26
27 no strict "subs";
28 unless ($return = do $specfile) {
29         warn "couldn't parse $specfile: $@" if $@;
30         warn "couldn't do $specfile: $!"    unless defined $return;
31         warn "couldn't run $specfile"       unless $return;
32 }
33 use strict "subs";
34
35 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
36 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
37
38 #print Dumper(%nodes);
39 #print Dumper(%operands);
40
41 # create c code file from specs
42
43 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
44 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
45 my @obst_constructor; # stack for node constructor functions
46 my @obst_new_irop;    # stack for the new_ir_op calls
47 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
48 my @obst_header;      # stack for function prototypes
49 my @obst_is_archirn;  # stack for the is_$arch_irn() function
50 my @obst_cmp_attr;    # stack for the compare attribute functions
51 my @obst_proj;        # stack for the pn_ numbers
52 my $orig_op;
53 my $arity;
54 my $cmp_attr_func;
55 my $temp;
56 my $n_opcodes = 0;    # number of opcodes
57
58 # for registering additional opcodes
59 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
60
61 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
62
63 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
64 foreach my $op (keys(%nodes)) {
65         my %n        = %{ $nodes{"$op"} };
66         my $tuple    = 0;
67         my $n_res    = 0;
68         my $num_outs = 0;
69         my @out_flags;
70
71         # determine arity from in requirements
72         $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
73         if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
74                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
75         }
76
77         $orig_op = $op;
78         $op      = $arch."_".$op;
79         $temp    = "";
80
81         # define some proj numbers
82         if (exists($n{"outs"})) {
83                 undef my @outs;
84
85                 @outs     = @{ $n{"outs"} };
86                 $num_outs = $#outs + 1;
87
88                 push(@obst_proj, "\nenum pn_$op {\n");
89
90                 for (my $idx = 0; $idx <= $#outs; $idx++) {
91                         # check, if we have additional flags annotated to out
92                         if ($outs[$idx] =~ /:(S|I(\|(S|I))*)/) {
93                                 push(@out_flags, $1);
94                                 $outs[$idx] =~ s/:(S|I(\|(S|I))*)//;
95                         }
96                         push(@obst_proj, "  pn_$op\_".$outs[$idx]." = $idx,\n");
97                 }
98
99                 push(@obst_proj, "};\n");
100                 $tuple = 1;
101         }
102
103         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
104         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
105         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
106
107         push(@obst_is_archirn, "is_$op(node)");
108
109         push(@obst_header, "extern ir_op *op_$op;\n");
110         push(@obst_header, "ir_op *get_op_$op(void);\n");
111         push(@obst_header, "int is_$op(const ir_node *n);\n");
112
113         $cmp_attr_func = 0;
114         # create compare attribute function if needed
115         if (exists($n{"cmp_attr"})) {
116                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
117                 push(@obst_cmp_attr, "  $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
118                 push(@obst_cmp_attr, "  $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
119                 push(@obst_cmp_attr, "  (void) attr_a;\n");
120                 push(@obst_cmp_attr, "  (void) attr_b;\n");
121                 push(@obst_cmp_attr, $n{"cmp_attr"});
122                 push(@obst_cmp_attr, "}\n\n");
123
124                 $cmp_attr_func = 1;
125         }
126
127         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
128                 # we explicitly skip the constructor if the specification entry says NONE
129         }
130         else {
131                 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
132                 $n{"comment"} =~ s/^"|"$//g;    # remove "
133                 $n{"comment"} = "/* ".$n{"comment"}." */\n";
134                 push(@obst_constructor, $n{"comment"});
135
136                 # create constructor head
137                 my $complete_args = "";
138                 my $arg_names     = "";
139                 $temp             = "";
140
141                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
142                 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
143                         if ($arity !~ /^\d+$/) {
144                                 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
145                                 next;
146                         }
147                         for (my $i = 1; $i <= $arity; $i++) {
148                                 $complete_args .= ", ir_node *op".$i;
149                                 $arg_names     .= ", op".$i;
150                         }
151                         if ($tuple == 0) {
152                                 $complete_args .= ", ir_mode *mode";
153                                 $arg_names     .= ", mode";
154                         }
155                 }
156                 else { # user defined args
157                         for my $href (@{ $n{"args"} }) {
158                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
159                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
160                                 $arg_names      .= ", ".$href->{"name"};
161                         }
162                 }
163
164                 # we have additional attribute arguements
165                 if (exists($n{"attr"})) {
166                         $complete_args .= ", ".$n{"attr"};
167                 }
168
169                 # $complete_args = substr($complete_args, 2);
170                 $temp .= "$complete_args)";
171                 push(@obst_constructor, $temp." {\n");
172                 push(@obst_header, $n{"comment"});
173                 push(@obst_header, $temp.";\n");
174
175                 # emit constructor code
176                 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
177                         if ($arity !~ /^\d+$/) {
178                                 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
179                                 next;
180                         }
181
182                         $temp  = "  ir_node *res;\n";
183                         $temp .= "  ir_node *in[$arity];\n" if ($arity > 0);
184                         $temp .= "  int flags = 0;\n";
185                         $temp .= "  $arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
186
187                         my $exec_units = "NULL";
188                         # set up static variables for cpu execution unit assigments
189                         if (exists($n{"units"})) {
190                                 $temp .= gen_execunit_list_initializer($n{"units"});
191                                 $exec_units = "_exec_units";
192                         }
193
194                         undef my $in_req_var;
195                         undef my $out_req_var;
196
197                         # set up static variables for requirements and registers
198                         if (exists($n{"reg_req"})) {
199                                 my %req = %{ $n{"reg_req"} };
200                                 my $idx;
201
202                                 undef my @in;
203                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
204                                 undef my @out;
205                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
206
207                                 if (@in) {
208                                         $in_req_var = "_in_req_$op";
209                                         $temp .= "  static const $arch\_register_req_t *".$in_req_var."[] =\n  {\n";
210                                         for ($idx = 0; $idx <= $#in; $idx++) {
211                                                 $temp .= "    ".$op."_reg_req_in_".$idx.",\n";
212                                         }
213                                         $temp .= "  };\n";
214                                 }
215
216                                 if (@out) {
217                                         $out_req_var = "_out_req_$op";
218
219                                         $temp .= "  static const $arch\_register_req_t *".$out_req_var."[] =\n  {\n";
220                                         for ($idx = 0; $idx <= $#out; $idx++) {
221                                                 $temp .= "    ".$op."_reg_req_out_".$idx.",\n";
222                                         }
223                                         $temp .= "  };\n";
224                                 }
225                         }
226
227                         $temp .= "\n";
228                         $temp .= "  if (!op_$op) {\n";
229                         $temp .= "    assert(0);\n";
230                         $temp .= "    return NULL;\n";
231                         $temp .= "  }\n\n";
232                         for (my $i = 1; $i <= $arity; $i++) {
233                                 $temp .= "  in[".($i - 1)."] = op".$i.";\n";
234                         }
235
236                         # set flags
237                         if (exists($n{"irn_flags"})) {
238                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
239                                         if ($flag eq "R") {
240                                                 $temp .= "  flags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */\n";
241                                         }
242                                         elsif ($flag eq "N") {
243                                                 $temp .= "  flags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
244                                         }
245                                         elsif ($flag eq "I") {
246                                                 $temp .= "  flags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
247                                         }
248                                         elsif ($flag eq "S") {
249                                                 $temp .= "  flags |= arch_irn_flags_modify_sp;          /* op modifies stack pointer */\n";
250                                         }
251                                 }
252                         }
253
254                         my $in_param;
255                         my $out_param;
256                         # allocate memory and set pointer to register requirements
257                         if (exists($n{"reg_req"})) {
258                                 my %req = %{ $n{"reg_req"} };
259
260                                 undef my @in;
261                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
262                                 undef my @out;
263                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
264
265                                 if (@in) {
266                                         $in_param = $in_req_var;
267                                 }
268                                 else {
269                                         $in_param = "NULL";
270                                 }
271
272                                 if (@out) {
273                                         $n_res     = $#out + 1;
274                                         $out_param = "$out_req_var, $exec_units, $n_res";
275                                 }
276                                 else {
277                                         $out_param = "NULL, $exec_units, 0";
278                                 }
279                         }
280                         else {
281                                 $in_param  = "NULL";
282                                 $out_param = "NULL, $exec_units, 0";
283                         }
284                         $temp .= "\n  /* create node */\n";
285
286                         my $latency = 1;
287                         if (exists($n{"latency"})) {
288                                 $latency = $n{"latency"};
289                         }
290
291                         my $mode = "mode";
292                         if ($tuple == 1) {
293                                 $mode = "mode_T";
294                         }
295                         $temp .= "  res = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
296
297                         $temp .= "\n  /* init node attributes */\n";
298                         $temp .= "  init_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
299
300                         # set flags for outs
301                         if ($#out_flags >= 0) {
302                                 $temp .= "\n  /* set flags for outs */\n";
303                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
304                                         my $flags  = "";
305                                         my $prefix = "";
306
307                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
308                                                 if ($flag eq "I") {
309                                                         $flags .= $prefix."arch_irn_flags_ignore";
310                                                         $prefix = " | ";
311                                                 }
312                                                 elsif ($flag eq "S") {
313                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
314                                                         $prefix = " | ";
315                                                 }
316                                         }
317
318                                         $temp .= "  set_$arch\_out_flags(res, $flags, $idx);\n";
319                                 }
320                         }
321
322
323                         if (exists($n{"init_attr"})) {
324                                 $temp .= "  attr = get_$arch\_attr(res);\n";
325                                 $temp .= $n{"init_attr"}."\n";
326                         }
327
328                         $temp .= "\n  /* optimize node */\n";
329                         $temp .= "  res = optimize_node(res);\n";
330                         $temp .= "  irn_vrfy_irg(res, irg);\n\n";
331
332                         $temp .= "\n  return res;\n";
333
334                         push(@obst_constructor, $temp);
335                 }
336                 else { # user defined constructor
337                         push(@obst_constructor, $n{"rd_constructor"});
338                 }
339
340                 # close constructor function
341                 push(@obst_constructor, "}\n\n");
342         } # constructor creation
343
344         # set default values for state and flags if not given
345         $n{"state"}    = "floats" if (! exists($n{"state"}));
346         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
347
348
349         push(@obst_new_irop, "\n  memset(&ops, 0, sizeof(ops));\n");
350         push(@obst_new_irop, "  ops.dump_node     = $arch\_dump_node;\n");
351
352         if ($cmp_attr_func) {
353                 push(@obst_new_irop, "  ops.node_cmp_attr = cmp_attr_$op;\n");
354         }
355
356         $n_opcodes++;
357         $temp  = "  op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
358         $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
359         push(@obst_new_irop, $temp);
360         push(@obst_new_irop, "  set_op_tag(op_$op, &$arch\_op_tag);\n");
361         push(@obst_enum_op, "  iro_$op,\n");
362
363         push(@obst_header, "\n");
364 }
365 push(@obst_enum_op, "  iro_$arch\_last_generated,\n");
366 push(@obst_enum_op, "  iro_$arch\_last = iro_$arch\_last_generated");
367 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
368 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
369
370 # emit the code
371
372 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
373
374 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
375 print OUT @obst_cmp_attr;
376 print OUT "\n";
377 print OUT @obst_opvar;
378 print OUT "\n";
379 print OUT @obst_get_opvar;
380 print OUT "\n";
381
382 print OUT<<EOF;
383
384 static int $arch\_opcode_start = -1;
385 static int $arch\_opcode_end   = -1;
386
387 EOF
388
389 # build the FOURCC arguments from $arch
390
391 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
392
393 if (length($arch) >= 1) {
394         $a = uc(substr($arch, 0, 1));
395 }
396
397 if (length($arch) >= 2) {
398         $b = uc(substr($arch, 1, 1));
399 }
400
401 if (length($arch) >= 3) {
402         $c = uc(substr($arch, 2, 1));
403 }
404
405 if (length($arch) >= 4) {
406         $d = uc(substr($arch, 3, 1));
407 }
408
409 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
410
411 print OUT<<ENDOFISIRN;
412
413 /** Return the opcode number of the first $arch opcode. */
414 int get_$arch\_opcode_first(void) {
415   return $arch\_opcode_start;
416 }
417
418 /** Return the opcode number of the last $arch opcode + 1. */
419 int get_$arch\_opcode_last(void) {
420   return $arch\_opcode_end;
421 }
422
423 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
424 int is_$arch\_irn(const ir_node *node) {
425   return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
426 }
427
428 int get_$arch\_irn_opcode(const ir_node *node) {
429   if (is_$arch\_irn(node))
430         return get_irn_opcode(node) - $arch\_opcode_start;
431   return -1;
432 }
433
434 ENDOFISIRN
435
436 print OUT @obst_constructor;
437
438 print OUT<<ENDOFMAIN;
439 /**
440  * Creates the $arch specific Firm machine operations
441  * needed for the assembler irgs.
442  */
443 void $arch\_create_opcodes(void) {
444 #define N   irop_flag_none
445 #define L   irop_flag_labeled
446 #define C   irop_flag_commutative
447 #define X   irop_flag_cfopcode
448 #define I   irop_flag_ip_cfopcode
449 #define F   irop_flag_fragile
450 #define Y   irop_flag_forking
451 #define H   irop_flag_highlevel
452 #define c   irop_flag_constlike
453 #define K   irop_flag_keep
454 #define M   irop_flag_machine
455 #define O   irop_flag_machine_op
456 #define R   (irop_flag_user << 0)
457
458   ir_op_ops ops;
459   int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
460
461   $arch\_opcode_start = cur_opcode;
462 ENDOFMAIN
463
464 print OUT @obst_new_irop;
465 print OUT "\n";
466 print OUT "  $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
467 print OUT "  $arch\_opcode_end = cur_opcode + iro_$arch\_last";
468 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
469 print OUT ";\n";
470 print OUT "}\n";
471
472 close(OUT);
473
474 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
475
476 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
477 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
478 print OUT @obst_enum_op;
479 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
480 print OUT "int get_$arch\_opcode_first(void);\n";
481 print OUT "int get_$arch\_opcode_last(void);\n";
482 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
483 print OUT @obst_header;
484 print OUT @obst_proj;
485 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
486
487 close(OUT);
488
489 ###
490 # Translates numeric arity into string constant.
491 ###
492 sub translate_arity {
493         my $arity = shift;
494
495         if ($arity =~ /^\d+$/) {
496                 if    ($arity == 0) {
497                         return "oparity_zero";
498                 }
499                 elsif ($arity == 1) {
500                         return "oparity_unary";
501                 }
502                 elsif ($arity == 2) {
503                         return "oparity_binary";
504                 }
505                 elsif ($arity == 3) {
506                         return "oparity_trinary";
507                 }
508                 else {
509                         return "$arity";
510                 }
511         }
512         else {
513                 return "oparity_".$arity;
514         }
515 }
516
517 ###
518 # Return the list of pointers for the given execution units.
519 ###
520 sub gen_execunit_list_initializer {
521         my $units   = shift;
522         my $uc_arch = uc($arch);
523         my $ret     = "";
524         my $ret2    = "";
525         my %init;
526
527         foreach my $unit (@{ $units }) {
528                 if ($unit eq "DUMMY") {
529                         push(@{ $init{"DUMMY"} }, "    &be_machine_execution_units_DUMMY[0]");
530                 }
531                 elsif (exists($cpu{"$unit"})) {
532                         # operation can be executed on all units of this type
533                         # -> add them all
534                         my $tp_name = "$arch\_execution_units_$unit";
535                         my $idx     = 0;
536                         foreach (@{ $cpu{"$unit"} }) {
537                                 next if ($idx++ == 0);  # skip first element (it's not a unit)
538                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
539                                 push(@{ $init{"$unit"} }, "    &".$tp_name."[".$unit_name."]");
540                         }
541                 }
542                 else {
543                         # operation can be executed only a certain unit
544                         # -> find corresponding unit type
545                         my $found = 0;
546 TP_SEARCH:      foreach my $cur_type (keys(%cpu)) {
547                                 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
548                                         if ($unit eq $cur_unit) {
549                                                 my $tp_name   = "$arch\_execution_units_$cur_type";
550                                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
551                                                 push(@{ $init{"$unit"} }, "    &".$tp_name."[".$unit_name."]");
552                                                 $found = 1;
553                                                 last TP_SEARCH;
554                                         }
555                                 }
556                         }
557
558                         if (! $found) {
559                                 print STDERR "Invalid execution unit $unit specified!\n";
560                         }
561                 }
562         }
563
564         # prepare the 2-dim array init
565         foreach my $key (keys(%init)) {
566                 $ret .= "  static const be_execution_unit_t *_allowed_units_".$key."[] =\n  {\n";
567                 foreach (@{ $init{"$key"} }) {
568                         $ret .= "$_,\n";
569                 }
570                 $ret .= "    NULL\n";
571                 $ret .= "  };\n";
572                 $ret2 .= "    _allowed_units_$key,\n";
573         }
574         $ret2 .= "    NULL\n";
575
576         $ret .= "  static const be_execution_unit_t **_exec_units[] =\n  {\n".$ret2."  };\n";
577
578         return $ret;
579 }