irio.c 42 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
69
	tt_cond_kind,
	tt_cond_jmp_predicate,
70
71
	tt_initializer,
	tt_iro,
72
	tt_keyword,
73
	tt_mode_sort,
74
	tt_mode_arithmetic,
75
76
	tt_peculiarity,
	tt_pin_state,
77
	tt_tpo,
78
79
80
	tt_type_state,
	tt_variability,
	tt_visibility,
81
82
	tt_volatility,
	tt_segment
83
84
} typetag_t;

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

98
typedef struct symbol_t
99
{
100
101
102
103
	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;
104
105
106
107
108
109
110

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

111
112
/** The symbol table, a set of symbol_t elements. */
static set *symtbl;
113

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

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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);
}

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

	/* Only initialize once */
Michael Beck's avatar
Michael Beck committed
161
	if (symtbl != NULL)
162
		return;
163

164
	symtbl = new_set(symbol_cmp, 256);
165

166
#define INSERT(tt, s, cod)                                       \
167
168
169
	key.str = (s);                                               \
	key.typetag = (tt);                                          \
	key.code = (cod);                                            \
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
	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);
196

197
198
199
	INSERTKEYWORD(constirg);
	INSERTKEYWORD(entity);
	INSERTKEYWORD(irg);
200
201
	INSERTKEYWORD(mode);
	INSERTKEYWORD(modes);
202
203
	INSERTKEYWORD(type);
	INSERTKEYWORD(typegraph);
204
205
	INSERTKEYWORD(program);
	INSERTKEYWORD(segment_type);
206

207
208
209
210
211
212
213
214
215
216
#include "gen_irio_lex.inl"

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

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

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
241
242
243
	INSERTENUM(tt_builtin, ir_bk_prefetch);
	INSERTENUM(tt_builtin, ir_bk_ffs);
	INSERTENUM(tt_builtin, ir_bk_clz);
	INSERTENUM(tt_builtin, ir_bk_ctz);
	INSERTENUM(tt_builtin, ir_bk_popcount);
	INSERTENUM(tt_builtin, ir_bk_parity);
	INSERTENUM(tt_builtin, ir_bk_bswap);
	INSERTENUM(tt_builtin, ir_bk_inport);
	INSERTENUM(tt_builtin, ir_bk_outport);
	INSERTENUM(tt_builtin, ir_bk_inner_trampoline);

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

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

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

244
245
246
247
248
249
250
251
	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);

252
253
254
255
	INSERTENUM(tt_peculiarity, peculiarity_description);
	INSERTENUM(tt_peculiarity, peculiarity_inherited);
	INSERTENUM(tt_peculiarity, peculiarity_existent);

256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
	INSERTENUM(tt_pin_state, op_pin_state_floats);
	INSERTENUM(tt_pin_state, op_pin_state_pinned);
	INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
	INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);

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

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

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

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

276
#undef INSERTKEYWORD
277
278
279
280
#undef INSERTENUM
#undef INSERT
}

281
282
283
284
285
286
287
288
289
290
291
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";
}

292
293
/** 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)
294
{
295
	symbol_t key, *entry;
296
297

	key.str = str;
298
	key.typetag = typetag;
299

300
	entry = set_find(symtbl, &key, sizeof(key), firm_fnv_hash_str(str) + typetag * 17);
301
	return entry ? entry->code : SYMERROR;
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
}

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

327
328
329
330
331
332
333
334
335
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);
}

336
337
338
339
static void write_align(io_env_t *env, ir_node *irn)
{
	ir_align align;

Michael Beck's avatar
Michael Beck committed
340
341
342
343
344
345
	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");
346
347
348
349
350

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

351
352
353
354
355
356
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);
}

357
358
359
360
361
362
363
364
365
366
367
368
static void write_cond_kind(io_env_t *env, ir_node *irn)
{
	fputs(get_cond_kind_name(get_Cond_kind(irn)), env->file);
	fputc(' ', env->file);
}

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

369
370
371
372
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);
373

374
375
376
	fputs(get_initializer_kind_name(ini_kind), f);
	fputc(' ', f);

377
378
379
380
	switch (ini_kind) {
	case IR_INITIALIZER_CONST:
		fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
		break;
381

382
383
384
	case IR_INITIALIZER_TARVAL:
		write_tarval(env, get_initializer_tarval_value(ini));
		break;
385

386
387
	case IR_INITIALIZER_NULL:
		break;
388

389
390
391
392
393
394
395
	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;
	}
396

397
398
	default:
		panic("Unknown initializer kind");
399
400
401
	}
}

402
403
404
405
406
407
408
409
410
411
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
412
413
414
415
416
417
	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");
418
419
420
421
422

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

423
static void export_type_common(io_env_t *env, ir_type *tp)
424
{
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
	fprintf(env->file, "\ttype %ld %s %u %u %s %s %d ",
	        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)),
	        get_visibility_name(get_type_visibility(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));
	}
443
444
445
446
447
448
}

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

Michael Beck's avatar
Michael Beck committed
449
	/* skip types to be handled by post walker */
