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