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);
39 # create c code file from specs
41 my @obst_opvar; # stack for the "ir_op *op_<arch>_<op-name> = NULL;" statements
42 my @obst_get_opvar; # stack for the get_op_<arch>_<op-name>() functions
43 my @obst_constructor; # stack for node constructor functions
44 my @obst_new_irop; # stack for the new_ir_op calls
45 my @obst_enum_op; # stack for creating the <arch>_opcode enum
46 my @obst_header; # stack for function prototypes
47 my @obst_is_archirn; # stack for the is_$arch_irn() function
48 my @obst_cmp_attr; # stack for the compare attribute functions
53 my $n_opcodes = 0; # number of opcodes
55 # for registering additional opcodes
56 $n_opcodes += $additional_opcodes if (defined($additional_opcodes));
58 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
60 push(@obst_enum_op, "typedef enum _$arch\_opcodes {\n");
61 foreach my $op (keys(%nodes)) {
62 my %n = %{ $nodes{"$op"} };
64 # determine arity from in requirements
66 if (exists($n{"reg_req"}) && exists($n{"reg_req"}{"in"})) {
67 $arity = scalar(@{ $n{"reg_req"}{"in"} });
74 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
75 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
76 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
78 push(@obst_is_archirn, "is_$op(node)");
80 push(@obst_header, "extern ir_op *op_$op;\n");
81 push(@obst_header, "ir_op *get_op_$op(void);\n");
82 push(@obst_header, "int is_$op(const ir_node *n);\n");
85 # create compare attribute function if needed
86 if (exists($n{"cmp_attr"})) {
87 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
88 push(@obst_cmp_attr, " $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
89 push(@obst_cmp_attr, " $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
90 push(@obst_cmp_attr, $n{"cmp_attr"});
91 push(@obst_cmp_attr, "}\n\n");
96 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
97 # we explicitly skip the constructor if the specification entry says NONE
100 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
101 $n{"comment"} =~ s/^"|"$//g; # remove "
102 $n{"comment"} = "/* ".$n{"comment"}." */\n";
103 push(@obst_constructor, $n{"comment"});
105 # create constructor head
106 my $complete_args = "";
110 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
111 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
112 if ($arity !~ /^\d+$/) {
113 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
116 for (my $i = 1; $i <= $arity; $i++) {
117 $complete_args .= ", ir_node *op".$i;
118 $arg_names .= ", op".$i;
120 $complete_args .= ", ir_mode *mode";
121 $arg_names .= ", mode";
123 else { # user defined args
124 for my $href (@{ $n{"args"} }) {
125 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
126 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
127 $arg_names .= ", ".$href->{"name"};
131 # we have additional attribute arguements
132 if (exists($n{"attr"})) {
133 $complete_args .= ", ".$n{"attr"};
136 $complete_args = substr($complete_args, 2);
137 $temp .= ", $complete_args)";
138 push(@obst_constructor, $temp." {\n");
139 push(@obst_header, $temp.";\n");
141 # emit constructor code
142 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
143 if ($arity !~ /^\d+$/) {
144 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
148 $temp = " ir_node *res;\n";
149 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
150 $temp .= " int flags = 0;\n";
151 $temp .= " $arch\_attr_t *attr;\n" if (exists($n{"init_attr"}));
153 undef my $in_req_var;
154 undef my $out_req_var;
156 # set up static variables for requirements and registers
157 if (exists($n{"reg_req"})) {
158 my %req = %{ $n{"reg_req"} };
162 @in = @{ $req{"in"} } if (exists($req{"in"}));
164 @out = @{ $req{"out"} } if exists(($req{"out"}));
167 $in_req_var = "_in_req_$op";
168 $temp .= " static const $arch\_register_req_t *".$in_req_var."[] =\n {\n";
169 for ($idx = 0; $idx <= $#in; $idx++) {
170 $temp .= " ".$op."_reg_req_in_".$idx.",\n";
176 $out_req_var = "_out_req_$op";
178 $temp .= " static const $arch\_register_req_t *".$out_req_var."[] =\n {\n";
179 for ($idx = 0; $idx <= $#out; $idx++) {
180 $temp .= " ".$op."_reg_req_out_".$idx.",\n";
187 $temp .= " if (!op_$op) {\n";
188 $temp .= " assert(0);\n";
189 $temp .= " return NULL;\n";
191 for (my $i = 1; $i <= $arity; $i++) {
192 $temp .= " in[".($i - 1)."] = op".$i.";\n";
196 if (exists($n{"irn_flags"})) {
197 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
199 $temp .= " flags |= arch_irn_flags_rematerializable; /* op can be easily recalculated */\n";
201 elsif ($flag eq "N") {
202 $temp .= " flags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
204 elsif ($flag eq "I") {
205 $temp .= " flags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
212 # allocate memory and set pointer to register requirements
213 if (exists($n{"reg_req"})) {
214 my %req = %{ $n{"reg_req"} };
217 @in = @{ $req{"in"} } if (exists($req{"in"}));
219 @out = @{ $req{"out"} } if exists(($req{"out"}));
222 $in_param = $in_req_var;
229 $out_param = $out_req_var.", ".($#out + 1);
232 $out_param = "NULL, 0";
235 $temp .= "\n /* create node */\n";
236 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
238 $temp .= "\n /* init node attributes */\n";
239 $temp .= " init_$arch\_attributes(res, flags, $in_param, $out_param);\n";
241 if (exists($n{"init_attr"})) {
242 $temp .= " attr = get_$arch\_attr(res);\n";
243 $temp .= $n{"init_attr"}."\n";
246 $temp .= "\n /* optimize node */\n";
247 $temp .= " res = optimize_node(res);\n";
248 $temp .= " irn_vrfy_irg(res, irg);\n\n";
250 $temp .= "\n return res;\n";
252 push(@obst_constructor, $temp);
254 else { # user defined constructor
255 push(@obst_constructor, $n{"rd_constructor"});
258 # close constructor function
259 push(@obst_constructor, "}\n\n");
261 } # constructor creation
263 # set default values for state and flags if not given
264 $n{"state"} = "floats" if (! exists($n{"state"}));
265 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
267 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
268 push(@obst_new_irop, " ops.dump_node = dump_node_$arch;\n");
270 if ($cmp_attr_func) {
271 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
275 $temp = " op_$op = new_ir_op(cur_opcode + iro_$op, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
276 $temp .= "|M, ".translate_arity($arity).", 0, sizeof($arch\_attr_t), &ops);\n";
277 push(@obst_new_irop, $temp);
278 push(@obst_enum_op, " iro_$op,\n");
280 push(@obst_enum_op, " iro_$arch\_last_generated,\n");
281 push(@obst_enum_op, " iro_$arch\_last = iro_$arch\_last_generated");
282 push(@obst_enum_op, " + $additional_opcodes") if (defined($additional_opcodes));
283 push(@obst_enum_op, "\n} $arch\_opcodes;\n\n");
287 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
289 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
290 print OUT @obst_cmp_attr;
292 print OUT @obst_opvar;
294 print OUT @obst_get_opvar;
297 print OUT<<ENDOFISIRN;
299 static int $arch\_opcode_start = -1;
300 static int $arch\_opcode_end = -1;
302 /** Return the opcode number of the first $arch opcode. */
303 int get_$arch\_opcode_first(void) {
304 return $arch\_opcode_start;
307 /** Return the opcode number of the last $arch opcode + 1. */
308 int get_$arch\_opcode_last(void) {
309 return $arch\_opcode_end;
312 /** Return non-zero if the given node is a $arch machine node. */
313 int is_$arch\_irn(const ir_node *node) {
314 unsigned opc = (unsigned)get_irn_opcode(node);
316 assert($arch\_opcode_start > 0 && "missing opcode init");
317 assert($arch\_opcode_end > 0 && "missing opcode init");
319 if (opc - (unsigned)$arch\_opcode_start < (unsigned)($arch\_opcode_end - $arch\_opcode_start))
325 int get_$arch\_irn_opcode(const ir_node *node) {
326 assert(is_$arch\_irn(node));
327 return get_irn_opcode(node) - $arch\_opcode_start;
332 print OUT @obst_constructor;
334 print OUT<<ENDOFMAIN;
336 * Creates the $arch specific Firm machine operations
337 * needed for the assembler irgs.
339 void $arch\_create_opcodes(void) {
340 #define N irop_flag_none
341 #define L irop_flag_labeled
342 #define C irop_flag_commutative
343 #define X irop_flag_cfopcode
344 #define I irop_flag_ip_cfopcode
345 #define F irop_flag_fragile
346 #define Y irop_flag_forking
347 #define H irop_flag_highlevel
348 #define c irop_flag_constlike
349 #define K irop_flag_keep
350 #define M irop_flag_machine
351 #define R (irop_flag_machine<<1)
354 int cur_opcode = get_next_ir_opcodes(iro_$arch\_last);
356 $arch\_opcode_start = cur_opcode;
359 print OUT @obst_new_irop;
361 print OUT " $arch\_register_additional_opcodes(cur_opcode);\n" if (defined($additional_opcodes));
362 print OUT " $arch\_opcode_end = cur_opcode + iro_$arch\_last";
363 print OUT " + $additional_opcodes" if (defined($additional_opcodes));
369 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
371 print OUT "#ifndef __GEN_$arch\_NEW_NODES_H__\n";
372 print OUT "#define __GEN_$arch\_NEW_NODES_H__\n\n";
373 print OUT @obst_enum_op;
374 print OUT "int is_$arch\_irn(const ir_node *node);\n\n";
375 print OUT "int get_$arch\_opcode_first(void);\n";
376 print OUT "int get_$arch\_opcode_last(void);\n";
377 print OUT "int get_$arch\_irn_opcode(const ir_node *node);\n";
378 print OUT @obst_header;
379 print OUT "\n#endif /* __GEN_$arch\_NEW_NODES_H__ */\n";
384 # Translates numeric arity into string constant.
386 sub translate_arity {
389 if ($arity =~ /^\d+$/) {
391 return "oparity_zero";
393 elsif ($arity == 1) {
394 return "oparity_unary";
396 elsif ($arity == 2) {
397 return "oparity_binary";
399 elsif ($arity == 3) {
400 return "oparity_trinary";
407 return "oparity_".$arity;