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