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)
46 typedef struct io_env_t
49 set *idset; /**< id_entry set, which maps from file ids to new Firm elements */
55 typedef struct lex_state_t
61 typedef enum typetag_t
77 typedef struct symbol_t
79 const char *str; /**< The name of this symbol. */
80 typetag_t typetag; /**< The type tag of this symbol. */
81 unsigned code; /**< The value of this symbol. */
84 typedef struct id_entry
90 /** The symbol table, a set of symbol_t elements. */
95 * Calculate a hash value for a string.
97 static unsigned string_hash(const char *str, int len)
99 return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
103 * Compare two symbol table entries.
105 static int symbol_cmp(const void *elt, const void *key, size_t size)
108 const symbol_t *entry = (const symbol_t *) elt;
109 const symbol_t *keyentry = (const symbol_t *) key;
111 res = entry->typetag - keyentry->typetag;
113 return strcmp(entry->str, keyentry->str);
116 static int id_cmp(const void *elt, const void *key, size_t size)
118 const id_entry *entry = (const id_entry *) elt;
119 const id_entry *keyentry = (const id_entry *) key;
121 return entry->id - keyentry->id;
124 /** Initializes the symbol table. May be called more than once without problems. */
125 static void symtbl_init(void)
129 /* Only initialize once */
133 symtbl = new_set(symbol_cmp, 256);
135 #define INSERT(s, tt, cod) \
137 key.typetag = (tt); \
139 set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1) + tt * 17)
141 #define INSERTENUM(tt, e) INSERT(#e, tt, e)
143 INSERT("array", tt_tpo, tpo_array);
144 INSERT("class", tt_tpo, tpo_class);
145 INSERT("method", tt_tpo, tpo_method);
146 INSERT("pointer", tt_tpo, tpo_pointer);
147 INSERT("primitive", tt_tpo, tpo_primitive);
148 INSERT("struct", tt_tpo, tpo_struct);
149 INSERT("Unknown", tt_tpo, tpo_unknown);
151 #include "gen_irio_lex.inl"
153 INSERTENUM(tt_align, align_non_aligned);
154 INSERTENUM(tt_align, align_is_aligned);
156 INSERTENUM(tt_allocation, allocation_automatic);
157 INSERTENUM(tt_allocation, allocation_parameter);
158 INSERTENUM(tt_allocation, allocation_dynamic);
159 INSERTENUM(tt_allocation, allocation_static);
161 INSERTENUM(tt_pin_state, op_pin_state_floats);
162 INSERTENUM(tt_pin_state, op_pin_state_pinned);
163 INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
164 INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
166 INSERTENUM(tt_type_state, layout_undefined);
167 INSERTENUM(tt_type_state, layout_fixed);
169 INSERTENUM(tt_variability, variability_uninitialized);
170 INSERTENUM(tt_variability, variability_initialized);
171 INSERTENUM(tt_variability, variability_part_constant);
172 INSERTENUM(tt_variability, variability_constant);
174 INSERTENUM(tt_visibility, visibility_local);
175 INSERTENUM(tt_visibility, visibility_external_visible);
176 INSERTENUM(tt_visibility, visibility_external_allocated);
178 INSERTENUM(tt_volatility, volatility_non_volatile);
179 INSERTENUM(tt_volatility, volatility_is_volatile);
181 INSERTENUM(tt_peculiarity, peculiarity_description);
182 INSERTENUM(tt_peculiarity, peculiarity_inherited);
183 INSERTENUM(tt_peculiarity, peculiarity_existent);
185 INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
186 INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
187 INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
188 INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
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_addess);
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);
209 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
210 static unsigned symbol(const char *str, typetag_t typetag)
212 symbol_t key, *entry;
215 key.typetag = typetag;
217 entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)) + typetag * 17);
218 return entry ? entry->code : SYMERROR;
221 static void *get_id(io_env_t *env, long id)
223 id_entry key, *entry;
226 entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
227 return entry ? entry->elem : NULL;
230 static void set_id(io_env_t *env, long id, void *elem)
235 set_insert(env->idset, &key, sizeof(key), (unsigned) id);
238 static void write_mode(io_env_t *env, ir_mode *mode)
240 fputs(get_mode_name(mode), env->file);
241 fputc(' ', env->file);
244 static void write_tarval(io_env_t *env, tarval *tv)
247 write_mode(env, get_tarval_mode(tv));
248 tarval_snprintf(buf, sizeof(buf), tv);
249 fputs(buf, env->file);
250 fputc(' ', env->file);
253 static void write_pin_state(io_env_t *env, ir_node *irn)
255 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
256 fputc(' ', env->file);
259 static void write_volatility(io_env_t *env, ir_node *irn)
263 if(is_Load(irn)) vol = get_Load_volatility(irn);
264 else if(is_Store(irn)) vol = get_Store_volatility(irn);
265 else assert(0 && "Invalid optype for write_volatility");
267 fputs(get_volatility_name(vol), env->file);
268 fputc(' ', env->file);
271 static void write_align(io_env_t *env, ir_node *irn)
275 if(is_Load(irn)) align = get_Load_align(irn);
276 else if(is_Store(irn)) align = get_Store_align(irn);
277 else assert(0 && "Invalid optype for write_align");
279 fputs(get_align_name(align), env->file);
280 fputc(' ', env->file);
283 static void write_builtin_kind(io_env_t *env, ir_node *irn)
285 fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
286 fputc(' ', env->file);
289 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
292 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
294 fputs(get_initializer_kind_name(ini_kind), f);
299 case IR_INITIALIZER_CONST:
300 fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
303 case IR_INITIALIZER_TARVAL:
304 write_tarval(env, get_initializer_tarval_value(ini));
307 case IR_INITIALIZER_NULL:
310 case IR_INITIALIZER_COMPOUND:
312 unsigned i, n = get_initializer_compound_n_entries(ini);
313 fprintf(f, "%d ", n);
314 for(i = 0; i < n; i++)
315 write_initializer(env, get_initializer_compound_value(ini, i));
320 panic("Unknown initializer kind");
324 static void export_type(io_env_t *env, ir_type *tp)
328 fprintf(f, "\ttype %ld %s \"%s\" %u %u %s %s ",
330 get_type_tpop_name(tp),
332 get_type_size_bytes(tp),
333 get_type_alignment_bytes(tp),
334 get_type_state_name(get_type_state(tp)),
335 get_visibility_name(get_type_visibility(tp)));
337 switch(get_type_tpop_code(tp))
341 int n = get_array_n_dimensions(tp);
342 fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
343 for(i = 0; i < n; i++)
345 ir_node *lower = get_array_lower_bound(tp, i);
346 ir_node *upper = get_array_upper_bound(tp, i);
348 if(is_Const(lower)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
349 else panic("Lower array bound is not constant");
351 if(is_Const(upper)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
352 else if(is_Unknown(upper)) fputs("unknown ", f);
353 else panic("Upper array bound is not constant");
359 /* TODO: inheritance stuff not supported yet */
360 printf("Inheritance of classes not supported yet!\n");
365 int nparams = get_method_n_params(tp);
366 int nresults = get_method_n_ress(tp);
367 fprintf(f, "%i %i ", nparams, nresults);
368 for(i = 0; i < nparams; i++)
369 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
370 for(i = 0; i < nresults; i++)
371 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
376 write_mode(env, get_type_mode(tp));
377 fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
381 write_mode(env, get_type_mode(tp));
391 printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
397 static void export_entity(io_env_t *env, ir_entity *ent)
399 ir_type *owner = get_entity_owner(ent);
400 fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %s %s %s %s %s ",
402 get_entity_name(ent),
403 ent->ld_name ? get_id_str(ent->ld_name) : "",
404 get_type_nr(get_entity_type(ent)),
406 get_entity_offset(ent),
407 (unsigned) get_entity_offset_bits_remainder(ent),
408 get_allocation_name(get_entity_allocation(ent)),
409 get_visibility_name(get_entity_visibility(ent)),
410 get_variability_name(get_entity_variability(ent)),
411 get_peculiarity_name(get_entity_peculiarity(ent)),
412 get_volatility_name(get_entity_volatility(ent)));
414 // TODO: inheritance stuff for class entities not supported yet
415 if(is_Class_type(owner) && owner != get_glob_type())
416 printf("Inheritance of class entities not supported yet!\n");
418 if(get_entity_variability(ent) != variability_uninitialized
419 && get_entity_visibility(ent) != visibility_external_allocated)
421 if(is_compound_entity(ent))
423 if(has_entity_initializer(ent))
425 fputs("initializer ", env->file);
426 write_initializer(env, get_entity_initializer(ent));
430 int i, n = get_compound_ent_n_values(ent);
431 fputs("noninitializer ", env->file);
432 fprintf(env->file, "%d ", n);
433 for(i = 0; i < n; i++)
435 ir_entity *member = get_compound_ent_value_member(ent, i);
436 ir_node *irn = get_compound_ent_value(ent, i);
437 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
443 ir_node *irn = get_atomic_ent_value(ent);
444 fprintf(env->file, "%ld ", get_irn_node_nr(irn));
448 fputc('\n', env->file);
451 static void export_type_or_ent(type_or_ent tore, void *ctx)
453 io_env_t *env = (io_env_t *) ctx;
455 switch(get_kind(tore.ent))
458 export_entity(env, tore.ent);
462 export_type(env, tore.typ);
466 printf("export_type_or_ent: Unknown type or entity.\n");
471 static void export_node(ir_node *irn, void *ctx)
473 io_env_t *env = (io_env_t *) ctx;
475 unsigned opcode = get_irn_opcode(irn);
477 if(env->ignoreblocks && opcode == iro_Block)
480 n = get_irn_arity(irn);
482 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
484 for(i = -1; i < n; i++)
486 ir_node *pred = get_irn_n(irn, i);
488 /* Anchor node may have NULL predecessors */
489 assert(is_Anchor(irn));
490 fputs("-1 ", env->file);
492 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
496 fprintf(env->file, "] { ");
500 #include "gen_irio_export.inl"
502 fputs("}\n", env->file);
505 /** Exports the whole irp to the given file in a textual form. */
506 void ir_export(const char *filename)
509 int i, n_irgs = get_irp_n_irgs(), n_pseudoirgs = get_irp_n_pseudo_irgs();
511 env.file = fopen(filename, "wt");
518 fputs("typegraph {\n", env.file);
520 type_walk_plus_frames(NULL, export_type_or_ent, &env);
521 /* TODO: Visit frame types and "types for value params"? */
523 for(i = 0; i < n_irgs; i++)
525 ir_graph *irg = get_irp_irg(i);
527 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
529 env.ignoreblocks = 0;
530 irg_block_walk_graph(irg, NULL, export_node, &env);
532 env.ignoreblocks = 1;
533 irg_walk_anchors(irg, NULL, export_node, &env);
536 fputs("}\n\nconstirg {\n", env.file);
537 walk_const_code(NULL, export_node, &env);
538 fputs("}\n", env.file);
543 /** Exports the given irg to the given file. */
544 void ir_export_irg(ir_graph *irg, const char *filename)
548 env.file = fopen(filename, "wt");
555 fputs("typegraph {\n", env.file);
557 type_walk_irg(irg, NULL, export_type_or_ent, &env);
559 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
561 env.ignoreblocks = 0;
562 irg_block_walk_graph(irg, NULL, export_node, &env);
564 env.ignoreblocks = 1;
565 irg_walk_anchors(irg, NULL, export_node, &env);
567 /* TODO: Only output needed constants */
568 fputs("}\n\nconstirg {\n", env.file);
569 walk_const_code(NULL, export_node, &env);
570 fputs("}\n", env.file);
575 static void save_lex_state(io_env_t *env, lex_state_t *state)
577 state->offs = ftell(env->file);
578 state->line = env->line;
579 state->col = env->col;
582 static void restore_lex_state(io_env_t *env, lex_state_t *state)
584 fseek(env->file, state->offs, SEEK_SET);
585 env->line = state->line;
586 env->col = state->col;
589 static int read_c(io_env_t *env)
591 int ch = fgetc(env->file);
610 /** Returns the first non-whitespace character or EOF. **/
611 static int skip_ws(io_env_t *env)
615 int ch = read_c(env);
630 static void skip_to(io_env_t *env, char to_ch)
637 while(ch != to_ch && ch != EOF);
640 static int expect_char(io_env_t *env, char ch)
642 int curch = skip_ws(env);
645 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
651 #define EXPECT(c) if(expect_char(env, (c))) {} else return 0
652 #define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)
654 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
657 for(i = 0; i < bufsize - 1; i++)
659 int ch = read_c(env);
669 i--; // skip whitespace
682 static const char *read_str(io_env_t *env)
684 static char buf[1024];
685 return read_str_to(env, buf, sizeof(buf));
688 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
691 EXPECT_OR_EXIT('\"');
692 for(i = 0; i < bufsize - 1; i++)
694 int ch = read_c(env);
697 printf("Unexpected end of quoted string!\n");
700 if(ch == '\"') break;
706 printf("Quoted string too long!\n");
713 static long read_long2(io_env_t *env, char **endptr)
715 static char buf[1024];
716 return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
719 static long read_long(io_env_t *env)
721 return read_long2(env, NULL);
724 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
726 ir_node *node = (ir_node *) get_id(env, nodenr);
727 if(node && node->kind != k_ir_node)
729 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
734 static ir_node *get_node(io_env_t *env, long nodenr)
736 ir_node *node = get_node_or_null(env, nodenr);
738 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
743 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
745 ir_node *node = get_node_or_null(env, nodenr);
748 node = new_Dummy(mode_X);
749 set_id(env, nodenr, node);
754 static ir_type *get_type(io_env_t *env, long typenr)
756 ir_type *type = (ir_type *) get_id(env, typenr);
759 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
761 else if(type->kind != k_type)
763 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
768 static ir_type *read_type(io_env_t *env)
770 return get_type(env, read_long(env));
773 static ir_entity *get_entity(io_env_t *env, long entnr)
775 ir_entity *entity = (ir_entity *) get_id(env, entnr);
778 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
781 else if(entity->kind != k_entity)
783 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
788 static ir_entity *read_entity(io_env_t *env)
790 return get_entity(env, read_long(env));
793 static ir_mode *read_mode(io_env_t *env)
795 static char buf[128];
798 read_str_to(env, buf, sizeof(buf));
800 n = get_irp_n_modes();
801 for(i = 0; i < n; i++)
803 ir_mode *mode = get_irp_mode(i);
804 if(!strcmp(buf, get_mode_name(mode)))
808 printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
812 static const char *get_typetag_name(typetag_t typetag)
816 case tt_align: return "align";
817 case tt_allocation: return "allocation";
818 case tt_builtin: return "builtin kind";
819 case tt_initializer: return "initializer kind";
820 case tt_iro: return "opcode";
821 case tt_peculiarity: return "peculiarity";
822 case tt_pin_state: return "pin state";
823 case tt_tpo: return "type";
824 case tt_type_state: return "type state";
825 case tt_variability: return "variability";
826 case tt_visibility: return "visibility";
827 case tt_volatility: return "volatility";
828 default: return "<UNKNOWN>";
833 * Read and decode an enum constant.
835 static unsigned read_enum(io_env_t *env, typetag_t typetag)
837 static char buf[128];
838 unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
842 printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
846 #define read_align(env) ((ir_align) read_enum(env, tt_align))
847 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
848 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
849 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
850 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
851 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
852 #define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
853 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
854 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
855 #define read_builtin_kind(env) ((ir_builtin_kind) read_enum(env, tt_builtin))
857 static ir_cons_flags get_cons_flags(io_env_t *env)
859 ir_cons_flags flags = cons_none;
861 op_pin_state pinstate = read_pin_state(env);
864 case op_pin_state_floats: flags |= cons_floats; break;
865 case op_pin_state_pinned: break;
867 panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
870 if(read_volatility(env) == volatility_is_volatile) flags |= cons_volatile;
871 if(read_align(env) == align_non_aligned) flags |= cons_unaligned;
876 static tarval *read_tv(io_env_t *env)
878 static char buf[128];
879 ir_mode *tvmode = read_mode(env);
880 read_str_to(env, buf, sizeof(buf));
881 return new_tarval_from_str(buf, strlen(buf), tvmode);
884 static ir_initializer_t *read_initializer(io_env_t *env)
887 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
891 case IR_INITIALIZER_CONST:
893 ir_node *irn = get_node_or_dummy(env, read_long(env));
894 return create_initializer_const(irn);
897 case IR_INITIALIZER_TARVAL:
898 return create_initializer_tarval(read_tv(env));
900 case IR_INITIALIZER_NULL:
901 return get_initializer_null();
903 case IR_INITIALIZER_COMPOUND:
905 unsigned i, n = (unsigned) read_long(env);
906 ir_initializer_t *ini = create_initializer_compound(n);
907 for(i = 0; i < n; i++)
909 ir_initializer_t *curini = read_initializer(env);
910 set_initializer_compound_value(ini, i, curini);
916 panic("Unknown initializer kind");
921 /** Reads a type description and remembers it by its id. */
922 static void import_type(io_env_t *env)
927 long typenr = read_long(env);
928 const char *tpop = read_str(env);
929 const char *name = read_qstr_to(env, buf, sizeof(buf));
930 unsigned size = (unsigned) read_long(env);
931 unsigned align = (unsigned) read_long(env);
932 ir_type_state state = read_type_state(env);
933 ir_visibility vis = read_visibility(env);
935 ident *id = new_id_from_str(name);
937 switch(symbol(tpop, tt_tpo))
941 int ndims = (int) read_long(env);
942 long elemtypenr = read_long(env);
943 ir_type *elemtype = get_type(env, elemtypenr);
945 type = new_type_array(id, ndims, elemtype);
946 for(i = 0; i < ndims; i++)
948 const char *str = read_str(env);
949 if(strcmp(str, "unknown"))
951 long lowerbound = strtol(str, NULL, 0);
952 set_array_lower_bound_int(type, i, lowerbound);
955 if(strcmp(str, "unknown"))
957 long upperbound = strtol(str, NULL, 0);
958 set_array_upper_bound_int(type, i, upperbound);
961 set_type_size_bytes(type, size);
966 type = new_type_class(id);
967 set_type_size_bytes(type, size);
972 int nparams = (int) read_long(env);
973 int nresults = (int) read_long(env);
975 type = new_type_method(id, nparams, nresults);
977 for(i = 0; i < nparams; i++)
979 long typenr = read_long(env);
980 ir_type *paramtype = get_type(env, typenr);
982 set_method_param_type(type, i, paramtype);
984 for(i = 0; i < nresults; i++)
986 long typenr = read_long(env);
987 ir_type *restype = get_type(env, typenr);
989 set_method_res_type(type, i, restype);
996 ir_mode *mode = read_mode(env);
997 ir_type *pointsto = get_type(env, read_long(env));
998 type = new_type_pointer(id, pointsto, mode);
1004 ir_mode *mode = read_mode(env);
1005 type = new_type_primitive(id, mode);
1010 type = new_type_struct(id);
1011 set_type_size_bytes(type, size);
1015 type = new_type_union(id);
1016 set_type_size_bytes(type, size);
1020 return; // ignore unknown type
1023 if(typenr != 0) // ignore global type
1024 printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
1029 set_type_alignment_bytes(type, align);
1030 set_type_visibility(type, vis);
1032 if(state == layout_fixed)
1033 ARR_APP1(ir_type *, env->fixedtypes, type);
1035 set_id(env, typenr, type);
1036 printf("Insert type %s %ld\n", name, typenr);
1039 /** Reads an entity description and remembers it by its id. */
1040 static void import_entity(io_env_t *env)
1042 char buf[1024], buf2[1024];
1043 long entnr = read_long(env);
1044 const char *name = read_qstr_to(env, buf, sizeof(buf));
1045 const char *ld_name = read_qstr_to(env, buf2, sizeof(buf2));
1046 long typenr = read_long(env);
1047 long ownertypenr = read_long(env);
1049 ir_type *type = get_type(env, typenr);
1050 ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1051 ir_entity *entity = new_entity(ownertype, new_id_from_str(name), type);
1053 if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
1054 set_entity_offset (entity, (int) read_long(env));
1055 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1056 set_entity_allocation (entity, read_allocation(env));
1057 set_entity_visibility (entity, read_visibility(env));
1058 set_entity_variability(entity, read_variability(env));
1059 set_entity_peculiarity(entity, read_peculiarity(env));
1060 set_entity_volatility (entity, read_volatility(env));
1062 if(get_entity_variability(entity) != variability_uninitialized
1063 && get_entity_visibility(entity) != visibility_external_allocated)
1065 if(is_compound_entity(entity))
1067 if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
1069 set_entity_initializer(entity, read_initializer(env));
1073 int i, n = (int) read_long(env);
1074 for(i = 0; i < n; i++)
1076 ir_entity *member = get_entity(env, read_long(env));
1077 ir_node *irn = get_node_or_dummy(env, read_long(env));
1078 add_compound_ent_value(entity, irn, member);
1084 ir_node *irn = get_node_or_dummy(env, read_long(env));
1085 set_atomic_ent_value(entity, irn);
1089 set_id(env, entnr, entity);
1090 printf("Insert entity %s %ld\n", name, entnr);
1093 /** Parses the whole type graph. */
1094 static int parse_typegraph(io_env_t *env)
1097 lex_state_t oldstate;
1101 save_lex_state(env, &oldstate);
1103 current_ir_graph = get_const_code_irg();
1105 // parse all types first
1108 kind = read_str(env);
1109 if(kind[0] == '}' && !kind[1]) break;
1111 if(!strcmp(kind, "type"))
1118 restore_lex_state(env, &oldstate);
1122 kind = read_str(env);
1123 if(kind[0] == '}' && !kind[1]) break;
1125 if(!strcmp(kind, "type"))
1127 else if(!strcmp(kind, "entity"))
1131 printf("Type graph element not supported yet: \"%s\"\n", kind);
1138 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1141 *nodename = read_str(env);
1142 if((*nodename)[0] == '}' && !(*nodename)[1]) return -1; // end-of-graph
1144 *nodenr = read_long(env);
1146 ARR_RESIZE(ir_node *, *preds, 0);
1149 for(numpreds = 0; !feof(env->file); numpreds++)
1152 ARR_APP1(long, *preds, read_long2(env, &endptr));
1153 if(*endptr == ']') break;
1158 /** Parses an IRG. */
1159 static int parse_graph(io_env_t *env, ir_graph *irg)
1161 long *preds = NEW_ARR_F(long, 16);
1162 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1163 int i, numpreds, ret = 1;
1165 const char *nodename;
1166 ir_node *node, *newnode;
1168 current_ir_graph = irg;
1174 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1175 if(numpreds == -1) break; // end-of-graph
1178 printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1183 ARR_RESIZE(ir_node *, prednodes, numpreds);
1184 for(i = 0; i < numpreds - 1; i++)
1185 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1187 node = get_node_or_null(env, nodenr);
1192 switch(symbol(nodename, tt_iro))
1196 ir_node *newendblock = get_node(env, preds[0]);
1197 newnode = get_irg_end(current_ir_graph);
1198 exchange(get_nodes_block(newnode), newendblock);
1199 for(i = 0; i < numpreds - 1; i++)
1200 add_irn_n(newnode, prednodes[i]);
1206 ir_node *newstartblock = get_node(env, preds[0]);
1207 newnode = get_irg_start(current_ir_graph);
1208 exchange(get_nodes_block(newnode), newstartblock);
1214 if(preds[0] != nodenr)
1216 printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1222 newnode = new_Block(numpreds - 1, prednodes);
1227 newnode = current_ir_graph->anchor;
1228 for(i = 0; i < numpreds - 1; i++)
1229 set_irn_n(newnode, i, prednodes[i]);
1230 set_irn_n(newnode, -1, get_node(env, preds[0]));
1235 long entnr = read_long(env);
1236 union symconst_symbol sym;
1237 sym.entity_p = get_entity(env, entnr);
1238 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1242 #include "gen_irio_import.inl"
1253 panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1257 exchange(node, newnode);
1258 /* Always update hash entry to avoid more uses of id nodes */
1259 set_id(env, nodenr, newnode);
1260 //printf("Insert %s %ld\n", nodename, nodenr);
1265 DEL_ARR_F(prednodes);
1270 /** Imports an previously exported textual representation of an (maybe partial) irp */
1271 void ir_import(const char *filename)
1273 int oldoptimize = get_optimize();
1274 firm_verification_t oldver = get_node_verification_mode();
1276 io_env_t *env = &ioenv;
1281 memset(env, 0, sizeof(*env));
1282 env->idset = new_set(id_cmp, 128);
1283 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1285 env->file = fopen(filename, "rt");
1293 do_node_verification(FIRM_VERIFICATION_OFF);
1297 const char *str = read_str(env);
1299 if(!strcmp(str, "typegraph"))
1301 if(!parse_typegraph(env)) break;
1303 else if(!strcmp(str, "irg"))
1305 ir_graph *irg = new_ir_graph(get_entity(env, read_long(env)), 0);
1306 if(!parse_graph(env, irg)) break;
1308 else if(!strcmp(str, "constirg"))
1310 if(!parse_graph(env, get_const_code_irg())) break;
1314 n = ARR_LEN(env->fixedtypes);
1315 for(i = 0; i < n; i++)
1316 set_type_state(env->fixedtypes[i], layout_fixed);
1318 DEL_ARR_F(env->fixedtypes);
1320 del_set(env->idset);
1322 irp_finalize_cons();
1324 do_node_verification(oldver);
1325 set_optimize(oldoptimize);