bescripts: Remove support for emit templates.
[libfirm] / ir / be / scripts / generate_emitter.pl
1 #!/usr/bin/perl -w
2
3 #
4 # Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
5 #
6 # This file is part of libFirm.
7 #
8 # This file may be distributed and/or modified under the terms of the
9 # GNU General Public License version 2 as published by the Free Software
10 # Foundation and appearing in the file LICENSE.GPL included in the
11 # packaging of this file.
12 #
13 # Licensees holding valid libFirm Professional Edition licenses may use
14 # this file in accordance with the libFirm Commercial License.
15 # Agreement provided with the Software.
16 #
17 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 # WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE.
20 #
21
22 # This script generates C code which emits assembler code for the
23 # assembler ir nodes. It takes a "emit" key from the node specification
24 # and substitutes lines starting with . with a corresponding fprintf().
25
26 use strict;
27 use Data::Dumper;
28
29 our $specfile   = $ARGV[0];
30 our $target_dir = $ARGV[1];
31
32 our $arch;
33 our %nodes;
34
35 my $return;
36
37 no strict "subs";
38 unless ($return = do $specfile) {
39         die "Fatal error: couldn't parse $specfile: $@" if $@;
40         die "Fatal error: couldn't do $specfile: $!"    unless defined $return;
41         die "Fatal error: couldn't run $specfile"       unless $return;
42 }
43 use strict "subs";
44
45 my $target_c = $target_dir."/gen_".$arch."_emitter.c";
46 my $target_h = $target_dir."/gen_".$arch."_emitter.h";
47
48 # stacks for output
49 my @obst_func;   # stack for the emit functions
50 my @obst_register;  # stack for emitter register code
51 my $line;
52
53
54 foreach my $op (keys(%nodes)) {
55         my %n = %{ $nodes{"$op"} };
56
57         # skip this node description if no emit information is available
58         next if (!defined($n{"emit"}));
59
60         $line = "static void emit_${arch}_${op}(const ir_node *node)";
61
62         push(@obst_register, "\tbe_set_emitter(op_${arch}_${op}, emit_${arch}_${op});\n");
63
64         if($n{"emit"} eq "") {
65                 push(@obst_func, $line."\n");
66                 push(@obst_func, "{\n");
67                 push(@obst_func, "\t(void) node;\n");
68                 push(@obst_func, "}\n\n");
69                 next;
70         }
71
72         push(@obst_func, $line."\n");
73         push(@obst_func, "{\n");
74
75         my @emit = split(/\n/, $n{"emit"});
76
77         foreach my $template (@emit) {
78                 if ($template ne '') {
79                         push(@obst_func, "\t${arch}_emitf(node, \"$template\");\n");
80                 }
81         }
82
83         push(@obst_func, "}\n\n");
84 }
85
86 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
87
88 my $creation_time = localtime(time());
89
90 my $tmp = uc($arch);
91
92 print OUT<<EOF;
93 /**
94  * \@file
95  * \@brief Function prototypes for the emitter functions.
96  * \@note  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
100  */
101 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
102 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
103
104 #include "irnode.h"
105 #include "${arch}_emitter.h"
106
107 void ${arch}_register_spec_emitters(void);
108
109 #endif
110
111 EOF
112
113 close(OUT);
114
115 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
116
117 $creation_time = localtime(time());
118
119 print OUT<<EOF;
120 /**
121  * \@file
122  * \@brief     Generated functions to emit code for assembler ir nodes.
123  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
124  *            Edit $specfile instead.
125  *            created by: $0 $specfile $target_dir
126  * \@date      $creation_time
127  */
128 #include "config.h"
129
130 #include <stdio.h>
131 #include <assert.h>
132
133 #include "irnode.h"
134 #include "irop_t.h"
135 #include "irprog_t.h"
136 #include "beemitter.h"
137
138 #include "gen_${arch}_emitter.h"
139 #include "${arch}_new_nodes.h"
140 #include "${arch}_emitter.h"
141
142 EOF
143
144 print OUT @obst_func;
145
146 print OUT<<EOF;
147
148 /**
149  * Enters the emitter functions for handled nodes into the generic
150  * pointer of an opcode.
151  */
152 void $arch\_register_spec_emitters(void)
153 {
154 EOF
155
156 print OUT @obst_register;
157
158 print OUT<<EOF;
159 }
160
161 EOF
162
163 close(OUT);