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