4 # Copyright (C) 1995-2007 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
31 my $specfile = $ARGV[0];
32 my $target_dir = $ARGV[1];
38 our $additional_opcodes;
42 our $default_attr_type;
43 our $default_cmp_attr;
44 our $default_copy_attr;
46 our $custom_init_attr_func;
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";
69 if(!defined(%init_attr)) {
71 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
74 if(!defined($default_cmp_attr)) {
75 $default_cmp_attr = "${arch}_compare_attr";
77 if(!defined(%compare_attr)) {
79 "${default_attr_type}" => "${default_cmp_attr}",
83 # Operands are really just nodes with some special constraints, we check
84 # these and create new entries in the nodes hashmap
85 foreach my $op (keys(%operands)) {
86 my %operand = %{ $operands{"$op"} };
90 if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
91 if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
92 if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
93 if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
94 if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
97 foreach my $flag (keys(%operand)) {
98 $op_node{$flag} = $operand{$flag};
100 $op_node{op_flags} = "O";
101 $op_node{cmp_attr} = 'return 1;';
102 $op_node{mode} = 'mode_ANY';
104 $nodes{$op} = \%op_node;
107 #print Dumper(%nodes);
108 #print Dumper(%operands);
110 # create c code file from specs
114 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
115 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
116 my @obst_constructor; # stack for node constructor functions
117 my @obst_new_irop; # stack for the new_ir_op calls
118 my @obst_enum_op; # stack for creating the <arch>_opcode enum
119 my @obst_header; # stack for function prototypes
120 my @obst_is_archirn; # stack for the is_$arch_irn() function
121 my @obst_cmp_attr; # stack for the compare attribute functions
122 my @obst_proj; # stack for the pn_ numbers
127 my $n_opcodes = 0; # number of opcodes
128 my $ARITY_VARIABLE = -1;
129 my $ARITY_DYNAMIC = -2;
130 my %requirements = ();
131 my %limit_bitsets = ();
133 my %regclass2len = ();
135 # build register->class hashes
136 foreach my $class_name (keys(%reg_classes)) {
137 my @class = @{ $reg_classes{"$class_name"} };
138 my $old_classname = $class_name;
142 $class_name = $arch."_".$class_name;
146 $reg2class{$_->{name}} = {
147 "class" => $old_classname,
153 $regclass2len{$old_classname} = $idx;
157 # for registering additional opcodes
158 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
160 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
162 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
163 foreach my $op (keys(%nodes)) {
164 my %n = %{ $nodes{"$op"} };
172 if(exists($n{"arity"})) {
173 $arity = $n{"arity"};
174 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
175 $arity = scalar(@{ $n{"reg_req"}{"in"} });
176 } elsif (exists($n{"ins"})) {
177 $arity = scalar(@{ $n{"ins"} });
179 if($arity eq "variable") {
180 $arity = $ARITY_VARIABLE;
181 } elsif($arity eq "dynamic") {
182 $arity = $ARITY_DYNAMIC;
185 # determine out arity
187 if(exists($n{"out_arity"})) {
188 $out_arity = $n{"out_arity"};
189 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
190 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
191 } elsif (exists($n{"outs"})) {
192 $out_arity = scalar(@{ $n{"outs"} });
194 if($out_arity eq "variable") {
195 $out_arity = $ARITY_VARIABLE;
196 } elsif($out_arity eq "dynamic") {
197 $out_arity = $ARITY_DYNAMIC;
204 # define proj numbers and in numbers
205 if (exists($n{"outs"})) {
208 @outs = @{ $n{"outs"} };
209 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
210 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
213 $num_outs = $#outs + 1;
215 push(@obst_proj, "\nenum pn_$op {\n");
217 for (my $idx = 0; $idx <= $#outs; $idx++) {
218 # check, if we have additional flags annotated to out
219 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
220 push(@out_flags, $1);
221 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
223 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
226 push(@obst_proj, "};\n");
227 # outs have names, it must be a mode_T node
228 $known_mode = "mode_T";
230 if (exists($n{"ins"})) {
233 @ins = @{ $n{"ins"} };
234 if($arity >= 0 && scalar(@ins) != $arity) {
235 die "Fatal error: Op ${op} has different number of ins and arity\n";
238 push(@obst_proj, "\nenum n_$op {\n");
240 for (my $idx = 0; $idx <= $#ins; $idx++) {
241 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
244 push(@obst_proj, "};\n");
248 if (exists($n{"mode"})) {
249 $known_mode = $n{"mode"};
252 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
253 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
254 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
256 push(@obst_is_archirn, "is_$op(node)");
258 push(@obst_header, "extern ir_op *op_$op;\n");
259 push(@obst_header, "ir_op *get_op_$op(void);\n");
260 push(@obst_header, "int is_$op(const ir_node *n);\n");
262 my $attr_type= $n{"attr_type"};
263 if(!defined($attr_type)) {
264 $attr_type = $default_attr_type;
267 # determine compare function
269 if (exists($n{"cmp_attr"})) {
270 my $cmpcode = $n{"cmp_attr"};
272 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
273 if($cmpcode =~ m/attr_a/) {
274 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
276 push(@obst_cmp_attr, "\t(void) a;\n");
278 if($cmpcode =~ m/attr_b/) {
279 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
281 push(@obst_cmp_attr, "\t(void) b;\n");
283 push(@obst_cmp_attr, "\t${cmpcode}\n");
284 push(@obst_cmp_attr, "}\n\n");
286 $cmp_attr_func = "cmp_attr_${op}";
288 if(defined($compare_attr{${attr_type}})) {
289 $cmp_attr_func = $compare_attr{${attr_type}};
291 die "Fatal error: No compare function defined for ${attr_type} attributes.";
295 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
296 # we explicitly skip the constructor if the specification entry says NONE
298 my $comment = $n{"comment"};
299 if(!exists($n{"comment"})) {
300 $comment = "construct ${orig_op} node";
307 push(@obst_constructor, $comment);
309 # create constructor head
310 my $complete_args = "";
313 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
314 if (!exists($n{"args"})) { # default args
315 if ($arity == $ARITY_VARIABLE) {
316 $complete_args = ", int arity, ir_node *in[]";
317 } elsif ($arity == $ARITY_DYNAMIC) {
320 for (my $i = 0; $i < $arity; $i++) {
321 my $opname = "op${i}";
322 if (exists($n{"ins"})) {
323 my @ins = @{ $n{"ins"} };
327 $complete_args .= ", ir_node *${opname}";
330 if ($out_arity == $ARITY_VARIABLE) {
331 $complete_args .= ", int n_res";
334 if (!defined($known_mode)) {
335 $complete_args .= ", ir_mode *mode";
337 } else { # user defined args
338 for my $href (@{ $n{"args"} }) {
339 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
340 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
344 # we have additional attribute arguements
345 if (exists($n{"attr"})) {
346 $complete_args .= ", ".$n{"attr"};
349 $temp .= "$complete_args)";
350 push(@obst_constructor, $temp."\n{\n");
351 push(@obst_header, $comment);
352 push(@obst_header, $temp.";\n");
354 # emit constructor code
355 if (!exists($n{"rd_constructor"})) { # default constructor
356 $temp = "\tir_node *res;\n";
357 $temp .= "\tir_op *op = op_${op};\n";
358 $temp .= "\tint flags = 0;\n";
360 if($arity == $ARITY_DYNAMIC) {
361 $temp .= "\tint arity = -1;\n";
362 $temp .= "\tir_node **in = NULL;\n";
363 } elsif($arity == $ARITY_VARIABLE) {
365 $temp .= "\tint arity = $arity;\n";
367 $temp .= "\tir_node *in[$arity];\n";
369 $temp .= "\tir_node **in = NULL;\n";
372 if($out_arity == $ARITY_DYNAMIC) {
373 $temp .= "\tint n_res = -1;\n";
374 } elsif($out_arity == $ARITY_VARIABLE) {
376 $temp .= "\tint n_res = ${out_arity};\n";
379 my $latency = $n{"latency"};
380 if (!defined($latency)) {
383 $temp .= "\tunsigned latency = ${latency};\n";
385 if (defined($known_mode)) {
386 $temp .= "\tir_mode *mode = ${known_mode};\n";
389 # set up static variables for cpu execution unit assigments
390 if (exists($n{"units"})) {
391 $temp .= gen_execunit_list_initializer($n{"units"});
393 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
396 undef my $in_req_var;
397 undef my $out_req_var;
399 # set up static variables for requirements and registers
400 if (exists($n{"reg_req"})) {
401 my %req = %{ $n{"reg_req"} };
405 @in = @{ $req{"in"} } if (exists($req{"in"}));
407 @out = @{ $req{"out"} } if exists(($req{"out"}));
409 for(my $idx = 0; $idx < $#in; $idx++) {
411 generate_requirements($req, \%n, $op, $idx, 1);
413 for(my $idx = 0; $idx < $#out; $idx++) {
414 my $req = $out[$idx];
415 generate_requirements($req, \%n, $op, $idx, 0);
419 if($arity >= 0 && scalar(@in) != $arity) {
420 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
423 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
425 for ($idx = 0; $idx <= $#in; $idx++) {
427 my $reqstruct = generate_requirements($req, \%n, $op, $idx, 1);
428 $temp .= "\t\t& ${reqstruct},\n";
433 die "Fatal error: need in requirements for ${op}\n";
435 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
439 if($out_arity >= 0 && scalar(@out) != $out_arity) {
440 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
443 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
445 for ($idx = 0; $idx <= $#out; $idx++) {
446 my $req = $out[$idx];
447 my $reqstruct = generate_requirements($req, \%n, $op, $idx, 0);
448 $temp .= "\t\t& ${reqstruct},\n";
453 die "Fatal error: need out requirements for ${op}\n";
455 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
458 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
459 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
461 if(exists($n{"init_attr"})) {
462 $temp .= "\t${attr_type} *attr;\n";
468 $temp .= "\t/* construct in array */\n";
469 for (my $i = 0; $i < $arity; $i++) {
470 my $opname = "op${i}";
471 if (exists($n{"ins"})) {
472 my @ins = @{ $n{"ins"} };
476 $temp .= "\tin[${i}] = ${opname};\n";
482 if (exists($n{"irn_flags"})) {
483 $temp .= "\t/* flags */\n";
484 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
486 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
487 } elsif ($flag eq "N") {
488 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
489 } elsif ($flag eq "I") {
490 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
491 } elsif ($flag eq "S") {
492 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
498 $temp .= "\t/* create node */\n";
499 $temp .= "\tassert(op != NULL);\n";
500 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
503 $temp .= "\t/* init node attributes */\n";
504 # lookup init function
505 my $attr_init_code = $init_attr{$attr_type};
506 if(!defined($attr_init_code)) {
507 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
509 $temp .= "${attr_init_code}\n";
510 if(defined($custom_init_attr_func)) {
511 $temp .= &$custom_init_attr_func(\%n, $op);
516 if ($#out_flags >= 0) {
517 $temp .= "\t/* set flags for outs */\n";
518 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
522 foreach my $flag (split(/\|/, $out_flags[$idx])) {
524 $flags .= $prefix."arch_irn_flags_ignore";
527 elsif ($flag eq "S") {
528 $flags .= $prefix."arch_irn_flags_modify_sp";
533 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
539 if (exists($n{"init_attr"})) {
540 $temp .= "\tattr = get_irn_generic_attr(res);\n";
541 $temp .= "\t".$n{"init_attr"}."\n";
544 $temp .= "\t/* optimize node */\n";
545 $temp .= "\tres = optimize_node(res);\n";
546 $temp .= "\tirn_vrfy_irg(res, irg);\n";
549 $temp .= "\treturn res;\n";
551 push(@obst_constructor, $temp);
553 else { # user defined constructor
554 push(@obst_constructor, $n{"rd_constructor"});
557 # close constructor function
558 push(@obst_constructor, "}\n\n");
559 } # constructor creation
561 # set default values for state and flags if not given
562 $n{"state"} = "floats" if (! exists($n{"state"}));
563 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
566 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
567 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
569 if (defined($cmp_attr_func)) {
570 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
572 my $copy_attr_func = $copy_attr{$attr_type};
573 if (!defined($copy_attr_func)) {
574 $copy_attr_func = $default_copy_attr;
576 if (defined($copy_attr_func)) {
577 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
581 my $n_res = $out_arity;
583 $n_res = "20"; # hacky....
585 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
586 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
587 push(@obst_new_irop, $temp);
588 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
589 push(@obst_enum_op, "\tiro_$op,\n");
591 push(@obst_header, "\n");
593 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
594 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
595 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
596 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
600 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
602 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n\n";
603 print OUT @obst_cmp_attr;
605 print OUT @obst_opvar;
607 print OUT @obst_get_opvar;
612 static int $arch\_opcode_start = -1;
613 static int $arch\_opcode_end = -1;
617 # build the FOURCC arguments from $arch
619 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
621 if (length($arch) >= 1) {
622 $a = uc(substr($arch, 0, 1));
625 if (length($arch) >= 2) {
626 $b = uc(substr($arch, 1, 1));
629 if (length($arch) >= 3) {
630 $c = uc(substr($arch, 2, 1));
633 if (length($arch) >= 4) {
634 $d = uc(substr($arch, 3, 1));
637 print OUT<<ENDOFISIRN;
639 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
640 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
642 /** Return the opcode number of the first $arch opcode. */
643 int get_$arch\_opcode_first(void) {
644 return $arch\_opcode_start;
647 /** Return the opcode number of the last $arch opcode + 1. */
648 int get_$arch\_opcode_last(void) {
649 return $arch\_opcode_end;
652 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
653 int is_$arch\_op(const ir_op *op) {
654 return get_op_tag(op) == &$arch\_op_tag;
657 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
658 int is_$arch\_irn(const ir_node *node) {
659 return is_$arch\_op(get_irn_op(node));
662 int get_$arch\_irn_opcode(const ir_node *node) {
663 if (is_$arch\_irn(node))
664 return get_irn_opcode(node) - $arch\_opcode_start;
674 #define BIT(x) (1 << (x % 32))
678 print OUT @obst_limit_func;
681 print OUT @obst_reg_reqs;
684 print OUT @obst_constructor;
686 print OUT<<ENDOFMAIN;
688 * Creates the $arch specific Firm machine operations
689 * needed for the assembler irgs.
691 void $arch\_create_opcodes(void) {
692 #define N irop_flag_none
693 #define L irop_flag_labeled
694 #define C irop_flag_commutative
695 #define X irop_flag_cfopcode
696 #define I irop_flag_ip_cfopcode
697 #define F irop_flag_fragile
698 #define Y irop_flag_forking
699 #define H irop_flag_highlevel
700 #define c irop_flag_constlike
701 #define K irop_flag_keep
702 #define M irop_flag_machine
703 #define O irop_flag_machine_op
704 #define R (irop_flag_user << 0)
708 static int run_once = 0;
714 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
716 $arch\_opcode_start = cur_opcode;
719 print OUT @obst_new_irop;
721 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
722 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
723 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
729 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
731 my $creation_time = localtime(time());
737 * \@brief Function prototypes for the new opcode functions.
738 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
739 * Edit $specfile instead.
740 * created by: $0 $specfile $target_dir
741 * \@date $creation_time
743 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
744 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
748 print OUT @obst_enum_op;
749 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
750 print OUT "int get_$arch\_opcode_first(void);\n";
751 print OUT "int get_$arch\_opcode_last(void);\n";
752 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
753 print OUT @obst_header;
754 print OUT @obst_proj;
763 # Translates numeric arity into string constant.
765 sub translate_arity {
768 if ($arity =~ /^\d+$/) {
770 return "oparity_zero";
772 elsif ($arity == 1) {
773 return "oparity_unary";
775 elsif ($arity == 2) {
776 return "oparity_binary";
778 elsif ($arity == 3) {
779 return "oparity_trinary";
782 return "oparity_any";
784 } elsif ($arity == $ARITY_VARIABLE) {
785 return "oparity_variable";
786 } elsif ($arity == $ARITY_DYNAMIC) {
787 return "oparity_dynamic";
789 die "Fatal error: Unknown arity $arity";
794 # Return the list of pointers for the given execution units.
796 sub gen_execunit_list_initializer {
798 my $uc_arch = uc($arch);
803 foreach my $unit (@{ $units }) {
804 if ($unit eq "DUMMY") {
805 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
807 elsif (exists($cpu{"$unit"})) {
808 # operation can be executed on all units of this type
810 my $tp_name = "$arch\_execution_units_$unit";
812 foreach (@{ $cpu{"$unit"} }) {
813 next if ($idx++ == 0); # skip first element (it's not a unit)
814 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
815 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
819 # operation can be executed only a certain unit
820 # -> find corresponding unit type
822 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
823 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
824 if ($unit eq $cur_unit) {
825 my $tp_name = "$arch\_execution_units_$cur_type";
826 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
827 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
835 print STDERR "Invalid execution unit $unit specified!\n";
840 # prepare the 2-dim array init
841 foreach my $key (keys(%init)) {
842 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
844 foreach (@{ $init{"$key"} }) {
847 $ret .= "\t\tNULL\n";
849 $ret2 .= "\t\tallowed_units_$key,\n";
851 $ret2 .= "\t\tNULL\n";
853 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
861 sub mangle_requirements {
865 my @alternatives = split(/ /, $reqs);
866 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
867 $alternatives[$idx] =~ s/!/not_/g;
870 @alternatives = sort @alternatives;
872 my $name = $class."_".join('_', @alternatives);
878 # Determines whether $name is a specified register class or not.
879 # @return 1 if name is register class, 0 otherwise
883 return 1 if exists($reg_classes{"$name"});
888 # Returns the register class for a given register.
889 # @return class or undef
893 $reg = substr($reg, 1) if ($reg =~ /!.*/);
894 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
899 # Returns the index of a given register within it's register class.
900 # @return index or undef
904 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
909 # Remember the register class for each index in the given requirements.
910 # We need this information for requirements like "in_sX" or "out_dX"
911 # @return array of classes corresponding to the requirement for each index
913 sub build_inout_idx_class {
919 my $inout = ($is_in ? "in" : "out");
921 if (exists($n->{"reg_req"}{"$inout"})) {
922 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
924 for (my $idx = 0; $idx <= $#reqs; $idx++) {
927 if ($reqs[$idx] eq "none") {
929 } elsif (is_reg_class($reqs[$idx])) {
930 $class = $reqs[$idx];
932 my @regs = split(/ /, $reqs[$idx]);
933 GET_CLASS: foreach my $reg (@regs) {
934 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
935 $class = "UNKNOWN_CLASS";
937 $class = get_reg_class($reg);
938 if (!defined $class) {
939 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
947 push(@idx_class, $class);
955 # Generates the function for a given $op and a given IN-index
956 # which returns a subset of possible register from a register class
957 # @return classname from which the subset is derived or undef and
958 # pos which corresponds to in/out reference position or undef
960 sub build_subset_class_func {
965 my $same_pos = undef;
966 my $same_pos2 = undef;
967 my $different_pos = undef;
973 my $limit_reqs; #used for name mangling
975 # build function header
980 my @regs = split(/ /, shift);
982 my $outin = $is_in ? "out" : "in";
984 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
986 # set/unset registers
987 CHECK_REQS: foreach (@regs) {
988 if (/(!)?$outin\_r(\d+)/) {
989 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos2))) {
990 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
991 return (undef, undef, undef, undef, undef);
995 $different_pos = $is_in ? -$2 : $2 - 1;
997 if (!defined($same_pos)) {
998 $same_pos = $is_in ? -$2 : $2 - 1;
1000 $same_pos2 = $is_in ? -$2 : $2 - 1;
1004 $class = $idx_class[$2 - 1];
1007 $class = $idx_class[0];
1008 return ($class, "NULL", undef, undef, 666);
1012 if (substr($_, 0, 1) eq "!") {
1013 if (defined($neg) && $neg == 0) {
1014 # we have seen a positiv constraint as first one but this one is negative
1015 # this doesn't make sense
1016 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1017 return (undef, undef, undef, undef, undef);
1020 if (!defined($neg)) {
1024 $_ = substr($_, 1); # skip '!'
1027 if (defined($neg) && $neg == 1) {
1028 # we have seen a negative constraint as first one but this one is positive
1029 # this doesn't make sense
1030 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1031 return (undef, undef, undef, undef, undef);
1038 # check if register belongs to one of the given classes
1039 $temp = get_reg_class($_);
1040 if (!defined($temp)) {
1041 print STDERR "Unknown register '$_'!\n";
1042 return (undef, undef, undef, undef, undef);
1046 if (!defined($class)) {
1048 } elsif ($class ne $temp) {
1049 # all registers must belong to the same class
1050 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1051 return (undef, undef, undef, undef, undef);
1054 # calculate position inside the initializer bitfield (only 32 bits per
1056 my $regidx = get_reg_index($_);
1057 my $arrayp = $regidx / 32;
1058 push(@{$limit_array[$arrayp]}, $_);
1059 $limit_reqs .= "$_ ";
1062 # don't allow ignore regs in negative constraints
1064 my @cur_class = @{ $reg_classes{"$class"} };
1065 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1066 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1067 my $reg = $cur_class[$idx]{"name"};
1068 my $regix = get_reg_index($reg);
1069 my $arrayp = $regix / 32;
1070 push(@{$limit_array[$arrayp]}, $reg);
1071 $limit_reqs .= "$reg ";
1076 if ($has_limit == 1) {
1077 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1079 if(defined($limit_bitsets{$limit_name})) {
1080 $limit_name = $limit_bitsets{$limit_name};
1081 return ($class, $limit_name, $same_pos, $same_pos2, $different_pos);
1084 $limit_bitsets{$limit_name} = $limit_name;
1086 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1088 my $limitbitsetlen = $regclass2len{$class};
1089 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
1090 for(my $i = 0; $i < $limitarraylen; $i++) {
1092 my $limitarraypart = $limit_array[$i];
1096 push(@obst_limit_func, ", ");
1100 $temp = "0xFFFFFFFF";
1102 foreach my $reg (@{$limitarraypart}) {
1105 } elsif(defined($temp)) {
1108 $temp .= "BIT(REG_".uc(${reg}).")";
1110 if(defined($temp)) {
1111 push(@obst_limit_func, "${temp}");
1113 push(@obst_limit_func, "0");
1116 push(@obst_limit_func, " };\n");
1119 return ($class, $limit_name, $same_pos, $same_pos2, $different_pos);
1123 # Generate register requirements structure
1125 sub generate_requirements {
1134 if ($reqs eq "none") {
1138 arch_register_req_type_none,
1139 NULL, /* regclass */
1140 NULL, /* limit bitset */
1141 { -1, -1 }, /* same pos */
1142 -1 /* different pos */
1146 } elsif ($reqs =~ /^new_reg_(.*)$/) {
1147 if(!is_reg_class($1)) {
1148 die "$1 is not a register class in requirements for $op\n";
1153 arch_register_req_type_should_be_different_from_all,
1154 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1155 NULL, /* limit bitset */
1156 { -1, -1 }, /* same pos */
1157 -1 /* different pos */
1161 } elsif (is_reg_class($reqs)) {
1165 arch_register_req_type_normal,
1166 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1167 NULL, /* limit bitset */
1168 { -1, -1 }, /* same pos */
1169 -1 /* different pos */
1176 my ($regclass, $limit_bitset, $same_pos, $same_pos2, $different_pos)
1177 = build_subset_class_func($node, $op, $idx, $is_in, $reqs);
1179 if (!defined($regclass)) {
1180 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1183 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1184 push(@req_type_mask, "arch_register_req_type_limited");
1186 if (defined($same_pos)) {
1187 push(@req_type_mask, "arch_register_req_type_should_be_same");
1189 if (defined($different_pos)) {
1190 if ($different_pos == 666) {
1191 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
1192 undef $different_pos;
1194 push(@req_type_mask, "arch_register_req_type_should_be_different");
1197 my $reqtype = join(" | ", @req_type_mask);
1199 if(!defined($limit_bitset)) {
1200 $limit_bitset = "NULL";
1202 my $same_pos_str = (defined($same_pos) ? $same_pos : "-1");
1203 my $same_pos_str2 = (defined($same_pos2) ? $same_pos2 : "-1");
1204 my $different_pos_str = (defined($different_pos) ? $different_pos : "-1");
1210 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1212 { ${same_pos_str}, ${same_pos_str2} }, /* same pos */
1213 ${different_pos_str} /* different pos */
1219 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class);
1220 if(defined($requirements{$name})) {
1223 $requirements{$name} = $name;
1224 push(@obst_reg_reqs, <<EOF);
1225 static const arch_register_req_t ${name} = ${result}