irio.c 35.6 KB
Newer Older
1
2
3
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
/*
 * Copyright (C) 1995-2009 University of Karlsruhe.  All right reserved.
 *
 * 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.
 */

/**
 * @file
 * @brief   Write textual representation of firm to file.
 * @author  Moritz Kroll
 * @version $Id$
 */
#include "config.h"

#include <string.h>

#include "irio.h"

32
#include "irnode_t.h"
33
34
35
36
37
38
39
40
41
42
43
#include "irprog.h"
#include "irgraph_t.h"
#include "ircons.h"
#include "irgmod.h"
#include "irflag_t.h"
#include "irgwalk.h"
#include "tv.h"
#include "array.h"
#include "error.h"
#include "adt/set.h"

44
#define SYMERROR ((unsigned) ~0)
45

Moritz Kroll's avatar
Moritz Kroll committed
46
typedef struct io_env_t
47
48
49
50
51
52
53
54
{
	FILE *file;
	set *idset;               /**< id_entry set, which maps from file ids to new Firm elements */
	int ignoreblocks;
	int line, col;
	ir_type **fixedtypes;
} io_env_t;

Moritz Kroll's avatar
Moritz Kroll committed
55
56
57
58
59
60
typedef struct lex_state_t
{
	long offs;
	int line, col;
} lex_state_t;

61
62
63
64
typedef enum typetag_t
{
	tt_align,
	tt_allocation,
65
	tt_builtin,
66
67
	tt_cond_kind,
	tt_cond_jmp_predicate,
68
69
	tt_initializer,
	tt_iro,
70
	tt_keyword,
71
72
	tt_peculiarity,
	tt_pin_state,
73
	tt_tpo,
74
75
76
	tt_type_state,
	tt_variability,
	tt_visibility,
77
	tt_volatility
78
79
} typetag_t;

80
81
82
83
84
85
86
87
88
89
90
typedef enum keyword_t
{
	kw_constirg,
	kw_entity,
	kw_frametype,
	kw_irg,
	kw_valuetype,
	kw_type,
	kw_typegraph
} keyword_t;

91
typedef struct symbol_t
92
{
93
94
95
96
	const char *str;      /**< The name of this symbol. */
	typetag_t   typetag;  /**< The type tag of this symbol. */
	unsigned    code;     /**< The value of this symbol. */
} symbol_t;
97
98
99
100
101
102
103

typedef struct id_entry
{
	long id;
	void *elem;
} id_entry;

104
105
/** The symbol table, a set of symbol_t elements. */
static set *symtbl;
106
107


108
109
110
111
/**
 * Calculate a hash value for a string.
 */
static unsigned string_hash(const char *str, int len)
112
113
114
115
{
	return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
}

116
117
118
119
/**
 * Compare two symbol table entries.
 */
static int symbol_cmp(const void *elt, const void *key, size_t size)
120
{
121
	int res;
122
123
	const symbol_t *entry = (const symbol_t *) elt;
	const symbol_t *keyentry = (const symbol_t *) key;
124
	(void) size;
125
126
	res = entry->typetag - keyentry->typetag;
	if(res) return res;
127
128
129
130
131
132
133
134
135
136
137
	return strcmp(entry->str, keyentry->str);
}

static int id_cmp(const void *elt, const void *key, size_t size)
{
	const id_entry *entry = (const id_entry *) elt;
	const id_entry *keyentry = (const id_entry *) key;
	(void) size;
	return entry->id - keyentry->id;
}

138
139
/** Initializes the symbol table. May be called more than once without problems. */
static void symtbl_init(void)
140
{
141
	symbol_t key;
142
143

	/* Only initialize once */
144
145
	if(symtbl != NULL)
		return;
146

147
	symtbl = new_set(symbol_cmp, 256);
148
149
150
151
152

#define INSERT(s, tt, cod)                                       \
	key.str = (s);                                               \
	key.typetag = (tt);                                          \
	key.code = (cod);                                            \
153
	set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1) + tt * 17)
154
155

#define INSERTENUM(tt, e) INSERT(#e, tt, e)
156
#define INSERTKEYWORD(k) INSERT(#k, tt_keyword, kw_##k)
157
158

	INSERT("array", tt_tpo, tpo_array);
159
160
161
162
	INSERT("class", tt_tpo, tpo_class);
	INSERT("method", tt_tpo, tpo_method);
	INSERT("pointer", tt_tpo, tpo_pointer);
	INSERT("primitive", tt_tpo, tpo_primitive);
163
	INSERT("struct", tt_tpo, tpo_struct);
164
	INSERT("union", tt_tpo, tpo_union);
165
166
	INSERT("Unknown", tt_tpo, tpo_unknown);

167
168
169
170
171
172
173
174
	INSERTKEYWORD(constirg);
	INSERTKEYWORD(entity);
	INSERTKEYWORD(frametype);
	INSERTKEYWORD(irg);
	INSERTKEYWORD(valuetype);
	INSERTKEYWORD(type);
	INSERTKEYWORD(typegraph);

