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