eb5fef6776ce6e5e58db44fbab9b38f11e29e048
[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
22 # include spec file
23
24 my $return;
25
26 no strict "subs";
27 unless ($return = do $specfile) {
28         warn "couldn't parse $specfile: $@" if $@;
29         warn "couldn't do $specfile: $!"    unless defined $return;
30         warn "couldn't run $specfile"       unless $return;
31 }
32 use strict "subs";
33
34 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
35 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
36
37 #print Dumper(%nodes);
38 #print Dumper(%operands);
39
40 # create c code file from specs
41
42 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
43 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
44 my @obst_constructor; # stack for node constructor functions
45 my @obst_new_irop;    # stack for the new_ir_op calls
46 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
47 my @obst_header;      # stack for function prototypes
48 my @obst_is_archirn;  # stack for the is_$arch_irn() function
49 my @obst_cmp_attr;    # stack for the compare attribute functions
50 my @obst_proj;        # stack for the pn_ numbers
51 my $orig_op;
52 my $arity;
53 my $cmp_attr_func;
54 my $temp;
55 my $n_opcodes = 0;    # number of opcodes
56
57 # for registering additional opcodes
58 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
59
60 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
61
62 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
63 foreach my $op (keys(%nodes)) {
64         my %n        = %{ $nodes{"$op"} };
65         my $tuple    = 0;
66         my $n_res    = 0;
67         my $num_outs = 0;
68         my @out_flags;
69
70         # determine arity from in requirements
71         $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
72         if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
73                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
74         }
75
76         $orig_op = $op;
77         $op      = $arch."_".$op;
78         $temp    = "";
79
80         # define some proj numbers
81         if (exists($n{"outs"})) {
82                 undef my @outs;
83
84                 @outs     = @{ $n{"outs"} };
85                 $num_outs = $#outs;
86
87                 push(@obst_proj, "\nenum pn_$op {\n");
88
89                 for (my $idx = 0; $idx <= $#outs; $idx++) {
90                         # check, if we have additional flags annotated to out
91                         if ($outs[$idx] =~ /:(S|I(\|(S|I))*)/) {
92                                 push(@out_flags, $1);
93                                 $outs[$idx] =~ s/:(S|I(\|(S|I))*)//;
94                         }
95                         push(@obst_proj, "  pn_$op\_".$outs[$idx]." = $idx,\n");
96                 }
97
98                 push(@obst_proj, "};\n");
99                 $tuple = 1;
100         }
101
102         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
103         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
104         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
105
106         push(@obst_is_archirn, "is_$op(node)");
107
108         push(@obst_header, "extern ir_op *op_$op;\n");
109         push(@obst_header, "ir_op *get_op_$op(void);\n");
110         push(@obst_header, "int is_$op(const ir_node *n);\n");
111
112         $cmp_attr_func = 0;
113         # create compare attribute function if needed
114         if (exists($n{"cmp_attr"})) {
115                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
116                 push(@obst_cmp_attr, "  $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
117                 push(@obst_cmp_attr, "  $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
118                 push(@obst_cmp_attr, "  (void) attr_a;\n");
119                 push(@obst_cmp_attr, "  (void) attr_b;\n");
120                 push(@obst_cmp_attr, $n{"cmp_attr"});
121                 push(@obst_cmp_attr, "}\n\n");
122
123                 $cmp_attr_func = 1;
124         }
125
126         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
127                 # we explicitly skip the constructor if the specification entry says NONE
128         }
129         else {
130                 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
131                 $n{"comment"} =~ s/^"|"$//g;    # remove "
132                 $n{"comment"} = "/* ".$n{"comment"}." */\n";
133                 push(@obst_constructor, $n{"comment"});
134
135                 # create constructor head
136                 my $complete_args = "";
137                 my $arg_names     = "";
138                 $temp             = "";
139
140                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
141                 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
142                         if ($arity !~ /^\d+$/) {
143                                 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
144                                 next;
145                         }
146                         for (my $i = 1; $i <= $arity; $i++) {
147                                 $complete_args .= ", ir_node *op".$i;
148                                 $arg_names     .= ", op".$i;
149                         }
150                         if ($tuple == 0) {
151                                 $complete_args .= ", ir_mode *mode";
152                                 $arg_names     .= ", mode";
153                         }
154                 }
155                 else { # user defined args
156                         for my $href (@{ $n{"args"} }) {
157                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
158                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
159                                 $arg_names      .= ", ".$href->{"name"};
160                         }
161                 }
162
163                 # we have additional attribute arguements
164                 if (exists($n{"attr"})) {
165                         $complete_args .= ", ".$n{"attr"};
166                 }
167
168                 # $complete_args = substr($complete_args, 2);
169                 $temp .= "$complete_args)";
170                 push(@obst_constructor, $temp." {\n");
171                 push(@obst_header, $n{"comment"});
172                 push(@obst_header, $temp.";\n");
173
174                 # emit constructor code
175                 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
176                         if ($arity !~ /^\d+$/) {
177                                 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
178                                 next;
179                         }
180
181                         $temp  = "  ir_node *res;\n";
182                         $temp .= "  ir_node *in[$arity];\n" if ($arity > 0);
183                         $temp .= "  int flags = 0;\n";
184                         $temp .= "  $arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
185
186                         undef my $in_req_var;
187                         undef my $out_req_var;
188
189                         # set up static variables for requirements and registers
190                         if (exists($n{"reg_req"})) {
191                                 my %req = %{ $n{"reg_req"} };
192                                 my $idx;
193
194                                 undef my @in;
195                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
196                                 undef my @out;
197                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
198
199                                 if (@in) {
200                                         $in_req_var = "_in_req_$op";
201                                         $temp .= "  static const $arch\_register_req_t *".$in_req_var."[] =\n  {\n";
202                                         for ($idx = 0; $idx <= $#in; $idx++) {
203                                                 $temp .= "    ".$op."_reg_req_in_".$idx.",\n";
204                                         }
205                                         $temp .= "  };\n";
206                                 }
207
208                                 if (@out) {
209                                         $out_req_var = "_out_req_$op";
210
211                                         $temp .= "  static const $arch\_register_req_t *".$out_req_var."[] =\n  {\n";
212                                         for ($idx = 0; $idx <= $#out; $idx++) {
213                                                 $temp .= "    ".$op."_reg_req_out_".$idx.",\n";
214                                         }
215                                         $temp .= "  };\n";
216                                 }
217                         }
218
219                         $temp .= "\n";
220                         $temp .= "  if (!op_$op) {\n";
221                         $temp .= "    assert(0);\n";
222                         $temp .= "    return NULL;\n";
223                         $temp .= "  }\n\n";
224                         for (my $i = 1; $i <= $arity; $i++) {
225                                 $temp .= "  in[".($i - 1)."] = op".$i.";\n";
226                         }
227
228                         # set flags
229                         if (exists($n{"irn_flags"})) {
230                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
231                                         if ($flag eq "R") {
232                                                 $temp .= "  flags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */\n";
233                                         }
234                                         elsif ($flag eq "N") {
235                                                 $temp .= "  flags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
236                                         }
237                                         elsif ($flag eq "I") {
238                                                 $temp .= "  flags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
239                                         }
240                                         elsif ($flag eq "S") {
241                                                 $temp .= "  flags |= arch_irn_flags_modify_sp;          /* op modifies stack pointer */\n";
242                                         }
243                                 }
244                         }
245
246                         my $in_param;
247                         my $out_param;
248                         # allocate memory and set pointer to register requirements
249                         if (exists($n{"reg_req"})) {
250                                 my %req = %{ $n{"reg_req"} };
251
252                                 undef my @in;
253                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
254                                 undef my @out;
255                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
256
257                                 if (@in) {
258                                         $in_param = $in_req_var;
259                                 }
260                                 else {
261                                         $in_param = "NULL";
262                                 }
263
264                                 if (@out) {
265                                         $out_param = $out_req_var.", ".($#out + 1);
266                                         $n_res     = $#out;
267                                 }
268                                 else {
269                                         $out_param = "NULL, 0";
270                                 }
271                         }
272                         else {
273                                 $in_param = "NULL";
274                                 $out_param = "NULL, 0";
275                         }
276                         $temp .= "\n  /* create node */\n";
277
278                         my $latency = 1;
279                         if (exists($n{"latency"})) {
280                                 $latency = $n{"latency"};
281                         }
282
283                         my $mode = "mode";
284                         if ($tuple == 1) {
285                                 $mode = "mode_T";
286                         }
287                         $temp .= "  res = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
288
289                         $temp .= "\n  /* init node attributes */\n";
290                         $temp .= "  init_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
291
292                         # set flags for outs
293                         if ($#out_flags >= 0) {
294                                 $temp .= "\n  /* set flags for outs */\n";
295                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
296                                         my $flags  = "";
297                                         my $prefix = "";
298
299                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
300                                                 if ($flag eq "I") {
301                                                         $flags .= $prefix."arch_irn_flags_ignore";
302                                                         $prefix = " | ";
303                                                 }
304                                                 elsif ($flag eq "S") {
305                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
306                                                         $prefix = " | ";
307                                                 }
308                                         }
309
310                                         $temp .= "  set_$arch\_out_flags(res, $flags, $idx);\n";
311                                 }
312                         }
313
314
315                         if (exists($n{"init_attr"})) {
316                                 $temp .= "  attr = get_$arch\_attr(res);\n";
317                                 $temp .= $n{"init_attr"}."\n";
318                         }
319
320                         $temp .= "\n  /* optimize node */\n";
321                         $temp .= "  res = optimize_node(res);\n";
322                         $temp .= "  irn_vrfy_irg(res, irg);\n\n";
323
324                         $temp .= "\n  return res;\n";
325
326                         push(@obst_constructor, $temp);
327                 }
328                 else { # user defined constructor
329                         push(@obst_constructor, $n{"rd_constructor"});
330                 }
331
332                 # close constructor function
333                 push(@obst_constructor, "}\n\n");
334         } # constructor creation
335
336         # set default values for state and flags if not given
337         $n{"state"}    = "floats" if (! exists($n{"state"}));
338         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
339
340
341         push(@obst_new_irop, "\n  memset(&ops, 0, sizeof(ops));\n");
342         push(@obst_new_irop, "  ops.dump_node     = $arch\_dump_node;\n");
343
344         if ($cmp_attr_func) {
345                 push(@obst_new_irop, "  ops.node_cmp_attr = cmp_attr_$op;\n");
346         }
347
348         $n_opcodes++;
349         $temp  = "  op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
350         $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
351         push(@obst_new_irop, $temp);
352         push(@obst_new_irop, "  set_op_tag(op_$op, &$arch\_op_tag);\n");
353         push(@obst_enum_op, "  iro_$op,\n");
354
355         push(@obst_header, "\n");
356 }
357 push(@obst_enum_op, "  iro_$arch\_last_generated,\n");
358 push(@obst_enum_op, "  iro_$arch\_last = iro_$arch\_last_generated");
359 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
360 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
361
362 # emit the code
363
364 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
365
366 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
367 print OUT @obst_cmp_attr;
368 print OUT "\n";
369 print OUT @obst_opvar;
370 print OUT "\n";
371 print OUT @obst_get_opvar;
372 print OUT "\n";
373
374 print OUT<<EOF;
375
376 static int $arch\_opcode_start = -1;
377 static int $arch\_opcode_end   = -1;
378
379 EOF
380
381 # build the FOURCC arguments from $arch
382
383 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
384
385 if (length($arch) >= 1) {
386         $a = uc(substr($arch, 0, 1));
387 }
388
389 if (length($arch) >= 2) {
390         $b = uc(substr($arch, 1, 1));
391 }
392
393 if (length($arch) >= 3) {
394         $c = uc(substr($arch, 2, 1));
395 }
396
397 if (length($arch) >= 4) {
398         $d = uc(substr($arch, 3, 1));
399 }
400
401 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
402
403 print OUT<<ENDOFISIRN;
404
405 /** Return the opcode number of the first $arch opcode. */
406 int get_$arch\_opcode_first(void) {
407   return $arch\_opcode_start;
408 }
409
410 /** Return the opcode number of the last $arch opcode + 1. */
411 int get_$arch\_opcode_last(void) {
412   return $arch\_opcode_end;
413 }
414
415 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
416 int is_$arch\_irn(const ir_node *node) {
417   return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
418 }
419
420 int get_$arch\_irn_opcode(const ir_node *node) {
421   if (is_$arch\_irn(node))
422         return get_irn_opcode(node) - $arch\_opcode_start;
423   return -1;
424 }
425
426 ENDOFISIRN
427
428 print OUT @obst_constructor;
429
430 print OUT<<ENDOFMAIN;
431 /**
432  * Creates the $arch specific Firm machine operations
433  * needed for the assembler irgs.
434  */
435 void $arch\_create_opcodes(void) {
436 #define N   irop_flag_none
437 #define L   irop_flag_labeled
438 #define C   irop_flag_commutative
439 #define X   irop_flag_cfopcode
440 #define I   irop_flag_ip_cfopcode
441 #define F   irop_flag_fragile
442 #define Y   irop_flag_forking
443 #define H   irop_flag_highlevel
444 #define c   irop_flag_constlike
445 #define K   irop_flag_keep
446 #define M   irop_flag_machine
447 #define O   irop_flag_machine_op
448 #define R   (irop_flag_user << 0)
449
450   ir_op_ops ops;
451   int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
452
453   $arch\_opcode_start = cur_opcode;
454 ENDOFMAIN
455
456 print OUT @obst_new_irop;
457 print OUT "\n";
458 print OUT "  $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
459 print OUT "  $arch\_opcode_end = cur_opcode + iro_$arch\_last";
460 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
461 print OUT ";\n";
462 print OUT "}\n";
463
464 close(OUT);
465
466 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
467
468 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
469 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
470 print OUT @obst_enum_op;
471 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
472 print OUT "int get_$arch\_opcode_first(void);\n";
473 print OUT "int get_$arch\_opcode_last(void);\n";
474 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
475 print OUT @obst_header;
476 print OUT @obst_proj;
477 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
478
479 close(OUT);
480
481 ###
482 # Translates numeric arity into string constant.
483 ###
484 sub translate_arity {
485         my $arity = shift;
486
487         if ($arity =~ /^\d+$/) {
488                 if    ($arity == 0) {
489                         return "oparity_zero";
490                 }
491                 elsif ($arity == 1) {
492                         return "oparity_unary";
493                 }
494                 elsif ($arity == 2) {
495                         return "oparity_binary";
496                 }
497                 elsif ($arity == 3) {
498                         return "oparity_trinary";
499                 }
500                 else {
501                         return "$arity";
502                 }
503         }
504         else {
505                 return "oparity_".$arity;
506         }
507 }