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