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