175
176
177
178
179
180
181
182
183
184
#include "gen_irio_lex.inl"

	INSERTENUM(tt_align, align_non_aligned);
	INSERTENUM(tt_align, align_is_aligned);

	INSERTENUM(tt_allocation, allocation_automatic);
	INSERTENUM(tt_allocation, allocation_parameter);
	INSERTENUM(tt_allocation, allocation_dynamic);
	INSERTENUM(tt_allocation, allocation_static);

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
212
213
214
215
	INSERTENUM(tt_builtin, ir_bk_trap);
	INSERTENUM(tt_builtin, ir_bk_debugbreak);
	INSERTENUM(tt_builtin, ir_bk_return_address);
	INSERTENUM(tt_builtin, ir_bk_frame_addess);
	INSERTENUM(tt_builtin, ir_bk_prefetch);
	INSERTENUM(tt_builtin, ir_bk_ffs);
	INSERTENUM(tt_builtin, ir_bk_clz);
	INSERTENUM(tt_builtin, ir_bk_ctz);
	INSERTENUM(tt_builtin, ir_bk_popcount);
	INSERTENUM(tt_builtin, ir_bk_parity);
	INSERTENUM(tt_builtin, ir_bk_bswap);
	INSERTENUM(tt_builtin, ir_bk_inport);
	INSERTENUM(tt_builtin, ir_bk_outport);
	INSERTENUM(tt_builtin, ir_bk_inner_trampoline);

	INSERTENUM(tt_cond_kind, dense);
	INSERTENUM(tt_cond_kind, fragmentary);

	INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_NONE);
	INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_TRUE);
	INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_FALSE);

	INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
	INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
	INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
	INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);

	INSERTENUM(tt_peculiarity, peculiarity_description);
	INSERTENUM(tt_peculiarity, peculiarity_inherited);
	INSERTENUM(tt_peculiarity, peculiarity_existent);

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	INSERTENUM(tt_pin_state, op_pin_state_floats);
	INSERTENUM(tt_pin_state, op_pin_state_pinned);
	INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
	INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);

	INSERTENUM(tt_type_state, layout_undefined);
	INSERTENUM(tt_type_state, layout_fixed);

	INSERTENUM(tt_variability, variability_uninitialized);
	INSERTENUM(tt_variability, variability_initialized);
	INSERTENUM(tt_variability, variability_part_constant);
	INSERTENUM(tt_variability, variability_constant);

	INSERTENUM(tt_visibility, visibility_local);
	INSERTENUM(tt_visibility, visibility_external_visible);
	INSERTENUM(tt_visibility, visibility_external_allocated);

	INSERTENUM(tt_volatility, volatility_non_volatile);
	INSERTENUM(tt_volatility, volatility_is_volatile);

236

237
238
239
240
#undef INSERTENUM
#undef INSERT
}

241
242
/** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
static unsigned symbol(const char *str, typetag_t typetag)
243
{
244
	symbol_t key, *entry;
245
246

	key.str = str;
247
	key.typetag = typetag;
248

249
250
	entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)) + typetag * 17);
	return entry ? entry->code : SYMERROR;
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
}

static void *get_id(io_env_t *env, long id)
{
	id_entry key, *entry;
	key.id = id;

	entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
	return entry ? entry->elem : NULL;
}

static void set_id(io_env_t *env, long id, void *elem)
{
	id_entry key;
	key.id = id;
	key.elem = elem;
	set_insert(env->idset, &key, sizeof(key), (unsigned) id);
}

static void write_mode(io_env_t *env, ir_mode *mode)
{
	fputs(get_mode_name(mode), env->file);
	fputc(' ', env->file);
}

276
277
278
279
280
281
282
283
284
static void write_tarval(io_env_t *env, tarval *tv)
{
	char buf[1024];
	write_mode(env, get_tarval_mode(tv));
	tarval_snprintf(buf, sizeof(buf), tv);
	fputs(buf, env->file);
	fputc(' ', env->file);
}

285
286
287
288
289
290
291
292
293
294
295
296
static void write_align(io_env_t *env, ir_node *irn)
{
	ir_align align;

	if(is_Load(irn)) align = get_Load_align(irn);
	else if(is_Store(irn)) align = get_Store_align(irn);
	else assert(0 && "Invalid optype for write_align");

	fputs(get_align_name(align), env->file);
	fputc(' ', env->file);
}

297
298
299
300
301
302
static void write_builtin_kind(io_env_t *env, ir_node *irn)
{
	fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
	fputc(' ', env->file);
}

303
304
305
306
307
308
309
310
311
312
313
314
static void write_cond_kind(io_env_t *env, ir_node *irn)
{
	fputs(get_cond_kind_name(get_Cond_kind(irn)), env->file);
	fputc(' ', env->file);
}

static void write_cond_jmp_predicate(io_env_t *env, ir_node *irn)
{
	fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(irn)), env->file);
	fputc(' ', env->file);
}

315
316
317
318
static void write_initializer(io_env_t *env, ir_initializer_t *ini)
{
	FILE *f = env->file;
	ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
319

320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
	fputs(get_initializer_kind_name(ini_kind), f);
	fputc(' ', f);

	switch(ini_kind)
	{
		case IR_INITIALIZER_CONST:
			fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
			break;

		case IR_INITIALIZER_TARVAL:
			write_tarval(env, get_initializer_tarval_value(ini));
			break;

		case IR_INITIALIZER_NULL:
			break;

		case IR_INITIALIZER_COMPOUND:
		{
			unsigned i, n = get_initializer_compound_n_entries(ini);
			fprintf(f, "%d ", n);
			for(i = 0; i < n; i++)
				write_initializer(env, get_initializer_compound_value(ini, i));
			break;
		}

		default:
			panic("Unknown initializer kind");
	}
}

350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
static void write_pin_state(io_env_t *env, ir_node *irn)
{
	fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
	fputc(' ', env->file);
}

static void write_volatility(io_env_t *env, ir_node *irn)
{
	ir_volatility vol;

	if(is_Load(irn)) vol = get_Load_volatility(irn);
	else if(is_Store(irn)) vol = get_Store_volatility(irn);
	else assert(0 && "Invalid optype for write_volatility");

	fputs(get_volatility_name(vol), env->file);
	fputc(' ', env->file);
}

368
static void export_type_common(io_env_t *env, ir_type *tp)
369
{
370
	fprintf(env->file, "\t%s %ld %s \"%s\" %u %u %s %s ",
371
			is_frame_type(tp) ? "frametype" : is_value_param_type(tp) ? "valuetype" : "type",
372
373
374
375
376
377
378
			get_type_nr(tp),
			get_type_tpop_name(tp),
			get_type_name(tp),
			get_type_size_bytes(tp),
			get_type_alignment_bytes(tp),
			get_type_state_name(get_type_state(tp)),
			get_visibility_name(get_type_visibility(tp)));
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
}

static void export_type_pre(io_env_t *env, ir_type *tp)
{
	FILE *f = env->file;
	int i;

	// skip types to be handled by post walker
	switch(get_type_tpop_code(tp))
	{
		case tpo_array:
		case tpo_method:
		case tpo_pointer:
			return;
	}

	export_type_common(env, tp);

	switch(get_type_tpop_code(tp))
	{
		case tpo_class:
			/* TODO: inheritance stuff not supported yet */
			printf("Inheritance of classes not supported yet!\n");
			break;

		case tpo_primitive:
			write_mode(env, get_type_mode(tp));
			break;

		case tpo_struct:
			break;

		case tpo_union:
			break;

		case tpo_unknown:
			break;

		default:
			printf("export_type_pre: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
			break;
	}
	fputc('\n', f);
}

