start support for machine operands in spec files
[libfirm] / ir / be / scripts / generate_new_opcodes.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
5 #
6 # This file is part of libFirm.
7 #
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
12 #
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
16 #
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE.
20 #
21
22 # This script generates the C code which creates the irop's and
23 # their coresponding node constructors for all operations in a given spec
24 # so they can be used as normal firm nodes.
25 # Creation: 2005/10/19
26 # $Id$
27
28 use strict;
29 use Data::Dumper;
30
31 my $specfile   = $ARGV[0];
32 my $target_dir = $ARGV[1];
33 my $state      = 1;
34 my $cur_op     = "";
35 my $line_nr    = 0;
36
37 our $arch;
38 our $additional_opcodes;
39 our %nodes;
40 our %operands;
41 our %cpu;
42 our $default_attr_type;
43 our $default_cmp_attr;
44 our %init_attr;
45 our %compare_attr;
46
47 # include spec file
48
49 my $return;
50
51 no strict "subs";
52 unless ($return = do $specfile) {
53         die "Fatal error: couldn't parse $specfile: $@" if $@;
54         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
55         die "Fatal error: couldn't run $specfile"       unless $return;
56 }
57 use strict "subs";
58
59 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
60 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
61
62 if(!defined($default_attr_type)) {
63         $default_attr_type = "${arch}_attr_t";
64 }
65 if(!defined(%init_attr)) {
66         %init_attr = (
67                 "$default_attr_type" => "\tinit_${arch}_attributes(res, flags, in_reqs, out_reqs, exec_units, n_res, latency);",
68         );
69 }
70 if(!defined($default_cmp_attr)) {
71         $default_cmp_attr = "${arch}_compare_attr";
72 }
73 if(!defined(%compare_attr)) {
74         %compare_attr = (
75                 "${default_attr_type}" => "${default_cmp_attr}",
76         );
77 }
78
79 foreach my $op (keys(%operands)) {
80         my %operand = %{ $operands{"$op"} };
81         my %op_node;
82
83         # constraints
84         if(defined($operand{op_flags})) { die "Fatal error: operands can't have op_flags ($op)"; }
85         if(defined($operand{cmp_attr})) { die "Fatal error: cmp_attr not allowed for operands ($op)"; }
86         if(defined($operand{mode})) { die "Operand must not have a mode defined ($op)"; }
87         if(defined($operand{out_arity})) { die "operand must not have out_arity defined ($op)"; }
88         if(defined($nodes{$op})) { die "$op defined as operand and as node"; };
89
90
91         foreach my $flag (keys(%operand)) {
92                 $op_node{$flag} = $operand{$flag};
93         }
94         $op_node{op_flags} = "O";
95         $op_node{cmp_attr} = 'return 1;';
96         $op_node{mode}     = 'mode_any';
97
98         $nodes{$op} = \%op_node;
99
100         print Dumper(%op_node);
101 }
102
103 #print Dumper(%nodes);
104 #print Dumper(%operands);
105
106 # create c code file from specs
107
108 my @obst_opvar;       # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
109 my @obst_get_opvar;   # stack for the get_op_<arch>_<op-name>() functions
110 my @obst_constructor; # stack for node constructor functions
111 my @obst_new_irop;    # stack for the new_ir_op calls
112 my @obst_enum_op;     # stack for creating the <arch>_opcode enum
113 my @obst_header;      # stack for function prototypes
114 my @obst_is_archirn;  # stack for the is_$arch_irn() function
115 my @obst_cmp_attr;    # stack for the compare attribute functions
116 my @obst_proj;        # stack for the pn_ numbers
117 my $orig_op;
118 my $arity;
119 my $cmp_attr_func;
120 my $temp;
121 my $n_opcodes = 0;    # number of opcodes
122 my $ARITY_VARIABLE = -1;
123 my $ARITY_DYNAMIC  = -2;
124
125 # for registering additional opcodes
126 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
127
128 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
129
130 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
131 foreach my $op (keys(%nodes)) {
132         my %n        = %{ $nodes{"$op"} };
133         my $known_mode;
134         my $num_outs = 0;
135         my $out_arity;
136         my @out_flags;
137
138         # determine arity
139         $arity = 0;
140         if(exists($n{"arity"})) {
141                 $arity = $n{"arity"};
142         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
143                 $arity = scalar(@{ $n{"reg_req"}{"in"} });
144         } elsif (exists($n{"ins"})) {
145                 $arity = scalar(@{ $n{"ins"} });
146         }
147         if($arity eq "variable") {
148                 $arity = $ARITY_VARIABLE;
149         } elsif($arity eq "dynamic") {
150                 $arity = $ARITY_DYNAMIC;
151         }
152
153         # determine out arity
154         $out_arity = 0;
155         if(exists($n{"out_arity"})) {
156                 $out_arity = $n{"out_arity"};
157         } elsif (exists($n{"reg_req"}) && exists($n{"reg_req"}{"out"})) {
158                 $out_arity = scalar(@{ $n{"reg_req"}{"out"} });
159         } elsif (exists($n{"outs"})) {
160                 $out_arity = scalar(@{ $n{"outs"} });
161         }
162         if($out_arity eq "variable") {
163                 $out_arity = $ARITY_VARIABLE;
164         } elsif($out_arity eq "dynamic") {
165                 $out_arity = $ARITY_DYNAMIC;
166         }
167
168         $orig_op = $op;
169         $op      = $arch."_".$op;
170         $temp    = "";
171
172         # define proj numbers and in numbers
173         if (exists($n{"outs"})) {
174                 undef my @outs;
175
176                 @outs     = @{ $n{"outs"} };
177                 if($out_arity >= 0 && scalar(@outs) != $out_arity) {
178                         die "Fatal error: Op ${op} has different number of outs and out_arity\n";
179                 }
180
181                 $num_outs = $#outs + 1;
182
183                 push(@obst_proj, "\nenum pn_$op {\n");
184
185                 for (my $idx = 0; $idx <= $#outs; $idx++) {
186                         # check, if we have additional flags annotated to out
187                         if ($outs[$idx] =~ /:((S|I)(\|(S|I))*)/) {
188                                 push(@out_flags, $1);
189                                 $outs[$idx] =~ s/:((S|I)(\|(S|I))*)//;
190                         }
191                         push(@obst_proj, "\tpn_$op\_".$outs[$idx]." = $idx,\n");
192                 }
193
194                 push(@obst_proj, "};\n");
195                 # outs have names, it must be a mode_T node
196                 $known_mode = "mode_T";
197         }
198         if (exists($n{"ins"})) {
199                 undef my @ins;
200
201                 @ins = @{ $n{"ins"} };
202                 if($arity >= 0 && scalar(@ins) != $arity) {
203                         die "Fatal error: Op ${op} has different number of ins and arity\n";
204                 }
205
206                 push(@obst_proj, "\nenum n_$op {\n");
207
208                 for (my $idx = 0; $idx <= $#ins; $idx++) {
209                         push(@obst_proj, "\tn_${op}_".$ins[$idx]." = $idx,\n");
210                 }
211
212                 push(@obst_proj, "};\n");
213         }
214
215         # determine mode
216         if (exists($n{"mode"})) {
217                 $known_mode = $n{"mode"};
218         }
219
220         push(@obst_opvar, "ir_op *op_$op = NULL;\n");
221         push(@obst_get_opvar, "ir_op *get_op_$op(void)         { return op_$op; }\n");
222         push(@obst_get_opvar, "int    is_$op(const ir_node *n) { return get_$arch\_irn_opcode(n) == iro_$op; }\n\n");
223
224         push(@obst_is_archirn, "is_$op(node)");
225
226         push(@obst_header, "extern ir_op *op_$op;\n");
227         push(@obst_header, "ir_op *get_op_$op(void);\n");
228         push(@obst_header, "int is_$op(const ir_node *n);\n");
229
230         my $attr_type= $n{"attr_type"};
231         if(!defined($attr_type)) {
232                 $attr_type = $default_attr_type;
233         }
234
235         # determine compare function
236         my $cmp_attr_func;
237         if (exists($n{"cmp_attr"})) {
238                 my $cmpcode = $n{"cmp_attr"};
239
240                 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
241                 if($cmpcode =~ m/attr_a/) {
242                         push(@obst_cmp_attr, "\t${attr_type} *attr_a = get_irn_generic_attr(a);\n");
243                 } else {
244                         push(@obst_cmp_attr, "\t(void) a;\n");
245                 }
246                 if($cmpcode =~ m/attr_b/) {
247                         push(@obst_cmp_attr, "\t${attr_type} *attr_b = get_irn_generic_attr(b);\n");
248                 } else {
249                         push(@obst_cmp_attr, "\t(void) b;\n");
250                 }
251                 push(@obst_cmp_attr, "\t${cmpcode}\n");
252                 push(@obst_cmp_attr, "}\n\n");
253
254                 $cmp_attr_func = "cmp_attr_${op}";
255         } else {
256                 if(defined($compare_attr{${attr_type}})) {
257                         $cmp_attr_func = $compare_attr{${attr_type}};
258                 } else {
259                         die "Fatal error: No compare function defined for ${attr_type} attributes.";
260                 }
261         }
262
263         if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
264                 # we explicitly skip the constructor if the specification entry says NONE
265         } else {
266                 my $comment = $n{"comment"};
267                 if(!exists($n{"comment"})) {
268                         $comment = "construct ${orig_op} node";
269                 }
270                 $comment =
271                         "/**\n".
272                         " * ${comment}\n".
273                         " */\n";
274
275                 push(@obst_constructor, $comment);
276
277                 # create constructor head
278                 my $complete_args = "";
279                 $temp             = "";
280
281                 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
282                 if (!exists($n{"args"})) { # default args
283                         if ($arity == $ARITY_VARIABLE) {
284                                 $complete_args = ", int arity, ir_node *in[]";
285                         } elsif ($arity == $ARITY_DYNAMIC) {
286                                 $complete_args = "";
287                         } else {
288                                 for (my $i = 0; $i < $arity; $i++) {
289                                         my $opname = "op${i}";
290                                         if (exists($n{"ins"})) {
291                                                 my @ins = @{ $n{"ins"} };
292                                                 $opname = $ins[$i];
293                                         }
294
295                                         $complete_args .= ", ir_node *${opname}";
296                                 }
297                         }
298                         if ($out_arity == $ARITY_VARIABLE) {
299                                 $complete_args .= ", int n_res";
300                         }
301
302                         if (!defined($known_mode)) {
303                                 $complete_args .= ", ir_mode *mode";
304                         }
305                 } else { # user defined args
306                         for my $href (@{ $n{"args"} }) {
307                                 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
308                                 $complete_args  .= ", ".$href->{"type"}.$href->{"name"};
309                         }
310                 }
311
312                 # we have additional attribute arguements
313                 if (exists($n{"attr"})) {
314                         $complete_args .= ", ".$n{"attr"};
315                 }
316
317                 $temp .= "$complete_args)";
318                 push(@obst_constructor, $temp."\n{\n");
319                 push(@obst_header, $comment);
320                 push(@obst_header, $temp.";\n");
321
322                 # emit constructor code
323                 if (!exists($n{"rd_constructor"})) { # default constructor
324                         $temp  = "\tir_node  *res;\n";
325                         $temp .= "\tir_op    *op      = op_${op};\n";
326                         $temp .= "\tint       flags   = 0;\n";
327
328                         if($arity == $ARITY_DYNAMIC) {
329                                 $temp .= "\tint        arity   = -1;\n";
330                                 $temp .= "\tir_node  **in      = NULL;\n";
331                         } elsif($arity == $ARITY_VARIABLE) {
332                         } else {
333                                 $temp .= "\tint       arity   = $arity;\n";
334                                 if($arity > 0) {
335                                         $temp .= "\tir_node  *in[$arity];\n";
336                                 } else {
337                                         $temp .= "\tir_node **in    = NULL;\n";
338                                 }
339                         }
340                         if($out_arity == $ARITY_DYNAMIC) {
341                                 $temp .= "\tint       n_res   = -1;\n";
342                         } elsif($out_arity == $ARITY_VARIABLE) {
343                         } else {
344                                 $temp .= "\tint       n_res   = ${out_arity};\n";
345                         }
346
347                         my $latency = $n{"latency"};
348                         if (!defined($latency)) {
349                                 $latency = 1;
350                         }
351                         $temp .= "\tunsigned  latency = ${latency};\n";
352
353                         if (defined($known_mode)) {
354                                 $temp .= "\tir_mode  *mode    = ${known_mode};\n";
355                         }
356
357                         # set up static variables for cpu execution unit assigments
358                         if (exists($n{"units"})) {
359                                 $temp .= gen_execunit_list_initializer($n{"units"});
360                         } else {
361                                 $temp .= "\tstatic const be_execution_unit_t ***exec_units = NULL;\n";
362                         }
363
364                         undef my $in_req_var;
365                         undef my $out_req_var;
366
367                         # set up static variables for requirements and registers
368                         if (exists($n{"reg_req"})) {
369                                 my %req = %{ $n{"reg_req"} };
370                                 my $idx;
371
372                                 undef my @in;
373                                 @in = @{ $req{"in"} } if (exists($req{"in"}));
374                                 undef my @out;
375                                 @out = @{ $req{"out"} } if exists(($req{"out"}));
376
377                                 if (@in) {
378                                         if($arity >= 0 && scalar(@in) != $arity) {
379                                                 die "Fatal error: Arity and number of in requirements don't match for ${op}\n";
380                                         }
381
382                                         $temp .= "\tstatic const arch_register_req_t *in_reqs[] =\n";
383                                         $temp .= "\t{\n";
384                                         for ($idx = 0; $idx <= $#in; $idx++) {
385                                                 $temp .= "\t\t&".$op."_reg_req_in_".$idx.",\n";
386                                         }
387                                         $temp .= "\t};\n";
388                                 } else {
389                                         if($arity > 0) {
390                                                 die "Fatal error: need in requirements for ${op}\n";
391                                         }
392                                         $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
393                                 }
394
395                                 if (@out) {
396                                         if($out_arity >= 0 && scalar(@out) != $out_arity) {
397                                                 die "Fatal error: Out-Arity and number of out requirements don't match for ${op}\n";
398                                         }
399
400                                         $temp .= "\tstatic const arch_register_req_t *out_reqs[] =\n";
401                                         $temp .= "\t{\n";
402                                         for ($idx = 0; $idx <= $#out; $idx++) {
403                                                 $temp .= "\t\t&".$op."_reg_req_out_".$idx.",\n";
404                                         }
405                                         $temp .= "\t};\n";
406                                 } else {
407                                         if($out_arity > 0) {
408                                                 die "Fatal error: need out requirements for ${op}\n";
409                                         }
410                                         $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
411                                 }
412                         } else {
413                                 $temp .= "\tstatic const arch_register_req_t **in_reqs = NULL;\n";
414                                 $temp .= "\tstatic const arch_register_req_t **out_reqs = NULL;\n";
415                         }
416                         if(exists($n{"init_attr"})) {
417                                 $temp .= "\t${attr_type} *attr;\n";
418                         }
419
420                         $temp .= "\n";
421
422                         if($arity > 0) {
423                                 $temp .= "\t/* construct in array */\n";
424                                 for (my $i = 0; $i < $arity; $i++) {
425                                         my $opname = "op${i}";
426                                         if (exists($n{"ins"})) {
427                                                 my @ins = @{ $n{"ins"} };
428                                                 $opname = $ins[$i];
429                                         }
430
431                                         $temp .= "\tin[${i}] = ${opname};\n";
432                                 }
433                                 $temp .= "\n";
434                         }
435
436                         # set flags
437                         if (exists($n{"irn_flags"})) {
438                                 $temp .= "\t/* flags */\n";
439                                 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
440                                         if ($flag eq "R") {
441                                                 $temp .= "\tflags |= arch_irn_flags_rematerializable;\n";
442                                         } elsif ($flag eq "N") {
443                                                 $temp .= "\tflags |= arch_irn_flags_dont_spill;\n";
444                                         } elsif ($flag eq "I") {
445                                                 $temp .= "\tflags |= arch_irn_flags_ignore;\n";
446                                         } elsif ($flag eq "S") {
447                                                 $temp .= "\tflags |= arch_irn_flags_modify_sp;\n";
448                                         }
449                                 }
450                                 $temp .= "\n";
451                         }
452
453                         $temp .= "\t/* create node */\n";
454                         $temp .= "\tassert(op != NULL);\n";
455                         $temp .= "\tres = new_ir_node(db, irg, block, op, mode, arity, in);\n";
456                         $temp .= "\n";
457
458                         $temp .= "\t/* init node attributes */\n";
459                         # lookup init function
460                         my $attr_init_code = $init_attr{$attr_type};
461                         if(!defined($attr_init_code)) {
462                                 die "Fatal error: Couldn't find attribute initialisation code for type '${attr_type}'";
463                         }
464                         $temp .= "${attr_init_code}\n";
465                         $temp .= "\n";
466
467                         # set flags for outs
468                         if ($#out_flags >= 0) {
469                                 $temp .= "\t/* set flags for outs */\n";
470                                 for (my $idx = 0; $idx <= $#out_flags; $idx++) {
471                                         my $flags  = "";
472                                         my $prefix = "";
473
474                                         foreach my $flag (split(/\|/, $out_flags[$idx])) {
475                                                 if ($flag eq "I") {
476                                                         $flags .= $prefix."arch_irn_flags_ignore";
477                                                         $prefix = " | ";
478                                                 }
479                                                 elsif ($flag eq "S") {
480                                                         $flags .= $prefix."arch_irn_flags_modify_sp";
481                                                         $prefix = " | ";
482                                                 }
483                                         }
484
485                                         $temp .= "\tset_$arch\_out_flags(res, $flags, $idx);\n";
486                                 }
487                                 $temp .= "\n";
488                         }
489
490
491                         if (exists($n{"init_attr"})) {
492                                 $temp .= "\tattr = get_irn_generic_attr(res);\n";
493                                 $temp .= "\t".$n{"init_attr"}."\n";
494                         }
495
496                         $temp .= "\t/* optimize node */\n";
497                         $temp .= "\tres = optimize_node(res);\n";
498                         $temp .= "\tirn_vrfy_irg(res, irg);\n";
499                         $temp .= "\n";
500
501                         $temp .= "\treturn res;\n";
502
503                         push(@obst_constructor, $temp);
504                 }
505                 else { # user defined constructor
506                         push(@obst_constructor, $n{"rd_constructor"});
507                 }
508
509                 # close constructor function
510                 push(@obst_constructor, "}\n\n");
511         } # constructor creation
512
513         # set default values for state and flags if not given
514         $n{"state"}    = "floats" if (! exists($n{"state"}));
515         $n{"op_flags"} = "N"      if (! exists($n{"op_flags"}));
516
517
518         push(@obst_new_irop, "\n\tmemset(&ops, 0, sizeof(ops));\n");
519         push(@obst_new_irop, "\tops.dump_node     = $arch\_dump_node;\n");
520
521         if (defined($cmp_attr_func)) {
522                 push(@obst_new_irop, "\tops.node_cmp_attr = ${cmp_attr_func};\n");
523         }
524
525         $n_opcodes++;
526         my $n_res = $out_arity;
527         if($n_res < 0) {
528                 $n_res = "20"; # hacky....
529         }
530         $temp  = "\top_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
531         $temp .= "|M, ".translate_arity($arity).", 0, sizeof(${attr_type}), &ops);\n";
532         push(@obst_new_irop, $temp);
533         push(@obst_new_irop, "\tset_op_tag(op_$op, &$arch\_op_tag);\n");
534         push(@obst_enum_op, "\tiro_$op,\n");
535
536         push(@obst_header, "\n");
537 }
538 push(@obst_enum_op, "\tiro_$arch\_last_generated,\n");
539 push(@obst_enum_op, "\tiro_$arch\_last = iro_$arch\_last_generated");
540 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
541 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
542
543 # emit the code
544
545 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
546
547 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
548 print OUT @obst_cmp_attr;
549 print OUT "\n";
550 print OUT @obst_opvar;
551 print OUT "\n";
552 print OUT @obst_get_opvar;
553 print OUT "\n";
554
555 print OUT<<EOF;
556
557 static int $arch\_opcode_start = -1;
558 static int $arch\_opcode_end   = -1;
559
560 EOF
561
562 # build the FOURCC arguments from $arch
563
564 my ($a, $b, $c, $d) = ('\0', '\0', '\0', '\0');
565
566 if (length($arch) >= 1) {
567         $a = uc(substr($arch, 0, 1));
568 }
569
570 if (length($arch) >= 2) {
571         $b = uc(substr($arch, 1, 1));
572 }
573
574 if (length($arch) >= 3) {
575         $c = uc(substr($arch, 2, 1));
576 }
577
578 if (length($arch) >= 4) {
579         $d = uc(substr($arch, 3, 1));
580 }
581
582 print OUT<<ENDOFISIRN;
583
584 /** A tag for the $arch opcodes. Note that the address is used as a tag value, NOT the FOURCC code. */
585 static unsigned $arch\_op_tag = FOURCC('$a', '$b', '$c', '$d');
586
587 /** Return the opcode number of the first $arch opcode. */
588 int get_$arch\_opcode_first(void) {
589         return $arch\_opcode_start;
590 }
591
592 /** Return the opcode number of the last $arch opcode + 1. */
593 int get_$arch\_opcode_last(void) {
594         return $arch\_opcode_end;
595 }
596
597 /** Return 1 if the given opcode is a $arch machine op, 0 otherwise */
598 int is_$arch\_op(const ir_op *op) {
599         return get_op_tag(op) == &$arch\_op_tag;
600 }
601
602 /** Return 1 if the given node is a $arch machine node, 0 otherwise */
603 int is_$arch\_irn(const ir_node *node) {
604         return is_$arch\_op(get_irn_op(node));
605 }
606
607 int get_$arch\_irn_opcode(const ir_node *node) {
608         if (is_$arch\_irn(node))
609                 return get_irn_opcode(node) - $arch\_opcode_start;
610         return -1;
611 }
612
613 ENDOFISIRN
614
615 print OUT @obst_constructor;
616
617 print OUT<<ENDOFMAIN;
618 /**
619  * Creates the $arch specific Firm machine operations
620  * needed for the assembler irgs.
621  */
622 void $arch\_create_opcodes(void) {
623 #define N   irop_flag_none
624 #define L   irop_flag_labeled
625 #define C   irop_flag_commutative
626 #define X   irop_flag_cfopcode
627 #define I   irop_flag_ip_cfopcode
628 #define F   irop_flag_fragile
629 #define Y   irop_flag_forking
630 #define H   irop_flag_highlevel
631 #define c   irop_flag_constlike
632 #define K   irop_flag_keep
633 #define M   irop_flag_machine
634 #define O   irop_flag_machine_op
635 #define R   (irop_flag_user << 0)
636
637         ir_op_ops  ops;
638         int        cur_opcode;
639         static int run_once = 0;
640
641         if (run_once)
642                 return;
643         run_once = 1;
644
645         cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
646
647         $arch\_opcode_start = cur_opcode;
648 ENDOFMAIN
649
650 print OUT @obst_new_irop;
651 print OUT "\n";
652 print OUT "\t$arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
653 print OUT "\t$arch\_opcode_end = cur_opcode + iro_$arch\_last";
654 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
655 print OUT ";\n";
656 print OUT "}\n";
657
658 close(OUT);
659
660 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
661
662 my $creation_time = localtime(time());
663 my $tmp = uc($arch);
664
665 print OUT<<EOF;
666 /**
667  * \@file
668  * \@brief Function prototypes for the new opcode functions.
669  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
670  *        Edit $specfile instead.
671  *        created by: $0 $specfile $target_dir
672  * \@date  $creation_time
673  */
674 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
675 #define FIRM_BE_${tmp}_GEN_${tmp}_NEW_NODES_H
676
677 EOF
678
679 print OUT @obst_enum_op;
680 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
681 print OUT "int get_$arch\_opcode_first(void);\n";
682 print OUT "int get_$arch\_opcode_last(void);\n";
683 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
684 print OUT @obst_header;
685 print OUT @obst_proj;
686
687 print OUT <<EOF;
688 #endif
689 EOF
690
691 close(OUT);
692
693 ###
694 # Translates numeric arity into string constant.
695 ###
696 sub translate_arity {
697         my $arity = shift;
698
699         if ($arity =~ /^\d+$/) {
700                 if    ($arity == 0) {
701                         return "oparity_zero";
702                 }
703                 elsif ($arity == 1) {
704                         return "oparity_unary";
705                 }
706                 elsif ($arity == 2) {
707                         return "oparity_binary";
708                 }
709                 elsif ($arity == 3) {
710                         return "oparity_trinary";
711                 }
712                 else {
713                         return "oparity_any";
714                 }
715         } elsif ($arity == $ARITY_VARIABLE) {
716                 return "oparity_variable";
717         } elsif ($arity == $ARITY_DYNAMIC) {
718                 return "oparity_dynamic";
719         } else {
720                 die "Fatal error: Unknown arity $arity";
721         }
722 }
723
724 ###
725 # Return the list of pointers for the given execution units.
726 ###
727 sub gen_execunit_list_initializer {
728         my $units   = shift;
729         my $uc_arch = uc($arch);
730         my $ret     = "";
731         my $ret2    = "";
732         my %init;
733
734         foreach my $unit (@{ $units }) {
735                 if ($unit eq "DUMMY") {
736                         push(@{ $init{"DUMMY"} }, "\t\t&be_machine_execution_units_DUMMY[0]");
737                 }
738                 elsif (exists($cpu{"$unit"})) {
739                         # operation can be executed on all units of this type
740                         # -> add them all
741                         my $tp_name = "$arch\_execution_units_$unit";
742                         my $idx     = 0;
743                         foreach (@{ $cpu{"$unit"} }) {
744                                 next if ($idx++ == 0);  # skip first element (it's not a unit)
745                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$unit\_$_";
746                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
747                         }
748                 }
749                 else {
750                         # operation can be executed only a certain unit
751                         # -> find corresponding unit type
752                         my $found = 0;
753 TP_SEARCH:      foreach my $cur_type (keys(%cpu)) {
754                                 foreach my $cur_unit (@{ $cpu{"$cur_type"} }) {
755                                         if ($unit eq $cur_unit) {
756                                                 my $tp_name   = "$arch\_execution_units_$cur_type";
757                                                 my $unit_name = "$uc_arch\_EXECUNIT_TP_$cur_type\_$unit";
758                                                 push(@{ $init{"$unit"} }, "\t\t&".$tp_name."[".$unit_name."]");
759                                                 $found = 1;
760                                                 last TP_SEARCH;
761                                         }
762                                 }
763                         }
764
765                         if (! $found) {
766                                 print STDERR "Invalid execution unit $unit specified!\n";
767                         }
768                 }
769         }
770
771         # prepare the 2-dim array init
772         foreach my $key (keys(%init)) {
773                 $ret .= "\tstatic const be_execution_unit_t *allowed_units_".$key."[] =\n";
774                 $ret .= "\t{\n";
775                 foreach (@{ $init{"$key"} }) {
776                         $ret .= "$_,\n";
777                 }
778                 $ret .= "\t\tNULL\n";
779                 $ret .= "\t};\n";
780                 $ret2 .= "\t\tallowed_units_$key,\n";
781         }
782         $ret2 .= "\t\tNULL\n";
783
784         $ret .= "\tstatic const be_execution_unit_t **exec_units[] =\n";
785         $ret .= "\t{\n";
786         $ret .= $ret2;
787         $ret .= "\t};\n";
788
789         return $ret;
790 }