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
71 typedef struct symbol_t
73 const char *str; /**< The name of this symbol. */
74 typetag_t typetag; /**< The type tag of this symbol. */
75 unsigned code; /**< The value of this symbol. */
78 typedef struct id_entry
84 /** The symbol table, a set of symbol_t elements. */
89 * Calculate a hash value for a string.
91 static unsigned string_hash(const char *str, int len)
93 return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
97 * Compare two symbol table entries.
99 static int symbol_cmp(const void *elt, const void *key, size_t size)
101 const symbol_t *entry = (const symbol_t *) elt;
102 const symbol_t *keyentry = (const symbol_t *) key;
104 return strcmp(entry->str, keyentry->str);
107 static int id_cmp(const void *elt, const void *key, size_t size)
109 const id_entry *entry = (const id_entry *) elt;
110 const id_entry *keyentry = (const id_entry *) key;
112 return entry->id - keyentry->id;
115 /** Initializes the symbol table. May be called more than once without problems. */
116 static void symtbl_init(void)
120 /* Only initialize once */
124 symtbl = new_set(symbol_cmp, 256);
126 #define INSERT(s, tt, cod) \
128 key.typetag = (tt); \
130 set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1))
132 #define INSERTENUM(tt, e) INSERT(#e, tt, e)
134 INSERT("array", tt_tpo, tpo_array);
135 INSERT("class", tt_tpo, tpo_class);
136 INSERT("method", tt_tpo, tpo_method);
137 INSERT("pointer", tt_tpo, tpo_pointer);
138 INSERT("primitive", tt_tpo, tpo_primitive);
139 INSERT("struct", tt_tpo, tpo_struct);
140 INSERT("Unknown", tt_tpo, tpo_unknown);
142 #include "gen_irio_lex.inl"
144 INSERTENUM(tt_align, align_non_aligned);
145 INSERTENUM(tt_align, align_is_aligned);
147 INSERTENUM(tt_allocation, allocation_automatic);
148 INSERTENUM(tt_allocation, allocation_parameter);
149 INSERTENUM(tt_allocation, allocation_dynamic);
150 INSERTENUM(tt_allocation, allocation_static);
152 INSERTENUM(tt_pin_state, op_pin_state_floats);
153 INSERTENUM(tt_pin_state, op_pin_state_pinned);
154 INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
155 INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
157 INSERTENUM(tt_type_state, layout_undefined);
158 INSERTENUM(tt_type_state, layout_fixed);
160 INSERTENUM(tt_variability, variability_uninitialized);
161 INSERTENUM(tt_variability, variability_initialized);
162 INSERTENUM(tt_variability, variability_part_constant);
163 INSERTENUM(tt_variability, variability_constant);
165 INSERTENUM(tt_visibility, visibility_local);
166 INSERTENUM(tt_visibility, visibility_external_visible);
167 INSERTENUM(tt_visibility, visibility_external_allocated);
169 INSERTENUM(tt_volatility, volatility_non_volatile);
170 INSERTENUM(tt_volatility, volatility_is_volatile);
172 INSERTENUM(tt_peculiarity, peculiarity_description);
173 INSERTENUM(tt_peculiarity, peculiarity_inherited);
174 INSERTENUM(tt_peculiarity, peculiarity_existent);
176 INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
177 INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
178 INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
179 INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
181 INSERTENUM(tt_builtin, ir_bk_trap);
182 INSERTENUM(tt_builtin, ir_bk_debugbreak);
183 INSERTENUM(tt_builtin, ir_bk_return_address);
184 INSERTENUM(tt_builtin, ir_bk_frame_addess);
185 INSERTENUM(tt_builtin, ir_bk_prefetch);
186 INSERTENUM(tt_builtin, ir_bk_ffs);
187 INSERTENUM(tt_builtin, ir_bk_clz);
188 INSERTENUM(tt_builtin, ir_bk_ctz);
189 INSERTENUM(tt_builtin, ir_bk_popcount);
190 INSERTENUM(tt_builtin, ir_bk_parity);
191 INSERTENUM(tt_builtin, ir_bk_bswap);
192 INSERTENUM(tt_builtin, ir_bk_inport);
193 INSERTENUM(tt_builtin, ir_bk_outport);
194 INSERTENUM(tt_builtin, ir_bk_inner_trampoline);
200 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
201 static unsigned symbol(const char *str, typetag_t typetag)
203 symbol_t key, *entry;
207 entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)));
208 if (entry && entry->typetag == typetag) {
214 static void *get_id(io_env_t *env, long id)
216 id_entry key, *entry;
219 entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
220 return entry ? entry->elem : NULL;
223 static void set_id(io_env_t *env, long id, void *elem)
228 set_insert(env->idset, &key, sizeof(key), (unsigned) id);
231 static void write_mode(io_env_t *env, ir_mode *mode)
233 fputs(get_mode_name(mode), env->file);
234 fputc(' ', env->file);
237 static void write_tarval(io_env_t *env, tarval *tv)
240 write_mode(env, get_tarval_mode(tv));
241 tarval_snprintf(buf, sizeof(buf), tv);
242 fputs(buf, env->file);
243 fputc(' ', env->file);
246 static void write_pin_state(io_env_t *env, ir_node *irn)
248 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
249 fputc(' ', env->file);
252 static void write_volatility(io_env_t *env, ir_node *irn)
256 if(is_Load(irn)) vol = get_Load_volatility(irn);
257 else if(is_Store(irn)) vol = get_Store_volatility(irn);
258 else assert(0 && "Invalid optype for write_volatility");
260 fputs(get_volatility_name(vol), env->file);
261 fputc(' ', env->file);
264 static void write_align(io_env_t *env, ir_node *irn)
268 if(is_Load(irn)) align = get_Load_align(irn);
269 else if(is_Store(irn)) align = get_Store_align(irn);
270 else assert(0 && "Invalid optype for write_align");
272 fputs(get_align_name(align), env->file);
273 fputc(' ', env->file);
276 static void write_builtin_kind(io_env_t *env, ir_node *irn)
278 fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
279 fputc(' ', env->file);
282 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
285 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
287 fputs(get_initializer_kind_name(ini_kind), f);
292 case IR_INITIALIZER_CONST:
293 fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
296 case IR_INITIALIZER_TARVAL:
297 write_tarval(env, get_initializer_tarval_value(ini));
300 case IR_INITIALIZER_NULL:
303 case IR_INITIALIZER_COMPOUND:
305 unsigned i, n = get_initializer_compound_n_entries(ini);
306 fprintf(f, "%d ", n);
307 for(i = 0; i < n; i++)
308 write_initializer(env, get_initializer_compound_value(ini, i));
313 panic("Unknown initializer kind");
317 static void export_type(io_env_t *env, ir_type *tp)
321 fprintf(f, "\ttype %ld %s \"%s\" %u %u %s %s ",
323 get_type_tpop_name(tp),
325 get_type_size_bytes(tp),
326 get_type_alignment_bytes(tp),
327 get_type_state_name(get_type_state(tp)),
328 get_visibility_name(get_type_visibility(tp)));
330 switch(get_type_tpop_code(tp))
334 int n = get_array_n_dimensions(tp);
335 fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
336 for(i = 0; i < n; i++)
338 ir_node *lower = get_array_lower_bound(tp, i);
339 ir_node *upper = get_array_upper_bound(tp, i);
341 if(is_Const(lower)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
342 else panic("Lower array bound is not constant");
344 if(is_Const(upper)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
345 else if(is_Unknown(upper)) fputs("unknown ", f);
346 else panic("Upper array bound is not constant");
352 /* TODO: inheritance stuff not supported yet */
353 printf("Inheritance of classes not supported yet!\n");
358 int nparams = get_method_n_params(tp);
359 int nresults = get_method_n_ress(tp);
360 fprintf(f, "%i %i ", nparams, nresults);
361 for(i = 0; i < nparams; i++)
362 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
363 for(i = 0; i < nresults; i++)
364 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
369 write_mode(env, get_type_mode(tp));
370 fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
374 write_mode(env, get_type_mode(tp));
384 printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
390 static void export_entity(io_env_t *env, ir_entity *ent)
392 ir_type *owner = get_entity_owner(ent);
393 fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %s %s %s %s %s ",
395 get_entity_name(ent),
396 ent->ld_name ? get_id_str(ent->ld_name) : "",
397 get_type_nr(get_entity_type(ent)),
399 get_entity_offset(ent),
400 (unsigned) get_entity_offset_bits_remainder(ent),
401 get_allocation_name(get_entity_allocation(ent)),
402 get_visibility_name(get_entity_visibility(ent)),
403 get_variability_name(get_entity_variability(ent)),
404 get_peculiarity_name(get_entity_peculiarity(ent)),
405 get_volatility_name(get_entity_volatility(ent)));
407 // TODO: inheritance stuff for class entities not supported yet
408 if(is_Class_type(owner) && owner != get_glob_type())
409 printf("Inheritance of class entities not supported yet!\n");
411 if(get_entity_variability(ent) != variability_uninitialized
412 && get_entity_visibility(ent) != visibility_external_allocated)
414 if(is_compound_entity(ent))
416 if(has_entity_initializer(ent))
418 fputs("initializer ", env->file);
419 write_initializer(env, get_entity_initializer(ent));
423 int i, n = get_compound_ent_n_values(ent);
424 fputs("noninitializer ", env->file);
425 fprintf(env->file, "%d ", n);
426 for(i = 0; i < n; i++)
428 ir_entity *member = get_compound_ent_value_member(ent, i);
429 ir_node *irn = get_compound_ent_value(ent, i);
430 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
436 ir_node *irn = get_atomic_ent_value(ent);
437 fprintf(env->file, "%ld ", get_irn_node_nr(irn));
441 fputc('\n', env->file);
444 static void export_type_or_ent(type_or_ent tore, void *ctx)
446 io_env_t *env = (io_env_t *) ctx;
448 switch(get_kind(tore.ent))
451 export_entity(env, tore.ent);
455 export_type(env, tore.typ);
459 printf("export_type_or_ent: Unknown type or entity.\n");
464 static void export_node(ir_node *irn, void *ctx)
466 io_env_t *env = (io_env_t *) ctx;
468 unsigned opcode = get_irn_opcode(irn);
470 if(env->ignoreblocks && opcode == iro_Block)
473 n = get_irn_arity(irn);
475 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
477 for(i = -1; i < n; i++)
479 ir_node *pred = get_irn_n(irn, i);
481 /* Anchor node may have NULL predecessors */
482 assert(is_Anchor(irn));
483 fputs("-1 ", env->file);
485 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
489 fprintf(env->file, "] { ");
493 #include "gen_irio_export.inl"
495 fputs("}\n", env->file);
498 /** Exports the whole irp to the given file in a textual form. */
499 void ir_export(const char *filename)
502 int i, n_irgs = get_irp_n_irgs(), n_pseudoirgs = get_irp_n_pseudo_irgs();
504 env.file = fopen(filename, "wt");
511 fputs("typegraph {\n", env.file);
513 type_walk_plus_frames(NULL, export_type_or_ent, &env);
514 /* TODO: Visit frame types and "types for value params"? */
516 for(i = 0; i < n_irgs; i++)
518 ir_graph *irg = get_irp_irg(i);
520 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
522 env.ignoreblocks = 0;
523 irg_block_walk_graph(irg, NULL, export_node, &env);
525 env.ignoreblocks = 1;
526 irg_walk_anchors(irg, NULL, export_node, &env);
529 fputs("}\n\nconstirg {\n", env.file);
530 walk_const_code(NULL, export_node, &env);
531 fputs("}\n", env.file);
536 /** Exports the given irg to the given file. */
537 void ir_export_irg(ir_graph *irg, const char *filename)
541 env.file = fopen(filename, "wt");
548 fputs("typegraph {\n", env.file);
550 type_walk_irg(irg, NULL, export_type_or_ent, &env);
552 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
554 env.ignoreblocks = 0;
555 irg_block_walk_graph(irg, NULL, export_node, &env);
557 env.ignoreblocks = 1;
558 irg_walk_anchors(irg, NULL, export_node, &env);
560 /* TODO: Only output needed constants */
561 fputs("}\n\nconstirg {\n", env.file);
562 walk_const_code(NULL, export_node, &env);
563 fputs("}\n", env.file);
568 static int read_c(io_env_t *env)
570 int ch = fgetc(env->file);
589 /** Returns the first non-whitespace character or EOF. **/
590 static int skip_ws(io_env_t *env)
594 int ch = read_c(env);
609 static void skip_to(io_env_t *env, char to_ch)
616 while(ch != to_ch && ch != EOF);
619 static int expect_char(io_env_t *env, char ch)
621 int curch = skip_ws(env);
624 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
630 #define EXPECT(c) if(expect_char(env, (c))) {} else return 0
631 #define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)
633 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
636 for(i = 0; i < bufsize - 1; i++)
638 int ch = read_c(env);
648 i--; // skip whitespace
661 static const char *read_str(io_env_t *env)
663 static char buf[1024];
664 return read_str_to(env, buf, sizeof(buf));
667 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
670 EXPECT_OR_EXIT('\"');
671 for(i = 0; i < bufsize - 1; i++)
673 int ch = read_c(env);
676 printf("Unexpected end of quoted string!\n");
679 if(ch == '\"') break;
685 printf("Quoted string too long!\n");
692 static long read_long2(io_env_t *env, char **endptr)
694 static char buf[1024];
695 return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
698 static long read_long(io_env_t *env)
700 return read_long2(env, NULL);
703 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
705 ir_node *node = (ir_node *) get_id(env, nodenr);
706 if(node && node->kind != k_ir_node)
708 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
713 static ir_node *get_node(io_env_t *env, long nodenr)
715 ir_node *node = get_node_or_null(env, nodenr);
717 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
722 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
724 ir_node *node = get_node_or_null(env, nodenr);
727 node = new_Dummy(mode_X);
728 set_id(env, nodenr, node);
733 static ir_type *get_type(io_env_t *env, long typenr)
735 ir_type *type = (ir_type *) get_id(env, typenr);
738 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
740 else if(type->kind != k_type)
742 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
747 static ir_type *read_type(io_env_t *env)
749 return get_type(env, read_long(env));
752 static ir_entity *get_entity(io_env_t *env, long entnr)
754 ir_entity *entity = (ir_entity *) get_id(env, entnr);
757 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
760 else if(entity->kind != k_entity)
762 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
767 static ir_entity *read_entity(io_env_t *env)
769 return get_entity(env, read_long(env));
772 static ir_mode *read_mode(io_env_t *env)
774 static char buf[128];
777 read_str_to(env, buf, sizeof(buf));
779 n = get_irp_n_modes();
780 for(i = 0; i < n; i++)
782 ir_mode *mode = get_irp_mode(i);
783 if(!strcmp(buf, get_mode_name(mode)))
787 printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
791 static const char *get_typetag_name(typetag_t typetag)
795 case tt_align: return "align";
796 case tt_allocation: return "allocation";
797 case tt_builtin: return "builtin kind";
798 case tt_initializer: return "initializer kind";
799 case tt_iro: return "opcode";
800 case tt_peculiarity: return "peculiarity";
801 case tt_pin_state: return "pin state";
802 case tt_tpo: return "type";
803 case tt_type_state: return "type state";
804 case tt_variability: return "variability";
805 case tt_visibility: return "visibility";
806 case tt_volatility: return "volatility";
807 default: return "<UNKNOWN>";
812 * Read and decode an enum constant.
814 static unsigned read_enum(io_env_t *env, typetag_t typetag)
816 static char buf[128];
817 unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
821 printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
825 #define read_align(env) ((ir_align) read_enum(env, tt_align))
826 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
827 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
828 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
829 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
830 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
831 #define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
832 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
833 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
834 #define read_builtin_kind(env) ((ir_builtin_kind) read_enum(env, tt_builtin))
836 static ir_cons_flags get_cons_flags(io_env_t *env)
838 ir_cons_flags flags = cons_none;
840 op_pin_state pinstate = read_pin_state(env);
843 case op_pin_state_floats: flags |= cons_floats; break;
844 case op_pin_state_pinned: break;
846 panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
849 if(read_volatility(env) == volatility_is_volatile) flags |= cons_volatile;
850 if(read_align(env) == align_non_aligned) flags |= cons_unaligned;
855 static tarval *read_tv(io_env_t *env)
857 static char buf[128];
858 ir_mode *tvmode = read_mode(env);
859 read_str_to(env, buf, sizeof(buf));
860 return new_tarval_from_str(buf, strlen(buf), tvmode);
863 static ir_initializer_t *read_initializer(io_env_t *env)
866 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
870 case IR_INITIALIZER_CONST:
872 ir_node *irn = get_node_or_dummy(env, read_long(env));
873 return create_initializer_const(irn);
876 case IR_INITIALIZER_TARVAL:
877 return create_initializer_tarval(read_tv(env));
879 case IR_INITIALIZER_NULL:
880 return get_initializer_null();
882 case IR_INITIALIZER_COMPOUND:
884 unsigned i, n = (unsigned) read_long(env);
885 ir_initializer_t *ini = create_initializer_compound(n);
886 for(i = 0; i < n; i++)
888 ir_initializer_t *curini = read_initializer(env);
889 set_initializer_compound_value(ini, i, curini);
895 panic("Unknown initializer kind");
900 /** Reads a type description and remembers it by its id. */
901 static void import_type(io_env_t *env)
906 long typenr = read_long(env);
907 const char *tpop = read_str(env);
908 const char *name = read_qstr_to(env, buf, sizeof(buf));
909 unsigned size = (unsigned) read_long(env);
910 unsigned align = (unsigned) read_long(env);
911 ir_type_state state = read_type_state(env);
912 ir_visibility vis = read_visibility(env);
914 ident *id = new_id_from_str(name);
916 switch(symbol(tpop, tt_tpo))
920 int ndims = (int) read_long(env);
921 long elemtypenr = read_long(env);
922 ir_type *elemtype = get_type(env, elemtypenr);
924 type = new_type_array(id, ndims, elemtype);
925 for(i = 0; i < ndims; i++)
927 const char *str = read_str(env);
928 if(strcmp(str, "unknown"))
930 long lowerbound = strtol(str, NULL, 0);
931 set_array_lower_bound_int(type, i, lowerbound);
934 if(strcmp(str, "unknown"))
936 long upperbound = strtol(str, NULL, 0);
937 set_array_upper_bound_int(type, i, upperbound);
940 set_type_size_bytes(type, size);
945 type = new_type_class(id);
946 set_type_size_bytes(type, size);
951 int nparams = (int) read_long(env);
952 int nresults = (int) read_long(env);
954 type = new_type_method(id, nparams, nresults);
956 for(i = 0; i < nparams; i++)
958 long typenr = read_long(env);
959 ir_type *paramtype = get_type(env, typenr);
961 set_method_param_type(type, i, paramtype);
963 for(i = 0; i < nresults; i++)
965 long typenr = read_long(env);
966 ir_type *restype = get_type(env, typenr);
968 set_method_res_type(type, i, restype);
975 ir_mode *mode = read_mode(env);
976 ir_type *pointsto = get_type(env, read_long(env));
977 type = new_type_pointer(id, pointsto, mode);
983 ir_mode *mode = read_mode(env);
984 type = new_type_primitive(id, mode);
989 type = new_type_struct(id);
990 set_type_size_bytes(type, size);
994 type = new_type_union(id);
995 set_type_size_bytes(type, size);
999 return; // ignore unknown type
1002 if(typenr != 0) // ignore global type
1003 printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
1008 set_type_alignment_bytes(type, align);
1009 set_type_visibility(type, vis);
1011 if(state == layout_fixed)
1012 ARR_APP1(ir_type *, env->fixedtypes, type);
1014 set_id(env, typenr, type);
1015 printf("Insert type %s %ld\n", name, typenr);
1018 /** Reads an entity description and remembers it by its id. */
1019 static void import_entity(io_env_t *env)
1021 char buf[1024], buf2[1024];
1022 long entnr = read_long(env);
1023 const char *name = read_qstr_to(env, buf, sizeof(buf));
1024 const char *ld_name = read_qstr_to(env, buf2, sizeof(buf2));
1025 long typenr = read_long(env);
1026 long ownertypenr = read_long(env);
1028 ir_type *type = get_type(env, typenr);
1029 ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1030 ir_entity *entity = new_entity(ownertype, new_id_from_str(name), type);
1032 if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
1033 set_entity_offset (entity, (int) read_long(env));
1034 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1035 set_entity_allocation (entity, read_allocation(env));
1036 set_entity_visibility (entity, read_visibility(env));
1037 set_entity_variability(entity, read_variability(env));
1038 set_entity_peculiarity(entity, read_peculiarity(env));
1039 set_entity_volatility (entity, read_volatility(env));
1041 if(get_entity_variability(entity) != variability_uninitialized
1042 && get_entity_visibility(entity) != visibility_external_allocated)
1044 if(is_compound_entity(entity))
1046 if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
1048 set_entity_initializer(entity, read_initializer(env));
1052 int i, n = (int) read_long(env);
1053 for(i = 0; i < n; i++)
1055 ir_entity *member = get_entity(env, read_long(env));
1056 ir_node *irn = get_node_or_dummy(env, read_long(env));
1057 add_compound_ent_value(entity, irn, member);
1063 ir_node *irn = get_node_or_dummy(env, read_long(env));
1064 set_atomic_ent_value(entity, irn);
1068 set_id(env, entnr, entity);
1069 printf("Insert entity %s %ld\n", name, entnr);
1072 /** Parses the whole type graph. */
1073 static int parse_typegraph(io_env_t *env)
1080 curfpos = ftell(env->file);
1082 current_ir_graph = get_const_code_irg();
1084 // parse all types first
1087 kind = read_str(env);
1088 if(kind[0] == '}' && !kind[1]) break;
1090 if(!strcmp(kind, "type"))
1097 fseek(env->file, curfpos, SEEK_SET);
1100 kind = read_str(env);
1101 if(kind[0] == '}' && !kind[1]) break;
1103 if(!strcmp(kind, "type"))
1105 else if(!strcmp(kind, "entity"))
1109 printf("Type graph element not supported yet: \"%s\"\n", kind);
1116 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1119 *nodename = read_str(env);
1120 if((*nodename)[0] == '}' && !(*nodename)[1]) return -1; // end-of-graph
1122 *nodenr = read_long(env);
1124 ARR_RESIZE(ir_node *, *preds, 0);
1127 for(numpreds = 0; !feof(env->file); numpreds++)
1130 ARR_APP1(long, *preds, read_long2(env, &endptr));
1131 if(*endptr == ']') break;
1136 /** Parses an IRG. */
1137 static int parse_graph(io_env_t *env, ir_graph *irg)
1139 long *preds = NEW_ARR_F(long, 16);
1140 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1141 int i, numpreds, ret = 1;
1143 const char *nodename;
1144 ir_node *node, *newnode;
1146 current_ir_graph = irg;
1152 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1153 if(numpreds == -1) break; // end-of-graph
1156 printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1161 ARR_RESIZE(ir_node *, prednodes, numpreds);
1162 for(i = 0; i < numpreds - 1; i++)
1163 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1165 node = get_node_or_null(env, nodenr);
1170 switch(symbol(nodename, tt_iro))
1174 ir_node *newendblock = get_node(env, preds[0]);
1175 newnode = get_irg_end(current_ir_graph);
1176 exchange(get_nodes_block(newnode), newendblock);
1177 for(i = 0; i < numpreds - 1; i++)
1178 add_irn_n(newnode, prednodes[i]);
1184 ir_node *newstartblock = get_node(env, preds[0]);
1185 newnode = get_irg_start(current_ir_graph);
1186 exchange(get_nodes_block(newnode), newstartblock);
1192 if(preds[0] != nodenr)
1194 printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1200 newnode = new_Block(numpreds - 1, prednodes);
1205 newnode = current_ir_graph->anchor;
1206 for(i = 0; i < numpreds - 1; i++)
1207 set_irn_n(newnode, i, prednodes[i]);
1208 set_irn_n(newnode, -1, get_node(env, preds[0]));
1213 long entnr = read_long(env);
1214 union symconst_symbol sym;
1215 sym.entity_p = get_entity(env, entnr);
1216 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1220 #include "gen_irio_import.inl"
1231 panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1235 exchange(node, newnode);
1236 /* Always update hash entry to avoid more uses of id nodes */
1237 set_id(env, nodenr, newnode);
1238 //printf("Insert %s %ld\n", nodename, nodenr);
1243 DEL_ARR_F(prednodes);
1248 /** Imports an previously exported textual representation of an (maybe partial) irp */
1249 void ir_import(const char *filename)
1251 int oldoptimize = get_optimize();
1252 firm_verification_t oldver = get_node_verification_mode();
1254 io_env_t *env = &ioenv;
1259 memset(env, 0, sizeof(*env));
1260 env->idset = new_set(id_cmp, 128);
1261 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1263 env->file = fopen(filename, "rt");
1271 do_node_verification(FIRM_VERIFICATION_OFF);
1275 const char *str = read_str(env);
1277 if(!strcmp(str, "typegraph"))
1279 if(!parse_typegraph(env)) break;
1281 else if(!strcmp(str, "irg"))
1283 ir_graph *irg = new_ir_graph(get_entity(env, read_long(env)), 0);
1284 if(!parse_graph(env, irg)) break;
1286 else if(!strcmp(str, "constirg"))
1288 if(!parse_graph(env, get_const_code_irg())) break;
1292 n = ARR_LEN(env->fixedtypes);
1293 for(i = 0; i < n; i++)
1294 set_type_state(env->fixedtypes[i], layout_fixed);
1296 DEL_ARR_F(env->fixedtypes);
1298 del_set(env->idset);
1300 irp_finalize_cons();
1302 do_node_verification(oldver);
1303 set_optimize(oldoptimize);