static void export_type_post(io_env_t *env, ir_type *tp)
{
	FILE *f = env->file;
	int i;

	// skip types already handled by pre walker
	switch(get_type_tpop_code(tp))
	{
		case tpo_class:
		case tpo_primitive:
		case tpo_struct:
		case tpo_union:
		case tpo_unknown:
			return;
	}

	export_type_common(env, tp);
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456

	switch(get_type_tpop_code(tp))
	{
		case tpo_array:
		{
			int n = get_array_n_dimensions(tp);
			fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
			for(i = 0; i < n; i++)
			{
				ir_node *lower = get_array_lower_bound(tp, i);
				ir_node *upper = get_array_upper_bound(tp, i);

				if(is_Const(lower)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
				else panic("Lower array bound is not constant");

				if(is_Const(upper)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
457
				else if(is_Unknown(upper)) fputs("unknown ", f);
458
459
460
461
462
463
464
465
466
				else panic("Upper array bound is not constant");
			}
			break;
		}

		case tpo_method:
		{
			int nparams = get_method_n_params(tp);
			int nresults = get_method_n_ress(tp);
467
468
			fprintf(f, "0x%X 0x%X %i %i ", get_method_calling_convention(tp),
				get_method_additional_properties(tp), nparams, nresults);
469
470
471
472
			for(i = 0; i < nparams; i++)
				fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
			for(i = 0; i < nresults; i++)
				fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
Moritz Kroll's avatar
Moritz Kroll committed
473
			fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
474
475
476
			break;
		}

477
		case tpo_pointer:
478
			write_mode(env, get_type_mode(tp));
479
			fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
480
481
482
483
484
485
486
487
488
489
490
491
			break;

		default:
			printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
			break;
	}
	fputc('\n', f);
}

static void export_entity(io_env_t *env, ir_entity *ent)
{
	ir_type *owner = get_entity_owner(ent);
492
	fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %s %s %s %s %s ",
493
494
			get_entity_nr(ent),
			get_entity_name(ent),
495
			ent->ld_name ? get_id_str(ent->ld_name) : "",
496
497
498
			get_type_nr(get_entity_type(ent)),
			get_type_nr(owner),
			get_entity_offset(ent),
499
			(unsigned) get_entity_offset_bits_remainder(ent),
500
501
502
503
504
505
506
507
508
			get_allocation_name(get_entity_allocation(ent)),
			get_visibility_name(get_entity_visibility(ent)),
			get_variability_name(get_entity_variability(ent)),
			get_peculiarity_name(get_entity_peculiarity(ent)),
			get_volatility_name(get_entity_volatility(ent)));

	// TODO: inheritance stuff for class entities not supported yet
	if(is_Class_type(owner) && owner != get_glob_type())
		printf("Inheritance of class entities not supported yet!\n");
509
510
511
512
513
514

	if(get_entity_variability(ent) != variability_uninitialized
		&& get_entity_visibility(ent) != visibility_external_allocated)
	{
		if(is_compound_entity(ent))
		{
515
516
517
518
519
520
			if(has_entity_initializer(ent))
			{
				fputs("initializer ", env->file);
				write_initializer(env, get_entity_initializer(ent));
			}
			else
521
			{
522
				int i, n = get_compound_ent_n_values(ent);
523
				fputs("noninitializer ", env->file);
524
525
526
527
528
529
530
				fprintf(env->file, "%d ", n);
				for(i = 0; i < n; i++)
				{
					ir_entity *member = get_compound_ent_value_member(ent, i);
					ir_node   *irn    = get_compound_ent_value(ent, i);
					fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
				}
531
532
533
534
535
536
537
538
539
540
			}
		}
		else
		{
			ir_node *irn = get_atomic_ent_value(ent);
			fprintf(env->file, "%ld ", get_irn_node_nr(irn));
		}
	}

	fputc('\n', env->file);
541
542
}

543
544
545
546
547
548
549
550
static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
{
	io_env_t *env = (io_env_t *) ctx;
	if(get_kind(tore.typ) == k_type)
		export_type_pre(env, tore.typ);
}

static void export_type_or_ent_post(type_or_ent tore, void *ctx)
551
552
553
554
555
556
557
558
559
560
{
	io_env_t *env = (io_env_t *) ctx;

	switch(get_kind(tore.ent))
	{
		case k_entity:
			export_entity(env, tore.ent);
			break;

		case k_type:
561
			export_type_post(env, tore.typ);
562
563
564
			break;

		default:
565
			printf("export_type_or_ent_post: Unknown type or entity.\n");
566
567
568
569
570
571
572
573
574
575
			break;
	}
}

static void export_node(ir_node *irn, void *ctx)
{
	io_env_t *env = (io_env_t *) ctx;
	int i, n;
	unsigned opcode = get_irn_opcode(irn);

576
577
	if(env->ignoreblocks && opcode == iro_Block)
		return;
578
579
580

	n = get_irn_arity(irn);

581
	fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
582
583
584
585

	for(i = -1; i < n; i++)
	{
		ir_node *pred = get_irn_n(irn, i);
586
		if(pred == NULL) {
587
			/* Anchor node may have NULL predecessors */
588
			assert(is_Anchor(irn));
589
			fputs("-1 ", env->file);
590
		} else {
591
			fprintf(env->file, "%ld ", get_irn_node_nr(pred));
592
		}
593
594
595
596
597
598
599
600
	}

	fprintf(env->file, "] { ");

	switch(opcode)
	{
		#include "gen_irio_export.inl"
	}
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
	fputs("}\n", env->file);
}

/** Exports the whole irp to the given file in a textual form. */
void ir_export(const char *filename)
{
	io_env_t env;
	int i, n_irgs = get_irp_n_irgs(), n_pseudoirgs = get_irp_n_pseudo_irgs();

	env.file = fopen(filename, "wt");
	if(!env.file)
	{
		perror(filename);
		return;
	}

	fputs("typegraph {\n", env.file);

619
	type_walk_plus_frames(export_type_or_ent_pre, export_type_or_ent_post, &env);
620
621
622
623
624
	/* TODO: Visit frame types and "types for value params"? */

	for(i = 0; i < n_irgs; i++)
	{
		ir_graph *irg = get_irp_irg(i);
625
		ir_type *valuetype = get_irg_value_param_type(irg);
626

627
628
629
		fprintf(env.file, "}\n\nirg %ld %ld %ld {\n", get_entity_nr(get_irg_entity(irg)),
			get_type_nr(get_irg_frame_type(irg)),
			valuetype == NULL ? -1 : get_type_nr(valuetype));
630
631
632
633
634
635
636
637

		env.ignoreblocks = 0;
		irg_block_walk_graph(irg, NULL, export_node, &env);

		env.ignoreblocks = 1;
		irg_walk_anchors(irg, NULL, export_node, &env);
	}

638
639
	fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));

