/*
- * Copyright (C) 1995-2009 University of Karlsruhe. All right reserved.
+ * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
*
* This file is part of libFirm.
*
#include "irnode_t.h"
#include "irprog.h"
#include "irgraph_t.h"
+#include "irprintf.h"
#include "ircons.h"
#include "irgmod.h"
#include "irflag_t.h"
int line;
ir_type **fixedtypes;
struct obstack obst;
+ ir_graph *irg;
} io_env_t;
typedef enum typetag_t
tt_keyword,
tt_mode_sort,
tt_mode_arithmetic,
- tt_peculiarity,
tt_pin_state,
tt_tpo,
tt_type_state,
- tt_variability,
- tt_visibility,
tt_volatility,
- tt_segment
+ tt_linkage,
+ tt_segment,
+ tt_visibility
} typetag_t;
typedef enum keyword_t
INSERT(tt_segment, "constructors", IR_SEGMENT_CONSTRUCTORS);
INSERT(tt_segment, "destructors", IR_SEGMENT_DESTRUCTORS);
+ INSERT(tt_linkage, "constant", IR_LINKAGE_CONSTANT);
+ INSERT(tt_linkage, "weak", IR_LINKAGE_WEAK);
+ INSERT(tt_linkage, "garbage_collect", IR_LINKAGE_GARBAGE_COLLECT);
+ INSERT(tt_linkage, "merge", IR_LINKAGE_MERGE);
+ INSERT(tt_linkage, "hidden_user", IR_LINKAGE_HIDDEN_USER);
+
+ INSERT(tt_visibility, "local", ir_visibility_local);
+ INSERT(tt_visibility, "external", ir_visibility_external);
+ INSERT(tt_visibility, "default", ir_visibility_default);
+ INSERT(tt_visibility, "private", ir_visibility_private);
+
INSERTKEYWORD(constirg);
INSERTKEYWORD(entity);
INSERTKEYWORD(irg);
INSERTENUM(tt_align, align_non_aligned);
INSERTENUM(tt_align, align_is_aligned);
- INSERTENUM(tt_allocation, allocation_automatic);
- INSERTENUM(tt_allocation, allocation_parameter);
- INSERTENUM(tt_allocation, allocation_dynamic);
- INSERTENUM(tt_allocation, allocation_static);
-
INSERTENUM(tt_builtin, ir_bk_trap);
INSERTENUM(tt_builtin, ir_bk_debugbreak);
INSERTENUM(tt_builtin, ir_bk_return_address);
INSERTENUM(tt_mode_arithmetic, irma_ieee754);
INSERTENUM(tt_mode_arithmetic, irma_float_BCD);
- INSERTENUM(tt_peculiarity, peculiarity_description);
- INSERTENUM(tt_peculiarity, peculiarity_inherited);
- INSERTENUM(tt_peculiarity, peculiarity_existent);
-
INSERTENUM(tt_pin_state, op_pin_state_floats);
INSERTENUM(tt_pin_state, op_pin_state_pinned);
INSERTENUM(tt_pin_state, op_pin_state_exc_pinned);
INSERTENUM(tt_type_state, layout_undefined);
INSERTENUM(tt_type_state, layout_fixed);
- INSERTENUM(tt_variability, variability_uninitialized);
- INSERTENUM(tt_variability, variability_initialized);
- INSERTENUM(tt_variability, variability_part_constant);
- INSERTENUM(tt_variability, variability_constant);
-
- INSERTENUM(tt_visibility, visibility_local);
- INSERTENUM(tt_visibility, visibility_external_visible);
- INSERTENUM(tt_visibility, visibility_external_allocated);
-
INSERTENUM(tt_volatility, volatility_non_volatile);
INSERTENUM(tt_volatility, volatility_is_volatile);
return "INVALID_SEGMENT";
}
+static const char *get_visibility_name(ir_visibility visibility)
+{
+ switch (visibility) {
+ case ir_visibility_local: return "local";
+ case ir_visibility_external: return "external";
+ case ir_visibility_default: return "default";
+ case ir_visibility_private: return "private";
+ }
+ return "INVALID_VISIBILITY";
+}
+
/** Returns the according symbol value for the given string and tag, or SYMERROR if none was found. */
static unsigned symbol(const char *str, typetag_t typetag)
{
key.str = str;
key.typetag = typetag;
- entry = set_find(symtbl, &key, sizeof(key), firm_fnv_hash_str(str) + typetag * 17);
+ entry = (symbol_t*)set_find(symtbl, &key, sizeof(key), firm_fnv_hash_str(str) + typetag * 17);
return entry ? entry->code : SYMERROR;
}
id_entry key, *entry;
key.id = id;
- entry = set_find(env->idset, &key, sizeof(key), (unsigned) id);
+ entry = (id_entry*)set_find(env->idset, &key, sizeof(key), (unsigned) id);
return entry ? entry->elem : NULL;
}
set_insert(env->idset, &key, sizeof(key), (unsigned) id);
}
+static void write_long(io_env_t *env, long value)
+{
+ fprintf(env->file, "%ld ", value);
+}
+
+static void write_int(io_env_t *env, int value)
+{
+ fprintf(env->file, "%d ", value);
+}
+
+static void write_unsigned(io_env_t *env, unsigned value)
+{
+ fprintf(env->file, "%u ", value);
+}
+
+static void write_entity_ref(io_env_t *env, ir_entity *entity)
+{
+ write_long(env, get_entity_nr(entity));
+}
+
+static void write_type_ref(io_env_t *env, ir_type *type)
+{
+ write_long(env, get_type_nr(type));
+}
+
+static void write_string(io_env_t *env, const char *string)
+{
+ const char *c;
+ fputc('"', env->file);
+ for (c = string; *c != '\0'; ++c) {
+ switch (*c) {
+ case '\n':
+ fputc('\\', env->file);
+ fputc('n', env->file);
+ break;
+ case '"':
+ case '\\':
+ fputc('\\', env->file);
+ /* FALLTHROUGH */
+ default:
+ fputc(*c, env->file);
+ break;
+ }
+ }
+ fputc('"', env->file);
+}
+
+static void write_ident(io_env_t *env, ident *id)
+{
+ write_string(env, get_id_str(id));
+ fputc(' ', env->file);
+}
+
+static void write_ident_null(io_env_t *env, ident *id)
+{
+ if (id == NULL) {
+ fputs("NULL ", env->file);
+ } else {
+ write_ident(env, id);
+ }
+}
+
static void write_mode(io_env_t *env, ir_mode *mode)
{
fputs(get_mode_name(mode), env->file);
fputc(' ', env->file);
}
-static void write_tarval(io_env_t *env, tarval *tv)
+static void write_tarval(io_env_t *env, ir_tarval *tv)
{
char buf[1024];
write_mode(env, get_tarval_mode(tv));
fputc(' ', env->file);
}
-static void write_align(io_env_t *env, ir_node *irn)
+static void write_align(io_env_t *env, ir_align align)
{
- ir_align align;
-
- if (is_Load(irn))
- align = get_Load_align(irn);
- else if (is_Store(irn))
- align = get_Store_align(irn);
- else
- panic("Invalid optype for write_align");
-
fputs(get_align_name(align), env->file);
fputc(' ', env->file);
}
fputc(' ', env->file);
}
+static void write_list_begin(io_env_t *env)
+{
+ fputs("[", env->file);
+}
+
+static void write_list_end(io_env_t *env)
+{
+ fputs("] ", env->file);
+}
+
static void write_initializer(io_env_t *env, ir_initializer_t *ini)
{
FILE *f = env->file;
switch (ini_kind) {
case IR_INITIALIZER_CONST:
- fprintf(f, "%ld ", get_irn_node_nr(get_initializer_const_value(ini)));
+ write_long(env, get_irn_node_nr(get_initializer_const_value(ini)));
break;
case IR_INITIALIZER_TARVAL:
break;
case IR_INITIALIZER_COMPOUND: {
- unsigned i, n = get_initializer_compound_n_entries(ini);
- fprintf(f, "%d ", n);
- for (i = 0; i < n; i++)
+ size_t i, n = get_initializer_compound_n_entries(ini);
+ ir_fprintf(f, "%zu ", n);
+ for (i = 0; i < n; ++i)
write_initializer(env, get_initializer_compound_value(ini, i));
break;
}
fputc(' ', env->file);
}
-static void write_volatility(io_env_t *env, ir_node *irn)
+static void write_volatility(io_env_t *env, ir_volatility vol)
{
- ir_volatility vol;
-
- if (is_Load(irn))
- vol = get_Load_volatility(irn);
- else if (is_Store(irn))
- vol = get_Store_volatility(irn);
- else
- panic("Invalid optype for write_volatility");
-
fputs(get_volatility_name(vol), env->file);
fputc(' ', env->file);
}
static void export_type_common(io_env_t *env, ir_type *tp)
{
- fprintf(env->file, "\ttype %ld %s %u %u %s %s %d ",
+ fprintf(env->file, "\ttype %ld %s %u %u %s %u ",
get_type_nr(tp),
get_type_tpop_name(tp),
get_type_size_bytes(tp),
get_type_alignment_bytes(tp),
get_type_state_name(get_type_state(tp)),
- get_visibility_name(get_type_visibility(tp)),
tp->flags);
}
-static void export_compound_name(FILE *f, const ir_type *tp)
-{
- ident *name = get_compound_ident(tp);
- if (name == NULL) {
- fputs("NULL ", f);
- } else {
- fprintf(f, "\"%s\" ", get_id_str(name));
- }
-}
-
static void export_type_pre(io_env_t *env, ir_type *tp)
{
FILE *f = env->file;
panic("invalid type found");
case tpo_class:
- export_compound_name(f, tp);
+ write_ident_null(env, get_compound_ident(tp));
break;
case tpo_primitive:
case tpo_union:
case tpo_struct:
case tpo_enumeration:
- export_compound_name(f, tp);
+ write_ident_null(env, get_compound_ident(tp));
break;
case tpo_array:
static void export_type_post(io_env_t *env, ir_type *tp)
{
FILE *f = env->file;
- int i;
+ size_t i;
/* skip types already handled by pre walker */
switch (get_type_tpop_code(tp)) {
switch (get_type_tpop_code(tp)) {
case tpo_array: {
- int n = get_array_n_dimensions(tp);
- fprintf(f, "%d %ld ", n, get_type_nr(get_array_element_type(tp)));
+ size_t n = get_array_n_dimensions(tp);
+ ir_fprintf(f, "%zu %ld ", n, get_type_nr(get_array_element_type(tp)));
for (i = 0; i < n; i++) {
ir_node *lower = get_array_lower_bound(tp, i);
ir_node *upper = get_array_upper_bound(tp, i);
if (is_Const(lower))
- fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(lower)));
+ write_long(env, get_tarval_long(get_Const_tarval(lower)));
else
panic("Lower array bound is not constant");
if (is_Const(upper))
- fprintf(f, "%ld ", get_tarval_long(get_Const_tarval(upper)));
+ write_long(env, get_tarval_long(get_Const_tarval(upper)));
else if (is_Unknown(upper))
fputs("unknown ", f);
else
}
case tpo_method: {
- int nparams = get_method_n_params(tp);
- int nresults = get_method_n_ress(tp);
- fprintf(f, "%u %u %d %d ", get_method_calling_convention(tp),
+ size_t nparams = get_method_n_params(tp);
+ size_t nresults = get_method_n_ress(tp);
+ ir_fprintf(f, "%u %u %zu %zu ", get_method_calling_convention(tp),
get_method_additional_properties(tp), nparams, nresults);
for (i = 0; i < nparams; i++)
- fprintf(f, "%ld ", get_type_nr(get_method_param_type(tp, i)));
+ write_long(env, get_type_nr(get_method_param_type(tp, i)));
for (i = 0; i < nresults; i++)
- fprintf(f, "%ld ", get_type_nr(get_method_res_type(tp, i)));
- fprintf(f, "%d ", get_method_first_variadic_param_index(tp));
+ write_long(env, get_type_nr(get_method_res_type(tp, i)));
+ ir_fprintf(f, "%u ", get_method_variadicity(tp));
break;
}
case tpo_pointer:
write_mode(env, get_type_mode(tp));
- fprintf(f, "%ld ", get_type_nr(get_pointer_points_to_type(tp)));
+ write_long(env, get_type_nr(get_pointer_points_to_type(tp)));
break;
case tpo_enumeration:
static void export_entity(io_env_t *env, ir_entity *ent)
{
- ir_type *owner = get_entity_owner(ent);
+ FILE *file = env->file;
+ ir_type *owner = get_entity_owner(ent);
+ ir_visibility visibility = get_entity_visibility(ent);
+ ir_linkage linkage = get_entity_linkage(ent);
/* we don't dump array_element_ent entities. They're a strange concept
* and lead to cycles in type_graph.
if (is_Array_type(owner))
return;
- fprintf(env->file, "\tentity %ld \"%s\" ",
- get_entity_nr(ent), get_entity_name(ent));
- if (ent->ld_name != NULL) {
- fprintf(env->file, "\"%s\" ", get_entity_ld_name(ent));
+ fprintf(env->file, "\tentity ");
+ write_long(env, get_entity_nr(ent));
+ write_ident_null(env, get_entity_ident(ent));
+ if (!entity_has_ld_ident(ent)) {
+ write_ident_null(env, NULL);
} else {
- fprintf(env->file, "NULL ");
+ write_ident_null(env, get_entity_ld_ident(ent));
}
- fprintf(env->file, "%ld %ld %d %u %d %s %s %s %s %s ",
+ /* visibility + linkage */
+ if (visibility != ir_visibility_default) {
+ fprintf(file, "%s ", get_visibility_name(visibility));
+ }
+ if (linkage & IR_LINKAGE_CONSTANT)
+ fputs("constant ", file);
+ if (linkage & IR_LINKAGE_WEAK)
+ fputs("weak ", file);
+ if (linkage & IR_LINKAGE_GARBAGE_COLLECT)
+ fputs("garbage_collect ", file);
+ if (linkage & IR_LINKAGE_MERGE)
+ fputs("merge ", file);
+ if (linkage & IR_LINKAGE_HIDDEN_USER)
+ fputs("hidden_user ", file);
+
+ fprintf(file, "%ld %ld %d %u %d %s ",
get_type_nr(get_entity_type(ent)),
get_type_nr(owner),
get_entity_offset(ent),
(unsigned) get_entity_offset_bits_remainder(ent),
is_entity_compiler_generated(ent),
- get_allocation_name(get_entity_allocation(ent)),
- get_visibility_name(get_entity_visibility(ent)),
- get_variability_name(get_entity_variability(ent)),
- get_peculiarity_name(get_entity_peculiarity(ent)),
get_volatility_name(get_entity_volatility(ent)));
- /* TODO: inheritance stuff for class entities not supported yet */
- if (is_Class_type(owner) && owner != get_glob_type())
- fprintf(stderr, "Inheritance of class entities not supported yet!\n");
-
- if (get_entity_variability(ent) != variability_uninitialized &&
- get_entity_visibility(ent) != visibility_external_allocated)
- {
- if (is_compound_entity(ent)) {
- if (has_entity_initializer(ent)) {
- fputs("initializer ", env->file);
- write_initializer(env, get_entity_initializer(ent));
- } else {
- int i, n = get_compound_ent_n_values(ent);
- fputs("noninitializer ", env->file);
- fprintf(env->file, "%d ", n);
- for (i = 0; i < n; i++) {
- ir_entity *member = get_compound_ent_value_member(ent, i);
- ir_node *irn = get_compound_ent_value(ent, i);
- fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
- }
- }
- } else {
- ir_node *irn = get_atomic_ent_value(ent);
- fprintf(env->file, "%ld ", get_irn_node_nr(irn));
+ if (ent->initializer != NULL) {
+ fputs("initializer ", env->file);
+ write_initializer(env, get_entity_initializer(ent));
+ } else if (entity_has_compound_ent_values(ent)) {
+ size_t i, n = get_compound_ent_n_values(ent);
+ fputs("compoundgraph ", env->file);
+ ir_fprintf(env->file, "%zu ", n);
+ for (i = 0; i < n; i++) {
+ ir_entity *member = get_compound_ent_value_member(ent, i);
+ ir_node *irn = get_compound_ent_value(ent, i);
+ fprintf(env->file, "%ld %ld ", get_entity_nr(member), get_irn_node_nr(irn));
}
+ } else {
+ fputs("none", env->file);
}
fputc('\n', env->file);
}
}
+static void export_ASM(io_env_t *env, ir_node *node)
+{
+ ir_asm_constraint *input_constraints = get_ASM_input_constraints(node);
+ ir_asm_constraint *output_constraints = get_ASM_output_constraints(node);
+ ident **clobbers = get_ASM_clobbers(node);
+ int n_input_constraints = get_ASM_n_input_constraints(node);
+ int n_output_constraints = get_ASM_n_output_constraints(node);
+ int n_clobbers = get_ASM_n_clobbers(node);
+ int i;
+
+ write_ident(env, get_ASM_text(node));
+ write_list_begin(env);
+ for (i = 0; i < n_input_constraints; ++i) {
+ const ir_asm_constraint *constraint = &input_constraints[i];
+ write_unsigned(env, constraint->pos);
+ write_ident(env, constraint->constraint);
+ write_mode(env, constraint->mode);
+ }
+ write_list_end(env);
+
+ write_list_begin(env);
+ for (i = 0; i < n_output_constraints; ++i) {
+ const ir_asm_constraint *constraint = &output_constraints[i];
+ write_unsigned(env, constraint->pos);
+ write_ident(env, constraint->constraint);
+ write_mode(env, constraint->mode);
+ }
+ write_list_end(env);
+
+ write_list_begin(env);
+ for (i = 0; i < n_clobbers; ++i) {
+ ident *clobber = clobbers[i];
+ write_ident(env, clobber);
+ }
+ write_list_end(env);
+}
+
/**
* Walker: exports every node.
*/
if (env->ignoreblocks && opcode == iro_Block)
return;
- fprintf(env->file, "\t%s %ld [ ", get_irn_opname(irn), get_irn_node_nr(irn));
+ fprintf(env->file, "\t%s ", get_irn_opname(irn));
+ write_long(env, get_irn_node_nr(irn));
+ write_list_begin(env);
n = get_irn_arity(irn);
- for (i = -1; i < n; i++) {
+ if (!is_Block(irn)) {
+ ir_node *block = get_nodes_block(irn);
+ write_long(env, get_irn_node_nr(block));
+ }
+
+ for (i = 0; i < n; i++) {
ir_node *pred = get_irn_n(irn, i);
if (pred == NULL) {
/* Anchor node may have NULL predecessors */
assert(is_Anchor(irn));
fputs("-1 ", env->file);
} else {
- fprintf(env->file, "%ld ", get_irn_node_nr(pred));
+ write_long(env, get_irn_node_nr(pred));
}
}
+ write_list_end(env);
- fprintf(env->file, "] { ");
+ fputs("{ ", env->file);
- switch (opcode) {
- #include "gen_irio_export.inl"
+ switch (opcode) {
+ case iro_Start:
+ case iro_End:
+ case iro_Block:
+ case iro_Anchor:
+ break;
+ case iro_SymConst:
+ /* TODO: only symconst_addr_ent implemented yet */
+ assert(get_SymConst_kind(irn) == symconst_addr_ent);
+ fprintf(env->file, "%ld ", get_entity_nr(get_SymConst_entity(irn)));
+ break;
+ case iro_Proj:
+ write_mode(env, get_irn_mode(irn));
+ fprintf(env->file, "%ld ", get_Proj_proj(irn));
+ break;
+ case iro_ASM:
+ export_ASM(env, irn);
+ break;
+#include "gen_irio_export.inl"
+ default:
+ panic("no export code for node %+F\n", irn);
}
fputs("}\n", env->file);
}
static void export_modes(io_env_t *env)
{
- int i, n_modes = get_irp_n_modes();
+ size_t i, n_modes = get_irp_n_modes();
fputs("modes {\n", env->file);
break;
}
- fprintf(env->file, "\tmode \"%s\" %s %d %d %s %d %d ",
- get_mode_name(mode), get_mode_sort_name(get_mode_sort(mode)),
+ fprintf(env->file, "\tmode ");
+ write_string(env, get_mode_name(mode));
+ fprintf(env->file, "%s %u %d %s %u %u ",
+ get_mode_sort_name(get_mode_sort(mode)),
get_mode_size_bits(mode), get_mode_sign(mode),
get_mode_arithmetic_name(get_mode_arithmetic(mode)),
get_mode_modulo_shift(mode),
fputs("\nprogram {\n", f);
if (irp_prog_name_is_set()) {
- fprintf(f, "\tname \"%s\"\n", get_irp_name());
+ fprintf(f, "\tname ");
+ write_string(env, get_irp_name());
+ fputc('\n', f);
}
- /* We need numbers for irgs... */
-#if 0
- if (get_irp_main_irg() != NULL) {
- fprintf(f, "\tmain_irg %d\n", get_irp_main_irg
-#endif
- for (s = 0; s <= IR_SEGMENT_LAST; ++s) {
+ for (s = IR_SEGMENT_FIRST; s <= IR_SEGMENT_LAST; ++s) {
ir_type *segment_type = get_segment_type(s);
- fprintf(f, "\tsegment_type %s", get_segment_name(s));
+ fprintf(f, "\tsegment_type %s ", get_segment_name(s));
if (segment_type == NULL) {
fputs(" NULL\n", f);
} else {
- fprintf(f, " %ld\n", get_type_nr(segment_type));
+ write_long(env, get_type_nr(segment_type));
+ fputc('\n', f);
}
}
fputs("}\n", f);
void ir_export_file(FILE *file, const char *outputname)
{
io_env_t env;
- int i, n_irgs = get_irp_n_irgs();
+ size_t i, n_irgs = get_irp_n_irgs();
(void) outputname;
env.file = file;
endofword:
obstack_1grow(&env->obst, '\0');
- return obstack_finish(&env->obst);
+ return (char*)obstack_finish(&env->obst);
}
-static char *read_quoted_string(io_env_t *env)
+static char *read_string(io_env_t *env)
{
skip_ws(env);
- if (env->c != '\"') {
- parse_error(env, "Expected '\"', found '%c'\n", env->c);
+ if (env->c != '"') {
+ parse_error(env, "Expected string, got '%c'\n", env->c);
exit(1);
}
read_c(env);
assert(obstack_object_size(&env->obst) == 0);
- while (true) {
- int ch = env->c;
- if (ch == EOF) {
- parse_error(env, "Unexpected end of quoted string!\n");
+ while (env->c != '"') {
+ if (env->c == EOF) {
+ parse_error(env, "Unexpected EOF while parsing string\n");
exit(1);
}
- if (ch == '\"') {
+
+ if (env->c == '\\') {
read_c(env);
- break;
+ switch (env->c) {
+ case 'n':
+ obstack_1grow(&env->obst, '\n');
+ break;
+ case '"':
+ case '\\':
+ obstack_1grow(&env->obst, env->c);
+ break;
+ default:
+ parse_error(env, "Unknown escape sequence '\\%c'\n", env->c);
+ exit(1);
+ break;
+ }
+ } else {
+ obstack_1grow(&env->obst, env->c);
}
- obstack_1grow(&env->obst, ch);
read_c(env);
}
- obstack_1grow(&env->obst, '\0');
+ read_c(env);
+ obstack_1grow(&env->obst, 0);
- return obstack_finish(&env->obst);
+ return (char*)obstack_finish(&env->obst);
}
static ident *read_ident(io_env_t *env)
{
- char *str = read_quoted_string(env);
+ char *str = read_string(env);
ident *res = new_id_from_str(str);
obstack_free(&env->obst, str);
-
return res;
}
/*
* reads a "quoted string" or alternatively the token NULL
*/
-static char *read_quoted_string_null(io_env_t *env)
+static char *read_string_null(io_env_t *env)
{
skip_ws(env);
if (env->c == 'N') {
return NULL;
}
} else if (env->c == '"') {
- return read_quoted_string(env);
+ return read_string(env);
}
parse_error(env, "Expected \"string\" or NULL\n");
static ident *read_ident_null(io_env_t *env)
{
ident *res;
- char *str = read_quoted_string_null(env);
+ char *str = read_string_null(env);
if (str == NULL)
return NULL;
do {
obstack_1grow(&env->obst, env->c);
read_c(env);
- } while(isdigit(env->c));
+ } while (isdigit(env->c));
obstack_1grow(&env->obst, 0);
- str = obstack_finish(&env->obst);
+ str = (char*)obstack_finish(&env->obst);
result = atol(str);
obstack_free(&env->obst, str);
return result;
}
+static int read_int(io_env_t *env)
+{
+ return (int) read_long(env);
+}
+
+static unsigned read_unsigned(io_env_t *env)
+{
+ return (unsigned) read_long(env);
+}
+
+static size_t read_size_t(io_env_t *env)
+{
+ /* FIXME */
+ return (size_t) read_unsigned(env);
+}
+
+static void expect_list_begin(io_env_t *env)
+{
+ skip_ws(env);
+ if (env->c != '[') {
+ parse_error(env, "Expected list, got '%c'\n", env->c);
+ exit(1);
+ }
+ read_c(env);
+}
+
+static bool list_has_next(io_env_t *env)
+{
+ if (feof(env->file)) {
+ parse_error(env, "Unexpected EOF while reading list");
+ exit(1);
+ }
+ skip_ws(env);
+ if (env->c == ']') {
+ read_c(env);
+ return false;
+ }
+
+ return true;
+}
+
static ir_node *get_node_or_null(io_env_t *env, long nodenr)
{
ir_node *node = (ir_node *) get_id(env, nodenr);
return node;
}
-static ir_node *get_node(io_env_t *env, long nodenr)
-{
- ir_node *node = get_node_or_null(env, nodenr);
- if (!node)
- panic("Unknown node: %ld in line %d\n", nodenr, env->line);
-
- return node;
-}
-
static ir_node *get_node_or_dummy(io_env_t *env, long nodenr)
{
ir_node *node = get_node_or_null(env, nodenr);
if (node == NULL) {
- node = new_Dummy(mode_X);
+ node = new_r_Dummy(env->irg, mode_X);
set_id(env, nodenr, node);
}
return node;
static ir_mode *read_mode(io_env_t *env)
{
char *str = read_word(env);
- int i, n;
+ size_t i, n;
n = get_irp_n_modes();
for (i = 0; i < n; i++) {
case tt_align: return "align";
case tt_allocation: return "allocation";
case tt_builtin: return "builtin kind";
+ case tt_cond_jmp_predicate: return "cond_jmp_predicate";
case tt_initializer: return "initializer kind";
case tt_iro: return "opcode";
- case tt_peculiarity: return "peculiarity";
+ case tt_keyword: return "keyword";
+ case tt_linkage: return "linkage";
+ case tt_mode_arithmetic: return "mode_arithmetic";
+ case tt_mode_sort: return "mode_sort";
case tt_pin_state: return "pin state";
+ case tt_segment: return "segment";
case tt_tpo: return "type";
case tt_type_state: return "type state";
- case tt_variability: return "variability";
- case tt_visibility: return "visibility";
case tt_volatility: return "volatility";
- case tt_cond_jmp_predicate: return "cond_jmp_predicate";
- case tt_keyword: return "keyword";
- case tt_mode_sort: return "mode_sort";
- case tt_mode_arithmetic: return "mode_arithmetic";
- case tt_segment: return "segment";
+ case tt_visibility: return "visibility";
}
return "<UNKNOWN>";
}
#define read_pin_state(env) ((op_pin_state) read_enum(env, tt_pin_state))
#define read_type_state(env) ((ir_type_state) read_enum(env, tt_type_state))
#define read_variability(env) ((ir_variability) read_enum(env, tt_variability))
-#define read_visibility(env) ((ir_visibility) read_enum(env, tt_visibility))
#define read_volatility(env) ((ir_volatility) read_enum(env, tt_volatility))
static ir_cons_flags get_cons_flags(io_env_t *env)
return flags;
}
-static tarval *read_tv(io_env_t *env)
+static ir_tarval *read_tv(io_env_t *env)
{
- ir_mode *tvmode = read_mode(env);
- char *str = read_word(env);
- tarval *tv = new_tarval_from_str(str, strlen(str), tvmode);
+ ir_mode *tvmode = read_mode(env);
+ char *str = read_word(env);
+ ir_tarval *tv = new_tarval_from_str(str, strlen(str), tvmode);
obstack_free(&env->obst, str);
return tv;
return get_initializer_null();
case IR_INITIALIZER_COMPOUND: {
- unsigned i, n = (unsigned) read_long(env);
+ size_t i, n = read_size_t(env);
ir_initializer_t *ini = create_initializer_compound(n);
for (i = 0; i < n; i++) {
ir_initializer_t *curini = read_initializer(env);
unsigned size = (unsigned) read_long(env);
unsigned align = (unsigned) read_long(env);
ir_type_state state = read_type_state(env);
- ir_visibility vis = read_visibility(env);
unsigned flags = (unsigned) read_long(env);
switch (tpop) {
}
case tpo_class: {
- const char *name = read_quoted_string_null(env);
- ident *id = name != NULL ? new_id_from_str(name) : NULL;
+ ident *id = read_ident_null(env);
- if(typenr == (long) IR_SEGMENT_GLOBAL)
+ if (typenr == (long) IR_SEGMENT_GLOBAL)
type = get_glob_type();
else
type = new_type_class(id);
}
case tpo_method: {
- unsigned callingconv = (unsigned) read_long(env);
- unsigned addprops = (unsigned) read_long(env);
+ unsigned callingconv = (unsigned) read_long(env);
+ mtp_additional_properties addprops = (mtp_additional_properties) read_long(env);
int nparams = (int) read_long(env);
int nresults = (int) read_long(env);
- int variaindex;
+ int variadicity;
type = new_type_method(nparams, nresults);
set_method_res_type(type, i, restype);
}
- variaindex = (int) read_long(env);
- if (variaindex != -1) {
- set_method_variadicity(type, variadicity_variadic);
- if (variaindex != nparams)
- set_method_first_variadic_param_index(type, variaindex);
- }
+ variadicity = (int) read_long(env);
+ set_method_variadicity(type, variadicity);
set_method_calling_convention(type, callingconv);
set_method_additional_properties(type, addprops);
}
case tpo_struct: {
- const char *name = read_quoted_string_null(env);
- ident *id = name != NULL ? new_id_from_str(name) : NULL;
+ ident *id = read_ident_null(env);
type = new_type_struct(id);
set_type_size_bytes(type, size);
break;
}
case tpo_union: {
- const char *name = read_quoted_string_null(env);
- ident *id = name != NULL ? new_id_from_str(name) : NULL;
-
+ ident *id = read_ident_null(env);
type = new_type_union(id);
set_type_size_bytes(type, size);
break;
}
set_type_alignment_bytes(type, align);
- set_type_visibility(type, vis);
type->flags = flags;
if (state == layout_fixed)
/** Reads an entity description and remembers it by its id. */
static void import_entity(io_env_t *env)
{
- long entnr = read_long(env);
- ident *name = read_ident(env);
- ident *ld_name = read_ident_null(env);
- long typenr = read_long(env);
- long ownertypenr = read_long(env);
+ long entnr = read_long(env);
+ ident *name = read_ident(env);
+ ident *ld_name = read_ident_null(env);
+ ir_visibility visibility = ir_visibility_default;
+ ir_linkage linkage = IR_LINKAGE_DEFAULT;
+ long typenr;
+ long ownertypenr;
+ const char *str;
+ ir_type *type;
+ ir_type *ownertype;
+ ir_entity *entity;
- ir_type *type = get_type(env, typenr);
- ir_type *ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
- ir_entity *entity = new_entity(ownertype, name, type);
+ skip_ws(env);
+ while (!isdigit(env->c)) {
+ char *str = read_word(env);
+ unsigned v;
+
+ skip_ws(env);
+
+ v = symbol(str, tt_visibility);
+ if (v != SYMERROR) {
+ visibility = (ir_visibility)v;
+ continue;
+ }
+ v = symbol(str, tt_linkage);
+ if (v != SYMERROR) {
+ linkage |= (ir_linkage)v;
+ continue;
+ }
+ printf("Parser error, expected visibility or linkage, got '%s'\n",
+ str);
+ break;
+ }
+
+ typenr = read_long(env);
+ ownertypenr = read_long(env);
+
+ type = get_type(env, typenr);
+ ownertype = !ownertypenr ? get_glob_type() : get_type(env, ownertypenr);
+ entity = new_entity(ownertype, name, type);
if (ld_name != NULL)
set_entity_ld_ident(entity, ld_name);
- set_entity_offset (entity, (int) read_long(env));
+ set_entity_offset(entity, (int) read_long(env));
set_entity_offset_bits_remainder(entity, (unsigned char) read_long(env));
set_entity_compiler_generated(entity, (int) read_long(env));
- set_entity_allocation (entity, read_allocation(env));
- set_entity_visibility (entity, read_visibility(env));
- set_entity_variability(entity, read_variability(env));
- set_entity_peculiarity(entity, read_peculiarity(env));
- set_entity_volatility (entity, read_volatility(env));
-
- if (get_entity_variability(entity) != variability_uninitialized &&
- get_entity_visibility(entity) != visibility_external_allocated) {
-
- if (is_compound_entity(entity)) {
- char *str = read_word(env);
- if (strcmp(str, "initializer") == 0) {
- set_entity_initializer(entity, read_initializer(env));
- } else if (strcmp(str, "noninitializer") == 0) {
- int n = (int) read_long(env);
- int i;
- for (i = 0; i < n; i++) {
- ir_entity *member = get_entity(env, read_long(env));
- ir_node *irn = get_node_or_dummy(env, read_long(env));
- add_compound_ent_value(entity, irn, member);
- }
- } else {
- parse_error(env, "expected 'initializer' or 'noninitializer', got '%s'\n", str);
- exit(1);
- }
- } else {
- ir_node *irn = get_node_or_dummy(env, read_long(env));
- set_atomic_ent_value(entity, irn);
+ set_entity_volatility(entity, read_volatility(env));
+ set_entity_visibility(entity, visibility);
+ set_entity_linkage(entity, linkage);
+
+ str = read_word(env);
+ if (strcmp(str, "initializer") == 0) {
+ set_entity_initializer(entity, read_initializer(env));
+ } else if (strcmp(str, "compoundgraph") == 0) {
+ int n = (int) read_long(env);
+ int i;
+ for (i = 0; i < n; i++) {
+ ir_entity *member = get_entity(env, read_long(env));
+ ir_node *irn = get_node_or_dummy(env, read_long(env));
+ add_compound_ent_value(entity, irn, member);
}
+ } else if (strcmp(str, "none") == 0) {
+ /* do nothing */
+ } else {
+ parse_error(env, "expected 'initializer', 'compoundgraph' or 'none' got '%s'\n", str);
+ exit(1);
}
set_id(env, entnr, entity);
/** Parses the whole type graph. */
static int parse_typegraph(io_env_t *env)
{
+ ir_graph *old_irg = env->irg;
keyword_t kwkind;
EXPECT('{');
- current_ir_graph = get_const_code_irg();
+ env->irg = get_const_code_irg();
/* parse all types first */
while (true) {
break;
}
}
+ env->irg = old_irg;
return 1;
}
-static int read_node_header(io_env_t *env, long *nodenr, long **preds,
+static int read_node_header(io_env_t *env, long *nodenr, ir_node ***preds,
const char **nodename)
{
int numpreds;
*nodename = read_word(env);
*nodenr = read_long(env);
- ARR_RESIZE(ir_node *, *preds, 0);
+ ARR_RESIZE(ir_node*, *preds, 0);
- EXPECT('[');
- for (numpreds = 0; !feof(env->file); numpreds++) {
- long val;
-
- skip_ws(env);
- if (env->c == ']') {
- read_c(env);
- break;
- }
- val = read_long(env);
- ARR_APP1(long, *preds, val);
+ expect_list_begin(env);
+ for (numpreds = 0; list_has_next(env); numpreds++) {
+ long val = read_long(env);
+ ir_node *pred = get_node_or_dummy(env, val);
+ ARR_APP1(ir_node*, *preds, pred);
}
return numpreds;
}
+static ir_node *read_ASM(io_env_t *env, int numpreds, ir_node **preds)
+{
+ ir_node *newnode;
+ ir_asm_constraint *input_constraints = NEW_ARR_F(ir_asm_constraint, 0);
+ ir_asm_constraint *output_constraints = NEW_ARR_F(ir_asm_constraint, 0);
+ ident **clobbers = NEW_ARR_F(ident*, 0);
+
+ ident *asm_text = read_ident(env);
+
+ expect_list_begin(env);
+ while (list_has_next(env)) {
+ ir_asm_constraint constraint;
+ constraint.pos = read_unsigned(env);
+ constraint.constraint = read_ident(env);
+ constraint.mode = read_mode(env);
+ ARR_APP1(ir_asm_constraint, input_constraints, constraint);
+ }
+
+ expect_list_begin(env);
+ while (list_has_next(env)) {
+ ir_asm_constraint constraint;
+ constraint.pos = read_unsigned(env);
+ constraint.constraint = read_ident(env);
+ constraint.mode = read_mode(env);
+ ARR_APP1(ir_asm_constraint, output_constraints, constraint);
+ }
+
+ expect_list_begin(env);
+ while (list_has_next(env)) {
+ ident *clobber = read_ident(env);
+ ARR_APP1(ident*, clobbers, clobber);
+ }
+
+ assert(ARR_LEN(input_constraints) == (size_t)numpreds-1);
+
+ newnode = new_r_ASM(preds[0], numpreds-1, preds+1,
+ input_constraints,
+ ARR_LEN(output_constraints),
+ output_constraints,
+ ARR_LEN(clobbers),
+ clobbers,
+ asm_text);
+ DEL_ARR_F(clobbers);
+ DEL_ARR_F(output_constraints);
+ DEL_ARR_F(input_constraints);
+ return newnode;
+}
+
/** Parses an IRG. */
static int parse_graph(io_env_t *env, ir_graph *irg)
{
- long *preds = NEW_ARR_F(long, 16);
- ir_node **prednodes = NEW_ARR_F(ir_node *, 16);
+ ir_node **preds = NEW_ARR_F(ir_node*,0);
int i, numpreds, ret = 1;
long nodenr;
const char *nodename;
ir_node *node, *newnode;
- current_ir_graph = irg;
+ env->irg = irg;
EXPECT('{');
}
numpreds = read_node_header(env, &nodenr, &preds, &nodename);
- if (numpreds == -1) break; /* end-of-graph */
- if (!numpreds) {
- parse_error(env, "node %s %ld is missing predecessors!",
- nodename, nodenr);
- ret = 0;
- break;
- }
-
- ARR_RESIZE(ir_node *, prednodes, numpreds);
- for (i = 0; i < numpreds - 1; i++)
- prednodes[i] = get_node_or_dummy(env, preds[i + 1]);
node = get_node_or_null(env, nodenr);
newnode = NULL;
EXPECT('{');
switch (symbol(nodename, tt_iro)) {
- case iro_End:
- {
- ir_node *newendblock = get_node(env, preds[0]);
- newnode = get_irg_end(current_ir_graph);
+ case iro_End: {
+ ir_node *newendblock = preds[0];
+ newnode = get_irg_end(irg);
exchange(get_nodes_block(newnode), newendblock);
- for (i = 0; i < numpreds - 1; i++)
- add_irn_n(newnode, prednodes[i]);
+ for (i = 1; i < numpreds; i++)
+ add_irn_n(newnode, preds[i]);
break;
}
- case iro_Start:
- {
- ir_node *newstartblock = get_node(env, preds[0]);
- newnode = get_irg_start(current_ir_graph);
+ case iro_Start: {
+ ir_node *newstartblock = preds[0];
+ newnode = get_irg_start(irg);
exchange(get_nodes_block(newnode), newstartblock);
break;
}
case iro_Block:
- {
- if (preds[0] != nodenr) {
- parse_error(env, "invalid block: preds[0] != nodenr (%ld != %ld)\n",
- preds[0], nodenr);
- ret = 0;
- goto endloop;
- }
-
- newnode = new_Block(numpreds - 1, prednodes);
+ newnode = new_r_Block(irg, numpreds, preds);
break;
- }
case iro_Anchor:
- newnode = current_ir_graph->anchor;
- for (i = 0; i < numpreds - 1; i++)
- set_irn_n(newnode, i, prednodes[i]);
- set_irn_n(newnode, -1, get_node(env, preds[0]));
+ newnode = irg->anchor;
+ for (i = 1; i < numpreds; i++)
+ set_irn_n(newnode, i-1, preds[i]);
+ set_nodes_block(newnode, preds[0]);
break;
- case iro_SymConst:
- {
+ case iro_SymConst: {
long entnr = read_long(env);
union symconst_symbol sym;
sym.entity_p = get_entity(env, entnr);
- newnode = new_SymConst(mode_P, sym, symconst_addr_ent);
+ newnode = new_r_SymConst(irg, mode_P, sym, symconst_addr_ent);
break;
}
+ case iro_Proj: {
+ ir_mode *mode = read_mode(env);
+ long pn = read_long(env);
+ newnode = new_r_Proj(preds[1], mode, pn);
+ /* explicitely set block, since preds[1] might be a dummy node
+ * which is always in the startblock */
+ set_nodes_block(newnode, preds[0]);
+ break;
+ }
+
+ case iro_ASM:
+ newnode = read_ASM(env, numpreds, preds);
+ break;
+
#include "gen_irio_import.inl"
default:
set_id(env, nodenr, newnode);
}
-endloop:
DEL_ARR_F(preds);
- DEL_ARR_F(prednodes);
return ret;
}
kwkind = (keyword_t) read_enum(env, tt_keyword);
switch (kwkind) {
case kw_mode: {
- const char *name = read_quoted_string(env);
- ir_mode_sort sort = read_enum(env, tt_mode_sort);
+ const char *name = read_string(env);
+ ir_mode_sort sort = (ir_mode_sort)read_enum(env, tt_mode_sort);
int size = read_long(env);
int sign = read_long(env);
ir_mode_arithmetic arith = read_mode_arithmetic(env);
firm_verification_t oldver = get_node_verification_mode();
io_env_t ioenv;
io_env_t *env = &ioenv;
- int i, n;
+ size_t i, n;
symtbl_init();
if (env->c == EOF)
break;
- kw = read_enum(env, tt_keyword);
+ kw = (keyword_t)read_enum(env, tt_keyword);
switch (kw) {
case kw_modes:
if (!parse_modes(env)) goto end;