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;
41 our $default_attr_type;
42 our $default_cmp_attr;
51 unless ($return = do $specfile) {
52 die "Fatal error: couldn't parse $specfile: $@" if $@;
53 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
54 die "Fatal error: couldn't run $specfile" unless $return;
58 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
59 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
61 if(!defined($default_attr_type)) {
62 $default_attr_type = "${arch}_attr_t";
64 if(!defined(%init_attr)) {
66 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
69 if(!defined($default_cmp_attr)) {
70 $default_cmp_attr = "${arch}_compare_attr";
72 if(!defined(%compare_attr)) {
74 "${default_attr_type}" => "${default_cmp_attr}",
78 #print Dumper(%nodes);
79 #print Dumper(%operands);
81 # create c code file from specs
83 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
84 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
85 my @obst_constructor; # stack for node constructor functions
86 my @obst_new_irop; # stack for the new_ir_op calls
87 my @obst_enum_op; # stack for creating the <arch>_opcode enum
88 my @obst_header; # stack for function prototypes
89 my @obst_is_archirn; # stack for the is_$arch_irn() function
90 my @obst_cmp_attr; # stack for the compare attribute functions
91 my @obst_proj; # stack for the pn_ numbers
96 my $n_opcodes = 0; # number of opcodes
97 my $ARITY_VARIABLE = -1;
98 my $ARITY_DYNAMIC = -2;
100 # for registering additional opcodes
101 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
103 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
105 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
106 foreach my $op (keys(%nodes)) {
107 my %n = %{ $nodes{"$op"} };
115 if(exists($n{"arity"})) {
116 $arity = $n{"arity"};
117 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
118 $arity = scalar(@{ $n{"reg_req"}{"in"} });
119 } elsif (exists($n{"ins"})) {
120 $arity = scalar(@{ $n{"ins"} });
122 if($arity eq "variable") {
123 $arity = $ARITY_VARIABLE;
124 } elsif($arity eq "dynamic") {
125 $arity = $ARITY_DYNAMIC;
128 # determine out arity
130 if(exists($n{"out_arity"})) {
131 $out_arity = $n{"out_arity"};
132 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
133 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
134 } elsif (exists($n{"outs"})) {
135 $out_arity = scalar(@{ $n{"outs"} });
137 if($out_arity eq "variable") {
138 $out_arity = $ARITY_VARIABLE;
139 } elsif($out_arity eq "dynamic") {
140 $out_arity = $ARITY_DYNAMIC;
147 # define proj numbers and in numbers
148 if (exists($n{"outs"})) {
151 @outs = @{ $n{"outs"} };
152 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
153 die "Fatal error: Op ${op} has different number of outs and out_arity\n";
156 $num_outs = $#outs + 1;
158 push(@obst_proj, "\nenum pn_$op {\n");
160 for (my $idx = 0; $idx <= $#outs; $idx++) {
161 # check, if we have additional flags annotated to out
162 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
163 push(@out_flags, $1);
164 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
166 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
169 push(@obst_proj, "};\n");
170 # outs have names, it must be a mode_T node
171 $known_mode = "mode_T";
173 if (exists($n{"ins"})) {
176 @ins = @{ $n{"ins"} };
177 if($arity >= 0 && scalar(@ins) != $arity) {
178 die "Fatal error: Op ${op} has different number of ins and arity\n";
181 push(@obst_proj, "\nenum n_$op {\n");
183 for (my $idx = 0; $idx <= $#ins; $idx++) {
184 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
187 push(@obst_proj, "};\n");
191 if (exists($n{"mode"})) {
192 $known_mode = $n{"mode"};
195 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
196 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
197 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
199 push(@obst_is_archirn, "is_$op(node)");
201 push(@obst_header, "extern ir_op *op_$op;\n");
202 push(@obst_header, "ir_op *get_op_$op(void);\n");
203 push(@obst_header, "int is_$op(const ir_node *n);\n");
205 my $attr_type= $n{"attr_type"};
206 if(!defined($attr_type)) {
207 $attr_type = $default_attr_type;
210 # determine compare function
212 if (exists($n{"cmp_attr"})) {
213 my $cmpcode = $n{"cmp_attr"};
215 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
216 if($cmpcode =~ m/attr_a/) {
217 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
219 push(@obst_cmp_attr, "\t(void) a;\n");
221 if($cmpcode =~ m/attr_b/) {
222 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
224 push(@obst_cmp_attr, "\t(void) b;\n");
226 push(@obst_cmp_attr, "\t${cmpcode}\n");
227 push(@obst_cmp_attr, "}\n\n");
229 $cmp_attr_func = "cmp_attr_${op}";
231 if(defined($compare_attr{${attr_type}})) {
232 $cmp_attr_func = $compare_attr{${attr_type}};
234 die "Fatal error: No compare function defined for ${attr_type} attributes.";
238 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
239 # we explicitly skip the constructor if the specification entry says NONE
241 my $comment = $n{"comment"};
242 if(!exists($n{"comment"})) {
243 $comment = "construct ${orig_op} node";
250 push(@obst_constructor, $comment);
252 # create constructor head
253 my $complete_args = "";
256 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
257 if (!exists($n{"args"})) { # default args
258 if ($arity == $ARITY_VARIABLE) {
259 $complete_args = ", int arity, ir_node *in[]";
260 } elsif ($arity == $ARITY_DYNAMIC) {
263 for (my $i = 0; $i < $arity; $i++) {
264 my $opname = "op${i}";
265 if (exists($n{"ins"})) {
266 my @ins = @{ $n{"ins"} };
270 $complete_args .= ", ir_node *${opname}";
273 if ($out_arity == $ARITY_VARIABLE) {
274 $complete_args .= ", int n_res";
277 if (!defined($known_mode)) {
278 $complete_args .= ", ir_mode *mode";
280 } else { # user defined args
281 for my $href (@{ $n{"args"} }) {
282 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
283 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
287 # we have additional attribute arguements
288 if (exists($n{"attr"})) {
289 $complete_args .= ", ".$n{"attr"};
292 $temp .= "$complete_args)";
293 push(@obst_constructor, $temp."\n{\n");
294 push(@obst_header, $comment);
295 push(@obst_header, $temp.";\n");
297 # emit constructor code
298 if (!exists($n{"rd_constructor"})) { # default constructor
299 $temp = "\tir_node *res;\n";
300 $temp .= "\tir_op *op = op_${op};\n";
301 $temp .= "\tint flags = 0;\n";
303 if($arity == $ARITY_DYNAMIC) {
304 $temp .= "\tint arity = -1;\n";
305 $temp .= "\tir_node **in = NULL;\n";
306 } elsif($arity == $ARITY_VARIABLE) {
308 $temp .= "\tint arity = $arity;\n";
310 $temp .= "\tir_node *in[$arity];\n";
312 $temp .= "\tir_node **in = NULL;\n";
315 if($out_arity == $ARITY_DYNAMIC) {
316 $temp .= "\tint n_res = -1;\n";
317 } elsif($out_arity == $ARITY_VARIABLE) {
319 $temp .= "\tint n_res = ${out_arity};\n";
322 my $latency = $n{"latency"};
323 if (!defined($latency)) {
326 $temp .= "\tunsigned latency = ${latency};\n";
328 if (defined($known_mode)) {
329 $temp .= "\tir_mode *mode = ${known_mode};\n";
332 # set up static variables for cpu execution unit assigments
333 if (exists($n{"units"})) {
334 $temp .= gen_execunit_list_initializer($n{"units"});
336 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
339 undef my $in_req_var;
340 undef my $out_req_var;
342 # set up static variables for requirements and registers
343 if (exists($n{"reg_req"})) {
344 my %req = %{ $n{"reg_req"} };
348 @in = @{ $req{"in"} } if (exists($req{"in"}));
350 @out = @{ $req{"out"} } if exists(($req{"out"}));
353 if($arity >= 0 && scalar(@in) != $arity) {
354 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
357 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
359 for ($idx = 0; $idx <= $#in; $idx++) {
360 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\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 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
377 for ($idx = 0; $idx <= $#out; $idx++) {
378 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
383 die "Fatal error: need out requirements for ${op}\n";
385 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
388 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
389 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
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 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
416 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
417 } elsif ($flag eq "N") {
418 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
419 } elsif ($flag eq "I") {
420 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
421 } elsif ($flag eq "S") {
422 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
428 $temp .= "\t/* create node */\n";
429 $temp .= "\tassert(op != NULL);\n";
430 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
433 $temp .= "\t/* init node attributes */\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}'";
439 $temp .= "${attr_init_code}\n";
443 if ($#out_flags >= 0) {
444 $temp .= "\t/* set flags for outs */\n";
445 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
449 foreach my $flag (split(/\|/, $out_flags[$idx])) {
451 $flags .= $prefix."arch_irn_flags_ignore";
454 elsif ($flag eq "S") {
455 $flags .= $prefix."arch_irn_flags_modify_sp";
460 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
466 if (exists($n{"init_attr"})) {
467 $temp .= "\tattr = get_irn_generic_attr(res);\n";
468 $temp .= "\t".$n{"init_attr"}."\n";
471 $temp .= "\t/* optimize node */\n";
472 $temp .= "\tres = optimize_node(res);\n";
473 $temp .= "\tirn_vrfy_irg(res, irg);\n";
476 $temp .= "\treturn res;\n";
478 push(@obst_constructor, $temp);
480 else { # user defined constructor
481 push(@obst_constructor, $n{"rd_constructor"});
484 # close constructor function
485 push(@obst_constructor, "}\n\n");
486 } # constructor creation
488 # set default values for state and flags if not given
489 $n{"state"} = "floats" if (! exists($n{"state"}));
490 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
493 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
494 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
496 if (defined($cmp_attr_func)) {
497 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
501 my $n_res = $out_arity;
503 $n_res = "20"; # hacky....
505 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
506 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
507 push(@obst_new_irop, $temp);
508 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
509 push(@obst_enum_op, "\tiro_$op,\n");
511 push(@obst_header, "\n");
513 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
514 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
515 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
516 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
520 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
522 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
523 print OUT @obst_cmp_attr;
525 print OUT @obst_opvar;
527 print OUT @obst_get_opvar;
532 static int $arch\_opcode_start = -1;
533 static int $arch\_opcode_end = -1;
537 # build the FOURCC arguments from $arch
539 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
541 if (length($arch) >= 1) {
542 $a = uc(substr($arch, 0, 1));
545 if (length($arch) >= 2) {
546 $b = uc(substr($arch, 1, 1));
549 if (length($arch) >= 3) {
550 $c = uc(substr($arch, 2, 1));
553 if (length($arch) >= 4) {
554 $d = uc(substr($arch, 3, 1));
557 print OUT<<ENDOFISIRN;
559 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
560 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
562 /** Return the opcode number of the first $arch opcode. */
563 int get_$arch\_opcode_first(void) {
564 return $arch\_opcode_start;
567 /** Return the opcode number of the last $arch opcode + 1. */
568 int get_$arch\_opcode_last(void) {
569 return $arch\_opcode_end;
572 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
573 int is_$arch\_op(const ir_op *op) {
574 return get_op_tag(op) == &$arch\_op_tag;
577 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
578 int is_$arch\_irn(const ir_node *node) {
579 return is_$arch\_op(get_irn_op(node));
582 int get_$arch\_irn_opcode(const ir_node *node) {
583 if (is_$arch\_irn(node))
584 return get_irn_opcode(node) - $arch\_opcode_start;
590 print OUT @obst_constructor;
592 print OUT<<ENDOFMAIN;
594 * Creates the $arch specific Firm machine operations
595 * needed for the assembler irgs.
597 void $arch\_create_opcodes(void) {
598 #define N irop_flag_none
599 #define L irop_flag_labeled
600 #define C irop_flag_commutative
601 #define X irop_flag_cfopcode
602 #define I irop_flag_ip_cfopcode
603 #define F irop_flag_fragile
604 #define Y irop_flag_forking
605 #define H irop_flag_highlevel
606 #define c irop_flag_constlike
607 #define K irop_flag_keep
608 #define M irop_flag_machine
609 #define O irop_flag_machine_op
610 #define R (irop_flag_user << 0)
614 static int run_once = 0;
620 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
622 $arch\_opcode_start = cur_opcode;
625 print OUT @obst_new_irop;
627 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
628 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
629 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
635 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
637 my $creation_time = localtime(time());
643 * \@brief Function prototypes for the new opcode functions.
644 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
645 * Edit $specfile instead.
646 * created by: $0 $specfile $target_dir
647 * \@date $creation_time
649 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
650 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
654 print OUT @obst_enum_op;
655 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
656 print OUT "int get_$arch\_opcode_first(void);\n";
657 print OUT "int get_$arch\_opcode_last(void);\n";
658 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
659 print OUT @obst_header;
660 print OUT @obst_proj;
669 # Translates numeric arity into string constant.
671 sub translate_arity {
674 if ($arity =~ /^\d+$/) {
676 return "oparity_zero";
678 elsif ($arity == 1) {
679 return "oparity_unary";
681 elsif ($arity == 2) {
682 return "oparity_binary";
684 elsif ($arity == 3) {
685 return "oparity_trinary";
688 return "oparity_any";
690 } elsif ($arity == $ARITY_VARIABLE) {
691 return "oparity_variable";
692 } elsif ($arity == $ARITY_DYNAMIC) {
693 return "oparity_dynamic";
695 die "Fatal error: Unknown arity $arity";
700 # Return the list of pointers for the given execution units.
702 sub gen_execunit_list_initializer {
704 my $uc_arch = uc($arch);
709 foreach my $unit (@{ $units }) {
710 if ($unit eq "DUMMY") {
711 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
713 elsif (exists($cpu{"$unit"})) {
714 # operation can be executed on all units of this type
716 my $tp_name = "$arch\_execution_units_$unit";
718 foreach (@{ $cpu{"$unit"} }) {
719 next if ($idx++ == 0); # skip first element (it's not a unit)
720 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
721 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
725 # operation can be executed only a certain unit
726 # -> find corresponding unit type
728 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
729 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
730 if ($unit eq $cur_unit) {
731 my $tp_name = "$arch\_execution_units_$cur_type";
732 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
733 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
741 print STDERR "Invalid execution unit $unit specified!\n";
746 # prepare the 2-dim array init
747 foreach my $key (keys(%init)) {
748 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
750 foreach (@{ $init{"$key"} }) {
753 $ret .= "\t\tNULL\n";
755 $ret2 .= "\t\tallowed_units_$key,\n";
757 $ret2 .= "\t\tNULL\n";
759 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";