3 # This script generates the C code which creates the irop's and
4 # their coresponding node constructors for all operations in a given spec
5 # so they can be used as normal firm nodes.
12 my $specfile = $ARGV[0];
13 my $target_dir = $ARGV[1];
19 our $additional_opcodes;
28 unless ($return = do $specfile) {
29 warn "couldn't parse $specfile: $@" if $@;
30 warn "couldn't do $specfile: $!" unless defined $return;
31 warn "couldn't run $specfile" unless $return;
35 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
36 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
38 #print Dumper(%nodes);
39 #print Dumper(%operands);
41 # create c code file from specs
43 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
44 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
45 my @obst_constructor; # stack for node constructor functions
46 my @obst_new_irop; # stack for the new_ir_op calls
47 my @obst_enum_op; # stack for creating the <arch>_opcode enum
48 my @obst_header; # stack for function prototypes
49 my @obst_is_archirn; # stack for the is_$arch_irn() function
50 my @obst_cmp_attr; # stack for the compare attribute functions
51 my @obst_proj; # stack for the pn_ numbers
56 my $n_opcodes = 0; # number of opcodes
58 # for registering additional opcodes
59 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
61 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
63 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
64 foreach my $op (keys(%nodes)) {
65 my %n = %{ $nodes{"$op"} };
71 # determine arity from in requirements
72 $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
73 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
74 $arity = scalar(@{ $n{"reg_req"}{"in"} });
81 # define some proj numbers
82 if (exists($n{"outs"})) {
85 @outs = @{ $n{"outs"} };
86 $num_outs = $#outs + 1;
88 push(@obst_proj, "\nenum pn_$op {\n");
90 for (my $idx = 0; $idx <= $#outs; $idx++) {
91 # check, if we have additional flags annotated to out
92 if ($outs[$idx] =~ /:(S|I(\|(S|I))*)/) {
94 $outs[$idx] =~ s/:(S|I(\|(S|I))*)//;
96 push(@obst_proj, " pn_$op\_".$outs[$idx]." = $idx,\n");
99 push(@obst_proj, "};\n");
103 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
104 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
105 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
107 push(@obst_is_archirn, "is_$op(node)");
109 push(@obst_header, "extern ir_op *op_$op;\n");
110 push(@obst_header, "ir_op *get_op_$op(void);\n");
111 push(@obst_header, "int is_$op(const ir_node *n);\n");
114 # create compare attribute function if needed
115 if (exists($n{"cmp_attr"})) {
116 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
117 push(@obst_cmp_attr, " $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
118 push(@obst_cmp_attr, " $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
119 push(@obst_cmp_attr, " (void) attr_a;\n");
120 push(@obst_cmp_attr, " (void) attr_b;\n");
121 push(@obst_cmp_attr, $n{"cmp_attr"});
122 push(@obst_cmp_attr, "}\n\n");
127 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
128 # we explicitly skip the constructor if the specification entry says NONE
131 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
132 $n{"comment"} =~ s/^"|"$//g; # remove "
133 $n{"comment"} = "/* ".$n{"comment"}." */\n";
134 push(@obst_constructor, $n{"comment"});
136 # create constructor head
137 my $complete_args = "";
141 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
142 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
143 if ($arity !~ /^\d+$/) {
144 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
147 for (my $i = 1; $i <= $arity; $i++) {
148 $complete_args .= ", ir_node *op".$i;
149 $arg_names .= ", op".$i;
152 $complete_args .= ", ir_mode *mode";
153 $arg_names .= ", mode";
156 else { # user defined args
157 for my $href (@{ $n{"args"} }) {
158 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
159 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
160 $arg_names .= ", ".$href->{"name"};
164 # we have additional attribute arguements
165 if (exists($n{"attr"})) {
166 $complete_args .= ", ".$n{"attr"};
169 # $complete_args = substr($complete_args, 2);
170 $temp .= "$complete_args)";
171 push(@obst_constructor, $temp." {\n");
172 push(@obst_header, $n{"comment"});
173 push(@obst_header, $temp.";\n");
175 # emit constructor code
176 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
177 if ($arity !~ /^\d+$/) {
178 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
182 $temp = " ir_node *res;\n";
183 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
184 $temp .= " int flags = 0;\n";
185 $temp .= " $arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
187 my $exec_units = "NULL";
188 # set up static variables for cpu execution unit assigments
189 if (exists($n{"units"})) {
190 $temp .= gen_execunit_list_initializer($n{"units"});
191 $exec_units = "_exec_units";
194 undef my $in_req_var;
195 undef my $out_req_var;
197 # set up static variables for requirements and registers
198 if (exists($n{"reg_req"})) {
199 my %req = %{ $n{"reg_req"} };
203 @in = @{ $req{"in"} } if (exists($req{"in"}));
205 @out = @{ $req{"out"} } if exists(($req{"out"}));
208 $in_req_var = "_in_req_$op";
209 $temp .= " static const $arch\_register_req_t *".$in_req_var."[] =\n {\n";
210 for ($idx = 0; $idx <= $#in; $idx++) {
211 $temp .= " ".$op."_reg_req_in_".$idx.",\n";
217 $out_req_var = "_out_req_$op";
219 $temp .= " static const $arch\_register_req_t *".$out_req_var."[] =\n {\n";
220 for ($idx = 0; $idx <= $#out; $idx++) {
221 $temp .= " ".$op."_reg_req_out_".$idx.",\n";
228 $temp .= " if (!op_$op) {\n";
229 $temp .= " assert(0);\n";
230 $temp .= " return NULL;\n";
232 for (my $i = 1; $i <= $arity; $i++) {
233 $temp .= " in[".($i - 1)."] = op".$i.";\n";
237 if (exists($n{"irn_flags"})) {
238 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
240 $temp .= " flags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
242 elsif ($flag eq "N") {
243 $temp .= " flags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
245 elsif ($flag eq "I") {
246 $temp .= " flags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
248 elsif ($flag eq "S") {
249 $temp .= " flags |= arch_irn_flags_modify_sp; /* op modifies stack pointer */\n";
256 # allocate memory and set pointer to register requirements
257 if (exists($n{"reg_req"})) {
258 my %req = %{ $n{"reg_req"} };
261 @in = @{ $req{"in"} } if (exists($req{"in"}));
263 @out = @{ $req{"out"} } if exists(($req{"out"}));
266 $in_param = $in_req_var;
274 $out_param = "$out_req_var, $exec_units, $n_res";
277 $out_param = "NULL, $exec_units, 0";
282 $out_param = "NULL, $exec_units, 0";
284 $temp .= "\n /* create node */\n";
287 if (exists($n{"latency"})) {
288 $latency = $n{"latency"};
295 $temp .= " res = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
297 $temp .= "\n /* init node attributes */\n";
298 $temp .= " init_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
301 if ($#out_flags >= 0) {
302 $temp .= "\n /* set flags for outs */\n";
303 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
307 foreach my $flag (split(/\|/, $out_flags[$idx])) {
309 $flags .= $prefix."arch_irn_flags_ignore";
312 elsif ($flag eq "S") {
313 $flags .= $prefix."arch_irn_flags_modify_sp";
318 $temp .= " set_$arch\_out_flags(res, $flags, $idx);\n";
323 if (exists($n{"init_attr"})) {
324 $temp .= " attr = get_$arch\_attr(res);\n";
325 $temp .= $n{"init_attr"}."\n";
328 $temp .= "\n /* optimize node */\n";
329 $temp .= " res = optimize_node(res);\n";
330 $temp .= " irn_vrfy_irg(res, irg);\n\n";
332 $temp .= "\n return res;\n";
334 push(@obst_constructor, $temp);
336 else { # user defined constructor
337 push(@obst_constructor, $n{"rd_constructor"});
340 # close constructor function
341 push(@obst_constructor, "}\n\n");
342 } # constructor creation
344 # set default values for state and flags if not given
345 $n{"state"} = "floats" if (! exists($n{"state"}));
346 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
349 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
350 push(@obst_new_irop, " ops.dump_node = $arch\_dump_node;\n");
352 if ($cmp_attr_func) {
353 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
357 $temp = " op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
358 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
359 push(@obst_new_irop, $temp);
360 push(@obst_new_irop, " set_op_tag(op_$op, &$arch\_op_tag);\n");
361 push(@obst_enum_op, " iro_$op,\n");
363 push(@obst_header, "\n");
365 push(@obst_enum_op, " iro_$arch\_last_generated,\n");
366 push(@obst_enum_op, " iro_$arch\_last = iro_$arch\_last_generated");
367 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
368 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
372 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
374 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
375 print OUT @obst_cmp_attr;
377 print OUT @obst_opvar;
379 print OUT @obst_get_opvar;
384 static int $arch\_opcode_start = -1;
385 static int $arch\_opcode_end = -1;
389 # build the FOURCC arguments from $arch
391 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
393 if (length($arch) >= 1) {
394 $a = uc(substr($arch, 0, 1));
397 if (length($arch) >= 2) {
398 $b = uc(substr($arch, 1, 1));
401 if (length($arch) >= 3) {
402 $c = uc(substr($arch, 2, 1));
405 if (length($arch) >= 4) {
406 $d = uc(substr($arch, 3, 1));
409 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
411 print OUT<<ENDOFISIRN;
413 /** Return the opcode number of the first $arch opcode. */
414 int get_$arch\_opcode_first(void) {
415 return $arch\_opcode_start;
418 /** Return the opcode number of the last $arch opcode + 1. */
419 int get_$arch\_opcode_last(void) {
420 return $arch\_opcode_end;
423 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
424 int is_$arch\_irn(const ir_node *node) {
425 return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
428 int get_$arch\_irn_opcode(const ir_node *node) {
429 if (is_$arch\_irn(node))
430 return get_irn_opcode(node) - $arch\_opcode_start;
436 print OUT @obst_constructor;
438 print OUT<<ENDOFMAIN;
440 * Creates the $arch specific Firm machine operations
441 * needed for the assembler irgs.
443 void $arch\_create_opcodes(void) {
444 #define N irop_flag_none
445 #define L irop_flag_labeled
446 #define C irop_flag_commutative
447 #define X irop_flag_cfopcode
448 #define I irop_flag_ip_cfopcode
449 #define F irop_flag_fragile
450 #define Y irop_flag_forking
451 #define H irop_flag_highlevel
452 #define c irop_flag_constlike
453 #define K irop_flag_keep
454 #define M irop_flag_machine
455 #define O irop_flag_machine_op
456 #define R (irop_flag_user << 0)
459 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
461 $arch\_opcode_start = cur_opcode;
464 print OUT @obst_new_irop;
466 print OUT " $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
467 print OUT " $arch\_opcode_end = cur_opcode + iro_$arch\_last";
468 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
474 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
476 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
477 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
478 print OUT @obst_enum_op;
479 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
480 print OUT "int get_$arch\_opcode_first(void);\n";
481 print OUT "int get_$arch\_opcode_last(void);\n";
482 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
483 print OUT @obst_header;
484 print OUT @obst_proj;
485 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
490 # Translates numeric arity into string constant.
492 sub translate_arity {
495 if ($arity =~ /^\d+$/) {
497 return "oparity_zero";
499 elsif ($arity == 1) {
500 return "oparity_unary";
502 elsif ($arity == 2) {
503 return "oparity_binary";
505 elsif ($arity == 3) {
506 return "oparity_trinary";
513 return "oparity_".$arity;
518 # Return the list of pointers for the given execution units.
520 sub gen_execunit_list_initializer {
522 my $uc_arch = uc($arch);
527 foreach my $unit (@{ $units }) {
528 if (exists($cpu{"$unit"})) {
529 # operation can be executed on all units of this type
531 my $tp_name = "$arch\_execution_units_$unit";
532 foreach (@{ $cpu{"$unit"} }) {
533 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
534 push(@{ $init{"$unit"} }, " &".$tp_name."[".$unit_name."]");
538 # operation can be executed only a certain unit
539 # -> find corresponding unit type
541 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
542 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
543 if ($unit eq $cur_unit) {
544 my $tp_name = "$arch\_execution_units_$cur_type";
545 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
546 push(@{ $init{"$unit"} }, " &".$tp_name."[".$unit_name."]");
554 print STDERR "Invalid execution unit $unit specified!\n";
559 # prepare the 2-dim array init
560 foreach my $key (keys(%init)) {
561 $ret .= " static const be_execution_unit_t *_allowed_units_".$key."[] =\n {\n";
562 foreach (@{ $init{"$key"} }) {
567 $ret2 .= " _allowed_units_$key,\n";
571 $ret .= " static const be_execution_unit_t **_exec_units[] =\n {\n".$ret2." };\n";