irio.c 61.9 KB
Newer Older
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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.
Matthias Braun's avatar
Matthias Braun committed
23
 * @author  Moritz Kroll, Matthias Braun
24
25
26
27
 */
#include "config.h"

#include <string.h>
28
29
30
#include <ctype.h>
#include <stdbool.h>
#include <stdarg.h>
31
32
33

#include "irio.h"

34
#include "irnode_t.h"
35
36
#include "irprog.h"
#include "irgraph_t.h"
Michael Beck's avatar
Michael Beck committed
37
#include "irprintf.h"
Matthias Braun's avatar
Matthias Braun committed
38
#include "ircons_t.h"
39
40
41
42
43
44
#include "irgmod.h"
#include "irflag_t.h"
#include "irgwalk.h"
#include "tv.h"
#include "array.h"
#include "error.h"
45
#include "typerep.h"
Matthias Braun's avatar
Matthias Braun committed
46
47
48
49
#include "set.h"
#include "obst.h"
#include "pmap.h"
#include "pdeq.h"
50

51
#define SYMERROR ((unsigned) ~0)
52

Matthias Braun's avatar
Matthias Braun committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
78
	struct obstack obst;
Matthias Braun's avatar
Matthias Braun committed
79
80
81
82
	struct obstack preds_obst;
	delayed_initializer_t *delayed_initializers;
	const delayed_pred_t **delayed_preds;
} read_env_t;
83

Matthias Braun's avatar
Matthias Braun committed
84
85
86
87
88
89
typedef struct write_env_t {
	FILE *file;
	pdeq *write_queue;
} write_env_t;

typedef enum typetag_t {
90
	tt_align,
Matthias Braun's avatar
Matthias Braun committed
91
	tt_builtin_kind,
92
	tt_cond_jmp_predicate,
93
	tt_initializer,
Matthias Braun's avatar
Matthias Braun committed
94
	tt_irg_inline_property,
95
	tt_keyword,
Matthias Braun's avatar
Matthias Braun committed
96
	tt_linkage,
97
	tt_mode_arithmetic,
98
	tt_pin_state,
Matthias Braun's avatar
Matthias Braun committed
99
100
	tt_segment,
	tt_throws,
101
	tt_tpo,
102
	tt_type_state,
Matthias Braun's avatar
Matthias Braun committed
103
	tt_visibility,
104
	tt_volatility,
Matthias Braun's avatar
Matthias Braun committed
105
	tt_where_alloc,
106
107
} typetag_t;

Matthias Braun's avatar
Matthias Braun committed
108
109
110
typedef enum keyword_t {
	kw_asm,
	kw_compound_member,
111
112
	kw_constirg,
	kw_entity,
113
	kw_float_mode,
Matthias Braun's avatar
Matthias Braun committed
114
115
116
117
	kw_int_mode,
	kw_irg,
	kw_label,
	kw_method,
118
	kw_modes,
Matthias Braun's avatar
Matthias Braun committed
119
120
121
122
	kw_parameter,
	kw_program,
	kw_reference_mode,
	kw_segment_type,
123
	kw_type,
124
	kw_typegraph,
125
	kw_unknown,
126
127
} keyword_t;

Matthias Braun's avatar
Matthias Braun committed
128
typedef struct symbol_t {
129
130
131
132
	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;
133

Matthias Braun's avatar
Matthias Braun committed
134
typedef struct id_entry {
135
136
137
138
	long id;
	void *elem;
} id_entry;

139
140
/** The symbol table, a set of symbol_t elements. */
static set *symtbl;
141

142
143
144
145
/**
 * Compare two symbol table entries.
 */
static int symbol_cmp(const void *elt, const void *key, size_t size)
146
{
147
	int res;
148
149
	const symbol_t *entry = (const symbol_t *) elt;
	const symbol_t *keyentry = (const symbol_t *) key;
150
	(void) size;
151
	res = entry->typetag - keyentry->typetag;
Michael Beck's avatar
Michael Beck committed
152
	if (res) return res;
153
154
155
156
157
158
159
160
161
162
163
	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;
}

164
static void __attribute__((format(printf, 2, 3)))
Matthias Braun's avatar
Matthias Braun committed
165
parse_error(read_env_t *env, const char *fmt, ...)
166
{
Matthias Braun's avatar
Matthias Braun committed
167
168
	va_list  ap;
	unsigned line = env->line;
169
170
171
172
173
174
175

	/* 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
176
177
178
179
180
	fprintf(stderr, "%s:%u: error ", env->inputname, line);
	env->read_errors = true;

	/* let's hope firm doesn't die on further errors */
	do_node_verification(0);
181
182
183
184
185
186

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);
}

