3 # This script generates the C code which creates the irop's and
4 # their coresponding node constructors for all operations in a given spec
5 # so they can be used as normal firm nodes.
12 my $specfile = $ARGV[0];
13 my $target_dir = $ARGV[1];
18 my $target_c = $target_dir."/new_nodes.c";
19 my $target_h = $target_dir."/new_nodes.h";
29 unless ($return = do $specfile) {
30 warn "couldn't parse $specfile: $@" if $@;
31 warn "couldn't do $specfile: $!" unless defined $return;
32 warn "couldn't run $specfile" unless $return;
36 #print Dumper(%nodes);
38 # create c code file from specs
40 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
41 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
42 my @obst_constructor; # stack for node constructor functions
43 my @obst_new_irop; # stack for the new_ir_op calls
44 my @obst_header; # stack for function prototypes
49 push(@obst_header, "void create_bearch_asm_opcodes(void);");
51 foreach my $op (keys(%nodes)) {
52 my %n = %{ $nodes{"$op"} };
58 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
59 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
61 $n{"comment"} =~ s/^"|"$//g;
62 $n{"comment"} = "/* ".$n{"comment"}." */\n";
63 push(@obst_constructor, $n{"comment"});
65 # create constructor head
66 my $complete_args = "";
68 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
69 if ($n{"args"} eq "DEFAULT") { # default args
70 if ($n{"arity"} !~ /^[0-3]$/) {
71 print "DEFAULT args require arity 0,1,2 or 3! Ignoring op $orig_op!\n";
74 for (my $i = 1; $i <= $n{"arity"}; $i++) {
75 $complete_args .= ", ir_node *op".$i;
76 $arg_names .= ", op".$i;
78 $complete_args .= ", ir_mode *mode";
79 $arg_names .= ", mode";
81 else { # user defined args
82 for my $href (@{ $n{"args"} }) {
83 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
84 $arg_names .= ", ".$href->{"name"};
87 $complete_args = substr($complete_args, 2);
88 $temp .= ", $complete_args)";
89 push(@obst_constructor, $temp." {\n");
90 push(@obst_header, $temp.";\n");
92 # emit constructor code
93 if ($n{"rd_constructor"} eq "DEFAULT") { # default constructor
94 if ($n{"arity"} !~ /^[0-3]$/) {
95 print "DEFAULT rd_constructor requires arity 0,1,2 or 3! Ignoring op $orig_op!\n";
98 $temp = " ir_node *res;\n";
99 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
101 $temp .= " if (!op_$op) {\n";
102 $temp .= " assert(0);\n";
103 $temp .= " return NULL;\n";
105 for (my $i = 1; $i <= $arity; $i++) {
106 $temp .= " in[".($i - 1)."] = op".$i.";\n";
108 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
109 $temp .= " res = optimize_node(res);\n";
110 $temp .= " irn_vrfy_irg(res, irg);\n";
111 $temp .= " return res;\n";
113 push(@obst_constructor, $temp);
115 else { # user defined constructor
116 push(@obst_constructor, $n{"rd_constructor"});
119 # close constructor function
120 push(@obst_constructor, "}\n\n");
122 # create the _r and _d wrapper
123 $temp = "ir_node *new_r_$op(ir_graph *irg, ir_node *block, $complete_args)";
124 push(@obst_header, $temp.";\n");
126 $temp .= " return new_rd_$op(NULL, irg, block".$arg_names.");\n";
128 push(@obst_constructor, $temp);
130 $temp = "ir_node *new_d_$op(dbg_info *db, $complete_args)";
131 push(@obst_header, $temp.";\n");
133 $temp .= " return new_rd_$op(db, current_ir_graph, current_ir_graph->current_block".$arg_names.");\n";
135 push(@obst_constructor, $temp);
137 $temp = "ir_node *new_$op($complete_args)";
138 push(@obst_header, $temp.";\n");
140 $temp .= " return new_d_$op(NULL".$arg_names.");\n";
142 push(@obst_constructor, $temp);
144 # construct the new_ir_op calls
145 my $arity_str = $arity == 0 ? "zero" : ($arity == 1 ? "unary" : ($arity == 2 ? "binary" : ($arity == 3 ? "trinary" : $arity)));
146 $temp = " op_$op = new_ir_op(get_next_ir_opcode(), \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
147 $temp .= ", oparity_".$arity_str.", 0, sizeof(asmop_attr), NULL);\n";
148 push(@obst_new_irop, $temp);
152 my $creation_time = localtime(time());
154 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
156 print OUT<<ENDOFHEADER;
158 * This file implements the creation of the achitecture specific firm opcodes
159 * and the coresponding node constructors for the $arch assembler irg.
160 * DO NOT EDIT THIS FILE, your changes will be lost.
161 * Edit $specfile instead.
162 * created by: $0 $specfile $target_dir
163 * date: $creation_time
166 #include <libfirm/firm.h>
169 #include "irprog_t.h"
170 #include "irgraph_t.h"
171 #include "irnode_t.h"
172 #include "irmode_t.h"
173 #include "ircons_t.h"
175 #include "firm_common_t.h"
176 #include "irvrfy_t.h"
178 #include "iropt_dbg.h"
180 #include "../firm2arch_nodes_attr.h"
182 #include "new_nodes.h"
186 print OUT @obst_opvar;
188 print OUT @obst_get_opvar;
190 print OUT @obst_constructor;
192 print OUT<<ENDOFMAIN;
194 * Creates the architecture specific firm operations
195 * needed for the assembler irgs.
197 void create_bearch_asm_opcodes(void) {
198 #define N irop_flag_none
199 #define L irop_flag_labeled
200 #define C irop_flag_commutative
201 #define X irop_flag_cfopcode
202 #define I irop_flag_ip_cfopcode
203 #define F irop_flag_fragile
204 #define Y irop_flag_forking
205 #define H irop_flag_highlevel
206 #define c irop_flag_constlike
210 print OUT @obst_new_irop;
215 $creation_time = localtime(time());
217 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
223 * Function prototypes for the assembler ir node constructors.
224 * DO NOT EDIT THIS FILE, your changes will be lost.
225 * Edit $specfile instead.
226 * created by: $0 $specfile $target_dir
227 * date: $creation_time
232 print OUT @obst_header;
233 print OUT "\n#endif\n";