450
451
452
453
454
455
456
	switch (get_type_tpop_code(tp)) {
	case tpo_array:
	case tpo_method:
	case tpo_pointer:
		return;
	default:
		break;
457
458
459
460
	}

	export_type_common(env, tp);

461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
	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;
486
487
488
489
490
491
492
	}
	fputc('\n', f);
}

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

Michael Beck's avatar
Michael Beck committed
495
	/* skip types already handled by pre walker */
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
	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;
511
512
513
	}

	export_type_common(env, tp);
514

515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
	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;
	}
537

538
539
540
541
542
543
544
545
546
547
548
549
	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;
	}
550

551
552
553
554
	case tpo_pointer:
		write_mode(env, get_type_mode(tp));
		fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
		break;
555

556
557
558
559
560
561
562
563
	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;
564
565
566
567
568
569
570
	}
	fputc('\n', f);
}

static void export_entity(io_env_t *env, ir_entity *ent)
{
	ir_type *owner = get_entity_owner(ent);
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586

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

	fprintf(env->file, "%ld %ld %d %u %d %s %s %s %s %s ",
587
588
589
			get_type_nr(get_entity_type(ent)),
			get_type_nr(owner),
			get_entity_offset(ent),
590
			(unsigned) get_entity_offset_bits_remainder(ent),
591
			is_entity_compiler_generated(ent),
592
593
594
595
596
597
			get_allocation_name(get_entity_allocation(ent)),
			get_visibility_name(get_entity_visibility(ent)),
			get_variability_name(get_entity_variability(ent)),
			get_peculiarity_name(get_entity_peculiarity(ent)),
			get_volatility_name(get_entity_volatility(ent)));

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

Michael Beck's avatar
Michael Beck committed
602
603
	if (get_entity_variability(ent) != variability_uninitialized &&
	    get_entity_visibility(ent) != visibility_external_allocated)
604
	{
Michael Beck's avatar
Michael Beck committed
605
606
		if (is_compound_entity(ent)) {
			if (has_entity_initializer(ent)) {
607
608
				fputs("initializer ", env->file);
				write_initializer(env, get_entity_initializer(ent));
Michael Beck's avatar
Michael Beck committed
609
			} else {
610
				int i, n = get_compound_ent_n_values(ent);
611
				fputs("noninitializer ", env->file);
612
				fprintf(env->file, "%d ", n);
Michael Beck's avatar
Michael Beck committed
613
				for (i = 0; i < n; i++) {
614
615
616
617
					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));
				}
618
			}
Michael Beck's avatar
Michael Beck committed
619
		} else {
620
621
622
623
624
625
			ir_node *irn = get_atomic_ent_value(ent);
			fprintf(env->file, "%ld ", get_irn_node_nr(irn));
		}
	}

	fputc('\n', env->file);
626
627
}

628
629
630
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
631
	if (get_kind(tore.typ) == k_type)
632
633
634
635
		export_type_pre(env, tore.typ);
}

static void export_type_or_ent_post(type_or_ent tore, void *ctx)
636
637
638
{
	io_env_t *env = (io_env_t *) ctx;

639
640
641
642
	switch (get_kind(tore.ent)) {
	case k_entity:
		export_entity(env, tore.ent);
		break;
643

644
645
646
	case k_type:
		export_type_post(env, tore.typ);
		break;
647

648
649
650
	default:
		panic("export_type_or_ent_post: Unknown type or entity.");
		break;
651
652
653
	}
}

654
655
656
/**
 * Walker: exports every node.
 */
657
658
659
660
661
662
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
663
	if (env->ignoreblocks && opcode == iro_Block)
664
		return;
665

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

668
	n = get_irn_arity(irn);
Michael Beck's avatar
Michael Beck committed
669
	for (i = -1; i < n; i++) {
670
		ir_node *pred = get_irn_n(irn, i);
Michael Beck's avatar
Michael Beck committed
671
		if (pred == NULL) {
672
			/* Anchor node may have NULL predecessors */
673
			assert(is_Anchor(irn));
674
			fputs("-1 ", env->file);
675
		} else {
676
			fprintf(env->file, "%ld ", get_irn_node_nr(pred));
677
		}
678
679
680
681
	}

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

682
	switch (opcode)	{
683
	#include "gen_irio_export.inl"
684
	}
685
686
687
	fputs("}\n", env->file);
}

688
689
690
691
692
693
694
695
696
697
698
699
700
701
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");
}

702
703
704
705
706
707
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
708
	for (i = 0; i < n_modes; i++) {
709
		ir_mode *mode = get_irp_mode(i);
710
711
712
713
714
715
716
717
718
		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;
719
720
		}

