9b4c336ba9b5d642517b4a94cbe9b8b769d2abbd
[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 \"%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_name(tp),
394                         get_type_size_bytes(tp),
395                         get_type_alignment_bytes(tp),
396                         get_type_state_name(get_type_state(tp)),
397                         get_visibility_name(get_type_visibility(tp)));
398 }
399
400 static void export_type_pre(io_env_t *env, ir_type *tp)
401 {
402         FILE *f = env->file;
403
404         /* skip types to be handled by post walker */
405         switch (get_type_tpop_code(tp))
406         {
407                 case tpo_array:
408                 case tpo_method:
409                 case tpo_pointer:
410                         return;
411                 default:
412                         break;
413         }
414
415         export_type_common(env, tp);
416
417         switch (get_type_tpop_code(tp))
418         {
419                 case tpo_class:
420                         /* TODO: inheritance stuff not supported yet */
421                         printf("Inheritance of classes not supported yet!\n");
422                         break;
423
424                 case tpo_primitive:
425                         write_mode(env, get_type_mode(tp));
426                         break;
427
428                 case tpo_struct:
429                         break;
430
431                 case tpo_union:
432                         break;
433
434                 case tpo_unknown:
435                         break;
436
437                 default:
438                         printf("export_type_pre: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
439                         break;
440         }
441         fputc('\n', f);
442 }
443
444 static void export_type_post(io_env_t *env, ir_type *tp)
445 {
446         FILE *f = env->file;
447         int i, n, nparams, nresults;
448
449         /* skip types already handled by pre walker */
450         switch (get_type_tpop_code(tp))
451         {
452                 case tpo_class:
453                 case tpo_primitive:
454                 case tpo_struct:
455                 case tpo_union:
456                 case tpo_unknown:
457                         return;
458                 default:
459                         break;
460         }
461
462         export_type_common(env, tp);
463
464         switch (get_type_tpop_code(tp))
465         {
466                 case tpo_array:
467                         n = get_array_n_dimensions(tp);
468                         fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
469                         for (i = 0; i < n; i++) {
470                                 ir_node *lower = get_array_lower_bound(tp, i);
471                                 ir_node *upper = get_array_upper_bound(tp, i);
472
473                                 if (is_Const(lower))
474                                         fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
475                                 else
476                                         panic("Lower array bound is not constant");
477
478                                 if (is_Const(upper))
479                                         fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
480                                 else if (is_Unknown(upper))
481                                         fputs("unknown ", f);
482                                 else
483                                         panic("Upper array bound is not constant");
484                         }
485                         break;
486
487                 case tpo_method:
488                         nparams  = get_method_n_params(tp);
489                         nresults = get_method_n_ress(tp);
490                         fprintf(f, "0x%X 0x%X %i %i ", get_method_calling_convention(tp),
491                                 get_method_additional_properties(tp), nparams, nresults);
492                         for (i = 0; i < nparams; i++)
493                                 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
494                         for (i = 0; i < nresults; i++)
495                                 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
496                         fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
497                         break;
498
499                 case tpo_pointer:
500                         write_mode(env, get_type_mode(tp));
501                         fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
502                         break;
503
504                 default:
505                         printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
506                         break;
507         }
508         fputc('\n', f);
509 }
510
511 static void export_entity(io_env_t *env, ir_entity *ent)
512 {
513         ir_type *owner = get_entity_owner(ent);
514         fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %d %s %s %s %s %s ",
515                         get_entity_nr(ent),
516                         get_entity_name(ent),
517                         ent->ld_name ? get_id_str(ent->ld_name) : "",
518                         get_type_nr(get_entity_type(ent)),
519                         get_type_nr(owner),
520                         get_entity_offset(ent),
521                         (unsigned) get_entity_offset_bits_remainder(ent),
522                         is_entity_compiler_generated(ent),
523                         get_allocation_name(get_entity_allocation(ent)),
524                         get_visibility_name(get_entity_visibility(ent)),
525                         get_variability_name(get_entity_variability(ent)),
526                         get_peculiarity_name(get_entity_peculiarity(ent)),
527                         get_volatility_name(get_entity_volatility(ent)));
528
529         /* TODO: inheritance stuff for class entities not supported yet */
530         if (is_Class_type(owner) && owner != get_glob_type())
531                 printf("Inheritance of class entities not supported yet!\n");
532
533         if (get_entity_variability(ent) != variability_uninitialized &&
534             get_entity_visibility(ent) != visibility_external_allocated)
535         {
536                 if (is_compound_entity(ent)) {
537                         if (has_entity_initializer(ent)) {
538                                 fputs("initializer ", env->file);
539                                 write_initializer(env, get_entity_initializer(ent));
540                         } else {
541                                 int i, n = get_compound_ent_n_values(ent);
542                                 fputs("noninitializer ", env->file);
543                                 fprintf(env->file, "%d ", n);
544                                 for (i = 0; i < n; i++) {
545                                         ir_entity *member = get_compound_ent_value_member(ent, i);
546                                         ir_node   *irn    = get_compound_ent_value(ent, i);
547                                         fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
548                                 }
549                         }
550                 } else {
551                         ir_node *irn = get_atomic_ent_value(ent);
552                         fprintf(env->file, "%ld ", get_irn_node_nr(irn));
553                 }
554         }
555
556         fputc('\n', env->file);
557 }
558
559 static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
560 {
561         io_env_t *env = (io_env_t *) ctx;
562         if (get_kind(tore.typ) == k_type)
563                 export_type_pre(env, tore.typ);
564 }
565
566 static void export_type_or_ent_post(type_or_ent tore, void *ctx)
567 {
568         io_env_t *env = (io_env_t *) ctx;
569
570         switch (get_kind(tore.ent))
571         {
572                 case k_entity:
573                         export_entity(env, tore.ent);
574                         break;
575
576                 case k_type:
577                         export_type_post(env, tore.typ);
578                         break;
579
580                 default:
581                         panic("export_type_or_ent_post: Unknown type or entity.");
582                         break;
583         }
584 }
585
586 /**
587  * Walker: exports every node.
588  */
589 static void export_node(ir_node *irn, void *ctx)
590 {
591         io_env_t *env = (io_env_t *) ctx;
592         int i, n;
593         unsigned opcode = get_irn_opcode(irn);
594
595         if (env->ignoreblocks && opcode == iro_Block)
596                 return;
597
598         fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
599
600         n = get_irn_arity(irn);
601         for (i = -1; i < n; i++) {
602                 ir_node *pred = get_irn_n(irn, i);
603                 if (pred == NULL) {
604                         /* Anchor node may have NULL predecessors */
605                         assert(is_Anchor(irn));
606                         fputs("-1 ", env->file);
607                 } else {
608                         fprintf(env->file, "%ld ", get_irn_node_nr(pred));
609                 }
610         }
611
612         fprintf(env->file, "] { ");
613
614         switch (opcode) {
615                 #include "gen_irio_export.inl"
616         }
617         fputs("}\n", env->file);
618 }
619
620 static void export_modes(io_env_t *env)
621 {
622         int i, n_modes = get_irp_n_modes();
623
624         fputs("modes {\n", env->file);
625
626         for (i = 0; i < n_modes; i++) {
627                 ir_mode *mode = get_irp_mode(i);
628                 switch (get_mode_sort(mode))
629                 {
630                         case irms_auxiliary:
631                         case irms_control_flow:
632                         case irms_memory:
633                         case irms_internal_boolean:
634                                 /* skip "internal" modes, which may not be user defined */
635                                 continue;
636                         default:
637                                 break;
638                 }
639
640                 fprintf(env->file, "\tmode \"%s\" 0x%X %d %d %s %d %d ", get_mode_name(mode),
641                         get_mode_sort(mode), get_mode_size_bits(mode), get_mode_sign(mode),
642                         get_mode_arithmetic_name(get_mode_arithmetic(mode)), get_mode_modulo_shift(mode),
643                         get_mode_n_vector_elems(mode));
644                 if (mode_is_reference(mode)) {
645                         write_mode(env, get_reference_mode_signed_eq(mode));
646                         write_mode(env, get_reference_mode_unsigned_eq(mode));
647                 }
648                 fputc('\n', env->file);
649         }
650
651         fputs("}\n\n", env->file);
652 }
653
654 /* Exports the whole irp to the given file in a textual form. */
655 void ir_export(const char *filename)
656 {
657         io_env_t env;
658         int i, n_irgs = get_irp_n_irgs();
659
660         env.file = fopen(filename, "wt");
661         if (!env.file) {
662                 perror(filename);
663                 return;
664         }
665
666         export_modes(&env);
667
668         fputs("typegraph {\n", env.file);
669
670         type_walk_prog(export_type_or_ent_pre, export_type_or_ent_post, &env);
671
672         for (i = 0; i < n_irgs; i++) {
673                 ir_graph *irg = get_irp_irg(i);
674                 ir_type *valuetype = get_irg_value_param_type(irg);
675
676                 fprintf(env.file, "}\n\nirg %ld %ld %ld {\n", get_entity_nr(get_irg_entity(irg)),
677                         get_type_nr(get_irg_frame_type(irg)),
678                         valuetype == NULL ? -1 : get_type_nr(valuetype));
679
680                 env.ignoreblocks = 0;
681                 irg_block_walk_graph(irg, NULL, export_node, &env);
682
683                 env.ignoreblocks = 1;
684                 irg_walk_anchors(irg, NULL, export_node, &env);
685         }
686
687         fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
688
689         walk_const_code(NULL, export_node, &env);
690         fputs("}\n", env.file);
691
692         fclose(env.file);
693 }
694
695 /* Exports the given irg to the given file. */
696 void ir_export_irg(ir_graph *irg, const char *filename)
697 {
698         io_env_t env;
699
700         env.file = fopen(filename, "wt");
701         if (!env.file) {
702                 perror(filename);
703                 return;
704         }
705
706         export_modes(&env);
707
708         fputs("typegraph {\n", env.file);
709
710         type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
711
712         fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
713
714         env.ignoreblocks = 0;
715         irg_block_walk_graph(irg, NULL, export_node, &env);
716
717         env.ignoreblocks = 1;
718         irg_walk_anchors(irg, NULL, export_node, &env);
719
720         /* TODO: Only output needed constants */
721         fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
722         walk_const_code(NULL, export_node, &env);
723         fputs("}\n", env.file);
724
725         fclose(env.file);
726 }
727
728 static void save_lex_state(io_env_t *env, lex_state_t *state)
729 {
730         state->offs = ftell(env->file);
731         state->line = env->line;
732         state->col  = env->col;
733 }
734
735 static void restore_lex_state(io_env_t *env, lex_state_t *state)
736 {
737         fseek(env->file, state->offs, SEEK_SET);
738         env->line = state->line;
739         env->col  = state->col;
740 }
741
742 static int read_c(io_env_t *env)
743 {
744         int ch = fgetc(env->file);
745         switch (ch)
746         {
747                 case '\t':
748                         env->col += 4;
749                         break;
750
751                 case '\n':
752                         env->col = 0;
753                         env->line++;
754                         break;
755
756                 default:
757                         env->col++;
758                         break;
759         }
760         return ch;
761 }
762
763 /** Returns the first non-whitespace character or EOF. **/
764 static int skip_ws(io_env_t *env)
765 {
766         while (1)
767         {
768                 int ch = read_c(env);
769                 switch (ch)
770                 {
771                         case ' ':
772                         case '\t':
773                         case '\n':
774                         case '\r':
775                                 break;
776
777                         default:
778                                 return ch;
779                 }
780         }
781 }
782
783 static void skip_to(io_env_t *env, char to_ch)
784 {
785         int ch;
786         do
787                 ch = read_c(env);
788         while (ch != to_ch && ch != EOF);
789 }
790
791 static int expect_char(io_env_t *env, char ch)
792 {
793         int curch = skip_ws(env);
794         if (curch != ch) {
795                 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
796                 return 0;
797         }
798         return 1;
799 }
800
801 #define EXPECT(c) if (expect_char(env, (c))) {} else return 0
802 #define EXPECT_OR_EXIT(c) if (expect_char(env, (c))) {} else exit(1)
803
804 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
805 {
806         size_t i;
807         for (i = 0; i < bufsize - 1; i++) {
808                 int ch = read_c(env);
809                 if (ch == EOF) break;
810                 switch (ch)
811                 {
812                         case ' ':
813                         case '\t':
814                         case '\n':
815                         case '\r':
816                                 if (i != 0)
817                                         goto endofword;
818                                 i--;    /* skip whitespace */
819                                 break;
820
821                         default:
822                                 buf[i] = ch;
823                                 break;
824                 }
825         }
826 endofword:
827         buf[i] = 0;
828         return buf;
829 }
830
831 static const char *read_str(io_env_t *env)
832 {
833         static char buf[1024];
834         return read_str_to(env, buf, sizeof(buf));
835 }
836
837 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
838 {
839         size_t i;
840         EXPECT_OR_EXIT('\"');
841         for (i = 0; i < bufsize - 1; i++) {
842                 int ch = read_c(env);
843                 if (ch == EOF) {
844                         printf("Unexpected end of quoted string!\n");
845                         exit(1);
846                 }
847                 if (ch == '\"') break;
848
849                 buf[i] = ch;
850         }
851         if (i == bufsize - 1) {
852                 printf("Quoted string too long!\n");
853                 exit(1);
854         }
855         buf[i] = 0;
856         return buf;
857 }
858
859 static const char *read_qstr(io_env_t *env)
860 {
861         static char buf[1024];
862         return read_qstr_to(env, buf, sizeof(buf));
863 }
864
865 static long read_long2(io_env_t *env, char **endptr)
866 {
867         static char buf[1024];
868         return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
869 }
870
871 static long read_long(io_env_t *env)
872 {
873         return read_long2(env, NULL);
874 }
875
876 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
877 {
878         ir_node *node = (ir_node *) get_id(env, nodenr);
879         if (node && node->kind != k_ir_node)
880                 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
881         return node;
882 }
883
884 static ir_node *get_node(io_env_t *env, long nodenr)
885 {
886         ir_node *node = get_node_or_null(env, nodenr);
887         if (!node)
888                 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
889
890         return node;
891 }
892
893 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
894 {
895         ir_node *node = get_node_or_null(env, nodenr);
896         if (node == NULL) {
897                 node = new_Dummy(mode_X);
898                 set_id(env, nodenr, node);
899         }
900         return node;
901 }
902
903 static ir_type *get_type(io_env_t *env, long typenr)
904 {
905         ir_type *type = (ir_type *) get_id(env, typenr);
906         if (type == NULL)
907                 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
908         else if (type->kind != k_type)
909                 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
910         return type;
911 }
912
913 static ir_type *read_type(io_env_t *env)
914 {
915         return get_type(env, read_long(env));
916 }
917
918 static ir_entity *get_entity(io_env_t *env, long entnr)
919 {
920         ir_entity *entity = (ir_entity *) get_id(env, entnr);
921         if (entity == NULL) {
922                 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
923                 exit(1);
924         } else if (entity->kind != k_entity)
925                 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
926         return entity;
927 }
928
929 static ir_entity *read_entity(io_env_t *env)
930 {
931         return get_entity(env, read_long(env));
932 }
933
934 static ir_mode *read_mode(io_env_t *env)
935 {
936         static char buf[128];
937         int i, n;
938
939         read_str_to(env, buf, sizeof(buf));
940
941         n = get_irp_n_modes();
942         for (i = 0; i < n; i++) {
943                 ir_mode *mode = get_irp_mode(i);
944                 if (!strcmp(buf, get_mode_name(mode)))
945                         return mode;
946         }
947
948         printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
949         return mode_ANY;
950 }
951
952 static const char *get_typetag_name(typetag_t typetag)
953 {
954         switch (typetag)
955         {
956                 case tt_align:       return "align";
957                 case tt_allocation:  return "allocation";
958                 case tt_builtin:     return "builtin kind";
959                 case tt_initializer: return "initializer kind";
960                 case tt_iro:         return "opcode";
961                 case tt_peculiarity: return "peculiarity";
962                 case tt_pin_state:   return "pin state";
963                 case tt_tpo:         return "type";
964                 case tt_type_state:  return "type state";
965                 case tt_variability: return "variability";
966                 case tt_visibility:  return "visibility";
967                 case tt_volatility:  return "volatility";
968                 default: return "<UNKNOWN>";
969         }
970 }
971
972 /**
973  * Read and decode an enum constant.
974  */
975 static unsigned read_enum(io_env_t *env, typetag_t typetag)
976 {
977         static char buf[128];
978         unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
979         if (code != SYMERROR)
980                 return code;
981
982         printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
983         return 0;
984 }
985
986 #define read_align(env)              ((ir_align)              read_enum(env, tt_align))
987 #define read_allocation(env)         ((ir_allocation)         read_enum(env, tt_allocation))
988 #define read_builtin_kind(env)       ((ir_builtin_kind)       read_enum(env, tt_builtin))
989 #define read_cond_kind(env)          ((cond_kind)             read_enum(env, tt_cond_kind))
990 #define read_cond_jmp_predicate(env) ((cond_jmp_predicate)    read_enum(env, tt_cond_jmp_predicate))
991 #define read_initializer_kind(env)   ((ir_initializer_kind_t) read_enum(env, tt_initializer))
992 #define read_mode_arithmetic(env)    ((ir_mode_arithmetic)    read_enum(env, tt_mode_arithmetic))
993 #define read_peculiarity(env)        ((ir_peculiarity)        read_enum(env, tt_peculiarity))
994 #define read_pin_state(env)          ((op_pin_state)          read_enum(env, tt_pin_state))
995 #define read_type_state(env)         ((ir_type_state)         read_enum(env, tt_type_state))
996 #define read_variability(env)        ((ir_variability)        read_enum(env, tt_variability))
997 #define read_visibility(env)         ((ir_visibility)         read_enum(env, tt_visibility))
998 #define read_volatility(env)         ((ir_volatility)         read_enum(env, tt_volatility))
999
1000 static ir_cons_flags get_cons_flags(io_env_t *env)
1001 {
1002         ir_cons_flags flags = cons_none;
1003
1004         op_pin_state pinstate = read_pin_state(env);
1005         switch (pinstate)
1006         {
1007                 case op_pin_state_floats: flags |= cons_floats; break;
1008                 case op_pin_state_pinned: break;
1009                 default:
1010                         panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
1011         }
1012
1013         if (read_volatility(env) == volatility_is_volatile)
1014                 flags |= cons_volatile;
1015         if (read_align(env) == align_non_aligned)
1016                 flags |= cons_unaligned;
1017
1018         return flags;
1019 }
1020
1021 static tarval *read_tv(io_env_t *env)
1022 {
1023         static char buf[128];
1024         ir_mode *tvmode = read_mode(env);
1025         read_str_to(env, buf, sizeof(buf));
1026         return new_tarval_from_str(buf, strlen(buf), tvmode);
1027 }
1028
1029 static ir_initializer_t *read_initializer(io_env_t *env)
1030 {
1031         ir_initializer_kind_t ini_kind = read_initializer_kind(env);
1032
1033         switch (ini_kind)
1034         {
1035                 case IR_INITIALIZER_CONST:
1036                 {
1037                         ir_node *irn = get_node_or_dummy(env, read_long(env));
1038                         return create_initializer_const(irn);
1039                 }
1040
1041                 case IR_INITIALIZER_TARVAL:
1042                         return create_initializer_tarval(read_tv(env));
1043
1044                 case IR_INITIALIZER_NULL:
1045                         return get_initializer_null();
1046
1047                 case IR_INITIALIZER_COMPOUND:
1048                 {
1049                         unsigned i, n = (unsigned) read_long(env);
1050                         ir_initializer_t *ini = create_initializer_compound(n);
1051                         for (i = 0; i < n; i++) {
1052                                 ir_initializer_t *curini = read_initializer(env);
1053                                 set_initializer_compound_value(ini, i, curini);
1054                         }
1055                         return ini;
1056                 }
1057
1058                 default:
1059                         panic("Unknown initializer kind");
1060         }
1061 }
1062
1063
1064 /** Reads a type description and remembers it by its id. */
1065 static void import_type(io_env_t *env, keyword_t kwkind)
1066 {
1067         int            i;
1068         ir_type       *type;
1069         long           typenr = read_long(env);
1070         const char    *tpop   = read_str(env);
1071         const char    *name   = read_qstr(env);
1072         unsigned       size   = (unsigned) read_long(env);
1073         unsigned       align  = (unsigned) read_long(env);
1074         ir_type_state  state  = read_type_state(env);
1075         ir_visibility  vis    = read_visibility(env);
1076
1077         ident         *id     = new_id_from_str(name);
1078
1079         const char    *kindstr;
1080
1081         if (kwkind == kw_frametype) {
1082                 if (symbol(tpop, tt_tpo) != tpo_class) {
1083                         printf("Frame type must be a class type in line %i:%i\n", env->line, env->col);
1084                         skip_to(env, '\n');
1085                         return;
1086                 }
1087
1088                 type = new_type_frame(id);
1089                 set_type_size_bytes(type, size);
1090
1091                 kindstr = "frametype";
1092         } else if (kwkind == kw_valuetype) {
1093                 if (symbol(tpop, tt_tpo) != tpo_struct) {
1094                         printf("Value type must be a struct type in line %i:%i\n", env->line, env->col);
1095                         skip_to(env, '\n');
1096                         return;
1097                 }
1098
1099                 type = new_type_value(id);
1100                 set_type_size_bytes(type, size);
1101
1102                 kindstr = "valuetype";
1103         } else {
1104                 switch (symbol(tpop, tt_tpo))
1105                 {
1106                         case tpo_array:
1107                         {
1108                                 int ndims = (int) read_long(env);
1109                                 long elemtypenr = read_long(env);
1110                                 ir_type *elemtype = get_type(env, elemtypenr);
1111
1112                                 type = new_type_array(id, ndims, elemtype);
1113                                 for (i = 0; i < ndims; i++) {
1114                                         const char *str = read_str(env);
1115                                         if (strcmp(str, "unknown") != 0) {
1116                                                 long lowerbound = strtol(str, NULL, 0);
1117                                                 set_array_lower_bound_int(type, i, lowerbound);
1118                                         }
1119                                         str = read_str(env);
1120                                         if (strcmp(str, "unknown") != 0) {
1121                                                 long upperbound = strtol(str, NULL, 0);
1122                                                 set_array_upper_bound_int(type, i, upperbound);
1123                                         }
1124                                 }
1125                                 set_type_size_bytes(type, size);
1126                                 break;
1127                         }
1128
1129                         case tpo_class:
1130                                 if(typenr == (long) IR_SEGMENT_GLOBAL)
1131                                         type = get_glob_type();
1132                                 else
1133                                         type = new_type_class(id);
1134                                 set_type_size_bytes(type, size);
1135                                 break;
1136
1137                         case tpo_method:
1138                         {
1139                                 unsigned callingconv = (unsigned) read_long(env);
1140                                 unsigned addprops    = (unsigned) read_long(env);
1141                                 int nparams          = (int)      read_long(env);
1142                                 int nresults         = (int)      read_long(env);
1143                                 int variaindex;
1144
1145                                 type = new_type_method(id, nparams, nresults);
1146
1147                                 for (i = 0; i < nparams; i++) {
1148                                         long     typenr = read_long(env);
1149                                         ir_type *paramtype = get_type(env, typenr);
1150
1151                                         set_method_param_type(type, i, paramtype);
1152                                 }
1153                                 for (i = 0; i < nresults; i++) {
1154                                         long typenr = read_long(env);
1155                                         ir_type *restype = get_type(env, typenr);
1156
1157                                         set_method_res_type(type, i, restype);
1158                                 }
1159
1160                                 variaindex = (int) read_long(env);
1161                                 if (variaindex != -1) {
1162                                         set_method_variadicity(type, variadicity_variadic);
1163                                         if (variaindex != nparams)
1164                                                 set_method_first_variadic_param_index(type, variaindex);
1165                                 }
1166
1167                                 set_method_calling_convention(type, callingconv);
1168                                 set_method_additional_properties(type, addprops);
1169                                 break;
1170                         }
1171
1172                         case tpo_pointer:
1173                         {
1174                                 ir_mode *mode = read_mode(env);
1175                                 ir_type *pointsto = get_type(env, read_long(env));
1176                                 type = new_type_pointer(id, pointsto, mode);
1177                                 break;
1178                         }
1179
1180                         case tpo_primitive:
1181                         {
1182                                 ir_mode *mode = read_mode(env);
1183                                 type = new_type_primitive(id, mode);
1184                                 break;
1185                         }
1186
1187                         case tpo_struct:
1188                                 if(typenr < (long) IR_SEGMENT_COUNT)
1189                                         type = get_segment_type((ir_segment_t) typenr);
1190                                 else
1191                                         type = new_type_struct(id);
1192                                 set_type_size_bytes(type, size);
1193                                 break;
1194
1195                         case tpo_union:
1196                                 type = new_type_union(id);
1197                                 set_type_size_bytes(type, size);
1198                                 break;
1199
1200                         case tpo_unknown:
1201                                 return;   /* ignore unknown type */
1202
1203                         default:
1204                                 if (typenr != 0)  /* ignore global type */
1205                                         printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
1206                                 skip_to(env, '\n');
1207                                 return;
1208                 }
1209                 kindstr = "type";
1210         }
1211
1212         set_type_alignment_bytes(type, align);
1213         set_type_visibility(type, vis);
1214
1215         if (state == layout_fixed)
1216                 ARR_APP1(ir_type *, env->fixedtypes, type);
1217
1218         set_id(env, typenr, type);
1219         printf("Insert %s %s %ld\n", kindstr, name, typenr);
1220 }
1221
1222 /** Reads an entity description and remembers it by its id. */
1223 static void import_entity(io_env_t *env)
1224 {
1225         char          buf[1024], buf2[1024];
1226         long          entnr       = read_long(env);
1227         const char   *name        = read_qstr_to(env, buf, sizeof(buf));
1228         const char   *ld_name     = read_qstr_to(env, buf2, sizeof(buf2));
1229         long          typenr      = read_long(env);
1230         long          ownertypenr = read_long(env);
1231
1232         ir_type   *type      = get_type(env, typenr);
1233         ir_type   *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1234         ir_entity *entity    = new_entity(ownertype, new_id_from_str(name), type);
1235
1236         if (*ld_name)
1237                 set_entity_ld_ident(entity, new_id_from_str(ld_name));
1238         set_entity_offset     (entity, (int) read_long(env));
1239         set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1240         set_entity_compiler_generated(entity, (int) read_long(env));
1241         set_entity_allocation (entity, read_allocation(env));
1242         set_entity_visibility (entity, read_visibility(env));
1243         set_entity_variability(entity, read_variability(env));
1244         set_entity_peculiarity(entity, read_peculiarity(env));
1245         set_entity_volatility (entity, read_volatility(env));
1246
1247         if (get_entity_variability(entity) != variability_uninitialized &&
1248             get_entity_visibility(entity) != visibility_external_allocated)
1249         {
1250                 if (is_compound_entity(entity)) {
1251                         if (!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer")) {
1252                                 set_entity_initializer(entity, read_initializer(env));
1253                         } else {
1254                                 int i, n = (int) read_long(env);
1255                                 for (i = 0; i < n; i++) {
1256                                         ir_entity *member = get_entity(env, read_long(env));
1257                                         ir_node   *irn    = get_node_or_dummy(env, read_long(env));
1258                                         add_compound_ent_value(entity, irn, member);
1259                                 }
1260                         }
1261                 } else {
1262                         ir_node *irn = get_node_or_dummy(env, read_long(env));
1263                         set_atomic_ent_value(entity, irn);
1264                 }
1265         }
1266
1267         set_id(env, entnr, entity);
1268         printf("Insert entity %s %ld\n", name, entnr);
1269 }
1270
1271 /** Parses the whole type graph. */
1272 static int parse_typegraph(io_env_t *env)
1273 {
1274         const char *kind;
1275         keyword_t kwkind;
1276         lex_state_t oldstate;
1277
1278         EXPECT('{');
1279
1280         save_lex_state(env, &oldstate);
1281
1282         current_ir_graph = get_const_code_irg();
1283
1284         /* parse all types first */
1285         while (1) {
1286                 kind = read_str(env);
1287                 if (kind[0] == '}' && kind[1] == '\0')
1288                         break;
1289
1290                 kwkind = (keyword_t) symbol(kind, tt_keyword);
1291                 switch (kwkind)
1292                 {
1293                         case kw_type:
1294                         case kw_frametype:
1295                         case kw_valuetype:
1296                                 import_type(env, kwkind);
1297                                 break;
1298
1299                         default:
1300                                 skip_to(env, '\n');
1301                                 break;
1302                 }
1303         }
1304
1305         /* now parse rest */
1306         restore_lex_state(env, &oldstate);
1307
1308         while (1) {
1309                 kind = read_str(env);
1310                 if (kind[0] == '}' && kind[1] == '\0')
1311                         break;
1312
1313                 switch (symbol(kind, tt_keyword))
1314                 {
1315                         case kw_type:
1316                         case kw_frametype:
1317                         case kw_valuetype:
1318                                 skip_to(env, '\n');
1319                                 break;
1320
1321                         case kw_entity:
1322                                 import_entity(env);
1323                                 break;
1324
1325                         default:
1326                                 printf("Type graph element not supported yet: \"%s\"\n", kind);
1327                                 skip_to(env, '\n');
1328                                 break;
1329                 }
1330         }
1331         return 1;
1332 }
1333
1334 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1335 {
1336         int numpreds;
1337         *nodename = read_str(env);
1338         if ((*nodename)[0] == '}' && !(*nodename)[1]) return -1;  /* end-of-graph */
1339
1340         *nodenr = read_long(env);
1341
1342         ARR_RESIZE(ir_node *, *preds, 0);
1343
1344         EXPECT('[');
1345         for (numpreds = 0; !feof(env->file); numpreds++) {
1346                 char *endptr;
1347                 ARR_APP1(long, *preds, read_long2(env, &endptr));
1348                 if (*endptr == ']') break;
1349         }
1350         return numpreds;
1351 }
1352
1353 /** Parses an IRG. */
1354 static int parse_graph(io_env_t *env, ir_graph *irg)
1355 {
1356         long       *preds = NEW_ARR_F(long, 16);
1357         ir_node   **prednodes = NEW_ARR_F(ir_node *, 16);
1358         int         i, numpreds, ret = 1;
1359         long        nodenr;
1360         const char *nodename;
1361         ir_node    *node, *newnode;
1362
1363         current_ir_graph = irg;
1364
1365         EXPECT('{');
1366
1367         while (1) {
1368                 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1369                 if (numpreds == -1) break;  /* end-of-graph */
1370                 if (!numpreds) {
1371                         printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1372                         ret = 0;
1373                         break;
1374                 }
1375
1376                 ARR_RESIZE(ir_node *, prednodes, numpreds);
1377                 for (i = 0; i < numpreds - 1; i++)
1378                         prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1379
1380                 node = get_node_or_null(env, nodenr);
1381                 newnode = NULL;
1382
1383                 EXPECT('{');
1384
1385                 switch (symbol(nodename, tt_iro))
1386                 {
1387                         case iro_End:
1388                         {
1389                                 ir_node *newendblock = get_node(env, preds[0]);
1390                                 newnode = get_irg_end(current_ir_graph);
1391                                 exchange(get_nodes_block(newnode), newendblock);
1392                                 for (i = 0; i < numpreds - 1; i++)
1393                                         add_irn_n(newnode, prednodes[i]);
1394                                 break;
1395                         }
1396
1397                         case iro_Start:
1398                         {
1399                                 ir_node *newstartblock = get_node(env, preds[0]);
1400                                 newnode = get_irg_start(current_ir_graph);
1401                                 exchange(get_nodes_block(newnode), newstartblock);
1402                                 break;
1403                         }
1404
1405                         case iro_Block:
1406                         {
1407                                 if (preds[0] != nodenr) {
1408                                         printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1409                                                 preds[0], nodenr);
1410                                         ret = 0;
1411                                         goto endloop;
1412                                 }
1413
1414                                 newnode = new_Block(numpreds - 1, prednodes);
1415                                 break;
1416                         }
1417
1418                         case iro_Anchor:
1419                                 newnode = current_ir_graph->anchor;
1420                                 for (i = 0; i < numpreds - 1; i++)
1421                                         set_irn_n(newnode, i, prednodes[i]);
1422                                 set_irn_n(newnode, -1, get_node(env, preds[0]));
1423                                 break;
1424
1425                         case iro_SymConst:
1426                         {
1427                                 long entnr = read_long(env);
1428                                 union symconst_symbol sym;
1429                                 sym.entity_p = get_entity(env, entnr);
1430                                 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1431                                 break;
1432                         }
1433
1434                         #include "gen_irio_import.inl"
1435
1436                         default:
1437                                 goto notsupported;
1438                 }
1439
1440                 EXPECT('}');
1441
1442                 if (!newnode) {
1443 notsupported:
1444                         panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1445                 }
1446
1447                 if (node)
1448                         exchange(node, newnode);
1449                 /* Always update hash entry to avoid more uses of id nodes */
1450                 set_id(env, nodenr, newnode);
1451                 /* printf("Insert %s %ld\n", nodename, nodenr); */
1452         }
1453
1454 endloop:
1455         DEL_ARR_F(preds);
1456         DEL_ARR_F(prednodes);
1457
1458         return ret;
1459 }
1460
1461 static int parse_modes(io_env_t *env)
1462 {
1463         const char *kind;
1464         keyword_t kwkind;
1465
1466         EXPECT('{');
1467
1468         while (1) {
1469                 kind = read_str(env);
1470                 if (kind[0] == '}' && kind[1] == '\0')
1471                         break;
1472
1473                 kwkind = (keyword_t) symbol(kind, tt_keyword);
1474                 switch (kwkind)
1475                 {
1476                         case kw_mode:
1477                         {
1478                                 const char *name = read_qstr(env);
1479                                 ir_mode_sort sort = (ir_mode_sort) read_long(env);
1480                                 int size = read_long(env);
1481                                 int sign = read_long(env);
1482                                 ir_mode_arithmetic arith = read_mode_arithmetic(env);
1483                                 unsigned modulo_shift = read_long(env);
1484                                 int vector_elems = read_long(env);
1485                                 ir_mode *mode;
1486
1487                                 if (vector_elems != 1) {
1488                                         panic("no support for import of vector modes yes");
1489                                 }
1490
1491                                 mode = new_ir_mode(name, sort, size, sign, arith, modulo_shift);
1492                                 if (mode_is_reference(mode)) {
1493                                         set_reference_mode_signed_eq(mode, read_mode(env));
1494                                         set_reference_mode_unsigned_eq(mode, read_mode(env));
1495                                 }
1496                                 break;
1497                         }
1498
1499                         default:
1500                                 skip_to(env, '\n');
1501                                 break;
1502                 }
1503         }
1504         return 1;
1505 }
1506
1507 /** Imports an previously exported textual representation of an (maybe partial) irp */
1508 void ir_import(const char *filename)
1509 {
1510         int oldoptimize = get_optimize();
1511         firm_verification_t oldver = get_node_verification_mode();
1512         io_env_t ioenv;
1513         io_env_t *env = &ioenv;
1514         int i, n;
1515
1516         symtbl_init();
1517
1518         memset(env, 0, sizeof(*env));
1519         env->idset = new_set(id_cmp, 128);
1520         env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1521
1522         env->file = fopen(filename, "rt");
1523         if (!env->file) {
1524                 perror(filename);
1525                 exit(1);
1526         }
1527
1528         set_optimize(0);
1529         do_node_verification(FIRM_VERIFICATION_OFF);
1530
1531         while (1) {
1532                 const char *str = read_str(env);
1533                 if (!*str) break;
1534                 switch (symbol(str, tt_keyword))
1535                 {
1536                         case kw_modes:
1537                                 if (!parse_modes(env)) goto end;
1538                                 break;
1539
1540                         case kw_typegraph:
1541                                 if (!parse_typegraph(env)) goto end;
1542                                 break;
1543
1544                         case kw_irg:
1545                         {
1546                                 ir_entity *irgent = get_entity(env, read_long(env));
1547                                 long valuetypeid;
1548                                 ir_graph *irg = new_ir_graph(irgent, 0);
1549                                 set_irg_frame_type(irg, get_type(env, read_long(env)));
1550                                 valuetypeid = read_long(env);
1551                                 if (valuetypeid != -1)
1552                                         set_method_value_param_type(get_entity_type(irgent),
1553                                                         get_type(env, valuetypeid));
1554
1555                                 if (!parse_graph(env, irg)) goto end;
1556                                 break;
1557                         }
1558
1559                         case kw_constirg:
1560                         {
1561                                 ir_graph *constirg = get_const_code_irg();
1562                                 long bodyblockid = read_long(env);
1563                                 set_id(env, bodyblockid, constirg->current_block);
1564                                 if (!parse_graph(env, constirg)) goto end;
1565                                 break;
1566                         }
1567                 }
1568         }
1569
1570 end:
1571         n = ARR_LEN(env->fixedtypes);
1572         for (i = 0; i < n; i++)
1573                 set_type_state(env->fixedtypes[i], layout_fixed);
1574
1575         DEL_ARR_F(env->fixedtypes);
1576
1577         del_set(env->idset);
1578
1579         irp_finalize_cons();
1580
1581         do_node_verification(oldver);
1582         set_optimize(oldoptimize);
1583
1584         fclose(env->file);
1585 }