The big committ:
[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 # 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");
70         }
71         if($cmpcode =~ m/attr_b/) {
72                 push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
73         }
74         push(@obst_cmp_attr, "\t${cmpcode}\n");
75         push(@obst_cmp_attr, "}\n\n");
76 }
77
78 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
79 foreach my $op (keys(%nodes)) {
80         my %n        = %{ $nodes{"$op"} };
81         my $known_mode;
82         my $n_res    = 0;
83         my $num_outs = 0;
84         my @out_flags;
85
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"} });
90         }
91
92         $orig_op = $op;
93         $op      = $arch."_".$op;
94         $temp    = "";
95
96         # define some proj numbers
97         if (exists($n{"outs"})) {
98                 undef my @outs;
99
100                 @outs     = @{ $n{"outs"} };
101                 $num_outs = $#outs + 1;
102
103                 push(@obst_proj, "\nenum pn_$op {\n");
104
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))*)//;
110                         }
111                         push(@obst_proj, "  pn_$op\_".$outs[$idx]." = $idx,\n");
112                 }
113
114                 push(@obst_proj, "};\n");
115                 $known_mode = "mode_T";
116         }
117         if (exists($n{"mode"})) {
118                 $known_mode = $n{"mode"};
119         }
120
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");
124
125         push(@obst_is_archirn, "is_$op(node)");
126
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");
130
131         my $cmp_attr_func;
132         if(defined($default_cmp_attr)) {
133                 $cmp_attr_func = "default_cmp_attr";
134         }
135         # create compare attribute function if needed
136         if (exists($n{"cmp_attr"})) {
137                 my $cmpcode = $n{"cmp_attr"};
138
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");
142                 }
143                 if($cmpcode =~ m/attr_b/) {
144                         push(@obst_cmp_attr, "\t$arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
145                 }
146                 push(@obst_cmp_attr, "\t${cmpcode}\n");
147                 push(@obst_cmp_attr, "}\n\n");
148
149                 $cmp_attr_func = "cmp_attr_${op}";
150         }
151
152         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
153                 # we explicitly skip the constructor if the specification entry says NONE
154         }
155         else {
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"});
160
161                 # create constructor head
162                 my $complete_args = "";
163                 my $arg_names     = "";
164                 $temp             = "";
165
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";
170                                 next;
171                         }
172                         for (my $i = 1; $i <= $arity; $i++) {
173                                 $complete_args .= ", ir_node *op".$i;
174                                 $arg_names     .= ", op".$i;
175                         }
176                         if (!defined($known_mode)) {
177                                 $complete_args .= ", ir_mode *mode";
178                                 $arg_names     .= ", mode";
179                         }
180                 }
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"};
186                         }
187                 }
188
189                 # we have additional attribute arguements
190                 if (exists($n{"attr"})) {
191                         $complete_args .= ", ".$n{"attr"};
192                 }
193
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");
199
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";
204                                 next;
205                         }
206
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"}));
211
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";
217                         }
218
219                         undef my $in_req_var;
220                         undef my $out_req_var;
221
222                         # set up static variables for requirements and registers
223                         if (exists($n{"reg_req"})) {
224                                 my %req = %{ $n{"reg_req"} };
225                                 my $idx;
226
227                                 undef my @in;
228                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
229                                 undef my @out;
230                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
231
232                                 if (@in) {
233                                         $in_req_var = "_in_req_$op";
234                                         $temp .= "\tstatic const arch_register_req_t *".$in_req_var."[] =\n";
235                                         $temp .= "\t{\n";
236                                         for ($idx = 0; $idx <= $#in; $idx++) {
237                                                 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
238                                         }
239                                         $temp .= "\t};\n";
240                                 }
241
242                                 if (@out) {
243                                         $out_req_var = "_out_req_$op";
244
245                                         $temp .= "\tstatic const arch_register_req_t *".$out_req_var."[] =\n";
246                                         $temp .= "\t{\n";
247                                         for ($idx = 0; $idx <= $#out; $idx++) {
248                                                 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
249                                         }
250                                         $temp .= "\t};\n";
251                                 }
252                         }
253
254                         $temp .= "\n";
255                         $temp .= "\tassert(op_$op != NULL);\n\n";
256
257                         for (my $i = 1; $i <= $arity; $i++) {
258                                 $temp .= "\tin[".($i - 1)."] = op".$i.";\n";
259                         }
260
261                         # set flags
262                         if (exists($n{"irn_flags"})) {
263                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
264                                         if ($flag eq "R") {
265                                                 $temp .= "\tflags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */\n";
266                                         }
267                                         elsif ($flag eq "N") {
268                                                 $temp .= "\tflags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
269                                         }
270                                         elsif ($flag eq "I") {
271                                                 $temp .= "\tflags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
272                                         }
273                                         elsif ($flag eq "S") {
274                                                 $temp .= "\tflags |= arch_irn_flags_modify_sp;          /* op modifies stack pointer */\n";
275                                         }
276                                 }
277                         }
278
279                         my $in_param;
280                         my $out_param;
281                         # allocate memory and set pointer to register requirements
282                         if (exists($n{"reg_req"})) {
283                                 my %req = %{ $n{"reg_req"} };
284
285                                 undef my @in;
286                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
287                                 undef my @out;
288                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
289
290                                 if (@in) {
291                                         $in_param = $in_req_var;
292                                 }
293                                 else {
294                                         $in_param = "NULL";
295                                 }
296
297                                 if (@out) {
298                                         $n_res     = $#out + 1;
299                                         $out_param = "$out_req_var, $exec_units, $n_res";
300                                 }
301                                 else {
302                                         $out_param = "NULL, $exec_units, 0";
303                                 }
304                         }
305                         else {
306                                 $in_param  = "NULL";
307                                 $out_param = "NULL, $exec_units, 0";
308                         }
309                         $temp .= "\n\t/* create node */\n";
310
311                         my $latency = 1;
312                         if (exists($n{"latency"})) {
313                                 $latency = $n{"latency"};
314                         }
315
316                         my $mode = "mode";
317                         if (defined($known_mode)) {
318                                 $mode = $known_mode;
319                         }
320                         $temp .= "\tres = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
321
322                         $temp .= "\n\t/* init node attributes */\n";
323                         $temp .= "\tinit_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
324
325                         # set flags for outs
326                         if ($#out_flags >= 0) {
327                                 $temp .= "\n\t/* set flags for outs */\n";
328                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
329                                         my $flags  = "";
330                                         my $prefix = "";
331
332                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
333                                                 if ($flag eq "I") {
334                                                         $flags .= $prefix."arch_irn_flags_ignore";
335                                                         $prefix = " | ";
336                                                 }
337                                                 elsif ($flag eq "S") {
338                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
339                                                         $prefix = " | ";
340                                                 }
341                                         }
342
343                                         $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
344                                 }
345                         }
346
347
348                         if (exists($n{"init_attr"})) {
349                                 $temp .= "\tattr = get_$arch\_attr(res);\n";
350                                 $temp .= $n{"init_attr"}."\n";
351                         }
352
353                         $temp .= "\n\t/* optimize node */\n";
354                         $temp .= "\tres = optimize_node(res);\n";
355                         $temp .= "\tirn_vrfy_irg(res, irg);\n\n";
356
357                         $temp .= "\n\treturn res;\n";
358
359                         push(@obst_constructor, $temp);
360                 }
361                 else { # user defined constructor
362                         push(@obst_constructor, $n{"rd_constructor"});
363                 }
364
365                 # close constructor function
366                 push(@obst_constructor, "}\n\n");
367         } # constructor creation
368
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"}));
372
373
374         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
375         push(@obst_new_irop, "\tops.dump_node     = $arch\_dump_node;\n");
376
377         if (defined($cmp_attr_func)) {
378                 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
379         }
380
381         $n_opcodes++;
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");
387
388         push(@obst_header, "\n");
389 }
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");
394
395 # emit the code
396
397 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
398
399 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
400 print OUT @obst_cmp_attr;
401 print OUT "\n";
402 print OUT @obst_opvar;
403 print OUT "\n";
404 print OUT @obst_get_opvar;
405 print OUT "\n";
406
407 print OUT<<EOF;
408
409 static int $arch\_opcode_start = -1;
410 static int $arch\_opcode_end   = -1;
411
412 EOF
413
414 # build the FOURCC arguments from $arch
415
416 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
417
418 if (length($arch) >= 1) {
419         $a = uc(substr($arch, 0, 1));
420 }
421
422 if (length($arch) >= 2) {
423         $b = uc(substr($arch, 1, 1));
424 }
425
426 if (length($arch) >= 3) {
427         $c = uc(substr($arch, 2, 1));
428 }
429
430 if (length($arch) >= 4) {
431         $d = uc(substr($arch, 3, 1));
432 }
433
434 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
435
436 print OUT<<ENDOFISIRN;
437
438 /** Return the opcode number of the first $arch opcode. */
439 int get_$arch\_opcode_first(void) {
440         return $arch\_opcode_start;
441 }
442
443 /** Return the opcode number of the last $arch opcode + 1. */
444 int get_$arch\_opcode_last(void) {
445         return $arch\_opcode_end;
446 }
447
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;
451 }
452
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;
456         return -1;
457 }
458
459 ENDOFISIRN
460
461 print OUT @obst_constructor;
462
463 print OUT<<ENDOFMAIN;
464 /**
465  * Creates the $arch specific Firm machine operations
466  * needed for the assembler irgs.
467  */
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)
482
483         ir_op_ops  ops;
484         int        cur_opcode;
485         static int run_once = 0;
486
487         if (run_once)
488                 return;
489         run_once = 1;
490
491         cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
492
493         $arch\_opcode_start = cur_opcode;
494 ENDOFMAIN
495
496 print OUT @obst_new_irop;
497 print OUT "\n";
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));
501 print OUT ";\n";
502 print OUT "}\n";
503
504 close(OUT);
505
506 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
507
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";
518
519 close(OUT);
520
521 ###
522 # Translates numeric arity into string constant.
523 ###
524 sub translate_arity {
525         my $arity = shift;
526
527         if ($arity =~ /^\d+$/) {
528                 if    ($arity == 0) {
529                         return "oparity_zero";
530                 }
531                 elsif ($arity == 1) {
532                         return "oparity_unary";
533                 }
534                 elsif ($arity == 2) {
535                         return "oparity_binary";
536                 }
537                 elsif ($arity == 3) {
538                         return "oparity_trinary";
539                 }
540                 else {
541                         return "oparity_any";
542                 }
543         }
544         else {
545                 return "oparity_".$arity;
546         }
547 }
548
549 ###
550 # Return the list of pointers for the given execution units.
551 ###
552 sub gen_execunit_list_initializer {
553         my $units   = shift;
554         my $uc_arch = uc($arch);
555         my $ret     = "";
556         my $ret2    = "";
557         my %init;
558
559         foreach my $unit (@{ $units }) {
560                 if ($unit eq "DUMMY") {
561                         push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
562                 }
563                 elsif (exists($cpu{"$unit"})) {
564                         # operation can be executed on all units of this type
565                         # -> add them all
566                         my $tp_name = "$arch\_execution_units_$unit";
567                         my $idx     = 0;
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."]");
572                         }
573                 }
574                 else {
575                         # operation can be executed only a certain unit
576                         # -> find corresponding unit type
577                         my $found = 0;
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."]");
584                                                 $found = 1;
585                                                 last TP_SEARCH;
586                                         }
587                                 }
588                         }
589
590                         if (! $found) {
591                                 print STDERR "Invalid execution unit $unit specified!\n";
592                         }
593                 }
594         }
595
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";
599                 $ret .= "\t{\n";
600                 foreach (@{ $init{"$key"} }) {
601                         $ret .= "$_,\n";
602                 }
603                 $ret .= "\t\tNULL\n";
604                 $ret .= "\t};\n";
605                 $ret2 .= "\t\t_allowed_units_$key,\n";
606         }
607         $ret2 .= "\t\tNULL\n";
608
609         $ret .= "\tstatic const be_execution_unit_t **_exec_units[] =\n";
610         $ret .= "\t{\n";
611         $ret .= $ret2;
612         $ret .= "\t};\n";
613
614         return $ret;
615 }