721
722
723
724
725
726
		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
727
		if (mode_is_reference(mode)) {
728
729
730
731
732
733
			write_mode(env, get_reference_mode_signed_eq(mode));
			write_mode(env, get_reference_mode_unsigned_eq(mode));
		}
		fputc('\n', env->file);
	}

734
	fputs("}\n", env->file);
735
736
}

737
static void export_program(io_env_t *env)
738
{
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
	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);
}
763

764
765
766
767
void ir_export(const char *filename)
{
	FILE *file = fopen(filename, "wt");
	if (file == NULL) {
768
769
770
771
		perror(filename);
		return;
	}

772
773
774
	ir_export_file(file, filename);
	fclose(file);
}
775

776
777
778
779
780
781
782
783
784
785
/* 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);
786

787
	fputs("\ntypegraph {\n", env.file);
788
	type_walk_prog(export_type_or_ent_pre, export_type_or_ent_post, &env);
789
	fputs("}\n", env.file);
790

Michael Beck's avatar
Michael Beck committed
791
	for (i = 0; i < n_irgs; i++) {
792
793
		ir_graph *irg       = get_irp_irg(i);
		ir_type  *valuetype = get_irg_value_param_type(irg);
794

795
796
797
798
		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));
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
806

		fputs("}\n", env.file);
807
808
	}

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

811
812
813
	walk_const_code(NULL, export_node, &env);
	fputs("}\n", env.file);

814
	export_program(&env);
815
816
}

817
/* Exports the given irg to the given file. */
818
void ir_export_irg(ir_graph *irg, FILE *file, const char *outputname)
819
820
821
{
	io_env_t env;

822
823
	(void) outputname;
	env.file = file;
824

825
826
	export_modes(&env);

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

829
	type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
830

831
	fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
832
833
834
835
836
837
838

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

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

839
	/* TODO: Only output needed constants */
840
	fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
841
842
	walk_const_code(NULL, export_node, &env);
	fputs("}\n", env.file);
Moritz Kroll's avatar
Moritz Kroll committed
843
844
}

845
static void read_c(io_env_t *env)
Moritz Kroll's avatar
Moritz Kroll committed
846
{
847
848
849
850
	int c = fgetc(env->file);
	env->c = c;
	if (c == '\n')
		env->line++;
Moritz Kroll's avatar
Moritz Kroll committed
851
852
}

853
854
/** Returns the first non-whitespace character or EOF. **/
static void skip_ws(io_env_t *env)
855
{
856
857
858
	while (true) {
		switch (env->c) {
		case ' ':
859
860
		case '\t':
		case '\n':
861
862
863
		case '\r':
			read_c(env);
			continue;
864
865

		default:
866
			return;
867
868
869
870
871
872
		}
	}
}

static void skip_to(io_env_t *env, char to_ch)
{
873
874
875
	while (env->c != to_ch && env->c != EOF) {
		read_c(env);
	}
876
877
878
879
}

static int expect_char(io_env_t *env, char ch)
{
880
881
882
883
	skip_ws(env);
	if (env->c != ch) {
		parse_error(env, "Unexpected char '%c', expected '%c'\n",
		            env->c, ch);
884
885
		return 0;
	}
886
	read_c(env);
887
888
889
	return 1;
}

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

892
static char *read_word(io_env_t *env)
893
{
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
	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;
910
		}
911
		read_c(env);
912
	}
913

914
endofword:
915
916
	obstack_1grow(&env->obst, '\0');
	return obstack_finish(&env->obst);
917
918
}

919
static char *read_quoted_string(io_env_t *env)
920
{
921
922
923
924
925
926
	skip_ws(env);
	if (env->c != '\"') {
		parse_error(env, "Expected '\"', found '%c'\n", env->c);
		exit(1);
	}
	read_c(env);
927

928
929
930
	assert(obstack_object_size(&env->obst) == 0);
	while (true) {
		int ch = env->c;
Michael Beck's avatar
Michael Beck committed
931
		if (ch == EOF) {
932
			parse_error(env, "Unexpected end of quoted string!\n");
933
934
			exit(1);
		}
935
936
937
938
939
940
		if (ch == '\"') {
			read_c(env);
			break;
		}
		obstack_1grow(&env->obst, ch);
		read_c(env);
941
	}
942
943
944
	obstack_1grow(&env->obst, '\0');

	return obstack_finish(&env->obst);
945
946
}

947
static ident *read_ident(io_env_t *env)
948
{
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
	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);
974
975
}

976
static ident *read_ident_null(io_env_t *env)
977
{
978
979
980
981
982
983
984
985
	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;
986
987
988
989
}

static long read_long(io_env_t *env)
{
990
991
992
993
994
995
996
997
998
999
1000
	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 {
For faster browsing, not all history is shown. View entire blame