c126f1e25c1d5c64abe184bb9a05356202b835e3
[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         $line = "  BE_EMIT($op);\n";
47         push(@obst_register, $line);
48
49         my @emit = split(/\n/, $n{"emit"});
50
51         foreach my $template (@emit) {
52                 # substitute only lines, starting with a '.'
53                 if ($template =~ /^(\d*)\.\s*/) {
54                         my @params;
55                         my $res    = "";
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                         # substitute all format parameter
62                         while ($template =~ /\%([ASD])(\d)|\%([COM])|\%(\w+)/) {
63                                 $res  .= $`;      # get everything before the match
64
65                                 if ($1 && $1 eq "S") {
66                                         push(@params, "n");
67                                         $res .= "%".$2."S"; # substitute %Sx with %xS
68                                 }
69                                 elsif ($1 && $1 eq "D") {
70                                         push(@params, "n");
71                                         $res .= "%".$2."D"; # substitute %Dx with %xD
72                                 }
73                                 elsif ($1 && $1 eq "A") {
74                                         push(@params, "get_irn_n(n, ".($2 - 1).")");
75                                         $res .= "%+F";
76                                 }
77                                 elsif ($3) {
78                                         push(@params, "n");
79                                         $res .= "%".$3;
80                                 }
81                                 elsif ($4) {  # backend provided function to call, has to return a string
82                                         push(@params, $4."(n)");
83                                         $res .= "\%s";
84                                 }
85
86                                 $template = $'; # scan everything after the match
87                         }
88                         $res  .= $template; # get the remaining string
89
90                         my $parm = "";
91                         $parm = ", ".join(", ", @params) if (@params);
92
93                         push(@obst_func, $indent.'lc_efprintf('.$arch.'_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
94                 }
95                 else {
96                         push(@obst_func, $template,"\n");
97                 }
98         }
99         push(@obst_func, "}\n\n");
100 }
101
102 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
103
104 my $creation_time = localtime(time());
105
106 my $tmp = uc($arch);
107
108 print OUT<<EOF;
109 #ifndef _GEN_$tmp\_EMITTER_H_
110 #define _GEN_$tmp\_EMITTER_H_
111
112 /**
113  * Function prototypes for the emitter functions.
114  * DO NOT EDIT THIS FILE, your changes will be lost.
115  * Edit $specfile instead.
116  * created by: $0 $specfile $target_dir
117  * date:       $creation_time
118  */
119
120 #include "irnode.h"
121 #include "$arch\_emitter.h"
122
123 void $arch\_register_spec_emitters(void);
124
125 #endif /* _GEN_$tmp\_EMITTER_H_ */
126
127 EOF
128
129 close(OUT);
130
131 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
132
133 $creation_time = localtime(time());
134
135 print OUT<<EOF;
136 /**
137  * Generated functions to emit code for assembler ir nodes.
138  * DO NOT EDIT THIS FILE, your changes will be lost.
139  * Edit $specfile instead.
140  * created by: $0 $specfile $target_dir
141  * date:       $creation_time
142  */
143 #ifdef HAVE_CONFIG_H
144 #include "config.h"
145 #endif
146
147 #include <stdio.h>
148
149 #include "irnode.h"
150 #include "irop_t.h"
151 #include "irprog_t.h"
152
153 #include "gen_$arch\_emitter.h"
154 #include "$arch\_new_nodes.h"
155
156 EOF
157
158 print OUT @obst_func;
159
160 print OUT<<EOF;
161 /**
162  * Enters the emitter functions for handled nodes into the generic
163  * pointer of an opcode.
164  */
165 void $arch\_register_spec_emitters(void) {
166   int i;
167
168 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
169
170   /* first clear all generic operations */
171   for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
172     ir_op *op = get_irp_opcode(i);
173     op->ops.generic = (op_func)NULL;
174   }
175
176   /* generated emitter functions */
177 EOF
178
179 print OUT @obst_register;
180
181 print OUT<<EOF;
182
183 #undef BE_EMIT
184 }
185
186 EOF
187
188 close(OUT);