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
31 my $specfile = $ARGV[0];
32 my $target_dir = $ARGV[1];
38 our $additional_opcodes;
42 our $default_op_attr_type;
43 our $default_attr_type;
44 our $default_cmp_attr;
45 our $default_copy_attr;
47 our $custom_init_attr_func;
57 unless ($return = do $specfile) {
58 die "Fatal error: couldn't parse $specfile: $@" if $@;
59 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
60 die "Fatal error: couldn't run $specfile" unless $return;
64 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
65 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
67 if(!defined($default_attr_type)) {
68 $default_attr_type = "${arch}_attr_t";
70 if(!defined(%init_attr)) {
72 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res);",
75 if(!defined($default_cmp_attr)) {
76 $default_cmp_attr = "${arch}_compare_attr";
78 if(!defined(%compare_attr)) {
80 "${default_attr_type}" => "${default_cmp_attr}",
84 # Operands are really just nodes with some special constraints, we check
85 # these and create new entries in the nodes hashmap
86 foreach my $op (keys(%operands)) {
87 my %operand = %{ $operands{"$op"} };
91 if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
92 if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
93 if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
94 if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
95 if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
98 foreach my $flag (keys(%operand)) {
99 $op_node{$flag} = $operand{$flag};
101 $op_node{op_flags} = "O";
102 $op_node{cmp_attr} = 'return 1;';
103 $op_node{mode} = 'mode_ANY';
105 $nodes{$op} = \%op_node;
108 #print Dumper(%nodes);
109 #print Dumper(%operands);
111 # create c code file from specs
115 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
116 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
117 my @obst_constructor; # stack for node constructor functions
118 my @obst_new_irop; # stack for the new_ir_op calls
119 my @obst_enum_op; # stack for creating the <arch>_opcode enum
120 my @obst_header; # stack for function prototypes
121 my @obst_is_archirn; # stack for the is_$arch_irn() function
122 my @obst_cmp_attr; # stack for the compare attribute functions
123 my @obst_proj; # stack for the pn_ numbers
128 my $n_opcodes = 0; # number of opcodes
129 my $ARITY_VARIABLE = -1;
130 my $ARITY_DYNAMIC = -2;
131 my %requirements = ();
132 my %limit_bitsets = ();
134 my %regclass2len = ();
136 # build register->class hashes
137 foreach my $class_name (keys(%reg_classes)) {
138 my @class = @{ $reg_classes{"$class_name"} };
139 my $old_classname = $class_name;
143 $class_name = $arch."_".$class_name;
147 $reg2class{$_->{name}} = {
148 "class" => $old_classname,
154 $regclass2len{$old_classname} = $idx;
158 # for registering additional opcodes
159 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
161 push(@obst_header, "void ".$arch."_create_opcodes(const arch_irn_ops_t *be_ops);\n");
163 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
164 foreach my $op (keys(%nodes)) {
165 my %n = %{ $nodes{"$op"} };
173 if(exists($n{"arity"})) {
174 $arity = $n{"arity"};
175 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
176 $arity = scalar(@{ $n{"reg_req"}{"in"} });
177 } elsif (exists($n{"ins"})) {
178 $arity = scalar(@{ $n{"ins"} });
180 if($arity eq "variable") {
181 $arity = $ARITY_VARIABLE;
182 } elsif($arity eq "dynamic") {
183 $arity = $ARITY_DYNAMIC;
186 # determine out arity
188 if(exists($n{"out_arity"})) {
189 $out_arity = $n{"out_arity"};
190 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
191 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
192 } elsif (exists($n{"outs"})) {
193 $out_arity = scalar(@{ $n{"outs"} });
195 if($out_arity eq "variable") {
196 $out_arity = $ARITY_VARIABLE;
197 } elsif($out_arity eq "dynamic") {
198 $out_arity = $ARITY_DYNAMIC;
205 # define proj numbers and in numbers
206 if (exists($n{"outs"})) {
209 @outs = @{ $n{"outs"} };
210 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
211 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
214 $num_outs = $#outs + 1;
216 push(@obst_proj, "\nenum pn_$op {\n");
218 for (my $idx = 0; $idx <= $#outs; $idx++) {
219 # check, if we have additional flags annotated to out
220 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
221 push(@out_flags, $1);
222 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
224 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
227 push(@obst_proj, "};\n");
228 # outs have names, it must be a mode_T node
229 $known_mode = "mode_T";
231 if (exists($n{"ins"})) {
234 @ins = @{ $n{"ins"} };
235 if($arity >= 0 && scalar(@ins) != $arity) {
236 die "Fatal error: Op ${op} has different number of ins and arity\n";
239 push(@obst_proj, "\nenum n_$op {\n");
241 for (my $idx = 0; $idx <= $#ins; $idx++) {
242 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
245 push(@obst_proj, "};\n");
249 if (exists($n{"mode"})) {
250 $known_mode = $n{"mode"};
253 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
254 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
255 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
257 push(@obst_is_archirn, "is_$op(node)");
259 push(@obst_header, "extern ir_op *op_$op;\n");
260 push(@obst_header, "ir_op *get_op_$op(void);\n");
261 push(@obst_header, "int is_$op(const ir_node *n);\n");
263 my $attr_type= $n{"attr_type"};
264 if(!defined($attr_type)) {
265 $attr_type = $default_attr_type;
268 # determine hash function
270 if (exists($n{"hash_func"})) {
271 $hash_func = $n{"hash_func"};
274 # determine compare function
276 if (exists($n{"cmp_attr"})) {
277 my $cmpcode = $n{"cmp_attr"};
279 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
280 if($cmpcode =~ m/attr_a/) {
281 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
283 push(@obst_cmp_attr, "\t(void) a;\n");
285 if($cmpcode =~ m/attr_b/) {
286 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
288 push(@obst_cmp_attr, "\t(void) b;\n");
290 push(@obst_cmp_attr, "\t${cmpcode}\n");
291 push(@obst_cmp_attr, "}\n\n");
293 $cmp_attr_func = "cmp_attr_${op}";
295 if(defined($compare_attr{${attr_type}})) {
296 $cmp_attr_func = $compare_attr{${attr_type}};
298 die "Fatal error: No compare function defined for ${attr_type} attributes.";
302 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
303 # we explicitly skip the constructor if the specification entry says NONE
305 my $comment = $n{"comment"};
306 if(!exists($n{"comment"})) {
307 $comment = "construct ${orig_op} node";
314 push(@obst_constructor, $comment);
316 # create constructor head
317 my $complete_args = "";
320 $temp = "ir_node *new_bd_$op(dbg_info *db, ir_node *block";
321 if (!exists($n{"args"})) { # default args
322 if ($arity == $ARITY_VARIABLE) {
323 $complete_args = ", int arity, ir_node *in[]";
324 } elsif ($arity == $ARITY_DYNAMIC) {
327 for (my $i = 0; $i < $arity; $i++) {
328 my $opname = "op${i}";
329 if (exists($n{"ins"})) {
330 my @ins = @{ $n{"ins"} };
334 $complete_args .= ", ir_node *${opname}";
337 if ($out_arity == $ARITY_VARIABLE) {
338 $complete_args .= ", int n_res";
341 if (!defined($known_mode)) {
342 $complete_args .= ", ir_mode *mode";
344 } else { # user defined args
345 for my $href (@{ $n{"args"} }) {
346 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
347 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
351 # we have additional attribute arguements
352 if (exists($n{"attr"})) {
353 $complete_args .= ", ".$n{"attr"};
356 $temp .= "$complete_args)";
357 push(@obst_constructor, $temp."\n{\n");
358 push(@obst_header, $comment);
359 push(@obst_header, $temp.";\n");
361 # emit constructor code
362 if (!exists($n{"rd_constructor"})) { # default constructor
363 $temp = "\tir_node *res;\n";
364 $temp .= "\tir_op *op = op_${op};\n";
365 $temp .= "\tint flags = 0;\n";
367 if($arity == $ARITY_DYNAMIC) {
368 $temp .= "\tint arity = -1;\n";
369 $temp .= "\tir_node **in = NULL;\n";
370 } elsif($arity == $ARITY_VARIABLE) {
372 $temp .= "\tint arity = $arity;\n";
374 $temp .= "\tir_node *in[$arity];\n";
376 $temp .= "\tir_node **in = NULL;\n";
379 if($out_arity == $ARITY_DYNAMIC) {
380 $temp .= "\tint n_res = -1;\n";
381 } elsif($out_arity == $ARITY_VARIABLE) {
383 $temp .= "\tint n_res = ${out_arity};\n";
386 if (defined($known_mode)) {
387 $temp .= "\tir_mode *mode = ${known_mode};\n";
390 # set up static variables for cpu execution unit assigments
391 if (exists($n{"units"})) {
392 $temp .= gen_execunit_list_initializer($n{"units"});
394 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
397 undef my $in_req_var;
398 undef my $out_req_var;
400 # set up static variables for requirements and registers
401 if (exists($n{"reg_req"})) {
402 my %req = %{ $n{"reg_req"} };
406 @in = @{ $req{"in"} } if (exists($req{"in"}));
408 @out = @{ $req{"out"} } if exists(($req{"out"}));
410 for(my $idx = 0; $idx < $#in; $idx++) {
412 generate_requirements($req, \%n, $op, $idx, 1);
414 for(my $idx = 0; $idx < $#out; $idx++) {
415 my $req = $out[$idx];
416 generate_requirements($req, \%n, $op, $idx, 0);
420 if($arity >= 0 && scalar(@in) != $arity) {
421 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
424 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
426 for ($idx = 0; $idx <= $#in; $idx++) {
428 my $reqstruct = generate_requirements($req, \%n, $op, $idx, 1);
429 $temp .= "\t\t& ${reqstruct},\n";
434 die "Fatal error: need in requirements for ${op}\n";
436 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
440 if($out_arity >= 0 && scalar(@out) != $out_arity) {
441 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
444 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
446 for ($idx = 0; $idx <= $#out; $idx++) {
447 my $req = $out[$idx];
448 my $reqstruct = generate_requirements($req, \%n, $op, $idx, 0);
449 $temp .= "\t\t& ${reqstruct},\n";
454 die "Fatal error: need out requirements for ${op}\n";
456 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
459 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
460 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
462 if(exists($n{"init_attr"})) {
463 $temp .= "\t${attr_type} *attr;\n";
469 $temp .= "\t/* construct in array */\n";
470 for (my $i = 0; $i < $arity; $i++) {
471 my $opname = "op${i}";
472 if (exists($n{"ins"})) {
473 my @ins = @{ $n{"ins"} };
477 $temp .= "\tin[${i}] = ${opname};\n";
483 if (exists($n{"irn_flags"})) {
484 $temp .= "\t/* flags */\n";
485 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
487 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
488 } elsif ($flag eq "N") {
489 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
491 die "Fatal error: unknown flag $flag for ${op}\n";
497 $temp .= "\t/* create node */\n";
498 $temp .= "\tassert(op != NULL);\n";
499 $temp .= "\tres = new_ir_node(db, current_ir_graph, block, op, mode, arity, in);\n";
502 $temp .= "\t/* init node attributes */\n";
503 # lookup init function
504 my $attr_init_code = $init_attr{$attr_type};
505 if(!defined($attr_init_code)) {
506 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
508 $temp .= "${attr_init_code}\n";
509 if(defined($custom_init_attr_func)) {
510 $temp .= &$custom_init_attr_func(\%n, $op);
514 if (exists($n{"init_attr"})) {
515 $temp .= "\tattr = get_irn_generic_attr(res);\n";
516 $temp .= "\t".$n{"init_attr"}."\n";
519 $temp .= "\t/* optimize node */\n";
520 $temp .= "\tres = optimize_node(res);\n";
521 $temp .= "\tirn_vrfy_irg(res, current_ir_graph);\n";
524 $temp .= "\treturn res;\n";
526 push(@obst_constructor, $temp);
528 else { # user defined constructor
529 push(@obst_constructor, $n{"rd_constructor"});
532 # close constructor function
533 push(@obst_constructor, "}\n\n");
534 } # constructor creation
536 # set default values for state and flags if not given
537 $n{"state"} = "floats" if (! exists($n{"state"}));
538 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
541 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
542 push(@obst_new_irop, "\tops.be_ops = be_ops;\n");
543 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
545 if (defined($cmp_attr_func)) {
546 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
548 my $copy_attr_func = $copy_attr{$attr_type};
549 if (!defined($copy_attr_func)) {
550 $copy_attr_func = $default_copy_attr;
552 if (defined($copy_attr_func)) {
553 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
555 if (defined($hash_func)) {
556 push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
560 my $n_res = $out_arity;
562 $n_res = "20"; # hacky....
564 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
565 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
566 push(@obst_new_irop, $temp);
567 push(@obst_new_irop, "\tset_op_tag(op_$op, $arch\_op_tag);\n");
568 if(defined($default_op_attr_type)) {
569 push(@obst_new_irop, "\tattr = &attrs[iro_$op];\n");
570 if(defined($n{op_attr_init})) {
571 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
573 push(@obst_new_irop, "\tset_op_attr(op_$op, attr);\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 #define $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(const arch_irn_ops_t *be_ops) {
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 NB irop_flag_dump_noblock
692 #define NI irop_flag_dump_noinput
693 #define R (irop_flag_user << 0)
697 static int run_once = 0;
700 if (defined($default_op_attr_type)) {
701 print OUT "\t$default_op_attr_type *attr, *attrs;\n";
704 print OUT<<ENDOFMAIN;
710 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
712 $arch\_opcode_start = cur_opcode;
715 if (defined($default_op_attr_type)) {
716 print OUT "\tattrs = xmalloc(sizeof(attr[0]) * iro_$arch\_last);\n";
717 print OUT "\tmemset(attrs, 0, sizeof(attr[0]) * iro_$arch\_last);\n";
720 print OUT @obst_new_irop;
722 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
723 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
724 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
730 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
732 my $creation_time = localtime(time());
738 * \@brief Function prototypes for the new opcode functions.
739 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
740 * Edit $specfile instead.
741 * created by: $0 $specfile $target_dir
742 * \@date $creation_time
744 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
745 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
749 print OUT @obst_enum_op;
750 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
751 print OUT "int get_$arch\_opcode_first(void);\n";
752 print OUT "int get_$arch\_opcode_last(void);\n";
753 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
754 print OUT @obst_header;
755 print OUT @obst_proj;
764 # Translates numeric arity into string constant.
766 sub translate_arity {
769 if ($arity =~ /^\d+$/) {
771 return "oparity_zero";
773 elsif ($arity == 1) {
774 return "oparity_unary";
776 elsif ($arity == 2) {
777 return "oparity_binary";
779 elsif ($arity == 3) {
780 return "oparity_trinary";
783 return "oparity_any";
785 } elsif ($arity == $ARITY_VARIABLE) {
786 return "oparity_variable";
787 } elsif ($arity == $ARITY_DYNAMIC) {
788 return "oparity_dynamic";
790 die "Fatal error: Unknown arity $arity";
795 # Return the list of pointers for the given execution units.
797 sub gen_execunit_list_initializer {
799 my $uc_arch = uc($arch);
804 foreach my $unit (@{ $units }) {
805 if ($unit eq "DUMMY") {
806 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
808 elsif (exists($cpu{"$unit"})) {
809 # operation can be executed on all units of this type
811 my $tp_name = "$arch\_execution_units_$unit";
813 foreach (@{ $cpu{"$unit"} }) {
814 next if ($idx++ == 0); # skip first element (it's not a unit)
815 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
816 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
820 # operation can be executed only a certain unit
821 # -> find corresponding unit type
823 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
824 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
825 if ($unit eq $cur_unit) {
826 my $tp_name = "$arch\_execution_units_$cur_type";
827 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
828 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
836 print STDERR "Invalid execution unit $unit specified!\n";
841 # prepare the 2-dim array init
842 foreach my $key (keys(%init)) {
843 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
845 foreach (@{ $init{"$key"} }) {
848 $ret .= "\t\tNULL\n";
850 $ret2 .= "\t\tallowed_units_$key,\n";
852 $ret2 .= "\t\tNULL\n";
854 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
862 sub mangle_requirements {
867 my @alternatives = split(/ /, $reqs);
868 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
869 $alternatives[$idx] =~ s/!/not_/g;
872 @alternatives = sort @alternatives;
874 my $name = $class."_".join('_', @alternatives);
875 if (defined($flags)) {
884 # Determines whether $name is a specified register class or not.
885 # @return 1 if name is register class, 0 otherwise
889 return 1 if exists($reg_classes{"$name"});
894 # Returns the register class for a given register.
895 # @return class or undef
899 $reg = substr($reg, 1) if ($reg =~ /!.*/);
900 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
905 # Returns the index of a given register within it's register class.
906 # @return index or undef
910 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
915 # Remember the register class for each index in the given requirements.
916 # We need this information for requirements like "in_sX" or "out_dX"
917 # @return array of classes corresponding to the requirement for each index
919 sub build_inout_idx_class {
925 my $inout = ($is_in ? "in" : "out");
927 if (exists($n->{"reg_req"}{"$inout"})) {
928 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
930 for (my $idx = 0; $idx <= $#reqs; $idx++) {
932 my ($req,) = split(/:/, $reqs[$idx]);
934 if ($req eq "none") {
936 } elsif (is_reg_class($req)) {
939 my @regs = split(/ /, $req);
940 GET_CLASS: foreach my $reg (@regs) {
941 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
942 $class = "UNKNOWN_CLASS";
944 $class = get_reg_class($reg);
945 if (!defined $class) {
946 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
954 push(@idx_class, $class);
962 # Generates the function for a given $op and a given IN-index
963 # which returns a subset of possible register from a register class
964 # @return classname from which the subset is derived or undef and
965 # pos which corresponds to in/out reference position or undef
967 sub build_subset_class_func {
973 my $different_pos = 0;
979 my $limit_reqs; #used for name mangling
981 # build function header
986 my @regs = split(/ /, shift);
989 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
991 # set/unset registers
992 CHECK_REQS: foreach (@regs) {
993 if (!$is_in && /(!)?in_r(\d+)/) {
994 my $bit_pos = 1 << ($2 - 1);
995 if ($different_pos & $bit_pos) {
997 print STDERR "duplicate !in constraint\n";
999 print STDERR "conflicting !in and in constraints\n";
1001 return (undef, undef, undef, undef);
1004 if ($same_pos & $bit_pos) {
1006 print STDERR "conflicting !in and in constraints\n";
1008 print STDERR "duplicate in constraint\n";
1010 return (undef, undef, undef, undef);
1014 $different_pos |= $bit_pos;
1016 $same_pos |= $bit_pos;
1019 $class = $idx_class[$2 - 1];
1024 if (substr($_, 0, 1) eq "!") {
1025 if (defined($neg) && $neg == 0) {
1026 # we have seen a positiv constraint as first one but this one is negative
1027 # this doesn't make sense
1028 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1029 return (undef, undef, undef, undef);
1032 if (!defined($neg)) {
1036 $_ = substr($_, 1); # skip '!'
1039 if (defined($neg) && $neg == 1) {
1040 # we have seen a negative constraint as first one but this one is positive
1041 # this doesn't make sense
1042 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1043 return (undef, undef, undef, undef);
1050 # check if register belongs to one of the given classes
1051 $temp = get_reg_class($_);
1052 if (!defined($temp)) {
1053 print STDERR "Unknown register '$_'!\n";
1054 return (undef, undef, undef, undef);
1058 if (!defined($class)) {
1060 } elsif ($class ne $temp) {
1061 # all registers must belong to the same class
1062 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1063 return (undef, undef, undef, undef);
1066 # calculate position inside the initializer bitfield (only 32 bits per
1068 my $regidx = get_reg_index($_);
1069 my $arrayp = $regidx / 32;
1070 push(@{$limit_array[$arrayp]}, $_);
1071 $limit_reqs .= "$_ ";
1074 # don't allow ignore regs in negative constraints
1076 my @cur_class = @{ $reg_classes{"$class"} };
1077 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1078 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1079 my $reg = $cur_class[$idx]{"name"};
1080 my $regix = get_reg_index($reg);
1081 my $arrayp = $regix / 32;
1082 push(@{$limit_array[$arrayp]}, $reg);
1083 $limit_reqs .= "$reg ";
1088 if ($has_limit == 1) {
1089 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1091 if(defined($limit_bitsets{$limit_name})) {
1092 $limit_name = $limit_bitsets{$limit_name};
1093 return ($class, $limit_name, $same_pos, $different_pos);
1096 $limit_bitsets{$limit_name} = $limit_name;
1098 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1100 my $limitbitsetlen = $regclass2len{$class};
1101 my $limitarraylen = $limitbitsetlen / 32 + ($limitbitsetlen % 32 > 0 ? 1 : 0);
1102 for(my $i = 0; $i < $limitarraylen; $i++) {
1104 my $limitarraypart = $limit_array[$i];
1108 push(@obst_limit_func, ", ");
1112 $temp = "0xFFFFFFFF";
1114 foreach my $reg (@{$limitarraypart}) {
1117 } elsif(defined($temp)) {
1120 $temp .= "BIT(REG_".uc(${reg}).")";
1122 if(defined($temp)) {
1123 push(@obst_limit_func, "${temp}");
1125 push(@obst_limit_func, "0");
1128 push(@obst_limit_func, " };\n");
1131 return ($class, $limit_name, $same_pos, $different_pos);
1135 # Generate register requirements structure
1137 sub generate_requirements {
1138 my ($reqs, $flags) = split(/:/, shift);
1147 if (defined($flags)) {
1148 foreach my $f (split(/|/, $flags)) {
1150 push(@req_type_mask, "arch_register_req_type_ignore");
1151 } elsif ($f eq "S") {
1152 push(@req_type_mask, "arch_register_req_type_produces_sp");
1157 if ($reqs eq "none") {
1161 arch_register_req_type_none,
1162 NULL, /* regclass */
1163 NULL, /* limit bitset */
1165 0 /* different pos */
1169 } elsif (is_reg_class($reqs)) {
1170 push(@req_type_mask, "arch_register_req_type_normal");
1171 my $reqtype = join(" | ", @req_type_mask);
1176 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1177 NULL, /* limit bitset */
1179 0 /* different pos */
1185 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1186 = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1188 if (!defined($regclass)) {
1189 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1192 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1193 push(@req_type_mask, "arch_register_req_type_limited");
1195 if ($same_pos != 0) {
1196 push(@req_type_mask, "arch_register_req_type_should_be_same");
1198 if ($different_pos != 0) {
1199 push(@req_type_mask, "arch_register_req_type_must_be_different");
1201 my $reqtype = join(" | ", @req_type_mask);
1203 if(!defined($limit_bitset)) {
1204 $limit_bitset = "NULL";
1211 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1213 ${same_pos}, /* same pos */
1214 ${different_pos} /* different pos */
1220 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1221 if(defined($requirements{$name})) {
1224 $requirements{$name} = $name;
1225 push(@obst_reg_reqs, <<EOF);
1226 static const arch_register_req_t ${name} = ${result}