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;
51 our %custom_irn_flags;
58 unless ($return = do $specfile) {
59 die "Fatal error: couldn't parse $specfile: $@" if $@;
60 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
61 die "Fatal error: couldn't run $specfile" unless $return;
65 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
66 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
68 if(!defined($default_attr_type)) {
69 $default_attr_type = "${arch}_attr_t";
71 if(!defined(%init_attr)) {
73 "$default_attr_type" => "\tinit_${arch}_attributes(res, irn_flags_, in_reqs, exec_units, n_res);",
76 if(!defined($default_cmp_attr)) {
77 $default_cmp_attr = "${arch}_compare_attr";
79 if(!defined(%compare_attr)) {
81 "${default_attr_type}" => "${default_cmp_attr}",
85 # Operands are really just nodes with some special constraints, we check
86 # these and create new entries in the nodes hashmap
87 foreach my $op (keys(%operands)) {
88 my %operand = %{ $operands{"$op"} };
92 if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
93 if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
94 if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
95 if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
96 if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
99 foreach my $flag (keys(%operand)) {
100 $op_node{$flag} = $operand{$flag};
102 $op_node{op_flags} = "O";
103 $op_node{cmp_attr} = 'return 1;';
104 $op_node{mode} = 'mode_ANY';
106 $nodes{$op} = \%op_node;
109 #print Dumper(%nodes);
110 #print Dumper(%operands);
112 # create c code file from specs
116 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
117 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
118 my $obst_constructor; # stack for node constructor functions
119 my @obst_new_irop; # stack for the new_ir_op calls
120 my @obst_enum_op; # stack for creating the <arch>_opcode enum
121 my $obst_header; # stack for function prototypes
122 my @obst_is_archirn; # stack for the is_$arch_irn() function
123 my @obst_cmp_attr; # stack for the compare attribute functions
124 my $obst_proj = ""; # stack for the pn_ numbers
129 my $n_opcodes = 0; # number of opcodes
130 my $ARITY_VARIABLE = -1;
131 my $ARITY_DYNAMIC = -2;
132 my %requirements = ();
133 my %limit_bitsets = ();
135 my %regclass2len = ();
137 # build register->class hashes
138 foreach my $class_name (keys(%reg_classes)) {
139 my @class = @{ $reg_classes{"$class_name"} };
140 my $old_classname = $class_name;
144 $class_name = $arch."_".$class_name;
148 $reg2class{$_->{name}} = {
149 "class" => $old_classname,
155 $regclass2len{$old_classname} = $idx;
159 # for registering additional opcodes
160 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
162 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
164 sub create_constructor {
173 $suffix = "_${name}";
177 if (exists($n->{mode})) {
178 $known_mode = $n->{mode};
183 if(exists($n->{"arity"})) {
184 $arity = $n->{"arity"};
185 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
186 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
187 } elsif (exists($n->{"ins"})) {
188 $arity = scalar(@{ $n->{"ins"} });
190 if($arity eq "variable") {
191 $arity = $ARITY_VARIABLE;
192 } elsif($arity eq "dynamic") {
193 $arity = $ARITY_DYNAMIC;
196 # determine out arity
198 if(exists($n->{"out_arity"})) {
199 $out_arity = $n->{"out_arity"};
200 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
201 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
202 } elsif (exists($n->{"outs"})) {
203 $out_arity = scalar(@{ $n->{"outs"} });
205 if($out_arity eq "variable") {
206 $out_arity = $ARITY_VARIABLE;
207 } elsif($out_arity eq "dynamic") {
208 $out_arity = $ARITY_DYNAMIC;
210 if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
211 $known_mode = "mode_T";
214 my $comment = $n->{"comment"};
215 if(!exists($n->{"comment"})) {
216 $comment = "construct ${orig_op} node";
223 $obst_constructor .= $comment;
225 # create constructor head
226 my $complete_args = "";
229 $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
230 if (!exists($n->{"args"})) { # default args
231 if ($arity == $ARITY_VARIABLE) {
232 $complete_args = ", int arity, ir_node *in[]";
233 } elsif ($arity == $ARITY_DYNAMIC) {
236 for (my $i = 0; $i < $arity; $i++) {
237 my $opname = "op${i}";
238 if (exists($n->{"ins"})) {
239 my @ins = @{ $n->{"ins"} };
243 $complete_args .= ", ir_node *${opname}";
246 if ($out_arity == $ARITY_VARIABLE) {
247 $complete_args .= ", int n_res";
250 if (!defined($known_mode)) {
251 $complete_args .= ", ir_mode *mode";
253 } else { # user defined args
254 for my $href (@{ $n->{"args"} }) {
255 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
256 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
260 # we have additional attribute arguements
261 if (exists($n->{"attr"})) {
262 $complete_args .= ", ".$n->{"attr"};
265 $temp .= "$complete_args)";
266 $obst_constructor .= "${temp}\n{\n";
268 $obst_header .= $comment;
269 $obst_header .= "${temp};\n";
271 # emit constructor code
273 ir_graph *irg = get_irn_irg(block);
274 ir_op *op = op_${arch}_${op};
275 arch_irn_flags_t irn_flags_ = arch_irn_flags_none;
277 backend_info_t *info;
280 if($arity == $ARITY_DYNAMIC) {
285 } elsif($arity == $ARITY_VARIABLE) {
300 if($out_arity == $ARITY_DYNAMIC) {
304 } elsif($out_arity == $ARITY_VARIABLE) {
307 int n_res = ${out_arity};
311 if (defined($known_mode)) {
313 ir_mode *mode = ${known_mode};
317 # set up static variables for cpu execution unit assigments
318 if (exists($n->{"units"})) {
319 $temp .= gen_execunit_list_initializer($n->{"units"});
322 static const be_execution_unit_t ***exec_units = NULL;
326 undef my $in_req_var;
327 undef my $out_req_var;
329 my $set_out_reqs = "";
331 # set up static variables for requirements and registers
332 if (exists($n->{"reg_req"})) {
333 my %req = %{ $n->{"reg_req"} };
337 @in = @{ $req{"in"} } if (exists($req{"in"}));
339 @out = @{ $req{"out"} } if exists(($req{"out"}));
341 for(my $idx = 0; $idx < $#in; $idx++) {
343 generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
345 for(my $idx = 0; $idx < $#out; $idx++) {
346 my $req = $out[$idx];
347 generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
351 if($arity >= 0 && scalar(@in) != $arity) {
352 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
355 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
357 for ($idx = 0; $idx <= $#in; $idx++) {
359 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
360 $temp .= "\t\t& ${reqstruct},\n";
365 die "Fatal error: need in requirements for ${op}\n";
367 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
371 if($out_arity >= 0 && scalar(@out) != $out_arity) {
372 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
375 for ($idx = 0; $idx <= $#out; $idx++) {
376 my $req = $out[$idx];
377 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
378 $set_out_reqs .= <<EOF;
379 info->out_infos[${idx}].req = &${reqstruct};
384 die "Fatal error: need out requirements for ${op}\n";
388 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
390 my $attr_type = $on->{attr_type};
391 if(exists($n->{"init_attr"})) {
392 $temp .= "\t${attr_type} *attr;\n";
398 $temp .= "\t/* construct in array */\n";
399 for (my $i = 0; $i < $arity; $i++) {
400 my $opname = "op${i}";
401 if (exists($n->{"ins"})) {
402 my @ins = @{ $n->{"ins"} };
406 $temp .= "\tin[${i}] = ${opname};\n";
412 if (exists($n->{"irn_flags"})) {
413 $temp .= "\t/* flags */\n";
414 my %known_irn_flags = (
415 "none" => "arch_irn_flags_none",
416 "dont_spill" => "arch_irn_flags_dont_spill",
417 "rematerializable" => "arch_irn_flags_rematerializable",
418 "modify_flags" => "arch_irn_flags_modify_flags",
419 "simple_jump" => "arch_irn_flags_simple_jump",
420 "not_scheduled" => "arch_irn_flags_not_scheduled",
422 if (defined(%custom_irn_flags)) {
423 %known_irn_flags = (%known_irn_flags, %custom_irn_flags);
425 foreach my $flag (@{$n->{"irn_flags"}}) {
426 if (not defined($known_irn_flags{$flag})) {
427 print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
429 $temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
435 # lookup init function
436 my $attr_init_code = "(void)in;(void)exec_units;(void)irn_flags_;(void)in_reqs;(void)n_res;";
437 if ($attr_type ne "") {
438 $attr_init_code = $init_attr{$attr_type};
439 if(!defined($attr_init_code)) {
440 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
444 if(defined($custom_init_attr_func)) {
445 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
447 if(defined($n->{custominit})) {
448 $custominit .= $n->{custominit};
454 res = new_ir_node(dbgi, irg, block, op, mode, arity, in);
456 /* init node attributes */
459 info = be_get_info(res);
460 (void) info; /* avoid potential warning */
465 if (exists($n->{"init_attr"})) {
466 $temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
467 $temp .= "\t(void) attr; /* avoid potential warning */\n";
468 $temp .= "\t".$n->{"init_attr"}."\n";
473 res = optimize_node(res);
474 irn_verify_irg(res, irg);
479 $obst_constructor .= $temp;
481 # close constructor function
482 $obst_constructor .= "}\n\n";
485 push(@obst_enum_op, "typedef enum ${arch}_opcodes {\n");
486 foreach my $op (keys(%nodes)) {
487 my %n = %{ $nodes{"$op"} };
495 if(exists($n{"arity"})) {
496 $arity = $n{"arity"};
497 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
498 $arity = scalar(@{ $n{"reg_req"}{"in"} });
499 } elsif (exists($n{"ins"})) {
500 $arity = scalar(@{ $n{"ins"} });
502 if($arity eq "variable") {
503 $arity = $ARITY_VARIABLE;
504 } elsif($arity eq "dynamic") {
505 $arity = $ARITY_DYNAMIC;
508 # determine out arity
510 if(exists($n{"out_arity"})) {
511 $out_arity = $n{"out_arity"};
512 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
513 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
514 } elsif (exists($n{"outs"})) {
515 $out_arity = scalar(@{ $n{"outs"} });
517 if($out_arity eq "variable") {
518 $out_arity = $ARITY_VARIABLE;
519 } elsif($out_arity eq "dynamic") {
520 $out_arity = $ARITY_DYNAMIC;
527 # define proj numbers and in numbers
528 if (exists($n{"outs"})) {
531 @outs = @{ $n{"outs"} };
532 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
533 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
536 $num_outs = $#outs + 1;
539 $obst_proj .= "\nenum pn_${op} {\n";
541 for (my $idx = 0; $idx <= $#outs; $idx++) {
542 # check, if we have additional flags annotated to out
543 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
544 push(@out_flags, $1);
545 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
547 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
550 $obst_proj .= "};\n";
552 # outs have names, it must be a mode_T node
553 if (!defined($n{mode})) {
557 if (exists($n{"ins"})) {
560 @ins = @{ $n{"ins"} };
561 if($arity >= 0 && scalar(@ins) != $arity) {
562 die "Fatal error: Op ${op} has different number of ins and arity\n";
566 $obst_proj .= "\nenum n_$op {\n";
567 for (my $idx = 0; $idx <= $#ins; $idx++) {
568 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
570 $obst_proj .= "};\n";
575 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
576 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
577 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
579 push(@obst_is_archirn, "is_$op(node)");
581 $obst_header .= <<EOF;
582 extern ir_op *op_${op};
583 ir_op *get_op_${op}(void);
584 int is_${op}(const ir_node *n);
587 my $attr_type= $n{attr_type};
588 if(!defined($attr_type)) {
589 $attr_type = $default_attr_type;
590 $n{attr_type} = $attr_type;
593 # determine hash function
595 if (exists($n{"hash_func"})) {
596 $hash_func = $n{"hash_func"};
599 # determine compare function
601 if (exists($n{"cmp_attr"})) {
602 my $cmpcode = $n{"cmp_attr"};
604 push(@obst_cmp_attr, "static int cmp_attr_$op(const ir_node *a, const ir_node *b) {\n");
605 if($cmpcode =~ m/attr_a/) {
606 push(@obst_cmp_attr, "\tconst ${attr_type} *attr_a = get_irn_generic_attr_const(a);\n");
608 push(@obst_cmp_attr, "\t(void) a;\n");
610 if($cmpcode =~ m/attr_b/) {
611 push(@obst_cmp_attr, "\tconst ${attr_type} *attr_b = get_irn_generic_attr_const(b);\n");
613 push(@obst_cmp_attr, "\t(void) b;\n");
615 push(@obst_cmp_attr, "\t${cmpcode}\n");
616 push(@obst_cmp_attr, "}\n\n");
618 $cmp_attr_func = "cmp_attr_${op}";
619 } elsif ($attr_type eq "") {
620 $cmp_attr_func = "NULL";
622 if(defined($compare_attr{${attr_type}})) {
623 $cmp_attr_func = $compare_attr{${attr_type}};
625 die "Fatal error: No compare function defined for ${attr_type} attributes.";
630 if (exists($n{constructors})) {
631 %constructors = %{ $n{constructors} };
633 # Create 1 default constructor
634 my %constructor = ();
635 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
636 "reg_req", "init_attr", "irn_flags", "mode", "arity",
637 "out_arity", "custominit") {
638 if (defined($n{$a})) {
639 $constructor{$a} = $n{$a};
642 %constructors = ( "" => \%constructor );
645 foreach my $constr (keys(%constructors)) {
646 my %cstr = %{ $constructors{$constr} };
647 # Copy some values from outer node if they don't exists in the constr
648 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
650 if (!defined($cstr{$a}) && defined($n{$a})) {
654 create_constructor($orig_op, $constr, \%cstr, \%n);
657 # set default values for state and flags if not given
658 $n{"state"} = "floats" if (! exists($n{"state"}));
659 $n{"op_flags"} = ["none"] if (! exists($n{"op_flags"}));
660 $n{"dump_func"} = "${arch}_dump_node" if (!exists($n{"dump_func"}));
661 my $dump_func = $n{"dump_func"};
663 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
664 push(@obst_new_irop, "\tops.be_ops = be_ops;\n");
665 push(@obst_new_irop, "\tops.dump_node = ${dump_func};\n");
667 if (defined($cmp_attr_func)) {
668 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
670 my $copy_attr_func = $copy_attr{$attr_type};
671 if (!defined($copy_attr_func)) {
672 if ($attr_type eq "") {
673 $copy_attr_func = "NULL";
675 $copy_attr_func = $default_copy_attr;
678 if (defined($copy_attr_func)) {
679 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
681 if (defined($hash_func)) {
682 push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
685 my %known_flags = map { $_ => 1 } (
686 "none", "labeled", "commutative", "cfopcode", "unknown_jump", "fragile",
687 "forking", "highlevel", "constlike", "always_opt", "keep",
688 "start_block", "uses_memory", "dump_noblock", "dump_noinput",
689 "machine", "machine_op", "cse_neutral"
692 foreach my $flag (@{$n{"op_flags"}}) {
693 if (not defined($known_flags{$flag})) {
694 print STDERR "WARNING: Flag '$flag' in opcode $op is unknown\n";
696 if ($flag eq "fragile") {
700 my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
701 my $op_flags = join('|', @mapped);
704 if ($attr_type ne "") {
705 $attr_size = "sizeof(${attr_type})"
709 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
710 $temp .= "|irop_flag_machine, ".translate_arity($arity).", 0, ${attr_size}, &ops);\n";
711 push(@obst_new_irop, $temp);
713 push(@obst_new_irop, "\tir_op_set_fragile_indices(op_${op}, n_${op}_mem, pn_${op}_X_regular, pn_${op}_X_except);\n");
715 push(@obst_new_irop, "\tset_op_tag(op_$op, $arch\_op_tag);\n");
716 if(defined($default_op_attr_type)) {
717 push(@obst_new_irop, "\tattr = &attrs[iro_$op];\n");
718 if(defined($n{op_attr_init})) {
719 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
721 push(@obst_new_irop, "\tset_op_attr(op_$op, attr);\n");
724 push(@obst_enum_op, "\tiro_$op,\n");
726 $obst_header .= "\n";
728 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
729 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
730 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
731 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
735 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
737 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n";
738 print OUT "#include \"irverify_t.h\"\n";
740 print OUT @obst_cmp_attr;
742 print OUT @obst_opvar;
744 print OUT @obst_get_opvar;
749 static int $arch\_opcode_start = -1;
750 static int $arch\_opcode_end = -1;
754 # build the FOURCC arguments from $arch
756 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
758 if (length($arch) >= 1) {
759 $a = uc(substr($arch, 0, 1));
762 if (length($arch) >= 2) {
763 $b = uc(substr($arch, 1, 1));
766 if (length($arch) >= 3) {
767 $c = uc(substr($arch, 2, 1));
770 if (length($arch) >= 4) {
771 $d = uc(substr($arch, 3, 1));
774 print OUT<<ENDOFISIRN;
776 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
777 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
779 /** Return the opcode number of the first $arch opcode. */
780 int get_$arch\_opcode_first(void) {
781 return $arch\_opcode_start;
784 /** Return the opcode number of the last $arch opcode + 1. */
785 int get_$arch\_opcode_last(void) {
786 return $arch\_opcode_end;
789 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
790 int is_$arch\_op(const ir_op *op) {
791 return get_op_tag(op) == $arch\_op_tag;
794 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
795 int is_$arch\_irn(const ir_node *node) {
796 return is_$arch\_op(get_irn_op(node));
799 int get_$arch\_irn_opcode(const ir_node *node) {
800 if (is_$arch\_irn(node))
801 return get_irn_opcode(node) - $arch\_opcode_start;
811 #define BIT(x) (1 << (x))
815 print OUT @obst_limit_func;
818 print OUT @obst_reg_reqs;
821 print OUT<<ENDOFMAIN;
825 * Creates the $arch specific Firm machine operations
826 * needed for the assembler irgs.
828 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops) {
831 static int run_once = 0;
834 if (defined($default_op_attr_type)) {
835 print OUT "\t$default_op_attr_type *attr, *attrs;\n";
838 print OUT<<ENDOFMAIN;
844 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
846 $arch\_opcode_start = cur_opcode;
849 if (defined($default_op_attr_type)) {
850 print OUT "\tattrs = XMALLOCNZ(${default_op_attr_type}, iro_${arch}_last);\n";
853 print OUT @obst_new_irop;
855 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
856 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
857 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
863 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
865 my $creation_time = localtime(time());
871 * \@brief Function prototypes for the new opcode functions.
872 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
873 * Edit $specfile instead.
874 * created by: $0 $specfile $target_dir
875 * \@date $creation_time
877 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
878 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
882 print OUT @obst_enum_op;
884 int is_${arch}_irn(const ir_node *node);
885 int is_${arch}_op(const ir_op *op);
887 int get_${arch}_opcode_first(void);
888 int get_${arch}_opcode_last(void);
889 int get_${arch}_irn_opcode(const ir_node *node);
899 # Translates numeric arity into string constant.
901 sub translate_arity {
904 if ($arity =~ /^\d+$/) {
906 return "oparity_zero";
908 elsif ($arity == 1) {
909 return "oparity_unary";
911 elsif ($arity == 2) {
912 return "oparity_binary";
914 elsif ($arity == 3) {
915 return "oparity_trinary";
918 return "oparity_any";
920 } elsif ($arity == $ARITY_VARIABLE) {
921 return "oparity_variable";
922 } elsif ($arity == $ARITY_DYNAMIC) {
923 return "oparity_dynamic";
925 die "Fatal error: Unknown arity $arity";
930 # Return the list of pointers for the given execution units.
932 sub gen_execunit_list_initializer {
934 my $uc_arch = uc($arch);
939 foreach my $unit (@{ $units }) {
940 if ($unit eq "DUMMY") {
941 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
943 elsif (exists($cpu{"$unit"})) {
944 # operation can be executed on all units of this type
946 my $tp_name = "$arch\_execution_units_$unit";
948 foreach (@{ $cpu{"$unit"} }) {
949 next if ($idx++ == 0); # skip first element (it's not a unit)
950 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
951 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
955 # operation can be executed only a certain unit
956 # -> find corresponding unit type
958 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
959 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
960 if ($unit eq $cur_unit) {
961 my $tp_name = "$arch\_execution_units_$cur_type";
962 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
963 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
971 print STDERR "Invalid execution unit $unit specified!\n";
976 # prepare the 2-dim array init
977 foreach my $key (keys(%init)) {
978 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
980 foreach (@{ $init{"$key"} }) {
983 $ret .= "\t\tNULL\n";
985 $ret2 .= "\t\tallowed_units_$key,\n";
987 $ret2 .= "\t\tNULL\n";
989 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
997 sub mangle_requirements {
1002 my @alternatives = split(/ /, $reqs);
1003 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
1004 $alternatives[$idx] =~ s/!/not_/g;
1007 @alternatives = sort @alternatives;
1009 my $name = $class."_".join('_', @alternatives);
1010 if (defined($flags)) {
1019 # Determines whether $name is a specified register class or not.
1020 # @return 1 if name is register class, 0 otherwise
1024 return 1 if exists($reg_classes{"$name"});
1029 # Returns the register class for a given register.
1030 # @return class or undef
1034 $reg = substr($reg, 1) if ($reg =~ /!.*/);
1035 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
1040 # Returns the index of a given register within its register class.
1041 # @return index or undef
1045 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
1050 # Remember the register class for each index in the given requirements.
1051 # We need this information for requirements like "in_sX" or "out_dX"
1052 # @return array of classes corresponding to the requirement for each index
1054 sub build_inout_idx_class {
1060 my $inout = ($is_in ? "in" : "out");
1062 if (exists($n->{"reg_req"}{"$inout"})) {
1063 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
1065 for (my $idx = 0; $idx <= $#reqs; $idx++) {
1067 my ($req,) = split(/:/, $reqs[$idx]);
1069 if ($req eq "none") {
1071 } elsif (is_reg_class($req)) {
1074 my @regs = split(/ /, $req);
1075 GET_CLASS: foreach my $reg (@regs) {
1076 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
1077 $class = "UNKNOWN_CLASS";
1079 $class = get_reg_class($reg);
1080 if (!defined $class) {
1081 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
1089 push(@idx_class, $class);
1097 # Generates the function for a given $op and a given IN-index
1098 # which returns a subset of possible register from a register class
1099 # @return classname from which the subset is derived or undef and
1100 # pos which corresponds to in/out reference position or undef
1102 sub build_subset_class_func {
1108 my $different_pos = 0;
1114 my $limit_reqs; #used for name mangling
1116 # build function header
1121 my @regs = split(/ /, shift);
1124 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1126 # set/unset registers
1127 CHECK_REQS: foreach (@regs) {
1128 if (!$is_in && /(!)?in_r(\d+)/) {
1129 my $bit_pos = 1 << ($2 - 1);
1130 if ($different_pos & $bit_pos) {
1132 print STDERR "duplicate !in constraint\n";
1134 print STDERR "conflicting !in and in constraints\n";
1136 return (undef, undef, undef, undef);
1139 if ($same_pos & $bit_pos) {
1141 print STDERR "conflicting !in and in constraints\n";
1143 print STDERR "duplicate in constraint\n";
1145 return (undef, undef, undef, undef);
1149 $different_pos |= $bit_pos;
1151 $same_pos |= $bit_pos;
1154 $class = $idx_class[$2 - 1];
1159 if (substr($_, 0, 1) eq "!") {
1160 if (defined($neg) && $neg == 0) {
1161 # we have seen a positiv constraint as first one but this one is negative
1162 # this doesn't make sense
1163 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1164 return (undef, undef, undef, undef);
1167 if (!defined($neg)) {
1171 $_ = substr($_, 1); # skip '!'
1174 if (defined($neg) && $neg == 1) {
1175 # we have seen a negative constraint as first one but this one is positive
1176 # this doesn't make sense
1177 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1178 return (undef, undef, undef, undef);
1185 # check if register belongs to one of the given classes
1186 $temp = get_reg_class($_);
1187 if (!defined($temp)) {
1188 print STDERR "Unknown register '$_'!\n";
1189 return (undef, undef, undef, undef);
1193 if (!defined($class)) {
1195 } elsif ($class ne $temp) {
1196 # all registers must belong to the same class
1197 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1198 return (undef, undef, undef, undef);
1201 # calculate position inside the initializer bitfield (only 32 bits per
1203 my $regidx = get_reg_index($_);
1204 my $arrayp = $regidx / 32;
1205 push(@{$limit_array[$arrayp]}, $_);
1206 $limit_reqs .= "$_ ";
1209 # don't allow ignore regs in negative constraints
1211 my @cur_class = @{ $reg_classes{"$class"} };
1212 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1213 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1214 my $reg = $cur_class[$idx]{"name"};
1215 my $regix = get_reg_index($reg);
1216 my $arrayp = $regix / 32;
1217 push(@{$limit_array[$arrayp]}, $reg);
1218 $limit_reqs .= "$reg ";
1223 if ($has_limit == 1) {
1224 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1226 if(defined($limit_bitsets{$limit_name})) {
1227 $limit_name = $limit_bitsets{$limit_name};
1228 return ($class, $limit_name, $same_pos, $different_pos);
1231 $limit_bitsets{$limit_name} = $limit_name;
1233 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1235 my $limitbitsetlen = $regclass2len{$class};
1236 my $limitarraylen = ($limitbitsetlen+31) / 32;
1237 for(my $i = 0; $i < $limitarraylen; $i++) {
1239 my $limitarraypart = $limit_array[$i];
1243 push(@obst_limit_func, ", ");
1247 $temp = "0xFFFFFFFF";
1249 foreach my $reg (@{$limitarraypart}) {
1252 } elsif(defined($temp)) {
1255 my $firstreg = uc($reg_classes{$class}[0]->{"name"});
1256 my $classuc = uc($class);
1257 my $reguc = uc($reg);
1258 $temp .= "BIT(REG_${classuc}_${reguc})";
1260 if(defined($temp)) {
1261 push(@obst_limit_func, "${temp}");
1263 push(@obst_limit_func, "0");
1266 push(@obst_limit_func, " };\n");
1269 return ($class, $limit_name, $same_pos, $different_pos);
1273 # Generate register requirements structure
1275 sub generate_requirements {
1276 my ($reqs, $flags) = split(/:/, shift);
1286 if (defined($flags)) {
1287 foreach my $f (split(/|/, $flags)) {
1289 push(@req_type_mask, "arch_register_req_type_ignore");
1290 } elsif ($f eq "S") {
1291 push(@req_type_mask, "arch_register_req_type_produces_sp");
1292 } elsif ($f eq "a") {
1293 push(@req_type_mask, "arch_register_req_type_aligned");
1294 } elsif ($f eq "2" or $f eq "4" or $f eq "8") {
1300 if ($reqs eq "none") {
1304 arch_register_req_type_none,
1305 NULL, /* regclass */
1306 NULL, /* limit bitset */
1308 0, /* different pos */
1313 } elsif (is_reg_class($reqs)) {
1314 push(@req_type_mask, "arch_register_req_type_normal");
1315 my $reqtype = join(" | ", @req_type_mask);
1320 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1321 NULL, /* limit bitset */
1323 0, /* different pos */
1330 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1331 = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1333 if (!defined($regclass)) {
1334 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1337 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1338 push(@req_type_mask, "arch_register_req_type_limited");
1340 if ($same_pos != 0) {
1341 push(@req_type_mask, "arch_register_req_type_should_be_same");
1343 if ($different_pos != 0) {
1344 push(@req_type_mask, "arch_register_req_type_must_be_different");
1346 my $reqtype = join(" | ", @req_type_mask);
1348 if(!defined($limit_bitset)) {
1349 $limit_bitset = "NULL";
1356 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1358 ${same_pos}, /* same pos */
1359 ${different_pos}, /* different pos */
1366 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1367 if(defined($requirements{$name})) {
1370 $requirements{$name} = $name;
1371 push(@obst_reg_reqs, <<EOF);
1372 static const arch_register_req_t ${name} = ${result}