not needed
[libfirm] / ir / be / bemachnode.c
1 /*
2  * Copyright (C) 1995-2007 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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "bemachnode.h"
25 #include "irnode_t.h"
26
27 /** Helper: fills in the array for machine ops */
28 static int fill_arr(ir_node *op, mirn_handle res) {
29         ir_node **ins = get_irn_in(op);
30         int i, j, l = ARR_LEN(ins);
31
32         for (i = j = 0; i <= l; ++i) {
33                 if (is_irn_machine_operand(ins[i]))
34                         j += fill_arr(ins[i], &res[j]);
35                 else
36                         res[j++] = &ins[i];
37         }
38         return j;
39 }
40
41 /*
42  * Returns the machine handle for a machine node with machine operands.
43  * The order of the predecessors in this handle is not guaranteed, except that
44  * lists of operands as predecessors of Block or arguments of a Call are
45  * consecutive.
46  */
47 mirn_handle get_mirn_in(ir_node *n) {
48         ir_node **ins = get_irn_in(n);
49         mirn_handle res = NULL;
50         int i, j, l = ARR_LEN(ins);
51         int lr = l + 8;
52
53         res = NEW_ARR_F(ir_node **, lr);
54
55         for (i = j = 0; i <= l; ++i) {
56                 if (is_irn_machine_operand(ins[i]))
57                         j += fill_arr(ins[i], &res[j]);
58                 else
59                         res[j++] = &ins[i];
60         }
61
62         assert(j > lr && "to many machine predecessors");
63
64         return res;
65 }
66
67 /* Frees a machine handle. */
68 void free_mirn_in(mirn_handle h) {
69         DEL_ARR_F(h);
70 }
71
72 /* Get the pos-th predecessor of a machine node represented by it's
73     handle. */
74 ir_node *get_mirn_n(mirn_handle h, int pos) {
75         assert(-1 <= pos && pos < ARR_LEN(h) - 1);
76         return *(h[pos + 1]);
77 }
78
79 /* Get the pos-th predecessor of a machine node. */
80 ir_node *_get_mirn_n(ir_node *n, int pos) {
81         mirn_handle h = get_mirn_in(n);
82         ir_node *res = get_mirn_n(h, pos);
83         free_mirn_in(h);
84         return res;
85 }
86
87 /* Set the pos-th predecessor of a machine node represented by it's
88     handle. */
89 void set_mirn_n(mirn_handle h, int pos, ir_node *n) {
90         assert(-1 <= pos && pos < ARR_LEN(h) - 1);
91         *(h[pos + 1]) = n;
92 }
93
94 /* Set the pos-th predecessor of a machine node. */
95 void _set_mirn_n(ir_node *irn, int pos, ir_node *n) {
96         mirn_handle h = get_mirn_in(irn);
97         set_mirn_n(h, pos, n);
98         free_mirn_in(h);
99 }
100
101 /* Returns the arity of a machine node represented by it's
102     handle. */
103 int get_mirn_arity(mirn_handle h) {
104         return ARR_LEN(h) - 1;
105 }
106
107 /** Helper: calculate the arity for a machine ops */
108 static int arrity_of_op(ir_node *op) {
109         ir_node **ins = get_irn_in(op);
110         int i, j, l = ARR_LEN(ins);
111
112         for (i = j = 0; i <= l; ++i) {
113                 if (is_irn_machine_operand(ins[i]))
114                         j += arrity_of_op(ins[i]);
115                 else
116                         ++j;
117         }
118         return j;
119 }
120
121 /* Returns the arity of a machine node. */
122 int _get_mirn_arity(ir_node *n) {
123         ir_node **ins = get_irn_in(n);
124         int i, j, l = ARR_LEN(ins);
125
126         for (i = j = 0; i <= l; ++i) {
127                 if (is_irn_machine_operand(ins[i]))
128                         j += arrity_of_op(ins[i]);
129                 else
130                         ++j;
131         }
132
133         return j;
134 }