some workaround to avoid condeval creating Phibs which not all backends like
[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 #include "../beblocksched.h"
23
24 #include "arm_emitter.h"
25 #include "gen_arm_emitter.h"
26 #include "arm_nodes_attr.h"
27 #include "arm_new_nodes.h"
28 #include "arm_map_regs.h"
29 #include "gen_arm_regalloc_if.h"
30
31 #include "../benode_t.h"
32
33 #define SNPRINTF_BUF_LEN 128
34
35 static const arch_env_t *arch_env = NULL;
36
37 /**
38  * Switch to a new section
39  */
40 void arm_switch_section(FILE *f, sections sec) {
41         static sections curr_sec = NO_SECTION;
42
43         if (curr_sec == sec)
44                 return;
45
46         curr_sec = sec;
47         switch (sec) {
48
49         case NO_SECTION:
50                 break;
51
52         case SECTION_TEXT:
53                 fprintf(f, "\t.text\n");
54                 break;
55
56         case SECTION_DATA:
57                 fprintf(f, "\t.data\n");
58                 break;
59
60         default:
61                 assert(0);
62         }
63 }
64
65 /*************************************************************
66  *             _       _    __   _          _
67  *            (_)     | |  / _| | |        | |
68  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
69  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
70  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
71  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
72  * | |                                       | |
73  * |_|                                       |_|
74  *************************************************************/
75
76 /**
77  * Returns non-zero if a mode has a Immediate attribute.
78  */
79 int is_immediate_node(ir_node *irn) {
80         arm_attr_t *attr = get_arm_attr(irn);
81         return ARM_GET_SHF_MOD(attr) == ARM_SHF_IMM;
82 }
83
84 /**
85  * Return a const or SymConst as string.
86  */
87 static const char *node_const_to_str(ir_node *n, char *buf, int buflen) {
88         if (is_immediate_node(n)) {
89                 snprintf(buf, buflen, "#0x%X", arm_decode_imm_w_shift(get_arm_value(n)));
90                 return buf;
91         }
92         else if (is_arm_SymConst(n))
93                 return get_arm_symconst_label(n);
94
95         assert( 0 && "das ist gar keine Konstante");
96         return NULL;
97 }
98
99 /**
100  * Returns node's offset as string.
101  */
102 static const char *node_offset_to_str(ir_node *n, char *buf, int buflen) {
103         int offset = 0;
104         ir_op *irn_op = get_irn_op(n);
105
106         if (irn_op == op_be_StackParam) {
107                 ir_entity *ent = be_get_frame_entity(n);
108                 offset = get_entity_offset(ent);
109         } else if (irn_op == op_be_Reload || irn_op == op_be_Spill) {
110                 ir_entity *ent = be_get_frame_entity(n);
111                 offset = get_entity_offset(ent);
112         } else if (irn_op == op_be_IncSP) {
113                 offset = - be_get_IncSP_offset(n);
114         } else {
115                 return "node_offset_to_str will fuer diesen Knotentyp noch implementiert werden";
116         }
117         snprintf(buf, buflen, "%d", offset);
118         return buf;
119 }
120
121 /* We always pass the ir_node which is a pointer. */
122 static int arm_get_arg_type(const lc_arg_occ_t *occ) {
123         return lc_arg_type_ptr;
124 }
125
126
127 /**
128  * Returns the register at in position pos.
129  */
130 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
131         ir_node                *op;
132         const arch_register_t  *reg = NULL;
133
134         assert(get_irn_arity(irn) > pos && "Invalid IN position");
135
136         /* The out register of the operator at position pos is the
137            in register we need. */
138         op = get_irn_n(irn, pos);
139
140         reg = arch_get_irn_register(arch_env, op);
141
142         assert(reg && "no in register found");
143         return reg;
144 }
145
146 /**
147  * Returns the register at out position pos.
148  */
149 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
150         ir_node                *proj;
151         const arch_register_t  *reg = NULL;
152
153         assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
154
155         /* 1st case: irn is not of mode_T, so it has only                 */
156         /*           one OUT register -> good                             */
157         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
158         /*           Proj with the corresponding projnum for the register */
159
160         if (get_irn_mode(irn) != mode_T) {
161                 reg = arch_get_irn_register(arch_env, irn);
162         }
163         else if (is_arm_irn(irn)) {
164                 reg = get_arm_out_reg(irn, pos);
165         }
166         else {
167                 const ir_edge_t *edge;
168
169                 foreach_out_edge(irn, edge) {
170                         proj = get_edge_src_irn(edge);
171                         assert(is_Proj(proj) && "non-Proj from mode_T node");
172                         if (get_Proj_proj(proj) == pos) {
173                                 reg = arch_get_irn_register(arch_env, proj);
174                                 break;
175                         }
176                 }
177         }
178
179         assert(reg && "no out register found");
180         return reg;
181 }
182
183 /**
184  * Returns the number of the in register at position pos.
185  */
186 int get_arm_reg_nr(ir_node *irn, int pos, int in_out) {
187         const arch_register_t *reg;
188
189         if (in_out == 1) {
190                 reg = get_in_reg(irn, pos);
191         }
192         else {
193                 reg = get_out_reg(irn, pos);
194         }
195
196         return arch_register_get_index(reg);
197 }
198
199 /**
200  * Returns the name of the in register at position pos.
201  */
202 const char *get_arm_reg_name(ir_node *irn, int pos, int in_out) {
203         const arch_register_t *reg;
204
205         if (in_out == 1) {
206                 reg = get_in_reg(irn, pos);
207         }
208         else {
209                 reg = get_out_reg(irn, pos);
210         }
211
212         return arch_register_get_name(reg);
213 }
214
215 /**
216  * Get the register name for a node.
217  */
218 static int arm_get_reg_name(lc_appendable_t *app,
219     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
220 {
221         const char *buf;
222         ir_node    *X  = arg->v_ptr;
223         int         nr = occ->width - 1;
224
225         if (!X)
226                 return lc_appendable_snadd(app, "(null)", 6);
227
228         if (occ->conversion == 'S') {
229                 buf = get_arm_reg_name(X, nr, 1);
230         }
231         else { /* 'D' */
232                 buf = get_arm_reg_name(X, nr, 0);
233         }
234
235         lc_appendable_chadd(app, '%');
236         return lc_appendable_snadd(app, buf, strlen(buf));
237 }
238
239 /**
240  * Returns the tarval or offset of an arm node as a string.
241  */
242 static int arm_const_to_str(lc_appendable_t *app,
243     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
244 {
245         char buffer[SNPRINTF_BUF_LEN];
246         const char *buf;
247         ir_node    *X = arg->v_ptr;
248
249         if (!X)
250                 return lc_appendable_snadd(app, "(null)", 6);
251
252         if (occ->conversion == 'C') {
253                 buf = node_const_to_str(X, buffer, sizeof(buffer));
254         }
255         else { /* 'O' */
256                 buf = node_offset_to_str(X, buffer, sizeof(buffer));
257         }
258
259         return lc_appendable_snadd(app, buf, strlen(buf));
260 }
261
262 /**
263  * Returns the tarval or offset of an arm node as a string.
264  */
265 static int arm_shift_str(lc_appendable_t *app,
266     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
267 {
268         char buffer[SNPRINTF_BUF_LEN];
269         ir_node            *irn = arg->v_ptr;
270         arm_shift_modifier mod;
271
272         if (!irn)
273                 return lc_appendable_snadd(app, "(null)", 6);
274
275         mod = get_arm_shift_modifier(irn);
276         if (ARM_HAS_SHIFT(mod)) {
277                 long v = get_tarval_long(get_arm_value(irn));
278
279                 snprintf(buffer, sizeof(buffer), ", %s #%ld", arm_shf_mod_name(mod), v);
280                 return lc_appendable_snadd(app, buffer, strlen(buffer));
281         }
282         return 0;
283 }
284
285 /**
286  * Determines the instruction suffix depending on the mode.
287  */
288 static int arm_get_mode_suffix(lc_appendable_t *app,
289     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
290 {
291         ir_node *irn = arg->v_ptr;
292         arm_attr_t *attr;
293         ir_mode *mode;
294         int bits;
295
296         if (! irn)
297                 return lc_appendable_snadd(app, "(null)", 6);
298
299         attr = get_arm_attr(irn);
300         mode = attr->op_mode ? attr->op_mode : get_irn_mode(irn);
301         bits = get_mode_size_bits(mode);
302
303         if (bits == 32)
304                 return lc_appendable_chadd(app, 's');
305         else if (bits == 64)
306                 return lc_appendable_chadd(app, 'd');
307         else
308                 return lc_appendable_chadd(app, 'e');
309 }
310
311 /**
312  * Return the arm printf arg environment.
313  * We use the firm environment with some additional handlers.
314  */
315 const lc_arg_env_t *arm_get_arg_env(void) {
316         static lc_arg_env_t *env = NULL;
317
318         static const lc_arg_handler_t arm_reg_handler   = { arm_get_arg_type, arm_get_reg_name };
319         static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
320         static const lc_arg_handler_t arm_mode_handler  = { arm_get_arg_type, arm_get_mode_suffix };
321         static const lc_arg_handler_t arm_shf_handler   = { arm_get_arg_type, arm_shift_str };
322
323         if (env == NULL) {
324                 /* extend the firm printer */
325                 env = firm_get_arg_env();
326                         //lc_arg_new_env();
327
328                 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
329                 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
330                 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
331                 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
332                 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
333                 lc_arg_register(env, "arm:shf",  'X', &arm_shf_handler);
334         }
335
336         return env;
337 }
338
339 /**
340  * Formated print of commands and comments.
341  */
342 static void arm_fprintf_format(FILE *F, const char *cmd_buf, const char *cmnt_buf, const ir_node *irn) {
343         lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F (%G) */\n", cmd_buf, cmnt_buf, irn, irn);
344 }
345
346 /**
347  * Returns a unique label. This number will not be used a second time.
348  */
349 static unsigned get_unique_label(void) {
350         static unsigned id = 0;
351         return ++id;
352 }
353
354 /**
355  * Emit a SymConst
356  */
357 static void emit_arm_SymConst(ir_node *irn, void *env) {
358         arm_emit_env_t *emit_env = env;
359         FILE *F = emit_env->out;
360         SymConstEntry *entry = obstack_alloc(&emit_env->obst, sizeof(*entry));
361         const lc_arg_env_t *arg_env = arm_get_arg_env();
362         char cmd_buf[256];
363
364         entry->label      = get_unique_label();
365         entry->symconst   = irn;
366         entry->next       = emit_env->symbols;
367         emit_env->symbols = entry;
368
369         lc_esnprintf(arg_env, cmd_buf, 256, "ldr %1D, .L%u", irn, entry->label);
370         arm_fprintf_format(F, cmd_buf, "/* indirect SymConst */", irn);
371 }
372
373 /**
374  * Returns the next block in a block schedule.
375  */
376 static ir_node *sched_next_block(ir_node *block) {
377     return get_irn_link(block);
378 }
379
380 /**
381  * Emit a conditional jump.
382  */
383 static void emit_arm_CondJmp(ir_node *irn, void *env) {
384         arm_emit_env_t *emit_env = env;
385         FILE *out = emit_env->out;
386         const ir_edge_t *edge;
387         ir_node *true_block = NULL;
388         ir_node *false_block = NULL;
389         ir_node *op1 = get_irn_n(irn, 0);
390         ir_mode *opmode = get_irn_mode(op1);
391         char *suffix;
392         int proj_num = get_arm_proj_num(irn);
393         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
394
395
396         foreach_out_edge(irn, edge) {
397                 ir_node *proj = get_edge_src_irn(edge);
398                 long nr = get_Proj_proj(proj);
399                 ir_node *block = get_irn_link(proj);
400                 if ( nr == pn_Cond_true) {
401                         true_block = block;
402                 } else if (nr == pn_Cond_false) {
403                         false_block = block;
404                 } else {
405                         assert(0 && "tertium non datur! (CondJmp)");
406                 }
407         }
408
409         if (proj_num == pn_Cmp_False) {
410                 /* always false: should not happen */
411                 fprintf(out, "\tb BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
412         } else if (proj_num == pn_Cmp_True) {
413                 /* always true: should not happen */
414                 fprintf(out, "\tb BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
415         } else {
416                 ir_node *block = get_nodes_block(irn);
417
418                 if (mode_is_float(opmode)) {
419                         suffix = "ICHWILLIMPLEMENTIERTWERDEN";
420
421                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fcmp %1S, %2S", irn, irn);
422                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
423                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
424
425                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "fmstat", irn, irn);
426                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
427                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
428                 } else {
429                         if (true_block == sched_next_block(block)) {
430                                 /* negate it */
431                                 proj_num = get_negated_pnc(proj_num, opmode);
432                         }
433                         switch(proj_num) {
434                                 case pn_Cmp_Eq:  suffix = "eq"; break;
435                                 case pn_Cmp_Lt:  suffix = "lt"; break;
436                                 case pn_Cmp_Le:  suffix = "le"; break;
437                                 case pn_Cmp_Gt:  suffix = "gt"; break;
438                                 case pn_Cmp_Ge:  suffix = "ge"; break;
439                                 case pn_Cmp_Lg:  suffix = "ne"; break;
440                                 case pn_Cmp_Leg: suffix = "al"; break;
441                         default: assert(0 && "komische Dinge geschehen"); suffix = "al";
442                         }
443
444                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, %2S", irn, irn);
445                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", irn, irn );
446                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
447                 }
448
449                 if (true_block == sched_next_block(block)) {
450                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
451                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
452                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
453
454                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* fallthrough BLOCK_%d */", get_irn_node_nr(false_block));
455                         arm_fprintf_format(out, "", cmnt_buf, irn);
456                 }
457                 else {
458                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "b%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
459                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
460                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
461
462             if (false_block == sched_next_block(block)) {
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 BLOCK_%d", get_irn_node_nr(false_block));
468                             lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
469                             arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
470             }
471         }
472         }
473 }
474
475 static void emit_arm_CopyB(ir_node *irn, void *env) {
476         arm_emit_env_t *emit_env = env;
477         FILE *out = emit_env->out;
478         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
479         unsigned int size = get_tarval_long(get_arm_value(irn));
480         const lc_arg_env_t *arg_env = arm_get_arg_env();
481
482         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);
483
484         assert ( size > 0 && "CopyB needs size > 0" );
485         if (size & 3)
486                 size += 4;
487         size >>= 2;
488         switch(size & 3) {
489         case 0:
490                 break;
491         case 1:
492                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, [%2S, #0]!", irn);
493                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
494                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "str  %%r12, [%1S, #0]!", irn);
495                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
496                 break;
497         case 2:
498                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S}", irn, irn);
499                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
500                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S}", irn, irn);
501                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
502                 break;
503         case 3:
504                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
505                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
506                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
507                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
508                 break;
509         }
510         size >>= 2;
511         while (size) {
512                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "ldmia %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
513                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
514                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "stmia %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
515                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
516                 --size;
517         }
518 }
519
520 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
521         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
522         const ir_edge_t    *edge;
523         ir_node            *proj;
524         arm_emit_env_t *emit_env = env;
525         FILE *out = emit_env->out;
526         int i;
527         ir_node **projs;
528         int n_projs;
529         int block_nr;
530         int default_block_num = -1;
531
532         block_nr = get_irn_node_nr(irn);
533         n_projs = get_arm_n_projs(irn);
534
535         projs = xcalloc(n_projs , sizeof(ir_node*));
536
537         foreach_out_edge(irn, edge) {
538                 proj = get_edge_src_irn(edge);
539                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
540
541                 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
542                         default_block_num = get_irn_node_nr(get_irn_link(proj));
543
544                 projs[get_Proj_proj(proj)] = proj;
545         }
546         assert(default_block_num >= 0);
547
548         // CMP %1S, n_projs - 1
549         // BHI default
550
551
552
553         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "cmp %1S, #%u", irn, n_projs - 1);
554         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
555         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
556
557         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bhi BLOCK_%d", default_block_num);
558         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
559         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
560
561
562         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r12, TABLE_%d_START", block_nr);
563         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
564         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
565
566         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "add %%r12, %%r12, %1S, LSL #2", irn);
567         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
568         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
569
570         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ldr %%r15, [%%r12, #0]");
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         // LDR %r12, .TABLE_X_START
575         // ADD %r12, %r12, [%1S, LSL #2]
576         // LDR %r15, %r12
577
578         fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
579         fprintf(out, "\t.align 2\n");
580         fprintf(out, "TABLE_%d:\n", block_nr);
581
582
583         for ( i=0; i<n_projs; i++) {
584                 ir_node *block;
585                 proj = projs[i];
586                 if ( proj ) {
587                         block = get_irn_link(proj);
588                 } else {
589                         block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
590                 }
591                 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
592         }
593         fprintf(out, "\t.align 2\n");
594
595         xfree(projs);
596 }
597
598 /************************************************************************/
599 /* emit_be                                                              */
600 /************************************************************************/
601
602 static void emit_be_Call(ir_node *irn, void *env) {
603         arm_emit_env_t *emit_env = env;
604         FILE *F = emit_env->out;
605         ir_entity *ent = be_Call_get_entity(irn);
606     char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
607
608     if (ent)
609             lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "bl %s", get_entity_ld_name(ent));
610     else
611         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "%1D", get_irn_n(irn, be_pos_Call_ptr));
612     lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* %+F (be_Call) */", irn);
613     arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
614 }
615
616 /** Emit an IncSP node */
617 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
618         FILE *F = emit_env->out;
619         int offs = be_get_IncSP_offset(irn);
620
621         if (offs != 0) {
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                 arm_fprintf_format(F, 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                 arm_fprintf_format(F, "", 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         const lc_arg_env_t *arm_env = arm_get_arg_env();
645         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
646
647         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
648                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted Copy: %1S -> %1D */", irn, irn);
649                 lc_efprintf(arm_env, F, "\t%-35s %-60s /* %+F */\n", "", cmnt_buf, irn);
650                 return;
651         }
652
653         if (mode_is_float(mode)) {
654                 if (USE_FPA(emit_env->cg->isa)) {
655                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mvf%M %1D, %1S", irn, irn, irn);
656                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
657                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
658                 }
659                 else {
660                         assert(0 && "move not supported for this mode");
661                 }
662         } else if (mode_is_numP(mode)) {
663                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "mov %1D, %1S", irn, irn);
664                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Copy: %1S -> %1D */", irn, irn);
665                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
666         } else {
667                 assert(0 && "move not supported for this mode");
668         }
669 //      emit_arm_Copy(irn, emit_env);
670 }
671
672 /**
673  * Emit code for a Spill.
674  */
675 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
676         FILE *F = emit_env->out;
677         ir_mode *mode = get_irn_mode(irn);
678         const lc_arg_env_t *arm_env = arm_get_arg_env();
679         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
680
681         if (mode_is_float(mode)) {
682                 if (USE_FPA(emit_env->cg->isa)) {
683                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "stf %2S, [%1S, #%O]", irn, irn, irn );
684                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
685                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
686                 }
687                 else {
688                         assert(0 && "move not supported for this mode");
689                 }
690         } else if (mode_is_dataM(mode)) {
691                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "str %2S, [%1S, #%O]", irn, irn, irn );
692                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Spill(%2S) -> (%1S) */", irn, irn);
693                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
694         } else {
695                 assert(0 && "spill not supported for this mode");
696         }
697 }
698
699 /**
700  * Emit code for a Reload.
701  */
702 static void emit_be_Reload(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         const lc_arg_env_t *arm_env = arm_get_arg_env();
706         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
707
708         if (mode_is_float(mode)) {
709                 if (USE_FPA(emit_env->cg->isa)) {
710                         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldf %1D, [%1S, #%O]", irn, irn, irn );
711                         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
712                         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
713                 }
714                 else {
715                         assert(0 && "move not supported for this mode");
716                 }
717         } else if (mode_is_dataM(mode)) {
718                 lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "ldr %1D, [%1S, #%O]", irn, irn, irn );
719                 lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* Reload(%1S) -> (%1D) */", irn, irn);
720                 arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
721         } else {
722                 assert(0 && "reload not supported for this mode");
723         }
724 }
725
726 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
727         FILE *F = emit_env->out;
728         const lc_arg_env_t *arm_env = arm_get_arg_env();
729         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
730
731         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
732         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* begin Perm(%1S, %2S) */", irn, irn);
733         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
734
735         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %2S, %1S, %2S", irn, irn, irn);
736         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, " ");
737         arm_fprintf_format(F, cmd_buf, cmnt_buf, irn);
738
739         lc_esnprintf(arm_env, cmd_buf, SNPRINTF_BUF_LEN, "eor %1S, %1S, %2S", irn, irn, irn);
740         lc_esnprintf(arm_env, cmnt_buf, SNPRINTF_BUF_LEN, "/* end Perm(%1S, %2S) */", irn, irn);
741         arm_fprintf_format(F, cmd_buf, cmnt_buf, 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_%ld:\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         ir_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 = be_create_block_schedule(cg->irg, cg->birg->exec_freq);
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 }