TEMPLATE_emitter.c 9.73 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
/* TEMPLATE emitter */
/* $Id$ */

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <limits.h>

#include "xmalloc.h"
#include "tv.h"
#include "iredges.h"
#include "debug.h"
#include "irgwalk.h"
#include "irprintf.h"
#include "irop_t.h"
#include "irargs_t.h"

#include "../besched.h"

#include "TEMPLATE_emitter.h"
#include "gen_TEMPLATE_emitter.h"
#include "TEMPLATE_nodes_attr.h"
#include "TEMPLATE_new_nodes.h"
#include "TEMPLATE_map_regs.h"

#define SNPRINTF_BUF_LEN 128

static const arch_env_t *arch_env = NULL;


/*************************************************************
 *             _       _    __   _          _
 *            (_)     | |  / _| | |        | |
 *  _ __  _ __ _ _ __ | |_| |_  | |__   ___| |_ __   ___ _ __
 * | '_ \| '__| | '_ \| __|  _| | '_ \ / _ \ | '_ \ / _ \ '__|
 * | |_) | |  | | | | | |_| |   | | | |  __/ | |_) |  __/ |
 * | .__/|_|  |_|_| |_|\__|_|   |_| |_|\___|_| .__/ \___|_|
 * | |                                       | |
 * |_|                                       |_|
 *************************************************************/

/**
 * Return a const or symconst as string.
 */
static const char *node_const_to_str(ir_node *n) {
	/* TODO */
}

/**
 * Returns node's offset as string.
 */
static char *node_offset_to_str(ir_node *n) {
	/* TODO */
}

/* We always pass the ir_node which is a pointer. */
static int TEMPLATE_get_arg_type(const lc_arg_occ_t *occ) {
	return lc_arg_type_ptr;
}


/**
 * Returns the register at in position pos.
 */
static const arch_register_t *get_in_reg(ir_node *irn, int pos) {
	ir_node                *op;
	const arch_register_t  *reg = NULL;

	assert(get_irn_arity(irn) > pos && "Invalid IN position");

	/* The out register of the operator at position pos is the
	   in register we need. */
	op = get_irn_n(irn, pos);

	reg = arch_get_irn_register(arch_env, op);

	assert(reg && "no in register found");
	return reg;
}

/**
 * Returns the register at out position pos.
 */
static const arch_register_t *get_out_reg(ir_node *irn, int pos) {
	ir_node                *proj;
	const arch_register_t  *reg = NULL;

	/* 1st case: irn is not of mode_T, so it has only                 */
	/*           one OUT register -> good                             */
	/* 2nd case: irn is of mode_T -> collect all Projs and ask the    */
	/*           Proj with the corresponding projnum for the register */

	if (get_irn_mode(irn) != mode_T) {
		reg = arch_get_irn_register(arch_env, irn);
	}
	else if (is_TEMPLATE_irn(irn)) {
		reg = get_TEMPLATE_out_reg(irn, pos);
	}
	else {
		const ir_edge_t *edge;

		foreach_out_edge(irn, edge) {
			proj = get_edge_src_irn(edge);
			assert(is_Proj(proj) && "non-Proj from mode_T node");
			if (get_Proj_proj(proj) == pos) {
				reg = arch_get_irn_register(arch_env, proj);
				break;
			}
		}
	}

	assert(reg && "no out register found");
	return reg;
}

/**
 * Returns the number of the in register at position pos.
 */
int get_TEMPLATE_reg_nr(ir_node *irn, int pos, int in_out) {
	const arch_register_t *reg;

	if (in_out == 1) {
		reg = get_in_reg(irn, pos);
	}
	else {
		reg = get_out_reg(irn, pos);
	}

	return arch_register_get_index(reg);
}

/**
 * Returns the name of the in register at position pos.
 */
const char *get_TEMPLATE_reg_name(ir_node *irn, int pos, int in_out) {
	const arch_register_t *reg;

	if (in_out == 1) {
		reg = get_in_reg(irn, pos);
	}
	else {
		reg = get_out_reg(irn, pos);
	}

	return arch_register_get_name(reg);
}

/**
 * Get the register name for a node.
 */
