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_header; # stack for the function prototypes
38 # some default emitter functions (Copy, Perm)
40 $line = "#undef is_ia32_Perm\n";
41 $line .= "#define is_ia32_Perm(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_perm && ! is_Proj(irn))\n";
42 $line .= "#undef is_ia32_Copy\n";
43 $line .= "#define is_ia32_Copy(irn) (arch_irn_classify(arch_env, irn) == arch_irn_class_copy)\n";
44 push(@obst_header, $line."\n");
46 $line = "void emit_".$arch."_Copy(ir_node *n, emit_env_t *env)";
47 push(@obst_header, $line.";\n");
48 $line .= " {\n FILE *F = env->out;\n";
49 $line .= ' lc_efprintf(ia32_get_arg_env(), F, "\tmov %1s, %1d\t\t\t/* %+F */\n", n, n, n);'."\n}\n\n";
50 push(@obst_func, $line);
52 $line = "void emit_".$arch."_Perm(ir_node *n, emit_env_t *env)";
53 push(@obst_header, $line.";\n");
54 $line .= " {\n FILE *F = env->out;\n";
55 $line .= ' lc_efprintf(ia32_get_arg_env(), F, "\txchg %1s, %1d\t\t\t/* %+F */\n", n, n, n);'."\n}\n\n";
56 push(@obst_func, $line);
58 foreach my $op (keys(%nodes)) {
59 my %n = %{ $nodes{"$op"} };
61 # skip this node description if no emit information is available
62 next if (!$n{"emit"} || length($n{"emit"}) < 1);
64 $line = "void emit_".$arch."_".$op."(ir_node *n, emit_env_t *env)";
65 push(@obst_header, $line.";\n");
66 push(@obst_func, $line." {\n FILE *F = env->out;\n");
69 # check in/out register if needed
70 if (exists($n{"check_inout"}) && $n{"check_inout"} == 1) {
71 push(@obst_func, " equalize_dest_src(F, n);\n\n");
75 my @emit = split(/\n/, $n{"emit"});
77 foreach my $template (@emit) {
78 # substitute only lines, starting with a '.'
79 if ($template =~ /^(\d*)\.\s*/) {
83 my $indent = " "; # default indent is 2 spaces
85 $indent = " " x $1 if ($1 && $1 > 0);
86 # remove indent, dot and trailing spaces
87 $template =~ s/^\d*\.\s*//;
88 # substitute all format parameter
89 while ($template =~ /\%(([asd])(\d)|([com]))/) {
90 $res .= $`; # get everything before the match
93 if ($4 && $4 eq "c") {
98 elsif ($4 && $4 eq "o") {
103 elsif ($4 && $4 eq "m") {
108 elsif ($2 && $2 eq "s") {
110 if ($cio && $3 == 2) {
111 # check_in_out was set: if (s1 != d1) we
112 # need to exchange s2 by s1
113 $res2 .= "%1s"; # get name for first register
116 $res2 .= "%".$3."s"; # substitute %sx with %xs
118 $res .= "%".$3."s"; # substitute %sx with %xs
120 elsif ($2 && $2 eq "d") {
122 $res .= "%".$3."d"; # substitute %sx with %xs
123 $res2 .= "%".$3."d"; # substitute %sx with %xs
125 elsif ($2 && $2 eq "a") {
126 push(@params, "get_irn_n(n, ".($3 - 1).")");
131 $template = $'; # scan everything after the match
133 $res .= $template; # get the remaining string
134 $res2 .= $template; # get the remaining string
137 $parm = ", ".join(", ", @params) if (@params);
140 push(@obst_func, $indent."if (get_irn_arity(n) > 1 && get_$arch\_reg_nr(n, 1, 1) == get_$arch\_reg_nr(n, 0, 0)) {\n");
141 push(@obst_func, $indent.' lc_efprintf(ia32_get_arg_env(), F, "\t'.$res2.'\n"'.$parm.');'."\n");
142 push(@obst_func, $indent."}\n");
143 push(@obst_func, $indent."else {\n");
144 push(@obst_func, $indent.' lc_efprintf(ia32_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
145 push(@obst_func, $indent."}\n");
148 push(@obst_func, $indent.'lc_efprintf(ia32_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
152 push(@obst_func, $template,"\n");
155 push(@obst_func, "}\n\n");
158 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
160 my $creation_time = localtime(time());
165 #ifndef _GEN_$tmp\_EMITTER_H_
166 #define _GEN_$tmp\_EMITTER_H_
169 * Function prototypes for the emitter functions.
170 * DO NOT EDIT THIS FILE, your changes will be lost.
171 * Edit $specfile instead.
172 * created by: $0 $specfile $target_dir
173 * date: $creation_time
177 #include "$arch\_emitter.h"
181 print OUT @obst_header;
183 print OUT "#endif /* _GEN_$tmp\_EMITTER_H_ */\n";
187 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
189 $creation_time = localtime(time());
193 * Generated functions to emit code for assembler ir nodes.
194 * DO NOT EDIT THIS FILE, your changes will be lost.
195 * Edit $specfile instead.
196 * created by: $0 $specfile $target_dir
197 * date: $creation_time
203 #include "gen_$arch\_emitter.h"
204 #include "$arch\_new_nodes.h"
208 print OUT @obst_func;