irio.c 60 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
8
 */

/**
 * @file
 * @brief   Write textual representation of firm to file.
Matthias Braun's avatar
Matthias Braun committed
9
 * @author  Moritz Kroll, Matthias Braun
10
11
 */
#include <string.h>
12
13
14
#include <ctype.h>
#include <stdbool.h>
#include <stdarg.h>
15
16
17

#include "irio.h"

18
#include "irnode_t.h"
19
20
#include "irprog.h"
#include "irgraph_t.h"
Michael Beck's avatar
Michael Beck committed
21
#include "irprintf.h"
Matthias Braun's avatar
Matthias Braun committed
22
#include "ircons_t.h"
23
24
25
26
27
28
#include "irgmod.h"
#include "irflag_t.h"
#include "irgwalk.h"
#include "tv.h"
#include "array.h"
#include "error.h"
29
#include "typerep.h"
Matthias Braun's avatar
Matthias Braun committed
30
31
32
33
#include "set.h"
#include "obst.h"
#include "pmap.h"
#include "pdeq.h"
34

35
#define SYMERROR ((unsigned) ~0)
36

Matthias Braun's avatar
Matthias Braun committed
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
static void register_generated_node_readers(void);
static void register_generated_node_writers(void);

typedef struct delayed_initializer_t {
	ir_initializer_t *initializer;
	long              node_nr;
} delayed_initializer_t;

typedef struct delayed_pred_t {
	ir_node *node;
	int      n_preds;
	long     preds[];
} delayed_pred_t;

typedef struct read_env_t {
	int            c;           /**< currently read char */
	FILE          *file;
	const char    *inputname;
	unsigned       line;

	ir_graph      *irg;
	set           *idset;       /**< id_entry set, which maps from file ids to
	                                 new Firm elements */
	ir_type      **fixedtypes;
	bool           read_errors;
62
	struct obstack obst;
Matthias Braun's avatar
Matthias Braun committed
63
64
65
66
	struct obstack preds_obst;
	delayed_initializer_t *delayed_initializers;
	const delayed_pred_t **delayed_preds;
} read_env_t;
67

Matthias Braun's avatar
Matthias Braun committed
68
69
70
typedef struct write_env_t {
	FILE *file;
	pdeq *write_queue;
71
	pdeq *entity_queue;
Matthias Braun's avatar
Matthias Braun committed
72
73
74
} write_env_t;

typedef enum typetag_t {
75
	tt_align,
Matthias Braun's avatar
Matthias Braun committed
76
	tt_builtin_kind,
77
	tt_cond_jmp_predicate,
78
	tt_initializer,
79
	tt_keyword,
Matthias Braun's avatar
Matthias Braun committed
80
	tt_linkage,
81
	tt_mode_arithmetic,
82
	tt_pin_state,
Matthias Braun's avatar
Matthias Braun committed
83
84
	tt_segment,
	tt_throws,
85
	tt_tpo,
86
	tt_type_state,
Matthias Braun's avatar
Matthias Braun committed
87
	tt_visibility,
88
	tt_volatility,
89
90
} typetag_t;

Matthias Braun's avatar
Matthias Braun committed
91
92
93
typedef enum keyword_t {
	kw_asm,
	kw_compound_member,
94
95
	kw_constirg,
	kw_entity,
96
	kw_float_mode,
Matthias Braun's avatar
Matthias Braun committed
97
98
	kw_int_mode,
	kw_irg,
99
	kw_alias,
Matthias Braun's avatar
Matthias Braun committed
100
	kw_gotentry,
Matthias Braun's avatar
Matthias Braun committed
101
102
	kw_label,
	kw_method,
103
	kw_modes,
Matthias Braun's avatar
Matthias Braun committed
104
105
106
107
	kw_parameter,
	kw_program,
	kw_reference_mode,
	kw_segment_type,
108
	kw_type,
109
	kw_typegraph,
110
	kw_unknown,
111
112
} keyword_t;

