7c55b6a64eaa5fe89f0711b7dfed316604614ea6
[libfirm] / ir / be / benode.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Backend node support for generic backend nodes.
23  * @author      Sebastian Hack
24  * @date        17.05.2005
25  * @version     $Id$
26  *
27  * Backend node support for generic backend nodes.
28  * This file provides Perm, Copy, Spill and Reload nodes.
29  */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <stdlib.h>
35
36 #include "obst.h"
37 #include "set.h"
38 #include "pmap.h"
39 #include "util.h"
40 #include "debug.h"
41 #include "fourcc.h"
42 #include "offset.h"
43 #include "bitfiddle.h"
44 #include "raw_bitset.h"
45
46 #include "irop_t.h"
47 #include "irmode_t.h"
48 #include "irnode_t.h"
49 #include "ircons_t.h"
50 #include "irprintf.h"
51 #include "irgwalk.h"
52 #include "iropt_t.h"
53
54 #include "be_t.h"
55 #include "belive_t.h"
56 #include "besched_t.h"
57 #include "benode_t.h"
58 #include "bearch_t.h"
59
60 #include "beirgmod.h"
61
62 #define OUT_POS(x) (-((x) + 1))
63
64 #define get_irn_attr(irn) get_irn_generic_attr(irn)
65 #define get_irn_attr_const(irn) get_irn_generic_attr_const(irn)
66
67 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
68
69 typedef struct {
70         arch_register_req_t req;
71         arch_irn_flags_t    flags;
72 } be_req_t;
73
74 typedef struct {
75         const arch_register_t *reg;
76         be_req_t               req;
77         be_req_t               in_req;
78 } be_reg_data_t;
79
80 /** The generic be nodes attribute type. */
81 typedef struct {
82         be_reg_data_t *reg_data;
83 } be_node_attr_t;
84
85 /** The be_Return nodes attribute type. */
86 typedef struct {
87         be_node_attr_t node_attr;     /**< base attributes of every be node. */
88         int            num_ret_vals;  /**< number of return values */
89         unsigned       pop;           /**< number of bytes that should be popped */
90         int            emit_pop;      /**< if set, emit pop bytes, even if pop = 0 */
91 } be_return_attr_t;
92
93 /** The be_IncSP attribute type. */
94 typedef struct {
95         be_node_attr_t node_attr;   /**< base attributes of every be node. */
96         int            offset;      /**< The offset by which the stack shall be expanded/shrinked. */
97         int            align;       /**< whether stack should be aligned after the
98                                          IncSP */
99 } be_incsp_attr_t;
100
101 /** The be_Frame attribute type. */
102 typedef struct {
103         be_node_attr_t  node_attr;   /**< base attributes of every be node. */
104         ir_entity      *ent;
105         int             offset;
106 } be_frame_attr_t;
107
108 /** The be_Call attribute type. */
109 typedef struct {
110         be_node_attr_t  node_attr;  /**< base attributes of every be node. */
111         ir_entity      *ent;        /**< The called entity if this is a static call. */
112         unsigned        pop;
113         ir_type        *call_tp;    /**< The call type, copied from the original Call node. */
114 } be_call_attr_t;
115
116 typedef struct {
117         be_node_attr_t   node_attr;  /**< base attributes of every be node. */
118         ir_entity      **in_entities;
119         ir_entity      **out_entities;
120 } be_memperm_attr_t;
121
122 ir_op *op_be_Spill;
123 ir_op *op_be_Reload;
124 ir_op *op_be_Perm;
125 ir_op *op_be_MemPerm;
126 ir_op *op_be_Copy;
127 ir_op *op_be_Keep;
128 ir_op *op_be_CopyKeep;
129 ir_op *op_be_Call;
130 ir_op *op_be_Return;
131 ir_op *op_be_IncSP;
132 ir_op *op_be_AddSP;
133 ir_op *op_be_SubSP;
134 ir_op *op_be_RegParams;
135 ir_op *op_be_FrameAddr;
136 ir_op *op_be_Barrier;
137 ir_op *op_be_Unwind;
138
139 static int beo_base = -1;
140
141 static const ir_op_ops be_node_op_ops;
142
143 #define N   irop_flag_none
144 #define L   irop_flag_labeled
145 #define C   irop_flag_commutative
146 #define X   irop_flag_cfopcode
147 #define I   irop_flag_ip_cfopcode
148 #define F   irop_flag_fragile
149 #define Y   irop_flag_forking
150 #define H   irop_flag_highlevel
151 #define c   irop_flag_constlike
152 #define K   irop_flag_keep
153 #define M   irop_flag_uses_memory
154
155 static int be_reqs_equal(const be_req_t *req1, const be_req_t *req2)
156 {
157         if(!reg_reqs_equal(&req1->req, &req2->req))
158                 return 0;
159         if(req1->flags != req2->flags)
160                 return 0;
161
162         return 1;
163 }
164
165 /**
166  * Compare two be node attributes.
167  *
168  * @return zero if both attributes are identically
169  */
170 static int _node_cmp_attr(const be_node_attr_t *a, const be_node_attr_t *b) {
171         int i, len;
172
173         if (ARR_LEN(a->reg_data) != ARR_LEN(b->reg_data))
174                 return 1;
175
176         len = ARR_LEN(a->reg_data);
177         for (i = 0; i < len; ++i) {
178                 if (a->reg_data[i].reg != b->reg_data[i].reg ||
179                                 !be_reqs_equal(&a->reg_data[i].in_req, &b->reg_data[i].in_req) ||
180                             !be_reqs_equal(&a->reg_data[i].req,    &b->reg_data[i].req))
181                         return 1;
182         }
183
184         return 0;
185 }
186
187 /**
188  * Compare the node attributes of two be_node's.
189  *
190  * @return zero if both nodes have identically attributes
191  */
192 static int node_cmp_attr(ir_node *a, ir_node *b) {
193         const be_node_attr_t *a_attr = get_irn_attr_const(a);
194         const be_node_attr_t *b_attr = get_irn_attr_const(b);
195
196         return _node_cmp_attr(a_attr, b_attr);
197 }
198
199 /**
200  * Compare the attributes of two be_FrameAddr nodes.
201  *
202  * @return zero if both nodes have identically attributes
203  */
204 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
205         const be_frame_attr_t *a_attr = get_irn_attr_const(a);
206         const be_frame_attr_t *b_attr = get_irn_attr_const(b);
207
208         if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
209                 return 1;
210
211         return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
212 }
213
214 /**
215  * Compare the attributes of two be_Return nodes.
216  *
217  * @return zero if both nodes have identically attributes
218  */
219 static int Return_cmp_attr(ir_node *a, ir_node *b) {
220         const be_return_attr_t *a_attr = get_irn_attr_const(a);
221         const be_return_attr_t *b_attr = get_irn_attr_const(b);
222
223         if (a_attr->num_ret_vals != b_attr->num_ret_vals)
224                 return 1;
225         if (a_attr->pop != b_attr->pop)
226                 return 1;
227         if (a_attr->emit_pop != b_attr->emit_pop)
228                 return 1;
229
230         return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
231 }
232
233 /**
234  * Compare the attributes of two be_IncSP nodes.
235  *
236  * @return zero if both nodes have identically attributes
237  */
238 static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
239         const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
240         const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
241
242         if (a_attr->offset != b_attr->offset)
243                 return 1;
244
245         return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
246 }
247
248 /**
249  * Compare the attributes of two be_Call nodes.
250  *
251  * @return zero if both nodes have identically attributes
252  */
253 static int Call_cmp_attr(ir_node *a, ir_node *b) {
254         const be_call_attr_t *a_attr = get_irn_attr_const(a);
255         const be_call_attr_t *b_attr = get_irn_attr_const(b);
256
257         if (a_attr->ent != b_attr->ent ||
258                         a_attr->call_tp != b_attr->call_tp)
259                 return 1;
260
261         return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
262 }
263
264 static INLINE be_req_t *get_be_req(const ir_node *node, int pos)
265 {
266         int idx;
267         const be_node_attr_t *attr;
268         be_reg_data_t *rd;
269
270         assert(is_be_node(node));
271         attr = get_irn_attr_const(node);
272
273         if(pos < 0) {
274                 idx = -(pos + 1);
275         } else {
276                 idx = pos;
277                 assert(idx < get_irn_arity(node));
278         }
279         assert(idx < ARR_LEN(attr->reg_data));
280         rd = &attr->reg_data[idx];
281
282         return pos < 0 ? &rd->req : &rd->in_req;
283 }
284
285 static INLINE arch_register_req_t *get_req(const ir_node *node, int pos)
286 {
287         be_req_t *bereq = get_be_req(node, pos);
288         return &bereq->req;
289 }
290
291 void be_node_init(void) {
292         static int inited = 0;
293
294         if(inited)
295                 return;
296
297         inited = 1;
298
299         /* Acquire all needed opcodes. */
300         beo_base = get_next_ir_opcodes(beo_Last);
301
302         op_be_Spill      = new_ir_op(beo_base + beo_Spill,     "be_Spill",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
303         op_be_Reload     = new_ir_op(beo_base + beo_Reload,    "be_Reload",    op_pin_state_pinned, N,   oparity_zero,     0, sizeof(be_frame_attr_t),   &be_node_op_ops);
304         op_be_Perm       = new_ir_op(beo_base + beo_Perm,      "be_Perm",      op_pin_state_pinned, N,   oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
305         op_be_MemPerm    = new_ir_op(beo_base + beo_MemPerm,   "be_MemPerm",   op_pin_state_pinned, N,   oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
306         op_be_Copy       = new_ir_op(beo_base + beo_Copy,      "be_Copy",      op_pin_state_floats, N,   oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
307         op_be_Keep       = new_ir_op(beo_base + beo_Keep,      "be_Keep",      op_pin_state_pinned, K,   oparity_dynamic,  0, sizeof(be_node_attr_t),    &be_node_op_ops);
308         op_be_CopyKeep   = new_ir_op(beo_base + beo_CopyKeep,  "be_CopyKeep",  op_pin_state_pinned, K,   oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
309         op_be_Call       = new_ir_op(beo_base + beo_Call,      "be_Call",      op_pin_state_pinned, F|M, oparity_variable, 0, sizeof(be_call_attr_t),    &be_node_op_ops);
310         op_be_Return     = new_ir_op(beo_base + beo_Return,    "be_Return",    op_pin_state_pinned, X,   oparity_dynamic,  0, sizeof(be_return_attr_t),  &be_node_op_ops);
311         op_be_AddSP      = new_ir_op(beo_base + beo_AddSP,     "be_AddSP",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
312         op_be_SubSP      = new_ir_op(beo_base + beo_SubSP,     "be_SubSP",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
313         op_be_IncSP      = new_ir_op(beo_base + beo_IncSP,     "be_IncSP",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_incsp_attr_t),   &be_node_op_ops);
314         op_be_RegParams  = new_ir_op(beo_base + beo_RegParams, "be_RegParams", op_pin_state_pinned, N,   oparity_zero,     0, sizeof(be_node_attr_t),    &be_node_op_ops);
315         op_be_FrameAddr  = new_ir_op(beo_base + beo_FrameAddr, "be_FrameAddr", op_pin_state_floats, N,   oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
316         op_be_Barrier    = new_ir_op(beo_base + beo_Barrier,   "be_Barrier",   op_pin_state_pinned, N,   oparity_dynamic,  0, sizeof(be_node_attr_t),    &be_node_op_ops);
317         op_be_Unwind     = new_ir_op(beo_base + beo_Unwind,    "be_Unwind",    op_pin_state_pinned, X,   oparity_zero,     0, sizeof(be_node_attr_t),    &be_node_op_ops);
318
319         set_op_tag(op_be_Spill,      &be_node_tag);
320         op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
321         set_op_tag(op_be_Reload,     &be_node_tag);
322         op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
323         set_op_tag(op_be_Perm,       &be_node_tag);
324         op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
325         set_op_tag(op_be_MemPerm,    &be_node_tag);
326         op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
327         set_op_tag(op_be_Copy,       &be_node_tag);
328         op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
329         set_op_tag(op_be_Keep,       &be_node_tag);
330         op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
331         set_op_tag(op_be_CopyKeep,   &be_node_tag);
332         op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
333         set_op_tag(op_be_Call,       &be_node_tag);
334         op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
335         set_op_tag(op_be_Return,     &be_node_tag);
336         op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
337         set_op_tag(op_be_AddSP,      &be_node_tag);
338         op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
339         set_op_tag(op_be_SubSP,      &be_node_tag);
340         op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
341         set_op_tag(op_be_IncSP,      &be_node_tag);
342         op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
343         set_op_tag(op_be_RegParams,  &be_node_tag);
344         op_be_RegParams->ops.node_cmp_attr = node_cmp_attr;
345         set_op_tag(op_be_FrameAddr,  &be_node_tag);
346         op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
347         set_op_tag(op_be_Barrier,    &be_node_tag);
348         op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
349         set_op_tag(op_be_Unwind,     &be_node_tag);
350         op_be_Unwind->ops.node_cmp_attr = node_cmp_attr;
351 }
352
353 /**
354  * Initializes the generic attribute of all be nodes and return ir.
355  */
356 static void *init_node_attr(ir_node *node, int max_reg_data)
357 {
358         ir_graph *irg = get_irn_irg(node);
359         struct obstack *obst = get_irg_obstack(irg);
360         be_node_attr_t *a = get_irn_attr(node);
361
362         memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
363
364         if(max_reg_data >= 0) {
365                 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
366                 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
367         } else {
368                 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
369         }
370
371         return a;
372 }
373
374 static void add_register_req(ir_node *node)
375 {
376         be_node_attr_t *a = get_irn_attr(node);
377         be_reg_data_t regreq;
378         memset(&regreq, 0, sizeof(regreq));
379         ARR_APP1(be_reg_data_t, a->reg_data, regreq);
380 }
381
382 int is_be_node(const ir_node *irn)
383 {
384         return get_op_tag(get_irn_op(irn)) == &be_node_tag;
385 }
386
387 be_opcode_t be_get_irn_opcode(const ir_node *irn)
388 {
389         return is_be_node(irn) ? (be_opcode_t) get_irn_opcode(irn) - beo_base : beo_NoBeOp;
390 }
391
392 /**
393  * Skip Proj nodes and return their Proj numbers.
394  *
395  * If *node is a Proj or Proj(Proj) node, skip it.
396  *
397  * @param node  points to the node to be skipped
398  *
399  * @return 0 if *node was no Proj node, its Proj number else.
400  */
401 static int redir_proj(const ir_node **node)
402 {
403         const ir_node *n = *node;
404
405         if(is_Proj(n)) {
406                 ir_node *irn;
407
408                 *node = irn = get_Proj_pred(n);
409                 if(is_Proj(irn)) {
410                         assert(get_irn_mode(irn) == mode_T);
411                         *node = get_Proj_pred(irn);
412                 }
413                 return get_Proj_proj(n);
414         }
415
416         return 0;
417 }
418
419 static be_reg_data_t *retrieve_reg_data(const ir_node *node)
420 {
421         const be_node_attr_t *attr;
422         int pos = 0;
423
424         if(is_Proj(node)) {
425                 pos = get_Proj_proj(node);
426                 node = get_Proj_pred(node);
427         }
428
429         assert(is_be_node(node));
430         attr = get_irn_attr_const(node);
431         assert(pos >= 0 && pos < ARR_LEN(attr->reg_data) && "illegal proj number");
432
433         return &attr->reg_data[pos];
434 }
435
436 static void
437 be_node_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
438 {
439         be_reg_data_t *r = retrieve_reg_data(irn);
440         (void) self;
441         r->reg = reg;
442 }
443
444 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
445         ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill)
446 {
447         be_frame_attr_t *a;
448         ir_node         *in[2];
449         ir_node         *res;
450
451         in[0]     = frame;
452         in[1]     = to_spill;
453         res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
454         a         = init_node_attr(res, 2);
455         a->ent    = NULL;
456         a->offset = 0;
457
458         be_node_set_reg_class(res, be_pos_Spill_frame, cls_frame);
459         be_node_set_reg_class(res, be_pos_Spill_val, cls);
460         return res;
461 }
462
463 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
464         ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
465 {
466         ir_node *in[2];
467         ir_node *res;
468
469         in[0] = frame;
470         in[1] = mem;
471         res   = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
472
473         init_node_attr(res, 2);
474         be_node_set_reg_class(res, -1, cls);
475         be_node_set_reg_class(res, be_pos_Reload_frame, cls_frame);
476         be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
477         return res;
478 }
479
480 ir_node *be_get_Reload_mem(const ir_node *irn)
481 {
482         assert(be_is_Reload(irn));
483         return get_irn_n(irn, be_pos_Reload_mem);
484 }
485
486 ir_node *be_get_Reload_frame(const ir_node *irn)
487 {
488         assert(be_is_Reload(irn));
489         return get_irn_n(irn, be_pos_Reload_frame);
490 }
491
492 ir_node *be_get_Spill_val(const ir_node *irn)
493 {
494         assert(be_is_Spill(irn));
495         return get_irn_n(irn, be_pos_Spill_val);
496 }
497
498 ir_node *be_get_Spill_frame(const ir_node *irn)
499 {
500         assert(be_is_Spill(irn));
501         return get_irn_n(irn, be_pos_Spill_frame);
502 }
503
504 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
505 {
506         int i;
507         ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
508         init_node_attr(irn, n);
509         for(i = 0; i < n; ++i) {
510                 be_node_set_reg_class(irn, i, cls);
511                 be_node_set_reg_class(irn, OUT_POS(i), cls);
512         }
513
514         return irn;
515 }
516
517 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
518 {
519         ir_graph *irg           = get_irn_irg(perm);
520         int            arity    = get_irn_arity(perm);
521         be_reg_data_t *old_data = alloca(arity * sizeof(old_data[0]));
522         be_node_attr_t *attr    = get_irn_attr(perm);
523         ir_node **new_in        = NEW_ARR_D(ir_node *, irg->obst, new_size);
524
525         int i;
526
527         assert(be_is_Perm(perm));
528         assert(new_size <= arity);
529
530         /* save the old register data */
531         memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
532
533         /* compose the new in array and set the new register data directly in place */
534         for (i = 0; i < new_size; ++i) {
535                 int idx = map[i];
536                 new_in[i]         = get_irn_n(perm, idx);
537                 attr->reg_data[i] = old_data[idx];
538         }
539
540         set_irn_in(perm, new_size, new_in);
541 }
542
543 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
544 {
545         int i;
546         ir_node *frame = get_irg_frame(irg);
547         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
548         ir_node *irn;
549         const arch_register_t *sp = arch_env->isa->sp;
550         be_memperm_attr_t *attr;
551         ir_node **real_in;
552
553         real_in = alloca((n+1) * sizeof(real_in[0]));
554         real_in[0] = frame;
555         memcpy(&real_in[1], in, n * sizeof(real_in[0]));
556
557         irn =  new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
558
559         init_node_attr(irn, n + 1);
560         be_node_set_reg_class(irn, 0, sp->reg_class);
561         for (i = 0; i < n; ++i) {
562                 be_node_set_reg_class(irn, i + 1, cls_frame);
563                 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
564         }
565
566         attr = get_irn_attr(irn);
567
568         attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
569         memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
570         attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
571         memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
572
573         return irn;
574 }
575
576
577 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
578 {
579         ir_node *in[1];
580         ir_node *res;
581         arch_register_req_t *req;
582
583         in[0] = op;
584         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
585         init_node_attr(res, 1);
586         be_node_set_reg_class(res, 0, cls);
587         be_node_set_reg_class(res, OUT_POS(0), cls);
588
589         req = get_req(res, OUT_POS(0));
590         req->cls = cls;
591         req->type = arch_register_req_type_should_be_same;
592         req->other_same = 1U << 0;
593
594         return res;
595 }
596
597 ir_node *be_get_Copy_op(const ir_node *cpy) {
598         return get_irn_n(cpy, be_pos_Copy_op);
599 }
600
601 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
602         set_irn_n(cpy, be_pos_Copy_op, op);
603 }
604
605 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
606 {
607         int i;
608         ir_node *res;
609
610         res = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, -1, NULL);
611         init_node_attr(res, -1);
612
613         for(i = 0; i < n; ++i) {
614                 add_irn_n(res, in[i]);
615                 add_register_req(res);
616                 be_node_set_reg_class(res, i, cls);
617         }
618         keep_alive(res);
619
620         return res;
621 }
622
623 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
624 {
625         int n;
626
627         assert(be_is_Keep(keep));
628         n = add_irn_n(keep, node);
629         add_register_req(keep);
630         be_node_set_reg_class(keep, n, cls);
631 }
632
633 /* creates a be_Call */
634 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
635                      int n_outs, int n, ir_node *in[], ir_type *call_tp)
636 {
637         be_call_attr_t *a;
638         int real_n = be_pos_Call_first_arg + n;
639         ir_node *irn;
640         ir_node **real_in;
641
642         NEW_ARR_A(ir_node *, real_in, real_n);
643         real_in[be_pos_Call_mem] = mem;
644         real_in[be_pos_Call_sp]  = sp;
645         real_in[be_pos_Call_ptr] = ptr;
646         memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
647
648         irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
649         a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
650         a->ent     = NULL;
651         a->call_tp = call_tp;
652         a->pop     = 0;
653         return irn;
654 }
655
656 /* Gets the call entity or NULL if this is no static call. */
657 ir_entity *be_Call_get_entity(const ir_node *call) {
658         const be_call_attr_t *a = get_irn_attr_const(call);
659         assert(be_is_Call(call));
660         return a->ent;
661 }
662
663 /* Sets the call entity. */
664 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
665         be_call_attr_t *a = get_irn_attr(call);
666         assert(be_is_Call(call));
667         a->ent = ent;
668 }
669
670 /* Gets the call type. */
671 ir_type *be_Call_get_type(ir_node *call) {
672         const be_call_attr_t *a = get_irn_attr_const(call);
673         assert(be_is_Call(call));
674         return a->call_tp;
675 }
676
677 /* Sets the call type. */
678 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
679         be_call_attr_t *a = get_irn_attr(call);
680         assert(be_is_Call(call));
681         a->call_tp = call_tp;
682 }
683
684 void be_Call_set_pop(ir_node *call, unsigned pop) {
685         be_call_attr_t *a = get_irn_attr(call);
686         a->pop = pop;
687 }
688
689 unsigned be_Call_get_pop(const ir_node *call) {
690         const be_call_attr_t *a = get_irn_attr_const(call);
691         return a->pop;
692 }
693
694 /* Construct a new be_Return. */
695 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
696                        unsigned pop, int n, ir_node *in[])
697 {
698         be_return_attr_t *a;
699         ir_node *res;
700         int i;
701
702         res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
703         init_node_attr(res, -1);
704         for(i = 0; i < n; ++i) {
705                 add_irn_n(res, in[i]);
706                 add_register_req(res);
707         }
708
709         a = get_irn_attr(res);
710         a->num_ret_vals = n_res;
711         a->pop          = pop;
712         a->emit_pop     = 0;
713
714         return res;
715 }
716
717 /* Returns the number of real returns values */
718 int be_Return_get_n_rets(const ir_node *ret) {
719         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
720         return a->num_ret_vals;
721 }
722
723 /* return the number of bytes that should be popped from stack when executing the Return. */
724 unsigned be_Return_get_pop(const ir_node *ret) {
725         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
726         return a->pop;
727 }
728
729 /* return non-zero, if number of popped bytes must be always emitted */
730 int be_Return_get_emit_pop(const ir_node *ret) {
731         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
732         return a->emit_pop;
733 }
734
735 /* return non-zero, if number of popped bytes must be always emitted */
736 void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
737         be_return_attr_t *a = get_irn_generic_attr(ret);
738         a->emit_pop = emit_pop;
739 }
740
741 int be_Return_append_node(ir_node *ret, ir_node *node) {
742         int pos;
743
744         pos = add_irn_n(ret, node);
745         add_register_req(ret);
746
747         return pos;
748 }
749
750 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl,
751                       ir_node *old_sp, int offset, int align)
752 {
753         be_incsp_attr_t *a;
754         ir_node *irn;
755         ir_node *in[1];
756
757         in[0]     = old_sp;
758         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
759                                 sizeof(in) / sizeof(in[0]), in);
760         a         = init_node_attr(irn, 1);
761         a->offset = offset;
762         a->align  = align;
763
764         be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
765
766         /* Set output constraint to stack register. */
767         be_node_set_reg_class(irn, 0, sp->reg_class);
768         be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
769         be_node_set_irn_reg(NULL, irn, sp);
770
771         return irn;
772 }
773
774 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
775 {
776         be_node_attr_t *a;
777         ir_node *irn;
778         ir_node *in[be_pos_AddSP_last];
779         const arch_register_class_t *class;
780
781         in[be_pos_AddSP_old_sp] = old_sp;
782         in[be_pos_AddSP_size]   = sz;
783
784         irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
785         a   = init_node_attr(irn, be_pos_AddSP_last);
786
787         be_node_set_flags(irn, OUT_POS(pn_be_AddSP_sp),
788                           arch_irn_flags_ignore | arch_irn_flags_modify_sp);
789
790         /* Set output constraint to stack register. */
791         be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
792         be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
793         be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_sp), sp);
794         a->reg_data[pn_be_AddSP_sp].reg = sp;
795
796         class = arch_register_get_class(sp);
797         be_node_set_reg_class(irn, OUT_POS(pn_be_AddSP_res), class);
798
799         return irn;
800 }
801
802 ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
803 {
804         be_node_attr_t *a;
805         ir_node *irn;
806         ir_node *in[be_pos_SubSP_last];
807
808         in[be_pos_SubSP_old_sp] = old_sp;
809         in[be_pos_SubSP_size]   = sz;
810
811         irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
812         a   = init_node_attr(irn, be_pos_SubSP_last);
813
814         be_node_set_flags(irn, OUT_POS(pn_be_SubSP_sp),
815                           arch_irn_flags_ignore | arch_irn_flags_modify_sp);
816
817         /* Set output constraint to stack register. */
818         be_set_constr_single_reg(irn, be_pos_SubSP_old_sp, sp);
819         be_node_set_reg_class(irn, be_pos_SubSP_size, arch_register_get_class(sp));
820         be_set_constr_single_reg(irn, OUT_POS(pn_be_SubSP_sp), sp);
821         a->reg_data[pn_be_SubSP_sp].reg = sp;
822
823         return irn;
824 }
825
826 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
827 {
828         ir_node *res;
829         int i;
830
831         res = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, NULL);
832         init_node_attr(res, -1);
833         for(i = 0; i < n_outs; ++i)
834                 add_register_req(res);
835
836         return res;
837 }
838
839 ir_node *be_RegParams_append_out_reg(ir_node *regparams,
840                                      const arch_env_t *arch_env,
841                                      const arch_register_t *reg)
842 {
843         ir_graph *irg = get_irn_irg(regparams);
844         ir_node *block = get_nodes_block(regparams);
845         be_node_attr_t *attr = get_irn_attr(regparams);
846         const arch_register_class_t *cls = arch_register_get_class(reg);
847         ir_mode *mode = arch_register_class_mode(cls);
848         int n = ARR_LEN(attr->reg_data);
849         ir_node *proj;
850
851         assert(be_is_RegParams(regparams));
852         proj = new_r_Proj(irg, block, regparams, mode, n);
853         add_register_req(regparams);
854         be_set_constr_single_reg(regparams, BE_OUT_POS(n), reg);
855         arch_set_irn_register(arch_env, proj, reg);
856
857         /* TODO decide, whether we need to set ignore/modify sp flags here? */
858
859         return proj;
860 }
861
862 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent)
863 {
864         be_frame_attr_t *a;
865         ir_node *irn;
866         ir_node *in[1];
867
868         in[0]  = frame;
869         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
870         a      = init_node_attr(irn, 1);
871         a->ent = ent;
872         a->offset = 0;
873         be_node_set_reg_class(irn, 0, cls_frame);
874         be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
875
876         return optimize_node(irn);
877 }
878
879 ir_node *be_get_FrameAddr_frame(const ir_node *node) {
880         assert(be_is_FrameAddr(node));
881         return get_irn_n(node, be_pos_FrameAddr_ptr);
882 }
883
884 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
885 {
886         const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
887         return attr->ent;
888 }
889
890 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
891 {
892         ir_node *irn;
893         ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
894
895         in[0] = src;
896         memcpy(&in[1], in_keep, n * sizeof(in[0]));
897         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
898         init_node_attr(irn, n + 1);
899         be_node_set_reg_class(irn, OUT_POS(0), cls);
900         be_node_set_reg_class(irn, 0, cls);
901
902         return irn;
903 }
904
905 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
906 {
907         return be_new_CopyKeep(cls, irg, bl, src, 1, &keep, mode);
908 }
909
910 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
911         return get_irn_n(cpy, be_pos_CopyKeep_op);
912 }
913
914 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
915         set_irn_n(cpy, be_pos_CopyKeep_op, op);
916 }
917
918 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
919 {
920         ir_node *res;
921         int i;
922
923         res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
924         init_node_attr(res, -1);
925         for(i = 0; i < n; ++i) {
926                 add_irn_n(res, in[i]);
927                 add_register_req(res);
928         }
929
930         return res;
931 }
932
933 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
934 {
935         ir_graph *irg = get_irn_irg(barrier);
936         ir_node *block = get_nodes_block(barrier);
937         ir_mode *mode = get_irn_mode(node);
938         int n = add_irn_n(barrier, node);
939
940         ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
941         add_register_req(barrier);
942
943         return proj;
944 }
945
946 /* Construct a new be_Unwind. */
947 ir_node *be_new_Unwind(dbg_info *dbg, ir_graph *irg, ir_node *block,
948                                            ir_node *mem, ir_node *sp)
949 {
950         ir_node *res;
951         ir_node *in[2];
952
953         in[be_pos_Unwind_mem] = mem;
954         in[be_pos_Unwind_sp]  = sp;
955         res = new_ir_node(dbg, irg, block, op_be_Unwind, mode_X, 2, in);
956         init_node_attr(res, -1);
957
958         return res;
959 }
960
961 int be_is_Spill         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill          ; }
962 int be_is_Reload        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload         ; }
963 int be_is_Copy          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy           ; }
964 int be_is_CopyKeep      (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep       ; }
965 int be_is_Perm          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm           ; }
966 int be_is_MemPerm       (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm        ; }
967 int be_is_Keep          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep           ; }
968 int be_is_Call          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call           ; }
969 int be_is_Return        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return         ; }
970 int be_is_IncSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP          ; }
971 int be_is_AddSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP          ; }
972 int be_is_SubSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SubSP          ; }
973 int be_is_RegParams     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams      ; }
974 int be_is_FrameAddr     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr      ; }
975 int be_is_Barrier       (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier        ; }
976 int be_is_Unwind        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Unwind         ; }
977
978 int be_has_frame_entity(const ir_node *irn)
979 {
980         switch (be_get_irn_opcode(irn)) {
981         case beo_Spill:
982         case beo_Reload:
983         case beo_FrameAddr:
984                 return 1;
985         default:
986                 return 0;
987         }
988 }
989
990 ir_entity *be_get_frame_entity(const ir_node *irn)
991 {
992         if (be_has_frame_entity(irn)) {
993                 const be_frame_attr_t *a = get_irn_attr_const(irn);
994                 return a->ent;
995         }
996         return NULL;
997 }
998
999 int be_get_frame_offset(const ir_node *irn)
1000 {
1001         assert(is_be_node(irn));
1002         if (be_has_frame_entity(irn)) {
1003                 const be_frame_attr_t *a = get_irn_attr_const(irn);
1004                 return a->offset;
1005         }
1006         return 0;
1007 }
1008
1009 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
1010 {
1011         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1012
1013         assert(be_is_MemPerm(irn));
1014         assert(n < be_get_MemPerm_entity_arity(irn));
1015
1016         attr->in_entities[n] = ent;
1017 }
1018
1019 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
1020 {
1021         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1022
1023         assert(be_is_MemPerm(irn));
1024         assert(n < be_get_MemPerm_entity_arity(irn));
1025
1026         return attr->in_entities[n];
1027 }
1028
1029 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
1030 {
1031         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1032
1033         assert(be_is_MemPerm(irn));
1034         assert(n < be_get_MemPerm_entity_arity(irn));
1035
1036         attr->out_entities[n] = ent;
1037 }
1038
1039 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
1040 {
1041         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1042
1043         assert(be_is_MemPerm(irn));
1044         assert(n < be_get_MemPerm_entity_arity(irn));
1045
1046         return attr->out_entities[n];
1047 }
1048
1049 int be_get_MemPerm_entity_arity(const ir_node *irn)
1050 {
1051         return get_irn_arity(irn) - 1;
1052 }
1053
1054 void be_set_constr_single_reg(ir_node *node, int pos, const arch_register_t *reg)
1055 {
1056         arch_register_req_t *req = get_req(node, pos);
1057         const arch_register_class_t *cls = arch_register_get_class(reg);
1058         ir_graph *irg = get_irn_irg(node);
1059         struct obstack *obst = get_irg_obstack(irg);
1060         unsigned *limited_bitset;
1061
1062         assert(req->cls == NULL || req->cls == cls);
1063         assert(! (req->type & arch_register_req_type_limited));
1064         assert(req->limited == NULL);
1065
1066         limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
1067         rbitset_set(limited_bitset, arch_register_get_index(reg));
1068
1069         req->cls = cls;
1070         req->type |= arch_register_req_type_limited;
1071         req->limited = limited_bitset;
1072 }
1073
1074 void be_set_constr_limited(ir_node *node, int pos, const arch_register_req_t *req)
1075 {
1076         ir_graph *irg = get_irn_irg(node);
1077         struct obstack *obst = get_irg_obstack(irg);
1078         arch_register_req_t *r = get_req(node, pos);
1079
1080         assert(arch_register_req_is(req, limited));
1081         assert(! (req->type & (arch_register_req_type_should_be_same | arch_register_req_type_should_be_different)));
1082         memcpy(r, req, sizeof(r[0]));
1083         r->limited = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1084 }
1085
1086 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1087 {
1088         be_req_t *bereq = get_be_req(irn, pos);
1089         bereq->flags = flags;
1090 }
1091
1092 void be_node_add_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1093 {
1094         be_req_t *bereq = get_be_req(irn, pos);
1095         bereq->flags |= flags;
1096 }
1097
1098 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
1099 {
1100         arch_register_req_t *req = get_req(irn, pos);
1101
1102         req->cls = cls;
1103
1104         if (cls == NULL) {
1105                 req->type = arch_register_req_type_none;
1106         } else if (req->type == arch_register_req_type_none) {
1107                 req->type = arch_register_req_type_normal;
1108         }
1109 }
1110
1111 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
1112 {
1113         arch_register_req_t *req = get_req(irn, pos);
1114         req->type = type;
1115 }
1116
1117 ir_node *be_get_IncSP_pred(ir_node *irn) {
1118         assert(be_is_IncSP(irn));
1119         return get_irn_n(irn, 0);
1120 }
1121
1122 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
1123         assert(be_is_IncSP(incsp));
1124         set_irn_n(incsp, 0, pred);
1125 }
1126
1127 void be_set_IncSP_offset(ir_node *irn, int offset)
1128 {
1129         be_incsp_attr_t *a = get_irn_attr(irn);
1130         assert(be_is_IncSP(irn));
1131         a->offset = offset;
1132 }
1133
1134 int be_get_IncSP_offset(const ir_node *irn)
1135 {
1136         const be_incsp_attr_t *a = get_irn_attr_const(irn);
1137         assert(be_is_IncSP(irn));
1138         return a->offset;
1139 }
1140
1141 int be_get_IncSP_align(const ir_node *irn)
1142 {
1143         const be_incsp_attr_t *a = get_irn_attr_const(irn);
1144         assert(be_is_IncSP(irn));
1145         return a->align;
1146 }
1147
1148 ir_node *be_spill(const arch_env_t *arch_env, ir_node *block, ir_node *irn)
1149 {
1150         ir_graph                    *irg       = get_irn_irg(block);
1151         ir_node                     *frame     = get_irg_frame(irg);
1152         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
1153         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1154         ir_node                     *spill;
1155
1156         spill = be_new_Spill(cls, cls_frame, irg, block, frame, irn);
1157         return spill;
1158 }
1159
1160 ir_node *be_reload(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
1161 {
1162         ir_node  *reload;
1163         ir_node  *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
1164         ir_graph *irg   = get_irn_irg(bl);
1165         ir_node  *frame = get_irg_frame(irg);
1166         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1167
1168         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1169
1170         reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
1171
1172         if (is_Block(insert)) {
1173                 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
1174                 sched_add_after(insert, reload);
1175         } else {
1176                 sched_add_before(insert, reload);
1177         }
1178
1179         return reload;
1180 }
1181
1182 /*
1183   ____              ____
1184  |  _ \ ___  __ _  |  _ \ ___  __ _ ___
1185  | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1186  |  _ <  __/ (_| | |  _ <  __/ (_| \__ \
1187  |_| \_\___|\__, | |_| \_\___|\__, |___/
1188             |___/                |_|
1189
1190 */
1191
1192
1193 static const
1194 arch_register_req_t *get_out_reg_req(const ir_node *irn, int out_pos)
1195 {
1196         const be_node_attr_t *a = get_irn_attr_const(irn);
1197
1198         if(out_pos >= ARR_LEN(a->reg_data)) {
1199                 return arch_no_register_req;
1200         }
1201
1202         return &a->reg_data[out_pos].req.req;
1203 }
1204
1205 static const
1206 arch_register_req_t *get_in_reg_req(const ir_node *irn, int pos)
1207 {
1208         const be_node_attr_t *a = get_irn_attr_const(irn);
1209
1210         if(pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1211                 return arch_no_register_req;
1212
1213         return &a->reg_data[pos].in_req.req;
1214 }
1215
1216 static const arch_register_req_t *
1217 be_node_get_irn_reg_req(const void *self, const ir_node *irn, int pos)
1218 {
1219         int out_pos = pos;
1220
1221         (void) self;
1222         if (pos < 0) {
1223                 if (get_irn_mode(irn) == mode_T)
1224                         return arch_no_register_req;
1225
1226                 out_pos = redir_proj((const ir_node **)&irn);
1227                 assert(is_be_node(irn));
1228                 return get_out_reg_req(irn, out_pos);
1229         } else if (is_be_node(irn)) {
1230                 /*
1231                  * For spills and reloads, we return "none" as requirement for frame
1232                  * pointer, so every input is ok. Some backends need this (e.g. STA).
1233                  */
1234                 if ((be_is_Spill(irn)  && pos == be_pos_Spill_frame) ||
1235                                 (be_is_Reload(irn) && pos == be_pos_Reload_frame))
1236                         return arch_no_register_req;
1237
1238                 return get_in_reg_req(irn, pos);
1239         }
1240
1241         return arch_no_register_req;
1242 }
1243
1244 const arch_register_t *
1245 be_node_get_irn_reg(const void *self, const ir_node *irn)
1246 {
1247         be_reg_data_t *r;
1248
1249         (void) self;
1250         if (get_irn_mode(irn) == mode_T)
1251                 return NULL;
1252         r = retrieve_reg_data(irn);
1253         return r->reg;
1254 }
1255
1256 static arch_irn_class_t be_node_classify(const void *self, const ir_node *irn)
1257 {
1258         redir_proj((const ir_node **) &irn);
1259
1260         (void) self;
1261         switch(be_get_irn_opcode(irn)) {
1262 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1263                 XXX(Spill, spill);
1264                 XXX(Reload, reload);
1265                 XXX(Perm, perm);
1266                 XXX(Copy, copy);
1267                 XXX(Return, branch);
1268 #undef XXX
1269                 default:
1270                 return arch_irn_class_normal;
1271         }
1272
1273         return 0;
1274 }
1275
1276 static arch_irn_flags_t be_node_get_flags(const void *self, const ir_node *node)
1277 {
1278         be_req_t *bereq;
1279         int pos = -1;
1280         (void) self;
1281
1282         if(is_Proj(node)) {
1283                 pos = OUT_POS(get_Proj_proj(node));
1284                 node = skip_Proj_const(node);
1285         }
1286
1287         bereq = get_be_req(node, pos);
1288
1289         return bereq->flags;
1290 }
1291
1292 static ir_entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1293 {
1294         (void) self;
1295         return be_get_frame_entity(irn);
1296 }
1297
1298 static void be_node_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent)
1299 {
1300         be_frame_attr_t *a;
1301         (void) self;
1302
1303         assert(be_has_frame_entity(irn));
1304
1305         a = get_irn_attr(irn);
1306         a->ent = ent;
1307 }
1308
1309 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1310 {
1311         (void) self;
1312         if(be_has_frame_entity(irn)) {
1313                 be_frame_attr_t *a = get_irn_attr(irn);
1314                 a->offset = offset;
1315         }
1316 }
1317
1318 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1319 {
1320         (void) self;
1321         if(be_is_IncSP(irn))
1322                 return be_get_IncSP_offset(irn);
1323         if(be_is_Call(irn))
1324                 return -(int)be_Call_get_pop(irn);
1325
1326         return 0;
1327 }
1328
1329 /*
1330   ___ ____  _   _   _   _                 _ _
1331  |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1332   | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1333   | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1334  |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1335
1336 */
1337
1338 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1339         be_node_get_irn_reg_req,
1340         be_node_set_irn_reg,
1341         be_node_get_irn_reg,
1342         be_node_classify,
1343         be_node_get_flags,
1344         be_node_get_frame_entity,
1345         be_node_set_frame_entity,
1346         be_node_set_frame_offset,
1347         be_node_get_sp_bias,
1348         NULL,    /* get_inverse             */
1349         NULL,    /* get_op_estimated_cost   */
1350         NULL,    /* possible_memory_operand */
1351         NULL,    /* perform_memory_operand  */
1352 };
1353
1354 static const arch_irn_ops_t be_node_irn_ops = {
1355         &be_node_irn_ops_if
1356 };
1357
1358 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1359 {
1360         redir_proj((const ir_node **) &irn);
1361         (void) self;
1362         return is_be_node(irn) ? &be_node_irn_ops : NULL;
1363 }
1364
1365 const arch_irn_handler_t be_node_irn_handler = {
1366         be_node_get_irn_ops
1367 };
1368
1369 /*
1370   ____  _     _   ___ ____  _   _   _   _                 _ _
1371  |  _ \| |__ (_) |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1372  | |_) | '_ \| |  | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1373  |  __/| | | | |  | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1374  |_|   |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1375
1376 */
1377
1378 typedef struct {
1379         const arch_register_t *reg;
1380         arch_register_req_t    req;
1381         arch_irn_flags_t       flags;
1382 } phi_attr_t;
1383
1384 typedef struct {
1385         arch_irn_handler_t irn_handler;
1386         arch_irn_ops_t     irn_ops;
1387         const arch_env_t   *arch_env;
1388         pmap               *phi_attrs;
1389 } phi_handler_t;
1390
1391 #define get_phi_handler_from_handler(h)  container_of(h, phi_handler_t, irn_handler)
1392 #define get_phi_handler_from_ops(h)      container_of(h, phi_handler_t, irn_ops)
1393
1394 static
1395 const void *phi_get_irn_ops(const arch_irn_handler_t *handler,
1396                             const ir_node *irn)
1397 {
1398         const phi_handler_t *h;
1399         if(!is_Phi(irn) || !mode_is_datab(get_irn_mode(irn)))
1400                 return NULL;
1401
1402         h = get_phi_handler_from_handler(handler);
1403         return &h->irn_ops;
1404 }
1405
1406 static INLINE
1407 phi_attr_t *get_Phi_attr(const phi_handler_t *handler, const ir_node *phi)
1408 {
1409         phi_attr_t *attr = pmap_get(handler->phi_attrs, (void*) phi);
1410         if(attr == NULL) {
1411                 ir_graph *irg = get_irn_irg(phi);
1412                 struct obstack *obst = get_irg_obstack(irg);
1413                 attr = obstack_alloc(obst, sizeof(attr[0]));
1414                 memset(attr, 0, sizeof(attr[0]));
1415                 pmap_insert(handler->phi_attrs, phi, attr);
1416         }
1417
1418         return attr;
1419 }
1420
1421 /**
1422  * Get register class of a Phi.
1423  */
1424 static
1425 const arch_register_req_t *get_Phi_reg_req_recursive(const phi_handler_t *h,
1426                                                      const ir_node *phi,
1427                                                      pset **visited)
1428 {
1429         int n = get_irn_arity(phi);
1430         ir_node *op;
1431         int i;
1432
1433         if(*visited && pset_find_ptr(*visited, phi))
1434                 return NULL;
1435
1436         for(i = 0; i < n; ++i) {
1437                 op = get_irn_n(phi, i);
1438                 /* Matze: don't we unnecessary constraint our phis with this?
1439                  * we only need to take the regclass IMO*/
1440                 if(!is_Phi(op))
1441                         return arch_get_register_req(h->arch_env, op, BE_OUT_POS(0));
1442         }
1443
1444         /*
1445          * The operands of that Phi were all Phis themselves.
1446          * We have to start a DFS for a non-Phi argument now.
1447          */
1448         if(!*visited)
1449                 *visited = pset_new_ptr(16);
1450
1451         pset_insert_ptr(*visited, phi);
1452
1453         for(i = 0; i < n; ++i) {
1454                 const arch_register_req_t *req;
1455                 op = get_irn_n(phi, i);
1456                 req = get_Phi_reg_req_recursive(h, op, visited);
1457                 if(req != NULL)
1458                         return req;
1459         }
1460
1461         return NULL;
1462 }
1463
1464 static
1465 const arch_register_req_t *phi_get_irn_reg_req(const void *self,
1466                                                const ir_node *irn, int pos)
1467 {
1468         phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1469         phi_attr_t *attr;
1470         (void) self;
1471         (void) pos;
1472
1473         if(!mode_is_datab(get_irn_mode(irn)))
1474                 return arch_no_register_req;
1475
1476         attr = get_Phi_attr(phi_handler, irn);
1477
1478         if(attr->req.type == arch_register_req_type_none) {
1479                 pset *visited = NULL;
1480                 const arch_register_req_t *req;
1481                 req = get_Phi_reg_req_recursive(phi_handler, irn, &visited);
1482
1483                 memcpy(&attr->req, req, sizeof(req[0]));
1484                 assert(attr->req.cls != NULL);
1485                 attr->req.type = arch_register_req_type_normal;
1486
1487                 if(visited != NULL)
1488                         del_pset(visited);
1489         }
1490
1491         return &attr->req;
1492 }
1493
1494 void be_set_phi_reg_req(const arch_env_t *arch_env, ir_node *node,
1495                         const arch_register_req_t *req)
1496 {
1497         const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1498         const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1499         phi_attr_t *attr;
1500
1501         assert(mode_is_datab(get_irn_mode(node)));
1502
1503         attr = get_Phi_attr(handler, node);
1504         memcpy(&attr->req, req, sizeof(req[0]));
1505 }
1506
1507 void be_set_phi_flags(const arch_env_t *arch_env, ir_node *node,
1508                       arch_irn_flags_t flags)
1509 {
1510         const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1511         const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1512         phi_attr_t *attr;
1513
1514         assert(mode_is_datab(get_irn_mode(node)));
1515
1516         attr = get_Phi_attr(handler, node);
1517         attr->flags = flags;
1518 }
1519
1520 static
1521 void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1522 {
1523         phi_handler_t *h = get_phi_handler_from_ops(self);
1524         phi_attr_t *attr = get_Phi_attr(h, irn);
1525         attr->reg = reg;
1526 }
1527
1528 static
1529 const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1530 {
1531         phi_handler_t *h = get_phi_handler_from_ops(self);
1532         phi_attr_t *attr = get_Phi_attr(h, irn);
1533         return attr->reg;
1534 }
1535
1536 static
1537 arch_irn_class_t phi_classify(const void *self, const ir_node *irn)
1538 {
1539         (void) self;
1540         (void) irn;
1541         return arch_irn_class_normal;
1542 }
1543
1544 static
1545 arch_irn_flags_t phi_get_flags(const void *self, const ir_node *irn)
1546 {
1547         phi_handler_t *h = get_phi_handler_from_ops(self);
1548         phi_attr_t *attr = get_Phi_attr(h, irn);
1549         (void) self;
1550         return attr->flags;
1551 }
1552
1553 static
1554 ir_entity *phi_get_frame_entity(const void *self, const ir_node *irn)
1555 {
1556         (void) self;
1557         (void) irn;
1558         return NULL;
1559 }
1560
1561 static
1562 void phi_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent)
1563 {
1564         (void) self;
1565         (void) irn;
1566         (void) ent;
1567         assert(0);
1568 }
1569
1570 static
1571 void phi_set_frame_offset(const void *self, ir_node *irn, int bias)
1572 {
1573         (void) self;
1574         (void) irn;
1575         (void) bias;
1576         assert(0);
1577 }
1578
1579 static
1580 int phi_get_sp_bias(const void* self, const ir_node *irn)
1581 {
1582         (void) self;
1583         (void) irn;
1584         return 0;
1585 }
1586
1587 static
1588 const arch_irn_ops_if_t phi_irn_ops = {
1589         phi_get_irn_reg_req,
1590         phi_set_irn_reg,
1591         phi_get_irn_reg,
1592         phi_classify,
1593         phi_get_flags,
1594         phi_get_frame_entity,
1595         phi_set_frame_entity,
1596         phi_set_frame_offset,
1597         phi_get_sp_bias,
1598         NULL,    /* get_inverse             */
1599         NULL,    /* get_op_estimated_cost   */
1600         NULL,    /* possible_memory_operand */
1601         NULL,    /* perform_memory_operand  */
1602 };
1603
1604 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1605 {
1606         phi_handler_t *h           = xmalloc(sizeof(h[0]));
1607         h->irn_handler.get_irn_ops = phi_get_irn_ops;
1608         h->irn_ops.impl            = &phi_irn_ops;
1609         h->arch_env                = arch_env;
1610         h->phi_attrs               = pmap_create();
1611         return (arch_irn_handler_t *) h;
1612 }
1613
1614 void be_phi_handler_free(arch_irn_handler_t *handler)
1615 {
1616         phi_handler_t *h = get_phi_handler_from_handler(handler);
1617         pmap_destroy(h->phi_attrs);
1618         h->phi_attrs = NULL;
1619         free(handler);
1620 }
1621
1622 void be_phi_handler_reset(arch_irn_handler_t *handler)
1623 {
1624         phi_handler_t *h = get_phi_handler_from_handler(handler);
1625         if(h->phi_attrs)
1626                 pmap_destroy(h->phi_attrs);
1627         h->phi_attrs = pmap_create();
1628 }
1629
1630 /*
1631   _   _           _        ____                        _
1632  | \ | | ___   __| | ___  |  _ \ _   _ _ __ ___  _ __ (_)_ __   __ _
1633  |  \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1634  | |\  | (_) | (_| |  __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1635  |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1636                                                 |_|            |___/
1637 */
1638
1639 /**
1640  * Dumps a register requirement to a file.
1641  */
1642 static void dump_node_req(FILE *f, int idx, const arch_register_req_t *req,
1643                           const ir_node *node)
1644 {
1645         int did_something = 0;
1646         char buf[16];
1647         const char *prefix = buf;
1648
1649         snprintf(buf, sizeof(buf), "#%d ", idx);
1650         buf[sizeof(buf) - 1] = '\0';
1651
1652         if(req->cls != 0) {
1653                 char tmp[256];
1654                 fprintf(f, prefix);
1655                 arch_register_req_format(tmp, sizeof(tmp), req, node);
1656                 fprintf(f, "%s", tmp);
1657                 did_something = 1;
1658         }
1659
1660         if(did_something)
1661                 fprintf(f, "\n");
1662 }
1663
1664 /**
1665  * Dumps node register requirements to a file.
1666  */
1667 static void dump_node_reqs(FILE *f, ir_node *node)
1668 {
1669         int i;
1670         be_node_attr_t *a = get_irn_attr(node);
1671         int len = ARR_LEN(a->reg_data);
1672
1673         fprintf(f, "registers: \n");
1674         for(i = 0; i < len; ++i) {
1675                 be_reg_data_t *rd = &a->reg_data[i];
1676                 if(rd->reg)
1677                         fprintf(f, "#%d: %s\n", i, rd->reg->name);
1678         }
1679
1680         fprintf(f, "in requirements:\n");
1681         for(i = 0; i < len; ++i) {
1682                 dump_node_req(f, i, &a->reg_data[i].in_req.req, node);
1683         }
1684
1685         fprintf(f, "\nout requirements:\n");
1686         for(i = 0; i < len; ++i) {
1687                 dump_node_req(f, i, &a->reg_data[i].req.req, node);
1688         }
1689 }
1690
1691 /**
1692  * ir_op-Operation: dump a be node to file
1693  */
1694 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1695 {
1696         be_node_attr_t *at = get_irn_attr(irn);
1697
1698         assert(is_be_node(irn));
1699
1700         switch(reason) {
1701                 case dump_node_opcode_txt:
1702                         fprintf(f, get_op_name(get_irn_op(irn)));
1703                         break;
1704                 case dump_node_mode_txt:
1705                         if(be_is_Perm(irn) || be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1706                                 fprintf(f, " %s", get_mode_name(get_irn_mode(irn)));
1707                         }
1708                         break;
1709                 case dump_node_nodeattr_txt:
1710                         if(be_is_Call(irn)) {
1711                                 be_call_attr_t *a = (be_call_attr_t *) at;
1712                                 if (a->ent)
1713                                         fprintf(f, " [%s] ", get_entity_name(a->ent));
1714                         }
1715                         if(be_is_IncSP(irn)) {
1716                                 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1717                                 if(attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1718                                         fprintf(f, " [Setup Stackframe] ");
1719                                 } else if(attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1720                                         fprintf(f, " [Destroy Stackframe] ");
1721                                 } else {
1722                                         fprintf(f, " [%d] ", attr->offset);
1723                                 }
1724                         }
1725                         break;
1726                 case dump_node_info_txt:
1727                         dump_node_reqs(f, irn);
1728
1729                         if(be_has_frame_entity(irn)) {
1730                                 be_frame_attr_t *a = (be_frame_attr_t *) at;
1731                                 if (a->ent) {
1732                                         unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1733                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1734                                           a->ent, a->offset, a->offset, size, size);
1735                                 }
1736
1737                         }
1738
1739                         switch(be_get_irn_opcode(irn)) {
1740                         case beo_IncSP:
1741                                 {
1742                                         be_incsp_attr_t *a = (be_incsp_attr_t *) at;
1743                                         if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1744                                                 fprintf(f, "offset: FRAME_SIZE\n");
1745                                         else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1746                                                 fprintf(f, "offset: -FRAME SIZE\n");
1747                                         else
1748                                                 fprintf(f, "offset: %u\n", a->offset);
1749                                 }
1750                                 break;
1751                         case beo_Call:
1752                                 {
1753                                         be_call_attr_t *a = (be_call_attr_t *) at;
1754
1755                                         if (a->ent)
1756                                                 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1757                                 }
1758                                 break;
1759                         case beo_MemPerm:
1760                                 {
1761                                         int i;
1762                                         for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1763                                                 ir_entity *in, *out;
1764                                                 in = be_get_MemPerm_in_entity(irn, i);
1765                                                 out = be_get_MemPerm_out_entity(irn, i);
1766                                                 if(in) {
1767                                                         fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1768                                                 }
1769                                                 if(out) {
1770                                                         fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1771                                                 }
1772                                         }
1773                                 }
1774                                 break;
1775
1776                         default:
1777                                 break;
1778                         }
1779         }
1780
1781         return 0;
1782 }
1783
1784 /**
1785  * ir_op-Operation:
1786  * Copies the backend specific attributes from old node to new node.
1787  */
1788 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1789 {
1790         const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1791         be_node_attr_t *new_attr = get_irn_attr(new_node);
1792         struct obstack *obst = get_irg_obstack(get_irn_irg(new_node));
1793         unsigned i, len;
1794
1795         assert(is_be_node(old_node));
1796         assert(is_be_node(new_node));
1797
1798         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1799         new_attr->reg_data = NULL;
1800
1801         if(old_attr->reg_data != NULL)
1802                 len = ARR_LEN(old_attr->reg_data);
1803         else
1804                 len = 0;
1805
1806         if(get_irn_op(old_node)->opar == oparity_dynamic
1807                         || be_is_RegParams(old_node)) {
1808                 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, len);
1809         } else {
1810                 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, len);
1811         }
1812
1813         if(len > 0) {
1814                 memcpy(new_attr->reg_data, old_attr->reg_data, len * sizeof(be_reg_data_t));
1815                 for(i = 0; i < len; ++i) {
1816                         const be_reg_data_t *rd = &old_attr->reg_data[i];
1817                         be_reg_data_t *newrd = &new_attr->reg_data[i];
1818                         if(arch_register_req_is(&rd->req.req, limited)) {
1819                                 const arch_register_req_t *req = &rd->req.req;
1820                                 arch_register_req_t *new_req = &newrd->req.req;
1821                                 new_req->limited
1822                                         = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1823                         }
1824                         if(arch_register_req_is(&rd->in_req.req, limited)) {
1825                                 const arch_register_req_t *req = &rd->in_req.req;
1826                                 arch_register_req_t *new_req = &newrd->in_req.req;
1827                                 new_req->limited
1828                                         = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1829                         }
1830                 }
1831         }
1832 }
1833
1834 static const ir_op_ops be_node_op_ops = {
1835         NULL,
1836         NULL,
1837         NULL,
1838         NULL,
1839         NULL,
1840         copy_attr,
1841         NULL,
1842         NULL,
1843         NULL,
1844         NULL,
1845         NULL,
1846         dump_node,
1847         NULL
1848 };