Do not add a Proj to the schedule.
[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  */
24 #include "config.h"
25
26 #include <limits.h>
27
28 #include "xmalloc.h"
29 #include "tv.h"
30 #include "iredges.h"
31 #include "debug.h"
32 #include "irgwalk.h"
33 #include "irprintf.h"
34 #include "irop_t.h"
35 #include "irargs_t.h"
36 #include "irprog.h"
37
38 #include "besched.h"
39 #include "begnuas.h"
40 #include "beblocksched.h"
41 #include "benode.h"
42
43 #include "TEMPLATE_emitter.h"
44 #include "gen_TEMPLATE_emitter.h"
45 #include "gen_TEMPLATE_regalloc_if.h"
46 #include "TEMPLATE_nodes_attr.h"
47 #include "TEMPLATE_new_nodes.h"
48
49 static void TEMPLATE_emit_immediate(const ir_node *node)
50 {
51         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
52         be_emit_irprintf("%T", attr->value);
53 }
54
55 static void emit_register(const arch_register_t *reg)
56 {
57         be_emit_string(arch_register_get_name(reg));
58 }
59
60 static void TEMPLATE_emit_source_register(const ir_node *node, int pos)
61 {
62         const arch_register_t *reg = arch_get_irn_register_in(node, pos);
63         emit_register(reg);
64 }
65
66 static void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
67 {
68         const arch_register_t *reg = arch_get_irn_register_out(node, pos);
69         emit_register(reg);
70 }
71
72 /**
73  * Returns the target label for a control flow node.
74  */
75 static void TEMPLATE_emit_cfop_target(const ir_node *node)
76 {
77         ir_node *block = (ir_node*)get_irn_link(node);
78         be_gas_emit_block_name(block);
79 }
80
81 void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
82 {
83         va_list ap;
84         va_start(ap, format);
85         be_emit_char('\t');
86         for (;;) {
87                 const char *start = format;
88                 while (*format != '%' && *format != '\0')
89                         ++format;
90                 be_emit_string_len(start, format-start);
91                 if (*format == '\0')
92                         break;
93                 ++format;
94
95                 switch (*format++) {
96                 case '%':
97                         be_emit_char('%');
98                         break;
99
100                 case 'S': {
101                         if (*format < '0' || '9' <= *format)
102                                 goto unknown;
103                         unsigned const pos = *format++ - '0';
104                         TEMPLATE_emit_source_register(node, pos);
105                         break;
106                 }
107
108                 case 'D': {
109                         if (*format < '0' || '9' <= *format)
110                                 goto unknown;
111                         unsigned const pos = *format++ - '0';
112                         TEMPLATE_emit_dest_register(node, pos);
113                         break;
114                 }
115
116                 case 'I':
117                         TEMPLATE_emit_immediate(node);
118                         break;
119
120                 case 'X': {
121                         int num = va_arg(ap, int);
122                         be_emit_irprintf("%X", num);
123                         break;
124                 }
125
126                 case 'u': {
127                         unsigned num = va_arg(ap, unsigned);
128                         be_emit_irprintf("%u", num);
129                         break;
130                 }
131
132                 case 'd': {
133                         int num = va_arg(ap, int);
134                         be_emit_irprintf("%d", num);
135                         break;
136                 }
137
138                 case 's': {
139                         const char *string = va_arg(ap, const char *);
140                         be_emit_string(string);
141                         break;
142                 }
143
144                 case 'L': {
145                         TEMPLATE_emit_cfop_target(node);
146                         break;
147                 }
148
149                 case '\n':
150                         be_emit_char('\n');
151                         be_emit_write_line();
152                         be_emit_char('\t');
153                         break;
154
155                 default:
156 unknown:
157                         panic("unknown format conversion");
158                 }
159         }
160         va_end(ap);
161         be_emit_finish_line_gas(node);
162
163 }
164
165 /**
166  * Emits code for a unconditional jump.
167  */
168 static void emit_TEMPLATE_Jmp(const ir_node *node)
169 {
170         TEMPLATE_emitf(node, "jmp %L");
171 }
172
173 static void emit_be_IncSP(const ir_node *node)
174 {
175         int offset = be_get_IncSP_offset(node);
176
177         if (offset == 0)
178                 return;
179
180         /* downwards growing stack */
181         const char *op = "add";
182         if (offset < 0) {
183                 op = "sub";
184                 offset = -offset;
185         }
186
187         TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
188 }
189
190 static void emit_be_Start(const ir_node *node)
191 {
192         ir_graph *irg        = get_irn_irg(node);
193         ir_type  *frame_type = get_irg_frame_type(irg);
194         unsigned  size       = get_type_size_bytes(frame_type);
195
196         /* emit function prolog */
197
198         /* allocate stackframe */
199         if (size > 0) {
200                 TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
201         }
202 }
203
204 static void emit_be_Return(const ir_node *node)
205 {
206         ir_graph *irg        = get_irn_irg(node);
207         ir_type  *frame_type = get_irg_frame_type(irg);
208         unsigned  size       = get_type_size_bytes(frame_type);
209
210         /* emit function epilog here */
211
212         /* deallocate stackframe */
213         if (size > 0) {
214                 TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
215         }
216
217         /* return */
218         TEMPLATE_emitf(node, "ret");
219 }
220
221 static void emit_nothing(const ir_node *node)
222 {
223         (void) node;
224 }
225
226 /**
227  * The type of a emitter function.
228  */
229 typedef void (emit_func)(const ir_node *node);
230
231 static inline void set_emitter(ir_op *op, emit_func func)
232 {
233         op->ops.generic = (op_func)func;
234 }
235
236 /**
237  * Enters the emitter functions for handled nodes into the generic
238  * pointer of an opcode.
239  */
240 static void TEMPLATE_register_emitters(void)
241 {
242         /* first clear the generic function pointer for all ops */
243         ir_clear_opcodes_generic_func();
244
245         /* register all emitter functions defined in spec */
246         TEMPLATE_register_spec_emitters();
247
248         /* custom emitters not provided by the spec */
249         set_emitter(op_TEMPLATE_Jmp,   emit_TEMPLATE_Jmp);
250         set_emitter(op_be_IncSP,       emit_be_IncSP);
251         set_emitter(op_be_Return,      emit_be_Return);
252         set_emitter(op_be_Start,       emit_be_Start);
253
254         /* no need to emit anything for the following nodes */
255         set_emitter(op_Phi,            emit_nothing);
256         set_emitter(op_be_Keep,        emit_nothing);
257 }
258
259 typedef void (*emit_func_ptr) (const ir_node *);
260
261 /**
262  * Emits code for a node.
263  */
264 static void TEMPLATE_emit_node(const ir_node *node)
265 {
266         ir_op               *op       = get_irn_op(node);
267
268         if (op->ops.generic) {
269                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
270                 (*func) (node);
271         } else {
272                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
273         }
274 }
275
276 /**
277  * Walks over the nodes in a block connected by scheduling edges
278  * and emits code for each node.
279  */
280 static void TEMPLATE_emit_block(ir_node *block)
281 {
282         be_gas_begin_block(block, true);
283
284         sched_foreach(block, node) {
285                 TEMPLATE_emit_node(node);
286         }
287 }
288
289 /**
290  * Sets labels for control flow nodes (jump target)
291  */
292 static void TEMPLATE_gen_labels(ir_node *block, void *env)
293 {
294         ir_node *pred;
295         int n = get_Block_n_cfgpreds(block);
296         (void) env;
297
298         for (n--; n >= 0; n--) {
299                 pred = get_Block_cfgpred(block, n);
300                 set_irn_link(pred, block);
301         }
302 }
303
304 /**
305  * Main driver
306  */
307 void TEMPLATE_emit_routine(ir_graph *irg)
308 {
309         ir_node   **block_schedule;
310         ir_entity  *entity = get_irg_entity(irg);
311         size_t      i;
312         size_t      n;
313
314         /* register all emitter functions */
315         TEMPLATE_register_emitters();
316
317         /* create the block schedule */
318         block_schedule = be_create_block_schedule(irg);
319
320         /* emit assembler prolog */
321         be_gas_emit_function_prolog(entity, 4, NULL);
322
323         /* populate jump link fields with their destinations */
324         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
325
326         n = ARR_LEN(block_schedule);
327         for (i = 0; i < n; ++i) {
328                 ir_node *block = block_schedule[i];
329                 TEMPLATE_emit_block(block);
330         }
331         be_gas_emit_function_epilog(entity);
332 }