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