be: Replace generate_emitter.pl by generate_emitter_new.pl.
[libfirm] / ir / be / scripts / generate_emitter.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
26 use strict;
27 use Data::Dumper;
28
29 our $specfile   = $ARGV[0];
30 our $target_dir = $ARGV[1];
31
32 our $arch;
33 our %nodes;
34 our %emit_templates;
35 our $finish_line_template = "be_emit_finish_line_gas(node);";
36 our $indent_line_func;
37
38 my $return;
39
40 no strict "subs";
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;
45 }
46 use strict "subs";
47
48 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
49 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
50
51 # stacks for output
52 my @obst_func;   # stack for the emit functions
53 my @obst_register;  # stack for emitter register code
54 my $line;
55
56 sub create_emitter {
57         my $result = shift;
58         my $indent = shift;
59         my $template = shift;
60         our %emit_templates;
61         our $arch;
62
63         if (!defined($indent_line_func)) {
64                 $template = "\\t" . $template;
65         } else {
66                 push(@{$result}, "${indent}${indent_line_func};\n");
67         }
68
69         my @tokens = ($template =~ m/(?:[^%]|%%)+|\%[a-zA-Z_][a-zA-Z0-9_]*|%\./g);
70         for (@tokens) {
71                 SWITCH: {
72                         if (/%\./)       { last SWITCH; }
73                         if (/^%([^%]+)/) {
74                                 if(defined($emit_templates{$1})) {
75                                         push(@{$result}, "${indent}$emit_templates{$1}\n");
76                                 } else {
77                                         print "Warning: No emit_template defined for '$1'\n";
78                                         push(@{$result}, "${indent}$1(node);\n");
79                                 }
80                                 last SWITCH;
81                         }
82                         $_ =~ s/%%/%/g;
83                         if (length($_) == 1) {
84                                 push(@{$result}, "${indent}be_emit_char('$_');\n");
85                         } else {
86                                 push(@{$result}, "${indent}be_emit_cstring(\"$_\");\n");
87                         }
88                 }
89         }
90         push(@{$result}, "${indent}${finish_line_template}\n");
91 }
92
93
94
95 foreach my $op (keys(%nodes)) {
96         my %n = %{ $nodes{"$op"} };
97
98         # skip this node description if no emit information is available
99         next if (!defined($n{"emit"}));
100
101         $line = "static void emit_${arch}_${op}(const ir_node *node)";
102
103         push(@obst_register, "  ${arch}_register_emitter(op_${arch}_${op}, emit_${arch}_${op});\n");
104
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");
110                 next;
111         }
112
113         push(@obst_func, $line."\n");
114         push(@obst_func, "{\n");
115
116         my @emit = split(/\n/, $n{"emit"});
117
118         foreach my $template (@emit) {
119                 # substitute only lines, starting with a '.'
120                 if ($template eq '') {
121                         # nothing
122                 } elsif ($template =~ /^(\s*)\.\s*(.*)/) {
123                         my $indent = "\t$1";
124                         create_emitter(\@obst_func, $indent, $2);
125                 } else {
126                         push(@obst_func, "\t${arch}_emitf(node, \"$template\");\n");
127                 }
128         }
129
130         push(@obst_func, "}\n\n");
131 }
132
133 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
134
135 my $creation_time = localtime(time());
136
137 my $tmp = uc($arch);
138
139 print OUT<<EOF;
140 /**
141  * \@file
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
147  */
148 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
149 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
150
151 #include "irnode.h"
152 #include "${arch}_emitter.h"
153
154 void ${arch}_register_spec_emitters(void);
155
156 #endif
157
158 EOF
159
160 close(OUT);
161
162 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
163
164 $creation_time = localtime(time());
165
166 print OUT<<EOF;
167 /**
168  * \@file
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
174  */
175 #include "config.h"
176
177 #include <stdio.h>
178 #include <assert.h>
179
180 #include "irnode.h"
181 #include "irop_t.h"
182 #include "irprog_t.h"
183 #include "beemitter.h"
184
185 #include "gen_${arch}_emitter.h"
186 #include "${arch}_new_nodes.h"
187 #include "${arch}_emitter.h"
188
189 EOF
190
191 print OUT @obst_func;
192
193 print OUT<<EOF;
194
195 typedef void (*emit_func)(const ir_node *node);
196
197 static void ${arch}_register_emitter(ir_op *op, emit_func func)
198 {
199         assert(op->ops.generic == NULL);
200         op->ops.generic = (op_func)func;
201 }
202
203 /**
204  * Enters the emitter functions for handled nodes into the generic
205  * pointer of an opcode.
206  */
207 void $arch\_register_spec_emitters(void)
208 {
209 EOF
210
211 print OUT @obst_register;
212
213 print OUT<<EOF;
214 }
215
216 EOF
217
218 close(OUT);