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, "ir_op *get_op_$op(void);\n");
75 push(@obst_header, "int is_$op(const ir_node *n);\n");
78 # create compare attribute function if needed
79 if (exists($n{"cmp_attr"})) {
80 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
81 push(@obst_cmp_attr, " $arch\_attr_t *attr_a = get_$arch\_attr(a);\n");
82 push(@obst_cmp_attr, " $arch\_attr_t *attr_b = get_$arch\_attr(b);\n");
83 push(@obst_cmp_attr, $n{"cmp_attr"});
84 push(@obst_cmp_attr, "}\n\n");
89 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
90 # we explicitly skip the constructor if the specification entry says NONE
93 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
94 $n{"comment"} =~ s/^"|"$//g; # remove "
95 $n{"comment"} = "/* ".$n{"comment"}." */\n";
96 push(@obst_constructor, $n{"comment"});
98 # create constructor head
99 my $complete_args = "";
103 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
104 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
105 if ($arity !~ /^\d+$/) {
106 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
109 for (my $i = 1; $i <= $arity; $i++) {
110 $complete_args .= ", ir_node *op".$i;
111 $arg_names .= ", op".$i;
113 $complete_args .= ", ir_mode *mode";
114 $arg_names .= ", mode";
116 else { # user defined args
117 for my $href (@{ $n{"args"} }) {
118 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
119 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
120 $arg_names .= ", ".$href->{"name"};
123 $complete_args = substr($complete_args, 2);
124 $temp .= ", $complete_args)";
125 push(@obst_constructor, $temp." {\n");
126 push(@obst_header, $temp.";\n");
128 # emit constructor code
129 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
130 if ($arity !~ /^\d+$/) {
131 print "DEFAULT rd_constructor requires numeric arity! Ignoring op $orig_op!\n";
134 $temp = " $arch\_attr_t *attr;\n";
135 $temp .= " ir_node *res;\n";
136 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
138 undef my $in_req_var;
139 undef my $out_req_var;
141 # set up static variables for requirements and registers
142 if (exists($n{"reg_req"})) {
143 my %req = %{ $n{"reg_req"} };
147 @in = @{ $req{"in"} } if (exists($req{"in"}));
149 @out = @{ $req{"out"} } if exists(($req{"out"}));
152 $in_req_var = "_in_req_$op";
153 $temp .= " static const $arch\_register_req_t *".$in_req_var."[] =\n {\n";
154 for ($idx = 0; $idx <= $#in; $idx++) {
155 $temp .= " ".$op."_reg_req_in_".$idx.",\n";
161 $out_req_var = "_out_req_$op";
163 $temp .= " static const $arch\_register_req_t *".$out_req_var."[] =\n {\n";
164 for ($idx = 0; $idx <= $#out; $idx++) {
165 $temp .= " ".$op."_reg_req_out_".$idx.",\n";
172 $temp .= " if (!op_$op) {\n";
173 $temp .= " assert(0);\n";
174 $temp .= " return NULL;\n";
176 for (my $i = 1; $i <= $arity; $i++) {
177 $temp .= " in[".($i - 1)."] = op".$i.";\n";
179 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
180 $temp .= " res = optimize_node(res);\n";
181 $temp .= " irn_vrfy_irg(res, irg);\n\n";
184 $temp .= " attr = get_$arch\_attr(res);\n\n";
185 $temp .= " attr->flags = 0; /* clear flags */\n";
187 if (exists($n{"irn_flags"})) {
188 foreach my $flag (split(/\|/, $n{"irn_flags"})) {
190 $temp .= " attr->flags |= arch_irn_flags_rematerializable; /* op can be easily recalulated */\n";
192 elsif ($flag eq "N") {
193 $temp .= " attr->flags |= arch_irn_flags_dont_spill; /* op is NOT spillable */\n";
195 elsif ($flag eq "I") {
196 $temp .= " attr->flags |= arch_irn_flags_ignore; /* ignore op for register allocation */\n";
201 # allocate memory and set pointer to register requirements
202 if (exists($n{"reg_req"})) {
203 my %req = %{ $n{"reg_req"} };
206 @in = @{ $req{"in"} } if (exists($req{"in"}));
208 @out = @{ $req{"out"} } if exists(($req{"out"}));
210 $temp .= "\n /* set IN register requirements */\n";
212 $temp .= " attr->in_req = ".$in_req_var.";\n";
215 $temp .= " attr->in_req = NULL;\n";
218 $temp .= "\n /* set OUT register requirements and get space for registers */\n";
220 $temp .= " attr->out_req = ".$out_req_var.";\n";
221 $temp .= " attr->slots = xcalloc(".($#out + 1).", sizeof(attr->slots[0]));\n";
222 $temp .= " attr->n_res = ".($#out + 1).";\n";
225 $temp .= " attr->out_req = NULL;\n";
226 $temp .= " attr->slots = NULL;\n";
227 $temp .= " attr->n_res = 0;\n";
231 $temp .= "\n return res;\n";
233 push(@obst_constructor, $temp);
235 else { # user defined constructor
236 push(@obst_constructor, $n{"rd_constructor"});
239 # close constructor function
240 push(@obst_constructor, "}\n\n");
242 } # constructor creation
244 # set default values for state and flags if not given
245 $n{"state"} = "floats" if (! exists($n{"state"}));
246 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
248 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
249 push(@obst_new_irop, " ops.dump_node = dump_node_$arch;\n");
251 if ($cmp_attr_func) {
252 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
256 $temp = " op_$op = new_ir_op(cur_opcode++, \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
257 $temp .= ", ".translate_arity($arity).", 2, sizeof($arch\_attr_t), &ops);\n";
258 push(@obst_new_irop, $temp);
263 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
265 print OUT "#include \"gen_$arch\_regalloc_if_t.h\"\n\n";
266 print OUT @obst_cmp_attr;
268 print OUT @obst_opvar;
270 print OUT @obst_get_opvar;
273 print OUT<<ENDOFISIRN;
275 static opcode ia32_opcode_start = -1;
276 static opcode ia32_opcode_end = -1;
278 int is_$arch\_irn(const ir_node *node) {
279 opcode opc = get_irn_opcode(node);
281 assert(ia32_opcode_start > 0 && "missing opcode init");
282 assert(ia32_opcode_end > 0 && "missing opcode init");
284 if (opc > ia32_opcode_start && opc < ia32_opcode_end)
292 print OUT @obst_constructor;
294 print OUT<<ENDOFMAIN;
296 * Creates the $arch specific firm operations
297 * needed for the assembler irgs.
299 void $arch\_create_opcodes(void) {
300 #define N irop_flag_none
301 #define L irop_flag_labeled
302 #define C irop_flag_commutative
303 #define X irop_flag_cfopcode
304 #define I irop_flag_ip_cfopcode
305 #define F irop_flag_fragile
306 #define Y irop_flag_forking
307 #define H irop_flag_highlevel
308 #define c irop_flag_constlike
309 #define K irop_flag_keep
312 int cur_opcode = get_next_ir_opcodes($n_opcodes);
314 ia32_opcode_start = cur_opcode++;
318 print OUT @obst_new_irop;
319 print OUT "\n ia32_opcode_end = cur_opcode;\n";
324 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
326 print OUT "int is_$arch\_irn(const ir_node *node);\n";
327 print OUT @obst_header;
332 # Translates numeric arity into string constant.
334 sub translate_arity {
337 if ($arity =~ /^\d+$/) {
339 return "oparity_zero";
341 elsif ($arity == 1) {
342 return "oparity_unary";
344 elsif ($arity == 2) {
345 return "oparity_binary";
347 elsif ($arity == 3) {
348 return "oparity_trinary";
355 return "oparity_".$arity;