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_cmp_attr;
42 our $default_attr_type;
49 unless ($return = do $specfile) {
50 die "couldn't parse $specfile: $@" if $@;
51 die "couldn't do $specfile: $!" unless defined $return;
52 die "couldn't run $specfile" unless $return;
56 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
57 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
59 if(!defined($default_attr_type)) {
60 $default_attr_type = "${arch}_attr_t";
63 #print Dumper(%nodes);
64 #print Dumper(%operands);
66 # create c code file from specs
68 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
69 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
70 my @obst_constructor; # stack for node constructor functions
71 my @obst_new_irop; # stack for the new_ir_op calls
72 my @obst_enum_op; # stack for creating the <arch>_opcode enum
73 my @obst_header; # stack for function prototypes
74 my @obst_is_archirn; # stack for the is_$arch_irn() function
75 my @obst_cmp_attr; # stack for the compare attribute functions
76 my @obst_proj; # stack for the pn_ numbers
81 my $n_opcodes = 0; # number of opcodes
82 my $ARITY_VARIABLE = -1;
83 my $ARITY_DYNAMIC = -2;
85 # for registering additional opcodes
86 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
88 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
90 # create default compare function
91 if(defined($default_cmp_attr)) {
92 my $cmpcode = $default_cmp_attr;
93 push(@obst_cmp_attr, "static int default_cmp_attr(ir_node *a, ir_node *b) {\n");
94 if($cmpcode =~ m/attr_a/) {
95 push(@obst_cmp_attr, "\t${default_attr_type} *attr_a = get_irn_generic_attr(a);\n");
97 if($cmpcode =~ m/attr_b/) {
98 push(@obst_cmp_attr, "\t${default_attr_type} *attr_b = get_irn_generic_attr(b);\n");
100 push(@obst_cmp_attr, "\t${cmpcode}\n");
101 push(@obst_cmp_attr, "}\n\n");
104 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
105 foreach my $op (keys(%nodes)) {
106 my %n = %{ $nodes{"$op"} };
114 if(exists($n{"arity"})) {
115 $arity = $n{"arity"};
116 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
117 $arity = scalar(@{ $n{"reg_req"}{"in"} });
118 } elsif (exists($n{"ins"})) {
119 $arity = scalar(@{ $n{"ins"} });
121 if($arity eq "variable") {
122 $arity = $ARITY_VARIABLE;
123 } elsif($arity eq "dynamic") {
124 $arity = $ARITY_DYNAMIC;
127 # determine out arity
129 if(exists($n{"out_arity"})) {
130 $out_arity = $n{"out_arity"};
131 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
132 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
133 } elsif (exists($n{"outs"})) {
134 $out_arity = scalar(@{ $n{"outs"} });
136 if($out_arity eq "variable") {
137 $out_arity = $ARITY_VARIABLE;
138 } elsif($out_arity eq "dynamic") {
139 $out_arity = $ARITY_DYNAMIC;
146 # define proj numbers and in numbers
147 if (exists($n{"outs"})) {
150 @outs = @{ $n{"outs"} };
151 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
152 die "Op ${op} has different number of outs and out_arity\n";
155 $num_outs = $#outs + 1;
157 push(@obst_proj, "\nenum pn_$op {\n");
159 for (my $idx = 0; $idx <= $#outs; $idx++) {
160 # check, if we have additional flags annotated to out
161 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
162 push(@out_flags, $1);
163 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
165 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
168 push(@obst_proj, "};\n");
169 # outs have names, it must be a mode_T node
170 $known_mode = "mode_T";
172 if (exists($n{"ins"})) {
175 @ins = @{ $n{"ins"} };
176 if($arity >= 0 && scalar(@ins) != $arity) {
177 die "Op ${op} has different number of ins and arity\n";
180 push(@obst_proj, "\nenum n_$op {\n");
182 for (my $idx = 0; $idx <= $#ins; $idx++) {
183 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
186 push(@obst_proj, "};\n");
190 if (exists($n{"mode"})) {
191 $known_mode = $n{"mode"};
194 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
195 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
196 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
198 push(@obst_is_archirn, "is_$op(node)");
200 push(@obst_header, "extern ir_op *op_$op;\n");
201 push(@obst_header, "ir_op *get_op_$op(void);\n");
202 push(@obst_header, "int is_$op(const ir_node *n);\n");
204 my $attr_type= $n{"attr_type"};
205 if(!defined($attr_type)) {
206 $attr_type = $default_attr_type;
209 # determine compare function
211 if(defined($default_cmp_attr)) {
212 $cmp_attr_func = "default_cmp_attr";
214 if (exists($n{"cmp_attr"})) {
215 my $cmpcode = $n{"cmp_attr"};
217 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
218 if($cmpcode =~ m/attr_a/) {
219 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(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${cmpcode}\n");
225 push(@obst_cmp_attr, "}\n\n");
227 $cmp_attr_func = "cmp_attr_${op}";
230 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
231 # we explicitly skip the constructor if the specification entry says NONE
233 my $comment = $n{"comment"};
234 if(!exists($n{"comment"})) {
235 $comment = "construct ${orig_op} node";
242 push(@obst_constructor, $comment);
244 # create constructor head
245 my $complete_args = "";
248 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
249 if (!exists($n{"args"})) { # default args
250 if ($arity == $ARITY_VARIABLE) {
251 $complete_args = ", int arity, ir_node *in[]";
252 } elsif ($arity == $ARITY_DYNAMIC) {
255 for (my $i = 0; $i < $arity; $i++) {
256 my $opname = "op${i}";
257 if (exists($n{"ins"})) {
258 my @ins = @{ $n{"ins"} };
262 $complete_args .= ", ir_node *${opname}";
265 if ($out_arity == $ARITY_VARIABLE) {
266 $complete_args .= ", int n_res";
269 if (!defined($known_mode)) {
270 $complete_args .= ", ir_mode *mode";
272 } else { # user defined args
273 for my $href (@{ $n{"args"} }) {
274 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
275 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
279 # we have additional attribute arguements
280 if (exists($n{"attr"})) {
281 $complete_args .= ", ".$n{"attr"};
284 $temp .= "$complete_args)";
285 push(@obst_constructor, $temp."\n{\n");
286 push(@obst_header, $comment);
287 push(@obst_header, $temp.";\n");
289 # emit constructor code
290 if (!exists($n{"rd_constructor"})) { # default constructor
291 $temp = "\tir_node *res;\n";
292 $temp .= "\tir_op *op = op_${op};\n";
293 $temp .= "\tint flags = 0;\n";
295 if($arity == $ARITY_DYNAMIC) {
296 $temp .= "\tint arity = -1;\n";
297 $temp .= "\tir_node **in = NULL;\n";
298 } elsif($arity == $ARITY_VARIABLE) {
300 $temp .= "\tint arity = $arity;\n";
302 $temp .= "\tir_node *in[$arity];\n";
304 $temp .= "\tir_node **in = NULL;\n";
307 if($out_arity == $ARITY_DYNAMIC) {
308 $temp .= "\tint n_res = -1;\n";
309 } elsif($out_arity == $ARITY_VARIABLE) {
311 $temp .= "\tint n_res = ${out_arity};\n";
314 my $latency = $n{"latency"};
315 if (!defined($latency)) {
318 $temp .= "\tunsigned latency = ${latency};\n";
320 if (defined($known_mode)) {
321 $temp .= "\tir_mode *mode = ${known_mode};\n";
324 # set up static variables for cpu execution unit assigments
325 if (exists($n{"units"})) {
326 $temp .= gen_execunit_list_initializer($n{"units"});
328 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
331 undef my $in_req_var;
332 undef my $out_req_var;
334 # set up static variables for requirements and registers
335 if (exists($n{"reg_req"})) {
336 my %req = %{ $n{"reg_req"} };
340 @in = @{ $req{"in"} } if (exists($req{"in"}));
342 @out = @{ $req{"out"} } if exists(($req{"out"}));
345 if($arity >= 0 && scalar(@in) != $arity) {
346 die "Arity and number of in requirements don't match for ${op}\n";
349 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
351 for ($idx = 0; $idx <= $#in; $idx++) {
352 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
357 die "need in requirements for ${op}\n";
359 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
363 if($out_arity >= 0 && scalar(@out) != $out_arity) {
364 die "Out-Arity and number of out requirements don't match for ${op}\n";
367 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
369 for ($idx = 0; $idx <= $#out; $idx++) {
370 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
375 die "need out requirements for ${op}\n";
377 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
380 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
381 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
383 if(exists($n{"init_attr"})) {
384 $temp .= "\t${attr_type} *attr;\n";
390 $temp .= "\t/* construct in array */\n";
391 for (my $i = 0; $i < $arity; $i++) {
392 my $opname = "op${i}";
393 if (exists($n{"ins"})) {
394 my @ins = @{ $n{"ins"} };
398 $temp .= "\tin[${i}] = ${opname};\n";
404 if (exists($n{"irn_flags"})) {
405 $temp .= "\t/* flags */\n";
406 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
408 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
409 } elsif ($flag eq "N") {
410 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
411 } elsif ($flag eq "I") {
412 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
413 } elsif ($flag eq "S") {
414 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
420 $temp .= "\t/* create node */\n";
421 $temp .= "\tassert(op != NULL);\n";
422 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
425 $temp .= "\t/* init node attributes */\n";
426 $temp .= "\tinit_$arch\_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);\n";
430 if ($#out_flags >= 0) {
431 $temp .= "\t/* set flags for outs */\n";
432 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
436 foreach my $flag (split(/\|/, $out_flags[$idx])) {
438 $flags .= $prefix."arch_irn_flags_ignore";
441 elsif ($flag eq "S") {
442 $flags .= $prefix."arch_irn_flags_modify_sp";
447 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
453 if (exists($n{"init_attr"})) {
454 $temp .= "\tattr = get_$arch\_attr(res);\n";
455 $temp .= $n{"init_attr"}."\n";
458 $temp .= "\t/* optimize node */\n";
459 $temp .= "\tres = optimize_node(res);\n";
460 $temp .= "\tirn_vrfy_irg(res, irg);\n";
463 $temp .= "\treturn res;\n";
465 push(@obst_constructor, $temp);
467 else { # user defined constructor
468 push(@obst_constructor, $n{"rd_constructor"});
471 # close constructor function
472 push(@obst_constructor, "}\n\n");
473 } # constructor creation
475 # set default values for state and flags if not given
476 $n{"state"} = "floats" if (! exists($n{"state"}));
477 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
480 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
481 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
483 if (defined($cmp_attr_func)) {
484 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
488 my $n_res = $out_arity;
490 $n_res = "20"; # hacky....
492 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
493 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
494 push(@obst_new_irop, $temp);
495 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
496 push(@obst_enum_op, "\tiro_$op,\n");
498 push(@obst_header, "\n");
500 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
501 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
502 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
503 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
507 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
509 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
510 print OUT @obst_cmp_attr;
512 print OUT @obst_opvar;
514 print OUT @obst_get_opvar;
519 static int $arch\_opcode_start = -1;
520 static int $arch\_opcode_end = -1;
524 # build the FOURCC arguments from $arch
526 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
528 if (length($arch) >= 1) {
529 $a = uc(substr($arch, 0, 1));
532 if (length($arch) >= 2) {
533 $b = uc(substr($arch, 1, 1));
536 if (length($arch) >= 3) {
537 $c = uc(substr($arch, 2, 1));
540 if (length($arch) >= 4) {
541 $d = uc(substr($arch, 3, 1));
544 print OUT<<ENDOFISIRN;
546 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
547 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
549 /** Return the opcode number of the first $arch opcode. */
550 int get_$arch\_opcode_first(void) {
551 return $arch\_opcode_start;
554 /** Return the opcode number of the last $arch opcode + 1. */
555 int get_$arch\_opcode_last(void) {
556 return $arch\_opcode_end;
559 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
560 int is_$arch\_op(const ir_op *op) {
561 return get_op_tag(op) == &$arch\_op_tag;
564 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
565 int is_$arch\_irn(const ir_node *node) {
566 return is_$arch\_op(get_irn_op(node));
569 int get_$arch\_irn_opcode(const ir_node *node) {
570 if (is_$arch\_irn(node))
571 return get_irn_opcode(node) - $arch\_opcode_start;
577 print OUT @obst_constructor;
579 print OUT<<ENDOFMAIN;
581 * Creates the $arch specific Firm machine operations
582 * needed for the assembler irgs.
584 void $arch\_create_opcodes(void) {
585 #define N irop_flag_none
586 #define L irop_flag_labeled
587 #define C irop_flag_commutative
588 #define X irop_flag_cfopcode
589 #define I irop_flag_ip_cfopcode
590 #define F irop_flag_fragile
591 #define Y irop_flag_forking
592 #define H irop_flag_highlevel
593 #define c irop_flag_constlike
594 #define K irop_flag_keep
595 #define M irop_flag_machine
596 #define O irop_flag_machine_op
597 #define R (irop_flag_user << 0)
601 static int run_once = 0;
607 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
609 $arch\_opcode_start = cur_opcode;
612 print OUT @obst_new_irop;
614 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
615 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
616 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
622 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
624 my $creation_time = localtime(time());
630 * \@brief Function prototypes for the new opcode functions.
631 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
632 * Edit $specfile instead.
633 * created by: $0 $specfile $target_dir
634 * \@date $creation_time
636 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
637 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
641 print OUT @obst_enum_op;
642 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
643 print OUT "int get_$arch\_opcode_first(void);\n";
644 print OUT "int get_$arch\_opcode_last(void);\n";
645 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
646 print OUT @obst_header;
647 print OUT @obst_proj;
656 # Translates numeric arity into string constant.
658 sub translate_arity {
661 if ($arity =~ /^\d+$/) {
663 return "oparity_zero";
665 elsif ($arity == 1) {
666 return "oparity_unary";
668 elsif ($arity == 2) {
669 return "oparity_binary";
671 elsif ($arity == 3) {
672 return "oparity_trinary";
675 return "oparity_any";
677 } elsif ($arity == $ARITY_VARIABLE) {
678 return "oparity_variable";
679 } elsif ($arity == $ARITY_DYNAMIC) {
680 return "oparity_dynamic";
682 die "Unknown arity $arity";
687 # Return the list of pointers for the given execution units.
689 sub gen_execunit_list_initializer {
691 my $uc_arch = uc($arch);
696 foreach my $unit (@{ $units }) {
697 if ($unit eq "DUMMY") {
698 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
700 elsif (exists($cpu{"$unit"})) {
701 # operation can be executed on all units of this type
703 my $tp_name = "$arch\_execution_units_$unit";
705 foreach (@{ $cpu{"$unit"} }) {
706 next if ($idx++ == 0); # skip first element (it's not a unit)
707 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
708 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
712 # operation can be executed only a certain unit
713 # -> find corresponding unit type
715 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
716 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
717 if ($unit eq $cur_unit) {
718 my $tp_name = "$arch\_execution_units_$cur_type";
719 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
720 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
728 print STDERR "Invalid execution unit $unit specified!\n";
733 # prepare the 2-dim array init
734 foreach my $key (keys(%init)) {
735 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
737 foreach (@{ $init{"$key"} }) {
740 $ret .= "\t\tNULL\n";
742 $ret2 .= "\t\tallowed_units_$key,\n";
744 $ret2 .= "\t\tNULL\n";
746 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";