BugFix: the name o for the NEW NODE was a little bit misleading ...
[libfirm] / ir / ir / irio.c
1 /*
2  * Copyright (C) 1995-2009 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Write textual representation of firm to file.
23  * @author  Moritz Kroll
24  * @version $Id$
25  */
26 #include "config.h"
27
28 #include <string.h>
29
30 #include "irio.h"
31
32 #include "irnode_t.h"
33 #include "irprog.h"
34 #include "irgraph_t.h"
35 #include "ircons.h"
36 #include "irgmod.h"
37 #include "irflag_t.h"
38 #include "irgwalk.h"
39 #include "tv.h"
40 #include "array.h"
41 #include "error.h"
42 #include "adt/set.h"
43
44 #define SYMERROR ((unsigned) ~0)
45
46 typedef struct io_env_t
47 {
48         FILE *file;
49         set *idset;               /**< id_entry set, which maps from file ids to new Firm elements */
50         int ignoreblocks;
51         int line, col;
52         ir_type **fixedtypes;
53 } io_env_t;
54
55 typedef struct lex_state_t
56 {
57         long offs;
58         int line, col;
59 } lex_state_t;
60
61 typedef enum typetag_t
62 {
63         tt_align,
64         tt_allocation,
65         tt_builtin,
66         tt_cond_kind,
67         tt_cond_jmp_predicate,
68         tt_initializer,
69         tt_iro,
70         tt_keyword,
71         tt_mode_arithmetic,
72         tt_peculiarity,
73         tt_pin_state,
74         tt_tpo,
75         tt_type_state,
76         tt_variability,
77         tt_visibility,
78         tt_volatility
79 } typetag_t;
80
81 typedef enum keyword_t
82 {
83         kw_constirg,
84         kw_entity,
85         kw_frametype,
86         kw_irg,
87         kw_mode,
88         kw_modes,
89         kw_valuetype,
90         kw_type,
91         kw_typegraph
92 } keyword_t;
93
94 typedef struct symbol_t
95 {
96         const char *str;      /**< The name of this symbol. */
97         typetag_t   typetag;  /**< The type tag of this symbol. */
98         unsigned    code;     /**< The value of this symbol. */
99 } symbol_t;
100
101 typedef struct id_entry
102 {
103         long id;
104         void *elem;
105 } id_entry;
106
107 /** The symbol table, a set of symbol_t elements. */
108 static set *symtbl;
109
110
111 /**
112  * Calculate a hash value for a string.
113  */
114 static unsigned string_hash(const char *str, int len)
115 {
116         return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
117 }
118
119 /**
120  * Compare two symbol table entries.
121  */
122 static int symbol_cmp(const void *elt, const void *key, size_t size)
123 {
124         int res;
125         const symbol_t *entry = (const symbol_t *) elt;
126         const symbol_t *keyentry = (const symbol_t *) key;
127         (void) size;
128         res = entry->typetag - keyentry->typetag;
129         if (res) return res;
130         return strcmp(entry->str, keyentry->str);
131 }
132
133 static int id_cmp(const void *elt, const void *key, size_t size)
134 {
135         const id_entry *entry = (const id_entry *) elt;
136         const id_entry *keyentry = (const id_entry *) key;
137         (void) size;
138         return entry->id - keyentry->id;
139 }
140
141 /** Initializes the symbol table. May be called more than once without problems. */
142 static void symtbl_init(void)
143 {
144         symbol_t key;
145
146         /* Only initialize once */
147         if (symtbl != NULL)
148                 return;
149
150         symtbl = new_set(symbol_cmp, 256);
151
152 #define INSERT(s, tt, cod)                                       \
153         key.str = (s);                                               \
154         key.typetag = (tt);                                          \
155         key.code = (cod);                                            \
156         set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1) + tt * 17)
157
158 #define INSERTENUM(tt, e) INSERT(#e, tt, e)
159 #define INSERTKEYWORD(k) INSERT(#k, tt_keyword, kw_##k)
160
161         INSERT("array", tt_tpo, tpo_array);
162         INSERT("class", tt_tpo, tpo_class);
163         INSERT("method", tt_tpo, tpo_method);
164         INSERT("pointer", tt_tpo, tpo_pointer);
165         INSERT("primitive", tt_tpo, tpo_primitive);
166         INSERT("struct", tt_tpo, tpo_struct);
167         INSERT("union", tt_tpo, tpo_union);
168         INSERT("Unknown", tt_tpo, tpo_unknown);
169
170         INSERTKEYWORD(constirg);
171         INSERTKEYWORD(entity);
172         INSERTKEYWORD(frametype);
173         INSERTKEYWORD(irg);
174         INSERTKEYWORD(mode);
175         INSERTKEYWORD(modes);
176         INSERTKEYWORD(valuetype);
177         INSERTKEYWORD(type);
178         INSERTKEYWORD(typegraph);
179
180 #include "gen_irio_lex.inl"
181
182         INSERTENUM(tt_align, align_non_aligned);
183         INSERTENUM(tt_align, align_is_aligned);
184
185         INSERTENUM(tt_allocation, allocation_automatic);
186         INSERTENUM(tt_allocation, allocation_parameter);
187         INSERTENUM(tt_allocation, allocation_dynamic);
188         INSERTENUM(tt_allocation, allocation_static);
189
190         INSERTENUM(tt_builtin, ir_bk_trap);
191         INSERTENUM(tt_builtin, ir_bk_debugbreak);
192         INSERTENUM(tt_builtin, ir_bk_return_address);
193         INSERTENUM(tt_builtin, ir_bk_frame_address);
194         INSERTENUM(tt_builtin, ir_bk_prefetch);
195         INSERTENUM(tt_builtin, ir_bk_ffs);
196         INSERTENUM(tt_builtin, ir_bk_clz);
197         INSERTENUM(tt_builtin, ir_bk_ctz);
198         INSERTENUM(tt_builtin, ir_bk_popcount);
199         INSERTENUM(tt_builtin, ir_bk_parity);
200         INSERTENUM(tt_builtin, ir_bk_bswap);
201         INSERTENUM(tt_builtin, ir_bk_inport);
202         INSERTENUM(tt_builtin, ir_bk_outport);
203         INSERTENUM(tt_builtin, ir_bk_inner_trampoline);
204
205         INSERTENUM(tt_cond_kind, dense);
206         INSERTENUM(tt_cond_kind, fragmentary);
207
208         INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_NONE);
209         INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_TRUE);
210         INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_FALSE);
211
212         INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
213         INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
214         INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
215         INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
216
217         INSERTENUM(tt_mode_arithmetic, irma_uninitialized);
218         INSERTENUM(tt_mode_arithmetic, irma_none);
219         INSERTENUM(tt_mode_arithmetic, irma_twos_complement);
220         INSERTENUM(tt_mode_arithmetic, irma_ones_complement);
221         INSERTENUM(tt_mode_arithmetic, irma_int_BCD);
222         INSERTENUM(tt_mode_arithmetic, irma_ieee754);
223         INSERTENUM(tt_mode_arithmetic, irma_float_BCD);
224
225         INSERTENUM(tt_peculiarity, peculiarity_description);
226         INSERTENUM(tt_peculiarity, peculiarity_inherited);
227         INSERTENUM(tt_peculiarity, peculiarity_existent);
228
229         INSERTENUM(tt_pin_state, op_pin_state_floats);
230         INSERTENUM(tt_pin_state, op_pin_state_pinned);
231         INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
232         INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
233
234         INSERTENUM(tt_type_state, layout_undefined);
235         INSERTENUM(tt_type_state, layout_fixed);
236
237         INSERTENUM(tt_variability, variability_uninitialized);
238         INSERTENUM(tt_variability, variability_initialized);
239         INSERTENUM(tt_variability, variability_part_constant);
240         INSERTENUM(tt_variability, variability_constant);
241
242         INSERTENUM(tt_visibility, visibility_local);
243         INSERTENUM(tt_visibility, visibility_external_visible);
244         INSERTENUM(tt_visibility, visibility_external_allocated);
245
246         INSERTENUM(tt_volatility, volatility_non_volatile);
247         INSERTENUM(tt_volatility, volatility_is_volatile);
248
249 #undef INSERTKEYWORD
250 #undef INSERTENUM
251 #undef INSERT
252 }
253
254 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
255 static unsigned symbol(const char *str, typetag_t typetag)
256 {
257         symbol_t key, *entry;
258
259         key.str = str;
260         key.typetag = typetag;
261
262         entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)) + typetag * 17);
263         return entry ? entry->code : SYMERROR;
264 }
265
266 static void *get_id(io_env_t *env, long id)
267 {
268         id_entry key, *entry;
269         key.id = id;
270
271         entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
272         return entry ? entry->elem : NULL;
273 }
274
275 static void set_id(io_env_t *env, long id, void *elem)
276 {
277         id_entry key;
278         key.id = id;
279         key.elem = elem;
280         set_insert(env->idset, &key, sizeof(key), (unsigned) id);
281 }
282
283 static void write_mode(io_env_t *env, ir_mode *mode)
284 {
285         fputs(get_mode_name(mode), env->file);
286         fputc(' ', env->file);
287 }
288
289 static void write_tarval(io_env_t *env, tarval *tv)
290 {
291         char buf[1024];
292         write_mode(env, get_tarval_mode(tv));
293         tarval_snprintf(buf, sizeof(buf), tv);
294         fputs(buf, env->file);
295         fputc(' ', env->file);
296 }
297
298 static void write_align(io_env_t *env, ir_node *irn)
299 {
300         ir_align align;
301
302         if (is_Load(irn))
303                 align = get_Load_align(irn);
304         else if (is_Store(irn))
305                 align = get_Store_align(irn);
306         else
307                 panic("Invalid optype for write_align");
308
309         fputs(get_align_name(align), env->file);
310         fputc(' ', env->file);
311 }
312
313 static void write_builtin_kind(io_env_t *env, ir_node *irn)
314 {
315         fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
316         fputc(' ', env->file);
317 }
318
319 static void write_cond_kind(io_env_t *env, ir_node *irn)
320 {
321         fputs(get_cond_kind_name(get_Cond_kind(irn)), env->file);
322         fputc(' ', env->file);
323 }
324
325 static void write_cond_jmp_predicate(io_env_t *env, ir_node *irn)
326 {
327         fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(irn)), env->file);
328         fputc(' ', env->file);
329 }
330
331 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
332 {
333         FILE *f = env->file;
334         ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
335
336         fputs(get_initializer_kind_name(ini_kind), f);
337         fputc(' ', f);
338
339         switch (ini_kind)
340         {
341                 case IR_INITIALIZER_CONST:
342                         fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
343                         break;
344
345                 case IR_INITIALIZER_TARVAL:
346                         write_tarval(env, get_initializer_tarval_value(ini));
347                         break;
348
349                 case IR_INITIALIZER_NULL:
350                         break;
351
352                 case IR_INITIALIZER_COMPOUND:
353                 {
354                         unsigned i, n = get_initializer_compound_n_entries(ini);
355                         fprintf(f, "%d ", n);
356                         for (i = 0; i < n; i++)
357                                 write_initializer(env, get_initializer_compound_value(ini, i));
358                         break;
359                 }
360
361                 default:
362                         panic("Unknown initializer kind");
363         }
364 }
365
366 static void write_pin_state(io_env_t *env, ir_node *irn)
367 {
368         fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
369         fputc(' ', env->file);
370 }
371
372 static void write_volatility(io_env_t *env, ir_node *irn)
373 {
374         ir_volatility vol;
375
376         if (is_Load(irn))
377                 vol = get_Load_volatility(irn);
378         else if (is_Store(irn))
379                 vol = get_Store_volatility(irn);
380         else
381                 panic("Invalid optype for write_volatility");
382
383         fputs(get_volatility_name(vol), env->file);
384         fputc(' ', env->file);
385 }
386
387 static void export_type_common(io_env_t *env, ir_type *tp)
388 {
389         fprintf(env->file, "\t%s %ld %s %u %u %s %s ",
390                         is_frame_type(tp) ? "frametype" : is_value_param_type(tp) ? "valuetype" : "type",
391                         get_type_nr(tp),
392                         get_type_tpop_name(tp),
393                         get_type_size_bytes(tp),
394                         get_type_alignment_bytes(tp),
395                         get_type_state_name(get_type_state(tp)),
396                         get_visibility_name(get_type_visibility(tp)));
397 }
398
399 static void export_type_pre(io_env_t *env, ir_type *tp)
400 {
401         FILE *f = env->file;
402
403         /* skip types to be handled by post walker */
404         switch (get_type_tpop_code(tp))
405         {
406                 case tpo_array:
407                 case tpo_method:
408                 case tpo_pointer:
409                         return;
410                 default:
411                         break;
412         }
413
414         export_type_common(env, tp);
415
416         switch (get_type_tpop_code(tp))
417         {
418                 case tpo_uninitialized:
419                         panic("invalid type found");
420
421                 case tpo_class:
422                         fputs(get_class_name(tp), f);
423                         fputc(' ', f);
424                         /* TODO: inheritance stuff not supported yet */
425                         printf("Inheritance of classes not supported yet!\n");
426                         break;
427
428                 case tpo_primitive:
429                         write_mode(env, get_type_mode(tp));
430                         break;
431
432                 case tpo_union:
433                 case tpo_struct:
434                 case tpo_enumeration:
435                         fputs(get_compound_name(tp), f);
436                         fputc(' ', f);
437                         break;
438
439                 case tpo_method:
440                 case tpo_pointer:
441                 case tpo_code:
442                 case tpo_array:
443                 case tpo_none:
444                 case tpo_unknown:
445                         break;
446         }
447         fputc('\n', f);
448 }
449
450 static void export_type_post(io_env_t *env, ir_type *tp)
451 {
452         FILE *f = env->file;
453         int i, n, nparams, nresults;
454
455         /* skip types already handled by pre walker */
456         switch (get_type_tpop_code(tp))
457         {
458                 case tpo_class:
459                 case tpo_primitive:
460                 case tpo_struct:
461                 case tpo_union:
462                 case tpo_unknown:
463                         return;
464                 default:
465                         break;
466         }
467
468         export_type_common(env, tp);
469
470         switch (get_type_tpop_code(tp))
471         {
472                 case tpo_array:
473                         n = get_array_n_dimensions(tp);
474                         fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
475                         for (i = 0; i < n; i++) {
476                                 ir_node *lower = get_array_lower_bound(tp, i);
477                                 ir_node *upper = get_array_upper_bound(tp, i);
478
479                                 if (is_Const(lower))
480                                         fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
481                                 else
482                                         panic("Lower array bound is not constant");
483
484                                 if (is_Const(upper))
485                                         fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
486                                 else if (is_Unknown(upper))
487                                         fputs("unknown ", f);
488                                 else
489                                         panic("Upper array bound is not constant");
490                         }
491                         break;
492
493                 case tpo_method:
494                         nparams  = get_method_n_params(tp);
495                         nresults = get_method_n_ress(tp);
496                         fprintf(f, "0x%X 0x%X %i %i ", get_method_calling_convention(tp),
497                                 get_method_additional_properties(tp), nparams, nresults);
498                         for (i = 0; i < nparams; i++)
499                                 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
500                         for (i = 0; i < nresults; i++)
501                                 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
502                         fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
503                         break;
504
505                 case tpo_pointer:
506                         write_mode(env, get_type_mode(tp));
507                         fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
508                         break;
509
510                 default:
511                         printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
512                         break;
513         }
514         fputc('\n', f);
515 }
516
517 static void export_entity(io_env_t *env, ir_entity *ent)
518 {
519         ir_type *owner = get_entity_owner(ent);
520         fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %d %s %s %s %s %s ",
521                         get_entity_nr(ent),
522                         get_entity_name(ent),
523                         ent->ld_name ? get_id_str(ent->ld_name) : "",
524                         get_type_nr(get_entity_type(ent)),
525                         get_type_nr(owner),
526                         get_entity_offset(ent),
527                         (unsigned) get_entity_offset_bits_remainder(ent),
528                         is_entity_compiler_generated(ent),
529                         get_allocation_name(get_entity_allocation(ent)),
530                         get_visibility_name(get_entity_visibility(ent)),
531                         get_variability_name(get_entity_variability(ent)),
532                         get_peculiarity_name(get_entity_peculiarity(ent)),
533                         get_volatility_name(get_entity_volatility(ent)));
534
535         /* TODO: inheritance stuff for class entities not supported yet */
536         if (is_Class_type(owner) && owner != get_glob_type())
537                 printf("Inheritance of class entities not supported yet!\n");
538
539         if (get_entity_variability(ent) != variability_uninitialized &&
540             get_entity_visibility(ent) != visibility_external_allocated)
541         {
542                 if (is_compound_entity(ent)) {
543                         if (has_entity_initializer(ent)) {
544                                 fputs("initializer ", env->file);
545                                 write_initializer(env, get_entity_initializer(ent));
546                         } else {
547                                 int i, n = get_compound_ent_n_values(ent);
548                                 fputs("noninitializer ", env->file);
549                                 fprintf(env->file, "%d ", n);
550                                 for (i = 0; i < n; i++) {
551                                         ir_entity *member = get_compound_ent_value_member(ent, i);
552                                         ir_node   *irn    = get_compound_ent_value(ent, i);
553                                         fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
554                                 }
555                         }
556                 } else {
557                         ir_node *irn = get_atomic_ent_value(ent);
558                         fprintf(env->file, "%ld ", get_irn_node_nr(irn));
559                 }
560         }
561
562         fputc('\n', env->file);
563 }
564
565 static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
566 {
567         io_env_t *env = (io_env_t *) ctx;
568         if (get_kind(tore.typ) == k_type)
569                 export_type_pre(env, tore.typ);
570 }
571
572 static void export_type_or_ent_post(type_or_ent tore, void *ctx)
573 {
574         io_env_t *env = (io_env_t *) ctx;
575
576         switch (get_kind(tore.ent))
577         {
578                 case k_entity:
579                         export_entity(env, tore.ent);
580                         break;
581
582                 case k_type:
583                         export_type_post(env, tore.typ);
584                         break;
585
586                 default:
587                         panic("export_type_or_ent_post: Unknown type or entity.");
588                         break;
589         }
590 }
591
592 /**
593  * Walker: exports every node.
594  */
595 static void export_node(ir_node *irn, void *ctx)
596 {
597         io_env_t *env = (io_env_t *) ctx;
598         int i, n;
599         unsigned opcode = get_irn_opcode(irn);
600
601         if (env->ignoreblocks && opcode == iro_Block)
602                 return;
603
604         fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
605
606         n = get_irn_arity(irn);
607         for (i = -1; i < n; i++) {
608                 ir_node *pred = get_irn_n(irn, i);
609                 if (pred == NULL) {
610                         /* Anchor node may have NULL predecessors */
611                         assert(is_Anchor(irn));
612                         fputs("-1 ", env->file);
613                 } else {
614                         fprintf(env->file, "%ld ", get_irn_node_nr(pred));
615                 }
616         }
617
618         fprintf(env->file, "] { ");
619
620         switch (opcode) {
621                 #include "gen_irio_export.inl"
622         }
623         fputs("}\n", env->file);
624 }
625
626 static void export_modes(io_env_t *env)
627 {
628         int i, n_modes = get_irp_n_modes();
629
630         fputs("modes {\n", env->file);
631
632         for (i = 0; i < n_modes; i++) {
633                 ir_mode *mode = get_irp_mode(i);
634                 switch (get_mode_sort(mode))
635                 {
636                         case irms_auxiliary:
637                         case irms_control_flow:
638                         case irms_memory:
639                         case irms_internal_boolean:
640                                 /* skip "internal" modes, which may not be user defined */
641                                 continue;
642                         default:
643                                 break;
644                 }
645
646                 fprintf(env->file, "\tmode \"%s\" 0x%X %d %d %s %d %d ", get_mode_name(mode),
647                         get_mode_sort(mode), get_mode_size_bits(mode), get_mode_sign(mode),
648                         get_mode_arithmetic_name(get_mode_arithmetic(mode)), get_mode_modulo_shift(mode),
649                         get_mode_n_vector_elems(mode));
650                 if (mode_is_reference(mode)) {
651                         write_mode(env, get_reference_mode_signed_eq(mode));
652                         write_mode(env, get_reference_mode_unsigned_eq(mode));
653                 }
654                 fputc('\n', env->file);
655         }
656
657         fputs("}\n\n", env->file);
658 }
659
660 /* Exports the whole irp to the given file in a textual form. */
661 void ir_export(const char *filename)
662 {
663         io_env_t env;
664         int i, n_irgs = get_irp_n_irgs();
665
666         env.file = fopen(filename, "wt");
667         if (!env.file) {
668                 perror(filename);
669                 return;
670         }
671
672         export_modes(&env);
673
674         fputs("typegraph {\n", env.file);
675
676         type_walk_prog(export_type_or_ent_pre, export_type_or_ent_post, &env);
677
678         for (i = 0; i < n_irgs; i++) {
679                 ir_graph *irg = get_irp_irg(i);
680                 ir_type *valuetype = get_irg_value_param_type(irg);
681
682                 fprintf(env.file, "}\n\nirg %ld %ld %ld {\n", get_entity_nr(get_irg_entity(irg)),
683                         get_type_nr(get_irg_frame_type(irg)),
684                         valuetype == NULL ? -1 : get_type_nr(valuetype));
685
686                 env.ignoreblocks = 0;
687                 irg_block_walk_graph(irg, NULL, export_node, &env);
688
689                 env.ignoreblocks = 1;
690                 irg_walk_anchors(irg, NULL, export_node, &env);
691         }
692
693         fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
694
695         walk_const_code(NULL, export_node, &env);
696         fputs("}\n", env.file);
697
698         fclose(env.file);
699 }
700
701 /* Exports the given irg to the given file. */
702 void ir_export_irg(ir_graph *irg, const char *filename)
703 {
704         io_env_t env;
705
706         env.file = fopen(filename, "wt");
707         if (!env.file) {
708                 perror(filename);
709                 return;
710         }
711
712         export_modes(&env);
713
714         fputs("typegraph {\n", env.file);
715
716         type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
717
718         fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
719
720         env.ignoreblocks = 0;
721         irg_block_walk_graph(irg, NULL, export_node, &env);
722
723         env.ignoreblocks = 1;
724         irg_walk_anchors(irg, NULL, export_node, &env);
725
726         /* TODO: Only output needed constants */
727         fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
728         walk_const_code(NULL, export_node, &env);
729         fputs("}\n", env.file);
730
731         fclose(env.file);
732 }
733
734 static void save_lex_state(io_env_t *env, lex_state_t *state)
735 {
736         state->offs = ftell(env->file);
737         state->line = env->line;
738         state->col  = env->col;
739 }
740
741 static void restore_lex_state(io_env_t *env, lex_state_t *state)
742 {
743         fseek(env->file, state->offs, SEEK_SET);
744         env->line = state->line;
745         env->col  = state->col;
746 }
747
748 static int read_c(io_env_t *env)
749 {
750         int ch = fgetc(env->file);
751         switch (ch)
752         {
753                 case '\t':
754                         env->col += 4;
755                         break;
756
757                 case '\n':
758                         env->col = 0;
759                         env->line++;
760                         break;
761
762                 default:
763                         env->col++;
764                         break;
765         }
766         return ch;
767 }
768
769 /** Returns the first non-whitespace character or EOF. **/
770 static int skip_ws(io_env_t *env)
771 {
772         while (1)
773         {
774                 int ch = read_c(env);
775                 switch (ch)
776                 {
777                         case ' ':
778                         case '\t':
779                         case '\n':
780                         case '\r':
781                                 break;
782
783                         default:
784                                 return ch;
785                 }
786         }
787 }
788
789 static void skip_to(io_env_t *env, char to_ch)
790 {
791         int ch;
792         do
793                 ch = read_c(env);
794         while (ch != to_ch && ch != EOF);
795 }
796
797 static int expect_char(io_env_t *env, char ch)
798 {
799         int curch = skip_ws(env);
800         if (curch != ch) {
801                 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
802                 return 0;
803         }
804         return 1;
805 }
806
807 #define EXPECT(c) if (expect_char(env, (c))) {} else return 0
808 #define EXPECT_OR_EXIT(c) if (expect_char(env, (c))) {} else exit(1)
809
810 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
811 {
812         size_t i;
813         for (i = 0; i < bufsize - 1; i++) {
814                 int ch = read_c(env);
815                 if (ch == EOF) break;
816                 switch (ch)
817                 {
818                         case ' ':
819                         case '\t':
820                         case '\n':
821                         case '\r':
822                                 if (i != 0)
823                                         goto endofword;
824                                 i--;    /* skip whitespace */
825                                 break;
826
827                         default:
828                                 buf[i] = ch;
829                                 break;
830                 }
831         }
832 endofword:
833         buf[i] = 0;
834         return buf;
835 }
836
837 static const char *read_str(io_env_t *env)
838 {
839         static char buf[1024];
840         return read_str_to(env, buf, sizeof(buf));
841 }
842
843 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
844 {
845         size_t i;
846         EXPECT_OR_EXIT('\"');
847         for (i = 0; i < bufsize - 1; i++) {
848                 int ch = read_c(env);
849                 if (ch == EOF) {
850                         printf("Unexpected end of quoted string!\n");
851                         exit(1);
852                 }
853                 if (ch == '\"') break;
854
855                 buf[i] = ch;
856         }
857         if (i == bufsize - 1) {
858                 printf("Quoted string too long!\n");
859                 exit(1);
860         }
861         buf[i] = 0;
862         return buf;
863 }
864
865 static const char *read_qstr(io_env_t *env)
866 {
867         static char buf[1024];
868         return read_qstr_to(env, buf, sizeof(buf));
869 }
870
871 static long read_long2(io_env_t *env, char **endptr)
872 {
873         static char buf[1024];
874         return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
875 }
876
877 static long read_long(io_env_t *env)
878 {
879         return read_long2(env, NULL);
880 }
881
882 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
883 {
884         ir_node *node = (ir_node *) get_id(env, nodenr);
885         if (node && node->kind != k_ir_node)
886                 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
887         return node;
888 }
889
890 static ir_node *get_node(io_env_t *env, long nodenr)
891 {
892         ir_node *node = get_node_or_null(env, nodenr);
893         if (!node)
894                 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
895
896         return node;
897 }
898
899 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
900 {
901         ir_node *node = get_node_or_null(env, nodenr);
902         if (node == NULL) {
903                 node = new_Dummy(mode_X);
904                 set_id(env, nodenr, node);
905         }
906         return node;
907 }
908
909 static ir_type *get_type(io_env_t *env, long typenr)
910 {
911         ir_type *type = (ir_type *) get_id(env, typenr);
912         if (type == NULL)
913                 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
914         else if (type->kind != k_type)
915                 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
916         return type;
917 }
918
919 static ir_type *read_type(io_env_t *env)
920 {
921         return get_type(env, read_long(env));
922 }
923
924 static ir_entity *get_entity(io_env_t *env, long entnr)
925 {
926         ir_entity *entity = (ir_entity *) get_id(env, entnr);
927         if (entity == NULL) {
928                 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
929                 exit(1);
930         } else if (entity->kind != k_entity)
931                 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
932         return entity;
933 }
934
935 static ir_entity *read_entity(io_env_t *env)
936 {
937         return get_entity(env, read_long(env));
938 }
939
940 static ir_mode *read_mode(io_env_t *env)
941 {
942         static char buf[128];
943         int i, n;
944
945         read_str_to(env, buf, sizeof(buf));
946
947         n = get_irp_n_modes();
948         for (i = 0; i < n; i++) {
949                 ir_mode *mode = get_irp_mode(i);
950                 if (!strcmp(buf, get_mode_name(mode)))
951                         return mode;
952         }
953
954         printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
955         return mode_ANY;
956 }
957
958 static const char *get_typetag_name(typetag_t typetag)
959 {
960         switch (typetag)
961         {
962                 case tt_align:       return "align";
963                 case tt_allocation:  return "allocation";
964                 case tt_builtin:     return "builtin kind";
965                 case tt_initializer: return "initializer kind";
966                 case tt_iro:         return "opcode";
967                 case tt_peculiarity: return "peculiarity";
968                 case tt_pin_state:   return "pin state";
969                 case tt_tpo:         return "type";
970                 case tt_type_state:  return "type state";
971                 case tt_variability: return "variability";
972                 case tt_visibility:  return "visibility";
973                 case tt_volatility:  return "volatility";
974                 default: return "<UNKNOWN>";
975         }
976 }
977
978 /**
979  * Read and decode an enum constant.
980  */
981 static unsigned read_enum(io_env_t *env, typetag_t typetag)
982 {
983         static char buf[128];
984         unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
985         if (code != SYMERROR)
986                 return code;
987
988         printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
989         return 0;
990 }
991
992 #define read_align(env)              ((ir_align)              read_enum(env, tt_align))
993 #define read_allocation(env)         ((ir_allocation)         read_enum(env, tt_allocation))
994 #define read_builtin_kind(env)       ((ir_builtin_kind)       read_enum(env, tt_builtin))
995 #define read_cond_kind(env)          ((cond_kind)             read_enum(env, tt_cond_kind))
996 #define read_cond_jmp_predicate(env) ((cond_jmp_predicate)    read_enum(env, tt_cond_jmp_predicate))
997 #define read_initializer_kind(env)   ((ir_initializer_kind_t) read_enum(env, tt_initializer))
998 #define read_mode_arithmetic(env)    ((ir_mode_arithmetic)    read_enum(env, tt_mode_arithmetic))
999 #define read_peculiarity(env)        ((ir_peculiarity)        read_enum(env, tt_peculiarity))
1000 #define read_pin_state(env)          ((op_pin_state)          read_enum(env, tt_pin_state))
1001 #define read_type_state(env)         ((ir_type_state)         read_enum(env, tt_type_state))
1002 #define read_variability(env)        ((ir_variability)        read_enum(env, tt_variability))
1003 #define read_visibility(env)         ((ir_visibility)         read_enum(env, tt_visibility))
1004 #define read_volatility(env)         ((ir_volatility)         read_enum(env, tt_volatility))
1005
1006 static ir_cons_flags get_cons_flags(io_env_t *env)
1007 {
1008         ir_cons_flags flags = cons_none;
1009
1010         op_pin_state pinstate = read_pin_state(env);
1011         switch (pinstate)
1012         {
1013                 case op_pin_state_floats: flags |= cons_floats; break;
1014                 case op_pin_state_pinned: break;
1015                 default:
1016                         panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
1017         }
1018
1019         if (read_volatility(env) == volatility_is_volatile)
1020                 flags |= cons_volatile;
1021         if (read_align(env) == align_non_aligned)
1022                 flags |= cons_unaligned;
1023
1024         return flags;
1025 }
1026
1027 static tarval *read_tv(io_env_t *env)
1028 {
1029         static char buf[128];
1030         ir_mode *tvmode = read_mode(env);
1031         read_str_to(env, buf, sizeof(buf));
1032         return new_tarval_from_str(buf, strlen(buf), tvmode);
1033 }
1034
1035 static ir_initializer_t *read_initializer(io_env_t *env)
1036 {
1037         ir_initializer_kind_t ini_kind = read_initializer_kind(env);
1038
1039         switch (ini_kind)
1040         {
1041                 case IR_INITIALIZER_CONST:
1042                 {
1043                         ir_node *irn = get_node_or_dummy(env, read_long(env));
1044                         return create_initializer_const(irn);
1045                 }
1046
1047                 case IR_INITIALIZER_TARVAL:
1048                         return create_initializer_tarval(read_tv(env));
1049
1050                 case IR_INITIALIZER_NULL:
1051                         return get_initializer_null();
1052
1053                 case IR_INITIALIZER_COMPOUND:
1054                 {
1055                         unsigned i, n = (unsigned) read_long(env);
1056                         ir_initializer_t *ini = create_initializer_compound(n);
1057                         for (i = 0; i < n; i++) {
1058                                 ir_initializer_t *curini = read_initializer(env);
1059                                 set_initializer_compound_value(ini, i, curini);
1060                         }
1061                         return ini;
1062                 }
1063
1064                 default:
1065                         panic("Unknown initializer kind");
1066         }
1067 }
1068
1069
1070 /** Reads a type description and remembers it by its id. */
1071 static void import_type(io_env_t *env, keyword_t kwkind)
1072 {
1073         int            i;
1074         ir_type       *type;
1075         long           typenr = read_long(env);
1076         const char    *tpop   = read_str(env);
1077         unsigned       size   = (unsigned) read_long(env);
1078         unsigned       align  = (unsigned) read_long(env);
1079         ir_type_state  state  = read_type_state(env);
1080         ir_visibility  vis    = read_visibility(env);
1081
1082         const char    *kindstr;
1083
1084         if (kwkind == kw_frametype) {
1085                 if (symbol(tpop, tt_tpo) != tpo_class) {
1086                         printf("Frame type must be a class type in line %i:%i\n", env->line, env->col);
1087                         skip_to(env, '\n');
1088                         return;
1089                 }
1090
1091                 type = new_type_frame();
1092                 set_type_size_bytes(type, size);
1093
1094                 kindstr = "frametype";
1095         } else if (kwkind == kw_valuetype) {
1096                 if (symbol(tpop, tt_tpo) != tpo_struct) {
1097                         printf("Value type must be a struct type in line %i:%i\n", env->line, env->col);
1098                         skip_to(env, '\n');
1099                         return;
1100                 }
1101
1102                 type = new_type_value();
1103                 set_type_size_bytes(type, size);
1104
1105                 kindstr = "valuetype";
1106         } else {
1107                 switch (symbol(tpop, tt_tpo))
1108                 {
1109                         case tpo_array:
1110                         {
1111                                 int ndims = (int) read_long(env);
1112                                 long elemtypenr = read_long(env);
1113                                 ir_type *elemtype = get_type(env, elemtypenr);
1114
1115                                 type = new_type_array(ndims, elemtype);
1116                                 for (i = 0; i < ndims; i++) {
1117                                         const char *str = read_str(env);
1118                                         if (strcmp(str, "unknown") != 0) {
1119                                                 long lowerbound = strtol(str, NULL, 0);
1120                                                 set_array_lower_bound_int(type, i, lowerbound);
1121                                         }
1122                                         str = read_str(env);
1123                                         if (strcmp(str, "unknown") != 0) {
1124                                                 long upperbound = strtol(str, NULL, 0);
1125                                                 set_array_upper_bound_int(type, i, upperbound);
1126                                         }
1127                                 }
1128                                 set_type_size_bytes(type, size);
1129                                 break;
1130                         }
1131
1132                         case tpo_class: {
1133                                 const char *name = read_qstr(env);
1134                                 ident      *id   = new_id_from_str(name);
1135
1136                                 if(typenr == (long) IR_SEGMENT_GLOBAL)
1137                                         type = get_glob_type();
1138                                 else
1139                                         type = new_type_class(id);
1140                                 set_type_size_bytes(type, size);
1141                                 break;
1142                         }
1143
1144                         case tpo_method:
1145                         {
1146                                 unsigned callingconv = (unsigned) read_long(env);
1147                                 unsigned addprops    = (unsigned) read_long(env);
1148                                 int nparams          = (int)      read_long(env);
1149                                 int nresults         = (int)      read_long(env);
1150                                 int variaindex;
1151
1152                                 type = new_type_method(nparams, nresults);
1153
1154                                 for (i = 0; i < nparams; i++) {
1155                                         long     typenr = read_long(env);
1156                                         ir_type *paramtype = get_type(env, typenr);
1157
1158                                         set_method_param_type(type, i, paramtype);
1159                                 }
1160                                 for (i = 0; i < nresults; i++) {
1161                                         long typenr = read_long(env);
1162                                         ir_type *restype = get_type(env, typenr);
1163
1164                                         set_method_res_type(type, i, restype);
1165                                 }
1166
1167                                 variaindex = (int) read_long(env);
1168                                 if (variaindex != -1) {
1169                                         set_method_variadicity(type, variadicity_variadic);
1170                                         if (variaindex != nparams)
1171                                                 set_method_first_variadic_param_index(type, variaindex);
1172                                 }
1173
1174                                 set_method_calling_convention(type, callingconv);
1175                                 set_method_additional_properties(type, addprops);
1176                                 break;
1177                         }
1178
1179                         case tpo_pointer:
1180                         {
1181                                 ir_mode *mode     = read_mode(env);
1182                                 ir_type *pointsto = get_type(env, read_long(env));
1183                                 type = new_type_pointer(pointsto);
1184                                 set_type_mode(type, mode);
1185                                 break;
1186                         }
1187
1188                         case tpo_primitive:
1189                         {
1190                                 ir_mode *mode = read_mode(env);
1191                                 type = new_type_primitive(mode);
1192                                 break;
1193                         }
1194
1195                         case tpo_struct: {
1196                                 const char *name = read_qstr(env);
1197                                 ident      *id   = new_id_from_str(name);
1198
1199                                 if(typenr < (long) IR_SEGMENT_COUNT)
1200                                         type = get_segment_type((ir_segment_t) typenr);
1201                                 else
1202                                         type = new_type_struct(id);
1203                                 set_type_size_bytes(type, size);
1204                                 break;
1205                         }
1206
1207                         case tpo_union: {
1208                                 const char *name = read_qstr(env);
1209                                 ident      *id   = new_id_from_str(name);
1210
1211                                 type = new_type_union(id);
1212                                 set_type_size_bytes(type, size);
1213                                 break;
1214                         }
1215
1216                         case tpo_unknown:
1217                                 return;   /* ignore unknown type */
1218
1219                         default:
1220                                 if (typenr != 0)  /* ignore global type */
1221                                         printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
1222                                 skip_to(env, '\n');
1223                                 return;
1224                 }
1225                 kindstr = "type";
1226         }
1227
1228         set_type_alignment_bytes(type, align);
1229         set_type_visibility(type, vis);
1230
1231         if (state == layout_fixed)
1232                 ARR_APP1(ir_type *, env->fixedtypes, type);
1233
1234         set_id(env, typenr, type);
1235         printf("Insert %s %ld\n", kindstr, typenr);
1236 }
1237
1238 /** Reads an entity description and remembers it by its id. */
1239 static void import_entity(io_env_t *env)
1240 {
1241         char          buf[1024], buf2[1024];
1242         long          entnr       = read_long(env);
1243         const char   *name        = read_qstr_to(env, buf, sizeof(buf));
1244         const char   *ld_name     = read_qstr_to(env, buf2, sizeof(buf2));
1245         long          typenr      = read_long(env);
1246         long          ownertypenr = read_long(env);
1247
1248         ir_type   *type      = get_type(env, typenr);
1249         ir_type   *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1250         ir_entity *entity    = new_entity(ownertype, new_id_from_str(name), type);
1251
1252         if (*ld_name)
1253                 set_entity_ld_ident(entity, new_id_from_str(ld_name));
1254         set_entity_offset     (entity, (int) read_long(env));
1255         set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1256         set_entity_compiler_generated(entity, (int) read_long(env));
1257         set_entity_allocation (entity, read_allocation(env));
1258         set_entity_visibility (entity, read_visibility(env));
1259         set_entity_variability(entity, read_variability(env));
1260         set_entity_peculiarity(entity, read_peculiarity(env));
1261         set_entity_volatility (entity, read_volatility(env));
1262
1263         if (get_entity_variability(entity) != variability_uninitialized &&
1264             get_entity_visibility(entity) != visibility_external_allocated)
1265         {
1266                 if (is_compound_entity(entity)) {
1267                         if (!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer")) {
1268                                 set_entity_initializer(entity, read_initializer(env));
1269                         } else {
1270                                 int i, n = (int) read_long(env);
1271                                 for (i = 0; i < n; i++) {
1272                                         ir_entity *member = get_entity(env, read_long(env));
1273                                         ir_node   *irn    = get_node_or_dummy(env, read_long(env));
1274                                         add_compound_ent_value(entity, irn, member);
1275                                 }
1276                         }
1277                 } else {
1278                         ir_node *irn = get_node_or_dummy(env, read_long(env));
1279                         set_atomic_ent_value(entity, irn);
1280                 }
1281         }
1282
1283         set_id(env, entnr, entity);
1284         printf("Insert entity %s %ld\n", name, entnr);
1285 }
1286
1287 /** Parses the whole type graph. */
1288 static int parse_typegraph(io_env_t *env)
1289 {
1290         const char *kind;
1291         keyword_t kwkind;
1292         lex_state_t oldstate;
1293
1294         EXPECT('{');
1295
1296         save_lex_state(env, &oldstate);
1297
1298         current_ir_graph = get_const_code_irg();
1299
1300         /* parse all types first */
1301         while (1) {
1302                 kind = read_str(env);
1303                 if (kind[0] == '}' && kind[1] == '\0')
1304                         break;
1305
1306                 kwkind = (keyword_t) symbol(kind, tt_keyword);
1307                 switch (kwkind)
1308                 {
1309                         case kw_type:
1310                         case kw_frametype:
1311                         case kw_valuetype:
1312                                 import_type(env, kwkind);
1313                                 break;
1314
1315                         default:
1316                                 skip_to(env, '\n');
1317                                 break;
1318                 }
1319         }
1320
1321         /* now parse rest */
1322         restore_lex_state(env, &oldstate);
1323
1324         while (1) {
1325                 kind = read_str(env);
1326                 if (kind[0] == '}' && kind[1] == '\0')
1327                         break;
1328
1329                 switch (symbol(kind, tt_keyword))
1330                 {
1331                         case kw_type:
1332                         case kw_frametype:
1333                         case kw_valuetype:
1334                                 skip_to(env, '\n');
1335                                 break;
1336
1337                         case kw_entity:
1338                                 import_entity(env);
1339                                 break;
1340
1341                         default:
1342                                 printf("Type graph element not supported yet: \"%s\"\n", kind);
1343                                 skip_to(env, '\n');
1344                                 break;
1345                 }
1346         }
1347         return 1;
1348 }
1349
1350 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1351 {
1352         int numpreds;
1353         *nodename = read_str(env);
1354         if ((*nodename)[0] == '}' && !(*nodename)[1]) return -1;  /* end-of-graph */
1355
1356         *nodenr = read_long(env);
1357
1358         ARR_RESIZE(ir_node *, *preds, 0);
1359
1360         EXPECT('[');
1361         for (numpreds = 0; !feof(env->file); numpreds++) {
1362                 char *endptr;
1363                 ARR_APP1(long, *preds, read_long2(env, &endptr));
1364                 if (*endptr == ']') break;
1365         }
1366         return numpreds;
1367 }
1368
1369 /** Parses an IRG. */
1370 static int parse_graph(io_env_t *env, ir_graph *irg)
1371 {
1372         long       *preds = NEW_ARR_F(long, 16);
1373         ir_node   **prednodes = NEW_ARR_F(ir_node *, 16);
1374         int         i, numpreds, ret = 1;
1375         long        nodenr;
1376         const char *nodename;
1377         ir_node    *node, *newnode;
1378
1379         current_ir_graph = irg;
1380
1381         EXPECT('{');
1382
1383         while (1) {
1384                 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1385                 if (numpreds == -1) break;  /* end-of-graph */
1386                 if (!numpreds) {
1387                         printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1388                         ret = 0;
1389                         break;
1390                 }
1391
1392                 ARR_RESIZE(ir_node *, prednodes, numpreds);
1393                 for (i = 0; i < numpreds - 1; i++)
1394                         prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1395
1396                 node = get_node_or_null(env, nodenr);
1397                 newnode = NULL;
1398
1399                 EXPECT('{');
1400
1401                 switch (symbol(nodename, tt_iro))
1402                 {
1403                         case iro_End:
1404                         {
1405                                 ir_node *newendblock = get_node(env, preds[0]);
1406                                 newnode = get_irg_end(current_ir_graph);
1407                                 exchange(get_nodes_block(newnode), newendblock);
1408                                 for (i = 0; i < numpreds - 1; i++)
1409                                         add_irn_n(newnode, prednodes[i]);
1410                                 break;
1411                         }
1412
1413                         case iro_Start:
1414                         {
1415                                 ir_node *newstartblock = get_node(env, preds[0]);
1416                                 newnode = get_irg_start(current_ir_graph);
1417                                 exchange(get_nodes_block(newnode), newstartblock);
1418                                 break;
1419                         }
1420
1421                         case iro_Block:
1422                         {
1423                                 if (preds[0] != nodenr) {
1424                                         printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1425                                                 preds[0], nodenr);
1426                                         ret = 0;
1427                                         goto endloop;
1428                                 }
1429
1430                                 newnode = new_Block(numpreds - 1, prednodes);
1431                                 break;
1432                         }
1433
1434                         case iro_Anchor:
1435                                 newnode = current_ir_graph->anchor;
1436                                 for (i = 0; i < numpreds - 1; i++)
1437                                         set_irn_n(newnode, i, prednodes[i]);
1438                                 set_irn_n(newnode, -1, get_node(env, preds[0]));
1439                                 break;
1440
1441                         case iro_SymConst:
1442                         {
1443                                 long entnr = read_long(env);
1444                                 union symconst_symbol sym;
1445                                 sym.entity_p = get_entity(env, entnr);
1446                                 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1447                                 break;
1448                         }
1449
1450                         #include "gen_irio_import.inl"
1451
1452                         default:
1453                                 goto notsupported;
1454                 }
1455
1456                 EXPECT('}');
1457
1458                 if (!newnode) {
1459 notsupported:
1460                         panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1461                 }
1462
1463                 if (node)
1464                         exchange(node, newnode);
1465                 /* Always update hash entry to avoid more uses of id nodes */
1466                 set_id(env, nodenr, newnode);
1467                 /* printf("Insert %s %ld\n", nodename, nodenr); */
1468         }
1469
1470 endloop:
1471         DEL_ARR_F(preds);
1472         DEL_ARR_F(prednodes);
1473
1474         return ret;
1475 }
1476
1477 static int parse_modes(io_env_t *env)
1478 {
1479         const char *kind;
1480         keyword_t kwkind;
1481
1482         EXPECT('{');
1483
1484         while (1) {
1485                 kind = read_str(env);
1486                 if (kind[0] == '}' && kind[1] == '\0')
1487                         break;
1488
1489                 kwkind = (keyword_t) symbol(kind, tt_keyword);
1490                 switch (kwkind)
1491                 {
1492                         case kw_mode:
1493                         {
1494                                 const char *name = read_qstr(env);
1495                                 ir_mode_sort sort = (ir_mode_sort) read_long(env);
1496                                 int size = read_long(env);
1497                                 int sign = read_long(env);
1498                                 ir_mode_arithmetic arith = read_mode_arithmetic(env);
1499                                 unsigned modulo_shift = read_long(env);
1500                                 int vector_elems = read_long(env);
1501                                 ir_mode *mode;
1502
1503                                 if (vector_elems != 1) {
1504                                         panic("no support for import of vector modes yes");
1505                                 }
1506
1507                                 mode = new_ir_mode(name, sort, size, sign, arith, modulo_shift);
1508                                 if (mode_is_reference(mode)) {
1509                                         set_reference_mode_signed_eq(mode, read_mode(env));
1510                                         set_reference_mode_unsigned_eq(mode, read_mode(env));
1511                                 }
1512                                 break;
1513                         }
1514
1515                         default:
1516                                 skip_to(env, '\n');
1517                                 break;
1518                 }
1519         }
1520         return 1;
1521 }
1522
1523 /** Imports an previously exported textual representation of an (maybe partial) irp */
1524 void ir_import(const char *filename)
1525 {
1526         int oldoptimize = get_optimize();
1527         firm_verification_t oldver = get_node_verification_mode();
1528         io_env_t ioenv;
1529         io_env_t *env = &ioenv;
1530         int i, n;
1531
1532         symtbl_init();
1533
1534         memset(env, 0, sizeof(*env));
1535         env->idset = new_set(id_cmp, 128);
1536         env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1537
1538         env->file = fopen(filename, "rt");
1539         if (!env->file) {
1540                 perror(filename);
1541                 exit(1);
1542         }
1543
1544         set_optimize(0);
1545         do_node_verification(FIRM_VERIFICATION_OFF);
1546
1547         while (1) {
1548                 const char *str = read_str(env);
1549                 if (!*str) break;
1550                 switch (symbol(str, tt_keyword))
1551                 {
1552                         case kw_modes:
1553                                 if (!parse_modes(env)) goto end;
1554                                 break;
1555
1556                         case kw_typegraph:
1557                                 if (!parse_typegraph(env)) goto end;
1558                                 break;
1559
1560                         case kw_irg:
1561                         {
1562                                 ir_entity *irgent = get_entity(env, read_long(env));
1563                                 long valuetypeid;
1564                                 ir_graph *irg = new_ir_graph(irgent, 0);
1565                                 set_irg_frame_type(irg, get_type(env, read_long(env)));
1566                                 valuetypeid = read_long(env);
1567                                 if (valuetypeid != -1)
1568                                         set_method_value_param_type(get_entity_type(irgent),
1569                                                         get_type(env, valuetypeid));
1570
1571                                 if (!parse_graph(env, irg)) goto end;
1572                                 break;
1573                         }
1574
1575                         case kw_constirg:
1576                         {
1577                                 ir_graph *constirg = get_const_code_irg();
1578                                 long bodyblockid = read_long(env);
1579                                 set_id(env, bodyblockid, constirg->current_block);
1580                                 if (!parse_graph(env, constirg)) goto end;
1581                                 break;
1582                         }
1583                 }
1584         }
1585
1586 end:
1587         n = ARR_LEN(env->fixedtypes);
1588         for (i = 0; i < n; i++)
1589                 set_type_state(env->fixedtypes[i], layout_fixed);
1590
1591         DEL_ARR_F(env->fixedtypes);
1592
1593         del_set(env->idset);
1594
1595         irp_finalize_cons();
1596
1597         do_node_verification(oldver);
1598         set_optimize(oldoptimize);
1599
1600         fclose(env->file);
1601 }