irverify.c 58.2 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2011 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
/**
 * @file
 * @brief    Check irnodes for correctness.
 * @author   Christian Schaefer, Goetz Lindenmaier, Till Riedel, Michael Beck
24
 */
Matthias Braun's avatar
Matthias Braun committed
25
#include "config.h"
Boris Boesler's avatar
added    
Boris Boesler committed
26

27
28
29
#include "irprog.h"
#include "irop_t.h"
#include "irgraph_t.h"
30
#include "irverify_t.h"
31
32
33
34
35
36
#include "irgwalk.h"
#include "irdump.h"
#include "irdom_t.h"
#include "irprintf.h"
#include "irouts.h"
#include "irflag_t.h"
37
#include "irpass_t.h"
38
#include "irnodeset.h"
39
#include "ircons.h"
Christian Schäfer's avatar
Christian Schäfer committed
40

41
/** if this flag is set, verify entity types in Load & Store nodes */
42
static int verify_entities = 0;
43

44
const char *firm_verify_failure_msg;
45

46
void verify_enable_entity_tests(int enable)
47
{
48
	verify_entities = enable;
49
50
}

51
52
#ifndef NDEBUG

53
54
55
/**
 * little helper for NULL modes
 */
56
57
static const char *get_mode_name_ex(ir_mode *mode)
{
58
59
60
	if (! mode)
		return "<no mode>";
	return get_mode_name(mode);
61
62
}

63
/** the last IRG, on which a verification error was found */
Michael Beck's avatar
Michael Beck committed
64
65
66
67
static ir_graph *last_irg_error = NULL;

/**
 * print the name of the entity of an verification failure
68
69
 *
 * @param node  the node caused the failure
Michael Beck's avatar
Michael Beck committed
70
 */
71
static void show_entity_failure(const ir_node *node)
72
{
73
74
75
76
77
78
79
80
	ir_graph *irg = get_irn_irg(node);

	if (last_irg_error == irg)
		return;

	last_irg_error = irg;

	if (irg == get_const_code_irg()) {
81
		fprintf(stderr, "\nFIRM: irn_verify_irg() <of CONST_CODE_IRG> failed\n");
82
83
84
85
86
87
88
	} else {
		ir_entity *ent = get_irg_entity(irg);

		if (ent) {
			ir_type *ent_type = get_entity_owner(ent);

			if (ent_type) {
89
				ir_fprintf(stderr, "\nFIRM: irn_verify_irg() %+F::%s failed\n",
90
				           ent_type, get_entity_name(ent));
91
			} else {
92
				fprintf(stderr, "\nFIRM: irn_verify_irg() <NULL>::%s failed\n", get_entity_name(ent));
93
94
			}
		} else {
95
			fprintf(stderr, "\nFIRM: irn_verify_irg() <IRG %p> failed\n", (void *)irg);
96
97
		}
	}
Michael Beck's avatar
Michael Beck committed
98
99
}

100
101
102
103
104
105
static const char *get_irn_modename(const ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
	return get_mode_name(mode);
}

106
107
108
/**
 * Prints a failure for a Node
 */
109
static void show_node_failure(const ir_node *n)
110
{
111
112
113
114
115
	show_entity_failure(n);
	fprintf(stderr, "  node %ld %s%s\n" ,
		get_irn_node_nr(n),
		get_irn_opname(n), get_irn_modename(n)
	);
116
117
}

Michael Beck's avatar
Michael Beck committed
118
119
120
/**
 * Prints a failure message for a binop
 */
121
static void show_binop_failure(const ir_node *n, const char *text)
122
{
123
124
	ir_node *left  = get_binop_left(n);
	ir_node *right = get_binop_right(n);
Michael Beck's avatar
Michael Beck committed
125

126
127
128
129
130
131
132
	show_entity_failure(n);
	fprintf(stderr, "  node %ld %s%s(%s%s, %s%s) did not match (%s)\n",
		get_irn_node_nr(n),
		get_irn_opname(n), get_irn_modename(n),
		get_irn_opname(left), get_irn_modename(left),
		get_irn_opname(right), get_irn_modename(right),
		text);
Michael Beck's avatar
Michael Beck committed
133
134
135
136
137
}

/**
 * Prints a failure message for an unop
 */
138
static void show_unop_failure(const ir_node *n, const char *text)
139
{
140
	ir_node *op  = get_unop_op(n);
Michael Beck's avatar
Michael Beck committed
141

142
143
144
145
146
147
	show_entity_failure(n);
	fprintf(stderr, "  node %ld %s%s(%s%s) did not match (%s)\n",
		get_irn_node_nr(n),
		get_irn_opname(n), get_irn_modename(n),
		get_irn_opname(op), get_irn_modename(op),
		text);
Michael Beck's avatar
Michael Beck committed
148
149
}

Michael Beck's avatar
Michael Beck committed
150
151
152
/**
 * Prints a failure message for an op with 3 operands
 */
