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;
48 unless ($return = do $specfile) {
49 die "couldn't parse $specfile: $@" if $@;
50 die "couldn't do $specfile: $!" unless defined $return;
51 die "couldn't run $specfile" unless $return;
55 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
56 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
58 #print Dumper(%nodes);
59 #print Dumper(%operands);
61 # create c code file from specs
63 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
64 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
65 my @obst_constructor; # stack for node constructor functions
66 my @obst_new_irop; # stack for the new_ir_op calls
67 my @obst_enum_op; # stack for creating the <arch>_opcode enum
68 my @obst_header; # stack for function prototypes
69 my @obst_is_archirn; # stack for the is_$arch_irn() function
70 my @obst_cmp_attr; # stack for the compare attribute functions
71 my @obst_proj; # stack for the pn_ numbers
76 my $n_opcodes = 0; # number of opcodes
78 # for registering additional opcodes
79 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
81 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
83 # create default compare function
84 if(defined($default_cmp_attr)) {
85 my $cmpcode = $default_cmp_attr;
86 push(@obst_cmp_attr, "static int default_cmp_attr(ir_node *a, ir_node *b) {\n");
87 if($cmpcode =~ m/attr_a/) {
88 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
90 if($cmpcode =~ m/attr_b/) {
91 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
93 push(@obst_cmp_attr, "\t${cmpcode}\n");
94 push(@obst_cmp_attr, "}\n\n");
97 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
98 foreach my $op (keys(%nodes)) {
99 my %n = %{ $nodes{"$op"} };
105 # determine arity from in requirements
106 $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
107 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
108 $arity = scalar(@{ $n{"reg_req"}{"in"} });
115 # define some proj numbers
116 if (exists($n{"outs"})) {
119 @outs = @{ $n{"outs"} };
120 $num_outs = $#outs + 1;
122 push(@obst_proj, "\nenum pn_$op {\n");
124 for (my $idx = 0; $idx <= $#outs; $idx++) {
125 # check, if we have additional flags annotated to out
126 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
127 push(@out_flags, $1);
128 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
130 push(@obst_proj, " pn_$op\_".$outs[$idx]." = $idx,\n");
133 push(@obst_proj, "};\n");
134 $known_mode = "mode_T";
136 if (exists($n{"mode"})) {
137 $known_mode = $n{"mode"};
140 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
141 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
142 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
144 push(@obst_is_archirn, "is_$op(node)");
146 push(@obst_header, "extern ir_op *op_$op;\n");
147 push(@obst_header, "ir_op *get_op_$op(void);\n");
148 push(@obst_header, "int is_$op(const ir_node *n);\n");
151 if(defined($default_cmp_attr)) {
152 $cmp_attr_func = "default_cmp_attr";
154 # create compare attribute function if needed
155 if (exists($n{"cmp_attr"})) {
156 my $cmpcode = $n{"cmp_attr"};
158 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
159 if($cmpcode =~ m/attr_a/) {
160 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
162 if($cmpcode =~ m/attr_b/) {
163 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
165 push(@obst_cmp_attr, "\t${cmpcode}\n");
166 push(@obst_cmp_attr, "}\n\n");
168 $cmp_attr_func = "cmp_attr_${op}";
171 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
172 # we explicitly skip the constructor if the specification entry says NONE
175 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
176 $n{"comment"} =~ s/^"|"$//g; # remove "
177 $n{"comment"} = "/* ".$n{"comment"}." */\n";
178 push(@obst_constructor, $n{"comment"});
180 # create constructor head
181 my $complete_args = "";
185 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
186 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
187 if ($arity !~ /^\d+$/) {
188 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
191 for (my $i = 1; $i <= $arity; $i++) {
192 $complete_args .= ", ir_node *op".$i;
193 $arg_names .= ", op".$i;
195 if (!defined($known_mode)) {
196 $complete_args .= ", ir_mode *mode";
197 $arg_names .= ", mode";
200 else { # user defined args
201 for my $href (@{ $n{"args"} }) {
202 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
203 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
204 $arg_names .= ", ".$href->{"name"};
208 # we have additional attribute arguements
209 if (exists($n{"attr"})) {
210 $complete_args .= ", ".$n{"attr"};
213 # $complete_args = substr($complete_args, 2);
214 $temp .= "$complete_args)";
215 push(@obst_constructor, $temp." {\n");
216 push(@obst_header, $n{"comment"});
217 push(@obst_header, $temp.";\n");
219 # emit constructor code
220 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
221 if ($arity !~ /^\d+$/) {
222 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
226 $temp = "\tir_node *res;\n";
227 $temp .= "\tir_node *in[$arity];\n" if ($arity > 0);
228 $temp .= "\tint flags = 0;\n";
229 $temp .= "\t$arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
231 my $exec_units = "NULL";
232 # set up static variables for cpu execution unit assigments
233 if (exists($n{"units"})) {
234 $temp .= gen_execunit_list_initializer($n{"units"});
235 $exec_units = "_exec_units";
238 undef my $in_req_var;
239 undef my $out_req_var;
241 # set up static variables for requirements and registers
242 if (exists($n{"reg_req"})) {
243 my %req = %{ $n{"reg_req"} };
247 @in = @{ $req{"in"} } if (exists($req{"in"}));
249 @out = @{ $req{"out"} } if exists(($req{"out"}));
252 $in_req_var = "_in_req_$op";
253 $temp .= "\tstatic const arch_register_req_t *".$in_req_var."[] =\n";
255 for ($idx = 0; $idx <= $#in; $idx++) {
256 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
262 $out_req_var = "_out_req_$op";
264 $temp .= "\tstatic const arch_register_req_t *".$out_req_var."[] =\n";
266 for ($idx = 0; $idx <= $#out; $idx++) {
267 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
274 $temp .= "\tassert(op_$op != NULL);\n\n";
276 for (my $i = 1; $i <= $arity; $i++) {
277 $temp .= "\tin[".($i - 1)."] = op".$i.";\n";
281 if (exists($n{"irn_flags"})) {
282 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
284 $temp .= "\tflags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
286 elsif ($flag eq "N") {
287 $temp .= "\tflags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
289 elsif ($flag eq "I") {
290 $temp .= "\tflags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
292 elsif ($flag eq "S") {
293 $temp .= "\tflags |= arch_irn_flags_modify_sp; /* op modifies stack pointer */\n";
300 # allocate memory and set pointer to register requirements
301 if (exists($n{"reg_req"})) {
302 my %req = %{ $n{"reg_req"} };
305 @in = @{ $req{"in"} } if (exists($req{"in"}));
307 @out = @{ $req{"out"} } if exists(($req{"out"}));
310 $in_param = $in_req_var;
318 $out_param = "$out_req_var, $exec_units, $n_res";
321 $out_param = "NULL, $exec_units, 0";
326 $out_param = "NULL, $exec_units, 0";
328 $temp .= "\n\t/* create node */\n";
331 if (exists($n{"latency"})) {
332 $latency = $n{"latency"};
336 if (defined($known_mode)) {
339 $temp .= "\tres = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
341 $temp .= "\n\t/* init node attributes */\n";
342 $temp .= "\tinit_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
345 if ($#out_flags >= 0) {
346 $temp .= "\n\t/* set flags for outs */\n";
347 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
351 foreach my $flag (split(/\|/, $out_flags[$idx])) {
353 $flags .= $prefix."arch_irn_flags_ignore";
356 elsif ($flag eq "S") {
357 $flags .= $prefix."arch_irn_flags_modify_sp";
362 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
367 if (exists($n{"init_attr"})) {
368 $temp .= "\tattr = get_$arch\_attr(res);\n";
369 $temp .= $n{"init_attr"}."\n";
372 $temp .= "\n\t/* optimize node */\n";
373 $temp .= "\tres = optimize_node(res);\n";
374 $temp .= "\tirn_vrfy_irg(res, irg);\n\n";
376 $temp .= "\n\treturn res;\n";
378 push(@obst_constructor, $temp);
380 else { # user defined constructor
381 push(@obst_constructor, $n{"rd_constructor"});
384 # close constructor function
385 push(@obst_constructor, "}\n\n");
386 } # constructor creation
388 # set default values for state and flags if not given
389 $n{"state"} = "floats" if (! exists($n{"state"}));
390 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
393 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
394 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
396 if (defined($cmp_attr_func)) {
397 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
401 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
402 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
403 push(@obst_new_irop, $temp);
404 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
405 push(@obst_enum_op, "\tiro_$op,\n");
407 push(@obst_header, "\n");
409 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
410 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
411 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
412 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
416 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
418 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
419 print OUT @obst_cmp_attr;
421 print OUT @obst_opvar;
423 print OUT @obst_get_opvar;
428 static int $arch\_opcode_start = -1;
429 static int $arch\_opcode_end = -1;
433 # build the FOURCC arguments from $arch
435 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
437 if (length($arch) >= 1) {
438 $a = uc(substr($arch, 0, 1));
441 if (length($arch) >= 2) {
442 $b = uc(substr($arch, 1, 1));
445 if (length($arch) >= 3) {
446 $c = uc(substr($arch, 2, 1));
449 if (length($arch) >= 4) {
450 $d = uc(substr($arch, 3, 1));
453 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
455 print OUT<<ENDOFISIRN;
457 /** Return the opcode number of the first $arch opcode. */
458 int get_$arch\_opcode_first(void) {
459 return $arch\_opcode_start;
462 /** Return the opcode number of the last $arch opcode + 1. */
463 int get_$arch\_opcode_last(void) {
464 return $arch\_opcode_end;
467 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
468 int is_$arch\_irn(const ir_node *node) {
469 return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
472 int get_$arch\_irn_opcode(const ir_node *node) {
473 if (is_$arch\_irn(node))
474 return get_irn_opcode(node) - $arch\_opcode_start;
480 print OUT @obst_constructor;
482 print OUT<<ENDOFMAIN;
484 * Creates the $arch specific Firm machine operations
485 * needed for the assembler irgs.
487 void $arch\_create_opcodes(void) {
488 #define N irop_flag_none
489 #define L irop_flag_labeled
490 #define C irop_flag_commutative
491 #define X irop_flag_cfopcode
492 #define I irop_flag_ip_cfopcode
493 #define F irop_flag_fragile
494 #define Y irop_flag_forking
495 #define H irop_flag_highlevel
496 #define c irop_flag_constlike
497 #define K irop_flag_keep
498 #define M irop_flag_machine
499 #define O irop_flag_machine_op
500 #define R (irop_flag_user << 0)
504 static int run_once = 0;
510 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
512 $arch\_opcode_start = cur_opcode;
515 print OUT @obst_new_irop;
517 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
518 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
519 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
525 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
527 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
528 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
529 print OUT @obst_enum_op;
530 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
531 print OUT "int get_$arch\_opcode_first(void);\n";
532 print OUT "int get_$arch\_opcode_last(void);\n";
533 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
534 print OUT @obst_header;
535 print OUT @obst_proj;
536 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
541 # Translates numeric arity into string constant.
543 sub translate_arity {
546 if ($arity =~ /^\d+$/) {
548 return "oparity_zero";
550 elsif ($arity == 1) {
551 return "oparity_unary";
553 elsif ($arity == 2) {
554 return "oparity_binary";
556 elsif ($arity == 3) {
557 return "oparity_trinary";
560 return "oparity_any";
564 return "oparity_".$arity;
569 # Return the list of pointers for the given execution units.
571 sub gen_execunit_list_initializer {
573 my $uc_arch = uc($arch);
578 foreach my $unit (@{ $units }) {
579 if ($unit eq "DUMMY") {
580 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
582 elsif (exists($cpu{"$unit"})) {
583 # operation can be executed on all units of this type
585 my $tp_name = "$arch\_execution_units_$unit";
587 foreach (@{ $cpu{"$unit"} }) {
588 next if ($idx++ == 0); # skip first element (it's not a unit)
589 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
590 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
594 # operation can be executed only a certain unit
595 # -> find corresponding unit type
597 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
598 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
599 if ($unit eq $cur_unit) {
600 my $tp_name = "$arch\_execution_units_$cur_type";
601 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
602 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
610 print STDERR "Invalid execution unit $unit specified!\n";
615 # prepare the 2-dim array init
616 foreach my $key (keys(%init)) {
617 $ret .= "\tstatic const be_execution_unit_t *_allowed_units_".$key."[] =\n";
619 foreach (@{ $init{"$key"} }) {
622 $ret .= "\t\tNULL\n";
624 $ret2 .= "\t\t_allowed_units_$key,\n";
626 $ret2 .= "\t\tNULL\n";
628 $ret .= "\tstatic const be_execution_unit_t **_exec_units[] =\n";