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 /**
355  * Returns the target label for a control flow node.
356  */
357 static char *get_cfop_target(const ir_node *irn, char *buf) {
358         ir_node *bl = get_irn_link(irn);
359
360         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
361         return buf;
362 }
363
364 /**
365  * Emit a SymConst
366  */
367 static void emit_arm_SymConst(ir_node *irn, void *env) {
368         arm_emit_env_t *emit_env = env;
369         FILE *F = emit_env->out;
370         SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
371         const lc_arg_env_t *arg_env = arm_get_arg_env();
372         char cmd_buf[256];
373
374         entry->label      = get_unique_label();
375         entry->symconst   = irn;
376         entry->next       = emit_env->symbols;
377         emit_env->symbols = entry;
378
379         lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
380         arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
381 }
382
383 /**
384  * Returns the next block in a block schedule.
385  */
386 static ir_node *sched_next_block(ir_node *block) {
387     return get_irn_link(block);
388 }
389
390 /**
391  * Emit a conditional jump.
392  */
393 static void emit_arm_CondJmp(ir_node *irn, void *env) {
394         arm_emit_env_t *emit_env = env;
395         FILE *out = emit_env->out;
396         const ir_edge_t *edge;
397         ir_node *true_block = NULL;
398         ir_node *false_block = NULL;
399         ir_node *op1 = get_irn_n(irn, 0);
400         ir_mode *opmode = get_irn_mode(op1);
401         char *suffix;
402         int proj_num = get_arm_proj_num(irn);
403         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
404
405
406         foreach_out_edge(irn, edge) {
407                 ir_node *proj = get_edge_src_irn(edge);
408                 long nr = get_Proj_proj(proj);
409                 ir_node *block = get_irn_link(proj);
410                 if ( nr == pn_Cond_true) {
411                         true_block = block;
412                 } else if (nr == pn_Cond_false) {
413                         false_block = block;
414                 } else {
415                         assert(0 && "tertium non datur! (CondJmp)");
416                 }
417         }
418
419         if (proj_num == pn_Cmp_False) {
420                 /* always false: should not happen */
421                 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
422         } else if (proj_num == pn_Cmp_True) {
423                 /* always true: should not happen */
424                 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
425         } else {
426                 ir_node *block = get_nodes_block(irn);
427
428                 if (mode_is_float(opmode)) {
429                         suffix = "ICHWILLIMPLEMENTIERTWERDEN";
430
431                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
432                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
433                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
434
435                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
436                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
437                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
438                 } else {
439                         if (true_block == sched_next_block(block)) {
440                                 /* negate it */
441                                 proj_num = get_negated_pnc(proj_num, opmode);
442                         }
443                         switch(proj_num) {
444                                 case pn_Cmp_Eq:  suffix = "eq"; break;
445                                 case pn_Cmp_Lt:  suffix = "lt"; break;
446                                 case pn_Cmp_Le:  suffix = "le"; break;
447                                 case pn_Cmp_Gt:  suffix = "gt"; break;
448                                 case pn_Cmp_Ge:  suffix = "ge"; break;
449                                 case pn_Cmp_Lg:  suffix = "ne"; break;
450                                 case pn_Cmp_Leg: suffix = "al"; break;
451                         default: assert(0 && "komische Dinge geschehen"); suffix = "al";
452                         }
453
454                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
455                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
456                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
457                 }
458
459                 if (true_block == sched_next_block(block)) {
460                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
461                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
462                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
463
464                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
465                         arm_fprintf_format(out, "", cmnt_buf, irn);
466                 }
467                 else {
468                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
469                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
470                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
471
472             if (false_block == sched_next_block(block)) {
473                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
474                 arm_fprintf_format(out, "", cmnt_buf, irn);
475             }
476             else {
477                             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(false_block));
478                             lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
479                             arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
480             }
481         }
482         }
483 }
484
485 static void emit_arm_CopyB(ir_node *irn, void *env) {
486         arm_emit_env_t *emit_env = env;
487         FILE *out = emit_env->out;
488         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
489         unsigned int size = get_tarval_long(get_arm_value(irn));
490         const lc_arg_env_t *arg_env = arm_get_arg_env();
491
492         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);
493
494         assert ( size > 0 && "CopyB needs size > 0" );
495         if (size & 3)
496                 size += 4;
497         size >>= 2;
498         switch(size & 3) {
499         case 0:
500                 break;
501         case 1:
502                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
503                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
504                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str  %%r12, [%1S, #0]!", irn);
505                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
506                 break;
507         case 2:
508                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
509                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
510                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
511                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
512                 break;
513         case 3:
514                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
515                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
516                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
517                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
518                 break;
519         }
520         size >>= 2;
521         while (size) {
522                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
523                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
524                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
525                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
526                 --size;
527         }
528 }
529
530 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
531         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
532         const ir_edge_t    *edge;
533         ir_node            *proj;
534         arm_emit_env_t *emit_env = env;
535         FILE *out = emit_env->out;
536         int i;
537         ir_node **projs;
538         int n_projs;
539         int block_nr;
540         int default_block_num;
541
542         block_nr = get_irn_node_nr(irn);
543         n_projs = get_arm_n_projs(irn);
544
545         projs = xcalloc(n_projs , sizeof(ir_node*));
546
547         foreach_out_edge(irn, edge) {
548                 proj = get_edge_src_irn(edge);
549                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
550
551                 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
552                         default_block_num = get_irn_node_nr(get_irn_link(proj));
553
554                 projs[get_Proj_proj(proj)] = proj;
555         }
556
557         // CMP %1S, n_projs - 1
558         // BHI default
559
560
561
562         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
563         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
564         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
565
566         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
567         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
568         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
569
570
571         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
572         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
573         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
574
575         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
576         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
577         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
578
579         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
580         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
581         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
582
583         // LDR %r12, .TABLE_X_START
584         // ADD %r12, %r12, [%1S, LSL #2]
585         // LDR %r15, %r12
586
587         fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
588         fprintf(out, "\t.align 2\n");
589         fprintf(out, "TABLE_%d:\n", block_nr);
590
591
592         for ( i=0; i<n_projs; i++) {
593                 ir_node *block;
594                 proj = projs[i];
595                 if ( proj ) {
596                         block = get_irn_link(proj);
597                 } else {
598                         block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
599                 }
600                 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
601         }
602         fprintf(out, "\t.align 2\n");
603
604         xfree(projs);
605 }
606
607 /************************************************************************/
608 /* emit_be                                                              */
609 /************************************************************************/
610
611 static void emit_be_Call(ir_node *irn, void *env) {
612         arm_emit_env_t *emit_env = env;
613         FILE *F = emit_env->out;
614         ir_entity *ent = be_Call_get_entity(irn);
615     char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
616
617     if (ent)
618             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
619     else
620         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
621     lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
622     arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
623 }
624
625 /** Emit an IncSP node */
626 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
627         FILE *F = emit_env->out;
628         int offs = be_get_IncSP_offset(irn);
629
630         if (offs != 0) {
631                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
632                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %1D, %1S, #%O", irn, irn, irn );
633                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
634                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
635         } else {
636                 char cmnt_buf[SNPRINTF_BUF_LEN];
637                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
638                 arm_fprintf_format(F, "", cmnt_buf, irn);
639         }
640 }
641
642 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
643 //      FILE *F = emit_env->out;
644 //      char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
645 //      lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
646 //      lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
647 //      lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
648 // }
649
650 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
651         FILE *F    = emit_env->out;
652         ir_mode *mode = get_irn_mode(irn);
653         const lc_arg_env_t *arm_env = arm_get_arg_env();
654         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
655
656         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
657                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
658                 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
659                 return;
660         }
661
662         if (mode_is_float(mode)) {
663                 if (USE_FPA(emit_env->cg->isa)) {
664                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
665                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
666                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
667                 }
668                 else {
669                         assert(0 && "move not supported for this mode");
670                 }
671         } else if (mode_is_numP(mode)) {
672                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
673                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
674                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
675         } else {
676                 assert(0 && "move not supported for this mode");
677         }
678 //      emit_arm_Copy(irn, emit_env);
679 }
680
681 /**
682  * Emit code for a Spill.
683  */
684 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
685         FILE *F = emit_env->out;
686         ir_mode *mode = get_irn_mode(irn);
687         const lc_arg_env_t *arm_env = arm_get_arg_env();
688         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
689
690         if (mode_is_float(mode)) {
691                 if (USE_FPA(emit_env->cg->isa)) {
692                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
693                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
694                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
695                 }
696                 else {
697                         assert(0 && "move not supported for this mode");
698                 }
699         } else if (mode_is_dataM(mode)) {
700                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %2S, [%1S, #%O]", irn, irn, irn );
701                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
702                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
703         } else {
704                 assert(0 && "spill not supported for this mode");
705         }
706 }
707
708 /**
709  * Emit code for a Reload.
710  */
711 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
712         FILE *F = emit_env->out;
713         ir_mode *mode = get_irn_mode(irn);
714         const lc_arg_env_t *arm_env = arm_get_arg_env();
715         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
716
717         if (mode_is_float(mode)) {
718                 if (USE_FPA(emit_env->cg->isa)) {
719                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
720                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
721                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
722                 }
723                 else {
724                         assert(0 && "move not supported for this mode");
725                 }
726         } else if (mode_is_dataM(mode)) {
727                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
728                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
729                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
730         } else {
731                 assert(0 && "reload not supported for this mode");
732         }
733 }
734
735 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
736         FILE *F = emit_env->out;
737         const lc_arg_env_t *arm_env = arm_get_arg_env();
738         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
739
740         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
741         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
742         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
743
744         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
745         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
746         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
747
748         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
749         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* end Perm(%1S, %2S) */", irn, irn);
750         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
751 }
752
753 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
754         FILE *F = emit_env->out;
755         ir_mode *mode = get_irn_mode(irn);
756         const lc_arg_env_t *arm_env = arm_get_arg_env();
757         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
758
759         if (mode_is_float(mode)) {
760                 if (USE_FPA(emit_env->cg->isa)) {
761                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
762                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
763                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
764                 }
765                 else {
766                         assert(0 && "move not supported for this mode");
767                 }
768         } else {
769                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
770                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
771                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
772         }
773 }
774
775 /************************************************************************/
776 /* emit                                                                 */
777 /************************************************************************/
778
779 static void emit_Jmp(ir_node *irn, void *env) {
780         char cmd_buf[SNPRINTF_BUF_LEN];
781         arm_emit_env_t *emit_env = env;
782         FILE *F = emit_env->out;
783         const ir_edge_t *edge = get_irn_out_edge_first(irn);
784         const lc_arg_env_t *arm_env = arm_get_arg_env();
785         ir_node *target_block = get_edge_src_irn(edge);
786
787         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "b BLOCK_%d", get_irn_node_nr(target_block));
788         arm_fprintf_format(F, cmd_buf, "/* unconditional Jump */", irn);
789 }
790
791 static void emit_arm_fpaDbl2GP(const ir_node *n, arm_emit_env_t *env) {
792   FILE *F = env->out;
793   char cmd_buf[256];
794   const lc_arg_env_t *arg_env = arm_get_arg_env();
795
796   lc_esnprintf(arg_env, cmd_buf, 256, "stfd %1S, [sp, #-8]! ", n);
797   arm_fprintf_format(F, cmd_buf, "/* Push fp to stack */", n);
798
799   lc_esnprintf(arg_env, cmd_buf, 256, "ldmfd sp!, {%2D, %1D} ", n, n);
800   arm_fprintf_format(F, cmd_buf, "/* Pop destination */", n);
801 }
802
803 static void emit_silence(ir_node *irn, void *env) {
804
805 }
806
807
808 /***********************************************************************************
809  *                  _          __                                             _
810  *                 (_)        / _|                                           | |
811  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
812  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
813  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
814  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
815  *
816  ***********************************************************************************/
817
818 /**
819  * Enters the emitter functions for handled nodes into the generic
820  * pointer of an opcode.
821  */
822 static void arm_register_emitters(void) {
823
824 #define ARM_EMIT(a)  op_arm_##a->ops.generic = (op_func)emit_arm_##a
825 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
826 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
827 #define SILENCE(a)   op_##a->ops.generic = (op_func)emit_silence
828
829         /* first clear the generic function pointer for all ops */
830         clear_irp_opcodes_generic_func();
831
832         /* register all emitter functions defined in spec */
833         arm_register_spec_emitters();
834
835         /* other emitter functions */
836         ARM_EMIT(CondJmp);
837         ARM_EMIT(CopyB);
838 //      ARM_EMIT(CopyB_i);
839 //      ARM_EMIT(Const);
840         ARM_EMIT(SymConst);
841         ARM_EMIT(SwitchJmp);
842         ARM_EMIT(fpaDbl2GP);
843
844         /* benode emitter */
845         BE_EMIT(Call);
846         BE_EMIT(IncSP);
847 //      BE_EMIT(AddSP);
848         BE_EMIT(Copy);
849         BE_EMIT(Spill);
850         BE_EMIT(Reload);
851         BE_EMIT(Perm);
852         BE_EMIT(StackParam);
853
854         /* firm emitter */
855         EMIT(Jmp);
856
857         /* noisy stuff */
858 #ifdef SILENCER
859         SILENCE(Proj);
860         SILENCE(Phi);
861         SILENCE(be_Keep);
862         SILENCE(be_CopyKeep);
863 #endif
864
865 #undef ARM_EMIT
866 #undef BE_EMIT
867 #undef EMIT
868 #undef SILENCE
869 }
870
871 typedef void (node_emitter)(const ir_node *, void *);
872
873 /**
874  * Emits code for a node.
875  */
876 static void arm_emit_node(const ir_node *irn, void *env) {
877         arm_emit_env_t *emit_env = env;
878         FILE           *F        = emit_env->out;
879         ir_op          *op       = get_irn_op(irn);
880         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
881
882         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
883
884         if (op->ops.generic) {
885                 node_emitter *emit = (node_emitter *)op->ops.generic;
886                 emit(irn, env);
887         }
888         else {
889                 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
890         }
891 }
892
893 /**
894  * Walks over the nodes in a block connected by scheduling edges
895  * and emits code for each node.
896  */
897 void arm_gen_block(ir_node *block, void *env) {
898         ir_node *irn;
899
900         fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
901         sched_foreach(block, irn) {
902                 arm_emit_node(irn, env);
903         }
904 }
905
906
907 /**
908  * Emits code for function start.
909  */
910 void arm_emit_start(FILE *F, ir_graph *irg) {
911         ir_entity *ent = get_irg_entity(irg);
912         const char *irg_name = get_entity_ld_name(ent);
913         arm_switch_section(F, SECTION_TEXT);
914         fprintf(F, "\t.align  2\n");
915
916         if (get_entity_visibility(ent) == visibility_external_visible)
917                 fprintf(F, "\t.global %s\n", irg_name);
918         fprintf(F, "%s:\n", irg_name);
919 }
920
921 /**
922  * Emits code for function end
923  */
924 void arm_emit_end(FILE *F, ir_graph *irg) {
925         fprintf(F, "\t.ident \"firmcc\"\n");
926 }
927
928 /**
929  * Sets labels for control flow nodes (jump target)
930  * TODO: Jump optimization
931  */
932 void arm_gen_labels(ir_node *block, void *env) {
933         ir_node *pred;
934         int n = get_Block_n_cfgpreds(block);
935
936         for (n--; n >= 0; n--) {
937                 pred = get_Block_cfgpred(block, n);
938                 set_irn_link(pred, block);
939         }
940 }
941
942
943 /**
944  * Main driver. Emits the code for one routine.
945  */
946 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
947         SymConstEntry *entry;
948         arm_emit_env_t emit_env;
949     ir_node **blk_sched;
950     int i, n;
951
952         emit_env.out      = F;
953         emit_env.arch_env = cg->arch_env;
954         emit_env.cg       = cg;
955         emit_env.symbols  = NULL;
956         obstack_init(&emit_env.obst);
957         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
958
959         /* set the global arch_env (needed by print hooks) */
960         arch_env = cg->arch_env;
961
962         arm_register_emitters();
963
964         /* create the block schedule. For now, we don't need it earlier. */
965         blk_sched = sched_create_block_schedule(cg->irg, cg->birg->exec_freq);
966
967         arm_emit_start(F, irg);
968         irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
969
970         n = ARR_LEN(blk_sched);
971         for (i = 0; i < n;) {
972                 ir_node *block, *next_bl;
973
974                 block   = blk_sched[i];
975                 ++i;
976                 next_bl = i < n ? blk_sched[i] : NULL;
977
978                 /* set here the link. the emitter expects to find the next block here */
979                 set_irn_link(block, next_bl);
980                 arm_gen_block(block, &emit_env);
981         }
982
983         /* emit SymConst values */
984         if (emit_env.symbols)
985                 fprintf(F, "\t.align 2\n");
986
987         for (entry = emit_env.symbols; entry; entry = entry->next) {
988                 fprintf(F, ".L%u:\n", entry->label);
989                 lc_efprintf(arm_get_arg_env(), F, "\t.word\t%C\n", entry->symconst);
990         }
991
992         obstack_free(&emit_env.obst, NULL);
993 }