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
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_name(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());
93 #ifndef _GEN_$tmp\_EMITTER_H_
94 #define _GEN_$tmp\_EMITTER_H_
97 * Function prototypes for the emitter functions.
98 * DO NOT EDIT THIS FILE, your changes will be lost.
99 * Edit $specfile instead.
100 * created by: $0 $specfile $target_dir
101 * date: $creation_time
108 print OUT @obst_header;
110 print OUT "#endif /* _GEN_$tmp\_EMITTER_H_ */\n";
114 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
116 $creation_time = localtime(time());
120 * Generated functions to emit code for assembler ir nodes.
121 * DO NOT EDIT THIS FILE, your changes will be lost.
122 * Edit $specfile instead.
123 * created by: $0 $specfile $target_dir
124 * date: $creation_time
130 #include "gen_$arch\_emitter.h"
131 #include "$arch\_emitter.h"
132 #include "$arch\_new_nodes.h"
136 print OUT @obst_func;