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 # check in/out register if needed
50 if (exists($n{"check_inout"}) && $n{"check_inout"} == 1) {
51 push(@obst_func, " equalize_dest_src(F, n);\n\n");
54 my @emit = split(/\n/, $n{"emit"});
57 # substitute only lines, starting with a '.'
61 my $indent = " "; # default indent is 2 spaces
63 $indent = " " x $1 if ($1 && $1 > 0);
64 # remove indent, dot and trailing spaces
66 # substitute all format parameter
67 while (/%(([sd])(\d)|([co]))/) {
68 if ($4 && $4 eq "c") {
69 push(@params, "node_const_to_str(n)");
71 elsif ($4 && $4 eq "o") {
72 push(@params, "node_offset_to_str(n)");
75 $regkind = ($2 eq "s" ? "source" : "dest");
76 push(@params, "get_".$regkind."_reg_name(n, $3)");
81 $parm = ", ".join(", ", @params) if (@params);
82 push(@obst_func, $indent.'fprintf(F, "\t'.$_.'\n"'.$parm.');'."\n");
85 push(@obst_func, $_,"\n");
88 push(@obst_func, "}\n\n");
91 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
93 my $creation_time = localtime(time());
98 #ifndef _GEN_$tmp\_EMITTER_H_
99 #define _GEN_$tmp\_EMITTER_H_
102 * Function prototypes for the emitter functions.
103 * DO NOT EDIT THIS FILE, your changes will be lost.
104 * Edit $specfile instead.
105 * created by: $0 $specfile $target_dir
106 * date: $creation_time
113 print OUT @obst_header;
115 print OUT "#endif /* _GEN_$tmp\_EMITTER_H_ */\n";
119 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
121 $creation_time = localtime(time());
125 * Generated functions to emit code for assembler ir nodes.
126 * DO NOT EDIT THIS FILE, your changes will be lost.
127 * Edit $specfile instead.
128 * created by: $0 $specfile $target_dir
129 * date: $creation_time
135 #include "gen_$arch\_emitter.h"
136 #include "$arch\_emitter.h"
137 #include "$arch\_new_nodes.h"
141 print OUT @obst_func;