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
78 typedef enum keyword_t
89 typedef struct symbol_t
91 const char *str; /**< The name of this symbol. */
92 typetag_t typetag; /**< The type tag of this symbol. */
93 unsigned code; /**< The value of this symbol. */
96 typedef struct id_entry
102 /** The symbol table, a set of symbol_t elements. */
107 * Calculate a hash value for a string.
109 static unsigned string_hash(const char *str, int len)
111 return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
115 * Compare two symbol table entries.
117 static int symbol_cmp(const void *elt, const void *key, size_t size)
120 const symbol_t *entry = (const symbol_t *) elt;
121 const symbol_t *keyentry = (const symbol_t *) key;
123 res = entry->typetag - keyentry->typetag;
125 return strcmp(entry->str, keyentry->str);
128 static int id_cmp(const void *elt, const void *key, size_t size)
130 const id_entry *entry = (const id_entry *) elt;
131 const id_entry *keyentry = (const id_entry *) key;
133 return entry->id - keyentry->id;
136 /** Initializes the symbol table. May be called more than once without problems. */
137 static void symtbl_init(void)
141 /* Only initialize once */
145 symtbl = new_set(symbol_cmp, 256);
147 #define INSERT(s, tt, cod) \
149 key.typetag = (tt); \
151 set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1) + tt * 17)
153 #define INSERTENUM(tt, e) INSERT(#e, tt, e)
154 #define INSERTKEYWORD(k) INSERT(#k, tt_keyword, kw_##k)
156 INSERT("array", tt_tpo, tpo_array);
157 INSERT("class", tt_tpo, tpo_class);
158 INSERT("method", tt_tpo, tpo_method);
159 INSERT("pointer", tt_tpo, tpo_pointer);
160 INSERT("primitive", tt_tpo, tpo_primitive);
161 INSERT("struct", tt_tpo, tpo_struct);
162 INSERT("union", tt_tpo, tpo_union);
163 INSERT("Unknown", tt_tpo, tpo_unknown);
165 INSERTKEYWORD(constirg);
166 INSERTKEYWORD(entity);
167 INSERTKEYWORD(frametype);
169 INSERTKEYWORD(valuetype);
171 INSERTKEYWORD(typegraph);
173 #include "gen_irio_lex.inl"
175 INSERTENUM(tt_align, align_non_aligned);
176 INSERTENUM(tt_align, align_is_aligned);
178 INSERTENUM(tt_allocation, allocation_automatic);
179 INSERTENUM(tt_allocation, allocation_parameter);
180 INSERTENUM(tt_allocation, allocation_dynamic);
181 INSERTENUM(tt_allocation, allocation_static);
183 INSERTENUM(tt_pin_state, op_pin_state_floats);
184 INSERTENUM(tt_pin_state, op_pin_state_pinned);
185 INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
186 INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
188 INSERTENUM(tt_type_state, layout_undefined);
189 INSERTENUM(tt_type_state, layout_fixed);
191 INSERTENUM(tt_variability, variability_uninitialized);
192 INSERTENUM(tt_variability, variability_initialized);
193 INSERTENUM(tt_variability, variability_part_constant);
194 INSERTENUM(tt_variability, variability_constant);
196 INSERTENUM(tt_visibility, visibility_local);
197 INSERTENUM(tt_visibility, visibility_external_visible);
198 INSERTENUM(tt_visibility, visibility_external_allocated);
200 INSERTENUM(tt_volatility, volatility_non_volatile);
201 INSERTENUM(tt_volatility, volatility_is_volatile);
203 INSERTENUM(tt_peculiarity, peculiarity_description);
204 INSERTENUM(tt_peculiarity, peculiarity_inherited);
205 INSERTENUM(tt_peculiarity, peculiarity_existent);
207 INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
208 INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
209 INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
210 INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
212 INSERTENUM(tt_builtin, ir_bk_trap);
213 INSERTENUM(tt_builtin, ir_bk_debugbreak);
214 INSERTENUM(tt_builtin, ir_bk_return_address);
215 INSERTENUM(tt_builtin, ir_bk_frame_addess);
216 INSERTENUM(tt_builtin, ir_bk_prefetch);
217 INSERTENUM(tt_builtin, ir_bk_ffs);
218 INSERTENUM(tt_builtin, ir_bk_clz);
219 INSERTENUM(tt_builtin, ir_bk_ctz);
220 INSERTENUM(tt_builtin, ir_bk_popcount);
221 INSERTENUM(tt_builtin, ir_bk_parity);
222 INSERTENUM(tt_builtin, ir_bk_bswap);
223 INSERTENUM(tt_builtin, ir_bk_inport);
224 INSERTENUM(tt_builtin, ir_bk_outport);
225 INSERTENUM(tt_builtin, ir_bk_inner_trampoline);
231 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
232 static unsigned symbol(const char *str, typetag_t typetag)
234 symbol_t key, *entry;
237 key.typetag = typetag;
239 entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)) + typetag * 17);
240 return entry ? entry->code : SYMERROR;
243 static void *get_id(io_env_t *env, long id)
245 id_entry key, *entry;
248 entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
249 return entry ? entry->elem : NULL;
252 static void set_id(io_env_t *env, long id, void *elem)
257 set_insert(env->idset, &key, sizeof(key), (unsigned) id);
260 static void write_mode(io_env_t *env, ir_mode *mode)
262 fputs(get_mode_name(mode), env->file);
263 fputc(' ', env->file);
266 static void write_tarval(io_env_t *env, tarval *tv)
269 write_mode(env, get_tarval_mode(tv));
270 tarval_snprintf(buf, sizeof(buf), tv);
271 fputs(buf, env->file);
272 fputc(' ', env->file);
275 static void write_pin_state(io_env_t *env, ir_node *irn)
277 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
278 fputc(' ', env->file);
281 static void write_volatility(io_env_t *env, ir_node *irn)
285 if(is_Load(irn)) vol = get_Load_volatility(irn);
286 else if(is_Store(irn)) vol = get_Store_volatility(irn);
287 else assert(0 && "Invalid optype for write_volatility");
289 fputs(get_volatility_name(vol), env->file);
290 fputc(' ', env->file);
293 static void write_align(io_env_t *env, ir_node *irn)
297 if(is_Load(irn)) align = get_Load_align(irn);
298 else if(is_Store(irn)) align = get_Store_align(irn);
299 else assert(0 && "Invalid optype for write_align");
301 fputs(get_align_name(align), env->file);
302 fputc(' ', env->file);
305 static void write_builtin_kind(io_env_t *env, ir_node *irn)
307 fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
308 fputc(' ', env->file);
311 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
314 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
316 fputs(get_initializer_kind_name(ini_kind), f);
321 case IR_INITIALIZER_CONST:
322 fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
325 case IR_INITIALIZER_TARVAL:
326 write_tarval(env, get_initializer_tarval_value(ini));
329 case IR_INITIALIZER_NULL:
332 case IR_INITIALIZER_COMPOUND:
334 unsigned i, n = get_initializer_compound_n_entries(ini);
335 fprintf(f, "%d ", n);
336 for(i = 0; i < n; i++)
337 write_initializer(env, get_initializer_compound_value(ini, i));
342 panic("Unknown initializer kind");
346 static void export_type_common(io_env_t *env, ir_type *tp)
348 fprintf(env->file, "\t%s %ld %s \"%s\" %u %u %s %s ",
349 is_frame_type(tp) ? "frametype" : is_value_param_type(tp) ? "valuetype" : "type",
351 get_type_tpop_name(tp),
353 get_type_size_bytes(tp),
354 get_type_alignment_bytes(tp),
355 get_type_state_name(get_type_state(tp)),
356 get_visibility_name(get_type_visibility(tp)));
359 static void export_type_pre(io_env_t *env, ir_type *tp)
364 // skip types to be handled by post walker
365 switch(get_type_tpop_code(tp))
373 export_type_common(env, tp);
375 switch(get_type_tpop_code(tp))
378 /* TODO: inheritance stuff not supported yet */
379 printf("Inheritance of classes not supported yet!\n");
383 write_mode(env, get_type_mode(tp));
396 printf("export_type_pre: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
402 static void export_type_post(io_env_t *env, ir_type *tp)
407 // skip types already handled by pre walker
408 switch(get_type_tpop_code(tp))
418 export_type_common(env, tp);
420 switch(get_type_tpop_code(tp))
424 int n = get_array_n_dimensions(tp);
425 fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
426 for(i = 0; i < n; i++)
428 ir_node *lower = get_array_lower_bound(tp, i);
429 ir_node *upper = get_array_upper_bound(tp, i);
431 if(is_Const(lower)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
432 else panic("Lower array bound is not constant");
434 if(is_Const(upper)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
435 else if(is_Unknown(upper)) fputs("unknown ", f);
436 else panic("Upper array bound is not constant");
443 int nparams = get_method_n_params(tp);
444 int nresults = get_method_n_ress(tp);
445 fprintf(f, "0x%X 0x%X %i %i ", get_method_calling_convention(tp),
446 get_method_additional_properties(tp), nparams, nresults);
447 for(i = 0; i < nparams; i++)
448 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
449 for(i = 0; i < nresults; i++)
450 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
451 fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
456 write_mode(env, get_type_mode(tp));
457 fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
461 printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
467 static void export_entity(io_env_t *env, ir_entity *ent)
469 ir_type *owner = get_entity_owner(ent);
470 fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %s %s %s %s %s ",
472 get_entity_name(ent),
473 ent->ld_name ? get_id_str(ent->ld_name) : "",
474 get_type_nr(get_entity_type(ent)),
476 get_entity_offset(ent),
477 (unsigned) get_entity_offset_bits_remainder(ent),
478 get_allocation_name(get_entity_allocation(ent)),
479 get_visibility_name(get_entity_visibility(ent)),
480 get_variability_name(get_entity_variability(ent)),
481 get_peculiarity_name(get_entity_peculiarity(ent)),
482 get_volatility_name(get_entity_volatility(ent)));
484 // TODO: inheritance stuff for class entities not supported yet
485 if(is_Class_type(owner) && owner != get_glob_type())
486 printf("Inheritance of class entities not supported yet!\n");
488 if(get_entity_variability(ent) != variability_uninitialized
489 && get_entity_visibility(ent) != visibility_external_allocated)
491 if(is_compound_entity(ent))
493 if(has_entity_initializer(ent))
495 fputs("initializer ", env->file);
496 write_initializer(env, get_entity_initializer(ent));
500 int i, n = get_compound_ent_n_values(ent);
501 fputs("noninitializer ", env->file);
502 fprintf(env->file, "%d ", n);
503 for(i = 0; i < n; i++)
505 ir_entity *member = get_compound_ent_value_member(ent, i);
506 ir_node *irn = get_compound_ent_value(ent, i);
507 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
513 ir_node *irn = get_atomic_ent_value(ent);
514 fprintf(env->file, "%ld ", get_irn_node_nr(irn));
518 fputc('\n', env->file);
521 static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
523 io_env_t *env = (io_env_t *) ctx;
524 if(get_kind(tore.typ) == k_type)
525 export_type_pre(env, tore.typ);
528 static void export_type_or_ent_post(type_or_ent tore, void *ctx)
530 io_env_t *env = (io_env_t *) ctx;
532 switch(get_kind(tore.ent))
535 export_entity(env, tore.ent);
539 export_type_post(env, tore.typ);
543 printf("export_type_or_ent_post: Unknown type or entity.\n");
548 static void export_node(ir_node *irn, void *ctx)
550 io_env_t *env = (io_env_t *) ctx;
552 unsigned opcode = get_irn_opcode(irn);
554 if(env->ignoreblocks && opcode == iro_Block)
557 n = get_irn_arity(irn);
559 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
561 for(i = -1; i < n; i++)
563 ir_node *pred = get_irn_n(irn, i);
565 /* Anchor node may have NULL predecessors */
566 assert(is_Anchor(irn));
567 fputs("-1 ", env->file);
569 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
573 fprintf(env->file, "] { ");
577 #include "gen_irio_export.inl"
579 fputs("}\n", env->file);
582 /** Exports the whole irp to the given file in a textual form. */
583 void ir_export(const char *filename)
586 int i, n_irgs = get_irp_n_irgs(), n_pseudoirgs = get_irp_n_pseudo_irgs();
588 env.file = fopen(filename, "wt");
595 fputs("typegraph {\n", env.file);
597 type_walk_plus_frames(export_type_or_ent_pre, export_type_or_ent_post, &env);
598 /* TODO: Visit frame types and "types for value params"? */
600 for(i = 0; i < n_irgs; i++)
602 ir_graph *irg = get_irp_irg(i);
603 ir_type *valuetype = get_irg_value_param_type(irg);
605 fprintf(env.file, "}\n\nirg %ld %ld %ld {\n", get_entity_nr(get_irg_entity(irg)),
606 get_type_nr(get_irg_frame_type(irg)),
607 valuetype == NULL ? -1 : get_type_nr(valuetype));
609 env.ignoreblocks = 0;
610 irg_block_walk_graph(irg, NULL, export_node, &env);
612 env.ignoreblocks = 1;
613 irg_walk_anchors(irg, NULL, export_node, &env);
616 fputs("}\n\nconstirg {\n", env.file);
617 walk_const_code(NULL, export_node, &env);
618 fputs("}\n", env.file);
623 /** Exports the given irg to the given file. */
624 void ir_export_irg(ir_graph *irg, const char *filename)
628 env.file = fopen(filename, "wt");
635 fputs("typegraph {\n", env.file);
637 type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
639 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
641 env.ignoreblocks = 0;
642 irg_block_walk_graph(irg, NULL, export_node, &env);
644 env.ignoreblocks = 1;
645 irg_walk_anchors(irg, NULL, export_node, &env);
647 /* TODO: Only output needed constants */
648 fputs("}\n\nconstirg {\n", env.file);
649 walk_const_code(NULL, export_node, &env);
650 fputs("}\n", env.file);
655 static void save_lex_state(io_env_t *env, lex_state_t *state)
657 state->offs = ftell(env->file);
658 state->line = env->line;
659 state->col = env->col;
662 static void restore_lex_state(io_env_t *env, lex_state_t *state)
664 fseek(env->file, state->offs, SEEK_SET);
665 env->line = state->line;
666 env->col = state->col;
669 static int read_c(io_env_t *env)
671 int ch = fgetc(env->file);
690 /** Returns the first non-whitespace character or EOF. **/
691 static int skip_ws(io_env_t *env)
695 int ch = read_c(env);
710 static void skip_to(io_env_t *env, char to_ch)
717 while(ch != to_ch && ch != EOF);
720 static int expect_char(io_env_t *env, char ch)
722 int curch = skip_ws(env);
725 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
731 #define EXPECT(c) if(expect_char(env, (c))) {} else return 0
732 #define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)
734 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
737 for(i = 0; i < bufsize - 1; i++)
739 int ch = read_c(env);
749 i--; // skip whitespace
762 static const char *read_str(io_env_t *env)
764 static char buf[1024];
765 return read_str_to(env, buf, sizeof(buf));
768 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
771 EXPECT_OR_EXIT('\"');
772 for(i = 0; i < bufsize - 1; i++)
774 int ch = read_c(env);
777 printf("Unexpected end of quoted string!\n");
780 if(ch == '\"') break;
786 printf("Quoted string too long!\n");
793 static long read_long2(io_env_t *env, char **endptr)
795 static char buf[1024];
796 return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
799 static long read_long(io_env_t *env)
801 return read_long2(env, NULL);
804 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
806 ir_node *node = (ir_node *) get_id(env, nodenr);
807 if(node && node->kind != k_ir_node)
809 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
814 static ir_node *get_node(io_env_t *env, long nodenr)
816 ir_node *node = get_node_or_null(env, nodenr);
818 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
823 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
825 ir_node *node = get_node_or_null(env, nodenr);
828 node = new_Dummy(mode_X);
829 set_id(env, nodenr, node);
834 static ir_type *get_type(io_env_t *env, long typenr)
836 ir_type *type = (ir_type *) get_id(env, typenr);
839 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
841 else if(type->kind != k_type)
843 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
848 static ir_type *read_type(io_env_t *env)
850 return get_type(env, read_long(env));
853 static ir_entity *get_entity(io_env_t *env, long entnr)
855 ir_entity *entity = (ir_entity *) get_id(env, entnr);
858 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
861 else if(entity->kind != k_entity)
863 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
868 static ir_entity *read_entity(io_env_t *env)
870 return get_entity(env, read_long(env));
873 static ir_mode *read_mode(io_env_t *env)
875 static char buf[128];
878 read_str_to(env, buf, sizeof(buf));
880 n = get_irp_n_modes();
881 for(i = 0; i < n; i++)
883 ir_mode *mode = get_irp_mode(i);
884 if(!strcmp(buf, get_mode_name(mode)))
888 printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
892 static const char *get_typetag_name(typetag_t typetag)
896 case tt_align: return "align";
897 case tt_allocation: return "allocation";
898 case tt_builtin: return "builtin kind";
899 case tt_initializer: return "initializer kind";
900 case tt_iro: return "opcode";
901 case tt_peculiarity: return "peculiarity";
902 case tt_pin_state: return "pin state";
903 case tt_tpo: return "type";
904 case tt_type_state: return "type state";
905 case tt_variability: return "variability";
906 case tt_visibility: return "visibility";
907 case tt_volatility: return "volatility";
908 default: return "<UNKNOWN>";
913 * Read and decode an enum constant.
915 static unsigned read_enum(io_env_t *env, typetag_t typetag)
917 static char buf[128];
918 unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
922 printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
926 #define read_align(env) ((ir_align) read_enum(env, tt_align))
927 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
928 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
929 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
930 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
931 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
932 #define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
933 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
934 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
935 #define read_builtin_kind(env) ((ir_builtin_kind) read_enum(env, tt_builtin))
937 static ir_cons_flags get_cons_flags(io_env_t *env)
939 ir_cons_flags flags = cons_none;
941 op_pin_state pinstate = read_pin_state(env);
944 case op_pin_state_floats: flags |= cons_floats; break;
945 case op_pin_state_pinned: break;
947 panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
950 if(read_volatility(env) == volatility_is_volatile) flags |= cons_volatile;
951 if(read_align(env) == align_non_aligned) flags |= cons_unaligned;
956 static tarval *read_tv(io_env_t *env)
958 static char buf[128];
959 ir_mode *tvmode = read_mode(env);
960 read_str_to(env, buf, sizeof(buf));
961 return new_tarval_from_str(buf, strlen(buf), tvmode);
964 static ir_initializer_t *read_initializer(io_env_t *env)
967 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
971 case IR_INITIALIZER_CONST:
973 ir_node *irn = get_node_or_dummy(env, read_long(env));
974 return create_initializer_const(irn);
977 case IR_INITIALIZER_TARVAL:
978 return create_initializer_tarval(read_tv(env));
980 case IR_INITIALIZER_NULL:
981 return get_initializer_null();
983 case IR_INITIALIZER_COMPOUND:
985 unsigned i, n = (unsigned) read_long(env);
986 ir_initializer_t *ini = create_initializer_compound(n);
987 for(i = 0; i < n; i++)
989 ir_initializer_t *curini = read_initializer(env);
990 set_initializer_compound_value(ini, i, curini);
996 panic("Unknown initializer kind");
1001 /** Reads a type description and remembers it by its id. */
1002 static void import_type(io_env_t *env, keyword_t kwkind)
1007 long typenr = read_long(env);
1008 const char *tpop = read_str(env);
1009 const char *name = read_qstr_to(env, buf, sizeof(buf));
1010 unsigned size = (unsigned) read_long(env);
1011 unsigned align = (unsigned) read_long(env);
1012 ir_type_state state = read_type_state(env);
1013 ir_visibility vis = read_visibility(env);
1015 ident *id = new_id_from_str(name);
1017 const char *kindstr;
1019 if(kwkind == kw_frametype)
1021 if(symbol(tpop, tt_tpo) != tpo_class)
1023 printf("Frame type must be a class type in line %i:%i\n", env->line, env->col);
1028 type = new_type_frame(id);
1029 set_type_size_bytes(type, size);
1031 kindstr = "frametype";
1033 else if(kwkind == kw_valuetype)
1035 if(symbol(tpop, tt_tpo) != tpo_struct)
1037 printf("Value type must be a struct type in line %i:%i\n", env->line, env->col);
1042 type = new_type_value(id);
1043 set_type_size_bytes(type, size);
1045 kindstr = "valuetype";
1049 switch(symbol(tpop, tt_tpo))
1053 int ndims = (int) read_long(env);
1054 long elemtypenr = read_long(env);
1055 ir_type *elemtype = get_type(env, elemtypenr);
1057 type = new_type_array(id, ndims, elemtype);
1058 for(i = 0; i < ndims; i++)
1060 const char *str = read_str(env);
1061 if(strcmp(str, "unknown"))
1063 long lowerbound = strtol(str, NULL, 0);
1064 set_array_lower_bound_int(type, i, lowerbound);
1066 str = read_str(env);
1067 if(strcmp(str, "unknown"))
1069 long upperbound = strtol(str, NULL, 0);
1070 set_array_upper_bound_int(type, i, upperbound);
1073 set_type_size_bytes(type, size);
1078 type = new_type_class(id);
1079 set_type_size_bytes(type, size);
1084 unsigned callingconv = (unsigned) read_long(env);
1085 unsigned addprops = (unsigned) read_long(env);
1086 int nparams = (int) read_long(env);
1087 int nresults = (int) read_long(env);
1090 type = new_type_method(id, nparams, nresults);
1092 for(i = 0; i < nparams; i++)
1094 long typenr = read_long(env);
1095 ir_type *paramtype = get_type(env, typenr);
1097 set_method_param_type(type, i, paramtype);
1099 for(i = 0; i < nresults; i++)
1101 long typenr = read_long(env);
1102 ir_type *restype = get_type(env, typenr);
1104 set_method_res_type(type, i, restype);
1107 variaindex = (int) read_long(env);
1108 if(variaindex != -1)
1110 set_method_variadicity(type, variadicity_variadic);
1111 if(variaindex != nparams)
1112 set_method_first_variadic_param_index(type, variaindex);
1115 set_method_calling_convention(type, callingconv);
1116 set_method_additional_properties(type, addprops);
1122 ir_mode *mode = read_mode(env);
1123 ir_type *pointsto = get_type(env, read_long(env));
1124 type = new_type_pointer(id, pointsto, mode);
1130 ir_mode *mode = read_mode(env);
1131 type = new_type_primitive(id, mode);
1136 type = new_type_struct(id);
1137 set_type_size_bytes(type, size);
1141 type = new_type_union(id);
1142 set_type_size_bytes(type, size);
1146 return; // ignore unknown type
1149 if(typenr != 0) // ignore global type
1150 printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
1157 set_type_alignment_bytes(type, align);
1158 set_type_visibility(type, vis);
1160 if(state == layout_fixed)
1161 ARR_APP1(ir_type *, env->fixedtypes, type);
1163 set_id(env, typenr, type);
1164 printf("Insert %s %s %ld\n", kindstr, name, typenr);
1167 /** Reads an entity description and remembers it by its id. */
1168 static void import_entity(io_env_t *env)
1170 char buf[1024], buf2[1024];
1171 long entnr = read_long(env);
1172 const char *name = read_qstr_to(env, buf, sizeof(buf));
1173 const char *ld_name = read_qstr_to(env, buf2, sizeof(buf2));
1174 long typenr = read_long(env);
1175 long ownertypenr = read_long(env);
1177 ir_type *type = get_type(env, typenr);
1178 ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1179 ir_entity *entity = new_entity(ownertype, new_id_from_str(name), type);
1181 if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
1182 set_entity_offset (entity, (int) read_long(env));
1183 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1184 set_entity_allocation (entity, read_allocation(env));
1185 set_entity_visibility (entity, read_visibility(env));
1186 set_entity_variability(entity, read_variability(env));
1187 set_entity_peculiarity(entity, read_peculiarity(env));
1188 set_entity_volatility (entity, read_volatility(env));
1190 if(get_entity_variability(entity) != variability_uninitialized
1191 && get_entity_visibility(entity) != visibility_external_allocated)
1193 if(is_compound_entity(entity))
1195 if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
1197 set_entity_initializer(entity, read_initializer(env));
1201 int i, n = (int) read_long(env);
1202 for(i = 0; i < n; i++)
1204 ir_entity *member = get_entity(env, read_long(env));
1205 ir_node *irn = get_node_or_dummy(env, read_long(env));
1206 add_compound_ent_value(entity, irn, member);
1212 ir_node *irn = get_node_or_dummy(env, read_long(env));
1213 set_atomic_ent_value(entity, irn);
1217 set_id(env, entnr, entity);
1218 printf("Insert entity %s %ld\n", name, entnr);
1221 /** Parses the whole type graph. */
1222 static int parse_typegraph(io_env_t *env)
1225 lex_state_t oldstate;
1229 save_lex_state(env, &oldstate);
1231 current_ir_graph = get_const_code_irg();
1233 // parse all types first
1236 int isframetype = 0;
1238 kind = read_str(env);
1239 if(kind[0] == '}' && !kind[1]) break;
1241 keyword_t kwkind = (keyword_t) symbol(kind, tt_keyword);
1247 import_type(env, kwkind);
1257 restore_lex_state(env, &oldstate);
1261 kind = read_str(env);
1262 if(kind[0] == '}' && !kind[1]) break;
1264 switch(symbol(kind, tt_keyword))
1277 printf("Type graph element not supported yet: \"%s\"\n", kind);
1285 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1288 *nodename = read_str(env);
1289 if((*nodename)[0] == '}' && !(*nodename)[1]) return -1; // end-of-graph
1291 *nodenr = read_long(env);
1293 ARR_RESIZE(ir_node *, *preds, 0);
1296 for(numpreds = 0; !feof(env->file); numpreds++)
1299 ARR_APP1(long, *preds, read_long2(env, &endptr));
1300 if(*endptr == ']') break;
1305 /** Parses an IRG. */
1306 static int parse_graph(io_env_t *env, ir_graph *irg)
1308 long *preds = NEW_ARR_F(long, 16);
1309 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1310 int i, numpreds, ret = 1;
1312 const char *nodename;
1313 ir_node *node, *newnode;
1315 current_ir_graph = irg;
1321 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1322 if(numpreds == -1) break; // end-of-graph
1325 printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1330 ARR_RESIZE(ir_node *, prednodes, numpreds);
1331 for(i = 0; i < numpreds - 1; i++)
1332 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1334 node = get_node_or_null(env, nodenr);
1339 switch(symbol(nodename, tt_iro))
1343 ir_node *newendblock = get_node(env, preds[0]);
1344 newnode = get_irg_end(current_ir_graph);
1345 exchange(get_nodes_block(newnode), newendblock);
1346 for(i = 0; i < numpreds - 1; i++)
1347 add_irn_n(newnode, prednodes[i]);
1353 ir_node *newstartblock = get_node(env, preds[0]);
1354 newnode = get_irg_start(current_ir_graph);
1355 exchange(get_nodes_block(newnode), newstartblock);
1361 if(preds[0] != nodenr)
1363 printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1369 newnode = new_Block(numpreds - 1, prednodes);
1374 newnode = current_ir_graph->anchor;
1375 for(i = 0; i < numpreds - 1; i++)
1376 set_irn_n(newnode, i, prednodes[i]);
1377 set_irn_n(newnode, -1, get_node(env, preds[0]));
1382 long entnr = read_long(env);
1383 union symconst_symbol sym;
1384 sym.entity_p = get_entity(env, entnr);
1385 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1389 #include "gen_irio_import.inl"
1400 panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1404 exchange(node, newnode);
1405 /* Always update hash entry to avoid more uses of id nodes */
1406 set_id(env, nodenr, newnode);
1407 //printf("Insert %s %ld\n", nodename, nodenr);
1412 DEL_ARR_F(prednodes);
1417 /** Imports an previously exported textual representation of an (maybe partial) irp */
1418 void ir_import(const char *filename)
1420 int oldoptimize = get_optimize();
1421 firm_verification_t oldver = get_node_verification_mode();
1423 io_env_t *env = &ioenv;
1428 memset(env, 0, sizeof(*env));
1429 env->idset = new_set(id_cmp, 128);
1430 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1432 env->file = fopen(filename, "rt");
1440 do_node_verification(FIRM_VERIFICATION_OFF);
1444 const char *str = read_str(env);
1446 switch(symbol(str, tt_keyword))
1449 if(!parse_typegraph(env)) goto end;
1454 ir_entity *irgent = get_entity(env, read_long(env));
1456 ir_graph *irg = new_ir_graph(irgent, 0);
1457 set_irg_frame_type(irg, get_type(env, read_long(env)));
1458 valuetypeid = read_long(env);
1459 if(valuetypeid != -1)
1460 set_method_value_param_type(get_entity_type(irgent),
1461 get_type(env, valuetypeid));
1463 if(!parse_graph(env, irg)) goto end;
1468 if(!parse_graph(env, get_const_code_irg())) goto end;
1474 n = ARR_LEN(env->fixedtypes);
1475 for(i = 0; i < n; i++)
1476 set_type_state(env->fixedtypes[i], layout_fixed);
1478 DEL_ARR_F(env->fixedtypes);
1480 del_set(env->idset);
1482 irp_finalize_cons();
1484 do_node_verification(oldver);
1485 set_optimize(oldoptimize);