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;
50 unless ($return = do $specfile) {
51 die "couldn't parse $specfile: $@" if $@;
52 die "couldn't do $specfile: $!" unless defined $return;
53 die "couldn't run $specfile" unless $return;
57 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
58 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
60 if(!defined($default_attr_type)) {
61 $default_attr_type = "${arch}_attr_t";
63 if(!defined(%init_attr)) {
65 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
69 #print Dumper(%nodes);
70 #print Dumper(%operands);
72 # create c code file from specs
74 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
75 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
76 my @obst_constructor; # stack for node constructor functions
77 my @obst_new_irop; # stack for the new_ir_op calls
78 my @obst_enum_op; # stack for creating the <arch>_opcode enum
79 my @obst_header; # stack for function prototypes
80 my @obst_is_archirn; # stack for the is_$arch_irn() function
81 my @obst_cmp_attr; # stack for the compare attribute functions
82 my @obst_proj; # stack for the pn_ numbers
87 my $n_opcodes = 0; # number of opcodes
88 my $ARITY_VARIABLE = -1;
89 my $ARITY_DYNAMIC = -2;
91 # for registering additional opcodes
92 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
94 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
96 # create default compare function
97 if(defined($default_cmp_attr)) {
98 my $cmpcode = $default_cmp_attr;
99 push(@obst_cmp_attr, "static int default_cmp_attr(ir_node *a, ir_node *b) {\n");
100 if($cmpcode =~ m/attr_a/) {
101 push(@obst_cmp_attr, "\t${default_attr_type} *attr_a = get_irn_generic_attr(a);\n");
103 if($cmpcode =~ m/attr_b/) {
104 push(@obst_cmp_attr, "\t${default_attr_type} *attr_b = get_irn_generic_attr(b);\n");
106 push(@obst_cmp_attr, "\t${cmpcode}\n");
107 push(@obst_cmp_attr, "}\n\n");
110 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
111 foreach my $op (keys(%nodes)) {
112 my %n = %{ $nodes{"$op"} };
120 if(exists($n{"arity"})) {
121 $arity = $n{"arity"};
122 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
123 $arity = scalar(@{ $n{"reg_req"}{"in"} });
124 } elsif (exists($n{"ins"})) {
125 $arity = scalar(@{ $n{"ins"} });
127 if($arity eq "variable") {
128 $arity = $ARITY_VARIABLE;
129 } elsif($arity eq "dynamic") {
130 $arity = $ARITY_DYNAMIC;
133 # determine out arity
135 if(exists($n{"out_arity"})) {
136 $out_arity = $n{"out_arity"};
137 } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
138 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
139 } elsif (exists($n{"outs"})) {
140 $out_arity = scalar(@{ $n{"outs"} });
142 if($out_arity eq "variable") {
143 $out_arity = $ARITY_VARIABLE;
144 } elsif($out_arity eq "dynamic") {
145 $out_arity = $ARITY_DYNAMIC;
152 # define proj numbers and in numbers
153 if (exists($n{"outs"})) {
156 @outs = @{ $n{"outs"} };
157 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
158 die "Op ${op} has different number of outs and out_arity\n";
161 $num_outs = $#outs + 1;
163 push(@obst_proj, "\nenum pn_$op {\n");
165 for (my $idx = 0; $idx <= $#outs; $idx++) {
166 # check, if we have additional flags annotated to out
167 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
168 push(@out_flags, $1);
169 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
171 push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
174 push(@obst_proj, "};\n");
175 # outs have names, it must be a mode_T node
176 $known_mode = "mode_T";
178 if (exists($n{"ins"})) {
181 @ins = @{ $n{"ins"} };
182 if($arity >= 0 && scalar(@ins) != $arity) {
183 die "Op ${op} has different number of ins and arity\n";
186 push(@obst_proj, "\nenum n_$op {\n");
188 for (my $idx = 0; $idx <= $#ins; $idx++) {
189 push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
192 push(@obst_proj, "};\n");
196 if (exists($n{"mode"})) {
197 $known_mode = $n{"mode"};
200 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
201 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
202 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
204 push(@obst_is_archirn, "is_$op(node)");
206 push(@obst_header, "extern ir_op *op_$op;\n");
207 push(@obst_header, "ir_op *get_op_$op(void);\n");
208 push(@obst_header, "int is_$op(const ir_node *n);\n");
210 my $attr_type= $n{"attr_type"};
211 if(!defined($attr_type)) {
212 $attr_type = $default_attr_type;
215 # determine compare function
217 if(defined($default_cmp_attr)) {
218 $cmp_attr_func = "default_cmp_attr";
220 if (exists($n{"cmp_attr"})) {
221 my $cmpcode = $n{"cmp_attr"};
223 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
224 if($cmpcode =~ m/attr_a/) {
225 push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
227 if($cmpcode =~ m/attr_b/) {
228 push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
230 push(@obst_cmp_attr, "\t${cmpcode}\n");
231 push(@obst_cmp_attr, "}\n\n");
233 $cmp_attr_func = "cmp_attr_${op}";
236 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
237 # we explicitly skip the constructor if the specification entry says NONE
239 my $comment = $n{"comment"};
240 if(!exists($n{"comment"})) {
241 $comment = "construct ${orig_op} node";
248 push(@obst_constructor, $comment);
250 # create constructor head
251 my $complete_args = "";
254 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
255 if (!exists($n{"args"})) { # default args
256 if ($arity == $ARITY_VARIABLE) {
257 $complete_args = ", int arity, ir_node *in[]";
258 } elsif ($arity == $ARITY_DYNAMIC) {
261 for (my $i = 0; $i < $arity; $i++) {
262 my $opname = "op${i}";
263 if (exists($n{"ins"})) {
264 my @ins = @{ $n{"ins"} };
268 $complete_args .= ", ir_node *${opname}";
271 if ($out_arity == $ARITY_VARIABLE) {
272 $complete_args .= ", int n_res";
275 if (!defined($known_mode)) {
276 $complete_args .= ", ir_mode *mode";
278 } else { # user defined args
279 for my $href (@{ $n{"args"} }) {
280 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
281 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
285 # we have additional attribute arguements
286 if (exists($n{"attr"})) {
287 $complete_args .= ", ".$n{"attr"};
290 $temp .= "$complete_args)";
291 push(@obst_constructor, $temp."\n{\n");
292 push(@obst_header, $comment);
293 push(@obst_header, $temp.";\n");
295 # emit constructor code
296 if (!exists($n{"rd_constructor"})) { # default constructor
297 $temp = "\tir_node *res;\n";
298 $temp .= "\tir_op *op = op_${op};\n";
299 $temp .= "\tint flags = 0;\n";
301 if($arity == $ARITY_DYNAMIC) {
302 $temp .= "\tint arity = -1;\n";
303 $temp .= "\tir_node **in = NULL;\n";
304 } elsif($arity == $ARITY_VARIABLE) {
306 $temp .= "\tint arity = $arity;\n";
308 $temp .= "\tir_node *in[$arity];\n";
310 $temp .= "\tir_node **in = NULL;\n";
313 if($out_arity == $ARITY_DYNAMIC) {
314 $temp .= "\tint n_res = -1;\n";
315 } elsif($out_arity == $ARITY_VARIABLE) {
317 $temp .= "\tint n_res = ${out_arity};\n";
320 my $latency = $n{"latency"};
321 if (!defined($latency)) {
324 $temp .= "\tunsigned latency = ${latency};\n";
326 if (defined($known_mode)) {
327 $temp .= "\tir_mode *mode = ${known_mode};\n";
330 # set up static variables for cpu execution unit assigments
331 if (exists($n{"units"})) {
332 $temp .= gen_execunit_list_initializer($n{"units"});
334 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
337 undef my $in_req_var;
338 undef my $out_req_var;
340 # set up static variables for requirements and registers
341 if (exists($n{"reg_req"})) {
342 my %req = %{ $n{"reg_req"} };
346 @in = @{ $req{"in"} } if (exists($req{"in"}));
348 @out = @{ $req{"out"} } if exists(($req{"out"}));
351 if($arity >= 0 && scalar(@in) != $arity) {
352 die "Arity and number of in requirements don't match for ${op}\n";
355 $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
357 for ($idx = 0; $idx <= $#in; $idx++) {
358 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
363 die "need in requirements for ${op}\n";
365 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
369 if($out_arity >= 0 && scalar(@out) != $out_arity) {
370 die "Out-Arity and number of out requirements don't match for ${op}\n";
373 $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
375 for ($idx = 0; $idx <= $#out; $idx++) {
376 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
381 die "need out requirements for ${op}\n";
383 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
386 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
387 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
389 if(exists($n{"init_attr"})) {
390 $temp .= "\t${attr_type} *attr;\n";
396 $temp .= "\t/* construct in array */\n";
397 for (my $i = 0; $i < $arity; $i++) {
398 my $opname = "op${i}";
399 if (exists($n{"ins"})) {
400 my @ins = @{ $n{"ins"} };
404 $temp .= "\tin[${i}] = ${opname};\n";
410 if (exists($n{"irn_flags"})) {
411 $temp .= "\t/* flags */\n";
412 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
414 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
415 } elsif ($flag eq "N") {
416 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
417 } elsif ($flag eq "I") {
418 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
419 } elsif ($flag eq "S") {
420 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
426 $temp .= "\t/* create node */\n";
427 $temp .= "\tassert(op != NULL);\n";
428 $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
431 $temp .= "\t/* init node attributes */\n";
432 # lookup init function
433 my $attr_init_code = $init_attr{$attr_type};
434 if(!defined($attr_init_code)) {
435 die "Couldn't find attribute initialisation code for type '${attr_type}'";
437 $temp .= "${attr_init_code}\n";
441 if ($#out_flags >= 0) {
442 $temp .= "\t/* set flags for outs */\n";
443 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
447 foreach my $flag (split(/\|/, $out_flags[$idx])) {
449 $flags .= $prefix."arch_irn_flags_ignore";
452 elsif ($flag eq "S") {
453 $flags .= $prefix."arch_irn_flags_modify_sp";
458 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
464 if (exists($n{"init_attr"})) {
465 $temp .= "\tattr = get_$arch\_attr(res);\n";
466 $temp .= $n{"init_attr"}."\n";
469 $temp .= "\t/* optimize node */\n";
470 $temp .= "\tres = optimize_node(res);\n";
471 $temp .= "\tirn_vrfy_irg(res, irg);\n";
474 $temp .= "\treturn res;\n";
476 push(@obst_constructor, $temp);
478 else { # user defined constructor
479 push(@obst_constructor, $n{"rd_constructor"});
482 # close constructor function
483 push(@obst_constructor, "}\n\n");
484 } # constructor creation
486 # set default values for state and flags if not given
487 $n{"state"} = "floats" if (! exists($n{"state"}));
488 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
491 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
492 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
494 if (defined($cmp_attr_func)) {
495 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
499 my $n_res = $out_arity;
501 $n_res = "20"; # hacky....
503 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
504 $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
505 push(@obst_new_irop, $temp);
506 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
507 push(@obst_enum_op, "\tiro_$op,\n");
509 push(@obst_header, "\n");
511 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
512 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
513 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
514 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
518 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
520 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
521 print OUT @obst_cmp_attr;
523 print OUT @obst_opvar;
525 print OUT @obst_get_opvar;
530 static int $arch\_opcode_start = -1;
531 static int $arch\_opcode_end = -1;
535 # build the FOURCC arguments from $arch
537 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
539 if (length($arch) >= 1) {
540 $a = uc(substr($arch, 0, 1));
543 if (length($arch) >= 2) {
544 $b = uc(substr($arch, 1, 1));
547 if (length($arch) >= 3) {
548 $c = uc(substr($arch, 2, 1));
551 if (length($arch) >= 4) {
552 $d = uc(substr($arch, 3, 1));
555 print OUT<<ENDOFISIRN;
557 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
558 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
560 /** Return the opcode number of the first $arch opcode. */
561 int get_$arch\_opcode_first(void) {
562 return $arch\_opcode_start;
565 /** Return the opcode number of the last $arch opcode + 1. */
566 int get_$arch\_opcode_last(void) {
567 return $arch\_opcode_end;
570 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
571 int is_$arch\_op(const ir_op *op) {
572 return get_op_tag(op) == &$arch\_op_tag;
575 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
576 int is_$arch\_irn(const ir_node *node) {
577 return is_$arch\_op(get_irn_op(node));
580 int get_$arch\_irn_opcode(const ir_node *node) {
581 if (is_$arch\_irn(node))
582 return get_irn_opcode(node) - $arch\_opcode_start;
588 print OUT @obst_constructor;
590 print OUT<<ENDOFMAIN;
592 * Creates the $arch specific Firm machine operations
593 * needed for the assembler irgs.
595 void $arch\_create_opcodes(void) {
596 #define N irop_flag_none
597 #define L irop_flag_labeled
598 #define C irop_flag_commutative
599 #define X irop_flag_cfopcode
600 #define I irop_flag_ip_cfopcode
601 #define F irop_flag_fragile
602 #define Y irop_flag_forking
603 #define H irop_flag_highlevel
604 #define c irop_flag_constlike
605 #define K irop_flag_keep
606 #define M irop_flag_machine
607 #define O irop_flag_machine_op
608 #define R (irop_flag_user << 0)
612 static int run_once = 0;
618 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
620 $arch\_opcode_start = cur_opcode;
623 print OUT @obst_new_irop;
625 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
626 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
627 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
633 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
635 my $creation_time = localtime(time());
641 * \@brief Function prototypes for the new opcode functions.
642 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
643 * Edit $specfile instead.
644 * created by: $0 $specfile $target_dir
645 * \@date $creation_time
647 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
648 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
652 print OUT @obst_enum_op;
653 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
654 print OUT "int get_$arch\_opcode_first(void);\n";
655 print OUT "int get_$arch\_opcode_last(void);\n";
656 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
657 print OUT @obst_header;
658 print OUT @obst_proj;
667 # Translates numeric arity into string constant.
669 sub translate_arity {
672 if ($arity =~ /^\d+$/) {
674 return "oparity_zero";
676 elsif ($arity == 1) {
677 return "oparity_unary";
679 elsif ($arity == 2) {
680 return "oparity_binary";
682 elsif ($arity == 3) {
683 return "oparity_trinary";
686 return "oparity_any";
688 } elsif ($arity == $ARITY_VARIABLE) {
689 return "oparity_variable";
690 } elsif ($arity == $ARITY_DYNAMIC) {
691 return "oparity_dynamic";
693 die "Unknown arity $arity";
698 # Return the list of pointers for the given execution units.
700 sub gen_execunit_list_initializer {
702 my $uc_arch = uc($arch);
707 foreach my $unit (@{ $units }) {
708 if ($unit eq "DUMMY") {
709 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
711 elsif (exists($cpu{"$unit"})) {
712 # operation can be executed on all units of this type
714 my $tp_name = "$arch\_execution_units_$unit";
716 foreach (@{ $cpu{"$unit"} }) {
717 next if ($idx++ == 0); # skip first element (it's not a unit)
718 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
719 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
723 # operation can be executed only a certain unit
724 # -> find corresponding unit type
726 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
727 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
728 if ($unit eq $cur_unit) {
729 my $tp_name = "$arch\_execution_units_$cur_type";
730 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
731 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
739 print STDERR "Invalid execution unit $unit specified!\n";
744 # prepare the 2-dim array init
745 foreach my $key (keys(%init)) {
746 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
748 foreach (@{ $init{"$key"} }) {
751 $ret .= "\t\tNULL\n";
753 $ret2 .= "\t\tallowed_units_$key,\n";
755 $ret2 .= "\t\tNULL\n";
757 $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";