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.inl";
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
49 push(@obst_header, "void ".$arch."_create_opcodes(void);\n");
51 foreach my $op (keys(%nodes)) {
52 my %n = %{ $nodes{"$op"} };
58 push(@obst_opvar, "ir_op *op_$op = NULL;\n");
59 push(@obst_get_opvar, "ir_op *get_op_$op(void) { return op_$op; }\n");
60 push(@obst_get_opvar, "int is_$op(const ir_node *n) { return get_irn_op(n) == op_$op; }\n\n");
62 push(@obst_is_archirn, "is_$op(node)");
64 push(@obst_header, "int is_$op(const ir_node *n);\n");
66 $n{"comment"} =~ s/^"|"$//g;
67 $n{"comment"} = "/* ".$n{"comment"}." */\n";
68 push(@obst_constructor, $n{"comment"});
70 # create constructor head
71 my $complete_args = "";
75 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
76 if (!exists($n{"args"}) || $n{"args"} eq "DEFAULT") { # default args
77 if ($n{"arity"} !~ /^[0-3]$/) {
78 print "DEFAULT args require arity 0,1,2 or 3! Ignoring op $orig_op!\n";
81 for (my $i = 1; $i <= $n{"arity"}; $i++) {
82 $complete_args .= ", ir_node *op".$i;
83 $arg_names .= ", op".$i;
85 $complete_args .= ", ir_mode *mode";
86 $arg_names .= ", mode";
88 else { # user defined args
89 for my $href (@{ $n{"args"} }) {
90 $href->{"type"} .= " " if ($href->{"type"} !~ / [*]?$/); # put a space between name and type if there is none at the end
91 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
92 $arg_names .= ", ".$href->{"name"};
95 $complete_args = substr($complete_args, 2);
96 $temp .= ", $complete_args)";
97 push(@obst_constructor, $temp." {\n");
98 push(@obst_header, $temp.";\n");
100 # emit constructor code
101 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} eq "DEFAULT") { # default constructor
102 if ($n{"arity"} !~ /^[0-3]$/) {
103 print "DEFAULT rd_constructor requires arity 0,1,2 or 3! Ignoring op $orig_op!\n";
106 $temp = " asmop_attr *attr;\n";
107 $temp .= " ir_node *res;\n";
108 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
110 $temp .= " if (!op_$op) {\n";
111 $temp .= " assert(0);\n";
112 $temp .= " return NULL;\n";
114 for (my $i = 1; $i <= $arity; $i++) {
115 $temp .= " in[".($i - 1)."] = op".$i.";\n";
117 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
120 $temp .= " attr = get_ia32_attr(res);\n\n";
121 $temp .= " attr->flags = 0; /* clear flags */\n";
122 if (!exists($n{"spill"}) || $n{"spill"} == 1) {
123 $temp .= " attr->flags |= arch_irn_flags_spillable; /* op is spillable */\n";
125 if (exists($n{"remat"}) && $n{"remat"} == 1) {
126 $temp .= " attr->flags |= arch_irn_flags_rematerializable; /* op can be easily recalulated */\n";
129 # allocate memory and set pointer to register requirements
130 if (exists($n{"reg_req"})) {
131 my %req = %{ $n{"reg_req"} };
135 @in = @{ $req{"in"} } if (exists($req{"in"}));
137 @out = @{ $req{"out"} } if exists(($req{"out"}));
140 $temp .= "\n /* allocate memory for IN register requirements and assigned registers */\n";
141 $temp .= " attr->in_req = malloc(".($#in + 1)." * sizeof(arch_register_req_t *)); /* space for in requirements */\n";
142 for ($idx = 0; $idx <= $#in; $idx++) {
143 $temp .= " attr->in_req[$idx] = &".$op."_reg_req_in_".$idx.";\n";
148 $temp .= "\n /* allocate memory for OUT register requirements and assigned registers */\n";
149 $temp .= " attr->out_req = malloc(".($#out + 1)." * sizeof(arch_register_req_t *)); /* space for out requirements */\n";
150 $temp .= " attr->slots = calloc(sizeof(arch_register_t *), ".($#out + 1)."); /* space for assigned registers */\n";
151 for ($idx = 0; $idx <= $#out; $idx++) {
152 $temp .= " attr->out_req[$idx] = &".$op."_reg_req_out_".$idx.";\n";
154 $temp .= " attr->n_res = ".($#out + 1).";\n";
157 $temp .= " attr->n_res = 0;\n";
161 $temp .= "\n return res;\n";
163 push(@obst_constructor, $temp);
165 else { # user defined constructor
166 push(@obst_constructor, $n{"rd_constructor"});
169 # close constructor function
170 push(@obst_constructor, "}\n\n");
172 # set default values for state and flags if not given
173 $n{"state"} = "pinned" if (! exists($n{"state"}));
174 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
176 $temp = " op_$op = new_ir_op(get_next_ir_opcode(), \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
177 $temp .= ", ".translate_arity($arity).", 0, sizeof(asmop_attr), &ops);\n";
178 push(@obst_new_irop, $temp);
183 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
185 print OUT @obst_opvar;
187 print OUT @obst_get_opvar;
189 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";
190 print OUT @obst_constructor;
192 print OUT<<ENDOFMAIN;
194 * Creates the $arch specific firm operations
195 * needed for the assembler irgs.
197 void $arch\_create_opcodes(void) {
198 #define N irop_flag_none
199 #define L irop_flag_labeled
200 #define C irop_flag_commutative
201 #define X irop_flag_cfopcode
202 #define I irop_flag_ip_cfopcode
203 #define F irop_flag_fragile
204 #define Y irop_flag_forking
205 #define H irop_flag_highlevel
206 #define c irop_flag_constlike
210 memset(&ops, 0, sizeof(ops));
211 ops.dump_node = dump_node_$arch;
215 print OUT @obst_new_irop;
220 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
222 print OUT @obst_header;
227 # Translates numeric arity into string constant.
229 sub translate_arity {
233 return "oparity_zero";
235 elsif ($arity == 1) {
236 return "oparity_unary";
238 elsif ($arity == 1) {
239 return "oparity_binary";
241 elsif ($arity == 1) {
242 return "oparity_trinary";