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
37 #include "irgraph_t.h"
49 #define SYMERROR ((unsigned) ~0)
51 typedef struct io_env_t
53 int c; /**< currently read char */
55 set *idset; /**< id_entry set, which maps from file ids to new Firm elements */
57 const char *inputname;
64 typedef enum typetag_t
69 tt_cond_jmp_predicate,
84 typedef enum keyword_t
97 typedef struct symbol_t
99 const char *str; /**< The name of this symbol. */
100 typetag_t typetag; /**< The type tag of this symbol. */
101 unsigned code; /**< The value of this symbol. */
104 typedef struct id_entry
110 /** The symbol table, a set of symbol_t elements. */
114 * Compare two symbol table entries.
116 static int symbol_cmp(const void *elt, const void *key, size_t size)
119 const symbol_t *entry = (const symbol_t *) elt;
120 const symbol_t *keyentry = (const symbol_t *) key;
122 res = entry->typetag - keyentry->typetag;
124 return strcmp(entry->str, keyentry->str);
127 static int id_cmp(const void *elt, const void *key, size_t size)
129 const id_entry *entry = (const id_entry *) elt;
130 const id_entry *keyentry = (const id_entry *) key;
132 return entry->id - keyentry->id;
135 static void __attribute__((format(printf, 2, 3)))
136 parse_error(io_env_t *env, const char *fmt, ...)
139 int line = env->line;
141 /* workaround read_c "feature" that a '\n' triggers the line++
142 * instead of the character after the '\n' */
143 if (env->c == '\n') {
147 fprintf(stderr, "%s:%d: error ", env->inputname, line);
150 vfprintf(stderr, fmt, ap);
154 /** Initializes the symbol table. May be called more than once without problems. */
155 static void symtbl_init(void)
159 /* Only initialize once */
163 symtbl = new_set(symbol_cmp, 256);
165 #define INSERT(tt, s, cod) \
167 key.typetag = (tt); \
169 set_insert(symtbl, &key, sizeof(key), firm_fnv_hash_str(s) + tt * 17)
171 #define INSERTENUM(tt, e) INSERT(tt, #e, e)
172 #define INSERTKEYWORD(k) INSERT(tt_keyword, #k, kw_##k)
174 INSERT(tt_tpo, "array", tpo_array);
175 INSERT(tt_tpo, "class", tpo_class);
176 INSERT(tt_tpo, "method", tpo_method);
177 INSERT(tt_tpo, "pointer", tpo_pointer);
178 INSERT(tt_tpo, "primitive", tpo_primitive);
179 INSERT(tt_tpo, "struct", tpo_struct);
180 INSERT(tt_tpo, "union", tpo_union);
181 INSERT(tt_tpo, "Unknown", tpo_unknown);
183 INSERT(tt_mode_sort, "auxiliary", irms_auxiliary);
184 INSERT(tt_mode_sort, "control_flow", irms_control_flow);
185 INSERT(tt_mode_sort, "memory", irms_memory);
186 INSERT(tt_mode_sort, "internal_boolean", irms_internal_boolean);
187 INSERT(tt_mode_sort, "reference", irms_reference);
188 INSERT(tt_mode_sort, "int_number", irms_int_number);
189 INSERT(tt_mode_sort, "float_number", irms_float_number);
191 INSERT(tt_segment, "global", IR_SEGMENT_GLOBAL);
192 INSERT(tt_segment, "thread_local", IR_SEGMENT_THREAD_LOCAL);
193 INSERT(tt_segment, "constructors", IR_SEGMENT_CONSTRUCTORS);
194 INSERT(tt_segment, "destructors", IR_SEGMENT_DESTRUCTORS);
196 INSERT(tt_linkage, "constant", IR_LINKAGE_CONSTANT);
197 INSERT(tt_linkage, "weak", IR_LINKAGE_WEAK);
198 INSERT(tt_linkage, "garbage_collect", IR_LINKAGE_GARBAGE_COLLECT);
199 INSERT(tt_linkage, "merge", IR_LINKAGE_MERGE);
200 INSERT(tt_linkage, "hidden_user", IR_LINKAGE_HIDDEN_USER);
202 INSERT(tt_visibility, "local", ir_visibility_local);
203 INSERT(tt_visibility, "external", ir_visibility_external);
204 INSERT(tt_visibility, "default", ir_visibility_default);
205 INSERT(tt_visibility, "private", ir_visibility_private);
207 INSERTKEYWORD(constirg);
208 INSERTKEYWORD(entity);
211 INSERTKEYWORD(modes);
213 INSERTKEYWORD(typegraph);
214 INSERTKEYWORD(program);
215 INSERTKEYWORD(segment_type);
217 #include "gen_irio_lex.inl"
219 INSERTENUM(tt_align, align_non_aligned);
220 INSERTENUM(tt_align, align_is_aligned);
222 INSERTENUM(tt_builtin, ir_bk_trap);
223 INSERTENUM(tt_builtin, ir_bk_debugbreak);
224 INSERTENUM(tt_builtin, ir_bk_return_address);
225 INSERTENUM(tt_builtin, ir_bk_frame_address);
226 INSERTENUM(tt_builtin, ir_bk_prefetch);
227 INSERTENUM(tt_builtin, ir_bk_ffs);
228 INSERTENUM(tt_builtin, ir_bk_clz);
229 INSERTENUM(tt_builtin, ir_bk_ctz);
230 INSERTENUM(tt_builtin, ir_bk_popcount);
231 INSERTENUM(tt_builtin, ir_bk_parity);
232 INSERTENUM(tt_builtin, ir_bk_bswap);
233 INSERTENUM(tt_builtin, ir_bk_inport);
234 INSERTENUM(tt_builtin, ir_bk_outport);
235 INSERTENUM(tt_builtin, ir_bk_inner_trampoline);
237 INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_NONE);
238 INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_TRUE);
239 INSERTENUM(tt_cond_jmp_predicate, COND_JMP_PRED_FALSE);
241 INSERTENUM(tt_initializer, IR_INITIALIZER_CONST);
242 INSERTENUM(tt_initializer, IR_INITIALIZER_TARVAL);
243 INSERTENUM(tt_initializer, IR_INITIALIZER_NULL);
244 INSERTENUM(tt_initializer, IR_INITIALIZER_COMPOUND);
246 INSERTENUM(tt_mode_arithmetic, irma_uninitialized);
247 INSERTENUM(tt_mode_arithmetic, irma_none);
248 INSERTENUM(tt_mode_arithmetic, irma_twos_complement);
249 INSERTENUM(tt_mode_arithmetic, irma_ones_complement);
250 INSERTENUM(tt_mode_arithmetic, irma_int_BCD);
251 INSERTENUM(tt_mode_arithmetic, irma_ieee754);
252 INSERTENUM(tt_mode_arithmetic, irma_float_BCD);
254 INSERTENUM(tt_pin_state, op_pin_state_floats);
255 INSERTENUM(tt_pin_state, op_pin_state_pinned);
256 INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
257 INSERTENUM(tt_pin_state, op_pin_state_mem_pinned);
259 INSERTENUM(tt_type_state, layout_undefined);
260 INSERTENUM(tt_type_state, layout_fixed);
262 INSERTENUM(tt_volatility, volatility_non_volatile);
263 INSERTENUM(tt_volatility, volatility_is_volatile);
270 static const char *get_segment_name(ir_segment_t segment)
273 case IR_SEGMENT_GLOBAL: return "global";
274 case IR_SEGMENT_THREAD_LOCAL: return "thread_local";
275 case IR_SEGMENT_CONSTRUCTORS: return "constructors";
276 case IR_SEGMENT_DESTRUCTORS: return "destructors";
278 return "INVALID_SEGMENT";
281 static const char *get_visibility_name(ir_visibility visibility)
283 switch (visibility) {
284 case ir_visibility_local: return "local";
285 case ir_visibility_external: return "external";
286 case ir_visibility_default: return "default";
287 case ir_visibility_private: return "private";
289 return "INVALID_VISIBILITY";
292 /** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
293 static unsigned symbol(const char *str, typetag_t typetag)
295 symbol_t key, *entry;
298 key.typetag = typetag;
300 entry = (symbol_t*)set_find(symtbl, &key, sizeof(key), firm_fnv_hash_str(str) + typetag * 17);
301 return entry ? entry->code : SYMERROR;
304 static void *get_id(io_env_t *env, long id)
306 id_entry key, *entry;
309 entry = (id_entry*)set_find(env->idset, &key, sizeof(key), (unsigned) id);
310 return entry ? entry->elem : NULL;
313 static void set_id(io_env_t *env, long id, void *elem)
318 set_insert(env->idset, &key, sizeof(key), (unsigned) id);
321 static void write_mode(io_env_t *env, ir_mode *mode)
323 fputs(get_mode_name(mode), env->file);
324 fputc(' ', env->file);
327 static void write_tarval(io_env_t *env, ir_tarval *tv)
330 write_mode(env, get_tarval_mode(tv));
331 tarval_snprintf(buf, sizeof(buf), tv);
332 fputs(buf, env->file);
333 fputc(' ', env->file);
336 static void write_align(io_env_t *env, ir_node *irn)
341 align = get_Load_align(irn);
342 else if (is_Store(irn))
343 align = get_Store_align(irn);
345 panic("Invalid optype for write_align");
347 fputs(get_align_name(align), env->file);
348 fputc(' ', env->file);
351 static void write_builtin_kind(io_env_t *env, ir_node *irn)
353 fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
354 fputc(' ', env->file);
357 static void write_cond_jmp_predicate(io_env_t *env, ir_node *irn)
359 fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(irn)), env->file);
360 fputc(' ', env->file);
363 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
366 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
368 fputs(get_initializer_kind_name(ini_kind), f);
372 case IR_INITIALIZER_CONST:
373 fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
376 case IR_INITIALIZER_TARVAL:
377 write_tarval(env, get_initializer_tarval_value(ini));
380 case IR_INITIALIZER_NULL:
383 case IR_INITIALIZER_COMPOUND: {
384 unsigned i, n = get_initializer_compound_n_entries(ini);
385 fprintf(f, "%u ", n);
386 for (i = 0; i < n; i++)
387 write_initializer(env, get_initializer_compound_value(ini, i));
392 panic("Unknown initializer kind");
396 static void write_pin_state(io_env_t *env, ir_node *irn)
398 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
399 fputc(' ', env->file);
402 static void write_volatility(io_env_t *env, ir_node *irn)
407 vol = get_Load_volatility(irn);
408 else if (is_Store(irn))
409 vol = get_Store_volatility(irn);
411 panic("Invalid optype for write_volatility");
413 fputs(get_volatility_name(vol), env->file);
414 fputc(' ', env->file);
417 static void export_type_common(io_env_t *env, ir_type *tp)
419 fprintf(env->file, "\ttype %ld %s %u %u %s %u ",
421 get_type_tpop_name(tp),
422 get_type_size_bytes(tp),
423 get_type_alignment_bytes(tp),
424 get_type_state_name(get_type_state(tp)),
428 static void export_compound_name(FILE *f, const ir_type *tp)
430 ident *name = get_compound_ident(tp);
434 fprintf(f, "\"%s\" ", get_id_str(name));
438 static void export_type_pre(io_env_t *env, ir_type *tp)
442 /* skip types to be handled by post walker */
443 switch (get_type_tpop_code(tp)) {
452 export_type_common(env, tp);
454 switch (get_type_tpop_code(tp)) {
455 case tpo_uninitialized:
456 panic("invalid type found");
459 export_compound_name(f, tp);
463 write_mode(env, get_type_mode(tp));
468 case tpo_enumeration:
469 export_compound_name(f, tp);
483 static void export_type_post(io_env_t *env, ir_type *tp)
488 /* skip types already handled by pre walker */
489 switch (get_type_tpop_code(tp)) {
495 case tpo_uninitialized:
502 case tpo_enumeration:
506 export_type_common(env, tp);
508 switch (get_type_tpop_code(tp)) {
510 int n = get_array_n_dimensions(tp);
511 fprintf(f, "%d %ld ", n, get_type_nr(get_array_element_type(tp)));
512 for (i = 0; i < n; i++) {
513 ir_node *lower = get_array_lower_bound(tp, i);
514 ir_node *upper = get_array_upper_bound(tp, i);
517 fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
519 panic("Lower array bound is not constant");
522 fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
523 else if (is_Unknown(upper))
524 fputs("unknown ", f);
526 panic("Upper array bound is not constant");
532 int nparams = get_method_n_params(tp);
533 int nresults = get_method_n_ress(tp);
534 fprintf(f, "%u %u %d %d ", get_method_calling_convention(tp),
535 get_method_additional_properties(tp), nparams, nresults);
536 for (i = 0; i < nparams; i++)
537 fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
538 for (i = 0; i < nresults; i++)
539 fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
540 fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
545 write_mode(env, get_type_mode(tp));
546 fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
549 case tpo_enumeration:
550 fprintf(stderr, "Enumeration type not handled yet by exporter\n");
554 printf("export_type: Unknown type code \"%s\".\n",
555 get_type_tpop_name(tp));
561 static void export_entity(io_env_t *env, ir_entity *ent)
563 FILE *file = env->file;
564 ir_type *owner = get_entity_owner(ent);
565 ir_visibility visibility = get_entity_visibility(ent);
566 ir_linkage linkage = get_entity_linkage(ent);
568 /* we don't dump array_element_ent entities. They're a strange concept
569 * and lead to cycles in type_graph.
571 if (is_Array_type(owner))
574 fprintf(env->file, "\tentity %ld \"%s\" ",
575 get_entity_nr(ent), get_entity_name(ent));
576 if (ent->ld_name != NULL) {
577 fprintf(env->file, "\"%s\" ", get_entity_ld_name(ent));
579 fprintf(env->file, "NULL ");
582 /* visibility + linkage */
583 if (visibility != ir_visibility_default) {
584 fprintf(file, "%s ", get_visibility_name(visibility));
586 if (linkage & IR_LINKAGE_CONSTANT)
587 fputs("constant ", file);
588 if (linkage & IR_LINKAGE_WEAK)
589 fputs("weak ", file);
590 if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
591 fputs("garbage_collect ", file);
592 if (linkage & IR_LINKAGE_MERGE)
593 fputs("merge ", file);
594 if (linkage & IR_LINKAGE_HIDDEN_USER)
595 fputs("hidden_user ", file);
597 fprintf(file, "%ld %ld %d %u %d %s ",
598 get_type_nr(get_entity_type(ent)),
600 get_entity_offset(ent),
601 (unsigned) get_entity_offset_bits_remainder(ent),
602 is_entity_compiler_generated(ent),
603 get_volatility_name(get_entity_volatility(ent)));
605 if (ent->initializer != NULL) {
606 fputs("initializer ", env->file);
607 write_initializer(env, get_entity_initializer(ent));
608 } else if (entity_has_compound_ent_values(ent)) {
609 int i, n = get_compound_ent_n_values(ent);
610 fputs("compoundgraph ", env->file);
611 fprintf(env->file, "%d ", n);
612 for (i = 0; i < n; i++) {
613 ir_entity *member = get_compound_ent_value_member(ent, i);
614 ir_node *irn = get_compound_ent_value(ent, i);
615 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
618 fputs("none", env->file);
621 fputc('\n', env->file);
624 static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
626 io_env_t *env = (io_env_t *) ctx;
627 if (get_kind(tore.typ) == k_type)
628 export_type_pre(env, tore.typ);
631 static void export_type_or_ent_post(type_or_ent tore, void *ctx)
633 io_env_t *env = (io_env_t *) ctx;
635 switch (get_kind(tore.ent)) {
637 export_entity(env, tore.ent);
641 export_type_post(env, tore.typ);
645 panic("export_type_or_ent_post: Unknown type or entity.");
651 * Walker: exports every node.
653 static void export_node(ir_node *irn, void *ctx)
655 io_env_t *env = (io_env_t *) ctx;
657 unsigned opcode = get_irn_opcode(irn);
659 if (env->ignoreblocks && opcode == iro_Block)
662 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
664 n = get_irn_arity(irn);
665 for (i = -1; i < n; i++) {
666 ir_node *pred = get_irn_n(irn, i);
668 /* Anchor node may have NULL predecessors */
669 assert(is_Anchor(irn));
670 fputs("-1 ", env->file);
672 fprintf(env->file, "%ld ", get_irn_node_nr(pred));
676 fprintf(env->file, "] { ");
679 #include "gen_irio_export.inl"
681 fputs("}\n", env->file);
684 static const char *get_mode_sort_name(ir_mode_sort sort)
687 case irms_auxiliary: return "auxiliary";
688 case irms_control_flow: return "control_flow";
689 case irms_memory: return "memory";
690 case irms_internal_boolean: return "internal_boolean";
691 case irms_reference: return "reference";
692 case irms_int_number: return "int_number";
693 case irms_float_number: return "float_number";
695 panic("invalid mode sort found");
698 static void export_modes(io_env_t *env)
700 int i, n_modes = get_irp_n_modes();
702 fputs("modes {\n", env->file);
704 for (i = 0; i < n_modes; i++) {
705 ir_mode *mode = get_irp_mode(i);
706 switch (get_mode_sort(mode)) {
708 case irms_control_flow:
710 case irms_internal_boolean:
711 /* skip "internal" modes, which may not be user defined */
717 fprintf(env->file, "\tmode \"%s\" %s %u %d %s %u %u ",
718 get_mode_name(mode), get_mode_sort_name(get_mode_sort(mode)),
719 get_mode_size_bits(mode), get_mode_sign(mode),
720 get_mode_arithmetic_name(get_mode_arithmetic(mode)),
721 get_mode_modulo_shift(mode),
722 get_mode_n_vector_elems(mode));
723 if (mode_is_reference(mode)) {
724 write_mode(env, get_reference_mode_signed_eq(mode));
725 write_mode(env, get_reference_mode_unsigned_eq(mode));
727 fputc('\n', env->file);
730 fputs("}\n", env->file);
733 static void export_program(io_env_t *env)
738 fputs("\nprogram {\n", f);
739 if (irp_prog_name_is_set()) {
740 fprintf(f, "\tname \"%s\"\n", get_irp_name());
742 /* We need numbers for irgs... */
744 if (get_irp_main_irg() != NULL) {
745 fprintf(f, "\tmain_irg %d\n", get_irp_main_irg
748 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
749 ir_type *segment_type = get_segment_type(s);
750 fprintf(f, "\tsegment_type %s", get_segment_name(s));
751 if (segment_type == NULL) {
754 fprintf(f, " %ld\n", get_type_nr(segment_type));
760 void ir_export(const char *filename)
762 FILE *file = fopen(filename, "wt");
768 ir_export_file(file, filename);
772 /* Exports the whole irp to the given file in a textual form. */
773 void ir_export_file(FILE *file, const char *outputname)
776 int i, n_irgs = get_irp_n_irgs();
783 fputs("\ntypegraph {\n", env.file);
784 type_walk_prog(export_type_or_ent_pre, export_type_or_ent_post, &env);
785 fputs("}\n", env.file);
787 for (i = 0; i < n_irgs; i++) {
788 ir_graph *irg = get_irp_irg(i);
789 ir_type *valuetype = get_irg_value_param_type(irg);
791 fprintf(env.file, "\nirg %ld %ld %ld {\n",
792 get_entity_nr(get_irg_entity(irg)),
793 get_type_nr(get_irg_frame_type(irg)),
794 valuetype == NULL ? -1 : get_type_nr(valuetype));
796 env.ignoreblocks = 0;
797 irg_block_walk_graph(irg, NULL, export_node, &env);
799 env.ignoreblocks = 1;
800 irg_walk_anchors(irg, NULL, export_node, &env);
802 fputs("}\n", env.file);
805 fprintf(env.file, "\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
807 walk_const_code(NULL, export_node, &env);
808 fputs("}\n", env.file);
810 export_program(&env);
813 /* Exports the given irg to the given file. */
814 void ir_export_irg(ir_graph *irg, FILE *file, const char *outputname)
823 fputs("typegraph {\n", env.file);
825 type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
827 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
829 env.ignoreblocks = 0;
830 irg_block_walk_graph(irg, NULL, export_node, &env);
832 env.ignoreblocks = 1;
833 irg_walk_anchors(irg, NULL, export_node, &env);
835 /* TODO: Only output needed constants */
836 fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
837 walk_const_code(NULL, export_node, &env);
838 fputs("}\n", env.file);
841 static void read_c(io_env_t *env)
843 int c = fgetc(env->file);
849 /** Returns the first non-whitespace character or EOF. **/
850 static void skip_ws(io_env_t *env)
867 static void skip_to(io_env_t *env, char to_ch)
869 while (env->c != to_ch && env->c != EOF) {
874 static int expect_char(io_env_t *env, char ch)
878 parse_error(env, "Unexpected char '%c', expected '%c'\n",
886 #define EXPECT(c) if (expect_char(env, (c))) {} else return 0
888 static char *read_word(io_env_t *env)
892 assert(obstack_object_size(&env->obst) == 0);
904 obstack_1grow(&env->obst, c);
911 obstack_1grow(&env->obst, '\0');
912 return (char*)obstack_finish(&env->obst);
915 static char *read_quoted_string(io_env_t *env)
918 if (env->c != '\"') {
919 parse_error(env, "Expected '\"', found '%c'\n", env->c);
924 assert(obstack_object_size(&env->obst) == 0);
928 parse_error(env, "Unexpected end of quoted string!\n");
935 obstack_1grow(&env->obst, ch);
938 obstack_1grow(&env->obst, '\0');
940 return (char*)obstack_finish(&env->obst);
943 static ident *read_ident(io_env_t *env)
945 char *str = read_quoted_string(env);
946 ident *res = new_id_from_str(str);
947 obstack_free(&env->obst, str);
953 * reads a "quoted string" or alternatively the token NULL
955 static char *read_quoted_string_null(io_env_t *env)
959 char *str = read_word(env);
960 if (strcmp(str, "NULL") == 0) {
961 obstack_free(&env->obst, str);
964 } else if (env->c == '"') {
965 return read_quoted_string(env);
968 parse_error(env, "Expected \"string\" or NULL\n");
972 static ident *read_ident_null(io_env_t *env)
975 char *str = read_quoted_string_null(env);
979 res = new_id_from_str(str);
980 obstack_free(&env->obst, str);
984 static long read_long(io_env_t *env)
990 if (!isdigit(env->c) && env->c != '-') {
991 parse_error(env, "Expected number, got '%c'\n", env->c);
995 assert(obstack_object_size(&env->obst) == 0);
997 obstack_1grow(&env->obst, env->c);
999 } while (isdigit(env->c));
1000 obstack_1grow(&env->obst, 0);
1002 str = (char*)obstack_finish(&env->obst);
1004 obstack_free(&env->obst, str);
1009 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
1011 ir_node *node = (ir_node *) get_id(env, nodenr);
1012 if (node && node->kind != k_ir_node) {
1013 panic("Irn ID %ld collides with something else in line %d\n",
1019 static ir_node *get_node(io_env_t *env, long nodenr)
1021 ir_node *node = get_node_or_null(env, nodenr);
1023 panic("Unknown node: %ld in line %d\n", nodenr, env->line);
1028 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
1030 ir_node *node = get_node_or_null(env, nodenr);
1032 node = new_r_Dummy(env->irg, mode_X);
1033 set_id(env, nodenr, node);
1038 static ir_type *get_type(io_env_t *env, long typenr)
1040 ir_type *type = (ir_type *) get_id(env, typenr);
1042 panic("unknown type: %ld in line %d\n", typenr, env->line);
1043 else if (type->kind != k_type)
1044 panic("type ID %ld collides with something else in line %d\n",
1049 static ir_type *read_type(io_env_t *env)
1051 return get_type(env, read_long(env));
1054 static ir_entity *get_entity(io_env_t *env, long entnr)
1056 ir_entity *entity = (ir_entity *) get_id(env, entnr);
1057 if (entity == NULL) {
1058 parse_error(env, "unknown entity: %ld\n", entnr);
1060 } else if (entity->kind != k_entity) {
1061 panic("Entity ID %ld collides with something else in line %d\n",
1068 static ir_entity *read_entity(io_env_t *env)
1070 return get_entity(env, read_long(env));
1073 static ir_mode *read_mode(io_env_t *env)
1075 char *str = read_word(env);
1078 n = get_irp_n_modes();
1079 for (i = 0; i < n; i++) {
1080 ir_mode *mode = get_irp_mode(i);
1081 if (strcmp(str, get_mode_name(mode)) == 0) {
1082 obstack_free(&env->obst, str);
1087 parse_error(env, "unknown mode \"%s\"\n", str);
1091 static const char *get_typetag_name(typetag_t typetag)
1094 case tt_align: return "align";
1095 case tt_allocation: return "allocation";
1096 case tt_builtin: return "builtin kind";
1097 case tt_cond_jmp_predicate: return "cond_jmp_predicate";
1098 case tt_initializer: return "initializer kind";
1099 case tt_iro: return "opcode";
1100 case tt_keyword: return "keyword";
1101 case tt_linkage: return "linkage";
1102 case tt_mode_arithmetic: return "mode_arithmetic";
1103 case tt_mode_sort: return "mode_sort";
1104 case tt_pin_state: return "pin state";
1105 case tt_segment: return "segment";
1106 case tt_tpo: return "type";
1107 case tt_type_state: return "type state";
1108 case tt_volatility: return "volatility";
1109 case tt_visibility: return "visibility";
1115 * Read and decode an enum constant.
1117 static unsigned read_enum(io_env_t *env, typetag_t typetag)
1119 char *str = read_word(env);
1120 unsigned code = symbol(str, typetag);
1122 if (code != SYMERROR) {
1123 obstack_free(&env->obst, str);
1127 parse_error(env, "invalid %s: \"%s\"\n", get_typetag_name(typetag), str);
1131 #define read_align(env) ((ir_align) read_enum(env, tt_align))
1132 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
1133 #define read_builtin_kind(env) ((ir_builtin_kind) read_enum(env, tt_builtin))
1134 #define read_cond_jmp_predicate(env) ((cond_jmp_predicate) read_enum(env, tt_cond_jmp_predicate))
1135 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
1136 #define read_mode_arithmetic(env) ((ir_mode_arithmetic) read_enum(env, tt_mode_arithmetic))
1137 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
1138 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
1139 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
1140 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
1141 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
1143 static ir_cons_flags get_cons_flags(io_env_t *env)
1145 ir_cons_flags flags = cons_none;
1147 op_pin_state pinstate = read_pin_state(env);
1149 case op_pin_state_floats: flags |= cons_floats; break;
1150 case op_pin_state_pinned: break;
1152 panic("Error in %d: Invalid pinstate: %s", env->line,
1153 get_op_pin_state_name(pinstate));
1156 if (read_volatility(env) == volatility_is_volatile)
1157 flags |= cons_volatile;
1158 if (read_align(env) == align_non_aligned)
1159 flags |= cons_unaligned;
1164 static ir_tarval *read_tv(io_env_t *env)
1166 ir_mode *tvmode = read_mode(env);
1167 char *str = read_word(env);
1168 ir_tarval *tv = new_tarval_from_str(str, strlen(str), tvmode);
1169 obstack_free(&env->obst, str);
1174 static ir_initializer_t *read_initializer(io_env_t *env)
1176 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
1179 case IR_INITIALIZER_CONST: {
1180 ir_node *irn = get_node_or_dummy(env, read_long(env));
1181 return create_initializer_const(irn);
1184 case IR_INITIALIZER_TARVAL:
1185 return create_initializer_tarval(read_tv(env));
1187 case IR_INITIALIZER_NULL:
1188 return get_initializer_null();
1190 case IR_INITIALIZER_COMPOUND: {
1191 unsigned i, n = (unsigned) read_long(env);
1192 ir_initializer_t *ini = create_initializer_compound(n);
1193 for (i = 0; i < n; i++) {
1194 ir_initializer_t *curini = read_initializer(env);
1195 set_initializer_compound_value(ini, i, curini);
1201 panic("Unknown initializer kind");
1206 /** Reads a type description and remembers it by its id. */
1207 static void import_type(io_env_t *env)
1211 long typenr = read_long(env);
1212 tp_opcode tpop = (tp_opcode) read_enum(env, tt_tpo);
1213 unsigned size = (unsigned) read_long(env);
1214 unsigned align = (unsigned) read_long(env);
1215 ir_type_state state = read_type_state(env);
1216 unsigned flags = (unsigned) read_long(env);
1220 int ndims = (int) read_long(env);
1221 long elemtypenr = read_long(env);
1222 ir_type *elemtype = get_type(env, elemtypenr);
1224 type = new_type_array(ndims, elemtype);
1225 for (i = 0; i < ndims; i++) {
1226 char *str = read_word(env);
1227 if (strcmp(str, "unknown") != 0) {
1228 long lowerbound = atol(str);
1229 set_array_lower_bound_int(type, i, lowerbound);
1231 obstack_free(&env->obst, str);
1233 str = read_word(env);
1234 if (strcmp(str, "unknown") != 0) {
1235 long upperbound = atol(str);
1236 set_array_upper_bound_int(type, i, upperbound);
1238 obstack_free(&env->obst, str);
1240 set_type_size_bytes(type, size);
1245 const char *name = read_quoted_string_null(env);
1246 ident *id = name != NULL ? new_id_from_str(name) : NULL;
1248 if (typenr == (long) IR_SEGMENT_GLOBAL)
1249 type = get_glob_type();
1251 type = new_type_class(id);
1252 set_type_size_bytes(type, size);
1257 unsigned callingconv = (unsigned) read_long(env);
1258 mtp_additional_properties addprops = (mtp_additional_properties) read_long(env);
1259 int nparams = (int) read_long(env);
1260 int nresults = (int) read_long(env);
1263 type = new_type_method(nparams, nresults);
1265 for (i = 0; i < nparams; i++) {
1266 long typenr = read_long(env);
1267 ir_type *paramtype = get_type(env, typenr);
1269 set_method_param_type(type, i, paramtype);
1271 for (i = 0; i < nresults; i++) {
1272 long typenr = read_long(env);
1273 ir_type *restype = get_type(env, typenr);
1275 set_method_res_type(type, i, restype);
1278 variaindex = (int) read_long(env);
1279 if (variaindex != -1) {
1280 set_method_variadicity(type, variadicity_variadic);
1281 if (variaindex != nparams)
1282 set_method_first_variadic_param_index(type, variaindex);
1285 set_method_calling_convention(type, callingconv);
1286 set_method_additional_properties(type, addprops);
1291 ir_mode *mode = read_mode(env);
1292 ir_type *pointsto = get_type(env, read_long(env));
1293 type = new_type_pointer(pointsto);
1294 set_type_mode(type, mode);
1298 case tpo_primitive: {
1299 ir_mode *mode = read_mode(env);
1300 type = new_type_primitive(mode);
1305 const char *name = read_quoted_string_null(env);
1306 ident *id = name != NULL ? new_id_from_str(name) : NULL;
1307 type = new_type_struct(id);
1308 set_type_size_bytes(type, size);
1313 const char *name = read_quoted_string_null(env);
1314 ident *id = name != NULL ? new_id_from_str(name) : NULL;
1316 type = new_type_union(id);
1317 set_type_size_bytes(type, size);
1322 return; /* ignore unknown type */
1325 parse_error(env, "unknown type kind: \"%d\"\n", tpop);
1330 set_type_alignment_bytes(type, align);
1331 type->flags = flags;
1333 if (state == layout_fixed)
1334 ARR_APP1(ir_type *, env->fixedtypes, type);
1336 set_id(env, typenr, type);
1339 /** Reads an entity description and remembers it by its id. */
1340 static void import_entity(io_env_t *env)
1342 long entnr = read_long(env);
1343 ident *name = read_ident(env);
1344 ident *ld_name = read_ident_null(env);
1345 ir_visibility visibility = ir_visibility_default;
1346 ir_linkage linkage = IR_LINKAGE_DEFAULT;
1355 while (!isdigit(env->c)) {
1356 char *str = read_word(env);
1361 v = symbol(str, tt_visibility);
1362 if (v != SYMERROR) {
1363 visibility = (ir_visibility)v;
1366 v = symbol(str, tt_linkage);
1367 if (v != SYMERROR) {
1368 linkage |= (ir_linkage)v;
1371 printf("Parser error, expected visibility or linkage, got '%s'\n",
1376 typenr = read_long(env);
1377 ownertypenr = read_long(env);
1379 type = get_type(env, typenr);
1380 ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1381 entity = new_entity(ownertype, name, type);
1383 if (ld_name != NULL)
1384 set_entity_ld_ident(entity, ld_name);
1385 set_entity_offset(entity, (int) read_long(env));
1386 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1387 set_entity_compiler_generated(entity, (int) read_long(env));
1388 set_entity_volatility(entity, read_volatility(env));
1389 set_entity_visibility(entity, visibility);
1390 set_entity_linkage(entity, linkage);
1392 str = read_word(env);
1393 if (strcmp(str, "initializer") == 0) {
1394 set_entity_initializer(entity, read_initializer(env));
1395 } else if (strcmp(str, "compoundgraph") == 0) {
1396 int n = (int) read_long(env);
1398 for (i = 0; i < n; i++) {
1399 ir_entity *member = get_entity(env, read_long(env));
1400 ir_node *irn = get_node_or_dummy(env, read_long(env));
1401 add_compound_ent_value(entity, irn, member);
1403 } else if (strcmp(str, "none") == 0) {
1406 parse_error(env, "expected 'initializer', 'compoundgraph' or 'none' got '%s'\n", str);
1410 set_id(env, entnr, entity);
1413 /** Parses the whole type graph. */
1414 static int parse_typegraph(io_env_t *env)
1416 ir_graph *old_irg = env->irg;
1421 env->irg = get_const_code_irg();
1423 /* parse all types first */
1426 if (env->c == '}') {
1431 kwkind = (keyword_t) read_enum(env, tt_keyword);
1442 parse_error(env, "type graph element not supported yet: %d\n", kwkind);
1451 static int read_node_header(io_env_t *env, long *nodenr, long **preds,
1452 const char **nodename)
1456 *nodename = read_word(env);
1457 *nodenr = read_long(env);
1459 ARR_RESIZE(long, *preds, 0);
1462 for (numpreds = 0; !feof(env->file); numpreds++) {
1466 if (env->c == ']') {
1470 val = read_long(env);
1471 ARR_APP1(long, *preds, val);
1477 /** Parses an IRG. */
1478 static int parse_graph(io_env_t *env, ir_graph *irg)
1480 long *preds = NEW_ARR_F(long, 16);
1481 ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
1482 int i, numpreds, ret = 1;
1484 const char *nodename;
1485 ir_node *node, *newnode;
1493 if (env->c == '}') {
1498 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1499 if (numpreds == -1) break; /* end-of-graph */
1501 parse_error(env, "node %s %ld is missing predecessors!",
1507 ARR_RESIZE(ir_node *, prednodes, numpreds);
1508 for (i = 0; i < numpreds - 1; i++)
1509 prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
1511 node = get_node_or_null(env, nodenr);
1516 switch (symbol(nodename, tt_iro)) {
1519 ir_node *newendblock = get_node(env, preds[0]);
1520 newnode = get_irg_end(irg);
1521 exchange(get_nodes_block(newnode), newendblock);
1522 for (i = 0; i < numpreds - 1; i++)
1523 add_irn_n(newnode, prednodes[i]);
1529 ir_node *newstartblock = get_node(env, preds[0]);
1530 newnode = get_irg_start(irg);
1531 exchange(get_nodes_block(newnode), newstartblock);
1537 if (preds[0] != nodenr) {
1538 parse_error(env, "invalid block: preds[0] != nodenr (%ld != %ld)\n",
1544 newnode = new_r_Block(irg, numpreds - 1, prednodes);
1549 newnode = irg->anchor;
1550 for (i = 0; i < numpreds - 1; i++)
1551 set_irn_n(newnode, i, prednodes[i]);
1552 set_irn_n(newnode, -1, get_node(env, preds[0]));
1557 long entnr = read_long(env);
1558 union symconst_symbol sym;
1559 sym.entity_p = get_entity(env, entnr);
1560 newnode = new_r_SymConst(irg, mode_P, sym, symconst_addr_ent);
1564 #include "gen_irio_import.inl"
1574 parse_error(env, "node type not supported yet: %s\n", nodename);
1579 exchange(node, newnode);
1580 /* Always update hash entry to avoid more uses of id nodes */
1581 set_id(env, nodenr, newnode);
1586 DEL_ARR_F(prednodes);
1591 static int parse_modes(io_env_t *env)
1599 if (env->c == '}') {
1604 kwkind = (keyword_t) read_enum(env, tt_keyword);
1607 const char *name = read_quoted_string(env);
1608 ir_mode_sort sort = (ir_mode_sort)read_enum(env, tt_mode_sort);
1609 int size = read_long(env);
1610 int sign = read_long(env);
1611 ir_mode_arithmetic arith = read_mode_arithmetic(env);
1612 unsigned modulo_shift = read_long(env);
1613 int vector_elems = read_long(env);
1616 if (vector_elems != 1) {
1617 panic("no support for import of vector modes yes");
1620 mode = new_ir_mode(name, sort, size, sign, arith, modulo_shift);
1621 if (mode_is_reference(mode)) {
1622 set_reference_mode_signed_eq(mode, read_mode(env));
1623 set_reference_mode_unsigned_eq(mode, read_mode(env));
1636 static int parse_program(io_env_t *env)
1644 if (env->c == '}') {
1649 kwkind = (keyword_t) read_enum(env, tt_keyword);
1651 case kw_segment_type: {
1652 ir_segment_t segment = (ir_segment_t) read_enum(env, tt_segment);
1653 ir_type *type = read_type(env);
1654 set_segment_type(segment, type);
1658 parse_error(env, "unexpected keyword %d\n", kwkind);
1665 void ir_import(const char *filename)
1667 FILE *file = fopen(filename, "rt");
1673 ir_import_file(file, filename);
1678 void ir_import_file(FILE *input, const char *inputname)
1680 int oldoptimize = get_optimize();
1681 firm_verification_t oldver = get_node_verification_mode();
1683 io_env_t *env = &ioenv;
1688 memset(env, 0, sizeof(*env));
1689 obstack_init(&env->obst);
1690 env->idset = new_set(id_cmp, 128);
1691 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1692 env->inputname = inputname;
1696 /* read first character */
1700 do_node_verification(FIRM_VERIFICATION_OFF);
1709 kw = (keyword_t)read_enum(env, tt_keyword);
1712 if (!parse_modes(env)) goto end;
1716 if (!parse_typegraph(env)) goto end;
1721 ir_entity *irgent = get_entity(env, read_long(env));
1723 ir_graph *irg = new_ir_graph(irgent, 0);
1724 set_irg_frame_type(irg, get_type(env, read_long(env)));
1725 valuetypeid = read_long(env);
1726 if (valuetypeid != -1)
1727 set_method_value_param_type(get_entity_type(irgent),
1728 get_type(env, valuetypeid));
1730 if (!parse_graph(env, irg)) goto end;
1735 ir_graph *constirg = get_const_code_irg();
1736 long bodyblockid = read_long(env);
1737 set_id(env, bodyblockid, constirg->current_block);
1738 if (!parse_graph(env, constirg)) goto end;
1747 parse_error(env, "Unexpected keyword %d at toplevel\n", kw);
1754 n = ARR_LEN(env->fixedtypes);
1755 for (i = 0; i < n; i++)
1756 set_type_state(env->fixedtypes[i], layout_fixed);
1758 DEL_ARR_F(env->fixedtypes);
1760 del_set(env->idset);
1762 irp_finalize_cons();
1764 do_node_verification(oldver);
1765 set_optimize(oldoptimize);
1767 obstack_free(&env->obst, NULL);