640
641
642
643
	walk_const_code(NULL, export_node, &env);
	fputs("}\n", env.file);

	fclose(env.file);
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
}

/** Exports the given irg to the given file. */
void ir_export_irg(ir_graph *irg, const char *filename)
{
	io_env_t env;

	env.file = fopen(filename, "wt");
	if(!env.file)
	{
		perror(filename);
		return;
	}

	fputs("typegraph {\n", env.file);

660
	type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
661

662
	fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
663
664
665
666
667
668
669

	env.ignoreblocks = 0;
	irg_block_walk_graph(irg, NULL, export_node, &env);

	env.ignoreblocks = 1;
	irg_walk_anchors(irg, NULL, export_node, &env);

670
671
672
673
	/* TODO: Only output needed constants */
	fputs("}\n\nconstirg {\n", env.file);
	walk_const_code(NULL, export_node, &env);
	fputs("}\n", env.file);
674
675
676
677

	fclose(env.file);
}

Moritz Kroll's avatar
Moritz Kroll committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
static void save_lex_state(io_env_t *env, lex_state_t *state)
{
	state->offs = ftell(env->file);
	state->line = env->line;
	state->col  = env->col;
}

static void restore_lex_state(io_env_t *env, lex_state_t *state)
{
	fseek(env->file, state->offs, SEEK_SET);
	env->line = state->line;
	env->col  = state->col;
}

692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
static int read_c(io_env_t *env)
{
	int ch = fgetc(env->file);
	switch(ch)
	{
		case '\t':
			env->col += 4;
			break;

		case '\n':
			env->col = 0;
			env->line++;
			break;

		default:
			env->col++;
			break;
	}
	return ch;
}

/** Returns the first non-whitespace character or EOF. **/
static int skip_ws(io_env_t *env)
{
	while(1)
	{
		int ch = read_c(env);
		switch(ch)
		{
			case ' ':
			case '\t':
			case '\n':
			case '\r':
				break;

			default:
				return ch;
		}
	}
}

static void skip_to(io_env_t *env, char to_ch)
{
	int ch;
	do
	{
		ch = read_c(env);
	}
	while(ch != to_ch && ch != EOF);
}

static int expect_char(io_env_t *env, char ch)
{
	int curch = skip_ws(env);
	if(curch != ch)
	{
		printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
		return 0;
	}
	return 1;
}

#define EXPECT(c) if(expect_char(env, (c))) {} else return 0
#define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)

inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
{
	size_t i;
	for(i = 0; i < bufsize - 1; i++)
	{
		int ch = read_c(env);
		if(ch == EOF) break;
		switch(ch)
		{
			case ' ':
			case '\t':
			case '\n':
			case '\r':
				if(i != 0)
					goto endofword;
				i--;	// skip whitespace
				break;

			default:
				buf[i] = ch;
				break;
		}
	}
endofword:
	buf[i] = 0;
	return buf;
}

static const char *read_str(io_env_t *env)
{
	static char buf[1024];
	return read_str_to(env, buf, sizeof(buf));
}

static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
{
	size_t i;
	EXPECT_OR_EXIT('\"');
	for(i = 0; i < bufsize - 1; i++)
	{
		int ch = read_c(env);
		if(ch == EOF)
		{
			printf("Unexpected end of quoted string!\n");
			exit(1);
		}
		if(ch == '\"') break;

		buf[i] = ch;
	}
	if(i == bufsize - 1)
	{
		printf("Quoted string too long!\n");
		exit(1);
	}
	buf[i] = 0;
	return buf;
}

static long read_long2(io_env_t *env, char **endptr)
{
	static char buf[1024];
	return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
}

static long read_long(io_env_t *env)
{
	return read_long2(env, NULL);
}

static ir_node *get_node_or_null(io_env_t *env, long nodenr)
{
	ir_node *node = (ir_node *) get_id(env, nodenr);
	if(node && node->kind != k_ir_node)
	{
		panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
	}
	return node;
}

static ir_node *get_node(io_env_t *env, long nodenr)
{
	ir_node *node = get_node_or_null(env, nodenr);
	if(!node)
		panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);

	return node;
}

static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
{
	ir_node *node = get_node_or_null(env, nodenr);
	if(!node)
	{
		node = new_Dummy(mode_X);
		set_id(env, nodenr, node);
	}
	return node;
}

static ir_type *get_type(io_env_t *env, long typenr)
{
	ir_type *type = (ir_type *) get_id(env, typenr);
	if(!type)
	{
		panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
	}
	else if(type->kind != k_type)
	{
		panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
	}
	return type;
}

static ir_type *read_type(io_env_t *env)
{
	return get_type(env, read_long(env));
}

static ir_entity *get_entity(io_env_t *env, long entnr)
{
	ir_entity *entity = (ir_entity *) get_id(env, entnr);
	if(!entity)
	{
		printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
		exit(1);
	}
	else if(entity->kind != k_entity)
	{
		panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
	}
	return entity;
}

static ir_entity *read_entity(io_env_t *env)
{
	return get_entity(env, read_long(env));
}

static ir_mode *read_mode(io_env_t *env)
{
	static char buf[128];
	int i, n;

	read_str_to(env, buf, sizeof(buf));

	n = get_irp_n_modes();
	for(i = 0; i < n; i++)
	{
		ir_mode *mode = get_irp_mode(i);
		if(!strcmp(buf, get_mode_name(mode)))
			return mode;
	}

	printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
	return mode_ANY;
}

static const char *get_typetag_name(typetag_t typetag)
{
	switch(typetag)
	{
		case tt_align:       return "align";
		case tt_allocation:  return "allocation";
921
922
923
		case tt_builtin:     return "builtin kind";
		case tt_initializer: return "initializer kind";
		case tt_iro:         return "opcode";
924
925
		case tt_peculiarity: return "peculiarity";
		case tt_pin_state:   return "pin state";
926
		case tt_tpo:         return "type";
927
928
929
930
931
932
933
934
		case tt_type_state:  return "type state";
		case tt_variability: return "variability";
		case tt_visibility:  return "visibility";
		case tt_volatility:  return "volatility";
		default: return "<UNKNOWN>";
	}
}

935
936
937
/**
 * Read and decode an enum constant.
 */
938
939
940
static unsigned read_enum(io_env_t *env, typetag_t typetag)
{
	static char buf[128];
941
942
943
	unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
	if(code != SYMERROR)
		return code;
944
945
946
947
948

	printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
	return 0;
}

949
950
951
952
953
954
955
956
957
958
959
960
#define read_align(env)              ((ir_align)              read_enum(env, tt_align))
#define read_allocation(env)         ((ir_allocation)         read_enum(env, tt_allocation))
#define read_builtin_kind(env)       ((ir_builtin_kind)       read_enum(env, tt_builtin))
#define read_cond_kind(env)          ((cond_kind)             read_enum(env, tt_cond_kind))
#define read_cond_jmp_predicate(env) ((cond_jmp_predicate)    read_enum(env, tt_cond_jmp_predicate))
#define read_initializer_kind(env)   ((ir_initializer_kind_t) read_enum(env, tt_initializer))
#define read_peculiarity(env)        ((ir_peculiarity)        read_enum(env, tt_peculiarity))
#define read_pin_state(env)          ((op_pin_state)          read_enum(env, tt_pin_state))
#define read_type_state(env)         ((ir_type_state)         read_enum(env, tt_type_state))
#define read_variability(env)        ((ir_variability)        read_enum(env, tt_variability))
#define read_visibility(env)         ((ir_visibility)         read_enum(env, tt_visibility))
#define read_volatility(env)         ((ir_volatility)         read_enum(env, tt_volatility))
961

