0e210aaeb4a95e341b4efbbc219d4f5df3443434
[libfirm] / ir / be / scripts / generate_emitter_new.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 $comment_string_end;
18 our %nodes;
19 our %emit_templates;
20 our $spec_version = 1;
21
22 # include spec file
23
24 my $return;
25
26 no strict "subs";
27 unless ($return = do $specfile) {
28         warn "couldn't parse $specfile: $@" if $@;
29         warn "couldn't do $specfile: $!"    unless defined $return;
30         warn "couldn't run $specfile"       unless $return;
31 }
32 use strict "subs";
33
34 my $comment_string_quoted = quotemeta($comment_string);
35
36 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
37 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
38
39 # stacks for output
40 my @obst_func;   # stack for the emit functions
41 my @obst_register;  # stack for emitter register code
42 my $line;
43
44 sub create_emitter {
45         my $result = shift;
46         my $indent = shift;
47         my $template = shift;
48         our %emit_templates;
49         our $arch;
50
51         my @tokens = ($template =~ m/[^\%]+|\%[a-zA-Z_][a-zA-Z0-9_]*|\%./g);
52         push(@{$result}, "${indent}${arch}_emit_char(env, '\t');\n");
53         for (@tokens) {
54                 SWITCH: {
55                         if (/%\./)      { last SWITCH; }
56                         if (/%%/)       { push(@{$result}, "${indent}${arch}_emit_char(env, '%');\n"); last SWITCH; }
57                         if (/%(.+)/)    {
58                                 if(defined($emit_templates{$1})) {
59                                         push(@{$result}, "${indent}$emit_templates{$1}\n");
60                                 } else {
61                                         print "Warning: No emit_template defined for '$1'\n";
62                                         push(@{$result}, "${indent}$1(env, node);\n");
63                                 }
64                                 last SWITCH;
65                         }
66                         push(@{$result}, "${indent}${arch}_emit_cstring(env, \"$_\");\n");
67                 }
68         }
69         push(@{$result}, "${indent}${arch}_emit_finish_line(env, node);\n");
70 }
71
72
73
74 foreach my $op (keys(%nodes)) {
75         my %n = %{ $nodes{"$op"} };
76
77         # skip this node description if no emit information is available
78         next if (!defined($n{"emit"}));
79
80         $line = "static void emit_".$arch."_".$op."(${arch}_emit_env_t *env, const ir_node *node)";
81
82         push(@obst_register, "  BE_EMIT($op);\n");
83
84         if($n{"emit"} eq "") {
85                 push(@obst_func, $line." {\n");
86                 push(@obst_func, "}\n\n");
87                 next;
88         }
89
90         push(@obst_func, $line." {\n");
91         my @emit = split(/\n/, $n{"emit"});
92
93         if($spec_version < 3) { last; }
94
95         foreach my $template (@emit) {
96                 # substitute only lines, starting with a '.'
97                 if ($template =~ /^(\s*)\.\s*(.*)/) {
98                         my $indent = "\t$1";
99                         create_emitter(\@obst_func, $indent, $2);
100                 } else {
101                         push(@obst_func, "\t$template\n");
102                 }
103         }
104
105         push(@obst_func, "}\n\n");
106 }
107
108 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
109
110 my $creation_time = localtime(time());
111
112 my $tmp = uc($arch);
113
114 print OUT<<EOF;
115 #ifndef _GEN_$tmp\_EMITTER_H_
116 #define _GEN_$tmp\_EMITTER_H_
117
118 /**
119  * Function prototypes for the emitter functions.
120  * DO NOT EDIT THIS FILE, your changes will be lost.
121  * Edit $specfile instead.
122  * created by: $0 $specfile $target_dir
123  * date:       $creation_time
124  */
125
126 #include "irnode.h"
127 #include "$arch\_emitter.h"
128
129 void $arch\_register_spec_emitters(void);
130
131 #endif /* _GEN_$tmp\_EMITTER_H_ */
132
133 EOF
134
135 close(OUT);
136
137 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
138
139 $creation_time = localtime(time());
140
141 print OUT<<EOF;
142 /**
143  * Generated functions to emit code for assembler ir nodes.
144  * DO NOT EDIT THIS FILE, your changes will be lost.
145  * Edit $specfile instead.
146  * created by: $0 $specfile $target_dir
147  * date:       $creation_time
148  */
149 #ifdef HAVE_CONFIG_H
150 #include <config.h>
151 #endif
152
153 #include <stdio.h>
154
155 #include "irnode.h"
156 #include "irop_t.h"
157 #include "irprog_t.h"
158
159 #include "gen_${arch}_emitter.h"
160 #include "${arch}_new_nodes.h"
161 #include "${arch}_emitter.h"
162
163 EOF
164
165 print OUT @obst_func;
166
167 print OUT<<EOF;
168 /**
169  * Enters the emitter functions for handled nodes into the generic
170  * pointer of an opcode.
171  */
172 void $arch\_register_spec_emitters(void) {
173
174 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
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);