187
188
/** Initializes the symbol table. May be called more than once without problems. */
static void symtbl_init(void)
189
{
190
	symbol_t key;
191
192

	/* Only initialize once */
Michael Beck's avatar
Michael Beck committed
193
	if (symtbl != NULL)
194
		return;
195

196
	symtbl = new_set(symbol_cmp, 256);
197

198
#define INSERT(tt, s, cod)                                       \
199
200
201
	key.str = (s);                                               \
	key.typetag = (tt);                                          \
	key.code = (cod);                                            \
202
	set_insert(symtbl, &key, sizeof(key), hash_str(s) + tt * 17)
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219

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

Matthias Braun's avatar
Matthias Braun committed
221
222
223
224
225
226
	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);

227
228
229
	INSERT(tt_visibility, "local", ir_visibility_local);
	INSERT(tt_visibility, "external", ir_visibility_external);
	INSERT(tt_visibility, "default", ir_visibility_default);
230
	INSERT(tt_visibility, "private", ir_visibility_private);
231

Matthias Braun's avatar
Matthias Braun committed
232
233
234
235
236
	INSERT(tt_throws, "throw",   true);
	INSERT(tt_throws, "nothrow", false);

	INSERTKEYWORD(asm);
	INSERTKEYWORD(compound_member);
237
238
	INSERTKEYWORD(constirg);
	INSERTKEYWORD(entity);
239
	INSERTKEYWORD(float_mode);
Matthias Braun's avatar
Matthias Braun committed
240
241
242
243
	INSERTKEYWORD(int_mode);
	INSERTKEYWORD(irg);
	INSERTKEYWORD(label);
	INSERTKEYWORD(method);
244
	INSERTKEYWORD(modes);
Matthias Braun's avatar
Matthias Braun committed
245
	INSERTKEYWORD(parameter);
246
	INSERTKEYWORD(program);
Matthias Braun's avatar
Matthias Braun committed
247
	INSERTKEYWORD(reference_mode);
248
	INSERTKEYWORD(segment_type);
Matthias Braun's avatar
Matthias Braun committed
249
250
	INSERTKEYWORD(type);
	INSERTKEYWORD(typegraph);
251
	INSERTKEYWORD(unknown);
252
253
254
255

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

Matthias Braun's avatar
Matthias Braun committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	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);
270
271
272
273
274
275
276
277
278
279

	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
280
281
282
283
284
285
286
287
288
289
290
	INSERT(tt_mode_arithmetic, "uninitialized",      irma_uninitialized);
	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);

	INSERT(tt_irg_inline_property, "any",            irg_inline_any);
	INSERT(tt_irg_inline_property, "recommended",    irg_inline_recomended);
	INSERT(tt_irg_inline_property, "forbidden",      irg_inline_forbidden);
	INSERT(tt_irg_inline_property, "forced",         irg_inline_forced);
	INSERT(tt_irg_inline_property, "forced_no_body", irg_inline_forced_no_body);
291

292
293
294
295
296
297
298
299
300
301
302
	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_volatility, volatility_non_volatile);
	INSERTENUM(tt_volatility, volatility_is_volatile);

Matthias Braun's avatar
Matthias Braun committed
303
304
305
	INSERTENUM(tt_where_alloc, stack_alloc);
	INSERTENUM(tt_where_alloc, heap_alloc);

306
#undef INSERTKEYWORD
307
308
309
310
#undef INSERTENUM
#undef INSERT
}

311
312
313
314
315
316
317
318
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
319
	panic("INVALID_SEGMENT");
320
321
}