Matthias Braun's avatar
Matthias Braun committed
113
typedef struct symbol_t {
114
115
116
117
	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;
118

Matthias Braun's avatar
Matthias Braun committed
119
typedef struct id_entry {
120
121
122
123
	long id;
	void *elem;
} id_entry;

124
125
/** The symbol table, a set of symbol_t elements. */
static set *symtbl;
126

127
128
129
130
/**
 * Compare two symbol table entries.
 */
static int symbol_cmp(const void *elt, const void *key, size_t size)
131
{
132
	int res;
133
134
	const symbol_t *entry = (const symbol_t *) elt;
	const symbol_t *keyentry = (const symbol_t *) key;
135
	(void) size;
136
	res = entry->typetag - keyentry->typetag;
Michael Beck's avatar
Michael Beck committed
137
	if (res) return res;
138
139
140
141
142
143
144
145
146
147
148
	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;
}

149
static void __attribute__((format(printf, 2, 3)))
Matthias Braun's avatar
Matthias Braun committed
150
parse_error(read_env_t *env, const char *fmt, ...)
151
{
Matthias Braun's avatar
Matthias Braun committed
152
153
	va_list  ap;
	unsigned line = env->line;
154
155
156
157
158
159
160

	/* workaround read_c "feature" that a '\n' triggers the line++
	 * instead of the character after the '\n' */
	if (env->c == '\n') {
		line--;
	}

Matthias Braun's avatar
Matthias Braun committed
161
162
163
	fprintf(stderr, "%s:%u: error ", env->inputname, line);
	env->read_errors = true;

164
165
166
167
168
	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
}

169
170
/** Initializes the symbol table. May be called more than once without problems. */
static void symtbl_init(void)
171
{
172
	symbol_t key;
173
174

	/* Only initialize once */
Michael Beck's avatar
Michael Beck committed
175
	if (symtbl != NULL)
176
		return;
177

178
	symtbl = new_set(symbol_cmp, 256);
179

180
#define INSERT(tt, s, cod)                                       \
181
182
183
	key.str = (s);                                               \
	key.typetag = (tt);                                          \
	key.code = (cod);                                            \
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
184
	(void)set_insert(symbol_t, symtbl, &key, sizeof(key), hash_str(s) + tt * 17)
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

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

	INSERT(tt_tpo, "array", tpo_array);
	INSERT(tt_tpo, "class", tpo_class);
	INSERT(tt_tpo, "method", tpo_method);
	INSERT(tt_tpo, "pointer", tpo_pointer);
	INSERT(tt_tpo, "primitive", tpo_primitive);
	INSERT(tt_tpo, "struct", tpo_struct);
	INSERT(tt_tpo, "union", tpo_union);
	INSERT(tt_tpo, "Unknown", tpo_unknown);

	INSERT(tt_segment, "global", IR_SEGMENT_GLOBAL);
	INSERT(tt_segment, "thread_local", IR_SEGMENT_THREAD_LOCAL);
	INSERT(tt_segment, "constructors", IR_SEGMENT_CONSTRUCTORS);
	INSERT(tt_segment, "destructors", IR_SEGMENT_DESTRUCTORS);
202

Matthias Braun's avatar
Matthias Braun committed
203
204
205
206
207
208
	INSERT(tt_linkage, "constant", IR_LINKAGE_CONSTANT);
	INSERT(tt_linkage, "weak", IR_LINKAGE_WEAK);
	INSERT(tt_linkage, "garbage_collect", IR_LINKAGE_GARBAGE_COLLECT);
	INSERT(tt_linkage, "merge", IR_LINKAGE_MERGE);
	INSERT(tt_linkage, "hidden_user", IR_LINKAGE_HIDDEN_USER);

209
210
	INSERT(tt_visibility, "local", ir_visibility_local);
	INSERT(tt_visibility, "external", ir_visibility_external);
211
	INSERT(tt_visibility, "private", ir_visibility_private);
212

Matthias Braun's avatar
Matthias Braun committed
213
214
215
	INSERT(tt_throws, "throw",   true);
	INSERT(tt_throws, "nothrow", false);

216
	INSERTKEYWORD(alias);
Matthias Braun's avatar
Matthias Braun committed
217
218
	INSERTKEYWORD(asm);
	INSERTKEYWORD(compound_member);
219
220
	INSERTKEYWORD(constirg);
	INSERTKEYWORD(entity);
221
	INSERTKEYWORD(float_mode);
Matthias Braun's avatar
Matthias Braun committed
222
	INSERTKEYWORD(gotentry);
Matthias Braun's avatar
Matthias Braun committed
223
224
225
226
	INSERTKEYWORD(int_mode);
	INSERTKEYWORD(irg);
	INSERTKEYWORD(label);
	INSERTKEYWORD(method);
227
	INSERTKEYWORD(modes);
Matthias Braun's avatar
Matthias Braun committed
228
	INSERTKEYWORD(parameter);
229
	INSERTKEYWORD(program);
Matthias Braun's avatar
Matthias Braun committed
230
	INSERTKEYWORD(reference_mode);
231
	INSERTKEYWORD(segment_type);
Matthias Braun's avatar
Matthias Braun committed
232
233
	INSERTKEYWORD(type);
	INSERTKEYWORD(typegraph);
234
	INSERTKEYWORD(unknown);
235
236
237
238

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

Matthias Braun's avatar
Matthias Braun committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	INSERTENUM(tt_builtin_kind, ir_bk_trap);
	INSERTENUM(tt_builtin_kind, ir_bk_debugbreak);
	INSERTENUM(tt_builtin_kind, ir_bk_return_address);
	INSERTENUM(tt_builtin_kind, ir_bk_frame_address);
	INSERTENUM(tt_builtin_kind, ir_bk_prefetch);
	INSERTENUM(tt_builtin_kind, ir_bk_ffs);
	INSERTENUM(tt_builtin_kind, ir_bk_clz);
	INSERTENUM(tt_builtin_kind, ir_bk_ctz);
	INSERTENUM(tt_builtin_kind, ir_bk_popcount);
	INSERTENUM(tt_builtin_kind, ir_bk_parity);
	INSERTENUM(tt_builtin_kind, ir_bk_bswap);
	INSERTENUM(tt_builtin_kind, ir_bk_inport);
	INSERTENUM(tt_builtin_kind, ir_bk_outport);
	INSERTENUM(tt_builtin_kind, ir_bk_inner_trampoline);
253
	INSERTENUM(tt_builtin_kind, ir_bk_saturating_increment);
254
	INSERTENUM(tt_builtin_kind, ir_bk_compare_swap);
255
256
257
258
259
260
261
262
263
264

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

Matthias Braun's avatar
Matthias Braun committed
265
266
267
268
269
	INSERT(tt_mode_arithmetic, "none",               irma_none);
	INSERT(tt_mode_arithmetic, "twos_complement",    irma_twos_complement);
	INSERT(tt_mode_arithmetic, "ieee754",            irma_ieee754);
	INSERT(tt_mode_arithmetic, "x86_extended_float", irma_x86_extended_float);

270
271
272
273
274
275
276
277
278
279
	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_type_state, layout_undefined);
	INSERTENUM(tt_type_state, layout_fixed);

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

280
#undef INSERTKEYWORD
281
282
283
284
#undef INSERTENUM
#undef INSERT
}

285
286
287
288
289
290
291
292
static const char *get_segment_name(ir_segment_t segment)
{
	switch (segment) {
	case IR_SEGMENT_GLOBAL:       return "global";
	case IR_SEGMENT_THREAD_LOCAL: return "thread_local";
	case IR_SEGMENT_CONSTRUCTORS: return "constructors";
	case IR_SEGMENT_DESTRUCTORS:  return "destructors";
	}
Matthias Braun's avatar
Matthias Braun committed
293
	panic("INVALID_SEGMENT");
294
295
}

296
297
298
299
300
301
302
static const char *get_visibility_name(ir_visibility visibility)
{
	switch (visibility) {
	case ir_visibility_local:    return "local";
	case ir_visibility_external: return "external";
	case ir_visibility_private:  return "private";
	}
Matthias Braun's avatar
Matthias Braun committed
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	panic("INVALID_VISIBILITY");
}

