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().
14 our $specfile = $ARGV[0];
15 our $target_dir = $ARGV[1];
18 our $comment_string = "/*";
19 our $comment_string_end = "*/" ;
21 our $new_emit_syntax = 0;
28 unless ($return = do $specfile) {
29 warn "couldn't parse $specfile: $@" if $@;
30 warn "couldn't do $specfile: $!" unless defined $return;
31 warn "couldn't run $specfile" unless $return;
35 if ($new_emit_syntax) {
36 my $newscript = dirname($myname) . "/generate_emitter_new.pl";
37 unless ($return = do "$newscript") {
38 warn "couldn't parse $newscript: $@" if $@;
39 warn "couldn't do $newscript: $!" unless defined $return;
40 warn "couldn't run $newscript" unless $return;
44 my $comment_string_quoted = quotemeta($comment_string);
46 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
47 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
50 my @obst_func; # stack for the emit functions
51 my @obst_register; # stack for emitter register code
54 foreach my $op (keys(%nodes)) {
55 my %n = %{ $nodes{"$op"} };
57 # skip this node description if no emit information is available
58 next if (!defined($n{"emit"}));
60 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
62 push(@obst_register, " BE_EMIT($op);\n");
64 if($n{"emit"} eq "") {
65 push(@obst_func, $line." {\n");
66 push(@obst_func, "}\n\n");
70 push(@obst_func, $line." {\n FILE *F = env->out;\n");
71 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
72 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
73 my @emit = split(/\n/, $n{"emit"});
75 foreach my $template (@emit) {
76 # substitute only lines, starting with a '.'
77 if ($template =~ /^(\d*)\.\s*/) {
78 my $indent = " "; # default indent is 2 spaces
80 $indent = " " x $1 if ($1 && $1 > 0);
81 # remove indent, dot and trailing spaces
82 $template =~ s/^\d*\.\s*//;
87 push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
88 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
93 $template =~ s/(\\t)*$//;
96 # add the comment begin string
97 $res .= $comment_string;
101 # substitute all format parameter
102 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
103 $res .= $`; # get everything before the match
108 elsif ($2 && $2 eq "S") {
110 $res .= "%".$3."S"; # substitute %Sx with %xS
112 elsif ($2 && $2 eq "D") {
114 $res .= "%".$3."D"; # substitute %Dx with %xD
116 elsif ($2 && $2 eq "X") {
118 $res .= "%".$3."X"; # substitute %Xx with %xX
120 elsif ($2 && $2 eq "A") {
121 push(@params, "get_irn_n(n, ".($3 - 1).")");
128 elsif ($5) { # backend provided function to call, has to return a string
129 push(@params, $5."(n, env)");
133 $template = $'; # scan everything after the match
135 $res .= $template; # get the remaining string
138 $parm = ", ".join(", ", @params) if (@params);
140 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
142 push(@obst_func, $indent.'lc_efprintf(arg_env, F, "\t%-35s %-60s '.$comment_string.' %+F (%+G) '.$comment_string_end.'\n", cmd_buf, cmnt_buf, n, n);'."\n");
145 push(@obst_func, $template,"\n");
149 push(@obst_func, "}\n\n");
152 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
154 my $creation_time = localtime(time());
159 #ifndef _GEN_$tmp\_EMITTER_H_
160 #define _GEN_$tmp\_EMITTER_H_
163 * Function prototypes for the emitter functions.
164 * DO NOT EDIT THIS FILE, your changes will be lost.
165 * Edit $specfile instead.
166 * created by: $0 $specfile $target_dir
167 * date: $creation_time
171 #include "$arch\_emitter.h"
173 void $arch\_register_spec_emitters(void);
175 #endif /* _GEN_$tmp\_EMITTER_H_ */
181 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
183 $creation_time = localtime(time());
187 * Generated functions to emit code for assembler ir nodes.
188 * DO NOT EDIT THIS FILE, your changes will be lost.
189 * Edit $specfile instead.
190 * created by: $0 $specfile $target_dir
191 * date: $creation_time
201 #include "irprog_t.h"
203 #include "gen_$arch\_emitter.h"
204 #include "$arch\_new_nodes.h"
208 print OUT @obst_func;
212 * Enters the emitter functions for handled nodes into the generic
213 * pointer of an opcode.
215 void $arch\_register_spec_emitters(void) {
217 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
219 /* generated emitter functions */
222 print OUT @obst_register;