- refactoring of backend generator scripts: You can create multiple constructors
[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 # $Id$
27
28 use strict;
29 use Data::Dumper;
30
31 my $specfile   = $ARGV[0];
32 my $target_dir = $ARGV[1];
33 my $state      = 1;
34 my $cur_op     = "";
35 my $line_nr    = 0;
36
37 our $arch;
38 our $additional_opcodes;
39 our %nodes;
40 our %operands;
41 our %cpu;
42 our $default_op_attr_type;
43 our $default_attr_type;
44 our $default_cmp_attr;
45 our $default_copy_attr;
46 our %init_attr;
47 our $custom_init_attr_func;
48 our %compare_attr;
49 our %copy_attr;
50 our %reg_classes;
51
52 # include spec file
53
54 my $return;
55
56 no strict "subs";
57 unless ($return = do $specfile) {
58         die "Fatal error: couldn't parse $specfile: $@" if $@;
59         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
60         die "Fatal error: couldn't run $specfile"       unless $return;
61 }
62 use strict "subs";
63
64 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
65 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
66
67 if(!defined($default_attr_type)) {
68         $default_attr_type = "${arch}_attr_t";
69 }
70 if(!defined(%init_attr)) {
71         %init_attr = (
72                 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, exec_units, n_res);",
73         );
74 }
75 if(!defined($default_cmp_attr)) {
76         $default_cmp_attr = "${arch}_compare_attr";
77 }
78 if(!defined(%compare_attr)) {
79         %compare_attr = (
80                 "${default_attr_type}" => "${default_cmp_attr}",
81         );
82 }
83
84 # Operands are really just nodes with some special constraints, we check
85 # these and create new entries in the nodes hashmap
86 foreach my $op (keys(%operands)) {
87         my %operand = %{ $operands{"$op"} };
88         my %op_node;
89
90         # constraints
91         if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
92         if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
93         if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
94         if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
95         if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
96
97
98         foreach my $flag (keys(%operand)) {
99                 $op_node{$flag} = $operand{$flag};
100         }
101         $op_node{op_flags} = "O";
102         $op_node{cmp_attr} = 'return 1;';
103         $op_node{mode}     = 'mode_ANY';
104
105         $nodes{$op} = \%op_node;
106 }
107
108 #print Dumper(%nodes);
109 #print Dumper(%operands);
110
111 # create c code file from specs
112
113 my @obst_limit_func;
114 my @obst_reg_reqs;
115 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
116 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
117 my $obst_constructor; # stack for node constructor functions
118 my @obst_new_irop;    # stack for the new_ir_op calls
119 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
120 my $obst_header;      # stack for function prototypes
121 my @obst_is_archirn;  # stack for the is_$arch_irn() function
122 my @obst_cmp_attr;    # stack for the compare attribute functions
123 my $obst_proj = "";   # stack for the pn_ numbers
124 my $orig_op;
125 my $arity;
126 my $cmp_attr_func;
127 my $temp;
128 my $n_opcodes = 0;    # number of opcodes
129 my $ARITY_VARIABLE = -1;
130 my $ARITY_DYNAMIC  = -2;
131 my %requirements = ();
132 my %limit_bitsets = ();
133 my %reg2class = ();
134 my %regclass2len = ();
135
136 # build register->class hashes
137 foreach my $class_name (keys(%reg_classes)) {
138         my @class         = @{ $reg_classes{"$class_name"} };
139         my $old_classname = $class_name;
140
141         pop(@class);
142
143         $class_name = $arch."_".$class_name;
144
145         my $idx = 0;
146         foreach (@class) {
147                 $reg2class{$_->{name}} = {
148                         "class" => $old_classname,
149                         "index" => $idx
150                 };
151                 $idx++;
152         }
153
154         $regclass2len{$old_classname} = $idx;
155 }
156
157
158 # for registering additional opcodes
159 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
160
161 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
162
163 sub create_constructor {
164         my $op   = shift;
165         my $name = shift;
166         my $n    = shift;
167         my $on   = shift;
168         my $known_mode;
169
170         my $suffix = "";
171         if ($name ne "") {
172                 $suffix = "_${name}";
173         }
174
175         # determine mode
176         if (exists($n->{mode})) {
177                 $known_mode = $n->{mode};
178         }
179
180         # determine arity
181         my $arity = 0;
182         if(exists($n->{"arity"})) {
183                 $arity = $n->{"arity"};
184         } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
185                 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
186         } elsif (exists($n->{"ins"})) {
187                 $arity = scalar(@{ $n->{"ins"} });
188         }
189         if($arity eq "variable") {
190                 $arity = $ARITY_VARIABLE;
191         } elsif($arity eq "dynamic") {
192                 $arity = $ARITY_DYNAMIC;
193         }
194
195         # determine out arity
196         my $out_arity = 0;
197         if(exists($n->{"out_arity"})) {
198                 $out_arity = $n->{"out_arity"};
199         } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
200                 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
201         } elsif (exists($n->{"outs"})) {
202                 $out_arity = scalar(@{ $n->{"outs"} });
203         }
204         if($out_arity eq "variable") {
205                 $out_arity = $ARITY_VARIABLE;
206         } elsif($out_arity eq "dynamic") {
207                 $out_arity = $ARITY_DYNAMIC;
208         }
209
210
211         my $comment = $n->{"comment"};
212         if(!exists($n->{"comment"})) {
213                 $comment = "construct ${orig_op} node";
214         }
215         $comment =
216                 "/**\n".
217                 " * ${comment}\n".
218                 " */\n";
219
220         $obst_constructor .= $comment;
221
222         # create constructor head
223         my $complete_args = "";
224         $temp             = "";
225
226         $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
227         if (!exists($n->{"args"})) { # default args
228                 if ($arity == $ARITY_VARIABLE) {
229                         $complete_args = ", int arity, ir_node *in[]";
230                 } elsif ($arity == $ARITY_DYNAMIC) {
231                         $complete_args = "";
232                 } else {
233                         for (my $i = 0; $i < $arity; $i++) {
234                                 my $opname = "op${i}";
235                                 if (exists($n->{"ins"})) {
236                                         my @ins = @{ $n->{"ins"} };
237                                         $opname = $ins[$i];
238                                 }
239
240                                 $complete_args .= ", ir_node *${opname}";
241                         }
242                 }
243                 if ($out_arity == $ARITY_VARIABLE) {
244                         $complete_args .= ", int n_res";
245                 }
246
247                 if (!defined($known_mode)) {
248                         $complete_args .= ", ir_mode *mode";
249                 }
250         } else { # user defined args
251                 for my $href (@{ $n->{"args"} }) {
252                         $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
253                         $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
254                 }
255         }
256
257         # we have additional attribute arguements
258         if (exists($n->{"attr"})) {
259                 $complete_args .= ", ".$n->{"attr"};
260         }
261
262         $temp .= "$complete_args)";
263         $obst_constructor .= "${temp}\n{\n";
264
265         $obst_header .= $comment;
266         $obst_header .= "${temp};\n";
267
268         # emit constructor code
269         $temp = <<EOF;
270         ir_node        *res;
271         ir_op          *op      = op_${arch}_${op};
272         int             flags   = 0;
273         backend_info_t *info;
274 EOF
275
276         if($arity == $ARITY_DYNAMIC) {
277                 $temp .= <<EOF;
278         int             arity   = -1;
279         ir_node       **in      = NULL;
280 EOF
281         } elsif($arity == $ARITY_VARIABLE) {
282         } else {
283                 $temp .= <<EOF;
284         int             arity   = $arity;
285 EOF
286                 if($arity > 0) {
287                         $temp .= <<EOF;
288         ir_node        *in[$arity];
289 EOF
290                 } else {
291                         $temp .= <<EOF;
292         ir_node       **in      = NULL;
293 EOF
294                 }
295         }
296         if($out_arity == $ARITY_DYNAMIC) {
297                 $temp .= <<EOF;
298         int             n_res   = -1;
299 EOF
300         } elsif($out_arity == $ARITY_VARIABLE) {
301         } else {
302                 $temp .= <<EOF;
303         int             n_res   = ${out_arity};
304 EOF
305         }
306
307         if (defined($known_mode)) {
308                 $temp .= <<EOF;
309         ir_mode        *mode    = ${known_mode};
310 EOF
311         }
312
313         # set up static variables for cpu execution unit assigments
314         if (exists($n->{"units"})) {
315                 $temp .= gen_execunit_list_initializer($n->{"units"});
316         } else {
317                 $temp .= <<EOF;
318         static const be_execution_unit_t ***exec_units = NULL;
319 EOF
320         }
321
322         undef my $in_req_var;
323         undef my $out_req_var;
324
325         my $set_out_reqs = "";
326
327         # set up static variables for requirements and registers
328         if (exists($n->{"reg_req"})) {
329                 my %req = %{ $n->{"reg_req"} };
330                 my $idx;
331
332                 undef my @in;
333                 @in = @{ $req{"in"} } if (exists($req{"in"}));
334                 undef my @out;
335                 @out = @{ $req{"out"} } if exists(($req{"out"}));
336
337                 for(my $idx = 0; $idx < $#in; $idx++) {
338                         my $req = $in[$idx];
339                         generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
340                 }
341                 for(my $idx = 0; $idx < $#out; $idx++) {
342                         my $req = $out[$idx];
343                         generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
344                 }
345
346                 if (@in) {
347                         if($arity >= 0 && scalar(@in) != $arity) {
348                                 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
349                         }
350
351                         $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
352                         $temp .= "\t{\n";
353                         for ($idx = 0; $idx <= $#in; $idx++) {
354                                 my $req = $in[$idx];
355                                 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
356                                 $temp .= "\t\t& ${reqstruct},\n";
357                         }
358                         $temp .= "\t};\n";
359                 } else {
360                         if($arity > 0) {
361                                 die "Fatal error: need in requirements for ${op}\n";
362                         }
363                         $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
364                 }
365
366                 if (@out) {
367                         if($out_arity >= 0 && scalar(@out) != $out_arity) {
368                                 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
369                         }
370
371                         for ($idx = 0; $idx <= $#out; $idx++) {
372                                 my $req = $out[$idx];
373                                 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
374                                 $set_out_reqs .= <<EOF;
375 info->out_infos[${idx}].req = &${reqstruct};
376 EOF
377                         }
378                 } else {
379                         if($out_arity > 0) {
380                                 die "Fatal error: need out requirements for ${op}\n";
381                         }
382                 }
383         } else {
384                 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
385         }
386         my $attr_type = $on->{attr_type};
387         if(exists($n->{"init_attr"})) {
388                 $temp .= "\t${attr_type} *attr;\n";
389         }
390
391         $temp .= "\n";
392
393         if($arity > 0) {
394                 $temp .= "\t/* construct in array */\n";
395                 for (my $i = 0; $i < $arity; $i++) {
396                         my $opname = "op${i}";
397                         if (exists($n->{"ins"})) {
398                                 my @ins = @{ $n->{"ins"} };
399                                 $opname = $ins[$i];
400                         }
401
402                         $temp .= "\tin[${i}] = ${opname};\n";
403                 }
404                 $temp .= "\n";
405         }
406
407         # set flags
408         if (exists($n->{"irn_flags"})) {
409                 $temp .= "\t/* flags */\n";
410                 foreach my $flag (split(/\|/, $n->{"irn_flags"})) {
411                         if ($flag eq "R") {
412                                 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
413                         } elsif ($flag eq "N") {
414                                 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
415                         } elsif ($flag eq "F") {
416                                 $temp .= "\tflags |= arch_irn_flags_modify_flags;\n";
417                         } elsif ($flag eq "J") {
418                                 $temp .= "\t flags |= arch_irn_flags_simple_jump;\n";
419                         } else {
420                                 die "Fatal error: unknown flag $flag for ${op}\n";
421                         }
422                 }
423                 $temp .= "\n";
424         }
425
426         # lookup init function
427         my $attr_init_code = $init_attr{$attr_type};
428         if(!defined($attr_init_code)) {
429                 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
430         }
431         my $custominit = "";
432         if(defined($custom_init_attr_func)) {
433                 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
434         }
435         if(defined($n->{custominit})) {
436                 $custominit .= $n->{custominit};
437         }
438
439         $temp .= <<EOF;
440         /* create node */
441         assert(op != NULL);
442         res = new_ir_node(dbgi, current_ir_graph, block, op, mode, arity, in);
443
444         /* init node attributes */
445         ${attr_init_code}
446         ${custominit}
447         info = be_get_info(res);
448         ${set_out_reqs}
449
450 EOF
451
452         if (exists($n->{"init_attr"})) {
453                 $temp .= "\tattr = get_irn_generic_attr(res);\n";
454                 $temp .= "\t".$n->{"init_attr"}."\n";
455         }
456
457         $temp .= <<EOF;
458         /* optimize node */
459         res = optimize_node(res);
460         irn_vrfy_irg(res, current_ir_graph);
461
462         return res;
463 EOF
464
465         $obst_constructor .= $temp;
466
467         # close constructor function
468         $obst_constructor .= "}\n\n";
469 }
470
471 push(@obst_enum_op, "typedef enum _${arch}_opcodes {\n");
472 foreach my $op (keys(%nodes)) {
473         my %n        = %{ $nodes{"$op"} };
474         my $known_mode;
475         my $num_outs = 0;
476         my $out_arity;
477         my @out_flags;
478
479         # determine arity
480         $arity = 0;
481         if(exists($n{"arity"})) {
482                 $arity = $n{"arity"};
483         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
484                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
485         } elsif (exists($n{"ins"})) {
486                 $arity = scalar(@{ $n{"ins"} });
487         }
488         if($arity eq "variable") {
489                 $arity = $ARITY_VARIABLE;
490         } elsif($arity eq "dynamic") {
491                 $arity = $ARITY_DYNAMIC;
492         }
493
494         # determine out arity
495         $out_arity = 0;
496         if(exists($n{"out_arity"})) {
497                 $out_arity = $n{"out_arity"};
498         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
499                 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
500         } elsif (exists($n{"outs"})) {
501                 $out_arity = scalar(@{ $n{"outs"} });
502         }
503         if($out_arity eq "variable") {
504                 $out_arity = $ARITY_VARIABLE;
505         } elsif($out_arity eq "dynamic") {
506                 $out_arity = $ARITY_DYNAMIC;
507         }
508
509         $orig_op = $op;
510         $op      = $arch."_".$op;
511         $temp    = "";
512
513         # define proj numbers and in numbers
514         if (exists($n{"outs"})) {
515                 undef my @outs;
516
517                 @outs = @{ $n{"outs"} };
518                 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
519                         die "Fatal error: Op ${op} has different number of outs and out_arity\n";
520                 }
521
522                 $num_outs = $#outs + 1;
523
524                 $obst_proj .= "\nenum pn_${op} {\n";
525
526                 for (my $idx = 0; $idx <= $#outs; $idx++) {
527                         # check, if we have additional flags annotated to out
528                         if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
529                                 push(@out_flags, $1);
530                                 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
531                         }
532                         $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
533                 }
534
535                 $obst_proj .= "};\n";
536                 # outs have names, it must be a mode_T node
537                 if (!defined($n{mode})) {
538                         $n{mode} = "mode_T";
539                 }
540         }
541         if (exists($n{"ins"})) {
542                 undef my @ins;
543
544                 @ins = @{ $n{"ins"} };
545                 if($arity >= 0 && scalar(@ins) != $arity) {
546                         die "Fatal error: Op ${op} has different number of ins and arity\n";
547                 }
548
549                 $obst_proj .= "\nenum n_$op {\n";
550                 for (my $idx = 0; $idx <= $#ins; $idx++) {
551                         $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
552                 }
553                 $obst_proj .= "};\n";
554         }
555
556         # Create opcode
557         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
558         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
559         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
560
561         push(@obst_is_archirn, "is_$op(node)");
562
563         $obst_header .= <<EOF;
564 extern ir_op *op_${op};
565 ir_op *get_op_${op}(void);
566 int is_${op}(const ir_node *n);
567 EOF
568
569         my $attr_type= $n{attr_type};
570         if(!defined($attr_type)) {
571                 $attr_type = $default_attr_type;
572                 $n{attr_type} = $attr_type;
573         }
574
575         # determine hash function
576         my $hash_func;
577         if (exists($n{"hash_func"})) {
578                 $hash_func = $n{"hash_func"};
579         }
580
581         # determine compare function
582         my $cmp_attr_func;
583         if (exists($n{"cmp_attr"})) {
584                 my $cmpcode = $n{"cmp_attr"};
585
586                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
587                 if($cmpcode =~ m/attr_a/) {
588                         push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
589                 } else {
590                         push(@obst_cmp_attr, "\t(void) a;\n");
591                 }
592                 if($cmpcode =~ m/attr_b/) {
593                         push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
594                 } else {
595                         push(@obst_cmp_attr, "\t(void) b;\n");
596                 }
597                 push(@obst_cmp_attr, "\t${cmpcode}\n");
598                 push(@obst_cmp_attr, "}\n\n");
599
600                 $cmp_attr_func = "cmp_attr_${op}";
601         } else {
602                 if(defined($compare_attr{${attr_type}})) {
603                         $cmp_attr_func = $compare_attr{${attr_type}};
604                 } else {
605                         die "Fatal error: No compare function defined for ${attr_type} attributes.";
606                 }
607         }
608
609         my %constructors;
610         if (exists($n{constructors})) {
611                 %constructors = %{ $n{constructors} };
612         } else {
613                 # Create 1 default constructor
614                 my %constructor = ();
615                 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
616                                 "reg_req", "init_attr", "irn_flags", "mode", "arity",
617                                 "out_arity", "custominit") {
618                         if (defined($n{$a})) {
619                                 $constructor{$a} = $n{$a};
620                         }
621                 }
622                 %constructors = ( "" => \%constructor );
623         }
624
625         foreach my $constr (keys(%constructors)) {
626                 my %cstr = %{ $constructors{$constr} };
627                 # Copy some values from outer node if they don't exists in the constr
628                 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
629                                "custominit") {
630                         if (!defined($cstr{$a}) && defined($n{$a})) {
631                                 $cstr{$a} = $n{$a};
632                         }
633                 }
634                 create_constructor($orig_op, $constr, \%cstr, \%n);
635         }
636
637 #       if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
638 #               # we explicitly skip the constructor if the specification entry says NONE
639 #       } else {
640 #               my $comment = $n{"comment"};
641 #               if(!exists($n{"comment"})) {
642 #                       $comment = "construct ${orig_op} node";
643 #               }
644 #               $comment =
645 #                       "/**\n".
646 #                       " * ${comment}\n".
647 #                       " */\n";
648 #
649 #               $obst_constructor .= $comment;
650 #
651 #               # create constructor head
652 #               my $complete_args = "";
653 #               $temp             = "";
654 #
655 #               $temp = "ir_node *new_bd_$op(dbg_info *db, ir_node *block";
656 #               if (!exists($n{"args"})) { # default args
657 #                       if ($arity == $ARITY_VARIABLE) {
658 #                               $complete_args = ", int arity, ir_node *in[]";
659 #                       } elsif ($arity == $ARITY_DYNAMIC) {
660 #                               $complete_args = "";
661 #                       } else {
662 #                               for (my $i = 0; $i < $arity; $i++) {
663 #                                       my $opname = "op${i}";
664 #                                       if (exists($n{"ins"})) {
665 #                                               my @ins = @{ $n{"ins"} };
666 #                                               $opname = $ins[$i];
667 #                                       }
668 #
669 #                                       $complete_args .= ", ir_node *${opname}";
670 #                               }
671 #                       }
672 #                       if ($out_arity == $ARITY_VARIABLE) {
673 #                               $complete_args .= ", int n_res";
674 #                       }
675 #
676 #                       if (!defined($known_mode)) {
677 #                               $complete_args .= ", ir_mode *mode";
678 #                       }
679 #               } else { # user defined args
680 #                       for my $href (@{ $n{"args"} }) {
681 #                               $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
682 #                               $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
683 #                       }
684 #               }
685 #
686 #               # we have additional attribute arguements
687 #               if (exists($n{"attr"})) {
688 #                       $complete_args .= ", ".$n{"attr"};
689 #               }
690 #
691 #               $temp .= "$complete_args)";
692 #               $obst_constructor .= "${temp}\n{\n";
693 #               $obst_header .= $comment;
694 #               $obst_header .= "${temp};\n";
695 #
696 #               # emit constructor code
697 #               if (!exists($n{"rd_constructor"})) { # default constructor
698 #                       $temp = <<EOF;
699 #       ir_node        *res;
700 #       ir_op          *op      = op_${op};
701 #       int             flags   = 0;
702 #       backend_info_t *info;
703 #EOF
704 #
705 #                       if($arity == $ARITY_DYNAMIC) {
706 #                               $temp .= <<EOF;
707 #       int             arity   = -1;
708 #       ir_node       **in      = NULL;
709 #EOF
710 #                       } elsif($arity == $ARITY_VARIABLE) {
711 #                       } else {
712 #                               $temp .= <<EOF;
713 #       int             arity   = $arity;
714 #EOF
715 #                               if($arity > 0) {
716 #                                       $temp .= <<EOF;
717 #       ir_node        *in[$arity];
718 #EOF
719 #                               } else {
720 #                                       $temp .= <<EOF;
721 #       ir_node       **in    = NULL;
722 #EOF
723 #                               }
724 #                       }
725 #                       if($out_arity == $ARITY_DYNAMIC) {
726 #                               $temp .= <<EOF;
727 #       int             n_res   = -1;
728 #EOF
729 #                       } elsif($out_arity == $ARITY_VARIABLE) {
730 #                       } else {
731 #                               $temp .= <<EOF;
732 #       int             n_res   = ${out_arity};
733 #EOF
734 #                       }
735 #
736 #                       if (defined($known_mode)) {
737 #                               $temp .= <<EOF;
738 #       ir_mode        *mode    = ${known_mode};
739 #EOF
740 #                       }
741 #
742 #                       # set up static variables for cpu execution unit assigments
743 #                       if (exists($n{"units"})) {
744 #                               $temp .= gen_execunit_list_initializer($n{"units"});
745 #                       } else {
746 #                               $temp .= <<EOF;
747 #       static const be_execution_unit_t ***exec_units = NULL;
748 #EOF
749 #                       }
750 #
751 #                       undef my $in_req_var;
752 #                       undef my $out_req_var;
753 #
754 #                       my $set_out_reqs = "";
755 #
756 #                       # set up static variables for requirements and registers
757 #                       if (exists($n{"reg_req"})) {
758 #                               my %req = %{ $n{"reg_req"} };
759 #                               my $idx;
760 #
761 #                               undef my @in;
762 #                               @in = @{ $req{"in"} } if (exists($req{"in"}));
763 #                               undef my @out;
764 #                               @out = @{ $req{"out"} } if exists(($req{"out"}));
765 #
766 #                               for(my $idx = 0; $idx < $#in; $idx++) {
767 #                                       my $req = $in[$idx];
768 #                                       generate_requirements($req, \%n, $op, $idx, 1);
769 #                               }
770 #                               for(my $idx = 0; $idx < $#out; $idx++) {
771 #                                       my $req = $out[$idx];
772 #                                       generate_requirements($req, \%n, $op, $idx, 0);
773 #                               }
774 #
775 #                               if (@in) {
776 #                                       if($arity >= 0 && scalar(@in) != $arity) {
777 #                                               die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
778 #                                       }
779 #
780 #                                       $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
781 #                                       $temp .= "\t{\n";
782 #                                       for ($idx = 0; $idx <= $#in; $idx++) {
783 #                                               my $req = $in[$idx];
784 #                                               my $reqstruct = generate_requirements($req, \%n, $op, $idx, 1);
785 #                                               $temp .= "\t\t& ${reqstruct},\n";
786 #                                       }
787 #                                       $temp .= "\t};\n";
788 #                               } else {
789 #                                       if($arity > 0) {
790 #                                               die "Fatal error: need in requirements for ${op}\n";
791 #                                       }
792 #                                       $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
793 #                               }
794 #
795 #                               if (@out) {
796 #                                       if($out_arity >= 0 && scalar(@out) != $out_arity) {
797 #                                               die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
798 #                                       }
799 #
800 #                                       for ($idx = 0; $idx <= $#out; $idx++) {
801 #                                               my $req = $out[$idx];
802 #                                               my $reqstruct = generate_requirements($req, \%n, $op, $idx, 0);
803 #                                               $set_out_reqs .= <<EOF;
804 #       info->out_infos[${idx}].req = &${reqstruct};
805 #EOF
806 #                                       }
807 #                               } else {
808 #                                       if($out_arity > 0) {
809 #                                               die "Fatal error: need out requirements for ${op}\n";
810 #                                       }
811 #                               }
812 #                       } else {
813 #                               $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
814 #                       }
815 #                       if(exists($n{"init_attr"})) {
816 #                               $temp .= "\t${attr_type} *attr;\n";
817 #                       }
818 #
819 #                       $temp .= "\n";
820 #
821 #                       if($arity > 0) {
822 #                               $temp .= "\t/* construct in array */\n";
823 #                               for (my $i = 0; $i < $arity; $i++) {
824 #                                       my $opname = "op${i}";
825 #                                       if (exists($n{"ins"})) {
826 #                                               my @ins = @{ $n{"ins"} };
827 #                                               $opname = $ins[$i];
828 #                                       }
829 #
830 #                                       $temp .= "\tin[${i}] = ${opname};\n";
831 #                               }
832 #                               $temp .= "\n";
833 #                       }
834 #
835 #                       # set flags
836 #                       if (exists($n{"irn_flags"})) {
837 #                               $temp .= "\t/* flags */\n";
838 #                               foreach my $flag (split(/\|/, $n{"irn_flags"})) {
839 #                                       if ($flag eq "R") {
840 #                                               $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
841 #                                       } elsif ($flag eq "N") {
842 #                                               $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
843 #                                       } elsif ($flag eq "J") {
844 #                                               $temp .= "\t flags |= arch_irn_flags_simple_jump;\n";
845 #                                       } else {
846 #                                               die "Fatal error: unknown flag $flag for ${op}\n";
847 #                                       }
848 #                               }
849 #                               $temp .= "\n";
850 #                       }
851 #
852 #                       # lookup init function
853 #                       my $attr_init_code = $init_attr{$attr_type};
854 #                       if(!defined($attr_init_code)) {
855 #                               die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
856 #                       }
857 #                       my $custominit = "";
858 #                       if(defined($custom_init_attr_func)) {
859 #                               $custominit .= &$custom_init_attr_func(\%n, $op);
860 #                       }
861 #
862 #                       $temp .= <<EOF;
863 #       /* create node */
864 #       assert(op != NULL);
865 #       res = new_ir_node(db, current_ir_graph, block, op, mode, arity, in);
866 #
867 #       /* init node attributes */
868 #       ${attr_init_code}
869 #       ${custominit}
870 #       info = be_get_info(res);
871 #       ${set_out_reqs}
872 #
873 #EOF
874 #
875 #                       if (exists($n{"init_attr"})) {
876 #                               $temp .= "\tattr = get_irn_generic_attr(res);\n";
877 #                               $temp .= "\t".$n{"init_attr"}."\n";
878 #                       }
879 #
880 #                       $temp .= <<EOF;
881 #       /* optimize node */
882 #       res = optimize_node(res);
883 #       irn_vrfy_irg(res, current_ir_graph);
884 #
885 #       return res;
886 #EOF
887 #
888 #                       $obst_constructor .= $temp;
889 #               }
890 #               else { # user defined constructor
891 #                       $obst_constructor .= $n{"rd_constructor"};
892 #               }
893 #
894 #               # close constructor function
895 #               $obst_constructor .= "}\n\n";
896 #       } # constructor creation
897
898         # set default values for state and flags if not given
899         $n{"state"}    = "floats" if (! exists($n{"state"}));
900         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
901
902
903         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
904         push(@obst_new_irop, "\tops.be_ops        = be_ops;\n");
905         push(@obst_new_irop, "\tops.dump_node     = $arch\_dump_node;\n");
906
907         if (defined($cmp_attr_func)) {
908                 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
909         }
910         my $copy_attr_func = $copy_attr{$attr_type};
911         if (!defined($copy_attr_func)) {
912                 $copy_attr_func = $default_copy_attr;
913         }
914         if (defined($copy_attr_func)) {
915                 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
916         }
917         if (defined($hash_func)) {
918                 push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
919         }
920
921         $n_opcodes++;
922         $temp  = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
923         $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
924         push(@obst_new_irop, $temp);
925         push(@obst_new_irop, "\tset_op_tag(op_$op, $arch\_op_tag);\n");
926         if(defined($default_op_attr_type)) {
927                 push(@obst_new_irop, "\tattr = &attrs[iro_$op];\n");
928                 if(defined($n{op_attr_init})) {
929                         push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
930                 }
931                 push(@obst_new_irop, "\tset_op_attr(op_$op, attr);\n");
932         }
933
934         push(@obst_enum_op, "\tiro_$op,\n");
935
936         $obst_header .= "\n";
937 }
938 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
939 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
940 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
941 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
942
943 # emit the code
944
945 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
946
947 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n\n";
948 print OUT @obst_cmp_attr;
949 print OUT "\n";
950 print OUT @obst_opvar;
951 print OUT "\n";
952 print OUT @obst_get_opvar;
953 print OUT "\n";
954
955 print OUT<<EOF;
956
957 static int $arch\_opcode_start = -1;
958 static int $arch\_opcode_end   = -1;
959
960 EOF
961
962 # build the FOURCC arguments from $arch
963
964 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
965
966 if (length($arch) >= 1) {
967         $a = uc(substr($arch, 0, 1));
968 }
969
970 if (length($arch) >= 2) {
971         $b = uc(substr($arch, 1, 1));
972 }
973
974 if (length($arch) >= 3) {
975         $c = uc(substr($arch, 2, 1));
976 }
977
978 if (length($arch) >= 4) {
979         $d = uc(substr($arch, 3, 1));
980 }
981
982 print OUT<<ENDOFISIRN;
983
984 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
985 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
986
987 /** Return the opcode number of the first $arch opcode. */
988 int get_$arch\_opcode_first(void) {
989         return $arch\_opcode_start;
990 }
991
992 /** Return the opcode number of the last $arch opcode + 1. */
993 int get_$arch\_opcode_last(void) {
994         return $arch\_opcode_end;
995 }
996
997 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
998 int is_$arch\_op(const ir_op *op) {
999         return get_op_tag(op) == $arch\_op_tag;
1000 }
1001
1002 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
1003 int is_$arch\_irn(const ir_node *node) {
1004         return is_$arch\_op(get_irn_op(node));
1005 }
1006
1007 int get_$arch\_irn_opcode(const ir_node *node) {
1008         if (is_$arch\_irn(node))
1009                 return get_irn_opcode(node) - $arch\_opcode_start;
1010         return -1;
1011 }
1012
1013 ENDOFISIRN
1014
1015 print OUT <<END;
1016 #ifdef BIT
1017 #undef BIT
1018 #endif
1019 #define BIT(x)  (1 << (x % 32))
1020
1021 END
1022
1023 print OUT @obst_limit_func;
1024 print OUT "\n";
1025
1026 print OUT @obst_reg_reqs;
1027 print OUT "\n";
1028
1029 print OUT<<ENDOFMAIN;
1030 $obst_constructor
1031
1032 /**
1033  * Creates the $arch specific Firm machine operations
1034  * needed for the assembler irgs.
1035  */
1036 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops) {
1037 #define N   irop_flag_none
1038 #define L   irop_flag_labeled
1039 #define C   irop_flag_commutative
1040 #define X   irop_flag_cfopcode
1041 #define I   irop_flag_ip_cfopcode
1042 #define F   irop_flag_fragile
1043 #define Y   irop_flag_forking
1044 #define H   irop_flag_highlevel
1045 #define c   irop_flag_constlike
1046 #define K   irop_flag_keep
1047 #define M   irop_flag_machine
1048 #define O   irop_flag_machine_op
1049 #define NB  irop_flag_dump_noblock
1050 #define NI  irop_flag_dump_noinput
1051 #define R   (irop_flag_user << 0)
1052
1053         ir_op_ops  ops;
1054         int        cur_opcode;
1055         static int run_once = 0;
1056 ENDOFMAIN
1057
1058         if (defined($default_op_attr_type)) {
1059                 print OUT "\t$default_op_attr_type *attr, *attrs;\n";
1060         }
1061
1062 print OUT<<ENDOFMAIN;
1063
1064         if (run_once)
1065                 return;
1066         run_once = 1;
1067
1068         cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
1069
1070         $arch\_opcode_start = cur_opcode;
1071 ENDOFMAIN
1072
1073         if (defined($default_op_attr_type)) {
1074                 print OUT "\tattrs = xmalloc(sizeof(attr[0]) * iro_$arch\_last);\n";
1075                 print OUT "\tmemset(attrs, 0, sizeof(attr[0]) * iro_$arch\_last);\n";
1076         }
1077
1078 print OUT @obst_new_irop;
1079 print OUT "\n";
1080 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
1081 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
1082 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
1083 print OUT ";\n";
1084 print OUT "}\n";
1085
1086 close(OUT);
1087
1088 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
1089
1090 my $creation_time = localtime(time());
1091 my $tmp = uc($arch);
1092
1093 print OUT<<EOF;
1094 /**
1095  * \@file
1096  * \@brief Function prototypes for the new opcode functions.
1097  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
1098  *        Edit $specfile instead.
1099  *        created by: $0 $specfile $target_dir
1100  * \@date  $creation_time
1101  */
1102 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
1103 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
1104
1105 EOF
1106
1107 print OUT @obst_enum_op;
1108 print OUT <<EOF;
1109 int is_${arch}_irn(const ir_node *node);
1110
1111 int get_${arch}_opcode_first(void);
1112 int get_${arch}_opcode_last(void);
1113 int get_${arch}_irn_opcode(const ir_node *node);
1114 ${obst_header}
1115 ${obst_proj}
1116
1117 #endif
1118 EOF
1119
1120 close(OUT);
1121
1122 ###
1123 # Translates numeric arity into string constant.
1124 ###
1125 sub translate_arity {
1126         my $arity = shift;
1127
1128         if ($arity =~ /^\d+$/) {
1129                 if    ($arity == 0) {
1130                         return "oparity_zero";
1131                 }
1132                 elsif ($arity == 1) {
1133                         return "oparity_unary";
1134                 }
1135                 elsif ($arity == 2) {
1136                         return "oparity_binary";
1137                 }
1138                 elsif ($arity == 3) {
1139                         return "oparity_trinary";
1140                 }
1141                 else {
1142                         return "oparity_any";
1143                 }
1144         } elsif ($arity == $ARITY_VARIABLE) {
1145                 return "oparity_variable";
1146         } elsif ($arity == $ARITY_DYNAMIC) {
1147                 return "oparity_dynamic";
1148         } else {
1149                 die "Fatal error: Unknown arity $arity";
1150         }
1151 }
1152
1153 ###
1154 # Return the list of pointers for the given execution units.
1155 ###
1156 sub gen_execunit_list_initializer {
1157         my $units   = shift;
1158         my $uc_arch = uc($arch);
1159         my $ret     = "";
1160         my $ret2    = "";
1161         my %init;
1162
1163         foreach my $unit (@{ $units }) {
1164                 if ($unit eq "DUMMY") {
1165                         push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
1166                 }
1167                 elsif (exists($cpu{"$unit"})) {
1168                         # operation can be executed on all units of this type
1169                         # -> add them all
1170                         my $tp_name = "$arch\_execution_units_$unit";
1171                         my $idx     = 0;
1172                         foreach (@{ $cpu{"$unit"} }) {
1173                                 next if ($idx++ == 0);  # skip first element (it's not a unit)
1174                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
1175                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
1176                         }
1177                 }
1178                 else {
1179                         # operation can be executed only a certain unit
1180                         # -> find corresponding unit type
1181                         my $found = 0;
1182 TP_SEARCH:      foreach my $cur_type (keys(%cpu)) {
1183                                 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
1184                                         if ($unit eq $cur_unit) {
1185                                                 my $tp_name   = "$arch\_execution_units_$cur_type";
1186                                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
1187                                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
1188                                                 $found = 1;
1189                                                 last TP_SEARCH;
1190                                         }
1191                                 }
1192                         }
1193
1194                         if (! $found) {
1195                                 print STDERR "Invalid execution unit $unit specified!\n";
1196                         }
1197                 }
1198         }
1199
1200         # prepare the 2-dim array init
1201         foreach my $key (keys(%init)) {
1202                 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
1203                 $ret .= "\t{\n";
1204                 foreach (@{ $init{"$key"} }) {
1205                         $ret .= "$_,\n";
1206                 }
1207                 $ret .= "\t\tNULL\n";
1208                 $ret .= "\t};\n";
1209                 $ret2 .= "\t\tallowed_units_$key,\n";
1210         }
1211         $ret2 .= "\t\tNULL\n";
1212
1213         $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
1214         $ret .= "\t{\n";
1215         $ret .= $ret2;
1216         $ret .= "\t};\n";
1217
1218         return $ret;
1219 }
1220
1221 sub mangle_requirements {
1222         my $reqs  = shift;
1223         my $class = shift;
1224         my $flags = shift;
1225
1226         my @alternatives = split(/ /, $reqs);
1227         for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
1228                 $alternatives[$idx] =~ s/!/not_/g;
1229         }
1230
1231         @alternatives = sort @alternatives;
1232
1233         my $name = $class."_".join('_', @alternatives);
1234         if (defined($flags)) {
1235                 $flags =~ s/\|/_/g;
1236                 $name .= "_$flags";
1237         }
1238
1239         return $name;
1240 }
1241
1242 ###
1243 # Determines whether $name is a specified register class or not.
1244 # @return 1 if name is register class, 0 otherwise
1245 ###
1246 sub is_reg_class {
1247     my $name = shift;
1248     return 1 if exists($reg_classes{"$name"});
1249     return 0;
1250 }
1251
1252 ###
1253 # Returns the register class for a given register.
1254 # @return class or undef
1255 ###
1256 sub get_reg_class {
1257     my $reg = shift;
1258     $reg = substr($reg, 1) if ($reg =~ /!.*/);
1259     return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
1260     return undef;
1261 }
1262
1263 ###
1264 # Returns the index of a given register within it's register class.
1265 # @return index or undef
1266 ###
1267 sub get_reg_index {
1268     my $reg = shift;
1269     return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
1270     return undef;
1271 }
1272
1273 ###
1274 # Remember the register class for each index in the given requirements.
1275 # We need this information for requirements like "in_sX" or "out_dX"
1276 # @return array of classes corresponding to the requirement for each index
1277 ###
1278 sub build_inout_idx_class {
1279         my $n     = shift;
1280         my $op    = shift;
1281         my $is_in = shift;
1282         my @idx_class;
1283
1284         my $inout = ($is_in ? "in" : "out");
1285
1286         if (exists($n->{"reg_req"}{"$inout"})) {
1287                 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
1288
1289                 for (my $idx = 0; $idx <= $#reqs; $idx++) {
1290                         my $class = undef;
1291                         my ($req,) = split(/:/, $reqs[$idx]);
1292
1293                         if ($req eq "none") {
1294                                 $class = "none";
1295                         } elsif (is_reg_class($req)) {
1296                                 $class = $req;
1297                         } else {
1298                                 my @regs = split(/ /, $req);
1299 GET_CLASS:              foreach my $reg (@regs) {
1300                                         if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
1301                                                 $class = "UNKNOWN_CLASS";
1302                                         } else {
1303                                                 $class = get_reg_class($reg);
1304                                                 if (!defined $class) {
1305                                                         die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
1306                                                 } else {
1307                                                         last GET_CLASS;
1308                                                 } # !defined class
1309                                         } # if (reg =~ ...
1310                                 } # foreach
1311                         } # if
1312
1313                         push(@idx_class, $class);
1314                 } # for
1315         } # if
1316
1317         return @idx_class;
1318 }
1319
1320 ###
1321 # Generates the function for a given $op and a given IN-index
1322 # which returns a subset of possible register from a register class
1323 # @return classname from which the subset is derived or undef and
1324 #         pos which corresponds to in/out reference position or undef
1325 ###
1326 sub build_subset_class_func {
1327         my $neg           = undef;
1328         my $class         = undef;
1329         my $has_limit     = 0;
1330         my $limit_name;
1331         my $same_pos      = 0;
1332         my $different_pos = 0;
1333         my $temp;
1334         my @obst_init;
1335         my @obst_limits;
1336         my @obst_ignore;
1337         my @limit_array;
1338         my $limit_reqs;   #used for name mangling
1339
1340         # build function header
1341         my $node  = shift;
1342         my $op    = shift;
1343         my $idx   = shift;
1344         my $is_in = shift;
1345         my @regs  = split(/ /, shift);
1346         my $flags = shift;
1347
1348         my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1349
1350         # set/unset registers
1351 CHECK_REQS: foreach (@regs) {
1352                 if (!$is_in && /(!)?in_r(\d+)/) {
1353                         my $bit_pos = 1 << ($2 - 1);
1354                         if ($different_pos & $bit_pos) {
1355                                 if ($1) {
1356                                         print STDERR "duplicate !in constraint\n";
1357                                 } else {
1358                                         print STDERR "conflicting !in and in constraints\n";
1359                                 }
1360                                 return (undef, undef, undef, undef);
1361                         }
1362
1363                         if ($same_pos & $bit_pos) {
1364                                 if ($1) {
1365                                         print STDERR "conflicting !in and in constraints\n";
1366                                 } else {
1367                                         print STDERR "duplicate in constraint\n";
1368                                 }
1369                                 return (undef, undef, undef, undef);
1370                         }
1371
1372                         if ($1) {
1373                                 $different_pos |= $bit_pos;
1374                         } else {
1375                                 $same_pos      |= $bit_pos;
1376                         }
1377
1378                         $class = $idx_class[$2 - 1];
1379                         next CHECK_REQS;
1380                 }
1381
1382                 # check for negate
1383                 if (substr($_, 0, 1) eq "!") {
1384                         if (defined($neg) && $neg == 0) {
1385                                 # we have seen a positiv constraint as first one but this one is negative
1386                                 # this doesn't make sense
1387                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1388                                 return (undef, undef, undef, undef);
1389                         }
1390
1391                         if (!defined($neg)) {
1392                                 $has_limit = 1;
1393                         }
1394
1395                         $_   = substr($_, 1); # skip '!'
1396                         $neg = 1;
1397                 } else {
1398                         if (defined($neg) && $neg == 1) {
1399                                 # we have seen a negative constraint as first one but this one is positive
1400                                 # this doesn't make sense
1401                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1402                                 return (undef, undef, undef, undef);
1403                         }
1404
1405                         $has_limit = 1;
1406                         $neg = 0;
1407                 }
1408
1409                 # check if register belongs to one of the given classes
1410                 $temp = get_reg_class($_);
1411                 if (!defined($temp)) {
1412                         print STDERR "Unknown register '$_'!\n";
1413                         return (undef, undef, undef, undef);
1414                 }
1415
1416                 # set class
1417                 if (!defined($class)) {
1418                         $class = $temp;
1419                 } elsif ($class ne $temp) {
1420                         # all registers must belong to the same class
1421                         print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1422                         return (undef, undef, undef, undef);
1423                 }
1424
1425                 # calculate position inside the initializer bitfield (only 32 bits per
1426                 # element)
1427                 my $regidx = get_reg_index($_);
1428                 my $arrayp = $regidx / 32;
1429                 push(@{$limit_array[$arrayp]}, $_);
1430                 $limit_reqs .= "$_ ";
1431         }
1432
1433         # don't allow ignore regs in negative constraints
1434         if($neg) {
1435                 my @cur_class = @{ $reg_classes{"$class"} };
1436                 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1437                         if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1438                                 my $reg    = $cur_class[$idx]{"name"};
1439                                 my $regix  = get_reg_index($reg);
1440                                 my $arrayp = $regix / 32;
1441                                 push(@{$limit_array[$arrayp]}, $reg);
1442                                 $limit_reqs .= "$reg ";
1443                         }
1444                 }
1445         }
1446
1447         if ($has_limit == 1) {
1448                 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1449
1450                 if(defined($limit_bitsets{$limit_name})) {
1451                         $limit_name = $limit_bitsets{$limit_name};
1452                         return ($class, $limit_name, $same_pos, $different_pos);
1453                 }
1454
1455                 $limit_bitsets{$limit_name} = $limit_name;
1456
1457                 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1458                 my $first = 1;
1459                 my $limitbitsetlen = $regclass2len{$class};
1460                 my $limitarraylen = ($limitbitsetlen+31) / 32;
1461                 for(my $i = 0; $i < $limitarraylen; $i++) {
1462
1463                         my $limitarraypart = $limit_array[$i];
1464                         if($first) {
1465                                 $first = 0;
1466                         } else {
1467                                 push(@obst_limit_func, ", ");
1468                         }
1469                         my $temp;
1470                         if($neg) {
1471                                 $temp = "0xFFFFFFFF";
1472                         }
1473                         foreach my $reg (@{$limitarraypart}) {
1474                                 if($neg) {
1475                                         $temp .= " & ~";
1476                                 } elsif(defined($temp)) {
1477                                         $temp .= " | ";
1478                                 }
1479                                 $temp .= "BIT(REG_".uc(${reg}).")";
1480                         }
1481                         if(defined($temp)) {
1482                                 push(@obst_limit_func, "${temp}");
1483                         } else {
1484                                 push(@obst_limit_func, "0");
1485                         }
1486                 }
1487                 push(@obst_limit_func, " };\n");
1488         }
1489
1490         return ($class, $limit_name, $same_pos, $different_pos);
1491 }
1492
1493 ###
1494 # Generate register requirements structure
1495 ###
1496 sub generate_requirements {
1497         my ($reqs, $flags) = split(/:/, shift);
1498         my $node  = shift;
1499         my $op    = shift;
1500         my $idx   = shift;
1501         my $is_in = shift;
1502         my $class = "";
1503         my $result;
1504
1505         my @req_type_mask;
1506         if (defined($flags)) {
1507                 foreach my $f (split(/|/, $flags)) {
1508                         if ($f eq "I") {
1509                                 push(@req_type_mask, "arch_register_req_type_ignore");
1510                         } elsif ($f eq "S") {
1511                                 push(@req_type_mask, "arch_register_req_type_produces_sp");
1512                         }
1513                 }
1514         }
1515
1516         if ($reqs eq "none") {
1517
1518                 $result = <<EOF;
1519 {
1520         arch_register_req_type_none,
1521         NULL,                         /* regclass */
1522         NULL,                         /* limit bitset */
1523         0,                            /* same pos */
1524         0                             /* different pos */
1525 };
1526
1527 EOF
1528         } elsif (is_reg_class($reqs)) {
1529                 push(@req_type_mask, "arch_register_req_type_normal");
1530                 my $reqtype = join(" | ", @req_type_mask);
1531                 $class  = $reqs;
1532                 $result = <<EOF;
1533 {
1534         ${reqtype},
1535         & ${arch}_reg_classes[CLASS_${arch}_${class}],
1536         NULL,        /* limit bitset */
1537         0,           /* same pos */
1538         0            /* different pos */
1539 };
1540
1541 EOF
1542
1543         } else {
1544                 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1545                         = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1546
1547                 if (!defined($regclass)) {
1548                         die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1549                 }
1550
1551                 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1552                         push(@req_type_mask, "arch_register_req_type_limited");
1553                 }
1554                 if ($same_pos != 0) {
1555                         push(@req_type_mask, "arch_register_req_type_should_be_same");
1556                 }
1557                 if ($different_pos != 0) {
1558                         push(@req_type_mask, "arch_register_req_type_must_be_different");
1559                 }
1560                 my $reqtype = join(" | ", @req_type_mask);
1561
1562                 if(!defined($limit_bitset)) {
1563                         $limit_bitset = "NULL";
1564                 }
1565
1566                 $class  = $regclass;
1567                 $result = <<EOF;
1568 {
1569         ${reqtype},
1570         & ${arch}_reg_classes[CLASS_${arch}_${class}],
1571         ${limit_bitset},
1572         ${same_pos},        /* same pos */
1573         ${different_pos}        /* different pos */
1574 };
1575
1576 EOF
1577         }
1578
1579         my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1580         if(defined($requirements{$name})) {
1581                 return $name;
1582         }
1583         $requirements{$name} = $name;
1584         push(@obst_reg_reqs, <<EOF);
1585 static const arch_register_req_t ${name} = ${result}
1586 EOF
1587
1588         return $name;
1589 }