153
static void show_triop_failure(const ir_node *n, const char *text)
154
{
155
156
157
	ir_node *op0  = get_irn_n(n, 0);
	ir_node *op1  = get_irn_n(n, 1);
	ir_node *op2  = get_irn_n(n, 2);
Michael Beck's avatar
Michael Beck committed
158

159
160
161
162
163
164
165
166
	show_entity_failure(n);
	fprintf(stderr, "  of node %ld %s%s(%s%s, %s%s, %s%s) did not match (%s)\n",
		get_irn_node_nr(n),
		get_irn_opname(n), get_irn_modename(n),
		get_irn_opname(op0), get_irn_modename(op0),
		get_irn_opname(op1), get_irn_modename(op1),
		get_irn_opname(op2), get_irn_modename(op2),
		text);
Michael Beck's avatar
Michael Beck committed
167
168
}

Michael Beck's avatar
Michael Beck committed
169
170
171
/**
 * Prints a failure message for a proj
 */
172
static void show_proj_failure(const ir_node *n)
173
{
174
175
	ir_node *op  = get_Proj_pred(n);
	int proj     = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
176

177
178
179
180
181
	show_entity_failure(n);
	fprintf(stderr, "  node %ld %s%s %d(%s%s) failed\n" ,
		get_irn_node_nr(n),
		get_irn_opname(n), get_irn_modename(n), proj,
		get_irn_opname(op), get_irn_modename(op));
Michael Beck's avatar
Michael Beck committed
182
183
}

Michael Beck's avatar
Michael Beck committed
184
185
186
/**
 * Prints a failure message for a proj from Start
 */
187
static void show_proj_mode_failure(const ir_node *n, ir_type *ty)
188
{
189
190
	long proj  = get_Proj_proj(n);
	ir_mode *m = get_type_mode(ty);
191
192
	char type_name[256];
	ir_print_type(type_name, sizeof(type_name), ty);
Michael Beck's avatar
Michael Beck committed
193

194
195
196
197
198
	show_entity_failure(n);
	fprintf(stderr, "  Proj %ld mode %s proj %ld (type %s mode %s) failed\n" ,
		get_irn_node_nr(n),
		get_irn_modename(n),
		proj,
199
		type_name,
200
		get_mode_name_ex(m));
Michael Beck's avatar
Michael Beck committed
201
202
}

Michael Beck's avatar
Michael Beck committed
203
204
205
/**
 * Prints a failure message for a proj
 */
206
static void show_proj_failure_ent(const ir_node *n, ir_entity *ent)
207
{
208
209
210
	ir_node *op  = get_Proj_pred(n);
	int proj     = get_Proj_proj(n);
	ir_mode *m   = get_type_mode(get_entity_type(ent));
211
212
	char type_name[256];
	ir_print_type(type_name, sizeof(type_name), get_entity_type(ent));
Michael Beck's avatar
Michael Beck committed
213

214
215
216
217
218
	show_entity_failure(n);
	fprintf(stderr, "  node %ld %s%s %d(%s%s) entity %s(type %s mode %s)failed\n" ,
		get_irn_node_nr(n),
		get_irn_opname(n), get_irn_modename(n), proj,
		get_irn_opname(op), get_irn_modename(op),
219
		get_entity_name(ent), type_name,
220
		get_mode_name_ex(m));
Michael Beck's avatar
Michael Beck committed
221
222
}

Michael Beck's avatar
Michael Beck committed
223
224
225
/**
 * Show a node and a graph
 */
226
static void show_node_on_graph(const ir_graph *irg, const ir_node *n)
227
{
228
	ir_fprintf(stderr, "\nFIRM: irn_verify_irg() of %+F, node %+F\n", irg, n);
Michael Beck's avatar
Michael Beck committed
229
230
231
}

/**
232
 * Show call parameters
Michael Beck's avatar
Michael Beck committed
233
 */
234
static void show_call_param(const ir_node *n, ir_type *mt)
235
{
236
	size_t i;
237
238
	char type_name[256];
	ir_print_type(type_name, sizeof(type_name), mt);
Michael Beck's avatar
Michael Beck committed
239

240
	show_entity_failure(n);
241
	fprintf(stderr, "  Call type-check failed: %s(", type_name);
242
243
244
245
	for (i = 0; i < get_method_n_params(mt); ++i) {
		fprintf(stderr, "%s ", get_mode_name_ex(get_type_mode(get_method_param_type(mt, i))));
	}
	fprintf(stderr, ") != CALL(");
Michael Beck's avatar
Michael Beck committed
246

247
248
249
250
	for (i = 0; i < get_Call_n_params(n); ++i) {
		fprintf(stderr, "%s ", get_mode_name_ex(get_irn_mode(get_Call_param(n, i))));
	}
	fprintf(stderr, ")\n");
Michael Beck's avatar
Michael Beck committed
251
}
252

Michael Beck's avatar
Michael Beck committed
253
254
255
/**
 * Show return modes
 */
256
257
static void show_return_modes(const ir_graph *irg, const ir_node *n,
                              ir_type *mt, int i)
258
{
259
	ir_entity *ent = get_irg_entity(irg);
Michael Beck's avatar
Michael Beck committed
260

261
262
263
264
265
266
	show_entity_failure(n);
	fprintf(stderr, "  Return node %ld in entity \"%s\" mode %s different from type mode %s\n",
		get_irn_node_nr(n), get_entity_name(ent),
		get_mode_name_ex(get_irn_mode(get_Return_res(n, i))),
		get_mode_name_ex(get_type_mode(get_method_res_type(mt, i)))
	);
Michael Beck's avatar
Michael Beck committed
267
268
269
270
271
}

