change unreachable code elmination to the simpler remove links into unreachable code...
[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 void TEMPLATE_emit_immediate(const ir_node *node)
53 {
54         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
55         be_emit_tarval(attr->value);
56 }
57
58 static void emit_register(const arch_register_t *reg)
59 {
60         be_emit_string(arch_register_get_name(reg));
61 }
62
63 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
64 {
65         const arch_register_t *reg = arch_get_irn_register_in(node, pos);
66         emit_register(reg);
67 }
68
69 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
70 {
71         const arch_register_t *reg = arch_get_irn_register_out(node, pos);
72         emit_register(reg);
73 }
74
75 /**
76  * Returns the target label for a control flow node.
77  */
78 static void TEMPLATE_emit_cfop_target(const ir_node *node)
79 {
80         ir_node *block = (ir_node*)get_irn_link(node);
81         be_gas_emit_block_name(block);
82 }
83
84
85
86 /**
87  * Emits code for a unconditional jump.
88  */
89 static void emit_TEMPLATE_Jmp(const ir_node *node)
90 {
91         be_emit_cstring("\tjmp ");
92         TEMPLATE_emit_cfop_target(node);
93         be_emit_finish_line_gas(node);
94 }
95
96 static void emit_be_IncSP(const ir_node *node)
97 {
98         int offset = be_get_IncSP_offset(node);
99
100         if (offset == 0)
101                 return;
102
103         /* downwards growing stack */
104         if (offset > 0) {
105                 be_emit_cstring("\tsub ");
106         } else {
107                 be_emit_cstring("\tadd ");
108                 offset = -offset;
109         }
110
111         TEMPLATE_emit_source_register(node, 0);
112         be_emit_irprintf(", %d, ", offset);
113         TEMPLATE_emit_dest_register(node, 0);
114         be_emit_finish_line_gas(node);
115 }
116
117 static void emit_be_Start(const ir_node *node)
118 {
119         ir_graph *irg        = get_irn_irg(node);
120         ir_type  *frame_type = get_irg_frame_type(irg);
121         unsigned  size       = get_type_size_bytes(frame_type);
122
123         /* emit function prolog */
124
125         /* allocate stackframe */
126         if (size > 0) {
127                 be_emit_cstring("\tsub ");
128                 emit_register(&TEMPLATE_registers[REG_SP]);
129                 be_emit_irprintf(", %u, ", size);
130                 emit_register(&TEMPLATE_registers[REG_SP]);
131                 be_emit_finish_line_gas(node);
132         }
133 }
134
135 static void emit_be_Return(const ir_node *node)
136 {
137         ir_graph *irg        = get_irn_irg(node);
138         ir_type  *frame_type = get_irg_frame_type(irg);
139         unsigned  size       = get_type_size_bytes(frame_type);
140
141         /* emit function epilog here */
142
143         /* deallocate stackframe */
144         if (size > 0) {
145                 be_emit_cstring("\tadd ");
146                 emit_register(&TEMPLATE_registers[REG_SP]);
147                 be_emit_irprintf(", %u, ", size);
148                 emit_register(&TEMPLATE_registers[REG_SP]);
149                 be_emit_finish_line_gas(node);
150         }
151
152         /* return */
153         be_emit_cstring("\tret");
154         be_emit_finish_line_gas(node);
155 }
156
157 static void emit_nothing(const ir_node *node)
158 {
159         (void) node;
160 }
161
162 /**
163  * The type of a emitter function.
164  */
165 typedef void (emit_func)(const ir_node *node);
166
167 static inline void set_emitter(ir_op *op, emit_func func)
168 {
169         op->ops.generic = (op_func)func;
170 }
171
172 /**
173  * Enters the emitter functions for handled nodes into the generic
174  * pointer of an opcode.
175  */
176 static void TEMPLATE_register_emitters(void)
177 {
178         /* first clear the generic function pointer for all ops */
179         clear_irp_opcodes_generic_func();
180
181         /* register all emitter functions defined in spec */
182         TEMPLATE_register_spec_emitters();
183
184         /* custom emitters not provided by the spec */
185         set_emitter(op_TEMPLATE_Jmp,   emit_TEMPLATE_Jmp);
186         set_emitter(op_be_IncSP,       emit_be_IncSP);
187         set_emitter(op_be_Return,      emit_be_Return);
188         set_emitter(op_be_Start,       emit_be_Start);
189
190         /* no need to emit anything for the following nodes */
191         set_emitter(op_Phi,            emit_nothing);
192         set_emitter(op_be_Keep,        emit_nothing);
193 }
194
195 typedef void (*emit_func_ptr) (const ir_node *);
196
197 /**
198  * Emits code for a node.
199  */
200 static void TEMPLATE_emit_node(const ir_node *node)
201 {
202         ir_op               *op       = get_irn_op(node);
203
204         if (op->ops.generic) {
205                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
206                 (*func) (node);
207         } else {
208                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
209         }
210 }
211
212 /**
213  * Walks over the nodes in a block connected by scheduling edges
214  * and emits code for each node.
215  */
216 static void TEMPLATE_emit_block(ir_node *block)
217 {
218         ir_node *node;
219
220         be_gas_emit_block_name(block);
221         be_emit_cstring(":\n");
222         be_emit_write_line();
223
224         sched_foreach(block, node) {
225                 TEMPLATE_emit_node(node);
226         }
227 }
228
229 /**
230  * Sets labels for control flow nodes (jump target)
231  */
232 static void TEMPLATE_gen_labels(ir_node *block, void *env)
233 {
234         ir_node *pred;
235         int n = get_Block_n_cfgpreds(block);
236         (void) env;
237
238         for (n--; n >= 0; n--) {
239                 pred = get_Block_cfgpred(block, n);
240                 set_irn_link(pred, block);
241         }
242 }
243
244 /**
245  * Main driver
246  */
247 void TEMPLATE_emit_routine(ir_graph *irg)
248 {
249         ir_node   **block_schedule;
250         ir_entity  *entity = get_irg_entity(irg);
251         size_t      i;
252         size_t      n;
253
254         /* register all emitter functions */
255         TEMPLATE_register_emitters();
256
257         /* create the block schedule */
258         block_schedule = be_create_block_schedule(irg);
259
260         /* emit assembler prolog */
261         be_gas_emit_function_prolog(entity, 4);
262
263         /* populate jump link fields with their destinations */
264         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
265
266         n = ARR_LEN(block_schedule);
267         for (i = 0; i < n; ++i) {
268                 ir_node *block = block_schedule[i];
269                 TEMPLATE_emit_block(block);
270         }
271         be_gas_emit_function_epilog(entity);
272 }