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
67 tt_cond_jmp_predicate,
81 typedef enum keyword_t
94 typedef struct symbol_t
96 const char *str; /**< The name of this symbol. */
97 typetag_t typetag; /**< The type tag of this symbol. */
98 unsigned code; /**< The value of this symbol. */
101 typedef struct id_entry
107 /** The symbol table, a set of symbol_t elements. */
112 * Calculate a hash value for a string.
114 static unsigned string_hash(const char *str, int len)
116 return str[0] * 27893 ^ str[len-1] * 81 ^ str[len >> 1];
120 * Compare two symbol table entries.
122 static int symbol_cmp(const void *elt, const void *key, size_t size)
125 const symbol_t *entry = (const symbol_t *) elt;
126 const symbol_t *keyentry = (const symbol_t *) key;
128 res = entry->typetag - keyentry->typetag;
130 return strcmp(entry->str, keyentry->str);
133 static int id_cmp(const void *elt, const void *key, size_t size)
135 const id_entry *entry = (const id_entry *) elt;
136 const id_entry *keyentry = (const id_entry *) key;
138 return entry->id - keyentry->id;
141 /** Initializes the symbol table. May be called more than once without problems. */
142 static void symtbl_init(void)
146 /* Only initialize once */
150 symtbl = new_set(symbol_cmp, 256);
152 #define INSERT(s, tt, cod) \
154 key.typetag = (tt); \
156 set_insert(symtbl, &key, sizeof(key), string_hash(s, sizeof(s)-1) + tt * 17)
158 #define INSERTENUM(tt, e) INSERT(#e, tt, e)
159 #define INSERTKEYWORD(k) INSERT(#k, tt_keyword, kw_##k)
161 INSERT("array", tt_tpo, tpo_array);
162 INSERT("class", tt_tpo, tpo_class);
163 INSERT("method", tt_tpo, tpo_method);
164 INSERT("pointer", tt_tpo, tpo_pointer);
165 INSERT("primitive", tt_tpo, tpo_primitive);
166 INSERT("struct", tt_tpo, tpo_struct);
167 INSERT("union", tt_tpo, tpo_union);
168 INSERT("Unknown", tt_tpo, tpo_unknown);
170 INSERTKEYWORD(constirg);
171 INSERTKEYWORD(entity);
172 INSERTKEYWORD(frametype);
175 INSERTKEYWORD(modes);
176 INSERTKEYWORD(valuetype);
178 INSERTKEYWORD(typegraph);
180 #include "gen_irio_lex.inl"
182 INSERTENUM(tt_align, align_non_aligned);
183 INSERTENUM(tt_align, align_is_aligned);
185 INSERTENUM(tt_allocation, allocation_automatic);
186 INSERTENUM(tt_allocation, allocation_parameter);
187 INSERTENUM(tt_allocation, allocation_dynamic);
188 INSERTENUM(tt_allocation, allocation_static);
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);
205 INSERTENUM(tt_cond_kind, dense);
206 INSERTENUM(tt_cond_kind, fragmentary);
208 INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_NONE);
209 INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_TRUE);
210 INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_FALSE);
212 INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
213 INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
214 INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
215 INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
217 INSERTENUM(tt_mode_arithmetic, irma_uninitialized);
218 INSERTENUM(tt_mode_arithmetic, irma_none);
219 INSERTENUM(tt_mode_arithmetic, irma_twos_complement);
220 INSERTENUM(tt_mode_arithmetic, irma_ones_complement);
221 INSERTENUM(tt_mode_arithmetic, irma_int_BCD);
222 INSERTENUM(tt_mode_arithmetic, irma_ieee754);
223 INSERTENUM(tt_mode_arithmetic, irma_float_BCD);
225 INSERTENUM(tt_peculiarity, peculiarity_description);
226 INSERTENUM(tt_peculiarity, peculiarity_inherited);
227 INSERTENUM(tt_peculiarity, peculiarity_existent);
229 INSERTENUM(tt_pin_state, op_pin_state_floats);
230 INSERTENUM(tt_pin_state, op_pin_state_pinned);
231 INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
232 INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
234 INSERTENUM(tt_type_state, layout_undefined);
235 INSERTENUM(tt_type_state, layout_fixed);
237 INSERTENUM(tt_variability, variability_uninitialized);
238 INSERTENUM(tt_variability, variability_initialized);
239 INSERTENUM(tt_variability, variability_part_constant);
240 INSERTENUM(tt_variability, variability_constant);
242 INSERTENUM(tt_visibility, visibility_local);
243 INSERTENUM(tt_visibility, visibility_external_visible);
244 INSERTENUM(tt_visibility, visibility_external_allocated);
246 INSERTENUM(tt_volatility, volatility_non_volatile);
247 INSERTENUM(tt_volatility, volatility_is_volatile);
254 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
255 static unsigned symbol(const char *str, typetag_t typetag)
257 symbol_t key, *entry;
260 key.typetag = typetag;
262 entry = set_find(symtbl, &key, sizeof(key), string_hash(str, strlen(str)) + typetag * 17);
263 return entry ? entry->code : SYMERROR;
266 static void *get_id(io_env_t *env, long id)
268 id_entry key, *entry;
271 entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
272 return entry ? entry->elem : NULL;
275 static void set_id(io_env_t *env, long id, void *elem)
280 set_insert(env->idset, &key, sizeof(key), (unsigned) id);
283 static void write_mode(io_env_t *env, ir_mode *mode)
285 fputs(get_mode_name(mode), env->file);
286 fputc(' ', env->file);
289 static void write_tarval(io_env_t *env, tarval *tv)
292 write_mode(env, get_tarval_mode(tv));
293 tarval_snprintf(buf, sizeof(buf), tv);
294 fputs(buf, env->file);
295 fputc(' ', env->file);
298 static void write_align(io_env_t *env, ir_node *irn)
302 if(is_Load(irn)) align = get_Load_align(irn);
303 else if(is_Store(irn)) align = get_Store_align(irn);
304 else assert(0 && "Invalid optype for write_align");
306 fputs(get_align_name(align), env->file);
307 fputc(' ', env->file);
310 static void write_builtin_kind(io_env_t *env, ir_node *irn)
312 fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
313 fputc(' ', env->file);
316 static void write_cond_kind(io_env_t *env, ir_node *irn)
318 fputs(get_cond_kind_name(get_Cond_kind(irn)), env->file);
319 fputc(' ', env->file);
322 static void write_cond_jmp_predicate(io_env_t *env, ir_node *irn)
324 fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(irn)), env->file);
325 fputc(' ', env->file);
328 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
331 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
333 fputs(get_initializer_kind_name(ini_kind), f);
338 case IR_INITIALIZER_CONST:
339 fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
342 case IR_INITIALIZER_TARVAL:
343 write_tarval(env, get_initializer_tarval_value(ini));
346 case IR_INITIALIZER_NULL:
349 case IR_INITIALIZER_COMPOUND:
351 unsigned i, n = get_initializer_compound_n_entries(ini);
352 fprintf(f, "%d ", n);
353 for(i = 0; i < n; i++)
354 write_initializer(env, get_initializer_compound_value(ini, i));
359 panic("Unknown initializer kind");
363 static void write_pin_state(io_env_t *env, ir_node *irn)
365 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
366 fputc(' ', env->file);
369 static void write_volatility(io_env_t *env, ir_node *irn)
373 if(is_Load(irn)) vol = get_Load_volatility(irn);
374 else if(is_Store(irn)) vol = get_Store_volatility(irn);
375 else assert(0 && "Invalid optype for write_volatility");
377 fputs(get_volatility_name(vol), env->file);
378 fputc(' ', env->file);
381 static void export_type_common(io_env_t *env, ir_type *tp)
383 fprintf(env->file, "\t%s %ld %s \"%s\" %u %u %s %s ",
384 is_frame_type(tp) ? "frametype" : is_value_param_type(tp) ? "valuetype" : "type",
386 get_type_tpop_name(tp),
388 get_type_size_bytes(tp),
389 get_type_alignment_bytes(tp),
390 get_type_state_name(get_type_state(tp)),
391 get_visibility_name(get_type_visibility(tp)));
394 static void export_type_pre(io_env_t *env, ir_type *tp)
399 // skip types to be handled by post walker
400 switch(get_type_tpop_code(tp))
408 export_type_common(env, tp);
410 switch(get_type_tpop_code(tp))
413 /* TODO: inheritance stuff not supported yet */
414 printf("Inheritance of classes not supported yet!\n");
418 write_mode(env, get_type_mode(tp));
431 printf("export_type_pre: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
437 static void export_type_post(io_env_t *env, ir_type *tp)
442 // skip types already handled by pre walker
443 switch(get_type_tpop_code(tp))
453 export_type_common(env, tp);
455 switch(get_type_tpop_code(tp))
459 int n = get_array_n_dimensions(tp);
460 fprintf(f, "%i %ld ", n, get_type_nr(get_array_element_type(tp)));
461 for(i = 0; i < n; i++)
463 ir_node *lower = get_array_lower_bound(tp, i);
464 ir_node *upper = get_array_upper_bound(tp, i);
466 if(is_Const(lower)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
467 else panic("Lower array bound is not constant");
469 if(is_Const(upper)) fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
470 else if(is_Unknown(upper)) fputs("unknown ", f);
471 else panic("Upper array bound is not constant");
478 int nparams = get_method_n_params(tp);
479 int nresults = get_method_n_ress(tp);
480 fprintf(f, "0x%X 0x%X %i %i ", get_method_calling_convention(tp),
481 get_method_additional_properties(tp), nparams, nresults);
482 for(i = 0; i < nparams; i++)
483 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
484 for(i = 0; i < nresults; i++)
485 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
486 fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
491 write_mode(env, get_type_mode(tp));
492 fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
496 printf("export_type: Unknown type code \"%s\".\n", get_type_tpop_name(tp));
502 static void export_entity(io_env_t *env, ir_entity *ent)
504 ir_type *owner = get_entity_owner(ent);
505 fprintf(env->file, "\tentity %ld \"%s\" \"%s\" %ld %ld %d %u %s %s %s %s %s ",
507 get_entity_name(ent),
508 ent->ld_name ? get_id_str(ent->ld_name) : "",
509 get_type_nr(get_entity_type(ent)),
511 get_entity_offset(ent),
512 (unsigned) get_entity_offset_bits_remainder(ent),
513 get_allocation_name(get_entity_allocation(ent)),
514 get_visibility_name(get_entity_visibility(ent)),
515 get_variability_name(get_entity_variability(ent)),
516 get_peculiarity_name(get_entity_peculiarity(ent)),
517 get_volatility_name(get_entity_volatility(ent)));
519 // TODO: inheritance stuff for class entities not supported yet
520 if(is_Class_type(owner) && owner != get_glob_type())
521 printf("Inheritance of class entities not supported yet!\n");
523 if(get_entity_variability(ent) != variability_uninitialized
524 && get_entity_visibility(ent) != visibility_external_allocated)
526 if(is_compound_entity(ent))
528 if(has_entity_initializer(ent))
530 fputs("initializer ", env->file);
531 write_initializer(env, get_entity_initializer(ent));
535 int i, n = get_compound_ent_n_values(ent);
536 fputs("noninitializer ", env->file);
537 fprintf(env->file, "%d ", n);
538 for(i = 0; i < n; i++)
540 ir_entity *member = get_compound_ent_value_member(ent, i);
541 ir_node *irn = get_compound_ent_value(ent, i);
542 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
548 ir_node *irn = get_atomic_ent_value(ent);
549 fprintf(env->file, "%ld ", get_irn_node_nr(irn));
553 fputc('\n', env->file);
556 static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
558 io_env_t *env = (io_env_t *) ctx;
559 if(get_kind(tore.typ) == k_type)
560 export_type_pre(env, tore.typ);
563 static void export_type_or_ent_post(type_or_ent tore, void *ctx)
565 io_env_t *env = (io_env_t *) ctx;
567 switch(get_kind(tore.ent))
570 export_entity(env, tore.ent);
574 export_type_post(env, tore.typ);
578 printf("export_type_or_ent_post: Unknown type or entity.\n");
583 static void export_node(ir_node *irn, void *ctx)
585 io_env_t *env = (io_env_t *) ctx;
587 unsigned opcode = get_irn_opcode(irn);
589 if(env->ignoreblocks && opcode == iro_Block)
592 n = get_irn_arity(irn);
594 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
596 for(i = -1; i < n; i++)
598 ir_node *pred = get_irn_n(irn, i);
600 /* Anchor node may have NULL predecessors */
601 assert(is_Anchor(irn));
602 fputs("-1 ", env->file);
604 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
608 fprintf(env->file, "] { ");
612 #include "gen_irio_export.inl"
614 fputs("}\n", env->file);
617 static void export_modes(io_env_t *env)
619 int i, n_modes = get_irp_n_modes();
621 fputs("modes {\n", env->file);
623 for(i = 0; i < n_modes; i++)
625 ir_mode *mode = get_irp_mode(i);
626 switch(get_mode_sort(mode))
629 case irms_control_flow:
631 case irms_internal_boolean:
632 // skip "internal" modes, which may not be user defined
636 fprintf(env->file, "\tmode \"%s\" 0x%X %d %d %s %d %d ", get_mode_name(mode),
637 get_mode_sort(mode), get_mode_size_bits(mode), get_mode_sign(mode),
638 get_mode_arithmetic_name(get_mode_arithmetic(mode)), get_mode_modulo_shift(mode),
639 get_mode_n_vector_elems(mode));
640 if(mode_is_reference(mode))
642 write_mode(env, get_reference_mode_signed_eq(mode));
643 write_mode(env, get_reference_mode_unsigned_eq(mode));
645 fputc('\n', env->file);
648 fputs("}\n\n", env->file);
651 /** Exports the whole irp to the given file in a textual form. */
652 void ir_export(const char *filename)
655 int i, n_irgs = get_irp_n_irgs();
657 env.file = fopen(filename, "wt");
666 fputs("typegraph {\n", env.file);
668 type_walk_plus_frames(export_type_or_ent_pre, export_type_or_ent_post, &env);
669 /* TODO: Visit frame types and "types for value params"? */
671 for(i = 0; i < n_irgs; i++)
673 ir_graph *irg = get_irp_irg(i);
674 ir_type *valuetype = get_irg_value_param_type(irg);
676 fprintf(env.file, "}\n\nirg %ld %ld %ld {\n", get_entity_nr(get_irg_entity(irg)),
677 get_type_nr(get_irg_frame_type(irg)),
678 valuetype == NULL ? -1 : get_type_nr(valuetype));
680 env.ignoreblocks = 0;
681 irg_block_walk_graph(irg, NULL, export_node, &env);
683 env.ignoreblocks = 1;
684 irg_walk_anchors(irg, NULL, export_node, &env);
687 fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
689 walk_const_code(NULL, export_node, &env);
690 fputs("}\n", env.file);
695 /** Exports the given irg to the given file. */
696 void ir_export_irg(ir_graph *irg, const char *filename)
700 env.file = fopen(filename, "wt");
709 fputs("typegraph {\n", env.file);
711 type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
713 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
715 env.ignoreblocks = 0;
716 irg_block_walk_graph(irg, NULL, export_node, &env);
718 env.ignoreblocks = 1;
719 irg_walk_anchors(irg, NULL, export_node, &env);
721 /* TODO: Only output needed constants */
722 fputs("}\n\nconstirg {\n", env.file);
723 walk_const_code(NULL, export_node, &env);
724 fputs("}\n", env.file);
729 static void save_lex_state(io_env_t *env, lex_state_t *state)
731 state->offs = ftell(env->file);
732 state->line = env->line;
733 state->col = env->col;
736 static void restore_lex_state(io_env_t *env, lex_state_t *state)
738 fseek(env->file, state->offs, SEEK_SET);
739 env->line = state->line;
740 env->col = state->col;
743 static int read_c(io_env_t *env)
745 int ch = fgetc(env->file);
764 /** Returns the first non-whitespace character or EOF. **/
765 static int skip_ws(io_env_t *env)
769 int ch = read_c(env);
784 static void skip_to(io_env_t *env, char to_ch)
791 while(ch != to_ch && ch != EOF);
794 static int expect_char(io_env_t *env, char ch)
796 int curch = skip_ws(env);
799 printf("Unexpected char '%c', expected '%c' in line %i:%i\n", curch, ch, env->line, env->col);
805 #define EXPECT(c) if(expect_char(env, (c))) {} else return 0
806 #define EXPECT_OR_EXIT(c) if(expect_char(env, (c))) {} else exit(1)
808 inline static const char *read_str_to(io_env_t *env, char *buf, size_t bufsize)
811 for(i = 0; i < bufsize - 1; i++)
813 int ch = read_c(env);
823 i--; // skip whitespace
836 static const char *read_str(io_env_t *env)
838 static char buf[1024];
839 return read_str_to(env, buf, sizeof(buf));
842 static const char *read_qstr_to(io_env_t *env, char *buf, size_t bufsize)
845 EXPECT_OR_EXIT('\"');
846 for(i = 0; i < bufsize - 1; i++)
848 int ch = read_c(env);
851 printf("Unexpected end of quoted string!\n");
854 if(ch == '\"') break;
860 printf("Quoted string too long!\n");
867 static const char *read_qstr(io_env_t *env)
869 static char buf[1024];
870 return read_qstr_to(env, buf, sizeof(buf));
873 static long read_long2(io_env_t *env, char **endptr)
875 static char buf[1024];
876 return strtol(read_str_to(env, buf, sizeof(buf)), endptr, 0);
879 static long read_long(io_env_t *env)
881 return read_long2(env, NULL);
884 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
886 ir_node *node = (ir_node *) get_id(env, nodenr);
887 if(node && node->kind != k_ir_node)
889 panic("Irn ID %ld collides with something else in line %i:%i\n", nodenr, env->line, env->col);
894 static ir_node *get_node(io_env_t *env, long nodenr)
896 ir_node *node = get_node_or_null(env, nodenr);
898 panic("Unknown node: %ld in line %i:%i\n", nodenr, env->line, env->col);
903 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
905 ir_node *node = get_node_or_null(env, nodenr);
908 node = new_Dummy(mode_X);
909 set_id(env, nodenr, node);
914 static ir_type *get_type(io_env_t *env, long typenr)
916 ir_type *type = (ir_type *) get_id(env, typenr);
919 panic("Unknown type: %ld in line %i:%i\n", typenr, env->line, env->col);
921 else if(type->kind != k_type)
923 panic("Type ID %ld collides with something else in line %i:%i\n", typenr, env->line, env->col);
928 static ir_type *read_type(io_env_t *env)
930 return get_type(env, read_long(env));
933 static ir_entity *get_entity(io_env_t *env, long entnr)
935 ir_entity *entity = (ir_entity *) get_id(env, entnr);
938 printf("Unknown entity: %ld in line %i:%i\n", entnr, env->line, env->col);
941 else if(entity->kind != k_entity)
943 panic("Entity ID %ld collides with something else in line %i:%i\n", entnr, env->line, env->col);
948 static ir_entity *read_entity(io_env_t *env)
950 return get_entity(env, read_long(env));
953 static ir_mode *read_mode(io_env_t *env)
955 static char buf[128];
958 read_str_to(env, buf, sizeof(buf));
960 n = get_irp_n_modes();
961 for(i = 0; i < n; i++)
963 ir_mode *mode = get_irp_mode(i);
964 if(!strcmp(buf, get_mode_name(mode)))
968 printf("Unknown mode \"%s\" in line %i:%i\n", buf, env->line, env->col);
972 static const char *get_typetag_name(typetag_t typetag)
976 case tt_align: return "align";
977 case tt_allocation: return "allocation";
978 case tt_builtin: return "builtin kind";
979 case tt_initializer: return "initializer kind";
980 case tt_iro: return "opcode";
981 case tt_peculiarity: return "peculiarity";
982 case tt_pin_state: return "pin state";
983 case tt_tpo: return "type";
984 case tt_type_state: return "type state";
985 case tt_variability: return "variability";
986 case tt_visibility: return "visibility";
987 case tt_volatility: return "volatility";
988 default: return "<UNKNOWN>";
993 * Read and decode an enum constant.
995 static unsigned read_enum(io_env_t *env, typetag_t typetag)
997 static char buf[128];
998 unsigned code = symbol(read_str_to(env, buf, sizeof(buf)), typetag);
1002 printf("Invalid %s: \"%s\" in %i:%i\n", get_typetag_name(typetag), buf, env->line, env->col);
1006 #define read_align(env) ((ir_align) read_enum(env, tt_align))
1007 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
1008 #define read_builtin_kind(env) ((ir_builtin_kind) read_enum(env, tt_builtin))
1009 #define read_cond_kind(env) ((cond_kind) read_enum(env, tt_cond_kind))
1010 #define read_cond_jmp_predicate(env) ((cond_jmp_predicate) read_enum(env, tt_cond_jmp_predicate))
1011 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
1012 #define read_mode_arithmetic(env) ((ir_mode_arithmetic) read_enum(env, tt_mode_arithmetic))
1013 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
1014 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
1015 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
1016 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
1017 #define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
1018 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
1020 static ir_cons_flags get_cons_flags(io_env_t *env)
1022 ir_cons_flags flags = cons_none;
1024 op_pin_state pinstate = read_pin_state(env);
1027 case op_pin_state_floats: flags |= cons_floats; break;
1028 case op_pin_state_pinned: break;
1030 panic("Error in %i:%i: Invalid pinstate: %s", env->line, env->col, get_op_pin_state_name(pinstate));
1033 if(read_volatility(env) == volatility_is_volatile) flags |= cons_volatile;
1034 if(read_align(env) == align_non_aligned) flags |= cons_unaligned;
1039 static tarval *read_tv(io_env_t *env)
1041 static char buf[128];
1042 ir_mode *tvmode = read_mode(env);
1043 read_str_to(env, buf, sizeof(buf));
1044 return new_tarval_from_str(buf, strlen(buf), tvmode);
1047 static ir_initializer_t *read_initializer(io_env_t *env)
1049 FILE *f = env->file;
1050 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
1054 case IR_INITIALIZER_CONST:
1056 ir_node *irn = get_node_or_dummy(env, read_long(env));
1057 return create_initializer_const(irn);
1060 case IR_INITIALIZER_TARVAL:
1061 return create_initializer_tarval(read_tv(env));
1063 case IR_INITIALIZER_NULL:
1064 return get_initializer_null();
1066 case IR_INITIALIZER_COMPOUND:
1068 unsigned i, n = (unsigned) read_long(env);
1069 ir_initializer_t *ini = create_initializer_compound(n);
1070 for(i = 0; i < n; i++)
1072 ir_initializer_t *curini = read_initializer(env);
1073 set_initializer_compound_value(ini, i, curini);
1079 panic("Unknown initializer kind");
1084 /** Reads a type description and remembers it by its id. */
1085 static void import_type(io_env_t *env, keyword_t kwkind)
1089 long typenr = read_long(env);
1090 const char *tpop = read_str(env);
1091 const char *name = read_qstr(env);
1092 unsigned size = (unsigned) read_long(env);
1093 unsigned align = (unsigned) read_long(env);
1094 ir_type_state state = read_type_state(env);
1095 ir_visibility vis = read_visibility(env);
1097 ident *id = new_id_from_str(name);
1099 const char *kindstr;
1101 if(kwkind == kw_frametype)
1103 if(symbol(tpop, tt_tpo) != tpo_class)
1105 printf("Frame type must be a class type in line %i:%i\n", env->line, env->col);
1110 type = new_type_frame(id);
1111 set_type_size_bytes(type, size);
1113 kindstr = "frametype";
1115 else if(kwkind == kw_valuetype)
1117 if(symbol(tpop, tt_tpo) != tpo_struct)
1119 printf("Value type must be a struct type in line %i:%i\n", env->line, env->col);
1124 type = new_type_value(id);
1125 set_type_size_bytes(type, size);
1127 kindstr = "valuetype";
1131 switch(symbol(tpop, tt_tpo))
1135 int ndims = (int) read_long(env);
1136 long elemtypenr = read_long(env);
1137 ir_type *elemtype = get_type(env, elemtypenr);
1139 type = new_type_array(id, ndims, elemtype);
1140 for(i = 0; i < ndims; i++)
1142 const char *str = read_str(env);
1143 if(strcmp(str, "unknown"))
1145 long lowerbound = strtol(str, NULL, 0);
1146 set_array_lower_bound_int(type, i, lowerbound);
1148 str = read_str(env);
1149 if(strcmp(str, "unknown"))
1151 long upperbound = strtol(str, NULL, 0);
1152 set_array_upper_bound_int(type, i, upperbound);
1155 set_type_size_bytes(type, size);
1160 type = new_type_class(id);
1161 set_type_size_bytes(type, size);
1166 unsigned callingconv = (unsigned) read_long(env);
1167 unsigned addprops = (unsigned) read_long(env);
1168 int nparams = (int) read_long(env);
1169 int nresults = (int) read_long(env);
1172 type = new_type_method(id, nparams, nresults);
1174 for(i = 0; i < nparams; i++)
1176 long typenr = read_long(env);
1177 ir_type *paramtype = get_type(env, typenr);
1179 set_method_param_type(type, i, paramtype);
1181 for(i = 0; i < nresults; i++)
1183 long typenr = read_long(env);
1184 ir_type *restype = get_type(env, typenr);
1186 set_method_res_type(type, i, restype);
1189 variaindex = (int) read_long(env);
1190 if(variaindex != -1)
1192 set_method_variadicity(type, variadicity_variadic);
1193 if(variaindex != nparams)
1194 set_method_first_variadic_param_index(type, variaindex);
1197 set_method_calling_convention(type, callingconv);
1198 set_method_additional_properties(type, addprops);
1204 ir_mode *mode = read_mode(env);
1205 ir_type *pointsto = get_type(env, read_long(env));
1206 type = new_type_pointer(id, pointsto, mode);
1212 ir_mode *mode = read_mode(env);
1213 type = new_type_primitive(id, mode);
1218 type = new_type_struct(id);
1219 set_type_size_bytes(type, size);
1223 type = new_type_union(id);
1224 set_type_size_bytes(type, size);
1228 return; // ignore unknown type
1231 if(typenr != 0) // ignore global type
1232 printf("Unknown type kind: \"%s\" in line %i:%i\n", tpop, env->line, env->col);
1239 set_type_alignment_bytes(type, align);
1240 set_type_visibility(type, vis);
1242 if(state == layout_fixed)
1243 ARR_APP1(ir_type *, env->fixedtypes, type);
1245 set_id(env, typenr, type);
1246 printf("Insert %s %s %ld\n", kindstr, name, typenr);
1249 /** Reads an entity description and remembers it by its id. */
1250 static void import_entity(io_env_t *env)
1252 char buf[1024], buf2[1024];
1253 long entnr = read_long(env);
1254 const char *name = read_qstr_to(env, buf, sizeof(buf));
1255 const char *ld_name = read_qstr_to(env, buf2, sizeof(buf2));
1256 long typenr = read_long(env);
1257 long ownertypenr = read_long(env);
1259 ir_type *type = get_type(env, typenr);
1260 ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1261 ir_entity *entity = new_entity(ownertype, new_id_from_str(name), type);
1263 if(*ld_name) set_entity_ld_ident(entity, new_id_from_str(ld_name));
1264 set_entity_offset (entity, (int) read_long(env));
1265 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1266 set_entity_allocation (entity, read_allocation(env));
1267 set_entity_visibility (entity, read_visibility(env));
1268 set_entity_variability(entity, read_variability(env));
1269 set_entity_peculiarity(entity, read_peculiarity(env));
1270 set_entity_volatility (entity, read_volatility(env));
1272 if(get_entity_variability(entity) != variability_uninitialized
1273 && get_entity_visibility(entity) != visibility_external_allocated)
1275 if(is_compound_entity(entity))
1277 if(!strcmp(read_str_to(env, buf2, sizeof(buf2)), "initializer"))
1279 set_entity_initializer(entity, read_initializer(env));
1283 int i, n = (int) read_long(env);
1284 for(i = 0; i < n; i++)
1286 ir_entity *member = get_entity(env, read_long(env));
1287 ir_node *irn = get_node_or_dummy(env, read_long(env));
1288 add_compound_ent_value(entity, irn, member);
1294 ir_node *irn = get_node_or_dummy(env, read_long(env));
1295 set_atomic_ent_value(entity, irn);
1299 set_id(env, entnr, entity);
1300 printf("Insert entity %s %ld\n", name, entnr);
1303 /** Parses the whole type graph. */
1304 static int parse_typegraph(io_env_t *env)
1308 lex_state_t oldstate;
1312 save_lex_state(env, &oldstate);
1314 current_ir_graph = get_const_code_irg();
1316 // parse all types first
1319 kind = read_str(env);
1320 if(kind[0] == '}' && !kind[1]) break;
1322 kwkind = (keyword_t) symbol(kind, tt_keyword);
1328 import_type(env, kwkind);
1338 restore_lex_state(env, &oldstate);
1342 kind = read_str(env);
1343 if(kind[0] == '}' && !kind[1]) break;
1345 switch(symbol(kind, tt_keyword))
1358 printf("Type graph element not supported yet: \"%s\"\n", kind);
1366 static int read_node_header(io_env_t *env, long *nodenr, long **preds, const char **nodename)
1369 *nodename = read_str(env);
1370 if((*nodename)[0] == '}' && !(*nodename)[1]) return -1; // end-of-graph
1372 *nodenr = read_long(env);
1374 ARR_RESIZE(ir_node *, *preds, 0);
1377 for(numpreds = 0; !feof(env->file); numpreds++)
1380 ARR_APP1(long, *preds, read_long2(env, &endptr));
1381 if(*endptr == ']') break;
1386 /** Parses an IRG. */
1387 static int parse_graph(io_env_t *env, ir_graph *irg)
1389 long *preds = NEW_ARR_F(long, 16);
1390 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1391 int i, numpreds, ret = 1;
1393 const char *nodename;
1394 ir_node *node, *newnode;
1396 current_ir_graph = irg;
1402 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1403 if(numpreds == -1) break; // end-of-graph
1406 printf("Node %s %ld is missing predecessors!", nodename, nodenr);
1411 ARR_RESIZE(ir_node *, prednodes, numpreds);
1412 for(i = 0; i < numpreds - 1; i++)
1413 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1415 node = get_node_or_null(env, nodenr);
1420 switch(symbol(nodename, tt_iro))
1424 ir_node *newendblock = get_node(env, preds[0]);
1425 newnode = get_irg_end(current_ir_graph);
1426 exchange(get_nodes_block(newnode), newendblock);
1427 for(i = 0; i < numpreds - 1; i++)
1428 add_irn_n(newnode, prednodes[i]);
1434 ir_node *newstartblock = get_node(env, preds[0]);
1435 newnode = get_irg_start(current_ir_graph);
1436 exchange(get_nodes_block(newnode), newstartblock);
1442 if(preds[0] != nodenr)
1444 printf("Invalid block: preds[0] != nodenr (%ld != %ld)\n",
1450 newnode = new_Block(numpreds - 1, prednodes);
1455 newnode = current_ir_graph->anchor;
1456 for(i = 0; i < numpreds - 1; i++)
1457 set_irn_n(newnode, i, prednodes[i]);
1458 set_irn_n(newnode, -1, get_node(env, preds[0]));
1463 long entnr = read_long(env);
1464 union symconst_symbol sym;
1465 sym.entity_p = get_entity(env, entnr);
1466 newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
1470 #include "gen_irio_import.inl"
1481 panic("Node type not supported yet: %s in line %i:%i\n", nodename, env->line, env->col);
1485 exchange(node, newnode);
1486 /* Always update hash entry to avoid more uses of id nodes */
1487 set_id(env, nodenr, newnode);
1488 //printf("Insert %s %ld\n", nodename, nodenr);
1493 DEL_ARR_F(prednodes);
1498 static int parse_modes(io_env_t *env)
1507 kind = read_str(env);
1508 if(kind[0] == '}' && !kind[1]) break;
1510 kwkind = (keyword_t) symbol(kind, tt_keyword);
1515 const char *name = read_qstr(env);
1516 ir_mode_sort sort = (ir_mode_sort) read_long(env);
1517 int size = read_long(env);
1518 int sign = read_long(env);
1519 ir_mode_arithmetic arith = read_mode_arithmetic(env);
1520 unsigned modulo_shift = read_long(env);
1521 int vector_elems = read_long(env);
1523 ir_mode *mode = new_ir_mode(name, sort, size, sign, arith, modulo_shift);
1525 if(mode_is_reference(mode))
1527 set_reference_mode_signed_eq(mode, read_mode(env));
1528 set_reference_mode_unsigned_eq(mode, read_mode(env));
1540 /** Imports an previously exported textual representation of an (maybe partial) irp */
1541 void ir_import(const char *filename)
1543 int oldoptimize = get_optimize();
1544 firm_verification_t oldver = get_node_verification_mode();
1546 io_env_t *env = &ioenv;
1551 memset(env, 0, sizeof(*env));
1552 env->idset = new_set(id_cmp, 128);
1553 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1555 env->file = fopen(filename, "rt");
1563 do_node_verification(FIRM_VERIFICATION_OFF);
1567 const char *str = read_str(env);
1569 switch(symbol(str, tt_keyword))
1572 if(!parse_modes(env)) goto end;
1576 if(!parse_typegraph(env)) goto end;
1581 ir_entity *irgent = get_entity(env, read_long(env));
1583 ir_graph *irg = new_ir_graph(irgent, 0);
1584 set_irg_frame_type(irg, get_type(env, read_long(env)));
1585 valuetypeid = read_long(env);
1586 if(valuetypeid != -1)
1587 set_method_value_param_type(get_entity_type(irgent),
1588 get_type(env, valuetypeid));
1590 if(!parse_graph(env, irg)) goto end;
1596 ir_graph *constirg = get_const_code_irg();
1597 long bodyblockid = read_long(env);
1598 set_id(env, bodyblockid, constirg->current_block);
1599 if(!parse_graph(env, constirg)) goto end;
1606 n = ARR_LEN(env->fixedtypes);
1607 for(i = 0; i < n; i++)
1608 set_type_state(env->fixedtypes[i], layout_fixed);
1610 DEL_ARR_F(env->fixedtypes);
1612 del_set(env->idset);
1614 irp_finalize_cons();
1616 do_node_verification(oldver);
1617 set_optimize(oldoptimize);