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];
18 my $target_c = $target_dir."/new_nodes.c";
19 my $target_h = $target_dir."/new_nodes.h";
29 unless ($return = do $specfile) {
30 warn "couldn't parse $specfile: $@" if $@;
31 warn "couldn't do $specfile: $!" unless defined $return;
32 warn "couldn't run $specfile" unless $return;
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
49 push(@obst_header, "void create_bearch_asm_opcodes(void);");
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(ir_node *n) { return get_irn_op(n) == op_$op ? 1 : 0; }\n\n");
62 push(@obst_header, "int is_$op(ir_node *n);\n");
64 $n{"comment"} =~ s/^"|"$//g;
65 $n{"comment"} = "/* ".$n{"comment"}." */\n";
66 push(@obst_constructor, $n{"comment"});
68 # create constructor head
69 my $complete_args = "";
71 $temp = "ir_node *new_rd_$op(dbg_info *db, ir_graph *irg, ir_node *block";
72 if (!exists($n{"args"}) || $n{"args"} eq "DEFAULT") { # default args
73 if ($n{"arity"} !~ /^[0-3]$/) {
74 print "DEFAULT args require arity 0,1,2 or 3! Ignoring op $orig_op!\n";
77 for (my $i = 1; $i <= $n{"arity"}; $i++) {
78 $complete_args .= ", ir_node *op".$i;
79 $arg_names .= ", op".$i;
81 $complete_args .= ", ir_mode *mode";
82 $arg_names .= ", mode";
84 else { # user defined args
85 for my $href (@{ $n{"args"} }) {
86 $complete_args .= ", ".$href->{"type"}.$href->{"name"};
87 $arg_names .= ", ".$href->{"name"};
90 $complete_args = substr($complete_args, 2);
91 $temp .= ", $complete_args)";
92 push(@obst_constructor, $temp." {\n");
93 push(@obst_header, $temp.";\n");
95 # emit constructor code
96 if (!exists($n{"rd_constructor"}) || $n{"rd_constructor"} eq "DEFAULT") { # default constructor
97 if ($n{"arity"} !~ /^[0-3]$/) {
98 print "DEFAULT rd_constructor requires arity 0,1,2 or 3! Ignoring op $orig_op!\n";
101 $temp = " ir_node *res;\n";
102 $temp .= " ir_node *in[$arity];\n" if ($arity > 0);
104 $temp .= " if (!op_$op) {\n";
105 $temp .= " assert(0);\n";
106 $temp .= " return NULL;\n";
108 for (my $i = 1; $i <= $arity; $i++) {
109 $temp .= " in[".($i - 1)."] = op".$i.";\n";
111 $temp .= " res = new_ir_node(db, irg, block, op_$op, mode, $arity, ".($arity > 0 ? "in" : "NULL").");\n";
112 $temp .= " res = optimize_node(res);\n";
113 $temp .= " irn_vrfy_irg(res, irg);\n";
114 $temp .= " return res;\n";
116 push(@obst_constructor, $temp);
118 else { # user defined constructor
119 push(@obst_constructor, $n{"rd_constructor"});
122 # close constructor function
123 push(@obst_constructor, "}\n\n");
125 # # create the _r and _d wrapper
126 # $temp = "ir_node *new_r_$op(ir_graph *irg, ir_node *block, $complete_args)";
127 # push(@obst_header, $temp.";\n");
129 # $temp .= " return new_rd_$op(NULL, irg, block".$arg_names.");\n";
131 # push(@obst_constructor, $temp);
133 # $temp = "ir_node *new_d_$op(dbg_info *db, $complete_args)";
134 # push(@obst_header, $temp.";\n");
136 # $temp .= " return new_rd_$op(db, current_ir_graph, current_ir_graph->current_block".$arg_names.");\n";
138 # push(@obst_constructor, $temp);
140 # $temp = "ir_node *new_$op($complete_args)";
141 # push(@obst_header, $temp.";\n");
143 # $temp .= " return new_d_$op(NULL".$arg_names.");\n";
145 # push(@obst_constructor, $temp);
147 # construct the new_ir_op calls
149 # set default values for state and flags if not given
150 $n{"state"} = "pinned" if (! exists($n{"state"}));
151 $n{"op_flags"} = "N" if (! exists($n{"op_flags"}));
153 my $arity_str = $arity == 0 ? "zero" : ($arity == 1 ? "unary" : ($arity == 2 ? "binary" : ($arity == 3 ? "trinary" : $arity)));
154 $temp = " op_$op = new_ir_op(get_next_ir_opcode(), \"$op\", op_pin_state_".$n{"state"}.", ".$n{"op_flags"};
155 $temp .= ", oparity_".$arity_str.", 0, sizeof(asmop_attr), &ops);\n";
156 push(@obst_new_irop, $temp);
160 my $creation_time = localtime(time());
162 open(OUT, ">$target_c") || die("Could not open $target_c, reason: $!\n");
164 print OUT<<ENDOFHEADER;
166 * This file implements the creation of the achitecture specific firm opcodes
167 * and the coresponding node constructors for the $arch assembler irg.
168 * DO NOT EDIT THIS FILE, your changes will be lost.
169 * Edit $specfile instead.
170 * created by: $0 $specfile $target_dir
171 * date: $creation_time
180 #include "irprog_t.h"
181 #include "irgraph_t.h"
182 #include "irnode_t.h"
183 #include "irmode_t.h"
184 #include "ircons_t.h"
186 #include "firm_common_t.h"
187 #include "irvrfy_t.h"
189 #include "../firm2arch_nodes_attr.h"
190 #include "../bearch_firm.h"
192 #include "new_nodes.h"
193 #include "dump_support.inl"
196 * Return the tarval of an immediate operation or NULL in case of SymConst
198 tarval *get_Immop_tarval(ir_node *node) {
199 asmop_attr *attr = (asmop_attr *)get_irn_generic_attr(node);
200 if (attr->tp == asmop_Const)
201 return attr->data.tv;
207 * Return the old_ir attribute.
209 ir_node *get_old_ir(ir_node *node) {
210 asmop_attr *attr = (asmop_attr *)get_irn_generic_attr(node);
211 return attr->data.old_ir;
215 * Copy the attributes from an Imm to an Immop (Add_i, Sub_i, ...) node
217 void set_Immop_attr(ir_node *node, ir_node *imm) {
218 asmop_attr *attr = (asmop_attr *)get_irn_generic_attr(node);
219 assert(is_Imm(imm) && "Need Imm to set Immop attr");
221 imm_attr_t *ia = (imm_attr_t *)get_irn_generic_attr(imm);
222 if (ia->tp == imm_Const) {
223 attr->tp = asmop_Const;
224 attr->data.tv = ia->data.tv;
227 attr->tp = asmop_SymConst;
228 attr->data.old_ir = ia->data.symconst;
233 * Sets the attributes of an immediate operation to the specified tarval
235 void set_Immop_attr_tv(ir_node *node, tarval *tv) {
236 asmop_attr *attr = (asmop_attr *)get_irn_generic_attr(node);
238 attr->tp = asmop_Const;
243 * Sets the offset for a Lea.
245 void set_ia32_Lea_offs(ir_node *node, tarval *offs) {
246 assert(is_ia32_Lea(node) && "Cannot set offset in non-Lea node.");
248 asmop_attr *attr = (asmop_attr *)get_irn_generic_attr(node);
249 attr->data.offset = offs;
253 * Gets the offset for a Lea.
255 tarval *get_ia32_Lea_offs(ir_node *node) {
256 assert(is_ia32_Lea(node) && "Cannot get offset for a non-Lea node.");
258 asmop_attr *attr = (asmop_attr *)get_irn_generic_attr(node);
259 return attr->data.offset;
264 print OUT @obst_opvar;
266 print OUT @obst_get_opvar;
268 print OUT @obst_constructor;
270 print OUT<<ENDOFMAIN;
272 * Creates the architecture specific firm operations
273 * needed for the assembler irgs.
275 void create_bearch_asm_opcodes(void) {
276 #define N irop_flag_none
277 #define L irop_flag_labeled
278 #define C irop_flag_commutative
279 #define X irop_flag_cfopcode
280 #define I irop_flag_ip_cfopcode
281 #define F irop_flag_fragile
282 #define Y irop_flag_forking
283 #define H irop_flag_highlevel
284 #define c irop_flag_constlike
288 memset(&ops, 0, sizeof(ops));
290 /* enter our modified dumper */
291 ops.dump_node = dump_node_ia32;
295 print OUT @obst_new_irop;
300 $creation_time = localtime(time());
302 open(OUT, ">$target_h") || die("Could not open $target_h, reason: $!\n");
308 * Function prototypes for the assembler ir node constructors.
309 * DO NOT EDIT THIS FILE, your changes will be lost.
310 * Edit $specfile instead.
311 * created by: $0 $specfile $target_dir
312 * date: $creation_time
315 tarval *get_Immop_tarval(ir_node *node);
316 ir_node *get_old_ir(ir_node *node);
317 void set_Immop_attr(ir_node *node, ir_node *imm);
318 void set_Immop_attr_tv(ir_node *node, tarval *tv);
319 void set_ia32_Lea_offs(ir_node *node, tarval *offs);
320 tarval *get_ia32_Lea_offs(ir_node *node);
324 print OUT @obst_header;
325 print OUT "\n#endif\n";