static int TEMPLATE_get_reg_name(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
{
	const char *buf;
	ir_node    *X  = arg->v_ptr;
	int         nr = occ->width - 1;

	if (!X)
		return lc_arg_append(app, occ, "(null)", 6);

	if (occ->conversion == 'S') {
		buf = get_TEMPLATE_reg_name(X, nr, 1);
	}
	else { /* 'D' */
		buf = get_TEMPLATE_reg_name(X, nr, 0);
	}

169
	return buf ? lc_arg_append(app, occ, buf, strlen(buf)) : 0;
170
171
172
}

/**
Christian Würdig's avatar
Christian Würdig committed
173
 * Returns the tarval or offset of an TEMPLATE node as a string.
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
 */
static int TEMPLATE_const_to_str(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
{
	const char *buf;
	ir_node    *X = arg->v_ptr;

	if (!X)
		return lc_arg_append(app, occ, "(null)", 6);

	if (occ->conversion == 'C') {
		buf = node_const_to_str(X);
	}
	else { /* 'O' */
		buf = node_offset_to_str(X);
	}

	return lc_arg_append(app, occ, buf, strlen(buf));
}

/**
 * Determines the SSE suffix depending on the mode.
 */
static int TEMPLATE_get_mode_suffix(lc_appendable_t *app,
    const lc_arg_occ_t *occ, const lc_arg_value_t *arg)
{
	ir_node *X = arg->v_ptr;

	if (!X)
		return lc_arg_append(app, occ, "(null)", 6);

	if (get_mode_size_bits(get_irn_mode(X)) == 32)
		return lc_appendable_chadd(app, 's');
	else
		return lc_appendable_chadd(app, 'd');
}

/**
Christian Würdig's avatar
Christian Würdig committed
212
 * Return the TEMPLATE printf arg environment.
213
214
215
216
217
 * We use the firm environment with some additional handlers.
 */
const lc_arg_env_t *TEMPLATE_get_arg_env(void) {
	static lc_arg_env_t *env = NULL;

Christian Würdig's avatar
Christian Würdig committed
218
219
220
	static const lc_arg_handler_t TEMPLATE_reg_handler   = { TEMPLATE_get_arg_type, TEMPLATE_get_reg_name };
	static const lc_arg_handler_t TEMPLATE_const_handler = { TEMPLATE_get_arg_type, TEMPLATE_const_to_str };
	static const lc_arg_handler_t TEMPLATE_mode_handler  = { TEMPLATE_get_arg_type, TEMPLATE_get_mode_suffix };
221
222
223
224
225
226

	if(env == NULL) {
		/* extend the firm printer */
		env = firm_get_arg_env();
			//lc_arg_new_env();

Christian Würdig's avatar
Christian Würdig committed
227
228
229
230
231
		lc_arg_register(env, "TEMPLATE:sreg", 'S', &TEMPLATE_reg_handler);
		lc_arg_register(env, "TEMPLATE:dreg", 'D', &TEMPLATE_reg_handler);
		lc_arg_register(env, "TEMPLATE:cnst", 'C', &TEMPLATE_const_handler);
		lc_arg_register(env, "TEMPLATE:offs", 'O', &TEMPLATE_const_handler);
		lc_arg_register(env, "TEMPLATE:mode", 'M', &TEMPLATE_mode_handler);
232
233
234
235
236
237
238
239
	}

	return env;
}

/*
 * Add a number to a prefix. This number will not be used a second time.
 */
Christian Würdig's avatar
Christian Würdig committed
240
static char *get_unique_label(char *buf, size_t buflen, const char *prefix) {
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	static unsigned long id = 0;
	snprintf(buf, buflen, "%s%lu", prefix, ++id);
	return buf;
}


/**
 * Returns the target label for a control flow node.
 */
static char *get_cfop_target(const ir_node *irn, char *buf) {
	ir_node *bl = get_irn_link(irn);

	snprintf(buf, SNPRINTF_BUF_LEN, "BLOCK_%ld", get_irn_node_nr(bl));
	return buf;
}



/***********************************************************************************
 *                  _          __                                             _
 *                 (_)        / _|                                           | |
 *  _ __ ___   __ _ _ _ __   | |_ _ __ __ _ _ __ ___   _____      _____  _ __| | __
 * | '_ ` _ \ / _` | | '_ \  |  _| '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
 * | | | | | | (_| | | | | | | | | | | (_| | | | | | |  __/\ V  V / (_) | |  |   <
 * |_| |_| |_|\__,_|_|_| |_| |_| |_|  \__,_|_| |_| |_|\___| \_/\_/ \___/|_|  |_|\_\
 *
 ***********************************************************************************/

/**
270
271
 * Enters the emitter functions for handled nodes into the generic
 * pointer of an opcode.
272
 */
273
static void TEMPLATE_register_emitters(void) {
274

275
276
277
278
279
/* some convienience macros to register additional emitter functions
   (other than the generated ones) */
#define TEMPLATE_EMIT(a) op_TEMPLATE_##a->ops.generic = (op_func)emit_TEMPLATE_##a
#define EMIT(a)          op_##a->ops.generic = (op_func)emit_##a
#define BE_EMIT(a)       op_be_##a->ops.generic = (op_func)emit_be_##a
280

281
282
	/* first clear the generic function pointer for all ops */
	clear_irp_opcodes_generic_func();
283

284
285
	/* register all emitter functions defined in spec */
	TEMPLATE_register_spec_emitters();
286

287
	/* register addtional emitter functions if needed */
288

289
290
291
292
#undef TEMPLATE_EMIT
#undef BE_EMIT
#undef EMIT
}
293
294


295
296
297
298
299
300
301
/**
 * Emits code for a node.
 */
void TEMPLATE_emit_node(ir_node *irn, void *env) {
	emit_env_t        *emit_env = env;
	FILE              *F        = emit_env->out;
	ir_op             *op       = get_irn_op(irn);
302
	DEBUG_ONLY(firm_dbg_module_t *mod      = emit_env->mod;)
303

304
	DBG((mod, LEVEL_1, "emitting code for %+F\n", irn));
305

306
307
308
309
310
311
312
	if (op->ops.generic) {
		void (*emit)(const ir_node *, void *) = (void (*)(const ir_node *, void *))op->ops.generic;
		(*emit)(irn, env);
	}
	else {
		ir_fprintf(F, "\t\t\t\t\t/* %+F */\n", irn);
	}
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
}

/**
 * Walks over the nodes in a block connected by scheduling edges
 * and emits code for each node.
 */
void TEMPLATE_gen_block(ir_node *block, void *env) {
	ir_node *irn;

	if (! is_Block(block))
		return;

	fprintf(((emit_env_t *)env)->out, "BLOCK_%ld:\n", get_irn_node_nr(block));
	sched_foreach(block, irn) {
		TEMPLATE_emit_node(irn, env);
	}
}


/**
 * Emits code for function start.
 */
335
void TEMPLATE_emit_func_prolog(FILE *F, ir_graph *irg) {
336
337
338
339
340
341
342
343
	const char *irg_name = get_entity_name(get_irg_entity(irg));

	/* TODO: emit function header */
}

/**
 * Emits code for function end
 */
344
void TEMPLATE_emit_func_epilog(FILE *F, ir_graph *irg) {
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
	const char *irg_name = get_entity_name(get_irg_entity(irg));

	/* TODO: emit function end */
}

/**
 * Sets labels for control flow nodes (jump target)
 * TODO: Jump optimization
 */
void TEMPLATE_gen_labels(ir_node *block, void *env) {
	ir_node *pred;
	int n = get_Block_n_cfgpreds(block);

	for (n--; n >= 0; n--) {
		pred = get_Block_cfgpred(block, n);
		set_irn_link(pred, block);
	}
}

/**
 * Main driver
 */
Christian Würdig's avatar
Christian Würdig committed
367
void TEMPLATE_gen_routine(FILE *F, ir_graph *irg, const TEMPLATE_code_gen_t *cg) {
368
369
370
371
372
	emit_env_t emit_env;

	emit_env.out      = F;
	emit_env.arch_env = cg->arch_env;
	emit_env.cg       = cg;
373
	FIRM_DBG_REGISTER(emit_env.mod, "firm.be.TEMPLATE.emit");
374
375
376
377

	/* set the global arch_env (needed by print hooks) */
	arch_env = cg->arch_env;

378
379
380
381
	/* register all emitter functions */
	TEMPLATE_register_emitters();

	TEMPLATE_emit_func_prolog(F, irg);
382
383
	irg_block_walk_graph(irg, TEMPLATE_gen_labels, NULL, &emit_env);
	irg_walk_blkwise_graph(irg, NULL, TEMPLATE_gen_block, &emit_env);
384
	TEMPLATE_emit_func_epilog(F, irg);
385
}