arch_spec: Remove remnants of unused operands.
[libfirm] / ir / be / scripts / generate_new_opcodes.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2008 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 C code which creates the irop's and
23 # their coresponding node constructors for all operations in a given spec
24 # so they can be used as normal firm nodes.
25 # Creation: 2005/10/19
26
27 use strict;
28 use Data::Dumper;
29
30 my $specfile   = $ARGV[0];
31 my $target_dir = $ARGV[1];
32 my $state      = 1;
33 my $cur_op     = "";
34 my $line_nr    = 0;
35
36 our $arch;
37 our $additional_opcodes;
38 our %nodes;
39 our %cpu;
40 our $default_op_attr_type;
41 our $default_attr_type;
42 our $default_cmp_attr;
43 our $default_copy_attr;
44 our %init_attr;
45 our $custom_init_attr_func;
46 our %compare_attr;
47 our %copy_attr;
48 our %reg_classes;
49 our %custom_irn_flags;
50
51 # include spec file
52
53 my $return;
54
55 no strict "subs";
56 unless ($return = do $specfile) {
57         die "Fatal error: couldn't parse $specfile: $@" if $@;
58         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
59         die "Fatal error: couldn't run $specfile"       unless $return;
60 }
61 use strict "subs";
62
63 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
64 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
65
66 if(!defined($default_attr_type)) {
67         $default_attr_type = "${arch}_attr_t";
68 }
69 if(! %init_attr) {
70         %init_attr = (
71                 "$default_attr_type" => "\tinit_${arch}_attributes(res, irn_flags_, in_reqs, n_res);",
72         );
73 }
74 if(!defined($default_cmp_attr)) {
75         $default_cmp_attr = "${arch}_compare_attr";
76 }
77 if(! %compare_attr) {
78         %compare_attr = (
79                 "${default_attr_type}" => "${default_cmp_attr}",
80         );
81 }
82
83 # create c code file from specs
84
85 my @obst_limit_func;
86 my @obst_reg_reqs;
87 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
88 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
89 my $obst_constructor; # stack for node constructor functions
90 my @obst_new_irop;    # stack for the new_ir_op calls
91 my @obst_free_irop;   # stack for free_ir_op calls
92 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
93 my $obst_header;      # stack for function prototypes
94 my @obst_is_archirn;  # stack for the is_$arch_irn() function
95 my @obst_cmp_attr;    # stack for the compare attribute functions
96 my $obst_proj = "";   # stack for the pn_ numbers
97 my $orig_op;
98 my $arity;
99 my $cmp_attr_func;
100 my $temp;
101 my $n_opcodes = 0;    # number of opcodes
102 my $ARITY_VARIABLE = -1;
103 my $ARITY_DYNAMIC  = -2;
104 my %requirements = ();
105 my %limit_bitsets = ();
106 my %reg2class = ();
107 my %regclass2len = ();
108
109 # build register->class hashes
110 foreach my $class_name (keys(%reg_classes)) {
111         my @class         = @{ $reg_classes{"$class_name"} };
112         my $old_classname = $class_name;
113
114         pop(@class);
115
116         $class_name = $arch."_".$class_name;
117
118         my $idx = 0;
119         foreach (@class) {
120                 $reg2class{$_->{name}} = {
121                         "class" => $old_classname,
122                         "index" => $idx
123                 };
124                 $idx++;
125         }
126
127         $regclass2len{$old_classname} = $idx;
128 }
129
130
131 # for registering additional opcodes
132 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
133
134 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
135 $obst_header .= "void ${arch}_free_opcodes(void);\n";
136
137 sub create_constructor {
138         my $op   = shift;
139         my $name = shift;
140         my $n    = shift;
141         my $on   = shift;
142         my $known_mode;
143
144         my $suffix = "";
145         if ($name ne "") {
146                 $suffix = "_${name}";
147         }
148
149         # determine mode
150         if (exists($n->{mode})) {
151                 $known_mode = $n->{mode};
152         }
153
154         # determine arity
155         my $arity = 0;
156         if(exists($n->{"arity"})) {
157                 $arity = $n->{"arity"};
158         } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
159                 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
160         } elsif (exists($n->{"ins"})) {
161                 $arity = scalar(@{ $n->{"ins"} });
162         }
163         if($arity eq "variable") {
164                 $arity = $ARITY_VARIABLE;
165         } elsif($arity eq "dynamic") {
166                 $arity = $ARITY_DYNAMIC;
167         }
168
169         # determine out arity
170         my $out_arity = 0;
171         if(exists($n->{"out_arity"})) {
172                 $out_arity = $n->{"out_arity"};
173         } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
174                 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
175         } elsif (exists($n->{"outs"})) {
176                 $out_arity = scalar(@{ $n->{"outs"} });
177         }
178         if($out_arity eq "variable") {
179                 $out_arity = $ARITY_VARIABLE;
180         } elsif($out_arity eq "dynamic") {
181                 $out_arity = $ARITY_DYNAMIC;
182         }
183         if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
184                 $known_mode = "mode_T";
185         }
186
187         my $comment = $n->{"comment"};
188         if(!exists($n->{"comment"})) {
189                 $comment = "construct ${orig_op} node";
190         }
191         $comment =
192                 "/**\n".
193                 " * ${comment}\n".
194                 " */\n";
195
196         $obst_constructor .= $comment;
197
198         # create constructor head
199         my $complete_args = "";
200         $temp             = "";
201
202         $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
203         if (!exists($n->{"args"})) { # default args
204                 if ($arity == $ARITY_VARIABLE) {
205                         $complete_args = ", int arity, ir_node *in[]";
206                 } elsif ($arity == $ARITY_DYNAMIC) {
207                         $complete_args = "";
208                 } else {
209                         for (my $i = 0; $i < $arity; $i++) {
210                                 my $opname = "op${i}";
211                                 if (exists($n->{"ins"})) {
212                                         my @ins = @{ $n->{"ins"} };
213                                         $opname = $ins[$i];
214                                 }
215
216                                 $complete_args .= ", ir_node *${opname}";
217                         }
218                 }
219                 if ($out_arity == $ARITY_VARIABLE) {
220                         $complete_args .= ", int n_res";
221                 }
222
223                 if (!defined($known_mode)) {
224                         $complete_args .= ", ir_mode *mode";
225                 }
226         } else { # user defined args
227                 for my $href (@{ $n->{"args"} }) {
228                         $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
229                         $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
230                 }
231         }
232
233         # we have additional attribute arguements
234         if (exists($n->{"attr"})) {
235                 $complete_args .= ", ".$n->{"attr"};
236         }
237
238         $temp .= "$complete_args)";
239         $obst_constructor .= "${temp}\n{\n";
240
241         $obst_header .= $comment;
242         $obst_header .= "${temp};\n";
243
244         # emit constructor code
245         $temp = <<EOF;
246         ir_graph         *irg        = get_irn_irg(block);
247         ir_op            *op         = op_${arch}_${op};
248         arch_irn_flags_t  irn_flags_ = arch_irn_flags_none;
249         ir_node          *res;
250         backend_info_t   *info;
251 EOF
252
253         if($arity == $ARITY_DYNAMIC) {
254                 $temp .= <<EOF;
255         int             arity   = -1;
256         ir_node       **in      = NULL;
257 EOF
258         } elsif($arity == $ARITY_VARIABLE) {
259         } else {
260                 $temp .= <<EOF;
261         int             arity   = $arity;
262 EOF
263                 if($arity > 0) {
264                         $temp .= <<EOF;
265         ir_node        *in[$arity];
266 EOF
267                 } else {
268                         $temp .= <<EOF;
269         ir_node       **in      = NULL;
270 EOF
271                 }
272         }
273         if($out_arity == $ARITY_DYNAMIC) {
274                 $temp .= <<EOF;
275         int             n_res   = -1;
276 EOF
277         } elsif($out_arity == $ARITY_VARIABLE) {
278         } else {
279                 $temp .= <<EOF;
280         int             n_res   = ${out_arity};
281 EOF
282         }
283
284         if (defined($known_mode)) {
285                 $temp .= <<EOF;
286         ir_mode        *mode    = ${known_mode};
287 EOF
288         }
289
290         undef my $in_req_var;
291         undef my $out_req_var;
292
293         my $set_out_reqs = "";
294
295         # set up static variables for requirements and registers
296         if (exists($n->{"reg_req"})) {
297                 my %req = %{ $n->{"reg_req"} };
298                 my $idx;
299
300                 undef my @in;
301                 @in = @{ $req{"in"} } if (exists($req{"in"}));
302                 undef my @out;
303                 @out = @{ $req{"out"} } if exists(($req{"out"}));
304
305                 for(my $idx = 0; $idx < $#in; $idx++) {
306                         my $req = $in[$idx];
307                         generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
308                 }
309                 for(my $idx = 0; $idx < $#out; $idx++) {
310                         my $req = $out[$idx];
311                         generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
312                 }
313
314                 if (@in) {
315                         if($arity >= 0 && scalar(@in) != $arity) {
316                                 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
317                         }
318
319                         $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
320                         $temp .= "\t{\n";
321                         for ($idx = 0; $idx <= $#in; $idx++) {
322                                 my $req = $in[$idx];
323                                 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
324                                 $temp .= "\t\t& ${reqstruct},\n";
325                         }
326                         $temp .= "\t};\n";
327                 } else {
328                         if($arity > 0) {
329                                 die "Fatal error: need in requirements for ${op}\n";
330                         }
331                         $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
332                 }
333
334                 if (@out) {
335                         if($out_arity >= 0 && scalar(@out) != $out_arity) {
336                                 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
337                         }
338
339                         for ($idx = 0; $idx <= $#out; $idx++) {
340                                 my $req = $out[$idx];
341                                 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
342                                 $set_out_reqs .= <<EOF;
343         info->out_infos[${idx}].req = &${reqstruct};
344 EOF
345                         }
346                 } else {
347                         if($out_arity > 0) {
348                                 die "Fatal error: need out requirements for ${op}\n";
349                         }
350                 }
351         } else {
352                 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
353         }
354         my $attr_type = $on->{attr_type};
355         if(exists($n->{"init_attr"})) {
356                 $temp .= "\t${attr_type} *attr;\n";
357         }
358
359         $temp .= "\n";
360
361         if($arity > 0) {
362                 $temp .= "\t/* construct in array */\n";
363                 for (my $i = 0; $i < $arity; $i++) {
364                         my $opname = "op${i}";
365                         if (exists($n->{"ins"})) {
366                                 my @ins = @{ $n->{"ins"} };
367                                 $opname = $ins[$i];
368                         }
369
370                         $temp .= "\tin[${i}] = ${opname};\n";
371                 }
372                 $temp .= "\n";
373         }
374
375         # set flags
376         if (exists($n->{"irn_flags"})) {
377                 $temp .= "\t/* flags */\n";
378                 my %known_irn_flags = (
379                         "none"             => "arch_irn_flags_none",
380                         "dont_spill"       => "arch_irn_flags_dont_spill",
381                         "rematerializable" => "arch_irn_flags_rematerializable",
382                         "modify_flags"     => "arch_irn_flags_modify_flags",
383                         "simple_jump"      => "arch_irn_flags_simple_jump",
384                         "not_scheduled"    => "arch_irn_flags_not_scheduled",
385                 );
386                 if (%custom_irn_flags) {
387                         %known_irn_flags = (%known_irn_flags, %custom_irn_flags);
388                 }
389                 foreach my $flag (@{$n->{"irn_flags"}}) {
390                         if (not defined($known_irn_flags{$flag})) {
391                                 print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
392                         } else {
393                                 $temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
394                         }
395                 }
396                 $temp .= "\n";
397         }
398
399         # lookup init function
400         my $attr_init_code = "(void)in;(void)irn_flags_;(void)in_reqs;(void)n_res;";
401         if ($attr_type ne "") {
402                 $attr_init_code = $init_attr{$attr_type};
403                 if(!defined($attr_init_code)) {
404                         die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
405                 }
406         }
407         my $custominit = "";
408         if(defined($custom_init_attr_func)) {
409                 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
410         }
411         if(defined($n->{custominit})) {
412                 $custominit .= $n->{custominit};
413         }
414
415         $temp .= <<EOF;
416         /* create node */
417         assert(op != NULL);
418         res = new_ir_node(dbgi, irg, block, op, mode, arity, in);
419
420         /* init node attributes */
421         ${attr_init_code}
422         ${custominit}
423         info = be_get_info(res);
424         (void) info; /* avoid potential warning */
425 ${set_out_reqs}
426
427 EOF
428
429         if (exists($n->{"init_attr"})) {
430                 $temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
431                 $temp .= "\t(void) attr; /* avoid potential warning */\n";
432                 $temp .= "\t".$n->{"init_attr"}."\n";
433         }
434
435         $temp .= <<EOF;
436         /* optimize node */
437         res = optimize_node(res);
438         irn_verify_irg(res, irg);
439
440         return res;
441 EOF
442
443         $obst_constructor .= $temp;
444
445         # close constructor function
446         $obst_constructor .= "}\n\n";
447 }
448
449 push(@obst_enum_op, "typedef enum ${arch}_opcodes {\n");
450 foreach my $op (keys(%nodes)) {
451         my %n        = %{ $nodes{"$op"} };
452         my $known_mode;
453         my $num_outs = 0;
454         my $out_arity;
455         my @out_flags;
456
457         # determine arity
458         $arity = 0;
459         if(exists($n{"arity"})) {
460                 $arity = $n{"arity"};
461         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
462                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
463         } elsif (exists($n{"ins"})) {
464                 $arity = scalar(@{ $n{"ins"} });
465         }
466         if($arity eq "variable") {
467                 $arity = $ARITY_VARIABLE;
468         } elsif($arity eq "dynamic") {
469                 $arity = $ARITY_DYNAMIC;
470         }
471
472         # determine out arity
473         $out_arity = 0;
474         if(exists($n{"out_arity"})) {
475                 $out_arity = $n{"out_arity"};
476         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
477                 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
478         } elsif (exists($n{"outs"})) {
479                 $out_arity = scalar(@{ $n{"outs"} });
480         }
481         if($out_arity eq "variable") {
482                 $out_arity = $ARITY_VARIABLE;
483         } elsif($out_arity eq "dynamic") {
484                 $out_arity = $ARITY_DYNAMIC;
485         }
486
487         $orig_op = $op;
488         $op      = $arch."_".$op;
489         $temp    = "";
490
491         # define proj numbers and in numbers
492         if (exists($n{"outs"})) {
493                 undef my @outs;
494
495                 @outs = @{ $n{"outs"} };
496                 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
497                         die "Fatal error: Op ${op} has different number of outs and out_arity\n";
498                 }
499
500                 $num_outs = $#outs + 1;
501
502                 if ($num_outs > 0) {
503                         $obst_proj .= "\nenum pn_${op} {\n";
504
505                         for (my $idx = 0; $idx <= $#outs; $idx++) {
506                                 # check, if we have additional flags annotated to out
507                                 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
508                                         push(@out_flags, $1);
509                                         $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
510                                 }
511                                 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
512                         }
513
514                         $obst_proj .= "};\n";
515                 }
516                 # outs have names, it must be a mode_T node
517                 if (!defined($n{mode})) {
518                         $n{mode} = "mode_T";
519                 }
520         }
521         if (exists($n{"ins"})) {
522                 undef my @ins;
523
524                 @ins = @{ $n{"ins"} };
525                 if($arity >= 0 && scalar(@ins) != $arity) {
526                         die "Fatal error: Op ${op} has different number of ins and arity\n";
527                 }
528
529                 if ($#ins >= 0) {
530                         $obst_proj .= "\nenum n_$op {\n";
531                         for (my $idx = 0; $idx <= $#ins; $idx++) {
532                                 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
533                         }
534                         $obst_proj .= "};\n";
535                 }
536         }
537
538         # Create opcode
539         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
540         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
541         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
542
543         push(@obst_is_archirn, "is_$op(node)");
544
545         $obst_header .= <<EOF;
546 extern ir_op *op_${op};
547 ir_op *get_op_${op}(void);
548 int is_${op}(const ir_node *n);
549 EOF
550
551         my $attr_type= $n{attr_type};
552         if(!defined($attr_type)) {
553                 $attr_type = $default_attr_type;
554                 $n{attr_type} = $attr_type;
555         }
556
557         # determine hash function
558         my $hash_func;
559         if (exists($n{"hash_func"})) {
560                 $hash_func = $n{"hash_func"};
561         }
562
563         # determine compare function
564         my $cmp_attr_func;
565         if (exists($n{"cmp_attr"})) {
566                 my $cmpcode = $n{"cmp_attr"};
567
568                 push(@obst_cmp_attr, "static int cmp_attr_$op(const ir_node *a, const ir_node *b) {\n");
569                 if($cmpcode =~ m/attr_a/) {
570                         push(@obst_cmp_attr, "\tconst ${attr_type} *attr_a = get_irn_generic_attr_const(a);\n");
571                 } else {
572                         push(@obst_cmp_attr, "\t(void) a;\n");
573                 }
574                 if($cmpcode =~ m/attr_b/) {
575                         push(@obst_cmp_attr, "\tconst ${attr_type} *attr_b = get_irn_generic_attr_const(b);\n");
576                 } else {
577                         push(@obst_cmp_attr, "\t(void) b;\n");
578                 }
579                 push(@obst_cmp_attr, "\t${cmpcode}\n");
580                 push(@obst_cmp_attr, "}\n\n");
581
582                 $cmp_attr_func = "cmp_attr_${op}";
583         } elsif ($attr_type eq "") {
584                 $cmp_attr_func = "NULL";
585         } else {
586                 if(defined($compare_attr{${attr_type}})) {
587                         $cmp_attr_func = $compare_attr{${attr_type}};
588                 } else {
589                         die "Fatal error: No compare function defined for ${attr_type} attributes.";
590                 }
591         }
592
593         my %constructors;
594         if (exists($n{constructors})) {
595                 %constructors = %{ $n{constructors} };
596         } else {
597                 # Create 1 default constructor
598                 my %constructor = ();
599                 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
600                                 "reg_req", "init_attr", "irn_flags", "mode", "arity",
601                                 "out_arity", "custominit") {
602                         if (defined($n{$a})) {
603                                 $constructor{$a} = $n{$a};
604                         }
605                 }
606                 %constructors = ( "" => \%constructor );
607         }
608
609         foreach my $constr (keys(%constructors)) {
610                 my %cstr = %{ $constructors{$constr} };
611                 # Copy some values from outer node if they don't exists in the constr
612                 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
613                                "custominit") {
614                         if (!defined($cstr{$a}) && defined($n{$a})) {
615                                 $cstr{$a} = $n{$a};
616                         }
617                 }
618                 create_constructor($orig_op, $constr, \%cstr, \%n);
619         }
620
621         # set default values for state and flags if not given
622         $n{"state"}     = "floats" if (! exists($n{"state"}));
623         $n{"op_flags"}  = ["none"] if (! exists($n{"op_flags"}));
624         $n{"dump_func"} = "${arch}_dump_node" if (!exists($n{"dump_func"}));
625         my $dump_func = $n{"dump_func"};
626
627         my %known_flags = map { $_ => 1 } (
628                 "none", "commutative", "cfopcode", "unknown_jump", "fragile",
629                 "forking", "highlevel", "constlike", "keep", "start_block",
630                 "uses_memory", "dump_noblock", "cse_neutral"
631         );
632         my $is_fragile = 0;
633         foreach my $flag (@{$n{"op_flags"}}) {
634                 if (not defined($known_flags{$flag})) {
635                         print STDERR "WARNING: Flag '$flag' in opcode $op is unknown\n";
636                 }
637                 if ($flag eq "fragile") {
638                         $is_fragile = 1;
639                 }
640         }
641         my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
642         my $op_flags = join('|', @mapped);
643
644         my $attr_size = "0";
645         if ($attr_type ne "") {
646                 $attr_size = "sizeof(${attr_type})"
647         }
648
649         $n_opcodes++;
650         $temp  = "\top = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
651         $temp .= ", ".translate_arity($arity).", 0, ${attr_size});\n";
652         push(@obst_new_irop, $temp);
653         push(@obst_new_irop, "\top->ops.be_ops        = be_ops;\n");
654         push(@obst_new_irop, "\top->ops.dump_node     = ${dump_func};\n");
655         if (defined($cmp_attr_func)) {
656                 push(@obst_new_irop, "\top->ops.node_cmp_attr = ${cmp_attr_func};\n");
657         }
658         my $copy_attr_func = $copy_attr{$attr_type};
659         if (!defined($copy_attr_func)) {
660                 # don't set a copy_attr function if the node has no additional attributes.
661                 if ($attr_type ne "") {
662                         $copy_attr_func = $default_copy_attr;
663                 }
664         }
665         if (defined($copy_attr_func)) {
666                 push(@obst_new_irop, "\top->ops.copy_attr     = ${copy_attr_func};\n");
667         }
668         if (defined($hash_func)) {
669                 push(@obst_new_irop, "\top->ops.hash          = ${hash_func};\n");
670         }
671
672         if ($is_fragile) {
673                 push(@obst_new_irop, "\tir_op_set_memory_index(op, n_${op}_mem);\n");
674                 push(@obst_new_irop, "\tir_op_set_fragile_indices(op, pn_${op}_X_regular, pn_${op}_X_except);\n");
675         }
676         push(@obst_new_irop, "\tset_op_tag(op, $arch\_op_tag);\n");
677         if(defined($n{op_attr_init})) {
678                 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
679         }
680         push(@obst_new_irop, "\top_${op} = op;\n");
681
682         push(@obst_free_irop, "\tfree_ir_op(op_$op); op_$op = NULL;\n");
683
684         push(@obst_enum_op, "\tiro_$op,\n");
685
686         $obst_header .= "\n";
687 }
688 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
689 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
690 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
691 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
692
693 # emit the code
694
695 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
696
697 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n";
698 print OUT "#include \"irverify_t.h\"\n";
699 print OUT "#include \"fourcc.h\"\n";
700 print OUT "\n";
701 print OUT @obst_cmp_attr;
702 print OUT "\n";
703 print OUT @obst_opvar;
704 print OUT "\n";
705 print OUT @obst_get_opvar;
706 print OUT "\n";
707
708 print OUT<<EOF;
709
710 static int $arch\_opcode_start = -1;
711 static int $arch\_opcode_end   = -1;
712
713 EOF
714
715 # build the FOURCC arguments from $arch
716
717 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
718
719 if (length($arch) >= 1) {
720         $a = uc(substr($arch, 0, 1));
721 }
722
723 if (length($arch) >= 2) {
724         $b = uc(substr($arch, 1, 1));
725 }
726
727 if (length($arch) >= 3) {
728         $c = uc(substr($arch, 2, 1));
729 }
730
731 if (length($arch) >= 4) {
732         $d = uc(substr($arch, 3, 1));
733 }
734
735 print OUT<<ENDOFISIRN;
736
737 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
738 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
739
740 /** Return the opcode number of the first $arch opcode. */
741 int get_$arch\_opcode_first(void) {
742         return $arch\_opcode_start;
743 }
744
745 /** Return the opcode number of the last $arch opcode + 1. */
746 int get_$arch\_opcode_last(void) {
747         return $arch\_opcode_end;
748 }
749
750 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
751 int is_$arch\_op(const ir_op *op) {
752         return get_op_tag(op) == $arch\_op_tag;
753 }
754
755 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
756 int is_$arch\_irn(const ir_node *node) {
757         return is_$arch\_op(get_irn_op(node));
758 }
759
760 int get_$arch\_irn_opcode(const ir_node *node) {
761         if (is_$arch\_irn(node))
762                 return get_irn_opcode(node) - $arch\_opcode_start;
763         return -1;
764 }
765
766 ENDOFISIRN
767
768 print OUT <<END;
769 #ifdef BIT
770 #undef BIT
771 #endif
772 #define BIT(x)  (1 << (x))
773
774 END
775
776 print OUT @obst_limit_func;
777 print OUT "\n";
778
779 print OUT @obst_reg_reqs;
780 print OUT "\n";
781
782 print OUT<<ENDOFMAIN;
783 $obst_constructor
784
785 /**
786  * Creates the $arch specific Firm machine operations
787  * needed for the assembler irgs.
788  */
789 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops)
790 {
791         ir_op *op;
792         int    cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
793
794         $arch\_opcode_start = cur_opcode;
795 ENDOFMAIN
796
797 print OUT @obst_new_irop;
798 print OUT "\n";
799 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
800 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
801 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
802 print OUT ";\n";
803 print OUT <<ENDOFMAIN;
804 }
805
806 void $arch\_free_opcodes(void)
807 {
808 ENDOFMAIN
809
810 print OUT @obst_free_irop;
811
812 print OUT <<ENDOFMAIN;
813 }
814 ENDOFMAIN
815
816 close(OUT);
817
818 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
819
820 my $creation_time = localtime(time());
821 my $tmp = uc($arch);
822
823 print OUT<<EOF;
824 /**
825  * \@file
826  * \@brief Function prototypes for the new opcode functions.
827  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
828  *        Edit $specfile instead.
829  *        created by: $0 $specfile $target_dir
830  * \@date  $creation_time
831  */
832 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
833 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
834
835 EOF
836
837 print OUT @obst_enum_op;
838 print OUT <<EOF;
839 int is_${arch}_irn(const ir_node *node);
840 int is_${arch}_op(const ir_op *op);
841
842 int get_${arch}_opcode_first(void);
843 int get_${arch}_opcode_last(void);
844 int get_${arch}_irn_opcode(const ir_node *node);
845 ${obst_header}
846 ${obst_proj}
847
848 #endif
849 EOF
850
851 close(OUT);
852
853 ###
854 # Translates numeric arity into string constant.
855 ###
856 sub translate_arity {
857         my $arity = shift;
858
859         if ($arity =~ /^\d+$/) {
860                 if    ($arity == 0) {
861                         return "oparity_zero";
862                 }
863                 elsif ($arity == 1) {
864                         return "oparity_unary";
865                 }
866                 elsif ($arity == 2) {
867                         return "oparity_binary";
868                 }
869                 elsif ($arity == 3) {
870                         return "oparity_trinary";
871                 }
872                 else {
873                         return "oparity_any";
874                 }
875         } elsif ($arity == $ARITY_VARIABLE) {
876                 return "oparity_variable";
877         } elsif ($arity == $ARITY_DYNAMIC) {
878                 return "oparity_dynamic";
879         } else {
880                 die "Fatal error: Unknown arity $arity";
881         }
882 }
883
884 sub mangle_requirements {
885         my $reqs  = shift;
886         my $class = shift;
887         my $flags = shift;
888
889         my @alternatives = split(/ /, $reqs);
890         for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
891                 $alternatives[$idx] =~ s/!/not_/g;
892         }
893
894         @alternatives = sort @alternatives;
895
896         my $name = $class."_".join('_', @alternatives);
897         if (defined($flags)) {
898                 $flags =~ s/\|/_/g;
899                 $name .= "_$flags";
900         }
901
902         return $name;
903 }
904
905 ###
906 # Determines whether $name is a specified register class or not.
907 # @return 1 if name is register class, 0 otherwise
908 ###
909 sub is_reg_class {
910     my $name = shift;
911     return 1 if exists($reg_classes{"$name"});
912     return 0;
913 }
914
915 ###
916 # Returns the register class for a given register.
917 # @return class or undef
918 ###
919 sub get_reg_class {
920     my $reg = shift;
921     $reg = substr($reg, 1) if ($reg =~ /!.*/);
922     return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
923     return undef;
924 }
925
926 ###
927 # Returns the index of a given register within its register class.
928 # @return index or undef
929 ###
930 sub get_reg_index {
931     my $reg = shift;
932     return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
933     return undef;
934 }
935
936 ###
937 # Remember the register class for each index in the given requirements.
938 # We need this information for requirements like "in_sX" or "out_dX"
939 # @return array of classes corresponding to the requirement for each index
940 ###
941 sub build_inout_idx_class {
942         my $n     = shift;
943         my $op    = shift;
944         my $is_in = shift;
945         my @idx_class;
946
947         my $inout = ($is_in ? "in" : "out");
948
949         if (exists($n->{"reg_req"}{"$inout"})) {
950                 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
951
952                 for (my $idx = 0; $idx <= $#reqs; $idx++) {
953                         my $class = undef;
954                         my ($req,) = split(/:/, $reqs[$idx]);
955
956                         if ($req eq "none") {
957                                 $class = "none";
958                         } elsif (is_reg_class($req)) {
959                                 $class = $req;
960                         } else {
961                                 my @regs = split(/ /, $req);
962 GET_CLASS:              foreach my $reg (@regs) {
963                                         if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
964                                                 $class = "UNKNOWN_CLASS";
965                                         } else {
966                                                 $class = get_reg_class($reg);
967                                                 if (!defined $class) {
968                                                         die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
969                                                 } else {
970                                                         last GET_CLASS;
971                                                 } # !defined class
972                                         } # if (reg =~ ...
973                                 } # foreach
974                         } # if
975
976                         push(@idx_class, $class);
977                 } # for
978         } # if
979
980         return @idx_class;
981 }
982
983 ###
984 # Generates the function for a given $op and a given IN-index
985 # which returns a subset of possible register from a register class
986 # @return classname from which the subset is derived or undef and
987 #         pos which corresponds to in/out reference position or undef
988 ###
989 sub build_subset_class_func {
990         my $neg           = undef;
991         my $class         = undef;
992         my $has_limit     = 0;
993         my $limit_name;
994         my $same_pos      = 0;
995         my $different_pos = 0;
996         my $temp;
997         my @obst_init;
998         my @obst_limits;
999         my @obst_ignore;
1000         my @limit_array;
1001         my $limit_reqs;   #used for name mangling
1002
1003         # build function header
1004         my $node  = shift;
1005         my $op    = shift;
1006         my $idx   = shift;
1007         my $is_in = shift;
1008         my @regs  = split(/ /, shift);
1009         my $flags = shift;
1010
1011         my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1012
1013         # set/unset registers
1014 CHECK_REQS: foreach (@regs) {
1015                 if (!$is_in && /(!)?in_r(\d+)/) {
1016                         my $bit_pos = 1 << ($2 - 1);
1017                         if ($different_pos & $bit_pos) {
1018                                 if ($1) {
1019                                         print STDERR "duplicate !in constraint\n";
1020                                 } else {
1021                                         print STDERR "conflicting !in and in constraints\n";
1022                                 }
1023                                 return (undef, undef, undef, undef);
1024                         }
1025
1026                         if ($same_pos & $bit_pos) {
1027                                 if ($1) {
1028                                         print STDERR "conflicting !in and in constraints\n";
1029                                 } else {
1030                                         print STDERR "duplicate in constraint\n";
1031                                 }
1032                                 return (undef, undef, undef, undef);
1033                         }
1034
1035                         if ($1) {
1036                                 $different_pos |= $bit_pos;
1037                         } else {
1038                                 $same_pos      |= $bit_pos;
1039                         }
1040
1041                         $class = $idx_class[$2 - 1];
1042                         next CHECK_REQS;
1043                 }
1044
1045                 # check for negate
1046                 if (substr($_, 0, 1) eq "!") {
1047                         if (defined($neg) && $neg == 0) {
1048                                 # we have seen a positiv constraint as first one but this one is negative
1049                                 # this doesn't make sense
1050                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1051                                 return (undef, undef, undef, undef);
1052                         }
1053
1054                         if (!defined($neg)) {
1055                                 $has_limit = 1;
1056                         }
1057
1058                         $_   = substr($_, 1); # skip '!'
1059                         $neg = 1;
1060                 } else {
1061                         if (defined($neg) && $neg == 1) {
1062                                 # we have seen a negative constraint as first one but this one is positive
1063                                 # this doesn't make sense
1064                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1065                                 return (undef, undef, undef, undef);
1066                         }
1067
1068                         $has_limit = 1;
1069                         $neg = 0;
1070                 }
1071
1072                 # check if register belongs to one of the given classes
1073                 $temp = get_reg_class($_);
1074                 if (!defined($temp)) {
1075                         print STDERR "Unknown register '$_'!\n";
1076                         return (undef, undef, undef, undef);
1077                 }
1078
1079                 # set class
1080                 if (!defined($class)) {
1081                         $class = $temp;
1082                 } elsif ($class ne $temp) {
1083                         # all registers must belong to the same class
1084                         print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1085                         return (undef, undef, undef, undef);
1086                 }
1087
1088                 # calculate position inside the initializer bitfield (only 32 bits per
1089                 # element)
1090                 my $regidx = get_reg_index($_);
1091                 my $arrayp = $regidx / 32;
1092                 push(@{$limit_array[$arrayp]}, $_);
1093                 $limit_reqs .= "$_ ";
1094         }
1095
1096         # don't allow ignore regs in negative constraints
1097         if($neg) {
1098                 my @cur_class = @{ $reg_classes{"$class"} };
1099                 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1100                         if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1101                                 my $reg    = $cur_class[$idx]{"name"};
1102                                 my $regix  = get_reg_index($reg);
1103                                 my $arrayp = $regix / 32;
1104                                 push(@{$limit_array[$arrayp]}, $reg);
1105                                 $limit_reqs .= "$reg ";
1106                         }
1107                 }
1108         }
1109
1110         if ($has_limit == 1) {
1111                 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1112
1113                 if(defined($limit_bitsets{$limit_name})) {
1114                         $limit_name = $limit_bitsets{$limit_name};
1115                         return ($class, $limit_name, $same_pos, $different_pos);
1116                 }
1117
1118                 $limit_bitsets{$limit_name} = $limit_name;
1119
1120                 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1121                 my $first = 1;
1122                 my $limitbitsetlen = $regclass2len{$class};
1123                 my $limitarraylen = ($limitbitsetlen+31) / 32;
1124                 for(my $i = 0; $i < $limitarraylen; $i++) {
1125
1126                         my $limitarraypart = $limit_array[$i];
1127                         if($first) {
1128                                 $first = 0;
1129                         } else {
1130                                 push(@obst_limit_func, ", ");
1131                         }
1132                         my $temp;
1133                         if($neg) {
1134                                 $temp = "0xFFFFFFFF";
1135                         }
1136                         foreach my $reg (@{$limitarraypart}) {
1137                                 if($neg) {
1138                                         $temp .= " & ~";
1139                                 } elsif(defined($temp)) {
1140                                         $temp .= " | ";
1141                                 }
1142                                 my $firstreg = uc($reg_classes{$class}[0]->{"name"});
1143                                 my $classuc = uc($class);
1144                                 my $reguc = uc($reg);
1145                                 $temp .= "BIT(REG_${classuc}_${reguc})";
1146                         }
1147                         if(defined($temp)) {
1148                                 push(@obst_limit_func, "${temp}");
1149                         } else {
1150                                 push(@obst_limit_func, "0");
1151                         }
1152                 }
1153                 push(@obst_limit_func, " };\n");
1154         }
1155
1156         return ($class, $limit_name, $same_pos, $different_pos);
1157 }
1158
1159 ###
1160 # Generate register requirements structure
1161 ###
1162 sub generate_requirements {
1163         my ($reqs, $flags) = split(/:/, shift);
1164         my $node  = shift;
1165         my $op    = shift;
1166         my $idx   = shift;
1167         my $is_in = shift;
1168         my $class = "";
1169         my $width = 1;
1170         my $result;
1171
1172         my @req_type_mask;
1173         if (defined($flags)) {
1174                 foreach my $f (split(/|/, $flags)) {
1175                         if ($f eq "I") {
1176                                 push(@req_type_mask, "arch_register_req_type_ignore");
1177                         } elsif ($f eq "S") {
1178                                 push(@req_type_mask, "arch_register_req_type_produces_sp");
1179                         } elsif ($f eq "a") {
1180                                 push(@req_type_mask, "arch_register_req_type_aligned");
1181                         } elsif ($f eq "2" or $f eq "4" or $f eq "8") {
1182                                 $width = int($f);
1183                         }
1184                 }
1185         }
1186
1187         if ($reqs eq "none") {
1188
1189                 $result = <<EOF;
1190 {
1191         arch_register_req_type_none,
1192         NULL,                         /* regclass */
1193         NULL,                         /* limit bitset */
1194         0,                            /* same pos */
1195         0,                            /* different pos */
1196         0                             /* width */
1197 };
1198
1199 EOF
1200         } elsif (is_reg_class($reqs)) {
1201                 push(@req_type_mask, "arch_register_req_type_normal");
1202                 my $reqtype = join(" | ", @req_type_mask);
1203                 $class  = $reqs;
1204                 $result = <<EOF;
1205 {
1206         ${reqtype},
1207         & ${arch}_reg_classes[CLASS_${arch}_${class}],
1208         NULL,        /* limit bitset */
1209         0,           /* same pos */
1210         0,           /* different pos */
1211         $width            /* width */
1212 };
1213
1214 EOF
1215
1216         } else {
1217                 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1218                         = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1219
1220                 if (!defined($regclass)) {
1221                         die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1222                 }
1223
1224                 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1225                         push(@req_type_mask, "arch_register_req_type_limited");
1226                 }
1227                 if ($same_pos != 0) {
1228                         push(@req_type_mask, "arch_register_req_type_should_be_same");
1229                 }
1230                 if ($different_pos != 0) {
1231                         push(@req_type_mask, "arch_register_req_type_must_be_different");
1232                 }
1233                 my $reqtype = join(" | ", @req_type_mask);
1234
1235                 if(!defined($limit_bitset)) {
1236                         $limit_bitset = "NULL";
1237                 }
1238
1239                 $class  = $regclass;
1240                 $result = <<EOF;
1241 {
1242         ${reqtype},
1243         & ${arch}_reg_classes[CLASS_${arch}_${class}],
1244         ${limit_bitset},
1245         ${same_pos},        /* same pos */
1246         ${different_pos},       /* different pos */
1247         $width             /* width */
1248 };
1249
1250 EOF
1251         }
1252
1253         my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1254         if(defined($requirements{$name})) {
1255                 return $name;
1256         }
1257         $requirements{$name} = $name;
1258         push(@obst_reg_reqs, <<EOF);
1259 static const arch_register_req_t ${name} = ${result}
1260 EOF
1261
1262         return $name;
1263 }