4 # Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
6 # This file is part of libFirm.
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 # This script generates the C code which creates the irop's and
23 # their coresponding node constructors for all operations in a given spec
24 # so they can be used as normal firm nodes.
25 # Creation: 2005/10/19
31 my $specfile = $ARGV[0];
32 my $target_dir = $ARGV[1];
38 our $additional_opcodes;
42 our $default_attr_type;
43 our $default_cmp_attr;
52 unless ($return = do $specfile) {
53 die "Fatal error: couldn't parse $specfile: $@" if $@;
54 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
55 die "Fatal error: couldn't run $specfile" unless $return;
59 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
60 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
62 if(!defined($default_attr_type)) {
63 $default_attr_type = "${arch}_attr_t";
65 if(!defined(%init_attr)) {
67 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
70 if(!defined($default_cmp_attr)) {
71 $default_cmp_attr = "${arch}_compare_attr";
73 if(!defined(%compare_attr)) {
75 "${default_attr_type}" => "${default_cmp_attr}",
79 # Operands are really just nodes with some special constraints, we check
80 # these and create new entries in the nodes hashmap
81 foreach my $op (keys(%operands)) {
82 my %operand = %{ $operands{"$op"} };
86 if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
87 if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
88 if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
89 if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
90 if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
93 foreach my $flag (keys(%operand)) {
94 $op_node{$flag} = $operand{$flag};
96 $op_node{op_flags} = "O";
97 $op_node{cmp_attr} = 'return 1;';
98 $op_node{mode} = 'mode_any';
100 $nodes{$op} = \%op_node;
103 #print Dumper(%nodes);
104 #print Dumper(%operands);
106 # create c code file from specs
108 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
109 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
110 my @obst_constructor; # stack for node constructor functions
111 my @obst_new_irop; # stack for the new_ir_op calls
112 my @obst_enum_op; # stack for creating the <arch>_opcode enum
113 my @obst_header; # stack for function prototypes
114 my @obst_is_archirn; # stack for the is_$arch_irn() function
115 my @obst_cmp_attr; # stack for the compare attribute functions
116 my @obst_proj; # stack for the pn_ numbers
121 my $n_opcodes = 0; # number of opcodes
122 my $ARITY_VARIABLE = -1;
123 my $ARITY_DYNAMIC = -2;
125 # for registering additional opcodes
126 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
128 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
130 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
131 foreach my $op (keys(%nodes)) {
132 my %n = %{ $nodes{"$op"} };
140 if(exists($n{"arity"})) {
141 $arity = $n{"arity"};
142 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
143 $arity = scalar(@{ $n{"reg_req"}{"in"} });
144 } elsif (exists($n{"ins"})) {
145 $arity = scalar(@{ $n{"ins"} });
147 if($arity eq "variable") {
148 $arity = $ARITY_VARIABLE;
149 } elsif($arity eq "dynamic") {
150 $arity = $ARITY_DYNAMIC;
153 # determine out arity
155 if(exists($n{"out_arity"})) {
156 $out_arity = $n{"out_arity"};
157 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
158 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
159 } elsif (exists($n{"outs"})) {
160 $out_arity = scalar(@{ $n{"outs"} });
162 if($out_arity eq "variable") {
163 $out_arity = $ARITY_VARIABLE;
164 } elsif($out_arity eq "dynamic") {
165 $out_arity = $ARITY_DYNAMIC;
172 # define proj numbers and in numbers
173 if (exists($n{"outs"})) {
176 @outs = @{ $n{"outs"} };
177 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
178 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
181 $num_outs = $#outs + 1;
183 push(@obst_proj, "\nenum pn_$op {\n");
185 for (my $idx = 0; $idx <= $#outs; $idx++) {
186 # check, if we have additional flags annotated to out
187 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
188 push(@out_flags, $1);
189 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
191 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
194 push(@obst_proj, "};\n");
195 # outs have names, it must be a mode_T node
196 $known_mode = "mode_T";
198 if (exists($n{"ins"})) {
201 @ins = @{ $n{"ins"} };
202 if($arity >= 0 && scalar(@ins) != $arity) {
203 die "Fatal error: Op ${op} has different number of ins and arity\n";
206 push(@obst_proj, "\nenum n_$op {\n");
208 for (my $idx = 0; $idx <= $#ins; $idx++) {
209 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
212 push(@obst_proj, "};\n");
216 if (exists($n{"mode"})) {
217 $known_mode = $n{"mode"};
220 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
221 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
222 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
224 push(@obst_is_archirn, "is_$op(node)");
226 push(@obst_header, "extern ir_op *op_$op;\n");
227 push(@obst_header, "ir_op *get_op_$op(void);\n");
228 push(@obst_header, "int is_$op(const ir_node *n);\n");
230 my $attr_type= $n{"attr_type"};
231 if(!defined($attr_type)) {
232 $attr_type = $default_attr_type;
235 # determine compare function
237 if (exists($n{"cmp_attr"})) {
238 my $cmpcode = $n{"cmp_attr"};
240 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
241 if($cmpcode =~ m/attr_a/) {
242 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
244 push(@obst_cmp_attr, "\t(void) a;\n");
246 if($cmpcode =~ m/attr_b/) {
247 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
249 push(@obst_cmp_attr, "\t(void) b;\n");
251 push(@obst_cmp_attr, "\t${cmpcode}\n");
252 push(@obst_cmp_attr, "}\n\n");
254 $cmp_attr_func = "cmp_attr_${op}";
256 if(defined($compare_attr{${attr_type}})) {
257 $cmp_attr_func = $compare_attr{${attr_type}};
259 die "Fatal error: No compare function defined for ${attr_type} attributes.";
263 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
264 # we explicitly skip the constructor if the specification entry says NONE
266 my $comment = $n{"comment"};
267 if(!exists($n{"comment"})) {
268 $comment = "construct ${orig_op} node";
275 push(@obst_constructor, $comment);
277 # create constructor head
278 my $complete_args = "";
281 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
282 if (!exists($n{"args"})) { # default args
283 if ($arity == $ARITY_VARIABLE) {
284 $complete_args = ", int arity, ir_node *in[]";
285 } elsif ($arity == $ARITY_DYNAMIC) {
288 for (my $i = 0; $i < $arity; $i++) {
289 my $opname = "op${i}";
290 if (exists($n{"ins"})) {
291 my @ins = @{ $n{"ins"} };
295 $complete_args .= ", ir_node *${opname}";
298 if ($out_arity == $ARITY_VARIABLE) {
299 $complete_args .= ", int n_res";
302 if (!defined($known_mode)) {
303 $complete_args .= ", ir_mode *mode";
305 } else { # user defined args
306 for my $href (@{ $n{"args"} }) {
307 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
308 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
312 # we have additional attribute arguements
313 if (exists($n{"attr"})) {
314 $complete_args .= ", ".$n{"attr"};
317 $temp .= "$complete_args)";
318 push(@obst_constructor, $temp."\n{\n");
319 push(@obst_header, $comment);
320 push(@obst_header, $temp.";\n");
322 # emit constructor code
323 if (!exists($n{"rd_constructor"})) { # default constructor
324 $temp = "\tir_node *res;\n";
325 $temp .= "\tir_op *op = op_${op};\n";
326 $temp .= "\tint flags = 0;\n";
328 if($arity == $ARITY_DYNAMIC) {
329 $temp .= "\tint arity = -1;\n";
330 $temp .= "\tir_node **in = NULL;\n";
331 } elsif($arity == $ARITY_VARIABLE) {
333 $temp .= "\tint arity = $arity;\n";
335 $temp .= "\tir_node *in[$arity];\n";
337 $temp .= "\tir_node **in = NULL;\n";
340 if($out_arity == $ARITY_DYNAMIC) {
341 $temp .= "\tint n_res = -1;\n";
342 } elsif($out_arity == $ARITY_VARIABLE) {
344 $temp .= "\tint n_res = ${out_arity};\n";
347 my $latency = $n{"latency"};
348 if (!defined($latency)) {
351 $temp .= "\tunsigned latency = ${latency};\n";
353 if (defined($known_mode)) {
354 $temp .= "\tir_mode *mode = ${known_mode};\n";
357 # set up static variables for cpu execution unit assigments
358 if (exists($n{"units"})) {
359 $temp .= gen_execunit_list_initializer($n{"units"});
361 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
364 undef my $in_req_var;
365 undef my $out_req_var;
367 # set up static variables for requirements and registers
368 if (exists($n{"reg_req"})) {
369 my %req = %{ $n{"reg_req"} };
373 @in = @{ $req{"in"} } if (exists($req{"in"}));
375 @out = @{ $req{"out"} } if exists(($req{"out"}));
378 if($arity >= 0 && scalar(@in) != $arity) {
379 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
382 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
384 for ($idx = 0; $idx <= $#in; $idx++) {
385 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
390 die "Fatal error: need in requirements for ${op}\n";
392 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
396 if($out_arity >= 0 && scalar(@out) != $out_arity) {
397 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
400 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
402 for ($idx = 0; $idx <= $#out; $idx++) {
403 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
408 die "Fatal error: need out requirements for ${op}\n";
410 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
413 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
414 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
416 if(exists($n{"init_attr"})) {
417 $temp .= "\t${attr_type} *attr;\n";
423 $temp .= "\t/* construct in array */\n";
424 for (my $i = 0; $i < $arity; $i++) {
425 my $opname = "op${i}";
426 if (exists($n{"ins"})) {
427 my @ins = @{ $n{"ins"} };
431 $temp .= "\tin[${i}] = ${opname};\n";
437 if (exists($n{"irn_flags"})) {
438 $temp .= "\t/* flags */\n";
439 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
441 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
442 } elsif ($flag eq "N") {
443 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
444 } elsif ($flag eq "I") {
445 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
446 } elsif ($flag eq "S") {
447 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
453 $temp .= "\t/* create node */\n";
454 $temp .= "\tassert(op != NULL);\n";
455 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
458 $temp .= "\t/* init node attributes */\n";
459 # lookup init function
460 my $attr_init_code = $init_attr{$attr_type};
461 if(!defined($attr_init_code)) {
462 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
464 $temp .= "${attr_init_code}\n";
468 if ($#out_flags >= 0) {
469 $temp .= "\t/* set flags for outs */\n";
470 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
474 foreach my $flag (split(/\|/, $out_flags[$idx])) {
476 $flags .= $prefix."arch_irn_flags_ignore";
479 elsif ($flag eq "S") {
480 $flags .= $prefix."arch_irn_flags_modify_sp";
485 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
491 if (exists($n{"init_attr"})) {
492 $temp .= "\tattr = get_irn_generic_attr(res);\n";
493 $temp .= "\t".$n{"init_attr"}."\n";
496 $temp .= "\t/* optimize node */\n";
497 $temp .= "\tres = optimize_node(res);\n";
498 $temp .= "\tirn_vrfy_irg(res, irg);\n";
501 $temp .= "\treturn res;\n";
503 push(@obst_constructor, $temp);
505 else { # user defined constructor
506 push(@obst_constructor, $n{"rd_constructor"});
509 # close constructor function
510 push(@obst_constructor, "}\n\n");
511 } # constructor creation
513 # set default values for state and flags if not given
514 $n{"state"} = "floats" if (! exists($n{"state"}));
515 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
518 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
519 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
521 if (defined($cmp_attr_func)) {
522 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
526 my $n_res = $out_arity;
528 $n_res = "20"; # hacky....
530 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
531 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
532 push(@obst_new_irop, $temp);
533 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
534 push(@obst_enum_op, "\tiro_$op,\n");
536 push(@obst_header, "\n");
538 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
539 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
540 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
541 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
545 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
547 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
548 print OUT @obst_cmp_attr;
550 print OUT @obst_opvar;
552 print OUT @obst_get_opvar;
557 static int $arch\_opcode_start = -1;
558 static int $arch\_opcode_end = -1;
562 # build the FOURCC arguments from $arch
564 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
566 if (length($arch) >= 1) {
567 $a = uc(substr($arch, 0, 1));
570 if (length($arch) >= 2) {
571 $b = uc(substr($arch, 1, 1));
574 if (length($arch) >= 3) {
575 $c = uc(substr($arch, 2, 1));
578 if (length($arch) >= 4) {
579 $d = uc(substr($arch, 3, 1));
582 print OUT<<ENDOFISIRN;
584 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
585 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
587 /** Return the opcode number of the first $arch opcode. */
588 int get_$arch\_opcode_first(void) {
589 return $arch\_opcode_start;
592 /** Return the opcode number of the last $arch opcode + 1. */
593 int get_$arch\_opcode_last(void) {
594 return $arch\_opcode_end;
597 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
598 int is_$arch\_op(const ir_op *op) {
599 return get_op_tag(op) == &$arch\_op_tag;
602 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
603 int is_$arch\_irn(const ir_node *node) {
604 return is_$arch\_op(get_irn_op(node));
607 int get_$arch\_irn_opcode(const ir_node *node) {
608 if (is_$arch\_irn(node))
609 return get_irn_opcode(node) - $arch\_opcode_start;
615 print OUT @obst_constructor;
617 print OUT<<ENDOFMAIN;
619 * Creates the $arch specific Firm machine operations
620 * needed for the assembler irgs.
622 void $arch\_create_opcodes(void) {
623 #define N irop_flag_none
624 #define L irop_flag_labeled
625 #define C irop_flag_commutative
626 #define X irop_flag_cfopcode
627 #define I irop_flag_ip_cfopcode
628 #define F irop_flag_fragile
629 #define Y irop_flag_forking
630 #define H irop_flag_highlevel
631 #define c irop_flag_constlike
632 #define K irop_flag_keep
633 #define M irop_flag_machine
634 #define O irop_flag_machine_op
635 #define R (irop_flag_user << 0)
639 static int run_once = 0;
645 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
647 $arch\_opcode_start = cur_opcode;
650 print OUT @obst_new_irop;
652 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
653 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
654 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
660 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
662 my $creation_time = localtime(time());
668 * \@brief Function prototypes for the new opcode functions.
669 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
670 * Edit $specfile instead.
671 * created by: $0 $specfile $target_dir
672 * \@date $creation_time
674 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
675 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
679 print OUT @obst_enum_op;
680 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
681 print OUT "int get_$arch\_opcode_first(void);\n";
682 print OUT "int get_$arch\_opcode_last(void);\n";
683 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
684 print OUT @obst_header;
685 print OUT @obst_proj;
694 # Translates numeric arity into string constant.
696 sub translate_arity {
699 if ($arity =~ /^\d+$/) {
701 return "oparity_zero";
703 elsif ($arity == 1) {
704 return "oparity_unary";
706 elsif ($arity == 2) {
707 return "oparity_binary";
709 elsif ($arity == 3) {
710 return "oparity_trinary";
713 return "oparity_any";
715 } elsif ($arity == $ARITY_VARIABLE) {
716 return "oparity_variable";
717 } elsif ($arity == $ARITY_DYNAMIC) {
718 return "oparity_dynamic";
720 die "Fatal error: Unknown arity $arity";
725 # Return the list of pointers for the given execution units.
727 sub gen_execunit_list_initializer {
729 my $uc_arch = uc($arch);
734 foreach my $unit (@{ $units }) {
735 if ($unit eq "DUMMY") {
736 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
738 elsif (exists($cpu{"$unit"})) {
739 # operation can be executed on all units of this type
741 my $tp_name = "$arch\_execution_units_$unit";
743 foreach (@{ $cpu{"$unit"} }) {
744 next if ($idx++ == 0); # skip first element (it's not a unit)
745 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
746 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
750 # operation can be executed only a certain unit
751 # -> find corresponding unit type
753 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
754 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
755 if ($unit eq $cur_unit) {
756 my $tp_name = "$arch\_execution_units_$cur_type";
757 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
758 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
766 print STDERR "Invalid execution unit $unit specified!\n";
771 # prepare the 2-dim array init
772 foreach my $key (keys(%init)) {
773 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
775 foreach (@{ $init{"$key"} }) {
778 $ret .= "\t\tNULL\n";
780 $ret2 .= "\t\tallowed_units_$key,\n";
782 $ret2 .= "\t\tNULL\n";
784 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";