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().
25 # Creation: 2005/11/07
32 our $specfile = $ARGV[0];
33 our $target_dir = $ARGV[1];
36 our $comment_string = "/*";
37 our $comment_string_end = "*/" ;
39 our $new_emit_syntax = 1;
46 unless ($return = do $specfile) {
47 die "Fatal error: couldn't parse $specfile: $@" if $@;
48 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
49 die "Fatal error: couldn't run $specfile" unless $return;
53 if ($new_emit_syntax) {
54 my $newscript = dirname($myname) . "/generate_emitter_new.pl";
55 unless ($return = do "$newscript") {
56 die "Fatal error: couldn't parse $newscript: $@" if $@;
57 die "Fatal error: couldn't do $newscript: $!" unless defined $return;
58 die "Fatal error: couldn't run $newscript" unless $return;
63 my $comment_string_quoted = quotemeta($comment_string);
65 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
66 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
69 my @obst_func; # stack for the emit functions
70 my @obst_register; # stack for emitter register code
73 foreach my $op (keys(%nodes)) {
74 my %n = %{ $nodes{"$op"} };
76 # skip this node description if no emit information is available
77 next if (!defined($n{"emit"}));
79 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
81 push(@obst_register, " BE_EMIT($op);\n");
83 if($n{"emit"} eq "") {
84 push(@obst_func, $line." {\n");
85 push(@obst_func, "}\n\n");
89 push(@obst_func, $line." {\n FILE *F = env->out;\n");
90 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
91 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
92 my @emit = split(/\n/, $n{"emit"});
94 foreach my $template (@emit) {
95 # substitute only lines, starting with a '.'
96 if ($template =~ /^(\d*)\.\s*/) {
97 my $indent = " "; # default indent is 2 spaces
99 $indent = " " x $1 if ($1 && $1 > 0);
100 # remove indent, dot and trailing spaces
101 $template =~ s/^\d*\.\s*//;
106 push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
107 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
112 $template =~ s/(\\t)*$//;
115 # add the comment begin string
116 $res .= $comment_string;
120 # substitute all format parameter
121 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
122 $res .= $`; # get everything before the match
127 elsif ($2 && $2 eq "S") {
129 $res .= "%".$3."S"; # substitute %Sx with %xS
131 elsif ($2 && $2 eq "D") {
133 $res .= "%".$3."D"; # substitute %Dx with %xD
135 elsif ($2 && $2 eq "X") {
137 $res .= "%".$3."X"; # substitute %Xx with %xX
139 elsif ($2 && $2 eq "A") {
140 push(@params, "get_irn_n(n, ".($3 - 1).")");
147 elsif ($5) { # backend provided function to call, has to return a string
148 push(@params, $5."(n, env)");
152 $template = $'; # scan everything after the match
154 $res .= $template; # get the remaining string
157 $parm = ", ".join(", ", @params) if (@params);
159 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
161 push(@obst_func, $indent.'lc_efprintf(arg_env, F, "\t%-35s %-60s '.$comment_string.' %+F (%+G) '.$comment_string_end.'\n", cmd_buf, cmnt_buf, n, n);'."\n");
164 push(@obst_func, $template,"\n");
168 push(@obst_func, "}\n\n");
171 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
173 my $creation_time = localtime(time());
180 * \@brief Function prototypes for the emitter functions.
181 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
182 * Edit $specfile instead.
183 * created by: $0 $specfile $target_dir
184 * \@date $creation_time
186 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
187 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
190 #include "$arch\_emitter.h"
192 void $arch\_register_spec_emitters(void);
200 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
202 $creation_time = localtime(time());
207 * \@brief Generated functions to emit code for assembler ir nodes.
208 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
209 * Edit $specfile instead.
210 * created by: $0 $specfile $target_dir
211 * \@date $creation_time
219 #include "irprog_t.h"
221 #include "gen_$arch\_emitter.h"
222 #include "$arch\_new_nodes.h"
226 print OUT @obst_func;
230 * Enters the emitter functions for handled nodes into the generic
231 * pointer of an opcode.
233 void $arch\_register_spec_emitters(void) {
235 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
237 /* generated emitter functions */
240 print OUT @obst_register;