322
323
324
325
326
327
328
329
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_default:  return "default";
	case ir_visibility_private:  return "private";
	}
Matthias Braun's avatar
Matthias Braun committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	panic("INVALID_VISIBILITY");
}

static const char *get_mode_arithmetic_name(ir_mode_arithmetic arithmetic)
{
	switch (arithmetic) {
	case irma_uninitialized:      return "uninitialized";
	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");
}

static const char *get_irg_inline_property_name(irg_inline_property prop)
{
	switch (prop) {
	case irg_inline_any:            return "any";
	case irg_inline_recomended:     return "recommended";
	case irg_inline_forbidden:      return "forbidden";
	case irg_inline_forced:         return "forced";
	case irg_inline_forced_no_body: return "forced_no_body";
	}
	panic("invalid irg_inline_property");
355
356
}

357
358
/** 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)
359
{
360
	symbol_t key, *entry;
361
362

	key.str = str;
363
	key.typetag = typetag;
364

365
	entry = (symbol_t*)set_find(symtbl, &key, sizeof(key), hash_str(str) + typetag * 17);
366
	return entry ? entry->code : SYMERROR;
367
368
}

Matthias Braun's avatar
Matthias Braun committed
369
static void write_long(write_env_t *env, long value)
370
{
Matthias Braun's avatar
Matthias Braun committed
371
	fprintf(env->file, "%ld ", value);
372
373
}

Matthias Braun's avatar
Matthias Braun committed
374
static void write_int(write_env_t *env, int value)
375
{
Matthias Braun's avatar
Matthias Braun committed
376
	fprintf(env->file, "%d ", value);
377
378
}

Matthias Braun's avatar
Matthias Braun committed
379
static void write_unsigned(write_env_t *env, unsigned value)
380
{
Matthias Braun's avatar
Matthias Braun committed
381
	fprintf(env->file, "%u ", value);
382
383
}

Matthias Braun's avatar
Matthias Braun committed
384
static void write_size_t(write_env_t *env, size_t value)
385
{
Matthias Braun's avatar
Matthias Braun committed
386
	ir_fprintf(env->file, "%zu ", value);
387
388
}

Matthias Braun's avatar
Matthias Braun committed
389
static void write_symbol(write_env_t *env, const char *symbol)
390
{
Matthias Braun's avatar
Matthias Braun committed
391
392
	fputs(symbol, env->file);
	fputc(' ', env->file);
393
394
}

Matthias Braun's avatar
Matthias Braun committed
395
static void write_entity_ref(write_env_t *env, ir_entity *entity)
396
397
398
399
{
	write_long(env, get_entity_nr(entity));
}

Matthias Braun's avatar
Matthias Braun committed
400
static void write_type_ref(write_env_t *env, ir_type *type)
401
{
402
403
	switch (get_type_tpop_code(type)) {
	case tpo_unknown:
Matthias Braun's avatar
Matthias Braun committed
404
		write_symbol(env, "unknown");
405
406
		return;
	case tpo_none:
Matthias Braun's avatar
Matthias Braun committed
407
		write_symbol(env, "none");
408
409
		return;
	case tpo_code:
Matthias Braun's avatar
Matthias Braun committed
410
		write_symbol(env, "code");
411
412
413
		return;
	default:
		break;
Matthias Braun's avatar
Matthias Braun committed
414
	}
415
	write_long(env, get_type_nr(type));
416
417
}

Matthias Braun's avatar
Matthias Braun committed
418
static void write_string(write_env_t *env, const char *string)
Matthias Braun's avatar
Matthias Braun committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
{
	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
438
	fputc(' ', env->file);
Matthias Braun's avatar
Matthias Braun committed
439
440
}

Matthias Braun's avatar
Matthias Braun committed
441
static void write_ident(write_env_t *env, ident *id)
Matthias Braun's avatar
Matthias Braun committed
442
443
444
445
{
	write_string(env, get_id_str(id));
}

Matthias Braun's avatar
Matthias Braun committed
446
static void write_ident_null(write_env_t *env, ident *id)
Matthias Braun's avatar
Matthias Braun committed
447
448
449
450
451
452
453
454
{
	if (id == NULL) {
		fputs("NULL ", env->file);
	} else {
		write_ident(env, id);
	}
}

Matthias Braun's avatar
Matthias Braun committed
455
static void write_mode_ref(write_env_t *env, ir_mode *mode)
456
{
457
	write_string(env, get_mode_name(mode));
458
459
}

Matthias Braun's avatar
Matthias Braun committed
460
static void write_tarval(write_env_t *env, ir_tarval *tv)
461
{
Matthias Braun's avatar
Matthias Braun committed
462
463
464
465
466
467
468
469
470
	write_mode_ref(env, get_tarval_mode(tv));
	if (tv == tarval_bad) {
		write_symbol(env, "bad");
	} else {
		char buf[1024];
		tarval_snprintf(buf, sizeof(buf), tv);
		fputs(buf, env->file);
		fputc(' ', env->file);
	}
471
472
}

Matthias Braun's avatar
Matthias Braun committed
473
static void write_align(write_env_t *env, ir_align align)
474
475
476
477
478
{
	fputs(get_align_name(align), env->file);
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
479
static void write_builtin_kind(write_env_t *env, const ir_node *node)
480
{
Matthias Braun's avatar
Matthias Braun committed
481
	fputs(get_builtin_kind_name(get_Builtin_kind(node)), env->file);
482
483
484
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
485
static void write_cond_jmp_predicate(write_env_t *env, const ir_node *node)
486
{
Matthias Braun's avatar
Matthias Braun committed
487
	fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(node)), env->file);
488
489
490
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
static void write_relation(write_env_t *env, ir_relation relation)
{
	write_long(env, (long)relation);
}

static void write_where_alloc(write_env_t *env, ir_where_alloc where_alloc)
{
	switch (where_alloc) {
	case stack_alloc: write_symbol(env, "stack_alloc"); return;
	case heap_alloc:  write_symbol(env, "heap_alloc");  return;
	}
	panic("invalid where_alloc value");
}

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)
511
512
513
514
{
	fputs("[", env->file);
}

Matthias Braun's avatar
Matthias Braun committed
515
static void write_list_end(write_env_t *env)
516
517
518
519
{
	fputs("] ", env->file);
}

Matthias Braun's avatar
Matthias Braun committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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)
536
537
538
{
	FILE *f = env->file;
	ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
539

540
541
542
	fputs(get_initializer_kind_name(ini_kind), f);
	fputc(' ', f);

543
544
	switch (ini_kind) {
	case IR_INITIALIZER_CONST:
Matthias Braun's avatar
Matthias Braun committed
545
546
		write_node_ref(env, get_initializer_const_value(ini));
		return;
547

548
549
	case IR_INITIALIZER_TARVAL:
		write_tarval(env, get_initializer_tarval_value(ini));
Matthias Braun's avatar
Matthias Braun committed
550
		return;
551

552
	case IR_INITIALIZER_NULL:
Matthias Braun's avatar
Matthias Braun committed
553
		return;
554

555
	case IR_INITIALIZER_COMPOUND: {
Michael Beck's avatar
Michael Beck committed
556
		size_t i, n = get_initializer_compound_n_entries(ini);
Matthias Braun's avatar
Matthias Braun committed
557
		write_size_t(env, n);
Michael Beck's avatar
Michael Beck committed
558
		for (i = 0; i < n; ++i)
559
			write_initializer(env, get_initializer_compound_value(ini, i));
Matthias Braun's avatar
Matthias Braun committed
560
		return;
561
	}
562
	}
Matthias Braun's avatar
Matthias Braun committed
563
	panic("Unknown initializer kind");
564
565
}

Matthias Braun's avatar
Matthias Braun committed
566
static void write_pin_state(write_env_t *env, op_pin_state state)
567
{
Matthias Braun's avatar
Matthias Braun committed
568
	fputs(get_op_pin_state_name(state), env->file);
569
570
571
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
572
static void write_volatility(write_env_t *env, ir_volatility vol)
573
574
575
576
577
{
	fputs(get_volatility_name(vol), env->file);
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
578
static void write_inline_property(write_env_t *env, irg_inline_property prop)
579
{
Matthias Braun's avatar
Matthias Braun committed
580
581
	fputs(get_irg_inline_property_name(prop), env->file);
	fputc(' ', env->file);
582
583
}

Matthias Braun's avatar
Matthias Braun committed
584
static void write_type_state(write_env_t *env, ir_type_state state)
585
{
Matthias Braun's avatar
Matthias Braun committed
586
587
588
	fputs(get_type_state_name(state), env->file);
	fputc(' ', env->file);
}
589

Matthias Braun's avatar
Matthias Braun committed
590
591
592
593
594
static void write_visibility(write_env_t *env, ir_visibility visibility)
{
	fputs(get_visibility_name(visibility), env->file);
	fputc(' ', env->file);
}
595

Matthias Braun's avatar
Matthias Braun committed
596
597
598
599
600
static void write_mode_arithmetic(write_env_t *env, ir_mode_arithmetic arithmetic)
{
	fputs(get_mode_arithmetic_name(arithmetic), env->file);
	fputc(' ', env->file);
}
601

Matthias Braun's avatar
Matthias Braun committed
602
603
static void write_type(write_env_t *env, ir_type *tp);
static void write_entity(write_env_t *env, ir_entity *entity);
604

Matthias Braun's avatar
Matthias Braun committed
605
606
607
608
609
610
611
612
613
614
615
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);
}
616

Matthias Braun's avatar
Matthias Braun committed
617
618
619
static void write_type_primitive(write_env_t *env, ir_type *tp)
{
	ir_type *base_type = get_primitive_base_type(tp);
620

Matthias Braun's avatar
Matthias Braun committed
621
622
	if (base_type != NULL)
		write_type(env, base_type);
623

Matthias Braun's avatar
Matthias Braun committed
624
625
626
	write_type_common(env, tp);
	write_mode_ref(env, get_type_mode(tp));
	if (base_type == NULL)
627
		base_type = get_none_type();
Matthias Braun's avatar
Matthias Braun committed
628
629
	write_type_ref(env, base_type);
	fputc('\n', env->file);
630
631
}

Matthias Braun's avatar
Matthias Braun committed
632
static void write_type_compound(write_env_t *env, ir_type *tp)
633
{
Matthias Braun's avatar
Matthias Braun committed
634
	size_t n_members = get_compound_n_members(tp);
Michael Beck's avatar
Michael Beck committed
635
	size_t i;
636

Matthias Braun's avatar
Matthias Braun committed
637
638
639
640
641
642
643
	if (is_Class_type(tp)) {
		if (get_class_n_subtypes(tp) > 0 || get_class_n_supertypes(tp) > 0
		    || get_class_type_info(tp) != NULL || get_class_vtable_size(tp) > 0) {
			/* sub/superclass export not implemented yet, it's unclear wether
			 * class types will stay in libfirm anyway */
			panic("can't export class types yet");
		}
