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