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