TEMPLATE_transform.c 10.7 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
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

#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"

#include "../benode_t.h"
#include "bearch_TEMPLATE_t.h"

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

#include "gen_TEMPLATE_regalloc_if.h"

extern ir_op *get_op_Mulh(void);



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

/**
 * Creates an TEMPLATE Add.
 *
 * @param env   The transformation environment
 * @param op1   first operator
 * @param op2   second operator
 * @return the created TEMPLATE Add node
 */
static ir_node *gen_Add(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
70
	return new_bd_TEMPLATE_Add(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
}



/**
 * 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
 */
static ir_node *gen_Mul(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
	if (mode_is_float(env->mode)) {
87
		return new_bd_TEMPLATE_fMul(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
88
89
	}
	else {
90
		return new_bd_TEMPLATE_Mul(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	}
}



/**
 * 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
 */
static ir_node *gen_And(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
107
	return new_bd_TEMPLATE_And(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
}



/**
 * 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
 */
static ir_node *gen_Or(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
123
	return new_bd_TEMPLATE_Or(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
}



/**
 * 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
 */
static ir_node *gen_Eor(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
139
	return new_bd_TEMPLATE_Eor(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
}



/**
 * 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
 */
static ir_node *gen_Sub(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
	if (mode_is_float(env->mode)) {
156
		return new_bd_TEMPLATE_fSub(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
157
158
	}
	else {
159
		return new_bd_TEMPLATE_Sub(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
	}
}



/**
 * 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
 */
static ir_node *gen_Quot(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
176
	return new_bd_TEMPLATE_fDiv(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
}



/**
 * 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
 */
static ir_node *gen_Shl(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
192
	return new_bd_TEMPLATE_Shl(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
}



/**
 * 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
 */
static ir_node *gen_Shr(TEMPLATE_transform_env_t *env, ir_node *op1, ir_node *op2) {
208
	return new_bd_TEMPLATE_Shr(env->dbg, env->block, op1, op2, env->mode);
Christian Würdig's avatar
Christian Würdig committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
}



/**
 * 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
 */
static ir_node *gen_Minus(TEMPLATE_transform_env_t *env, ir_node *op) {
	if (mode_is_float(env->mode)) {
225
		return new_bd_TEMPLATE_fMinus(env->dbg, env->block, op, env->mode);
Christian Würdig's avatar
Christian Würdig committed
226
	}
227
	return new_bd_TEMPLATE_Minus(env->dbg, env->block, op, env->mode);
Christian Würdig's avatar
Christian Würdig committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
}



/**
 * 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
 */
static ir_node *gen_Not(TEMPLATE_transform_env_t *env, ir_node *op) {
243
	return new_bd_TEMPLATE_Not(env->dbg, env->block, op, env->mode);
Christian Würdig's avatar
Christian Würdig committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
}



/**
 * 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
 */
static ir_node *gen_Load(TEMPLATE_transform_env_t *env) {
	ir_node *node = env->irn;

	if (mode_is_float(env->mode)) {
261
		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
262
	}
263
	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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
}



/**
 * 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
 */
static ir_node *gen_Store(TEMPLATE_transform_env_t *env) {
	ir_node *node = env->irn;

	if (mode_is_float(env->mode)) {
281
		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
282
	}
283
	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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
}



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



/**
 * 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
 */
void TEMPLATE_transform_node(ir_node *node, void *env) {
Matthias Braun's avatar
Matthias Braun committed
308
#ifdef DEBUG_libfirm
Christian Würdig's avatar
Christian Würdig committed
309
	TEMPLATE_code_gen_t *cgenv = (TEMPLATE_code_gen_t *)env;
Matthias Braun's avatar
Matthias Braun committed
310
#endif
Michael Beck's avatar
Michael Beck committed
311
	ir_opcode code             = get_irn_opcode(node);
Christian Würdig's avatar
Christian Würdig committed
312
313
	ir_node *asm_node          = NULL;
	TEMPLATE_transform_env_t tenv;
Matthias Braun's avatar
Matthias Braun committed
314
	(void) env;
Christian Würdig's avatar
Christian Würdig committed
315
316
317
318
319
320
321
322

	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;
323
#ifdef DEBUG_libfirm
Christian Würdig's avatar
Christian Würdig committed
324
	tenv.mod      = cgenv->mod;
325
#endif
Christian Würdig's avatar
Christian Würdig committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
	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

	DBG((tenv.mod, LEVEL_1, "check %+F ... ", node));

	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);

		/* TODO: implement these nodes */
		IGN(Shrs);
		IGN(Div);
		IGN(Mod);
		IGN(DivMod);
		IGN(Const);
		IGN(SymConst);
		IGN(Conv);
		IGN(Abs);
		IGN(Cond);
		IGN(Mux);
366
		IGN(Mulh);
Christian Würdig's avatar
Christian Würdig committed
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
		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(Jmp);
		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);
		DB((tenv.mod, LEVEL_1, "created node %+F[%p]\n", asm_node, asm_node));
	}
	else {
		DB((tenv.mod, LEVEL_1, "ignored\n"));
	}
}