support for custom attribute types on nodes
[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 %cpu;
41 our $default_cmp_attr;
42 our $default_attr_type;
43
44 # include spec file
45
46 my $return;
47
48 no strict "subs";
49 unless ($return = do $specfile) {
50         die "couldn't parse $specfile: $@" if $@;
51         die "couldn't do $specfile: $!"    unless defined $return;
52         die "couldn't run $specfile"       unless $return;
53 }
54 use strict "subs";
55
56 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
57 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
58
59 if(!defined($default_attr_type)) {
60         $default_attr_type = "${arch}_attr_t";
61 }
62
63 #print Dumper(%nodes);
64 #print Dumper(%operands);
65
66 # create c code file from specs
67
68 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
69 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
70 my @obst_constructor; # stack for node constructor functions
71 my @obst_new_irop;    # stack for the new_ir_op calls
72 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
73 my @obst_header;      # stack for function prototypes
74 my @obst_is_archirn;  # stack for the is_$arch_irn() function
75 my @obst_cmp_attr;    # stack for the compare attribute functions
76 my @obst_proj;        # stack for the pn_ numbers
77 my $orig_op;
78 my $arity;
79 my $cmp_attr_func;
80 my $temp;
81 my $n_opcodes = 0;    # number of opcodes
82
83 # for registering additional opcodes
84 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
85
86 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
87
88 # create default compare function
89 if(defined($default_cmp_attr)) {
90         my $cmpcode = $default_cmp_attr;
91         push(@obst_cmp_attr, "static int default_cmp_attr(ir_node *a, ir_node *b) {\n");
92         if($cmpcode =~ m/attr_a/) {
93                 push(@obst_cmp_attr, "\t${default_attr_type} *attr_a = get_irn_generic_attr(a);\n");
94         }
95         if($cmpcode =~ m/attr_b/) {
96                 push(@obst_cmp_attr, "\t${default_attr_type} *attr_b = get_irn_generic_attr(b);\n");
97         }
98         push(@obst_cmp_attr, "\t${cmpcode}\n");
99         push(@obst_cmp_attr, "}\n\n");
100 }
101
102 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
103 foreach my $op (keys(%nodes)) {
104         my %n        = %{ $nodes{"$op"} };
105         my $known_mode;
106         my $n_res    = 0;
107         my $num_outs = 0;
108         my @out_flags;
109
110         # determine arity from in requirements
111         $arity = exists($n{"arity"}) ? $n{"arity"} : 0;
112         if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
113                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
114         }
115
116         $orig_op = $op;
117         $op      = $arch."_".$op;
118         $temp    = "";
119
120         # define some proj numbers
121         if (exists($n{"outs"})) {
122                 undef my @outs;
123
124                 @outs     = @{ $n{"outs"} };
125                 $num_outs = $#outs + 1;
126
127                 push(@obst_proj, "\nenum pn_$op {\n");
128
129                 for (my $idx = 0; $idx <= $#outs; $idx++) {
130                         # check, if we have additional flags annotated to out
131                         if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
132                                 push(@out_flags, $1);
133                                 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
134                         }
135                         push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
136                 }
137
138                 push(@obst_proj, "};\n");
139                 # outs have names, it must be a mode_T node
140                 $known_mode = "mode_T";
141         }
142         if (exists($n{"ins"})) {
143                 undef my @ins;
144
145                 @ins = @{ $n{"ins"} };
146                 my $num_ins = $#ins + 1;
147
148                 push(@obst_proj, "\nenum n_$op {\n");
149
150                 for (my $idx = 0; $idx <= $#ins; $idx++) {
151                         push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
152                 }
153
154                 push(@obst_proj, "};\n");
155         }
156         if (exists($n{"mode"})) {
157                 $known_mode = $n{"mode"};
158         }
159
160         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
161         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
162         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
163
164         push(@obst_is_archirn, "is_$op(node)");
165
166         push(@obst_header, "extern ir_op *op_$op;\n");
167         push(@obst_header, "ir_op *get_op_$op(void);\n");
168         push(@obst_header, "int is_$op(const ir_node *n);\n");
169
170         my $attr_type= $n{"attr_type"};
171         if(!defined($attr_type)) {
172                 $attr_type = $default_attr_type;
173         }
174
175         my $cmp_attr_func;
176         if(defined($default_cmp_attr)) {
177                 $cmp_attr_func = "default_cmp_attr";
178         }
179         # create compare attribute function if needed
180         if (exists($n{"cmp_attr"})) {
181                 my $cmpcode = $n{"cmp_attr"};
182
183                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
184                 if($cmpcode =~ m/attr_a/) {
185                         push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
186                 }
187                 if($cmpcode =~ m/attr_b/) {
188                         push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
189                 }
190                 push(@obst_cmp_attr, "\t${cmpcode}\n");
191                 push(@obst_cmp_attr, "}\n\n");
192
193                 $cmp_attr_func = "cmp_attr_${op}";
194         }
195
196         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
197                 # we explicitly skip the constructor if the specification entry says NONE
198         }
199         else {
200                 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
201                 $n{"comment"} =~ s/^"|"$//g;    # remove "
202                 $n{"comment"} = "/* ".$n{"comment"}." */\n";
203                 push(@obst_constructor, $n{"comment"});
204
205                 # create constructor head
206                 my $complete_args = "";
207                 my $arg_names     = "";
208                 $temp             = "";
209
210                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
211                 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
212                         if ($arity !~ /^\d+$/) {
213                                 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
214                                 next;
215                         }
216                         for (my $i = 1; $i <= $arity; $i++) {
217                                 $complete_args .= ", ir_node *op".$i;
218                                 $arg_names     .= ", op".$i;
219                         }
220                         if (!defined($known_mode)) {
221                                 $complete_args .= ", ir_mode *mode";
222                                 $arg_names     .= ", mode";
223                         }
224                 }
225                 else { # user defined args
226                         for my $href (@{ $n{"args"} }) {
227                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
228                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
229                                 $arg_names      .= ", ".$href->{"name"};
230                         }
231                 }
232
233                 # we have additional attribute arguements
234                 if (exists($n{"attr"})) {
235                         $complete_args .= ", ".$n{"attr"};
236                 }
237
238                 # $complete_args = substr($complete_args, 2);
239                 $temp .= "$complete_args)";
240                 push(@obst_constructor, $temp." {\n");
241                 push(@obst_header, $n{"comment"});
242                 push(@obst_header, $temp.";\n");
243
244                 # emit constructor code
245                 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
246                         if ($arity !~ /^\d+$/) {
247                                 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
248                                 next;
249                         }
250
251                         $temp  = "\tir_node *res;\n";
252                         $temp .= "\tir_node *in[$arity];\n" if ($arity > 0);
253                         $temp .= "\tint flags = 0;\n";
254                         $temp .= "\t${attr_type} *attr;\n" if (exists($n{"init_attr"}));
255
256                         my $exec_units = "NULL";
257                         # set up static variables for cpu execution unit assigments
258                         if (exists($n{"units"})) {
259                                 $temp .= gen_execunit_list_initializer($n{"units"});
260                                 $exec_units = "_exec_units";
261                         }
262
263                         undef my $in_req_var;
264                         undef my $out_req_var;
265
266                         # set up static variables for requirements and registers
267                         if (exists($n{"reg_req"})) {
268                                 my %req = %{ $n{"reg_req"} };
269                                 my $idx;
270
271                                 undef my @in;
272                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
273                                 undef my @out;
274                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
275
276                                 if (@in) {
277                                         $in_req_var = "_in_req_$op";
278                                         $temp .= "\tstatic const arch_register_req_t *".$in_req_var."[] =\n";
279                                         $temp .= "\t{\n";
280                                         for ($idx = 0; $idx <= $#in; $idx++) {
281                                                 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
282                                         }
283                                         $temp .= "\t};\n";
284                                 }
285
286                                 if (@out) {
287                                         $out_req_var = "_out_req_$op";
288
289                                         $temp .= "\tstatic const arch_register_req_t *".$out_req_var."[] =\n";
290                                         $temp .= "\t{\n";
291                                         for ($idx = 0; $idx <= $#out; $idx++) {
292                                                 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
293                                         }
294                                         $temp .= "\t};\n";
295                                 }
296                         }
297
298                         $temp .= "\n";
299                         $temp .= "\tassert(op_$op != NULL);\n\n";
300
301                         for (my $i = 1; $i <= $arity; $i++) {
302                                 $temp .= "\tin[".($i - 1)."] = op".$i.";\n";
303                         }
304
305                         # set flags
306                         if (exists($n{"irn_flags"})) {
307                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
308                                         if ($flag eq "R") {
309                                                 $temp .= "\tflags |= arch_irn_flags_rematerializable;   /* op can be easily recalculated */\n";
310                                         }
311                                         elsif ($flag eq "N") {
312                                                 $temp .= "\tflags |= arch_irn_flags_dont_spill;         /* op is NOT spillable */\n";
313                                         }
314                                         elsif ($flag eq "I") {
315                                                 $temp .= "\tflags |= arch_irn_flags_ignore;             /* ignore op for register allocation */\n";
316                                         }
317                                         elsif ($flag eq "S") {
318                                                 $temp .= "\tflags |= arch_irn_flags_modify_sp;          /* op modifies stack pointer */\n";
319                                         }
320                                 }
321                         }
322
323                         my $in_param;
324                         my $out_param;
325                         # allocate memory and set pointer to register requirements
326                         if (exists($n{"reg_req"})) {
327                                 my %req = %{ $n{"reg_req"} };
328
329                                 undef my @in;
330                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
331                                 undef my @out;
332                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
333
334                                 if (@in) {
335                                         $in_param = $in_req_var;
336                                 }
337                                 else {
338                                         $in_param = "NULL";
339                                 }
340
341                                 if (@out) {
342                                         $n_res     = $#out + 1;
343                                         $out_param = "$out_req_var, $exec_units, $n_res";
344                                 }
345                                 else {
346                                         $out_param = "NULL, $exec_units, 0";
347                                 }
348                         }
349                         else {
350                                 $in_param  = "NULL";
351                                 $out_param = "NULL, $exec_units, 0";
352                         }
353                         $temp .= "\n\t/* create node */\n";
354
355                         my $latency = 1;
356                         if (exists($n{"latency"})) {
357                                 $latency = $n{"latency"};
358                         }
359
360                         my $mode = "mode";
361                         if (defined($known_mode)) {
362                                 $mode = $known_mode;
363                         }
364                         $temp .= "\tres = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
365
366                         $temp .= "\n\t/* init node attributes */\n";
367                         $temp .= "\tinit_$arch\_attributes(res, flags, $in_param, $out_param, $latency);\n";
368
369                         # set flags for outs
370                         if ($#out_flags >= 0) {
371                                 $temp .= "\n\t/* set flags for outs */\n";
372                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
373                                         my $flags  = "";
374                                         my $prefix = "";
375
376                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
377                                                 if ($flag eq "I") {
378                                                         $flags .= $prefix."arch_irn_flags_ignore";
379                                                         $prefix = " | ";
380                                                 }
381                                                 elsif ($flag eq "S") {
382                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
383                                                         $prefix = " | ";
384                                                 }
385                                         }
386
387                                         $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
388                                 }
389                         }
390
391
392                         if (exists($n{"init_attr"})) {
393                                 $temp .= "\tattr = get_$arch\_attr(res);\n";
394                                 $temp .= $n{"init_attr"}."\n";
395                         }
396
397                         $temp .= "\n\t/* optimize node */\n";
398                         $temp .= "\tres = optimize_node(res);\n";
399                         $temp .= "\tirn_vrfy_irg(res, irg);\n\n";
400
401                         $temp .= "\n\treturn res;\n";
402
403                         push(@obst_constructor, $temp);
404                 }
405                 else { # user defined constructor
406                         push(@obst_constructor, $n{"rd_constructor"});
407                 }
408
409                 # close constructor function
410                 push(@obst_constructor, "}\n\n");
411         } # constructor creation
412
413         # set default values for state and flags if not given
414         $n{"state"}    = "floats" if (! exists($n{"state"}));
415         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
416
417
418         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
419         push(@obst_new_irop, "\tops.dump_node     = $arch\_dump_node;\n");
420
421         if (defined($cmp_attr_func)) {
422                 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
423         }
424
425         $n_opcodes++;
426         $temp  = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
427         $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}) + $n_res * sizeof(arch_register_t *), &ops);\n";
428         push(@obst_new_irop, $temp);
429         push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
430         push(@obst_enum_op, "\tiro_$op,\n");
431
432         push(@obst_header, "\n");
433 }
434 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
435 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
436 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
437 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
438
439 # emit the code
440
441 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
442
443 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
444 print OUT @obst_cmp_attr;
445 print OUT "\n";
446 print OUT @obst_opvar;
447 print OUT "\n";
448 print OUT @obst_get_opvar;
449 print OUT "\n";
450
451 print OUT<<EOF;
452
453 static int $arch\_opcode_start = -1;
454 static int $arch\_opcode_end   = -1;
455
456 EOF
457
458 # build the FOURCC arguments from $arch
459
460 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
461
462 if (length($arch) >= 1) {
463         $a = uc(substr($arch, 0, 1));
464 }
465
466 if (length($arch) >= 2) {
467         $b = uc(substr($arch, 1, 1));
468 }
469
470 if (length($arch) >= 3) {
471         $c = uc(substr($arch, 2, 1));
472 }
473
474 if (length($arch) >= 4) {
475         $d = uc(substr($arch, 3, 1));
476 }
477
478 print OUT "static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');\n";
479
480 print OUT<<ENDOFISIRN;
481
482 /** Return the opcode number of the first $arch opcode. */
483 int get_$arch\_opcode_first(void) {
484         return $arch\_opcode_start;
485 }
486
487 /** Return the opcode number of the last $arch opcode + 1. */
488 int get_$arch\_opcode_last(void) {
489         return $arch\_opcode_end;
490 }
491
492 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
493 int is_$arch\_irn(const ir_node *node) {
494         return get_op_tag(get_irn_op(node)) == &$arch\_op_tag;
495 }
496
497 int get_$arch\_irn_opcode(const ir_node *node) {
498         if (is_$arch\_irn(node))
499                 return get_irn_opcode(node) - $arch\_opcode_start;
500         return -1;
501 }
502
503 ENDOFISIRN
504
505 print OUT @obst_constructor;
506
507 print OUT<<ENDOFMAIN;
508 /**
509  * Creates the $arch specific Firm machine operations
510  * needed for the assembler irgs.
511  */
512 void $arch\_create_opcodes(void) {
513 #define N   irop_flag_none
514 #define L   irop_flag_labeled
515 #define C   irop_flag_commutative
516 #define X   irop_flag_cfopcode
517 #define I   irop_flag_ip_cfopcode
518 #define F   irop_flag_fragile
519 #define Y   irop_flag_forking
520 #define H   irop_flag_highlevel
521 #define c   irop_flag_constlike
522 #define K   irop_flag_keep
523 #define M   irop_flag_machine
524 #define O   irop_flag_machine_op
525 #define R   (irop_flag_user << 0)
526
527         ir_op_ops  ops;
528         int        cur_opcode;
529         static int run_once = 0;
530
531         if (run_once)
532                 return;
533         run_once = 1;
534
535         cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
536
537         $arch\_opcode_start = cur_opcode;
538 ENDOFMAIN
539
540 print OUT @obst_new_irop;
541 print OUT "\n";
542 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
543 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
544 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
545 print OUT ";\n";
546 print OUT "}\n";
547
548 close(OUT);
549
550 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
551
552 my $creation_time = localtime(time());
553 my $tmp = uc($arch);
554
555 print OUT<<EOF;
556 /**
557  * \@file
558  * \@brief Function prototypes for the new opcode functions.
559  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
560  *        Edit $specfile instead.
561  *        created by: $0 $specfile $target_dir
562  * \@date  $creation_time
563  */
564 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
565 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
566
567 EOF
568
569 print OUT @obst_enum_op;
570 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
571 print OUT "int get_$arch\_opcode_first(void);\n";
572 print OUT "int get_$arch\_opcode_last(void);\n";
573 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
574 print OUT @obst_header;
575 print OUT @obst_proj;
576
577 print OUT <<EOF;
578 #endif
579 EOF
580
581 close(OUT);
582
583 ###
584 # Translates numeric arity into string constant.
585 ###
586 sub translate_arity {
587         my $arity = shift;
588
589         if ($arity =~ /^\d+$/) {
590                 if    ($arity == 0) {
591                         return "oparity_zero";
592                 }
593                 elsif ($arity == 1) {
594                         return "oparity_unary";
595                 }
596                 elsif ($arity == 2) {
597                         return "oparity_binary";
598                 }
599                 elsif ($arity == 3) {
600                         return "oparity_trinary";
601                 }
602                 else {
603                         return "oparity_any";
604                 }
605         }
606         else {
607                 return "oparity_".$arity;
608         }
609 }
610
611 ###
612 # Return the list of pointers for the given execution units.
613 ###
614 sub gen_execunit_list_initializer {
615         my $units   = shift;
616         my $uc_arch = uc($arch);
617         my $ret     = "";
618         my $ret2    = "";
619         my %init;
620
621         foreach my $unit (@{ $units }) {
622                 if ($unit eq "DUMMY") {
623                         push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
624                 }
625                 elsif (exists($cpu{"$unit"})) {
626                         # operation can be executed on all units of this type
627                         # -> add them all
628                         my $tp_name = "$arch\_execution_units_$unit";
629                         my $idx     = 0;
630                         foreach (@{ $cpu{"$unit"} }) {
631                                 next if ($idx++ == 0);  # skip first element (it's not a unit)
632                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
633                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
634                         }
635                 }
636                 else {
637                         # operation can be executed only a certain unit
638                         # -> find corresponding unit type
639                         my $found = 0;
640 TP_SEARCH:      foreach my $cur_type (keys(%cpu)) {
641                                 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
642                                         if ($unit eq $cur_unit) {
643                                                 my $tp_name   = "$arch\_execution_units_$cur_type";
644                                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
645                                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
646                                                 $found = 1;
647                                                 last TP_SEARCH;
648                                         }
649                                 }
650                         }
651
652                         if (! $found) {
653                                 print STDERR "Invalid execution unit $unit specified!\n";
654                         }
655                 }
656         }
657
658         # prepare the 2-dim array init
659         foreach my $key (keys(%init)) {
660                 $ret .= "\tstatic const be_execution_unit_t *_allowed_units_".$key."[] =\n";
661                 $ret .= "\t{\n";
662                 foreach (@{ $init{"$key"} }) {
663                         $ret .= "$_,\n";
664                 }
665                 $ret .= "\t\tNULL\n";
666                 $ret .= "\t};\n";
667                 $ret2 .= "\t\t_allowed_units_$key,\n";
668         }
669         $ret2 .= "\t\tNULL\n";
670
671         $ret .= "\tstatic const be_execution_unit_t **_exec_units[] =\n";
672         $ret .= "\t{\n";
673         $ret .= $ret2;
674         $ret .= "\t};\n";
675
676         return $ret;
677 }