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