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 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
65 foreach my $op (keys(%nodes)) {
66 my %n = %{ $nodes{"$op"} };
72 # determine arity from in requirements
73 $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
74 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
75 $arity = scalar(@{ $n{"reg_req"}{"in"} });
82 # define some proj numbers
83 if (exists($n{"outs"})) {
86 @outs = @{ $n{"outs"} };
87 $num_outs = $#outs + 1;
89 push(@obst_proj, "\nenum pn_$op {\n");
91 for (my $idx = 0; $idx <= $#outs; $idx++) {
92 # check, if we have additional flags annotated to out
93 if ($outs[$idx] =~ /:(S|I(\|(S|I))*)/) {
95 $outs[$idx] =~ s/:(S|I(\|(S|I))*)//;
97 push(@obst_proj, " pn_$op\_".$outs[$idx]." = $idx,\n");
100 push(@obst_proj, "};\n");
101 $known_mode = "mode_T";
103 if (exists($n{"mode"})) {
104 $known_mode = $n{"mode"};
107 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
108 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
109 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
111 push(@obst_is_archirn, "is_$op(node)");
113 push(@obst_header, "extern ir_op *op_$op;\n");
114 push(@obst_header, "ir_op *get_op_$op(void);\n");
115 push(@obst_header, "int is_$op(const ir_node *n);\n");
118 # create compare attribute function if needed
119 if (exists($n{"cmp_attr"}) || defined($default_cmp_attr)) {
120 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
121 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
122 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
123 push(@obst_cmp_attr, "\t(void) attr_a;\n");
124 push(@obst_cmp_attr, "\t(void) attr_b;\n");
125 if(exists($n{"cmp_attr"})) {
126 push(@obst_cmp_attr, "\t".$n{"cmp_attr"}."\n");
128 push(@obst_cmp_attr, "\t$default_cmp_attr\n");
130 push(@obst_cmp_attr, "}\n\n");
135 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
136 # we explicitly skip the constructor if the specification entry says NONE
139 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
140 $n{"comment"} =~ s/^"|"$//g; # remove "
141 $n{"comment"} = "/* ".$n{"comment"}." */\n";
142 push(@obst_constructor, $n{"comment"});
144 # create constructor head
145 my $complete_args = "";
149 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
150 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
151 if ($arity !~ /^\d+$/) {
152 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
155 for (my $i = 1; $i <= $arity; $i++) {
156 $complete_args .= ", ir_node *op".$i;
157 $arg_names .= ", op".$i;
159 if (!defined($known_mode)) {
160 $complete_args .= ", ir_mode *mode";
161 $arg_names .= ", mode";
164 else { # user defined args
165 for my $href (@{ $n{"args"} }) {
166 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
167 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
168 $arg_names .= ", ".$href->{"name"};
172 # we have additional attribute arguements
173 if (exists($n{"attr"})) {
174 $complete_args .= ", ".$n{"attr"};
177 # $complete_args = substr($complete_args, 2);
178 $temp .= "$complete_args)";
179 push(@obst_constructor, $temp." {\n");
180 push(@obst_header, $n{"comment"});
181 push(@obst_header, $temp.";\n");
183 # emit constructor code
184 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
185 if ($arity !~ /^\d+$/) {
186 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
190 $temp = "\tir_node *res;\n";
191 $temp .= "\tir_node *in[$arity];\n" if ($arity > 0);
192 $temp .= "\tint flags = 0;\n";
193 $temp .= "\t$arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
195 my $exec_units = "NULL";
196 # set up static variables for cpu execution unit assigments
197 if (exists($n{"units"})) {
198 $temp .= gen_execunit_list_initializer($n{"units"});
199 $exec_units = "_exec_units";
202 undef my $in_req_var;
203 undef my $out_req_var;
205 # set up static variables for requirements and registers
206 if (exists($n{"reg_req"})) {
207 my %req = %{ $n{"reg_req"} };
211 @in = @{ $req{"in"} } if (exists($req{"in"}));
213 @out = @{ $req{"out"} } if exists(($req{"out"}));
216 $in_req_var = "_in_req_$op";
217 $temp .= "\tstatic const $arch\_register_req_t *".$in_req_var."[] =\n";
219 for ($idx = 0; $idx <= $#in; $idx++) {
220 $temp .= "\t\t".$op."_reg_req_in_".$idx.",\n";
226 $out_req_var = "_out_req_$op";
228 $temp .= "\tstatic const $arch\_register_req_t *".$out_req_var."[] =\n";
230 for ($idx = 0; $idx <= $#out; $idx++) {
231 $temp .= "\t\t".$op."_reg_req_out_".$idx.",\n";
238 $temp .= "\tif (!op_$op) {\n";
239 $temp .= "\t\tassert(0);\n";
240 $temp .= "\t\treturn NULL;\n";
242 for (my $i = 1; $i <= $arity; $i++) {
243 $temp .= "\tin[".($i - 1)."] = op".$i.";\n";
247 if (exists($n{"irn_flags"})) {
248 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
250 $temp .= "\tflags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
252 elsif ($flag eq "N") {
253 $temp .= "\tflags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
255 elsif ($flag eq "I") {
256 $temp .= "\tflags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
258 elsif ($flag eq "S") {
259 $temp .= "\tflags |= arch_irn_flags_modify_sp; /* op modifies stack pointer */\n";
266 # allocate memory and set pointer to register requirements
267 if (exists($n{"reg_req"})) {
268 my %req = %{ $n{"reg_req"} };
271 @in = @{ $req{"in"} } if (exists($req{"in"}));
273 @out = @{ $req{"out"} } if exists(($req{"out"}));
276 $in_param = $in_req_var;
284 $out_param = "$out_req_var, $exec_units, $n_res";
287 $out_param = "NULL, $exec_units, 0";
292 $out_param = "NULL, $exec_units, 0";
294 $temp .= "\n\t/* create node */\n";
297 if (exists($n{"latency"})) {
298 $latency = $n{"latency"};
302 if (defined($known_mode)) {
305 $temp .= "\tres = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
307 $temp .= "\n\t/* init node attributes */\n";
308 $temp .= "\tinit_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
311 if ($#out_flags >= 0) {
312 $temp .= "\n\t/* set flags for outs */\n";
313 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
317 foreach my $flag (split(/\|/, $out_flags[$idx])) {
319 $flags .= $prefix."arch_irn_flags_ignore";
322 elsif ($flag eq "S") {
323 $flags .= $prefix."arch_irn_flags_modify_sp";
328 $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
333 if (exists($n{"init_attr"})) {
334 $temp .= "\tattr = get_$arch\_attr(res);\n";
335 $temp .= $n{"init_attr"}."\n";
338 $temp .= "\n\t/* optimize node */\n";
339 $temp .= "\tres = optimize_node(res);\n";
340 $temp .= "\tirn_vrfy_irg(res, irg);\n\n";
342 $temp .= "\n\treturn res;\n";
344 push(@obst_constructor, $temp);
346 else { # user defined constructor
347 push(@obst_constructor, $n{"rd_constructor"});
350 # close constructor function
351 push(@obst_constructor, "}\n\n");
352 } # constructor creation
354 # set default values for state and flags if not given
355 $n{"state"} = "floats" if (! exists($n{"state"}));
356 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
359 push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
360 push(@obst_new_irop, "\tops.dump_node = $arch\_dump_node;\n");
362 if ($cmp_attr_func) {
363 push(@obst_new_irop, "\tops.node_cmp_attr = cmp_attr_$op;\n");
367 $temp = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
368 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
369 push(@obst_new_irop, $temp);
370 push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
371 push(@obst_enum_op, "\tiro_$op,\n");
373 push(@obst_header, "\n");
375 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
376 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
377 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
378 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
382 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
384 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
385 print OUT @obst_cmp_attr;
387 print OUT @obst_opvar;
389 print OUT @obst_get_opvar;
394 static int $arch\_opcode_start = -1;
395 static int $arch\_opcode_end = -1;
399 # build the FOURCC arguments from $arch
401 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
403 if (length($arch) >= 1) {
404 $a = uc(substr($arch, 0, 1));
407 if (length($arch) >= 2) {
408 $b = uc(substr($arch, 1, 1));
411 if (length($arch) >= 3) {
412 $c = uc(substr($arch, 2, 1));
415 if (length($arch) >= 4) {
416 $d = uc(substr($arch, 3, 1));
419 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
421 print OUT<<ENDOFISIRN;
423 /** Return the opcode number of the first $arch opcode. */
424 int get_$arch\_opcode_first(void) {
425 return $arch\_opcode_start;
428 /** Return the opcode number of the last $arch opcode + 1. */
429 int get_$arch\_opcode_last(void) {
430 return $arch\_opcode_end;
433 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
434 int is_$arch\_irn(const ir_node *node) {
435 return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
438 int get_$arch\_irn_opcode(const ir_node *node) {
439 if (is_$arch\_irn(node))
440 return get_irn_opcode(node) - $arch\_opcode_start;
446 print OUT @obst_constructor;
448 print OUT<<ENDOFMAIN;
450 * Creates the $arch specific Firm machine operations
451 * needed for the assembler irgs.
453 void $arch\_create_opcodes(void) {
454 #define N irop_flag_none
455 #define L irop_flag_labeled
456 #define C irop_flag_commutative
457 #define X irop_flag_cfopcode
458 #define I irop_flag_ip_cfopcode
459 #define F irop_flag_fragile
460 #define Y irop_flag_forking
461 #define H irop_flag_highlevel
462 #define c irop_flag_constlike
463 #define K irop_flag_keep
464 #define M irop_flag_machine
465 #define O irop_flag_machine_op
466 #define R (irop_flag_user << 0)
469 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
471 $arch\_opcode_start = cur_opcode;
474 print OUT @obst_new_irop;
476 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
477 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
478 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
484 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
486 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
487 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
488 print OUT @obst_enum_op;
489 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
490 print OUT "int get_$arch\_opcode_first(void);\n";
491 print OUT "int get_$arch\_opcode_last(void);\n";
492 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
493 print OUT @obst_header;
494 print OUT @obst_proj;
495 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
500 # Translates numeric arity into string constant.
502 sub translate_arity {
505 if ($arity =~ /^\d+$/) {
507 return "oparity_zero";
509 elsif ($arity == 1) {
510 return "oparity_unary";
512 elsif ($arity == 2) {
513 return "oparity_binary";
515 elsif ($arity == 3) {
516 return "oparity_trinary";
519 return "oparity_any";
523 return "oparity_".$arity;
528 # Return the list of pointers for the given execution units.
530 sub gen_execunit_list_initializer {
532 my $uc_arch = uc($arch);
537 foreach my $unit (@{ $units }) {
538 if ($unit eq "DUMMY") {
539 push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
541 elsif (exists($cpu{"$unit"})) {
542 # operation can be executed on all units of this type
544 my $tp_name = "$arch\_execution_units_$unit";
546 foreach (@{ $cpu{"$unit"} }) {
547 next if ($idx++ == 0); # skip first element (it's not a unit)
548 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
549 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
553 # operation can be executed only a certain unit
554 # -> find corresponding unit type
556 TP_SEARCH: foreach my $cur_type (keys(%cpu)) {
557 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
558 if ($unit eq $cur_unit) {
559 my $tp_name = "$arch\_execution_units_$cur_type";
560 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
561 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
569 print STDERR "Invalid execution unit $unit specified!\n";
574 # prepare the 2-dim array init
575 foreach my $key (keys(%init)) {
576 $ret .= "\tstatic const be_execution_unit_t *_allowed_units_".$key."[] =\n";
578 foreach (@{ $init{"$key"} }) {
581 $ret .= "\t\tNULL\n";
583 $ret2 .= "\t\t_allowed_units_$key,\n";
585 $ret2 .= "\t\tNULL\n";
587 $ret .= "\tstatic const be_execution_unit_t **_exec_units[] =\n";