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;
45 my $comment_string_quoted = quotemeta($comment_string);
47 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
48 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
51 my @obst_func; # stack for the emit functions
52 my @obst_register; # stack for emitter register code
55 foreach my $op (keys(%nodes)) {
56 my %n = %{ $nodes{"$op"} };
58 # skip this node description if no emit information is available
59 next if (!defined($n{"emit"}));
61 $line = "static void emit_".$arch."_".$op."(const ir_node *n, $arch\_emit_env_t *env)";
63 push(@obst_register, " BE_EMIT($op);\n");
65 if($n{"emit"} eq "") {
66 push(@obst_func, $line." {\n");
67 push(@obst_func, "}\n\n");
71 push(@obst_func, $line." {\n FILE *F = env->out;\n");
72 push(@obst_func, " char cmd_buf[256], cmnt_buf[256];\n");
73 push(@obst_func, " const lc_arg_env_t *arg_env = $arch\_get_arg_env();\n\n");
74 my @emit = split(/\n/, $n{"emit"});
76 foreach my $template (@emit) {
77 # substitute only lines, starting with a '.'
78 if ($template =~ /^(\d*)\.\s*/) {
79 my $indent = " "; # default indent is 2 spaces
81 $indent = " " x $1 if ($1 && $1 > 0);
82 # remove indent, dot and trailing spaces
83 $template =~ s/^\d*\.\s*//;
88 push(@obst_func, $indent."cmnt_buf[0] = '\\0';\n");
89 foreach $template (split(/$comment_string_quoted/, $fmt, 2)) {
94 $template =~ s/(\\t)*$//;
97 # add the comment begin string
98 $res .= $comment_string;
102 # substitute all format parameter
103 while ($template =~ /(\%\%)|\%([ASDX])(\d)|\%([COM])|\%(\w+)/) {
104 $res .= $`; # get everything before the match
109 elsif ($2 && $2 eq "S") {
111 $res .= "%".$3."S"; # substitute %Sx with %xS
113 elsif ($2 && $2 eq "D") {
115 $res .= "%".$3."D"; # substitute %Dx with %xD
117 elsif ($2 && $2 eq "X") {
119 $res .= "%".$3."X"; # substitute %Xx with %xX
121 elsif ($2 && $2 eq "A") {
122 push(@params, "get_irn_n(n, ".($3 - 1).")");
129 elsif ($5) { # backend provided function to call, has to return a string
130 push(@params, $5."(n, env)");
134 $template = $'; # scan everything after the match
136 $res .= $template; # get the remaining string
139 $parm = ", ".join(", ", @params) if (@params);
141 push(@obst_func, $indent.'lc_esnprintf(arg_env, '.$buf.', 256, "'.$res.'"'.$parm.');'."\n");
143 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");
146 push(@obst_func, $template,"\n");
150 push(@obst_func, "}\n\n");
153 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
155 my $creation_time = localtime(time());
160 #ifndef _GEN_$tmp\_EMITTER_H_
161 #define _GEN_$tmp\_EMITTER_H_
164 * Function prototypes for the emitter functions.
165 * DO NOT EDIT THIS FILE, your changes will be lost.
166 * Edit $specfile instead.
167 * created by: $0 $specfile $target_dir
168 * date: $creation_time
172 #include "$arch\_emitter.h"
174 void $arch\_register_spec_emitters(void);
176 #endif /* _GEN_$tmp\_EMITTER_H_ */
182 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
184 $creation_time = localtime(time());
188 * Generated functions to emit code for assembler ir nodes.
189 * DO NOT EDIT THIS FILE, your changes will be lost.
190 * Edit $specfile instead.
191 * created by: $0 $specfile $target_dir
192 * date: $creation_time
202 #include "irprog_t.h"
204 #include "gen_$arch\_emitter.h"
205 #include "$arch\_new_nodes.h"
209 print OUT @obst_func;
213 * Enters the emitter functions for handled nodes into the generic
214 * pointer of an opcode.
216 void $arch\_register_spec_emitters(void) {
218 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
220 /* generated emitter functions */
223 print OUT @obst_register;