644
	}
Matthias Braun's avatar
Matthias Braun committed
645
646
647
	write_type_common(env, tp);
	write_ident_null(env, get_compound_ident(tp));
	fputc('\n', env->file);
648

Matthias Braun's avatar
Matthias Braun committed
649
650
651
	for (i = 0; i < n_members; ++i) {
		ir_entity *member = get_compound_member(tp, i);
		write_entity(env, member);
652
	}
Matthias Braun's avatar
Matthias Braun committed
653
}
654

Matthias Braun's avatar
Matthias Braun committed
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
681
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");
682
	}
Matthias Braun's avatar
Matthias Braun committed
683
684
685
686
687
	/* 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);
}
688

Matthias Braun's avatar
Matthias Braun committed
689
690
691
692
693
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;
694

Matthias Braun's avatar
Matthias Braun committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
	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);
}
712

Matthias Braun's avatar
Matthias Braun committed
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
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_enumeration(write_env_t *env, ir_type *tp)
{
	write_type_common(env, tp);
	write_ident_null(env, get_enumeration_ident(tp));
	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_none:
	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_enumeration:  write_type_enumeration(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;
757
	}
Matthias Braun's avatar
Matthias Braun committed
758
	panic("can't write invalid type %+F\n", tp);
759
760
}

Matthias Braun's avatar
Matthias Braun committed
761
static void write_entity(write_env_t *env, ir_entity *ent)
762
{
Matthias Braun's avatar
Matthias Braun committed
763
	ir_type       *type       = get_entity_type(ent);
764
765
766
	ir_type       *owner      = get_entity_owner(ent);
	ir_visibility  visibility = get_entity_visibility(ent);
	ir_linkage     linkage    = get_entity_linkage(ent);
767

Matthias Braun's avatar
Matthias Braun committed
768
	if (entity_visited(ent))
769
		return;
Matthias Braun's avatar
Matthias Braun committed
770
	mark_entity_visited(ent);
771

Matthias Braun's avatar
Matthias Braun committed
772
773
774
775
776
777
778
779
780
781
	write_type(env, type);
	write_type(env, owner);

	fputc('\t', env->file);
	switch ((ir_entity_kind)ent->entity_kind) {
	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;
782
783
784
785
	case IR_ENTITY_UNKNOWN:
		write_symbol(env, "unknown");
		write_long(env, get_entity_nr(ent));
		return;
786
	}
Matthias Braun's avatar
Matthias Braun committed
787
	write_long(env, get_entity_nr(ent));
788

Matthias Braun's avatar
Matthias Braun committed
789
790
791
792
793
794
795
796
	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));
		}
797
	}
Matthias Braun's avatar
Matthias Braun committed
798
799
800

	write_visibility(env, visibility);
	write_list_begin(env);
801
	if (linkage & IR_LINKAGE_CONSTANT)
Matthias Braun's avatar
Matthias Braun committed
802
		write_symbol(env, "constant");
803
	if (linkage & IR_LINKAGE_WEAK)
Matthias Braun's avatar
Matthias Braun committed
804
		write_symbol(env, "weak");
805
	if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
Matthias Braun's avatar
Matthias Braun committed
806
		write_symbol(env, "garbage_collect");
807
	if (linkage & IR_LINKAGE_MERGE)
Matthias Braun's avatar
Matthias Braun committed
808
		write_symbol(env, "merge");
809
	if (linkage & IR_LINKAGE_HIDDEN_USER)
Matthias Braun's avatar
Matthias Braun committed
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
		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) {
	case IR_ENTITY_NORMAL:
		if (ent->initializer != NULL) {
			write_symbol(env, "initializer");
			write_initializer(env, get_entity_initializer(ent));
		} else if (entity_has_compound_ent_values(ent)) {
			/* compound graph API is deprecated */
			panic("exporting compound_graph initializers not supported");
		} else {
			write_symbol(env, "none");
829
		}
