X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Ftv%2Ftv.c;h=7af7434e5bafb849e7a75004bef963059cf799d6;hb=f73f5c542edb2e4ff58a4bdc4f9d53415a14c4a1;hp=330b941d4b62748f8ac8a579f715b83dd7466912;hpb=b8f363d7be29eb5702e130be3796ff7162410e6a;p=libfirm diff --git a/ir/tv/tv.c b/ir/tv/tv.c index 330b941d4..7af7434e5 100644 --- a/ir/tv/tv.c +++ b/ir/tv/tv.c @@ -28,7 +28,9 @@ #include /* assertions */ #include /* atoi() */ -#include /* nice things for strings */ +#ifdef HAVE_STRING_H +# include /* nice things for strings */ +#endif #ifdef HAVE_STRINGS_H #include /* strings.h also includes bsd only function strcasecmp */ #endif @@ -44,10 +46,8 @@ #include "set.h" /* to store tarvals in */ /* #include "tune.h" */ /* some constants */ #include "entity_t.h" /* needed to store pointers to entities */ -#include "irmode.h" /* defines modes etc */ #include "irmode_t.h" #include "irnode.h" /* defines boolean return values (pnc_number)*/ -#include "host.h" #include "strcalc.h" #include "fltcalc.h" @@ -55,11 +55,8 @@ target values */ #define N_CONSTANTS 2048 -/* XXX hack until theres's a proper interface */ -#define BAD 1 -#define SATURATE 2 -#define WRAP 3 -#define GET_OVERFLOW_MODE() BAD +/* get the integer overflow mode */ +#define GET_OVERFLOW_MODE() int_overflow_mode /* unused, float to int doesn't work yet */ #define TRUNCATE 1 @@ -96,6 +93,7 @@ static long long count = 0; ****************************************************************************/ static struct set *tarvals; /* container for tarval structs */ static struct set *values; /* container for values */ +static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP; /**************************************************************************** * private functions @@ -171,6 +169,9 @@ static tarval *get_tarval(const void *value, int length, ir_mode *mode) return (tarval *)INSERT_TARVAL(&tv); } +/** + * handle overflow + */ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode) { switch (get_mode_sort(mode)) @@ -178,9 +179,9 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode) case irms_int_number: if (sc_comp(value, get_mode_max(mode)->value) == 1) { switch (GET_OVERFLOW_MODE()) { - case SATURATE: + case TV_OVERFLOW_SATURATE: return get_mode_max(mode); - case WRAP: + case TV_OVERFLOW_WRAP: { char *temp = alloca(sc_get_buffer_length()); char *diff = alloca(sc_get_buffer_length()); @@ -192,7 +193,7 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode) sc_sub(temp, diff, temp); return get_tarval(temp, length, mode); } - case BAD: + case TV_OVERFLOW_BAD: return tarval_bad; default: return get_tarval(value, length, mode); @@ -200,9 +201,9 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode) } if (sc_comp(value, get_mode_min(mode)->value) == -1) { switch (GET_OVERFLOW_MODE()) { - case SATURATE: + case TV_OVERFLOW_SATURATE: return get_mode_min(mode); - case WRAP: + case TV_OVERFLOW_WRAP: { char *temp = alloca(sc_get_buffer_length()); char *diff = alloca(sc_get_buffer_length()); @@ -214,7 +215,7 @@ static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode) sc_add(temp, diff, temp); return get_tarval(temp, length, mode); } - case BAD: + case TV_OVERFLOW_BAD: return tarval_bad; default: return get_tarval(value, length, mode); @@ -359,7 +360,7 @@ int tarval_is_long(tarval *tv) } /* this might overflow the machine's long, so use only with small values */ -long tarval_to_long(tarval* tv) +long get_tarval_long(tarval* tv) { ANNOUNCE(); assert(tarval_is_long(tv) && "tarval too big to fit in long"); @@ -395,7 +396,7 @@ int tarval_is_double(tarval *tv) return (get_mode_sort(tv->mode) == irms_float_number); } -long double tarval_to_double(tarval *tv) +long double get_tarval_double(tarval *tv) { ANNOUNCE(); assert(tarval_is_double(tv)); @@ -403,56 +404,6 @@ long double tarval_to_double(tarval *tv) return fc_val_to_float(tv->value); } -/* The tarval represents the address of the entity. As the address must - be constant the entity must have as owner the global type. - * We no more support this function: Use the new SymConst instead. - */ -tarval *new_tarval_from_entity (entity *ent, ir_mode *mode) -{ - ANNOUNCE(); - assert(ent); - assert(mode && (get_mode_sort(mode) == irms_reference)); - - return get_tarval((void *)ent, 0, mode); -} - - -int tarval_is_entity(tarval *tv) -{ - ANNOUNCE(); - assert(tv); - /* tv->value == NULL means dereferencing a null pointer */ - return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0) - && (tv != tarval_P_void)); -} - -entity *tarval_to_entity(tarval *tv) -{ - ANNOUNCE(); - assert(tv); - - if (tarval_is_entity(tv)) - return (entity *)tv->value; - else { - assert(0 && "tarval did not represent an entity"); - return NULL; - } -} - -void free_tarval_entity(entity *ent) { - /* There can be a tarval referencing this entity. Even if the - tarval is not used by the code any more, it can still reference - the entity as tarvals live indepently of the entity referenced. - Further the tarval is hashed into a set. If a hash function - evaluation happens to collide with this tarval, we will vrfy that - it contains a proper entity and we will crash if the entity is - freed. - - Unluckily, tarvals can neither be changed nor deleted, and to find - one, all existing reference modes have to be tried -> a facility - to retrieve all modes of a kind is needed. */ - ANNOUNCE(); -} /* * Access routines for tarval fields ======================================== @@ -464,6 +415,15 @@ ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */ return tv->mode; } +/* +void *get_tarval_link (tarval *tv) +{ + ANNOUNCE (); + assert (tv); + return (tv->link); +} +*/ + /* * Special value query functions ============================================ * @@ -505,7 +465,7 @@ tarval *get_tarval_max(ir_mode *mode) ANNOUNCE(); assert(mode); - if (get_mode_vector_elems(mode) > 1) { + if (get_mode_n_vector_elems(mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -550,7 +510,7 @@ tarval *get_tarval_min(ir_mode *mode) ANNOUNCE(); assert(mode); - if (get_mode_vector_elems(mode) > 1) { + if (get_mode_n_vector_elems(mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -595,7 +555,7 @@ tarval *get_tarval_null(ir_mode *mode) ANNOUNCE(); assert(mode); - if (get_mode_vector_elems(mode) > 1) { + if (get_mode_n_vector_elems(mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -627,7 +587,7 @@ tarval *get_tarval_one(ir_mode *mode) ANNOUNCE(); assert(mode); - if (get_mode_vector_elems(mode) > 1) { + if (get_mode_n_vector_elems(mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -658,7 +618,7 @@ tarval *get_tarval_nan(ir_mode *mode) ANNOUNCE(); assert(mode); - if (get_mode_vector_elems(mode) > 1) { + if (get_mode_n_vector_elems(mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -689,7 +649,7 @@ tarval *get_tarval_inf(ir_mode *mode) ANNOUNCE(); assert(mode); - if (get_mode_vector_elems(mode) > 1) { + if (get_mode_n_vector_elems(mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -727,7 +687,7 @@ int tarval_is_negative(tarval *a) ANNOUNCE(); assert(a); - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ assert(0 && "tarval_is_negative is not allowed for vector modes"); return 0; @@ -783,7 +743,7 @@ pnc_number tarval_cmp(tarval *a, tarval *b) if (a == b) return Eq; if (a->mode != b->mode) return False; - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ assert(0 && "cmp not implemented for vector modes"); } @@ -828,7 +788,7 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m) if (src->mode == m) return src; - if (get_mode_vector_elems(src->mode) > 1) { + if (get_mode_n_vector_elems(src->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -843,7 +803,7 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m) /* cast float to something */ case irms_float_number: switch (get_mode_sort(m)) { - case irms_float_number: + case irms_float_number: switch (get_mode_size_bits(m)) { case 32: @@ -859,7 +819,6 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m) break; } return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m); - break; case irms_int_number: switch (GET_FLOAT_TO_INT_MODE()) @@ -878,7 +837,6 @@ tarval *tarval_convert_to(tarval *src, ir_mode *m) * an intermediate representation is needed here first. */ /* return get_tarval(); */ return tarval_bad; - break; default: /* the rest can't be converted */ @@ -982,9 +940,10 @@ tarval *tarval_neg(tarval *a) ANNOUNCE(); assert(a); assert(mode_is_num(a->mode)); /* negation only for numerical values */ - assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */ - if (get_mode_vector_elems(a->mode) > 1) { + /* note: negation is allowed even for unsigned modes. */ + + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1015,9 +974,9 @@ tarval *tarval_add(tarval *a, tarval *b) ANNOUNCE(); assert(a); assert(b); - assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode))); + assert(a->mode == b->mode); - if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1050,9 +1009,9 @@ tarval *tarval_sub(tarval *a, tarval *b) ANNOUNCE(); assert(a); assert(b); - assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode))); + assert(a->mode == b->mode); - if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1084,9 +1043,9 @@ tarval *tarval_mul(tarval *a, tarval *b) ANNOUNCE(); assert(a); assert(b); - assert((a->mode == b->mode) && mode_is_num(a->mode)); + assert(a->mode == b->mode); - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1118,7 +1077,7 @@ tarval *tarval_quo(tarval *a, tarval *b) assert(b); assert((a->mode == b->mode) && mode_is_float(a->mode)); - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1138,7 +1097,7 @@ tarval *tarval_div(tarval *a, tarval *b) assert(b); assert((a->mode == b->mode) && mode_is_int(a->mode)); - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1161,7 +1120,7 @@ tarval *tarval_mod(tarval *a, tarval *b) assert(b); assert((a->mode == b->mode) && mode_is_int(a->mode)); - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1184,7 +1143,7 @@ tarval *tarval_abs(tarval *a) assert(a); assert(mode_is_num(a->mode)); - if (get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1306,7 +1265,7 @@ tarval *tarval_shl(tarval *a, tarval *b) assert(b); assert(mode_is_int(a->mode) && mode_is_int(b->mode)); - if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1336,7 +1295,7 @@ tarval *tarval_shr(tarval *a, tarval *b) assert(b); assert(mode_is_int(a->mode) && mode_is_int(b->mode)); - if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1366,7 +1325,7 @@ tarval *tarval_shrs(tarval *a, tarval *b) assert(b); assert(mode_is_int(a->mode) && mode_is_int(b->mode)); - if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1396,7 +1355,7 @@ tarval *tarval_rot(tarval *a, tarval *b) assert(b); assert(mode_is_int(a->mode) && mode_is_int(b->mode)); - if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) { + if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) { /* vector arithmetic not implemented yet */ return tarval_bad; } @@ -1415,6 +1374,13 @@ tarval *tarval_rot(tarval *a, tarval *b) return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode); } +/* + * carry flag of the last operation + */ +int tarval_carry(void) +{ + return sc_had_carry(); +} /* * Output of tarvals @@ -1475,31 +1441,20 @@ int tarval_snprintf(char *buf, size_t len, tarval *tv) case irms_reference: if (tv == tarval_P_void) return snprintf(buf, len, "NULL"); - if (tv->value != NULL) - if (tarval_is_entity(tv)) { - if (get_entity_peculiarity((entity *)tv->value) != peculiarity_description) - return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix); - else { - if (mode_info->mode_output == TVO_NATIVE) - return snprintf(buf, len, "NULL"); - else - return snprintf(buf, len, "0"); - } - } - else { - if (len > tv->length) { - memcpy(buf, tv->value, tv->length); - buf[tv->length] = '\0'; - } - else { - /* truncated */ - memcpy(buf, tv->value, len-1); - buf[len-1] = '\0'; - } - return tv->length; - } + if (tv->value != NULL){ + if (len > tv->length) { + memcpy(buf, tv->value, tv->length); + buf[tv->length] = '\0'; + } + else { + /* truncated */ + memcpy(buf, tv->value, len-1); + buf[len-1] = '\0'; + } + return tv->length; + } else - return snprintf(buf, len, "void"); + return snprintf(buf, len, "void"); case irms_internal_boolean: switch (mode_info->mode_output) { @@ -1518,7 +1473,7 @@ int tarval_snprintf(char *buf, size_t len, tarval *tv) case irms_control_flow: case irms_memory: case irms_auxiliary: - return snprintf(buf, len, ""); + return snprintf(buf, len, ""); } return 0; @@ -1539,15 +1494,30 @@ int tarval_printf(tarval *tv) { } -char *tarval_bitpattern(tarval *tv) +char *get_tarval_bitpattern(tarval *tv) { - return NULL; + int i, j, pos = 0; + int n = get_mode_size_bits(tv->mode); + int bytes = (n + 7) / 8; + char *res = malloc((n + 1) * sizeof(char)); + unsigned char byte; + + for(i = 0; i < bytes; i++) { + byte = get_tarval_sub_bits(tv, i); + for(j = 1; j < 256; j <<= 1) + if(pos < n) + res[pos++] = j & byte ? '1' : '0'; + } + + res[n] = '\0'; + + return res; } /* * access to the bitpattern */ -unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs) +unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) { switch (get_mode_sort(tv->mode)) { case irms_int_number: @@ -1569,7 +1539,7 @@ unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs) * * Returns zero on success. */ -int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) +int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) { assert(mode); @@ -1582,7 +1552,7 @@ int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinf * * This functions returns the modinfo of a given mode. */ -const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode) +const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) { assert(mode); @@ -1598,7 +1568,7 @@ const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode) * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral * - TV_CLASSIFY_OTHER else */ -tarval_classification_t tarval_classify(tarval *tv) +tarval_classification_t classify_tarval(tarval *tv) { ANNOUNCE(); if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER; @@ -1614,6 +1584,20 @@ tarval_classification_t tarval_classify(tarval *tv) return TV_CLASSIFY_OTHER; } +/** + * Sets the overflow mode for integer operations. + */ +void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) { + int_overflow_mode = ov_mode; +} + +/** + * Get the overflow mode for integer operations. + */ +tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) { + return int_overflow_mode; +} + /** * default mode_info for output as HEX */ @@ -1666,17 +1650,17 @@ void init_tarval_2(void) * assign output modes that are compatible with the * old implementation: Hex output */ - tarval_set_mode_output_option(mode_U, &hex_output); - tarval_set_mode_output_option(mode_C, &hex_output); - tarval_set_mode_output_option(mode_Bs, &hex_output); - tarval_set_mode_output_option(mode_Bu, &hex_output); - tarval_set_mode_output_option(mode_Hs, &hex_output); - tarval_set_mode_output_option(mode_Hu, &hex_output); - tarval_set_mode_output_option(mode_Is, &hex_output); - tarval_set_mode_output_option(mode_Iu, &hex_output); - tarval_set_mode_output_option(mode_Ls, &hex_output); - tarval_set_mode_output_option(mode_Lu, &hex_output); - tarval_set_mode_output_option(mode_P, &reference_output); + set_tarval_mode_output_option(mode_U, &hex_output); + set_tarval_mode_output_option(mode_C, &hex_output); + set_tarval_mode_output_option(mode_Bs, &hex_output); + set_tarval_mode_output_option(mode_Bu, &hex_output); + set_tarval_mode_output_option(mode_Hs, &hex_output); + set_tarval_mode_output_option(mode_Hu, &hex_output); + set_tarval_mode_output_option(mode_Is, &hex_output); + set_tarval_mode_output_option(mode_Iu, &hex_output); + set_tarval_mode_output_option(mode_Ls, &hex_output); + set_tarval_mode_output_option(mode_Lu, &hex_output); + set_tarval_mode_output_option(mode_P, &reference_output); } /* free all memory occupied by tarval. */