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_long(io_env_t *env, long value)
323 fprintf(env->file, "%ld ", value);
326 static void write_int(io_env_t *env, int value)
328 fprintf(env->file, "%d ", value);
331 static void write_unsigned(io_env_t *env, unsigned value)
333 fprintf(env->file, "%u ", value);
336 static void write_entity_ref(io_env_t *env, ir_entity *entity)
338 write_long(env, get_entity_nr(entity));
341 static void write_type_ref(io_env_t *env, ir_type *type)
343 write_long(env, get_type_nr(type));
346 static void write_mode(io_env_t *env, ir_mode *mode)
348 fputs(get_mode_name(mode), env->file);
349 fputc(' ', env->file);
352 static void write_tarval(io_env_t *env, ir_tarval *tv)
355 write_mode(env, get_tarval_mode(tv));
356 tarval_snprintf(buf, sizeof(buf), tv);
357 fputs(buf, env->file);
358 fputc(' ', env->file);
361 static void write_align(io_env_t *env, ir_node *irn)
366 align = get_Load_align(irn);
367 else if (is_Store(irn))
368 align = get_Store_align(irn);
370 panic("Invalid optype for write_align");
372 fputs(get_align_name(align), env->file);
373 fputc(' ', env->file);
376 static void write_builtin_kind(io_env_t *env, ir_node *irn)
378 fputs(get_builtin_kind_name(get_Builtin_kind(irn)), env->file);
379 fputc(' ', env->file);
382 static void write_cond_jmp_predicate(io_env_t *env, ir_node *irn)
384 fputs(get_cond_jmp_predicate_name(get_Cond_jmp_pred(irn)), env->file);
385 fputc(' ', env->file);
388 static void write_initializer(io_env_t *env, ir_initializer_t *ini)
391 ir_initializer_kind_t ini_kind = get_initializer_kind(ini);
393 fputs(get_initializer_kind_name(ini_kind), f);
397 case IR_INITIALIZER_CONST:
398 write_long(env, get_irn_node_nr(get_initializer_const_value(ini)));
401 case IR_INITIALIZER_TARVAL:
402 write_tarval(env, get_initializer_tarval_value(ini));
405 case IR_INITIALIZER_NULL:
408 case IR_INITIALIZER_COMPOUND: {
409 unsigned i, n = get_initializer_compound_n_entries(ini);
410 fprintf(f, "%u ", n);
411 for (i = 0; i < n; i++)
412 write_initializer(env, get_initializer_compound_value(ini, i));
417 panic("Unknown initializer kind");
421 static void write_pin_state(io_env_t *env, ir_node *irn)
423 fputs(get_op_pin_state_name(get_irn_pinned(irn)), env->file);
424 fputc(' ', env->file);
427 static void write_volatility(io_env_t *env, ir_node *irn)
432 vol = get_Load_volatility(irn);
433 else if (is_Store(irn))
434 vol = get_Store_volatility(irn);
436 panic("Invalid optype for write_volatility");
438 fputs(get_volatility_name(vol), env->file);
439 fputc(' ', env->file);
442 static void export_type_common(io_env_t *env, ir_type *tp)
444 fprintf(env->file, "\ttype %ld %s %u %u %s %u ",
446 get_type_tpop_name(tp),
447 get_type_size_bytes(tp),
448 get_type_alignment_bytes(tp),
449 get_type_state_name(get_type_state(tp)),
453 static void export_compound_name(FILE *f, const ir_type *tp)
455 ident *name = get_compound_ident(tp);
459 fprintf(f, "\"%s\" ", get_id_str(name));
463 static void export_type_pre(io_env_t *env, ir_type *tp)
467 /* skip types to be handled by post walker */
468 switch (get_type_tpop_code(tp)) {
477 export_type_common(env, tp);
479 switch (get_type_tpop_code(tp)) {
480 case tpo_uninitialized:
481 panic("invalid type found");
484 export_compound_name(f, tp);
488 write_mode(env, get_type_mode(tp));
493 case tpo_enumeration:
494 export_compound_name(f, tp);
508 static void export_type_post(io_env_t *env, ir_type *tp)
513 /* skip types already handled by pre walker */
514 switch (get_type_tpop_code(tp)) {
520 case tpo_uninitialized:
527 case tpo_enumeration:
531 export_type_common(env, tp);
533 switch (get_type_tpop_code(tp)) {
535 int n = get_array_n_dimensions(tp);
536 fprintf(f, "%d %ld ", n, get_type_nr(get_array_element_type(tp)));
537 for (i = 0; i < n; i++) {
538 ir_node *lower = get_array_lower_bound(tp, i);
539 ir_node *upper = get_array_upper_bound(tp, i);
542 write_long(env, get_tarval_long(get_Const_tarval(lower)));
544 panic("Lower array bound is not constant");
547 write_long(env, get_tarval_long(get_Const_tarval(upper)));
548 else if (is_Unknown(upper))
549 fputs("unknown ", f);
551 panic("Upper array bound is not constant");
557 int nparams = get_method_n_params(tp);
558 int nresults = get_method_n_ress(tp);
559 fprintf(f, "%u %u %d %d ", get_method_calling_convention(tp),
560 get_method_additional_properties(tp), nparams, nresults);
561 for (i = 0; i < nparams; i++)
562 write_long(env, get_type_nr(get_method_param_type(tp, i)));
563 for (i = 0; i < nresults; i++)
564 write_long(env, get_type_nr(get_method_res_type(tp, i)));
565 fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
570 write_mode(env, get_type_mode(tp));
571 write_long(env, get_type_nr(get_pointer_points_to_type(tp)));
574 case tpo_enumeration:
575 fprintf(stderr, "Enumeration type not handled yet by exporter\n");
579 printf("export_type: Unknown type code \"%s\".\n",
580 get_type_tpop_name(tp));
586 static void export_entity(io_env_t *env, ir_entity *ent)
588 FILE *file = env->file;
589 ir_type *owner = get_entity_owner(ent);
590 ir_visibility visibility = get_entity_visibility(ent);
591 ir_linkage linkage = get_entity_linkage(ent);
593 /* we don't dump array_element_ent entities. They're a strange concept
594 * and lead to cycles in type_graph.
596 if (is_Array_type(owner))
599 fprintf(env->file, "\tentity %ld \"%s\" ",
600 get_entity_nr(ent), get_entity_name(ent));
601 if (ent->ld_name != NULL) {
602 fprintf(env->file, "\"%s\" ", get_entity_ld_name(ent));
604 fprintf(env->file, "NULL ");
607 /* visibility + linkage */
608 if (visibility != ir_visibility_default) {
609 fprintf(file, "%s ", get_visibility_name(visibility));
611 if (linkage & IR_LINKAGE_CONSTANT)
612 fputs("constant ", file);
613 if (linkage & IR_LINKAGE_WEAK)
614 fputs("weak ", file);
615 if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
616 fputs("garbage_collect ", file);
617 if (linkage & IR_LINKAGE_MERGE)
618 fputs("merge ", file);
619 if (linkage & IR_LINKAGE_HIDDEN_USER)
620 fputs("hidden_user ", file);
622 fprintf(file, "%ld %ld %d %u %d %s ",
623 get_type_nr(get_entity_type(ent)),
625 get_entity_offset(ent),
626 (unsigned) get_entity_offset_bits_remainder(ent),
627 is_entity_compiler_generated(ent),
628 get_volatility_name(get_entity_volatility(ent)));
630 if (ent->initializer != NULL) {
631 fputs("initializer ", env->file);
632 write_initializer(env, get_entity_initializer(ent));
633 } else if (entity_has_compound_ent_values(ent)) {
634 int i, n = get_compound_ent_n_values(ent);
635 fputs("compoundgraph ", env->file);
636 fprintf(env->file, "%d ", n);
637 for (i = 0; i < n; i++) {
638 ir_entity *member = get_compound_ent_value_member(ent, i);
639 ir_node *irn = get_compound_ent_value(ent, i);
640 fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
643 fputs("none", env->file);
646 fputc('\n', env->file);
649 static void export_type_or_ent_pre(type_or_ent tore, void *ctx)
651 io_env_t *env = (io_env_t *) ctx;
652 if (get_kind(tore.typ) == k_type)
653 export_type_pre(env, tore.typ);
656 static void export_type_or_ent_post(type_or_ent tore, void *ctx)
658 io_env_t *env = (io_env_t *) ctx;
660 switch (get_kind(tore.ent)) {
662 export_entity(env, tore.ent);
666 export_type_post(env, tore.typ);
670 panic("export_type_or_ent_post: Unknown type or entity.");
676 * Walker: exports every node.
678 static void export_node(ir_node *irn, void *ctx)
680 io_env_t *env = (io_env_t *) ctx;
682 unsigned opcode = get_irn_opcode(irn);
684 if (env->ignoreblocks && opcode == iro_Block)
687 fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
689 n = get_irn_arity(irn);
690 if (!is_Block(irn)) {
691 fprintf(env->file, "%ld ", get_irn_node_nr(get_nodes_block(irn)));
694 for (i = 0; i < n; i++) {
695 ir_node *pred = get_irn_n(irn, i);
697 /* Anchor node may have NULL predecessors */
698 assert(is_Anchor(irn));
699 fputs("-1 ", env->file);
701 write_long(env, get_irn_node_nr(pred));
705 fprintf(env->file, "] { ");
714 /* TODO: only symconst_addr_ent implemented yet */
715 assert(get_SymConst_kind(irn) == symconst_addr_ent);
716 fprintf(env->file, "%ld ", get_entity_nr(get_SymConst_entity(irn)));
719 write_mode(env, get_irn_mode(irn));
720 fprintf(env->file, "%ld ", get_Proj_proj(irn));
722 #include "gen_irio_export.inl"
724 panic("no export code for node %+F\n", irn);
726 fputs("}\n", env->file);
729 static const char *get_mode_sort_name(ir_mode_sort sort)
732 case irms_auxiliary: return "auxiliary";
733 case irms_control_flow: return "control_flow";
734 case irms_memory: return "memory";
735 case irms_internal_boolean: return "internal_boolean";
736 case irms_reference: return "reference";
737 case irms_int_number: return "int_number";
738 case irms_float_number: return "float_number";
740 panic("invalid mode sort found");
743 static void export_modes(io_env_t *env)
745 int i, n_modes = get_irp_n_modes();
747 fputs("modes {\n", env->file);
749 for (i = 0; i < n_modes; i++) {
750 ir_mode *mode = get_irp_mode(i);
751 switch (get_mode_sort(mode)) {
753 case irms_control_flow:
755 case irms_internal_boolean:
756 /* skip "internal" modes, which may not be user defined */
762 fprintf(env->file, "\tmode \"%s\" %s %u %d %s %u %u ",
763 get_mode_name(mode), get_mode_sort_name(get_mode_sort(mode)),
764 get_mode_size_bits(mode), get_mode_sign(mode),
765 get_mode_arithmetic_name(get_mode_arithmetic(mode)),
766 get_mode_modulo_shift(mode),
767 get_mode_n_vector_elems(mode));
768 if (mode_is_reference(mode)) {
769 write_mode(env, get_reference_mode_signed_eq(mode));
770 write_mode(env, get_reference_mode_unsigned_eq(mode));
772 fputc('\n', env->file);
775 fputs("}\n", env->file);
778 static void export_program(io_env_t *env)
783 fputs("\nprogram {\n", f);
784 if (irp_prog_name_is_set()) {
785 fprintf(f, "\tname \"%s\"\n", get_irp_name());
787 /* We need numbers for irgs... */
789 if (get_irp_main_irg() != NULL) {
790 fprintf(f, "\tmain_irg %d\n", get_irp_main_irg
793 for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
794 ir_type *segment_type = get_segment_type(s);
795 fprintf(f, "\tsegment_type %s", get_segment_name(s));
796 if (segment_type == NULL) {
799 fprintf(f, " %ld\n", get_type_nr(segment_type));
805 void ir_export(const char *filename)
807 FILE *file = fopen(filename, "wt");
813 ir_export_file(file, filename);
817 /* Exports the whole irp to the given file in a textual form. */
818 void ir_export_file(FILE *file, const char *outputname)
821 int i, n_irgs = get_irp_n_irgs();
828 fputs("\ntypegraph {\n", env.file);
829 type_walk_prog(export_type_or_ent_pre, export_type_or_ent_post, &env);
830 fputs("}\n", env.file);
832 for (i = 0; i < n_irgs; i++) {
833 ir_graph *irg = get_irp_irg(i);
834 ir_type *valuetype = get_irg_value_param_type(irg);
836 fprintf(env.file, "\nirg %ld %ld %ld {\n",
837 get_entity_nr(get_irg_entity(irg)),
838 get_type_nr(get_irg_frame_type(irg)),
839 valuetype == NULL ? -1 : get_type_nr(valuetype));
841 env.ignoreblocks = 0;
842 irg_block_walk_graph(irg, NULL, export_node, &env);
844 env.ignoreblocks = 1;
845 irg_walk_anchors(irg, NULL, export_node, &env);
847 fputs("}\n", env.file);
850 fprintf(env.file, "\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
852 walk_const_code(NULL, export_node, &env);
853 fputs("}\n", env.file);
855 export_program(&env);
858 /* Exports the given irg to the given file. */
859 void ir_export_irg(ir_graph *irg, FILE *file, const char *outputname)
868 fputs("typegraph {\n", env.file);
870 type_walk_irg(irg, export_type_or_ent_pre, export_type_or_ent_post, &env);
872 fprintf(env.file, "}\n\nirg %ld {\n", get_entity_nr(get_irg_entity(irg)));
874 env.ignoreblocks = 0;
875 irg_block_walk_graph(irg, NULL, export_node, &env);
877 env.ignoreblocks = 1;
878 irg_walk_anchors(irg, NULL, export_node, &env);
880 /* TODO: Only output needed constants */
881 fprintf(env.file, "}\n\nconstirg %ld {\n", get_irn_node_nr(get_const_code_irg()->current_block));
882 walk_const_code(NULL, export_node, &env);
883 fputs("}\n", env.file);
886 static void read_c(io_env_t *env)
888 int c = fgetc(env->file);
894 /** Returns the first non-whitespace character or EOF. **/
895 static void skip_ws(io_env_t *env)
912 static void skip_to(io_env_t *env, char to_ch)
914 while (env->c != to_ch && env->c != EOF) {
919 static int expect_char(io_env_t *env, char ch)
923 parse_error(env, "Unexpected char '%c', expected '%c'\n",
931 #define EXPECT(c) if (expect_char(env, (c))) {} else return 0
933 static char *read_word(io_env_t *env)
937 assert(obstack_object_size(&env->obst) == 0);
949 obstack_1grow(&env->obst, c);
956 obstack_1grow(&env->obst, '\0');
957 return (char*)obstack_finish(&env->obst);
960 static char *read_quoted_string(io_env_t *env)
963 if (env->c != '\"') {
964 parse_error(env, "Expected '\"', found '%c'\n", env->c);
969 assert(obstack_object_size(&env->obst) == 0);
973 parse_error(env, "Unexpected end of quoted string!\n");
980 obstack_1grow(&env->obst, ch);
983 obstack_1grow(&env->obst, '\0');
985 return (char*)obstack_finish(&env->obst);
988 static ident *read_ident(io_env_t *env)
990 char *str = read_quoted_string(env);
991 ident *res = new_id_from_str(str);
992 obstack_free(&env->obst, str);
998 * reads a "quoted string" or alternatively the token NULL
1000 static char *read_quoted_string_null(io_env_t *env)
1003 if (env->c == 'N') {
1004 char *str = read_word(env);
1005 if (strcmp(str, "NULL") == 0) {
1006 obstack_free(&env->obst, str);
1009 } else if (env->c == '"') {
1010 return read_quoted_string(env);
1013 parse_error(env, "Expected \"string\" or NULL\n");
1017 static ident *read_ident_null(io_env_t *env)
1020 char *str = read_quoted_string_null(env);
1024 res = new_id_from_str(str);
1025 obstack_free(&env->obst, str);
1029 static long read_long(io_env_t *env)
1035 if (!isdigit(env->c) && env->c != '-') {
1036 parse_error(env, "Expected number, got '%c'\n", env->c);
1040 assert(obstack_object_size(&env->obst) == 0);
1042 obstack_1grow(&env->obst, env->c);
1044 } while (isdigit(env->c));
1045 obstack_1grow(&env->obst, 0);
1047 str = (char*)obstack_finish(&env->obst);
1049 obstack_free(&env->obst, str);
1054 static int read_int(io_env_t *env)
1056 return (int) read_long(env);
1059 static unsigned read_unsigned(io_env_t *env)
1061 return (unsigned) read_long(env);
1064 static ir_node *get_node_or_null(io_env_t *env, long nodenr)
1066 ir_node *node = (ir_node *) get_id(env, nodenr);
1067 if (node && node->kind != k_ir_node) {
1068 panic("Irn ID %ld collides with something else in line %d\n",
1074 static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
1076 ir_node *node = get_node_or_null(env, nodenr);
1078 node = new_r_Dummy(env->irg, mode_X);
1079 set_id(env, nodenr, node);
1084 static ir_type *get_type(io_env_t *env, long typenr)
1086 ir_type *type = (ir_type *) get_id(env, typenr);
1088 panic("unknown type: %ld in line %d\n", typenr, env->line);
1089 else if (type->kind != k_type)
1090 panic("type ID %ld collides with something else in line %d\n",
1095 static ir_type *read_type(io_env_t *env)
1097 return get_type(env, read_long(env));
1100 static ir_entity *get_entity(io_env_t *env, long entnr)
1102 ir_entity *entity = (ir_entity *) get_id(env, entnr);
1103 if (entity == NULL) {
1104 parse_error(env, "unknown entity: %ld\n", entnr);
1106 } else if (entity->kind != k_entity) {
1107 panic("Entity ID %ld collides with something else in line %d\n",
1114 static ir_entity *read_entity(io_env_t *env)
1116 return get_entity(env, read_long(env));
1119 static ir_mode *read_mode(io_env_t *env)
1121 char *str = read_word(env);
1124 n = get_irp_n_modes();
1125 for (i = 0; i < n; i++) {
1126 ir_mode *mode = get_irp_mode(i);
1127 if (strcmp(str, get_mode_name(mode)) == 0) {
1128 obstack_free(&env->obst, str);
1133 parse_error(env, "unknown mode \"%s\"\n", str);
1137 static const char *get_typetag_name(typetag_t typetag)
1140 case tt_align: return "align";
1141 case tt_allocation: return "allocation";
1142 case tt_builtin: return "builtin kind";
1143 case tt_cond_jmp_predicate: return "cond_jmp_predicate";
1144 case tt_initializer: return "initializer kind";
1145 case tt_iro: return "opcode";
1146 case tt_keyword: return "keyword";
1147 case tt_linkage: return "linkage";
1148 case tt_mode_arithmetic: return "mode_arithmetic";
1149 case tt_mode_sort: return "mode_sort";
1150 case tt_pin_state: return "pin state";
1151 case tt_segment: return "segment";
1152 case tt_tpo: return "type";
1153 case tt_type_state: return "type state";
1154 case tt_volatility: return "volatility";
1155 case tt_visibility: return "visibility";
1161 * Read and decode an enum constant.
1163 static unsigned read_enum(io_env_t *env, typetag_t typetag)
1165 char *str = read_word(env);
1166 unsigned code = symbol(str, typetag);
1168 if (code != SYMERROR) {
1169 obstack_free(&env->obst, str);
1173 parse_error(env, "invalid %s: \"%s\"\n", get_typetag_name(typetag), str);
1177 #define read_align(env) ((ir_align) read_enum(env, tt_align))
1178 #define read_allocation(env) ((ir_allocation) read_enum(env, tt_allocation))
1179 #define read_builtin_kind(env) ((ir_builtin_kind) read_enum(env, tt_builtin))
1180 #define read_cond_jmp_predicate(env) ((cond_jmp_predicate) read_enum(env, tt_cond_jmp_predicate))
1181 #define read_initializer_kind(env) ((ir_initializer_kind_t) read_enum(env, tt_initializer))
1182 #define read_mode_arithmetic(env) ((ir_mode_arithmetic) read_enum(env, tt_mode_arithmetic))
1183 #define read_peculiarity(env) ((ir_peculiarity) read_enum(env, tt_peculiarity))
1184 #define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
1185 #define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
1186 #define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
1187 #define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
1189 static ir_cons_flags get_cons_flags(io_env_t *env)
1191 ir_cons_flags flags = cons_none;
1193 op_pin_state pinstate = read_pin_state(env);
1195 case op_pin_state_floats: flags |= cons_floats; break;
1196 case op_pin_state_pinned: break;
1198 panic("Error in %d: Invalid pinstate: %s", env->line,
1199 get_op_pin_state_name(pinstate));
1202 if (read_volatility(env) == volatility_is_volatile)
1203 flags |= cons_volatile;
1204 if (read_align(env) == align_non_aligned)
1205 flags |= cons_unaligned;
1210 static ir_tarval *read_tv(io_env_t *env)
1212 ir_mode *tvmode = read_mode(env);
1213 char *str = read_word(env);
1214 ir_tarval *tv = new_tarval_from_str(str, strlen(str), tvmode);
1215 obstack_free(&env->obst, str);
1220 static ir_initializer_t *read_initializer(io_env_t *env)
1222 ir_initializer_kind_t ini_kind = read_initializer_kind(env);
1225 case IR_INITIALIZER_CONST: {
1226 ir_node *irn = get_node_or_dummy(env, read_long(env));
1227 return create_initializer_const(irn);
1230 case IR_INITIALIZER_TARVAL:
1231 return create_initializer_tarval(read_tv(env));
1233 case IR_INITIALIZER_NULL:
1234 return get_initializer_null();
1236 case IR_INITIALIZER_COMPOUND: {
1237 unsigned i, n = (unsigned) read_long(env);
1238 ir_initializer_t *ini = create_initializer_compound(n);
1239 for (i = 0; i < n; i++) {
1240 ir_initializer_t *curini = read_initializer(env);
1241 set_initializer_compound_value(ini, i, curini);
1247 panic("Unknown initializer kind");
1252 /** Reads a type description and remembers it by its id. */
1253 static void import_type(io_env_t *env)
1257 long typenr = read_long(env);
1258 tp_opcode tpop = (tp_opcode) read_enum(env, tt_tpo);
1259 unsigned size = (unsigned) read_long(env);
1260 unsigned align = (unsigned) read_long(env);
1261 ir_type_state state = read_type_state(env);
1262 unsigned flags = (unsigned) read_long(env);
1266 int ndims = (int) read_long(env);
1267 long elemtypenr = read_long(env);
1268 ir_type *elemtype = get_type(env, elemtypenr);
1270 type = new_type_array(ndims, elemtype);
1271 for (i = 0; i < ndims; i++) {
1272 char *str = read_word(env);
1273 if (strcmp(str, "unknown") != 0) {
1274 long lowerbound = atol(str);
1275 set_array_lower_bound_int(type, i, lowerbound);
1277 obstack_free(&env->obst, str);
1279 str = read_word(env);
1280 if (strcmp(str, "unknown") != 0) {
1281 long upperbound = atol(str);
1282 set_array_upper_bound_int(type, i, upperbound);
1284 obstack_free(&env->obst, str);
1286 set_type_size_bytes(type, size);
1291 const char *name = read_quoted_string_null(env);
1292 ident *id = name != NULL ? new_id_from_str(name) : NULL;
1294 if (typenr == (long) IR_SEGMENT_GLOBAL)
1295 type = get_glob_type();
1297 type = new_type_class(id);
1298 set_type_size_bytes(type, size);
1303 unsigned callingconv = (unsigned) read_long(env);
1304 mtp_additional_properties addprops = (mtp_additional_properties) read_long(env);
1305 int nparams = (int) read_long(env);
1306 int nresults = (int) read_long(env);
1309 type = new_type_method(nparams, nresults);
1311 for (i = 0; i < nparams; i++) {
1312 long typenr = read_long(env);
1313 ir_type *paramtype = get_type(env, typenr);
1315 set_method_param_type(type, i, paramtype);
1317 for (i = 0; i < nresults; i++) {
1318 long typenr = read_long(env);
1319 ir_type *restype = get_type(env, typenr);
1321 set_method_res_type(type, i, restype);
1324 variaindex = (int) read_long(env);
1325 if (variaindex != -1) {
1326 set_method_variadicity(type, variadicity_variadic);
1327 if (variaindex != nparams)
1328 set_method_first_variadic_param_index(type, variaindex);
1331 set_method_calling_convention(type, callingconv);
1332 set_method_additional_properties(type, addprops);
1337 ir_mode *mode = read_mode(env);
1338 ir_type *pointsto = get_type(env, read_long(env));
1339 type = new_type_pointer(pointsto);
1340 set_type_mode(type, mode);
1344 case tpo_primitive: {
1345 ir_mode *mode = read_mode(env);
1346 type = new_type_primitive(mode);
1351 const char *name = read_quoted_string_null(env);
1352 ident *id = name != NULL ? new_id_from_str(name) : NULL;
1353 type = new_type_struct(id);
1354 set_type_size_bytes(type, size);
1359 const char *name = read_quoted_string_null(env);
1360 ident *id = name != NULL ? new_id_from_str(name) : NULL;
1362 type = new_type_union(id);
1363 set_type_size_bytes(type, size);
1368 return; /* ignore unknown type */
1371 parse_error(env, "unknown type kind: \"%d\"\n", tpop);
1376 set_type_alignment_bytes(type, align);
1377 type->flags = flags;
1379 if (state == layout_fixed)
1380 ARR_APP1(ir_type *, env->fixedtypes, type);
1382 set_id(env, typenr, type);
1385 /** Reads an entity description and remembers it by its id. */
1386 static void import_entity(io_env_t *env)
1388 long entnr = read_long(env);
1389 ident *name = read_ident(env);
1390 ident *ld_name = read_ident_null(env);
1391 ir_visibility visibility = ir_visibility_default;
1392 ir_linkage linkage = IR_LINKAGE_DEFAULT;
1401 while (!isdigit(env->c)) {
1402 char *str = read_word(env);
1407 v = symbol(str, tt_visibility);
1408 if (v != SYMERROR) {
1409 visibility = (ir_visibility)v;
1412 v = symbol(str, tt_linkage);
1413 if (v != SYMERROR) {
1414 linkage |= (ir_linkage)v;
1417 printf("Parser error, expected visibility or linkage, got '%s'\n",
1422 typenr = read_long(env);
1423 ownertypenr = read_long(env);
1425 type = get_type(env, typenr);
1426 ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
1427 entity = new_entity(ownertype, name, type);
1429 if (ld_name != NULL)
1430 set_entity_ld_ident(entity, ld_name);
1431 set_entity_offset(entity, (int) read_long(env));
1432 set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
1433 set_entity_compiler_generated(entity, (int) read_long(env));
1434 set_entity_volatility(entity, read_volatility(env));
1435 set_entity_visibility(entity, visibility);
1436 set_entity_linkage(entity, linkage);
1438 str = read_word(env);
1439 if (strcmp(str, "initializer") == 0) {
1440 set_entity_initializer(entity, read_initializer(env));
1441 } else if (strcmp(str, "compoundgraph") == 0) {
1442 int n = (int) read_long(env);
1444 for (i = 0; i < n; i++) {
1445 ir_entity *member = get_entity(env, read_long(env));
1446 ir_node *irn = get_node_or_dummy(env, read_long(env));
1447 add_compound_ent_value(entity, irn, member);
1449 } else if (strcmp(str, "none") == 0) {
1452 parse_error(env, "expected 'initializer', 'compoundgraph' or 'none' got '%s'\n", str);
1456 set_id(env, entnr, entity);
1459 /** Parses the whole type graph. */
1460 static int parse_typegraph(io_env_t *env)
1462 ir_graph *old_irg = env->irg;
1467 env->irg = get_const_code_irg();
1469 /* parse all types first */
1472 if (env->c == '}') {
1477 kwkind = (keyword_t) read_enum(env, tt_keyword);
1488 parse_error(env, "type graph element not supported yet: %d\n", kwkind);
1497 static int read_node_header(io_env_t *env, long *nodenr, ir_node ***preds,
1498 const char **nodename)
1502 *nodename = read_word(env);
1503 *nodenr = read_long(env);
1505 ARR_RESIZE(ir_node*, *preds, 0);
1508 for (numpreds = 0; !feof(env->file); numpreds++) {
1513 if (env->c == ']') {
1517 val = read_long(env);
1518 pred = get_node_or_dummy(env, val);
1519 ARR_APP1(ir_node*, *preds, pred);
1525 /** Parses an IRG. */
1526 static int parse_graph(io_env_t *env, ir_graph *irg)
1528 ir_node **preds = NEW_ARR_F(ir_node*,0);
1529 int i, numpreds, ret = 1;
1531 const char *nodename;
1532 ir_node *node, *newnode;
1540 if (env->c == '}') {
1545 numpreds = read_node_header(env, &nodenr, &preds, &nodename);
1547 node = get_node_or_null(env, nodenr);
1552 switch (symbol(nodename, tt_iro)) {
1554 ir_node *newendblock = preds[0];
1555 newnode = get_irg_end(irg);
1556 exchange(get_nodes_block(newnode), newendblock);
1557 for (i = 1; i < numpreds; i++)
1558 add_irn_n(newnode, preds[i]);
1563 ir_node *newstartblock = preds[0];
1564 newnode = get_irg_start(irg);
1565 exchange(get_nodes_block(newnode), newstartblock);
1570 newnode = new_r_Block(irg, numpreds, preds);
1574 newnode = irg->anchor;
1575 for (i = 1; i < numpreds; i++)
1576 set_irn_n(newnode, i-1, preds[i]);
1577 set_nodes_block(newnode, preds[0]);
1580 case iro_SymConst: {
1581 long entnr = read_long(env);
1582 union symconst_symbol sym;
1583 sym.entity_p = get_entity(env, entnr);
1584 newnode = new_r_SymConst(irg, mode_P, sym, symconst_addr_ent);
1589 ir_mode *mode = read_mode(env);
1590 long pn = read_long(env);
1591 newnode = new_r_Proj(preds[1], mode, pn);
1592 /* explicitely set block, since preds[1] might be a dummy node
1593 * which is always in the startblock */
1594 set_nodes_block(newnode, preds[0]);
1598 #include "gen_irio_import.inl"
1608 parse_error(env, "node type not supported yet: %s\n", nodename);
1613 exchange(node, newnode);
1614 /* Always update hash entry to avoid more uses of id nodes */
1615 set_id(env, nodenr, newnode);
1623 static int parse_modes(io_env_t *env)
1631 if (env->c == '}') {
1636 kwkind = (keyword_t) read_enum(env, tt_keyword);
1639 const char *name = read_quoted_string(env);
1640 ir_mode_sort sort = (ir_mode_sort)read_enum(env, tt_mode_sort);
1641 int size = read_long(env);
1642 int sign = read_long(env);
1643 ir_mode_arithmetic arith = read_mode_arithmetic(env);
1644 unsigned modulo_shift = read_long(env);
1645 int vector_elems = read_long(env);
1648 if (vector_elems != 1) {
1649 panic("no support for import of vector modes yes");
1652 mode = new_ir_mode(name, sort, size, sign, arith, modulo_shift);
1653 if (mode_is_reference(mode)) {
1654 set_reference_mode_signed_eq(mode, read_mode(env));
1655 set_reference_mode_unsigned_eq(mode, read_mode(env));
1668 static int parse_program(io_env_t *env)
1676 if (env->c == '}') {
1681 kwkind = (keyword_t) read_enum(env, tt_keyword);
1683 case kw_segment_type: {
1684 ir_segment_t segment = (ir_segment_t) read_enum(env, tt_segment);
1685 ir_type *type = read_type(env);
1686 set_segment_type(segment, type);
1690 parse_error(env, "unexpected keyword %d\n", kwkind);
1697 void ir_import(const char *filename)
1699 FILE *file = fopen(filename, "rt");
1705 ir_import_file(file, filename);
1710 void ir_import_file(FILE *input, const char *inputname)
1712 int oldoptimize = get_optimize();
1713 firm_verification_t oldver = get_node_verification_mode();
1715 io_env_t *env = &ioenv;
1720 memset(env, 0, sizeof(*env));
1721 obstack_init(&env->obst);
1722 env->idset = new_set(id_cmp, 128);
1723 env->fixedtypes = NEW_ARR_F(ir_type *, 0);
1724 env->inputname = inputname;
1728 /* read first character */
1732 do_node_verification(FIRM_VERIFICATION_OFF);
1741 kw = (keyword_t)read_enum(env, tt_keyword);
1744 if (!parse_modes(env)) goto end;
1748 if (!parse_typegraph(env)) goto end;
1753 ir_entity *irgent = get_entity(env, read_long(env));
1755 ir_graph *irg = new_ir_graph(irgent, 0);
1756 set_irg_frame_type(irg, get_type(env, read_long(env)));
1757 valuetypeid = read_long(env);
1758 if (valuetypeid != -1)
1759 set_method_value_param_type(get_entity_type(irgent),
1760 get_type(env, valuetypeid));
1762 if (!parse_graph(env, irg)) goto end;
1767 ir_graph *constirg = get_const_code_irg();
1768 long bodyblockid = read_long(env);
1769 set_id(env, bodyblockid, constirg->current_block);
1770 if (!parse_graph(env, constirg)) goto end;
1779 parse_error(env, "Unexpected keyword %d at toplevel\n", kw);
1786 n = ARR_LEN(env->fixedtypes);
1787 for (i = 0; i < n; i++)
1788 set_type_state(env->fixedtypes[i], layout_fixed);
1790 DEL_ARR_F(env->fixedtypes);
1792 del_set(env->idset);
1794 irp_finalize_cons();
1796 do_node_verification(oldver);
1797 set_optimize(oldoptimize);
1799 obstack_free(&env->obst, NULL);