static const char *get_mode_arithmetic_name(ir_mode_arithmetic arithmetic)
{
	switch (arithmetic) {
	case irma_none:               return "none";
	case irma_twos_complement:    return "twos_complement";
	case irma_ieee754:            return "ieee754";
	case irma_x86_extended_float: return "x86_extended_float";
	}
	panic("invalid mode_arithmetic");
}

317
318
/** 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)
319
{
320
	symbol_t key, *entry;
321
322

	key.str = str;
323
	key.typetag = typetag;
324

325
	entry = set_find(symbol_t, symtbl, &key, sizeof(key), hash_str(str) + typetag * 17);
326
	return entry ? entry->code : SYMERROR;
327
328
}

Matthias Braun's avatar
Matthias Braun committed
329
static void write_long(write_env_t *env, long value)
330
{
Matthias Braun's avatar
Matthias Braun committed
331
	fprintf(env->file, "%ld ", value);
332
333
}

Matthias Braun's avatar
Matthias Braun committed
334
static void write_int(write_env_t *env, int value)
335
{
Matthias Braun's avatar
Matthias Braun committed
336
	fprintf(env->file, "%d ", value);
337
338
}

Matthias Braun's avatar
Matthias Braun committed
339
static void write_unsigned(write_env_t *env, unsigned value)
340
{
Matthias Braun's avatar
Matthias Braun committed
341
	fprintf(env->file, "%u ", value);
342
343
}

Matthias Braun's avatar
Matthias Braun committed
344
static void write_size_t(write_env_t *env, size_t value)
345
{
Matthias Braun's avatar
Matthias Braun committed
346
	ir_fprintf(env->file, "%zu ", value);
347
348
}

Matthias Braun's avatar
Matthias Braun committed
349
static void write_symbol(write_env_t *env, const char *symbol)
350
{
Matthias Braun's avatar
Matthias Braun committed
351
352
	fputs(symbol, env->file);
	fputc(' ', env->file);
353
354
}

Matthias Braun's avatar
Matthias Braun committed
355
static void write_entity_ref(write_env_t *env, ir_entity *entity)
356
357
358
359
{
	write_long(env, get_entity_nr(entity));
}

Matthias Braun's avatar
Matthias Braun committed
360
static void write_type_ref(write_env_t *env, ir_type *type)
361
{
362
363
	switch (get_type_tpop_code(type)) {
	case tpo_unknown:
Matthias Braun's avatar
Matthias Braun committed
364
		write_symbol(env, "unknown");
365
366
		return;
	case tpo_code:
Matthias Braun's avatar
Matthias Braun committed
367
		write_symbol(env, "code");
368
369
370
		return;
	default:
		break;
Matthias Braun's avatar
Matthias Braun committed
371
	}
372
	write_long(env, get_type_nr(type));
373
374
}

Matthias Braun's avatar
Matthias Braun committed
375
static void write_string(write_env_t *env, const char *string)
Matthias Braun's avatar
Matthias Braun committed
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
{
	const char *c;
	fputc('"', env->file);
	for (c = string; *c != '\0'; ++c) {
		switch (*c) {
		case '\n':
			fputc('\\', env->file);
			fputc('n', env->file);
			break;
		case '"':
		case '\\':
			fputc('\\', env->file);
			/* FALLTHROUGH */
		default:
			fputc(*c, env->file);
			break;
		}
	}
	fputc('"', env->file);
Matthias Braun's avatar
Matthias Braun committed
395
	fputc(' ', env->file);
Matthias Braun's avatar
Matthias Braun committed
396
397
}

Matthias Braun's avatar
Matthias Braun committed
398
static void write_ident(write_env_t *env, ident *id)
Matthias Braun's avatar
Matthias Braun committed
399
400
401
402
{
	write_string(env, get_id_str(id));
}

Matthias Braun's avatar
Matthias Braun committed
403
static void write_ident_null(write_env_t *env, ident *id)
Matthias Braun's avatar
Matthias Braun committed
404
405
406
407
408
409
410
411
{
	if (id == NULL) {
		fputs("NULL ", env->file);
	} else {
		write_ident(env, id);
	}
}

Matthias Braun's avatar
Matthias Braun committed
412
static void write_mode_ref(write_env_t *env, ir_mode *mode)
413
{
414
	write_string(env, get_mode_name(mode));
415
416
}

Matthias Braun's avatar
Matthias Braun committed
417
static void write_tarval(write_env_t *env, ir_tarval *tv)
418
{
Matthias Braun's avatar
Matthias Braun committed
419
420
421
422
	write_mode_ref(env, get_tarval_mode(tv));
	if (tv == tarval_bad) {
		write_symbol(env, "bad");
	} else {
423
		fputs("0x", env->file);
Matthias Braun's avatar
Matthias Braun committed
424
		char buf[1024];
425
		tarval_snprintf(buf, sizeof(buf), tv);
Matthias Braun's avatar
Matthias Braun committed
426
427
428
		fputs(buf, env->file);
		fputc(' ', env->file);
	}
429
430
}

