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];
15 my $target_c = $target_dir."/emitter_gen.c";
16 my $target_h = $target_dir."/emitter_gen.h";
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;
34 my @obst_func; # stack for the emit functions
35 my @obst_header; # stack for the function prototypes
39 foreach my $op (keys(%nodes)) {
40 my %n = %{ $nodes{"$op"} };
42 # skip this node description if no emit information is available
43 next if (!$n{"emit"} || length($n{"emit"}) < 1);
45 $line = "void emit_".$arch."_".$op."(FILE *F, ir_node *n)";
46 push(@obst_header, $line.";\n");
47 push(@obst_func, $line." {\n");
49 my @emit = split(/\n/, $n{"emit"});
52 # substitute only lines, starting with a '.'
56 my $indent = " "; # default indent is 2 spaces
58 $indent = " " x $1 if ($1 && $1 > 0);
59 # remove indent, dot and trailing spaces
61 # substitute all format parameter
62 while (/%(([sd])(\d)|([co]))/) {
63 if ($4 && $4 eq "c") {
64 push(@params, "node_const_to_str(n)");
66 elsif ($4 && $4 eq "o") {
67 push(@params, "node_offset_to_str(n)");
70 $regkind = ($2 eq "s" ? "source" : "dest");
71 push(@params, "get_".$regkind."_reg(n, $3)");
76 $parm = ", ".join(", ", @params) if (@params);
77 push(@obst_func, $indent.'fprintf(F, "\t'.$_.'\n"'.$parm.');'."\n");
80 push(@obst_func, $_,"\n");
83 push(@obst_func, "}\n\n");
86 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
88 my $creation_time = localtime(time());
91 #ifndef _EMITTER_GEN_H_
92 #define _EMITTER_GEN_H_
95 * Function prototypes for the emitter functions.
96 * DO NOT EDIT THIS FILE, your changes will be lost.
97 * Edit $specfile instead.
98 * created by: $0 $specfile $target_dir
99 * date: $creation_time
106 print OUT @obst_header;
108 print OUT "#endif /* _EMITTER_GEN_H_ */\n";
112 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
114 $creation_time = localtime(time());
118 * Generated functions to emit code for assembler ir nodes.
119 * DO NOT EDIT THIS FILE, your changes will be lost.
120 * Edit $specfile instead.
121 * created by: $0 $specfile $target_dir
122 * date: $creation_time
128 #include "emitter_gen.h"
133 print OUT @obst_func;