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