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