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, 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
274 ir_op *op = op_${arch}_${op};
275 arch_irn_flags_t flags = arch_irn_flags_none;
276 backend_info_t *info;
279 if($arity == $ARITY_DYNAMIC) {
284 } elsif($arity == $ARITY_VARIABLE) {
299 if($out_arity == $ARITY_DYNAMIC) {
303 } elsif($out_arity == $ARITY_VARIABLE) {
306 int n_res = ${out_arity};
310 if (defined($known_mode)) {
312 ir_mode *mode = ${known_mode};
316 # set up static variables for cpu execution unit assigments
317 if (exists($n->{"units"})) {
318 $temp .= gen_execunit_list_initializer($n->{"units"});
321 static const be_execution_unit_t ***exec_units = NULL;
325 undef my $in_req_var;
326 undef my $out_req_var;
328 my $set_out_reqs = "";
330 # set up static variables for requirements and registers
331 if (exists($n->{"reg_req"})) {
332 my %req = %{ $n->{"reg_req"} };
336 @in = @{ $req{"in"} } if (exists($req{"in"}));
338 @out = @{ $req{"out"} } if exists(($req{"out"}));
340 for(my $idx = 0; $idx < $#in; $idx++) {
342 generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
344 for(my $idx = 0; $idx < $#out; $idx++) {
345 my $req = $out[$idx];
346 generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
350 if($arity >= 0 && scalar(@in) != $arity) {
351 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
354 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
356 for ($idx = 0; $idx <= $#in; $idx++) {
358 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
359 $temp .= "\t\t& ${reqstruct},\n";
364 die "Fatal error: need in requirements for ${op}\n";
366 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
370 if($out_arity >= 0 && scalar(@out) != $out_arity) {
371 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
374 for ($idx = 0; $idx <= $#out; $idx++) {
375 my $req = $out[$idx];
376 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
377 $set_out_reqs .= <<EOF;
378 info->out_infos[${idx}].req = &${reqstruct};
383 die "Fatal error: need out requirements for ${op}\n";
387 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
389 my $attr_type = $on->{attr_type};
390 if(exists($n->{"init_attr"})) {
391 $temp .= "\t${attr_type} *attr;\n";
397 $temp .= "\t/* construct in array */\n";
398 for (my $i = 0; $i < $arity; $i++) {
399 my $opname = "op${i}";
400 if (exists($n->{"ins"})) {
401 my @ins = @{ $n->{"ins"} };
405 $temp .= "\tin[${i}] = ${opname};\n";
411 if (exists($n->{"irn_flags"})) {
412 $temp .= "\t/* flags */\n";
413 my %known_irn_flags = (
414 "none" => "arch_irn_flags_none",
415 "dont_spill" => "arch_irn_flags_dont_spill",
416 "rematerializable" => "arch_irn_flags_rematerializable",
417 "modify_flags" => "arch_irn_flags_modify_flags",
418 "simple_jump" => "arch_irn_flags_simple_jump",
419 "not_scheduled" => "arch_irn_flags_not_scheduled",
421 if (defined(%custom_irn_flags)) {
422 %known_irn_flags = (%known_irn_flags, %custom_irn_flags);
424 foreach my $flag (@{$n->{"irn_flags"}}) {
425 if (not defined($known_irn_flags{$flag})) {
426 print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
428 $temp .= "\tflags |= " . $known_irn_flags{$flag} . ";\n";
434 # lookup init function
435 my $attr_init_code = $init_attr{$attr_type};
436 if(!defined($attr_init_code)) {
437 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
440 if(defined($custom_init_attr_func)) {
441 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
443 if(defined($n->{custominit})) {
444 $custominit .= $n->{custominit};
450 res = new_ir_node(dbgi, current_ir_graph, block, op, mode, arity, in);
452 /* init node attributes */
455 info = be_get_info(res);
456 (void) info; /* avoid potential warning */
461 if (exists($n->{"init_attr"})) {
462 $temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
463 $temp .= "\t".$n->{"init_attr"}."\n";
468 res = optimize_node(res);
469 irn_verify_irg(res, current_ir_graph);
474 $obst_constructor .= $temp;
476 # close constructor function
477 $obst_constructor .= "}\n\n";
480 push(@obst_enum_op, "typedef enum ${arch}_opcodes {\n");
481 foreach my $op (keys(%nodes)) {
482 my %n = %{ $nodes{"$op"} };
490 if(exists($n{"arity"})) {
491 $arity = $n{"arity"};
492 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
493 $arity = scalar(@{ $n{"reg_req"}{"in"} });
494 } elsif (exists($n{"ins"})) {
495 $arity = scalar(@{ $n{"ins"} });
497 if($arity eq "variable") {
498 $arity = $ARITY_VARIABLE;
499 } elsif($arity eq "dynamic") {
500 $arity = $ARITY_DYNAMIC;
503 # determine out arity
505 if(exists($n{"out_arity"})) {
506 $out_arity = $n{"out_arity"};
507 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
508 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
509 } elsif (exists($n{"outs"})) {
510 $out_arity = scalar(@{ $n{"outs"} });
512 if($out_arity eq "variable") {
513 $out_arity = $ARITY_VARIABLE;
514 } elsif($out_arity eq "dynamic") {
515 $out_arity = $ARITY_DYNAMIC;
522 # define proj numbers and in numbers
523 if (exists($n{"outs"})) {
526 @outs = @{ $n{"outs"} };
527 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
528 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
531 $num_outs = $#outs + 1;
534 $obst_proj .= "\nenum pn_${op} {\n";
536 for (my $idx = 0; $idx <= $#outs; $idx++) {
537 # check, if we have additional flags annotated to out
538 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
539 push(@out_flags, $1);
540 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
542 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
545 $obst_proj .= "};\n";
547 # outs have names, it must be a mode_T node
548 if (!defined($n{mode})) {
552 if (exists($n{"ins"})) {
555 @ins = @{ $n{"ins"} };
556 if($arity >= 0 && scalar(@ins) != $arity) {
557 die "Fatal error: Op ${op} has different number of ins and arity\n";
561 $obst_proj .= "\nenum n_$op {\n";
562 for (my $idx = 0; $idx <= $#ins; $idx++) {
563 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
565 $obst_proj .= "};\n";
570 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
571 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
572 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
574 push(@obst_is_archirn, "is_$op(node)");
576 $obst_header .= <<EOF;
577 extern ir_op *op_${op};
578 ir_op *get_op_${op}(void);
579 int is_${op}(const ir_node *n);
582 my $attr_type= $n{attr_type};
583 if(!defined($attr_type)) {
584 $attr_type = $default_attr_type;
585 $n{attr_type} = $attr_type;
588 # determine hash function
590 if (exists($n{"hash_func"})) {
591 $hash_func = $n{"hash_func"};
594 # determine compare function
596 if (exists($n{"cmp_attr"})) {
597 my $cmpcode = $n{"cmp_attr"};
599 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
600 if($cmpcode =~ m/attr_a/) {
601 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
603 push(@obst_cmp_attr, "\t(void) a;\n");
605 if($cmpcode =~ m/attr_b/) {
606 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
608 push(@obst_cmp_attr, "\t(void) b;\n");
610 push(@obst_cmp_attr, "\t${cmpcode}\n");
611 push(@obst_cmp_attr, "}\n\n");
613 $cmp_attr_func = "cmp_attr_${op}";
615 if(defined($compare_attr{${attr_type}})) {
616 $cmp_attr_func = $compare_attr{${attr_type}};
618 die "Fatal error: No compare function defined for ${attr_type} attributes.";
623 if (exists($n{constructors})) {
624 %constructors = %{ $n{constructors} };
626 # Create 1 default constructor
627 my %constructor = ();
628 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
629 "reg_req", "init_attr", "irn_flags", "mode", "arity",
630 "out_arity", "custominit") {
631 if (defined($n{$a})) {
632 $constructor{$a} = $n{$a};
635 %constructors = ( "" => \%constructor );
638 foreach my $constr (keys(%constructors)) {
639 my %cstr = %{ $constructors{$constr} };
640 # Copy some values from outer node if they don't exists in the constr
641 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
643 if (!defined($cstr{$a}) && defined($n{$a})) {
647 create_constructor($orig_op, $constr, \%cstr, \%n);
650 # set default values for state and flags if not given
651 $n{"state"} = "floats" if (! exists($n{"state"}));
652 $n{"op_flags"} = ["none"] if (! exists($n{"op_flags"}));
654 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
655 push(@obst_new_irop, "\tops.be_ops = be_ops;\n");
656 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
658 if (defined($cmp_attr_func)) {
659 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
661 my $copy_attr_func = $copy_attr{$attr_type};
662 if (!defined($copy_attr_func)) {
663 $copy_attr_func = $default_copy_attr;
665 if (defined($copy_attr_func)) {
666 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
668 if (defined($hash_func)) {
669 push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
672 my %known_flags = map { $_ => 1 } (
673 "none", "labeled", "commutative", "cfopcode", "op_cfopcode",
674 "fragile", "forking", "highlevel", "constlike", "always_opt",
675 "keep", "start_block", "uses_memory", "dump_noblock",
676 "dump_noinput", "machine", "machine_op", "cse_neutral"
678 foreach my $flag (@{$n{"op_flags"}}) {
679 if (not defined($known_flags{$flag})) {
680 print STDERR "WARNING: Flag '$flag' in opcode $op is unknown\n";
683 my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
684 my $op_flags = join('|', @mapped);
687 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
688 $temp .= "|irop_flag_machine, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
689 push(@obst_new_irop, $temp);
690 push(@obst_new_irop, "\tset_op_tag(op_$op, $arch\_op_tag);\n");
691 if(defined($default_op_attr_type)) {
692 push(@obst_new_irop, "\tattr = &attrs[iro_$op];\n");
693 if(defined($n{op_attr_init})) {
694 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
696 push(@obst_new_irop, "\tset_op_attr(op_$op, attr);\n");
699 push(@obst_enum_op, "\tiro_$op,\n");
701 $obst_header .= "\n";
703 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
704 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
705 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
706 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
710 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
712 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n";
713 print OUT "#include \"irverify_t.h\"\n";
715 print OUT @obst_cmp_attr;
717 print OUT @obst_opvar;
719 print OUT @obst_get_opvar;
724 static int $arch\_opcode_start = -1;
725 static int $arch\_opcode_end = -1;
729 # build the FOURCC arguments from $arch
731 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
733 if (length($arch) >= 1) {
734 $a = uc(substr($arch, 0, 1));
737 if (length($arch) >= 2) {
738 $b = uc(substr($arch, 1, 1));
741 if (length($arch) >= 3) {
742 $c = uc(substr($arch, 2, 1));
745 if (length($arch) >= 4) {
746 $d = uc(substr($arch, 3, 1));
749 print OUT<<ENDOFISIRN;
751 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
752 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
754 /** Return the opcode number of the first $arch opcode. */
755 int get_$arch\_opcode_first(void) {
756 return $arch\_opcode_start;
759 /** Return the opcode number of the last $arch opcode + 1. */
760 int get_$arch\_opcode_last(void) {
761 return $arch\_opcode_end;
764 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
765 int is_$arch\_op(const ir_op *op) {
766 return get_op_tag(op) == $arch\_op_tag;
769 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
770 int is_$arch\_irn(const ir_node *node) {
771 return is_$arch\_op(get_irn_op(node));
774 int get_$arch\_irn_opcode(const ir_node *node) {
775 if (is_$arch\_irn(node))
776 return get_irn_opcode(node) - $arch\_opcode_start;
786 #define BIT(x) (1 << (x))
790 print OUT @obst_limit_func;
793 print OUT @obst_reg_reqs;
796 print OUT<<ENDOFMAIN;
800 * Creates the $arch specific Firm machine operations
801 * needed for the assembler irgs.
803 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops) {
804 #define N irop_flag_none
805 #define L irop_flag_labeled
806 #define C irop_flag_commutative
807 #define X irop_flag_cfopcode
808 #define F irop_flag_fragile
809 #define Y irop_flag_forking
810 #define H irop_flag_highlevel
811 #define c irop_flag_constlike
812 #define K irop_flag_keep
813 #define M irop_flag_machine
814 #define O irop_flag_machine_op
815 #define NB irop_flag_dump_noblock
816 #define NI irop_flag_dump_noinput
817 #define n irop_flag_cse_neutral
818 #define R (irop_flag_user << 0)
822 static int run_once = 0;
825 if (defined($default_op_attr_type)) {
826 print OUT "\t$default_op_attr_type *attr, *attrs;\n";
829 print OUT<<ENDOFMAIN;
835 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
837 $arch\_opcode_start = cur_opcode;
840 if (defined($default_op_attr_type)) {
841 print OUT "\tattrs = XMALLOCNZ(${default_op_attr_type}, iro_${arch}_last);\n";
844 print OUT @obst_new_irop;
846 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
847 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
848 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
854 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
856 my $creation_time = localtime(time());
862 * \@brief Function prototypes for the new opcode functions.
863 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
864 * Edit $specfile instead.
865 * created by: $0 $specfile $target_dir
866 * \@date $creation_time
868 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
869 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
873 print OUT @obst_enum_op;
875 int is_${arch}_irn(const ir_node *node);
876 int is_${arch}_op(const ir_op *op);
878 int get_${arch}_opcode_first(void);
879 int get_${arch}_opcode_last(void);
880 int get_${arch}_irn_opcode(const ir_node *node);
890 # Translates numeric arity into string constant.
892 sub translate_arity {
895 if ($arity =~ /^\d+$/) {
897 return "oparity_zero";
899 elsif ($arity == 1) {
900 return "oparity_unary";
902 elsif ($arity == 2) {
903 return "oparity_binary";
905 elsif ($arity == 3) {
906 return "oparity_trinary";
909 return "oparity_any";
911 } elsif ($arity == $ARITY_VARIABLE) {
912 return "oparity_variable";
913 } elsif ($arity == $ARITY_DYNAMIC) {
914 return "oparity_dynamic";
916 die "Fatal error: Unknown arity $arity";
921 # Return the list of pointers for the given execution units.
923 sub gen_execunit_list_initializer {
925 my $uc_arch = uc($arch);
930 foreach my $unit (@{ $units }) {
931 if ($unit eq "DUMMY") {
932 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
934 elsif (exists($cpu{"$unit"})) {
935 # operation can be executed on all units of this type
937 my $tp_name = "$arch\_execution_units_$unit";
939 foreach (@{ $cpu{"$unit"} }) {
940 next if ($idx++ == 0); # skip first element (it's not a unit)
941 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
942 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
946 # operation can be executed only a certain unit
947 # -> find corresponding unit type
949 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
950 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
951 if ($unit eq $cur_unit) {
952 my $tp_name = "$arch\_execution_units_$cur_type";
953 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
954 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
962 print STDERR "Invalid execution unit $unit specified!\n";
967 # prepare the 2-dim array init
968 foreach my $key (keys(%init)) {
969 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
971 foreach (@{ $init{"$key"} }) {
974 $ret .= "\t\tNULL\n";
976 $ret2 .= "\t\tallowed_units_$key,\n";
978 $ret2 .= "\t\tNULL\n";
980 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
988 sub mangle_requirements {
993 my @alternatives = split(/ /, $reqs);
994 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
995 $alternatives[$idx] =~ s/!/not_/g;
998 @alternatives = sort @alternatives;
1000 my $name = $class."_".join('_', @alternatives);
1001 if (defined($flags)) {
1010 # Determines whether $name is a specified register class or not.
1011 # @return 1 if name is register class, 0 otherwise
1015 return 1 if exists($reg_classes{"$name"});
1020 # Returns the register class for a given register.
1021 # @return class or undef
1025 $reg = substr($reg, 1) if ($reg =~ /!.*/);
1026 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
1031 # Returns the index of a given register within it's register class.
1032 # @return index or undef
1036 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
1041 # Remember the register class for each index in the given requirements.
1042 # We need this information for requirements like "in_sX" or "out_dX"
1043 # @return array of classes corresponding to the requirement for each index
1045 sub build_inout_idx_class {
1051 my $inout = ($is_in ? "in" : "out");
1053 if (exists($n->{"reg_req"}{"$inout"})) {
1054 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
1056 for (my $idx = 0; $idx <= $#reqs; $idx++) {
1058 my ($req,) = split(/:/, $reqs[$idx]);
1060 if ($req eq "none") {
1062 } elsif (is_reg_class($req)) {
1065 my @regs = split(/ /, $req);
1066 GET_CLASS: foreach my $reg (@regs) {
1067 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
1068 $class = "UNKNOWN_CLASS";
1070 $class = get_reg_class($reg);
1071 if (!defined $class) {
1072 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
1080 push(@idx_class, $class);
1088 # Generates the function for a given $op and a given IN-index
1089 # which returns a subset of possible register from a register class
1090 # @return classname from which the subset is derived or undef and
1091 # pos which corresponds to in/out reference position or undef
1093 sub build_subset_class_func {
1099 my $different_pos = 0;
1105 my $limit_reqs; #used for name mangling
1107 # build function header
1112 my @regs = split(/ /, shift);
1115 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1117 # set/unset registers
1118 CHECK_REQS: foreach (@regs) {
1119 if (!$is_in && /(!)?in_r(\d+)/) {
1120 my $bit_pos = 1 << ($2 - 1);
1121 if ($different_pos & $bit_pos) {
1123 print STDERR "duplicate !in constraint\n";
1125 print STDERR "conflicting !in and in constraints\n";
1127 return (undef, undef, undef, undef);
1130 if ($same_pos & $bit_pos) {
1132 print STDERR "conflicting !in and in constraints\n";
1134 print STDERR "duplicate in constraint\n";
1136 return (undef, undef, undef, undef);
1140 $different_pos |= $bit_pos;
1142 $same_pos |= $bit_pos;
1145 $class = $idx_class[$2 - 1];
1150 if (substr($_, 0, 1) eq "!") {
1151 if (defined($neg) && $neg == 0) {
1152 # we have seen a positiv constraint as first one but this one is negative
1153 # this doesn't make sense
1154 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1155 return (undef, undef, undef, undef);
1158 if (!defined($neg)) {
1162 $_ = substr($_, 1); # skip '!'
1165 if (defined($neg) && $neg == 1) {
1166 # we have seen a negative constraint as first one but this one is positive
1167 # this doesn't make sense
1168 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1169 return (undef, undef, undef, undef);
1176 # check if register belongs to one of the given classes
1177 $temp = get_reg_class($_);
1178 if (!defined($temp)) {
1179 print STDERR "Unknown register '$_'!\n";
1180 return (undef, undef, undef, undef);
1184 if (!defined($class)) {
1186 } elsif ($class ne $temp) {
1187 # all registers must belong to the same class
1188 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1189 return (undef, undef, undef, undef);
1192 # calculate position inside the initializer bitfield (only 32 bits per
1194 my $regidx = get_reg_index($_);
1195 my $arrayp = $regidx / 32;
1196 push(@{$limit_array[$arrayp]}, $_);
1197 $limit_reqs .= "$_ ";
1200 # don't allow ignore regs in negative constraints
1202 my @cur_class = @{ $reg_classes{"$class"} };
1203 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1204 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1205 my $reg = $cur_class[$idx]{"name"};
1206 my $regix = get_reg_index($reg);
1207 my $arrayp = $regix / 32;
1208 push(@{$limit_array[$arrayp]}, $reg);
1209 $limit_reqs .= "$reg ";
1214 if ($has_limit == 1) {
1215 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1217 if(defined($limit_bitsets{$limit_name})) {
1218 $limit_name = $limit_bitsets{$limit_name};
1219 return ($class, $limit_name, $same_pos, $different_pos);
1222 $limit_bitsets{$limit_name} = $limit_name;
1224 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1226 my $limitbitsetlen = $regclass2len{$class};
1227 my $limitarraylen = ($limitbitsetlen+31) / 32;
1228 for(my $i = 0; $i < $limitarraylen; $i++) {
1230 my $limitarraypart = $limit_array[$i];
1234 push(@obst_limit_func, ", ");
1238 $temp = "0xFFFFFFFF";
1240 foreach my $reg (@{$limitarraypart}) {
1243 } elsif(defined($temp)) {
1246 my $firstreg = uc($reg_classes{$class}[0]->{"name"});
1247 my $classuc = uc($class);
1248 my $reguc = uc($reg);
1249 $temp .= "BIT(REG_${classuc}_${reguc})";
1251 if(defined($temp)) {
1252 push(@obst_limit_func, "${temp}");
1254 push(@obst_limit_func, "0");
1257 push(@obst_limit_func, " };\n");
1260 return ($class, $limit_name, $same_pos, $different_pos);
1264 # Generate register requirements structure
1266 sub generate_requirements {
1267 my ($reqs, $flags) = split(/:/, shift);
1277 if (defined($flags)) {
1278 foreach my $f (split(/|/, $flags)) {
1280 push(@req_type_mask, "arch_register_req_type_ignore");
1281 } elsif ($f eq "S") {
1282 push(@req_type_mask, "arch_register_req_type_produces_sp");
1283 } elsif ($f eq "a") {
1284 push(@req_type_mask, "arch_register_req_type_aligned");
1285 } elsif ($f eq "2" or $f eq "4" or $f eq "8") {
1291 if ($reqs eq "none") {
1295 arch_register_req_type_none,
1296 NULL, /* regclass */
1297 NULL, /* limit bitset */
1299 0, /* different pos */
1304 } elsif (is_reg_class($reqs)) {
1305 push(@req_type_mask, "arch_register_req_type_normal");
1306 my $reqtype = join(" | ", @req_type_mask);
1311 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1312 NULL, /* limit bitset */
1314 0, /* different pos */
1321 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1322 = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1324 if (!defined($regclass)) {
1325 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1328 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1329 push(@req_type_mask, "arch_register_req_type_limited");
1331 if ($same_pos != 0) {
1332 push(@req_type_mask, "arch_register_req_type_should_be_same");
1334 if ($different_pos != 0) {
1335 push(@req_type_mask, "arch_register_req_type_must_be_different");
1337 my $reqtype = join(" | ", @req_type_mask);
1339 if(!defined($limit_bitset)) {
1340 $limit_bitset = "NULL";
1347 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1349 ${same_pos}, /* same pos */
1350 ${different_pos}, /* different pos */
1357 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1358 if(defined($requirements{$name})) {
1361 $requirements{$name} = $name;
1362 push(@obst_reg_reqs, <<EOF);
1363 static const arch_register_req_t ${name} = ${result}