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