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