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