irio.c 40.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
 * Copyright (C) 1995-2009 University of Karlsruhe.  All right reserved.
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

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

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

#include "irio.h"

35
#include "irnode_t.h"
36
37
38
39
40
41
42
43
44
#include "irprog.h"
#include "irgraph_t.h"
#include "ircons.h"
#include "irgmod.h"
#include "irflag_t.h"
#include "irgwalk.h"
#include "tv.h"
#include "array.h"
#include "error.h"
45
#include "typerep.h"
46
#include "adt/set.h"
47
#include "adt/obst.h"
48

49
#define SYMERROR ((unsigned) ~0)
50

Moritz Kroll's avatar
Moritz Kroll committed
51
typedef struct io_env_t
52
{
53
	int c;                /**< currently read char */
54
	FILE *file;
55
	set *idset;           /**< id_entry set, which maps from file ids to new Firm elements */
56
	int ignoreblocks;
57
58
	const char *inputname;
	int line;
59
	ir_type **fixedtypes;
60
	struct obstack obst;
61
62
63
64
65
66
} io_env_t;

typedef enum typetag_t
{
	tt_align,
	tt_allocation,
67
	tt_builtin,
68
	tt_cond_jmp_predicate,
69
70
	tt_initializer,
	tt_iro,
71
	tt_keyword,
72
	tt_mode_sort,
73
	tt_mode_arithmetic,
74
	tt_pin_state,
75
	tt_tpo,
76
	tt_type_state,
77
	tt_volatility,
Matthias Braun's avatar
Matthias Braun committed
78
	tt_linkage,
79
	tt_segment
80
81
} typetag_t;

82
83
84
85
86
typedef enum keyword_t
{
	kw_constirg,
	kw_entity,
	kw_irg,
87
88
	kw_mode,
	kw_modes,
89
	kw_type,
90
91
92
	kw_typegraph,
	kw_program,
	kw_segment_type
93
94
} keyword_t;

95
typedef struct symbol_t
96
{
97
98
99
100
	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;
101
102
103
104
105
106
107

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

108
109
/** The symbol table, a set of symbol_t elements. */
static set *symtbl;
110

111
112
113
114
/**
 * Compare two symbol table entries.
 */
static int symbol_cmp(const void *elt, const void *key, size_t size)
115
{
116
	int res;
117
118
	const symbol_t *entry = (const symbol_t *) elt;
	const symbol_t *keyentry = (const symbol_t *) key;
119
	(void) size;
120
	res = entry->typetag - keyentry->typetag;
Michael Beck's avatar
Michael Beck committed
121
	if (res) return res;
122
123
124
125
126
127
128
129
130
131
132
	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;
}

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
static void __attribute__((format(printf, 2, 3)))
parse_error(io_env_t *env, const char *fmt, ...)
{
	va_list ap;
	int     line = env->line;

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

	fprintf(stderr, "%s:%d: error ", env->inputname, line);

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

152
153
/** Initializes the symbol table. May be called more than once without problems. */
static void symtbl_init(void)
154
{
155
	symbol_t key;
156
157

	/* Only initialize once */
Michael Beck's avatar
Michael Beck committed
158
	if (symtbl != NULL)
159
		return;
160

161
	symtbl = new_set(symbol_cmp, 256);
162

163
#define INSERT(tt, s, cod)                                       \
164
165
166
	key.str = (s);                                               \
	key.typetag = (tt);                                          \
	key.code = (cod);                                            \
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
	set_insert(symtbl, &key, sizeof(key), firm_fnv_hash_str(s) + tt * 17)

#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_mode_sort, "auxiliary", irms_auxiliary);
	INSERT(tt_mode_sort, "control_flow", irms_control_flow);
	INSERT(tt_mode_sort, "memory", irms_memory);
	INSERT(tt_mode_sort, "internal_boolean", irms_internal_boolean);
	INSERT(tt_mode_sort, "reference", irms_reference);
	INSERT(tt_mode_sort, "int_number", irms_int_number);
	INSERT(tt_mode_sort, "float_number", irms_float_number);

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

Matthias Braun's avatar
Matthias Braun committed
194
195
196
197
198
199
200
201
	INSERT(tt_linkage, "constant", IR_LINKAGE_CONSTANT);
	INSERT(tt_linkage, "weak", IR_LINKAGE_WEAK);
	INSERT(tt_linkage, "local", IR_LINKAGE_LOCAL);
	INSERT(tt_linkage, "extern", IR_LINKAGE_EXTERN);
	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);

202
203
204
	INSERTKEYWORD(constirg);
	INSERTKEYWORD(entity);
	INSERTKEYWORD(irg);
205
206
	INSERTKEYWORD(mode);
	INSERTKEYWORD(modes);
207
208
	INSERTKEYWORD(type);
	INSERTKEYWORD(typegraph);
209
210
	INSERTKEYWORD(program);
	INSERTKEYWORD(segment_type);
211

212
213
214
215
216
#include "gen_irio_lex.inl"

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

217
218
219
	INSERTENUM(tt_builtin, ir_bk_trap);
	INSERTENUM(tt_builtin, ir_bk_debugbreak);
	INSERTENUM(tt_builtin, ir_bk_return_address);
220
	INSERTENUM(tt_builtin, ir_bk_frame_address);
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	INSERTENUM(tt_builtin, ir_bk_prefetch);
	INSERTENUM(tt_builtin, ir_bk_ffs);
	INSERTENUM(tt_builtin, ir_bk_clz);
	INSERTENUM(tt_builtin, ir_bk_ctz);
	INSERTENUM(tt_builtin, ir_bk_popcount);
	INSERTENUM(tt_builtin, ir_bk_parity);
	INSERTENUM(tt_builtin, ir_bk_bswap);
	INSERTENUM(tt_builtin, ir_bk_inport);
	INSERTENUM(tt_builtin, ir_bk_outport);
	INSERTENUM(tt_builtin, ir_bk_inner_trampoline);

	INSERTENUM(tt_cond_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);

241
242
243
244
245
246
247
248
	INSERTENUM(tt_mode_arithmetic, irma_uninitialized);
	INSERTENUM(tt_mode_arithmetic, irma_none);
	INSERTENUM(tt_mode_arithmetic, irma_twos_complement);
	INSERTENUM(tt_mode_arithmetic, irma_ones_complement);
	INSERTENUM(tt_mode_arithmetic, irma_int_BCD);
	INSERTENUM(tt_mode_arithmetic, irma_ieee754);
	INSERTENUM(tt_mode_arithmetic, irma_float_BCD);

249
250
251
252
253
254
255
256
257
258
259
	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);

260
#undef INSERTKEYWORD
261
262
263
264
#undef INSERTENUM
#undef INSERT
}

