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