/**
 * Show return number of results
 */
272
static void show_return_nres(const ir_graph *irg, const ir_node *n, ir_type *mt)
273
{
274
	ir_entity *ent = get_irg_entity(irg);
Michael Beck's avatar
Michael Beck committed
275

276
	show_entity_failure(n);
277
	fprintf(stderr, "  Return node %ld in entity \"%s\" has %lu results different from type %lu\n",
278
		get_irn_node_nr(n), get_entity_name(ent),
279
280
		(unsigned long) get_Return_n_ress(n),
		(unsigned long) get_method_n_ress(mt));
Michael Beck's avatar
Michael Beck committed
281
282
}

Michael Beck's avatar
Michael Beck committed
283
284
285
/**
 * Show Phi input
 */
286
static void show_phi_failure(const ir_node *phi, const ir_node *pred, int pos)
287
{
288
	(void) pos;
289
290
291
292
	show_entity_failure(phi);
	fprintf(stderr, "  Phi node %ld has mode %s different from predeccessor node %ld mode %s\n",
		get_irn_node_nr(phi), get_mode_name_ex(get_irn_mode(phi)),
		get_irn_node_nr(pred), get_mode_name_ex(get_irn_mode(pred)));
Michael Beck's avatar
Michael Beck committed
293
294
}

Michael Beck's avatar
Michael Beck committed
295
296
297
/**
 * Show Phi inputs
 */
298
static void show_phi_inputs(const ir_node *phi, const ir_node *block)
299
{
300
301
302
303
	show_entity_failure(phi);
	fprintf(stderr, "  Phi node %ld has %d inputs, its Block %ld has %d\n",
		get_irn_node_nr(phi),   get_irn_arity(phi),
		get_irn_node_nr(block), get_irn_arity(block));
Michael Beck's avatar
Michael Beck committed
304
305
}

306
307
#endif /* #ifndef NDEBUG */

Michael Beck's avatar
Michael Beck committed
308
309
310
311
312
/**
 * If the address is Sel or SymConst, return the entity.
 *
 * @param ptr  the node representing the address
 */
313
static ir_entity *get_ptr_entity(const ir_node *ptr)
314
{
315
	if (is_Sel(ptr)) {
316
		return get_Sel_entity(ptr);
Michael Beck's avatar
Michael Beck committed
317
	} else if (is_SymConst_addr_ent(ptr)) {
318
319
320
		return get_SymConst_entity(ptr);
	}
	return NULL;
321
322
}

Michael Beck's avatar
Michael Beck committed
323
/**
Michael Beck's avatar
Michael Beck committed
324
 * verify a Proj(Start) node
Michael Beck's avatar
Michael Beck committed
325
 */
326
static int verify_node_Proj_Start(const ir_node *p)
327
{
328
329
330
331
332
333
	ir_mode *mode = get_irn_mode(p);
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
			(proj == pn_Start_X_initial_exec && mode == mode_X) ||
334
335
336
			(proj == pn_Start_M              && mode == mode_M) ||
			(proj == pn_Start_P_frame_base   && mode_is_reference(mode)) ||
			(proj == pn_Start_T_args         && mode == mode_T)
337
338
339
340
341
		),
		"wrong Proj from Start", 0,
		show_proj_failure(p);
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
342
}
343

Michael Beck's avatar
Michael Beck committed
344
345
346
/**
 * verify a Proj(Cond) node
 */
