remove remaining WITH_ILP uses, fix bug introduced when adapting lpp
[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 "gen_TEMPLATE_regalloc_if.h"
47 #include "TEMPLATE_nodes_attr.h"
48 #include "TEMPLATE_new_nodes.h"
49
50 #define SNPRINTF_BUF_LEN 128
51
52 /**
53  * Returns the register at in position pos.
54  */
55 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
56 {
57         ir_node                *op;
58         const arch_register_t  *reg = NULL;
59
60         assert(get_irn_arity(node) > pos && "Invalid IN position");
61
62         /* The out register of the operator at position pos is the
63            in register we need. */
64         op = get_irn_n(node, pos);
65
66         reg = arch_get_irn_register(op);
67
68         assert(reg && "no in register found");
69         return reg;
70 }
71
72 /**
73  * Returns the register at out position pos.
74  */
75 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
76 {
77         ir_node                *proj;
78         const arch_register_t  *reg = NULL;
79
80         /* 1st case: irn is not of mode_T, so it has only                 */
81         /*           one OUT register -> good                             */
82         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
83         /*           Proj with the corresponding projnum for the register */
84
85         if (get_irn_mode(node) != mode_T) {
86                 reg = arch_get_irn_register(node);
87         } else if (is_TEMPLATE_irn(node)) {
88                 reg = arch_irn_get_register(node, pos);
89         } else {
90                 const ir_edge_t *edge;
91
92                 foreach_out_edge(node, edge) {
93                         proj = get_edge_src_irn(edge);
94                         assert(is_Proj(proj) && "non-Proj from mode_T node");
95                         if (get_Proj_proj(proj) == pos) {
96                                 reg = arch_get_irn_register(proj);
97                                 break;
98                         }
99                 }
100         }
101
102         assert(reg && "no out register found");
103         return reg;
104 }
105
106
107
108 void TEMPLATE_emit_immediate(const ir_node *node)
109 {
110         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
111         be_emit_tarval(attr->value);
112 }
113
114 static void emit_register(const arch_register_t *reg)
115 {
116         be_emit_string(arch_register_get_name(reg));
117 }
118
119 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
120 {
121         const arch_register_t *reg = get_in_reg(node, pos);
122         emit_register(reg);
123 }
124
125 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
126 {
127         const arch_register_t *reg = get_out_reg(node, pos);
128         emit_register(reg);
129 }
130
131 /**
132  * Returns the target label for a control flow node.
133  */
134 static void TEMPLATE_emit_cfop_target(const ir_node *node)
135 {
136         ir_node *block = (ir_node*)get_irn_link(node);
137         be_gas_emit_block_name(block);
138 }
139
140
141
142 /**
143  * Emits code for a unconditional jump.
144  */
145 static void emit_TEMPLATE_Jmp(const ir_node *node)
146 {
147         be_emit_cstring("\tjmp ");
148         TEMPLATE_emit_cfop_target(node);
149         be_emit_finish_line_gas(node);
150 }
151
152 static void emit_be_IncSP(const ir_node *node)
153 {
154         int offset = be_get_IncSP_offset(node);
155
156         if (offset == 0)
157                 return;
158
159         /* downwards growing stack */
160         if (offset > 0) {
161                 be_emit_cstring("\tsub ");
162         } else {
163                 be_emit_cstring("\tadd ");
164                 offset = -offset;
165         }
166
167         TEMPLATE_emit_source_register(node, 0);
168         be_emit_irprintf(", %d, ", offset);
169         TEMPLATE_emit_dest_register(node, 0);
170         be_emit_finish_line_gas(node);
171 }
172
173 static void emit_be_Start(const ir_node *node)
174 {
175         ir_graph *irg        = get_irn_irg(node);
176         ir_type  *frame_type = get_irg_frame_type(irg);
177         unsigned  size       = get_type_size_bytes(frame_type);
178
179         /* emit function prolog */
180
181         /* allocate stackframe */
182         if (size > 0) {
183                 be_emit_cstring("\tsub ");
184                 emit_register(&TEMPLATE_registers[REG_SP]);
185                 be_emit_irprintf(", %u, ", size);
186                 emit_register(&TEMPLATE_registers[REG_SP]);
187                 be_emit_finish_line_gas(node);
188         }
189 }
190
191 static void emit_be_Return(const ir_node *node)
192 {
193         ir_graph *irg        = get_irn_irg(node);
194         ir_type  *frame_type = get_irg_frame_type(irg);
195         unsigned  size       = get_type_size_bytes(frame_type);
196
197         /* emit function epilog here */
198
199         /* deallocate stackframe */
200         if (size > 0) {
201                 be_emit_cstring("\tadd ");
202                 emit_register(&TEMPLATE_registers[REG_SP]);
203                 be_emit_irprintf(", %u, ", size);
204                 emit_register(&TEMPLATE_registers[REG_SP]);
205                 be_emit_finish_line_gas(node);
206         }
207
208         /* return */
209         be_emit_cstring("\tret");
210         be_emit_finish_line_gas(node);
211 }
212
213 static void emit_nothing(const ir_node *node)
214 {
215         (void) node;
216 }
217
218 /**
219  * The type of a emitter function.
220  */
221 typedef void (emit_func)(const ir_node *node);
222
223 static inline void set_emitter(ir_op *op, emit_func func)
224 {
225         op->ops.generic = (op_func)func;
226 }
227
228 /**
229  * Enters the emitter functions for handled nodes into the generic
230  * pointer of an opcode.
231  */
232 static void TEMPLATE_register_emitters(void)
233 {
234         /* first clear the generic function pointer for all ops */
235         clear_irp_opcodes_generic_func();
236
237         /* register all emitter functions defined in spec */
238         TEMPLATE_register_spec_emitters();
239
240         /* custom emitters not provided by the spec */
241         set_emitter(op_TEMPLATE_Jmp,   emit_TEMPLATE_Jmp);
242         set_emitter(op_be_IncSP,       emit_be_IncSP);
243         set_emitter(op_be_Return,      emit_be_Return);
244         set_emitter(op_be_Start,       emit_be_Start);
245
246         /* no need to emit anything for the following nodes */
247         set_emitter(op_Phi,            emit_nothing);
248         set_emitter(op_be_Keep,        emit_nothing);
249 }
250
251 typedef void (*emit_func_ptr) (const ir_node *);
252
253 /**
254  * Emits code for a node.
255  */
256 static void TEMPLATE_emit_node(const ir_node *node)
257 {
258         ir_op               *op       = get_irn_op(node);
259
260         if (op->ops.generic) {
261                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
262                 (*func) (node);
263         } else {
264                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
265         }
266 }
267
268 /**
269  * Walks over the nodes in a block connected by scheduling edges
270  * and emits code for each node.
271  */
272 static void TEMPLATE_emit_block(ir_node *block)
273 {
274         ir_node *node;
275
276         be_gas_emit_block_name(block);
277         be_emit_cstring(":\n");
278         be_emit_write_line();
279
280         sched_foreach(block, node) {
281                 TEMPLATE_emit_node(node);
282         }
283 }
284
285 /**
286  * Sets labels for control flow nodes (jump target)
287  */
288 static void TEMPLATE_gen_labels(ir_node *block, void *env)
289 {
290         ir_node *pred;
291         int n = get_Block_n_cfgpreds(block);
292         (void) env;
293
294         for (n--; n >= 0; n--) {
295                 pred = get_Block_cfgpred(block, n);
296                 set_irn_link(pred, block);
297         }
298 }
299
300 /**
301  * Main driver
302  */
303 void TEMPLATE_emit_routine(ir_graph *irg)
304 {
305         ir_node   **block_schedule;
306         ir_entity  *entity = get_irg_entity(irg);
307         size_t      i;
308         size_t      n;
309
310         /* register all emitter functions */
311         TEMPLATE_register_emitters();
312
313         /* create the block schedule */
314         block_schedule = be_create_block_schedule(irg);
315
316         /* emit assembler prolog */
317         be_gas_emit_function_prolog(entity, 4);
318
319         /* populate jump link fields with their destinations */
320         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
321
322         n = ARR_LEN(block_schedule);
323         for (i = 0; i < n; ++i) {
324                 ir_node *block = block_schedule[i];
325                 TEMPLATE_emit_block(block);
326         }
327         be_gas_emit_function_epilog(entity);
328 }