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;
22 our $default_cmp_attr;
29 unless ($return = do $specfile) {
30 warn "couldn't parse $specfile: $@" if $@;
31 warn "couldn't do $specfile: $!" unless defined $return;
32 warn "couldn't run $specfile" unless $return;
36 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
37 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
39 #print Dumper(%nodes);
40 #print Dumper(%operands);
42 # create c code file from specs
44 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
45 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
46 my @obst_constructor; # stack for node constructor functions
47 my @obst_new_irop; # stack for the new_ir_op calls
48 my @obst_enum_op; # stack for creating the <arch>_opcode enum
49 my @obst_header; # stack for function prototypes
50 my @obst_is_archirn; # stack for the is_$arch_irn() function
51 my @obst_cmp_attr; # stack for the compare attribute functions
52 my @obst_proj; # stack for the pn_ numbers
57 my $n_opcodes = 0; # number of opcodes
59 # for registering additional opcodes
60 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
62 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
64 # create default compare function
65 if(defined($default_cmp_attr)) {
66 my $cmpcode = $default_cmp_attr;
67 push(@obst_cmp_attr, "static int default_cmp_attr(ir_node *a, ir_node *b) {\n");
68 if($cmpcode =~ m/attr_a/) {
69 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
71 if($cmpcode =~ m/attr_b/) {
72 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
74 push(@obst_cmp_attr, "\t${cmpcode}\n");
75 push(@obst_cmp_attr, "}\n\n");
78 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
79 foreach my $op (keys(%nodes)) {
80 my %n = %{ $nodes{"$op"} };
86 # determine arity from in requirements
87 $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
88 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
89 $arity = scalar(@{ $n{"reg_req"}{"in"} });
96 # define some proj numbers
97 if (exists($n{"outs"})) {
100 @outs = @{ $n{"outs"} };
101 $num_outs = $#outs + 1;
103 push(@obst_proj, "\nenum pn_$op {\n");
105 for (my $idx = 0; $idx <= $#outs; $idx++) {
106 # check, if we have additional flags annotated to out
107 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
108 push(@out_flags, $1);
109 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
111 push(@obst_proj, " pn_$op\_".$outs[$idx]." = $idx,\n");
114 push(@obst_proj, "};\n");
115 $known_mode = "mode_T";
117 if (exists($n{"mode"})) {
118 $known_mode = $n{"mode"};
121 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
122 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
123 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
125 push(@obst_is_archirn, "is_$op(node)");
127 push(@obst_header, "extern ir_op *op_$op;\n");
128 push(@obst_header, "ir_op *get_op_$op(void);\n");
129 push(@obst_header, "int is_$op(const ir_node *n);\n");
132 if(defined($default_cmp_attr)) {
133 $cmp_attr_func = "default_cmp_attr";
135 # create compare attribute function if needed
136 if (exists($n{"cmp_attr"})) {
137 my $cmpcode = $n{"cmp_attr"};
139 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
140 if($cmpcode =~ m/attr_a/) {
141 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
143 if($cmpcode =~ m/attr_b/) {
144 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
146 push(@obst_cmp_attr, "\t${cmpcode}\n");
147 push(@obst_cmp_attr, "}\n\n");
149 $cmp_attr_func = "cmp_attr_${op}";
152 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
153 # we explicitly skip the constructor if the specification entry says NONE
156 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
157 $n{"comment"} =~ s/^"|"$//g; # remove "
158 $n{"comment"} = "/* ".$n{"comment"}." */\n";
159 push(@obst_constructor, $n{"comment"});
161 # create constructor head
162 my $complete_args = "";
166 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
167 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
168 if ($arity !~ /^\d+$/) {
169 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
172 for (my $i = 1; $i <= $arity; $i++) {
173 $complete_args .= ", ir_node *op".$i;
174 $arg_names .= ", op".$i;
176 if (!defined($known_mode)) {
177 $complete_args .= ", ir_mode *mode";
178 $arg_names .= ", mode";
181 else { # user defined args
182 for my $href (@{ $n{"args"} }) {
183 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
184 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
185 $arg_names .= ", ".$href->{"name"};
189 # we have additional attribute arguements
190 if (exists($n{"attr"})) {
191 $complete_args .= ", ".$n{"attr"};
194 # $complete_args = substr($complete_args, 2);
195 $temp .= "$complete_args)";
196 push(@obst_constructor, $temp." {\n");
197 push(@obst_header, $n{"comment"});
198 push(@obst_header, $temp.";\n");
200 # emit constructor code
201 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
202 if ($arity !~ /^\d+$/) {
203 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
207 $temp = "\tir_node *res;\n";
208 $temp .= "\tir_node *in[$arity];\n" if ($arity > 0);
209 $temp .= "\tint flags = 0;\n";
210 $temp .= "\t$arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
212 my $exec_units = "NULL";
213 # set up static variables for cpu execution unit assigments
214 if (exists($n{"units"})) {
215 $temp .= gen_execunit_list_initializer($n{"units"});
216 $exec_units = "_exec_units";
219 undef my $in_req_var;
220 undef my $out_req_var;
222 # set up static variables for requirements and registers
223 if (exists($n{"reg_req"})) {
224 my %req = %{ $n{"reg_req"} };
228 @in = @{ $req{"in"} } if (exists($req{"in"}));
230 @out = @{ $req{"out"} } if exists(($req{"out"}));
233 $in_req_var = "_in_req_$op";
234 $temp .= "\tstatic const arch_register_req_t *".$in_req_var."[] =\n";
236 for ($idx = 0; $idx <= $#in; $idx++) {
237 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
243 $out_req_var = "_out_req_$op";
245 $temp .= "\tstatic const arch_register_req_t *".$out_req_var."[] =\n";
247 for ($idx = 0; $idx <= $#out; $idx++) {
248 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
255 $temp .= "\tassert(op_$op != NULL);\n\n";
257 for (my $i = 1; $i <= $arity; $i++) {
258 $temp .= "\tin[".($i - 1)."] = op".$i.";\n";
262 if (exists($n{"irn_flags"})) {
263 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
265 $temp .= "\tflags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
267 elsif ($flag eq "N") {
268 $temp .= "\tflags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
270 elsif ($flag eq "I") {
271 $temp .= "\tflags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
273 elsif ($flag eq "S") {
274 $temp .= "\tflags |= arch_irn_flags_modify_sp; /* op modifies stack pointer */\n";
281 # allocate memory and set pointer to register requirements
282 if (exists($n{"reg_req"})) {
283 my %req = %{ $n{"reg_req"} };
286 @in = @{ $req{"in"} } if (exists($req{"in"}));
288 @out = @{ $req{"out"} } if exists(($req{"out"}));
291 $in_param = $in_req_var;
299 $out_param = "$out_req_var, $exec_units, $n_res";
302 $out_param = "NULL, $exec_units, 0";
307 $out_param = "NULL, $exec_units, 0";
309 $temp .= "\n\t/* create node */\n";
312 if (exists($n{"latency"})) {
313 $latency = $n{"latency"};
317 if (defined($known_mode)) {
320 $temp .= "\tres = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
322 $temp .= "\n\t/* init node attributes */\n";
323 $temp .= "\tinit_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
326 if ($#out_flags >= 0) {
327 $temp .= "\n\t/* set flags for outs */\n";
328 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
332 foreach my $flag (split(/\|/, $out_flags[$idx])) {
334 $flags .= $prefix."arch_irn_flags_ignore";
337 elsif ($flag eq "S") {
338 $flags .= $prefix."arch_irn_flags_modify_sp";
343 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
348 if (exists($n{"init_attr"})) {
349 $temp .= "\tattr = get_$arch\_attr(res);\n";
350 $temp .= $n{"init_attr"}."\n";
353 $temp .= "\n\t/* optimize node */\n";
354 $temp .= "\tres = optimize_node(res);\n";
355 $temp .= "\tirn_vrfy_irg(res, irg);\n\n";
357 $temp .= "\n\treturn res;\n";
359 push(@obst_constructor, $temp);
361 else { # user defined constructor
362 push(@obst_constructor, $n{"rd_constructor"});
365 # close constructor function
366 push(@obst_constructor, "}\n\n");
367 } # constructor creation
369 # set default values for state and flags if not given
370 $n{"state"} = "floats" if (! exists($n{"state"}));
371 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
374 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
375 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
377 if (defined($cmp_attr_func)) {
378 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
382 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
383 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
384 push(@obst_new_irop, $temp);
385 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
386 push(@obst_enum_op, "\tiro_$op,\n");
388 push(@obst_header, "\n");
390 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
391 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
392 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
393 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
397 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
399 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
400 print OUT @obst_cmp_attr;
402 print OUT @obst_opvar;
404 print OUT @obst_get_opvar;
409 static int $arch\_opcode_start = -1;
410 static int $arch\_opcode_end = -1;
414 # build the FOURCC arguments from $arch
416 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
418 if (length($arch) >= 1) {
419 $a = uc(substr($arch, 0, 1));
422 if (length($arch) >= 2) {
423 $b = uc(substr($arch, 1, 1));
426 if (length($arch) >= 3) {
427 $c = uc(substr($arch, 2, 1));
430 if (length($arch) >= 4) {
431 $d = uc(substr($arch, 3, 1));
434 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
436 print OUT<<ENDOFISIRN;
438 /** Return the opcode number of the first $arch opcode. */
439 int get_$arch\_opcode_first(void) {
440 return $arch\_opcode_start;
443 /** Return the opcode number of the last $arch opcode + 1. */
444 int get_$arch\_opcode_last(void) {
445 return $arch\_opcode_end;
448 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
449 int is_$arch\_irn(const ir_node *node) {
450 return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
453 int get_$arch\_irn_opcode(const ir_node *node) {
454 if (is_$arch\_irn(node))
455 return get_irn_opcode(node) - $arch\_opcode_start;
461 print OUT @obst_constructor;
463 print OUT<<ENDOFMAIN;
465 * Creates the $arch specific Firm machine operations
466 * needed for the assembler irgs.
468 void $arch\_create_opcodes(void) {
469 #define N irop_flag_none
470 #define L irop_flag_labeled
471 #define C irop_flag_commutative
472 #define X irop_flag_cfopcode
473 #define I irop_flag_ip_cfopcode
474 #define F irop_flag_fragile
475 #define Y irop_flag_forking
476 #define H irop_flag_highlevel
477 #define c irop_flag_constlike
478 #define K irop_flag_keep
479 #define M irop_flag_machine
480 #define O irop_flag_machine_op
481 #define R (irop_flag_user << 0)
485 static int run_once = 0;
491 cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
493 $arch\_opcode_start = cur_opcode;
496 print OUT @obst_new_irop;
498 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
499 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
500 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
506 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
508 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
509 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
510 print OUT @obst_enum_op;
511 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
512 print OUT "int get_$arch\_opcode_first(void);\n";
513 print OUT "int get_$arch\_opcode_last(void);\n";
514 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
515 print OUT @obst_header;
516 print OUT @obst_proj;
517 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
522 # Translates numeric arity into string constant.
524 sub translate_arity {
527 if ($arity =~ /^\d+$/) {
529 return "oparity_zero";
531 elsif ($arity == 1) {
532 return "oparity_unary";
534 elsif ($arity == 2) {
535 return "oparity_binary";
537 elsif ($arity == 3) {
538 return "oparity_trinary";
541 return "oparity_any";
545 return "oparity_".$arity;
550 # Return the list of pointers for the given execution units.
552 sub gen_execunit_list_initializer {
554 my $uc_arch = uc($arch);
559 foreach my $unit (@{ $units }) {
560 if ($unit eq "DUMMY") {
561 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
563 elsif (exists($cpu{"$unit"})) {
564 # operation can be executed on all units of this type
566 my $tp_name = "$arch\_execution_units_$unit";
568 foreach (@{ $cpu{"$unit"} }) {
569 next if ($idx++ == 0); # skip first element (it's not a unit)
570 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
571 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
575 # operation can be executed only a certain unit
576 # -> find corresponding unit type
578 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
579 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
580 if ($unit eq $cur_unit) {
581 my $tp_name = "$arch\_execution_units_$cur_type";
582 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
583 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
591 print STDERR "Invalid execution unit $unit specified!\n";
596 # prepare the 2-dim array init
597 foreach my $key (keys(%init)) {
598 $ret .= "\tstatic const be_execution_unit_t *_allowed_units_".$key."[] =\n";
600 foreach (@{ $init{"$key"} }) {
603 $ret .= "\t\tNULL\n";
605 $ret2 .= "\t\t_allowed_units_$key,\n";
607 $ret2 .= "\t\tNULL\n";
609 $ret .= "\tstatic const be_execution_unit_t **_exec_units[] =\n";