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