347
static int verify_node_Proj_Cond(const ir_node *p)
348
{
349
	ir_mode *mode = get_irn_mode(p);
Matthias Braun's avatar
Matthias Braun committed
350
	long     proj = get_Proj_proj(p);
Michael Beck's avatar
Michael Beck committed
351

352
	ASSERT_AND_RET_DBG(
Matthias Braun's avatar
Matthias Braun committed
353
		mode == mode_X && (proj == pn_Cond_false || proj == pn_Cond_true),
354
355
356
357
		"wrong Proj from Cond", 0,
		show_proj_failure(p);
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
358
}
359

Matthias Braun's avatar
Matthias Braun committed
360
361
362
363
364
365
366
367
368
369
370
371
372
static int verify_node_Proj_Switch(const ir_node *p)
{
	ir_mode *mode = get_irn_mode(p);
	long     pn   = get_Proj_proj(p);
	ir_node *pred = get_Proj_pred(p);
	ASSERT_AND_RET_DBG(
		mode == mode_X && (pn >= 0 && pn < (long)get_Switch_n_outs(pred)),
		"wrong Proj from Switch", 0,
		show_proj_failure(p);
	);
	return 1;
}

Michael Beck's avatar
Michael Beck committed
373
374
375
/**
 * verify a Proj(Raise) node
 */
376
static int verify_node_Proj_Raise(const ir_node *p)
377
{
378
379
	ir_mode *mode = get_irn_mode(p);
	long proj     = get_Proj_proj(p);
380

381
382
383
384
385
386
	ASSERT_AND_RET_DBG(
		((proj == pn_Raise_X && mode == mode_X) || (proj == pn_Raise_M && mode == mode_M)),
		"wrong Proj from Raise", 0,
		show_proj_failure(p);
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
387
}
388

Michael Beck's avatar
Michael Beck committed
389
390
391
/**
 * verify a Proj(InstOf) node
 */
392
static int verify_node_Proj_InstOf(const ir_node *p)
393
{
394
395
	ir_mode *mode = get_irn_mode(p);
	long proj     = get_Proj_proj(p);
Michael Beck's avatar
Michael Beck committed
396

397
398
	ASSERT_AND_RET_DBG(
		(
399
			(proj == pn_InstOf_M         && mode == mode_M) ||
400
401
			(proj == pn_InstOf_X_regular && mode == mode_X) ||
			(proj == pn_InstOf_X_except  && mode == mode_X) ||
402
			(proj == pn_InstOf_res       && mode_is_reference(mode))
403
404
405
406
407
		),
		"wrong Proj from InstOf", 0,
		show_proj_failure(p);
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
408
}
409

Michael Beck's avatar
Michael Beck committed
410
411
412
/**
 * verify a Proj(Call) node
 */
413
static int verify_node_Proj_Call(const ir_node *p)
414
{
415
	ir_mode *mode = get_irn_mode(p);
416
	ir_node *n    = get_Proj_pred(p);
417
418
419
420
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
421
			(proj == pn_Call_M                && mode == mode_M) ||
422
423
			(proj == pn_Call_X_regular        && mode == mode_X) ||
			(proj == pn_Call_X_except         && mode == mode_X) ||
424
			(proj == pn_Call_T_result         && mode == mode_T)
425
426
427
428
		),
		"wrong Proj from Call", 0,
		show_proj_failure(p);
	);
Michael Beck's avatar
Michael Beck committed
429
	/* if we have exception flow, we must have a real Memory input */
430
431
	if (proj == pn_Call_X_regular)
		ASSERT_AND_RET(
432
			!is_NoMem(get_Call_mem(n)),
433
434
435
			"Regular Proj from FunctionCall", 0);
	else if (proj == pn_Call_X_except)
		ASSERT_AND_RET(
436
			!is_NoMem(get_Call_mem(n)),
437
438
			"Exception Proj from FunctionCall", 0);
	return 1;
Michael Beck's avatar
Michael Beck committed
439
}
440

Michael Beck's avatar
Michael Beck committed
441
442
443
/**
 * verify a Proj(Div) node
 */
444
static int verify_node_Proj_Div(const ir_node *p)
445
{
446
	ir_mode *mode = get_irn_mode(p);
447
	ir_node *n    = get_Proj_pred(p);
448
449
450
451
452
453
454
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
			(proj == pn_Div_M         && mode == mode_M) ||
			(proj == pn_Div_X_regular && mode == mode_X) ||
			(proj == pn_Div_X_except  && mode == mode_X) ||
Michael Beck's avatar
Michael Beck committed
455
			(proj == pn_Div_res       && mode_is_data(mode) && mode == get_Div_resmode(n))
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
		),
		"wrong Proj from Div", 0,
		show_proj_failure(p);
	);
	if (proj == pn_Div_X_regular)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Regular Proj from unpinned Div", 0);
	else if (proj == pn_Div_X_except)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Exception Proj from unpinned Div", 0);
	else if (proj == pn_Div_M)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Memory Proj from unpinned Div", 0);
	return 1;
Michael Beck's avatar
Michael Beck committed
473
}
Michael Beck's avatar
Michael Beck committed
474

Michael Beck's avatar
Michael Beck committed
475
476
477
/**
 * verify a Proj(Mod) node
 */
478
static int verify_node_Proj_Mod(const ir_node *p)
479
{
480
	ir_mode *mode = get_irn_mode(p);
481
	ir_node *n    = get_Proj_pred(p);
482
483
484
485
486
487
488
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
			(proj == pn_Mod_M         && mode == mode_M) ||
			(proj == pn_Mod_X_regular && mode == mode_X) ||
			(proj == pn_Mod_X_except  && mode == mode_X) ||
489
			(proj == pn_Mod_res       && mode == get_Mod_resmode(n))
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
		),
		"wrong Proj from Mod", 0,
		show_proj_failure(p);
	);
	if (proj == pn_Mod_X_regular)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Regular Proj from unpinned Mod", 0);
	else if (proj == pn_Mod_X_except)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Exception Proj from unpinned Mod", 0);
	else if (proj == pn_Mod_M)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Memory Proj from unpinned Div", 0);
	return 1;
Michael Beck's avatar
Michael Beck committed
507
}
508

Michael Beck's avatar
Michael Beck committed
509
510
511
/**
 * verify a Proj(Load) node
 */
512
static int verify_node_Proj_Load(const ir_node *p)
513
{
514
	ir_mode *mode = get_irn_mode(p);
515
	ir_node *n    = get_Proj_pred(p);
516
517
518
	long proj     = get_Proj_proj(p);

	if (proj == pn_Load_res) {
519
		ir_node   *ptr = get_Load_ptr(n);
520
		ir_entity *ent = get_ptr_entity(ptr);
521
		ir_graph  *irg = get_irn_irg(n);
522

523
		if (verify_entities && ent && get_irg_phase_state(irg) == phase_high) {
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
			/* do NOT check this for lowered phases, see comment on Store */
			ASSERT_AND_RET_DBG(
				(mode == get_type_mode(get_entity_type(ent))),
				"wrong data Proj from Load, entity type_mode failed", 0,
				show_proj_failure_ent(p, ent);
			);
		}
		else {
			ASSERT_AND_RET_DBG(
				mode_is_data(mode) && mode == get_Load_mode(n),
				"wrong data Proj from Load", 0,
				show_proj_failure(p);
			);
		}
	}
	else {
		ASSERT_AND_RET_DBG(
			(
				(proj == pn_Load_M         && mode == mode_M) ||
				(proj == pn_Load_X_regular && mode == mode_X) ||
				(proj == pn_Load_X_except  && mode == mode_X)
			),
			"wrong Proj from Load", 0,
			show_proj_failure(p);
		);
	}
	if (proj == pn_Load_X_regular) {
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Regular Proj from unpinned Load", 0);
	} else if (proj == pn_Load_X_except) {
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Exception Proj from unpinned Load", 0);
	}
	return 1;
Michael Beck's avatar
Michael Beck committed
560
}
561

