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, 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 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
163 sub create_constructor {
172 $suffix = "_${name}";
176 if (exists($n->{mode})) {
177 $known_mode = $n->{mode};
182 if(exists($n->{"arity"})) {
183 $arity = $n->{"arity"};
184 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
185 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
186 } elsif (exists($n->{"ins"})) {
187 $arity = scalar(@{ $n->{"ins"} });
189 if($arity eq "variable") {
190 $arity = $ARITY_VARIABLE;
191 } elsif($arity eq "dynamic") {
192 $arity = $ARITY_DYNAMIC;
195 # determine out arity
197 if(exists($n->{"out_arity"})) {
198 $out_arity = $n->{"out_arity"};
199 } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
200 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
201 } elsif (exists($n->{"outs"})) {
202 $out_arity = scalar(@{ $n->{"outs"} });
204 if($out_arity eq "variable") {
205 $out_arity = $ARITY_VARIABLE;
206 } elsif($out_arity eq "dynamic") {
207 $out_arity = $ARITY_DYNAMIC;
211 my $comment = $n->{"comment"};
212 if(!exists($n->{"comment"})) {
213 $comment = "construct ${orig_op} node";
220 $obst_constructor .= $comment;
222 # create constructor head
223 my $complete_args = "";
226 $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
227 if (!exists($n->{"args"})) { # default args
228 if ($arity == $ARITY_VARIABLE) {
229 $complete_args = ", int arity, ir_node *in[]";
230 } elsif ($arity == $ARITY_DYNAMIC) {
233 for (my $i = 0; $i < $arity; $i++) {
234 my $opname = "op${i}";
235 if (exists($n->{"ins"})) {
236 my @ins = @{ $n->{"ins"} };
240 $complete_args .= ", ir_node *${opname}";
243 if ($out_arity == $ARITY_VARIABLE) {
244 $complete_args .= ", int n_res";
247 if (!defined($known_mode)) {
248 $complete_args .= ", ir_mode *mode";
250 } else { # user defined args
251 for my $href (@{ $n->{"args"} }) {
252 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
253 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
257 # we have additional attribute arguements
258 if (exists($n->{"attr"})) {
259 $complete_args .= ", ".$n->{"attr"};
262 $temp .= "$complete_args)";
263 $obst_constructor .= "${temp}\n{\n";
265 $obst_header .= $comment;
266 $obst_header .= "${temp};\n";
268 # emit constructor code
271 ir_op *op = op_${arch}_${op};
273 backend_info_t *info;
276 if($arity == $ARITY_DYNAMIC) {
281 } elsif($arity == $ARITY_VARIABLE) {
296 if($out_arity == $ARITY_DYNAMIC) {
300 } elsif($out_arity == $ARITY_VARIABLE) {
303 int n_res = ${out_arity};
307 if (defined($known_mode)) {
309 ir_mode *mode = ${known_mode};
313 # set up static variables for cpu execution unit assigments
314 if (exists($n->{"units"})) {
315 $temp .= gen_execunit_list_initializer($n->{"units"});
318 static const be_execution_unit_t ***exec_units = NULL;
322 undef my $in_req_var;
323 undef my $out_req_var;
325 my $set_out_reqs = "";
327 # set up static variables for requirements and registers
328 if (exists($n->{"reg_req"})) {
329 my %req = %{ $n->{"reg_req"} };
333 @in = @{ $req{"in"} } if (exists($req{"in"}));
335 @out = @{ $req{"out"} } if exists(($req{"out"}));
337 for(my $idx = 0; $idx < $#in; $idx++) {
339 generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
341 for(my $idx = 0; $idx < $#out; $idx++) {
342 my $req = $out[$idx];
343 generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
347 if($arity >= 0 && scalar(@in) != $arity) {
348 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
351 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
353 for ($idx = 0; $idx <= $#in; $idx++) {
355 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
356 $temp .= "\t\t& ${reqstruct},\n";
361 die "Fatal error: need in requirements for ${op}\n";
363 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
367 if($out_arity >= 0 && scalar(@out) != $out_arity) {
368 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
371 for ($idx = 0; $idx <= $#out; $idx++) {
372 my $req = $out[$idx];
373 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
374 $set_out_reqs .= <<EOF;
375 info->out_infos[${idx}].req = &${reqstruct};
380 die "Fatal error: need out requirements for ${op}\n";
384 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
386 my $attr_type = $on->{attr_type};
387 if(exists($n->{"init_attr"})) {
388 $temp .= "\t${attr_type} *attr;\n";
394 $temp .= "\t/* construct in array */\n";
395 for (my $i = 0; $i < $arity; $i++) {
396 my $opname = "op${i}";
397 if (exists($n->{"ins"})) {
398 my @ins = @{ $n->{"ins"} };
402 $temp .= "\tin[${i}] = ${opname};\n";
408 if (exists($n->{"irn_flags"})) {
409 $temp .= "\t/* flags */\n";
410 foreach my $flag (split(/\|/, $n->{"irn_flags"})) {
412 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
413 } elsif ($flag eq "N") {
414 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
415 } elsif ($flag eq "F") {
416 $temp .= "\tflags |= arch_irn_flags_modify_flags;\n";
417 } elsif ($flag eq "J") {
418 $temp .= "\t flags |= arch_irn_flags_simple_jump;\n";
420 die "Fatal error: unknown flag $flag for ${op}\n";
426 # lookup init function
427 my $attr_init_code = $init_attr{$attr_type};
428 if(!defined($attr_init_code)) {
429 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
432 if(defined($custom_init_attr_func)) {
433 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
435 if(defined($n->{custominit})) {
436 $custominit .= $n->{custominit};
442 res = new_ir_node(dbgi, current_ir_graph, block, op, mode, arity, in);
444 /* init node attributes */
447 info = be_get_info(res);
448 (void) info; /* avoid potential warning */
453 if (exists($n->{"init_attr"})) {
454 $temp .= "\tattr = get_irn_generic_attr(res);\n";
455 $temp .= "\t".$n->{"init_attr"}."\n";
460 res = optimize_node(res);
461 irn_vrfy_irg(res, current_ir_graph);
466 $obst_constructor .= $temp;
468 # close constructor function
469 $obst_constructor .= "}\n\n";
472 push(@obst_enum_op, "typedef enum _${arch}_opcodes {\n");
473 foreach my $op (keys(%nodes)) {
474 my %n = %{ $nodes{"$op"} };
482 if(exists($n{"arity"})) {
483 $arity = $n{"arity"};
484 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
485 $arity = scalar(@{ $n{"reg_req"}{"in"} });
486 } elsif (exists($n{"ins"})) {
487 $arity = scalar(@{ $n{"ins"} });
489 if($arity eq "variable") {
490 $arity = $ARITY_VARIABLE;
491 } elsif($arity eq "dynamic") {
492 $arity = $ARITY_DYNAMIC;
495 # determine out arity
497 if(exists($n{"out_arity"})) {
498 $out_arity = $n{"out_arity"};
499 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
500 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
501 } elsif (exists($n{"outs"})) {
502 $out_arity = scalar(@{ $n{"outs"} });
504 if($out_arity eq "variable") {
505 $out_arity = $ARITY_VARIABLE;
506 } elsif($out_arity eq "dynamic") {
507 $out_arity = $ARITY_DYNAMIC;
514 # define proj numbers and in numbers
515 if (exists($n{"outs"})) {
518 @outs = @{ $n{"outs"} };
519 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
520 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
523 $num_outs = $#outs + 1;
525 $obst_proj .= "\nenum pn_${op} {\n";
527 for (my $idx = 0; $idx <= $#outs; $idx++) {
528 # check, if we have additional flags annotated to out
529 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
530 push(@out_flags, $1);
531 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
533 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
536 $obst_proj .= "};\n";
537 # outs have names, it must be a mode_T node
538 if (!defined($n{mode})) {
542 if (exists($n{"ins"})) {
545 @ins = @{ $n{"ins"} };
546 if($arity >= 0 && scalar(@ins) != $arity) {
547 die "Fatal error: Op ${op} has different number of ins and arity\n";
550 $obst_proj .= "\nenum n_$op {\n";
551 for (my $idx = 0; $idx <= $#ins; $idx++) {
552 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
554 $obst_proj .= "};\n";
558 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
559 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
560 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
562 push(@obst_is_archirn, "is_$op(node)");
564 $obst_header .= <<EOF;
565 extern ir_op *op_${op};
566 ir_op *get_op_${op}(void);
567 int is_${op}(const ir_node *n);
570 my $attr_type= $n{attr_type};
571 if(!defined($attr_type)) {
572 $attr_type = $default_attr_type;
573 $n{attr_type} = $attr_type;
576 # determine hash function
578 if (exists($n{"hash_func"})) {
579 $hash_func = $n{"hash_func"};
582 # determine compare function
584 if (exists($n{"cmp_attr"})) {
585 my $cmpcode = $n{"cmp_attr"};
587 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
588 if($cmpcode =~ m/attr_a/) {
589 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
591 push(@obst_cmp_attr, "\t(void) a;\n");
593 if($cmpcode =~ m/attr_b/) {
594 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
596 push(@obst_cmp_attr, "\t(void) b;\n");
598 push(@obst_cmp_attr, "\t${cmpcode}\n");
599 push(@obst_cmp_attr, "}\n\n");
601 $cmp_attr_func = "cmp_attr_${op}";
603 if(defined($compare_attr{${attr_type}})) {
604 $cmp_attr_func = $compare_attr{${attr_type}};
606 die "Fatal error: No compare function defined for ${attr_type} attributes.";
611 if (exists($n{constructors})) {
612 %constructors = %{ $n{constructors} };
614 # Create 1 default constructor
615 my %constructor = ();
616 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
617 "reg_req", "init_attr", "irn_flags", "mode", "arity",
618 "out_arity", "custominit") {
619 if (defined($n{$a})) {
620 $constructor{$a} = $n{$a};
623 %constructors = ( "" => \%constructor );
626 foreach my $constr (keys(%constructors)) {
627 my %cstr = %{ $constructors{$constr} };
628 # Copy some values from outer node if they don't exists in the constr
629 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
631 if (!defined($cstr{$a}) && defined($n{$a})) {
635 create_constructor($orig_op, $constr, \%cstr, \%n);
638 # if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
639 # # we explicitly skip the constructor if the specification entry says NONE
641 # my $comment = $n{"comment"};
642 # if(!exists($n{"comment"})) {
643 # $comment = "construct ${orig_op} node";
650 # $obst_constructor .= $comment;
652 # # create constructor head
653 # my $complete_args = "";
656 # $temp = "ir_node *new_bd_$op(dbg_info *db, ir_node *block";
657 # if (!exists($n{"args"})) { # default args
658 # if ($arity == $ARITY_VARIABLE) {
659 # $complete_args = ", int arity, ir_node *in[]";
660 # } elsif ($arity == $ARITY_DYNAMIC) {
661 # $complete_args = "";
663 # for (my $i = 0; $i < $arity; $i++) {
664 # my $opname = "op${i}";
665 # if (exists($n{"ins"})) {
666 # my @ins = @{ $n{"ins"} };
667 # $opname = $ins[$i];
670 # $complete_args .= ", ir_node *${opname}";
673 # if ($out_arity == $ARITY_VARIABLE) {
674 # $complete_args .= ", int n_res";
677 # if (!defined($known_mode)) {
678 # $complete_args .= ", ir_mode *mode";
680 # } else { # user defined args
681 # for my $href (@{ $n{"args"} }) {
682 # $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
683 # $complete_args .= ", ".$href->{"type"}.$href->{"name"};
687 # # we have additional attribute arguements
688 # if (exists($n{"attr"})) {
689 # $complete_args .= ", ".$n{"attr"};
692 # $temp .= "$complete_args)";
693 # $obst_constructor .= "${temp}\n{\n";
694 # $obst_header .= $comment;
695 # $obst_header .= "${temp};\n";
697 # # emit constructor code
698 # if (!exists($n{"rd_constructor"})) { # default constructor
701 # ir_op *op = op_${op};
703 # backend_info_t *info;
706 # if($arity == $ARITY_DYNAMIC) {
709 # ir_node **in = NULL;
711 # } elsif($arity == $ARITY_VARIABLE) {
714 # int arity = $arity;
718 # ir_node *in[$arity];
722 # ir_node **in = NULL;
726 # if($out_arity == $ARITY_DYNAMIC) {
730 # } elsif($out_arity == $ARITY_VARIABLE) {
733 # int n_res = ${out_arity};
737 # if (defined($known_mode)) {
739 # ir_mode *mode = ${known_mode};
743 # # set up static variables for cpu execution unit assigments
744 # if (exists($n{"units"})) {
745 # $temp .= gen_execunit_list_initializer($n{"units"});
748 # static const be_execution_unit_t ***exec_units = NULL;
752 # undef my $in_req_var;
753 # undef my $out_req_var;
755 # my $set_out_reqs = "";
757 # # set up static variables for requirements and registers
758 # if (exists($n{"reg_req"})) {
759 # my %req = %{ $n{"reg_req"} };
763 # @in = @{ $req{"in"} } if (exists($req{"in"}));
765 # @out = @{ $req{"out"} } if exists(($req{"out"}));
767 # for(my $idx = 0; $idx < $#in; $idx++) {
768 # my $req = $in[$idx];
769 # generate_requirements($req, \%n, $op, $idx, 1);
771 # for(my $idx = 0; $idx < $#out; $idx++) {
772 # my $req = $out[$idx];
773 # generate_requirements($req, \%n, $op, $idx, 0);
777 # if($arity >= 0 && scalar(@in) != $arity) {
778 # die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
781 # $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
783 # for ($idx = 0; $idx <= $#in; $idx++) {
784 # my $req = $in[$idx];
785 # my $reqstruct = generate_requirements($req, \%n, $op, $idx, 1);
786 # $temp .= "\t\t& ${reqstruct},\n";
791 # die "Fatal error: need in requirements for ${op}\n";
793 # $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
797 # if($out_arity >= 0 && scalar(@out) != $out_arity) {
798 # die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
801 # for ($idx = 0; $idx <= $#out; $idx++) {
802 # my $req = $out[$idx];
803 # my $reqstruct = generate_requirements($req, \%n, $op, $idx, 0);
804 # $set_out_reqs .= <<EOF;
805 # info->out_infos[${idx}].req = &${reqstruct};
809 # if($out_arity > 0) {
810 # die "Fatal error: need out requirements for ${op}\n";
814 # $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
816 # if(exists($n{"init_attr"})) {
817 # $temp .= "\t${attr_type} *attr;\n";
823 # $temp .= "\t/* construct in array */\n";
824 # for (my $i = 0; $i < $arity; $i++) {
825 # my $opname = "op${i}";
826 # if (exists($n{"ins"})) {
827 # my @ins = @{ $n{"ins"} };
828 # $opname = $ins[$i];
831 # $temp .= "\tin[${i}] = ${opname};\n";
837 # if (exists($n{"irn_flags"})) {
838 # $temp .= "\t/* flags */\n";
839 # foreach my $flag (split(/\|/, $n{"irn_flags"})) {
840 # if ($flag eq "R") {
841 # $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
842 # } elsif ($flag eq "N") {
843 # $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
844 # } elsif ($flag eq "J") {
845 # $temp .= "\t flags |= arch_irn_flags_simple_jump;\n";
847 # die "Fatal error: unknown flag $flag for ${op}\n";
853 # # lookup init function
854 # my $attr_init_code = $init_attr{$attr_type};
855 # if(!defined($attr_init_code)) {
856 # die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
858 # my $custominit = "";
859 # if(defined($custom_init_attr_func)) {
860 # $custominit .= &$custom_init_attr_func(\%n, $op);
865 # assert(op != NULL);
866 # res = new_ir_node(db, current_ir_graph, block, op, mode, arity, in);
868 # /* init node attributes */
871 # info = be_get_info(res);
876 # if (exists($n{"init_attr"})) {
877 # $temp .= "\tattr = get_irn_generic_attr(res);\n";
878 # $temp .= "\t".$n{"init_attr"}."\n";
882 # /* optimize node */
883 # res = optimize_node(res);
884 # irn_vrfy_irg(res, current_ir_graph);
889 # $obst_constructor .= $temp;
891 # else { # user defined constructor
892 # $obst_constructor .= $n{"rd_constructor"};
895 # # close constructor function
896 # $obst_constructor .= "}\n\n";
897 # } # constructor creation
899 # set default values for state and flags if not given
900 $n{"state"} = "floats" if (! exists($n{"state"}));
901 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
904 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
905 push(@obst_new_irop, "\tops.be_ops = be_ops;\n");
906 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
908 if (defined($cmp_attr_func)) {
909 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
911 my $copy_attr_func = $copy_attr{$attr_type};
912 if (!defined($copy_attr_func)) {
913 $copy_attr_func = $default_copy_attr;
915 if (defined($copy_attr_func)) {
916 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
918 if (defined($hash_func)) {
919 push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
923 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
924 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
925 push(@obst_new_irop, $temp);
926 push(@obst_new_irop, "\tset_op_tag(op_$op, $arch\_op_tag);\n");
927 if(defined($default_op_attr_type)) {
928 push(@obst_new_irop, "\tattr = &attrs[iro_$op];\n");
929 if(defined($n{op_attr_init})) {
930 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
932 push(@obst_new_irop, "\tset_op_attr(op_$op, attr);\n");
935 push(@obst_enum_op, "\tiro_$op,\n");
937 $obst_header .= "\n";
939 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
940 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
941 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
942 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
946 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
948 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n\n";
949 print OUT @obst_cmp_attr;
951 print OUT @obst_opvar;
953 print OUT @obst_get_opvar;
958 static int $arch\_opcode_start = -1;
959 static int $arch\_opcode_end = -1;
963 # build the FOURCC arguments from $arch
965 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
967 if (length($arch) >= 1) {
968 $a = uc(substr($arch, 0, 1));
971 if (length($arch) >= 2) {
972 $b = uc(substr($arch, 1, 1));
975 if (length($arch) >= 3) {
976 $c = uc(substr($arch, 2, 1));
979 if (length($arch) >= 4) {
980 $d = uc(substr($arch, 3, 1));
983 print OUT<<ENDOFISIRN;
985 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
986 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
988 /** Return the opcode number of the first $arch opcode. */
989 int get_$arch\_opcode_first(void) {
990 return $arch\_opcode_start;
993 /** Return the opcode number of the last $arch opcode + 1. */
994 int get_$arch\_opcode_last(void) {
995 return $arch\_opcode_end;
998 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
999 int is_$arch\_op(const ir_op *op) {
1000 return get_op_tag(op) == $arch\_op_tag;
1003 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
1004 int is_$arch\_irn(const ir_node *node) {
1005 return is_$arch\_op(get_irn_op(node));
1008 int get_$arch\_irn_opcode(const ir_node *node) {
1009 if (is_$arch\_irn(node))
1010 return get_irn_opcode(node) - $arch\_opcode_start;
1020 #define BIT(x) (1 << (x % 32))
1024 print OUT @obst_limit_func;
1027 print OUT @obst_reg_reqs;
1030 print OUT<<ENDOFMAIN;
1034 * Creates the $arch specific Firm machine operations
1035 * needed for the assembler irgs.
1037 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops) {
1038 #define N irop_flag_none
1039 #define L irop_flag_labeled
1040 #define C irop_flag_commutative
1041 #define X irop_flag_cfopcode
1042 #define I irop_flag_ip_cfopcode
1043 #define F irop_flag_fragile
1044 #define Y irop_flag_forking
1045 #define H irop_flag_highlevel
1046 #define c irop_flag_constlike
1047 #define K irop_flag_keep
1048 #define M irop_flag_machine
1049 #define O irop_flag_machine_op
1050 #define NB irop_flag_dump_noblock
1051 #define NI irop_flag_dump_noinput
1052 #define n irop_flag_cse_neutral
1053 #define R (irop_flag_user << 0)
1057 static int run_once = 0;
1060 if (defined($default_op_attr_type)) {
1061 print OUT "\t$default_op_attr_type *attr, *attrs;\n";
1064 print OUT<<ENDOFMAIN;
1070 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
1072 $arch\_opcode_start = cur_opcode;
1075 if (defined($default_op_attr_type)) {
1076 print OUT "\tattrs = xmalloc(sizeof(attr[0]) * iro_$arch\_last);\n";
1077 print OUT "\tmemset(attrs, 0, sizeof(attr[0]) * iro_$arch\_last);\n";
1080 print OUT @obst_new_irop;
1082 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
1083 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
1084 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
1090 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
1092 my $creation_time = localtime(time());
1093 my $tmp = uc($arch);
1098 * \@brief Function prototypes for the new opcode functions.
1099 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
1100 * Edit $specfile instead.
1101 * created by: $0 $specfile $target_dir
1102 * \@date $creation_time
1104 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
1105 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
1109 print OUT @obst_enum_op;
1111 int is_${arch}_irn(const ir_node *node);
1112 int is_${arch}_op(const ir_op *op);
1114 int get_${arch}_opcode_first(void);
1115 int get_${arch}_opcode_last(void);
1116 int get_${arch}_irn_opcode(const ir_node *node);
1126 # Translates numeric arity into string constant.
1128 sub translate_arity {
1131 if ($arity =~ /^\d+$/) {
1133 return "oparity_zero";
1135 elsif ($arity == 1) {
1136 return "oparity_unary";
1138 elsif ($arity == 2) {
1139 return "oparity_binary";
1141 elsif ($arity == 3) {
1142 return "oparity_trinary";
1145 return "oparity_any";
1147 } elsif ($arity == $ARITY_VARIABLE) {
1148 return "oparity_variable";
1149 } elsif ($arity == $ARITY_DYNAMIC) {
1150 return "oparity_dynamic";
1152 die "Fatal error: Unknown arity $arity";
1157 # Return the list of pointers for the given execution units.
1159 sub gen_execunit_list_initializer {
1161 my $uc_arch = uc($arch);
1166 foreach my $unit (@{ $units }) {
1167 if ($unit eq "DUMMY") {
1168 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
1170 elsif (exists($cpu{"$unit"})) {
1171 # operation can be executed on all units of this type
1173 my $tp_name = "$arch\_execution_units_$unit";
1175 foreach (@{ $cpu{"$unit"} }) {
1176 next if ($idx++ == 0); # skip first element (it's not a unit)
1177 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
1178 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
1182 # operation can be executed only a certain unit
1183 # -> find corresponding unit type
1185 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
1186 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
1187 if ($unit eq $cur_unit) {
1188 my $tp_name = "$arch\_execution_units_$cur_type";
1189 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
1190 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
1198 print STDERR "Invalid execution unit $unit specified!\n";
1203 # prepare the 2-dim array init
1204 foreach my $key (keys(%init)) {
1205 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
1207 foreach (@{ $init{"$key"} }) {
1210 $ret .= "\t\tNULL\n";
1212 $ret2 .= "\t\tallowed_units_$key,\n";
1214 $ret2 .= "\t\tNULL\n";
1216 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
1224 sub mangle_requirements {
1229 my @alternatives = split(/ /, $reqs);
1230 for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
1231 $alternatives[$idx] =~ s/!/not_/g;
1234 @alternatives = sort @alternatives;
1236 my $name = $class."_".join('_', @alternatives);
1237 if (defined($flags)) {
1246 # Determines whether $name is a specified register class or not.
1247 # @return 1 if name is register class, 0 otherwise
1251 return 1 if exists($reg_classes{"$name"});
1256 # Returns the register class for a given register.
1257 # @return class or undef
1261 $reg = substr($reg, 1) if ($reg =~ /!.*/);
1262 return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
1267 # Returns the index of a given register within it's register class.
1268 # @return index or undef
1272 return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
1277 # Remember the register class for each index in the given requirements.
1278 # We need this information for requirements like "in_sX" or "out_dX"
1279 # @return array of classes corresponding to the requirement for each index
1281 sub build_inout_idx_class {
1287 my $inout = ($is_in ? "in" : "out");
1289 if (exists($n->{"reg_req"}{"$inout"})) {
1290 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
1292 for (my $idx = 0; $idx <= $#reqs; $idx++) {
1294 my ($req,) = split(/:/, $reqs[$idx]);
1296 if ($req eq "none") {
1298 } elsif (is_reg_class($req)) {
1301 my @regs = split(/ /, $req);
1302 GET_CLASS: foreach my $reg (@regs) {
1303 if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
1304 $class = "UNKNOWN_CLASS";
1306 $class = get_reg_class($reg);
1307 if (!defined $class) {
1308 die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
1316 push(@idx_class, $class);
1324 # Generates the function for a given $op and a given IN-index
1325 # which returns a subset of possible register from a register class
1326 # @return classname from which the subset is derived or undef and
1327 # pos which corresponds to in/out reference position or undef
1329 sub build_subset_class_func {
1335 my $different_pos = 0;
1341 my $limit_reqs; #used for name mangling
1343 # build function header
1348 my @regs = split(/ /, shift);
1351 my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1353 # set/unset registers
1354 CHECK_REQS: foreach (@regs) {
1355 if (!$is_in && /(!)?in_r(\d+)/) {
1356 my $bit_pos = 1 << ($2 - 1);
1357 if ($different_pos & $bit_pos) {
1359 print STDERR "duplicate !in constraint\n";
1361 print STDERR "conflicting !in and in constraints\n";
1363 return (undef, undef, undef, undef);
1366 if ($same_pos & $bit_pos) {
1368 print STDERR "conflicting !in and in constraints\n";
1370 print STDERR "duplicate in constraint\n";
1372 return (undef, undef, undef, undef);
1376 $different_pos |= $bit_pos;
1378 $same_pos |= $bit_pos;
1381 $class = $idx_class[$2 - 1];
1386 if (substr($_, 0, 1) eq "!") {
1387 if (defined($neg) && $neg == 0) {
1388 # we have seen a positiv constraint as first one but this one is negative
1389 # this doesn't make sense
1390 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1391 return (undef, undef, undef, undef);
1394 if (!defined($neg)) {
1398 $_ = substr($_, 1); # skip '!'
1401 if (defined($neg) && $neg == 1) {
1402 # we have seen a negative constraint as first one but this one is positive
1403 # this doesn't make sense
1404 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1405 return (undef, undef, undef, undef);
1412 # check if register belongs to one of the given classes
1413 $temp = get_reg_class($_);
1414 if (!defined($temp)) {
1415 print STDERR "Unknown register '$_'!\n";
1416 return (undef, undef, undef, undef);
1420 if (!defined($class)) {
1422 } elsif ($class ne $temp) {
1423 # all registers must belong to the same class
1424 print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1425 return (undef, undef, undef, undef);
1428 # calculate position inside the initializer bitfield (only 32 bits per
1430 my $regidx = get_reg_index($_);
1431 my $arrayp = $regidx / 32;
1432 push(@{$limit_array[$arrayp]}, $_);
1433 $limit_reqs .= "$_ ";
1436 # don't allow ignore regs in negative constraints
1438 my @cur_class = @{ $reg_classes{"$class"} };
1439 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1440 if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1441 my $reg = $cur_class[$idx]{"name"};
1442 my $regix = get_reg_index($reg);
1443 my $arrayp = $regix / 32;
1444 push(@{$limit_array[$arrayp]}, $reg);
1445 $limit_reqs .= "$reg ";
1450 if ($has_limit == 1) {
1451 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1453 if(defined($limit_bitsets{$limit_name})) {
1454 $limit_name = $limit_bitsets{$limit_name};
1455 return ($class, $limit_name, $same_pos, $different_pos);
1458 $limit_bitsets{$limit_name} = $limit_name;
1460 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1462 my $limitbitsetlen = $regclass2len{$class};
1463 my $limitarraylen = ($limitbitsetlen+31) / 32;
1464 for(my $i = 0; $i < $limitarraylen; $i++) {
1466 my $limitarraypart = $limit_array[$i];
1470 push(@obst_limit_func, ", ");
1474 $temp = "0xFFFFFFFF";
1476 foreach my $reg (@{$limitarraypart}) {
1479 } elsif(defined($temp)) {
1482 $temp .= "BIT(REG_".uc(${reg}).")";
1484 if(defined($temp)) {
1485 push(@obst_limit_func, "${temp}");
1487 push(@obst_limit_func, "0");
1490 push(@obst_limit_func, " };\n");
1493 return ($class, $limit_name, $same_pos, $different_pos);
1497 # Generate register requirements structure
1499 sub generate_requirements {
1500 my ($reqs, $flags) = split(/:/, shift);
1509 if (defined($flags)) {
1510 foreach my $f (split(/|/, $flags)) {
1512 push(@req_type_mask, "arch_register_req_type_ignore");
1513 } elsif ($f eq "S") {
1514 push(@req_type_mask, "arch_register_req_type_produces_sp");
1519 if ($reqs eq "none") {
1523 arch_register_req_type_none,
1524 NULL, /* regclass */
1525 NULL, /* limit bitset */
1527 0 /* different pos */
1531 } elsif (is_reg_class($reqs)) {
1532 push(@req_type_mask, "arch_register_req_type_normal");
1533 my $reqtype = join(" | ", @req_type_mask);
1538 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1539 NULL, /* limit bitset */
1541 0 /* different pos */
1547 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1548 = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1550 if (!defined($regclass)) {
1551 die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1554 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1555 push(@req_type_mask, "arch_register_req_type_limited");
1557 if ($same_pos != 0) {
1558 push(@req_type_mask, "arch_register_req_type_should_be_same");
1560 if ($different_pos != 0) {
1561 push(@req_type_mask, "arch_register_req_type_must_be_different");
1563 my $reqtype = join(" | ", @req_type_mask);
1565 if(!defined($limit_bitset)) {
1566 $limit_bitset = "NULL";
1573 & ${arch}_reg_classes[CLASS_${arch}_${class}],
1575 ${same_pos}, /* same pos */
1576 ${different_pos} /* different pos */
1582 my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1583 if(defined($requirements{$name})) {
1586 $requirements{$name} = $name;
1587 push(@obst_reg_reqs, <<EOF);
1588 static const arch_register_req_t ${name} = ${result}