Matthias Braun's avatar
Matthias Braun committed
431
static void write_align(write_env_t *env, ir_align align)
432
433
434
435
436
{
	fputs(get_align_name(align), env->file);
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
437
static void write_builtin_kind(write_env_t *env, const ir_node *node)
438
{
Matthias Braun's avatar
Matthias Braun committed
439
	fputs(get_builtin_kind_name(get_Builtin_kind(node)), env->file);
440
441
442
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
443
static void write_cond_jmp_predicate(write_env_t *env, const ir_node *node)
444
{
Matthias Braun's avatar
Matthias Braun committed
445
	fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(node)), env->file);
446
447
448
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
449
450
451
452
453
454
455
456
457
458
459
static void write_relation(write_env_t *env, ir_relation relation)
{
	write_long(env, (long)relation);
}

static void write_throws(write_env_t *env, bool throws)
{
	write_symbol(env, throws ? "throw" : "nothrow");
}

static void write_list_begin(write_env_t *env)
460
461
462
463
{
	fputs("[", env->file);
}

Matthias Braun's avatar
Matthias Braun committed
464
static void write_list_end(write_env_t *env)
465
466
467
468
{
	fputs("] ", env->file);
}

Matthias Braun's avatar
Matthias Braun committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
static void write_scope_begin(write_env_t *env)
{
	fputs("{\n", env->file);
}

static void write_scope_end(write_env_t *env)
{
	fputs("}\n\n", env->file);
}

static void write_node_ref(write_env_t *env, const ir_node *node)
{
	write_long(env, get_irn_node_nr(node));
}

static void write_initializer(write_env_t *env, ir_initializer_t *ini)
485
486
487
{
	FILE *f = env->file;
	ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
488

489
490
491
	fputs(get_initializer_kind_name(ini_kind), f);
	fputc(' ', f);

492
493
	switch (ini_kind) {
	case IR_INITIALIZER_CONST:
Matthias Braun's avatar
Matthias Braun committed
494
495
		write_node_ref(env, get_initializer_const_value(ini));
		return;
496

497
498
	case IR_INITIALIZER_TARVAL:
		write_tarval(env, get_initializer_tarval_value(ini));
Matthias Braun's avatar
Matthias Braun committed
499
		return;
500

501
	case IR_INITIALIZER_NULL:
Matthias Braun's avatar
Matthias Braun committed
502
		return;
503

504
	case IR_INITIALIZER_COMPOUND: {
Michael Beck's avatar
Michael Beck committed
505
		size_t i, n = get_initializer_compound_n_entries(ini);
Matthias Braun's avatar
Matthias Braun committed
506
		write_size_t(env, n);
Michael Beck's avatar
Michael Beck committed
507
		for (i = 0; i < n; ++i)
508
			write_initializer(env, get_initializer_compound_value(ini, i));
Matthias Braun's avatar
Matthias Braun committed
509
		return;
510
	}
511
	}
Matthias Braun's avatar
Matthias Braun committed
512
	panic("Unknown initializer kind");
513
514
}

Matthias Braun's avatar
Matthias Braun committed
515
static void write_pin_state(write_env_t *env, op_pin_state state)
516
{
Matthias Braun's avatar
Matthias Braun committed
517
	fputs(get_op_pin_state_name(state), env->file);
518
519
520
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
521
static void write_volatility(write_env_t *env, ir_volatility vol)
522
523
524
525
526
{
	fputs(get_volatility_name(vol), env->file);
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
527
static void write_type_state(write_env_t *env, ir_type_state state)
528
{
Matthias Braun's avatar
Matthias Braun committed
529
530
531
	fputs(get_type_state_name(state), env->file);
	fputc(' ', env->file);
}
532

Matthias Braun's avatar
Matthias Braun committed
533
534
535
536
537
static void write_visibility(write_env_t *env, ir_visibility visibility)
{
	fputs(get_visibility_name(visibility), env->file);
	fputc(' ', env->file);
}
538

Matthias Braun's avatar
Matthias Braun committed
539
540
541
542
543
static void write_mode_arithmetic(write_env_t *env, ir_mode_arithmetic arithmetic)
{
	fputs(get_mode_arithmetic_name(arithmetic), env->file);
	fputc(' ', env->file);
}
544

Matthias Braun's avatar
Matthias Braun committed
545
546
547
548
549
550
551
552
553
554
555
static void write_type_common(write_env_t *env, ir_type *tp)
{
	fputc('\t', env->file);
	write_symbol(env, "type");
	write_long(env, get_type_nr(tp));
	write_symbol(env, get_type_tpop_name(tp));
	write_unsigned(env, get_type_size_bytes(tp));
	write_unsigned(env, get_type_alignment_bytes(tp));
	write_type_state(env, get_type_state(tp));
	write_unsigned(env, tp->flags);
}
556

sebastian.buchwald1's avatar
sebastian.buchwald1 committed
557
558
static void write_type(write_env_t *env, ir_type *tp);

Matthias Braun's avatar
Matthias Braun committed
559
560
561
562
563
static void write_type_primitive(write_env_t *env, ir_type *tp)
{
	write_type_common(env, tp);
	write_mode_ref(env, get_type_mode(tp));
	fputc('\n', env->file);
564
565
}

Matthias Braun's avatar
Matthias Braun committed
566
static void write_type_compound(write_env_t *env, ir_type *tp)
567
{
Matthias Braun's avatar
Matthias Braun committed
568
	size_t n_members = get_compound_n_members(tp);
Michael Beck's avatar
Michael Beck committed
569
	size_t i;
570

Matthias Braun's avatar
Matthias Braun committed
571
	if (is_Class_type(tp)) {
572
		if (get_class_n_subtypes(tp) > 0 || get_class_n_supertypes(tp) > 0 || get_class_vtable_size(tp) > 0) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
573
			/* sub/superclass export not implemented yet, it's unclear whether
Matthias Braun's avatar
Matthias Braun committed
574
575
576
			 * class types will stay in libfirm anyway */
			panic("can't export class types yet");
		}
577
	}
Matthias Braun's avatar
Matthias Braun committed
578
579
580
	write_type_common(env, tp);
	write_ident_null(env, get_compound_ident(tp));
	fputc('\n', env->file);
581

Matthias Braun's avatar
Matthias Braun committed
582
583
	for (i = 0; i < n_members; ++i) {
		ir_entity *member = get_compound_member(tp, i);
584
		pdeq_putr(env->entity_queue, member);
585
	}
Matthias Braun's avatar
Matthias Braun committed
586
}
587

Matthias Braun's avatar
Matthias Braun committed
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
static void write_type_array(write_env_t *env, ir_type *tp)
{
	size_t     n_dimensions   = get_array_n_dimensions(tp);
	ir_type   *element_type   = get_array_element_type(tp);
	ir_entity *element_entity = get_array_element_entity(tp);
	size_t   i;

	write_type(env, element_type);

	write_type_common(env, tp);
	write_size_t(env, n_dimensions);
	write_type_ref(env, get_array_element_type(tp));
	for (i = 0; i < n_dimensions; i++) {
		ir_node *lower = get_array_lower_bound(tp, i);
		ir_node *upper = get_array_upper_bound(tp, i);

		if (is_Const(lower))
			write_long(env, get_tarval_long(get_Const_tarval(lower)));
		else
			panic("Lower array bound is not constant");

		if (is_Const(upper))
			write_long(env, get_tarval_long(get_Const_tarval(upper)));
		else if (is_Unknown(upper))
			write_symbol(env, "unknown");
		else
			panic("Upper array bound is not constant");
615
	}
Matthias Braun's avatar
Matthias Braun committed
616
617
618
619
620
	/* note that we just write a reference to the element entity
	 * but never the entity itself */
	write_entity_ref(env, element_entity);
	fputc('\n', env->file);
}
621

Matthias Braun's avatar
Matthias Braun committed
622
623
624
625
626
static void write_type_method(write_env_t *env, ir_type *tp)
{
	size_t nparams  = get_method_n_params(tp);
	size_t nresults = get_method_n_ress(tp);
	size_t i;
627

Matthias Braun's avatar
Matthias Braun committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
	for (i = 0; i < nparams; i++)
		write_type(env, get_method_param_type(tp, i));
	for (i = 0; i < nresults; i++)
		write_type(env, get_method_res_type(tp, i));

	write_type_common(env, tp);
	write_unsigned(env, get_method_calling_convention(tp));
	write_unsigned(env, get_method_additional_properties(tp));
	write_size_t(env, nparams);
	write_size_t(env, nresults);
	for (i = 0; i < nparams; i++)
		write_type_ref(env, get_method_param_type(tp, i));
	for (i = 0; i < nresults; i++)
		write_type_ref(env, get_method_res_type(tp, i));
	write_unsigned(env, get_method_variadicity(tp));
	fputc('\n', env->file);
}
645

Matthias Braun's avatar
Matthias Braun committed
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
static void write_type_pointer(write_env_t *env, ir_type *tp)
{
	ir_type *points_to = get_pointer_points_to_type(tp);

	write_type(env, points_to);

	write_type_common(env, tp);
	write_mode_ref(env, get_type_mode(tp));
	write_type_ref(env, points_to);
	fputc('\n', env->file);
}

static void write_type(write_env_t *env, ir_type *tp)
{
	if (type_visited(tp))
		return;
	mark_type_visited(tp);

	switch ((tp_opcode)get_type_tpop_code(tp)) {
	case tpo_unknown:
	case tpo_code:
	case tpo_uninitialized:
		/* no need to write special builtin types */
		return;

	case tpo_union:
	case tpo_struct:
	case tpo_class:
		write_type_compound(env, tp);
		return;

	case tpo_primitive:    write_type_primitive(env, tp);   return;
	case tpo_method:       write_type_method(env, tp);      return;
	case tpo_pointer:      write_type_pointer(env, tp);     return;
	case tpo_array:        write_type_array(env, tp);       return;
681
	}
Matthias Braun's avatar
Matthias Braun committed
682
	panic("can't write invalid type %+F\n", tp);
683
684
}

Matthias Braun's avatar
Matthias Braun committed
685
static void write_entity(write_env_t *env, ir_entity *ent)
686
{
Matthias Braun's avatar
Matthias Braun committed
687
	ir_type       *type       = get_entity_type(ent);
688
689
690
	ir_type       *owner      = get_entity_owner(ent);
	ir_visibility  visibility = get_entity_visibility(ent);
	ir_linkage     linkage    = get_entity_linkage(ent);
691

Matthias Braun's avatar
Matthias Braun committed
692
	if (entity_visited(ent))
693
		return;
Matthias Braun's avatar
Matthias Braun committed
694
	mark_entity_visited(ent);
695

Matthias Braun's avatar
Matthias Braun committed
696
697
698
699
700
	write_type(env, type);
	write_type(env, owner);

	fputc('\t', env->file);
	switch ((ir_entity_kind)ent->entity_kind) {
701
	case IR_ENTITY_ALIAS:           write_symbol(env, "alias");           break;
Matthias Braun's avatar
Matthias Braun committed
702
	case IR_ENTITY_GOTENTRY:        write_symbol(env, "gotentry");        break;
Matthias Braun's avatar
Matthias Braun committed
703
704
705
706
707
	case IR_ENTITY_NORMAL:          write_symbol(env, "entity");          break;
	case IR_ENTITY_METHOD:          write_symbol(env, "method");          break;
	case IR_ENTITY_LABEL:           write_symbol(env, "label");           break;
	case IR_ENTITY_COMPOUND_MEMBER: write_symbol(env, "compound_member"); break;
	case IR_ENTITY_PARAMETER:       write_symbol(env, "parameter");       break;
708
709
710
711
	case IR_ENTITY_UNKNOWN:
		write_symbol(env, "unknown");
		write_long(env, get_entity_nr(ent));
		return;
712
	}
Matthias Braun's avatar
Matthias Braun committed
713
	write_long(env, get_entity_nr(ent));
714

Matthias Braun's avatar
Matthias Braun committed
715
716
717
718
719
720
721
722
	if (ent->entity_kind != IR_ENTITY_LABEL
	    && ent->entity_kind != IR_ENTITY_PARAMETER) {
		write_ident_null(env, get_entity_ident(ent));
		if (!entity_has_ld_ident(ent)) {
			write_ident_null(env, NULL);
		} else {
			write_ident_null(env, get_entity_ld_ident(ent));
		}
723
	}
Matthias Braun's avatar
Matthias Braun committed
724
725
726

	write_visibility(env, visibility);
	write_list_begin(env);
727
	if (linkage & IR_LINKAGE_CONSTANT)
Matthias Braun's avatar
Matthias Braun committed
728
		write_symbol(env, "constant");
729
	if (linkage & IR_LINKAGE_WEAK)
Matthias Braun's avatar
Matthias Braun committed
730
		write_symbol(env, "weak");
731
	if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
Matthias Braun's avatar
Matthias Braun committed
732
		write_symbol(env, "garbage_collect");
733
	if (linkage & IR_LINKAGE_MERGE)
Matthias Braun's avatar
Matthias Braun committed
734
		write_symbol(env, "merge");
735
	if (linkage & IR_LINKAGE_HIDDEN_USER)
Matthias Braun's avatar
Matthias Braun committed
736
737
738
739
740
741
742
743
744
745
		write_symbol(env, "hidden_user");
	write_list_end(env);

	write_type_ref(env, type);
	if (ent->entity_kind != IR_ENTITY_LABEL)
		write_type_ref(env, owner);
	write_long(env, is_entity_compiler_generated(ent));
	write_volatility(env, get_entity_volatility(ent));

	switch ((ir_entity_kind)ent->entity_kind) {
746
747
748
	case IR_ENTITY_ALIAS:
		write_entity_ref(env, get_entity_alias(ent));
		break;
Matthias Braun's avatar
Matthias Braun committed
749
750
751
	case IR_ENTITY_GOTENTRY:
		write_entity_ref(env, ent->attr.got.referenced);
		break;
Matthias Braun's avatar
Matthias Braun committed
752
753
754
755
756
757
	case IR_ENTITY_NORMAL:
		if (ent->initializer != NULL) {
			write_symbol(env, "initializer");
			write_initializer(env, get_entity_initializer(ent));
		} else {
			write_symbol(env, "none");
758
		}
Matthias Braun's avatar
Matthias Braun committed
759
760
761
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		write_long(env, get_entity_offset(ent));
762
763
		write_unsigned(env, get_entity_bitfield_offset(ent));
		write_unsigned(env, get_entity_bitfield_size(ent));
Matthias Braun's avatar
Matthias Braun committed
764
765
766
767
768
769
770
771
		break;
	case IR_ENTITY_PARAMETER: {
		size_t num = get_entity_parameter_number(ent);
		if (num == IR_VA_START_PARAMETER_NUMBER) {
			write_symbol(env, "va_start");
		} else {
			write_size_t(env, num);
		}
772
773
774
		write_long(env, get_entity_offset(ent));
		write_unsigned(env, get_entity_bitfield_offset(ent));
		write_unsigned(env, get_entity_bitfield_size(ent));
Matthias Braun's avatar
Matthias Braun committed
775
776
		break;
	}
777
778
779
	case IR_ENTITY_METHOD:
		write_long(env, (long)get_entity_additional_properties(ent));
		break;
780
	case IR_ENTITY_UNKNOWN:
Matthias Braun's avatar
Matthias Braun committed
781
782
	case IR_ENTITY_LABEL:
		break;
783
784
785
	}

	fputc('\n', env->file);
786
787
}

Matthias Braun's avatar
Matthias Braun committed
788
static void write_switch_table(write_env_t *env, const ir_switch_table *table)
789
{
Matthias Braun's avatar
Matthias Braun committed
790
791
792
793
794
795
796
797
798
799
800
801
	size_t n_entries = ir_switch_table_get_n_entries(table);
	size_t i;

	write_size_t(env, n_entries);
	for (i = 0; i < n_entries; ++i) {
		long       pn  = ir_switch_table_get_pn(table, i);
		ir_tarval *min = ir_switch_table_get_min(table, i);
		ir_tarval *max = ir_switch_table_get_max(table, i);
		write_long(env, pn);
		write_tarval(env, min);
		write_tarval(env, max);
	}
802
803
}

Matthias Braun's avatar
Matthias Braun committed
804
static void write_pred_refs(write_env_t *env, const ir_node *node, int from)
805
{
Matthias Braun's avatar
Matthias Braun committed
806
807
808
809
810
811
812
	int arity = get_irn_arity(node);
	int i;
	write_list_begin(env);
	assert(from <= arity);
	for (i = from; i < arity; ++i) {
		ir_node *pred = get_irn_n(node, i);
		write_node_ref(env, pred);
813
	}
Matthias Braun's avatar
Matthias Braun committed
814
	write_list_end(env);
815
816
}

Matthias Braun's avatar
Matthias Braun committed
817
818
819
820
821
822
static void write_node_nr(write_env_t *env, const ir_node *node)
{
	write_long(env, get_irn_node_nr(node));
}

static void write_ASM(write_env_t *env, const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
823
{
Matthias Braun's avatar
Matthias Braun committed
824
825
826
	write_symbol(env, "ASM");
	write_node_nr(env, node);
	write_node_nr(env, get_nodes_block(node));
827
	write_node_nr(env, get_ASM_mem(node));
Matthias Braun's avatar
Matthias Braun committed
828
829
830

	write_ident(env, get_ASM_text(node));
	write_list_begin(env);
831
832
833
	ir_asm_constraint *input_constraints = get_ASM_input_constraints(node);
	int                n_inputs          = get_ASM_n_inputs(node);
	for (int i = 0; i < n_inputs; ++i) {
Matthias Braun's avatar
Matthias Braun committed
834
835
836
		const ir_asm_constraint *constraint = &input_constraints[i];
		write_unsigned(env, constraint->pos);
		write_ident(env, constraint->constraint);
Matthias Braun's avatar
Matthias Braun committed
837
		write_mode_ref(env, constraint->mode);
Matthias Braun's avatar
Matthias Braun committed
838
839
840
841
	}
	write_list_end(env);

	write_list_begin(env);
842
843
844
	ir_asm_constraint *output_constraints  = get_ASM_output_constraints(node);
	size_t            n_output_constraints = get_ASM_n_output_constraints(node);
	for (size_t i = 0; i < n_output_constraints; ++i) {
Matthias Braun's avatar
Matthias Braun committed
845
846
847
		const ir_asm_constraint *constraint = &output_constraints[i];
		write_unsigned(env, constraint->pos);
		write_ident(env, constraint->constraint);
Matthias Braun's avatar
Matthias Braun committed
848
		write_mode_ref(env, constraint->mode);
Matthias Braun's avatar
Matthias Braun committed
849
850
851
852
	}
	write_list_end(env);

	write_list_begin(env);
853
854
855
	ident **clobbers   = get_ASM_clobbers(node);
	size_t  n_clobbers = get_ASM_n_clobbers(node);
	for (size_t i = 0; i < n_clobbers; ++i) {
Matthias Braun's avatar
Matthias Braun committed
856
857
858
859
		ident *clobber = clobbers[i];
		write_ident(env, clobber);
	}
	write_list_end(env);
Matthias Braun's avatar
Matthias Braun committed
860
861

	write_pin_state(env, get_irn_pinned(node));
Matthias Braun's avatar
Matthias Braun committed
862
	write_pred_refs(env, node, n_ASM_max+1);
Matthias Braun's avatar
Matthias Braun committed
863
864
}

Matthias Braun's avatar
Matthias Braun committed
865
static void write_Phi(write_env_t *env, const ir_node *node)
866
{
Matthias Braun's avatar
Matthias Braun committed
867
868
869
870
871
872
	write_symbol(env, "Phi");
	write_node_nr(env, node);
	write_node_ref(env, get_nodes_block(node));
	write_mode_ref(env, get_irn_mode(node));
	write_pred_refs(env, node, 0);
}
873

Matthias Braun's avatar
Matthias Braun committed
874
875
876
static void write_Block(write_env_t *env, const ir_node *node)
{
	ir_entity *entity = get_Block_entity(node);
877

Matthias Braun's avatar
Matthias Braun committed
878
879
880
881
882
883
884
	if (entity != NULL) {
		write_symbol(env, "BlockL");
		write_node_nr(env, node);
		write_entity_ref(env, entity);
	} else {
		write_symbol(env, "Block");
		write_node_nr(env, node);
885
	}
Matthias Braun's avatar
Matthias Braun committed
886
887
	write_pred_refs(env, node, 0);
}
888

Matthias Braun's avatar
Matthias Braun committed
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
static void write_Anchor(write_env_t *env, const ir_node *node)
{
	write_symbol(env, "Anchor");
	write_node_nr(env, node);
	write_pred_refs(env, node, 0);
}

static void write_SymConst(write_env_t *env, const ir_node *node)
{
	/* TODO: only symconst_addr_ent implemented yet */
	if (get_SymConst_kind(node) != symconst_addr_ent)
		panic("Can't export %+F (only symconst_addr_ent supported)", node);

	write_symbol(env, "SymConst");
	write_node_nr(env, node);
	write_mode_ref(env, get_irn_mode(node));
	write_entity_ref(env, get_SymConst_entity(node));
}

908
typedef void write_node_func(write_env_t *env, ir_node const *node);
Matthias Braun's avatar
Matthias Braun committed
909

910
static void register_node_writer(ir_op *op, write_node_func *func)
Matthias Braun's avatar
Matthias Braun committed
911
{
912
	set_generic_function_ptr(op, func);
Matthias Braun's avatar
Matthias Braun committed
913
914
915
916
}

static void writers_init(void)
{
Matthias Braun's avatar
Matthias Braun committed
917
	ir_clear_opcodes_generic_func();
Matthias Braun's avatar
Matthias Braun committed
918
919
920
921
922
923
924
925
926
927
	register_node_writer(op_Anchor,   write_Anchor);
	register_node_writer(op_ASM,      write_ASM);
	register_node_writer(op_Block,    write_Block);
	register_node_writer(op_Phi,      write_Phi);
	register_node_writer(op_SymConst, write_SymConst);
	register_generated_node_writers();
}

static void write_node(const ir_node *node, write_env_t *env)
{
928
929
	ir_op           *const op   = get_irn_op(node);
	write_node_func *const func = get_generic_function_ptr(write_node_func, op);
Matthias Braun's avatar
Matthias Braun committed
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946

	fputc('\t', env->file);
	if (func == NULL)
		panic("No write_node_func for %+F", node);
	func(env, node);
	fputc('\n', env->file);
}

static void write_node_recursive(ir_node *node, write_env_t *env);

static void write_preds(ir_node *node, write_env_t *env)
{
	int arity = get_irn_arity(node);
	int i;
	for (i = 0; i < arity; ++i) {
		ir_node *pred = get_irn_n(node, i);
		write_node_recursive(pred, env);
947
	}
Matthias Braun's avatar
Matthias Braun committed
948
}
949

Matthias Braun's avatar
Matthias Braun committed
950
951
952
953
954
955
956
957
958
959
960
/**
 * Recursively write nodes.
 * The reader expects nodes in a way that except for block/phi/anchor nodes
 * all predecessors are already defined when we reach them. So usually we
 * recurse to all our predecessors except for block/phi/anchor nodes where
 * we put the predecessors into a queue for later processing.
 */
static void write_node_recursive(ir_node *node, write_env_t *env)
{
	if (irn_visited_else_mark(node))
		return;
961

Matthias Braun's avatar
Matthias Braun committed
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
	if (!is_Block(node)) {
		write_node_recursive(get_nodes_block(node), env);
	}
	/* write predecessors */
	if (!is_Phi(node) && !is_Block(node) && !is_Anchor(node)) {
		write_preds(node, env);
	} else {
		int arity = get_irn_arity(node);
		int i;
		for (i = 0; i < arity; ++i) {
			ir_node *pred = get_irn_n(node, i);
			pdeq_putr(env->write_queue, pred);
		}
	}
	write_node(node, env);
}

static void write_mode(write_env_t *env, ir_mode *mode)
{
	if (mode_is_int(mode)) {
		write_symbol(env, "int_mode");
		write_string(env, get_mode_name(mode));
		write_mode_arithmetic(env, get_mode_arithmetic(mode));
		write_unsigned(env, get_mode_size_bits(mode));
		write_int(env, get_mode_sign(mode));
		write_unsigned(env, get_mode_modulo_shift(mode));
	} else if (mode_is_reference(mode)) {
		write_symbol(env, "reference_mode");
		write_string(env, get_mode_name(mode));
		write_mode_arithmetic(env, get_mode_arithmetic(mode));
		write_unsigned(env, get_mode_size_bits(mode));
		write_unsigned(env, get_mode_modulo_shift(mode));

		write_mode_ref(env, get_reference_mode_signed_eq(mode));
		write_mode_ref(env, get_reference_mode_unsigned_eq(mode));
		write_int(env, (mode == mode_P ? 1 : 0));
	} else if (mode_is_float(mode)) {
		write_symbol(env, "float_mode");
		write_string(env, get_mode_name(mode));
		write_mode_arithmetic(env, get_mode_arithmetic(mode));
		write_unsigned(env, get_mode_exponent_size(mode));
		write_unsigned(env, get_mode_mantissa_size(mode));
1004
		write_unsigned(env, get_mode_float_int_overflow(mode));
Matthias Braun's avatar
Matthias Braun committed
1005
1006
	} else {
		panic("Can't write internal modes");
1007
	}
1008
1009
}

Matthias Braun's avatar
Matthias Braun committed
1010
static void write_modes(write_env_t *env)
1011
{
Matthias Braun's avatar
Matthias Braun committed
1012
1013
	size_t n_modes = ir_get_n_modes();
	size_t i;
1014

Matthias Braun's avatar
Matthias Braun committed
1015
1016
	write_symbol(env, "modes");
	fputs("{\n", env->file);
1017

Michael Beck's avatar
Michael Beck committed
1018
	for (i = 0; i < n_modes; i++) {
Matthias Braun's avatar
Matthias Braun committed
1019
		ir_mode *mode = ir_get_mode(i);
Matthias Braun's avatar
Matthias Braun committed
1020
1021
1022
1023
		if (!mode_is_int(mode) && !mode_is_reference(mode)
		    && !mode_is_float(mode)) {
		    /* skip internal modes */
		    continue;
1024
		}
Matthias Braun's avatar
Matthias Braun committed
1025
1026
		fputc('\t', env->file);
		write_mode(env, mode);
1027
1028
1029
		fputc('\n', env->file);
	}

Matthias Braun's avatar
Matthias Braun committed
1030
	fputs("}\n\n", env->file);
1031
1032
}

Matthias Braun's avatar
Matthias Braun committed
1033
static void write_program(write_env_t *env)
1034
{
Matthias Braun's avatar
Matthias Braun committed
1035
1036
1037
	ir_segment_t s;
	size_t n_asms = get_irp_n_asms();
	size_t i;
1038

Matthias Braun's avatar
Matthias Braun committed
1039
1040
	write_symbol(env, "program");
	write_scope_begin(env);
1041
	if (irp_prog_name_is_set()) {
Matthias Braun's avatar
Matthias Braun committed
1042
1043
		fputc('\t', env->file);
		write_symbol(env, "name");
Matthias Braun's avatar
Matthias Braun committed
1044
		write_string(env, get_irp_name());
Matthias Braun's avatar
Matthias Braun committed
1045
		fputc('\n', env->file);
1046
1047
	}

1048
	for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1049
		ir_type *segment_type = get_segment_type(s);
Matthias Braun's avatar
Matthias Braun committed
1050
1051
1052
		fputc('\t', env->file);
		write_symbol(env, "segment_type");
		write_symbol(env, get_segment_name(s));
1053
		if (segment_type == NULL) {
Matthias Braun's avatar
Matthias Braun committed
1054
			write_symbol(env, "NULL");
1055
		} else {
Matthias Braun's avatar
Matthias Braun committed
1056
			write_type_ref(env, segment_type);
1057
		}
Matthias Braun's avatar
Matthias Braun committed
1058
1059
1060
1061
1062
1063
1064
1065
1066
		fputc('\n', env->file);
	}

	for (i = 0; i < n_asms; ++i) {
		ident *asm_text = get_irp_asm(i);
		fputc('\t', env->file);
		write_symbol(env, "asm");
		write_ident(env, asm_text);
		fputc('\n', env->file);
1067
	}
Matthias Braun's avatar
Matthias Braun committed
1068
	write_scope_end(env);
1069
}
1070

Matthias Braun's avatar
Matthias Braun committed
1071
int ir_export(const char *filename)
1072
1073
{
	FILE *file = fopen(filename, "wt");
Matthias Braun's avatar
Matthias Braun committed
1074
	int   res  = 0;
1075
	if (file == NULL) {
1076
		perror(filename);
Matthias Braun's avatar
Matthias Braun committed
1077
		return 1;
1078
1079
	}

Matthias Braun's avatar
Matthias Braun committed
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
	ir_export_file(file);
	res = ferror(file);
	fclose(file);
	return res;
}

static void write_node_cb(ir_node *node, void *ctx)
{
	write_env_t *env = (write_env_t*)ctx;
	write_node(node, env);
}

static void write_typegraph(write_env_t *env)
{
	size_t n_types = get_irp_n_types();
	size_t i;

	write_symbol(env, "typegraph");
	write_scope_begin(env);
	irp_reserve_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	inc_master_type_visited();
	for (i = 0; i < n_types; ++i) {
		ir_type *type = get_irp_type(i);
		write_type(env, type);
	}
1105
1106
1107
1108
1109
1110

	while (!pdeq_empty(env->entity_queue)) {
		ir_entity *entity = pdeq_getl(env->entity_queue);
		write_entity(env, entity);
	}

Matthias Braun's avatar
Matthias Braun committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
	irp_free_resources(irp, IRP_RESOURCE_TYPE_VISITED);
	write_scope_end(env);
}

static void write_irg(write_env_t *env, ir_graph *irg)
{
	write_symbol(env, "irg");
	write_entity_ref(env, get_irg_entity(irg));
	write_type_ref(env, get_irg_frame_type(irg));
	write_scope_begin(env);
	ir_reserve_resources(irg, IR_RESOURCE_IRN_VISITED);
	inc_irg_visited(irg);
	assert(pdeq_empty(env->write_queue));
	pdeq_putr(env->write_queue, irg->anchor);
	do {
		ir_node *node = (ir_node*) pdeq_getl(env->write_queue);
		write_node_recursive(node, env);
	} while (!pdeq_empty(env->write_queue));
	ir_free_resources(irg, IR_RESOURCE_IRN_VISITED);
	write_scope_end(env);
1131
}
1132

1133
/* Exports the whole irp to the given file in a textual form. */
Matthias Braun's avatar
Matthias Braun committed
1134
void ir_export_file(FILE *file)
1135
{
Matthias Braun's avatar
Matthias Braun committed
1136
1137
	write_env_t my_env;
	write_env_t *env = &my_env;
Michael Beck's avatar
Michael Beck committed
1138
	size_t i, n_irgs = get_irp_n_irgs();