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