f2540727e4644fecbc072317b9b938fc67f2c355
[libfirm] / ir / be / scripts / generate_emitter.pl
1 #!/usr/bin/perl -w
2
3 # This script generates C code which emits assembler code for the
4 # assembler ir nodes. It takes a "emit" key from the node specification
5 # and substitutes lines starting with . with a corresponding fprintf().
6 # Creation: 2005/11/07
7 # $Id$
8
9 use strict;
10 use Data::Dumper;
11 use File::Basename;
12
13 my $myname = $0;
14 our $specfile   = $ARGV[0];
15 our $target_dir = $ARGV[1];
16
17 our $arch;
18 our $comment_string = "/*";
19 our $comment_string_end = "*/" ;
20 our %nodes;
21 our $new_emit_syntax = 0;
22
23 # include spec file
24
25 my $return;
26
27 no strict "subs";
28 unless ($return = do $specfile) {
29         warn "couldn't parse $specfile: $@" if $@;
30         warn "couldn't do $specfile: $!"    unless defined $return;
31         warn "couldn't run $specfile"       unless $return;
32 }
33 use strict "subs";
34
35 if ($new_emit_syntax) {
36         my $newscript = dirname($myname) . "/generate_emitter_new.pl";
37         unless ($return = do "$newscript") {
38                 warn "couldn't parse $newscript: $@" if $@;
39                 warn "couldn't do $newscript: $!"    unless defined $return;
40                 warn "couldn't run $newscript"       unless $return;
41         }
42 }
43
44 my $comment_string_quoted = quotemeta($comment_string);
45
46 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
47 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
48
49 # stacks for output
50 my @obst_func;   # stack for the emit functions
51 my @obst_register;  # stack for emitter register code
52 my $line;
53
54 foreach my $op (keys(%nodes)) {
55         my %n = %{ $nodes{"$op"} };
56
57         # skip this node description if no emit information is available
58         next if (!defined($n{"emit"}));
59
60         $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
61
62         push(@obst_register, "  BE_EMIT($op);\n");
63
64         if($n{"emit"} eq "") {
65                 push(@obst_func, $line." {\n");
66                 push(@obst_func, "}\n\n");
67                 next;
68         }
69
70         push(@obst_func, $line." {\n  FILE *F = env->out;\n");
71         push(@obst_func, "  char cmd_buf[256], cmnt_buf[256];\n");
72         push(@obst_func, "  const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
73         my @emit = split(/\n/, $n{"emit"});
74
75         foreach my $template (@emit) {
76                 # substitute only lines, starting with a '.'
77                 if ($template =~ /^(\d*)\.\s*/) {
78                         my $indent = "  "; # default indent is 2 spaces
79
80                         $indent = " " x $1 if ($1 && $1 > 0);
81                         # remove indent, dot and trailing spaces
82                         $template =~ s/^\d*\.\s*//;
83                         my $fmt = $template;
84                         my $cnt = 0;
85                         my $buf = 'cmd_buf';
86
87                         push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
88                         foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
89                                 my @params;
90                                 my $res = "";
91                                 $cnt++;
92
93                                 $template =~ s/(\\t)*$//;
94
95                                 if ($cnt == 2) {
96                                         # add the comment begin string
97                                         $res .= $comment_string;
98                                         $buf  = "cmnt_buf";
99                                 }
100
101                                 # substitute all format parameter
102                                 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
103                                         $res  .= $`;      # get everything before the match
104
105                                         if ($1) {
106                                                 $res .= "%%";
107                                         }
108                                         elsif ($2 && $2 eq "S") {
109                                                 push(@params, "n");
110                                                 $res .= "%".$3."S"; # substitute %Sx with %xS
111                                         }
112                                         elsif ($2 && $2 eq "D") {
113                                                 push(@params, "n");
114                                                 $res .= "%".$3."D"; # substitute %Dx with %xD
115                                         }
116                                         elsif ($2 && $2 eq "X") {
117                                                 push(@params, "n");
118                                                 $res .= "%".$3."X"; # substitute %Xx with %xX
119                                         }
120                                         elsif ($2 && $2 eq "A") {
121                                                 push(@params, "get_irn_n(n, ".($3 - 1).")");
122                                                 $res .= "%+F";
123                                         }
124                                         elsif ($4) {
125                                                 push(@params, "n");
126                                                 $res .= "%".$4;
127                                         }
128                                         elsif ($5) {  # backend provided function to call, has to return a string
129                                                 push(@params, $5."(n, env)");
130                                                 $res .= "\%s";
131                                         }
132
133                                         $template = $'; # scan everything after the match
134                                 }
135                                 $res .= $template; # get the remaining string
136
137                                 my $parm = "";
138                                 $parm = ", ".join(", ", @params) if (@params);
139
140                                 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
141                         }
142                         push(@obst_func, $indent.'lc_efprintf(arg_env, F, "\t%-35s %-60s '.$comment_string.' %+F (%+G) '.$comment_string_end.'\n", cmd_buf, cmnt_buf, n, n);'."\n");
143                 }
144                 else {
145                         push(@obst_func, $template,"\n");
146                 }
147         }
148
149         push(@obst_func, "}\n\n");
150 }
151
152 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
153
154 my $creation_time = localtime(time());
155
156 my $tmp = uc($arch);
157
158 print OUT<<EOF;
159 #ifndef _GEN_$tmp\_EMITTER_H_
160 #define _GEN_$tmp\_EMITTER_H_
161
162 /**
163  * Function prototypes for the emitter functions.
164  * DO NOT EDIT THIS FILE, your changes will be lost.
165  * Edit $specfile instead.
166  * created by: $0 $specfile $target_dir
167  * date:       $creation_time
168  */
169
170 #include "irnode.h"
171 #include "$arch\_emitter.h"
172
173 void $arch\_register_spec_emitters(void);
174
175 #endif /* _GEN_$tmp\_EMITTER_H_ */
176
177 EOF
178
179 close(OUT);
180
181 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
182
183 $creation_time = localtime(time());
184
185 print OUT<<EOF;
186 /**
187  * Generated functions to emit code for assembler ir nodes.
188  * DO NOT EDIT THIS FILE, your changes will be lost.
189  * Edit $specfile instead.
190  * created by: $0 $specfile $target_dir
191  * date:       $creation_time
192  */
193 #ifdef HAVE_CONFIG_H
194 #include "config.h"
195 #endif
196
197 #include <stdio.h>
198
199 #include "irnode.h"
200 #include "irop_t.h"
201 #include "irprog_t.h"
202
203 #include "gen_$arch\_emitter.h"
204 #include "$arch\_new_nodes.h"
205
206 EOF
207
208 print OUT @obst_func;
209
210 print OUT<<EOF;
211 /**
212  * Enters the emitter functions for handled nodes into the generic
213  * pointer of an opcode.
214  */
215 void $arch\_register_spec_emitters(void) {
216
217 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
218
219   /* generated emitter functions */
220 EOF
221
222 print OUT @obst_register;
223
224 print OUT<<EOF;
225
226 #undef BE_EMIT
227 }
228
229 EOF
230
231 close(OUT);