fixed indents
[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
51 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
52
53 foreach my $op (keys(%nodes)) {
54         my %n = %{ $nodes{"$op"} };
55
56         $orig_op = $op;
57         $op      = $arch."_".$op;
58         $arity   = $n{"arity"};
59
60         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
61         push(@obst_get_opvar, "ir_op *get_op_$op(void)   { return op_$op; }\n");
62         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
63
64         push(@obst_is_archirn, "is_$op(node)");
65
66         push(@obst_header, "int is_$op(const ir_node *n);\n");
67
68         $cmp_attr_func = 0;
69         # create compare attribute function if needed
70         if (exists($n{"cmp_attr"})) {
71                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
72                 push(@obst_cmp_attr, "  asmop_attr *attr_a = get_ia32_attr(a);\n");
73                 push(@obst_cmp_attr, "  asmop_attr *attr_b = get_ia32_attr(b);\n");
74                 push(@obst_cmp_attr, $n{"cmp_attr"});
75                 push(@obst_cmp_attr, "}\n\n");
76
77                 $cmp_attr_func = 1;
78         }
79
80         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
81                 # we explicitly skip the constructor if the specification entry says NONE
82         }
83         else {
84                 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
85                 $n{"comment"} =~ s/^"|"$//g;    # remove "
86                 $n{"comment"} = "/* ".$n{"comment"}." */\n";
87                 push(@obst_constructor, $n{"comment"});
88
89                 # create constructor head
90                 my $complete_args = "";
91                 my $arg_names     = "";
92                 my $temp          = "";
93
94                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
95                 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
96                         if ($n{"arity"} !~ /^[0-3]$/) {
97                                 print "DEFAULT args require arity 0,1,2 or 3! Ignoring op $orig_op!\n";
98                                 next;
99                         }
100                         for (my $i = 1; $i <= $n{"arity"}; $i++) {
101                                 $complete_args .= ", ir_node *op".$i;
102                                 $arg_names     .= ", op".$i;
103                         }
104                         $complete_args .= ", ir_mode *mode";
105                         $arg_names     .= ", mode";
106                 }
107                 else { # user defined args
108                         for my $href (@{ $n{"args"} }) {
109                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
110                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
111                                 $arg_names      .= ", ".$href->{"name"};
112                         }
113                 }
114                 $complete_args = substr($complete_args, 2);
115                 $temp .= ", $complete_args)";
116                 push(@obst_constructor, $temp." {\n");
117                 push(@obst_header, $temp.";\n");
118
119                 # emit constructor code
120                 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
121                         if ($n{"arity"} !~ /^[0-3]$/) {
122                                 print "DEFAULT rd_constructor requires arity 0,1,2 or 3! Ignoring op $orig_op!\n";
123                                 next;
124                         }
125                         $temp  = "  asmop_attr *attr;\n";
126                         $temp .= "  ir_node *res;\n";
127                         $temp .= "  ir_node *in[$arity];\n" if ($arity > 0);
128                         $temp .= "\n";
129                         $temp .= "  if (!op_$op) {\n";
130                         $temp .= "    assert(0);\n";
131                         $temp .= "    return NULL;\n";
132                         $temp .= "  }\n\n";
133                         for (my $i = 1; $i <= $arity; $i++) {
134                                 $temp .= "  in[".($i - 1)."] = op".$i.";\n";
135                         }
136                         $temp .= "  res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
137                         $temp .= "  set_ia32_pncode(res, -1);\n";
138                         $temp .= "  res = optimize_node(res);\n";
139                         $temp .= "  irn_vrfy_irg(res, irg);\n\n";
140
141                         # set register flags
142                         $temp .= "  attr = get_ia32_attr(res);\n\n";
143                         $temp .= "  attr->flags  = 0;                                 /* clear flags */\n";
144                         if (!exists($n{"spill"}) || $n{"spill"} == 1) {
145                                 $temp .= "  attr->flags |= arch_irn_flags_spillable;          /* op is spillable */\n";
146                         }
147                         if (exists($n{"remat"}) && $n{"remat"} == 1) {
148                                 $temp .= "  attr->flags |= arch_irn_flags_rematerializable;   /* op can be easily recalulated */\n";
149                         }
150
151                         # allocate memory and set pointer to register requirements
152                         if (exists($n{"reg_req"})) {
153                                 my %req = %{ $n{"reg_req"} };
154                                 my $idx;
155
156                                 undef my @in;
157                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
158                                 undef my @out;
159                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
160
161                                 if (@in) {
162                                         $temp .= "\n  /* allocate memory for IN register requirements and assigned registers */\n";
163                                         $temp .= "  attr->in_req    = calloc(".($#in + 1).", sizeof(arch_register_req_t *));  /* space for in requirements */\n";
164                                         for ($idx = 0; $idx <= $#in; $idx++) {
165                                                 $temp .= "  attr->in_req[$idx] = ".$op."_reg_req_in_".$idx.";\n";
166                                         }
167                                 }
168
169                                 if (@out) {
170                                         $temp .= "\n  /* allocate memory for OUT register requirements and assigned registers */\n";
171                                         $temp .= "  attr->out_req    = calloc(".($#out + 1).", sizeof(arch_register_req_t *)); /* space for out requirements */\n";
172                                         $temp .= "  attr->slots      = calloc(".($#out + 1).", sizeof(arch_register_t *));     /* space for assigned registers */\n";
173                                         for ($idx = 0; $idx <= $#out; $idx++) {
174                                                 $temp .= "  attr->out_req[$idx] = ".$op."_reg_req_out_".$idx.";\n";
175                                         }
176                                         $temp .= "  attr->n_res      = ".($#out + 1).";\n";
177                                 }
178                                 else {
179                                         $temp .= "  attr->n_res      = 0;\n";
180                                 }
181                         }
182
183                         $temp .= "\n  return res;\n";
184
185                         push(@obst_constructor, $temp);
186                 }
187                 else { # user defined constructor
188                         push(@obst_constructor, $n{"rd_constructor"});
189                 }
190
191                 # close constructor function
192                 push(@obst_constructor, "}\n\n");
193
194         } # constructor creation
195
196         # set default values for state and flags if not given
197         $n{"state"}    = "pinned" if (! exists($n{"state"}));
198         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
199
200         push(@obst_new_irop, "\n  memset(&ops, 0, sizeof(ops));\n");
201         push(@obst_new_irop, "  ops.dump_node     = dump_node_$arch;\n");
202
203         if ($cmp_attr_func) {
204                 push(@obst_new_irop, "  ops.node_cmp_attr = cmp_attr_$op;\n");
205         }
206
207         $temp  = "  op_$op = new_ir_op(get_next_ir_opcode(), \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
208         $temp .= ", ".translate_arity($arity).", 0, sizeof(asmop_attr), &ops);\n";
209         push(@obst_new_irop, $temp);
210 }
211
212 # emit the code
213
214 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
215
216 print OUT @obst_cmp_attr;
217 print OUT "\n";
218 print OUT @obst_opvar;
219 print OUT "\n";
220 print OUT @obst_get_opvar;
221 print OUT "\n";
222 print OUT "int is_".$arch."_irn(const ir_node *node) {\n  if (".join(" ||\n      ", @obst_is_archirn).")\n    return 1;\n  else\n    return 0;\n}\n\n";
223 print OUT @obst_constructor;
224
225 print OUT<<ENDOFMAIN;
226 /**
227  * Creates the $arch specific firm operations
228  * needed for the assembler irgs.
229  */
230 void $arch\_create_opcodes(void) {
231 #define N   irop_flag_none
232 #define L   irop_flag_labeled
233 #define C   irop_flag_commutative
234 #define X   irop_flag_cfopcode
235 #define I   irop_flag_ip_cfopcode
236 #define F   irop_flag_fragile
237 #define Y   irop_flag_forking
238 #define H   irop_flag_highlevel
239 #define c   irop_flag_constlike
240
241         ir_op_ops ops;
242
243 ENDOFMAIN
244
245 print OUT @obst_new_irop;
246 print OUT "}\n";
247
248 close(OUT);
249
250 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
251
252 print OUT @obst_header;
253
254 close(OUT);
255
256 ###
257 # Translates numeric arity into string constant.
258 ###
259 sub translate_arity {
260         my $arity = shift;
261
262         if ($arity =~ /^\d+$/) {
263                 if    ($arity == 0) {
264                         return "oparity_zero";
265                 }
266                 elsif ($arity == 1) {
267                         return "oparity_unary";
268                 }
269                 elsif ($arity == 2) {
270                         return "oparity_binary";
271                 }
272                 elsif ($arity == 3) {
273                         return "oparity_trinary";
274                 }
275                 else {
276                         return "$arity";
277                 }
278         }
279         else {
280                 return "oparity_".$arity;
281         }
282 }