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