removed assert
[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 =~ /\%([ASD])(\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 "A") {
93                                                 push(@params, "get_irn_n(n, ".($2 - 1).")");
94                                                 $res .= "%+F";
95                                         }
96                                         elsif ($3) {
97                                                 push(@params, "n");
98                                                 $res .= "%".$3;
99                                         }
100                                         elsif ($4) {  # backend provided function to call, has to return a string
101                                                 push(@params, $4."(n, env)");
102                                                 $res .= "\%s";
103                                         }
104
105                                         $template = $'; # scan everything after the match
106                                 }
107                                 $res .= $template; # get the remaining string
108
109                                 my $parm = "";
110                                 $parm = ", ".join(", ", @params) if (@params);
111
112                                 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
113                         }
114                 }
115                 else {
116                         push(@obst_func, $template,"\n");
117                 }
118         }
119
120         push(@obst_func, '  lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, n);'."\n");
121         push(@obst_func, "}\n\n");
122 }
123
124 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
125
126 my $creation_time = localtime(time());
127
128 my $tmp = uc($arch);
129
130 print OUT<<EOF;
131 #ifndef _GEN_$tmp\_EMITTER_H_
132 #define _GEN_$tmp\_EMITTER_H_
133
134 /**
135  * Function prototypes for the emitter functions.
136  * DO NOT EDIT THIS FILE, your changes will be lost.
137  * Edit $specfile instead.
138  * created by: $0 $specfile $target_dir
139  * date:       $creation_time
140  */
141
142 #include "irnode.h"
143 #include "$arch\_emitter.h"
144
145 void $arch\_register_spec_emitters(void);
146
147 #endif /* _GEN_$tmp\_EMITTER_H_ */
148
149 EOF
150
151 close(OUT);
152
153 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
154
155 $creation_time = localtime(time());
156
157 print OUT<<EOF;
158 /**
159  * Generated functions to emit code for assembler ir nodes.
160  * DO NOT EDIT THIS FILE, your changes will be lost.
161  * Edit $specfile instead.
162  * created by: $0 $specfile $target_dir
163  * date:       $creation_time
164  */
165 #ifdef HAVE_CONFIG_H
166 #include "config.h"
167 #endif
168
169 #include <stdio.h>
170
171 #include "irnode.h"
172 #include "irop_t.h"
173 #include "irprog_t.h"
174
175 #include "gen_$arch\_emitter.h"
176 #include "$arch\_new_nodes.h"
177
178 EOF
179
180 print OUT @obst_func;
181
182 print OUT<<EOF;
183 /**
184  * Enters the emitter functions for handled nodes into the generic
185  * pointer of an opcode.
186  */
187 void $arch\_register_spec_emitters(void) {
188
189 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
190
191   /* generated emitter functions */
192 EOF
193
194 print OUT @obst_register;
195
196 print OUT<<EOF;
197
198 #undef BE_EMIT
199 }
200
201 EOF
202
203 close(OUT);