amd64: small changes w.r.t. stack alignment.
[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_register(const arch_register_t *reg)
119 {
120         be_emit_char('%');
121         be_emit_string(arch_register_get_name(reg));
122 }
123
124 void amd64_emit_immediate(const ir_node *node)
125 {
126         const amd64_attr_t *attr = get_amd64_attr_const (node);
127         be_emit_char('$');
128         be_emit_irprintf("0x%X", attr->ext.imm_value);
129 }
130
131 void amd64_emit_source_register(const ir_node *node, int pos)
132 {
133         amd64_emit_register(get_in_reg(node, pos));
134 }
135
136 void amd64_emit_dest_register(const ir_node *node, int pos)
137 {
138         amd64_emit_register(get_out_reg(node, pos));
139 }
140
141 /**
142  * Returns the target label for a control flow node.
143  */
144 /*
145 static void amd64_emit_cfop_target(const ir_node *node)
146 {
147         ir_node *block = get_irn_link(node);
148
149         be_emit_irprintf("BLOCK_%ld", get_irn_node_nr(block));
150 }
151 */
152
153 /***********************************************************************************
154  *                  _          __                                             _
155  *                 (_)        / _|                                           | |
156  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
157  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
158  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
159  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
160  *
161  ***********************************************************************************/
162
163 /**
164  * Default emitter for anything that we don't want to generate code for.
165  */
166 static void emit_nothing(const ir_node *node)
167 {
168         (void) node;
169 }
170
171 /**
172  * Emit a SymConst.
173  */
174 static void emit_amd64_SymConst(const ir_node *irn)
175 {
176         const amd64_SymConst_attr_t *attr = get_amd64_SymConst_attr_const(irn);
177 //      sym_or_tv_t key, *entry;
178 //      unsigned label;
179 //
180 //      key.u.id     = get_entity_ld_ident(attr->entity);
181 //      key.is_ident = 1;
182 //      key.label    = 0;
183 //      entry = (sym_or_tv_t *)set_insert(sym_or_tv, &key, sizeof(key), HASH_PTR(key.u.generic));
184 //      if (entry->label == 0) {
185 //              /* allocate a label */
186 //              entry->label = get_unique_label();
187 //      }
188 //      label = entry->label;
189
190         be_gas_emit_entity(attr->entity);
191         be_emit_char(':');
192         be_emit_finish_line_gas(irn);
193         be_emit_cstring("\t.long 0x0");
194         be_emit_finish_line_gas(irn);
195 }
196
197 /**
198  * Returns the next block in a block schedule.
199  */
200 static ir_node *sched_next_block(const ir_node *block)
201 {
202     return get_irn_link(block);
203 }
204
205 /**
206  * Returns the target block for a control flow node.
207  */
208 static ir_node *get_cfop_target_block(const ir_node *irn)
209 {
210         return get_irn_link(irn);
211 }
212
213 /**
214  * Emit the target label for a control flow node.
215  */
216 static void amd64_emit_cfop_target(const ir_node *irn)
217 {
218         ir_node *block = get_cfop_target_block(irn);
219
220         be_gas_emit_block_name(block);
221 }
222
223 /**
224  * Emit a Jmp.
225  */
226 static void emit_amd64_Jmp(const ir_node *node)
227 {
228         ir_node *block, *next_block;
229
230         /* for now, the code works for scheduled and non-schedules blocks */
231         block = get_nodes_block(node);
232
233         /* we have a block schedule */
234         next_block = sched_next_block(block);
235         if (get_cfop_target_block(node) != next_block) {
236                 be_emit_cstring("\tjmp ");
237                 amd64_emit_cfop_target(node);
238         } else {
239                 be_emit_cstring("\t/* fallthrough to ");
240                 amd64_emit_cfop_target(node);
241                 be_emit_cstring(" */");
242         }
243         be_emit_finish_line_gas(node);
244 }
245
246 /**
247  * Emit a Compare with conditional branch.
248  */
249 static void emit_amd64_Jcc(const ir_node *irn)
250 {
251         const ir_edge_t      *edge;
252         const ir_node        *proj_true  = NULL;
253         const ir_node        *proj_false = NULL;
254         const ir_node        *block;
255         const ir_node        *next_block;
256         const char           *suffix;
257         const amd64_attr_t   *attr      = get_irn_generic_attr_const(irn);
258         int                   proj_num  = attr->ext.pnc;
259         ir_node              *op1       = get_irn_n(irn, 0);
260         const amd64_attr_t   *cmp_attr  = get_irn_generic_attr_const(op1);
261         bool                  is_signed = !cmp_attr->data.cmp_unsigned;
262
263         assert(is_amd64_Cmp(op1));
264
265         foreach_out_edge(irn, edge) {
266                 ir_node *proj = get_edge_src_irn(edge);
267                 long nr = get_Proj_proj(proj);
268                 if (nr == pn_Cond_true) {
269                         proj_true = proj;
270                 } else {
271                         proj_false = proj;
272                 }
273         }
274
275         if (cmp_attr->data.ins_permuted) {
276                 proj_num = get_mirrored_pnc(proj_num);
277         }
278
279         /* for now, the code works for scheduled and non-schedules blocks */
280         block = get_nodes_block(irn);
281
282         /* we have a block schedule */
283         next_block = sched_next_block(block);
284
285         assert(proj_num != pn_Cmp_False);
286         assert(proj_num != pn_Cmp_True);
287
288         if (get_cfop_target_block(proj_true) == next_block) {
289                 /* exchange both proj's so the second one can be omitted */
290                 const ir_node *t = proj_true;
291
292                 proj_true  = proj_false;
293                 proj_false = t;
294                 proj_num   = get_negated_pnc(proj_num, mode_Iu);
295         }
296
297         switch (proj_num) {
298                 case pn_Cmp_Eq:  suffix = "e"; break;
299                 case pn_Cmp_Lt:  suffix = is_signed ? "l"  : "b"; break;
300                 case pn_Cmp_Le:  suffix = is_signed ? "le" : "be"; break;
301                 case pn_Cmp_Gt:  suffix = is_signed ? "g"  : "o"; break;
302                 case pn_Cmp_Ge:  suffix = is_signed ? "ge" : "oe"; break;
303                 case pn_Cmp_Lg:  suffix = "ne"; break;
304                 case pn_Cmp_Leg: suffix = "mp"; break;
305                 default: panic("Cmp has unsupported pnc");
306         }
307
308         /* emit the true proj */
309         be_emit_irprintf("\tj%s ", suffix);
310         amd64_emit_cfop_target(proj_true);
311         be_emit_finish_line_gas(proj_true);
312
313         if (get_cfop_target_block(proj_false) == next_block) {
314                 be_emit_cstring("\t/* fallthrough to ");
315                 amd64_emit_cfop_target(proj_false);
316                 be_emit_cstring(" */");
317                 be_emit_finish_line_gas(proj_false);
318         } else {
319                 be_emit_cstring("\tjmp ");
320                 amd64_emit_cfop_target(proj_false);
321                 be_emit_finish_line_gas(proj_false);
322         }
323 }
324
325 /**
326  * Emits code for a call.
327  */
328 static void emit_be_Call(const ir_node *node)
329 {
330         ir_entity *entity = be_Call_get_entity (node);
331
332         if (entity) {
333                 be_emit_cstring("\tcall ");
334                 be_gas_emit_entity (be_Call_get_entity(node));
335                 be_emit_finish_line_gas(node);
336         } else {
337                 be_emit_pad_comment();
338                 be_emit_cstring("/* FIXME: call NULL entity?! */\n");
339         }
340 }
341
342 /**
343  * emit copy node
344  */
345 static void emit_be_Copy(const ir_node *irn)
346 {
347         ir_mode *mode = get_irn_mode(irn);
348
349         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
350                 /* omitted Copy */
351                 return;
352         }
353
354         if (mode_is_float(mode)) {
355                 panic("emit_be_Copy: move not supported for FP");
356         } else if (mode_is_data(mode)) {
357                 be_emit_cstring("\tmov ");
358                 amd64_emit_source_register(irn, 0);
359                 be_emit_cstring(", ");
360                 amd64_emit_dest_register(irn, 0);
361                 be_emit_finish_line_gas(irn);
362         } else {
363                 panic("emit_be_Copy: move not supported for this mode");
364         }
365 }
366
367 static void emit_amd64_FrameAddr(const ir_node *irn)
368 {
369         const amd64_SymConst_attr_t *attr = get_irn_generic_attr_const(irn);
370
371         be_emit_cstring("\tmov ");
372         amd64_emit_source_register(irn, 0);
373         be_emit_cstring(", ");
374         amd64_emit_dest_register(irn, 0);
375         be_emit_finish_line_gas(irn);
376
377         be_emit_cstring("\tadd ");
378         be_emit_irprintf("$0x%X", attr->fp_offset);
379         be_emit_cstring(", ");
380         amd64_emit_dest_register(irn, 0);
381         be_emit_finish_line_gas(irn);
382 }
383
384 /**
385  * Emits code to increase stack pointer.
386  */
387 static void emit_be_IncSP(const ir_node *node)
388 {
389         int offs = be_get_IncSP_offset(node);
390
391         if (offs == 0)
392                 return;
393
394         if (offs > 0) {
395                 be_emit_irprintf("\tsub ");
396                 amd64_emit_dest_register(node, 0);
397                 be_emit_irprintf(", $%u", offs);
398                 be_emit_finish_line_gas(node);
399         } else {
400                 be_emit_irprintf("\tadd ");
401                 amd64_emit_dest_register(node, 0);
402                 be_emit_irprintf(", $%u", -offs);
403                 be_emit_finish_line_gas(node);
404         }
405 }
406
407 /**
408  * Emits code for a return.
409  */
410 static void emit_be_Return(const ir_node *node)
411 {
412         be_emit_cstring("\tret");
413         be_emit_finish_line_gas(node);
414 }
415
416
417 static void emit_amd64_binop_op(const ir_node *irn, int second_op)
418 {
419         if (irn->op == op_amd64_Add) {
420                 be_emit_cstring("\tadd ");
421         } else if (irn->op == op_amd64_Mul) {
422                 be_emit_cstring("\tmul ");
423         } else if (irn->op == op_amd64_Sub) {
424                 be_emit_cstring("\tsub ");
425         }
426
427         amd64_emit_dest_register(irn, 0);
428         be_emit_cstring(", ");
429         amd64_emit_source_register(irn, second_op);
430         be_emit_finish_line_gas(irn);
431 }
432
433 /**
434  * Emits an arithmetic operation that handles arbitraty input registers.
435  */
436 static void emit_amd64_binop(const ir_node *irn)
437 {
438         const arch_register_t *reg_s1 = get_in_reg(irn, 0);
439         const arch_register_t *reg_s2 = get_in_reg(irn, 1);
440         const arch_register_t *reg_d1 = get_out_reg(irn, 0);
441
442         int second_op = 0;
443
444         if (reg_d1 != reg_s1 && reg_d1 != reg_s2) {
445                 be_emit_cstring("\tmov ");
446                 amd64_emit_register(reg_s1);
447                 be_emit_cstring(", ");
448                 amd64_emit_register(reg_d1);
449                 be_emit_finish_line_gas(irn);
450                 second_op = 1;
451
452         } else if (reg_d1 == reg_s2 && reg_d1 != reg_s1) {
453                 second_op = 0;
454
455         }
456
457         emit_amd64_binop_op(irn, second_op);
458
459 }
460
461 /**
462  * The type of a emitter function.
463  */
464 typedef void (emit_func)(const ir_node *irn);
465
466 /**
467  * Set a node emitter. Make it a bit more type safe.
468  */
469 static inline void set_emitter(ir_op *op, emit_func arm_emit_node)
470 {
471         op->ops.generic = (op_func)arm_emit_node;
472 }
473
474 /**
475  * Enters the emitter functions for handled nodes into the generic
476  * pointer of an opcode.
477  */
478 static void amd64_register_emitters(void)
479 {
480         /* first clear the generic function pointer for all ops */
481         clear_irp_opcodes_generic_func();
482
483         /* register all emitter functions defined in spec */
484         amd64_register_spec_emitters();
485
486         set_emitter(op_amd64_SymConst,   emit_amd64_SymConst);
487         set_emitter(op_amd64_Jmp,        emit_amd64_Jmp);
488         set_emitter(op_amd64_Jcc,        emit_amd64_Jcc);
489         set_emitter(op_amd64_FrameAddr,  emit_amd64_FrameAddr);
490         set_emitter(op_be_Return,        emit_be_Return);
491         set_emitter(op_be_Call,          emit_be_Call);
492         set_emitter(op_be_Copy,          emit_be_Copy);
493         set_emitter(op_be_IncSP,         emit_be_IncSP);
494
495         set_emitter(op_amd64_Add,        emit_amd64_binop);
496         set_emitter(op_amd64_Mul,        emit_amd64_binop);
497
498         set_emitter(op_be_Start,         emit_nothing);
499         set_emitter(op_be_Keep,          emit_nothing);
500         set_emitter(op_be_Barrier,       emit_nothing);
501         set_emitter(op_Phi,              emit_nothing);
502 }
503
504 typedef void (*emit_func_ptr) (const ir_node *);
505
506 /**
507  * Emits code for a node.
508  */
509 static void amd64_emit_node(const ir_node *node)
510 {
511         ir_op               *op       = get_irn_op(node);
512
513         if (op->ops.generic) {
514                 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
515                 (*func) (node);
516         } else {
517                 ir_fprintf(stderr, "No emitter for node %+F\n", node);
518         }
519 }
520
521 /**
522  * Walks over the nodes in a block connected by scheduling edges
523  * and emits code for each node.
524  */
525 static void amd64_gen_block(ir_node *block, void *data)
526 {
527         ir_node *node;
528         (void) data;
529
530         if (! is_Block(block))
531                 return;
532
533         be_gas_emit_block_name(block);
534         be_emit_char(':');
535
536         be_emit_write_line();
537
538         sched_foreach(block, node) {
539                 amd64_emit_node(node);
540         }
541 }
542
543
544 /**
545  * Sets labels for control flow nodes (jump target)
546  * TODO: Jump optimization
547  */
548 static void amd64_gen_labels(ir_node *block, void *env)
549 {
550         ir_node *pred;
551         int n = get_Block_n_cfgpreds(block);
552         (void) env;
553
554         for (n--; n >= 0; n--) {
555                 pred = get_Block_cfgpred(block, n);
556                 set_irn_link(pred, block);
557         }
558 }
559
560 /**
561  * Main driver
562  */
563 void amd64_gen_routine(const amd64_code_gen_t *cg, ir_graph *irg)
564 {
565         ir_entity *entity = get_irg_entity(irg);
566         ir_node  **blk_sched;
567         int i, n;
568         (void)cg;
569
570         /* register all emitter functions */
571         amd64_register_emitters();
572
573         blk_sched = be_create_block_schedule(irg);
574
575         be_dbg_method_begin(entity, be_abi_get_stack_layout(cg->birg->abi));
576         be_gas_emit_function_prolog(entity, 4);
577
578         irg_block_walk_graph(irg, amd64_gen_labels, NULL, NULL);
579
580         n = ARR_LEN(blk_sched);
581         for (i = 0; i < n; i++) {
582                 ir_node *block = blk_sched[i];
583                 ir_node *next  = (i + 1) < n ? blk_sched[i+1] : NULL;
584
585                 set_irn_link(block, next);
586         }
587
588         for (i = 0; i < n; ++i) {
589                 ir_node *block = blk_sched[i];
590
591                 amd64_gen_block(block, 0);
592         }
593
594         be_gas_emit_function_epilog(entity);
595         be_dbg_method_end();
596         be_emit_char('\n');
597         be_emit_write_line();
598 }