irio.c 57.7 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
#include "irprog_t.h"
20
#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
#include "irgmod.h"
#include "irflag_t.h"
#include "irgwalk.h"
26
#include "tv_t.h"
27
#include "array.h"
Matthias Braun's avatar
Matthias Braun committed
28
#include "panic.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_loop,
82
	tt_mode_arithmetic,
83
	tt_pin_state,
Matthias Braun's avatar
Matthias Braun committed
84
85
	tt_segment,
	tt_throws,
86
	tt_tpo,
87
	tt_type_state,
Matthias Braun's avatar
Matthias Braun committed
88
	tt_visibility,
89
	tt_volatility,
90
91
} typetag_t;

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

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

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

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

128
129
130
131
/**
 * Compare two symbol table entries.
 */
static int symbol_cmp(const void *elt, const void *key, size_t size)
132
{
Matthias Braun's avatar
cleanup    
Matthias Braun committed
133
	(void)size;
134
135
	const symbol_t *entry = (const symbol_t *) elt;
	const symbol_t *keyentry = (const symbol_t *) key;
Matthias Braun's avatar
Matthias Braun committed
136
137
138
	int res = entry->typetag - keyentry->typetag;
	if (res != 0)
		return res;
139
140
141
142
143
	return strcmp(entry->str, keyentry->str);
}

static int id_cmp(const void *elt, const void *key, size_t size)
{
Matthias Braun's avatar
cleanup    
Matthias Braun committed
144
	(void)size;
145
146
147
148
149
	const id_entry *entry = (const id_entry *) elt;
	const id_entry *keyentry = (const id_entry *) key;
	return entry->id - keyentry->id;
}

150
static void FIRM_PRINTF(2, 3)
Matthias Braun's avatar
Matthias Braun committed
151
parse_error(read_env_t *env, const char *fmt, ...)
152
153
154
{
	/* workaround read_c "feature" that a '\n' triggers the line++
	 * instead of the character after the '\n' */
Matthias Braun's avatar
Matthias Braun committed
155
	unsigned line = env->line;
156
157
158
159
	if (env->c == '\n') {
		line--;
	}

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

Matthias Braun's avatar
Matthias Braun committed
163
	va_list ap;
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
{
	/* Only initialize once */
Michael Beck's avatar
Michael Beck committed
173
	if (symtbl != NULL)
174
		return;
175

176
	symtbl = new_set(symbol_cmp, 256);
177

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

#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);
Matthias Braun's avatar
Matthias Braun committed
201
	INSERT(tt_segment, "jcr", IR_SEGMENT_JCR);
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
211
	INSERT(tt_loop, "loop",   true);
	INSERT(tt_loop, "noloop", false);

212
213
214
215
216
	INSERT(tt_visibility, "external",           ir_visibility_external);
	INSERT(tt_visibility, "external_private",   ir_visibility_external_private);
	INSERT(tt_visibility, "external_protected", ir_visibility_external_protected);
	INSERT(tt_visibility, "local",              ir_visibility_local);
	INSERT(tt_visibility, "private",            ir_visibility_private);
217

Matthias Braun's avatar
Matthias Braun committed
218
219
220
	INSERT(tt_throws, "throw",   true);
	INSERT(tt_throws, "nothrow", false);

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

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

Matthias Braun's avatar
Matthias Braun committed
244
245
246
247
248
249
250
251
252
253
254
255
256
	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);
257
	INSERTENUM(tt_builtin_kind, ir_bk_saturating_increment);
258
	INSERTENUM(tt_builtin_kind, ir_bk_compare_swap);
259
260
261
262
263
264
265
266
267
268

	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
269
270
271
272
273
	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);

274
275
276
277
278
279
280
281
282
283
	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);

284
#undef INSERTKEYWORD
285
286
287
288
#undef INSERTENUM
#undef INSERT
}

289
290
291
292
293
294
295
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
296
	case IR_SEGMENT_JCR:          return "jcr";
297
	}
298
	panic("invalid segment");
299
300
}

