#include <assert.h> /* assertions */
#include <stdlib.h> /* atoi() */
-#include <string.h> /* nice things for strings */
+#ifdef HAVE_STRING_H
+# include <string.h> /* nice things for strings */
+#endif
#ifdef HAVE_STRINGS_H
#include <strings.h> /* strings.h also includes bsd only function strcasecmp */
#endif
#include "tv_t.h"
#include "set.h" /* to store tarvals in */
-//#include "tune.h" /* some constants */
+/* #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"
/** Size of hash tables. Should correspond to average number of distinct constant
target values */
-#define N_CONSTANTS 2048
+#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
****************************************************************************/
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
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))
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());
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);
}
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());
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);
else if (strcasecmp(str, "false")) return tarval_b_true;
else
/* XXX This is C semantics */
- return atoi(str) ? tarval_b_true : tarval_b_false;
+ return atoi(str) ? tarval_b_true : tarval_b_false;
case irms_float_number:
switch(get_mode_size_bits(mode)) {
}
/* 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");
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));
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. */
-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 ========================================
return tv->mode;
}
+/*
+void *get_tarval_link (tarval *tv)
+{
+ ANNOUNCE ();
+ assert (tv);
+ return (tv->link);
+}
+*/
+
/*
* Special value query functions ============================================
*
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
case irms_int_number:
if (!mode_is_signed(a->mode)) return 0;
else
- return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
+ return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
case irms_float_number:
return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
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");
}
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;
}
/* 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:
break;
}
return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
- break;
case irms_int_number:
switch (GET_FLOAT_TO_INT_MODE())
* an intermediate representation is needed here first. */
/* return get_tarval(); */
return tarval_bad;
- break;
default:
/* the rest can't be converted */
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
else
temp_val = (char*)b->value;
- sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
}
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;
}
else
temp_val = (char*)b->value;
- sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
}
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;
}
else
temp_val = (char*)b->value;
- sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
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
case TVO_DECIMAL:
str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
- break;
+ break;
case TVO_OCTAL:
str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
- break;
+ break;
case TVO_HEX:
case TVO_NATIVE:
default:
str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
- break;
+ break;
}
return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
break;
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 (size > tv->length) {
- memcpy(buf, tv->value, tv->length);
- buf[tv->length] = '\0';
- }
- else {
- /* truncated */
- memcpy(buf, tv->value, size-1);
- buf[size-1] = '\0';
- }
- return tv->length;
- }
+ if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
+ 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) {
case irms_control_flow:
case irms_memory:
case irms_auxiliary:
- return snprintf(buf, len, "<BAD>");
+ return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
}
return 0;
}
-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:
*
* 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);
*
* 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);
* - 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;
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
*/
* 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. */