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