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 fputs("noninitializer ", env->file);
401 int i, n = get_compound_ent_n_values(ent);
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);
448 if(env->ignoreblocks && opcode == iro_Block)
451 n = get_irn_arity(irn);
453 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
455 for(i = -1; i < n; i++)
457 ir_node *pred = get_irn_n(irn, i);
459 /* Anchor node may have NULL predecessors */
460 assert(is_Anchor(irn));
461 fputs("-1 ", env->file);
463 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
467 fprintf(env->file, "] { ");
471 #include "gen_irio_export.inl"
473 fputs("}\n", env->file);
476 /** Exports the whole irp to the given file in a textual form. */
477 void ir_export(const char *filename)
480 int i, n_irgs = get_irp_n_irgs(), n_pseudoirgs = get_irp_n_pseudo_irgs();
482 env.file = fopen(filename, "wt");
489 fputs("typegraph {\n", env.file);
491 type_walk(NULL, export_type_or_ent, &env);
492 /* TODO: Visit frame types and "types for value params"? */
494 for(i = 0; i < n_irgs; i++)
496 ir_graph *irg = get_irp_irg(i);
498 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
500 env.ignoreblocks = 0;
501 irg_block_walk_graph(irg, NULL, export_node, &env);
503 env.ignoreblocks = 1;
504 irg_walk_anchors(irg, NULL, export_node, &env);
507 fputs("}\n\nconstirg {\n", env.file);
508 walk_const_code(NULL, export_node, &env);
509 fputs("}\n", env.file);
514 /** Exports the given irg to the given file. */
515 void ir_export_irg(ir_graph *irg, const char *filename)
519 env.file = fopen(filename, "wt");
526 fputs("typegraph {\n", env.file);
528 type_walk_irg(irg, NULL, export_type_or_ent, &env);
530 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
532 env.ignoreblocks = 0;
533 irg_block_walk_graph(irg, NULL, export_node, &env);
535 env.ignoreblocks = 1;
536 irg_walk_anchors(irg, NULL, export_node, &env);
538 /* TODO: Only output needed constants */
539 fputs("}\n\nconstirg {\n", env.file);
540 walk_const_code(NULL, export_node, &env);
541 fputs("}\n", env.file);
546 static int read_c(io_env_t *env)
548 int ch = fgetc(env->file);
567 /** Returns the first non-whitespace character or EOF. **/
568 static int skip_ws(io_env_t *env)
572 int ch = read_c(env);
587 static void skip_to(io_env_t *env, char to_ch)
594 while(ch != to_ch && ch != EOF);
597 static int expect_char(io_env_t *env, char ch)
599 int curch = skip_ws(env);
602 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
608 #define EXPECT(c) if(expect_char(env, (c))) {} else return 0
609 #define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)
611 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
614 for(i = 0; i < bufsize - 1; i++)
616 int ch = read_c(env);
626 i--; // skip whitespace
639 static const char *read_str(io_env_t *env)
641 static char buf[1024];
642 return read_str_to(env, buf, sizeof(buf));
645 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
648 EXPECT_OR_EXIT('\"');
649 for(i = 0; i < bufsize - 1; i++)
651 int ch = read_c(env);
654 printf("Unexpected end of quoted string!\n");
657 if(ch == '\"') break;
663 printf("Quoted string too long!\n");
670 static long read_long2(io_env_t *env, char **endptr)
672 static char buf[1024];
673 return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
676 static long read_long(io_env_t *env)
678 return read_long2(env, NULL);
681 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
683 ir_node *node = (ir_node *) get_id(env, nodenr);
684 if(node && node->kind != k_ir_node)
686 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
691 static ir_node *get_node(io_env_t *env, long nodenr)
693 ir_node *node = get_node_or_null(env, nodenr);
695 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
700 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
702 ir_node *node = get_node_or_null(env, nodenr);
705 node = new_Dummy(mode_X);
706 set_id(env, nodenr, node);
711 static ir_type *get_type(io_env_t *env, long typenr)
713 ir_type *type = (ir_type *) get_id(env, typenr);
716 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
718 else if(type->kind != k_type)
720 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
725 static ir_type *read_type(io_env_t *env)
727 return get_type(env, read_long(env));
730 static ir_entity *get_entity(io_env_t *env, long entnr)
732 ir_entity *entity = (ir_entity *) get_id(env, entnr);
735 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
738 else if(entity->kind != k_entity)
740 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
745 static ir_entity *read_entity(io_env_t *env)
747 return get_entity(env, read_long(env));
750 static ir_mode *read_mode(io_env_t *env)
752 static char buf[128];
755 read_str_to(env, buf, sizeof(buf));
757 n = get_irp_n_modes();
758 for(i = 0; i < n; i++)
760 ir_mode *mode = get_irp_mode(i);
761 if(!strcmp(buf, get_mode_name(mode)))
765 printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
769 static const char *get_typetag_name(typetag_t typetag)
773 case tt_iro: return "opcode";
774 case tt_tpo: return "type";
775 case tt_align: return "align";
776 case tt_allocation: return "allocation";
777 case tt_peculiarity: return "peculiarity";
778 case tt_pin_state: return "pin state";
779 case tt_type_state: return "type state";
780 case tt_variability: return "variability";
781 case tt_visibility: return "visibility";
782 case tt_volatility: return "volatility";
783 default: return "<UNKNOWN>";
788 * Read and decode an enum constant.
790 static unsigned read_enum(io_env_t *env, typetag_t typetag)
792 static char buf[128];
793 unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
797 printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
801 #define read_align(env) ((ir_align) read_enum(env, tt_align))
802 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
803 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
804 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
805 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
806 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
807 #define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
808 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
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 /** Reads a type description and remembers it by its id. */
838 static void import_type(io_env_t *env)
843 long typenr = read_long(env);
844 const char *tpop = read_str(env);
845 const char *name = read_qstr_to(env, buf, sizeof(buf));
846 unsigned size = (unsigned) read_long(env);
847 unsigned align = (unsigned) read_long(env);
848 ir_type_state state = read_type_state(env);
849 ir_visibility vis = read_visibility(env);
851 ident *id = new_id_from_str(name);
853 switch(symbol(tpop, tt_tpo))
857 int ndims = (int) read_long(env);
858 long elemtypenr = read_long(env);
859 ir_type *elemtype = get_type(env, elemtypenr);
861 type = new_type_array(id, ndims, elemtype);
862 for(i = 0; i < ndims; i++)
864 const char *str = read_str(env);
865 if(strcmp(str, "unknown"))
867 long lowerbound = strtol(str, NULL, 0);
868 set_array_lower_bound_int(type, i, lowerbound);
871 if(strcmp(str, "unknown"))
873 long upperbound = strtol(str, NULL, 0);
874 set_array_upper_bound_int(type, i, upperbound);
877 set_type_size_bytes(type, size);
882 type = new_type_class(id);
883 set_type_size_bytes(type, size);
888 int nparams = (int) read_long(env);
889 int nresults = (int) read_long(env);
891 type = new_type_method(id, nparams, nresults);
893 for(i = 0; i < nparams; i++)
895 long typenr = read_long(env);
896 ir_type *paramtype = get_type(env, typenr);
898 set_method_param_type(type, i, paramtype);
900 for(i = 0; i < nresults; i++)
902 long typenr = read_long(env);
903 ir_type *restype = get_type(env, typenr);
905 set_method_res_type(type, i, restype);
912 ir_mode *mode = read_mode(env);
913 ir_type *pointsto = get_type(env, read_long(env));
914 type = new_type_pointer(id, pointsto, mode);
920 ir_mode *mode = read_mode(env);
921 type = new_type_primitive(id, mode);
926 type = new_type_struct(id);
927 set_type_size_bytes(type, size);
931 type = new_type_union(id);
932 set_type_size_bytes(type, size);
936 return; // ignore unknown type
939 if(typenr != 0) // ignore global type
940 printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
945 set_type_alignment_bytes(type, align);
946 set_type_visibility(type, vis);
948 if(state == layout_fixed)
949 ARR_APP1(ir_type *, env->fixedtypes, type);
951 set_id(env, typenr, type);
952 printf("Insert type %s %ld\n", name, typenr);
955 /** Reads an entity description and remembers it by its id. */
956 static void import_entity(io_env_t *env)
958 char buf[1024], buf2[1024];
959 long entnr = read_long(env);
960 const char *name = read_qstr_to(env, buf, sizeof(buf));
961 const char *ld_name = read_qstr_to(env, buf2, sizeof(buf2));
962 long typenr = read_long(env);
963 long ownertypenr = read_long(env);
965 ir_type *type = get_type(env, typenr);
966 ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
967 ir_entity *entity = new_entity(ownertype, new_id_from_str(name), type);
969 if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
970 set_entity_offset (entity, (int) read_long(env));
971 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
972 set_entity_allocation (entity, read_allocation(env));
973 set_entity_visibility (entity, read_visibility(env));
974 set_entity_variability(entity, read_variability(env));
975 set_entity_peculiarity(entity, read_peculiarity(env));
976 set_entity_volatility (entity, read_volatility(env));
978 if(get_entity_variability(entity) != variability_uninitialized
979 && get_entity_visibility(entity) != visibility_external_allocated)
981 if(is_compound_entity(entity))
983 if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
989 int i, n = (int) read_long(env);
990 for(i = 0; i < n; i++)
992 ir_entity *member = get_entity(env, read_long(env));
993 ir_node *irn = get_node_or_dummy(env, read_long(env));
994 add_compound_ent_value(entity, irn, member);
1000 ir_node *irn = get_node_or_dummy(env, read_long(env));
1001 set_atomic_ent_value(entity, irn);
1005 set_id(env, entnr, entity);
1006 printf("Insert entity %s %ld\n", name, entnr);
1009 /** Parses the whole type graph. */
1010 static int parse_typegraph(io_env_t *env)
1017 curfpos = ftell(env->file);
1019 current_ir_graph = get_const_code_irg();
1021 // parse all types first
1024 kind = read_str(env);
1025 if(kind[0] == '}' && !kind[1]) break;
1027 if(!strcmp(kind, "type"))
1034 fseek(env->file, curfpos, SEEK_SET);
1037 kind = read_str(env);
1038 if(kind[0] == '}' && !kind[1]) break;
1040 if(!strcmp(kind, "type"))
1042 else if(!strcmp(kind, "entity"))
1046 printf("Type graph element not supported yet: \"%s\"\n", kind);
1053 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1056 *nodename = read_str(env);
1057 if((*nodename)[0] == '}' && !(*nodename)[1]) return -1; // end-of-graph
1059 *nodenr = read_long(env);
1061 ARR_RESIZE(ir_node *, *preds, 0);
1064 for(numpreds = 0; !feof(env->file); numpreds++)
1067 ARR_APP1(long, *preds, read_long2(env, &endptr));
1068 if(*endptr == ']') break;
1073 /** Parses an IRG. */
1074 static int parse_graph(io_env_t *env, ir_graph *irg)
1076 long *preds = NEW_ARR_F(long, 16);
1077 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1078 int i, numpreds, ret = 1;
1080 const char *nodename;
1081 ir_node *node, *newnode;
1083 current_ir_graph = irg;
1089 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1090 if(numpreds == -1) break; // end-of-graph
1093 printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1098 ARR_RESIZE(ir_node *, prednodes, numpreds);
1099 for(i = 0; i < numpreds - 1; i++)
1100 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1102 node = get_node_or_null(env, nodenr);
1107 switch(symbol(nodename, tt_iro))
1111 ir_node *newendblock = get_node(env, preds[0]);
1112 newnode = get_irg_end(current_ir_graph);
1113 exchange(get_nodes_block(newnode), newendblock);
1114 for(i = 0; i < numpreds - 1; i++)
1115 add_irn_n(newnode, prednodes[i]);
1121 ir_node *newstartblock = get_node(env, preds[0]);
1122 newnode = get_irg_start(current_ir_graph);
1123 exchange(get_nodes_block(newnode), newstartblock);
1129 if(preds[0] != nodenr)
1131 printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1137 newnode = new_Block(numpreds - 1, prednodes);
1142 newnode = current_ir_graph->anchor;
1143 for(i = 0; i < numpreds - 1; i++)
1144 set_irn_n(newnode, i, prednodes[i]);
1145 set_irn_n(newnode, -1, get_node(env, preds[0]));
1150 long entnr = read_long(env);
1151 union symconst_symbol sym;
1152 sym.entity_p = get_entity(env, entnr);
1153 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1157 #include "gen_irio_import.inl"
1168 panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1172 exchange(node, newnode);
1173 /* Always update hash entry to avoid more uses of id nodes */
1174 set_id(env, nodenr, newnode);
1175 //printf("Insert %s %ld\n", nodename, nodenr);
1180 DEL_ARR_F(prednodes);
1185 /** Imports an previously exported textual representation of an (maybe partial) irp */
1186 void ir_import(const char *filename)
1188 int oldoptimize = get_optimize();
1189 firm_verification_t oldver = get_node_verification_mode();
1191 io_env_t *env = &ioenv;
1196 memset(env, 0, sizeof(*env));
1197 env->idset = new_set(id_cmp, 128);
1198 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1200 env->file = fopen(filename, "rt");
1208 do_node_verification(FIRM_VERIFICATION_OFF);
1212 const char *str = read_str(env);
1214 if(!strcmp(str, "typegraph"))
1216 if(!parse_typegraph(env)) break;
1218 else if(!strcmp(str, "irg"))
1220 ir_graph *irg = new_ir_graph(get_entity(env, read_long(env)), 0);
1221 if(!parse_graph(env, irg)) break;
1223 else if(!strcmp(str, "constirg"))
1225 if(!parse_graph(env, get_const_code_irg())) break;
1229 n = ARR_LEN(env->fixedtypes);
1230 for(i = 0; i < n; i++)
1231 set_type_state(env->fixedtypes[i], layout_fixed);
1233 DEL_ARR_F(env->fixedtypes);
1235 del_set(env->idset);
1237 irp_finalize_cons();
1239 do_node_verification(oldver);
1240 set_optimize(oldoptimize);