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);";
36 our $indent_line_func;
41 unless ($return = do $specfile) {
42 die "Fatal error: couldn't parse $specfile: $@" if $@;
43 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
44 die "Fatal error: couldn't run $specfile" unless $return;
48 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
49 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
52 my @obst_func; # stack for the emit functions
53 my @obst_register; # stack for emitter register code
63 if (!defined($indent_line_func)) {
64 $template = "\\t" . $template;
66 push(@{$result}, "${indent}${indent_line_func};\n");
69 my @tokens = ($template =~ m/(?:[^%]|%%)+|\%[a-zA-Z_][a-zA-Z0-9_]*|%\./g);
72 if (/%\./) { last SWITCH; }
74 if(defined($emit_templates{$1})) {
75 push(@{$result}, "${indent}$emit_templates{$1}\n");
77 print "Warning: No emit_template defined for '$1'\n";
78 push(@{$result}, "${indent}$1(node);\n");
83 if (length($_) == 1) {
84 push(@{$result}, "${indent}be_emit_char('$_');\n");
86 push(@{$result}, "${indent}be_emit_cstring(\"$_\");\n");
90 push(@{$result}, "${indent}${finish_line_template}\n");
95 foreach my $op (keys(%nodes)) {
96 my %n = %{ $nodes{"$op"} };
98 # skip this node description if no emit information is available
99 next if (!defined($n{"emit"}));
101 $line = "static void emit_${arch}_${op}(const ir_node *node)";
103 push(@obst_register, " ${arch}_register_emitter(op_${arch}_${op}, emit_${arch}_${op});\n");
105 if($n{"emit"} eq "") {
106 push(@obst_func, $line."\n");
107 push(@obst_func, "{\n");
108 push(@obst_func, "\t(void) node;\n");
109 push(@obst_func, "}\n\n");
113 push(@obst_func, $line."\n");
114 push(@obst_func, "{\n");
116 my @emit = split(/\n/, $n{"emit"});
118 foreach my $template (@emit) {
119 # substitute only lines, starting with a '.'
120 if ($template eq '') {
122 } elsif ($template =~ /^(\s*)\.\s*(.*)/) {
124 create_emitter(\@obst_func, $indent, $2);
126 push(@obst_func, "\t${arch}_emitf(node, \"$template\");\n");
130 push(@obst_func, "}\n\n");
133 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
135 my $creation_time = localtime(time());
142 * \@brief Function prototypes for the emitter functions.
143 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
144 * Edit $specfile instead.
145 * created by: $0 $specfile $target_dir
146 * \@date $creation_time
148 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
149 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
152 #include "${arch}_emitter.h"
154 void ${arch}_register_spec_emitters(void);
162 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
164 $creation_time = localtime(time());
169 * \@brief Generated functions to emit code for assembler ir nodes.
170 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
171 * Edit $specfile instead.
172 * created by: $0 $specfile $target_dir
173 * \@date $creation_time
182 #include "irprog_t.h"
183 #include "beemitter.h"
185 #include "gen_${arch}_emitter.h"
186 #include "${arch}_new_nodes.h"
187 #include "${arch}_emitter.h"
191 print OUT @obst_func;
195 typedef void (*emit_func)(const ir_node *node);
197 static void ${arch}_register_emitter(ir_op *op, emit_func func)
199 assert(op->ops.generic == NULL);
200 op->ops.generic = (op_func)func;
204 * Enters the emitter functions for handled nodes into the generic
205 * pointer of an opcode.
207 void $arch\_register_spec_emitters(void)
211 print OUT @obst_register;