2 * Copyright (C) 1995-2007 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 "../besched.h"
43 #include "TEMPLATE_emitter.h"
44 #include "gen_TEMPLATE_emitter.h"
45 #include "TEMPLATE_nodes_attr.h"
46 #include "TEMPLATE_new_nodes.h"
47 #include "TEMPLATE_map_regs.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 arch_env_t *arch_env,
55 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(arch_env, 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 arch_env_t *arch_env,
76 const ir_node *node, int pos)
79 const arch_register_t *reg = NULL;
81 /* 1st case: irn is not of mode_T, so it has only */
82 /* one OUT register -> good */
83 /* 2nd case: irn is of mode_T -> collect all Projs and ask the */
84 /* Proj with the corresponding projnum for the register */
86 if (get_irn_mode(node) != mode_T) {
87 reg = arch_get_irn_register(arch_env, node);
88 } else if (is_TEMPLATE_irn(node)) {
89 reg = get_TEMPLATE_out_reg(node, pos);
91 const ir_edge_t *edge;
93 foreach_out_edge(node, edge) {
94 proj = get_edge_src_irn(edge);
95 assert(is_Proj(proj) && "non-Proj from mode_T node");
96 if (get_Proj_proj(proj) == pos) {
97 reg = arch_get_irn_register(arch_env, proj);
103 assert(reg && "no out register found");
107 /*************************************************************
109 * (_) | | / _| | | | |
110 * _ __ _ __ _ _ __ | |_| |_ | |__ ___| |_ __ ___ _ __
111 * | '_ \| '__| | '_ \| __| _| | '_ \ / _ \ | '_ \ / _ \ '__|
112 * | |_) | | | | | | | |_| | | | | | __/ | |_) | __/ |
113 * | .__/|_| |_|_| |_|\__|_| |_| |_|\___|_| .__/ \___|_|
116 *************************************************************/
118 void TEMPLATE_emit_immediate(TEMPLATE_emit_env_t *env, const ir_node *node)
125 void TEMPLATE_emit_source_register(TEMPLATE_emit_env_t *env, const ir_node *node, int pos)
127 const arch_register_t *reg = get_in_reg(env->arch_env, node, pos);
128 be_emit_string(env->emit, arch_register_get_name(reg));
131 void TEMPLATE_emit_dest_register(TEMPLATE_emit_env_t *env, const ir_node *node, int pos)
133 const arch_register_t *reg = get_out_reg(env->arch_env, node, pos);
134 be_emit_string(env->emit, arch_register_get_name(reg));
138 * Returns the target label for a control flow node.
140 static void TEMPLATE_emit_cfop_target(TEMPLATE_emit_env_t *env, const ir_node *node) {
141 ir_node *block = get_irn_link(node);
143 be_emit_irprintf(env->emit, "BLOCK_%ld", get_irn_node_nr(block));
146 /***********************************************************************************
149 * _ __ ___ __ _ _ _ __ | |_ _ __ __ _ _ __ ___ _____ _____ _ __| | __
150 * | '_ ` _ \ / _` | | '_ \ | _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
151 * | | | | | | (_| | | | | | | | | | | (_| | | | | | | __/\ V V / (_) | | | <
152 * |_| |_| |_|\__,_|_|_| |_| |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
154 ***********************************************************************************/
157 * Emits code for a unconditional jump.
159 static void emit_Jmp(TEMPLATE_emit_env_t *env, const ir_node *node) {
162 /* for now, the code works for scheduled and non-schedules blocks */
163 block = get_nodes_block(node);
165 be_emit_cstring(env->emit, "\tjmp ");
166 TEMPLATE_emit_cfop_target(env, node);
167 be_emit_finish_line_gas(env->emit, node);
171 * Enters the emitter functions for handled nodes into the generic
172 * pointer of an opcode.
174 static void TEMPLATE_register_emitters(void) {
176 /* some convienience macros to register additional emitter functions
177 (other than the generated ones) */
178 #define TEMPLATE_EMIT(a) op_TEMPLATE_##a->ops.generic = (op_func)emit_TEMPLATE_##a
179 #define EMIT(a) op_##a->ops.generic = (op_func)emit_##a
180 #define BE_EMIT(a) op_be_##a->ops.generic = (op_func)emit_be_##a
182 /* first clear the generic function pointer for all ops */
183 clear_irp_opcodes_generic_func();
185 /* register all emitter functions defined in spec */
186 TEMPLATE_register_spec_emitters();
188 /* register addtional emitter functions if needed */
196 typedef void (*emit_func_ptr) (TEMPLATE_emit_env_t *, const ir_node *);
199 * Emits code for a node.
201 void TEMPLATE_emit_node(TEMPLATE_emit_env_t *env, const ir_node *node) {
202 ir_op *op = get_irn_op(node);
204 if (op->ops.generic) {
205 emit_func_ptr func = (emit_func_ptr) op->ops.generic;
208 ir_fprintf(stderr, "No emitter for node %+F\n", node);
213 * Walks over the nodes in a block connected by scheduling edges
214 * and emits code for each node.
216 void TEMPLATE_gen_block(ir_node *block, void *data) {
217 TEMPLATE_emit_env_t *env = data;
220 if (! is_Block(block))
223 be_emit_cstring(env->emit, "BLOCK_");
224 be_emit_irprintf(env->emit, "%ld:\n", get_irn_node_nr(block));
225 be_emit_write_line(env->emit);
227 sched_foreach(block, node) {
228 TEMPLATE_emit_node(env, node);
234 * Emits code for function start.
236 void TEMPLATE_emit_func_prolog(TEMPLATE_emit_env_t *env, ir_graph *irg) {
237 const char *irg_name = get_entity_name(get_irg_entity(irg));
239 /* TODO: emit function header */
240 be_emit_cstring(env->emit, "/* start of ");
241 be_emit_string(env->emit, irg_name);
242 be_emit_cstring(env->emit, " */\n");
243 be_emit_write_line(env->emit);
247 * Emits code for function end
249 void TEMPLATE_emit_func_epilog(TEMPLATE_emit_env_t *env, ir_graph *irg) {
250 const char *irg_name = get_entity_name(get_irg_entity(irg));
252 /* TODO: emit function end */
253 be_emit_cstring(env->emit, "/* end of ");
254 be_emit_string(env->emit, irg_name);
255 be_emit_cstring(env->emit, " */\n");
256 be_emit_write_line(env->emit);
260 * Sets labels for control flow nodes (jump target)
261 * TODO: Jump optimization
263 void TEMPLATE_gen_labels(ir_node *block, void *env) {
265 int n = get_Block_n_cfgpreds(block);
268 for (n--; n >= 0; n--) {
269 pred = get_Block_cfgpred(block, n);
270 set_irn_link(pred, block);
277 void TEMPLATE_gen_routine(const TEMPLATE_code_gen_t *cg, ir_graph *irg) {
278 TEMPLATE_emit_env_t env;
280 env.isa = (TEMPLATE_isa_t *) cg->arch_env->isa;
281 env.emit = &env.isa->emit;
282 env.arch_env = cg->arch_env;
285 /* register all emitter functions */
286 TEMPLATE_register_emitters();
288 TEMPLATE_emit_func_prolog(&env, irg);
289 irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, &env);
290 irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, &env);
291 TEMPLATE_emit_func_epilog(&env, irg);