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