eb883de0810e25b642ceb07fa75048f3e84dd842
[libfirm] / ir / be / scripts / generate_emitter.pl
1 #!/usr/bin/perl -w
2
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().
6 # Creation: 2005/11/07
7 # $Id$
8
9 use strict;
10 use Data::Dumper;
11
12 my $specfile   = $ARGV[0];
13 my $target_dir = $ARGV[1];
14
15 our $arch;
16 our %nodes;
17
18 # include spec file
19
20 my $return;
21
22 no strict "subs";
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;
27 }
28 use strict "subs";
29
30 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
31 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
32
33 # stacks for output
34 my @obst_func;   # stack for the emit functions
35 my @obst_header;  # stack for the function prototypes
36 my $line;
37
38 foreach my $op (keys(%nodes)) {
39         my %n = %{ $nodes{"$op"} };
40
41         # skip this node description if no emit information is available
42         next if (!$n{"emit"} || length($n{"emit"}) < 1);
43
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");
47
48         my @emit = split(/\n/, $n{"emit"});
49
50         foreach my $template (@emit) {
51                 # substitute only lines, starting with a '.'
52                 if ($template =~ /^(\d*)\.\s*/) {
53                         my @params;
54                         my $res    = "";
55                         my $indent = "  "; # default indent is 2 spaces
56
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
63
64                                 if ($1 && $1 eq "S") {
65                                         push(@params, "n");
66                                         $res .= "%".$2."S"; # substitute %Sx with %xS
67                                 }
68                                 elsif ($1 && $1 eq "D") {
69                                         push(@params, "n");
70                                         $res .= "%".$2."D"; # substitute %Dx with %xD
71                                 }
72                                 elsif ($1 && $1 eq "A") {
73                                         push(@params, "get_irn_n(n, ".($2 - 1).")");
74                                         $res .= "%+F";
75                                 }
76                                 elsif ($3) {
77                                         push(@params, "n");
78                                         $res .= "%".$3;
79                                 }
80                                 elsif ($4) {  # backend provided function to call, has to return a string
81                                         push(@params, $4."(n)");
82                                         $res .= "\%s";
83                                 }
84
85                                 $template = $'; # scan everything after the match
86                         }
87                         $res  .= $template; # get the remaining string
88
89                         my $parm = "";
90                         $parm = ", ".join(", ", @params) if (@params);
91
92                         push(@obst_func, $indent.'lc_efprintf('.$arch.'_get_arg_env(), F, "\t'.$res.'\n"'.$parm.');'."\n");
93                 }
94                 else {
95                         push(@obst_func, $template,"\n");
96                 }
97         }
98         push(@obst_func, "}\n\n");
99 }
100
101 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
102
103 my $creation_time = localtime(time());
104
105 my $tmp = uc($arch);
106
107 print OUT<<EOF;
108 #ifndef _GEN_$tmp\_EMITTER_H_
109 #define _GEN_$tmp\_EMITTER_H_
110
111 /**
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
117  */
118
119 #include "irnode.h"
120 #include "$arch\_emitter.h"
121
122 EOF
123
124 print OUT @obst_header;
125
126 print OUT "#endif /* _GEN_$tmp\_EMITTER_H_ */\n";
127
128 close(OUT);
129
130 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
131
132 $creation_time = localtime(time());
133
134 print OUT<<EOF;
135 /**
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
141  */
142 #ifdef HAVE_CONFIG_H
143 #include "config.h"
144 #endif
145
146 #include <stdio.h>
147
148 #include "irnode.h"
149 #include "gen_$arch\_emitter.h"
150 #include "$arch\_new_nodes.h"
151
152 EOF
153
154 print OUT @obst_func;
155
156 close(OUT);