/* This implementation assumes:
* - target has IEEE-754 floating-point arithmetic. */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+
#include <assert.h> /* assertions */
#include <stdlib.h> /* atoi() */
#include <string.h> /* nice things for strings */
+#ifdef HAVE_STRINGS_H
#include <strings.h> /* strings.h also includes bsd only function strcasecmp */
+#endif
#include <stdlib.h>
-#include <alloca.h>
+#ifdef HAVE_ALLOCA_H
+# include <alloca.h>
+#endif
+#ifdef HAVE_MALLOC_H
+# include <malloc.h>
+#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"
/** 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
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 ========================================
ANNOUNCE();
assert(mode);
+ if (get_mode_n_vector_elems(mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch(get_mode_sort(mode))
{
case irms_reference:
ANNOUNCE();
assert(mode);
+ if (get_mode_n_vector_elems(mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch(get_mode_sort(mode))
{
case irms_reference:
ANNOUNCE();
assert(mode);
+ if (get_mode_n_vector_elems(mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch(get_mode_sort(mode))
{
case irms_control_flow:
ANNOUNCE();
assert(mode);
+ if (get_mode_n_vector_elems(mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch(get_mode_sort(mode))
{
case irms_control_flow:
ANNOUNCE();
assert(mode);
+ if (get_mode_n_vector_elems(mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
if (get_mode_sort(mode) == irms_float_number) {
switch(get_mode_size_bits(mode))
{
ANNOUNCE();
assert(mode);
+ if (get_mode_n_vector_elems(mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
if (get_mode_sort(mode) == irms_float_number) {
switch(get_mode_size_bits(mode))
{
ANNOUNCE();
assert(a);
+ 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;
+ }
+
switch (get_mode_sort(a->mode))
{
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;
return a == get_tarval_null(m);
}
+/*
+ * test if one, 1 means 'yes'
+ */
+int tarval_is_one(tarval *a)
+{
+ ir_mode *m = get_tarval_mode(a);
+
+ return a == get_tarval_one(m);
+}
+
/*
* comparison
*/
if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
if (a == tarval_undefined || b == tarval_undefined) return False;
if (a == b) return Eq;
- if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
+ if (a->mode != b->mode) return False;
+
+ if (get_mode_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ assert(0 && "cmp not implemented for vector modes");
+ }
/* Here the two tarvals are unequal and of the same mode */
switch (get_mode_sort(a->mode))
if (src->mode == m) return src;
+ if (get_mode_n_vector_elems(src->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch (get_mode_sort(src->mode))
{
case irms_control_flow:
/* 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;
+ 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;
+ break;
default:
/* the rest can't be converted */
assert(a);
assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
+ /* works for vector mode without changes */
+
switch (get_mode_sort(a->mode))
{
case irms_int_number:
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_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch (get_mode_sort(a->mode))
{
case irms_int_number:
assert(b);
assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
+ 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;
+ }
+
switch (get_mode_sort(a->mode))
{
case irms_character:
assert(b);
assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
+ 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;
+ }
switch (get_mode_sort(a->mode))
{
case irms_character:
assert(b);
assert((a->mode == b->mode) && mode_is_num(a->mode));
+ if (get_mode_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch (get_mode_sort(a->mode))
{
case irms_int_number:
assert(b);
assert((a->mode == b->mode) && mode_is_float(a->mode));
+ if (get_mode_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
fc_div(a->value, b->value, NULL);
return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
}
assert(b);
assert((a->mode == b->mode) && mode_is_int(a->mode));
+ if (get_mode_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
/* x/0 error */
if (b == get_mode_null(b->mode)) return tarval_bad;
/* modes of a,b are equal */
assert(b);
assert((a->mode == b->mode) && mode_is_int(a->mode));
+ if (get_mode_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
/* x/0 error */
if (b == get_mode_null(b->mode)) return tarval_bad;
/* modes of a,b are equal */
assert(a);
assert(mode_is_num(a->mode));
+ if (get_mode_n_vector_elems(a->mode) > 1) {
+ /* vector arithmetic not implemented yet */
+ return tarval_bad;
+ }
+
switch (get_mode_sort(a->mode))
{
case irms_int_number:
assert(b);
assert(a->mode == b->mode);
+ /* works even for vector modes */
+
switch(get_mode_sort(a->mode))
{
case irms_internal_boolean:
assert(b);
assert(a->mode == b->mode);
+ /* works even for vector modes */
+
switch (get_mode_sort(a->mode))
{
case irms_internal_boolean:
assert(b);
assert((a->mode == b->mode));
+ /* works even for vector modes */
+
switch (get_mode_sort(a->mode))
{
case irms_internal_boolean:
*/
tarval *tarval_shl(tarval *a, tarval *b)
{
+ char *temp_val = NULL;
ANNOUNCE();
assert(a);
assert(b);
assert(mode_is_int(a->mode) && mode_is_int(b->mode));
- sc_shl(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ 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;
+ }
+
+ if (get_mode_modulo_shift(a->mode) != 0)
+ {
+ temp_val = alloca(sc_get_buffer_length());
+
+ sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
+ sc_mod(b->value, temp_val, temp_val);
+ }
+ else
+ temp_val = (char*)b->value;
+
+ sc_shl(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);
}
*/
tarval *tarval_shr(tarval *a, tarval *b)
{
+ char *temp_val = NULL;
ANNOUNCE();
assert(a);
assert(b);
assert(mode_is_int(a->mode) && mode_is_int(b->mode));
- sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ 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;
+ }
+
+ if (get_mode_modulo_shift(a->mode) != 0)
+ {
+ temp_val = alloca(sc_get_buffer_length());
+
+ sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
+ sc_mod(b->value, temp_val, temp_val);
+ }
+ else
+ temp_val = (char*)b->value;
+
+ 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);
}
*/
tarval *tarval_shrs(tarval *a, tarval *b)
{
+ char *temp_val = NULL;
ANNOUNCE();
assert(a);
assert(b);
assert(mode_is_int(a->mode) && mode_is_int(b->mode));
- sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ 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;
+ }
+
+ if (get_mode_modulo_shift(a->mode) != 0)
+ {
+ temp_val = alloca(sc_get_buffer_length());
+
+ sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
+ sc_mod(b->value, temp_val, temp_val);
+ }
+ else
+ temp_val = (char*)b->value;
+
+ 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);
}
*/
tarval *tarval_rot(tarval *a, tarval *b)
{
+ char *temp_val = NULL;
ANNOUNCE();
assert(a);
assert(b);
assert(mode_is_int(a->mode) && mode_is_int(b->mode));
- sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
+ 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;
+ }
+
+ if (get_mode_modulo_shift(a->mode) != 0)
+ {
+ temp_val = alloca(sc_get_buffer_length());
+
+ sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
+ sc_mod(b->value, temp_val, temp_val);
+ }
+ else
+ temp_val = (char*)b->value;
+
+ 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);
}
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_existent)
- 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) {
+ 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, size-1);
- buf[size-1] = '\0';
+ memcpy(buf, tv->value, len-1);
+ buf[len-1] = '\0';
}
- return tv->length;
- }
+ return tv->length;
+ }
else
- return snprintf(buf, len, "void");
+ return snprintf(buf, len, "void");
case irms_internal_boolean:
switch (mode_info->mode_output) {
}
-char *tarval_bitpattern(tarval *tv)
+char *get_tarval_bitpattern(tarval *tv)
{
return NULL;
}
/*
* 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);
return mode->tv_priv;
}
-
-/* Identifying some tarvals ??? */
-/* Implemented in old tv.c as such:
- * return 0 for additive neutral,
- * 1 for multiplicative neutral,
- * -1 for bitwise-and neutral
- * 2 else
+/*
+ * Identifying tarvals values for algebraic simplifications.
*
- * Implemented for compatibility */
-long tarval_classify(tarval *tv)
+ * Returns:
+ * - TV_CLASSIFY_NULL for additive neutral,
+ * - TV_CLASSIFY_ONE for multiplicative neutral,
+ * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
+ * - TV_CLASSIFY_OTHER else
+ */
+tarval_classification_t classify_tarval(tarval *tv)
{
ANNOUNCE();
- if (!tv || tv == tarval_bad) return 2;
+ if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
- if (tv == get_mode_null(tv->mode)) return 0;
- else if (tv == get_mode_one(tv->mode)) return 1;
+ if (tv == get_mode_null(tv->mode))
+ return TV_CLASSIFY_NULL;
+ else if (tv == get_mode_one(tv->mode))
+ return TV_CLASSIFY_ONE;
else if ((get_mode_sort(tv->mode) == irms_int_number)
- && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
+ && (tv == new_tarval_from_long(-1, tv->mode)))
+ return TV_CLASSIFY_ALL_ONE;
- return 2;
+ return TV_CLASSIFY_OTHER;
}
/**
* 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. */
+void finish_tarval(void) {
+ finish_strcalc ();
+ finish_fltcalc ();
+ del_set(tarvals); tarvals = NULL;
+ del_set(values); values = NULL;
}
/****************************************************************************