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 if($cmpcode =~ m/attr_b/) {
220 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
222 push(@obst_cmp_attr, "\t${cmpcode}\n");
223 push(@obst_cmp_attr, "}\n\n");
225 $cmp_attr_func = "cmp_attr_${op}";
227 if(defined($compare_attr{${attr_type}})) {
228 $cmp_attr_func = $compare_attr{${attr_type}};
230 die "Fatal error: No compare function defined for ${attr_type} attributes.";
234 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
235 # we explicitly skip the constructor if the specification entry says NONE
237 my $comment = $n{"comment"};
238 if(!exists($n{"comment"})) {
239 $comment = "construct ${orig_op} node";
246 push(@obst_constructor, $comment);
248 # create constructor head
249 my $complete_args = "";
252 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
253 if (!exists($n{"args"})) { # default args
254 if ($arity == $ARITY_VARIABLE) {
255 $complete_args = ", int arity, ir_node *in[]";
256 } elsif ($arity == $ARITY_DYNAMIC) {
259 for (my $i = 0; $i < $arity; $i++) {
260 my $opname = "op${i}";
261 if (exists($n{"ins"})) {
262 my @ins = @{ $n{"ins"} };
266 $complete_args .= ", ir_node *${opname}";
269 if ($out_arity == $ARITY_VARIABLE) {
270 $complete_args .= ", int n_res";
273 if (!defined($known_mode)) {
274 $complete_args .= ", ir_mode *mode";
276 } else { # user defined args
277 for my $href (@{ $n{"args"} }) {
278 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
279 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
283 # we have additional attribute arguements
284 if (exists($n{"attr"})) {
285 $complete_args .= ", ".$n{"attr"};
288 $temp .= "$complete_args)";
289 push(@obst_constructor, $temp."\n{\n");
290 push(@obst_header, $comment);
291 push(@obst_header, $temp.";\n");
293 # emit constructor code
294 if (!exists($n{"rd_constructor"})) { # default constructor
295 $temp = "\tir_node *res;\n";
296 $temp .= "\tir_op *op = op_${op};\n";
297 $temp .= "\tint flags = 0;\n";
299 if($arity == $ARITY_DYNAMIC) {
300 $temp .= "\tint arity = -1;\n";
301 $temp .= "\tir_node **in = NULL;\n";
302 } elsif($arity == $ARITY_VARIABLE) {
304 $temp .= "\tint arity = $arity;\n";
306 $temp .= "\tir_node *in[$arity];\n";
308 $temp .= "\tir_node **in = NULL;\n";
311 if($out_arity == $ARITY_DYNAMIC) {
312 $temp .= "\tint n_res = -1;\n";
313 } elsif($out_arity == $ARITY_VARIABLE) {
315 $temp .= "\tint n_res = ${out_arity};\n";
318 my $latency = $n{"latency"};
319 if (!defined($latency)) {
322 $temp .= "\tunsigned latency = ${latency};\n";
324 if (defined($known_mode)) {
325 $temp .= "\tir_mode *mode = ${known_mode};\n";
328 # set up static variables for cpu execution unit assigments
329 if (exists($n{"units"})) {
330 $temp .= gen_execunit_list_initializer($n{"units"});
332 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
335 undef my $in_req_var;
336 undef my $out_req_var;
338 # set up static variables for requirements and registers
339 if (exists($n{"reg_req"})) {
340 my %req = %{ $n{"reg_req"} };
344 @in = @{ $req{"in"} } if (exists($req{"in"}));
346 @out = @{ $req{"out"} } if exists(($req{"out"}));
349 if($arity >= 0 && scalar(@in) != $arity) {
350 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
353 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
355 for ($idx = 0; $idx <= $#in; $idx++) {
356 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\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 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
373 for ($idx = 0; $idx <= $#out; $idx++) {
374 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
379 die "Fatal error: need out requirements for ${op}\n";
381 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
384 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
385 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
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 "I") {
416 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
417 } elsif ($flag eq "S") {
418 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
424 $temp .= "\t/* create node */\n";
425 $temp .= "\tassert(op != NULL);\n";
426 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
429 $temp .= "\t/* init node attributes */\n";
430 # lookup init function
431 my $attr_init_code = $init_attr{$attr_type};
432 if(!defined($attr_init_code)) {
433 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
435 $temp .= "${attr_init_code}\n";
439 if ($#out_flags >= 0) {
440 $temp .= "\t/* set flags for outs */\n";
441 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
445 foreach my $flag (split(/\|/, $out_flags[$idx])) {
447 $flags .= $prefix."arch_irn_flags_ignore";
450 elsif ($flag eq "S") {
451 $flags .= $prefix."arch_irn_flags_modify_sp";
456 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
462 if (exists($n{"init_attr"})) {
463 $temp .= "\tattr = get_irn_generic_attr(res);\n";
464 $temp .= "\t".$n{"init_attr"}."\n";
467 $temp .= "\t/* optimize node */\n";
468 $temp .= "\tres = optimize_node(res);\n";
469 $temp .= "\tirn_vrfy_irg(res, irg);\n";
472 $temp .= "\treturn res;\n";
474 push(@obst_constructor, $temp);
476 else { # user defined constructor
477 push(@obst_constructor, $n{"rd_constructor"});
480 # close constructor function
481 push(@obst_constructor, "}\n\n");
482 } # constructor creation
484 # set default values for state and flags if not given
485 $n{"state"} = "floats" if (! exists($n{"state"}));
486 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
489 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
490 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
492 if (defined($cmp_attr_func)) {
493 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
497 my $n_res = $out_arity;
499 $n_res = "20"; # hacky....
501 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
502 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
503 push(@obst_new_irop, $temp);
504 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
505 push(@obst_enum_op, "\tiro_$op,\n");
507 push(@obst_header, "\n");
509 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
510 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
511 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
512 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
516 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
518 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
519 print OUT @obst_cmp_attr;
521 print OUT @obst_opvar;
523 print OUT @obst_get_opvar;
528 static int $arch\_opcode_start = -1;
529 static int $arch\_opcode_end = -1;
533 # build the FOURCC arguments from $arch
535 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
537 if (length($arch) >= 1) {
538 $a = uc(substr($arch, 0, 1));
541 if (length($arch) >= 2) {
542 $b = uc(substr($arch, 1, 1));
545 if (length($arch) >= 3) {
546 $c = uc(substr($arch, 2, 1));
549 if (length($arch) >= 4) {
550 $d = uc(substr($arch, 3, 1));
553 print OUT<<ENDOFISIRN;
555 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
556 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
558 /** Return the opcode number of the first $arch opcode. */
559 int get_$arch\_opcode_first(void) {
560 return $arch\_opcode_start;
563 /** Return the opcode number of the last $arch opcode + 1. */
564 int get_$arch\_opcode_last(void) {
565 return $arch\_opcode_end;
568 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
569 int is_$arch\_op(const ir_op *op) {
570 return get_op_tag(op) == &$arch\_op_tag;
573 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
574 int is_$arch\_irn(const ir_node *node) {
575 return is_$arch\_op(get_irn_op(node));
578 int get_$arch\_irn_opcode(const ir_node *node) {
579 if (is_$arch\_irn(node))
580 return get_irn_opcode(node) - $arch\_opcode_start;
586 print OUT @obst_constructor;
588 print OUT<<ENDOFMAIN;
590 * Creates the $arch specific Firm machine operations
591 * needed for the assembler irgs.
593 void $arch\_create_opcodes(void) {
594 #define N irop_flag_none
595 #define L irop_flag_labeled
596 #define C irop_flag_commutative
597 #define X irop_flag_cfopcode
598 #define I irop_flag_ip_cfopcode
599 #define F irop_flag_fragile
600 #define Y irop_flag_forking
601 #define H irop_flag_highlevel
602 #define c irop_flag_constlike
603 #define K irop_flag_keep
604 #define M irop_flag_machine
605 #define O irop_flag_machine_op
606 #define R (irop_flag_user << 0)
610 static int run_once = 0;
616 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
618 $arch\_opcode_start = cur_opcode;
621 print OUT @obst_new_irop;
623 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
624 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
625 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
631 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
633 my $creation_time = localtime(time());
639 * \@brief Function prototypes for the new opcode functions.
640 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
641 * Edit $specfile instead.
642 * created by: $0 $specfile $target_dir
643 * \@date $creation_time
645 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
646 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
650 print OUT @obst_enum_op;
651 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
652 print OUT "int get_$arch\_opcode_first(void);\n";
653 print OUT "int get_$arch\_opcode_last(void);\n";
654 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
655 print OUT @obst_header;
656 print OUT @obst_proj;
665 # Translates numeric arity into string constant.
667 sub translate_arity {
670 if ($arity =~ /^\d+$/) {
672 return "oparity_zero";
674 elsif ($arity == 1) {
675 return "oparity_unary";
677 elsif ($arity == 2) {
678 return "oparity_binary";
680 elsif ($arity == 3) {
681 return "oparity_trinary";
684 return "oparity_any";
686 } elsif ($arity == $ARITY_VARIABLE) {
687 return "oparity_variable";
688 } elsif ($arity == $ARITY_DYNAMIC) {
689 return "oparity_dynamic";
691 die "Fatal error: Unknown arity $arity";
696 # Return the list of pointers for the given execution units.
698 sub gen_execunit_list_initializer {
700 my $uc_arch = uc($arch);
705 foreach my $unit (@{ $units }) {
706 if ($unit eq "DUMMY") {
707 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
709 elsif (exists($cpu{"$unit"})) {
710 # operation can be executed on all units of this type
712 my $tp_name = "$arch\_execution_units_$unit";
714 foreach (@{ $cpu{"$unit"} }) {
715 next if ($idx++ == 0); # skip first element (it's not a unit)
716 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
717 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
721 # operation can be executed only a certain unit
722 # -> find corresponding unit type
724 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
725 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
726 if ($unit eq $cur_unit) {
727 my $tp_name = "$arch\_execution_units_$cur_type";
728 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
729 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
737 print STDERR "Invalid execution unit $unit specified!\n";
742 # prepare the 2-dim array init
743 foreach my $key (keys(%init)) {
744 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
746 foreach (@{ $init{"$key"} }) {
749 $ret .= "\t\tNULL\n";
751 $ret2 .= "\t\tallowed_units_$key,\n";
753 $ret2 .= "\t\tNULL\n";
755 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";