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
52 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
54 foreach my $op (keys(%nodes)) {
55 my %n = %{ $nodes{"$op"} };
62 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
63 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
64 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
66 push(@obst_is_archirn, "is_$op(node)");
68 push(@obst_header, "int is_$op(const ir_node *n);\n");
71 # create compare attribute function if needed
72 if (exists($n{"cmp_attr"})) {
73 push(@obst_cmp_attr, "static int cmp_attr_$op(ir_node *a, ir_node *b) {\n");
74 push(@obst_cmp_attr, " asmop_attr *attr_a = get_ia32_attr(a);\n");
75 push(@obst_cmp_attr, " asmop_attr *attr_b = get_ia32_attr(b);\n");
76 push(@obst_cmp_attr, $n{"cmp_attr"});
77 push(@obst_cmp_attr, "}\n\n");
82 if (exists($n{"rd_constructor"}) && $n{"rd_constructor"} =~ /^NONE$/i) {
83 # we explicitly skip the constructor if the specification entry says NONE
86 $n{"comment"} = "construct $op" if(!exists($n{"comment"}));
87 $n{"comment"} =~ s/^"|"$//g; # remove "
88 $n{"comment"} = "/* ".$n{"comment"}." */\n";
89 push(@obst_constructor, $n{"comment"});
91 # create constructor head
92 my $complete_args = "";
96 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
97 if (!exists($n{"args"}) || $n{"args"} =~ /^DEFAULT$/i) { # default args
98 if ($n{"arity"} !~ /^\d+$/) {
99 print "DEFAULT args require numeric arity (0, 1, 2, ...)! Ignoring op $orig_op!\n";
102 for (my $i = 1; $i <= $n{"arity"}; $i++) {
103 $complete_args .= ", ir_node *op".$i;
104 $arg_names .= ", op".$i;
106 $complete_args .= ", ir_mode *mode";
107 $arg_names .= ", mode";
109 else { # user defined args
110 for my $href (@{ $n{"args"} }) {
111 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
112 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
113 $arg_names .= ", ".$href->{"name"};
116 $complete_args = substr($complete_args, 2);
117 $temp .= ", $complete_args)";
118 push(@obst_constructor, $temp." {\n");
119 push(@obst_header, $temp.";\n");
121 # emit constructor code
122 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} =~ /^DEFAULT$/i) { # default constructor
123 if ($n{"arity"} !~ /^\d+$/) {
124 print "DEFAULT rd_constructor requires arity 0,1,2 or 3! Ignoring op $orig_op!\n";
127 $temp = " asmop_attr *attr;\n";
128 $temp .= " ir_node *res;\n";
129 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
131 $temp .= " if (!op_$op) {\n";
132 $temp .= " assert(0);\n";
133 $temp .= " return NULL;\n";
135 for (my $i = 1; $i <= $arity; $i++) {
136 $temp .= " in[".($i - 1)."] = op".$i.";\n";
138 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
139 $temp .= " set_ia32_pncode(res, -1);\n";
140 $temp .= " res = optimize_node(res);\n";
141 $temp .= " irn_vrfy_irg(res, irg);\n\n";
144 $temp .= " attr = get_ia32_attr(res);\n\n";
145 $temp .= " attr->flags = 0; /* clear flags */\n";
146 if (!exists($n{"spill"}) || $n{"spill"} == 1) {
147 $temp .= " attr->flags |= arch_irn_flags_spillable; /* op is spillable */\n";
149 if (exists($n{"remat"}) && $n{"remat"} == 1) {
150 $temp .= " attr->flags |= arch_irn_flags_rematerializable; /* op can be easily recalulated */\n";
153 # allocate memory and set pointer to register requirements
154 if (exists($n{"reg_req"})) {
155 my %req = %{ $n{"reg_req"} };
159 @in = @{ $req{"in"} } if (exists($req{"in"}));
161 @out = @{ $req{"out"} } if exists(($req{"out"}));
164 $temp .= "\n /* allocate memory for IN register requirements and assigned registers */\n";
165 $temp .= " attr->in_req = calloc(".($#in + 1).", sizeof(arch_register_req_t *)); /* space for in requirements */\n";
166 for ($idx = 0; $idx <= $#in; $idx++) {
167 $temp .= " attr->in_req[$idx] = ".$op."_reg_req_in_".$idx.";\n";
172 $temp .= "\n /* allocate memory for OUT register requirements and assigned registers */\n";
173 $temp .= " attr->out_req = calloc(".($#out + 1).", sizeof(arch_register_req_t *)); /* space for out requirements */\n";
174 $temp .= " attr->slots = calloc(".($#out + 1).", sizeof(arch_register_t *)); /* space for assigned registers */\n";
175 for ($idx = 0; $idx <= $#out; $idx++) {
176 $temp .= " attr->out_req[$idx] = ".$op."_reg_req_out_".$idx.";\n";
178 $temp .= " attr->n_res = ".($#out + 1).";\n";
181 $temp .= " attr->n_res = 0;\n";
185 $temp .= "\n return res;\n";
187 push(@obst_constructor, $temp);
189 else { # user defined constructor
190 push(@obst_constructor, $n{"rd_constructor"});
193 # close constructor function
194 push(@obst_constructor, "}\n\n");
196 } # constructor creation
198 # set default values for state and flags if not given
199 $n{"state"} = "pinned" if (! exists($n{"state"}));
200 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
202 push(@obst_new_irop, "\n memset(&ops, 0, sizeof(ops));\n");
203 push(@obst_new_irop, " ops.dump_node = dump_node_$arch;\n");
205 if ($cmp_attr_func) {
206 push(@obst_new_irop, " ops.node_cmp_attr = cmp_attr_$op;\n");
209 $temp = " op_$op = new_ir_op(get_next_ir_opcode(), \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
210 $temp .= ", ".translate_arity($arity).", 0, sizeof(asmop_attr), &ops);\n";
211 push(@obst_new_irop, $temp);
216 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
218 print OUT @obst_cmp_attr;
220 print OUT @obst_opvar;
222 print OUT @obst_get_opvar;
224 print OUT "int is_".$arch."_irn(const ir_node *node) {\n if (".join(" ||\n ", @obst_is_archirn).")\n return 1;\n else\n return 0;\n}\n\n";
225 print OUT @obst_constructor;
227 print OUT<<ENDOFMAIN;
229 * Creates the $arch specific firm operations
230 * needed for the assembler irgs.
232 void $arch\_create_opcodes(void) {
233 #define N irop_flag_none
234 #define L irop_flag_labeled
235 #define C irop_flag_commutative
236 #define X irop_flag_cfopcode
237 #define I irop_flag_ip_cfopcode
238 #define F irop_flag_fragile
239 #define Y irop_flag_forking
240 #define H irop_flag_highlevel
241 #define c irop_flag_constlike
247 print OUT @obst_new_irop;
252 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
254 print OUT @obst_header;
259 # Translates numeric arity into string constant.
261 sub translate_arity {
264 if ($arity =~ /^\d+$/) {
266 return "oparity_zero";
268 elsif ($arity == 1) {
269 return "oparity_unary";
271 elsif ($arity == 2) {
272 return "oparity_binary";
274 elsif ($arity == 3) {
275 return "oparity_trinary";
282 return "oparity_".$arity;