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;
53 unless ($return = do $specfile) {
54 die "Fatal error: couldn't parse $specfile: $@" if $@;
55 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
56 die "Fatal error: couldn't run $specfile" unless $return;
60 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
61 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
63 if(!defined($default_attr_type)) {
64 $default_attr_type = "${arch}_attr_t";
66 if(!defined(%init_attr)) {
68 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
71 if(!defined($default_cmp_attr)) {
72 $default_cmp_attr = "${arch}_compare_attr";
74 if(!defined(%compare_attr)) {
76 "${default_attr_type}" => "${default_cmp_attr}",
80 # Operands are really just nodes with some special constraints, we check
81 # these and create new entries in the nodes hashmap
82 foreach my $op (keys(%operands)) {
83 my %operand = %{ $operands{"$op"} };
87 if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
88 if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
89 if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
90 if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
91 if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
94 foreach my $flag (keys(%operand)) {
95 $op_node{$flag} = $operand{$flag};
97 $op_node{op_flags} = "O";
98 $op_node{cmp_attr} = 'return 1;';
99 $op_node{mode} = 'mode_ANY';
101 $nodes{$op} = \%op_node;
104 #print Dumper(%nodes);
105 #print Dumper(%operands);
107 # create c code file from specs
111 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
112 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
113 my @obst_constructor; # stack for node constructor functions
114 my @obst_new_irop; # stack for the new_ir_op calls
115 my @obst_enum_op; # stack for creating the <arch>_opcode enum
116 my @obst_header; # stack for function prototypes
117 my @obst_is_archirn; # stack for the is_$arch_irn() function
118 my @obst_cmp_attr; # stack for the compare attribute functions
119 my @obst_proj; # stack for the pn_ numbers
124 my $n_opcodes = 0; # number of opcodes
125 my $ARITY_VARIABLE = -1;
126 my $ARITY_DYNAMIC = -2;
127 my %requirements = ();
128 my %limit_bitsets = ();
130 my %regclass2len = ();
132 # build register->class hashes
133 foreach my $class_name (keys(%reg_classes)) {
134 my @class = @{ $reg_classes{"$class_name"} };
135 my $old_classname = $class_name;
139 $class_name = $arch."_".$class_name;
143 $reg2class{$_->{name}} = {
144 "class" => $old_classname,
150 $regclass2len{$old_classname} = $idx;
154 # for registering additional opcodes
155 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
157 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
159 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
160 foreach my $op (keys(%nodes)) {
161 my %n = %{ $nodes{"$op"} };
169 if(exists($n{"arity"})) {
170 $arity = $n{"arity"};
171 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
172 $arity = scalar(@{ $n{"reg_req"}{"in"} });
173 } elsif (exists($n{"ins"})) {
174 $arity = scalar(@{ $n{"ins"} });
176 if($arity eq "variable") {
177 $arity = $ARITY_VARIABLE;
178 } elsif($arity eq "dynamic") {
179 $arity = $ARITY_DYNAMIC;
182 # determine out arity
184 if(exists($n{"out_arity"})) {
185 $out_arity = $n{"out_arity"};
186 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
187 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
188 } elsif (exists($n{"outs"})) {
189 $out_arity = scalar(@{ $n{"outs"} });
191 if($out_arity eq "variable") {
192 $out_arity = $ARITY_VARIABLE;
193 } elsif($out_arity eq "dynamic") {
194 $out_arity = $ARITY_DYNAMIC;
201 # define proj numbers and in numbers
202 if (exists($n{"outs"})) {
205 @outs = @{ $n{"outs"} };
206 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
207 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
210 $num_outs = $#outs + 1;
212 push(@obst_proj, "\nenum pn_$op {\n");
214 for (my $idx = 0; $idx <= $#outs; $idx++) {
215 # check, if we have additional flags annotated to out
216 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
217 push(@out_flags, $1);
218 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
220 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
223 push(@obst_proj, "};\n");
224 # outs have names, it must be a mode_T node
225 $known_mode = "mode_T";
227 if (exists($n{"ins"})) {
230 @ins = @{ $n{"ins"} };
231 if($arity >= 0 && scalar(@ins) != $arity) {
232 die "Fatal error: Op ${op} has different number of ins and arity\n";
235 push(@obst_proj, "\nenum n_$op {\n");
237 for (my $idx = 0; $idx <= $#ins; $idx++) {
238 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
241 push(@obst_proj, "};\n");
245 if (exists($n{"mode"})) {
246 $known_mode = $n{"mode"};
249 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
250 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
251 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
253 push(@obst_is_archirn, "is_$op(node)");
255 push(@obst_header, "extern ir_op *op_$op;\n");
256 push(@obst_header, "ir_op *get_op_$op(void);\n");
257 push(@obst_header, "int is_$op(const ir_node *n);\n");
259 my $attr_type= $n{"attr_type"};
260 if(!defined($attr_type)) {
261 $attr_type = $default_attr_type;
264 # determine compare function
266 if (exists($n{"cmp_attr"})) {
267 my $cmpcode = $n{"cmp_attr"};
269 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
270 if($cmpcode =~ m/attr_a/) {
271 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
273 push(@obst_cmp_attr, "\t(void) a;\n");
275 if($cmpcode =~ m/attr_b/) {
276 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
278 push(@obst_cmp_attr, "\t(void) b;\n");
280 push(@obst_cmp_attr, "\t${cmpcode}\n");
281 push(@obst_cmp_attr, "}\n\n");
283 $cmp_attr_func = "cmp_attr_${op}";
285 if(defined($compare_attr{${attr_type}})) {
286 $cmp_attr_func = $compare_attr{${attr_type}};
288 die "Fatal error: No compare function defined for ${attr_type} attributes.";
292 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
293 # we explicitly skip the constructor if the specification entry says NONE
295 my $comment = $n{"comment"};
296 if(!exists($n{"comment"})) {
297 $comment = "construct ${orig_op} node";
304 push(@obst_constructor, $comment);
306 # create constructor head
307 my $complete_args = "";
310 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
311 if (!exists($n{"args"})) { # default args
312 if ($arity == $ARITY_VARIABLE) {
313 $complete_args = ", int arity, ir_node *in[]";
314 } elsif ($arity == $ARITY_DYNAMIC) {
317 for (my $i = 0; $i < $arity; $i++) {
318 my $opname = "op${i}";
319 if (exists($n{"ins"})) {
320 my @ins = @{ $n{"ins"} };
324 $complete_args .= ", ir_node *${opname}";
327 if ($out_arity == $ARITY_VARIABLE) {
328 $complete_args .= ", int n_res";
331 if (!defined($known_mode)) {
332 $complete_args .= ", ir_mode *mode";
334 } else { # user defined args
335 for my $href (@{ $n{"args"} }) {
336 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
337 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
341 # we have additional attribute arguements
342 if (exists($n{"attr"})) {
343 $complete_args .= ", ".$n{"attr"};
346 $temp .= "$complete_args)";
347 push(@obst_constructor, $temp."\n{\n");
348 push(@obst_header, $comment);
349 push(@obst_header, $temp.";\n");
351 # emit constructor code
352 if (!exists($n{"rd_constructor"})) { # default constructor
353 $temp = "\tir_node *res;\n";
354 $temp .= "\tir_op *op = op_${op};\n";
355 $temp .= "\tint flags = 0;\n";
357 if($arity == $ARITY_DYNAMIC) {
358 $temp .= "\tint arity = -1;\n";
359 $temp .= "\tir_node **in = NULL;\n";
360 } elsif($arity == $ARITY_VARIABLE) {
362 $temp .= "\tint arity = $arity;\n";
364 $temp .= "\tir_node *in[$arity];\n";
366 $temp .= "\tir_node **in = NULL;\n";
369 if($out_arity == $ARITY_DYNAMIC) {
370 $temp .= "\tint n_res = -1;\n";
371 } elsif($out_arity == $ARITY_VARIABLE) {
373 $temp .= "\tint n_res = ${out_arity};\n";
376 my $latency = $n{"latency"};
377 if (!defined($latency)) {
380 $temp .= "\tunsigned latency = ${latency};\n";
382 if (defined($known_mode)) {
383 $temp .= "\tir_mode *mode = ${known_mode};\n";
386 # set up static variables for cpu execution unit assigments
387 if (exists($n{"units"})) {
388 $temp .= gen_execunit_list_initializer($n{"units"});
390 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
393 undef my $in_req_var;
394 undef my $out_req_var;
396 # set up static variables for requirements and registers
397 if (exists($n{"reg_req"})) {
398 my %req = %{ $n{"reg_req"} };
402 @in = @{ $req{"in"} } if (exists($req{"in"}));
404 @out = @{ $req{"out"} } if exists(($req{"out"}));
406 for(my $idx = 0; $idx < $#in; $idx++) {
408 generate_requirements($req, \%n, $op, $idx, 1);
410 for(my $idx = 0; $idx < $#out; $idx++) {
411 my $req = $out[$idx];
412 generate_requirements($req, \%n, $op, $idx, 0);
416 if($arity >= 0 && scalar(@in) != $arity) {
417 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
420 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
422 for ($idx = 0; $idx <= $#in; $idx++) {
424 my $reqstruct = generate_requirements($req, \%n, $op, $idx, 1);
425 $temp .= "\t\t& ${reqstruct},\n";
430 die "Fatal error: need in requirements for ${op}\n";
432 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
436 if($out_arity >= 0 && scalar(@out) != $out_arity) {
437 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
440 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
442 for ($idx = 0; $idx <= $#out; $idx++) {
443 my $req = $out[$idx];
444 my $reqstruct = generate_requirements($req, \%n, $op, $idx, 0);
445 $temp .= "\t\t& ${reqstruct},\n";
450 die "Fatal error: need out requirements for ${op}\n";
452 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
455 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
456 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
458 if(exists($n{"init_attr"})) {
459 $temp .= "\t${attr_type} *attr;\n";
465 $temp .= "\t/* construct in array */\n";
466 for (my $i = 0; $i < $arity; $i++) {
467 my $opname = "op${i}";
468 if (exists($n{"ins"})) {
469 my @ins = @{ $n{"ins"} };
473 $temp .= "\tin[${i}] = ${opname};\n";
479 if (exists($n{"irn_flags"})) {
480 $temp .= "\t/* flags */\n";
481 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
483 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
484 } elsif ($flag eq "N") {
485 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
486 } elsif ($flag eq "I") {
487 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
488 } elsif ($flag eq "S") {
489 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
495 $temp .= "\t/* create node */\n";
496 $temp .= "\tassert(op != NULL);\n";
497 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
500 $temp .= "\t/* init node attributes */\n";
501 # lookup init function
502 my $attr_init_code = $init_attr{$attr_type};
503 if(!defined($attr_init_code)) {
504 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
506 $temp .= "${attr_init_code}\n";
510 if ($#out_flags >= 0) {
511 $temp .= "\t/* set flags for outs */\n";
512 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
516 foreach my $flag (split(/\|/, $out_flags[$idx])) {
518 $flags .= $prefix."arch_irn_flags_ignore";
521 elsif ($flag eq "S") {
522 $flags .= $prefix."arch_irn_flags_modify_sp";
527 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
533 if (exists($n{"init_attr"})) {
534 $temp .= "\tattr = get_irn_generic_attr(res);\n";
535 $temp .= "\t".$n{"init_attr"}."\n";
538 $temp .= "\t/* optimize node */\n";
539 $temp .= "\tres = optimize_node(res);\n";
540 $temp .= "\tirn_vrfy_irg(res, irg);\n";
543 $temp .= "\treturn res;\n";
545 push(@obst_constructor, $temp);
547 else { # user defined constructor
548 push(@obst_constructor, $n{"rd_constructor"});
551 # close constructor function
552 push(@obst_constructor, "}\n\n");
553 } # constructor creation
555 # set default values for state and flags if not given
556 $n{"state"} = "floats" if (! exists($n{"state"}));
557 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
560 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
561 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
563 if (defined($cmp_attr_func)) {
564 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
568 my $n_res = $out_arity;
570 $n_res = "20"; # hacky....
572 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
573 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
574 push(@obst_new_irop, $temp);
575 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
576 push(@obst_enum_op, "\tiro_$op,\n");
578 push(@obst_header, "\n");
580 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
581 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
582 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
583 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
587 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
589 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n\n";
590 print OUT @obst_cmp_attr;
592 print OUT @obst_opvar;
594 print OUT @obst_get_opvar;
599 static int $arch\_opcode_start = -1;
600 static int $arch\_opcode_end = -1;
604 # build the FOURCC arguments from $arch
606 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
608 if (length($arch) >= 1) {
609 $a = uc(substr($arch, 0, 1));
612 if (length($arch) >= 2) {
613 $b = uc(substr($arch, 1, 1));
616 if (length($arch) >= 3) {
617 $c = uc(substr($arch, 2, 1));
620 if (length($arch) >= 4) {
621 $d = uc(substr($arch, 3, 1));
624 print OUT<<ENDOFISIRN;
626 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
627 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
629 /** Return the opcode number of the first $arch opcode. */
630 int get_$arch\_opcode_first(void) {
631 return $arch\_opcode_start;
634 /** Return the opcode number of the last $arch opcode + 1. */
635 int get_$arch\_opcode_last(void) {
636 return $arch\_opcode_end;
639 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
640 int is_$arch\_op(const ir_op *op) {
641 return get_op_tag(op) == &$arch\_op_tag;
644 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
645 int is_$arch\_irn(const ir_node *node) {
646 return is_$arch\_op(get_irn_op(node));
649 int get_$arch\_irn_opcode(const ir_node *node) {
650 if (is_$arch\_irn(node))
651 return get_irn_opcode(node) - $arch\_opcode_start;
661 #define BIT(x) (1 << (x % 32))
665 print OUT @obst_limit_func;
668 print OUT @obst_reg_reqs;
671 print OUT @obst_constructor;
673 print OUT<<ENDOFMAIN;
675 * Creates the $arch specific Firm machine operations
676 * needed for the assembler irgs.
678 void $arch\_create_opcodes(void) {
679 #define N irop_flag_none
680 #define L irop_flag_labeled
681 #define C irop_flag_commutative
682 #define X irop_flag_cfopcode
683 #define I irop_flag_ip_cfopcode
684 #define F irop_flag_fragile
685 #define Y irop_flag_forking
686 #define H irop_flag_highlevel
687 #define c irop_flag_constlike
688 #define K irop_flag_keep
689 #define M irop_flag_machine
690 #define O irop_flag_machine_op
691 #define R (irop_flag_user << 0)
695 static int run_once = 0;
701 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
703 $arch\_opcode_start = cur_opcode;
706 print OUT @obst_new_irop;
708 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
709 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
710 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
716 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
718 my $creation_time = localtime(time());
724 * \@brief Function prototypes for the new opcode functions.
725 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
726 * Edit $specfile instead.
727 * created by: $0 $specfile $target_dir
728 * \@date $creation_time
730 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
731 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
735 print OUT @obst_enum_op;
736 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
737 print OUT "int get_$arch\_opcode_first(void);\n";
738 print OUT "int get_$arch\_opcode_last(void);\n";
739 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
740 print OUT @obst_header;
741 print OUT @obst_proj;
750 # Translates numeric arity into string constant.
752 sub translate_arity {
755 if ($arity =~ /^\d+$/) {
757 return "oparity_zero";
759 elsif ($arity == 1) {
760 return "oparity_unary";
762 elsif ($arity == 2) {
763 return "oparity_binary";
765 elsif ($arity == 3) {
766 return "oparity_trinary";
769 return "oparity_any";
771 } elsif ($arity == $ARITY_VARIABLE) {
772 return "oparity_variable";
773 } elsif ($arity == $ARITY_DYNAMIC) {
774 return "oparity_dynamic";
776 die "Fatal error: Unknown arity $arity";
781 # Return the list of pointers for the given execution units.
783 sub gen_execunit_list_initializer {
785 my $uc_arch = uc($arch);
790 foreach my $unit (@{ $units }) {
791 if ($unit eq "DUMMY") {
792 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
794 elsif (exists($cpu{"$unit"})) {
795 # operation can be executed on all units of this type
797 my $tp_name = "$arch\_execution_units_$unit";
799 foreach (@{ $cpu{"$unit"} }) {
800 next if ($idx++ == 0); # skip first element (it's not a unit)
801 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
802 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
806 # operation can be executed only a certain unit
807 # -> find corresponding unit type
809 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
810 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
811 if ($unit eq $cur_unit) {
812 my $tp_name = "$arch\_execution_units_$cur_type";
813 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
814 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
822 print STDERR "Invalid execution unit $unit specified!\n";
827 # prepare the 2-dim array init
828 foreach my $key (keys(%init)) {
829 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
831 foreach (@{ $init{"$key"} }) {
834 $ret .= "\t\tNULL\n";
836 $ret2 .= "\t\tallowed_units_$key,\n";
838 $ret2 .= "\t\tNULL\n";
840 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
848 sub mangle_requirements {
851 my @alternatives = split(/ /, $reqs);
852 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
853 $alternatives[$idx] =~ s/!/not_/g;
856 @alternatives = sort @alternatives;
858 my $name = join('_', @alternatives);
864 # Determines whether $name is a specified register class or not.
865 # @return 1 if name is register class, 0 otherwise
869 return 1 if exists($reg_classes{"$name"});
874 # Returns the register class for a given register.
875 # @return class or undef
879 $reg = substr($reg, 1) if ($reg =~ /!.*/);
880 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
885 # Returns the index of a given register within it's register class.
886 # @return index or undef
890 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
895 # Remember the register class for each index in the given requirements.
896 # We need this information for requirements like "in_sX" or "out_dX"
897 # @return array of classes corresponding to the requirement for each index
899 sub build_inout_idx_class {
905 my $inout = ($is_in ? "in" : "out");
907 if (exists($n->{"reg_req"}{"$inout"})) {
908 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
910 for (my $idx = 0; $idx <= $#reqs; $idx++) {
913 if ($reqs[$idx] eq "none") {
915 } elsif (is_reg_class($reqs[$idx])) {
916 $class = $reqs[$idx];
918 my @regs = split(/ /, $reqs[$idx]);
919 GET_CLASS: foreach my $reg (@regs) {
920 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
921 $class = "UNKNOWN_CLASS";
923 $class = get_reg_class($reg);
924 if (!defined $class) {
925 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
933 push(@idx_class, $class);
941 # Generates the function for a given $op and a given IN-index
942 # which returns a subset of possible register from a register class
943 # @return classname from which the subset is derived or undef and
944 # pos which corresponds to in/out reference position or undef
946 sub build_subset_class_func {
951 my $same_pos = undef;
952 my $different_pos = undef;
958 my $limit_reqs; #used for name mangling
960 # build function header
965 my @regs = split(/ /, shift);
967 my $outin = $is_in ? "out" : "in";
969 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
971 # set/unset registers
972 CHECK_REQS: foreach (@regs) {
973 if (/(!)?$outin\_r(\d+)/) {
974 if (($1 && defined($different_pos)) || (!$1 && defined($same_pos))) {
975 print STDERR "Multiple in/out references of same type in one requirement not allowed.\n";
976 return (undef, undef, undef, undef);
980 $different_pos = $is_in ? -$2 : $2 - 1;
982 $same_pos = $is_in ? -$2 : $2 - 1;
985 $class = $idx_class[$2 - 1];
988 $class = $idx_class[0];
989 return ($class, "NULL", undef, 666);
993 if (substr($_, 0, 1) eq "!") {
994 if (defined($neg) && $neg == 0) {
995 # we have seen a positiv constraint as first one but this one is negative
996 # this doesn't make sense
997 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
998 return (undef, undef, undef, undef);
1001 if (!defined($neg)) {
1005 $_ = substr($_, 1); # skip '!'
1008 if (defined($neg) && $neg == 1) {
1009 # we have seen a negative constraint as first one but this one is positive
1010 # this doesn't make sense
1011 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1012 return (undef, undef, undef, undef);
1019 # check if register belongs to one of the given classes
1020 $temp = get_reg_class($_);
1021 if (!defined($temp)) {
1022 print STDERR "Unknown register '$_'!\n";
1023 return (undef, undef, undef, undef);
1027 if (!defined($class)) {
1029 } elsif ($class ne $temp) {
1030 # all registers must belong to the same class
1031 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1032 return (undef, undef, undef, undef);
1035 # calculate position inside the initializer bitfield (only 32 bits per
1037 my $regidx = get_reg_index($_);
1038 my $arrayp = $regidx / 32;
1039 push(@{$limit_array[$arrayp]}, $_);
1040 $limit_reqs .= "$_ ";
1043 # don't allow ignore regs in negative constraints
1045 my @cur_class = @{ $reg_classes{"$class"} };
1046 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1047 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1048 my $reg = $cur_class[$idx]{"name"};
1049 my $regix = get_reg_index($reg);
1050 my $arrayp = $regix / 32;
1051 push(@{$limit_array[$arrayp]}, $reg);
1052 $limit_reqs .= "$reg ";
1057 if ($has_limit == 1) {
1058 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs);
1060 if(defined($limit_bitsets{$limit_name})) {
1061 return $limit_bitsets{$limit_name};
1064 $limit_bitsets{$limit_name} = $limit_name;
1066 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1068 my $limitbitsetlen = $regclass2len{$class};
1069 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
1070 for(my $i = 0; $i < $limitarraylen; $i++) {
1072 my $limitarraypart = $limit_array[$i];
1076 push(@obst_limit_func, ", ");
1080 $temp = "0xFFFFFFFF";
1082 foreach my $reg (@{$limitarraypart}) {
1085 } elsif(defined($temp)) {
1088 $temp .= "BIT(REG_".uc(${reg}).")";
1090 if(defined($temp)) {
1091 push(@obst_limit_func, "${temp}");
1093 push(@obst_limit_func, "0");
1096 push(@obst_limit_func, " };\n");
1099 return ($class, $limit_name, $same_pos, $different_pos);
1103 # Generate register requirements structure
1105 sub generate_requirements {
1112 my $name = "${arch}_requirements_".mangle_requirements($reqs);
1113 if(defined($requirements{$name})) {
1116 $requirements{$name} = $name;
1118 if ($reqs eq "none") {
1119 push(@obst_reg_reqs, <<EOF);
1120 static const arch_register_req_t $name = {
1121 arch_register_req_type_none,
1122 NULL, /* regclass */
1123 NULL, /* limit bitset */
1125 -1 /* different pos */
1129 } elsif ($reqs =~ /^new_reg_(.*)$/) {
1130 if(!is_reg_class($1)) {
1131 die "$1 is not a register class in requirements for $op\n";
1133 push(@obst_reg_reqs, <<EOF);
1134 static const arch_register_req_t $name = {
1135 arch_register_req_type_should_be_different_from_all,
1136 & ${arch}_reg_classes[CLASS_${arch}_$1],
1137 NULL, /* limit bitset */
1139 -1 /* different pos */
1143 } elsif (is_reg_class($reqs)) {
1144 push(@obst_reg_reqs, <<EOF);
1145 static const arch_register_req_t $name = {
1146 arch_register_req_type_normal,
1147 & ${arch}_reg_classes[CLASS_${arch}_${reqs}],
1148 NULL, /* limit bitset */
1150 -1 /* different pos */
1157 my ($class, $limit_bitset, $same_pos, $different_pos)
1158 = build_subset_class_func($node, $op, $idx, $is_in, $reqs);
1160 if (!defined($class)) {
1161 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1164 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1165 push(@req_type_mask, "arch_register_req_type_limited");
1167 if (defined($same_pos)) {
1168 push(@req_type_mask, "arch_register_req_type_should_be_same");
1170 if (defined($different_pos)) {
1171 if ($different_pos == 666) {
1172 push(@req_type_mask, "arch_register_req_type_should_be_different_from_all");
1173 undef $different_pos;
1175 push(@req_type_mask, "arch_register_req_type_should_be_different");
1178 my $reqtype = join(" | ", @req_type_mask);
1180 if(!defined($limit_bitset)) {
1181 $limit_bitset = "NULL";
1183 my $same_pos_str = (defined($same_pos) ? $same_pos : "-1");
1184 my $different_pos_str = (defined($different_pos) ? $different_pos : "-1");
1186 push(@obst_reg_reqs, <<EOF);
1187 static const arch_register_req_t $name = {
1189 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1191 $same_pos_str, /* same pos */
1192 $different_pos_str, /* different pos */