265
266
267
268
269
270
271
272
273
274
275
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";
	}
	return "INVALID_SEGMENT";
}

276
277
/** 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)
278
{
279
	symbol_t key, *entry;
280
281

	key.str = str;
282
	key.typetag = typetag;
283

284
	entry = set_find(symtbl, &key, sizeof(key), firm_fnv_hash_str(str) + typetag * 17);
285
	return entry ? entry->code : SYMERROR;
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
}

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

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

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

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

311
312
313
314
315
316
317
318
319
static void write_tarval(io_env_t *env, tarval *tv)
{
	char buf[1024];
	write_mode(env, get_tarval_mode(tv));
	tarval_snprintf(buf, sizeof(buf), tv);
	fputs(buf, env->file);
	fputc(' ', env->file);
}

320
321
322
323
static void write_align(io_env_t *env, ir_node *irn)
{
	ir_align align;

Michael Beck's avatar
Michael Beck committed
324
325
326
327
328
329
	if (is_Load(irn))
		align = get_Load_align(irn);
	else if (is_Store(irn))
		align = get_Store_align(irn);
	else
		panic("Invalid optype for write_align");
330
331
332
333
334

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

335
336
337
338
339
340
static void write_builtin_kind(io_env_t *env, ir_node *irn)
{
	fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
	fputc(' ', env->file);
}

341
342
343
344
345
346
static void write_cond_jmp_predicate(io_env_t *env, ir_node *irn)
{
	fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(irn)), env->file);
	fputc(' ', env->file);
}

347
348
349
350
static void write_initializer(io_env_t *env, ir_initializer_t *ini)
{
	FILE *f = env->file;
	ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
351

352
353
354
	fputs(get_initializer_kind_name(ini_kind), f);
	fputc(' ', f);

355
356
357
358
	switch (ini_kind) {
	case IR_INITIALIZER_CONST:
		fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
		break;
359

360
361
362
	case IR_INITIALIZER_TARVAL:
		write_tarval(env, get_initializer_tarval_value(ini));
		break;
363

364
365
	case IR_INITIALIZER_NULL:
		break;
366

367
368
369
370
371
372
373
	case IR_INITIALIZER_COMPOUND: {
		unsigned i, n = get_initializer_compound_n_entries(ini);
		fprintf(f, "%d ", n);
		for (i = 0; i < n; i++)
			write_initializer(env, get_initializer_compound_value(ini, i));
		break;
	}
374

375
376
	default:
		panic("Unknown initializer kind");
377
378
379
	}
}

380
381
382
383
384
385
386
387
388
389
static void write_pin_state(io_env_t *env, ir_node *irn)
{
	fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
	fputc(' ', env->file);
}

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

Michael Beck's avatar
Michael Beck committed
390
391
392
393
394
395
	if (is_Load(irn))
		vol = get_Load_volatility(irn);
	else if (is_Store(irn))
		vol = get_Store_volatility(irn);
	else
		panic("Invalid optype for write_volatility");
396
397
398
399
400

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

401
static void export_type_common(io_env_t *env, ir_type *tp)
402
{
Matthias Braun's avatar
Matthias Braun committed
403
	fprintf(env->file, "\ttype %ld %s %u %u %s %d ",
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
	        get_type_nr(tp),
	        get_type_tpop_name(tp),
	        get_type_size_bytes(tp),
	        get_type_alignment_bytes(tp),
	        get_type_state_name(get_type_state(tp)),
	        tp->flags);
}

static void export_compound_name(FILE *f, const ir_type *tp)
{
	ident *name = get_compound_ident(tp);
	if (name == NULL) {
		fputs("NULL ", f);
	} else {
		fprintf(f, "\"%s\" ", get_id_str(name));
	}
420
421
422
423
424
425
}

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

Michael Beck's avatar
Michael Beck committed
426
	/* skip types to be handled by post walker */