962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
static ir_cons_flags get_cons_flags(io_env_t *env)
{
	ir_cons_flags flags = cons_none;

	op_pin_state pinstate = read_pin_state(env);
	switch(pinstate)
	{
		case op_pin_state_floats: flags |= cons_floats; break;
		case op_pin_state_pinned: break;
		default:
			panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
	}

	if(read_volatility(env) == volatility_is_volatile) flags |= cons_volatile;
	if(read_align(env) == align_non_aligned) flags |= cons_unaligned;

	return flags;
}

981
982
983
984
985
986
987
988
static tarval *read_tv(io_env_t *env)
{
	static char buf[128];
	ir_mode *tvmode = read_mode(env);
	read_str_to(env, buf, sizeof(buf));
	return new_tarval_from_str(buf, strlen(buf), tvmode);
}

989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
static ir_initializer_t *read_initializer(io_env_t *env)
{
	FILE *f = env->file;
	ir_initializer_kind_t ini_kind = read_initializer_kind(env);

	switch(ini_kind)
	{
		case IR_INITIALIZER_CONST:
		{
			ir_node *irn = get_node_or_dummy(env, read_long(env));
			return create_initializer_const(irn);
		}

		case IR_INITIALIZER_TARVAL:
			return create_initializer_tarval(read_tv(env));

		case IR_INITIALIZER_NULL:
			return get_initializer_null();

		case IR_INITIALIZER_COMPOUND:
		{
			unsigned i, n = (unsigned) read_long(env);
			ir_initializer_t *ini = create_initializer_compound(n);
			for(i = 0; i < n; i++)
			{
				ir_initializer_t *curini = read_initializer(env);
				set_initializer_compound_value(ini, i, curini);
			}
			return ini;
		}

		default:
			panic("Unknown initializer kind");
	}
}


1026
/** Reads a type description and remembers it by its id. */
1027
static void import_type(io_env_t *env, keyword_t kwkind)
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
{
	char           buf[1024];
	int            i;
	ir_type       *type;
	long           typenr = read_long(env);
	const char    *tpop   = read_str(env);
	const char    *name   = read_qstr_to(env, buf, sizeof(buf));
	unsigned       size   = (unsigned) read_long(env);
	unsigned       align  = (unsigned) read_long(env);
	ir_type_state  state  = read_type_state(env);
	ir_visibility  vis    = read_visibility(env);

	ident         *id     = new_id_from_str(name);

1042
1043
1044
	const char    *kindstr;

	if(kwkind == kw_frametype)
1045
	{
1046
		if(symbol(tpop, tt_tpo) != tpo_class)
1047
		{
1048
1049
1050
			printf("Frame type must be a class type in line %i:%i\n", env->line, env->col);
			skip_to(env, '\n');
			return;
1051
1052
		}

1053
1054
		type = new_type_frame(id);
		set_type_size_bytes(type, size);
1055

1056
1057
1058
1059
1060
		kindstr = "frametype";
	}
	else if(kwkind == kw_valuetype)
	{
		if(symbol(tpop, tt_tpo) != tpo_struct)
1061
		{
1062
1063
1064
1065
			printf("Value type must be a struct type in line %i:%i\n", env->line, env->col);
			skip_to(env, '\n');
			return;
		}
1066

1067
1068
		type = new_type_value(id);
		set_type_size_bytes(type, size);
1069

1070
1071
1072
1073
1074
1075
1076
		kindstr = "valuetype";
	}
	else
	{
		switch(symbol(tpop, tt_tpo))
		{
			case tpo_array:
1077
			{
1078
1079
1080
				int ndims = (int) read_long(env);
				long elemtypenr = read_long(env);
				ir_type *elemtype = get_type(env, elemtypenr);
1081

1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
				type = new_type_array(id, ndims, elemtype);
				for(i = 0; i < ndims; i++)
				{
					const char *str = read_str(env);
					if(strcmp(str, "unknown"))
					{
						long lowerbound = strtol(str, NULL, 0);
						set_array_lower_bound_int(type, i, lowerbound);
					}
					str = read_str(env);
					if(strcmp(str, "unknown"))
					{
						long upperbound = strtol(str, NULL, 0);
						set_array_upper_bound_int(type, i, upperbound);
					}
				}
				set_type_size_bytes(type, size);
				break;
1100
			}
1101
1102
1103
1104
1105
1106
1107

			case tpo_class:
				type = new_type_class(id);
				set_type_size_bytes(type, size);
				break;

			case tpo_method:
1108
			{
1109
1110
1111
1112
				unsigned callingconv = (unsigned) read_long(env);
				unsigned addprops    = (unsigned) read_long(env);
				int nparams          = (int)      read_long(env);
				int nresults         = (int)      read_long(env);
Moritz Kroll's avatar
Moritz Kroll committed
1113
				int variaindex;
1114

1115
				type = new_type_method(id, nparams, nresults);
1116

1117
1118
1119
1120
				for(i = 0; i < nparams; i++)
				{
					long     typenr = read_long(env);
					ir_type *paramtype = get_type(env, typenr);
1121

1122
1123
1124
1125
1126
1127
					set_method_param_type(type, i, paramtype);
				}
				for(i = 0; i < nresults; i++)
				{
					long typenr = read_long(env);
					ir_type *restype = get_type(env, typenr);
1128

1129
1130
					set_method_res_type(type, i, restype);
				}
1131

Moritz Kroll's avatar
Moritz Kroll committed
1132
1133
1134
1135
1136
1137
1138
1139
				variaindex = (int) read_long(env);
				if(variaindex != -1)
				{
					set_method_variadicity(type, variadicity_variadic);
					if(variaindex != nparams)
						set_method_first_variadic_param_index(type, variaindex);
				}

1140
1141
1142
1143
				set_method_calling_convention(type, callingconv);
				set_method_additional_properties(type, addprops);
				break;
			}
1144

1145
1146
1147
1148
1149
1150
1151
			case tpo_pointer:
			{
				ir_mode *mode = read_mode(env);
				ir_type *pointsto = get_type(env, read_long(env));
				type = new_type_pointer(id, pointsto, mode);
				break;
			}
1152

1153
1154
1155
1156
1157
1158
			case tpo_primitive:
			{
				ir_mode *mode = read_mode(env);
				type = new_type_primitive(id, mode);
				break;
			}
1159

1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
			case tpo_struct:
				type = new_type_struct(id);
				set_type_size_bytes(type, size);
				break;

			case tpo_union:
				type = new_type_union(id);
				set_type_size_bytes(type, size);
				break;

			case tpo_unknown:
				return;   // ignore unknown type

			default:
				if(typenr != 0)  // ignore global type
					printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
				skip_to(env, '\n');
				return;
		}
		kindstr = "type";
1180
1181
1182
1183
1184
1185
1186
1187
1188
	}

	set_type_alignment_bytes(type, align);
	set_type_visibility(type, vis);

	if(state == layout_fixed)
		ARR_APP1(ir_type *, env->fixedtypes, type);

	set_id(env, typenr, type);
1189
	printf("Insert %s %s %ld\n", kindstr, name, typenr);
1190
1191
1192
1193
1194
}