Michael Beck's avatar
Michael Beck committed
562
563
564
/**
 * verify a Proj(Store) node
 */
565
static int verify_node_Proj_Store(const ir_node *p)
566
{
567
	ir_mode *mode = get_irn_mode(p);
568
	ir_node *n    = get_Proj_pred(p);
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
			(proj == pn_Store_M         && mode == mode_M) ||
			(proj == pn_Store_X_regular && mode == mode_X) ||
			(proj == pn_Store_X_except  && mode == mode_X)
		),
		"wrong Proj from Store", 0,
		show_proj_failure(p);
	);
	if (proj == pn_Store_X_regular) {
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Regular Proj from unpinned Store", 0);
	} else if (proj == pn_Store_X_except) {
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Exception Proj from unpinned Store", 0);
	}
	return 1;
Michael Beck's avatar
Michael Beck committed
590
591
592
593
594
}

/**
 * verify a Proj(Alloc) node
 */
595
static int verify_node_Proj_Alloc(const ir_node *p)
596
{
597
598
	ir_mode *mode = get_irn_mode(p);
	long proj     = get_Proj_proj(p);
Michael Beck's avatar
Michael Beck committed
599

600
601
602
603
604
605
606
607
608
609
610
	ASSERT_AND_RET_DBG(
		(
			(proj == pn_Alloc_M         && mode == mode_M) ||
			(proj == pn_Alloc_X_regular && mode == mode_X) ||
			(proj == pn_Alloc_X_except  && mode == mode_X) ||
			(proj == pn_Alloc_res       && mode_is_reference(mode))
		),
		"wrong Proj from Alloc", 0,
		show_proj_failure(p);
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
611
}
612

Michael Beck's avatar
Michael Beck committed
613
614
615
/**
 * verify a Proj(Proj) node
 */
616
static int verify_node_Proj_Proj(const ir_node *p)
617
{
618
	ir_mode *mode = get_irn_mode(p);
619
	ir_node *pred = get_Proj_pred(p);
620
621
622
623
624
625
626
627
628
629
630
631
632
	long proj     = get_Proj_proj(p);
	long nr       = get_Proj_proj(pred);
	ir_type *mt; /* A method type */

	pred = skip_Id(get_Proj_pred(pred));
	ASSERT_AND_RET((get_irn_mode(pred) == mode_T), "Proj from something not a tuple", 0);

	switch (get_irn_opcode(pred)) {
	case iro_Start:
		mt = get_entity_type(get_irg_entity(get_irn_irg(pred)));

		if (nr == pn_Start_T_args) {
			ASSERT_AND_RET(
633
				(proj >= 0 && mode_is_datab(mode)),
634
635
				"wrong Proj from Proj from Start", 0);
			ASSERT_AND_RET(
636
				(proj < (int)get_method_n_params(mt)),
637
638
639
640
641
				"More Projs for args than args in type", 0
				);
			if ((mode_is_reference(mode)) && is_compound_type(get_method_param_type(mt, proj)))
				/* value argument */ break;

642
			if (get_irg_phase_state(get_irn_irg(pred)) != phase_backend) {
643
				ASSERT_AND_RET_DBG(
644
645
646
647
648
						(mode == get_type_mode(get_method_param_type(mt, proj))),
						"Mode of Proj from Start doesn't match mode of param type.", 0,
						show_proj_mode_failure(p, get_method_param_type(mt, proj));
						);
			}
649
650
651
652
653
654
		}
		break;

	case iro_Call:
		{
			ASSERT_AND_RET(
655
				(proj >= 0 && mode_is_datab(mode)),
656
657
				"wrong Proj from Proj from Call", 0);
			mt = get_Call_type(pred);
658
			ASSERT_AND_RET(is_unknown_type(mt) || is_Method_type(mt),
659
					"wrong call type on call", 0);
660
			ASSERT_AND_RET(
661
				(proj < (int)get_method_n_ress(mt)),
662
663
664
665
666
667
668
669
				"More Projs for results than results in type.", 0);
			if ((mode_is_reference(mode)) && is_compound_type(get_method_res_type(mt, proj)))
				/* value result */ break;

				ASSERT_AND_RET(
				(mode == get_type_mode(get_method_res_type(mt, proj))),
				"Mode of Proj from Call doesn't match mode of result type.", 0);
		}
Christian Würdig's avatar
Christian Würdig committed
670
		break;
671
672
673
674
675
676
677
678
679
680

	case iro_Tuple:
		/* We don't test */
		break;

	default:
		/* ASSERT_AND_RET(0, "Unknown opcode", 0); */
		break;
	}
	return 1;
681
}
682

Michael Beck's avatar
Michael Beck committed
683
684
685
/**
 * verify a Proj(Tuple) node
 */
686
static int verify_node_Proj_Tuple(const ir_node *p)
687
{
688
	(void) p;
689
690
	/* We don't test */
	return 1;
Michael Beck's avatar
Michael Beck committed
691
}
Christian Schäfer's avatar
Christian Schäfer committed
692

Michael Beck's avatar
Michael Beck committed
693
694
695
/**
 * verify a Proj(CopyB) node
 */
696
static int verify_node_Proj_CopyB(const ir_node *p)
697
{
698
	ir_mode *mode = get_irn_mode(p);
699
	ir_node *n    = get_Proj_pred(p);
700
701
702
703
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
704
			(proj == pn_CopyB_M         && mode == mode_M) ||
705
			(proj == pn_CopyB_X_regular && mode == mode_X) ||
706
			(proj == pn_CopyB_X_except  && mode == mode_X)
707
708
709
710
711
712
713
714
715
716
717
718
719
		),
		"wrong Proj from CopyB", 0,
		show_proj_failure(p);
	);
	if (proj == pn_CopyB_X_regular)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Regular Proj from unpinned CopyB", 0);
	else if (proj == pn_CopyB_X_except)
		ASSERT_AND_RET(
			get_irn_pinned(n) == op_pin_state_pinned,
			"Exception Proj from unpinned CopyB", 0);
	return 1;
