2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief emit assembler for a backend graph
27 #include "beblocksched.h"
30 #include "TEMPLATE_emitter.h"
31 #include "gen_TEMPLATE_emitter.h"
32 #include "gen_TEMPLATE_regalloc_if.h"
33 #include "TEMPLATE_nodes_attr.h"
34 #include "TEMPLATE_new_nodes.h"
36 static void TEMPLATE_emit_immediate(const ir_node *node)
38 const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
39 be_emit_irprintf("%T", attr->value);
42 static void emit_register(const arch_register_t *reg)
44 be_emit_string(reg->name);
47 static void TEMPLATE_emit_source_register(const ir_node *node, int pos)
49 const arch_register_t *reg = arch_get_irn_register_in(node, pos);
53 static void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
55 const arch_register_t *reg = arch_get_irn_register_out(node, pos);
60 * Returns the target label for a control flow node.
62 static void TEMPLATE_emit_cfop_target(const ir_node *node)
64 ir_node *block = (ir_node*)get_irn_link(node);
65 be_gas_emit_block_name(block);
68 void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
74 const char *start = format;
75 while (*format != '%' && *format != '\0')
77 be_emit_string_len(start, format-start);
88 if (*format < '0' || '9' <= *format)
90 unsigned const pos = *format++ - '0';
91 TEMPLATE_emit_source_register(node, pos);
96 if (*format < '0' || '9' <= *format)
98 unsigned const pos = *format++ - '0';
99 TEMPLATE_emit_dest_register(node, pos);
104 TEMPLATE_emit_immediate(node);
108 int num = va_arg(ap, int);
109 be_emit_irprintf("%X", num);
114 unsigned num = va_arg(ap, unsigned);
115 be_emit_irprintf("%u", num);
120 int num = va_arg(ap, int);
121 be_emit_irprintf("%d", num);
126 const char *string = va_arg(ap, const char *);
127 be_emit_string(string);
132 TEMPLATE_emit_cfop_target(node);
138 be_emit_write_line();
144 panic("unknown format conversion");
148 be_emit_finish_line_gas(node);
153 * Emits code for a unconditional jump.
155 static void emit_TEMPLATE_Jmp(const ir_node *node)
157 TEMPLATE_emitf(node, "jmp %L");
160 static void emit_be_IncSP(const ir_node *node)
162 int offset = be_get_IncSP_offset(node);
167 /* downwards growing stack */
168 const char *op = "add";
174 TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
177 static void emit_be_Start(const ir_node *node)
179 ir_graph *irg = get_irn_irg(node);
180 ir_type *frame_type = get_irg_frame_type(irg);
181 unsigned size = get_type_size_bytes(frame_type);
183 /* emit function prolog */
185 /* allocate stackframe */
187 TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
191 static void emit_be_Return(const ir_node *node)
193 ir_graph *irg = get_irn_irg(node);
194 ir_type *frame_type = get_irg_frame_type(irg);
195 unsigned size = get_type_size_bytes(frame_type);
197 /* emit function epilog here */
199 /* deallocate stackframe */
201 TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
205 TEMPLATE_emitf(node, "ret");
209 * Enters the emitter functions for handled nodes into the generic
210 * pointer of an opcode.
212 static void TEMPLATE_register_emitters(void)
214 /* first clear the generic function pointer for all ops */
215 ir_clear_opcodes_generic_func();
217 /* register all emitter functions defined in spec */
218 TEMPLATE_register_spec_emitters();
220 /* custom emitters not provided by the spec */
221 be_set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
222 be_set_emitter(op_be_IncSP, emit_be_IncSP);
223 be_set_emitter(op_be_Return, emit_be_Return);
224 be_set_emitter(op_be_Start, emit_be_Start);
226 /* no need to emit anything for the following nodes */
227 be_set_emitter(op_Phi, be_emit_nothing);
228 be_set_emitter(op_be_Keep, be_emit_nothing);
232 * Walks over the nodes in a block connected by scheduling edges
233 * and emits code for each node.
235 static void TEMPLATE_emit_block(ir_node *block)
237 be_gas_begin_block(block, true);
239 sched_foreach(block, node) {
245 * Sets labels for control flow nodes (jump target)
247 static void TEMPLATE_gen_labels(ir_node *block, void *env)
250 int n = get_Block_n_cfgpreds(block);
253 for (n--; n >= 0; n--) {
254 pred = get_Block_cfgpred(block, n);
255 set_irn_link(pred, block);
262 void TEMPLATE_emit_routine(ir_graph *irg)
264 ir_node **block_schedule;
265 ir_entity *entity = get_irg_entity(irg);
269 /* register all emitter functions */
270 TEMPLATE_register_emitters();
272 /* create the block schedule */
273 block_schedule = be_create_block_schedule(irg);
275 /* emit assembler prolog */
276 be_gas_emit_function_prolog(entity, 4, NULL);
278 /* populate jump link fields with their destinations */
279 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
281 n = ARR_LEN(block_schedule);
282 for (i = 0; i < n; ++i) {
283 ir_node *block = block_schedule[i];
284 TEMPLATE_emit_block(block);
286 be_gas_emit_function_epilog(entity);