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