emit arm symconsts as .LC%u instead of .L%u to avoid clashed with block numbers
[libfirm] / ir / be / TEMPLATE / TEMPLATE_emitter.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   emit assembler for a backend graph
23  * @version $Id$
24  */
25 #include "config.h"
26
27 #include <limits.h>
28
29 #include "xmalloc.h"
30 #include "tv.h"
31 #include "iredges.h"
32 #include "debug.h"
33 #include "irgwalk.h"
34 #include "irprintf.h"
35 #include "irop_t.h"
36 #include "irargs_t.h"
37 #include "irprog.h"
38
39 #include "../besched.h"
40 #include "../begnuas.h"
41 #include "../beblocksched.h"
42 #include "../benode.h"
43
44 #include "TEMPLATE_emitter.h"
45 #include "gen_TEMPLATE_emitter.h"
46 #include "TEMPLATE_nodes_attr.h"
47 #include "TEMPLATE_new_nodes.h"
48
49 #define SNPRINTF_BUF_LEN 128
50
51 /**
52  * Returns the register at in position pos.
53  */
54 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
55 {
56         ir_node                *op;
57         const arch_register_t  *reg = NULL;
58
59         assert(get_irn_arity(node) > pos && "Invalid IN position");
60
61         /* The out register of the operator at position pos is the
62            in register we need. */
63         op = get_irn_n(node, pos);
64
65         reg = arch_get_irn_register(op);
66
67         assert(reg && "no in register found");
68         return reg;
69 }
70
71 /**
72  * Returns the register at out position pos.
73  */
74 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
75 {
76         ir_node                *proj;
77         const arch_register_t  *reg = NULL;
78
79         /* 1st case: irn is not of mode_T, so it has only                 */
80         /*           one OUT register -> good                             */
81         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
82         /*           Proj with the corresponding projnum for the register */
83
84         if (get_irn_mode(node) != mode_T) {
85                 reg = arch_get_irn_register(node);
86         } else if (is_TEMPLATE_irn(node)) {
87                 reg = arch_irn_get_register(node, pos);
88         } else {
89                 const ir_edge_t *edge;
90
91                 foreach_out_edge(node, edge) {
92                         proj = get_edge_src_irn(edge);
93                         assert(is_Proj(proj) && "non-Proj from mode_T node");
94                         if (get_Proj_proj(proj) == pos) {
95                                 reg = arch_get_irn_register(proj);
96                                 break;
97                         }
98                 }
99         }
100
101         assert(reg && "no out register found");
102         return reg;
103 }
104
105
106
107 void TEMPLATE_emit_immediate(const ir_node *node)
108 {
109         const TEMPLATE_attr_t *attr = get_irn_generic_attr_const(node);
110         be_emit_tarval(attr->value);
111 }
112
113 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
114 {
115         const arch_register_t *reg = get_in_reg(node, pos);
116         be_emit_string(arch_register_get_name(reg));
117 }
118
119 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
120 {
121         const arch_register_t *reg = get_out_reg(node, pos);
122         be_emit_string(arch_register_get_name(reg));
123 }
124
125 /**
126  * Returns the target label for a control flow node.
127  */
128 static void TEMPLATE_emit_cfop_target(const ir_node *node)
129 {
130         ir_node *block = get_irn_link(node);
131         be_gas_emit_block_name(block);
132 }
133
134
135
136 /**
137  * Emits code for a unconditional jump.
138  */
139 static void emit_TEMPLATE_Jmp(const ir_node *node)
140 {
141         be_emit_cstring("\tjmp ");
142         TEMPLATE_emit_cfop_target(node);
143         be_emit_finish_line_gas(node);
144 }
145
146 static void emit_be_IncSP(const ir_node *node)
147 {
148         int offset = be_get_IncSP_offset(node);
149
150         if (offset == 0)
151                 return;
152
153         /* downwards growing stack */
154         if (offset > 0) {
155                 be_emit_cstring("\tsub ");
156         } else {
157                 be_emit_cstring("\tadd ");
158                 offset = -offset;
159         }
160
161         TEMPLATE_emit_source_register(node, 0);
162         be_emit_irprintf(", %d, ", offset);
163         TEMPLATE_emit_dest_register(node, 0);
164         be_emit_finish_line_gas(node);
165 }
166
167 static void emit_be_Return(const ir_node *node)
168 {
169         be_emit_cstring("\tret");
170         be_emit_finish_line_gas(node);
171 }
172
173 static void emit_nothing(const ir_node *node)
174 {
175         (void) node;
176 }
177
178 /**
179  * The type of a emitter function.
180  */
181 typedef void (emit_func)(const ir_node *node);
182
183 static inline void set_emitter(ir_op *op, emit_func func)
184 {
185         op->ops.generic = (op_func)func;
186 }
187
188 /**
189  * Enters the emitter functions for handled nodes into the generic
190  * pointer of an opcode.
191  */
192 static void TEMPLATE_register_emitters(void)
193 {
194         /* first clear the generic function pointer for all ops */
195         clear_irp_opcodes_generic_func();
196
197         /* register all emitter functions defined in spec */
198         TEMPLATE_register_spec_emitters();
199
200         /* custom emitters not provided by the spec */
201         set_emitter(op_TEMPLATE_Jmp,   emit_TEMPLATE_Jmp);
202         set_emitter(op_be_Return,      emit_be_Return);
203         set_emitter(op_be_IncSP,       emit_be_IncSP);
204
205         /* no need to emit anything for the following nodes */
206         set_emitter(op_Phi,            emit_nothing);
207         set_emitter(op_be_Keep,        emit_nothing);
208         set_emitter(op_be_Start,       emit_nothing);
209         set_emitter(op_be_Barrier,     emit_nothing);
210 }
211
212 typedef void (*emit_func_ptr) (const ir_node *);
213
214 /**
215  * Emits code for a node.
216  */
217 static void TEMPLATE_emit_node(const ir_node *node)
218 {
219         ir_op               *op       = get_irn_op(node);
220
221         if (op->ops.generic) {
222                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
223                 (*func) (node);
224         } else {
225                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
226         }
227 }
228
229 /**
230  * Walks over the nodes in a block connected by scheduling edges
231  * and emits code for each node.
232  */
233 static void TEMPLATE_emit_block(ir_node *block)
234 {
235         ir_node *node;
236
237         be_gas_emit_block_name(block);
238         be_emit_cstring(":\n");
239         be_emit_write_line();
240
241         sched_foreach(block, node) {
242                 TEMPLATE_emit_node(node);
243         }
244 }
245
246 /**
247  * Sets labels for control flow nodes (jump target)
248  */
249 static void TEMPLATE_gen_labels(ir_node *block, void *env)
250 {
251         ir_node *pred;
252         int n = get_Block_n_cfgpreds(block);
253         (void) env;
254
255         for (n--; n >= 0; n--) {
256                 pred = get_Block_cfgpred(block, n);
257                 set_irn_link(pred, block);
258         }
259 }
260
261 /**
262  * Main driver
263  */
264 void TEMPLATE_emit_routine(ir_graph *irg)
265 {
266         ir_node   **block_schedule;
267         ir_entity  *entity = get_irg_entity(irg);
268         int         i;
269         int         n;
270
271         /* register all emitter functions */
272         TEMPLATE_register_emitters();
273
274         /* create the block schedule */
275         block_schedule = be_create_block_schedule(irg);
276
277         /* emit assembler prolog */
278         be_gas_emit_function_prolog(entity, 4);
279
280         /* populate jump link fields with their destinations */
281         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
282
283         n = ARR_LEN(block_schedule);
284         for (i = 0; i < n; ++i) {
285                 ir_node *block = block_schedule[i];
286                 TEMPLATE_emit_block(block);
287         }
288         be_gas_emit_function_epilog(entity);
289 }