427
428
429
430
431
432
433
	switch (get_type_tpop_code(tp)) {
	case tpo_array:
	case tpo_method:
	case tpo_pointer:
		return;
	default:
		break;
434
435
436
437
	}

	export_type_common(env, tp);

438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
	switch (get_type_tpop_code(tp)) {
	case tpo_uninitialized:
		panic("invalid type found");

	case tpo_class:
		export_compound_name(f, tp);
		break;

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

	case tpo_union:
	case tpo_struct:
	case tpo_enumeration:
		export_compound_name(f, tp);
		break;

	case tpo_array:
	case tpo_method:
	case tpo_pointer:
	case tpo_code:
	case tpo_none:
	case tpo_unknown:
		break;
463
464
465
466
467
468
469
	}
	fputc('\n', f);
}

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

Michael Beck's avatar
Michael Beck committed
472
	/* skip types already handled by pre walker */
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
	switch (get_type_tpop_code(tp)) {
	case tpo_class:
	case tpo_primitive:
	case tpo_struct:
	case tpo_union:
	case tpo_unknown:
	case tpo_uninitialized:
	case tpo_code:
	case tpo_none:
		return;
	case tpo_array:
	case tpo_method:
	case tpo_pointer:
	case tpo_enumeration:
		break;
488
489
490
	}

	export_type_common(env, tp);
491

492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	switch (get_type_tpop_code(tp)) {
	case tpo_array: {
		int n = get_array_n_dimensions(tp);
		fprintf(f, "%d %ld ", n, get_type_nr(get_array_element_type(tp)));
		for (i = 0; i < n; i++) {
			ir_node *lower = get_array_lower_bound(tp, i);
			ir_node *upper = get_array_upper_bound(tp, i);

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

			if (is_Const(upper))
				fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
			else if (is_Unknown(upper))
				fputs("unknown ", f);
			else
				panic("Upper array bound is not constant");
		}
		break;
	}
514

515
516
517
518
519
520
521
522
523
524
525
526
	case tpo_method: {
		int nparams  = get_method_n_params(tp);
		int nresults = get_method_n_ress(tp);
		fprintf(f, "%u %u %d %d ", get_method_calling_convention(tp),
			get_method_additional_properties(tp), nparams, nresults);
		for (i = 0; i < nparams; i++)
			fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
		for (i = 0; i < nresults; i++)
			fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
		fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
		break;
	}
527

528
529
530
531
	case tpo_pointer:
		write_mode(env, get_type_mode(tp));
		fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
		break;
532

533
534
535
536
537
538
539
540
	case tpo_enumeration:
		fprintf(stderr, "Enumeration type not handled yet by exporter\n");
		break;

	default:
		printf("export_type: Unknown type code \"%s\".\n",
		       get_type_tpop_name(tp));
		break;
541
542
543
544
545
546
547
	}
	fputc('\n', f);
}

