becopyilp: Do not advertise the switch to dump the solution, because this is not...
[libfirm] / ir / be / TEMPLATE / TEMPLATE_emitter.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   emit assembler for a backend graph
9  */
10 #include "config.h"
11
12 #include <limits.h>
13
14 #include "error.h"
15 #include "xmalloc.h"
16 #include "tv.h"
17 #include "iredges.h"
18 #include "debug.h"
19 #include "irgwalk.h"
20 #include "irprintf.h"
21 #include "irop_t.h"
22 #include "irargs_t.h"
23 #include "irprog.h"
24
25 #include "besched.h"
26 #include "begnuas.h"
27 #include "beblocksched.h"
28 #include "benode.h"
29
30 #include "TEMPLATE_emitter.h"
31 #include "gen_TEMPLATE_emitter.h"
32 #include "gen_TEMPLATE_regalloc_if.h"
33 #include "TEMPLATE_nodes_attr.h"
34 #include "TEMPLATE_new_nodes.h"
35
36 static void TEMPLATE_emit_immediate(const ir_node *node)
37 {
38         const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
39         be_emit_irprintf("%T", attr->value);
40 }
41
42 static void emit_register(const arch_register_t *reg)
43 {
44         be_emit_string(reg->name);
45 }
46
47 static void TEMPLATE_emit_source_register(const ir_node *node, int pos)
48 {
49         const arch_register_t *reg = arch_get_irn_register_in(node, pos);
50         emit_register(reg);
51 }
52
53 static void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
54 {
55         const arch_register_t *reg = arch_get_irn_register_out(node, pos);
56         emit_register(reg);
57 }
58
59 /**
60  * Returns the target label for a control flow node.
61  */
62 static void TEMPLATE_emit_cfop_target(const ir_node *node)
63 {
64         ir_node *block = (ir_node*)get_irn_link(node);
65         be_gas_emit_block_name(block);
66 }
67
68 void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
69 {
70         va_list ap;
71         va_start(ap, format);
72         be_emit_char('\t');
73         for (;;) {
74                 const char *start = format;
75                 while (*format != '%' && *format != '\0')
76                         ++format;
77                 be_emit_string_len(start, format-start);
78                 if (*format == '\0')
79                         break;
80                 ++format;
81
82                 switch (*format++) {
83                 case '%':
84                         be_emit_char('%');
85                         break;
86
87                 case 'S': {
88                         if (*format < '0' || '9' <= *format)
89                                 goto unknown;
90                         unsigned const pos = *format++ - '0';
91                         TEMPLATE_emit_source_register(node, pos);
92                         break;
93                 }
94
95                 case 'D': {
96                         if (*format < '0' || '9' <= *format)
97                                 goto unknown;
98                         unsigned const pos = *format++ - '0';
99                         TEMPLATE_emit_dest_register(node, pos);
100                         break;
101                 }
102
103                 case 'I':
104                         TEMPLATE_emit_immediate(node);
105                         break;
106
107                 case 'X': {
108                         int num = va_arg(ap, int);
109                         be_emit_irprintf("%X", num);
110                         break;
111                 }
112
113                 case 'u': {
114                         unsigned num = va_arg(ap, unsigned);
115                         be_emit_irprintf("%u", num);
116                         break;
117                 }
118
119                 case 'd': {
120                         int num = va_arg(ap, int);
121                         be_emit_irprintf("%d", num);
122                         break;
123                 }
124
125                 case 's': {
126                         const char *string = va_arg(ap, const char *);
127                         be_emit_string(string);
128                         break;
129                 }
130
131                 case 'L': {
132                         TEMPLATE_emit_cfop_target(node);
133                         break;
134                 }
135
136                 case '\n':
137                         be_emit_char('\n');
138                         be_emit_write_line();
139                         be_emit_char('\t');
140                         break;
141
142                 default:
143 unknown:
144                         panic("unknown format conversion");
145                 }
146         }
147         va_end(ap);
148         be_emit_finish_line_gas(node);
149
150 }
151
152 /**
153  * Emits code for a unconditional jump.
154  */
155 static void emit_TEMPLATE_Jmp(const ir_node *node)
156 {
157         TEMPLATE_emitf(node, "jmp %L");
158 }
159
160 static void emit_be_IncSP(const ir_node *node)
161 {
162         int offset = be_get_IncSP_offset(node);
163
164         if (offset == 0)
165                 return;
166
167         /* downwards growing stack */
168         const char *op = "add";
169         if (offset < 0) {
170                 op = "sub";
171                 offset = -offset;
172         }
173
174         TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
175 }
176
177 static void emit_be_Start(const ir_node *node)
178 {
179         ir_graph *irg        = get_irn_irg(node);
180         ir_type  *frame_type = get_irg_frame_type(irg);
181         unsigned  size       = get_type_size_bytes(frame_type);
182
183         /* emit function prolog */
184
185         /* allocate stackframe */
186         if (size > 0) {
187                 TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
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                 TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
202         }
203
204         /* return */
205         TEMPLATE_emitf(node, "ret");
206 }
207
208 /**
209  * Enters the emitter functions for handled nodes into the generic
210  * pointer of an opcode.
211  */
212 static void TEMPLATE_register_emitters(void)
213 {
214         /* first clear the generic function pointer for all ops */
215         ir_clear_opcodes_generic_func();
216
217         /* register all emitter functions defined in spec */
218         TEMPLATE_register_spec_emitters();
219
220         /* custom emitters not provided by the spec */
221         be_set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
222         be_set_emitter(op_be_IncSP,     emit_be_IncSP);
223         be_set_emitter(op_be_Return,    emit_be_Return);
224         be_set_emitter(op_be_Start,     emit_be_Start);
225
226         /* no need to emit anything for the following nodes */
227         be_set_emitter(op_Phi,     be_emit_nothing);
228         be_set_emitter(op_be_Keep, be_emit_nothing);
229 }
230
231 /**
232  * Walks over the nodes in a block connected by scheduling edges
233  * and emits code for each node.
234  */
235 static void TEMPLATE_emit_block(ir_node *block)
236 {
237         be_gas_begin_block(block, true);
238
239         sched_foreach(block, node) {
240                 be_emit_node(node);
241         }
242 }
243
244 /**
245  * Sets labels for control flow nodes (jump target)
246  */
247 static void TEMPLATE_gen_labels(ir_node *block, void *env)
248 {
249         ir_node *pred;
250         int n = get_Block_n_cfgpreds(block);
251         (void) env;
252
253         for (n--; n >= 0; n--) {
254                 pred = get_Block_cfgpred(block, n);
255                 set_irn_link(pred, block);
256         }
257 }
258
259 /**
260  * Main driver
261  */
262 void TEMPLATE_emit_routine(ir_graph *irg)
263 {
264         ir_node   **block_schedule;
265         ir_entity  *entity = get_irg_entity(irg);
266         size_t      i;
267         size_t      n;
268
269         /* register all emitter functions */
270         TEMPLATE_register_emitters();
271
272         /* create the block schedule */
273         block_schedule = be_create_block_schedule(irg);
274
275         /* emit assembler prolog */
276         be_gas_emit_function_prolog(entity, 4, NULL);
277
278         /* populate jump link fields with their destinations */
279         irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
280
281         n = ARR_LEN(block_schedule);
282         for (i = 0; i < n; ++i) {
283                 ir_node *block = block_schedule[i];
284                 TEMPLATE_emit_block(block);
285         }
286         be_gas_emit_function_epilog(entity);
287 }