446e747b92a010de25ea7a32d3193f02565fc955
[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
23 # include spec file
24
25 my $return;
26
27 no strict "subs";
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;
32 }
33 use strict "subs";
34
35 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
36 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
37
38 #print Dumper(%nodes);
39 #print Dumper(%operands);
40
41 # create c code file from specs
42
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
52 my $orig_op;
53 my $arity;
54 my $cmp_attr_func;
55 my $temp;
56 my $n_opcodes = 0;    # number of opcodes
57
58 # for registering additional opcodes
59 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
60
61 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
62
63 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
64 foreach my $op (keys(%nodes)) {
65         my %n        = %{ $nodes{"$op"} };
66         my $tuple    = 0;
67         my $n_res    = 0;
68         my $num_outs = 0;
69         my @out_flags;
70
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"} });
75         }
76
77         $orig_op = $op;
78         $op      = $arch."_".$op;
79         $temp    = "";
80
81         # define some proj numbers
82         if (exists($n{"outs"})) {
83                 undef my @outs;
84
85                 @outs     = @{ $n{"outs"} };
86                 $num_outs = $#outs + 1;
87
88                 push(@obst_proj, "\nenum pn_$op {\n");
89
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))*)/) {
93                                 push(@out_flags, $1);
94                                 $outs[$idx] =~ s/:(S|I(\|(S|I))*)//;
95                         }
96                         push(@obst_proj, "  pn_$op\_".$outs[$idx]." = $idx,\n");
97                 }
98
99                 push(@obst_proj, "};\n");
100                 $tuple = 1;
101         }
102
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");
106
107         push(@obst_is_archirn, "is_$op(node)");
108
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");
112
113         $cmp_attr_func = 0;
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");
123
124                 $cmp_attr_func = 1;
125         }
126
127         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
128                 # we explicitly skip the constructor if the specification entry says NONE
129         }
130         else {
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"});
135
136                 # create constructor head
137                 my $complete_args = "";
138                 my $arg_names     = "";
139                 $temp             = "";
140
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";
145                                 next;
146                         }
147                         for (my $i = 1; $i <= $arity; $i++) {
148                                 $complete_args .= ", ir_node *op".$i;
149                                 $arg_names     .= ", op".$i;
150                         }
151                         if ($tuple == 0) {
152                                 $complete_args .= ", ir_mode *mode";
153                                 $arg_names     .= ", mode";
154                         }
155                 }
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"};
161                         }
162                 }
163
164                 # we have additional attribute arguements
165                 if (exists($n{"attr"})) {
166                         $complete_args .= ", ".$n{"attr"};
167                 }
168
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");
174
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";
179                                 next;
180                         }
181
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"}));
186
187                         my $exec_units = "NULL";
188                         # set up static variables for cpu execution unit assigments
189                         if (exists($n{"units"})) {
190                                 $temp .= "  static const be_execution_unit_t *_exec_units[] =\n  {\n";
191                                 $temp .= get_execunit_list($n{"units"});
192                                 $temp .= "  };\n";
193                                 $exec_units = "_exec_units";
194                         }
195
196                         undef my $in_req_var;
197                         undef my $out_req_var;
198
199                         # set up static variables for requirements and registers
200                         if (exists($n{"reg_req"})) {
201                                 my %req = %{ $n{"reg_req"} };
202                                 my $idx;
203
204                                 undef my @in;
205                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
206                                 undef my @out;
207                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
208
209                                 if (@in) {
210                                         $in_req_var = "_in_req_$op";
211                                         $temp .= "  static const $arch\_register_req_t *".$in_req_var."[] =\n  {\n";
212                                         for ($idx = 0; $idx <= $#in; $idx++) {
213                                                 $temp .= "    ".$op."_reg_req_in_".$idx.",\n";
214                                         }
215                                         $temp .= "  };\n";
216                                 }
217
218                                 if (@out) {
219                                         $out_req_var = "_out_req_$op";
220
221                                         $temp .= "  static const $arch\_register_req_t *".$out_req_var."[] =\n  {\n";
222                                         for ($idx = 0; $idx <= $#out; $idx++) {
223                                                 $temp .= "    ".$op."_reg_req_out_".$idx.",\n";
224                                         }
225                                         $temp .= "  };\n";
226                                 }
227                         }
228
229                         $temp .= "\n";
230                         $temp .= "  if (!op_$op) {\n";
231                         $temp .= "    assert(0);\n";
232                         $temp .= "    return NULL;\n";
233                         $temp .= "  }\n\n";
234                         for (my $i = 1; $i <= $arity; $i++) {
235                                 $temp .= "  in[".($i - 1)."] = op".$i.";\n";
236                         }
237
238                         # set flags
239                         if (exists($n{"irn_flags"})) {
240                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
241                                         if ($flag eq "R") {
242                                                 $temp .= "  flags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */\n";
243                                         }
244                                         elsif ($flag eq "N") {
245                                                 $temp .= "  flags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
246                                         }
247                                         elsif ($flag eq "I") {
248                                                 $temp .= "  flags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
249                                         }
250                                         elsif ($flag eq "S") {
251                                                 $temp .= "  flags |= arch_irn_flags_modify_sp;          /* op modifies stack pointer */\n";
252                                         }
253                                 }
254                         }
255
256                         my $in_param;
257                         my $out_param;
258                         # allocate memory and set pointer to register requirements
259                         if (exists($n{"reg_req"})) {
260                                 my %req = %{ $n{"reg_req"} };
261
262                                 undef my @in;
263                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
264                                 undef my @out;
265                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
266
267                                 if (@in) {
268                                         $in_param = $in_req_var;
269                                 }
270                                 else {
271                                         $in_param = "NULL";
272                                 }
273
274                                 if (@out) {
275                                         $n_res     = $#out + 1;
276                                         $out_param = "$out_req_var, $exec_units, $n_res";
277                                 }
278                                 else {
279                                         $out_param = "NULL, $exec_units, 0";
280                                 }
281                         }
282                         else {
283                                 $in_param  = "NULL";
284                                 $out_param = "NULL, $exec_units, 0";
285                         }
286                         $temp .= "\n  /* create node */\n";
287
288                         my $latency = 1;
289                         if (exists($n{"latency"})) {
290                                 $latency = $n{"latency"};
291                         }
292
293                         my $mode = "mode";
294                         if ($tuple == 1) {
295                                 $mode = "mode_T";
296                         }
297                         $temp .= "  res = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
298
299                         $temp .= "\n  /* init node attributes */\n";
300                         $temp .= "  init_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
301
302                         # set flags for outs
303                         if ($#out_flags >= 0) {
304                                 $temp .= "\n  /* set flags for outs */\n";
305                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
306                                         my $flags  = "";
307                                         my $prefix = "";
308
309                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
310                                                 if ($flag eq "I") {
311                                                         $flags .= $prefix."arch_irn_flags_ignore";
312                                                         $prefix = " | ";
313                                                 }
314                                                 elsif ($flag eq "S") {
315                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
316                                                         $prefix = " | ";
317                                                 }
318                                         }
319
320                                         $temp .= "  set_$arch\_out_flags(res, $flags, $idx);\n";
321                                 }
322                         }
323
324
325                         if (exists($n{"init_attr"})) {
326                                 $temp .= "  attr = get_$arch\_attr(res);\n";
327                                 $temp .= $n{"init_attr"}."\n";
328                         }
329
330                         $temp .= "\n  /* optimize node */\n";
331                         $temp .= "  res = optimize_node(res);\n";
332                         $temp .= "  irn_vrfy_irg(res, irg);\n\n";
333
334                         $temp .= "\n  return res;\n";
335
336                         push(@obst_constructor, $temp);
337                 }
338                 else { # user defined constructor
339                         push(@obst_constructor, $n{"rd_constructor"});
340                 }
341
342                 # close constructor function
343                 push(@obst_constructor, "}\n\n");
344         } # constructor creation
345
346         # set default values for state and flags if not given
347         $n{"state"}    = "floats" if (! exists($n{"state"}));
348         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
349
350
351         push(@obst_new_irop, "\n  memset(&ops, 0, sizeof(ops));\n");
352         push(@obst_new_irop, "  ops.dump_node     = $arch\_dump_node;\n");
353
354         if ($cmp_attr_func) {
355                 push(@obst_new_irop, "  ops.node_cmp_attr = cmp_attr_$op;\n");
356         }
357
358         $n_opcodes++;
359         $temp  = "  op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
360         $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
361         push(@obst_new_irop, $temp);
362         push(@obst_new_irop, "  set_op_tag(op_$op, &$arch\_op_tag);\n");
363         push(@obst_enum_op, "  iro_$op,\n");
364
365         push(@obst_header, "\n");
366 }
367 push(@obst_enum_op, "  iro_$arch\_last_generated,\n");
368 push(@obst_enum_op, "  iro_$arch\_last = iro_$arch\_last_generated");
369 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
370 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
371
372 # emit the code
373
374 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
375
376 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
377 print OUT @obst_cmp_attr;
378 print OUT "\n";
379 print OUT @obst_opvar;
380 print OUT "\n";
381 print OUT @obst_get_opvar;
382 print OUT "\n";
383
384 print OUT<<EOF;
385
386 static int $arch\_opcode_start = -1;
387 static int $arch\_opcode_end   = -1;
388
389 EOF
390
391 # build the FOURCC arguments from $arch
392
393 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
394
395 if (length($arch) >= 1) {
396         $a = uc(substr($arch, 0, 1));
397 }
398
399 if (length($arch) >= 2) {
400         $b = uc(substr($arch, 1, 1));
401 }
402
403 if (length($arch) >= 3) {
404         $c = uc(substr($arch, 2, 1));
405 }
406
407 if (length($arch) >= 4) {
408         $d = uc(substr($arch, 3, 1));
409 }
410
411 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
412
413 print OUT<<ENDOFISIRN;
414
415 /** Return the opcode number of the first $arch opcode. */
416 int get_$arch\_opcode_first(void) {
417   return $arch\_opcode_start;
418 }
419
420 /** Return the opcode number of the last $arch opcode + 1. */
421 int get_$arch\_opcode_last(void) {
422   return $arch\_opcode_end;
423 }
424
425 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
426 int is_$arch\_irn(const ir_node *node) {
427   return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
428 }
429
430 int get_$arch\_irn_opcode(const ir_node *node) {
431   if (is_$arch\_irn(node))
432         return get_irn_opcode(node) - $arch\_opcode_start;
433   return -1;
434 }
435
436 ENDOFISIRN
437
438 print OUT @obst_constructor;
439
440 print OUT<<ENDOFMAIN;
441 /**
442  * Creates the $arch specific Firm machine operations
443  * needed for the assembler irgs.
444  */
445 void $arch\_create_opcodes(void) {
446 #define N   irop_flag_none
447 #define L   irop_flag_labeled
448 #define C   irop_flag_commutative
449 #define X   irop_flag_cfopcode
450 #define I   irop_flag_ip_cfopcode
451 #define F   irop_flag_fragile
452 #define Y   irop_flag_forking
453 #define H   irop_flag_highlevel
454 #define c   irop_flag_constlike
455 #define K   irop_flag_keep
456 #define M   irop_flag_machine
457 #define O   irop_flag_machine_op
458 #define R   (irop_flag_user << 0)
459
460   ir_op_ops ops;
461   int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
462
463   $arch\_opcode_start = cur_opcode;
464 ENDOFMAIN
465
466 print OUT @obst_new_irop;
467 print OUT "\n";
468 print OUT "  $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
469 print OUT "  $arch\_opcode_end = cur_opcode + iro_$arch\_last";
470 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
471 print OUT ";\n";
472 print OUT "}\n";
473
474 close(OUT);
475
476 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
477
478 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
479 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
480 print OUT @obst_enum_op;
481 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
482 print OUT "int get_$arch\_opcode_first(void);\n";
483 print OUT "int get_$arch\_opcode_last(void);\n";
484 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
485 print OUT @obst_header;
486 print OUT @obst_proj;
487 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
488
489 close(OUT);
490
491 ###
492 # Translates numeric arity into string constant.
493 ###
494 sub translate_arity {
495         my $arity = shift;
496
497         if ($arity =~ /^\d+$/) {
498                 if    ($arity == 0) {
499                         return "oparity_zero";
500                 }
501                 elsif ($arity == 1) {
502                         return "oparity_unary";
503                 }
504                 elsif ($arity == 2) {
505                         return "oparity_binary";
506                 }
507                 elsif ($arity == 3) {
508                         return "oparity_trinary";
509                 }
510                 else {
511                         return "$arity";
512                 }
513         }
514         else {
515                 return "oparity_".$arity;
516         }
517 }
518
519 ###
520 # Return the list of pointers for the given execution units.
521 ###
522 sub get_execunit_list {
523         my $units   = shift;
524         my $uc_arch = uc($arch);
525         my $ret     = "";
526
527         foreach my $unit (@{ $units }) {
528                 if (exists($cpu{"$unit"})) {
529                         # operation can be executed on all units of this type
530                         # -> add them all
531                         my $tp_name = "$arch\_execution_units_$unit";
532                         foreach (@{ $cpu{"$unit"} }) {
533                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
534                                 $ret .= "    &".$tp_name."[".$unit_name."],\n";
535                         }
536                 }
537                 else {
538                         # operation can be executed only a certain unit
539                         # -> find corresponding unit type
540                         my $found = 0;
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                                                 $ret    .= "    &".$tp_name."[".$unit_name."],\n";
547                                                 $found   = 1;
548                                                 last TP_SEARCH;
549                                         }
550                                 }
551                         }
552
553                         if (! $found) {
554                                 print STDERR "Invalid execution unit $unit specified!\n";
555                         }
556                 }
557         }
558
559         $ret .= "    NULL\n";
560
561         return $ret;
562 }