301
302
303
static const char *get_visibility_name(ir_visibility visibility)
{
	switch (visibility) {
304
305
306
307
308
	case ir_visibility_external:           return "external";
	case ir_visibility_external_private:   return "external_private";
	case ir_visibility_external_protected: return "external_protected";
	case ir_visibility_local:              return "local";
	case ir_visibility_private:            return "private";
309
	}
310
	panic("invalid visibility");
Matthias Braun's avatar
Matthias Braun committed
311
312
313
314
315
316
317
318
319
320
321
322
323
}

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

324
325
/** 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)
326
{
Matthias Braun's avatar
Matthias Braun committed
327
328
	symbol_t key;
	key.str     = str;
329
	key.typetag = typetag;
330

Matthias Braun's avatar
Matthias Braun committed
331
332
	symbol_t *entry = set_find(symbol_t, symtbl, &key, sizeof(key),
	                           hash_str(str) + typetag * 17);
333
	return entry ? entry->code : SYMERROR;
334
335
}

Matthias Braun's avatar
Matthias Braun committed
336
static void write_long(write_env_t *env, long value)
337
{
Matthias Braun's avatar
Matthias Braun committed
338
	fprintf(env->file, "%ld ", value);
339
340
}

Matthias Braun's avatar
Matthias Braun committed
341
static void write_int(write_env_t *env, int value)
342
{
Matthias Braun's avatar
Matthias Braun committed
343
	fprintf(env->file, "%d ", value);
344
345
}

Matthias Braun's avatar
Matthias Braun committed
346
static void write_unsigned(write_env_t *env, unsigned value)
347
{
Matthias Braun's avatar
Matthias Braun committed
348
	fprintf(env->file, "%u ", value);
349
350
}

Matthias Braun's avatar
Matthias Braun committed
351
static void write_size_t(write_env_t *env, size_t value)
352
{
Matthias Braun's avatar
Matthias Braun committed
353
	ir_fprintf(env->file, "%zu ", value);
354
355
}

Matthias Braun's avatar
Matthias Braun committed
356
static void write_symbol(write_env_t *env, const char *symbol)
357
{
Matthias Braun's avatar
Matthias Braun committed
358
359
	fputs(symbol, env->file);
	fputc(' ', env->file);
360
361
}

Matthias Braun's avatar
Matthias Braun committed
362
static void write_entity_ref(write_env_t *env, ir_entity *entity)
363
364
365
366
{
	write_long(env, get_entity_nr(entity));
}

Matthias Braun's avatar
Matthias Braun committed
367
static void write_type_ref(write_env_t *env, ir_type *type)
368
{
369
370
	switch (get_type_tpop_code(type)) {
	case tpo_unknown:
Matthias Braun's avatar
Matthias Braun committed
371
		write_symbol(env, "unknown");
372
373
		return;
	case tpo_code:
Matthias Braun's avatar
Matthias Braun committed
374
		write_symbol(env, "code");
375
376
377
		return;
	default:
		break;
Matthias Braun's avatar
Matthias Braun committed
378
	}
379
	write_long(env, get_type_nr(type));
380
381
}

Matthias Braun's avatar
Matthias Braun committed
382
static void write_string(write_env_t *env, const char *string)
Matthias Braun's avatar
Matthias Braun committed
383
384
{
	fputc('"', env->file);
Matthias Braun's avatar
Matthias Braun committed
385
	for (const char *c = string; *c != '\0'; ++c) {
Matthias Braun's avatar
Matthias Braun committed
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
		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
401
	fputc(' ', env->file);
Matthias Braun's avatar
Matthias Braun committed
402
403
}

Matthias Braun's avatar
Matthias Braun committed
404
static void write_ident(write_env_t *env, ident *id)
Matthias Braun's avatar
Matthias Braun committed
405
406
407
408
{
	write_string(env, get_id_str(id));
}

Matthias Braun's avatar
Matthias Braun committed
409
static void write_ident_null(write_env_t *env, ident *id)
Matthias Braun's avatar
Matthias Braun committed
410
411
412
413
414
415
416
417
{
	if (id == NULL) {
		fputs("NULL ", env->file);
	} else {
		write_ident(env, id);
	}
}

Matthias Braun's avatar
Matthias Braun committed
418
static void write_mode_ref(write_env_t *env, ir_mode *mode)
419
{
420
	write_string(env, get_mode_name(mode));
421
422
}

423
static void write_tarval_ref(write_env_t *env, ir_tarval *tv)
424
{
425
426
427
428
429
430
	ir_mode *mode = get_tarval_mode(tv);
	write_mode_ref(env, mode);
	char buf[128];
	const char *ascii = ir_tarval_to_ascii(buf, sizeof(buf), tv);
	fputs(ascii, env->file);
	fputc(' ', env->file);
431
432
}

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

439
static void write_builtin_kind(write_env_t *env, ir_builtin_kind kind)
440
{
441
	fputs(get_builtin_kind_name(kind), env->file);
442
443
444
	fputc(' ', env->file);
}

445
static void write_cond_jmp_predicate(write_env_t *env, cond_jmp_predicate pred)
446
{
447
	fputs(get_cond_jmp_predicate_name(pred), env->file);
448
449
450
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
451
452
453
454
455
456
457
458
459
460
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");
}

461
462
463
464
465
static void write_loop(write_env_t *env, bool loop)
{
	write_symbol(env, loop ? "loop" : "noloop");
}

Matthias Braun's avatar
Matthias Braun committed
466
static void write_list_begin(write_env_t *env)
467
468
469
470
{
	fputs("[", env->file);
}

Matthias Braun's avatar
Matthias Braun committed
471
static void write_list_end(write_env_t *env)
472
473
474
475
{
	fputs("] ", env->file);
}

Matthias Braun's avatar
Matthias Braun committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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));
}

491
static void write_initializer(write_env_t *const env, ir_initializer_t const *const ini)
492
493
494
{
	FILE *f = env->file;
	ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
495

496
497
498
	fputs(get_initializer_kind_name(ini_kind), f);
	fputc(' ', f);

499
500
	switch (ini_kind) {
	case IR_INITIALIZER_CONST:
Matthias Braun's avatar
Matthias Braun committed
501
502
		write_node_ref(env, get_initializer_const_value(ini));
		return;
503

504
	case IR_INITIALIZER_TARVAL:
505
		write_tarval_ref(env, get_initializer_tarval_value(ini));
Matthias Braun's avatar
Matthias Braun committed
506
		return;
507

508
	case IR_INITIALIZER_NULL:
Matthias Braun's avatar
Matthias Braun committed
509
		return;
510

511
	case IR_INITIALIZER_COMPOUND: {
Matthias Braun's avatar
Matthias Braun committed
512
		size_t n = get_initializer_compound_n_entries(ini);
Matthias Braun's avatar
Matthias Braun committed
513
		write_size_t(env, n);
Matthias Braun's avatar
Matthias Braun committed
514
		for (size_t i = 0; i < n; ++i)
515
			write_initializer(env, get_initializer_compound_value(ini, i));
Matthias Braun's avatar
Matthias Braun committed
516
		return;
517
	}
518
	}
519
	panic("unknown initializer kind");
520
521
}

Matthias Braun's avatar
Matthias Braun committed
522
static void write_pin_state(write_env_t *env, op_pin_state state)
523
{
Matthias Braun's avatar
Matthias Braun committed
524
	fputs(get_op_pin_state_name(state), env->file);
525
526
527
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
528
static void write_volatility(write_env_t *env, ir_volatility vol)
529
530
531
532
533
{
	fputs(get_volatility_name(vol), env->file);
	fputc(' ', env->file);
}

Matthias Braun's avatar
Matthias Braun committed
534
static void write_type_state(write_env_t *env, ir_type_state state)
535
{
Matthias Braun's avatar
Matthias Braun committed
536
537
538
	fputs(get_type_state_name(state), env->file);
	fputc(' ', env->file);
}
539

Matthias Braun's avatar
Matthias Braun committed
540
541
542
543
544
static void write_visibility(write_env_t *env, ir_visibility visibility)
{
	fputs(get_visibility_name(visibility), env->file);
	fputc(' ', env->file);
}
545

Matthias Braun's avatar
Matthias Braun committed
546
547
548
549
550
static void write_mode_arithmetic(write_env_t *env, ir_mode_arithmetic arithmetic)
{
	fputs(get_mode_arithmetic_name(arithmetic), env->file);
	fputc(' ', env->file);
}
551

Matthias Braun's avatar
Matthias Braun committed
552
553
554
555
556
557
558
559
560
561
562
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);
}
563

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

566
567
568
569
570
571
572
573
574
575
576
577
578
static bool is_internal_mode(ir_mode *mode)
{
	return !mode_is_int(mode) && !mode_is_reference(mode)
	    && !mode_is_float(mode);
}

static bool is_default_mode(ir_mode *mode)
{
	/* some modes which are always available in libfirm */
	return mode == mode_b || mode == mode_X || mode == mode_BB
	    || mode == mode_T || mode == mode_ANY || mode == mode_BAD;
}

