4 # Copyright (C) 1995-2007 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
33 our $specfile = $ARGV[0];
34 our $target_dir = $ARGV[1];
37 our $comment_string = "/*";
38 our $comment_string_end = "*/" ;
40 our $new_emit_syntax = 0;
47 unless ($return = do $specfile) {
48 die "Fatal error: couldn't parse $specfile: $@" if $@;
49 die "Fatal error: couldn't do $specfile: $!" unless defined $return;
50 die "Fatal error: couldn't run $specfile" unless $return;
54 if ($new_emit_syntax) {
55 my $newscript = dirname($myname) . "/generate_emitter_new.pl";
56 unless ($return = do "$newscript") {
57 die "Fatal error: couldn't parse $newscript: $@" if $@;
58 die "Fatal error: couldn't do $newscript: $!" unless defined $return;
59 die "Fatal error: couldn't run $newscript" unless $return;
64 my $comment_string_quoted = quotemeta($comment_string);
66 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
67 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
70 my @obst_func; # stack for the emit functions
71 my @obst_register; # stack for emitter register code
74 foreach my $op (keys(%nodes)) {
75 my %n = %{ $nodes{"$op"} };
77 # skip this node description if no emit information is available
78 next if (!defined($n{"emit"}));
80 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
82 push(@obst_register, " BE_EMIT($op);\n");
84 if($n{"emit"} eq "") {
85 push(@obst_func, $line." {\n");
86 push(@obst_func, "}\n\n");
90 push(@obst_func, $line." {\n FILE *F = env->out;\n");
91 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
92 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
93 my @emit = split(/\n/, $n{"emit"});
95 foreach my $template (@emit) {
96 # substitute only lines, starting with a '.'
97 if ($template =~ /^(\d*)\.\s*/) {
98 my $indent = " "; # default indent is 2 spaces
100 $indent = " " x $1 if ($1 && $1 > 0);
101 # remove indent, dot and trailing spaces
102 $template =~ s/^\d*\.\s*//;
107 push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
108 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
113 $template =~ s/(\\t)*$//;
116 # add the comment begin string
117 $res .= $comment_string;
121 # substitute all format parameter
122 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
123 $res .= $`; # get everything before the match
128 elsif ($2 && $2 eq "S") {
130 $res .= "%".$3."S"; # substitute %Sx with %xS
132 elsif ($2 && $2 eq "D") {
134 $res .= "%".$3."D"; # substitute %Dx with %xD
136 elsif ($2 && $2 eq "X") {
138 $res .= "%".$3."X"; # substitute %Xx with %xX
140 elsif ($2 && $2 eq "A") {
141 push(@params, "get_irn_n(n, ".($3 - 1).")");
148 elsif ($5) { # backend provided function to call, has to return a string
149 push(@params, $5."(n, env)");
153 $template = $'; # scan everything after the match
155 $res .= $template; # get the remaining string
158 $parm = ", ".join(", ", @params) if (@params);
160 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
162 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");
165 push(@obst_func, $template,"\n");
169 push(@obst_func, "}\n\n");
172 open(OUT, ">$target_h") || die("Fatal error: Could not open $target_h, reason: $!\n");
174 my $creation_time = localtime(time());
181 * \@brief Function prototypes for the emitter functions.
182 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
183 * Edit $specfile instead.
184 * created by: $0 $specfile $target_dir
185 * \@date $creation_time
187 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
188 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
191 #include "$arch\_emitter.h"
193 void $arch\_register_spec_emitters(void);
201 open(OUT, ">$target_c") || die("Fatal error: Could not open $target_c, reason: $!\n");
203 $creation_time = localtime(time());
208 * \@brief Generated functions to emit code for assembler ir nodes.
209 * \@note DO NOT EDIT THIS FILE, your changes will be lost.
210 * Edit $specfile instead.
211 * created by: $0 $specfile $target_dir
212 * \@date $creation_time
222 #include "irprog_t.h"
224 #include "gen_$arch\_emitter.h"
225 #include "$arch\_new_nodes.h"
229 print OUT @obst_func;
233 * Enters the emitter functions for handled nodes into the generic
234 * pointer of an opcode.
236 void $arch\_register_spec_emitters(void) {
238 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
240 /* generated emitter functions */
243 print OUT @obst_register;