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];
37 our $additional_opcodes;
40 our $default_op_attr_type;
41 our $default_attr_type;
42 our $default_cmp_attr;
43 our $default_copy_attr;
45 our $custom_init_attr_func;
49 our %custom_irn_flags;
56 unless ($return = do $specfile) {
57 die "Fatal error: couldn't parse $specfile: $@" if $@;
58 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
59 die "Fatal error: couldn't run $specfile" unless $return;
63 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
64 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
66 if(!defined($default_attr_type)) {
67 $default_attr_type = "${arch}_attr_t";
71 "$default_attr_type" => "\tinit_${arch}_attributes(res, irn_flags_, in_reqs, n_res);",
74 if(!defined($default_cmp_attr)) {
75 $default_cmp_attr = "${arch}_compare_attr";
79 "${default_attr_type}" => "${default_cmp_attr}",
83 # create c code file from specs
87 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
88 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
89 my $obst_constructor; # stack for node constructor functions
90 my @obst_new_irop; # stack for the new_ir_op calls
91 my @obst_free_irop; # stack for free_ir_op calls
92 my @obst_enum_op; # stack for creating the <arch>_opcode enum
93 my $obst_header; # stack for function prototypes
94 my @obst_is_archirn; # stack for the is_$arch_irn() function
95 my @obst_cmp_attr; # stack for the compare attribute functions
96 my $obst_proj = ""; # stack for the pn_ numbers
101 my $n_opcodes = 0; # number of opcodes
102 my $ARITY_VARIABLE = -1;
103 my $ARITY_DYNAMIC = -2;
104 my %requirements = ();
105 my %limit_bitsets = ();
107 my %regclass2len = ();
109 # build register->class hashes
110 foreach my $class_name (keys(%reg_classes)) {
111 my @class = @{ $reg_classes{"$class_name"} };
112 my $old_classname = $class_name;
116 $class_name = $arch."_".$class_name;
120 $reg2class{$_->{name}} = {
121 "class" => $old_classname,
127 $regclass2len{$old_classname} = $idx;
131 # for registering additional opcodes
132 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
134 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
135 $obst_header .= "void ${arch}_free_opcodes(void);\n";
137 sub create_constructor {
146 $suffix = "_${name}";
150 if (exists($n->{mode})) {
151 $known_mode = $n->{mode};
156 if(exists($n->{"arity"})) {
157 $arity = $n->{"arity"};
158 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
159 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
160 } elsif (exists($n->{"ins"})) {
161 $arity = scalar(@{ $n->{"ins"} });
163 if($arity eq "variable") {
164 $arity = $ARITY_VARIABLE;
165 } elsif($arity eq "dynamic") {
166 $arity = $ARITY_DYNAMIC;
169 # determine out arity
171 if(exists($n->{"out_arity"})) {
172 $out_arity = $n->{"out_arity"};
173 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
174 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
175 } elsif (exists($n->{"outs"})) {
176 $out_arity = scalar(@{ $n->{"outs"} });
178 if($out_arity eq "variable") {
179 $out_arity = $ARITY_VARIABLE;
180 } elsif($out_arity eq "dynamic") {
181 $out_arity = $ARITY_DYNAMIC;
183 if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
184 $known_mode = "mode_T";
187 my $comment = $n->{"comment"};
188 if(!exists($n->{"comment"})) {
189 $comment = "construct ${orig_op} node";
196 $obst_constructor .= $comment;
198 # create constructor head
199 my $complete_args = "";
202 $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
203 if (!exists($n->{"args"})) { # default args
204 if ($arity == $ARITY_VARIABLE) {
205 $complete_args = ", int arity, ir_node *in[]";
206 } elsif ($arity == $ARITY_DYNAMIC) {
209 for (my $i = 0; $i < $arity; $i++) {
210 my $opname = "op${i}";
211 if (exists($n->{"ins"})) {
212 my @ins = @{ $n->{"ins"} };
216 $complete_args .= ", ir_node *${opname}";
219 if ($out_arity == $ARITY_VARIABLE) {
220 $complete_args .= ", int n_res";
223 if (!defined($known_mode)) {
224 $complete_args .= ", ir_mode *mode";
226 } else { # user defined args
227 for my $href (@{ $n->{"args"} }) {
228 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
229 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
233 # we have additional attribute arguements
234 if (exists($n->{"attr"})) {
235 $complete_args .= ", ".$n->{"attr"};
238 $temp .= "$complete_args)";
239 $obst_constructor .= "${temp}\n{\n";
241 $obst_header .= $comment;
242 $obst_header .= "${temp};\n";
244 # emit constructor code
246 ir_graph *irg = get_irn_irg(block);
247 ir_op *op = op_${arch}_${op};
248 arch_irn_flags_t irn_flags_ = arch_irn_flags_none;
250 backend_info_t *info;
253 if($arity == $ARITY_DYNAMIC) {
258 } elsif($arity == $ARITY_VARIABLE) {
273 if($out_arity == $ARITY_DYNAMIC) {
277 } elsif($out_arity == $ARITY_VARIABLE) {
280 int n_res = ${out_arity};
284 if (defined($known_mode)) {
286 ir_mode *mode = ${known_mode};
290 undef my $in_req_var;
291 undef my $out_req_var;
293 my $set_out_reqs = "";
295 # set up static variables for requirements and registers
296 if (exists($n->{"reg_req"})) {
297 my %req = %{ $n->{"reg_req"} };
301 @in = @{ $req{"in"} } if (exists($req{"in"}));
303 @out = @{ $req{"out"} } if exists(($req{"out"}));
305 for(my $idx = 0; $idx < $#in; $idx++) {
307 generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
309 for(my $idx = 0; $idx < $#out; $idx++) {
310 my $req = $out[$idx];
311 generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
315 if($arity >= 0 && scalar(@in) != $arity) {
316 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
319 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
321 for ($idx = 0; $idx <= $#in; $idx++) {
323 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
324 $temp .= "\t\t& ${reqstruct},\n";
329 die "Fatal error: need in requirements for ${op}\n";
331 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
335 if($out_arity >= 0 && scalar(@out) != $out_arity) {
336 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
339 for ($idx = 0; $idx <= $#out; $idx++) {
340 my $req = $out[$idx];
341 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
342 $set_out_reqs .= <<EOF;
343 info->out_infos[${idx}].req = &${reqstruct};
348 die "Fatal error: need out requirements for ${op}\n";
352 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
354 my $attr_type = $on->{attr_type};
355 if(exists($n->{"init_attr"})) {
356 $temp .= "\t${attr_type} *attr;\n";
362 $temp .= "\t/* construct in array */\n";
363 for (my $i = 0; $i < $arity; $i++) {
364 my $opname = "op${i}";
365 if (exists($n->{"ins"})) {
366 my @ins = @{ $n->{"ins"} };
370 $temp .= "\tin[${i}] = ${opname};\n";
376 if (exists($n->{"irn_flags"})) {
377 $temp .= "\t/* flags */\n";
378 my %known_irn_flags = (
379 "none" => "arch_irn_flags_none",
380 "dont_spill" => "arch_irn_flags_dont_spill",
381 "rematerializable" => "arch_irn_flags_rematerializable",
382 "modify_flags" => "arch_irn_flags_modify_flags",
383 "simple_jump" => "arch_irn_flags_simple_jump",
384 "not_scheduled" => "arch_irn_flags_not_scheduled",
386 if (%custom_irn_flags) {
387 %known_irn_flags = (%known_irn_flags, %custom_irn_flags);
389 foreach my $flag (@{$n->{"irn_flags"}}) {
390 if (not defined($known_irn_flags{$flag})) {
391 print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
393 $temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
399 # lookup init function
400 my $attr_init_code = "(void)in;(void)irn_flags_;(void)in_reqs;(void)n_res;";
401 if ($attr_type ne "") {
402 $attr_init_code = $init_attr{$attr_type};
403 if(!defined($attr_init_code)) {
404 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
408 if(defined($custom_init_attr_func)) {
409 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
411 if(defined($n->{custominit})) {
412 $custominit .= $n->{custominit};
418 res = new_ir_node(dbgi, irg, block, op, mode, arity, in);
420 /* init node attributes */
423 info = be_get_info(res);
424 (void) info; /* avoid potential warning */
429 if (exists($n->{"init_attr"})) {
430 $temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
431 $temp .= "\t(void) attr; /* avoid potential warning */\n";
432 $temp .= "\t".$n->{"init_attr"}."\n";
437 res = optimize_node(res);
438 irn_verify_irg(res, irg);
443 $obst_constructor .= $temp;
445 # close constructor function
446 $obst_constructor .= "}\n\n";
449 push(@obst_enum_op, "typedef enum ${arch}_opcodes {\n");
450 foreach my $op (keys(%nodes)) {
451 my %n = %{ $nodes{"$op"} };
459 if(exists($n{"arity"})) {
460 $arity = $n{"arity"};
461 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
462 $arity = scalar(@{ $n{"reg_req"}{"in"} });
463 } elsif (exists($n{"ins"})) {
464 $arity = scalar(@{ $n{"ins"} });
466 if($arity eq "variable") {
467 $arity = $ARITY_VARIABLE;
468 } elsif($arity eq "dynamic") {
469 $arity = $ARITY_DYNAMIC;
472 # determine out arity
474 if(exists($n{"out_arity"})) {
475 $out_arity = $n{"out_arity"};
476 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
477 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
478 } elsif (exists($n{"outs"})) {
479 $out_arity = scalar(@{ $n{"outs"} });
481 if($out_arity eq "variable") {
482 $out_arity = $ARITY_VARIABLE;
483 } elsif($out_arity eq "dynamic") {
484 $out_arity = $ARITY_DYNAMIC;
491 # define proj numbers and in numbers
492 if (exists($n{"outs"})) {
495 @outs = @{ $n{"outs"} };
496 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
497 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
500 $num_outs = $#outs + 1;
503 $obst_proj .= "\nenum pn_${op} {\n";
505 for (my $idx = 0; $idx <= $#outs; $idx++) {
506 # check, if we have additional flags annotated to out
507 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
508 push(@out_flags, $1);
509 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
511 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
514 $obst_proj .= "};\n";
516 # outs have names, it must be a mode_T node
517 if (!defined($n{mode})) {
521 if (exists($n{"ins"})) {
524 @ins = @{ $n{"ins"} };
525 if($arity >= 0 && scalar(@ins) != $arity) {
526 die "Fatal error: Op ${op} has different number of ins and arity\n";
530 $obst_proj .= "\nenum n_$op {\n";
531 for (my $idx = 0; $idx <= $#ins; $idx++) {
532 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
534 $obst_proj .= "};\n";
539 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
540 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
541 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
543 push(@obst_is_archirn, "is_$op(node)");
545 $obst_header .= <<EOF;
546 extern ir_op *op_${op};
547 ir_op *get_op_${op}(void);
548 int is_${op}(const ir_node *n);
551 my $attr_type= $n{attr_type};
552 if(!defined($attr_type)) {
553 $attr_type = $default_attr_type;
554 $n{attr_type} = $attr_type;
557 # determine hash function
559 if (exists($n{"hash_func"})) {
560 $hash_func = $n{"hash_func"};
563 # determine compare function
565 if (exists($n{"cmp_attr"})) {
566 my $cmpcode = $n{"cmp_attr"};
568 push(@obst_cmp_attr, "static int cmp_attr_$op(const ir_node *a, const ir_node *b) {\n");
569 if($cmpcode =~ m/attr_a/) {
570 push(@obst_cmp_attr, "\tconst ${attr_type} *attr_a = get_irn_generic_attr_const(a);\n");
572 push(@obst_cmp_attr, "\t(void) a;\n");
574 if($cmpcode =~ m/attr_b/) {
575 push(@obst_cmp_attr, "\tconst ${attr_type} *attr_b = get_irn_generic_attr_const(b);\n");
577 push(@obst_cmp_attr, "\t(void) b;\n");
579 push(@obst_cmp_attr, "\t${cmpcode}\n");
580 push(@obst_cmp_attr, "}\n\n");
582 $cmp_attr_func = "cmp_attr_${op}";
583 } elsif ($attr_type eq "") {
584 $cmp_attr_func = "NULL";
586 if(defined($compare_attr{${attr_type}})) {
587 $cmp_attr_func = $compare_attr{${attr_type}};
589 die "Fatal error: No compare function defined for ${attr_type} attributes.";
594 if (exists($n{constructors})) {
595 %constructors = %{ $n{constructors} };
597 # Create 1 default constructor
598 my %constructor = ();
599 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
600 "reg_req", "init_attr", "irn_flags", "mode", "arity",
601 "out_arity", "custominit") {
602 if (defined($n{$a})) {
603 $constructor{$a} = $n{$a};
606 %constructors = ( "" => \%constructor );
609 foreach my $constr (keys(%constructors)) {
610 my %cstr = %{ $constructors{$constr} };
611 # Copy some values from outer node if they don't exists in the constr
612 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
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 push(@obst_new_irop, $temp);
653 push(@obst_new_irop, "\top->ops.be_ops = be_ops;\n");
654 push(@obst_new_irop, "\top->ops.dump_node = ${dump_func};\n");
655 if (defined($cmp_attr_func)) {
656 push(@obst_new_irop, "\top->ops.node_cmp_attr = ${cmp_attr_func};\n");
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 push(@obst_new_irop, "\top->ops.copy_attr = ${copy_attr_func};\n");
668 if (defined($hash_func)) {
669 push(@obst_new_irop, "\top->ops.hash = ${hash_func};\n");
673 push(@obst_new_irop, "\tir_op_set_memory_index(op, n_${op}_mem);\n");
674 push(@obst_new_irop, "\tir_op_set_fragile_indices(op, pn_${op}_X_regular, pn_${op}_X_except);\n");
676 push(@obst_new_irop, "\tset_op_tag(op, $arch\_op_tag);\n");
677 if(defined($n{op_attr_init})) {
678 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
680 push(@obst_new_irop, "\top_${op} = op;\n");
682 push(@obst_free_irop, "\tfree_ir_op(op_$op); op_$op = NULL;\n");
684 push(@obst_enum_op, "\tiro_$op,\n");
686 $obst_header .= "\n";
688 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
689 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
690 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
691 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
695 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
697 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n";
698 print OUT "#include \"irverify_t.h\"\n";
699 print OUT "#include \"fourcc.h\"\n";
701 print OUT @obst_cmp_attr;
703 print OUT @obst_opvar;
705 print OUT @obst_get_opvar;
710 static int $arch\_opcode_start = -1;
711 static int $arch\_opcode_end = -1;
715 # build the FOURCC arguments from $arch
717 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
719 if (length($arch) >= 1) {
720 $a = uc(substr($arch, 0, 1));
723 if (length($arch) >= 2) {
724 $b = uc(substr($arch, 1, 1));
727 if (length($arch) >= 3) {
728 $c = uc(substr($arch, 2, 1));
731 if (length($arch) >= 4) {
732 $d = uc(substr($arch, 3, 1));
735 print OUT<<ENDOFISIRN;
737 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
738 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
740 /** Return the opcode number of the first $arch opcode. */
741 int get_$arch\_opcode_first(void) {
742 return $arch\_opcode_start;
745 /** Return the opcode number of the last $arch opcode + 1. */
746 int get_$arch\_opcode_last(void) {
747 return $arch\_opcode_end;
750 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
751 int is_$arch\_op(const ir_op *op) {
752 return get_op_tag(op) == $arch\_op_tag;
755 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
756 int is_$arch\_irn(const ir_node *node) {
757 return is_$arch\_op(get_irn_op(node));
760 int get_$arch\_irn_opcode(const ir_node *node) {
761 if (is_$arch\_irn(node))
762 return get_irn_opcode(node) - $arch\_opcode_start;
772 #define BIT(x) (1 << (x))
776 print OUT @obst_limit_func;
779 print OUT @obst_reg_reqs;
782 print OUT<<ENDOFMAIN;
786 * Creates the $arch specific Firm machine operations
787 * needed for the assembler irgs.
789 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops)
792 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
794 $arch\_opcode_start = cur_opcode;
797 print OUT @obst_new_irop;
799 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
800 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
801 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
803 print OUT <<ENDOFMAIN;
806 void $arch\_free_opcodes(void)
810 print OUT @obst_free_irop;
812 print OUT <<ENDOFMAIN;
818 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
820 my $creation_time = localtime(time());
826 * \@brief Function prototypes for the new opcode functions.
827 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
828 * Edit $specfile instead.
829 * created by: $0 $specfile $target_dir
830 * \@date $creation_time
832 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
833 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
837 print OUT @obst_enum_op;
839 int is_${arch}_irn(const ir_node *node);
840 int is_${arch}_op(const ir_op *op);
842 int get_${arch}_opcode_first(void);
843 int get_${arch}_opcode_last(void);
844 int get_${arch}_irn_opcode(const ir_node *node);
854 # Translates numeric arity into string constant.
856 sub translate_arity {
859 if ($arity =~ /^\d+$/) {
861 return "oparity_zero";
863 elsif ($arity == 1) {
864 return "oparity_unary";
866 elsif ($arity == 2) {
867 return "oparity_binary";
869 elsif ($arity == 3) {
870 return "oparity_trinary";
873 return "oparity_any";
875 } elsif ($arity == $ARITY_VARIABLE) {
876 return "oparity_variable";
877 } elsif ($arity == $ARITY_DYNAMIC) {
878 return "oparity_dynamic";
880 die "Fatal error: Unknown arity $arity";
884 sub mangle_requirements {
889 my @alternatives = split(/ /, $reqs);
890 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
891 $alternatives[$idx] =~ s/!/not_/g;
894 @alternatives = sort @alternatives;
896 my $name = $class."_".join('_', @alternatives);
897 if (defined($flags)) {
906 # Determines whether $name is a specified register class or not.
907 # @return 1 if name is register class, 0 otherwise
911 return 1 if exists($reg_classes{"$name"});
916 # Returns the register class for a given register.
917 # @return class or undef
921 $reg = substr($reg, 1) if ($reg =~ /!.*/);
922 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
927 # Returns the index of a given register within its register class.
928 # @return index or undef
932 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
937 # Remember the register class for each index in the given requirements.
938 # We need this information for requirements like "in_sX" or "out_dX"
939 # @return array of classes corresponding to the requirement for each index
941 sub build_inout_idx_class {
947 my $inout = ($is_in ? "in" : "out");
949 if (exists($n->{"reg_req"}{"$inout"})) {
950 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
952 for (my $idx = 0; $idx <= $#reqs; $idx++) {
954 my ($req,) = split(/:/, $reqs[$idx]);
956 if ($req eq "none") {
958 } elsif (is_reg_class($req)) {
961 my @regs = split(/ /, $req);
962 GET_CLASS: foreach my $reg (@regs) {
963 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
964 $class = "UNKNOWN_CLASS";
966 $class = get_reg_class($reg);
967 if (!defined $class) {
968 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
976 push(@idx_class, $class);
984 # Generates the function for a given $op and a given IN-index
985 # which returns a subset of possible register from a register class
986 # @return classname from which the subset is derived or undef and
987 # pos which corresponds to in/out reference position or undef
989 sub build_subset_class_func {
995 my $different_pos = 0;
1001 my $limit_reqs; #used for name mangling
1003 # build function header
1008 my @regs = split(/ /, shift);
1011 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1013 # set/unset registers
1014 CHECK_REQS: foreach (@regs) {
1015 if (!$is_in && /(!)?in_r(\d+)/) {
1016 my $bit_pos = 1 << ($2 - 1);
1017 if ($different_pos & $bit_pos) {
1019 print STDERR "duplicate !in constraint\n";
1021 print STDERR "conflicting !in and in constraints\n";
1023 return (undef, undef, undef, undef);
1026 if ($same_pos & $bit_pos) {
1028 print STDERR "conflicting !in and in constraints\n";
1030 print STDERR "duplicate in constraint\n";
1032 return (undef, undef, undef, undef);
1036 $different_pos |= $bit_pos;
1038 $same_pos |= $bit_pos;
1041 $class = $idx_class[$2 - 1];
1046 if (substr($_, 0, 1) eq "!") {
1047 if (defined($neg) && $neg == 0) {
1048 # we have seen a positiv constraint as first one but this one is negative
1049 # this doesn't make sense
1050 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1051 return (undef, undef, undef, undef);
1054 if (!defined($neg)) {
1058 $_ = substr($_, 1); # skip '!'
1061 if (defined($neg) && $neg == 1) {
1062 # we have seen a negative constraint as first one but this one is positive
1063 # this doesn't make sense
1064 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1065 return (undef, undef, undef, undef);
1072 # check if register belongs to one of the given classes
1073 $temp = get_reg_class($_);
1074 if (!defined($temp)) {
1075 print STDERR "Unknown register '$_'!\n";
1076 return (undef, undef, undef, undef);
1080 if (!defined($class)) {
1082 } elsif ($class ne $temp) {
1083 # all registers must belong to the same class
1084 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1085 return (undef, undef, undef, undef);
1088 # calculate position inside the initializer bitfield (only 32 bits per
1090 my $regidx = get_reg_index($_);
1091 my $arrayp = $regidx / 32;
1092 push(@{$limit_array[$arrayp]}, $_);
1093 $limit_reqs .= "$_ ";
1096 # don't allow ignore regs in negative constraints
1098 my @cur_class = @{ $reg_classes{"$class"} };
1099 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1100 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1101 my $reg = $cur_class[$idx]{"name"};
1102 my $regix = get_reg_index($reg);
1103 my $arrayp = $regix / 32;
1104 push(@{$limit_array[$arrayp]}, $reg);
1105 $limit_reqs .= "$reg ";
1110 if ($has_limit == 1) {
1111 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1113 if(defined($limit_bitsets{$limit_name})) {
1114 $limit_name = $limit_bitsets{$limit_name};
1115 return ($class, $limit_name, $same_pos, $different_pos);
1118 $limit_bitsets{$limit_name} = $limit_name;
1120 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1122 my $limitbitsetlen = $regclass2len{$class};
1123 my $limitarraylen = ($limitbitsetlen+31) / 32;
1124 for(my $i = 0; $i < $limitarraylen; $i++) {
1126 my $limitarraypart = $limit_array[$i];
1130 push(@obst_limit_func, ", ");
1134 $temp = "0xFFFFFFFF";
1136 foreach my $reg (@{$limitarraypart}) {
1139 } elsif(defined($temp)) {
1142 my $firstreg = uc($reg_classes{$class}[0]->{"name"});
1143 my $classuc = uc($class);
1144 my $reguc = uc($reg);
1145 $temp .= "BIT(REG_${classuc}_${reguc})";
1147 if(defined($temp)) {
1148 push(@obst_limit_func, "${temp}");
1150 push(@obst_limit_func, "0");
1153 push(@obst_limit_func, " };\n");
1156 return ($class, $limit_name, $same_pos, $different_pos);
1160 # Generate register requirements structure
1162 sub generate_requirements {
1163 my ($reqs, $flags) = split(/:/, shift);
1173 if (defined($flags)) {
1174 foreach my $f (split(/|/, $flags)) {
1176 push(@req_type_mask, "arch_register_req_type_ignore");
1177 } elsif ($f eq "S") {
1178 push(@req_type_mask, "arch_register_req_type_produces_sp");
1179 } elsif ($f eq "a") {
1180 push(@req_type_mask, "arch_register_req_type_aligned");
1181 } elsif ($f eq "2" or $f eq "4" or $f eq "8") {
1187 if ($reqs eq "none") {
1191 arch_register_req_type_none,
1192 NULL, /* regclass */
1193 NULL, /* limit bitset */
1195 0, /* different pos */
1200 } elsif (is_reg_class($reqs)) {
1201 push(@req_type_mask, "arch_register_req_type_normal");
1202 my $reqtype = join(" | ", @req_type_mask);
1207 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1208 NULL, /* limit bitset */
1210 0, /* different pos */
1217 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1218 = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1220 if (!defined($regclass)) {
1221 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1224 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1225 push(@req_type_mask, "arch_register_req_type_limited");
1227 if ($same_pos != 0) {
1228 push(@req_type_mask, "arch_register_req_type_should_be_same");
1230 if ($different_pos != 0) {
1231 push(@req_type_mask, "arch_register_req_type_must_be_different");
1233 my $reqtype = join(" | ", @req_type_mask);
1235 if(!defined($limit_bitset)) {
1236 $limit_bitset = "NULL";
1243 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1245 ${same_pos}, /* same pos */
1246 ${different_pos}, /* different pos */
1253 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1254 if(defined($requirements{$name})) {
1257 $requirements{$name} = $name;
1258 push(@obst_reg_reqs, <<EOF);
1259 static const arch_register_req_t ${name} = ${result}