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 "TEMPLATE_nodes_attr.h"
47 #include "TEMPLATE_new_nodes.h"
49 #define SNPRINTF_BUF_LEN 128
52 * Returns the register at in position pos.
54 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
57 const arch_register_t *reg = NULL;
59 assert(get_irn_arity(node) > pos && "Invalid IN position");
61 /* The out register of the operator at position pos is the
62 in register we need. */
63 op = get_irn_n(node, pos);
65 reg = arch_get_irn_register(op);
67 assert(reg && "no in register found");
72 * Returns the register at out position pos.
74 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
77 const arch_register_t *reg = NULL;
79 /* 1st case: irn is not of mode_T, so it has only */
80 /* one OUT register -> good */
81 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
82 /* Proj with the corresponding projnum for the register */
84 if (get_irn_mode(node) != mode_T) {
85 reg = arch_get_irn_register(node);
86 } else if (is_TEMPLATE_irn(node)) {
87 reg = arch_irn_get_register(node, pos);
89 const ir_edge_t *edge;
91 foreach_out_edge(node, edge) {
92 proj = get_edge_src_irn(edge);
93 assert(is_Proj(proj) && "non-Proj from mode_T node");
94 if (get_Proj_proj(proj) == pos) {
95 reg = arch_get_irn_register(proj);
101 assert(reg && "no out register found");
107 void TEMPLATE_emit_immediate(const ir_node *node)
109 const TEMPLATE_attr_t *attr = get_irn_generic_attr_const(node);
110 be_emit_tarval(attr->value);
113 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
115 const arch_register_t *reg = get_in_reg(node, pos);
116 be_emit_string(arch_register_get_name(reg));
119 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
121 const arch_register_t *reg = get_out_reg(node, pos);
122 be_emit_string(arch_register_get_name(reg));
126 * Returns the target label for a control flow node.
128 static void TEMPLATE_emit_cfop_target(const ir_node *node)
130 ir_node *block = get_irn_link(node);
131 be_gas_emit_block_name(block);
137 * Emits code for a unconditional jump.
139 static void emit_TEMPLATE_Jmp(const ir_node *node)
141 be_emit_cstring("\tjmp ");
142 TEMPLATE_emit_cfop_target(node);
143 be_emit_finish_line_gas(node);
146 static void emit_be_IncSP(const ir_node *node)
148 int offset = be_get_IncSP_offset(node);
153 /* downwards growing stack */
155 be_emit_cstring("\tsub ");
157 be_emit_cstring("\tadd ");
161 TEMPLATE_emit_source_register(node, 0);
162 be_emit_irprintf(", %d, ", offset);
163 TEMPLATE_emit_dest_register(node, 0);
164 be_emit_finish_line_gas(node);
167 static void emit_be_Return(const ir_node *node)
169 be_emit_cstring("\tret");
170 be_emit_finish_line_gas(node);
173 static void emit_nothing(const ir_node *node)
179 * The type of a emitter function.
181 typedef void (emit_func)(const ir_node *node);
183 static inline void set_emitter(ir_op *op, emit_func func)
185 op->ops.generic = (op_func)func;
189 * Enters the emitter functions for handled nodes into the generic
190 * pointer of an opcode.
192 static void TEMPLATE_register_emitters(void)
194 /* first clear the generic function pointer for all ops */
195 clear_irp_opcodes_generic_func();
197 /* register all emitter functions defined in spec */
198 TEMPLATE_register_spec_emitters();
200 /* custom emitters not provided by the spec */
201 set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
202 set_emitter(op_be_Return, emit_be_Return);
203 set_emitter(op_be_IncSP, emit_be_IncSP);
205 /* no need to emit anything for the following nodes */
206 set_emitter(op_Phi, emit_nothing);
207 set_emitter(op_be_Keep, emit_nothing);
208 set_emitter(op_be_Start, emit_nothing);
209 set_emitter(op_be_Barrier, emit_nothing);
212 typedef void (*emit_func_ptr) (const ir_node *);
215 * Emits code for a node.
217 static void TEMPLATE_emit_node(const ir_node *node)
219 ir_op *op = get_irn_op(node);
221 if (op->ops.generic) {
222 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
225 ir_fprintf(stderr, "No emitter for node %+F\n", node);
230 * Walks over the nodes in a block connected by scheduling edges
231 * and emits code for each node.
233 static void TEMPLATE_emit_block(ir_node *block)
237 be_gas_emit_block_name(block);
238 be_emit_cstring(":\n");
239 be_emit_write_line();
241 sched_foreach(block, node) {
242 TEMPLATE_emit_node(node);
247 * Sets labels for control flow nodes (jump target)
249 static void TEMPLATE_gen_labels(ir_node *block, void *env)
252 int n = get_Block_n_cfgpreds(block);
255 for (n--; n >= 0; n--) {
256 pred = get_Block_cfgpred(block, n);
257 set_irn_link(pred, block);
264 void TEMPLATE_emit_routine(ir_graph *irg)
266 ir_node **block_schedule;
267 ir_entity *entity = get_irg_entity(irg);
271 /* register all emitter functions */
272 TEMPLATE_register_emitters();
274 /* create the block schedule */
275 block_schedule = be_create_block_schedule(irg);
277 /* emit assembler prolog */
278 be_gas_emit_function_prolog(entity, 4);
280 /* populate jump link fields with their destinations */
281 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
283 n = ARR_LEN(block_schedule);
284 for (i = 0; i < n; ++i) {
285 ir_node *block = block_schedule[i];
286 TEMPLATE_emit_block(block);
288 be_gas_emit_function_epilog(entity);