static void export_entity(io_env_t *env, ir_entity *ent)
{
	ir_type *owner = get_entity_owner(ent);
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562

	/* we don't dump array_element_ent entities. They're a strange concept
	 * and lead to cycles in type_graph.
	 */
	if (is_Array_type(owner))
		return;

	fprintf(env->file, "\tentity %ld \"%s\" ",
	        get_entity_nr(ent), get_entity_name(ent));
	if (ent->ld_name != NULL) {
		fprintf(env->file, "\"%s\" ", get_entity_ld_name(ent));
	} else {
		fprintf(env->file, "NULL ");
	}

Matthias Braun's avatar
Matthias Braun committed
563
	fprintf(env->file, "%ld %ld %d %u %d %s ",
564
565
566
			get_type_nr(get_entity_type(ent)),
			get_type_nr(owner),
			get_entity_offset(ent),
567
			(unsigned) get_entity_offset_bits_remainder(ent),
568
			is_entity_compiler_generated(ent),
569
570
			get_volatility_name(get_entity_volatility(ent)));

Michael Beck's avatar
Michael Beck committed
571
572
	/* TODO: inheritance stuff for class entities not supported yet */
	if (is_Class_type(owner) && owner != get_glob_type())
573
		fprintf(stderr, "Inheritance of class entities not supported yet!\n");
574

Matthias Braun's avatar
Matthias Braun committed
575
576
577
578
579
580
581
582
583
584
585
	if (ent->initializer != NULL) {
		fputs("initializer ", env->file);
		write_initializer(env, get_entity_initializer(ent));
	} else if (entity_has_compound_ent_values(ent)) {
		int i, n = get_compound_ent_n_values(ent);
		fputs("compoundgraph ", env->file);
		fprintf(env->file, "%d ", n);
		for (i = 0; i < n; i++) {
			ir_entity *member = get_compound_ent_value_member(ent, i);
			ir_node   *irn    = get_compound_ent_value(ent, i);
			fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
586
		}
Matthias Braun's avatar
Matthias Braun committed
587
588
	} else {
		fputs("none", env->file);
589
590
591
	}

	fputc('\n', env->file);
592
593
}

594
595
596
static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
{
	io_env_t *env = (io_env_t *) ctx;
Michael Beck's avatar
Michael Beck committed
597
	if (get_kind(tore.typ) == k_type)
598
599
600
601
		export_type_pre(env, tore.typ);
}

static void export_type_or_ent_post(type_or_ent tore, void *ctx)
602
603
604
{
	io_env_t *env = (io_env_t *) ctx;

605
606
607
608
	switch (get_kind(tore.ent)) {
	case k_entity:
		export_entity(env, tore.ent);
		break;
609

610
611
612
	case k_type:
		export_type_post(env, tore.typ);
		break;
613

614
615
616
	default:
		panic("export_type_or_ent_post: Unknown type or entity.");
		break;
617
618
619
	}
}

620
621
622
/**
 * Walker: exports every node.
 */
623
624
625
626
627
628
static void export_node(ir_node *irn, void *ctx)
{
	io_env_t *env = (io_env_t *) ctx;
	int i, n;
	unsigned opcode = get_irn_opcode(irn);

Michael Beck's avatar
Michael Beck committed
629
	if (env->ignoreblocks && opcode == iro_Block)
630
		return;
631

632
	fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
633

634
	n = get_irn_arity(irn);
Michael Beck's avatar
Michael Beck committed
635
	for (i = -1; i < n; i++) {
636
		ir_node *pred = get_irn_n(irn, i);
Michael Beck's avatar
Michael Beck committed
637
		if (pred == NULL) {
638
			/* Anchor node may have NULL predecessors */
639
			assert(is_Anchor(irn));
640
			fputs("-1 ", env->file);
641
		} else {
642
			fprintf(env->file, "%ld ", get_irn_node_nr(pred));
643
		}
644
645
646
647
	}

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

648
	switch (opcode)	{
649
	#include "gen_irio_export.inl"
650
	}
651
652
653
	fputs("}\n", env->file);
}

654
655
656
657
658
659
660
661
662
663
664
665
666
667
static const char *get_mode_sort_name(ir_mode_sort sort)
{
	switch (sort) {
	case irms_auxiliary:        return "auxiliary";
	case irms_control_flow:     return "control_flow";
	case irms_memory:           return "memory";
	case irms_internal_boolean: return "internal_boolean";
	case irms_reference:        return "reference";
	case irms_int_number:       return "int_number";
	case irms_float_number:     return "float_number";
	}
	panic("invalid mode sort found");
}

668
669
670
671
672
673
static void export_modes(io_env_t *env)
{
	int i, n_modes = get_irp_n_modes();

	fputs("modes {\n", env->file);

Michael Beck's avatar
Michael Beck committed
674
	for (i = 0; i < n_modes; i++) {
675
		ir_mode *mode = get_irp_mode(i);
676
677
678
679
680
681
682
683
684
		switch (get_mode_sort(mode)) {
		case irms_auxiliary:
		case irms_control_flow:
		case irms_memory:
		case irms_internal_boolean:
			/* skip "internal" modes, which may not be user defined */
			continue;
		default:
			break;
685
686
		}

687
688
689
690
691
692
		fprintf(env->file, "\tmode \"%s\" %s %d %d %s %d %d ",
		        get_mode_name(mode), get_mode_sort_name(get_mode_sort(mode)),
		        get_mode_size_bits(mode), get_mode_sign(mode),
		        get_mode_arithmetic_name(get_mode_arithmetic(mode)),
		        get_mode_modulo_shift(mode),
		        get_mode_n_vector_elems(mode));
Michael Beck's avatar
Michael Beck committed
693
		if (mode_is_reference(mode)) {
694
695
696
697
698
699
			write_mode(env, get_reference_mode_signed_eq(mode));
			write_mode(env, get_reference_mode_unsigned_eq(mode));
		}
		fputc('\n', env->file);
	}

700
	fputs("}\n", env->file);
701
702
}

