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