/** Reads an entity description and remembers it by its id. */
static void import_entity(io_env_t *env)
{
1195
	char          buf[1024], buf2[1024];
1196
1197
	long          entnr       = read_long(env);
	const char   *name        = read_qstr_to(env, buf, sizeof(buf));
1198
	const char   *ld_name     = read_qstr_to(env, buf2, sizeof(buf2));
1199
1200
1201
1202
1203
1204
1205
	long          typenr      = read_long(env);
	long          ownertypenr = read_long(env);

	ir_type   *type      = get_type(env, typenr);
	ir_type   *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
	ir_entity *entity    = new_entity(ownertype, new_id_from_str(name), type);

1206
	if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
1207
1208
1209
1210
1211
1212
1213
1214
	set_entity_offset     (entity, (int) read_long(env));
	set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
	set_entity_allocation (entity, read_allocation(env));
	set_entity_visibility (entity, read_visibility(env));
	set_entity_variability(entity, read_variability(env));
	set_entity_peculiarity(entity, read_peculiarity(env));
	set_entity_volatility (entity, read_volatility(env));

1215
1216
1217
1218
1219
	if(get_entity_variability(entity) != variability_uninitialized
		&& get_entity_visibility(entity) != visibility_external_allocated)
	{
		if(is_compound_entity(entity))
		{
1220
1221
			if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
			{
1222
				set_entity_initializer(entity, read_initializer(env));
1223
1224
			}
			else
1225
			{
1226
1227
1228
1229
1230
1231
1232
				int i, n = (int) read_long(env);
				for(i = 0; i < n; i++)
				{
					ir_entity *member = get_entity(env, read_long(env));
					ir_node   *irn    = get_node_or_dummy(env, read_long(env));
					add_compound_ent_value(entity, irn, member);
				}
1233
1234
1235
1236
1237
1238
1239
1240
1241
			}
		}
		else
		{
			ir_node *irn = get_node_or_dummy(env, read_long(env));
			set_atomic_ent_value(entity, irn);
		}
	}

1242
1243
1244
1245
1246
1247
1248
1249
	set_id(env, entnr, entity);
	printf("Insert entity %s %ld\n", name, entnr);
}

/** Parses the whole type graph. */
static int parse_typegraph(io_env_t *env)
{
	const char *kind;
Moritz Kroll's avatar
Moritz Kroll committed
1250
	lex_state_t oldstate;
1251
1252
1253

	EXPECT('{');

Moritz Kroll's avatar
Moritz Kroll committed
1254
	save_lex_state(env, &oldstate);
1255

1256
1257
	current_ir_graph = get_const_code_irg();

1258
1259
1260
	// parse all types first
	while(1)
	{
1261
1262
		int isframetype = 0;

1263
1264
1265
		kind = read_str(env);
		if(kind[0] == '}' && !kind[1]) break;

1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
		keyword_t kwkind = (keyword_t) symbol(kind, tt_keyword);
		switch(kwkind)
		{
			case kw_type:
			case kw_frametype:
			case kw_valuetype:
				import_type(env, kwkind);
				break;

			default:
				skip_to(env, '\n');
				break;
		}
1279
1280
1281
	}

	// now parse rest
Moritz Kroll's avatar
Moritz Kroll committed
1282
1283
	restore_lex_state(env, &oldstate);

1284
1285
1286
1287
1288
	while(1)
	{
		kind = read_str(env);
		if(kind[0] == '}' && !kind[1]) break;

1289
		switch(symbol(kind, tt_keyword))
1290
		{
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
			case kw_type:
			case kw_frametype:
			case kw_valuetype:
				skip_to(env, '\n');
				break;

			case kw_entity:
				import_entity(env);
				break;

			default:
				printf("Type graph element not supported yet: \"%s\"\n", kind);
				skip_to(env, '\n');
				break;
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
		}
	}
	return 1;
}

