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