Matthias Braun's avatar
Matthias Braun committed
579
580
static void write_type_primitive(write_env_t *env, ir_type *tp)
{
581
582
583
584
585
	/* skip types for internal modes */
	ir_mode *mode = get_type_mode(tp);
	if (is_internal_mode(mode) && !is_default_mode(mode))
		return;

Matthias Braun's avatar
Matthias Braun committed
586
	write_type_common(env, tp);
587
	write_mode_ref(env, mode);
Matthias Braun's avatar
Matthias Braun committed
588
	fputc('\n', env->file);
589
590
}

Matthias Braun's avatar
Matthias Braun committed
591
static void write_type_compound(write_env_t *env, ir_type *tp)
592
{
Matthias Braun's avatar
Matthias Braun committed
593
	if (is_Class_type(tp)) {
594
		if (get_class_n_subtypes(tp) > 0 || get_class_n_supertypes(tp) > 0) {
sebastian.buchwald1's avatar
sebastian.buchwald1 committed
595
			/* sub/superclass export not implemented yet, it's unclear whether
Matthias Braun's avatar
Matthias Braun committed
596
597
598
			 * class types will stay in libfirm anyway */
			panic("can't export class types yet");
		}
599
	}
Matthias Braun's avatar
Matthias Braun committed
600
601
602
	write_type_common(env, tp);
	write_ident_null(env, get_compound_ident(tp));
	fputc('\n', env->file);
603

Matthias Braun's avatar
Matthias Braun committed
604
	for (size_t i = 0, n = get_compound_n_members(tp); i < n; ++i) {
Matthias Braun's avatar
Matthias Braun committed
605
		ir_entity *member = get_compound_member(tp, i);
606
		pdeq_putr(env->entity_queue, member);
607
	}
Matthias Braun's avatar
Matthias Braun committed
608
}
609

