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];
26 unless ($return = do $specfile) {
27 warn "couldn't parse $specfile: $@" if $@;
28 warn "couldn't do $specfile: $!" unless defined $return;
29 warn "couldn't run $specfile" unless $return;
33 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
34 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
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
45 my @obst_is_archirn; # stack for the is_$arch_irn() function
46 my @obst_cmp_attr; # stack for the compare attribute functions
51 my $n_opcodes = 2; # we have two additional border opcodes (lowest/highest)
53 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
55 foreach my $op (keys(%nodes)) {
56 my %n = %{ $nodes{"$op"} };
58 # determine arity from in requirements
60 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
61 $arity = scalar(@{ $n{"reg_req"}{"in"} });
68 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
69 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
70 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
72 push(@obst_is_archirn, "is_$op(node)");
74 push(@obst_header, "extern ir_op *op_$op;\n");
75 push(@obst_header, "ir_op *get_op_$op(void);\n");
76 push(@obst_header, "int is_$op(const ir_node *n);\n");
79 # create compare attribute function if needed
80 if (exists($n{"cmp_attr"})) {
81 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
82 push(@obst_cmp_attr, " $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
83 push(@obst_cmp_attr, " $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
84 push(@obst_cmp_attr, $n{"cmp_attr"});
85 push(@obst_cmp_attr, "}\n\n");
90 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
91 # we explicitly skip the constructor if the specification entry says NONE
94 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
95 $n{"comment"} =~ s/^"|"$//g; # remove "
96 $n{"comment"} = "/* ".$n{"comment"}." */\n";
97 push(@obst_constructor, $n{"comment"});
99 # create constructor head
100 my $complete_args = "";
104 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
105 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
106 if ($arity !~ /^\d+$/) {
107 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
110 for (my $i = 1; $i <= $arity; $i++) {
111 $complete_args .= ", ir_node *op".$i;
112 $arg_names .= ", op".$i;
114 $complete_args .= ", ir_mode *mode";
115 $arg_names .= ", mode";
117 else { # user defined args
118 for my $href (@{ $n{"args"} }) {
119 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
120 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
121 $arg_names .= ", ".$href->{"name"};
124 $complete_args = substr($complete_args, 2);
125 $temp .= ", $complete_args)";
126 push(@obst_constructor, $temp." {\n");
127 push(@obst_header, $temp.";\n");
129 # emit constructor code
130 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
131 if ($arity !~ /^\d+$/) {
132 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
136 $temp = " ir_node *res;\n";
137 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
138 $temp .= " int flags = 0;\n";
140 undef my $in_req_var;
141 undef my $out_req_var;
143 # set up static variables for requirements and registers
144 if (exists($n{"reg_req"})) {
145 my %req = %{ $n{"reg_req"} };
149 @in = @{ $req{"in"} } if (exists($req{"in"}));
151 @out = @{ $req{"out"} } if exists(($req{"out"}));
154 $in_req_var = "_in_req_$op";
155 $temp .= " static const $arch\_register_req_t *".$in_req_var."[] =\n {\n";
156 for ($idx = 0; $idx <= $#in; $idx++) {
157 $temp .= " ".$op."_reg_req_in_".$idx.",\n";
163 $out_req_var = "_out_req_$op";
165 $temp .= " static const $arch\_register_req_t *".$out_req_var."[] =\n {\n";
166 for ($idx = 0; $idx <= $#out; $idx++) {
167 $temp .= " ".$op."_reg_req_out_".$idx.",\n";
174 $temp .= " if (!op_$op) {\n";
175 $temp .= " assert(0);\n";
176 $temp .= " return NULL;\n";
178 for (my $i = 1; $i <= $arity; $i++) {
179 $temp .= " in[".($i - 1)."] = op".$i.";\n";
181 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
182 $temp .= " res = optimize_node(res);\n";
183 $temp .= " irn_vrfy_irg(res, irg);\n\n";
186 if (exists($n{"irn_flags"})) {
187 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
189 $temp .= " flags |= arch_irn_flags_rematerializable; /* op can be easily recalulated */\n";
191 elsif ($flag eq "N") {
192 $temp .= " flags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
194 elsif ($flag eq "I") {
195 $temp .= " flags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
202 # allocate memory and set pointer to register requirements
203 if (exists($n{"reg_req"})) {
204 my %req = %{ $n{"reg_req"} };
207 @in = @{ $req{"in"} } if (exists($req{"in"}));
209 @out = @{ $req{"out"} } if exists(($req{"out"}));
212 $in_param = $in_req_var;
219 $out_param = $out_req_var.", ".($#out + 1);
222 $out_param = "NULL, 0";
225 $temp .= "\n /* init node attributes */\n";
226 $temp .= " init_$arch\_attributes(res, flags, $in_param, $out_param);\n";
228 $temp .= "\n return res;\n";
230 push(@obst_constructor, $temp);
232 else { # user defined constructor
233 push(@obst_constructor, $n{"rd_constructor"});
236 # close constructor function
237 push(@obst_constructor, "}\n\n");
239 } # constructor creation
241 # set default values for state and flags if not given
242 $n{"state"} = "floats" if (! exists($n{"state"}));
243 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
245 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
246 push(@obst_new_irop, " ops.dump_node = dump_node_$arch;\n");
248 if ($cmp_attr_func) {
249 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
253 $temp = " op_$op = new_ir_op(cur_opcode++, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
254 $temp .= ", ".translate_arity($arity).", 0, sizeof($arch\_attr_t), &ops);\n";
255 push(@obst_new_irop, $temp);
260 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
262 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
263 print OUT @obst_cmp_attr;
265 print OUT @obst_opvar;
267 print OUT @obst_get_opvar;
270 print OUT<<ENDOFISIRN;
272 static opcode $arch\_opcode_start = -1;
273 static opcode $arch\_opcode_end = -1;
275 int is_$arch\_irn(const ir_node *node) {
276 opcode opc = get_irn_opcode(node);
278 assert($arch\_opcode_start > 0 && "missing opcode init");
279 assert($arch\_opcode_end > 0 && "missing opcode init");
281 if (opc > $arch\_opcode_start && opc < $arch\_opcode_end)
289 print OUT @obst_constructor;
291 print OUT<<ENDOFMAIN;
293 * Creates the $arch specific firm operations
294 * needed for the assembler irgs.
296 void $arch\_create_opcodes(void) {
297 #define N irop_flag_none
298 #define L irop_flag_labeled
299 #define C irop_flag_commutative
300 #define X irop_flag_cfopcode
301 #define I irop_flag_ip_cfopcode
302 #define F irop_flag_fragile
303 #define Y irop_flag_forking
304 #define H irop_flag_highlevel
305 #define c irop_flag_constlike
306 #define K irop_flag_keep
309 int cur_opcode = get_next_ir_opcodes($n_opcodes);
311 $arch\_opcode_start = cur_opcode++;
315 print OUT @obst_new_irop;
316 print OUT "\n $arch\_opcode_end = cur_opcode;\n";
321 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
323 print OUT "int is_$arch\_irn(const ir_node *node);\n";
324 print OUT @obst_header;
329 # Translates numeric arity into string constant.
331 sub translate_arity {
334 if ($arity =~ /^\d+$/) {
336 return "oparity_zero";
338 elsif ($arity == 1) {
339 return "oparity_unary";
341 elsif ($arity == 2) {
342 return "oparity_binary";
344 elsif ($arity == 3) {
345 return "oparity_trinary";
352 return "oparity_".$arity;