Matthias Braun's avatar
Matthias Braun committed
830
831
832
833
834
835
836
837
838
839
840
841
842
843
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		write_long(env, get_entity_offset(ent));
		write_unsigned(env, get_entity_offset_bits_remainder(ent));
		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);
		}
		break;
	}
844
	case IR_ENTITY_UNKNOWN:
Matthias Braun's avatar
Matthias Braun committed
845
846
847
	case IR_ENTITY_LABEL:
	case IR_ENTITY_METHOD:
		break;
848
849
850
	}

	fputc('\n', env->file);
851
852
}

Matthias Braun's avatar
Matthias Braun committed
853
static void write_switch_table(write_env_t *env, const ir_switch_table *table)
854
{
Matthias Braun's avatar
Matthias Braun committed
855
856
857
858
859
860
861
862
863
864
865
866
	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);
	}
867
868
}

Matthias Braun's avatar
Matthias Braun committed
869
static void write_pred_refs(write_env_t *env, const ir_node *node, int from)
870
{
Matthias Braun's avatar
Matthias Braun committed
871
872
873
874
875
876
877
	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);
878
	}
Matthias Braun's avatar
Matthias Braun committed
879
	write_list_end(env);
880
881
}

Matthias Braun's avatar
Matthias Braun committed
882
883
884
885
886
887
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
888
889
890
891
{
	ir_asm_constraint *input_constraints    = get_ASM_input_constraints(node);
	ir_asm_constraint *output_constraints   = get_ASM_output_constraints(node);
	ident            **clobbers             = get_ASM_clobbers(node);
Matthias Braun's avatar
Matthias Braun committed
892
893
894
895
896
897
898
899
	size_t             n_input_constraints  = get_ASM_n_input_constraints(node);
	size_t             n_output_constraints = get_ASM_n_output_constraints(node);
	size_t             n_clobbers           = get_ASM_n_clobbers(node);
	size_t             i;

	write_symbol(env, "ASM");
	write_node_nr(env, node);
	write_node_nr(env, get_nodes_block(node));
Matthias Braun's avatar
Matthias Braun committed
900
901
902
903
904
905
906

	write_ident(env, get_ASM_text(node));
	write_list_begin(env);
	for (i = 0; i < n_input_constraints; ++i) {
		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
907
		write_mode_ref(env, constraint->mode);
Matthias Braun's avatar
Matthias Braun committed
908
909
910
911
912
913
914
915
	}
	write_list_end(env);

	write_list_begin(env);
	for (i = 0; i < n_output_constraints; ++i) {
		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
916
		write_mode_ref(env, constraint->mode);
Matthias Braun's avatar
Matthias Braun committed
917
918
919
920
921
922
923
924
925
	}
	write_list_end(env);

	write_list_begin(env);
	for (i = 0; i < n_clobbers; ++i) {
		ident *clobber = clobbers[i];
		write_ident(env, clobber);
	}
	write_list_end(env);
Matthias Braun's avatar
Matthias Braun committed
926
927
928

	write_pin_state(env, get_irn_pinned(node));
	write_pred_refs(env, node, 0);
Matthias Braun's avatar
Matthias Braun committed
929
930
}

