Big Changes:
[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            *X = arg->v_ptr;
268         arm_shift_modifier mod;
269
270         if (!X)
271                 return lc_appendable_snadd(app, "(null)", 6);
272
273         mod = get_arm_shift_modifier(X);
274         if (ARM_HAS_SHIFT(mod)) {
275                 long v = get_tarval_long(get_arm_value(X));
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 *X = arg->v_ptr;
290
291         if (!X)
292                 return lc_appendable_snadd(app, "(null)", 6);
293
294         if (get_mode_size_bits(get_irn_mode(X)) == 32)
295                 return lc_appendable_chadd(app, 's');
296         else
297                 return lc_appendable_chadd(app, 'd');
298 }
299
300 /**
301  * Return the arm printf arg environment.
302  * We use the firm environment with some additional handlers.
303  */
304 const lc_arg_env_t *arm_get_arg_env(void) {
305         static lc_arg_env_t *env = NULL;
306
307         static const lc_arg_handler_t arm_reg_handler   = { arm_get_arg_type, arm_get_reg_name };
308         static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
309         static const lc_arg_handler_t arm_mode_handler  = { arm_get_arg_type, arm_get_mode_suffix };
310         static const lc_arg_handler_t arm_shf_handler   = { arm_get_arg_type, arm_shift_str };
311
312         if (env == NULL) {
313                 /* extend the firm printer */
314                 env = firm_get_arg_env();
315                         //lc_arg_new_env();
316
317                 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
318                 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
319                 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
320                 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
321                 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
322                 lc_arg_register(env, "arm:shf",  'X', &arm_shf_handler);
323         }
324
325         return env;
326 }
327
328 /**
329  * Formated print of commands and comments.
330  */
331 static void arm_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf, ir_node *irn) {
332         lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F (%G) */\n", cmd_buf, cmnt_buf, irn, irn);
333 }
334
335 /*
336  * Add a number to a prefix. This number will not be used a second time.
337  */
338 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
339         static unsigned long id = 0;
340         snprintf(buf, buflen, "%s%lu", prefix, ++id);
341         return buf;
342 }
343
344
345 /**
346  * Returns the target label for a control flow node.
347  */
348 static char *get_cfop_target(const ir_node *irn, char *buf) {
349         ir_node *bl = get_irn_link(irn);
350
351         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
352         return buf;
353 }
354
355 /************************************************************************/
356 /* emit_arm                                                             */
357 /************************************************************************/
358
359 static void emit_arm_SymConst(ir_node *irn, void *env) {
360         arm_emit_env_t *emit_env = env;
361         FILE *out = emit_env->out;
362         char buffer1[SNPRINTF_BUF_LEN];
363         char *skip_label = get_unique_label(buffer1, SNPRINTF_BUF_LEN, ".L");
364         char buffer2[SNPRINTF_BUF_LEN];
365         char *indi_label = get_unique_label(buffer2, SNPRINTF_BUF_LEN, ".L");
366         fprintf( out, "\tB %s\t\t\t\t\t/* start of indirect SymConst */\n", skip_label );
367         fprintf( out, "\t.align 2\n" );
368         fprintf( out, "%s:\n", indi_label );
369         lc_efprintf(arm_get_arg_env(), out, "\t.word\t%C\n", irn);
370         fprintf( out, "\t.align 2\n" );
371         fprintf( out, "%s:\n", skip_label );
372         lc_efprintf(arm_get_arg_env(), out, "\tLDR %1D, %s\t\t\t/* end of indirect SymConst */\n", irn, indi_label);
373 }
374
375 /**
376  * Returns the next block in a block schedule.
377  */
378 static ir_node *sched_next_block(ir_node *block) {
379     return get_irn_link(block);
380 }
381
382 /**
383  * Emit a conditional jump.
384  */
385 static void emit_arm_CondJmp(ir_node *irn, void *env) {
386         arm_emit_env_t *emit_env = env;
387         FILE *out = emit_env->out;
388         const ir_edge_t *edge;
389         ir_node *true_block = NULL;
390         ir_node *false_block = NULL;
391         ir_node *op1 = get_irn_n(irn, 0);
392         ir_mode *opmode = get_irn_mode(op1);
393         char *suffix;
394         int proj_num = get_arm_proj_num(irn);
395         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
396
397
398         foreach_out_edge(irn, edge) {
399                 ir_node *proj = get_edge_src_irn(edge);
400                 long nr = get_Proj_proj(proj);
401                 ir_node *block = get_irn_link(proj);
402                 if ( nr == pn_Cond_true) {
403                         true_block = block;
404                 } else if (nr == pn_Cond_false) {
405                         false_block = block;
406                 } else {
407                         assert(0 && "tertium non datur! (CondJmp)");
408                 }
409         }
410
411         if (proj_num == pn_Cmp_False) {
412                 /* always false: should not happen */
413                 fprintf(out, "\tB BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
414         } else if (proj_num == pn_Cmp_True) {
415                 /* always true: should not happen */
416                 fprintf(out, "\tB BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
417         } else {
418                 ir_node *block = get_nodes_block(irn);
419
420                 if (mode_is_float(opmode)) {
421                         suffix = "ICHWILLIMPLEMENTIERTWERDEN";
422
423                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "FCMP %1S, %2S", irn, irn);
424                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
425                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
426
427                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "FMSTAT", irn, irn);
428                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
429                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
430                 } else {
431                         if (true_block == sched_next_block(block)) {
432                                 /* negate it */
433                                 proj_num = get_negated_pnc(proj_num, opmode);
434                         }
435                         switch(proj_num) {
436                                 case pn_Cmp_Eq:  suffix = "EQ"; break;
437                                 case pn_Cmp_Lt:  suffix = "LT"; break;
438                                 case pn_Cmp_Le:  suffix = "LE"; break;
439                                 case pn_Cmp_Gt:  suffix = "GT"; break;
440                                 case pn_Cmp_Ge:  suffix = "GE"; break;
441                                 case pn_Cmp_Lg:  suffix = "NE"; break;
442                                 case pn_Cmp_Leg: suffix = "AL"; break;
443                         default: assert(0 && "komische Dinge geschehen"); suffix = "AL";
444                         }
445
446                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "CMP %1S, %2S", irn, irn);
447                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
448                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
449                 }
450
451                 if (true_block == sched_next_block(block)) {
452                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "B%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
453                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
454                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
455
456                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
457                         arm_fprintf_format(out, "", cmnt_buf, irn);
458                 }
459                 else {
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, "/* true case */");
462                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
463
464             if (false_block == sched_next_block(block)) {
465                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
466                 arm_fprintf_format(out, "", cmnt_buf, irn);
467             }
468             else {
469                             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "B BLOCK_%d", get_irn_node_nr(false_block));
470                             lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
471                             arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
472             }
473         }
474         }
475 }
476
477 static void emit_arm_CopyB(ir_node *irn, void *env) {
478         arm_emit_env_t *emit_env = env;
479         FILE *out = emit_env->out;
480         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
481         unsigned int size = get_tarval_long(get_arm_value(irn));
482         const lc_arg_env_t *arg_env = arm_get_arg_env();
483
484         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);
485
486         assert ( size > 0 && "CopyB needs size > 0" );
487         if (size & 3)
488                 size += 4;
489         size >>= 2;
490         switch(size & 3) {
491         case 0:
492                 break;
493         case 1:
494                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDR %%r12, [%2S, #0]!", irn);
495                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
496                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STR  %%r12, [%1S, #0]!", irn);
497                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
498                 break;
499         case 2:
500                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDMIA %2S!, {%%r12, %3S}", irn, irn);
501                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
502                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STMIA %1S!, {%%r12, %3S}", irn, irn);
503                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
504                 break;
505         case 3:
506                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDMIA %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
507                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
508                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STMIA %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
509                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
510                 break;
511         }
512         size >>= 2;
513         while (size) {
514                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDMIA %2S!, {%%r12, %3S, %4S, %5S}", irn, 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, %5S}", irn, irn, irn, irn);
517                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
518                 --size;
519         }
520 }
521
522 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
523         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
524         const ir_edge_t    *edge;
525         ir_node            *proj;
526         arm_emit_env_t *emit_env = env;
527         FILE *out = emit_env->out;
528         int i;
529         ir_node **projs;
530         int n_projs;
531         int block_nr;
532         int default_block_num;
533
534         block_nr = get_irn_node_nr(irn);
535         n_projs = get_arm_n_projs(irn);
536
537         projs = xcalloc(n_projs , sizeof(ir_node*));
538
539         foreach_out_edge(irn, edge) {
540                 proj = get_edge_src_irn(edge);
541                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
542
543                 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
544                         default_block_num = get_irn_node_nr(get_irn_link(proj));
545
546                 projs[get_Proj_proj(proj)] = proj;
547         }
548
549         // CMP %1S, n_projs - 1
550         // BHI default
551
552
553
554         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "CMP %1S, #%u", irn, n_projs - 1);
555         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
556         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
557
558         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "BHI BLOCK_%d", default_block_num);
559         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
560         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
561
562
563         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "LDR %%r12, TABLE_%d_START", block_nr);
564         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
565         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
566
567         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %%r12, %%r12, %1S, LSL #2", irn);
568         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
569         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
570
571         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "LDR %%r15, [%%r12, #0]");
572         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
573         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
574
575         // LDR %r12, .TABLE_X_START
576         // ADD %r12, %r12, [%1S, LSL #2]
577         // LDR %r15, %r12
578
579         fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
580         fprintf(out, "\t.align 2\n");
581         fprintf(out, "TABLE_%d:\n", block_nr);
582
583
584         for ( i=0; i<n_projs; i++) {
585                 ir_node *block;
586                 proj = projs[i];
587                 if ( proj ) {
588                         block = get_irn_link(proj);
589                 } else {
590                         block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
591                 }
592                 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
593         }
594         fprintf(out, "\t.align 2\n");
595
596         xfree(projs);
597 }
598
599 /************************************************************************/
600 /* emit_be                                                              */
601 /************************************************************************/
602
603 static void emit_be_Call(ir_node *irn, void *env) {
604         arm_emit_env_t *emit_env = env;
605         FILE *F = emit_env->out;
606         entity *ent = be_Call_get_entity(irn);
607     char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
608
609     if (ent)
610             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "BL %s", get_entity_ld_name(ent));
611     else
612         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
613     lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
614     lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %G */\n", cmd_buf, cmnt_buf, irn);
615 }
616
617 /** Emit an IncSP node */
618 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
619         FILE *F = emit_env->out;
620         unsigned offs = be_get_IncSP_offset(irn);
621         if (offs) {
622                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
623                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, #%O", irn, irn, irn );
624                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
625                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
626         } else {
627                 char cmnt_buf[SNPRINTF_BUF_LEN];
628                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
629                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
630         }
631 }
632
633 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
634 //      FILE *F = emit_env->out;
635 //      char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
636 //      lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
637 //      lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
638 //      lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
639 // }
640
641 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
642         FILE *F    = emit_env->out;
643         ir_mode *mode = get_irn_mode(irn);
644         assert( (mode != mode_E) && "IEEE Extended FP not supported");
645
646         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
647                 char cmnt_buf[SNPRINTF_BUF_LEN];
648                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
649                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
650                 return;
651         }
652
653         if (mode == mode_F) {
654                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
655                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "FCPYS %1D, %1S", irn, irn);
656                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
657                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
658         } else if (mode == mode_D) {
659                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
660                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "FCPYD %1D, %1S", irn, irn);
661                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
662                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
663         } else if (mode_is_numP(mode)) {
664                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
665                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "MOV %1D, %1S", irn, irn);
666                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
667                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
668         } else {
669                 assert(0 && "move not supported for this mode");
670         }
671 //      emit_arm_Copy(irn, emit_env);
672 }
673
674 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
675         FILE *F = emit_env->out;
676         ir_mode *mode = get_irn_mode(irn);
677         assert( (mode != mode_E) && "IEEE Extended FP not supported");
678         if (mode_is_dataM(mode)) {
679                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
680                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "STR %2S, [%1S, #%O]", irn, irn, irn );
681                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
682                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
683         } else {
684                 assert(0 && "spill not supported for this mode");
685         }
686 }
687
688 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
689         FILE *F = emit_env->out;
690         ir_mode *mode = get_irn_mode(irn);
691         assert( (mode != mode_E) && "IEEE Extended FP not supported");
692         if (mode_is_dataM(mode)) {
693                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
694                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "LDR %1D, [%1S, #%O]", irn, irn, irn );
695                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
696                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
697         } else {
698                 assert(0 && "reload not supported for this mode");
699         }
700 }
701
702 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
703         FILE *F = emit_env->out;
704         ir_mode *mode = get_irn_mode(irn);
705         assert( (mode != mode_E) && "IEEE Extended FP not supported");
706         lc_efprintf(arm_get_arg_env(), F, "\tEOR %1S, %1S, %2S\t\t\t/* begin Perm(%1S, %2S) */\n", irn, irn, irn, irn, irn);
707         lc_efprintf(arm_get_arg_env(), F, "\tEOR %2S, %1S, %2S\n", irn, irn, irn);
708         lc_efprintf(arm_get_arg_env(), F, "\tEOR %1S, %1S, %2S\t\t\t/* end Perm(%1S, %2S) */\n", irn, irn, irn, irn, irn);
709 }
710
711 static void emit_be_StackParam(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         char cmd_buf[256], cmnt_buf[SNPRINTF_BUF_LEN];
715         assert( (mode != mode_E) && "IEEE Extended FP not supported");
716
717         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "LDR %1D, [%1S, #%O]", irn, irn, irn );
718         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
719         lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
720 }
721
722 /************************************************************************/
723 /* emit                                                                 */
724 /************************************************************************/
725
726 static void emit_Jmp(ir_node *irn, void *env) {
727         arm_emit_env_t *emit_env = env;
728         FILE *out = emit_env->out;
729         const ir_edge_t *edge = get_irn_out_edge_first(irn);
730         ir_node *target_block = get_edge_src_irn(edge);
731         fprintf(out, "\tB BLOCK_%ld\t\t\t/* unconditional Jump */\n", get_irn_node_nr(target_block));
732 }
733
734 static void emit_silence(ir_node *irn, void *env) {
735
736 }
737
738
739
740 /***********************************************************************************
741  *                  _          __                                             _
742  *                 (_)        / _|                                           | |
743  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
744  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
745  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
746  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
747  *
748  ***********************************************************************************/
749
750 /**
751  * Enters the emitter functions for handled nodes into the generic
752  * pointer of an opcode.
753  */
754 static void arm_register_emitters(void) {
755
756 #define ARM_EMIT(a)  op_arm_##a->ops.generic = (op_func)emit_arm_##a
757 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
758 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
759 #define SILENCE(a)   op_##a->ops.generic = (op_func)emit_silence
760
761         /* first clear the generic function pointer for all ops */
762         clear_irp_opcodes_generic_func();
763
764         /* register all emitter functions defined in spec */
765         arm_register_spec_emitters();
766
767         /* other emitter functions */
768         ARM_EMIT(CondJmp);
769         ARM_EMIT(CopyB);
770 //      ARM_EMIT(CopyB_i);
771 //      ARM_EMIT(Const);
772         ARM_EMIT(SymConst);
773         ARM_EMIT(SwitchJmp);
774
775         /* benode emitter */
776         BE_EMIT(Call);
777         BE_EMIT(IncSP);
778 //      BE_EMIT(AddSP);
779         BE_EMIT(Copy);
780         BE_EMIT(Spill);
781         BE_EMIT(Reload);
782         BE_EMIT(Perm);
783         BE_EMIT(StackParam);
784
785         /* firm emitter */
786         EMIT(Jmp);
787
788         /* noisy stuff */
789 #ifdef SILENCER
790         SILENCE(Proj);
791         SILENCE(Phi);
792         SILENCE(be_Keep);
793         SILENCE(be_CopyKeep);
794 #endif
795
796 #undef ARM_EMIT
797 #undef BE_EMIT
798 #undef EMIT
799 #undef SILENCE
800 }
801
802 typedef void (node_emitter)(const ir_node *, void *);
803
804 /**
805  * Emits code for a node.
806  */
807 static void arm_emit_node(const ir_node *irn, void *env) {
808         arm_emit_env_t *emit_env = env;
809         FILE           *F        = emit_env->out;
810         ir_op          *op       = get_irn_op(irn);
811         DEBUG_ONLY(firm_dbg_module_t *mod = emit_env->mod;)
812
813         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
814
815         if (op->ops.generic) {
816                 node_emitter *emit = (node_emitter *)op->ops.generic;
817                 emit(irn, env);
818         }
819         else {
820                 ir_fprintf(F, "\t%-35s /* %+F %G */\n", "", irn, irn);
821         }
822 }
823
824 /**
825  * Walks over the nodes in a block connected by scheduling edges
826  * and emits code for each node.
827  */
828 void arm_gen_block(ir_node *block, void *env) {
829         ir_node *irn;
830
831         fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
832         sched_foreach(block, irn) {
833                 arm_emit_node(irn, env);
834         }
835 }
836
837
838 /**
839  * Emits code for function start.
840  */
841 void arm_emit_start(FILE *F, ir_graph *irg) {
842         entity *ent = get_irg_entity(irg);
843         const char *irg_name = get_entity_ld_name(ent);
844         arm_switch_section(F, SECTION_TEXT);
845         fprintf(F, "\t.align  2\n");
846
847         if (get_entity_visibility(ent) == visibility_external_visible)
848                 fprintf(F, "\t.global %s\n", irg_name);
849         fprintf(F, "%s:\n", irg_name);
850 }
851
852 /**
853  * Emits code for function end
854  */
855 void arm_emit_end(FILE *F, ir_graph *irg) {
856 }
857
858 /**
859  * Sets labels for control flow nodes (jump target)
860  * TODO: Jump optimization
861  */
862 void arm_gen_labels(ir_node *block, void *env) {
863         ir_node *pred;
864         int n = get_Block_n_cfgpreds(block);
865
866         for (n--; n >= 0; n--) {
867                 pred = get_Block_cfgpred(block, n);
868                 set_irn_link(pred, block);
869         }
870 }
871
872
873 /**
874 * Main driver. Emits the code for one routine.
875  */
876 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
877         arm_emit_env_t emit_env;
878     ir_node **blk_sched;
879     int i, n;
880
881         emit_env.out      = F;
882         emit_env.arch_env = cg->arch_env;
883         emit_env.cg       = cg;
884         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
885
886         /* set the global arch_env (needed by print hooks) */
887         arch_env = cg->arch_env;
888
889         arm_register_emitters();
890
891         /* create the block schedule. For now, we don't need it earlier. */
892         blk_sched = sched_create_block_schedule(cg->irg);
893
894         arm_emit_start(F, irg);
895         irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
896
897         n = ARR_LEN(blk_sched);
898         for (i = 0; i < n;) {
899                 ir_node *block, *next_bl;
900
901                 block   = blk_sched[i];
902                 ++i;
903                 next_bl = i < n ? blk_sched[i] : NULL;
904
905                 /* set here the link. the emitter expects to find the next block here */
906                 set_irn_link(block, next_bl);
907                 arm_gen_block(block, &emit_env);
908         }
909
910         arm_emit_end(F, irg);
911 }