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];
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;
30 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
31 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
34 my @obst_func; # stack for the emit functions
35 my @obst_register; # stack for emitter register code
38 foreach my $op (keys(%nodes)) {
39 my %n = %{ $nodes{"$op"} };
41 # skip this node description if no emit information is available
42 next if (!$n{"emit"} || length($n{"emit"}) < 1);
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 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
47 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
48 push(@obst_register, " BE_EMIT($op);\n");
50 my @emit = split(/\n/, $n{"emit"});
52 foreach my $template (@emit) {
53 # substitute only lines, starting with a '.'
54 if ($template =~ /^(\d*)\.\s*/) {
55 my $indent = " "; # default indent is 2 spaces
57 $indent = " " x $1 if ($1 && $1 > 0);
58 # remove indent, dot and trailing spaces
59 $template =~ s/^\d*\.\s*//;
64 foreach $template (split(/\/\*/, $fmt, 2)) {
69 $template =~ s/(\\t)*$//;
72 # add the comment begin string
77 # substitute all format parameter
78 while ($template =~ /\%([ASD])(\d)|\%([COM])|\%(\w+)/) {
79 $res .= $`; # get everything before the match
81 if ($1 && $1 eq "S") {
83 $res .= "%".$2."S"; # substitute %Sx with %xS
85 elsif ($1 && $1 eq "D") {
87 $res .= "%".$2."D"; # substitute %Dx with %xD
89 elsif ($1 && $1 eq "A") {
90 push(@params, "get_irn_n(n, ".($2 - 1).")");
97 elsif ($4) { # backend provided function to call, has to return a string
98 push(@params, $4."(n)");
102 $template = $'; # scan everything after the match
104 $res .= $template; # get the remaining string
107 $parm = ", ".join(", ", @params) if (@params);
109 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
113 push(@obst_func, $template,"\n");
117 push(@obst_func, ' lc_efprintf(arg_env, F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, n);'."\n");
118 push(@obst_func, "}\n\n");
121 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
123 my $creation_time = localtime(time());
128 #ifndef _GEN_$tmp\_EMITTER_H_
129 #define _GEN_$tmp\_EMITTER_H_
132 * Function prototypes for the emitter functions.
133 * DO NOT EDIT THIS FILE, your changes will be lost.
134 * Edit $specfile instead.
135 * created by: $0 $specfile $target_dir
136 * date: $creation_time
140 #include "$arch\_emitter.h"
142 void $arch\_register_spec_emitters(void);
144 #endif /* _GEN_$tmp\_EMITTER_H_ */
150 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
152 $creation_time = localtime(time());
156 * Generated functions to emit code for assembler ir nodes.
157 * DO NOT EDIT THIS FILE, your changes will be lost.
158 * Edit $specfile instead.
159 * created by: $0 $specfile $target_dir
160 * date: $creation_time
170 #include "irprog_t.h"
172 #include "gen_$arch\_emitter.h"
173 #include "$arch\_new_nodes.h"
177 print OUT @obst_func;
181 * Enters the emitter functions for handled nodes into the generic
182 * pointer of an opcode.
184 void $arch\_register_spec_emitters(void) {
186 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
188 /* generated emitter functions */
191 print OUT @obst_register;