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
39 #include "../besched.h"
40 #include "../begnuas.h"
41 #include "../beblocksched.h"
42 #include "../benode.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 #define SNPRINTF_BUF_LEN 128
53 * Returns the register at in position pos.
55 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
58 const arch_register_t *reg = NULL;
60 assert(get_irn_arity(node) > pos && "Invalid IN position");
62 /* The out register of the operator at position pos is the
63 in register we need. */
64 op = get_irn_n(node, pos);
66 reg = arch_get_irn_register(op);
68 assert(reg && "no in register found");
73 * Returns the register at out position pos.
75 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
78 const arch_register_t *reg = NULL;
80 /* 1st case: irn is not of mode_T, so it has only */
81 /* one OUT register -> good */
82 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
83 /* Proj with the corresponding projnum for the register */
85 if (get_irn_mode(node) != mode_T) {
86 reg = arch_get_irn_register(node);
87 } else if (is_TEMPLATE_irn(node)) {
88 reg = arch_irn_get_register(node, pos);
90 const ir_edge_t *edge;
92 foreach_out_edge(node, edge) {
93 proj = get_edge_src_irn(edge);
94 assert(is_Proj(proj) && "non-Proj from mode_T node");
95 if (get_Proj_proj(proj) == pos) {
96 reg = arch_get_irn_register(proj);
102 assert(reg && "no out register found");
108 void TEMPLATE_emit_immediate(const ir_node *node)
110 const TEMPLATE_attr_t *attr = get_TEMPLATE_attr_const(node);
111 be_emit_tarval(attr->value);
114 static void emit_register(const arch_register_t *reg)
116 be_emit_string(arch_register_get_name(reg));
119 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
121 const arch_register_t *reg = get_in_reg(node, pos);
125 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
127 const arch_register_t *reg = get_out_reg(node, pos);
132 * Returns the target label for a control flow node.
134 static void TEMPLATE_emit_cfop_target(const ir_node *node)
136 ir_node *block = (ir_node*)get_irn_link(node);
137 be_gas_emit_block_name(block);
143 * Emits code for a unconditional jump.
145 static void emit_TEMPLATE_Jmp(const ir_node *node)
147 be_emit_cstring("\tjmp ");
148 TEMPLATE_emit_cfop_target(node);
149 be_emit_finish_line_gas(node);
152 static void emit_be_IncSP(const ir_node *node)
154 int offset = be_get_IncSP_offset(node);
159 /* downwards growing stack */
161 be_emit_cstring("\tsub ");
163 be_emit_cstring("\tadd ");
167 TEMPLATE_emit_source_register(node, 0);
168 be_emit_irprintf(", %d, ", offset);
169 TEMPLATE_emit_dest_register(node, 0);
170 be_emit_finish_line_gas(node);
173 static void emit_be_Start(const ir_node *node)
175 ir_graph *irg = get_irn_irg(node);
176 ir_type *frame_type = get_irg_frame_type(irg);
177 unsigned size = get_type_size_bytes(frame_type);
179 /* emit function prolog */
181 /* allocate stackframe */
183 be_emit_cstring("\tsub ");
184 emit_register(&TEMPLATE_registers[REG_SP]);
185 be_emit_irprintf(", %u, ", size);
186 emit_register(&TEMPLATE_registers[REG_SP]);
187 be_emit_finish_line_gas(node);
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 be_emit_cstring("\tadd ");
202 emit_register(&TEMPLATE_registers[REG_SP]);
203 be_emit_irprintf(", %u, ", size);
204 emit_register(&TEMPLATE_registers[REG_SP]);
205 be_emit_finish_line_gas(node);
209 be_emit_cstring("\tret");
210 be_emit_finish_line_gas(node);
213 static void emit_nothing(const ir_node *node)
219 * The type of a emitter function.
221 typedef void (emit_func)(const ir_node *node);
223 static inline void set_emitter(ir_op *op, emit_func func)
225 op->ops.generic = (op_func)func;
229 * Enters the emitter functions for handled nodes into the generic
230 * pointer of an opcode.
232 static void TEMPLATE_register_emitters(void)
234 /* first clear the generic function pointer for all ops */
235 clear_irp_opcodes_generic_func();
237 /* register all emitter functions defined in spec */
238 TEMPLATE_register_spec_emitters();
240 /* custom emitters not provided by the spec */
241 set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
242 set_emitter(op_be_IncSP, emit_be_IncSP);
243 set_emitter(op_be_Return, emit_be_Return);
244 set_emitter(op_be_Start, emit_be_Start);
246 /* no need to emit anything for the following nodes */
247 set_emitter(op_Phi, emit_nothing);
248 set_emitter(op_be_Keep, emit_nothing);
251 typedef void (*emit_func_ptr) (const ir_node *);
254 * Emits code for a node.
256 static void TEMPLATE_emit_node(const ir_node *node)
258 ir_op *op = get_irn_op(node);
260 if (op->ops.generic) {
261 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
264 ir_fprintf(stderr, "No emitter for node %+F\n", node);
269 * Walks over the nodes in a block connected by scheduling edges
270 * and emits code for each node.
272 static void TEMPLATE_emit_block(ir_node *block)
276 be_gas_emit_block_name(block);
277 be_emit_cstring(":\n");
278 be_emit_write_line();
280 sched_foreach(block, node) {
281 TEMPLATE_emit_node(node);
286 * Sets labels for control flow nodes (jump target)
288 static void TEMPLATE_gen_labels(ir_node *block, void *env)
291 int n = get_Block_n_cfgpreds(block);
294 for (n--; n >= 0; n--) {
295 pred = get_Block_cfgpred(block, n);
296 set_irn_link(pred, block);
303 void TEMPLATE_emit_routine(ir_graph *irg)
305 ir_node **block_schedule;
306 ir_entity *entity = get_irg_entity(irg);
310 /* register all emitter functions */
311 TEMPLATE_register_emitters();
313 /* create the block schedule */
314 block_schedule = be_create_block_schedule(irg);
316 /* emit assembler prolog */
317 be_gas_emit_function_prolog(entity, 4);
319 /* populate jump link fields with their destinations */
320 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
322 n = ARR_LEN(block_schedule);
323 for (i = 0; i < n; ++i) {
324 ir_node *block = block_schedule[i];
325 TEMPLATE_emit_block(block);
327 be_gas_emit_function_epilog(entity);