Michael Beck's avatar
Michael Beck committed
720
721
722
723
724
}

/**
 * verify a Proj(Bound) node
 */
725
static int verify_node_Proj_Bound(const ir_node *p)
726
{
727
	ir_mode *mode = get_irn_mode(p);
728
	ir_node *n    = get_Proj_pred(p);
729
730
731
732
733
734
735
736
737
738
739
740
741
	long proj     = get_Proj_proj(p);

	ASSERT_AND_RET_DBG(
		(
			(proj == pn_Bound_M         && mode == mode_M) ||
			(proj == pn_Bound_X_regular && mode == mode_X) ||
			(proj == pn_Bound_X_except  && mode == mode_X) ||
			(proj == pn_Bound_res       && mode == get_irn_mode(get_Bound_index(n)))
		),
		"wrong Proj from Bound", 0,
		show_proj_failure(p);
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
742
743
}

744
745
746
747
748
749
750
751
752
753
754
static int verify_node_Proj_fragile(const ir_node *node)
{
	ir_node *pred             = get_Proj_pred(node);
	int      throws_exception = ir_throws_exception(pred);
	ASSERT_AND_RET((!is_x_except_Proj(node) || throws_exception)
	    && (!is_x_regular_Proj(node) || throws_exception),
	    "X_except und X_regular Proj only allowed when throws_exception is set",
	    0);
	return 1;
}

Michael Beck's avatar
Michael Beck committed
755
756
757
/**
 * verify a Proj node
 */
758
static int verify_node_Proj(const ir_node *p)
759
{
760
	ir_graph *irg = get_irn_irg(p);
761
762
	ir_node *pred;
	ir_op *op;
763

764
765
	pred = skip_Id(get_Proj_pred(p));
	ASSERT_AND_RET(get_irn_mode(pred) == mode_T, "mode of a 'projed' node is not Tuple", 0);
766
	ASSERT_AND_RET(get_irg_pinned(irg) == op_pin_state_floats || get_nodes_block(pred) == get_nodes_block(p), "Proj must be in same block as its predecessor", 0);
767

768
769
770
771
772
773
	if (is_fragile_op(pred)) {
		int res = verify_node_Proj_fragile(p);
		if (res != 1)
			return res;
	}

774
775
	op = get_irn_op(pred);
	if (op->ops.verify_proj_node)
776
		return op->ops.verify_proj_node(p);
777

778
779
	/* all went ok */
	return 1;
Michael Beck's avatar
Michael Beck committed
780
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
781

Michael Beck's avatar
Michael Beck committed
782
783
784
/**
 * verify a Block node
 */
785
static int verify_node_Block(const ir_node *n)
786
{
787
	ir_graph *irg = get_irn_irg(n);
788
	int i;
Michael Beck's avatar
Michael Beck committed
789

790
	for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
Matthias Braun's avatar
Matthias Braun committed
791
792
		ir_node *pred         = get_Block_cfgpred(n, i);
		ir_node *skipped_pred = skip_Proj(skip_Tuple(pred));
Matthias Braun's avatar
Matthias Braun committed
793
		ASSERT_AND_RET(get_irn_mode(pred) == mode_X,
794
			"Block node must have a mode_X predecessor", 0);
Matthias Braun's avatar
Matthias Braun committed
795
		ASSERT_AND_RET(is_cfop(skipped_pred) || is_Bad(skipped_pred), "Block predecessor must be a cfop (or Bad)", 0);
796
797
	}

798
799
800
801
	if (n == get_irg_start_block(irg)) {
		ASSERT_AND_RET(get_Block_n_cfgpreds(n) == 0, "Start Block node", 0);
	}

802
	if (n == get_irg_end_block(irg) && get_irg_phase_state(irg) != phase_backend) {
803
		/* End block may only have Return, Raise or fragile ops as preds. */
804
805
		for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
			ir_node *pred =  skip_Proj(get_Block_cfgpred(n, i));
806
			if (is_Proj(pred) || is_Tuple(pred))
807
808
809
				break;   /*  We can not test properly.  How many tuples are there? */
			ASSERT_AND_RET(
				(
810
811
812
					is_Return(pred) ||
					is_Bad(pred)    ||
					is_Raise(pred)  ||
813
814
815
816
					is_fragile_op(pred)
				),
				"End Block node", 0);
		}
817
818
819
820
	}
	/*  irg attr must == graph we are in. */
	ASSERT_AND_RET(((get_irn_irg(n) && get_irn_irg(n) == irg)), "Block node has wrong irg attribute", 0);
	return 1;
Michael Beck's avatar
Michael Beck committed
821
}
822