Matthias Braun's avatar
Matthias Braun committed
610
611
static void write_type_array(write_env_t *env, ir_type *tp)
{
Matthias Braun's avatar
Matthias Braun committed
612
	ir_type *element_type = get_array_element_type(tp);
Matthias Braun's avatar
Matthias Braun committed
613
614
615
	write_type(env, element_type);

	write_type_common(env, tp);
Matthias Braun's avatar
Matthias Braun committed
616
	write_type_ref(env, element_type);
617
618
	ir_node *size = get_array_size(tp);
	if (is_Const(size))
619
		write_long(env, get_Const_long(size));
620
621
622
	else if (is_Unknown(size))
		write_symbol(env, "unknown");
	else
623
		panic("upper array bound is not constant");
Matthias Braun's avatar
Matthias Braun committed
624
625
	fputc('\n', env->file);
}
626

Matthias Braun's avatar
Matthias Braun committed
627
628
629
static void write_type_method(write_env_t *env, ir_type *tp)
{
	size_t nparams  = get_method_n_params(tp);
Matthias Braun's avatar
Matthias Braun committed
630
	for (size_t i = 0; i < nparams; i++)
Matthias Braun's avatar
Matthias Braun committed
631
		write_type(env, get_method_param_type(tp, i));
Matthias Braun's avatar
Matthias Braun committed
632
633
	size_t nresults = get_method_n_ress(tp);
	for (size_t i = 0; i < nresults; i++)
Matthias Braun's avatar
Matthias Braun committed
634
635
636
637
638
639
640
		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);
