finish support for custom backend node attributes, separate x87 attributes from norma...
[libfirm] / ir / be / scripts / generate_new_opcodes.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
5 #
6 # This file is part of libFirm.
7 #
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
12 #
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
16 #
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE.
20 #
21
22 # This script generates the C code which creates the irop's and
23 # their coresponding node constructors for all operations in a given spec
24 # so they can be used as normal firm nodes.
25 # Creation: 2005/10/19
26 # $Id$
27
28 use strict;
29 use Data::Dumper;
30
31 my $specfile   = $ARGV[0];
32 my $target_dir = $ARGV[1];
33 my $state      = 1;
34 my $cur_op     = "";
35 my $line_nr    = 0;
36
37 our $arch;
38 our $additional_opcodes;
39 our %nodes;
40 our %cpu;
41 our $default_cmp_attr;
42 our $default_attr_type;
43 our %init_attr;
44
45 # include spec file
46
47 my $return;
48
49 no strict "subs";
50 unless ($return = do $specfile) {
51         die "couldn't parse $specfile: $@" if $@;
52         die "couldn't do $specfile: $!"    unless defined $return;
53         die "couldn't run $specfile"       unless $return;
54 }
55 use strict "subs";
56
57 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
58 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
59
60 if(!defined($default_attr_type)) {
61         $default_attr_type = "${arch}_attr_t";
62 }
63 if(!defined(%init_attr)) {
64         %init_attr = (
65                 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
66         );
67 }
68
69 #print Dumper(%nodes);
70 #print Dumper(%operands);
71
72 # create c code file from specs
73
74 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
75 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
76 my @obst_constructor; # stack for node constructor functions
77 my @obst_new_irop;    # stack for the new_ir_op calls
78 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
79 my @obst_header;      # stack for function prototypes
80 my @obst_is_archirn;  # stack for the is_$arch_irn() function
81 my @obst_cmp_attr;    # stack for the compare attribute functions
82 my @obst_proj;        # stack for the pn_ numbers
83 my $orig_op;
84 my $arity;
85 my $cmp_attr_func;
86 my $temp;
87 my $n_opcodes = 0;    # number of opcodes
88 my $ARITY_VARIABLE = -1;
89 my $ARITY_DYNAMIC  = -2;
90
91 # for registering additional opcodes
92 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
93
94 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
95
96 # create default compare function
97 if(defined($default_cmp_attr)) {
98         my $cmpcode = $default_cmp_attr;
99         push(@obst_cmp_attr, "static int default_cmp_attr(ir_node *a, ir_node *b) {\n");
100         if($cmpcode =~ m/attr_a/) {
101                 push(@obst_cmp_attr, "\t${default_attr_type} *attr_a = get_irn_generic_attr(a);\n");
102         }
103         if($cmpcode =~ m/attr_b/) {
104                 push(@obst_cmp_attr, "\t${default_attr_type} *attr_b = get_irn_generic_attr(b);\n");
105         }
106         push(@obst_cmp_attr, "\t${cmpcode}\n");
107         push(@obst_cmp_attr, "}\n\n");
108 }
109
110 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
111 foreach my $op (keys(%nodes)) {
112         my %n        = %{ $nodes{"$op"} };
113         my $known_mode;
114         my $num_outs = 0;
115         my $out_arity;
116         my @out_flags;
117
118         # determine arity
119         $arity = 0;
120         if(exists($n{"arity"})) {
121                 $arity = $n{"arity"};
122         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
123                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
124         } elsif (exists($n{"ins"})) {
125                 $arity = scalar(@{ $n{"ins"} });
126         }
127         if($arity eq "variable") {
128                 $arity = $ARITY_VARIABLE;
129         } elsif($arity eq "dynamic") {
130                 $arity = $ARITY_DYNAMIC;
131         }
132
133         # determine out arity
134         $out_arity = 0;
135         if(exists($n{"out_arity"})) {
136                 $out_arity = $n{"out_arity"};
137         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
138                 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
139         } elsif (exists($n{"outs"})) {
140                 $out_arity = scalar(@{ $n{"outs"} });
141         }
142         if($out_arity eq "variable") {
143                 $out_arity = $ARITY_VARIABLE;
144         } elsif($out_arity eq "dynamic") {
145                 $out_arity = $ARITY_DYNAMIC;
146         }
147
148         $orig_op = $op;
149         $op      = $arch."_".$op;
150         $temp    = "";
151
152         # define proj numbers and in numbers
153         if (exists($n{"outs"})) {
154                 undef my @outs;
155
156                 @outs     = @{ $n{"outs"} };
157                 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
158                         die "Op ${op} has different number of outs and out_arity\n";
159                 }
160
161                 $num_outs = $#outs + 1;
162
163                 push(@obst_proj, "\nenum pn_$op {\n");
164
165                 for (my $idx = 0; $idx <= $#outs; $idx++) {
166                         # check, if we have additional flags annotated to out
167                         if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
168                                 push(@out_flags, $1);
169                                 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
170                         }
171                         push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
172                 }
173
174                 push(@obst_proj, "};\n");
175                 # outs have names, it must be a mode_T node
176                 $known_mode = "mode_T";
177         }
178         if (exists($n{"ins"})) {
179                 undef my @ins;
180
181                 @ins = @{ $n{"ins"} };
182                 if($arity >= 0 && scalar(@ins) != $arity) {
183                         die "Op ${op} has different number of ins and arity\n";
184                 }
185
186                 push(@obst_proj, "\nenum n_$op {\n");
187
188                 for (my $idx = 0; $idx <= $#ins; $idx++) {
189                         push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
190                 }
191
192                 push(@obst_proj, "};\n");
193         }
194
195         # determine mode
196         if (exists($n{"mode"})) {
197                 $known_mode = $n{"mode"};
198         }
199
200         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
201         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
202         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
203
204         push(@obst_is_archirn, "is_$op(node)");
205
206         push(@obst_header, "extern ir_op *op_$op;\n");
207         push(@obst_header, "ir_op *get_op_$op(void);\n");
208         push(@obst_header, "int is_$op(const ir_node *n);\n");
209
210         my $attr_type= $n{"attr_type"};
211         if(!defined($attr_type)) {
212                 $attr_type = $default_attr_type;
213         }
214
215         # determine compare function
216         my $cmp_attr_func;
217         if(defined($default_cmp_attr)) {
218                 $cmp_attr_func = "default_cmp_attr";
219         }
220         if (exists($n{"cmp_attr"})) {
221                 my $cmpcode = $n{"cmp_attr"};
222
223                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
224                 if($cmpcode =~ m/attr_a/) {
225                         push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
226                 }
227                 if($cmpcode =~ m/attr_b/) {
228                         push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
229                 }
230                 push(@obst_cmp_attr, "\t${cmpcode}\n");
231                 push(@obst_cmp_attr, "}\n\n");
232
233                 $cmp_attr_func = "cmp_attr_${op}";
234         }
235
236         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
237                 # we explicitly skip the constructor if the specification entry says NONE
238         } else {
239                 my $comment = $n{"comment"};
240                 if(!exists($n{"comment"})) {
241                         $comment = "construct ${orig_op} node";
242                 }
243                 $comment =
244                         "/**\n".
245                         " * ${comment}\n".
246                         " */\n";
247
248                 push(@obst_constructor, $comment);
249
250                 # create constructor head
251                 my $complete_args = "";
252                 $temp             = "";
253
254                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
255                 if (!exists($n{"args"})) { # default args
256                         if ($arity == $ARITY_VARIABLE) {
257                                 $complete_args = ", int arity, ir_node *in[]";
258                         } elsif ($arity == $ARITY_DYNAMIC) {
259                                 $complete_args = "";
260                         } else {
261                                 for (my $i = 0; $i < $arity; $i++) {
262                                         my $opname = "op${i}";
263                                         if (exists($n{"ins"})) {
264                                                 my @ins = @{ $n{"ins"} };
265                                                 $opname = $ins[$i];
266                                         }
267
268                                         $complete_args .= ", ir_node *${opname}";
269                                 }
270                         }
271                         if ($out_arity == $ARITY_VARIABLE) {
272                                 $complete_args .= ", int n_res";
273                         }
274
275                         if (!defined($known_mode)) {
276                                 $complete_args .= ", ir_mode *mode";
277                         }
278                 } else { # user defined args
279                         for my $href (@{ $n{"args"} }) {
280                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
281                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
282                         }
283                 }
284
285                 # we have additional attribute arguements
286                 if (exists($n{"attr"})) {
287                         $complete_args .= ", ".$n{"attr"};
288                 }
289
290                 $temp .= "$complete_args)";
291                 push(@obst_constructor, $temp."\n{\n");
292                 push(@obst_header, $comment);
293                 push(@obst_header, $temp.";\n");
294
295                 # emit constructor code
296                 if (!exists($n{"rd_constructor"})) { # default constructor
297                         $temp  = "\tir_node  *res;\n";
298                         $temp .= "\tir_op    *op      = op_${op};\n";
299                         $temp .= "\tint       flags   = 0;\n";
300
301                         if($arity == $ARITY_DYNAMIC) {
302                                 $temp .= "\tint        arity   = -1;\n";
303                                 $temp .= "\tir_node  **in      = NULL;\n";
304                         } elsif($arity == $ARITY_VARIABLE) {
305                         } else {
306                                 $temp .= "\tint       arity   = $arity;\n";
307                                 if($arity > 0) {
308                                         $temp .= "\tir_node  *in[$arity];\n";
309                                 } else {
310                                         $temp .= "\tir_node **in    = NULL;\n";
311                                 }
312                         }
313                         if($out_arity == $ARITY_DYNAMIC) {
314                                 $temp .= "\tint       n_res   = -1;\n";
315                         } elsif($out_arity == $ARITY_VARIABLE) {
316                         } else {
317                                 $temp .= "\tint       n_res   = ${out_arity};\n";
318                         }
319
320                         my $latency = $n{"latency"};
321                         if (!defined($latency)) {
322                                 $latency = 1;
323                         }
324                         $temp .= "\tunsigned  latency = ${latency};\n";
325
326                         if (defined($known_mode)) {
327                                 $temp .= "\tir_mode  *mode    = ${known_mode};\n";
328                         }
329
330                         # set up static variables for cpu execution unit assigments
331                         if (exists($n{"units"})) {
332                                 $temp .= gen_execunit_list_initializer($n{"units"});
333                         } else {
334                                 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
335                         }
336
337                         undef my $in_req_var;
338                         undef my $out_req_var;
339
340                         # set up static variables for requirements and registers
341                         if (exists($n{"reg_req"})) {
342                                 my %req = %{ $n{"reg_req"} };
343                                 my $idx;
344
345                                 undef my @in;
346                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
347                                 undef my @out;
348                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
349
350                                 if (@in) {
351                                         if($arity >= 0 && scalar(@in) != $arity) {
352                                                 die "Arity and number of in requirements don't match for ${op}\n";
353                                         }
354
355                                         $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
356                                         $temp .= "\t{\n";
357                                         for ($idx = 0; $idx <= $#in; $idx++) {
358                                                 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
359                                         }
360                                         $temp .= "\t};\n";
361                                 } else {
362                                         if($arity > 0) {
363                                                 die "need in requirements for ${op}\n";
364                                         }
365                                         $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
366                                 }
367
368                                 if (@out) {
369                                         if($out_arity >= 0 && scalar(@out) != $out_arity) {
370                                                 die "Out-Arity and number of out requirements don't match for ${op}\n";
371                                         }
372
373                                         $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
374                                         $temp .= "\t{\n";
375                                         for ($idx = 0; $idx <= $#out; $idx++) {
376                                                 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
377                                         }
378                                         $temp .= "\t};\n";
379                                 } else {
380                                         if($out_arity > 0) {
381                                                 die "need out requirements for ${op}\n";
382                                         }
383                                         $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
384                                 }
385                         } else {
386                                 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
387                                 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
388                         }
389                         if(exists($n{"init_attr"})) {
390                                 $temp .= "\t${attr_type} *attr;\n";
391                         }
392
393                         $temp .= "\n";
394
395                         if($arity > 0) {
396                                 $temp .= "\t/* construct in array */\n";
397                                 for (my $i = 0; $i < $arity; $i++) {
398                                         my $opname = "op${i}";
399                                         if (exists($n{"ins"})) {
400                                                 my @ins = @{ $n{"ins"} };
401                                                 $opname = $ins[$i];
402                                         }
403
404                                         $temp .= "\tin[${i}] = ${opname};\n";
405                                 }
406                                 $temp .= "\n";
407                         }
408
409                         # set flags
410                         if (exists($n{"irn_flags"})) {
411                                 $temp .= "\t/* flags */\n";
412                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
413                                         if ($flag eq "R") {
414                                                 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
415                                         } elsif ($flag eq "N") {
416                                                 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
417                                         } elsif ($flag eq "I") {
418                                                 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
419                                         } elsif ($flag eq "S") {
420                                                 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
421                                         }
422                                 }
423                                 $temp .= "\n";
424                         }
425
426                         $temp .= "\t/* create node */\n";
427                         $temp .= "\tassert(op != NULL);\n";
428                         $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
429                         $temp .= "\n";
430
431                         $temp .= "\t/* init node attributes */\n";
432                         # lookup init function
433                         my $attr_init_code = $init_attr{$attr_type};
434                         if(!defined($attr_init_code)) {
435                                 die "Couldn't find attribute initialisation code for type '${attr_type}'";
436                         }
437                         $temp .= "${attr_init_code}\n";
438                         $temp .= "\n";
439
440                         # set flags for outs
441                         if ($#out_flags >= 0) {
442                                 $temp .= "\t/* set flags for outs */\n";
443                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
444                                         my $flags  = "";
445                                         my $prefix = "";
446
447                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
448                                                 if ($flag eq "I") {
449                                                         $flags .= $prefix."arch_irn_flags_ignore";
450                                                         $prefix = " | ";
451                                                 }
452                                                 elsif ($flag eq "S") {
453                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
454                                                         $prefix = " | ";
455                                                 }
456                                         }
457
458                                         $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
459                                 }
460                                 $temp .= "\n";
461                         }
462
463
464                         if (exists($n{"init_attr"})) {
465                                 $temp .= "\tattr = get_$arch\_attr(res);\n";
466                                 $temp .= $n{"init_attr"}."\n";
467                         }
468
469                         $temp .= "\t/* optimize node */\n";
470                         $temp .= "\tres = optimize_node(res);\n";
471                         $temp .= "\tirn_vrfy_irg(res, irg);\n";
472                         $temp .= "\n";
473
474                         $temp .= "\treturn res;\n";
475
476                         push(@obst_constructor, $temp);
477                 }
478                 else { # user defined constructor
479                         push(@obst_constructor, $n{"rd_constructor"});
480                 }
481
482                 # close constructor function
483                 push(@obst_constructor, "}\n\n");
484         } # constructor creation
485
486         # set default values for state and flags if not given
487         $n{"state"}    = "floats" if (! exists($n{"state"}));
488         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
489
490
491         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
492         push(@obst_new_irop, "\tops.dump_node     = $arch\_dump_node;\n");
493
494         if (defined($cmp_attr_func)) {
495                 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
496         }
497
498         $n_opcodes++;
499         my $n_res = $out_arity;
500         if($n_res < 0) {
501                 $n_res = "20"; # hacky....
502         }
503         $temp  = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
504         $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
505         push(@obst_new_irop, $temp);
506         push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
507         push(@obst_enum_op, "\tiro_$op,\n");
508
509         push(@obst_header, "\n");
510 }
511 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
512 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
513 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
514 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
515
516 # emit the code
517
518 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
519
520 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
521 print OUT @obst_cmp_attr;
522 print OUT "\n";
523 print OUT @obst_opvar;
524 print OUT "\n";
525 print OUT @obst_get_opvar;
526 print OUT "\n";
527
528 print OUT<<EOF;
529
530 static int $arch\_opcode_start = -1;
531 static int $arch\_opcode_end   = -1;
532
533 EOF
534
535 # build the FOURCC arguments from $arch
536
537 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
538
539 if (length($arch) >= 1) {
540         $a = uc(substr($arch, 0, 1));
541 }
542
543 if (length($arch) >= 2) {
544         $b = uc(substr($arch, 1, 1));
545 }
546
547 if (length($arch) >= 3) {
548         $c = uc(substr($arch, 2, 1));
549 }
550
551 if (length($arch) >= 4) {
552         $d = uc(substr($arch, 3, 1));
553 }
554
555 print OUT<<ENDOFISIRN;
556
557 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
558 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
559
560 /** Return the opcode number of the first $arch opcode. */
561 int get_$arch\_opcode_first(void) {
562         return $arch\_opcode_start;
563 }
564
565 /** Return the opcode number of the last $arch opcode + 1. */
566 int get_$arch\_opcode_last(void) {
567         return $arch\_opcode_end;
568 }
569
570 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
571 int is_$arch\_op(const ir_op *op) {
572         return get_op_tag(op) == &$arch\_op_tag;
573 }
574
575 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
576 int is_$arch\_irn(const ir_node *node) {
577         return is_$arch\_op(get_irn_op(node));
578 }
579
580 int get_$arch\_irn_opcode(const ir_node *node) {
581         if (is_$arch\_irn(node))
582                 return get_irn_opcode(node) - $arch\_opcode_start;
583         return -1;
584 }
585
586 ENDOFISIRN
587
588 print OUT @obst_constructor;
589
590 print OUT<<ENDOFMAIN;
591 /**
592  * Creates the $arch specific Firm machine operations
593  * needed for the assembler irgs.
594  */
595 void $arch\_create_opcodes(void) {
596 #define N   irop_flag_none
597 #define L   irop_flag_labeled
598 #define C   irop_flag_commutative
599 #define X   irop_flag_cfopcode
600 #define I   irop_flag_ip_cfopcode
601 #define F   irop_flag_fragile
602 #define Y   irop_flag_forking
603 #define H   irop_flag_highlevel
604 #define c   irop_flag_constlike
605 #define K   irop_flag_keep
606 #define M   irop_flag_machine
607 #define O   irop_flag_machine_op
608 #define R   (irop_flag_user << 0)
609
610         ir_op_ops  ops;
611         int        cur_opcode;
612         static int run_once = 0;
613
614         if (run_once)
615                 return;
616         run_once = 1;
617
618         cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
619
620         $arch\_opcode_start = cur_opcode;
621 ENDOFMAIN
622
623 print OUT @obst_new_irop;
624 print OUT "\n";
625 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
626 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
627 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
628 print OUT ";\n";
629 print OUT "}\n";
630
631 close(OUT);
632
633 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
634
635 my $creation_time = localtime(time());
636 my $tmp = uc($arch);
637
638 print OUT<<EOF;
639 /**
640  * \@file
641  * \@brief Function prototypes for the new opcode functions.
642  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
643  *        Edit $specfile instead.
644  *        created by: $0 $specfile $target_dir
645  * \@date  $creation_time
646  */
647 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
648 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
649
650 EOF
651
652 print OUT @obst_enum_op;
653 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
654 print OUT "int get_$arch\_opcode_first(void);\n";
655 print OUT "int get_$arch\_opcode_last(void);\n";
656 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
657 print OUT @obst_header;
658 print OUT @obst_proj;
659
660 print OUT <<EOF;
661 #endif
662 EOF
663
664 close(OUT);
665
666 ###
667 # Translates numeric arity into string constant.
668 ###
669 sub translate_arity {
670         my $arity = shift;
671
672         if ($arity =~ /^\d+$/) {
673                 if    ($arity == 0) {
674                         return "oparity_zero";
675                 }
676                 elsif ($arity == 1) {
677                         return "oparity_unary";
678                 }
679                 elsif ($arity == 2) {
680                         return "oparity_binary";
681                 }
682                 elsif ($arity == 3) {
683                         return "oparity_trinary";
684                 }
685                 else {
686                         return "oparity_any";
687                 }
688         } elsif ($arity == $ARITY_VARIABLE) {
689                 return "oparity_variable";
690         } elsif ($arity == $ARITY_DYNAMIC) {
691                 return "oparity_dynamic";
692         } else {
693                 die "Unknown arity $arity";
694         }
695 }
696
697 ###
698 # Return the list of pointers for the given execution units.
699 ###
700 sub gen_execunit_list_initializer {
701         my $units   = shift;
702         my $uc_arch = uc($arch);
703         my $ret     = "";
704         my $ret2    = "";
705         my %init;
706
707         foreach my $unit (@{ $units }) {
708                 if ($unit eq "DUMMY") {
709                         push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
710                 }
711                 elsif (exists($cpu{"$unit"})) {
712                         # operation can be executed on all units of this type
713                         # -> add them all
714                         my $tp_name = "$arch\_execution_units_$unit";
715                         my $idx     = 0;
716                         foreach (@{ $cpu{"$unit"} }) {
717                                 next if ($idx++ == 0);  # skip first element (it's not a unit)
718                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
719                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
720                         }
721                 }
722                 else {
723                         # operation can be executed only a certain unit
724                         # -> find corresponding unit type
725                         my $found = 0;
726 TP_SEARCH:      foreach my $cur_type (keys(%cpu)) {
727                                 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
728                                         if ($unit eq $cur_unit) {
729                                                 my $tp_name   = "$arch\_execution_units_$cur_type";
730                                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
731                                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
732                                                 $found = 1;
733                                                 last TP_SEARCH;
734                                         }
735                                 }
736                         }
737
738                         if (! $found) {
739                                 print STDERR "Invalid execution unit $unit specified!\n";
740                         }
741                 }
742         }
743
744         # prepare the 2-dim array init
745         foreach my $key (keys(%init)) {
746                 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
747                 $ret .= "\t{\n";
748                 foreach (@{ $init{"$key"} }) {
749                         $ret .= "$_,\n";
750                 }
751                 $ret .= "\t\tNULL\n";
752                 $ret .= "\t};\n";
753                 $ret2 .= "\t\tallowed_units_$key,\n";
754         }
755         $ret2 .= "\t\tNULL\n";
756
757         $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
758         $ret .= "\t{\n";
759         $ret .= $ret2;
760         $ret .= "\t};\n";
761
762         return $ret;
763 }