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"
42 #include "TEMPLATE_emitter.h"
43 #include "gen_TEMPLATE_emitter.h"
44 #include "TEMPLATE_nodes_attr.h"
45 #include "TEMPLATE_new_nodes.h"
47 #define SNPRINTF_BUF_LEN 128
50 * Returns the register at in position pos.
52 static const arch_register_t *get_in_reg(const ir_node *node, int pos)
55 const arch_register_t *reg = NULL;
57 assert(get_irn_arity(node) > pos && "Invalid IN position");
59 /* The out register of the operator at position pos is the
60 in register we need. */
61 op = get_irn_n(node, pos);
63 reg = arch_get_irn_register(op);
65 assert(reg && "no in register found");
70 * Returns the register at out position pos.
72 static const arch_register_t *get_out_reg(const ir_node *node, int pos)
75 const arch_register_t *reg = NULL;
77 /* 1st case: irn is not of mode_T, so it has only */
78 /* one OUT register -> good */
79 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
80 /* Proj with the corresponding projnum for the register */
82 if (get_irn_mode(node) != mode_T) {
83 reg = arch_get_irn_register(node);
84 } else if (is_TEMPLATE_irn(node)) {
85 reg = arch_irn_get_register(node, pos);
87 const ir_edge_t *edge;
89 foreach_out_edge(node, edge) {
90 proj = get_edge_src_irn(edge);
91 assert(is_Proj(proj) && "non-Proj from mode_T node");
92 if (get_Proj_proj(proj) == pos) {
93 reg = arch_get_irn_register(proj);
99 assert(reg && "no out register found");
103 /*************************************************************
105 * (_) | | / _| | | | |
106 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
107 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
108 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
109 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
112 *************************************************************/
114 void TEMPLATE_emit_immediate(const ir_node *node)
120 void TEMPLATE_emit_source_register(const ir_node *node, int pos)
122 const arch_register_t *reg = get_in_reg(node, pos);
123 be_emit_string(arch_register_get_name(reg));
126 void TEMPLATE_emit_dest_register(const ir_node *node, int pos)
128 const arch_register_t *reg = get_out_reg(node, pos);
129 be_emit_string(arch_register_get_name(reg));
133 * Returns the target label for a control flow node.
135 static void TEMPLATE_emit_cfop_target(const ir_node *node)
137 ir_node *block = get_irn_link(node);
138 be_gas_emit_block_name(block);
141 /***********************************************************************************
144 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
145 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
146 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
147 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
149 ***********************************************************************************/
152 * Emits code for a unconditional jump.
154 static void emit_Jmp(const ir_node *node)
156 be_emit_cstring("\tjmp ");
157 TEMPLATE_emit_cfop_target(node);
158 be_emit_finish_line_gas(node);
162 * Enters the emitter functions for handled nodes into the generic
163 * pointer of an opcode.
165 static void TEMPLATE_register_emitters(void)
168 /* some convienience macros to register additional emitter functions
169 (other than the generated ones) */
170 #define TEMPLATE_EMIT(a) op_TEMPLATE_##a->ops.generic = (op_func)emit_TEMPLATE_##a
171 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
172 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
174 /* first clear the generic function pointer for all ops */
175 clear_irp_opcodes_generic_func();
177 /* register all emitter functions defined in spec */
178 TEMPLATE_register_spec_emitters();
180 /* register addtional emitter functions if needed */
188 typedef void (*emit_func_ptr) (const ir_node *);
191 * Emits code for a node.
193 static void TEMPLATE_emit_node(const ir_node *node)
195 ir_op *op = get_irn_op(node);
197 if (op->ops.generic) {
198 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
201 ir_fprintf(stderr, "No emitter for node %+F\n", node);
206 * Walks over the nodes in a block connected by scheduling edges
207 * and emits code for each node.
209 static void TEMPLATE_gen_block(ir_node *block, void *data)
214 if (! is_Block(block))
217 be_gas_emit_block_name(block);
218 be_emit_cstring(":\n");
219 be_emit_write_line();
221 sched_foreach(block, node) {
222 TEMPLATE_emit_node(node);
228 * Emits code for function start.
230 static void TEMPLATE_emit_func_prolog(ir_graph *irg)
232 const char *irg_name = get_entity_name(get_irg_entity(irg));
234 /* TODO: emit function header */
235 be_emit_cstring("/* start of ");
236 be_emit_string(irg_name);
237 be_emit_cstring(" */\n");
238 be_emit_write_line();
242 * Emits code for function end
244 static void TEMPLATE_emit_func_epilog(ir_graph *irg)
246 const char *irg_name = get_entity_name(get_irg_entity(irg));
248 /* TODO: emit function end */
249 be_emit_cstring("/* end of ");
250 be_emit_string(irg_name);
251 be_emit_cstring(" */\n");
252 be_emit_write_line();
256 * Sets labels for control flow nodes (jump target)
258 static void TEMPLATE_gen_labels(ir_node *block, void *env)
261 int n = get_Block_n_cfgpreds(block);
264 for (n--; n >= 0; n--) {
265 pred = get_Block_cfgpred(block, n);
266 set_irn_link(pred, block);
273 void TEMPLATE_gen_routine(const TEMPLATE_code_gen_t *cg, ir_graph *irg)
277 /* register all emitter functions */
278 TEMPLATE_register_emitters();
280 TEMPLATE_emit_func_prolog(irg);
281 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, NULL);
282 irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, NULL);
283 TEMPLATE_emit_func_epilog(irg);