modernize TEMPLATE backend and simplify some backend APIs
[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  *            (_)     | |  / _| | |        | |
108  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
109  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
110  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
111  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
112  * | |                                       | |
113  * |_|                                       |_|
114  *************************************************************/
115
116 void TEMPLATE_emit_immediate(const ir_node *node)
117 {
118         const TEMPLATE_attr_t *attr = get_irn_generic_attr_const(node);
119         be_emit_tarval(attr->value);
120 }
121
122 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
123 {
124         const arch_register_t *reg = get_in_reg(node, pos);
125         be_emit_string(arch_register_get_name(reg));
126 }
127
128 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
129 {
130         const arch_register_t *reg = get_out_reg(node, pos);
131         be_emit_string(arch_register_get_name(reg));
132 }
133
134 /**
135  * Returns the target label for a control flow node.
136  */
137 static void TEMPLATE_emit_cfop_target(const ir_node *node)
138 {
139         ir_node *block = get_irn_link(node);
140         be_gas_emit_block_name(block);
141 }
142
143 /***********************************************************************************
144  *                  _          __                                             _
145  *                 (_)        / _|                                           | |
146  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
147  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
148  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
149  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
150  *
151  ***********************************************************************************/
152
153 /**
154  * Emits code for a unconditional jump.
155  */
156 static void emit_TEMPLATE_Jmp(const ir_node *node)
157 {
158         be_emit_cstring("\tjmp ");
159         TEMPLATE_emit_cfop_target(node);
160         be_emit_finish_line_gas(node);
161 }
162
163 static void emit_be_IncSP(const ir_node *node)
164 {
165         int offset = be_get_IncSP_offset(node);
166
167         if (offset == 0)
168                 return;
169
170         /* downwards growing stack */
171         if (offset > 0) {
172                 be_emit_cstring("\tsub ");
173         } else {
174                 be_emit_cstring("\tadd ");
175                 offset = -offset;
176         }
177
178         TEMPLATE_emit_source_register(node, 0);
179         be_emit_irprintf(", %d, ", offset);
180         TEMPLATE_emit_dest_register(node, 0);
181         be_emit_finish_line_gas(node);
182 }
183
184 static void emit_be_Return(const ir_node *node)
185 {
186         be_emit_cstring("\tret");
187         be_emit_finish_line_gas(node);
188 }
189
190 static void emit_nothing(const ir_node *node)
191 {
192         (void) node;
193 }
194
195 /**
196  * The type of a emitter function.
197  */
198 typedef void (emit_func)(const ir_node *node);
199
200 static inline void set_emitter(ir_op *op, emit_func func)
201 {
202         op->ops.generic = (op_func)func;
203 }
204
205 /**
206  * Enters the emitter functions for handled nodes into the generic
207  * pointer of an opcode.
208  */
209 static void TEMPLATE_register_emitters(void)
210 {
211         /* first clear the generic function pointer for all ops */
212         clear_irp_opcodes_generic_func();
213
214         /* register all emitter functions defined in spec */
215         TEMPLATE_register_spec_emitters();
216
217         /* custom emitters not provided by the spec */
218         set_emitter(op_TEMPLATE_Jmp,   emit_TEMPLATE_Jmp);
219         set_emitter(op_be_Return,      emit_be_Return);
220         set_emitter(op_be_IncSP,       emit_be_IncSP);
221
222         /* no need to emit anything for the following nodes */
223         set_emitter(op_Phi,            emit_nothing);
224         set_emitter(op_be_Keep,        emit_nothing);
225         set_emitter(op_be_Start,       emit_nothing);
226         set_emitter(op_be_Barrier,     emit_nothing);
227 }
228
229 typedef void (*emit_func_ptr) (const ir_node *);
230
231 /**
232  * Emits code for a node.
233  */
234 static void TEMPLATE_emit_node(const ir_node *node)
235 {
236         ir_op               *op       = get_irn_op(node);
237
238         if (op->ops.generic) {
239                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
240                 (*func) (node);
241         } else {
242                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
243         }
244 }
245
246 /**
247  * Walks over the nodes in a block connected by scheduling edges
248  * and emits code for each node.
249  */
250 static void TEMPLATE_emit_block(ir_node *block)
251 {
252         ir_node *node;
253
254         be_gas_emit_block_name(block);
255         be_emit_cstring(":\n");
256         be_emit_write_line();
257
258         sched_foreach(block, node) {
259                 TEMPLATE_emit_node(node);
260         }
261 }
262
263 /**
264  * Sets labels for control flow nodes (jump target)
265  */
266 static void TEMPLATE_gen_labels(ir_node *block, void *env)
267 {
268         ir_node *pred;
269         int n = get_Block_n_cfgpreds(block);
270         (void) env;
271
272         for (n--; n >= 0; n--) {
273                 pred = get_Block_cfgpred(block, n);
274                 set_irn_link(pred, block);
275         }
276 }
277
278 /**
279  * Main driver
280  */
281 void TEMPLATE_emit_routine(ir_graph *irg)
282 {
283         ir_node   **block_schedule;
284         ir_entity  *entity = get_irg_entity(irg);
285         int         i;
286         int         n;
287
288         /* register all emitter functions */
289         TEMPLATE_register_emitters();
290
291         /* create the block schedule */
292         block_schedule = be_create_block_schedule(irg);
293
294         /* emit assembler prolog */
295         be_gas_emit_function_prolog(entity, 4);
296
297         /* populate jump link fields with their destinations */
298         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
299
300         n = ARR_LEN(block_schedule);
301         for (i = 0; i < n; ++i) {
302                 ir_node *block = block_schedule[i];
303                 TEMPLATE_emit_block(block);
304         }
305         be_gas_emit_function_epilog(entity);
306 }