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