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