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