2 * Copyright (C) 1995-2009 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Write textual representation of firm to file.
23 * @author Moritz Kroll
34 #include "irgraph_t.h"
44 #define SYMERROR ((unsigned) ~0)
49 set *idset; /**< id_entry set, which maps from file ids to new Firm elements */
55 typedef enum typetag_t
70 typedef struct symbol_t
72 const char *str; /**< The name of this symbol. */
73 typetag_t typetag; /**< The type tag of this symbol. */
74 unsigned code; /**< The value of this symbol. */
77 typedef struct id_entry
83 /** The symbol table, a set of symbol_t elements. */
88 * Calculate a hash value for a string.
90 static unsigned string_hash(const char *str, int len)
92 return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
96 * Compare two symbol table entries.
98 static int symbol_cmp(const void *elt, const void *key, size_t size)
100 const symbol_t *entry = (const symbol_t *) elt;
101 const symbol_t *keyentry = (const symbol_t *) key;
103 return strcmp(entry->str, keyentry->str);
106 static int id_cmp(const void *elt, const void *key, size_t size)
108 const id_entry *entry = (const id_entry *) elt;
109 const id_entry *keyentry = (const id_entry *) key;
111 return entry->id - keyentry->id;
114 /** Initializes the symbol table. May be called more than once without problems. */
115 static void symtbl_init(void)
119 /* Only initialize once */
123 symtbl = new_set(symbol_cmp, 32);
125 #define INSERT(s, tt, cod) \
127 key.typetag = (tt); \
129 set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1))
131 #define INSERTENUM(tt, e) INSERT(#e, tt, e)
133 INSERT("array", tt_tpo, tpo_array);
134 INSERT("class", tt_tpo, tpo_class);
135 INSERT("method", tt_tpo, tpo_method);
136 INSERT("pointer", tt_tpo, tpo_pointer);
137 INSERT("primitive", tt_tpo, tpo_primitive);
138 INSERT("struct", tt_tpo, tpo_struct);
139 INSERT("Unknown", tt_tpo, tpo_unknown);
141 #include "gen_irio_lex.inl"
143 INSERTENUM(tt_align, align_non_aligned);
144 INSERTENUM(tt_align, align_is_aligned);
146 INSERTENUM(tt_allocation, allocation_automatic);
147 INSERTENUM(tt_allocation, allocation_parameter);
148 INSERTENUM(tt_allocation, allocation_dynamic);
149 INSERTENUM(tt_allocation, allocation_static);
151 INSERTENUM(tt_pin_state, op_pin_state_floats);
152 INSERTENUM(tt_pin_state, op_pin_state_pinned);
153 INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
154 INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
156 INSERTENUM(tt_type_state, layout_undefined);
157 INSERTENUM(tt_type_state, layout_fixed);
159 INSERTENUM(tt_variability, variability_uninitialized);
160 INSERTENUM(tt_variability, variability_initialized);
161 INSERTENUM(tt_variability, variability_part_constant);
162 INSERTENUM(tt_variability, variability_constant);
164 INSERTENUM(tt_visibility, visibility_local);
165 INSERTENUM(tt_visibility, visibility_external_visible);
166 INSERTENUM(tt_visibility, visibility_external_allocated);
168 INSERTENUM(tt_volatility, volatility_non_volatile);
169 INSERTENUM(tt_volatility, volatility_is_volatile);
171 INSERTENUM(tt_peculiarity, peculiarity_description);
172 INSERTENUM(tt_peculiarity, peculiarity_inherited);
173 INSERTENUM(tt_peculiarity, peculiarity_existent);
175 INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
176 INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
177 INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
178 INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
184 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
185 static unsigned symbol(const char *str, typetag_t typetag)
187 symbol_t key, *entry;
191 entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)));
192 if (entry && entry->typetag == typetag) {
198 static void *get_id(io_env_t *env, long id)
200 id_entry key, *entry;
203 entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
204 return entry ? entry->elem : NULL;
207 static void set_id(io_env_t *env, long id, void *elem)
212 set_insert(env->idset, &key, sizeof(key), (unsigned) id);
215 static void write_mode(io_env_t *env, ir_mode *mode)
217 fputs(get_mode_name(mode), env->file);
218 fputc(' ', env->file);
221 static void write_tarval(io_env_t *env, tarval *tv)
224 write_mode(env, get_tarval_mode(tv));
225 tarval_snprintf(buf, sizeof(buf), tv);
226 fputs(buf, env->file);
227 fputc(' ', env->file);
230 static void write_pin_state(io_env_t *env, ir_node *irn)
232 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
233 fputc(' ', env->file);
236 static void write_volatility(io_env_t *env, ir_node *irn)
240 if(is_Load(irn)) vol = get_Load_volatility(irn);
241 else if(is_Store(irn)) vol = get_Store_volatility(irn);
242 else assert(0 && "Invalid optype for write_volatility");
244 fputs(get_volatility_name(vol), env->file);
245 fputc(' ', env->file);
248 static void write_align(io_env_t *env, ir_node *irn)
252 if(is_Load(irn)) align = get_Load_align(irn);
253 else if(is_Store(irn)) align = get_Store_align(irn);
254 else assert(0 && "Invalid optype for write_align");
256 fputs(get_align_name(align), env->file);
257 fputc(' ', env->file);
260 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
263 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
264 fputs(get_initializer_kind_name(ini_kind), f);
269 case IR_INITIALIZER_CONST:
270 fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
273 case IR_INITIALIZER_TARVAL:
274 write_tarval(env, get_initializer_tarval_value(ini));
277 case IR_INITIALIZER_NULL:
280 case IR_INITIALIZER_COMPOUND:
282 unsigned i, n = get_initializer_compound_n_entries(ini);
283 fprintf(f, "%d ", n);
284 for(i = 0; i < n; i++)
285 write_initializer(env, get_initializer_compound_value(ini, i));
290 panic("Unknown initializer kind");
294 static void export_type(io_env_t *env, ir_type *tp)
298 fprintf(f, "\ttype %ld %s \"%s\" %u %u %s %s ",
300 get_type_tpop_name(tp),
302 get_type_size_bytes(tp),
303 get_type_alignment_bytes(tp),
304 get_type_state_name(get_type_state(tp)),
305 get_visibility_name(get_type_visibility(tp)));
307 switch(get_type_tpop_code(tp))
311 int n = get_array_n_dimensions(tp);
312 fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
313 for(i = 0; i < n; i++)
315 ir_node *lower = get_array_lower_bound(tp, i);
316 ir_node *upper = get_array_upper_bound(tp, i);
318 if(is_Const(lower)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
319 else panic("Lower array bound is not constant");
321 if(is_Const(upper)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
322 else if(is_Unknown(upper)) fputs("unknown ", f);
323 else panic("Upper array bound is not constant");
329 /* TODO: inheritance stuff not supported yet */
330 printf("Inheritance of classes not supported yet!\n");
335 int nparams = get_method_n_params(tp);
336 int nresults = get_method_n_ress(tp);
337 fprintf(f, "%i %i ", nparams, nresults);
338 for(i = 0; i < nparams; i++)
339 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
340 for(i = 0; i < nresults; i++)
341 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
346 write_mode(env, get_type_mode(tp));
347 fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
351 write_mode(env, get_type_mode(tp));
361 printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
367 static void export_entity(io_env_t *env, ir_entity *ent)
369 ir_type *owner = get_entity_owner(ent);
370 fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %s %s %s %s %s ",
372 get_entity_name(ent),
373 ent->ld_name ? get_id_str(ent->ld_name) : "",
374 get_type_nr(get_entity_type(ent)),
376 get_entity_offset(ent),
377 (unsigned) get_entity_offset_bits_remainder(ent),
378 get_allocation_name(get_entity_allocation(ent)),
379 get_visibility_name(get_entity_visibility(ent)),
380 get_variability_name(get_entity_variability(ent)),
381 get_peculiarity_name(get_entity_peculiarity(ent)),
382 get_volatility_name(get_entity_volatility(ent)));
384 // TODO: inheritance stuff for class entities not supported yet
385 if(is_Class_type(owner) && owner != get_glob_type())
386 printf("Inheritance of class entities not supported yet!\n");
388 if(get_entity_variability(ent) != variability_uninitialized
389 && get_entity_visibility(ent) != visibility_external_allocated)
391 if(is_compound_entity(ent))
393 if(has_entity_initializer(ent))
395 fputs("initializer ", env->file);
396 write_initializer(env, get_entity_initializer(ent));
400 int i, n = get_compound_ent_n_values(ent);
401 fputs("noninitializer ", env->file);
402 fprintf(env->file, "%d ", n);
403 for(i = 0; i < n; i++)
405 ir_entity *member = get_compound_ent_value_member(ent, i);
406 ir_node *irn = get_compound_ent_value(ent, i);
407 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
413 ir_node *irn = get_atomic_ent_value(ent);
414 fprintf(env->file, "%ld ", get_irn_node_nr(irn));
418 fputc('\n', env->file);
421 static void export_type_or_ent(type_or_ent tore, void *ctx)
423 io_env_t *env = (io_env_t *) ctx;
425 switch(get_kind(tore.ent))
428 export_entity(env, tore.ent);
432 export_type(env, tore.typ);
436 printf("export_type_or_ent: Unknown type or entity.\n");
441 static void export_node(ir_node *irn, void *ctx)
443 io_env_t *env = (io_env_t *) ctx;
445 unsigned opcode = get_irn_opcode(irn);
447 if(env->ignoreblocks && opcode == iro_Block)
450 n = get_irn_arity(irn);
452 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
454 for(i = -1; i < n; i++)
456 ir_node *pred = get_irn_n(irn, i);
458 /* Anchor node may have NULL predecessors */
459 assert(is_Anchor(irn));
460 fputs("-1 ", env->file);
462 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
466 fprintf(env->file, "] { ");
470 #include "gen_irio_export.inl"
472 fputs("}\n", env->file);
475 /** Exports the whole irp to the given file in a textual form. */
476 void ir_export(const char *filename)
479 int i, n_irgs = get_irp_n_irgs(), n_pseudoirgs = get_irp_n_pseudo_irgs();
481 env.file = fopen(filename, "wt");
488 fputs("typegraph {\n", env.file);
490 type_walk(NULL, export_type_or_ent, &env);
491 /* TODO: Visit frame types and "types for value params"? */
493 for(i = 0; i < n_irgs; i++)
495 ir_graph *irg = get_irp_irg(i);
497 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
499 env.ignoreblocks = 0;
500 irg_block_walk_graph(irg, NULL, export_node, &env);
502 env.ignoreblocks = 1;
503 irg_walk_anchors(irg, NULL, export_node, &env);
506 fputs("}\n\nconstirg {\n", env.file);
507 walk_const_code(NULL, export_node, &env);
508 fputs("}\n", env.file);
513 /** Exports the given irg to the given file. */
514 void ir_export_irg(ir_graph *irg, const char *filename)
518 env.file = fopen(filename, "wt");
525 fputs("typegraph {\n", env.file);
527 type_walk_irg(irg, NULL, export_type_or_ent, &env);
529 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
531 env.ignoreblocks = 0;
532 irg_block_walk_graph(irg, NULL, export_node, &env);
534 env.ignoreblocks = 1;
535 irg_walk_anchors(irg, NULL, export_node, &env);
537 /* TODO: Only output needed constants */
538 fputs("}\n\nconstirg {\n", env.file);
539 walk_const_code(NULL, export_node, &env);
540 fputs("}\n", env.file);
545 static int read_c(io_env_t *env)
547 int ch = fgetc(env->file);
566 /** Returns the first non-whitespace character or EOF. **/
567 static int skip_ws(io_env_t *env)
571 int ch = read_c(env);
586 static void skip_to(io_env_t *env, char to_ch)
593 while(ch != to_ch && ch != EOF);
596 static int expect_char(io_env_t *env, char ch)
598 int curch = skip_ws(env);
601 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
607 #define EXPECT(c) if(expect_char(env, (c))) {} else return 0
608 #define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)
610 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
613 for(i = 0; i < bufsize - 1; i++)
615 int ch = read_c(env);
625 i--; // skip whitespace
638 static const char *read_str(io_env_t *env)
640 static char buf[1024];
641 return read_str_to(env, buf, sizeof(buf));
644 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
647 EXPECT_OR_EXIT('\"');
648 for(i = 0; i < bufsize - 1; i++)
650 int ch = read_c(env);
653 printf("Unexpected end of quoted string!\n");
656 if(ch == '\"') break;
662 printf("Quoted string too long!\n");
669 static long read_long2(io_env_t *env, char **endptr)
671 static char buf[1024];
672 return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
675 static long read_long(io_env_t *env)
677 return read_long2(env, NULL);
680 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
682 ir_node *node = (ir_node *) get_id(env, nodenr);
683 if(node && node->kind != k_ir_node)
685 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
690 static ir_node *get_node(io_env_t *env, long nodenr)
692 ir_node *node = get_node_or_null(env, nodenr);
694 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
699 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
701 ir_node *node = get_node_or_null(env, nodenr);
704 node = new_Dummy(mode_X);
705 set_id(env, nodenr, node);
710 static ir_type *get_type(io_env_t *env, long typenr)
712 ir_type *type = (ir_type *) get_id(env, typenr);
715 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
717 else if(type->kind != k_type)
719 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
724 static ir_type *read_type(io_env_t *env)
726 return get_type(env, read_long(env));
729 static ir_entity *get_entity(io_env_t *env, long entnr)
731 ir_entity *entity = (ir_entity *) get_id(env, entnr);
734 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
737 else if(entity->kind != k_entity)
739 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
744 static ir_entity *read_entity(io_env_t *env)
746 return get_entity(env, read_long(env));
749 static ir_mode *read_mode(io_env_t *env)
751 static char buf[128];
754 read_str_to(env, buf, sizeof(buf));
756 n = get_irp_n_modes();
757 for(i = 0; i < n; i++)
759 ir_mode *mode = get_irp_mode(i);
760 if(!strcmp(buf, get_mode_name(mode)))
764 printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
768 static const char *get_typetag_name(typetag_t typetag)
772 case tt_iro: return "opcode";
773 case tt_tpo: return "type";
774 case tt_align: return "align";
775 case tt_allocation: return "allocation";
776 case tt_peculiarity: return "peculiarity";
777 case tt_pin_state: return "pin state";
778 case tt_type_state: return "type state";
779 case tt_variability: return "variability";
780 case tt_visibility: return "visibility";
781 case tt_volatility: return "volatility";
782 default: return "<UNKNOWN>";
787 * Read and decode an enum constant.
789 static unsigned read_enum(io_env_t *env, typetag_t typetag)
791 static char buf[128];
792 unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
796 printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
800 #define read_align(env) ((ir_align) read_enum(env, tt_align))
801 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
802 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
803 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
804 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
805 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
806 #define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
807 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
808 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
810 static ir_cons_flags get_cons_flags(io_env_t *env)
812 ir_cons_flags flags = cons_none;
814 op_pin_state pinstate = read_pin_state(env);
817 case op_pin_state_floats: flags |= cons_floats; break;
818 case op_pin_state_pinned: break;
820 panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
823 if(read_volatility(env) == volatility_is_volatile) flags |= cons_volatile;
824 if(read_align(env) == align_non_aligned) flags |= cons_unaligned;
829 static tarval *read_tv(io_env_t *env)
831 static char buf[128];
832 ir_mode *tvmode = read_mode(env);
833 read_str_to(env, buf, sizeof(buf));
834 return new_tarval_from_str(buf, strlen(buf), tvmode);
837 static ir_initializer_t *read_initializer(io_env_t *env)
840 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
844 case IR_INITIALIZER_CONST:
846 ir_node *irn = get_node_or_dummy(env, read_long(env));
847 return create_initializer_const(irn);
850 case IR_INITIALIZER_TARVAL:
851 return create_initializer_tarval(read_tv(env));
853 case IR_INITIALIZER_NULL:
854 return get_initializer_null();
856 case IR_INITIALIZER_COMPOUND:
858 unsigned i, n = (unsigned) read_long(env);
859 ir_initializer_t *ini = create_initializer_compound(n);
860 for(i = 0; i < n; i++)
862 ir_initializer_t *curini = read_initializer(env);
863 set_initializer_compound_value(ini, i, curini);
869 panic("Unknown initializer kind");
874 /** Reads a type description and remembers it by its id. */
875 static void import_type(io_env_t *env)
880 long typenr = read_long(env);
881 const char *tpop = read_str(env);
882 const char *name = read_qstr_to(env, buf, sizeof(buf));
883 unsigned size = (unsigned) read_long(env);
884 unsigned align = (unsigned) read_long(env);
885 ir_type_state state = read_type_state(env);
886 ir_visibility vis = read_visibility(env);
888 ident *id = new_id_from_str(name);
890 switch(symbol(tpop, tt_tpo))
894 int ndims = (int) read_long(env);
895 long elemtypenr = read_long(env);
896 ir_type *elemtype = get_type(env, elemtypenr);
898 type = new_type_array(id, ndims, elemtype);
899 for(i = 0; i < ndims; i++)
901 const char *str = read_str(env);
902 if(strcmp(str, "unknown"))
904 long lowerbound = strtol(str, NULL, 0);
905 set_array_lower_bound_int(type, i, lowerbound);
908 if(strcmp(str, "unknown"))
910 long upperbound = strtol(str, NULL, 0);
911 set_array_upper_bound_int(type, i, upperbound);
914 set_type_size_bytes(type, size);
919 type = new_type_class(id);
920 set_type_size_bytes(type, size);
925 int nparams = (int) read_long(env);
926 int nresults = (int) read_long(env);
928 type = new_type_method(id, nparams, nresults);
930 for(i = 0; i < nparams; i++)
932 long typenr = read_long(env);
933 ir_type *paramtype = get_type(env, typenr);
935 set_method_param_type(type, i, paramtype);
937 for(i = 0; i < nresults; i++)
939 long typenr = read_long(env);
940 ir_type *restype = get_type(env, typenr);
942 set_method_res_type(type, i, restype);
949 ir_mode *mode = read_mode(env);
950 ir_type *pointsto = get_type(env, read_long(env));
951 type = new_type_pointer(id, pointsto, mode);
957 ir_mode *mode = read_mode(env);
958 type = new_type_primitive(id, mode);
963 type = new_type_struct(id);
964 set_type_size_bytes(type, size);
968 type = new_type_union(id);
969 set_type_size_bytes(type, size);
973 return; // ignore unknown type
976 if(typenr != 0) // ignore global type
977 printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
982 set_type_alignment_bytes(type, align);
983 set_type_visibility(type, vis);
985 if(state == layout_fixed)
986 ARR_APP1(ir_type *, env->fixedtypes, type);
988 set_id(env, typenr, type);
989 printf("Insert type %s %ld\n", name, typenr);
992 /** Reads an entity description and remembers it by its id. */
993 static void import_entity(io_env_t *env)
995 char buf[1024], buf2[1024];
996 long entnr = read_long(env);
997 const char *name = read_qstr_to(env, buf, sizeof(buf));
998 const char *ld_name = read_qstr_to(env, buf2, sizeof(buf2));
999 long typenr = read_long(env);
1000 long ownertypenr = read_long(env);
1002 ir_type *type = get_type(env, typenr);
1003 ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1004 ir_entity *entity = new_entity(ownertype, new_id_from_str(name), type);
1006 if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
1007 set_entity_offset (entity, (int) read_long(env));
1008 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1009 set_entity_allocation (entity, read_allocation(env));
1010 set_entity_visibility (entity, read_visibility(env));
1011 set_entity_variability(entity, read_variability(env));
1012 set_entity_peculiarity(entity, read_peculiarity(env));
1013 set_entity_volatility (entity, read_volatility(env));
1015 if(get_entity_variability(entity) != variability_uninitialized
1016 && get_entity_visibility(entity) != visibility_external_allocated)
1018 if(is_compound_entity(entity))
1020 if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
1022 set_entity_initializer(entity, read_initializer(env));
1026 int i, n = (int) read_long(env);
1027 for(i = 0; i < n; i++)
1029 ir_entity *member = get_entity(env, read_long(env));
1030 ir_node *irn = get_node_or_dummy(env, read_long(env));
1031 add_compound_ent_value(entity, irn, member);
1037 ir_node *irn = get_node_or_dummy(env, read_long(env));
1038 set_atomic_ent_value(entity, irn);
1042 set_id(env, entnr, entity);
1043 printf("Insert entity %s %ld\n", name, entnr);
1046 /** Parses the whole type graph. */
1047 static int parse_typegraph(io_env_t *env)
1054 curfpos = ftell(env->file);
1056 current_ir_graph = get_const_code_irg();
1058 // parse all types first
1061 kind = read_str(env);
1062 if(kind[0] == '}' && !kind[1]) break;
1064 if(!strcmp(kind, "type"))
1071 fseek(env->file, curfpos, SEEK_SET);
1074 kind = read_str(env);
1075 if(kind[0] == '}' && !kind[1]) break;
1077 if(!strcmp(kind, "type"))
1079 else if(!strcmp(kind, "entity"))
1083 printf("Type graph element not supported yet: \"%s\"\n", kind);
1090 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1093 *nodename = read_str(env);
1094 if((*nodename)[0] == '}' && !(*nodename)[1]) return -1; // end-of-graph
1096 *nodenr = read_long(env);
1098 ARR_RESIZE(ir_node *, *preds, 0);
1101 for(numpreds = 0; !feof(env->file); numpreds++)
1104 ARR_APP1(long, *preds, read_long2(env, &endptr));
1105 if(*endptr == ']') break;
1110 /** Parses an IRG. */
1111 static int parse_graph(io_env_t *env, ir_graph *irg)
1113 long *preds = NEW_ARR_F(long, 16);
1114 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1115 int i, numpreds, ret = 1;
1117 const char *nodename;
1118 ir_node *node, *newnode;
1120 current_ir_graph = irg;
1126 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1127 if(numpreds == -1) break; // end-of-graph
1130 printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1135 ARR_RESIZE(ir_node *, prednodes, numpreds);
1136 for(i = 0; i < numpreds - 1; i++)
1137 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1139 node = get_node_or_null(env, nodenr);
1144 switch(symbol(nodename, tt_iro))
1148 ir_node *newendblock = get_node(env, preds[0]);
1149 newnode = get_irg_end(current_ir_graph);
1150 exchange(get_nodes_block(newnode), newendblock);
1151 for(i = 0; i < numpreds - 1; i++)
1152 add_irn_n(newnode, prednodes[i]);
1158 ir_node *newstartblock = get_node(env, preds[0]);
1159 newnode = get_irg_start(current_ir_graph);
1160 exchange(get_nodes_block(newnode), newstartblock);
1166 if(preds[0] != nodenr)
1168 printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1174 newnode = new_Block(numpreds - 1, prednodes);
1179 newnode = current_ir_graph->anchor;
1180 for(i = 0; i < numpreds - 1; i++)
1181 set_irn_n(newnode, i, prednodes[i]);
1182 set_irn_n(newnode, -1, get_node(env, preds[0]));
1187 long entnr = read_long(env);
1188 union symconst_symbol sym;
1189 sym.entity_p = get_entity(env, entnr);
1190 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1194 #include "gen_irio_import.inl"
1205 panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1209 exchange(node, newnode);
1210 /* Always update hash entry to avoid more uses of id nodes */
1211 set_id(env, nodenr, newnode);
1212 //printf("Insert %s %ld\n", nodename, nodenr);
1217 DEL_ARR_F(prednodes);
1222 /** Imports an previously exported textual representation of an (maybe partial) irp */
1223 void ir_import(const char *filename)
1225 int oldoptimize = get_optimize();
1226 firm_verification_t oldver = get_node_verification_mode();
1228 io_env_t *env = &ioenv;
1233 memset(env, 0, sizeof(*env));
1234 env->idset = new_set(id_cmp, 128);
1235 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1237 env->file = fopen(filename, "rt");
1245 do_node_verification(FIRM_VERIFICATION_OFF);
1249 const char *str = read_str(env);
1251 if(!strcmp(str, "typegraph"))
1253 if(!parse_typegraph(env)) break;
1255 else if(!strcmp(str, "irg"))
1257 ir_graph *irg = new_ir_graph(get_entity(env, read_long(env)), 0);
1258 if(!parse_graph(env, irg)) break;
1260 else if(!strcmp(str, "constirg"))
1262 if(!parse_graph(env, get_const_code_irg())) break;
1266 n = ARR_LEN(env->fixedtypes);
1267 for(i = 0; i < n; i++)
1268 set_type_state(env->fixedtypes[i], layout_fixed);
1270 DEL_ARR_F(env->fixedtypes);
1272 del_set(env->idset);
1274 irp_finalize_cons();
1276 do_node_verification(oldver);
1277 set_optimize(oldoptimize);