placed register_options() last in the interface
[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 /*************************************************************
38  *             _       _    __   _          _
39  *            (_)     | |  / _| | |        | |
40  *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
41  * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
42  * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
43  * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
44  * | |                                       | |
45  * |_|                                       |_|
46  *************************************************************/
47
48 int is_immediate_node(ir_node *irn) {
49         if (is_arm_Add_i(irn) || is_arm_Sub_i(irn))
50                 return 1;
51         if (is_arm_Shr_i(irn) || is_arm_Shr_i(irn) || is_arm_Shl_i(irn))
52                 return 1;
53         if (is_arm_And_i(irn) || is_arm_Or_i(irn) || is_arm_Eor_i(irn))
54                 return 1;
55         if (is_arm_Or_Shl_i(irn))
56                 return 1;
57         return 0;
58 }
59
60 /**
61  * Return a const or symconst as string.
62  */
63 static const char *node_const_to_str(ir_node *n) {
64         char buffer[SNPRINTF_BUF_LEN];
65
66         if ( is_immediate_node(n) ) {
67                 long longvalue = get_tarval_long(get_arm_value(n));
68                 char *str;
69                 assert(longvalue < 0x1000 && "constant doesn't fit in shifter_operand");
70                 snprintf(buffer, SNPRINTF_BUF_LEN - 1, "#%ld << %ld", longvalue & 0xff, (longvalue >> 8) << 1 );
71                 str = xmalloc(strlen(buffer) * sizeof(char));
72                 strcpy(str, buffer);
73                 return str;
74         }
75         if ( is_arm_Const(n) || is_arm_Const_Neg(n) ) {
76                 tarval *tv = get_arm_value(n);
77                 if ( mode_is_int(get_tarval_mode(tv)) ) {
78                         long longvalue = get_tarval_long(get_arm_value(n));
79                         char *str;
80                         assert(longvalue < 0x1000 && "constant doesn't fit in shifter_operand");
81                         snprintf(buffer, SNPRINTF_BUF_LEN - 1, "#%ld << %ld", longvalue & 0xff, (longvalue >> 8) << 1 );
82                         str = xmalloc(strlen(buffer) * sizeof(char));
83                         strcpy(str, buffer);
84                         return str;
85                 } else {
86                         return "found something else in arm_const";
87                 }
88         } else if ( is_arm_SymConst(n) ) {
89                 return get_arm_symconst_label(n);
90         } else {
91                 assert( 0 && "das ist gar keine Konstante");
92                 return NULL;
93         }
94
95 }
96
97 /**
98  * Returns node's offset as string.
99  */
100 static char *node_offset_to_str(ir_node *n) {
101         char buffer[SNPRINTF_BUF_LEN];
102         char *result;
103         int offset = 0;
104         ir_op *irn_op = get_irn_op(n);
105         if (irn_op == op_be_StackParam) {
106                 entity *ent = be_get_frame_entity(n);
107                 offset = get_entity_offset_bytes(ent);
108         } else if (irn_op==op_be_Reload || irn_op==op_be_Spill) {
109                 entity * ent = be_get_spill_entity(n);
110                 offset = get_entity_offset_bytes(ent);
111         } else if (irn_op==op_be_IncSP) {
112                 int offs = be_get_IncSP_offset(n);
113                 be_stack_dir_t dir  = be_get_IncSP_direction(n);
114                 offset = (dir == be_stack_dir_expand) ? -offs : offs;
115         } else {
116                 return "node_offset_to_str will fuer diesen Knotentyp noch implementiert werden";
117         }
118         snprintf(buffer, SNPRINTF_BUF_LEN, "%d", offset);
119         result = xmalloc(sizeof(char)*(strlen(buffer) + 1));
120         strcpy(result, buffer);
121         return result;
122 }
123
124 /* We always pass the ir_node which is a pointer. */
125 static int arm_get_arg_type(const lc_arg_occ_t *occ) {
126         return lc_arg_type_ptr;
127 }
128
129
130 /**
131  * Returns the register at in position pos.
132  */
133 static const arch_register_t *get_in_reg(const ir_node *irn, int pos) {
134         ir_node                *op;
135         const arch_register_t  *reg = NULL;
136
137         assert(get_irn_arity(irn) > pos && "Invalid IN position");
138
139         /* The out register of the operator at position pos is the
140            in register we need. */
141         op = get_irn_n(irn, pos);
142
143         reg = arch_get_irn_register(arch_env, op);
144
145         /* ONLY TEMPORARY WORK-AROUND */
146 //      if (!reg) {
147 //              printf("FIXME\n");
148 //              reg = &arm_general_purpose_regs[REG_MURX];
149 //      }
150
151         assert(reg && "no in register found");
152         return reg;
153 }
154
155 /**
156  * Returns the register at out position pos.
157  */
158 static const arch_register_t *get_out_reg(const ir_node *irn, int pos) {
159         ir_node                *proj;
160         const arch_register_t  *reg = NULL;
161
162         assert(get_irn_n_edges(irn) > pos && "Invalid OUT position");
163
164         /* 1st case: irn is not of mode_T, so it has only                 */
165         /*           one OUT register -> good                             */
166         /* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
167         /*           Proj with the corresponding projnum for the register */
168
169         if (get_irn_mode(irn) != mode_T) {
170                 reg = arch_get_irn_register(arch_env, irn);
171         }
172         else if (is_arm_irn(irn)) {
173                 reg = get_arm_out_reg(irn, pos);
174         }
175         else {
176                 const ir_edge_t *edge;
177
178                 foreach_out_edge(irn, edge) {
179                         proj = get_edge_src_irn(edge);
180                         assert(is_Proj(proj) && "non-Proj from mode_T node");
181                         if (get_Proj_proj(proj) == pos) {
182                                 reg = arch_get_irn_register(arch_env, proj);
183                                 break;
184                         }
185                 }
186         }
187
188         assert(reg && "no out register found");
189         return reg;
190 }
191
192 /**
193  * Returns the number of the in register at position pos.
194  */
195 int get_arm_reg_nr(ir_node *irn, int pos, int in_out) {
196         const arch_register_t *reg;
197
198         if (in_out == 1) {
199                 reg = get_in_reg(irn, pos);
200         }
201         else {
202                 reg = get_out_reg(irn, pos);
203         }
204
205         return arch_register_get_index(reg);
206 }
207
208 /**
209  * Returns the name of the in register at position pos.
210  */
211 const char *get_arm_reg_name(ir_node *irn, int pos, int in_out) {
212         const arch_register_t *reg;
213
214         if (in_out == 1) {
215                 reg = get_in_reg(irn, pos);
216         }
217         else {
218                 reg = get_out_reg(irn, pos);
219         }
220
221         return arch_register_get_name(reg);
222 }
223
224 /**
225  * Get the register name for a node.
226  */
227 static int arm_get_reg_name(lc_appendable_t *app,
228     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
229 {
230         const char *buf;
231         ir_node    *X  = arg->v_ptr;
232         int         nr = occ->width - 1;
233
234         if (!X)
235                 return lc_appendable_snadd(app, "(null)", 6);
236
237         if (occ->conversion == 'S') {
238                 buf = get_arm_reg_name(X, nr, 1);
239         }
240         else { /* 'D' */
241                 buf = get_arm_reg_name(X, nr, 0);
242         }
243
244         lc_appendable_chadd(app, '%');
245         return lc_appendable_snadd(app, buf, strlen(buf));
246 }
247
248 /**
249  * Returns the tarval or offset of an arm node as a string.
250  */
251 static int arm_const_to_str(lc_appendable_t *app,
252     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
253 {
254         const char *buf;
255         ir_node    *X = arg->v_ptr;
256
257         if (!X)
258                 return lc_appendable_snadd(app, "(null)", 6);
259
260         if (occ->conversion == 'C') {
261                 buf = node_const_to_str(X);
262         }
263         else { /* 'O' */
264                 buf = node_offset_to_str(X);
265         }
266
267         return lc_appendable_snadd(app, buf, strlen(buf));
268 }
269
270 /**
271  * Determines the SSE suffix depending on the mode.
272  */
273 static int arm_get_mode_suffix(lc_appendable_t *app,
274     const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
275 {
276         ir_node *X = arg->v_ptr;
277
278         if (!X)
279                 return lc_appendable_snadd(app, "(null)", 6);
280
281         if (get_mode_size_bits(get_irn_mode(X)) == 32)
282                 return lc_appendable_chadd(app, 's');
283         else
284                 return lc_appendable_chadd(app, 'd');
285 }
286
287 /**
288  * Return the arm printf arg environment.
289  * We use the firm environment with some additional handlers.
290  */
291 const lc_arg_env_t *arm_get_arg_env(void) {
292         static lc_arg_env_t *env = NULL;
293
294         static const lc_arg_handler_t arm_reg_handler   = { arm_get_arg_type, arm_get_reg_name };
295         static const lc_arg_handler_t arm_const_handler = { arm_get_arg_type, arm_const_to_str };
296         static const lc_arg_handler_t arm_mode_handler  = { arm_get_arg_type, arm_get_mode_suffix };
297
298         if(env == NULL) {
299                 /* extend the firm printer */
300                 env = firm_get_arg_env();
301                         //lc_arg_new_env();
302
303                 lc_arg_register(env, "arm:sreg", 'S', &arm_reg_handler);
304                 lc_arg_register(env, "arm:dreg", 'D', &arm_reg_handler);
305                 lc_arg_register(env, "arm:cnst", 'C', &arm_const_handler);
306                 lc_arg_register(env, "arm:offs", 'O', &arm_const_handler);
307                 lc_arg_register(env, "arm:mode", 'M', &arm_mode_handler);
308         }
309
310         return env;
311 }
312
313 /**
314  * Formated print of commands and comments.
315  */
316 static void arm_fprintf_format(FILE *F, char *cmd_buf, char *cmnt_buf, ir_node *irn) {
317         lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
318 }
319
320 /*
321  * Add a number to a prefix. This number will not be used a second time.
322  */
323 static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
324         static unsigned long id = 0;
325         snprintf(buf, buflen, "%s%lu", prefix, ++id);
326         return buf;
327 }
328
329
330 /**
331  * Returns the target label for a control flow node.
332  */
333 static char *get_cfop_target(const ir_node *irn, char *buf) {
334         ir_node *bl = get_irn_link(irn);
335
336         snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
337         return buf;
338 }
339
340 /************************************************************************/
341 /* emit_arm                                                             */
342 /************************************************************************/
343
344 static void emit_arm_SymConst(ir_node *irn, void *env) {
345         arm_emit_env_t *emit_env = env;
346         FILE *out = emit_env->out;
347         char buffer1[SNPRINTF_BUF_LEN];
348         char *skip_label = get_unique_label(buffer1, SNPRINTF_BUF_LEN, ".L");
349         char buffer2[SNPRINTF_BUF_LEN];
350         char *indi_label = get_unique_label(buffer2, SNPRINTF_BUF_LEN, ".L");
351         fprintf( out, "\tB %s\t\t\t\t\t/* start of indirect SymConst */\n", skip_label );
352         fprintf( out, "\t.align 2\n" );
353         fprintf( out, "%s:\n", indi_label );
354         lc_efprintf(arm_get_arg_env(), out, "\t.word\t%C\n", irn);
355         fprintf( out, "\t.align 2\n" );
356         fprintf( out, "%s:\n", skip_label );
357         lc_efprintf(arm_get_arg_env(), out, "\tLDR %1D, %s\t\t\t/* end of indirect SymConst */\n", irn, indi_label);
358 }
359
360 static void emit_arm_CondJmp(ir_node *irn, void *env) {
361         arm_emit_env_t *emit_env = env;
362         FILE *out = emit_env->out;
363         const ir_edge_t *edge;
364         ir_node *true_block = NULL;
365         ir_node *false_block = NULL;
366         ir_node *op1 = get_irn_n(irn, 0);
367         ir_mode *opmode = get_irn_mode(op1);
368         char *suffix;
369         int proj_num = get_arm_proj_num(irn);
370         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
371
372
373         foreach_out_edge(irn, edge) {
374                 ir_node* proj = get_edge_src_irn(edge);
375                 long nr = get_Proj_proj(proj);
376                 ir_node *block = get_irn_link(proj);
377                 if ( nr == pn_Cond_true) {
378                         true_block = block;
379                 } else if (nr == pn_Cond_false) {
380                         false_block = block;
381                 } else {
382                         assert(0 && "tertium non datur! (CondJmp)");
383                 }
384         }
385
386         if (proj_num == pn_Cmp_False) {
387                 fprintf(out, "\tB BLOCK_%ld\t\t\t/* false case */\n", get_irn_node_nr(false_block));
388         } else if (proj_num == pn_Cmp_True) {
389                 fprintf(out, "\tB BLOCK_%ld\t\t\t/* true case */\n", get_irn_node_nr(true_block));
390         } else {
391                 if (mode_is_float(opmode)) {
392                         suffix = "ICHWILLIMPLEMENTIERTWERDEN";
393
394                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "FCMP %1S, %2S", irn, irn);
395                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> FCPSR */", irn, irn );
396                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
397
398                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "FMSTAT", irn, irn);
399                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* FCSPR -> CPSR */");
400                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
401
402                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "B%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
403                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
404                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
405
406                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "B BLOCK_%d", get_irn_node_nr(false_block));
407                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
408                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
409                 } else {
410                         switch(proj_num) {
411                                 case pn_Cmp_Eq:  suffix = "EQ"; break;
412                                 case pn_Cmp_Lt:  suffix = "LT"; break;
413                                 case pn_Cmp_Le:  suffix = "LE"; break;
414                                 case pn_Cmp_Gt:  suffix = "GT"; break;
415                                 case pn_Cmp_Ge:  suffix = "GE"; break;
416                                 case pn_Cmp_Lg:  suffix = "NE"; break;
417                                 case pn_Cmp_Leg: suffix = "AL"; break;
418                         default: assert(0 && "komische Dinge geschehen");
419                         }
420
421                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "CMP %1S, %2S", irn, irn);
422                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* Compare(%1S, %2S) -> CPSR */", 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, "B%s BLOCK_%d", suffix, get_irn_node_nr(true_block));
426                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* true case */");
427                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
428
429                         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "B BLOCK_%d", get_irn_node_nr(false_block));
430                         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* false case */");
431                         arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
432                 }
433         }
434 }
435
436 static void emit_arm_CopyB(ir_node *irn, void *env) {
437         arm_emit_env_t *emit_env = env;
438         FILE *out = emit_env->out;
439         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
440         unsigned int size = get_tarval_long(get_arm_value(irn));
441         const lc_arg_env_t *arg_env = arm_get_arg_env();
442
443         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);
444
445         assert ( size > 0 && "CopyB needs size > 0" );
446         if (size & 3)
447                 size += 4;
448         size >>= 2;
449         switch(size & 3) {
450         case 0:
451                 break;
452         case 1:
453                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDR %%r12, [%2S, #0]!", irn);
454                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
455                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STR  %%r12, [%1S, #0]!", irn);
456                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
457                 break;
458         case 2:
459                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDMIA %2S!, {%%r12, %3S}", irn, irn);
460                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
461                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STMIA %1S!, {%%r12, %3S}", irn, irn);
462                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
463                 break;
464         case 3:
465                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDMIA %2S!, {%%r12, %3S, %4S}", irn, irn, irn);
466                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
467                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STMIA %1S!, {%%r12, %3S, %4S}", irn, irn, irn);
468                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
469                 break;
470         }
471         size >>= 2;
472         while (size) {
473                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "LDMIA %2S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
474                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
475                 lc_esnprintf(arg_env, cmd_buf, SNPRINTF_BUF_LEN, "STMIA %1S!, {%%r12, %3S, %4S, %5S}", irn, irn, irn, irn);
476                 arm_fprintf_format(out, cmd_buf, cmnt_buf, irn);
477                 --size;
478         }
479 }
480
481 static void emit_arm_SwitchJmp(ir_node *irn, void *env) {
482         char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
483         const ir_edge_t    *edge;
484         ir_node            *proj;
485         arm_emit_env_t *emit_env = env;
486         FILE *out = emit_env->out;
487         int i;
488         ir_node **projs;
489         int n_projs;
490         int block_nr;
491         int default_block_num;
492
493         block_nr = get_irn_node_nr(irn);
494         n_projs = get_arm_n_projs(irn);
495
496         projs = xcalloc(n_projs , sizeof(ir_node*));
497
498         foreach_out_edge(irn, edge) {
499                 proj = get_edge_src_irn(edge);
500                 assert(is_Proj(proj) && "Only proj allowed at SwitchJmp");
501
502                 if (get_Proj_proj(proj) == get_arm_default_proj_num(irn))
503                         default_block_num = get_irn_node_nr(get_irn_link(proj));
504
505                 projs[get_Proj_proj(proj)] = proj;
506         }
507
508         // CMP %1S, n_projs - 1
509         // BHI default
510
511
512
513         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "CMP %1S, #%u", irn, n_projs - 1);
514         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
515         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
516
517         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "BHI BLOCK_%d", default_block_num);
518         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
519         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
520
521
522         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "LDR %%r12, TABLE_%d_START", block_nr);
523         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
524         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
525
526         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %%r12, %%r12, %1S, LSL #2", irn);
527         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
528         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
529
530         lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "LDR %%r15, [%%r12, #0]");
531         lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "", irn);
532         lc_efprintf(arm_get_arg_env(), out, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
533
534         // LDR %r12, .TABLE_X_START
535         // ADD %r12, %r12, [%1S, LSL #2]
536         // LDR %r15, %r12
537
538         fprintf(out, "TABLE_%d_START:\n\t.word\tTABLE_%d\n", block_nr, block_nr);
539         fprintf(out, "\t.align 2\n");
540         fprintf(out, "TABLE_%d:\n", block_nr);
541
542
543         for ( i=0; i<n_projs; i++) {
544                 ir_node *block;
545                 proj = projs[i];
546                 if ( proj ) {
547                         block = get_irn_link(proj);
548                 } else {
549                         block = get_irn_link(projs[get_arm_default_proj_num(irn)]);
550                 }
551                 fprintf(out, "\t.word\tBLOCK_%ld\n",get_irn_node_nr(block));
552         }
553         fprintf(out, "\t.align 2\n");
554
555         xfree(projs);
556 }
557
558 /************************************************************************/
559 /* emit_be                                                              */
560 /************************************************************************/
561
562 static void emit_be_Call(ir_node *irn, void *env) {
563         arm_emit_env_t *emit_env = env;
564         FILE *out = emit_env->out;
565         entity *target_entity = be_Call_get_entity(irn);
566         const char *target_name = get_entity_name(target_entity);
567         fprintf(out, "\tBL %s\t\t\t\t/* Call */\n", target_name);
568 }
569
570 static void emit_be_IncSP(const ir_node *irn, arm_emit_env_t *emit_env) {
571         FILE *F = emit_env->out;
572         unsigned offs = be_get_IncSP_offset(irn);
573         if (offs) {
574                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
575                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, #%O", irn, irn, irn );
576                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* IncSP(%O) */", irn);
577                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
578         } else {
579                 char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
580                 lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "");
581                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* omitted IncSP(%O) */", irn);
582                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
583         }
584 }
585
586 // void emit_be_AddSP(const ir_node *irn, arm_emit_env_t *emit_env) {
587 //      FILE *F = emit_env->out;
588 //      char cmd_buf[SNPRINTF_BUF_LEN], cmnt_buf[SNPRINTF_BUF_LEN];
589 //      lc_esnprintf(arm_get_arg_env(), cmd_buf, SNPRINTF_BUF_LEN, "ADD %1D, %1S, %2S", irn, irn, irn );
590 //      lc_esnprintf(arm_get_arg_env(), cmnt_buf, SNPRINTF_BUF_LEN, "/* AddSP(%2S) */", irn);
591 //      lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
592 // }
593
594 static void emit_be_Copy(const ir_node *irn, arm_emit_env_t *emit_env) {
595         FILE *F    = emit_env->out;
596         ir_mode *mode = get_irn_mode(irn);
597         assert( (mode != mode_E) && "IEEE Extended FP not supported");
598
599         if (get_in_reg(irn, 0) == get_out_reg(irn, 0)) {
600                 char cmd_buf[256], cmnt_buf[256];
601                 lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "");
602                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* omitted Copy: %1S -> %1D */", irn, irn);
603                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
604                 return;
605         }
606
607         if (mode == mode_F) {
608                 char cmd_buf[256], cmnt_buf[256];
609                 lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "FCPYS %1D, %1S", irn, irn);
610                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* Copy: %1S -> %1D */", irn, irn);
611                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
612         } else if (mode == mode_D) {
613                 char cmd_buf[256], cmnt_buf[256];
614                 lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "FCPYD %1D, %1S", irn, irn);
615                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* Copy: %1S -> %1D */", irn, irn);
616                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
617         } else if (mode_is_numP(mode)) {
618                 char cmd_buf[256], cmnt_buf[256];
619                 lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "MOV %1D, %1S", irn, irn);
620                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* Copy: %1S -> %1D */", irn, irn);
621                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
622         } else {
623                 assert(0 && "move not supported for this mode");
624         }
625 //      emit_arm_Copy(irn, emit_env);
626 }
627
628 static void emit_be_Spill(const ir_node *irn, arm_emit_env_t *emit_env) {
629         FILE *F = emit_env->out;
630         ir_mode *mode = get_irn_mode(irn);
631         assert( (mode != mode_E) && "IEEE Extended FP not supported");
632         if (mode_is_dataM(mode)) {
633                 char cmd_buf[256], cmnt_buf[256];
634                 lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "STR %2S, [%1S, #%O]", irn, irn, irn );
635                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* Spill(%2S) -> (%1S) */", irn, irn);
636                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
637         } else {
638                 assert(0 && "spill not supported for this mode");
639         }
640 }
641
642 static void emit_be_Reload(const ir_node* irn, arm_emit_env_t *emit_env) {
643         FILE *F = emit_env->out;
644         ir_mode *mode = get_irn_mode(irn);
645         assert( (mode != mode_E) && "IEEE Extended FP not supported");
646         if (mode_is_dataM(mode)) {
647                 char cmd_buf[256], cmnt_buf[256];
648                 lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "LDR %1D, [%1S, #%O]", irn, irn, irn );
649                 lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* Reload(%1S) -> (%1D) */", irn, irn);
650                 lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
651         } else {
652                 assert(0 && "reload not supported for this mode");
653         }
654 }
655
656 static void emit_be_Perm(const ir_node* irn, arm_emit_env_t *emit_env) {
657         FILE *F = emit_env->out;
658         ir_mode *mode = get_irn_mode(irn);
659         assert( (mode != mode_E) && "IEEE Extended FP not supported");
660         lc_efprintf(arm_get_arg_env(), F, "\tEOR %1S, %1S, %2S\t\t\t/* begin Perm(%1S, %2S) */\n", irn, irn, irn, irn, irn);
661         lc_efprintf(arm_get_arg_env(), F, "\tEOR %2S, %1S, %2S\n", irn, irn, irn);
662         lc_efprintf(arm_get_arg_env(), F, "\tEOR %1S, %1S, %2S\t\t\t/* end Perm(%1S, %2S) */\n", irn, irn, irn, irn, irn);
663 }
664
665 static void emit_be_StackParam(const ir_node *irn, arm_emit_env_t *emit_env) {
666         FILE *F = emit_env->out;
667         ir_mode *mode = get_irn_mode(irn);
668         char cmd_buf[256], cmnt_buf[256];
669         assert( (mode != mode_E) && "IEEE Extended FP not supported");
670
671         lc_esnprintf(arm_get_arg_env(), cmd_buf, 256, "LDR %1D, [%1S, #%O]", irn, irn, irn );
672         lc_esnprintf(arm_get_arg_env(), cmnt_buf, 256, "/* StackParam: (%1S + %O) -> %1D */",irn , irn, irn, get_irn_n(irn, 0));
673         lc_efprintf(arm_get_arg_env(), F, "\t%-35s %-60s /* %+F */\n", cmd_buf, cmnt_buf, irn);
674 }
675
676 /************************************************************************/
677 /* emit                                                                 */
678 /************************************************************************/
679
680 static void emit_Jmp(ir_node *irn, void *env) {
681         arm_emit_env_t *emit_env = env;
682         FILE *out = emit_env->out;
683         const ir_edge_t *edge = get_irn_out_edge_first(irn);
684         ir_node *target_block = get_edge_src_irn(edge);
685         fprintf(out, "\tB BLOCK_%ld\t\t\t/* unconditional Jump */\n", get_irn_node_nr(target_block));
686 }
687
688 static void emit_silence(ir_node *irn, void *env) {
689
690 }
691
692
693
694 /***********************************************************************************
695  *                  _          __                                             _
696  *                 (_)        / _|                                           | |
697  *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
698  * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
699  * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
700  * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
701  *
702  ***********************************************************************************/
703
704 /**
705  * Enters the emitter functions for handled nodes into the generic
706  * pointer of an opcode.
707  */
708 static void arm_register_emitters(void) {
709
710 #define ARM_EMIT(a) op_arm_##a->ops.generic = (op_func)emit_arm_##a
711 #define EMIT(a)      op_##a->ops.generic = (op_func)emit_##a
712 #define BE_EMIT(a)   op_be_##a->ops.generic = (op_func)emit_be_##a
713 #define SILENCE(a)   op_##a->ops.generic = (op_func)emit_silence
714
715         /* first clear the generic function pointer for all ops */
716         clear_irp_opcodes_generic_func();
717
718         /* register all emitter functions defined in spec */
719         arm_register_spec_emitters();
720
721         /* other emitter functions */
722         ARM_EMIT(CondJmp);
723 //      ARM_EMIT(SwitchJmp);
724         ARM_EMIT(CopyB);
725 //      ARM_EMIT(CopyB_i);
726 //      ARM_EMIT(Const);
727         ARM_EMIT(SymConst);
728         ARM_EMIT(SwitchJmp);
729
730         /* benode emitter */
731         BE_EMIT(Call);
732         BE_EMIT(IncSP);
733 //      BE_EMIT(AddSP);
734         BE_EMIT(Copy);
735         BE_EMIT(Spill);
736         BE_EMIT(Reload);
737         BE_EMIT(Perm);
738         BE_EMIT(StackParam);
739
740         /* firm emitter */
741         EMIT(Jmp);
742
743
744         /* noisy stuff */
745 #ifdef SILENCER
746         SILENCE(Proj);
747         SILENCE(Phi);
748 #endif
749
750 #undef ARM_EMIT
751 #undef BE_EMIT
752 #undef EMIT
753 #undef SILENCE
754 }
755
756 /**
757  * Emits code for a node.
758  */
759 static void arm_emit_node(const ir_node *irn, void *env) {
760         arm_emit_env_t        *emit_env = env;
761         FILE              *F        = emit_env->out;
762         ir_op             *op       = get_irn_op(irn);
763         DEBUG_ONLY(firm_dbg_module_t *mod      = emit_env->mod;)
764
765         DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
766
767         if (op->ops.generic) {
768                 void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
769                 (*emit)(irn, env);
770         }
771         else {
772                 ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
773         }
774 }
775
776 /**
777  * Walks over the nodes in a block connected by scheduling edges
778  * and emits code for each node.
779  */
780 void arm_gen_block(ir_node *block, void *env) {
781         ir_node *irn;
782
783         if (! is_Block(block))
784                 return;
785
786         fprintf(((arm_emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
787         sched_foreach(block, irn) {
788                 arm_emit_node(irn, env);
789         }
790 }
791
792
793 /**
794  * Emits code for function start.
795  */
796 void arm_emit_start(FILE *F, ir_graph *irg) {
797         const char *irg_name = get_entity_name(get_irg_entity(irg));
798         fprintf(F, "\t.text\n");
799         fprintf(F, "\t.align  2\n");
800         fprintf(F, "\t.global %s\n", irg_name);
801         fprintf(F, "%s:\n", irg_name);
802 }
803
804 /**
805  * Emits code for function end
806  */
807 void arm_emit_end(FILE *F, ir_graph *irg) {
808 }
809
810 /**
811  * Sets labels for control flow nodes (jump target)
812  * TODO: Jump optimization
813  */
814 void arm_gen_labels(ir_node *block, void *env) {
815         ir_node *pred;
816         int n = get_Block_n_cfgpreds(block);
817
818         for (n--; n >= 0; n--) {
819                 pred = get_Block_cfgpred(block, n);
820                 set_irn_link(pred, block);
821         }
822 }
823
824
825 /**
826  * Main driver
827  */
828 void arm_gen_routine(FILE *F, ir_graph *irg, const arm_code_gen_t *cg) {
829         arm_emit_env_t emit_env;
830
831         emit_env.out      = F;
832         emit_env.arch_env = cg->arch_env;
833         emit_env.cg       = cg;
834         FIRM_DBG_REGISTER(emit_env.mod, "firm.be.arm.emit");
835
836         /* set the global arch_env (needed by print hooks) */
837         arch_env = cg->arch_env;
838
839         arm_register_emitters();
840
841         arm_emit_start(F, irg);
842         irg_block_walk_graph(irg, arm_gen_labels, NULL, &emit_env);
843         irg_walk_blkwise_graph(irg, NULL, arm_gen_block, &emit_env);
844         arm_emit_end(F, irg);
845 }