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 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 = "void emit_".$arch."_".$op."(ir_node *n, emit_env_t *env)";
45 push(@obst_header, $line.";\n");
46 push(@obst_func, $line." {\n FILE *F = env->out;\n");
48 my @emit = split(/\n/, $n{"emit"});
50 foreach my $template (@emit) {
51 # substitute only lines, starting with a '.'
52 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*//;
60 # substitute all format parameter
61 while ($template =~ /\%([ASD])(\d)|\%([COM])|\%(\w+)/) {
62 $res .= $`; # get everything before the match
64 if ($1 && $1 eq "S") {
66 $res .= "%".$2."S"; # substitute %Sx with %xS
68 elsif ($1 && $1 eq "D") {
70 $res .= "%".$2."D"; # substitute %Dx with %xD
72 elsif ($1 && $1 eq "A") {
73 push(@params, "get_irn_n(n, ".($2 - 1).")");
80 elsif ($4) { # backend provided function to call, has to return a string
81 push(@params, $4."(n)");
85 $template = $'; # scan everything after the match
87 $res .= $template; # get the remaining string
90 $parm = ", ".join(", ", @params) if (@params);
92 push(@obst_func, $indent.'lc_efprintf('.$arch.'_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
95 push(@obst_func, $template,"\n");
98 push(@obst_func, "}\n\n");
101 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
103 my $creation_time = localtime(time());
108 #ifndef _GEN_$tmp\_EMITTER_H_
109 #define _GEN_$tmp\_EMITTER_H_
112 * Function prototypes for the emitter functions.
113 * DO NOT EDIT THIS FILE, your changes will be lost.
114 * Edit $specfile instead.
115 * created by: $0 $specfile $target_dir
116 * date: $creation_time
120 #include "$arch\_emitter.h"
124 print OUT @obst_header;
126 print OUT "#endif /* _GEN_$tmp\_EMITTER_H_ */\n";
130 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
132 $creation_time = localtime(time());
136 * Generated functions to emit code for assembler ir nodes.
137 * DO NOT EDIT THIS FILE, your changes will be lost.
138 * Edit $specfile instead.
139 * created by: $0 $specfile $target_dir
140 * date: $creation_time
149 #include "gen_$arch\_emitter.h"
150 #include "$arch\_new_nodes.h"
154 print OUT @obst_func;