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];
16 our $comment_string = "/*";
17 our $comment_string_end = "*/" ;
19 our $new_emit_syntax = 0;
26 unless ($return = do $specfile) {
27 warn "couldn't parse $specfile: $@" if $@;
28 warn "couldn't do $specfile: $!" unless defined $return;
29 warn "couldn't run $specfile" unless $return;
33 if ($new_emit_syntax) {
34 do "generate_emitter_new.pl";
38 my $comment_string_quoted = quotemeta($comment_string);
40 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
41 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
44 my @obst_func; # stack for the emit functions
45 my @obst_register; # stack for emitter register code
48 foreach my $op (keys(%nodes)) {
49 my %n = %{ $nodes{"$op"} };
51 # skip this node description if no emit information is available
52 next if (!defined($n{"emit"}));
54 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
56 push(@obst_register, " BE_EMIT($op);\n");
58 if($n{"emit"} eq "") {
59 push(@obst_func, $line." {\n");
60 push(@obst_func, "}\n\n");
64 push(@obst_func, $line." {\n FILE *F = env->out;\n");
65 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
66 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
67 my @emit = split(/\n/, $n{"emit"});
69 foreach my $template (@emit) {
70 # substitute only lines, starting with a '.'
71 if ($template =~ /^(\d*)\.\s*/) {
72 my $indent = " "; # default indent is 2 spaces
74 $indent = " " x $1 if ($1 && $1 > 0);
75 # remove indent, dot and trailing spaces
76 $template =~ s/^\d*\.\s*//;
81 push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
82 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
87 $template =~ s/(\\t)*$//;
90 # add the comment begin string
91 $res .= $comment_string;
95 # substitute all format parameter
96 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
97 $res .= $`; # get everything before the match
102 elsif ($2 && $2 eq "S") {
104 $res .= "%".$3."S"; # substitute %Sx with %xS
106 elsif ($2 && $2 eq "D") {
108 $res .= "%".$3."D"; # substitute %Dx with %xD
110 elsif ($2 && $2 eq "X") {
112 $res .= "%".$3."X"; # substitute %Xx with %xX
114 elsif ($2 && $2 eq "A") {
115 push(@params, "get_irn_n(n, ".($3 - 1).")");
122 elsif ($5) { # backend provided function to call, has to return a string
123 push(@params, $5."(n, env)");
127 $template = $'; # scan everything after the match
129 $res .= $template; # get the remaining string
132 $parm = ", ".join(", ", @params) if (@params);
134 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
136 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");
139 push(@obst_func, $template,"\n");
143 push(@obst_func, "}\n\n");
146 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
148 my $creation_time = localtime(time());
153 #ifndef _GEN_$tmp\_EMITTER_H_
154 #define _GEN_$tmp\_EMITTER_H_
157 * Function prototypes for the emitter functions.
158 * DO NOT EDIT THIS FILE, your changes will be lost.
159 * Edit $specfile instead.
160 * created by: $0 $specfile $target_dir
161 * date: $creation_time
165 #include "$arch\_emitter.h"
167 void $arch\_register_spec_emitters(void);
169 #endif /* _GEN_$tmp\_EMITTER_H_ */
175 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
177 $creation_time = localtime(time());
181 * Generated functions to emit code for assembler ir nodes.
182 * DO NOT EDIT THIS FILE, your changes will be lost.
183 * Edit $specfile instead.
184 * created by: $0 $specfile $target_dir
185 * date: $creation_time
195 #include "irprog_t.h"
197 #include "gen_$arch\_emitter.h"
198 #include "$arch\_new_nodes.h"
202 print OUT @obst_func;
206 * Enters the emitter functions for handled nodes into the generic
207 * pointer of an opcode.
209 void $arch\_register_spec_emitters(void) {
211 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
213 /* generated emitter functions */
216 print OUT @obst_register;