Matthias Braun's avatar
Matthias Braun committed
931
static void write_Phi(write_env_t *env, const ir_node *node)
932
{
Matthias Braun's avatar
Matthias Braun committed
933
934
935
936
937
938
	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);
}
939

Matthias Braun's avatar
Matthias Braun committed
940
941
942
static void write_Block(write_env_t *env, const ir_node *node)
{
	ir_entity *entity = get_Block_entity(node);
943

Matthias Braun's avatar
Matthias Braun committed
944
945
946
947
948
949
950
	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);
951
	}
Matthias Braun's avatar
Matthias Braun committed
952
953
	write_pred_refs(env, node, 0);
}
954

Matthias Braun's avatar
Matthias Braun committed
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
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));
}

typedef void (*write_node_func)(write_env_t *env, const ir_node *node);

static void register_node_writer(ir_op *op, write_node_func func)
{
	set_generic_function_ptr(op, (op_func)func);
}

static void writers_init(void)
{
Matthias Braun's avatar
Matthias Braun committed
983
	ir_clear_opcodes_generic_func();
Matthias Braun's avatar
Matthias Braun committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
	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)
{
	ir_op          *op   = get_irn_op(node);
	write_node_func func = (write_node_func) get_generic_function_ptr(op);

	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);
1013
	}
