4 # Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
6 # This file is part of libFirm.
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.
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.
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
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
30 my $specfile = $ARGV[0];
31 my $target_dir = $ARGV[1];
35 our $default_attr_type;
36 our $default_cmp_attr;
37 our $default_copy_attr;
39 our $custom_init_attr_func;
43 our %custom_irn_flags;
50 unless ($return = do $specfile) {
51 die "Fatal error: couldn't parse $specfile: $@" if $@;
52 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
53 die "Fatal error: couldn't run $specfile" unless $return;
57 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
58 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
60 if(!defined($default_attr_type)) {
61 $default_attr_type = "${arch}_attr_t";
65 "$default_attr_type" => "\tinit_${arch}_attributes(res, irn_flags_, in_reqs, n_res);",
68 if(!defined($default_cmp_attr)) {
69 $default_cmp_attr = "${arch}_compare_attr";
73 "${default_attr_type}" => "${default_cmp_attr}",
77 # create c code file from specs
79 my $obst_limit_func = ""; #
80 my $obst_reg_reqs = ""; #
81 my $obst_opvar = ""; # buffer for the "ir_op *op_<arch>_<op-name> = NULL;" statements
82 my $obst_get_opvar = ""; # buffer for the get_op_<arch>_<op-name>() functions
83 my $obst_constructor = ""; # buffer for node constructor functions
84 my $obst_new_irop = ""; # buffer for the new_ir_op calls
85 my $obst_free_irop = ""; # buffer for free_ir_op calls
86 my $obst_enum_op = ""; # buffer for creating the <arch>_opcode enum
87 my $obst_header = ""; # buffer for function prototypes
88 my $obst_cmp_attr = ""; # buffer for the compare attribute functions
89 my $obst_proj = ""; # buffer for the pn_ numbers
94 my $n_opcodes = 0; # number of opcodes
95 my $ARITY_VARIABLE = -1;
96 my $ARITY_DYNAMIC = -2;
97 my %requirements = ();
98 my %limit_bitsets = ();
100 my %regclass2len = ();
102 # build register->class hashes
103 foreach my $class_name (keys(%reg_classes)) {
104 my @class = @{ $reg_classes{"$class_name"} };
105 my $old_classname = $class_name;
109 $class_name = $arch."_".$class_name;
113 $reg2class{$_->{name}} = {
114 "class" => $old_classname,
120 $regclass2len{$old_classname} = $idx;
124 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
125 $obst_header .= "void ${arch}_free_opcodes(void);\n";
127 sub create_constructor {
136 $suffix = "_${name}";
140 if (exists($n->{mode})) {
141 $known_mode = $n->{mode};
146 if(exists($n->{"arity"})) {
147 $arity = $n->{"arity"};
148 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
149 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
150 } elsif (exists($n->{"ins"})) {
151 $arity = scalar(@{ $n->{"ins"} });
153 if($arity eq "variable") {
154 $arity = $ARITY_VARIABLE;
155 } elsif($arity eq "dynamic") {
156 $arity = $ARITY_DYNAMIC;
159 # determine out arity
161 if(exists($n->{"out_arity"})) {
162 $out_arity = $n->{"out_arity"};
163 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
164 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
165 } elsif (exists($n->{"outs"})) {
166 $out_arity = scalar(@{ $n->{"outs"} });
168 if($out_arity eq "variable") {
169 $out_arity = $ARITY_VARIABLE;
170 } elsif($out_arity eq "dynamic") {
171 $out_arity = $ARITY_DYNAMIC;
173 if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
174 $known_mode = "mode_T";
177 my $comment = $n->{"comment"};
178 if(!exists($n->{"comment"})) {
179 $comment = "construct ${orig_op} node";
186 $obst_constructor .= $comment;
188 # create constructor head
189 my $complete_args = "";
192 $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
193 if (!exists($n->{"args"})) { # default args
194 if ($arity == $ARITY_VARIABLE) {
195 $complete_args = ", int arity, ir_node *in[]";
196 } elsif ($arity == $ARITY_DYNAMIC) {
199 for (my $i = 0; $i < $arity; $i++) {
200 my $opname = "op${i}";
201 if (exists($n->{"ins"})) {
202 my @ins = @{ $n->{"ins"} };
206 $complete_args .= ", ir_node *${opname}";
209 if ($out_arity == $ARITY_VARIABLE) {
210 $complete_args .= ", int n_res";
213 if (!defined($known_mode)) {
214 $complete_args .= ", ir_mode *mode";
216 } else { # user defined args
217 for my $href (@{ $n->{"args"} }) {
218 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
219 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
223 # we have additional attribute arguements
224 if (exists($n->{"attr"})) {
225 $complete_args .= ", ".$n->{"attr"};
228 $temp .= "$complete_args)";
229 $obst_constructor .= "${temp}\n{\n";
231 $obst_header .= $comment;
232 $obst_header .= "${temp};\n";
234 # emit constructor code
236 ir_graph *irg = get_irn_irg(block);
237 ir_op *op = op_${arch}_${op};
238 arch_irn_flags_t irn_flags_ = arch_irn_flags_none;
240 backend_info_t *info;
243 if($arity == $ARITY_DYNAMIC) {
248 } elsif($arity == $ARITY_VARIABLE) {
263 if($out_arity == $ARITY_DYNAMIC) {
267 } elsif($out_arity == $ARITY_VARIABLE) {
270 int n_res = ${out_arity};
274 if (defined($known_mode)) {
276 ir_mode *mode = ${known_mode};
280 undef my $in_req_var;
281 undef my $out_req_var;
283 my $set_out_reqs = "";
285 # set up static variables for requirements and registers
286 if (exists($n->{"reg_req"})) {
287 my %req = %{ $n->{"reg_req"} };
291 @in = @{ $req{"in"} } if (exists($req{"in"}));
293 @out = @{ $req{"out"} } if exists(($req{"out"}));
295 for(my $idx = 0; $idx < $#in; $idx++) {
297 generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
299 for(my $idx = 0; $idx < $#out; $idx++) {
300 my $req = $out[$idx];
301 generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
305 if($arity >= 0 && scalar(@in) != $arity) {
306 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
309 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
311 for ($idx = 0; $idx <= $#in; $idx++) {
313 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
314 $temp .= "\t\t& ${reqstruct},\n";
319 die "Fatal error: need in requirements for ${op}\n";
321 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
325 if($out_arity >= 0 && scalar(@out) != $out_arity) {
326 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
329 for ($idx = 0; $idx <= $#out; $idx++) {
330 my $req = $out[$idx];
331 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
332 $set_out_reqs .= <<EOF;
333 info->out_infos[${idx}].req = &${reqstruct};
338 die "Fatal error: need out requirements for ${op}\n";
342 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
344 my $attr_type = $on->{attr_type};
345 if(exists($n->{"init_attr"})) {
346 $temp .= "\t${attr_type} *attr;\n";
352 $temp .= "\t/* construct in array */\n";
353 for (my $i = 0; $i < $arity; $i++) {
354 my $opname = "op${i}";
355 if (exists($n->{"ins"})) {
356 my @ins = @{ $n->{"ins"} };
360 $temp .= "\tin[${i}] = ${opname};\n";
366 if (exists($n->{"irn_flags"})) {
367 $temp .= "\t/* flags */\n";
368 my %known_irn_flags = (
369 "none" => "arch_irn_flags_none",
370 "dont_spill" => "arch_irn_flags_dont_spill",
371 "rematerializable" => "arch_irn_flags_rematerializable",
372 "modify_flags" => "arch_irn_flags_modify_flags",
373 "simple_jump" => "arch_irn_flags_simple_jump",
374 "not_scheduled" => "arch_irn_flags_not_scheduled",
376 if (%custom_irn_flags) {
377 %known_irn_flags = (%known_irn_flags, %custom_irn_flags);
379 foreach my $flag (@{$n->{"irn_flags"}}) {
380 if (not defined($known_irn_flags{$flag})) {
381 print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
383 $temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
389 # lookup init function
390 my $attr_init_code = "(void)in;(void)irn_flags_;(void)in_reqs;(void)n_res;";
391 if ($attr_type ne "") {
392 $attr_init_code = $init_attr{$attr_type};
393 if(!defined($attr_init_code)) {
394 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
398 if(defined($custom_init_attr_func)) {
399 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
401 if(defined($n->{custominit})) {
402 $custominit .= $n->{custominit};
408 res = new_ir_node(dbgi, irg, block, op, mode, arity, in);
410 /* init node attributes */
413 info = be_get_info(res);
414 (void) info; /* avoid potential warning */
419 if (exists($n->{"init_attr"})) {
420 $temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
421 $temp .= "\t(void) attr; /* avoid potential warning */\n";
422 $temp .= "\t".$n->{"init_attr"}."\n";
427 res = optimize_node(res);
428 irn_verify_irg(res, irg);
433 $obst_constructor .= $temp;
435 # close constructor function
436 $obst_constructor .= "}\n\n";
454 $obst_enum_op .= "typedef enum ${arch}_opcodes {\n";
455 foreach my $op (keys(%nodes)) {
456 my %n = %{ $nodes{"$op"} };
464 if(exists($n{"arity"})) {
465 $arity = $n{"arity"};
466 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
467 $arity = scalar(@{ $n{"reg_req"}{"in"} });
468 } elsif (exists($n{"ins"})) {
469 $arity = scalar(@{ $n{"ins"} });
471 if($arity eq "variable") {
472 $arity = $ARITY_VARIABLE;
473 } elsif($arity eq "dynamic") {
474 $arity = $ARITY_DYNAMIC;
477 # determine out arity
479 if(exists($n{"out_arity"})) {
480 $out_arity = $n{"out_arity"};
481 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
482 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
483 } elsif (exists($n{"outs"})) {
484 $out_arity = scalar(@{ $n{"outs"} });
486 if($out_arity eq "variable") {
487 $out_arity = $ARITY_VARIABLE;
488 } elsif($out_arity eq "dynamic") {
489 $out_arity = $ARITY_DYNAMIC;
496 # define proj numbers and in numbers
497 if (exists($n{"outs"})) {
500 @outs = @{ $n{"outs"} };
501 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
502 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
505 $num_outs = $#outs + 1;
508 $obst_proj .= "\nenum pn_${op} {\n";
510 for (my $idx = 0; $idx <= $#outs; $idx++) {
511 # check, if we have additional flags annotated to out
512 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
513 push(@out_flags, $1);
514 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
516 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
519 $obst_proj .= "};\n";
521 # outs have names, it must be a mode_T node
522 if (!defined($n{mode})) {
526 if (exists($n{"ins"})) {
529 @ins = @{ $n{"ins"} };
530 if($arity >= 0 && scalar(@ins) != $arity) {
531 die "Fatal error: Op ${op} has different number of ins and arity\n";
535 $obst_proj .= "\nenum n_$op {\n";
536 for (my $idx = 0; $idx <= $#ins; $idx++) {
537 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
539 $obst_proj .= "};\n";
544 $obst_opvar .= "ir_op *op_$op = NULL;\n";
545 $obst_get_opvar .= "ir_op *get_op_$op(void) { return op_$op; }\n";
546 $obst_get_opvar .= "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n";
548 $obst_header .= <<EOF;
549 extern ir_op *op_${op};
550 ir_op *get_op_${op}(void);
551 int is_${op}(const ir_node *n);
554 my $attr_type= $n{attr_type};
555 if(!defined($attr_type)) {
556 $attr_type = $default_attr_type;
557 $n{attr_type} = $attr_type;
560 # determine hash function
562 if (exists($n{"hash_func"})) {
563 $hash_func = $n{"hash_func"};
566 # determine compare function
568 if (exists($n{"cmp_attr"})) {
569 my $cmpcode = $n{"cmp_attr"};
571 $obst_cmp_attr .= "static int cmp_attr_$op(const ir_node *a, const ir_node *b) {\n";
572 if($cmpcode =~ m/attr_a/) {
573 $obst_cmp_attr .= "\tconst ${attr_type} *attr_a = get_irn_generic_attr_const(a);\n";
575 $obst_cmp_attr .= "\t(void) a;\n";
577 if($cmpcode =~ m/attr_b/) {
578 $obst_cmp_attr .= "\tconst ${attr_type} *attr_b = get_irn_generic_attr_const(b);\n";
580 $obst_cmp_attr .= "\t(void) b;\n";
582 $obst_cmp_attr .= "\t${cmpcode}\n";
583 $obst_cmp_attr .= "}\n\n";
585 $cmp_attr_func = "cmp_attr_${op}";
586 } elsif ($attr_type eq "") {
587 $cmp_attr_func = "NULL";
589 if(defined($compare_attr{${attr_type}})) {
590 $cmp_attr_func = $compare_attr{${attr_type}};
592 die "Fatal error: No compare function defined for ${attr_type} attributes.";
597 if (exists($n{constructors})) {
598 %constructors = %{ $n{constructors} };
600 # Create 1 default constructor
601 my %constructor = ();
602 foreach my $a (@node_attrs) {
603 if (defined($n{$a})) {
604 $constructor{$a} = $n{$a};
607 %constructors = ( "" => \%constructor );
610 foreach my $constr (keys(%constructors)) {
611 my %cstr = %{ $constructors{$constr} };
612 # Copy some values from outer node if they don't exists in the constr
613 foreach my $a (@node_attrs) {
614 if (!defined($cstr{$a}) && defined($n{$a})) {
618 create_constructor($orig_op, $constr, \%cstr, \%n);
621 # set default values for state and flags if not given
622 $n{"state"} = "floats" if (! exists($n{"state"}));
623 $n{"op_flags"} = ["none"] if (! exists($n{"op_flags"}));
624 $n{"dump_func"} = "${arch}_dump_node" if (!exists($n{"dump_func"}));
625 my $dump_func = $n{"dump_func"};
627 my %known_flags = map { $_ => 1 } (
628 "none", "commutative", "cfopcode", "unknown_jump", "fragile",
629 "forking", "highlevel", "constlike", "keep", "start_block",
630 "uses_memory", "dump_noblock", "cse_neutral"
633 foreach my $flag (@{$n{"op_flags"}}) {
634 if (not defined($known_flags{$flag})) {
635 print STDERR "WARNING: Flag '$flag' in opcode $op is unknown\n";
637 if ($flag eq "fragile") {
641 my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
642 my $op_flags = join('|', @mapped);
645 if ($attr_type ne "") {
646 $attr_size = "sizeof(${attr_type})"
650 $temp = "\top = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
651 $temp .= ", ".translate_arity($arity).", 0, ${attr_size});\n";
652 $obst_new_irop .= $temp;
653 $obst_new_irop .= "\top->ops.be_ops = be_ops;\n";
654 $obst_new_irop .= "\top->ops.dump_node = ${dump_func};\n";
655 if (defined($cmp_attr_func)) {
656 $obst_new_irop .= "\top->ops.node_cmp_attr = ${cmp_attr_func};\n";
658 my $copy_attr_func = $copy_attr{$attr_type};
659 if (!defined($copy_attr_func)) {
660 # don't set a copy_attr function if the node has no additional attributes.
661 if ($attr_type ne "") {
662 $copy_attr_func = $default_copy_attr;
665 if (defined($copy_attr_func)) {
666 $obst_new_irop .= "\top->ops.copy_attr = ${copy_attr_func};\n";
668 if (defined($hash_func)) {
669 $obst_new_irop .= "\top->ops.hash = ${hash_func};\n";
673 $obst_new_irop .= "\tir_op_set_memory_index(op, n_${op}_mem);\n";
674 $obst_new_irop .= "\tir_op_set_fragile_indices(op, pn_${op}_X_regular, pn_${op}_X_except);\n";
676 $obst_new_irop .= "\tset_op_tag(op, $arch\_op_tag);\n";
677 if(defined($n{op_attr_init})) {
678 $obst_new_irop .= "\t".$n{op_attr_init}."\n";
680 $obst_new_irop .= "\top_${op} = op;\n";
682 $obst_free_irop .= "\tfree_ir_op(op_$op); op_$op = NULL;\n";
684 $obst_enum_op .= "\tiro_$op,\n";
686 $obst_header .= "\n";
688 $obst_enum_op .= "\tiro_$arch\_last\n";
689 $obst_enum_op .= "} $arch\_opcodes;\n\n";
693 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
696 #include "gen_$arch\_regalloc_if.h"
697 #include "irverify_t.h"
704 static int $arch\_opcode_start = -1;
705 static int $arch\_opcode_end = -1;
709 # build the FOURCC arguments from $arch
711 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
713 if (length($arch) >= 1) {
714 $a = uc(substr($arch, 0, 1));
717 if (length($arch) >= 2) {
718 $b = uc(substr($arch, 1, 1));
721 if (length($arch) >= 3) {
722 $c = uc(substr($arch, 2, 1));
725 if (length($arch) >= 4) {
726 $d = uc(substr($arch, 3, 1));
731 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
732 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
734 /** Return the opcode number of the first $arch opcode. */
735 int get_$arch\_opcode_first(void) {
736 return $arch\_opcode_start;
739 /** Return the opcode number of the last $arch opcode + 1. */
740 int get_$arch\_opcode_last(void) {
741 return $arch\_opcode_end;
744 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
745 int is_$arch\_op(const ir_op *op) {
746 return get_op_tag(op) == $arch\_op_tag;
749 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
750 int is_$arch\_irn(const ir_node *node) {
751 return is_$arch\_op(get_irn_op(node));
754 int get_$arch\_irn_opcode(const ir_node *node) {
755 if (is_$arch\_irn(node))
756 return get_irn_opcode(node) - $arch\_opcode_start;
763 #define BIT(x) (1 << (x))
770 * Creates the $arch specific Firm machine operations
771 * needed for the assembler irgs.
773 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops)
776 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
778 $arch\_opcode_start = cur_opcode;
780 $arch\_opcode_end = cur_opcode + iro_$arch\_last;
783 void $arch\_free_opcodes(void)
791 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
793 my $creation_time = localtime(time());
799 * \@brief Function prototypes for the new opcode functions.
800 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
801 * Edit $specfile instead.
802 * created by: $0 $specfile $target_dir
803 * \@date $creation_time
805 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
806 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
809 int is_${arch}_irn(const ir_node *node);
810 int is_${arch}_op(const ir_op *op);
812 int get_${arch}_opcode_first(void);
813 int get_${arch}_opcode_last(void);
814 int get_${arch}_irn_opcode(const ir_node *node);
824 # Translates numeric arity into string constant.
826 sub translate_arity {
829 if ($arity =~ /^\d+$/) {
831 return "oparity_zero";
833 elsif ($arity == 1) {
834 return "oparity_unary";
836 elsif ($arity == 2) {
837 return "oparity_binary";
839 elsif ($arity == 3) {
840 return "oparity_trinary";
843 return "oparity_any";
845 } elsif ($arity == $ARITY_VARIABLE) {
846 return "oparity_variable";
847 } elsif ($arity == $ARITY_DYNAMIC) {
848 return "oparity_dynamic";
850 die "Fatal error: Unknown arity $arity";
854 sub mangle_requirements {
859 my @alternatives = split(/ /, $reqs);
860 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
861 $alternatives[$idx] =~ s/!/not_/g;
864 @alternatives = sort @alternatives;
866 my $name = $class."_".join('_', @alternatives);
867 if (defined($flags)) {
876 # Determines whether $name is a specified register class or not.
877 # @return 1 if name is register class, 0 otherwise
881 return 1 if exists($reg_classes{"$name"});
886 # Returns the register class for a given register.
887 # @return class or undef
891 $reg = substr($reg, 1) if ($reg =~ /!.*/);
892 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
897 # Returns the index of a given register within its register class.
898 # @return index or undef
902 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
907 # Remember the register class for each index in the given requirements.
908 # We need this information for requirements like "in_sX" or "out_dX"
909 # @return array of classes corresponding to the requirement for each index
911 sub build_inout_idx_class {
917 my $inout = ($is_in ? "in" : "out");
919 if (exists($n->{"reg_req"}{"$inout"})) {
920 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
922 for (my $idx = 0; $idx <= $#reqs; $idx++) {
924 my ($req,) = split(/:/, $reqs[$idx]);
926 if ($req eq "none") {
928 } elsif (is_reg_class($req)) {
931 my @regs = split(/ /, $req);
932 GET_CLASS: foreach my $reg (@regs) {
933 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
934 $class = "UNKNOWN_CLASS";
936 $class = get_reg_class($reg);
937 if (!defined $class) {
938 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
946 push(@idx_class, $class);
954 # Generates the function for a given $op and a given IN-index
955 # which returns a subset of possible register from a register class
956 # @return classname from which the subset is derived or undef and
957 # pos which corresponds to in/out reference position or undef
959 sub build_subset_class_func {
965 my $different_pos = 0;
971 my $limit_reqs; #used for name mangling
973 # build function header
978 my @regs = split(/ /, shift);
981 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
983 # set/unset registers
984 CHECK_REQS: foreach (@regs) {
985 if (!$is_in && /(!)?in_r(\d+)/) {
986 my $bit_pos = 1 << ($2 - 1);
987 if ($different_pos & $bit_pos) {
989 print STDERR "duplicate !in constraint\n";
991 print STDERR "conflicting !in and in constraints\n";
993 return (undef, undef, undef, undef);
996 if ($same_pos & $bit_pos) {
998 print STDERR "conflicting !in and in constraints\n";
1000 print STDERR "duplicate in constraint\n";
1002 return (undef, undef, undef, undef);
1006 $different_pos |= $bit_pos;
1008 $same_pos |= $bit_pos;
1011 $class = $idx_class[$2 - 1];
1016 if (substr($_, 0, 1) eq "!") {
1017 if (defined($neg) && $neg == 0) {
1018 # we have seen a positiv constraint as first one but this one is negative
1019 # this doesn't make sense
1020 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1021 return (undef, undef, undef, undef);
1024 if (!defined($neg)) {
1028 $_ = substr($_, 1); # skip '!'
1031 if (defined($neg) && $neg == 1) {
1032 # we have seen a negative constraint as first one but this one is positive
1033 # this doesn't make sense
1034 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1035 return (undef, undef, undef, undef);
1042 # check if register belongs to one of the given classes
1043 $temp = get_reg_class($_);
1044 if (!defined($temp)) {
1045 print STDERR "Unknown register '$_'!\n";
1046 return (undef, undef, undef, undef);
1050 if (!defined($class)) {
1052 } elsif ($class ne $temp) {
1053 # all registers must belong to the same class
1054 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1055 return (undef, undef, undef, undef);
1058 # calculate position inside the initializer bitfield (only 32 bits per
1060 my $regidx = get_reg_index($_);
1061 my $arrayp = $regidx / 32;
1062 push(@{$limit_array[$arrayp]}, $_);
1063 $limit_reqs .= "$_ ";
1066 # don't allow ignore regs in negative constraints
1068 my @cur_class = @{ $reg_classes{"$class"} };
1069 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1070 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1071 my $reg = $cur_class[$idx]{"name"};
1072 my $regix = get_reg_index($reg);
1073 my $arrayp = $regix / 32;
1074 push(@{$limit_array[$arrayp]}, $reg);
1075 $limit_reqs .= "$reg ";
1080 if ($has_limit == 1) {
1081 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1083 if(defined($limit_bitsets{$limit_name})) {
1084 $limit_name = $limit_bitsets{$limit_name};
1085 return ($class, $limit_name, $same_pos, $different_pos);
1088 $limit_bitsets{$limit_name} = $limit_name;
1090 $obst_limit_func .= "static const unsigned $limit_name\[] = { ";
1092 my $limitbitsetlen = $regclass2len{$class};
1093 my $limitarraylen = ($limitbitsetlen+31) / 32;
1094 for(my $i = 0; $i < $limitarraylen; $i++) {
1096 my $limitarraypart = $limit_array[$i];
1100 $obst_limit_func .= ", ";
1104 $temp = "0xFFFFFFFF";
1106 foreach my $reg (@{$limitarraypart}) {
1109 } elsif(defined($temp)) {
1112 my $firstreg = uc($reg_classes{$class}[0]->{"name"});
1113 my $classuc = uc($class);
1114 my $reguc = uc($reg);
1115 $temp .= "BIT(REG_${classuc}_${reguc})";
1117 $obst_limit_func .= $temp || "0";
1119 $obst_limit_func .= " };\n";
1122 return ($class, $limit_name, $same_pos, $different_pos);
1126 # Generate register requirements structure
1128 sub generate_requirements {
1129 my ($reqs, $flags) = split(/:/, shift);
1139 if (defined($flags)) {
1140 foreach my $f (split(/|/, $flags)) {
1142 push(@req_type_mask, "arch_register_req_type_ignore");
1143 } elsif ($f eq "S") {
1144 push(@req_type_mask, "arch_register_req_type_produces_sp");
1145 } elsif ($f eq "a") {
1146 push(@req_type_mask, "arch_register_req_type_aligned");
1147 } elsif ($f eq "2" or $f eq "4" or $f eq "8") {
1153 if ($reqs eq "none") {
1157 arch_register_req_type_none,
1158 NULL, /* regclass */
1159 NULL, /* limit bitset */
1161 0, /* different pos */
1166 } elsif (is_reg_class($reqs)) {
1167 push(@req_type_mask, "arch_register_req_type_normal");
1168 my $reqtype = join(" | ", @req_type_mask);
1173 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1174 NULL, /* limit bitset */
1176 0, /* different pos */
1183 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1184 = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1186 if (!defined($regclass)) {
1187 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1190 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1191 push(@req_type_mask, "arch_register_req_type_limited");
1193 if ($same_pos != 0) {
1194 push(@req_type_mask, "arch_register_req_type_should_be_same");
1196 if ($different_pos != 0) {
1197 push(@req_type_mask, "arch_register_req_type_must_be_different");
1199 my $reqtype = join(" | ", @req_type_mask);
1201 if(!defined($limit_bitset)) {
1202 $limit_bitset = "NULL";
1209 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1211 ${same_pos}, /* same pos */
1212 ${different_pos}, /* different pos */
1219 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1220 if(defined($requirements{$name})) {
1223 $requirements{$name} = $name;
1224 $obst_reg_reqs .= "static const arch_register_req_t ${name} = ${result}\n";