Remove the never read flag irop_flag_dump_noinput.
[libfirm] / ir / be / scripts / generate_new_opcodes.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2008 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
27 use strict;
28 use Data::Dumper;
29
30 my $specfile   = $ARGV[0];
31 my $target_dir = $ARGV[1];
32 my $state      = 1;
33 my $cur_op     = "";
34 my $line_nr    = 0;
35
36 our $arch;
37 our $additional_opcodes;
38 our %nodes;
39 our %operands;
40 our %cpu;
41 our $default_op_attr_type;
42 our $default_attr_type;
43 our $default_cmp_attr;
44 our $default_copy_attr;
45 our %init_attr;
46 our $custom_init_attr_func;
47 our %compare_attr;
48 our %copy_attr;
49 our %reg_classes;
50 our %custom_irn_flags;
51
52 # include spec file
53
54 my $return;
55
56 no strict "subs";
57 unless ($return = do $specfile) {
58         die "Fatal error: couldn't parse $specfile: $@" if $@;
59         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
60         die "Fatal error: couldn't run $specfile"       unless $return;
61 }
62 use strict "subs";
63
64 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
65 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
66
67 if(!defined($default_attr_type)) {
68         $default_attr_type = "${arch}_attr_t";
69 }
70 if(! %init_attr) {
71         %init_attr = (
72                 "$default_attr_type" => "\tinit_${arch}_attributes(res, irn_flags_, in_reqs, n_res);",
73         );
74 }
75 if(!defined($default_cmp_attr)) {
76         $default_cmp_attr = "${arch}_compare_attr";
77 }
78 if(! %compare_attr) {
79         %compare_attr = (
80                 "${default_attr_type}" => "${default_cmp_attr}",
81         );
82 }
83
84 # Operands are really just nodes with some special constraints, we check
85 # these and create new entries in the nodes hashmap
86 foreach my $op (keys(%operands)) {
87         my %operand = %{ $operands{"$op"} };
88         my %op_node;
89
90         # constraints
91         if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
92         if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
93         if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
94         if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
95         if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
96
97
98         foreach my $flag (keys(%operand)) {
99                 $op_node{$flag} = $operand{$flag};
100         }
101         $op_node{op_flags} = "O";
102         $op_node{cmp_attr} = 'return 1;';
103         $op_node{mode}     = 'mode_ANY';
104
105         $nodes{$op} = \%op_node;
106 }
107
108 #print Dumper(%nodes);
109 #print Dumper(%operands);
110
111 # create c code file from specs
112
113 my @obst_limit_func;
114 my @obst_reg_reqs;
115 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
116 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
117 my $obst_constructor; # stack for node constructor functions
118 my @obst_new_irop;    # stack for the new_ir_op calls
119 my @obst_free_irop;   # stack for free_ir_op calls
120 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
121 my $obst_header;      # stack for function prototypes
122 my @obst_is_archirn;  # stack for the is_$arch_irn() function
123 my @obst_cmp_attr;    # stack for the compare attribute functions
124 my $obst_proj = "";   # stack for the pn_ numbers
125 my $orig_op;
126 my $arity;
127 my $cmp_attr_func;
128 my $temp;
129 my $n_opcodes = 0;    # number of opcodes
130 my $ARITY_VARIABLE = -1;
131 my $ARITY_DYNAMIC  = -2;
132 my %requirements = ();
133 my %limit_bitsets = ();
134 my %reg2class = ();
135 my %regclass2len = ();
136
137 # build register->class hashes
138 foreach my $class_name (keys(%reg_classes)) {
139         my @class         = @{ $reg_classes{"$class_name"} };
140         my $old_classname = $class_name;
141
142         pop(@class);
143
144         $class_name = $arch."_".$class_name;
145
146         my $idx = 0;
147         foreach (@class) {
148                 $reg2class{$_->{name}} = {
149                         "class" => $old_classname,
150                         "index" => $idx
151                 };
152                 $idx++;
153         }
154
155         $regclass2len{$old_classname} = $idx;
156 }
157
158
159 # for registering additional opcodes
160 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
161
162 $obst_header .= "void ${arch}_create_opcodes(const arch_irn_ops_t *be_ops);\n";
163 $obst_header .= "void ${arch}_free_opcodes(void);\n";
164
165 sub create_constructor {
166         my $op   = shift;
167         my $name = shift;
168         my $n    = shift;
169         my $on   = shift;
170         my $known_mode;
171
172         my $suffix = "";
173         if ($name ne "") {
174                 $suffix = "_${name}";
175         }
176
177         # determine mode
178         if (exists($n->{mode})) {
179                 $known_mode = $n->{mode};
180         }
181
182         # determine arity
183         my $arity = 0;
184         if(exists($n->{"arity"})) {
185                 $arity = $n->{"arity"};
186         } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"in"})) {
187                 $arity = scalar(@{ $n->{"reg_req"}{"in"} });
188         } elsif (exists($n->{"ins"})) {
189                 $arity = scalar(@{ $n->{"ins"} });
190         }
191         if($arity eq "variable") {
192                 $arity = $ARITY_VARIABLE;
193         } elsif($arity eq "dynamic") {
194                 $arity = $ARITY_DYNAMIC;
195         }
196
197         # determine out arity
198         my $out_arity = 0;
199         if(exists($n->{"out_arity"})) {
200                 $out_arity = $n->{"out_arity"};
201         } elsif (exists($n->{"reg_req"}) && exists($n->{"reg_req"}{"out"})) {
202                 $out_arity = scalar(@{ $n->{"reg_req"}{"out"} });
203         } elsif (exists($n->{"outs"})) {
204                 $out_arity = scalar(@{ $n->{"outs"} });
205         }
206         if($out_arity eq "variable") {
207                 $out_arity = $ARITY_VARIABLE;
208         } elsif($out_arity eq "dynamic") {
209                 $out_arity = $ARITY_DYNAMIC;
210         }
211         if ($out_arity != 0 && $out_arity != 1 && !defined($known_mode)) {
212                 $known_mode = "mode_T";
213         }
214
215         my $comment = $n->{"comment"};
216         if(!exists($n->{"comment"})) {
217                 $comment = "construct ${orig_op} node";
218         }
219         $comment =
220                 "/**\n".
221                 " * ${comment}\n".
222                 " */\n";
223
224         $obst_constructor .= $comment;
225
226         # create constructor head
227         my $complete_args = "";
228         $temp             = "";
229
230         $temp = "ir_node *new_bd_${arch}_${op}${suffix}(dbg_info *dbgi, ir_node *block";
231         if (!exists($n->{"args"})) { # default args
232                 if ($arity == $ARITY_VARIABLE) {
233                         $complete_args = ", int arity, ir_node *in[]";
234                 } elsif ($arity == $ARITY_DYNAMIC) {
235                         $complete_args = "";
236                 } else {
237                         for (my $i = 0; $i < $arity; $i++) {
238                                 my $opname = "op${i}";
239                                 if (exists($n->{"ins"})) {
240                                         my @ins = @{ $n->{"ins"} };
241                                         $opname = $ins[$i];
242                                 }
243
244                                 $complete_args .= ", ir_node *${opname}";
245                         }
246                 }
247                 if ($out_arity == $ARITY_VARIABLE) {
248                         $complete_args .= ", int n_res";
249                 }
250
251                 if (!defined($known_mode)) {
252                         $complete_args .= ", ir_mode *mode";
253                 }
254         } else { # user defined args
255                 for my $href (@{ $n->{"args"} }) {
256                         $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
257                         $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
258                 }
259         }
260
261         # we have additional attribute arguements
262         if (exists($n->{"attr"})) {
263                 $complete_args .= ", ".$n->{"attr"};
264         }
265
266         $temp .= "$complete_args)";
267         $obst_constructor .= "${temp}\n{\n";
268
269         $obst_header .= $comment;
270         $obst_header .= "${temp};\n";
271
272         # emit constructor code
273         $temp = <<EOF;
274         ir_graph         *irg        = get_irn_irg(block);
275         ir_op            *op         = op_${arch}_${op};
276         arch_irn_flags_t  irn_flags_ = arch_irn_flags_none;
277         ir_node          *res;
278         backend_info_t   *info;
279 EOF
280
281         if($arity == $ARITY_DYNAMIC) {
282                 $temp .= <<EOF;
283         int             arity   = -1;
284         ir_node       **in      = NULL;
285 EOF
286         } elsif($arity == $ARITY_VARIABLE) {
287         } else {
288                 $temp .= <<EOF;
289         int             arity   = $arity;
290 EOF
291                 if($arity > 0) {
292                         $temp .= <<EOF;
293         ir_node        *in[$arity];
294 EOF
295                 } else {
296                         $temp .= <<EOF;
297         ir_node       **in      = NULL;
298 EOF
299                 }
300         }
301         if($out_arity == $ARITY_DYNAMIC) {
302                 $temp .= <<EOF;
303         int             n_res   = -1;
304 EOF
305         } elsif($out_arity == $ARITY_VARIABLE) {
306         } else {
307                 $temp .= <<EOF;
308         int             n_res   = ${out_arity};
309 EOF
310         }
311
312         if (defined($known_mode)) {
313                 $temp .= <<EOF;
314         ir_mode        *mode    = ${known_mode};
315 EOF
316         }
317
318         undef my $in_req_var;
319         undef my $out_req_var;
320
321         my $set_out_reqs = "";
322
323         # set up static variables for requirements and registers
324         if (exists($n->{"reg_req"})) {
325                 my %req = %{ $n->{"reg_req"} };
326                 my $idx;
327
328                 undef my @in;
329                 @in = @{ $req{"in"} } if (exists($req{"in"}));
330                 undef my @out;
331                 @out = @{ $req{"out"} } if exists(($req{"out"}));
332
333                 for(my $idx = 0; $idx < $#in; $idx++) {
334                         my $req = $in[$idx];
335                         generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
336                 }
337                 for(my $idx = 0; $idx < $#out; $idx++) {
338                         my $req = $out[$idx];
339                         generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
340                 }
341
342                 if (@in) {
343                         if($arity >= 0 && scalar(@in) != $arity) {
344                                 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
345                         }
346
347                         $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
348                         $temp .= "\t{\n";
349                         for ($idx = 0; $idx <= $#in; $idx++) {
350                                 my $req = $in[$idx];
351                                 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 1);
352                                 $temp .= "\t\t& ${reqstruct},\n";
353                         }
354                         $temp .= "\t};\n";
355                 } else {
356                         if($arity > 0) {
357                                 die "Fatal error: 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 "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
365                         }
366
367                         for ($idx = 0; $idx <= $#out; $idx++) {
368                                 my $req = $out[$idx];
369                                 my $reqstruct = generate_requirements($req, $n, "${arch}_${op}", $idx, 0);
370                                 $set_out_reqs .= <<EOF;
371         info->out_infos[${idx}].req = &${reqstruct};
372 EOF
373                         }
374                 } else {
375                         if($out_arity > 0) {
376                                 die "Fatal error: need out requirements for ${op}\n";
377                         }
378                 }
379         } else {
380                 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
381         }
382         my $attr_type = $on->{attr_type};
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                 my %known_irn_flags = (
407                         "none"             => "arch_irn_flags_none",
408                         "dont_spill"       => "arch_irn_flags_dont_spill",
409                         "rematerializable" => "arch_irn_flags_rematerializable",
410                         "modify_flags"     => "arch_irn_flags_modify_flags",
411                         "simple_jump"      => "arch_irn_flags_simple_jump",
412                         "not_scheduled"    => "arch_irn_flags_not_scheduled",
413                 );
414                 if (%custom_irn_flags) {
415                         %known_irn_flags = (%known_irn_flags, %custom_irn_flags);
416                 }
417                 foreach my $flag (@{$n->{"irn_flags"}}) {
418                         if (not defined($known_irn_flags{$flag})) {
419                                 print STDERR "WARNING: irn_flag '$flag' in opcode $op is unknown\n";
420                         } else {
421                                 $temp .= "\tirn_flags_ |= " . $known_irn_flags{$flag} . ";\n";
422                         }
423                 }
424                 $temp .= "\n";
425         }
426
427         # lookup init function
428         my $attr_init_code = "(void)in;(void)irn_flags_;(void)in_reqs;(void)n_res;";
429         if ($attr_type ne "") {
430                 $attr_init_code = $init_attr{$attr_type};
431                 if(!defined($attr_init_code)) {
432                         die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
433                 }
434         }
435         my $custominit = "";
436         if(defined($custom_init_attr_func)) {
437                 $custominit .= &$custom_init_attr_func($n, $on, "${arch}_${op}");
438         }
439         if(defined($n->{custominit})) {
440                 $custominit .= $n->{custominit};
441         }
442
443         $temp .= <<EOF;
444         /* create node */
445         assert(op != NULL);
446         res = new_ir_node(dbgi, irg, block, op, mode, arity, in);
447
448         /* init node attributes */
449         ${attr_init_code}
450         ${custominit}
451         info = be_get_info(res);
452         (void) info; /* avoid potential warning */
453 ${set_out_reqs}
454
455 EOF
456
457         if (exists($n->{"init_attr"})) {
458                 $temp .= "\tattr = (${attr_type}*)get_irn_generic_attr(res);\n";
459                 $temp .= "\t(void) attr; /* avoid potential warning */\n";
460                 $temp .= "\t".$n->{"init_attr"}."\n";
461         }
462
463         $temp .= <<EOF;
464         /* optimize node */
465         res = optimize_node(res);
466         irn_verify_irg(res, irg);
467
468         return res;
469 EOF
470
471         $obst_constructor .= $temp;
472
473         # close constructor function
474         $obst_constructor .= "}\n\n";
475 }
476
477 push(@obst_enum_op, "typedef enum ${arch}_opcodes {\n");
478 foreach my $op (keys(%nodes)) {
479         my %n        = %{ $nodes{"$op"} };
480         my $known_mode;
481         my $num_outs = 0;
482         my $out_arity;
483         my @out_flags;
484
485         # determine arity
486         $arity = 0;
487         if(exists($n{"arity"})) {
488                 $arity = $n{"arity"};
489         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
490                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
491         } elsif (exists($n{"ins"})) {
492                 $arity = scalar(@{ $n{"ins"} });
493         }
494         if($arity eq "variable") {
495                 $arity = $ARITY_VARIABLE;
496         } elsif($arity eq "dynamic") {
497                 $arity = $ARITY_DYNAMIC;
498         }
499
500         # determine out arity
501         $out_arity = 0;
502         if(exists($n{"out_arity"})) {
503                 $out_arity = $n{"out_arity"};
504         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
505                 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
506         } elsif (exists($n{"outs"})) {
507                 $out_arity = scalar(@{ $n{"outs"} });
508         }
509         if($out_arity eq "variable") {
510                 $out_arity = $ARITY_VARIABLE;
511         } elsif($out_arity eq "dynamic") {
512                 $out_arity = $ARITY_DYNAMIC;
513         }
514
515         $orig_op = $op;
516         $op      = $arch."_".$op;
517         $temp    = "";
518
519         # define proj numbers and in numbers
520         if (exists($n{"outs"})) {
521                 undef my @outs;
522
523                 @outs = @{ $n{"outs"} };
524                 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
525                         die "Fatal error: Op ${op} has different number of outs and out_arity\n";
526                 }
527
528                 $num_outs = $#outs + 1;
529
530                 if ($num_outs > 0) {
531                         $obst_proj .= "\nenum pn_${op} {\n";
532
533                         for (my $idx = 0; $idx <= $#outs; $idx++) {
534                                 # check, if we have additional flags annotated to out
535                                 if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
536                                         push(@out_flags, $1);
537                                         $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
538                                 }
539                                 $obst_proj .= "\tpn_${op}_".$outs[$idx]." = ${idx},\n";
540                         }
541
542                         $obst_proj .= "};\n";
543                 }
544                 # outs have names, it must be a mode_T node
545                 if (!defined($n{mode})) {
546                         $n{mode} = "mode_T";
547                 }
548         }
549         if (exists($n{"ins"})) {
550                 undef my @ins;
551
552                 @ins = @{ $n{"ins"} };
553                 if($arity >= 0 && scalar(@ins) != $arity) {
554                         die "Fatal error: Op ${op} has different number of ins and arity\n";
555                 }
556
557                 if ($#ins >= 0) {
558                         $obst_proj .= "\nenum n_$op {\n";
559                         for (my $idx = 0; $idx <= $#ins; $idx++) {
560                                 $obst_proj .= "\tn_${op}_".$ins[$idx]." = ${idx},\n";
561                         }
562                         $obst_proj .= "};\n";
563                 }
564         }
565
566         # Create opcode
567         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
568         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
569         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
570
571         push(@obst_is_archirn, "is_$op(node)");
572
573         $obst_header .= <<EOF;
574 extern ir_op *op_${op};
575 ir_op *get_op_${op}(void);
576 int is_${op}(const ir_node *n);
577 EOF
578
579         my $attr_type= $n{attr_type};
580         if(!defined($attr_type)) {
581                 $attr_type = $default_attr_type;
582                 $n{attr_type} = $attr_type;
583         }
584
585         # determine hash function
586         my $hash_func;
587         if (exists($n{"hash_func"})) {
588                 $hash_func = $n{"hash_func"};
589         }
590
591         # determine compare function
592         my $cmp_attr_func;
593         if (exists($n{"cmp_attr"})) {
594                 my $cmpcode = $n{"cmp_attr"};
595
596                 push(@obst_cmp_attr, "static int cmp_attr_$op(const ir_node *a, const ir_node *b) {\n");
597                 if($cmpcode =~ m/attr_a/) {
598                         push(@obst_cmp_attr, "\tconst ${attr_type} *attr_a = get_irn_generic_attr_const(a);\n");
599                 } else {
600                         push(@obst_cmp_attr, "\t(void) a;\n");
601                 }
602                 if($cmpcode =~ m/attr_b/) {
603                         push(@obst_cmp_attr, "\tconst ${attr_type} *attr_b = get_irn_generic_attr_const(b);\n");
604                 } else {
605                         push(@obst_cmp_attr, "\t(void) b;\n");
606                 }
607                 push(@obst_cmp_attr, "\t${cmpcode}\n");
608                 push(@obst_cmp_attr, "}\n\n");
609
610                 $cmp_attr_func = "cmp_attr_${op}";
611         } elsif ($attr_type eq "") {
612                 $cmp_attr_func = "NULL";
613         } else {
614                 if(defined($compare_attr{${attr_type}})) {
615                         $cmp_attr_func = $compare_attr{${attr_type}};
616                 } else {
617                         die "Fatal error: No compare function defined for ${attr_type} attributes.";
618                 }
619         }
620
621         my %constructors;
622         if (exists($n{constructors})) {
623                 %constructors = %{ $n{constructors} };
624         } else {
625                 # Create 1 default constructor
626                 my %constructor = ();
627                 foreach my $a ("comment", "ins", "outs", "args", "attr", "units",
628                                 "reg_req", "init_attr", "irn_flags", "mode", "arity",
629                                 "out_arity", "custominit") {
630                         if (defined($n{$a})) {
631                                 $constructor{$a} = $n{$a};
632                         }
633                 }
634                 %constructors = ( "" => \%constructor );
635         }
636
637         foreach my $constr (keys(%constructors)) {
638                 my %cstr = %{ $constructors{$constr} };
639                 # Copy some values from outer node if they don't exists in the constr
640                 foreach my $a ("ins", "outs", "irn_flags", "mode", "args", "attr",
641                                "custominit") {
642                         if (!defined($cstr{$a}) && defined($n{$a})) {
643                                 $cstr{$a} = $n{$a};
644                         }
645                 }
646                 create_constructor($orig_op, $constr, \%cstr, \%n);
647         }
648
649         # set default values for state and flags if not given
650         $n{"state"}     = "floats" if (! exists($n{"state"}));
651         $n{"op_flags"}  = ["none"] if (! exists($n{"op_flags"}));
652         $n{"dump_func"} = "${arch}_dump_node" if (!exists($n{"dump_func"}));
653         my $dump_func = $n{"dump_func"};
654
655         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
656         push(@obst_new_irop, "\tops.be_ops        = be_ops;\n");
657         push(@obst_new_irop, "\tops.dump_node     = ${dump_func};\n");
658
659         if (defined($cmp_attr_func)) {
660                 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
661         }
662         my $copy_attr_func = $copy_attr{$attr_type};
663         if (!defined($copy_attr_func)) {
664                 if ($attr_type eq "") {
665                         $copy_attr_func = "NULL";
666                 } else {
667                         $copy_attr_func = $default_copy_attr;
668                 }
669         }
670         if (defined($copy_attr_func)) {
671                 push(@obst_new_irop, "\tops.copy_attr = ${copy_attr_func};\n");
672         }
673         if (defined($hash_func)) {
674                 push(@obst_new_irop, "\tops.hash = ${hash_func};\n");
675         }
676
677         my %known_flags = map { $_ => 1 } (
678                 "none", "commutative", "cfopcode", "unknown_jump", "fragile",
679                 "forking", "highlevel", "constlike", "always_opt", "keep",
680                 "start_block", "uses_memory", "dump_noblock", "cse_neutral"
681         );
682         my $is_fragile = 0;
683         foreach my $flag (@{$n{"op_flags"}}) {
684                 if (not defined($known_flags{$flag})) {
685                         print STDERR "WARNING: Flag '$flag' in opcode $op is unknown\n";
686                 }
687                 if ($flag eq "fragile") {
688                         $is_fragile = 1;
689                 }
690         }
691         my @mapped = map { "irop_flag_$_" } @{$n{"op_flags"}};
692         my $op_flags = join('|', @mapped);
693
694         my $attr_size = "0";
695         if ($attr_type ne "") {
696                 $attr_size = "sizeof(${attr_type})"
697         }
698
699         $n_opcodes++;
700         $temp  = "\top = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", $op_flags";
701         $temp .= ", ".translate_arity($arity).", 0, ${attr_size}, &ops);\n";
702         push(@obst_new_irop, $temp);
703         if ($is_fragile) {
704                 push(@obst_new_irop, "\tir_op_set_memory_index(op, n_${op}_mem);\n");
705                 push(@obst_new_irop, "\tir_op_set_fragile_indices(op, pn_${op}_X_regular, pn_${op}_X_except);\n");
706         }
707         push(@obst_new_irop, "\tset_op_tag(op, $arch\_op_tag);\n");
708         if(defined($n{op_attr_init})) {
709                 push(@obst_new_irop, "\t".$n{op_attr_init}."\n");
710         }
711         push(@obst_new_irop, "\top_${op} = op;\n");
712
713         push(@obst_free_irop, "\tfree_ir_op(op_$op); op_$op = NULL;\n");
714
715         push(@obst_enum_op, "\tiro_$op,\n");
716
717         $obst_header .= "\n";
718 }
719 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
720 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
721 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
722 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
723
724 # emit the code
725
726 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
727
728 print OUT "#include \"gen_$arch\_regalloc_if.h\"\n";
729 print OUT "#include \"irverify_t.h\"\n";
730 print OUT "#include \"fourcc.h\"\n";
731 print OUT "\n";
732 print OUT @obst_cmp_attr;
733 print OUT "\n";
734 print OUT @obst_opvar;
735 print OUT "\n";
736 print OUT @obst_get_opvar;
737 print OUT "\n";
738
739 print OUT<<EOF;
740
741 static int $arch\_opcode_start = -1;
742 static int $arch\_opcode_end   = -1;
743
744 EOF
745
746 # build the FOURCC arguments from $arch
747
748 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
749
750 if (length($arch) >= 1) {
751         $a = uc(substr($arch, 0, 1));
752 }
753
754 if (length($arch) >= 2) {
755         $b = uc(substr($arch, 1, 1));
756 }
757
758 if (length($arch) >= 3) {
759         $c = uc(substr($arch, 2, 1));
760 }
761
762 if (length($arch) >= 4) {
763         $d = uc(substr($arch, 3, 1));
764 }
765
766 print OUT<<ENDOFISIRN;
767
768 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
769 #define $arch\_op_tag FOURCC('$a', '$b', '$c', '$d')
770
771 /** Return the opcode number of the first $arch opcode. */
772 int get_$arch\_opcode_first(void) {
773         return $arch\_opcode_start;
774 }
775
776 /** Return the opcode number of the last $arch opcode + 1. */
777 int get_$arch\_opcode_last(void) {
778         return $arch\_opcode_end;
779 }
780
781 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
782 int is_$arch\_op(const ir_op *op) {
783         return get_op_tag(op) == $arch\_op_tag;
784 }
785
786 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
787 int is_$arch\_irn(const ir_node *node) {
788         return is_$arch\_op(get_irn_op(node));
789 }
790
791 int get_$arch\_irn_opcode(const ir_node *node) {
792         if (is_$arch\_irn(node))
793                 return get_irn_opcode(node) - $arch\_opcode_start;
794         return -1;
795 }
796
797 ENDOFISIRN
798
799 print OUT <<END;
800 #ifdef BIT
801 #undef BIT
802 #endif
803 #define BIT(x)  (1 << (x))
804
805 END
806
807 print OUT @obst_limit_func;
808 print OUT "\n";
809
810 print OUT @obst_reg_reqs;
811 print OUT "\n";
812
813 print OUT<<ENDOFMAIN;
814 $obst_constructor
815
816 /**
817  * Creates the $arch specific Firm machine operations
818  * needed for the assembler irgs.
819  */
820 void $arch\_create_opcodes(const arch_irn_ops_t *be_ops)
821 {
822         ir_op_ops  ops;
823         ir_op     *op;
824         int        cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
825
826         $arch\_opcode_start = cur_opcode;
827 ENDOFMAIN
828
829 print OUT @obst_new_irop;
830 print OUT "\n";
831 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
832 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
833 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
834 print OUT ";\n";
835 print OUT <<ENDOFMAIN;
836 }
837
838 void $arch\_free_opcodes(void)
839 {
840 ENDOFMAIN
841
842 print OUT @obst_free_irop;
843
844 print OUT <<ENDOFMAIN;
845 }
846 ENDOFMAIN
847
848 close(OUT);
849
850 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
851
852 my $creation_time = localtime(time());
853 my $tmp = uc($arch);
854
855 print OUT<<EOF;
856 /**
857  * \@file
858  * \@brief Function prototypes for the new opcode functions.
859  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
860  *        Edit $specfile instead.
861  *        created by: $0 $specfile $target_dir
862  * \@date  $creation_time
863  */
864 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
865 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
866
867 EOF
868
869 print OUT @obst_enum_op;
870 print OUT <<EOF;
871 int is_${arch}_irn(const ir_node *node);
872 int is_${arch}_op(const ir_op *op);
873
874 int get_${arch}_opcode_first(void);
875 int get_${arch}_opcode_last(void);
876 int get_${arch}_irn_opcode(const ir_node *node);
877 ${obst_header}
878 ${obst_proj}
879
880 #endif
881 EOF
882
883 close(OUT);
884
885 ###
886 # Translates numeric arity into string constant.
887 ###
888 sub translate_arity {
889         my $arity = shift;
890
891         if ($arity =~ /^\d+$/) {
892                 if    ($arity == 0) {
893                         return "oparity_zero";
894                 }
895                 elsif ($arity == 1) {
896                         return "oparity_unary";
897                 }
898                 elsif ($arity == 2) {
899                         return "oparity_binary";
900                 }
901                 elsif ($arity == 3) {
902                         return "oparity_trinary";
903                 }
904                 else {
905                         return "oparity_any";
906                 }
907         } elsif ($arity == $ARITY_VARIABLE) {
908                 return "oparity_variable";
909         } elsif ($arity == $ARITY_DYNAMIC) {
910                 return "oparity_dynamic";
911         } else {
912                 die "Fatal error: Unknown arity $arity";
913         }
914 }
915
916 sub mangle_requirements {
917         my $reqs  = shift;
918         my $class = shift;
919         my $flags = shift;
920
921         my @alternatives = split(/ /, $reqs);
922         for(my $idx = 0; $idx < scalar(@alternatives); $idx++) {
923                 $alternatives[$idx] =~ s/!/not_/g;
924         }
925
926         @alternatives = sort @alternatives;
927
928         my $name = $class."_".join('_', @alternatives);
929         if (defined($flags)) {
930                 $flags =~ s/\|/_/g;
931                 $name .= "_$flags";
932         }
933
934         return $name;
935 }
936
937 ###
938 # Determines whether $name is a specified register class or not.
939 # @return 1 if name is register class, 0 otherwise
940 ###
941 sub is_reg_class {
942     my $name = shift;
943     return 1 if exists($reg_classes{"$name"});
944     return 0;
945 }
946
947 ###
948 # Returns the register class for a given register.
949 # @return class or undef
950 ###
951 sub get_reg_class {
952     my $reg = shift;
953     $reg = substr($reg, 1) if ($reg =~ /!.*/);
954     return $reg2class{"$reg"}{"class"} if (exists($reg2class{"$reg"}));
955     return undef;
956 }
957
958 ###
959 # Returns the index of a given register within its register class.
960 # @return index or undef
961 ###
962 sub get_reg_index {
963     my $reg = shift;
964     return $reg2class{"$reg"}{"index"} if (exists($reg2class{"$reg"}));
965     return undef;
966 }
967
968 ###
969 # Remember the register class for each index in the given requirements.
970 # We need this information for requirements like "in_sX" or "out_dX"
971 # @return array of classes corresponding to the requirement for each index
972 ###
973 sub build_inout_idx_class {
974         my $n     = shift;
975         my $op    = shift;
976         my $is_in = shift;
977         my @idx_class;
978
979         my $inout = ($is_in ? "in" : "out");
980
981         if (exists($n->{"reg_req"}{"$inout"})) {
982                 my @reqs = @{ $n->{"reg_req"}{"$inout"} };
983
984                 for (my $idx = 0; $idx <= $#reqs; $idx++) {
985                         my $class = undef;
986                         my ($req,) = split(/:/, $reqs[$idx]);
987
988                         if ($req eq "none") {
989                                 $class = "none";
990                         } elsif (is_reg_class($req)) {
991                                 $class = $req;
992                         } else {
993                                 my @regs = split(/ /, $req);
994 GET_CLASS:              foreach my $reg (@regs) {
995                                         if ($reg =~ /!?(in|out)\_r\d+/ || $reg =~ /!in/) {
996                                                 $class = "UNKNOWN_CLASS";
997                                         } else {
998                                                 $class = get_reg_class($reg);
999                                                 if (!defined $class) {
1000                                                         die("Fatal error: Could not get ".uc($inout)." register class for '$op' pos $idx (reg $reg) ... exiting.\n");
1001                                                 } else {
1002                                                         last GET_CLASS;
1003                                                 } # !defined class
1004                                         } # if (reg =~ ...
1005                                 } # foreach
1006                         } # if
1007
1008                         push(@idx_class, $class);
1009                 } # for
1010         } # if
1011
1012         return @idx_class;
1013 }
1014
1015 ###
1016 # Generates the function for a given $op and a given IN-index
1017 # which returns a subset of possible register from a register class
1018 # @return classname from which the subset is derived or undef and
1019 #         pos which corresponds to in/out reference position or undef
1020 ###
1021 sub build_subset_class_func {
1022         my $neg           = undef;
1023         my $class         = undef;
1024         my $has_limit     = 0;
1025         my $limit_name;
1026         my $same_pos      = 0;
1027         my $different_pos = 0;
1028         my $temp;
1029         my @obst_init;
1030         my @obst_limits;
1031         my @obst_ignore;
1032         my @limit_array;
1033         my $limit_reqs;   #used for name mangling
1034
1035         # build function header
1036         my $node  = shift;
1037         my $op    = shift;
1038         my $idx   = shift;
1039         my $is_in = shift;
1040         my @regs  = split(/ /, shift);
1041         my $flags = shift;
1042
1043         my @idx_class = build_inout_idx_class($node, $op, !$is_in);
1044
1045         # set/unset registers
1046 CHECK_REQS: foreach (@regs) {
1047                 if (!$is_in && /(!)?in_r(\d+)/) {
1048                         my $bit_pos = 1 << ($2 - 1);
1049                         if ($different_pos & $bit_pos) {
1050                                 if ($1) {
1051                                         print STDERR "duplicate !in constraint\n";
1052                                 } else {
1053                                         print STDERR "conflicting !in and in constraints\n";
1054                                 }
1055                                 return (undef, undef, undef, undef);
1056                         }
1057
1058                         if ($same_pos & $bit_pos) {
1059                                 if ($1) {
1060                                         print STDERR "conflicting !in and in constraints\n";
1061                                 } else {
1062                                         print STDERR "duplicate in constraint\n";
1063                                 }
1064                                 return (undef, undef, undef, undef);
1065                         }
1066
1067                         if ($1) {
1068                                 $different_pos |= $bit_pos;
1069                         } else {
1070                                 $same_pos      |= $bit_pos;
1071                         }
1072
1073                         $class = $idx_class[$2 - 1];
1074                         next CHECK_REQS;
1075                 }
1076
1077                 # check for negate
1078                 if (substr($_, 0, 1) eq "!") {
1079                         if (defined($neg) && $neg == 0) {
1080                                 # we have seen a positiv constraint as first one but this one is negative
1081                                 # this doesn't make sense
1082                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1083                                 return (undef, undef, undef, undef);
1084                         }
1085
1086                         if (!defined($neg)) {
1087                                 $has_limit = 1;
1088                         }
1089
1090                         $_   = substr($_, 1); # skip '!'
1091                         $neg = 1;
1092                 } else {
1093                         if (defined($neg) && $neg == 1) {
1094                                 # we have seen a negative constraint as first one but this one is positive
1095                                 # this doesn't make sense
1096                                 print STDERR "Mixed positive and negative constraints for the same slot are not allowed.\n";
1097                                 return (undef, undef, undef, undef);
1098                         }
1099
1100                         $has_limit = 1;
1101                         $neg = 0;
1102                 }
1103
1104                 # check if register belongs to one of the given classes
1105                 $temp = get_reg_class($_);
1106                 if (!defined($temp)) {
1107                         print STDERR "Unknown register '$_'!\n";
1108                         return (undef, undef, undef, undef);
1109                 }
1110
1111                 # set class
1112                 if (!defined($class)) {
1113                         $class = $temp;
1114                 } elsif ($class ne $temp) {
1115                         # all registers must belong to the same class
1116                         print STDERR "Registerclass mismatch. '$_' is not member of class '$class'.\n";
1117                         return (undef, undef, undef, undef);
1118                 }
1119
1120                 # calculate position inside the initializer bitfield (only 32 bits per
1121                 # element)
1122                 my $regidx = get_reg_index($_);
1123                 my $arrayp = $regidx / 32;
1124                 push(@{$limit_array[$arrayp]}, $_);
1125                 $limit_reqs .= "$_ ";
1126         }
1127
1128         # don't allow ignore regs in negative constraints
1129         if($neg) {
1130                 my @cur_class = @{ $reg_classes{"$class"} };
1131                 for (my $idx = 0; $idx <= $#cur_class; $idx++) {
1132                         if (defined($cur_class[$idx]{"type"}) && ($cur_class[$idx]{"type"} & 4)) {
1133                                 my $reg    = $cur_class[$idx]{"name"};
1134                                 my $regix  = get_reg_index($reg);
1135                                 my $arrayp = $regix / 32;
1136                                 push(@{$limit_array[$arrayp]}, $reg);
1137                                 $limit_reqs .= "$reg ";
1138                         }
1139                 }
1140         }
1141
1142         if ($has_limit == 1) {
1143                 $limit_name = "${arch}_limit_".mangle_requirements($limit_reqs, $class);
1144
1145                 if(defined($limit_bitsets{$limit_name})) {
1146                         $limit_name = $limit_bitsets{$limit_name};
1147                         return ($class, $limit_name, $same_pos, $different_pos);
1148                 }
1149
1150                 $limit_bitsets{$limit_name} = $limit_name;
1151
1152                 push(@obst_limit_func, "static const unsigned " . $limit_name . "[] = { ");
1153                 my $first = 1;
1154                 my $limitbitsetlen = $regclass2len{$class};
1155                 my $limitarraylen = ($limitbitsetlen+31) / 32;
1156                 for(my $i = 0; $i < $limitarraylen; $i++) {
1157
1158                         my $limitarraypart = $limit_array[$i];
1159                         if($first) {
1160                                 $first = 0;
1161                         } else {
1162                                 push(@obst_limit_func, ", ");
1163                         }
1164                         my $temp;
1165                         if($neg) {
1166                                 $temp = "0xFFFFFFFF";
1167                         }
1168                         foreach my $reg (@{$limitarraypart}) {
1169                                 if($neg) {
1170                                         $temp .= " & ~";
1171                                 } elsif(defined($temp)) {
1172                                         $temp .= " | ";
1173                                 }
1174                                 my $firstreg = uc($reg_classes{$class}[0]->{"name"});
1175                                 my $classuc = uc($class);
1176                                 my $reguc = uc($reg);
1177                                 $temp .= "BIT(REG_${classuc}_${reguc})";
1178                         }
1179                         if(defined($temp)) {
1180                                 push(@obst_limit_func, "${temp}");
1181                         } else {
1182                                 push(@obst_limit_func, "0");
1183                         }
1184                 }
1185                 push(@obst_limit_func, " };\n");
1186         }
1187
1188         return ($class, $limit_name, $same_pos, $different_pos);
1189 }
1190
1191 ###
1192 # Generate register requirements structure
1193 ###
1194 sub generate_requirements {
1195         my ($reqs, $flags) = split(/:/, shift);
1196         my $node  = shift;
1197         my $op    = shift;
1198         my $idx   = shift;
1199         my $is_in = shift;
1200         my $class = "";
1201         my $width = 1;
1202         my $result;
1203
1204         my @req_type_mask;
1205         if (defined($flags)) {
1206                 foreach my $f (split(/|/, $flags)) {
1207                         if ($f eq "I") {
1208                                 push(@req_type_mask, "arch_register_req_type_ignore");
1209                         } elsif ($f eq "S") {
1210                                 push(@req_type_mask, "arch_register_req_type_produces_sp");
1211                         } elsif ($f eq "a") {
1212                                 push(@req_type_mask, "arch_register_req_type_aligned");
1213                         } elsif ($f eq "2" or $f eq "4" or $f eq "8") {
1214                                 $width = int($f);
1215                         }
1216                 }
1217         }
1218
1219         if ($reqs eq "none") {
1220
1221                 $result = <<EOF;
1222 {
1223         arch_register_req_type_none,
1224         NULL,                         /* regclass */
1225         NULL,                         /* limit bitset */
1226         0,                            /* same pos */
1227         0,                            /* different pos */
1228         0                             /* width */
1229 };
1230
1231 EOF
1232         } elsif (is_reg_class($reqs)) {
1233                 push(@req_type_mask, "arch_register_req_type_normal");
1234                 my $reqtype = join(" | ", @req_type_mask);
1235                 $class  = $reqs;
1236                 $result = <<EOF;
1237 {
1238         ${reqtype},
1239         & ${arch}_reg_classes[CLASS_${arch}_${class}],
1240         NULL,        /* limit bitset */
1241         0,           /* same pos */
1242         0,           /* different pos */
1243         $width            /* width */
1244 };
1245
1246 EOF
1247
1248         } else {
1249                 my ($regclass, $limit_bitset, $same_pos, $different_pos)
1250                         = build_subset_class_func($node, $op, $idx, $is_in, $reqs, $flags);
1251
1252                 if (!defined($regclass)) {
1253                         die("Fatal error: Could not build subset for requirements '$reqs' of '$op' pos $idx ... exiting.\n");
1254                 }
1255
1256                 if (defined($limit_bitset) && $limit_bitset ne "NULL") {
1257                         push(@req_type_mask, "arch_register_req_type_limited");
1258                 }
1259                 if ($same_pos != 0) {
1260                         push(@req_type_mask, "arch_register_req_type_should_be_same");
1261                 }
1262                 if ($different_pos != 0) {
1263                         push(@req_type_mask, "arch_register_req_type_must_be_different");
1264                 }
1265                 my $reqtype = join(" | ", @req_type_mask);
1266
1267                 if(!defined($limit_bitset)) {
1268                         $limit_bitset = "NULL";
1269                 }
1270
1271                 $class  = $regclass;
1272                 $result = <<EOF;
1273 {
1274         ${reqtype},
1275         & ${arch}_reg_classes[CLASS_${arch}_${class}],
1276         ${limit_bitset},
1277         ${same_pos},        /* same pos */
1278         ${different_pos},       /* different pos */
1279         $width             /* width */
1280 };
1281
1282 EOF
1283         }
1284
1285         my $name = "${arch}_requirements_".mangle_requirements($reqs, $class, $flags);
1286         if(defined($requirements{$name})) {
1287                 return $name;
1288         }
1289         $requirements{$name} = $name;
1290         push(@obst_reg_reqs, <<EOF);
1291 static const arch_register_req_t ${name} = ${result}
1292 EOF
1293
1294         return $name;
1295 }