Matthias Braun's avatar
Matthias Braun committed
1014
}
1015

Matthias Braun's avatar
Matthias Braun committed
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
/**
 * 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;
1027

Matthias Braun's avatar
Matthias Braun committed
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
	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));
	} else {
		panic("Can't write internal modes");
1072
	}
1073
1074
}

Matthias Braun's avatar
Matthias Braun committed
1075
static void write_modes(write_env_t *env)
1076
{
Michael Beck's avatar
Michael Beck committed
1077
	size_t i, n_modes = get_irp_n_modes();
1078

Matthias Braun's avatar
Matthias Braun committed
1079
1080
	write_symbol(env, "modes");
	fputs("{\n", env->file);
1081

Michael Beck's avatar
Michael Beck committed
1082
	for (i = 0; i < n_modes; i++) {
1083
		ir_mode *mode = get_irp_mode(i);
Matthias Braun's avatar
Matthias Braun committed
1084
1085
1086
1087
		if (!mode_is_int(mode) && !mode_is_reference(mode)
		    && !mode_is_float(mode)) {
		    /* skip internal modes */
		    continue;
1088
		}
Matthias Braun's avatar
Matthias Braun committed
1089
1090
		fputc('\t', env->file);
		write_mode(env, mode);
1091
1092
1093
		fputc('\n', env->file);
	}

