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().
12 my $specfile = $ARGV[0];
13 my $target_dir = $ARGV[1];
17 our $comment_string_end;
25 unless ($return = do $specfile) {
26 warn "couldn't parse $specfile: $@" if $@;
27 warn "couldn't do $specfile: $!" unless defined $return;
28 warn "couldn't run $specfile" unless $return;
32 my $comment_string_quoted = quotemeta($comment_string);
34 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
35 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
38 my @obst_func; # stack for the emit functions
39 my @obst_register; # stack for emitter register code
42 foreach my $op (keys(%nodes)) {
43 my %n = %{ $nodes{"$op"} };
45 # skip this node description if no emit information is available
46 next if (!defined($n{"emit"}));
48 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
50 push(@obst_register, " BE_EMIT($op);\n");
52 if($n{"emit"} eq "") {
53 push(@obst_func, $line." {\n");
54 push(@obst_func, "}\n\n");
58 push(@obst_func, $line." {\n FILE *F = env->out;\n");
59 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
60 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
61 my @emit = split(/\n/, $n{"emit"});
63 foreach my $template (@emit) {
64 # substitute only lines, starting with a '.'
65 if ($template =~ /^(\d*)\.\s*/) {
66 my $indent = " "; # default indent is 2 spaces
68 $indent = " " x $1 if ($1 && $1 > 0);
69 # remove indent, dot and trailing spaces
70 $template =~ s/^\d*\.\s*//;
75 push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
76 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
81 $template =~ s/(\\t)*$//;
84 # add the comment begin string
85 $res .= $comment_string;
89 # substitute all format parameter
90 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
91 $res .= $`; # get everything before the match
96 elsif ($2 && $2 eq "S") {
98 $res .= "%".$3."S"; # substitute %Sx with %xS
100 elsif ($2 && $2 eq "D") {
102 $res .= "%".$3."D"; # substitute %Dx with %xD
104 elsif ($2 && $2 eq "X") {
106 $res .= "%".$3."X"; # substitute %Xx with %xX
108 elsif ($2 && $2 eq "A") {
109 push(@params, "get_irn_n(n, ".($3 - 1).")");
116 elsif ($5) { # backend provided function to call, has to return a string
117 push(@params, $5."(n, env)");
121 $template = $'; # scan everything after the match
123 $res .= $template; # get the remaining string
126 $parm = ", ".join(", ", @params) if (@params);
128 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
130 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");
133 push(@obst_func, $template,"\n");
137 push(@obst_func, "}\n\n");
140 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
142 my $creation_time = localtime(time());
147 #ifndef _GEN_$tmp\_EMITTER_H_
148 #define _GEN_$tmp\_EMITTER_H_
151 * Function prototypes for the emitter functions.
152 * DO NOT EDIT THIS FILE, your changes will be lost.
153 * Edit $specfile instead.
154 * created by: $0 $specfile $target_dir
155 * date: $creation_time
159 #include "$arch\_emitter.h"
161 void $arch\_register_spec_emitters(void);
163 #endif /* _GEN_$tmp\_EMITTER_H_ */
169 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
171 $creation_time = localtime(time());
175 * Generated functions to emit code for assembler ir nodes.
176 * DO NOT EDIT THIS FILE, your changes will be lost.
177 * Edit $specfile instead.
178 * created by: $0 $specfile $target_dir
179 * date: $creation_time
189 #include "irprog_t.h"
191 #include "gen_$arch\_emitter.h"
192 #include "$arch\_new_nodes.h"
196 print OUT @obst_func;
200 * Enters the emitter functions for handled nodes into the generic
201 * pointer of an opcode.
203 void $arch\_register_spec_emitters(void) {
205 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
207 /* generated emitter functions */
210 print OUT @obst_register;