fix backend script generating wrong oparity
[libfirm] / ir / be / scripts / generate_new_opcodes.pl
1 #!/usr/bin/perl -w
2
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.
6 # Creation: 2005/10/19
7 # $Id$
8
9 use strict;
10 use Data::Dumper;
11
12 my $specfile   = $ARGV[0];
13 my $target_dir = $ARGV[1];
14 my $state      = 1;
15 my $cur_op     = "";
16 my $line_nr    = 0;
17
18 our $arch;
19 our $additional_opcodes;
20 our %nodes;
21 our %cpu;
22 our $default_cmp_attr;
23
24 # include spec file
25
26 my $return;
27
28 no strict "subs";
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;
33 }
34 use strict "subs";
35
36 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
37 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
38
39 #print Dumper(%nodes);
40 #print Dumper(%operands);
41
42 # create c code file from specs
43
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
53 my $orig_op;
54 my $arity;
55 my $cmp_attr_func;
56 my $temp;
57 my $n_opcodes = 0;    # number of opcodes
58
59 # for registering additional opcodes
60 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
61
62 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
63
64 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
65 foreach my $op (keys(%nodes)) {
66         my %n        = %{ $nodes{"$op"} };
67         my $known_mode;
68         my $n_res    = 0;
69         my $num_outs = 0;
70         my @out_flags;
71
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"} });
76         }
77
78         $orig_op = $op;
79         $op      = $arch."_".$op;
80         $temp    = "";
81
82         # define some proj numbers
83         if (exists($n{"outs"})) {
84                 undef my @outs;
85
86                 @outs     = @{ $n{"outs"} };
87                 $num_outs = $#outs + 1;
88
89                 push(@obst_proj, "\nenum pn_$op {\n");
90
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))*)/) {
94                                 push(@out_flags, $1);
95                                 $outs[$idx] =~ s/:(S|I(\|(S|I))*)//;
96                         }
97                         push(@obst_proj, "  pn_$op\_".$outs[$idx]." = $idx,\n");
98                 }
99
100                 push(@obst_proj, "};\n");
101                 $known_mode = "mode_T";
102         }
103         if (exists($n{"mode"})) {
104                 $known_mode = $n{"mode"};
105         }
106
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");
110
111         push(@obst_is_archirn, "is_$op(node)");
112
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");
116
117         $cmp_attr_func = 0;
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");
127                 } else {
128                         push(@obst_cmp_attr, "\t$default_cmp_attr\n");
129                 }
130                 push(@obst_cmp_attr, "}\n\n");
131
132                 $cmp_attr_func = 1;
133         }
134
135         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
136                 # we explicitly skip the constructor if the specification entry says NONE
137         }
138         else {
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"});
143
144                 # create constructor head
145                 my $complete_args = "";
146                 my $arg_names     = "";
147                 $temp             = "";
148
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";
153                                 next;
154                         }
155                         for (my $i = 1; $i <= $arity; $i++) {
156                                 $complete_args .= ", ir_node *op".$i;
157                                 $arg_names     .= ", op".$i;
158                         }
159                         if (!defined($known_mode)) {
160                                 $complete_args .= ", ir_mode *mode";
161                                 $arg_names     .= ", mode";
162                         }
163                 }
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"};
169                         }
170                 }
171
172                 # we have additional attribute arguements
173                 if (exists($n{"attr"})) {
174                         $complete_args .= ", ".$n{"attr"};
175                 }
176
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");
182
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";
187                                 next;
188                         }
189
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"}));
194
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";
200                         }
201
202                         undef my $in_req_var;
203                         undef my $out_req_var;
204
205                         # set up static variables for requirements and registers
206                         if (exists($n{"reg_req"})) {
207                                 my %req = %{ $n{"reg_req"} };
208                                 my $idx;
209
210                                 undef my @in;
211                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
212                                 undef my @out;
213                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
214
215                                 if (@in) {
216                                         $in_req_var = "_in_req_$op";
217                                         $temp .= "\tstatic const $arch\_register_req_t *".$in_req_var."[] =\n";
218                                         $temp .= "\t{\n";
219                                         for ($idx = 0; $idx <= $#in; $idx++) {
220                                                 $temp .= "\t\t".$op."_reg_req_in_".$idx.",\n";
221                                         }
222                                         $temp .= "\t};\n";
223                                 }
224
225                                 if (@out) {
226                                         $out_req_var = "_out_req_$op";
227
228                                         $temp .= "\tstatic const $arch\_register_req_t *".$out_req_var."[] =\n";
229                                         $temp .= "\t{\n";
230                                         for ($idx = 0; $idx <= $#out; $idx++) {
231                                                 $temp .= "\t\t".$op."_reg_req_out_".$idx.",\n";
232                                         }
233                                         $temp .= "\t};\n";
234                                 }
235                         }
236
237                         $temp .= "\n";
238                         $temp .= "\tif (!op_$op) {\n";
239                         $temp .= "\t\tassert(0);\n";
240                         $temp .= "\t\treturn NULL;\n";
241                         $temp .= "\t}\n\n";
242                         for (my $i = 1; $i <= $arity; $i++) {
243                                 $temp .= "\tin[".($i - 1)."] = op".$i.";\n";
244                         }
245
246                         # set flags
247                         if (exists($n{"irn_flags"})) {
248                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
249                                         if ($flag eq "R") {
250                                                 $temp .= "\tflags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */\n";
251                                         }
252                                         elsif ($flag eq "N") {
253                                                 $temp .= "\tflags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
254                                         }
255                                         elsif ($flag eq "I") {
256                                                 $temp .= "\tflags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
257                                         }
258                                         elsif ($flag eq "S") {
259                                                 $temp .= "\tflags |= arch_irn_flags_modify_sp;          /* op modifies stack pointer */\n";
260                                         }
261                                 }
262                         }
263
264                         my $in_param;
265                         my $out_param;
266                         # allocate memory and set pointer to register requirements
267                         if (exists($n{"reg_req"})) {
268                                 my %req = %{ $n{"reg_req"} };
269
270                                 undef my @in;
271                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
272                                 undef my @out;
273                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
274
275                                 if (@in) {
276                                         $in_param = $in_req_var;
277                                 }
278                                 else {
279                                         $in_param = "NULL";
280                                 }
281
282                                 if (@out) {
283                                         $n_res     = $#out + 1;
284                                         $out_param = "$out_req_var, $exec_units, $n_res";
285                                 }
286                                 else {
287                                         $out_param = "NULL, $exec_units, 0";
288                                 }
289                         }
290                         else {
291                                 $in_param  = "NULL";
292                                 $out_param = "NULL, $exec_units, 0";
293                         }
294                         $temp .= "\n\t/* create node */\n";
295
296                         my $latency = 1;
297                         if (exists($n{"latency"})) {
298                                 $latency = $n{"latency"};
299                         }
300
301                         my $mode = "mode";
302                         if (defined($known_mode)) {
303                                 $mode = $known_mode;
304                         }
305                         $temp .= "\tres = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
306
307                         $temp .= "\n\t/* init node attributes */\n";
308                         $temp .= "\tinit_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
309
310                         # set flags for outs
311                         if ($#out_flags >= 0) {
312                                 $temp .= "\n\t/* set flags for outs */\n";
313                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
314                                         my $flags  = "";
315                                         my $prefix = "";
316
317                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
318                                                 if ($flag eq "I") {
319                                                         $flags .= $prefix."arch_irn_flags_ignore";
320                                                         $prefix = " | ";
321                                                 }
322                                                 elsif ($flag eq "S") {
323                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
324                                                         $prefix = " | ";
325                                                 }
326                                         }
327
328                                         $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
329                                 }
330                         }
331
332
333                         if (exists($n{"init_attr"})) {
334                                 $temp .= "\tattr = get_$arch\_attr(res);\n";
335                                 $temp .= $n{"init_attr"}."\n";
336                         }
337
338                         $temp .= "\n\t/* optimize node */\n";
339                         $temp .= "\tres = optimize_node(res);\n";
340                         $temp .= "\tirn_vrfy_irg(res, irg);\n\n";
341
342                         $temp .= "\n\treturn res;\n";
343
344                         push(@obst_constructor, $temp);
345                 }
346                 else { # user defined constructor
347                         push(@obst_constructor, $n{"rd_constructor"});
348                 }
349
350                 # close constructor function
351                 push(@obst_constructor, "}\n\n");
352         } # constructor creation
353
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"}));
357
358
359         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
360         push(@obst_new_irop, "\tops.dump_node     = $arch\_dump_node;\n");
361
362         if ($cmp_attr_func) {
363                 push(@obst_new_irop, "\tops.node_cmp_attr = cmp_attr_$op;\n");
364         }
365
366         $n_opcodes++;
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");
372
373         push(@obst_header, "\n");
374 }
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");
379
380 # emit the code
381
382 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
383
384 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
385 print OUT @obst_cmp_attr;
386 print OUT "\n";
387 print OUT @obst_opvar;
388 print OUT "\n";
389 print OUT @obst_get_opvar;
390 print OUT "\n";
391
392 print OUT<<EOF;
393
394 static int $arch\_opcode_start = -1;
395 static int $arch\_opcode_end   = -1;
396
397 EOF
398
399 # build the FOURCC arguments from $arch
400
401 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
402
403 if (length($arch) >= 1) {
404         $a = uc(substr($arch, 0, 1));
405 }
406
407 if (length($arch) >= 2) {
408         $b = uc(substr($arch, 1, 1));
409 }
410
411 if (length($arch) >= 3) {
412         $c = uc(substr($arch, 2, 1));
413 }
414
415 if (length($arch) >= 4) {
416         $d = uc(substr($arch, 3, 1));
417 }
418
419 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
420
421 print OUT<<ENDOFISIRN;
422
423 /** Return the opcode number of the first $arch opcode. */
424 int get_$arch\_opcode_first(void) {
425         return $arch\_opcode_start;
426 }
427
428 /** Return the opcode number of the last $arch opcode + 1. */
429 int get_$arch\_opcode_last(void) {
430         return $arch\_opcode_end;
431 }
432
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;
436 }
437
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;
441         return -1;
442 }
443
444 ENDOFISIRN
445
446 print OUT @obst_constructor;
447
448 print OUT<<ENDOFMAIN;
449 /**
450  * Creates the $arch specific Firm machine operations
451  * needed for the assembler irgs.
452  */
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)
467
468         ir_op_ops ops;
469         int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
470
471         $arch\_opcode_start = cur_opcode;
472 ENDOFMAIN
473
474 print OUT @obst_new_irop;
475 print OUT "\n";
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));
479 print OUT ";\n";
480 print OUT "}\n";
481
482 close(OUT);
483
484 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
485
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";
496
497 close(OUT);
498
499 ###
500 # Translates numeric arity into string constant.
501 ###
502 sub translate_arity {
503         my $arity = shift;
504
505         if ($arity =~ /^\d+$/) {
506                 if    ($arity == 0) {
507                         return "oparity_zero";
508                 }
509                 elsif ($arity == 1) {
510                         return "oparity_unary";
511                 }
512                 elsif ($arity == 2) {
513                         return "oparity_binary";
514                 }
515                 elsif ($arity == 3) {
516                         return "oparity_trinary";
517                 }
518                 else {
519                         return "oparity_any";
520                 }
521         }
522         else {
523                 return "oparity_".$arity;
524         }
525 }
526
527 ###
528 # Return the list of pointers for the given execution units.
529 ###
530 sub gen_execunit_list_initializer {
531         my $units   = shift;
532         my $uc_arch = uc($arch);
533         my $ret     = "";
534         my $ret2    = "";
535         my %init;
536
537         foreach my $unit (@{ $units }) {
538                 if ($unit eq "DUMMY") {
539                         push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
540                 }
541                 elsif (exists($cpu{"$unit"})) {
542                         # operation can be executed on all units of this type
543                         # -> add them all
544                         my $tp_name = "$arch\_execution_units_$unit";
545                         my $idx     = 0;
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."]");
550                         }
551                 }
552                 else {
553                         # operation can be executed only a certain unit
554                         # -> find corresponding unit type
555                         my $found = 0;
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."]");
562                                                 $found = 1;
563                                                 last TP_SEARCH;
564                                         }
565                                 }
566                         }
567
568                         if (! $found) {
569                                 print STDERR "Invalid execution unit $unit specified!\n";
570                         }
571                 }
572         }
573
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";
577                 $ret .= "\t{\n";
578                 foreach (@{ $init{"$key"} }) {
579                         $ret .= "$_,\n";
580                 }
581                 $ret .= "\t\tNULL\n";
582                 $ret .= "\t};\n";
583                 $ret2 .= "\t\t_allowed_units_$key,\n";
584         }
585         $ret2 .= "\t\tNULL\n";
586
587         $ret .= "\tstatic const be_execution_unit_t **_exec_units[] =\n";
588         $ret .= "\t{\n";
589         $ret .= $ret2;
590         $ret .= "\t};\n";
591
592         return $ret;
593 }