warning fixes
[libfirm] / ir / be / arm / arm_emitter.c
1 #define SILENCER
2 /* arm emitter */
3 /* $Id$ */
4
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
8
9 #include <limits.h>
10
11 #include "xmalloc.h"
12 #include "tv.h"
13 #include "iredges.h"
14 #include "debug.h"
15 #include "irgwalk.h"
16 #include "irprintf.h"
17 #include "irop_t.h"
18 #include "irprog_t.h"
19 #include "irargs_t.h"
20
21 #include "../besched.h"
22
23 #include "arm_emitter.h"
24 #include "gen_arm_emitter.h"
25 #include "arm_nodes_attr.h"
26 #include "arm_new_nodes.h"
27 #include "arm_map_regs.h"
28 #include "gen_arm_regalloc_if.h"
29
30 #include "../benode_t.h"
31
32 #define SNPRINTF_BUF_LEN 128
33
34 static const arch_env_t *arch_env = NULL;
35
36 /**
37  * Switch to a new section
38  */
39 void arm_switch_section(FILE *f, sections sec) {
40         static sections curr_sec = NO_SECTION;
41
42         if (curr_sec == sec)
43                 return;
44
45         curr_sec = sec;
46         switch (sec) {
47
48         case NO_SECTION:
49                 break;
50
51         case SECTION_TEXT:
52                 fprintf(f, "\t.text\n");
53                 break;
54
55         case SECTION_DATA:
56                 fprintf(f, "\t.data\n");
57                 break;
58
59         default:
60                 assert(0);
61         }
62 }
63
64 /*************************************************************
65  *             _       _    __   _          _
66  *            (_)     | |  / _| | |        | |
67  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
68  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
69  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
70  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
71  * | |                                       | |
72  * |_|                                       |_|
73  *************************************************************/
74
75 /**
76  * Returns non-zero if a mode has a Immediate attribute.
77  */
78 int is_immediate_node(ir_node *irn) {
79         arm_attr_t *attr = get_arm_attr(irn);
80         return ARM_GET_SHF_MOD(attr) == ARM_SHF_IMM;
81 }
82
83 /**
84  * Return a const or SymConst as string.
85  */
86 static const char *node_const_to_str(ir_node *n, char *buf, int buflen) {
87         if (is_immediate_node(n)) {
88                 snprintf(buf, buflen, "#0x%X", arm_decode_imm_w_shift(get_arm_value(n)));
89                 return buf;
90         }
91         else if (is_arm_SymConst(n))
92                 return get_arm_symconst_label(n);
93
94         assert( 0 && "das ist gar keine Konstante");
95         return NULL;
96 }
97
98 /**
99  * Returns node's offset as string.
100  */
101 static const char *node_offset_to_str(ir_node *n, char *buf, int buflen) {
102         int offset = 0;
103         ir_op *irn_op = get_irn_op(n);
104
105         if (irn_op == op_be_StackParam) {
106                 ir_entity *ent = be_get_frame_entity(n);
107                 offset = get_entity_offset(ent);
108         } else if (irn_op == op_be_Reload || irn_op == op_be_Spill) {
109                 ir_entity *ent = be_get_frame_entity(n);
110                 offset = get_entity_offset(ent);
111         } else if (irn_op == op_be_IncSP) {
112                 offset = - be_get_IncSP_offset(n);
113         } else {
114                 return "node_offset_to_str will fuer diesen Knotentyp noch implementiert werden";
115         }
116         snprintf(buf, buflen, "%d", offset);
117         return buf;
118 }
119
120 /* We always pass the ir_node which is a pointer. */
121 static int arm_get_arg_type(const lc_arg_occ_t *occ) {
122         return lc_arg_type_ptr;
123 }
124
125
126 /**
127  * Returns the register at in position pos.
128  */
129 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
130         ir_node                *op;
131         const arch_register_t  *reg = NULL;
132
133         assert(get_irn_arity(irn) > pos && "Invalid IN position");
134
135         /* The out register of the operator at position pos is the
136            in register we need. */
137         op = get_irn_n(irn, pos);
138
139         reg = arch_get_irn_register(arch_env, op);
140
141         assert(reg && "no in register found");
142         return reg;
143 }
144
145 /**
146  * Returns the register at out position pos.
147  */
148 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
149         ir_node                *proj;
150         const arch_register_t  *reg = NULL;
151
152         assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
153
154         /* 1st case: irn is not of mode_T, so it has only                 */
155         /*           one OUT register -> good                             */
156         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
157         /*           Proj with the corresponding projnum for the register */
158
159         if (get_irn_mode(irn) != mode_T) {
160                 reg = arch_get_irn_register(arch_env, irn);
161         }
162         else if (is_arm_irn(irn)) {
163                 reg = get_arm_out_reg(irn, pos);
164         }
165         else {
166                 const ir_edge_t *edge;
167
168                 foreach_out_edge(irn, edge) {
169                         proj = get_edge_src_irn(edge);
170                         assert(is_Proj(proj) && "non-Proj from mode_T node");
171                         if (get_Proj_proj(proj) == pos) {
172                                 reg = arch_get_irn_register(arch_env, proj);
173                                 break;
174                         }
175                 }
176         }
177
178         assert(reg && "no out register found");
179         return reg;
180 }
181
182 /**
183  * Returns the number of the in register at position pos.
184  */
185 int get_arm_reg_nr(ir_node *irn, int pos, int in_out) {
186         const arch_register_t *reg;
187
188         if (in_out == 1) {
189                 reg = get_in_reg(irn, pos);
190         }
191         else {
192                 reg = get_out_reg(irn, pos);
193         }
194
195         return arch_register_get_index(reg);
196 }
197
198 /**
199  * Returns the name of the in register at position pos.
200  */
201 const char *get_arm_reg_name(ir_node *irn, int pos, int in_out) {
202         const arch_register_t *reg;
203
204         if (in_out == 1) {
205                 reg = get_in_reg(irn, pos);
206         }
207         else {
208                 reg = get_out_reg(irn, pos);
209         }
210
211         return arch_register_get_name(reg);
212 }
213
214 /**
215  * Get the register name for a node.
216  */
217 static int arm_get_reg_name(lc_appendable_t *app,
218     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
219 {
220         const char *buf;
221         ir_node    *X  = arg->v_ptr;
222         int         nr = occ->width - 1;
223
224         if (!X)
225                 return lc_appendable_snadd(app, "(null)", 6);
226
227         if (occ->conversion == 'S') {
228                 buf = get_arm_reg_name(X, nr, 1);
229         }
230         else { /* 'D' */
231                 buf = get_arm_reg_name(X, nr, 0);
232         }
233
234         lc_appendable_chadd(app, '%');
235         return lc_appendable_snadd(app, buf, strlen(buf));
236 }
237
238 /**
239  * Returns the tarval or offset of an arm node as a string.
240  */
241 static int arm_const_to_str(lc_appendable_t *app,
242     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
243 {
244         char buffer[SNPRINTF_BUF_LEN];
245         const char *buf;
246         ir_node    *X = arg->v_ptr;
247
248         if (!X)
249                 return lc_appendable_snadd(app, "(null)", 6);
250
251         if (occ->conversion == 'C') {
252                 buf = node_const_to_str(X, buffer, sizeof(buffer));
253         }
254         else { /* 'O' */
255                 buf = node_offset_to_str(X, buffer, sizeof(buffer));
256         }
257
258         return lc_appendable_snadd(app, buf, strlen(buf));
259 }
260
261 /**
262  * Returns the tarval or offset of an arm node as a string.
263  */
264 static int arm_shift_str(lc_appendable_t *app,
265     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
266 {
267         char buffer[SNPRINTF_BUF_LEN];
268         ir_node            *irn = arg->v_ptr;
269         arm_shift_modifier mod;
270
271         if (!irn)
272                 return lc_appendable_snadd(app, "(null)", 6);
273
274         mod = get_arm_shift_modifier(irn);
275         if (ARM_HAS_SHIFT(mod)) {
276                 long v = get_tarval_long(get_arm_value(irn));
277
278                 snprintf(buffer, sizeof(buffer), ", %s #%ld", arm_shf_mod_name(mod), v);
279                 return lc_appendable_snadd(app, buffer, strlen(buffer));
280         }
281         return 0;
282 }
283
284 /**
285  * Determines the instruction suffix depending on the mode.
286  */
287 static int arm_get_mode_suffix(lc_appendable_t *app,
288     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
289 {
290         ir_node *irn = arg->v_ptr;
291         arm_attr_t *attr;
292         ir_mode *mode;
293         int bits;
294
295         if (! irn)
296                 return lc_appendable_snadd(app, "(null)", 6);
297
298         attr = get_arm_attr(irn);
299         mode = attr->op_mode ? attr->op_mode : get_irn_mode(irn);
300         bits = get_mode_size_bits(mode);
301
302         if (bits == 32)
303                 return lc_appendable_chadd(app, 's');
304         else if (bits == 64)
305                 return lc_appendable_chadd(app, 'd');
306         else
307                 return lc_appendable_chadd(app, 'e');
308 }
309
310 /**
311  * Return the arm printf arg environment.
312  * We use the firm environment with some additional handlers.
313  */
314 const lc_arg_env_t *arm_get_arg_env(void) {
315         static lc_arg_env_t *env = NULL;
316
317         static const lc_arg_handler_t arm_reg_handler   = { arm_get_arg_type, arm_get_reg_name };
318         static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
319         static const lc_arg_handler_t arm_mode_handler  = { arm_get_arg_type, arm_get_mode_suffix };
320         static const lc_arg_handler_t arm_shf_handler   = { arm_get_arg_type, arm_shift_str };
321
322         if (env == NULL) {
323                 /* extend the firm printer */
324                 env = firm_get_arg_env();
325                         //lc_arg_new_env();
326
327                 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
328                 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
329                 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
330                 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
331                 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
332                 lc_arg_register(env, "arm:shf",  'X', &arm_shf_handler);
333         }
334
335         return env;
336 }
337
338 /**
339  * Formated print of commands and comments.
340  */
341 static void arm_fprintf_format(FILE *F, const char *cmd_buf, const char *cmnt_buf, const ir_node *irn) {
342         lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F (%G) */\n", cmd_buf, cmnt_buf, irn, irn);
343 }
344
345 /**
346  * Returns a unique label. This number will not be used a second time.
347  */
348 static unsigned get_unique_label(void) {
349         static unsigned id = 0;
350         return ++id;
351 }
352
353 /**
354  * Emit a SymConst
355  */
356 static void emit_arm_SymConst(ir_node *irn, void *env) {
357         arm_emit_env_t *emit_env = env;
358         FILE *F = emit_env->out;
359         SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
360         const lc_arg_env_t *arg_env = arm_get_arg_env();
361         char cmd_buf[256];
362
363         entry->label      = get_unique_label();
364         entry->symconst   = irn;
365         entry->next       = emit_env->symbols;
366         emit_env->symbols = entry;
367
368         lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
369         arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
370 }
371
372 /**
373  * Returns the next block in a block schedule.
374  */
375 static ir_node *sched_next_block(ir_node *block) {
376     return get_irn_link(block);
377 }
378
379 /**
380  * Emit a conditional jump.
381  */
382 static void emit_arm_CondJmp(ir_node *irn, void *env) {
383         arm_emit_env_t *emit_env = env;
384         FILE *out = emit_env->out;
385         const ir_edge_t *edge;
386         ir_node *true_block = NULL;
387         ir_node *false_block = NULL;
388         ir_node *op1 = get_irn_n(irn, 0);
389         ir_mode *opmode = get_irn_mode(op1);
390         char *suffix;
391         int proj_num = get_arm_proj_num(irn);
392         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
393
394
395         foreach_out_edge(irn, edge) {
396                 ir_node *proj = get_edge_src_irn(edge);
397                 long nr = get_Proj_proj(proj);
398                 ir_node *block = get_irn_link(proj);
399                 if ( nr == pn_Cond_true) {
400                         true_block = block;
401                 } else if (nr == pn_Cond_false) {
402                         false_block = block;
403                 } else {
404                         assert(0 && "tertium non datur! (CondJmp)");
405                 }
406         }
407
408         if (proj_num == pn_Cmp_False) {
409                 /* always false: should not happen */
410                 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
411         } else if (proj_num == pn_Cmp_True) {
412                 /* always true: should not happen */
413                 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
414         } else {
415                 ir_node *block = get_nodes_block(irn);
416
417                 if (mode_is_float(opmode)) {
418                         suffix = "ICHWILLIMPLEMENTIERTWERDEN";
419
420                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
421                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
422                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
423
424                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
425                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
426                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
427                 } else {
428                         if (true_block == sched_next_block(block)) {
429                                 /* negate it */
430                                 proj_num = get_negated_pnc(proj_num, opmode);
431                         }
432                         switch(proj_num) {
433                                 case pn_Cmp_Eq:  suffix = "eq"; break;
434                                 case pn_Cmp_Lt:  suffix = "lt"; break;
435                                 case pn_Cmp_Le:  suffix = "le"; break;
436                                 case pn_Cmp_Gt:  suffix = "gt"; break;
437                                 case pn_Cmp_Ge:  suffix = "ge"; break;
438                                 case pn_Cmp_Lg:  suffix = "ne"; break;
439                                 case pn_Cmp_Leg: suffix = "al"; break;
440                         default: assert(0 && "komische Dinge geschehen"); suffix = "al";
441                         }
442
443                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
444                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
445                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
446                 }
447
448                 if (true_block == sched_next_block(block)) {
449                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
450                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
451                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
452
453                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
454                         arm_fprintf_format(out, "", cmnt_buf, irn);
455                 }
456                 else {
457                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
458                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
459                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
460
461             if (false_block == sched_next_block(block)) {
462                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
463                 arm_fprintf_format(out, "", cmnt_buf, irn);
464             }
465             else {
466                             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(false_block));
467                             lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
468                             arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
469             }
470         }
471         }
472 }
473
474 static void emit_arm_CopyB(ir_node *irn, void *env) {
475         arm_emit_env_t *emit_env = env;
476         FILE *out = emit_env->out;
477         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
478         unsigned int size = get_tarval_long(get_arm_value(irn));
479         const lc_arg_env_t *arg_env = arm_get_arg_env();
480
481         lc_esnprintf(arg_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* MemCopy (%2S)->(%1S) [%d bytes], Use %3S, %4S, %5S and %%r12 */", irn, irn, size, irn, irn, irn);
482
483         assert ( size > 0 && "CopyB needs size > 0" );
484         if (size & 3)
485                 size += 4;
486         size >>= 2;
487         switch(size & 3) {
488         case 0:
489                 break;
490         case 1:
491                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
492                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
493                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str  %%r12, [%1S, #0]!", irn);
494                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
495                 break;
496         case 2:
497                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
498                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
499                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
500                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
501                 break;
502         case 3:
503                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
504                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
505                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
506                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
507                 break;
508         }
509         size >>= 2;
510         while (size) {
511                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
512                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
513                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
514                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
515                 --size;
516         }
517 }
518
519 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
520         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
521         const ir_edge_t    *edge;
522         ir_node            *proj;
523         arm_emit_env_t *emit_env = env;
524         FILE *out = emit_env->out;
525         int i;
526         ir_node **projs;
527         int n_projs;
528         int block_nr;
529         int default_block_num;
530
531         block_nr = get_irn_node_nr(irn);
532         n_projs = get_arm_n_projs(irn);
533
534         projs = xcalloc(n_projs , sizeof(ir_node*));
535
536         foreach_out_edge(irn, edge) {
537                 proj = get_edge_src_irn(edge);
538                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
539
540                 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
541                         default_block_num = get_irn_node_nr(get_irn_link(proj));
542
543                 projs[get_Proj_proj(proj)] = proj;
544         }
545
546         // CMP %1S, n_projs - 1
547         // BHI default
548
549
550
551         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
552         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
553         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
554
555         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
556         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
557         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
558
559
560         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
561         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
562         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
563
564         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
565         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
566         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
567
568         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
569         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
570         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
571
572         // LDR %r12, .TABLE_X_START
573         // ADD %r12, %r12, [%1S, LSL #2]
574         // LDR %r15, %r12
575
576         fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
577         fprintf(out, "\t.align 2\n");
578         fprintf(out, "TABLE_%d:\n", block_nr);
579
580
581         for ( i=0; i<n_projs; i++) {
582                 ir_node *block;
583                 proj = projs[i];
584                 if ( proj ) {
585                         block = get_irn_link(proj);
586                 } else {
587                         block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
588                 }
589                 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
590         }
591         fprintf(out, "\t.align 2\n");
592
593         xfree(projs);
594 }
595
596 /************************************************************************/
597 /* emit_be                                                              */
598 /************************************************************************/
599
600 static void emit_be_Call(ir_node *irn, void *env) {
601         arm_emit_env_t *emit_env = env;
602         FILE *F = emit_env->out;
603         ir_entity *ent = be_Call_get_entity(irn);
604     char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
605
606     if (ent)
607             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
608     else
609         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
610     lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
611     arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
612 }
613
614 /** Emit an IncSP node */
615 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
616         FILE *F = emit_env->out;
617         int offs = be_get_IncSP_offset(irn);
618
619         if (offs != 0) {
620                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
621                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %1S, #%O", irn, irn, irn );
622                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
623                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
624         } else {
625                 char cmnt_buf[SNPRINTF_BUF_LEN];
626                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
627                 arm_fprintf_format(F, "", cmnt_buf, irn);
628         }
629 }
630
631 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
632 //      FILE *F = emit_env->out;
633 //      char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
634 //      lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
635 //      lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
636 //      lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
637 // }
638
639 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
640         FILE *F    = emit_env->out;
641         ir_mode *mode = get_irn_mode(irn);
642         const lc_arg_env_t *arm_env = arm_get_arg_env();
643         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
644
645         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
646                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
647                 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
648                 return;
649         }
650
651         if (mode_is_float(mode)) {
652                 if (USE_FPA(emit_env->cg->isa)) {
653                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
654                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
655                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
656                 }
657                 else {
658                         assert(0 && "move not supported for this mode");
659                 }
660         } else if (mode_is_numP(mode)) {
661                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
662                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
663                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
664         } else {
665                 assert(0 && "move not supported for this mode");
666         }
667 //      emit_arm_Copy(irn, emit_env);
668 }
669
670 /**
671  * Emit code for a Spill.
672  */
673 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
674         FILE *F = emit_env->out;
675         ir_mode *mode = get_irn_mode(irn);
676         const lc_arg_env_t *arm_env = arm_get_arg_env();
677         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
678
679         if (mode_is_float(mode)) {
680                 if (USE_FPA(emit_env->cg->isa)) {
681                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
682                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
683                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
684                 }
685                 else {
686                         assert(0 && "move not supported for this mode");
687                 }
688         } else if (mode_is_dataM(mode)) {
689                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %2S, [%1S, #%O]", irn, irn, irn );
690                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
691                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
692         } else {
693                 assert(0 && "spill not supported for this mode");
694         }
695 }
696
697 /**
698  * Emit code for a Reload.
699  */
700 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
701         FILE *F = emit_env->out;
702         ir_mode *mode = get_irn_mode(irn);
703         const lc_arg_env_t *arm_env = arm_get_arg_env();
704         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
705
706         if (mode_is_float(mode)) {
707                 if (USE_FPA(emit_env->cg->isa)) {
708                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
709                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
710                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
711                 }
712                 else {
713                         assert(0 && "move not supported for this mode");
714                 }
715         } else if (mode_is_dataM(mode)) {
716                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
717                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
718                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
719         } else {
720                 assert(0 && "reload not supported for this mode");
721         }
722 }
723
724 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
725         FILE *F = emit_env->out;
726         const lc_arg_env_t *arm_env = arm_get_arg_env();
727         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
728
729         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
730         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
731         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
732
733         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
734         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
735         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
736
737         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
738         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* end Perm(%1S, %2S) */", irn, irn);
739         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
740 }
741
742 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
743         FILE *F = emit_env->out;
744         ir_mode *mode = get_irn_mode(irn);
745         const lc_arg_env_t *arm_env = arm_get_arg_env();
746         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
747
748         if (mode_is_float(mode)) {
749                 if (USE_FPA(emit_env->cg->isa)) {
750                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
751                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
752                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
753                 }
754                 else {
755                         assert(0 && "move not supported for this mode");
756                 }
757         } else {
758                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
759                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
760                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
761         }
762 }
763
764 /************************************************************************/
765 /* emit                                                                 */
766 /************************************************************************/
767
768 static void emit_Jmp(ir_node *irn, void *env) {
769         char cmd_buf[SNPRINTF_BUF_LEN];
770         arm_emit_env_t *emit_env = env;
771         FILE *F = emit_env->out;
772         const ir_edge_t *edge = get_irn_out_edge_first(irn);
773         const lc_arg_env_t *arm_env = arm_get_arg_env();
774         ir_node *target_block = get_edge_src_irn(edge);
775
776         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(target_block));
777         arm_fprintf_format(F, cmd_buf, "/* unconditional Jump */", irn);
778 }
779
780 static void emit_arm_fpaDbl2GP(const ir_node *n, arm_emit_env_t *env) {
781   FILE *F = env->out;
782   char cmd_buf[256];
783   const lc_arg_env_t *arg_env = arm_get_arg_env();
784
785   lc_esnprintf(arg_env, cmd_buf, 256, "stfd %1S, [sp, #-8]! ", n);
786   arm_fprintf_format(F, cmd_buf, "/* Push fp to stack */", n);
787
788   lc_esnprintf(arg_env, cmd_buf, 256, "ldmfd sp!, {%2D, %1D} ", n, n);
789   arm_fprintf_format(F, cmd_buf, "/* Pop destination */", n);
790 }
791
792 static void emit_silence(ir_node *irn, void *env) {
793
794 }
795
796
797 /***********************************************************************************
798  *                  _          __                                             _
799  *                 (_)        / _|                                           | |
800  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
801  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
802  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
803  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
804  *
805  ***********************************************************************************/
806
807 /**
808  * Enters the emitter functions for handled nodes into the generic
809  * pointer of an opcode.
810  */
811 static void arm_register_emitters(void) {
812
813 #define ARM_EMIT(a)  op_arm_##a->ops.generic = (op_func)emit_arm_##a
814 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
815 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
816 #define SILENCE(a)   op_##a->ops.generic = (op_func)emit_silence
817
818         /* first clear the generic function pointer for all ops */
819         clear_irp_opcodes_generic_func();
820
821         /* register all emitter functions defined in spec */
822         arm_register_spec_emitters();
823
824         /* other emitter functions */
825         ARM_EMIT(CondJmp);
826         ARM_EMIT(CopyB);
827 //      ARM_EMIT(CopyB_i);
828 //      ARM_EMIT(Const);
829         ARM_EMIT(SymConst);
830         ARM_EMIT(SwitchJmp);
831         ARM_EMIT(fpaDbl2GP);
832
833         /* benode emitter */
834         BE_EMIT(Call);
835         BE_EMIT(IncSP);
836 //      BE_EMIT(AddSP);
837         BE_EMIT(Copy);
838         BE_EMIT(Spill);
839         BE_EMIT(Reload);
840         BE_EMIT(Perm);
841         BE_EMIT(StackParam);
842
843         /* firm emitter */
844         EMIT(Jmp);
845
846         /* noisy stuff */
847 #ifdef SILENCER
848         SILENCE(Proj);
849         SILENCE(Phi);
850         SILENCE(be_Keep);
851         SILENCE(be_CopyKeep);
852 #endif
853
854 #undef ARM_EMIT
855 #undef BE_EMIT
856 #undef EMIT
857 #undef SILENCE
858 }
859
860 typedef void (node_emitter)(const ir_node *, void *);
861
862 /**
863  * Emits code for a node.
864  */
865 static void arm_emit_node(const ir_node *irn, void *env) {
866         arm_emit_env_t *emit_env = env;
867         FILE           *F        = emit_env->out;
868         ir_op          *op       = get_irn_op(irn);
869         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
870
871         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
872
873         if (op->ops.generic) {
874                 node_emitter *emit = (node_emitter *)op->ops.generic;
875                 emit(irn, env);
876         }
877         else {
878                 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
879         }
880 }
881
882 /**
883  * Walks over the nodes in a block connected by scheduling edges
884  * and emits code for each node.
885  */
886 void arm_gen_block(ir_node *block, void *env) {
887         ir_node *irn;
888
889         fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
890         sched_foreach(block, irn) {
891                 arm_emit_node(irn, env);
892         }
893 }
894
895
896 /**
897  * Emits code for function start.
898  */
899 void arm_emit_start(FILE *F, ir_graph *irg) {
900         ir_entity *ent = get_irg_entity(irg);
901         const char *irg_name = get_entity_ld_name(ent);
902         arm_switch_section(F, SECTION_TEXT);
903         fprintf(F, "\t.align  2\n");
904
905         if (get_entity_visibility(ent) == visibility_external_visible)
906                 fprintf(F, "\t.global %s\n", irg_name);
907         fprintf(F, "%s:\n", irg_name);
908 }
909
910 /**
911  * Emits code for function end
912  */
913 void arm_emit_end(FILE *F, ir_graph *irg) {
914         fprintf(F, "\t.ident \"firmcc\"\n");
915 }
916
917 /**
918  * Sets labels for control flow nodes (jump target)
919  * TODO: Jump optimization
920  */
921 void arm_gen_labels(ir_node *block, void *env) {
922         ir_node *pred;
923         int n = get_Block_n_cfgpreds(block);
924
925         for (n--; n >= 0; n--) {
926                 pred = get_Block_cfgpred(block, n);
927                 set_irn_link(pred, block);
928         }
929 }
930
931
932 /**
933  * Main driver. Emits the code for one routine.
934  */
935 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
936         SymConstEntry *entry;
937         arm_emit_env_t emit_env;
938     ir_node **blk_sched;
939     int i, n;
940
941         emit_env.out      = F;
942         emit_env.arch_env = cg->arch_env;
943         emit_env.cg       = cg;
944         emit_env.symbols  = NULL;
945         obstack_init(&emit_env.obst);
946         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
947
948         /* set the global arch_env (needed by print hooks) */
949         arch_env = cg->arch_env;
950
951         arm_register_emitters();
952
953         /* create the block schedule. For now, we don't need it earlier. */
954         blk_sched = sched_create_block_schedule(cg->irg, cg->birg->exec_freq);
955
956         arm_emit_start(F, irg);
957         irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
958
959         n = ARR_LEN(blk_sched);
960         for (i = 0; i < n;) {
961                 ir_node *block, *next_bl;
962
963                 block   = blk_sched[i];
964                 ++i;
965                 next_bl = i < n ? blk_sched[i] : NULL;
966
967                 /* set here the link. the emitter expects to find the next block here */
968                 set_irn_link(block, next_bl);
969                 arm_gen_block(block, &emit_env);
970         }
971
972         /* emit SymConst values */
973         if (emit_env.symbols)
974                 fprintf(F, "\t.align 2\n");
975
976         for (entry = emit_env.symbols; entry; entry = entry->next) {
977                 fprintf(F, ".L%u:\n", entry->label);
978                 lc_efprintf(arm_get_arg_env(), F, "\t.word\t%C\n", entry->symconst);
979         }
980
981         obstack_free(&emit_env.obst, NULL);
982 }