Michael Beck's avatar
Michael Beck committed
823
824
825
/**
 * verify a Start node
 */
826
static int verify_node_Start(const ir_node *n)
827
{
828
	ir_mode *mymode = get_irn_mode(n);
Götz Lindenmaier's avatar
bugfix    
Götz Lindenmaier committed
829

830
831
832
833
834
	ASSERT_AND_RET(
		/* Start: BB --> X x M x ref x data1 x ... x datan x ref */
		mymode == mode_T, "Start node", 0
		);
	return 1;
Michael Beck's avatar
Michael Beck committed
835
}
836

Michael Beck's avatar
Michael Beck committed
837
838
839
/**
 * verify a Jmp node
 */
840
static int verify_node_Jmp(const ir_node *n)
841
{
842
	ir_mode *mymode = get_irn_mode(n);
843

844
845
846
847
848
	ASSERT_AND_RET(
		/* Jmp: BB --> X */
		mymode == mode_X, "Jmp node", 0
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
849
}
850

Michael Beck's avatar
Michael Beck committed
851
852
853
/**
 * verify an IJmp node
 */
854
static int verify_node_IJmp(const ir_node *n)
855
{
856
857
	ir_mode *mymode  = get_irn_mode(n);
	ir_mode *op1mode = get_irn_mode(get_IJmp_target(n));
Michael Beck's avatar
Michael Beck committed
858

859
860
861
862
863
	ASSERT_AND_RET(
		/* IJmp: BB x ref --> X */
		mymode == mode_X && mode_is_reference(op1mode), "IJmp node", 0
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
864
865
}

Michael Beck's avatar
Michael Beck committed
866
867
868
/**
 * verify a Cond node
 */
869
static int verify_node_Cond(const ir_node *n)
870
{
871
872
	ir_mode *mymode  = get_irn_mode(n);
	ir_mode *op1mode = get_irn_mode(get_Cond_selector(n));
Michael Beck's avatar
Michael Beck committed
873

Matthias Braun's avatar
Matthias Braun committed
874
	ASSERT_AND_RET(op1mode == mode_b, "Cond operand not mode_b", 0);
875
	ASSERT_AND_RET(mymode == mode_T, "Cond mode is not a tuple", 0);
Matthias Braun's avatar
Matthias Braun committed
876
877
878
879
880
881
882
	return 1;
}

static int verify_switch_table(const ir_node *n)
{
	const ir_switch_table *table     = get_Switch_table(n);
	unsigned               n_outs    = get_Switch_n_outs(n);
883
884
	ir_node               *selector  = get_Switch_selector(n);
	ir_mode               *mode      = get_irn_mode(selector);
885
	size_t                 n_entries;
Matthias Braun's avatar
Matthias Braun committed
886
887
	size_t                 e;

888
889
890
	ASSERT_AND_RET(table != NULL, "switch table is NULL", 0);

	n_entries = ir_switch_table_get_n_entries(table);
Matthias Braun's avatar
Matthias Braun committed
891
892
893
894
895
896
897
	for (e = 0; e < n_entries; ++e) {
		const ir_switch_table_entry *entry
			= ir_switch_table_get_entry_const(table, e);
		if (entry->pn == 0)
			continue;
		ASSERT_AND_RET(entry->min != NULL && entry->max != NULL,
		               "switch table entry without min+max value", 0);
898
899
900
		ASSERT_AND_RET(get_tarval_mode(entry->min) == mode &&
		               get_tarval_mode(entry->max) == mode,
		               "switch table entry with wrong modes", 0);
Matthias Braun's avatar
Matthias Braun committed
901
902
903
904
905
906
907
		ASSERT_AND_RET(tarval_cmp(entry->min, entry->max) != ir_relation_greater,
		               "switch table entry without min+max value", 0);
		ASSERT_AND_RET(entry->pn >= 0 && entry->pn < (long)n_outs,
					   "switch table entry with invalid proj number", 0);
	}
	return 1;
}
908

Matthias Braun's avatar
Matthias Braun committed
909
910
911
912
913
914
915
916
917
static int verify_node_Switch(const ir_node *n)
{
	ir_mode *mymode  = get_irn_mode(n);
	ir_mode *op1mode = get_irn_mode(get_Switch_selector(n));
	if (!verify_switch_table(n))
		return 0;

	ASSERT_AND_RET(mode_is_int(op1mode), "Switch operand not integer", 0);
	ASSERT_AND_RET(mymode == mode_T, "Switch mode is not a tuple", 0);
918
	return 1;
Michael Beck's avatar
Michael Beck committed
919
920
921
922
923
}

/**
 * verify a Return node
 */
924
static int verify_node_Return(const ir_node *n)
925
{
926
927
928
929
930
	ir_graph *irg      = get_irn_irg(n);
	ir_mode  *mymode   = get_irn_mode(n);
	ir_mode  *mem_mode = get_irn_mode(get_Return_mem(n));
	ir_type  *mt;
	int       i;
931
932
933
934
935
936

	/* Return: BB x M x data1 x ... x datan --> X */

	ASSERT_AND_RET( mem_mode == mode_M, "Return node", 0 );  /* operand M */

	for (i = get_Return_n_ress(n) - 1; i >= 0; --i) {
937
		ASSERT_AND_RET( mode_is_datab(get_irn_mode(get_Return_res(n, i))), "Return node", 0 );  /* operand datai */
938
939
940
941
	}
	ASSERT_AND_RET( mymode == mode_X, "Result X", 0 );   /* result X */
	/* Compare returned results with result types of method type */
	mt = get_entity_type(get_irg_entity(irg));
942
	ASSERT_AND_RET_DBG(get_Return_n_ress(n) == get_method_n_ress(mt),
943
944
945
946
947
		"Number of results for Return doesn't match number of results in type.", 0,
		show_return_nres(irg, n, mt););
	for (i = get_Return_n_ress(n) - 1; i >= 0; --i) {
		ir_type *res_type = get_method_res_type(mt, i);

948
949
950
951
952
953
954
955
956
957
958
959
960
961
		if (get_irg_phase_state(irg) != phase_backend) {
			if (is_atomic_type(res_type)) {
				ASSERT_AND_RET_DBG(
					get_irn_mode(get_Return_res(n, i)) == get_type_mode(res_type),
					"Mode of result for Return doesn't match mode of result type.", 0,
					show_return_modes(irg, n, mt, i);
				);
			} else {
				ASSERT_AND_RET_DBG(
					mode_is_reference(get_irn_mode(get_Return_res(n, i))),
					"Mode of result for Return doesn't match mode of result type.", 0,
					show_return_modes(irg, n, mt, i);
				);
			}
962
963
964
		}
	}
	return 1;
Michael Beck's avatar
Michael Beck committed
965
966
967
968
969
}

/**
 * verify a Raise node
 */
970
static int verify_node_Raise(const ir_node *n)
971
{
972
973
974
	ir_mode *mymode  = get_irn_mode(n);
	ir_mode *op1mode = get_irn_mode(get_Raise_mem(n));
	ir_mode *op2mode = get_irn_mode(get_Raise_exo_ptr(n));
Michael Beck's avatar
Michael Beck committed
975

976
977
978
979
980
981
	ASSERT_AND_RET(
		/* Sel: BB x M x ref --> X x M */
		op1mode == mode_M && mode_is_reference(op2mode) &&
		mymode == mode_T, "Raise node", 0
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
982
983
984
985
986
}

/**
 * verify a Const node
 */
987
static int verify_node_Const(const ir_node *n)
988
{
989
	ir_mode *mymode = get_irn_mode(n);
Michael Beck's avatar
Michael Beck committed
990

991
992
993
994
995
996
997
998
999
1000
1001
1002
	ASSERT_AND_RET(
		/* Const: BB --> data */
		(mode_is_data(mymode) ||
		mymode == mode_b)      /* we want boolean constants for static evaluation */
		,"Const node", 0       /* of Cmp. */
	);
	ASSERT_AND_RET(
		/* the modes of the constant and teh tarval must match */
		mymode == get_tarval_mode(get_Const_tarval(n)),
		"Const node, tarval and node mode mismatch", 0
	);
	return 1;
Michael Beck's avatar
Michael Beck committed
1003
1004
1005
1006
1007
}

/**
 * verify a SymConst node
 */
1008
static int verify_node_SymConst(const ir_node *n)
Matthias Braun's avatar
Matthias Braun committed
1009
{
1010
	ir_mode *mymode = get_irn_mode(n);
Michael Beck's avatar
Michael Beck committed
1011

1012
1013
1014
1015
1016
1017
1018
	ASSERT_AND_RET(
		/* SymConst: BB --> int*/
		(mode_is_int(mymode) ||
		/* SymConst: BB --> ref */
		mode_is_reference(mymode))
		,"SymConst node", 0);
	return 1;
Michael Beck's avatar
Michael Beck committed
1019
}
1020

Michael Beck's avatar
Michael Beck committed
1021
1022
1023
/**
 * verify a Sel node
 */
1024
static int verify_node_Sel(const ir_node *n)
1025
{
1026
1027
1028
1029
1030
1031
1032
1033
1034
	int i;
	ir_mode *mymode  = get_irn_mode(n);
	ir_mode *op1mode = get_irn_mode(get_Sel_mem(n));
	ir_mode *op2mode = get_irn_mode(get_Sel_ptr(n));
	ir_entity *ent;

	ASSERT_AND_RET_DBG(
		/* Sel: BB x M x ref x int^n --> ref */
		(op1mode == mode_M && op2mode == mymode && mode_is_reference(mymode)),
1035
		"Sel node", 0, show_node_failure(n);