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"} };
67 # determine arity from in requirements
69 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
70 $arity = scalar(@{ $n{"reg_req"}{"in"} });
77 if (exists($n{"outs"})) {
79 @outs = @{ $n{"outs"} };
80 push(@obst_proj, "\nenum pn_$op {\n");
81 for (my $idx = 0; $idx <= $#outs; $idx++) {
82 push(@obst_proj, " pn_$op\_".$outs[$idx]." = $idx,\n");
84 push(@obst_proj, "};\n");
88 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
89 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
90 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
92 push(@obst_is_archirn, "is_$op(node)");
94 push(@obst_header, "extern ir_op *op_$op;\n");
95 push(@obst_header, "ir_op *get_op_$op(void);\n");
96 push(@obst_header, "int is_$op(const ir_node *n);\n");
99 # create compare attribute function if needed
100 if (exists($n{"cmp_attr"})) {
101 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
102 push(@obst_cmp_attr, " $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
103 push(@obst_cmp_attr, " $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
104 push(@obst_cmp_attr, $n{"cmp_attr"});
105 push(@obst_cmp_attr, "}\n\n");
110 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
111 # we explicitly skip the constructor if the specification entry says NONE
114 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
115 $n{"comment"} =~ s/^"|"$//g; # remove "
116 $n{"comment"} = "/* ".$n{"comment"}." */\n";
117 push(@obst_constructor, $n{"comment"});
119 # create constructor head
120 my $complete_args = "";
124 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
125 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
126 if ($arity !~ /^\d+$/) {
127 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
130 for (my $i = 1; $i <= $arity; $i++) {
131 $complete_args .= ", ir_node *op".$i;
132 $arg_names .= ", op".$i;
135 $complete_args .= ", ir_mode *mode";
136 $arg_names .= ", mode";
139 else { # user defined args
140 for my $href (@{ $n{"args"} }) {
141 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
142 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
143 $arg_names .= ", ".$href->{"name"};
147 # we have additional attribute arguements
148 if (exists($n{"attr"})) {
149 $complete_args .= ", ".$n{"attr"};
152 # $complete_args = substr($complete_args, 2);
153 $temp .= "$complete_args)";
154 push(@obst_constructor, $temp." {\n");
155 push(@obst_header, $temp.";\n");
157 # emit constructor code
158 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
159 if ($arity !~ /^\d+$/) {
160 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
164 $temp = " ir_node *res;\n";
165 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
166 $temp .= " int flags = 0;\n";
167 $temp .= " $arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
169 undef my $in_req_var;
170 undef my $out_req_var;
172 # set up static variables for requirements and registers
173 if (exists($n{"reg_req"})) {
174 my %req = %{ $n{"reg_req"} };
178 @in = @{ $req{"in"} } if (exists($req{"in"}));
180 @out = @{ $req{"out"} } if exists(($req{"out"}));
183 $in_req_var = "_in_req_$op";
184 $temp .= " static const $arch\_register_req_t *".$in_req_var."[] =\n {\n";
185 for ($idx = 0; $idx <= $#in; $idx++) {
186 $temp .= " ".$op."_reg_req_in_".$idx.",\n";
192 $out_req_var = "_out_req_$op";
194 $temp .= " static const $arch\_register_req_t *".$out_req_var."[] =\n {\n";
195 for ($idx = 0; $idx <= $#out; $idx++) {
196 $temp .= " ".$op."_reg_req_out_".$idx.",\n";
203 $temp .= " if (!op_$op) {\n";
204 $temp .= " assert(0);\n";
205 $temp .= " return NULL;\n";
207 for (my $i = 1; $i <= $arity; $i++) {
208 $temp .= " in[".($i - 1)."] = op".$i.";\n";
212 if (exists($n{"irn_flags"})) {
213 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
215 $temp .= " flags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
217 elsif ($flag eq "N") {
218 $temp .= " flags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
220 elsif ($flag eq "I") {
221 $temp .= " flags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
228 # allocate memory and set pointer to register requirements
229 if (exists($n{"reg_req"})) {
230 my %req = %{ $n{"reg_req"} };
233 @in = @{ $req{"in"} } if (exists($req{"in"}));
235 @out = @{ $req{"out"} } if exists(($req{"out"}));
238 $in_param = $in_req_var;
245 $out_param = $out_req_var.", ".($#out + 1);
248 $out_param = "NULL, 0";
251 $temp .= "\n /* create node */\n";
256 $temp .= " res = new_ir_node(db, irg, block, op_$op, $mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
258 $temp .= "\n /* init node attributes */\n";
259 $temp .= " init_$arch\_attributes(res, flags, $in_param, $out_param);\n";
261 if (exists($n{"init_attr"})) {
262 $temp .= " attr = get_$arch\_attr(res);\n";
263 $temp .= $n{"init_attr"}."\n";
266 $temp .= "\n /* optimize node */\n";
267 $temp .= " res = optimize_node(res);\n";
268 $temp .= " irn_vrfy_irg(res, irg);\n\n";
270 $temp .= "\n return res;\n";
272 push(@obst_constructor, $temp);
274 else { # user defined constructor
275 push(@obst_constructor, $n{"rd_constructor"});
278 # close constructor function
279 push(@obst_constructor, "}\n\n");
281 } # constructor creation
283 # set default values for state and flags if not given
284 $n{"state"} = "floats" if (! exists($n{"state"}));
285 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
287 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
288 push(@obst_new_irop, " ops.dump_node = dump_node_$arch;\n");
290 if ($cmp_attr_func) {
291 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
295 $temp = " op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
296 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t), &ops);\n";
297 push(@obst_new_irop, $temp);
298 push(@obst_enum_op, " iro_$op,\n");
300 push(@obst_enum_op, " iro_$arch\_last_generated,\n");
301 push(@obst_enum_op, " iro_$arch\_last = iro_$arch\_last_generated");
302 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
303 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
307 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
309 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
310 print OUT @obst_cmp_attr;
312 print OUT @obst_opvar;
314 print OUT @obst_get_opvar;
317 print OUT<<ENDOFISIRN;
319 static int $arch\_opcode_start = -1;
320 static int $arch\_opcode_end = -1;
322 /** Return the opcode number of the first $arch opcode. */
323 int get_$arch\_opcode_first(void) {
324 return $arch\_opcode_start;
327 /** Return the opcode number of the last $arch opcode + 1. */
328 int get_$arch\_opcode_last(void) {
329 return $arch\_opcode_end;
332 /** Return non-zero if the given node is a $arch machine node. */
333 int is_$arch\_irn(const ir_node *node) {
334 unsigned opc = (unsigned)get_irn_opcode(node);
336 assert($arch\_opcode_start > 0 && "missing opcode init");
337 assert($arch\_opcode_end > 0 && "missing opcode init");
339 if (opc - (unsigned)$arch\_opcode_start < (unsigned)($arch\_opcode_end - $arch\_opcode_start))
345 int get_$arch\_irn_opcode(const ir_node *node) {
346 assert(is_$arch\_irn(node));
347 return get_irn_opcode(node) - $arch\_opcode_start;
352 print OUT @obst_constructor;
354 print OUT<<ENDOFMAIN;
356 * Creates the $arch specific Firm machine operations
357 * needed for the assembler irgs.
359 void $arch\_create_opcodes(void) {
360 #define N irop_flag_none
361 #define L irop_flag_labeled
362 #define C irop_flag_commutative
363 #define X irop_flag_cfopcode
364 #define I irop_flag_ip_cfopcode
365 #define F irop_flag_fragile
366 #define Y irop_flag_forking
367 #define H irop_flag_highlevel
368 #define c irop_flag_constlike
369 #define K irop_flag_keep
370 #define M irop_flag_machine
371 #define O irop_flag_machine_op
372 #define R (irop_flag_user << 0)
375 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
377 $arch\_opcode_start = cur_opcode;
380 print OUT @obst_new_irop;
382 print OUT " $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
383 print OUT " $arch\_opcode_end = cur_opcode + iro_$arch\_last";
384 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
390 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
392 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
393 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
394 print OUT @obst_enum_op;
395 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
396 print OUT "int get_$arch\_opcode_first(void);\n";
397 print OUT "int get_$arch\_opcode_last(void);\n";
398 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
399 print OUT @obst_header;
400 print OUT @obst_proj;
401 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
406 # Translates numeric arity into string constant.
408 sub translate_arity {
411 if ($arity =~ /^\d+$/) {
413 return "oparity_zero";
415 elsif ($arity == 1) {
416 return "oparity_unary";
418 elsif ($arity == 2) {
419 return "oparity_binary";
421 elsif ($arity == 3) {
422 return "oparity_trinary";
429 return "oparity_".$arity;