Matthias Braun's avatar
Matthias Braun committed
1094
	fputs("}\n\n", env->file);
1095
1096
}

Matthias Braun's avatar
Matthias Braun committed
1097
static void write_program(write_env_t *env)
1098
{
Matthias Braun's avatar
Matthias Braun committed
1099
1100
1101
	ir_segment_t s;
	size_t n_asms = get_irp_n_asms();
	size_t i;
1102

Matthias Braun's avatar
Matthias Braun committed
1103
1104
	write_symbol(env, "program");
	write_scope_begin(env);
1105
	if (irp_prog_name_is_set()) {
Matthias Braun's avatar
Matthias Braun committed
1106
1107
		fputc('\t', env->file);
		write_symbol(env, "name");
Matthias Braun's avatar
Matthias Braun committed
1108
		write_string(env, get_irp_name());
Matthias Braun's avatar
Matthias Braun committed
1109
		fputc('\n', env->file);
1110
1111
	}

1112
	for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1113
		ir_type *segment_type = get_segment_type(s);
Matthias Braun's avatar
Matthias Braun committed
1114
1115
1116
		fputc('\t', env->file);
		write_symbol(env, "segment_type");
		write_symbol(env, get_segment_name(s));
1117
		if (segment_type == NULL) {
Matthias Braun's avatar
Matthias Braun committed
1118
			write_symbol(env, "NULL");
1119
		} else {
Matthias Braun's avatar
Matthias Braun committed
1120
			write_type_ref(env, segment_type);
1121
		}
Matthias Braun's avatar
Matthias Braun committed
1122
1123
1124
1125
1126
1127
1128
1129
1130
		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);
1131
	}
Matthias Braun's avatar
Matthias Braun committed
1132
	write_scope_end(env);
1133
}
1134

Matthias Braun's avatar
Matthias Braun committed
1135
int ir_export(const char *filename)
1136
1137
{
	FILE *file = fopen(filename, "wt");
Matthias Braun's avatar
Matthias Braun committed
1138
	int   res  = 0;
1139
	if (file == NULL) {
1140
		perror(filename);
Matthias Braun's avatar
Matthias Braun committed
1141
		return 1;
1142
1143
	}

Matthias Braun's avatar
Matthias Braun committed
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
	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);
	}
	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_inline_property(env, get_irg_inline_property(irg));
	write_unsigned(env, get_irg_additional_properties(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);
1191
}
1192

1193
/* Exports the whole irp to the given file in a textual form. */
Matthias Braun's avatar
Matthias Braun committed
1194
void ir_export_file(FILE *file)
1195
{
Matthias Braun's avatar
Matthias Braun committed
1196
1197
	write_env_t my_env;
	write_env_t *env = &my_env;
Michael Beck's avatar
Michael Beck committed
1198
	size_t i, n_irgs = get_irp_n_irgs();
1199

Matthias Braun's avatar
Matthias Braun committed
1200
1201
1202
	memset(env, 0, sizeof(*env));
	env->file        = file;
	env->write_queue = new_pdeq();
1203

Matthias Braun's avatar
Matthias Braun committed
1204
1205
	writers_init();
	write_modes(env);
1206

Matthias Braun's avatar
Matthias Braun committed
1207
	write_typegraph(env);
1208

Michael Beck's avatar
Michael Beck committed
1209
	for (i = 0; i < n_irgs; i++) {
Matthias Braun's avatar
Matthias Braun committed
1210
1211
		ir_graph *irg = get_irp_irg(i);
		write_irg(env, irg);
1212
1213
	}

Matthias Braun's avatar
Matthias Braun committed
1214
1215
1216
1217
1218
	write_symbol(env, "constirg");
	write_node_ref(env, get_const_code_irg()->current_block);
	write_scope_begin(env);
	walk_const_code(NULL, write_node_cb, env);
	write_scope_end(env);
1219

Matthias Braun's avatar
Matthias Braun committed