TEMPLATE_transform.c 9.54 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
24
/**
 * @file
 * @brief   code selection (transform FIRM into TEMPLATE FIRM)
 * @version $Id$
 */
25
#include "config.h"
Christian Würdig's avatar
Christian Würdig committed
26
27
28
29
30
31
32
33
34
35
36

#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "irgmod.h"
#include "iredges.h"
#include "irvrfy.h"
#include "ircons.h"
#include "iropt_t.h"
#include "debug.h"

37
#include "../benode.h"
Christian Würdig's avatar
Christian Würdig committed
38
39
40
41
42
43
44
45
#include "bearch_TEMPLATE_t.h"

#include "TEMPLATE_nodes_attr.h"
#include "TEMPLATE_transform.h"
#include "TEMPLATE_new_nodes.h"

#include "gen_TEMPLATE_regalloc_if.h"

46
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
Christian Würdig's avatar
Christian Würdig committed
47
48
49
50
51
52
53
54
55

/**
 * Creates an TEMPLATE Add.
 *
 * @param env   The transformation environment
 * @param op1   first operator
 * @param op2   second operator
 * @return the created TEMPLATE Add node
 */
56
57
static ir_node *gen_Add(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
58
	return new_bd_TEMPLATE_Add(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
}



/**
 * Creates an TEMPLATE Mul.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE Mul node
 */
73
74
static ir_node *gen_Mul(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
Christian Würdig's avatar
Christian Würdig committed
75
	if (mode_is_float(env->mode)) {
76
		return new_bd_TEMPLATE_fMul(env->dbg, env->block, op1, op2, env->mode);
77
	} else {
78
		return new_bd_TEMPLATE_Mul(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	}
}



/**
 * Creates an TEMPLATE And.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE And node
 */
94
95
static ir_node *gen_And(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
96
	return new_bd_TEMPLATE_And(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
}



/**
 * Creates an TEMPLATE Or.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE Or node
 */
111
112
static ir_node *gen_Or(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
113
	return new_bd_TEMPLATE_Or(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
114
115
116
117
118
119
120
121
122
123
124
125
126
127
}



/**
 * Creates an TEMPLATE Eor.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE Eor node
 */
128
129
static ir_node *gen_Eor(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
130
	return new_bd_TEMPLATE_Eor(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
}



/**
 * Creates an TEMPLATE Sub.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE Sub node
 */
145
146
static ir_node *gen_Sub(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
Christian Würdig's avatar
Christian Würdig committed
147
	if (mode_is_float(env->mode)) {
148
		return new_bd_TEMPLATE_fSub(env->dbg, env->block, op1, op2, env->mode);
149
	} else {
150
		return new_bd_TEMPLATE_Sub(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
	}
}



/**
 * Creates an TEMPLATE floating Div.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE fDiv node
 */
166
167
static ir_node *gen_Quot(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
168
	return new_bd_TEMPLATE_fDiv(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
}



/**
 * Creates an TEMPLATE Shl.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE Shl node
 */
183
184
static ir_node *gen_Shl(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
185
	return new_bd_TEMPLATE_Shl(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}



/**
 * Creates an TEMPLATE Shr.
 *
 * @param dbg       firm node dbg
 * @param block     the block the new node should belong to
 * @param op1       first operator
 * @param op2       second operator
 * @param mode      node mode
 * @return the created TEMPLATE Shr node
 */
200
201
static ir_node *gen_Shr(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2)
{
202
	return new_bd_TEMPLATE_Shr(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
}



/**
 * Transforms a Minus node.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Minus node
 * @param op      operator
 * @param mode    node mode
 * @return the created TEMPLATE Minus node
 */
217
218
static ir_node *gen_Minus(TEMPLATE_transform_env_t *env, ir_node *op)
{
Christian Würdig's avatar
Christian Würdig committed
219
	if (mode_is_float(env->mode)) {
220
		return new_bd_TEMPLATE_fMinus(env->dbg, env->block, op, env->mode);
Christian Würdig's avatar
Christian Würdig committed
221
	}
222
	return new_bd_TEMPLATE_Minus(env->dbg, env->block, op, env->mode);
Christian Würdig's avatar
Christian Würdig committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
}



/**
 * Transforms a Not node.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Not node
 * @param op      operator
 * @param mode    node mode
 * @return the created TEMPLATE Not node
 */
237
238
static ir_node *gen_Not(TEMPLATE_transform_env_t *env, ir_node *op)
{
239
	return new_bd_TEMPLATE_Not(env->dbg, env->block, op, env->mode);
Christian Würdig's avatar
Christian Würdig committed
240
241
242
243
244
245
246
247
248
249
250
251
252
}



/**
 * Transforms a Load.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Load node
 * @param mode    node mode
 * @return the created TEMPLATE Load node
 */
253
254
static ir_node *gen_Load(TEMPLATE_transform_env_t *env)
{
Christian Würdig's avatar
Christian Würdig committed
255
256
257
	ir_node *node = env->irn;

	if (mode_is_float(env->mode)) {
258
		return new_bd_TEMPLATE_fLoad(env->dbg, env->block, get_Load_ptr(node), get_Load_mem(node), env->mode);
Christian Würdig's avatar
Christian Würdig committed
259
	}
260
	return new_bd_TEMPLATE_Load(env->dbg, env->block, get_Load_ptr(node), get_Load_mem(node), env->mode);
Christian Würdig's avatar
Christian Würdig committed
261
262
263
264
265
266
267
268
269
270
271
272
273
}



/**
 * Transforms a Store.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Store node
 * @param mode    node mode
 * @return the created TEMPLATE Store node
 */
274
275
static ir_node *gen_Store(TEMPLATE_transform_env_t *env)
{
Christian Würdig's avatar
Christian Würdig committed
276
277
278
	ir_node *node = env->irn;

	if (mode_is_float(env->mode)) {
279
		return new_bd_TEMPLATE_fStore(env->dbg, env->block, get_Store_ptr(node), get_Store_value(node), get_Store_mem(node), env->mode);
Christian Würdig's avatar
Christian Würdig committed
280
	}
281
	return new_bd_TEMPLATE_Store(env->dbg, env->block, get_Store_ptr(node), get_Store_value(node), get_Store_mem(node), env->mode);
Christian Würdig's avatar
Christian Würdig committed
282
283
}

284
285
286
287
static ir_node *gen_Jmp(TEMPLATE_transform_env_t *env)
{
	return new_bd_TEMPLATE_Jmp(env->dbg, env->block);
}
Christian Würdig's avatar
Christian Würdig committed
288
289
290
291
292
293
294
295
296
297
298




/**
 * Transforms the given firm node (and maybe some other related nodes)
 * into one or more assembler nodes.
 *
 * @param node    the firm node
 * @param env     the debug module
 */
299
300
void TEMPLATE_transform_node(ir_node *node, void *env)
{
Michael Beck's avatar
Michael Beck committed
301
	ir_opcode code             = get_irn_opcode(node);
Christian Würdig's avatar
Christian Würdig committed
302
303
	ir_node *asm_node          = NULL;
	TEMPLATE_transform_env_t tenv;
Matthias Braun's avatar
Matthias Braun committed
304
	(void) env;
Christian Würdig's avatar
Christian Würdig committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320

	if (is_Block(node))
		return;

	tenv.block    = get_nodes_block(node);
	tenv.dbg      = get_irn_dbg_info(node);
	tenv.irg      = current_ir_graph;
	tenv.irn      = node;
	tenv.mode     = get_irn_mode(node);

#define UNOP(a)        case iro_##a: asm_node = gen_##a(&tenv, get_##a##_op(node)); break
#define BINOP(a)       case iro_##a: asm_node = gen_##a(&tenv, get_##a##_left(node), get_##a##_right(node)); break
#define GEN(a)         case iro_##a: asm_node = gen_##a(&tenv); break
#define IGN(a)         case iro_##a: break
#define BAD(a)         case iro_##a: goto bad

321
	DBG((dbg, LEVEL_1, "check %+F ... ", node));
Christian Würdig's avatar
Christian Würdig committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

	switch (code) {
		BINOP(Add);
		BINOP(Mul);
		BINOP(And);
		BINOP(Or);
		BINOP(Eor);

		BINOP(Sub);
		BINOP(Shl);
		BINOP(Shr);
		BINOP(Quot);


		UNOP(Minus);
		UNOP(Not);

		GEN(Load);
		GEN(Store);
341
		GEN(Jmp);
Christian Würdig's avatar
Christian Würdig committed
342
343
344
345
346
347
348
349
350
351
352
353

		/* TODO: implement these nodes */
		IGN(Shrs);
		IGN(Div);
		IGN(Mod);
		IGN(DivMod);
		IGN(Const);
		IGN(SymConst);
		IGN(Conv);
		IGN(Abs);
		IGN(Cond);
		IGN(Mux);
354
		IGN(Mulh);
Christian Würdig's avatar
Christian Würdig committed
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
		IGN(CopyB);
		IGN(Unknown);
		IGN(Cmp);

		/* You probably don't need to handle the following nodes */

		IGN(Call);
		IGN(Proj);
		IGN(Alloc);

		IGN(Block);
		IGN(Start);
		IGN(End);
		IGN(NoMem);
		IGN(Phi);
		IGN(IJmp);
		IGN(Break);
		IGN(Sync);

		BAD(Raise);
		BAD(Sel);
		BAD(InstOf);
		BAD(Cast);
		BAD(Free);
		BAD(Tuple);
		BAD(Id);
		BAD(Bad);
		BAD(Confirm);
		BAD(Filter);
		BAD(CallBegin);
		BAD(EndReg);
		BAD(EndExcept);

		default:
			break;
bad:
		fprintf(stderr, "Not implemented: %s\n", get_irn_opname(node));
		assert(0);
	}

	if (asm_node) {
		exchange(node, asm_node);
397
		DB((dbg, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
398
	} else {
399
		DB((dbg, LEVEL_1, "ignored\n"));
Christian Würdig's avatar
Christian Würdig committed
400
401
	}
}
402
403
404
405
406

void TEMPLATE_init_transform(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.TEMPLATE.transform");
}