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