2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief emit assembler for a backend graph
41 #include "beblocksched.h"
44 #include "TEMPLATE_emitter.h"
45 #include "gen_TEMPLATE_emitter.h"
46 #include "gen_TEMPLATE_regalloc_if.h"
47 #include "TEMPLATE_nodes_attr.h"
48 #include "TEMPLATE_new_nodes.h"
50 static void TEMPLATE_emit_immediate(const ir_node *node)
52 const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
53 be_emit_irprintf("%T", attr->value);
56 static void emit_register(const arch_register_t *reg)
58 be_emit_string(reg->name);
61 static void TEMPLATE_emit_source_register(const ir_node *node, int pos)
63 const arch_register_t *reg = arch_get_irn_register_in(node, pos);
67 static void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
69 const arch_register_t *reg = arch_get_irn_register_out(node, pos);
74 * Returns the target label for a control flow node.
76 static void TEMPLATE_emit_cfop_target(const ir_node *node)
78 ir_node *block = (ir_node*)get_irn_link(node);
79 be_gas_emit_block_name(block);
82 void TEMPLATE_emitf(const ir_node *node, const char *format, ...)
88 const char *start = format;
89 while (*format != '%' && *format != '\0')
91 be_emit_string_len(start, format-start);
102 if (*format < '0' || '9' <= *format)
104 unsigned const pos = *format++ - '0';
105 TEMPLATE_emit_source_register(node, pos);
110 if (*format < '0' || '9' <= *format)
112 unsigned const pos = *format++ - '0';
113 TEMPLATE_emit_dest_register(node, pos);
118 TEMPLATE_emit_immediate(node);
122 int num = va_arg(ap, int);
123 be_emit_irprintf("%X", num);
128 unsigned num = va_arg(ap, unsigned);
129 be_emit_irprintf("%u", num);
134 int num = va_arg(ap, int);
135 be_emit_irprintf("%d", num);
140 const char *string = va_arg(ap, const char *);
141 be_emit_string(string);
146 TEMPLATE_emit_cfop_target(node);
152 be_emit_write_line();
158 panic("unknown format conversion");
162 be_emit_finish_line_gas(node);
167 * Emits code for a unconditional jump.
169 static void emit_TEMPLATE_Jmp(const ir_node *node)
171 TEMPLATE_emitf(node, "jmp %L");
174 static void emit_be_IncSP(const ir_node *node)
176 int offset = be_get_IncSP_offset(node);
181 /* downwards growing stack */
182 const char *op = "add";
188 TEMPLATE_emitf(node, "%s %S0, %d, %D0", op, offset);
191 static void emit_be_Start(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 prolog */
199 /* allocate stackframe */
201 TEMPLATE_emitf(node, "sub %%sp, %u, %%sp", size);
205 static void emit_be_Return(const ir_node *node)
207 ir_graph *irg = get_irn_irg(node);
208 ir_type *frame_type = get_irg_frame_type(irg);
209 unsigned size = get_type_size_bytes(frame_type);
211 /* emit function epilog here */
213 /* deallocate stackframe */
215 TEMPLATE_emitf(node, "add %%sp, %u, %%sp", size);
219 TEMPLATE_emitf(node, "ret");
223 * Enters the emitter functions for handled nodes into the generic
224 * pointer of an opcode.
226 static void TEMPLATE_register_emitters(void)
228 /* first clear the generic function pointer for all ops */
229 ir_clear_opcodes_generic_func();
231 /* register all emitter functions defined in spec */
232 TEMPLATE_register_spec_emitters();
234 /* custom emitters not provided by the spec */
235 be_set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
236 be_set_emitter(op_be_IncSP, emit_be_IncSP);
237 be_set_emitter(op_be_Return, emit_be_Return);
238 be_set_emitter(op_be_Start, emit_be_Start);
240 /* no need to emit anything for the following nodes */
241 be_set_emitter(op_Phi, be_emit_nothing);
242 be_set_emitter(op_be_Keep, be_emit_nothing);
245 typedef void (*emit_func_ptr) (const ir_node *);
248 * Emits code for a node.
250 static void TEMPLATE_emit_node(const ir_node *node)
252 ir_op *op = get_irn_op(node);
254 if (op->ops.generic) {
255 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
258 ir_fprintf(stderr, "No emitter for node %+F\n", node);
263 * Walks over the nodes in a block connected by scheduling edges
264 * and emits code for each node.
266 static void TEMPLATE_emit_block(ir_node *block)
268 be_gas_begin_block(block, true);
270 sched_foreach(block, node) {
271 TEMPLATE_emit_node(node);
276 * Sets labels for control flow nodes (jump target)
278 static void TEMPLATE_gen_labels(ir_node *block, void *env)
281 int n = get_Block_n_cfgpreds(block);
284 for (n--; n >= 0; n--) {
285 pred = get_Block_cfgpred(block, n);
286 set_irn_link(pred, block);
293 void TEMPLATE_emit_routine(ir_graph *irg)
295 ir_node **block_schedule;
296 ir_entity *entity = get_irg_entity(irg);
300 /* register all emitter functions */
301 TEMPLATE_register_emitters();
303 /* create the block schedule */
304 block_schedule = be_create_block_schedule(irg);
306 /* emit assembler prolog */
307 be_gas_emit_function_prolog(entity, 4, NULL);
309 /* populate jump link fields with their destinations */
310 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
312 n = ARR_LEN(block_schedule);
313 for (i = 0; i < n; ++i) {
314 ir_node *block = block_schedule[i];
315 TEMPLATE_emit_block(block);
317 be_gas_emit_function_epilog(entity);