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