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");
105 /*************************************************************
107 * (_) | | / _| | | | |
108 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
109 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
110 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
111 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
114 *************************************************************/
116 void TEMPLATE_emit_immediate(const ir_node *node)
118 const TEMPLATE_attr_t *attr = get_irn_generic_attr_const(node);
119 be_emit_tarval(attr->value);
122 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
124 const arch_register_t *reg = get_in_reg(node, pos);
125 be_emit_string(arch_register_get_name(reg));
128 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
130 const arch_register_t *reg = get_out_reg(node, pos);
131 be_emit_string(arch_register_get_name(reg));
135 * Returns the target label for a control flow node.
137 static void TEMPLATE_emit_cfop_target(const ir_node *node)
139 ir_node *block = get_irn_link(node);
140 be_gas_emit_block_name(block);
143 /***********************************************************************************
146 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
147 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
148 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
149 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
151 ***********************************************************************************/
154 * Emits code for a unconditional jump.
156 static void emit_TEMPLATE_Jmp(const ir_node *node)
158 be_emit_cstring("\tjmp ");
159 TEMPLATE_emit_cfop_target(node);
160 be_emit_finish_line_gas(node);
163 static void emit_be_IncSP(const ir_node *node)
165 int offset = be_get_IncSP_offset(node);
170 /* downwards growing stack */
172 be_emit_cstring("\tsub ");
174 be_emit_cstring("\tadd ");
178 TEMPLATE_emit_source_register(node, 0);
179 be_emit_irprintf(", %d, ", offset);
180 TEMPLATE_emit_dest_register(node, 0);
181 be_emit_finish_line_gas(node);
184 static void emit_be_Return(const ir_node *node)
186 be_emit_cstring("\tret");
187 be_emit_finish_line_gas(node);
190 static void emit_nothing(const ir_node *node)
196 * The type of a emitter function.
198 typedef void (emit_func)(const ir_node *node);
200 static inline void set_emitter(ir_op *op, emit_func func)
202 op->ops.generic = (op_func)func;
206 * Enters the emitter functions for handled nodes into the generic
207 * pointer of an opcode.
209 static void TEMPLATE_register_emitters(void)
211 /* first clear the generic function pointer for all ops */
212 clear_irp_opcodes_generic_func();
214 /* register all emitter functions defined in spec */
215 TEMPLATE_register_spec_emitters();
217 /* custom emitters not provided by the spec */
218 set_emitter(op_TEMPLATE_Jmp, emit_TEMPLATE_Jmp);
219 set_emitter(op_be_Return, emit_be_Return);
220 set_emitter(op_be_IncSP, emit_be_IncSP);
222 /* no need to emit anything for the following nodes */
223 set_emitter(op_Phi, emit_nothing);
224 set_emitter(op_be_Keep, emit_nothing);
225 set_emitter(op_be_Start, emit_nothing);
226 set_emitter(op_be_Barrier, emit_nothing);
229 typedef void (*emit_func_ptr) (const ir_node *);
232 * Emits code for a node.
234 static void TEMPLATE_emit_node(const ir_node *node)
236 ir_op *op = get_irn_op(node);
238 if (op->ops.generic) {
239 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
242 ir_fprintf(stderr, "No emitter for node %+F\n", node);
247 * Walks over the nodes in a block connected by scheduling edges
248 * and emits code for each node.
250 static void TEMPLATE_emit_block(ir_node *block)
254 be_gas_emit_block_name(block);
255 be_emit_cstring(":\n");
256 be_emit_write_line();
258 sched_foreach(block, node) {
259 TEMPLATE_emit_node(node);
264 * Sets labels for control flow nodes (jump target)
266 static void TEMPLATE_gen_labels(ir_node *block, void *env)
269 int n = get_Block_n_cfgpreds(block);
272 for (n--; n >= 0; n--) {
273 pred = get_Block_cfgpred(block, n);
274 set_irn_link(pred, block);
281 void TEMPLATE_emit_routine(ir_graph *irg)
283 ir_node **block_schedule;
284 ir_entity *entity = get_irg_entity(irg);
288 /* register all emitter functions */
289 TEMPLATE_register_emitters();
291 /* create the block schedule */
292 block_schedule = be_create_block_schedule(irg);
294 /* emit assembler prolog */
295 be_gas_emit_function_prolog(entity, 4);
297 /* populate jump link fields with their destinations */
298 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
300 n = ARR_LEN(block_schedule);
301 for (i = 0; i < n; ++i) {
302 ir_node *block = block_schedule[i];
303 TEMPLATE_emit_block(block);
305 be_gas_emit_function_epilog(entity);