Matthias Braun's avatar
Matthias Braun committed
641
	for (size_t i = 0; i < nparams; i++)
Matthias Braun's avatar
Matthias Braun committed
642
		write_type_ref(env, get_method_param_type(tp, i));
Matthias Braun's avatar
Matthias Braun committed
643
	for (size_t i = 0; i < nresults; i++)
Matthias Braun's avatar
Matthias Braun committed
644
		write_type_ref(env, get_method_res_type(tp, i));
645
	write_unsigned(env, is_method_variadic(tp));
Matthias Braun's avatar
Matthias Braun committed
646
647
	fputc('\n', env->file);
}
648

Matthias Braun's avatar
Matthias Braun committed
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
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;

680
681
682
683
	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;
684
	}
685
	panic("can't write invalid type %+F", tp);
686
687
}

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

Matthias Braun's avatar
Matthias Braun committed
695
	if (entity_visited(ent))
696
		return;
Matthias Braun's avatar
Matthias Braun committed
697
	mark_entity_visited(ent);
698

Matthias Braun's avatar
Matthias Braun committed
699
700
	write_type(env, type);
	write_type(env, owner);
701
702
703
704
	if (is_alias_entity(ent)) {
		ir_entity *aliased = get_entity_alias(ent);
		write_entity(env, aliased);
	}
Matthias Braun's avatar
Matthias Braun committed
705
706
707

	fputc('\t', env->file);
	switch ((ir_entity_kind)ent->entity_kind) {
708
	case IR_ENTITY_ALIAS:           write_symbol(env, "alias");           break;
Matthias Braun's avatar
Matthias Braun committed
709
710
711
712
713
	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;
714
715
716
717
	case IR_ENTITY_UNKNOWN:
		write_symbol(env, "unknown");
		write_long(env, get_entity_nr(ent));
		return;
718
	}
Matthias Braun's avatar
Matthias Braun committed
719
	write_long(env, get_entity_nr(ent));
720

Matthias Braun's avatar
Matthias Braun committed
721
	if (ent->entity_kind != IR_ENTITY_LABEL
Matthias Braun's avatar
Matthias Braun committed
722
	 && ent->entity_kind != IR_ENTITY_PARAMETER) {
Matthias Braun's avatar
Matthias Braun committed
723
724
725
726
727
728
		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));
		}
729
	}
Matthias Braun's avatar
Matthias Braun committed
730
731
732

	write_visibility(env, visibility);
	write_list_begin(env);
733
	if (linkage & IR_LINKAGE_CONSTANT)
Matthias Braun's avatar
Matthias Braun committed
734
		write_symbol(env, "constant");
735
	if (linkage & IR_LINKAGE_WEAK)
Matthias Braun's avatar
Matthias Braun committed
736
		write_symbol(env, "weak");
737
	if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
Matthias Braun's avatar
Matthias Braun committed
738
		write_symbol(env, "garbage_collect");
739
	if (linkage & IR_LINKAGE_MERGE)
Matthias Braun's avatar
Matthias Braun committed
740
		write_symbol(env, "merge");
741
	if (linkage & IR_LINKAGE_HIDDEN_USER)
