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 $line = " BE_EMIT($op);\n";
47 push(@obst_register, $line);
49 my @emit = split(/\n/, $n{"emit"});
51 foreach my $template (@emit) {
52 # substitute only lines, starting with a '.'
53 if ($template =~ /^(\d*)\.\s*/) {
56 my $indent = " "; # default indent is 2 spaces
58 $indent = " " x $1 if ($1 && $1 > 0);
59 # remove indent, dot and trailing spaces
60 $template =~ s/^\d*\.\s*//;
61 # substitute all format parameter
62 while ($template =~ /\%([ASD])(\d)|\%([COM])|\%(\w+)/) {
63 $res .= $`; # get everything before the match
65 if ($1 && $1 eq "S") {
67 $res .= "%".$2."S"; # substitute %Sx with %xS
69 elsif ($1 && $1 eq "D") {
71 $res .= "%".$2."D"; # substitute %Dx with %xD
73 elsif ($1 && $1 eq "A") {
74 push(@params, "get_irn_n(n, ".($2 - 1).")");
81 elsif ($4) { # backend provided function to call, has to return a string
82 push(@params, $4."(n)");
86 $template = $'; # scan everything after the match
88 $res .= $template; # get the remaining string
91 $parm = ", ".join(", ", @params) if (@params);
93 push(@obst_func, $indent.'lc_efprintf('.$arch.'_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
96 push(@obst_func, $template,"\n");
99 push(@obst_func, "}\n\n");
102 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
104 my $creation_time = localtime(time());
109 #ifndef _GEN_$tmp\_EMITTER_H_
110 #define _GEN_$tmp\_EMITTER_H_
113 * Function prototypes for the emitter functions.
114 * DO NOT EDIT THIS FILE, your changes will be lost.
115 * Edit $specfile instead.
116 * created by: $0 $specfile $target_dir
117 * date: $creation_time
121 #include "$arch\_emitter.h"
123 void $arch\_register_spec_emitters(void);
125 #endif /* _GEN_$tmp\_EMITTER_H_ */
131 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
133 $creation_time = localtime(time());
137 * Generated functions to emit code for assembler ir nodes.
138 * DO NOT EDIT THIS FILE, your changes will be lost.
139 * Edit $specfile instead.
140 * created by: $0 $specfile $target_dir
141 * date: $creation_time
151 #include "irprog_t.h"
153 #include "gen_$arch\_emitter.h"
154 #include "$arch\_new_nodes.h"
158 print OUT @obst_func;
162 * Enters the emitter functions for handled nodes into the generic
163 * pointer of an opcode.
165 void $arch\_register_spec_emitters(void) {
168 #define BE_EMIT(a) op_$arch\_##a->ops.generic = (op_func)emit_$arch\_##a
170 /* first clear all generic operations */
171 for (i = get_irp_n_opcodes() - 1; i >= 0; --i) {
172 ir_op *op = get_irp_opcode(i);
173 op->ops.generic = (op_func)NULL;
176 /* generated emitter functions */
179 print OUT @obst_register;