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