703
static void export_program(io_env_t *env)
704
{
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
	FILE         *f = env->file;
	ir_segment_t  s;

	fputs("\nprogram {\n", f);
	if (irp_prog_name_is_set()) {
		fprintf(f, "\tname \"%s\"\n", get_irp_name());
	}
	/* We need numbers for irgs... */
#if 0
	if (get_irp_main_irg() != NULL) {
		fprintf(f, "\tmain_irg %d\n", get_irp_main_irg
#endif

	for (s = 0; s <= IR_SEGMENT_LAST; ++s) {
		ir_type *segment_type = get_segment_type(s);
		fprintf(f, "\tsegment_type %s", get_segment_name(s));
		if (segment_type == NULL) {
			fputs(" NULL\n", f);
		} else {
			fprintf(f, " %ld\n", get_type_nr(segment_type));
		}
	}
	fputs("}\n", f);
}
729

730
731
732
733
void ir_export(const char *filename)
{
	FILE *file = fopen(filename, "wt");
	if (file == NULL) {
734
735
736
737
		perror(filename);
		return;
	}

738
739
740
	ir_export_file(file, filename);
	fclose(file);
}
741

742
743
744
745
746
747
748
749
750
751
/* Exports the whole irp to the given file in a textual form. */
void ir_export_file(FILE *file, const char *outputname)
{
	io_env_t env;
	int i, n_irgs = get_irp_n_irgs();

	(void) outputname;
	env.file = file;

	export_modes(&env);
752

753
	fputs("\ntypegraph {\n", env.file);
754
	type_walk_prog(export_type_or_ent_pre, export_type_or_ent_post, &env);
755
	fputs("}\n", env.file);
756

Michael Beck's avatar
Michael Beck committed
757
	for (i = 0; i < n_irgs; i++) {
758
759
		ir_graph *irg       = get_irp_irg(i);
		ir_type  *valuetype = get_irg_value_param_type(irg);
760

761
762
763
764
		fprintf(env.file, "\nirg %ld %ld %ld {\n",
		        get_entity_nr(get_irg_entity(irg)),
		        get_type_nr(get_irg_frame_type(irg)),
		        valuetype == NULL ? -1 : get_type_nr(valuetype));
765
766
767
768
769
770

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

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

		fputs("}\n", env.file);
773
774
	}

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

777
778
779
	walk_const_code(NULL, export_node, &env);
	fputs("}\n", env.file);

780
	export_program(&env);
781
782
}

783
/* Exports the given irg to the given file. */
784
void ir_export_irg(ir_graph *irg, FILE *file, const char *outputname)
785
786
787
{
	io_env_t env;

788
789
	(void) outputname;
	env.file = file;
790

791
792
	export_modes(&env);

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

795
	type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
796

797
	fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
798
799
800
801
802
803
804

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

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

805
	/* TODO: Only output needed constants */
806
	fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
807
808
	walk_const_code(NULL, export_node, &env);
	fputs("}\n", env.file);
Moritz Kroll's avatar
Moritz Kroll committed
809
810
}

811
static void read_c(io_env_t *env)
Moritz Kroll's avatar
Moritz Kroll committed
812
{
813
814
815
816
	int c = fgetc(env->file);
	env->c = c;
	if (c == '\n')
		env->line++;
Moritz Kroll's avatar
Moritz Kroll committed
817
818
}

819
820
/** Returns the first non-whitespace character or EOF. **/
static void skip_ws(io_env_t *env)
821
{
822
823
824
	while (true) {
		switch (env->c) {
		case ' ':
825
826
		case '\t':
		case '\n':
827
828
829
		case '\r':
			read_c(env);
			continue;
830
831

		default:
832
			return;
833
834
835
836
837
838
		}
	}
}

static void skip_to(io_env_t *env, char to_ch)
{
839
840
841
	while (env->c != to_ch && env->c != EOF) {
		read_c(env);
	}
842
843
844
845
}

static int expect_char(io_env_t *env, char ch)
{
846
847
848
849
	skip_ws(env);
	if (env->c != ch) {
		parse_error(env, "Unexpected char '%c', expected '%c'\n",
		            env->c, ch);
850
851
		return 0;
	}
852
	read_c(env);
853
854
855
	return 1;
}

Michael Beck's avatar
Michael Beck committed
856
#define EXPECT(c) if (expect_char(env, (c))) {} else return 0
857

858
static char *read_word(io_env_t *env)
859
{
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
	skip_ws(env);

	assert(obstack_object_size(&env->obst) == 0);
	while (true) {
		int c = env->c;
		switch (c) {
		case EOF:
		case ' ':
		case '\t':
		case '\n':
		case '\r':
			goto endofword;

		default:
			obstack_1grow(&env->obst, c);
			break;
876
		}
877
		read_c(env);
878
	}
879

880
endofword:
881
882
	obstack_1grow(&env->obst, '\0');
	return obstack_finish(&env->obst);
883
884
}

885
static char *read_quoted_string(io_env_t *env)
886
{
887
888
889
890
891
892
	skip_ws(env);
	if (env->c != '\"') {
		parse_error(env, "Expected '\"', found '%c'\n", env->c);
		exit(1);
	}
	read_c(env);
893

894
895
896
	assert(obstack_object_size(&env->obst) == 0);
	while (true) {
		int ch = env->c;
Michael Beck's avatar
Michael Beck committed
897
		if (ch == EOF) {
898
			parse_error(env, "Unexpected end of quoted string!\n");
899
900
			exit(1);
		}
901
902
903
904
905
906
		if (ch == '\"') {
			read_c(env);
			break;
		}
		obstack_1grow(&env->obst, ch);
		read_c(env);
907
	}
908
909
910
	obstack_1grow(&env->obst, '\0');

	return obstack_finish(&env->obst);
911
912
}

913
static ident *read_ident(io_env_t *env)
914
{
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
	char  *str = read_quoted_string(env);
	ident *res = new_id_from_str(str);
	obstack_free(&env->obst, str);

	return res;
}

/*
 * reads a "quoted string" or alternatively the token NULL
 */
static char *read_quoted_string_null(io_env_t *env)
{
	skip_ws(env);
	if (env->c == 'N') {
		char *str = read_word(env);
		if (strcmp(str, "NULL") == 0) {
			obstack_free(&env->obst, str);
			return NULL;
		}
	} else if (env->c == '"') {
		return read_quoted_string(env);
	}

	parse_error(env, "Expected \"string\" or NULL\n");
	exit(1);
940
941
}

942
static ident *read_ident_null(io_env_t *env)
943
{
944
945
946
947
948
949
950
951
	ident *res;
	char  *str = read_quoted_string_null(env);
	if (str == NULL)
		return NULL;

	res = new_id_from_str(str);
	obstack_free(&env->obst, str);
	return res;
952
953
954
955
}

static long read_long(io_env_t *env)
{
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
	long  result;
	char *str;

	skip_ws(env);
	if (!isdigit(env->c) && env->c != '-') {
		parse_error(env, "Expected number, got '%c'\n", env->c);
		exit(1);
	}

	assert(obstack_object_size(&env->obst) == 0);
	do {
		obstack_1grow(&env->obst, env->c);
		read_c(env);
	} while(isdigit(env->c));
	obstack_1grow(&env->obst, 0);

	str = obstack_finish(&env->obst);
	result = atol(str);
	obstack_free(&env->obst, str);

	return result;
977
978
979
980
981
}

static ir_node *get_node_or_null(io_env_t *env, long nodenr)
{
	ir_node *node = (ir_node *) get_id(env, nodenr);
982
983
984
985
	if (node && node->kind != k_ir_node) {
		panic("Irn ID %ld collides with something else in line %d\n",
		      nodenr, env->line);
	}
986
987
988
989
990
991
	return node;
}

static ir_node *get_node(io_env_t *env, long nodenr)
{
	ir_node *node = get_node_or_null(env, nodenr);
Michael Beck's avatar
Michael Beck committed
992
	if (!node)
993
		panic("Unknown node: %ld in line %d\n", nodenr, env->line);
994
995
996
997
998
999
1000

	return node;
}

static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
{
	ir_node *node = get_node_or_null(env, nodenr);
Michael Beck's avatar
Michael Beck committed
1001
	if (node == NULL) {
1002
1003
1004
1005
1006
1007
1008
1009
1010
		node = new_Dummy(mode_X);
		set_id(env, nodenr, node);
	}
	return node;
}

static ir_type *get_type(io_env_t *env, long typenr)
{
	ir_type *type = (ir_type *) get_id(env, typenr);
Michael Beck's avatar
Michael Beck committed
1011
	if (type == NULL)
1012
		panic("unknown type: %ld in line %d\n", typenr, env->line);
Michael Beck's avatar
Michael Beck committed
1013
	else if (type->kind != k_type)
1014
1015
		panic("type ID %ld collides with something else in line %d\n",
		      typenr, env->line);
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
	return type;
}

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

static ir_entity *get_entity(io_env_t *env, long entnr)
{
	ir_entity *entity = (ir_entity *) get_id(env, entnr);
Michael Beck's avatar
Michael Beck committed
1027
	if (entity == NULL) {
1028
		parse_error(env, "unknown entity: %ld\n", entnr);
1029
		exit(1);
1030
1031
1032
1033
1034
	} else if (entity->kind != k_entity) {
		panic("Entity ID %ld collides with something else in line %d\n",
		      entnr, env->line);
	}

1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
	return entity;
}

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

static ir_mode *read_mode(io_env_t *env)
{
1045
	char *str = read_word(env);
1046
1047
1048
	int i, n;

	n = get_irp_n_modes();
Michael Beck's avatar
Michael Beck committed
1049
	for (i = 0; i < n; i++) {
1050
		ir_mode *mode = get_irp_mode(i);
1051
1052
		if (strcmp(str, get_mode_name(mode)) == 0) {
			obstack_free(&env->obst, str);
1053
			return mode;
1054
		}
1055
1056
	}

1057
1058
	parse_error(env, "unknown mode \"%s\"\n", str);
	exit(1);
1059
1060
1061
1062
}

static const char *get_typetag_name(typetag_t typetag)
{
1063
1064
1065
1066
	switch (typetag) {
	case tt_align:              return "align";
	case tt_allocation:         return "allocation";
	case tt_builtin:            return "builtin kind";
Matthias Braun's avatar
Matthias Braun committed
1067
	case tt_cond_jmp_predicate: return "cond_jmp_predicate";
1068
1069
	case tt_initializer:        return "initializer kind";
	case tt_iro:                return "opcode";
Matthias Braun's avatar
Matthias Braun committed
1070
1071
1072
1073
	case tt_keyword:            return "keyword";
	case tt_linkage:            return "linkage";
	case tt_mode_arithmetic:    return "mode_arithmetic";
	case tt_mode_sort:          return "mode_sort";
1074
	case tt_pin_state:          return "pin state";
Matthias Braun's avatar
Matthias Braun committed
1075
	case tt_segment:            return "segment";
1076
1077
1078
	case tt_tpo:                return "type";
	case tt_type_state:         return "type state";
	case tt_volatility:         return "volatility";
1079
	}
1080
	return "<UNKNOWN>";
1081
1082
}

1083
1084
1085
/**
 * Read and decode an enum constant.
 */
1086
1087
static unsigned read_enum(io_env_t *env, typetag_t typetag)
{
1088
1089
1090
1091
1092
	char     *str  = read_word(env);
	unsigned  code = symbol(str, typetag);

	if (code != SYMERROR) {
		obstack_free(&env->obst, str);
1093
		return code;
1094
	}
1095

1096
	parse_error(env, "invalid %s: \"%s\"\n", get_typetag_name(typetag), str);
1097
1098
1099
	return 0;
}

1100
1101
1102
1103
1104
#define read_align(env)              ((ir_align)              read_enum(env, tt_align))
#define read_allocation(env)         ((ir_allocation)         read_enum(env, tt_allocation))
#define read_builtin_kind(env)       ((ir_builtin_kind)       read_enum(env, tt_builtin))
#define read_cond_jmp_predicate(env) ((cond_jmp_predicate)    read_enum(env, tt_cond_jmp_predicate))
#define read_initializer_kind(env)   ((ir_initializer_kind_t) read_enum(env, tt_initializer))
1105
#define read_mode_arithmetic(env)    ((ir_mode_arithmetic)    read_enum(env, tt_mode_arithmetic))
1106
1107
1108
1109
1110
#define read_peculiarity(env)        ((ir_peculiarity)        read_enum(env, tt_peculiarity))
#define read_pin_state(env)          ((op_pin_state)          read_enum(env, tt_pin_state))
#define read_type_state(env)         ((ir_type_state)         read_enum(env, tt_type_state))
#define read_variability(env)        ((ir_variability)        read_enum(env, tt_variability))
#define read_volatility(env)         ((ir_volatility)         read_enum(env, tt_volatility))
1111

1112
1113
1114
1115
1116
static ir_cons_flags get_cons_flags(io_env_t *env)
{
	ir_cons_flags flags = cons_none;

	op_pin_state pinstate = read_pin_state(env);
1117
1118
1119
1120
1121
1122
	switch (pinstate) {
	case op_pin_state_floats: flags |= cons_floats; break;
	case op_pin_state_pinned: break;
	default:
		panic("Error in %d: Invalid pinstate: %s", env->line,
		      get_op_pin_state_name(pinstate));
1123
1124
	}

Michael Beck's avatar
Michael Beck committed
1125
1126
1127
1128
	if (read_volatility(env) == volatility_is_volatile)
		flags |= cons_volatile;
	if (read_align(env) == align_non_aligned)
		flags |= cons_unaligned;
1129
1130
1131
1132

	return flags;
}

1133
1134
1135
static tarval *read_tv(io_env_t *env)
{
	ir_mode *tvmode = read_mode(env);
1136
1137
1138
1139
1140
	char    *str    = read_word(env);
	tarval  *tv     = new_tarval_from_str(str, strlen(str), tvmode);
	obstack_free(&env->obst, str);

	return tv;
1141
1142
}

1143
1144
1145
1146
static ir_initializer_t *read_initializer(io_env_t *env)
{
	ir_initializer_kind_t ini_kind = read_initializer_kind(env);

1147
1148
1149
1150
1151
	switch (ini_kind) {
	case IR_INITIALIZER_CONST: {
		ir_node *irn = get_node_or_dummy(env, read_long(env));
		return create_initializer_const(irn);
	}
1152

1153
1154
	case IR_INITIALIZER_TARVAL:
		return create_initializer_tarval(read_tv(env));
1155

1156
1157
	case IR_INITIALIZER_NULL:
		return get_initializer_null();
1158

1159
1160
1161
1162
1163
1164
	case IR_INITIALIZER_COMPOUND: {
		unsigned i, n = (unsigned) read_long(env);
		ir_initializer_t *ini = create_initializer_compound(n);
		for (i = 0; i < n; i++) {
			ir_initializer_t *curini = read_initializer(env);
			set_initializer_compound_value(ini, i, curini);
1165
		}
1166
1167
		return ini;
	}
1168

1169
1170
	default:
		panic("Unknown initializer kind");
1171
1172
1173
1174
	}
}


1175
/** Reads a type description and remembers it by its id. */
1176
static void import_type(io_env_t *env)
1177
1178
1179
1180
{
	int            i;
	ir_type       *type;
	long           typenr = read_long(env);
1181
	tp_opcode      tpop   = (tp_opcode) read_enum(env, tt_tpo);
1182
1183
1184
	unsigned       size   = (unsigned) read_long(env);
	unsigned       align  = (unsigned) read_long(env);
	ir_type_state  state  = read_type_state(env);
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
	unsigned       flags  = (unsigned) read_long(env);

	switch (tpop) {
	case tpo_array: {
		int ndims = (int) read_long(env);
		long elemtypenr = read_long(env);
		ir_type *elemtype = get_type(env, elemtypenr);

		type = new_type_array(ndims, elemtype);
		for (i = 0; i < ndims; i++) {
			char *str = read_word(env);
			if (strcmp(str, "unknown") != 0) {
				long lowerbound = atol(str);
				set_array_lower_bound_int(type, i, lowerbound);
			}
			obstack_free(&env->obst, str);
1201

1202
1203
1204
1205
1206
1207
			str = read_word(env);
			if (strcmp(str, "unknown") != 0) {
				long upperbound = atol(str);
				set_array_upper_bound_int(type, i, upperbound);
			}
			obstack_free(&env->obst, str);
1208
		}
1209
		set_type_size_bytes(type, size);
1210
1211
		break;
	}
1212

1213
1214
1215
	case tpo_class: {
		const char *name = read_quoted_string_null(env);
		ident      *id   = name != NULL ? new_id_from_str(name) : NULL;
1216

1217
1218
1219
1220
		if(typenr == (long) IR_SEGMENT_GLOBAL)
			type = get_glob_type();
		else
			type = new_type_class(id);
1221
		set_type_size_bytes(type, size);
1222
1223
		break;
	}
1224

1225
1226
1227
1228
1229
1230
	case tpo_method: {
		unsigned callingconv = (unsigned) read_long(env);
		unsigned addprops    = (unsigned) read_long(env);
		int nparams          = (int)      read_long(env);
		int nresults         = (int)      read_long(env);
		int variaindex;
1231

1232
		type = new_type_method(nparams, nresults);
1233

1234
1235
1236
		for (i = 0; i < nparams; i++) {
			long     typenr = read_long(env);
			ir_type *paramtype = get_type(env, typenr);
1237

1238
1239
1240
1241
1242
			set_method_param_type(type, i, paramtype);
		}
		for (i = 0; i < nresults; i++) {
			long typenr = read_long(env);
			ir_type *restype = get_type(env, typenr);
1243

1244
1245
			set_method_res_type(type, i, restype);
		}
Moritz Kroll's avatar
Moritz Kroll committed
1246

1247
1248
1249
1250
1251
1252
		variaindex = (int) read_long(env);
		if (variaindex != -1) {
			set_method_variadicity(type, variadicity_variadic);
			if (variaindex != nparams)
				set_method_first_variadic_param_index(type, variaindex);
		}
1253

1254
1255
1256
1257
		set_method_calling_convention(type, callingconv);
		set_method_additional_properties(type, addprops);
		break;
	}
1258

1259
1260
1261
1262
1263
1264
1265
	case tpo_pointer: {
		ir_mode *mode     = read_mode(env);
		ir_type *pointsto = get_type(env, read_long(env));
		type = new_type_pointer(pointsto);
		set_type_mode(type, mode);
		break;
	}
1266

1267
1268
1269
1270
1271
	case tpo_primitive: {
		ir_mode *mode = read_mode(env);
		type = new_type_primitive(mode);
		break;
	}