Matthias Braun's avatar
Matthias Braun committed
742
743
744
745
746
747
748
749
750
		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_volatility(env, get_entity_volatility(ent));

	switch ((ir_entity_kind)ent->entity_kind) {
751
752
753
	case IR_ENTITY_ALIAS:
		write_entity_ref(env, get_entity_alias(ent));
		break;
754
755
756
757

	case IR_ENTITY_NORMAL: {
		ir_initializer_t const *const init = get_entity_initializer(ent);
		if (init) {
Matthias Braun's avatar
Matthias Braun committed
758
			write_symbol(env, "initializer");
759
			write_initializer(env, init);
Matthias Braun's avatar
Matthias Braun committed
760
761
		} else {
			write_symbol(env, "none");
762
		}
Matthias Braun's avatar
Matthias Braun committed
763
		break;
764
765
	}

Matthias Braun's avatar
Matthias Braun committed
766
767
	case IR_ENTITY_COMPOUND_MEMBER:
		write_long(env, get_entity_offset(ent));
768
769
		write_unsigned(env, get_entity_bitfield_offset(ent));
		write_unsigned(env, get_entity_bitfield_size(ent));
Matthias Braun's avatar
Matthias Braun committed
770
771
772
773
774
775
776
777
		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);
		}
778
779
780
		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
781
782
		break;
	}
783
784
785
	case IR_ENTITY_METHOD:
		write_long(env, (long)get_entity_additional_properties(ent));
		break;
786
	case IR_ENTITY_UNKNOWN:
Matthias Braun's avatar
Matthias Braun committed
787
788
	case IR_ENTITY_LABEL:
		break;
789
790
791
	}

	fputc('\n', env->file);
792
793
}

794
795
static void write_switch_table_ref(write_env_t *env,
                                   const ir_switch_table *table)
796
{
Matthias Braun's avatar
Matthias Braun committed
797
798
	size_t n_entries = ir_switch_table_get_n_entries(table);
	write_size_t(env, n_entries);
Matthias Braun's avatar
Matthias Braun committed
799
	for (size_t i = 0; i < n_entries; ++i) {
Matthias Braun's avatar
Matthias Braun committed
800
801
802
803
		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);
804
805
		write_tarval_ref(env, min);
		write_tarval_ref(env, max);
Matthias Braun's avatar
Matthias Braun committed
806
	}
807
808
}

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

Matthias Braun's avatar
Matthias Braun committed
821
822
823
824
825
826
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
827
{
Matthias Braun's avatar
Matthias Braun committed
828
829
830
	write_symbol(env, "ASM");
	write_node_nr(env, node);
	write_node_nr(env, get_nodes_block(node));
831
	write_node_nr(env, get_ASM_mem(node));
Matthias Braun's avatar
Matthias Braun committed
832
833
834

	write_ident(env, get_ASM_text(node));
	write_list_begin(env);
835
836
837
	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
838
839
840
		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
841
		write_mode_ref(env, constraint->mode);
Matthias Braun's avatar
Matthias Braun committed
842
843
844
845
	}
	write_list_end(env);

	write_list_begin(env);
846
847
848
	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
849
850
851
		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
852
		write_mode_ref(env, constraint->mode);
Matthias Braun's avatar
Matthias Braun committed
853
854
855
856
	}
	write_list_end(env);

	write_list_begin(env);
857
858
859
	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
860
861
862
863
		ident *clobber = clobbers[i];
		write_ident(env, clobber);
	}
	write_list_end(env);
Matthias Braun's avatar
Matthias Braun committed
864
865

	write_pin_state(env, get_irn_pinned(node));
Matthias Braun's avatar
Matthias Braun committed
866
	write_pred_refs(env, node, n_ASM_max+1);
Matthias Braun's avatar
Matthias Braun committed
867
868
}

Matthias Braun's avatar
Matthias Braun committed
869
static void write_Phi(write_env_t *env, const ir_node *node)
870
{
Matthias Braun's avatar
Matthias Braun committed
871
872
873
874
	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));
875
	write_loop(env, get_Phi_loop(node));
Matthias Braun's avatar
Matthias Braun committed
876
877
	write_pred_refs(env, node, 0);
}
878

Matthias Braun's avatar
Matthias Braun committed
879
880
881
static void write_Block(write_env_t *env, const ir_node *node)
{
	ir_entity *entity = get_Block_entity(node);
882

Matthias Braun's avatar
Matthias Braun committed
883
884
885
886
887
888
889
	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);
890
	}
Matthias Braun's avatar
Matthias Braun committed
891
892
	write_pred_refs(env, node, 0);
}
893

Matthias Braun's avatar
Matthias Braun committed
894
895
896
897
898
899
900
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);
}

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

