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];
19 our $additional_opcodes;
27 unless ($return = do $specfile) {
28 warn "couldn't parse $specfile: $@" if $@;
29 warn "couldn't do $specfile: $!" unless defined $return;
30 warn "couldn't run $specfile" unless $return;
34 my $target_c = $target_dir."/gen_".$arch."_new_nodes.c.inl";
35 my $target_h = $target_dir."/gen_".$arch."_new_nodes.h";
37 #print Dumper(%nodes);
38 #print Dumper(%operands);
40 # create c code file from specs
42 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
43 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
44 my @obst_constructor; # stack for node constructor functions
45 my @obst_new_irop; # stack for the new_ir_op calls
46 my @obst_enum_op; # stack for creating the <arch>_opcode enum
47 my @obst_header; # stack for function prototypes
48 my @obst_is_archirn; # stack for the is_$arch_irn() function
49 my @obst_cmp_attr; # stack for the compare attribute functions
50 my @obst_proj; # stack for the pn_ numbers
55 my $n_opcodes = 0; # number of opcodes
57 # for registering additional opcodes
58 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
60 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
62 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
63 foreach my $op (keys(%nodes)) {
64 my %n = %{ $nodes{"$op"} };
68 # determine arity from in requirements
70 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
71 $arity = scalar(@{ $n{"reg_req"}{"in"} });
78 if (exists($n{"outs"})) {
80 @outs = @{ $n{"outs"} };
81 push(@obst_proj, "\nenum pn_$op {\n");
82 for (my $idx = 0; $idx <= $#outs; $idx++) {
83 push(@obst_proj, " pn_$op\_".$outs[$idx]." = $idx,\n");
85 push(@obst_proj, "};\n");
89 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
90 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
91 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
93 push(@obst_is_archirn, "is_$op(node)");
95 push(@obst_header, "extern ir_op *op_$op;\n");
96 push(@obst_header, "ir_op *get_op_$op(void);\n");
97 push(@obst_header, "int is_$op(const ir_node *n);\n");
100 # create compare attribute function if needed
101 if (exists($n{"cmp_attr"})) {
102 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
103 push(@obst_cmp_attr, " $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
104 push(@obst_cmp_attr, " $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
105 push(@obst_cmp_attr, $n{"cmp_attr"});
106 push(@obst_cmp_attr, "}\n\n");
111 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
112 # we explicitly skip the constructor if the specification entry says NONE
115 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
116 $n{"comment"} =~ s/^"|"$//g; # remove "
117 $n{"comment"} = "/* ".$n{"comment"}." */\n";
118 push(@obst_constructor, $n{"comment"});
120 # create constructor head
121 my $complete_args = "";
125 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
126 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
127 if ($arity !~ /^\d+$/) {
128 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
131 for (my $i = 1; $i <= $arity; $i++) {
132 $complete_args .= ", ir_node *op".$i;
133 $arg_names .= ", op".$i;
136 $complete_args .= ", ir_mode *mode";
137 $arg_names .= ", mode";
140 else { # user defined args
141 for my $href (@{ $n{"args"} }) {
142 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
143 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
144 $arg_names .= ", ".$href->{"name"};
148 # we have additional attribute arguements
149 if (exists($n{"attr"})) {
150 $complete_args .= ", ".$n{"attr"};
153 # $complete_args = substr($complete_args, 2);
154 $temp .= "$complete_args)";
155 push(@obst_constructor, $temp." {\n");
156 push(@obst_header, $temp.";\n");
158 # emit constructor code
159 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
160 if ($arity !~ /^\d+$/) {
161 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
165 $temp = " ir_node *res;\n";
166 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
167 $temp .= " int flags = 0;\n";
168 $temp .= " $arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
170 undef my $in_req_var;
171 undef my $out_req_var;
173 # set up static variables for requirements and registers
174 if (exists($n{"reg_req"})) {
175 my %req = %{ $n{"reg_req"} };
179 @in = @{ $req{"in"} } if (exists($req{"in"}));
181 @out = @{ $req{"out"} } if exists(($req{"out"}));
184 $in_req_var = "_in_req_$op";
185 $temp .= " static const $arch\_register_req_t *".$in_req_var."[] =\n {\n";
186 for ($idx = 0; $idx <= $#in; $idx++) {
187 $temp .= " ".$op."_reg_req_in_".$idx.",\n";
193 $out_req_var = "_out_req_$op";
195 $temp .= " static const $arch\_register_req_t *".$out_req_var."[] =\n {\n";
196 for ($idx = 0; $idx <= $#out; $idx++) {
197 $temp .= " ".$op."_reg_req_out_".$idx.",\n";
204 $temp .= " if (!op_$op) {\n";
205 $temp .= " assert(0);\n";
206 $temp .= " return NULL;\n";
208 for (my $i = 1; $i <= $arity; $i++) {
209 $temp .= " in[".($i - 1)."] = op".$i.";\n";
213 if (exists($n{"irn_flags"})) {
214 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
216 $temp .= " flags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
218 elsif ($flag eq "N") {
219 $temp .= " flags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
221 elsif ($flag eq "I") {
222 $temp .= " flags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
229 # allocate memory and set pointer to register requirements
230 if (exists($n{"reg_req"})) {
231 my %req = %{ $n{"reg_req"} };
234 @in = @{ $req{"in"} } if (exists($req{"in"}));
236 @out = @{ $req{"out"} } if exists(($req{"out"}));
239 $in_param = $in_req_var;
246 $out_param = $out_req_var.", ".($#out + 1);
250 $out_param = "NULL, 0";
253 $temp .= "\n /* create node */\n";
258 $temp .= " res = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
260 $temp .= "\n /* init node attributes */\n";
261 $temp .= " init_$arch\_attributes(res, flags, $in_param, $out_param);\n";
263 if (exists($n{"init_attr"})) {
264 $temp .= " attr = get_$arch\_attr(res);\n";
265 $temp .= $n{"init_attr"}."\n";
268 $temp .= "\n /* optimize node */\n";
269 $temp .= " res = optimize_node(res);\n";
270 $temp .= " irn_vrfy_irg(res, irg);\n\n";
272 $temp .= "\n return res;\n";
274 push(@obst_constructor, $temp);
276 else { # user defined constructor
277 push(@obst_constructor, $n{"rd_constructor"});
280 # close constructor function
281 push(@obst_constructor, "}\n\n");
283 } # constructor creation
285 # set default values for state and flags if not given
286 $n{"state"} = "floats" if (! exists($n{"state"}));
287 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
290 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
291 push(@obst_new_irop, " ops.dump_node = $arch\_dump_node;\n");
293 if ($cmp_attr_func) {
294 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
298 $temp = " op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
299 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t) + $n_res * sizeof(arch_register_t *), &ops);\n";
300 push(@obst_new_irop, $temp);
301 push(@obst_enum_op, " iro_$op,\n");
303 push(@obst_enum_op, " iro_$arch\_last_generated,\n");
304 push(@obst_enum_op, " iro_$arch\_last = iro_$arch\_last_generated");
305 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
306 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
310 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
312 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
313 print OUT @obst_cmp_attr;
315 print OUT @obst_opvar;
317 print OUT @obst_get_opvar;
320 print OUT<<ENDOFISIRN;
322 static int $arch\_opcode_start = -1;
323 static int $arch\_opcode_end = -1;
325 /** Return the opcode number of the first $arch opcode. */
326 int get_$arch\_opcode_first(void) {
327 return $arch\_opcode_start;
330 /** Return the opcode number of the last $arch opcode + 1. */
331 int get_$arch\_opcode_last(void) {
332 return $arch\_opcode_end;
335 /** Return non-zero if the given node is a $arch machine node. */
336 int is_$arch\_irn(const ir_node *node) {
337 unsigned opc = (unsigned)get_irn_opcode(node);
339 assert($arch\_opcode_start > 0 && "missing opcode init");
340 assert($arch\_opcode_end > 0 && "missing opcode init");
342 if (opc - (unsigned)$arch\_opcode_start < (unsigned)($arch\_opcode_end - $arch\_opcode_start))
348 int get_$arch\_irn_opcode(const ir_node *node) {
349 assert(is_$arch\_irn(node));
350 return get_irn_opcode(node) - $arch\_opcode_start;
355 print OUT @obst_constructor;
357 print OUT<<ENDOFMAIN;
359 * Creates the $arch specific Firm machine operations
360 * needed for the assembler irgs.
362 void $arch\_create_opcodes(void) {
363 #define N irop_flag_none
364 #define L irop_flag_labeled
365 #define C irop_flag_commutative
366 #define X irop_flag_cfopcode
367 #define I irop_flag_ip_cfopcode
368 #define F irop_flag_fragile
369 #define Y irop_flag_forking
370 #define H irop_flag_highlevel
371 #define c irop_flag_constlike
372 #define K irop_flag_keep
373 #define M irop_flag_machine
374 #define O irop_flag_machine_op
375 #define R (irop_flag_user << 0)
378 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
380 $arch\_opcode_start = cur_opcode;
383 print OUT @obst_new_irop;
385 print OUT " $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
386 print OUT " $arch\_opcode_end = cur_opcode + iro_$arch\_last";
387 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
393 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
395 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
396 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
397 print OUT @obst_enum_op;
398 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
399 print OUT "int get_$arch\_opcode_first(void);\n";
400 print OUT "int get_$arch\_opcode_last(void);\n";
401 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
402 print OUT @obst_header;
403 print OUT @obst_proj;
404 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
409 # Translates numeric arity into string constant.
411 sub translate_arity {
414 if ($arity =~ /^\d+$/) {
416 return "oparity_zero";
418 elsif ($arity == 1) {
419 return "oparity_unary";
421 elsif ($arity == 2) {
422 return "oparity_binary";
424 elsif ($arity == 3) {
425 return "oparity_trinary";
432 return "oparity_".$arity;