/*
- * 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"
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_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, "%u ", 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);
}
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:
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));
}
/* visibility + linkage */
fputs("initializer ", env->file);
write_initializer(env, get_entity_initializer(ent));
} else if (entity_has_compound_ent_values(ent)) {
- int i, n = get_compound_ent_n_values(ent);
+ size_t i, n = get_compound_ent_n_values(ent);
fputs("compoundgraph ", env->file);
- fprintf(env->file, "%d ", n);
+ 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);
}
}
+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);
if (!is_Block(irn)) {
- fprintf(env->file, "%ld ", get_irn_node_nr(get_nodes_block(irn)));
+ ir_node *block = get_nodes_block(irn);
+ write_long(env, get_irn_node_nr(block));
}
for (i = 0; i < n; i++) {
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) {
case iro_Start:
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);
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 %u %d %s %u %u ",
- 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 = 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;
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 (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;
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);
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++) {
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);
}
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)
type = get_glob_type();
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;
ARR_RESIZE(ir_node*, *preds, 0);
- EXPECT('[');
- for (numpreds = 0; !feof(env->file); numpreds++) {
- long val;
- ir_node *pred;
-
- skip_ws(env);
- if (env->c == ']') {
- read_c(env);
- break;
- }
- val = read_long(env);
- pred = get_node_or_dummy(env, 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)
{
break;
}
+ case iro_ASM:
+ newnode = read_ASM(env, numpreds, preds);
+ break;
+
#include "gen_irio_import.inl"
default:
kwkind = (keyword_t) read_enum(env, tt_keyword);
switch (kwkind) {
case kw_mode: {
- const char *name = read_quoted_string(env);
+ 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);
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();