static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
{
	int numpreds;
	*nodename = read_str(env);
	if((*nodename)[0] == '}' && !(*nodename)[1]) return -1;  // end-of-graph

	*nodenr = read_long(env);

	ARR_RESIZE(ir_node *, *preds, 0);

	EXPECT('[');
	for(numpreds = 0; !feof(env->file); numpreds++)
	{
		char *endptr;
		ARR_APP1(long, *preds, read_long2(env, &endptr));
		if(*endptr == ']') break;
	}
	return numpreds;
}

/** Parses an IRG. */
1331
static int parse_graph(io_env_t *env, ir_graph *irg)
1332
1333
1334
1335
1336
1337
1338
1339
{
	long       *preds = NEW_ARR_F(long, 16);
	ir_node   **prednodes = NEW_ARR_F(ir_node *, 16);
	int         i, numpreds, ret = 1;
	long        nodenr;
	const char *nodename;
	ir_node    *node, *newnode;

1340
	current_ir_graph = irg;
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363

	EXPECT('{');

	while(1)
	{
		numpreds = read_node_header(env, &nodenr, &preds, &nodename);
		if(numpreds == -1) break;  // end-of-graph
		if(!numpreds)
		{
			printf("Node %s %ld is missing predecessors!", nodename, nodenr);
			ret = 0;
			break;
		}

		ARR_RESIZE(ir_node *, prednodes, numpreds);
		for(i = 0; i < numpreds - 1; i++)
			prednodes[i] = get_node_or_dummy(env, preds[i + 1]);

		node = get_node_or_null(env, nodenr);
		newnode = NULL;

		EXPECT('{');

1364
		switch(symbol(nodename, tt_iro))
1365
1366
1367
1368
1369
1370
		{
			case iro_End:
			{
				ir_node *newendblock = get_node(env, preds[0]);
				newnode = get_irg_end(current_ir_graph);
				exchange(get_nodes_block(newnode), newendblock);
1371
1372
				for(i = 0; i < numpreds - 1; i++)
					add_irn_n(newnode, prednodes[i]);
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
				break;
			}

			case iro_Start:
			{
				ir_node *newstartblock = get_node(env, preds[0]);
				newnode = get_irg_start(current_ir_graph);
				exchange(get_nodes_block(newnode), newstartblock);
				break;
			}

			case iro_Block:
			{
				if(preds[0] != nodenr)
				{
					printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
						preds[0], nodenr);
					ret = 0;
					goto endloop;
				}

				newnode = new_Block(numpreds - 1, prednodes);
				break;
			}

			case iro_Anchor:
				newnode = current_ir_graph->anchor;
				for(i = 0; i < numpreds - 1; i++)
					set_irn_n(newnode, i, prednodes[i]);
				set_irn_n(newnode, -1, get_node(env, preds[0]));
				break;

			case iro_SymConst:
			{
				long entnr = read_long(env);
				union symconst_symbol sym;
				sym.entity_p = get_entity(env, entnr);
				newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
				break;
			}

			#include "gen_irio_import.inl"

			default:
				goto notsupported;
		}

		EXPECT('}');

		if(!newnode)
		{
notsupported:
1425
			panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1426
1427
1428
1429
1430
1431
		}

		if(node)
			exchange(node, newnode);
		/* Always update hash entry to avoid more uses of id nodes */
		set_id(env, nodenr, newnode);
1432
		//printf("Insert %s %ld\n", nodename, nodenr);
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
	}

endloop:
	DEL_ARR_F(preds);
	DEL_ARR_F(prednodes);

	return ret;
}

/** Imports an previously exported textual representation of an (maybe partial) irp */
void ir_import(const char *filename)
{
	int oldoptimize = get_optimize();
	firm_verification_t oldver = get_node_verification_mode();
	io_env_t ioenv;
	io_env_t *env = &ioenv;
	int i, n;

1451
	symtbl_init();
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470

	memset(env, 0, sizeof(*env));
	env->idset = new_set(id_cmp, 128);
	env->fixedtypes = NEW_ARR_F(ir_type *, 0);

	env->file = fopen(filename, "rt");
	if(!env->file)
	{
		perror(filename);
		exit(1);
	}

	set_optimize(0);
	do_node_verification(FIRM_VERIFICATION_OFF);

	while(1)
	{
		const char *str = read_str(env);
		if(!*str) break;
1471
		switch(symbol(str, tt_keyword))
1472
		{
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
			case kw_typegraph:
				if(!parse_typegraph(env)) goto end;
				break;

			case kw_irg:
			{
				ir_entity *irgent = get_entity(env, read_long(env));
				long valuetypeid;
				ir_graph *irg = new_ir_graph(irgent, 0);
				set_irg_frame_type(irg, get_type(env, read_long(env)));
				valuetypeid = read_long(env);
				if(valuetypeid != -1)
					set_method_value_param_type(get_entity_type(irgent),
							get_type(env, valuetypeid));

				if(!parse_graph(env, irg)) goto end;
				break;
			}

			case kw_constirg:
1493
1494
1495
1496
1497
			{
				ir_graph *constirg = get_const_code_irg();
				long bodyblockid = read_long(env);
				set_id(env, bodyblockid, constirg->current_block);
				if(!parse_graph(env, constirg)) goto end;
1498
				break;
1499
			}
1500
1501
1502
		}
	}

1503
end:
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
	n = ARR_LEN(env->fixedtypes);
	for(i = 0; i < n; i++)
		set_type_state(env->fixedtypes[i], layout_fixed);

	DEL_ARR_F(env->fixedtypes);

	del_set(env->idset);

	irp_finalize_cons();

	do_node_verification(oldver);
	set_optimize(oldoptimize);

	fclose(env->file);
}