2c95ee30b626528864447c22f1f8f7c5a90aff03
[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_header;  # stack for the function prototypes
36 my $line;
37
38 # some default emitter functions (Copy, Perm)
39
40 $line  = "#undef is_ia32_Perm\n";
41 $line .= "#define is_ia32_Perm(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm && ! is_Proj(irn))\n";
42 $line .= "#undef is_ia32_Copy\n";
43 $line .= "#define is_ia32_Copy(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_copy)\n";
44 push(@obst_header, $line."\n");
45
46 $line = "void emit_".$arch."_Copy(ir_node *n, emit_env_t *env)";
47 push(@obst_header, $line.";\n");
48 $line .= " {\n  FILE *F = env->out;\n";
49 $line .= '  lc_efprintf(ia32_get_arg_env(), F, "\tmov %1S, %1D\t\t\t/* %+F */\n", n, n, n);'."\n}\n\n";
50 push(@obst_func, $line);
51
52 $line = "void emit_".$arch."_Perm(ir_node *n, emit_env_t *env)";
53 push(@obst_header, $line.";\n");
54 $line .= " {\n  FILE *F = env->out;\n";
55 $line .= '  lc_efprintf(ia32_get_arg_env(), F, "\txchg %1S, %1D\t\t\t/* %+F */\n", n, n, n);'."\n}\n\n";
56 push(@obst_func, $line);
57
58 foreach my $op (keys(%nodes)) {
59   my %n = %{ $nodes{"$op"} };
60
61   # skip this node description if no emit information is available
62   next if (!$n{"emit"} || length($n{"emit"}) < 1);
63
64   $line = "void emit_".$arch."_".$op."(ir_node *n, emit_env_t *env)";
65   push(@obst_header, $line.";\n");
66   push(@obst_func, $line." {\n  FILE *F = env->out;\n");
67
68   my $cio = 0;
69   # check in/out register if needed
70   if (exists($n{"check_inout"}) && $n{"check_inout"} == 1) {
71     push(@obst_func, "  equalize_dest_src(F, n);\n\n");
72     $cio = 1;
73   }
74
75   my @emit = split(/\n/, $n{"emit"});
76
77   foreach my $template (@emit) {
78     # substitute only lines, starting with a '.'
79     if ($template =~ /^(\d*)\.\s*/) {
80       my @params;
81       my $res    = "";
82       my $res2   = "";
83       my $indent = "  "; # default indent is 2 spaces
84
85       $indent = " " x $1 if ($1 && $1 > 0);
86       # remove indent, dot and trailing spaces
87       $template =~ s/^\d*\.\s*//;
88       # substitute all format parameter
89       while ($template =~ /\%(([ASD])(\d)|([COM]))/) {
90         $res  .= $`;      # get everything before the match
91         $res2 .= $`;
92
93         if ($4 && $4 eq "C") {
94           push(@params, "n");
95           $res  .= "\%C";
96           $res2 .= "\%C";
97         }
98         elsif ($4 && $4 eq "O") {
99           push(@params, "n");
100           $res  .= "\%O";
101           $res2 .= "\%O";
102         }
103         elsif ($4 && $4 eq "M") {
104           push(@params, "n");
105           $res  .= "\%M";
106           $res2 .= "\%M";
107         }
108         elsif ($2 && $2 eq "S") {
109           push(@params, "n");
110           if ($cio && $3 == 2) {
111             # check_in_out was set: if (s1 != d1) we
112             # need to exchange s2 by s1
113             $res2 .= "%1S"; # get name for first register
114           }
115           else {
116             $res2 .= "%".$3."S"; # substitute %sx with %xs
117           }
118           $res .= "%".$3."S"; # substitute %sx with %xs
119         }
120         elsif ($2 && $2 eq "D") {
121           push(@params, "n");
122           $res  .= "%".$3."D"; # substitute %sx with %xs
123           $res2 .= "%".$3."D"; # substitute %sx with %xs
124         }
125         elsif ($2 && $2 eq "A") {
126           push(@params, "get_irn_n(n, ".($3 - 1).")");
127           $res  .= "%+F";
128           $res2 .= "%+F";
129         }
130
131         $template = $'; # scan everything after the match
132       }
133       $res  .= $template; # get the remaining string
134       $res2 .= $template; # get the remaining string
135
136       my $parm = "";
137       $parm = ", ".join(", ", @params) if (@params);
138
139       if ($cio) {
140         push(@obst_func, $indent."if (get_irn_arity(n) > 1 && get_$arch\_reg_nr(n, 1, 1) == get_$arch\_reg_nr(n, 0, 0)) {\n");
141         push(@obst_func, $indent.'  lc_efprintf(ia32_get_arg_env(), F, "\t'.$res2.'\n"'.$parm.');'."\n");
142         push(@obst_func, $indent."}\n");
143         push(@obst_func, $indent."else {\n");
144         push(@obst_func, $indent.'  lc_efprintf(ia32_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
145         push(@obst_func, $indent."}\n");
146       }
147       else {
148         push(@obst_func, $indent.'lc_efprintf(ia32_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
149       }
150     }
151     else {
152       push(@obst_func, $template,"\n");
153     }
154   }
155   push(@obst_func, "}\n\n");
156 }
157
158 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
159
160 my $creation_time = localtime(time());
161
162 my $tmp = uc($arch);
163
164 print OUT<<EOF;
165 #ifndef _GEN_$tmp\_EMITTER_H_
166 #define _GEN_$tmp\_EMITTER_H_
167
168 /**
169  * Function prototypes for the emitter functions.
170  * DO NOT EDIT THIS FILE, your changes will be lost.
171  * Edit $specfile instead.
172  * created by: $0 $specfile $target_dir
173  * date:       $creation_time
174  */
175
176 #include "irnode.h"
177 #include "$arch\_emitter.h"
178
179 EOF
180
181 print OUT @obst_header;
182
183 print OUT "#endif /* _GEN_$tmp\_EMITTER_H_ */\n";
184
185 close(OUT);
186
187 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
188
189 $creation_time = localtime(time());
190
191 print OUT<<EOF;
192 /**
193  * Generated functions to emit code for assembler ir nodes.
194  * DO NOT EDIT THIS FILE, your changes will be lost.
195  * Edit $specfile instead.
196  * created by: $0 $specfile $target_dir
197  * date:       $creation_time
198  */
199 #ifdef HAVE_CONFIG_H
200 #include "config.h"
201 #endif
202
203 #include <stdio.h>
204
205 #include "irnode.h"
206 #include "gen_$arch\_emitter.h"
207 #include "$arch\_new_nodes.h"
208
209 EOF
210
211 print OUT @obst_func;
212
213 close(OUT);