Added preliminary Conv, Call and Jmp instructions to the amd64 backend.
[libfirm] / ir / be / amd64 / amd64_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: amd64_emitter.c 26746 2009-11-27 08:53:15Z matze $
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 "../be_dbgout.h"
43
44 #include "amd64_emitter.h"
45 #include "gen_amd64_emitter.h"
46 #include "amd64_nodes_attr.h"
47 #include "amd64_new_nodes.h"
48
49 #define SNPRINTF_BUF_LEN 128
50
51 #include "../benode.h"
52
53 /**
54  * Returns the register at in position pos.
55  */
56 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
57 {
58         ir_node                *op;
59         const arch_register_t  *reg = NULL;
60
61         assert(get_irn_arity(node) > pos && "Invalid IN position");
62
63         /* The out register of the operator at position pos is the
64            in register we need. */
65         op = get_irn_n(node, pos);
66
67         reg = arch_get_irn_register(op);
68
69         assert(reg && "no in register found");
70         return reg;
71 }
72
73 /**
74  * Returns the register at out position pos.
75  */
76 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
77 {
78         ir_node                *proj;
79         const arch_register_t  *reg = NULL;
80
81         /* 1st case: irn is not of mode_T, so it has only                 */
82         /*           one OUT register -> good                             */
83         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
84         /*           Proj with the corresponding projnum for the register */
85
86         if (get_irn_mode(node) != mode_T) {
87                 reg = arch_get_irn_register(node);
88         } else if (is_amd64_irn(node)) {
89                 reg = arch_irn_get_register(node, pos);
90         } else {
91                 const ir_edge_t *edge;
92
93                 foreach_out_edge(node, edge) {
94                         proj = get_edge_src_irn(edge);
95                         assert(is_Proj(proj) && "non-Proj from mode_T node");
96                         if (get_Proj_proj(proj) == pos) {
97                                 reg = arch_get_irn_register(proj);
98                                 break;
99                         }
100                 }
101         }
102
103         assert(reg && "no out register found");
104         return reg;
105 }
106
107 /*************************************************************
108  *             _       _    __   _          _
109  *            (_)     | |  / _| | |        | |
110  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
111  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
112  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
113  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
114  * | |                                       | |
115  * |_|                                       |_|
116  *************************************************************/
117
118 void amd64_emit_immediate(const ir_node *node)
119 {
120         const amd64_immediate_attr_t *attr = get_amd64_immediate_attr_const (node);
121         be_emit_char('$');
122         be_emit_irprintf("0x%X", attr->imm_value);
123 }
124
125 void amd64_emit_source_register(const ir_node *node, int pos)
126 {
127         const arch_register_t *reg = get_in_reg(node, pos);
128         be_emit_char('%');
129         be_emit_string(arch_register_get_name(reg));
130 }
131
132 void amd64_emit_dest_register(const ir_node *node, int pos)
133 {
134         const arch_register_t *reg = get_out_reg(node, pos);
135         be_emit_char('%');
136         be_emit_string(arch_register_get_name(reg));
137 }
138
139 /**
140  * Returns the target label for a control flow node.
141  */
142 /*
143 static void amd64_emit_cfop_target(const ir_node *node)
144 {
145         ir_node *block = get_irn_link(node);
146
147         be_emit_irprintf("BLOCK_%ld", get_irn_node_nr(block));
148 }
149 */
150
151 /***********************************************************************************
152  *                  _          __                                             _
153  *                 (_)        / _|                                           | |
154  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
155  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
156  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
157  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
158  *
159  ***********************************************************************************/
160
161 /**
162  * Default emitter for anything that we don't want to generate code for.
163  */
164 static void emit_nothing(const ir_node *node)
165 {
166         (void) node;
167 }
168
169 /**
170  * Emit a SymConst.
171  */
172 static void emit_amd64_SymConst(const ir_node *irn)
173 {
174         const amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr_const(irn);
175 //      sym_or_tv_t key, *entry;
176 //      unsigned label;
177 //
178 //      key.u.id     = get_entity_ld_ident(attr->entity);
179 //      key.is_ident = 1;
180 //      key.label    = 0;
181 //      entry = (sym_or_tv_t *)set_insert(sym_or_tv, &key, sizeof(key), HASH_PTR(key.u.generic));
182 //      if (entry->label == 0) {
183 //              /* allocate a label */
184 //              entry->label = get_unique_label();
185 //      }
186 //      label = entry->label;
187
188
189         be_gas_emit_entity(attr->entity);
190         be_emit_char(':');
191         be_emit_finish_line_gas(irn);
192         be_emit_cstring("\t.long 0x0");
193         be_emit_finish_line_gas(irn);
194 }
195
196 /**
197  * Returns the next block in a block schedule.
198  */
199 static ir_node *sched_next_block(const ir_node *block)
200 {
201     return get_irn_link(block);
202 }
203
204 /**
205  * Returns the target block for a control flow node.
206  */
207 static ir_node *get_cfop_target_block(const ir_node *irn)
208 {
209         return get_irn_link(irn);
210 }
211
212 /**
213  * Emit the target label for a control flow node.
214  */
215 static void amd64_emit_cfop_target(const ir_node *irn)
216 {
217         ir_node *block = get_cfop_target_block(irn);
218
219         be_gas_emit_block_name(block);
220 }
221
222 /**
223  * Emit a Jmp.
224  */
225 static void emit_amd64_Jmp(const ir_node *node)
226 {
227         ir_node *block, *next_block;
228
229         /* for now, the code works for scheduled and non-schedules blocks */
230         block = get_nodes_block(node);
231
232         /* we have a block schedule */
233         next_block = sched_next_block(block);
234         if (get_cfop_target_block(node) != next_block) {
235                 be_emit_cstring("\tjmp ");
236                 amd64_emit_cfop_target(node);
237         } else {
238                 be_emit_cstring("\t/* fallthrough to ");
239                 amd64_emit_cfop_target(node);
240                 be_emit_cstring(" */");
241         }
242         be_emit_finish_line_gas(node);
243 }
244
245 /**
246  * Emits code for a call.
247  */
248 static void emit_be_Call(const ir_node *node)
249 {
250         ir_entity *entity = be_Call_get_entity (node);
251
252         if (entity) {
253                 be_emit_cstring("\tcall ");
254                 be_gas_emit_entity (be_Call_get_entity(node));
255                 be_emit_finish_line_gas(node);
256         } else {
257                 be_emit_pad_comment();
258                 be_emit_cstring("/* FIXME: call NULL entity?! */\n");
259         }
260 }
261
262 /**
263  * emit copy node
264  */
265 static void emit_be_Copy(const ir_node *irn)
266 {
267         ir_mode *mode = get_irn_mode(irn);
268
269         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
270                 /* omitted Copy */
271                 return;
272         }
273
274         if (mode_is_float(mode)) {
275                 panic("emit_be_Copy: move not supported for FP");
276         } else if (mode_is_data(mode)) {
277                 be_emit_cstring("\tmov ");
278                 amd64_emit_source_register(irn, 0);
279                 be_emit_cstring(", ");
280                 amd64_emit_dest_register(irn, 0);
281                 be_emit_finish_line_gas(irn);
282         } else {
283                 panic("emit_be_Copy: move not supported for this mode");
284         }
285 }
286
287 /**
288  * Emits code for a return.
289  */
290 static void emit_be_Return(const ir_node *node)
291 {
292         be_emit_cstring("\tret");
293         be_emit_finish_line_gas(node);
294 }
295
296 /**
297  * The type of a emitter function.
298  */
299 typedef void (emit_func)(const ir_node *irn);
300
301 /**
302  * Set a node emitter. Make it a bit more type safe.
303  */
304 static inline void set_emitter(ir_op *op, emit_func arm_emit_node)
305 {
306         op->ops.generic = (op_func)arm_emit_node;
307 }
308
309 /**
310  * Enters the emitter functions for handled nodes into the generic
311  * pointer of an opcode.
312  */
313 static void amd64_register_emitters(void)
314 {
315         /* first clear the generic function pointer for all ops */
316         clear_irp_opcodes_generic_func();
317
318         /* register all emitter functions defined in spec */
319         amd64_register_spec_emitters();
320
321         set_emitter(op_amd64_SymConst,   emit_amd64_SymConst);
322         set_emitter(op_amd64_Jmp,        emit_amd64_Jmp);
323         set_emitter(op_be_Return,        emit_be_Return);
324         set_emitter(op_be_Call,          emit_be_Call);
325         set_emitter(op_be_Copy,          emit_be_Copy);
326
327         set_emitter(op_be_Start,         emit_nothing);
328         set_emitter(op_be_Keep,          emit_nothing);
329         set_emitter(op_be_Barrier,       emit_nothing);
330         set_emitter(op_be_IncSP,         emit_nothing);
331         set_emitter(op_Phi,              emit_nothing);
332 }
333
334 typedef void (*emit_func_ptr) (const ir_node *);
335
336 /**
337  * Emits code for a node.
338  */
339 static void amd64_emit_node(const ir_node *node)
340 {
341         ir_op               *op       = get_irn_op(node);
342
343         if (op->ops.generic) {
344                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
345                 (*func) (node);
346         } else {
347                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
348         }
349 }
350
351 /**
352  * Walks over the nodes in a block connected by scheduling edges
353  * and emits code for each node.
354  */
355 static void amd64_gen_block(ir_node *block, void *data)
356 {
357         ir_node *node;
358         (void) data;
359
360         if (! is_Block(block))
361                 return;
362
363         be_gas_emit_block_name(block);
364         be_emit_char(':');
365
366         be_emit_write_line();
367
368         sched_foreach(block, node) {
369                 amd64_emit_node(node);
370         }
371 }
372
373
374 /**
375  * Sets labels for control flow nodes (jump target)
376  * TODO: Jump optimization
377  */
378 static void amd64_gen_labels(ir_node *block, void *env)
379 {
380         ir_node *pred;
381         int n = get_Block_n_cfgpreds(block);
382         (void) env;
383
384         for (n--; n >= 0; n--) {
385                 pred = get_Block_cfgpred(block, n);
386                 set_irn_link(pred, block);
387         }
388 }
389
390 /**
391  * Main driver
392  */
393 void amd64_gen_routine(const amd64_code_gen_t *cg, ir_graph *irg)
394 {
395         ir_entity *entity = get_irg_entity(irg);
396         ir_node  **blk_sched;
397         int i, n;
398         (void)cg;
399
400         /* register all emitter functions */
401         amd64_register_emitters();
402
403         blk_sched = be_create_block_schedule(irg);
404
405         be_dbg_method_begin(entity, be_abi_get_stack_layout(cg->birg->abi));
406         be_gas_emit_function_prolog(entity, 4);
407
408         irg_block_walk_graph(irg, amd64_gen_labels, NULL, NULL);
409
410         n = ARR_LEN(blk_sched);
411         for (i = 0; i < n; ++i) {
412                 ir_node *block = blk_sched[i];
413                 ir_node *prev  = i > 0 ? blk_sched[i-1] : NULL;
414
415                 set_irn_link(block, prev);
416         }
417
418         for (i = 0; i < n; ++i) {
419                 ir_node *block = blk_sched[i];
420
421                 amd64_gen_block(block, 0);
422         }
423
424         be_gas_emit_function_epilog(entity);
425         be_dbg_method_end();
426         be_emit_char('\n');
427         be_emit_write_line();
428 }