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];
24 unless ($return = do $specfile) {
25 warn "couldn't parse $specfile: $@" if $@;
26 warn "couldn't do $specfile: $!" unless defined $return;
27 warn "couldn't run $specfile" unless $return;
31 my $comment_string_quoted = quotemeta($comment_string);
33 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
34 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
37 my @obst_func; # stack for the emit functions
38 my @obst_register; # stack for emitter register code
41 foreach my $op (keys(%nodes)) {
42 my %n = %{ $nodes{"$op"} };
44 # skip this node description if no emit information is available
45 next if (!$n{"emit"} || length($n{"emit"}) < 1);
47 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
48 push(@obst_func, $line." {\n FILE *F = env->out;\n");
49 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
50 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
51 push(@obst_register, " BE_EMIT($op);\n");
53 my @emit = split(/\n/, $n{"emit"});
55 foreach my $template (@emit) {
56 # substitute only lines, starting with a '.'
57 if ($template =~ /^(\d*)\.\s*/) {
58 my $indent = " "; # default indent is 2 spaces
60 $indent = " " x $1 if ($1 && $1 > 0);
61 # remove indent, dot and trailing spaces
62 $template =~ s/^\d*\.\s*//;
67 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
72 $template =~ s/(\\t)*$//;
75 # add the comment begin string
76 $res .= $comment_string;
80 # substitute all format parameter
81 while ($template =~ /\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
82 $res .= $`; # get everything before the match
84 if ($1 && $1 eq "S") {
86 $res .= "%".$2."S"; # substitute %Sx with %xS
88 elsif ($1 && $1 eq "D") {
90 $res .= "%".$2."D"; # substitute %Dx with %xD
92 elsif ($1 && $1 eq "X") {
94 $res .= "%".$2."X"; # substitute %Xx with %xX
96 elsif ($1 && $1 eq "A") {
97 push(@params, "get_irn_n(n, ".($2 - 1).")");
104 elsif ($4) { # backend provided function to call, has to return a string
105 push(@params, $4."(n, env)");
109 $template = $'; # scan everything after the match
111 $res .= $template; # get the remaining string
114 $parm = ", ".join(", ", @params) if (@params);
116 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
120 push(@obst_func, $template,"\n");
124 push(@obst_func, ' lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F (%+G) */\n", cmd_buf, cmnt_buf, n, n);'."\n");
125 push(@obst_func, "}\n\n");
128 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
130 my $creation_time = localtime(time());
135 #ifndef _GEN_$tmp\_EMITTER_H_
136 #define _GEN_$tmp\_EMITTER_H_
139 * Function prototypes for the emitter functions.
140 * DO NOT EDIT THIS FILE, your changes will be lost.
141 * Edit $specfile instead.
142 * created by: $0 $specfile $target_dir
143 * date: $creation_time
147 #include "$arch\_emitter.h"
149 void $arch\_register_spec_emitters(void);
151 #endif /* _GEN_$tmp\_EMITTER_H_ */
157 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
159 $creation_time = localtime(time());
163 * Generated functions to emit code for assembler ir nodes.
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
177 #include "irprog_t.h"
179 #include "gen_$arch\_emitter.h"
180 #include "$arch\_new_nodes.h"
184 print OUT @obst_func;
188 * Enters the emitter functions for handled nodes into the generic
189 * pointer of an opcode.
191 void $arch\_register_spec_emitters(void) {
193 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
195 /* generated emitter functions */
198 print OUT @obst_register;