4 # Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
6 # This file is part of libFirm.
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.
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.
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
22 # This script generates C code which emits assembler code for the
23 # assembler ir nodes. It takes a "emit" key from the node specification
24 # and substitutes lines starting with . with a corresponding fprintf().
29 our $specfile = $ARGV[0];
30 our $target_dir = $ARGV[1];
35 our $finish_line_template = "be_emit_finish_line_gas(node);";
40 unless ($return = do $specfile) {
41 die "Fatal error: couldn't parse $specfile: $@" if $@;
42 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
43 die "Fatal error: couldn't run $specfile" unless $return;
47 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
48 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
51 my @obst_func; # stack for the emit functions
52 my @obst_register; # stack for emitter register code
62 $template = "\\t" . $template;
64 my @tokens = ($template =~ m/(?:[^%]|%%)+|\%[a-zA-Z_][a-zA-Z0-9_]*|%\./g);
67 if (/%\./) { last SWITCH; }
69 if(defined($emit_templates{$1})) {
70 push(@{$result}, "${indent}$emit_templates{$1}\n");
72 print "Warning: No emit_template defined for '$1'\n";
73 push(@{$result}, "${indent}$1(node);\n");
78 if (length($_) == 1) {
79 push(@{$result}, "${indent}be_emit_char('$_');\n");
81 push(@{$result}, "${indent}be_emit_cstring(\"$_\");\n");
85 push(@{$result}, "${indent}${finish_line_template}\n");
90 foreach my $op (keys(%nodes)) {
91 my %n = %{ $nodes{"$op"} };
93 # skip this node description if no emit information is available
94 next if (!defined($n{"emit"}));
96 $line = "static void emit_${arch}_${op}(const ir_node *node)";
98 push(@obst_register, "\tbe_set_emitter(op_${arch}_${op}, emit_${arch}_${op});\n");
100 if($n{"emit"} eq "") {
101 push(@obst_func, $line."\n");
102 push(@obst_func, "{\n");
103 push(@obst_func, "\t(void) node;\n");
104 push(@obst_func, "}\n\n");
108 push(@obst_func, $line."\n");
109 push(@obst_func, "{\n");
111 my @emit = split(/\n/, $n{"emit"});
113 foreach my $template (@emit) {
114 # substitute only lines, starting with a '.'
115 if ($template eq '') {
117 } elsif ($template =~ /^(\s*)\.\s*(.*)/) {
119 create_emitter(\@obst_func, $indent, $2);
121 push(@obst_func, "\t${arch}_emitf(node, \"$template\");\n");
125 push(@obst_func, "}\n\n");
128 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
130 my $creation_time = localtime(time());
137 * \@brief Function prototypes for the emitter functions.
138 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
139 * Edit $specfile instead.
140 * created by: $0 $specfile $target_dir
141 * \@date $creation_time
143 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
144 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
147 #include "${arch}_emitter.h"
149 void ${arch}_register_spec_emitters(void);
157 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
159 $creation_time = localtime(time());
164 * \@brief Generated functions to emit code for assembler ir nodes.
165 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
166 * Edit $specfile instead.
167 * created by: $0 $specfile $target_dir
168 * \@date $creation_time
177 #include "irprog_t.h"
178 #include "beemitter.h"
180 #include "gen_${arch}_emitter.h"
181 #include "${arch}_new_nodes.h"
182 #include "${arch}_emitter.h"
186 print OUT @obst_func;
191 * Enters the emitter functions for handled nodes into the generic
192 * pointer of an opcode.
194 void $arch\_register_spec_emitters(void)
198 print OUT @obst_register;