libcore: Check, that a pointer to a char array is passed to LC_OPT_ENT_STR().
[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 # buffers for output
49 my $obst_func     = ""; # buffer for the emit functions
50 my $obst_register = ""; # buffer for emitter register code
51
52
53 foreach my $op (keys(%nodes)) {
54         my %n = %{ $nodes{"$op"} };
55
56         # skip this node description if no emit information is available
57         next if (!defined($n{"emit"}));
58
59         if ($n{"emit"} eq "") {
60                 $obst_register .= "\tbe_set_emitter(op_${arch}_${op}, be_emit_nothing);\n";
61                 next;
62         }
63
64         $obst_register .= "\tbe_set_emitter(op_${arch}_${op}, emit_${arch}_${op});\n";
65
66         $obst_func .= "static void emit_${arch}_${op}(ir_node const *const node)\n";
67         $obst_func .= "{\n";
68
69         my @emit = split(/\n/, $n{"emit"});
70
71         foreach my $template (@emit) {
72                 if ($template ne '') {
73                         $obst_func .= "\t${arch}_emitf(node, \"$template\");\n";
74                 }
75         }
76
77         $obst_func .= "}\n\n";
78 }
79
80 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
81
82 my $creation_time = localtime(time());
83
84 my $tmp = uc($arch);
85
86 print OUT<<EOF;
87 /**
88  * \@file
89  * \@brief Function prototypes for the emitter functions.
90  * \@note  DO NOT EDIT THIS FILE, your changes will be lost.
91  *        Edit $specfile instead.
92  *        created by: $0 $specfile $target_dir
93  * \@date  $creation_time
94  */
95 #ifndef FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
96 #define FIRM_BE_${tmp}_GEN_${tmp}_EMITTER_H
97
98 #include "irnode.h"
99 #include "${arch}_emitter.h"
100
101 void ${arch}_register_spec_emitters(void);
102
103 #endif
104
105 EOF
106
107 close(OUT);
108
109 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
110
111 $creation_time = localtime(time());
112
113 print OUT<<EOF;
114 /**
115  * \@file
116  * \@brief     Generated functions to emit code for assembler ir nodes.
117  * \@note      DO NOT EDIT THIS FILE, your changes will be lost.
118  *            Edit $specfile instead.
119  *            created by: $0 $specfile $target_dir
120  * \@date      $creation_time
121  */
122 #include "config.h"
123
124 #include <stdio.h>
125 #include <assert.h>
126
127 #include "irnode.h"
128 #include "irop_t.h"
129 #include "irprog_t.h"
130 #include "beemitter.h"
131
132 #include "gen_${arch}_emitter.h"
133 #include "${arch}_new_nodes.h"
134 #include "${arch}_emitter.h"
135
136 $obst_func
137
138 /**
139  * Enters the emitter functions for handled nodes into the generic
140  * pointer of an opcode.
141  */
142 void $arch\_register_spec_emitters(void)
143 {
144 $obst_register
145 }
146
147 EOF
148
149 close(OUT);