X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fstat%2Fpattern.c;h=c21555679bceac39626108976acb92d6b9cb49d4;hb=3c3425a50a1d721b74a015c6812257e32feeac85;hp=852f721ab5f360e95bfbe50c2fe02fd957393d31;hpb=438580abed840c55e82a03b47daeacfe2da8040f;p=libfirm diff --git a/ir/stat/pattern.c b/ir/stat/pattern.c index 852f721ab..c21555679 100644 --- a/ir/stat/pattern.c +++ b/ir/stat/pattern.c @@ -21,7 +21,6 @@ * @file * @brief Statistics for Firm. Pattern history. * @author Michael Beck - * @version $Id$ */ #include "config.h" @@ -184,7 +183,7 @@ static inline void put_byte(CODE_BUFFER *buf, BYTE byte) buf->hash = (buf->hash * 9) ^ byte; } else { buf->overrun = 1; - } /* if */ + } } /* put_byte */ /** @@ -398,10 +397,10 @@ static int addr_cmp(const void *p1, const void *p2, size_t size) */ static size_t find_mode_index(const ir_mode *mode) { - size_t i, n = get_irp_n_modes(); + size_t i, n = ir_get_n_modes(); for (i = 0; i < n; ++i) { - if (get_irp_mode(i) == mode) + if (ir_get_mode(i) == mode) return i; } /* should really not happen */ @@ -427,7 +426,7 @@ static int _encode_node(ir_node *node, int max_depth, codec_env_t *env) entry.addr = node; entry.id = env->curr_id; - s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), HASH_PTR(node)); + s_entry = set_hinsert(env->id_set, &entry, sizeof(entry), hash_ptr(node)); r_entry = (addr_entry_t *)s_entry->dptr; if (r_entry->id != env->curr_id) { @@ -746,8 +745,7 @@ static void calc_nodes_pattern(ir_node *node, void *ctx) static void store_pattern(const char *fname) { FILE *f; - pattern_entry_t *entry; - size_t i, count = pset_count(status->pattern_hash); + size_t count = pset_count(status->pattern_hash); if (count <= 0) return; @@ -761,9 +759,7 @@ static void store_pattern(const char *fname) fwrite("FPS1", 4, 1, f); fwrite(&count, sizeof(count), 1, f); - for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash); - entry && i < count; - entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) { + foreach_pset(status->pattern_hash, pattern_entry_t, entry) { fwrite(entry, offsetof(pattern_entry_t, buf) + entry->len, 1, f); } /* for */ fclose(f); @@ -784,6 +780,7 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) HASH_MAP(pattern_entry_t) *pattern_hash = new_pset(pattern_cmp, 8); BYTE buffer[PATTERN_STORE_SIZE]; CODE_BUFFER buf; + int res; f = fopen(fname, "rb"); if (! f) { @@ -791,19 +788,20 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) return NULL; } /* if */ - fread(magic, 4, 1, f); + res = fread(magic, 4, 1, f); + if (res != 1) + goto read_error; count = 0; - fread(&count, sizeof(count), 1, f); - if (memcmp(magic, "FPS1", 4) != 0 || count <= 0) { - fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname); - fclose(f); - return NULL; - } /* if */ + res = fread(&count, sizeof(count), 1, f); + if (res != 1 || memcmp(magic, "FPS1", 4) != 0 || count <= 0) + goto read_error; /* read all pattern entries and put them into the hash table. */ for (i = 0; i < count; ++i) { init_buf(&buf, buffer, sizeof(buffer)); - fread(&tmp, offsetof(pattern_entry_t, buf), 1, f); + res = fread(&tmp, offsetof(pattern_entry_t, buf), 1, f); + if (res != 1) + goto read_error; for (j = 0; j < tmp.len; ++j) put_byte(&buf, fgetc(f)); entry = pattern_get_entry(&buf, pattern_hash); @@ -815,6 +813,11 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) assert(pset_count(pattern_hash) == count); return pattern_hash; + +read_error: + fprintf(stderr, "Error: %s is not a Firm pattern store. Ignored.\n", fname); + fclose(f); + return NULL; } /* read_pattern */ /** @@ -824,7 +827,6 @@ static HASH_MAP(pattern_entry_t) *read_pattern(const char *fname) */ static void pattern_output(const char *fname) { - pattern_entry_t *entry; pattern_entry_t **pattern_arr; pattern_dumper_t *dump; size_t i, count = pset_count(status->pattern_hash); @@ -838,10 +840,9 @@ static void pattern_output(const char *fname) dump = new_vcg_dumper(fname, 100); pattern_arr = XMALLOCN(pattern_entry_t*, count); - for (i = 0, entry = (pattern_entry_t*)pset_first(status->pattern_hash); - entry && i < count; - entry = (pattern_entry_t*)pset_next(status->pattern_hash), ++i) { - pattern_arr[i] = entry; + i = 0; + foreach_pset(status->pattern_hash, pattern_entry_t, entry) { + pattern_arr[i++] = entry; } /* for */ assert(count == i); count = i; @@ -850,7 +851,7 @@ static void pattern_output(const char *fname) qsort(pattern_arr, count, sizeof(*pattern_arr), pattern_count_cmp); for (i = 0; i < count; ++i) { - entry = pattern_arr[i]; + pattern_entry_t *const entry = pattern_arr[i]; if (cnt_to_uint(&entry->count) < status->bound) continue;