9915030535baf5def5f4e2b92b3c95297e7bb8c9
[libfirm] / ir / be / scripts / generate_emitter_new.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 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
26 # $Id$
27
28 use strict;
29 use Data::Dumper;
30
31 our $specfile;
32 our $target_dir;
33
34 our $arch;
35 our %nodes;
36 our %emit_templates;
37 our $finish_line_template = "be_emit_finish_line_gas(emit, node);";
38
39 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
40 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
41
42 # stacks for output
43 my @obst_func;   # stack for the emit functions
44 my @obst_register;  # stack for emitter register code
45 my $line;
46
47 sub create_emitter {
48         my $result = shift;
49         my $indent = shift;
50         my $template = shift;
51         our %emit_templates;
52         our $arch;
53
54         my @tokens = ($template =~ m/[^\%]+|\%[a-zA-Z_][a-zA-Z0-9_]*|\%./g);
55         push(@{$result}, "${indent}be_emit_char(emit, '\t');\n");
56         for (@tokens) {
57                 SWITCH: {
58                         if (/%\./)      { last SWITCH; }
59                         if (/%%/)       { push(@{$result}, "${indent}be_emit_char(emit, '%');\n"); last SWITCH; }
60                         if (/%(.+)/)    {
61                                 if(defined($emit_templates{$1})) {
62                                         push(@{$result}, "${indent}$emit_templates{$1}\n");
63                                 } else {
64                                         print "Warning: No emit_template defined for '$1'\n";
65                                         push(@{$result}, "${indent}$1(emit, node);\n");
66                                 }
67                                 last SWITCH;
68                         }
69                         push(@{$result}, "${indent}be_emit_cstring(emit, \"$_\");\n");
70                 }
71         }
72         push(@{$result}, "${indent}${finish_line_template}\n");
73 }
74
75
76
77 foreach my $op (keys(%nodes)) {
78         my %n = %{ $nodes{"$op"} };
79
80         # skip this node description if no emit information is available
81         next if (!defined($n{"emit"}));
82
83         $line = "static void emit_${arch}_${op}(${arch}_emit_env_t *env, const ir_node *node)";
84
85         push(@obst_register, "  BE_EMIT($op);\n");
86
87         if($n{"emit"} eq "") {
88                 push(@obst_func, $line." {\n");
89                 push(@obst_func, "\t(void) env;\n");
90                 push(@obst_func, "\t(void) node;\n");
91                 push(@obst_func, "}\n\n");
92                 next;
93         }
94
95         push(@obst_func, $line." {\n");
96         push(@obst_func, "\tbe_emit_env_t *emit = env->emit;\n");
97
98         my @emit = split(/\n/, $n{"emit"});
99
100         foreach my $template (@emit) {
101                 # substitute only lines, starting with a '.'
102                 if ($template =~ /^(\s*)\.\s*(.*)/) {
103                         my $indent = "\t$1";
104                         create_emitter(\@obst_func, $indent, $2);
105                 } else {
106                         push(@obst_func, "\t$template\n");
107                 }
108         }
109
110         push(@obst_func, "}\n\n");
111 }
112
113 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
114
115 my $creation_time = localtime(time());
116
117 my $tmp = uc($arch);
118
119 print OUT<<EOF;
120 /**
121  * \@file
122  * \@brief Function prototypes for the emitter functions.
123  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
124  *        Edit $specfile instead.
125  *        created by: $0 $specfile $target_dir
126  * \@date  $creation_time
127  */
128 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
129 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
130
131 #include "irnode.h"
132 #include "${arch}_emitter.h"
133
134 void ${arch}_register_spec_emitters(void);
135
136 #endif
137
138 EOF
139
140 close(OUT);
141
142 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
143
144 $creation_time = localtime(time());
145
146 print OUT<<EOF;
147 /**
148  * \@file
149  * \@brief     Generated functions to emit code for assembler ir nodes.
150  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
151  *            Edit $specfile instead.
152  *            created by: $0 $specfile $target_dir
153  * \@date      $creation_time
154  */
155 #ifdef HAVE_CONFIG_H
156 #include "config.h"
157 #endif
158
159 #include <stdio.h>
160
161 #include "irnode.h"
162 #include "irop_t.h"
163 #include "irprog_t.h"
164
165 #include "gen_${arch}_emitter.h"
166 #include "${arch}_new_nodes.h"
167 #include "${arch}_emitter.h"
168
169 EOF
170
171 print OUT @obst_func;
172
173 print OUT<<EOF;
174 /**
175  * Enters the emitter functions for handled nodes into the generic
176  * pointer of an opcode.
177  */
178 void $arch\_register_spec_emitters(void) {
179
180 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
181
182   /* generated emitter functions */
183 EOF
184
185 print OUT @obst_register;
186
187 print OUT<<EOF;
188
189 #undef BE_EMIT
190 }
191
192 EOF
193
194 close(OUT);