903
static void register_node_writer(ir_op *op, write_node_func *func)
Matthias Braun's avatar
Matthias Braun committed
904
{
905
	set_generic_function_ptr(op, func);
Matthias Braun's avatar
Matthias Braun committed
906
907
908
909
}

static void writers_init(void)
{
Matthias Braun's avatar
Matthias Braun committed
910
	ir_clear_opcodes_generic_func();
911
912
913
914
	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);
Matthias Braun's avatar
Matthias Braun committed
915
916
917
918
919
	register_generated_node_writers();
}

static void write_node(const ir_node *node, write_env_t *env)
{
920
921
	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
922
923
924

	fputc('\t', env->file);
	if (func == NULL)
925
		panic("no write_node_func for %+F", node);
Matthias Braun's avatar
Matthias Braun committed
926
927
928
929
930
931
932
933
	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)
{
934
	foreach_irn_in(node, i, pred) {
Matthias Braun's avatar
Matthias Braun committed
935
		write_node_recursive(pred, env);
936
	}
Matthias Braun's avatar
Matthias Braun committed
937
}
938

Matthias Braun's avatar
Matthias Braun committed
939
940
941
942
943
944
945
946
947
948
949
/**
 * 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;
950

Matthias Braun's avatar
Matthias Braun committed
951
952
953
954
955
956
957
	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 {
958
		foreach_irn_in(node, i, pred) {
Matthias Braun's avatar
Matthias Braun committed
959
960
961
962
963
964
965
966
967
968
969
970
971
			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));
972
		write_int(env, mode_is_signed(mode));
Matthias Braun's avatar
Matthias Braun committed
973
974
975
976
977
978
979
980
		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));

981
		write_mode_ref(env, get_reference_offset_mode(mode));
Matthias Braun's avatar
Matthias Braun committed
982
983
984
985
986
987
988
		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));
989
		write_unsigned(env, get_mode_float_int_overflow(mode));
Matthias Braun's avatar
Matthias Braun committed
990
	} else {
991
		panic("cannot write internal modes");
992
	}
993
994
}

Matthias Braun's avatar
Matthias Braun committed
995
static void write_modes(write_env_t *env)
996
{
Matthias Braun's avatar
Matthias Braun committed
997
998
	write_symbol(env, "modes");
	fputs("{\n", env->file);
999

Matthias Braun's avatar
Matthias Braun committed
1000
	for (size_t i = 0, n_modes = ir_get_n_modes(); i < n_modes; i++) {
Matthias Braun's avatar
Matthias Braun committed
1001
		ir_mode *mode = ir_get_mode(i);
1002
1003
		if (is_internal_mode(mode))
			continue;
Matthias Braun's avatar
Matthias Braun committed
1004
1005
		fputc('\t', env->file);
		write_mode(env, mode);
1006
1007
1008
		fputc('\n', env->file);
	}

Matthias Braun's avatar
Matthias Braun committed
1009
	fputs("}\n\n", env->file);
1010
1011
}

Matthias Braun's avatar
Matthias Braun committed
1012
static void write_program(write_env_t *env)
1013
{
Matthias Braun's avatar
Matthias Braun committed
1014
1015
	write_symbol(env, "program");
	write_scope_begin(env);
1016
	if (irp_prog_name_is_set()) {
Matthias Braun's avatar
Matthias Braun committed
1017
1018
		fputc('\t', env->file);
		write_symbol(env, "name");
Matthias Braun's avatar
Matthias Braun committed
1019
		write_string(env, get_irp_name());
Matthias Braun's avatar
Matthias Braun committed
1020
		fputc('\n', env->file);
1021
1022
	}

Matthias Braun's avatar
Matthias Braun committed
1023
	for (ir_segment_t s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
1024
		ir_type *segment_type = get_segment_type(s);
Matthias Braun's avatar
Matthias Braun committed
1025
1026
1027
		fputc('\t', env->file);
		write_symbol(env, "segment_type");
		write_symbol(env, get_segment_name(s));
1028
		if (segment_type == NULL) {