X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=ir%2Fir%2Firmode.c;h=f83214e1062db9b695ca15cfe8f2311ceb8a25da;hb=9baadfac02ee53ae9a7caa7593225a49ccd54753;hp=628ed42b9840244856f4194774f634a40c263b71;hpb=fde2a35e025782ab8caf44a16cdbc10449cc5812;p=libfirm diff --git a/ir/ir/irmode.c b/ir/ir/irmode.c index 628ed42b9..f83214e10 100644 --- a/ir/ir/irmode.c +++ b/ir/ir/irmode.c @@ -23,27 +23,22 @@ * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil * @version $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif +#include "config.h" -#ifdef HAVE_STDLIB_H -# include -#endif -#ifdef HAVE_STRING_H -# include -#endif +#include +#include -# include +#include -# include "irprog_t.h" -# include "irmode_t.h" -# include "ident.h" -# include "tv_t.h" -# include "obst.h" -# include "irhooks.h" -# include "irtools.h" -# include "array.h" +#include "irprog_t.h" +#include "irmode_t.h" +#include "ident.h" +#include "tv_t.h" +#include "obst.h" +#include "irhooks.h" +#include "irtools.h" +#include "array.h" +#include "error.h" /** Obstack to hold all modes. */ static struct obstack modes; @@ -60,7 +55,7 @@ static ir_mode **mode_list; * * TODO: Add other fields **/ -static INLINE int modes_are_equal(const ir_mode *m, const ir_mode *n) { +static inline int modes_are_equal(const ir_mode *m, const ir_mode *n) { if (m == n) return 1; if (m->sort == n->sort && m->arithmetic == n->arithmetic && @@ -264,8 +259,8 @@ static ir_mode *register_mode(const ir_mode *new_mode) { /* * Creates a new mode. */ -ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int sign, - mode_arithmetic arithmetic, unsigned int modulo_shift) +ir_mode *new_ir_mode(const char *name, ir_mode_sort sort, int bit_size, int sign, + ir_mode_arithmetic arithmetic, unsigned int modulo_shift) { ir_mode mode_tmpl; ir_mode *mode = NULL; @@ -292,22 +287,23 @@ ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int sign, case irms_control_flow: case irms_memory: case irms_internal_boolean: - assert(0 && "internal modes cannot be user defined"); - break; + panic("internal modes cannot be user defined"); case irms_float_number: case irms_int_number: case irms_reference: mode = register_mode(&mode_tmpl); + break; } + assert(mode != NULL); return mode; } /* * Creates a new vector mode. */ -ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsigned num_of_elem, int sign, - mode_arithmetic arithmetic, unsigned int modulo_shift) +ir_mode *new_ir_vector_mode(const char *name, ir_mode_sort sort, int bit_size, unsigned num_of_elem, int sign, + ir_mode_arithmetic arithmetic, unsigned int modulo_shift) { ir_mode mode_tmpl; ir_mode *mode = NULL; @@ -339,25 +335,23 @@ ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsi case irms_control_flow: case irms_memory: case irms_internal_boolean: - assert(0 && "internal modes cannot be user defined"); - break; + panic("internal modes cannot be user defined"); case irms_reference: - assert(0 && "only integer and floating point modes can be vectorized"); - break; + panic("only integer and floating point modes can be vectorized"); case irms_float_number: - assert(0 && "not yet implemented"); - break; + panic("not yet implemented"); case irms_int_number: mode = register_mode(&mode_tmpl); } + assert(mode != NULL); return mode; } /* Functions for the direct access to all attributes of an ir_mode */ -modecode (get_mode_modecode)(const ir_mode *mode) { +ir_modecode (get_mode_modecode)(const ir_mode *mode) { return _get_mode_modecode(mode); } @@ -369,7 +363,7 @@ const char *get_mode_name(const ir_mode *mode) { return get_id_str(mode->name); } -mode_sort (get_mode_sort)(const ir_mode* mode) { +ir_mode_sort (get_mode_sort)(const ir_mode* mode) { return _get_mode_sort(mode); } @@ -385,7 +379,7 @@ int (get_mode_sign)(const ir_mode *mode) { return _get_mode_sign(mode); } -mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode) { +ir_mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode) { return get_mode_arithmetic(mode); } @@ -412,7 +406,7 @@ void (set_mode_link)(ir_mode *mode, void *l) { tarval *get_mode_min(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_data(mode)); return mode->min; @@ -420,7 +414,7 @@ tarval *get_mode_min(ir_mode *mode) { tarval *get_mode_max(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_data(mode)); return mode->max; @@ -428,7 +422,7 @@ tarval *get_mode_max(ir_mode *mode) { tarval *get_mode_null(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_datab(mode)); return mode->null; @@ -436,7 +430,7 @@ tarval *get_mode_null(ir_mode *mode) { tarval *get_mode_one(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_datab(mode)); return mode->one; @@ -444,7 +438,7 @@ tarval *get_mode_one(ir_mode *mode) { tarval *get_mode_minus_one(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_data(mode)); return mode->minus_one; @@ -452,14 +446,14 @@ tarval *get_mode_minus_one(ir_mode *mode) { tarval *get_mode_all_one(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_datab(mode)); return mode->all_one; } tarval *get_mode_infinite(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_float(mode)); return get_tarval_plus_inf(mode); @@ -467,7 +461,7 @@ tarval *get_mode_infinite(ir_mode *mode) { tarval *get_mode_NAN(ir_mode *mode) { assert(mode); - assert(get_mode_modecode(mode) < (modecode) num_modes); + assert(get_mode_modecode(mode) < (ir_modecode) num_modes); assert(mode_is_float(mode)); return get_tarval_nan(mode); @@ -536,11 +530,11 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) { case irms_int_number: switch (get_mode_sort(lm)) { case irms_int_number: - if(get_mode_arithmetic(sm) != get_mode_arithmetic(lm)) + if (get_mode_arithmetic(sm) != get_mode_arithmetic(lm)) return 0; /* only two complement implemented */ - assert(get_mode_arithmetic(sm)==irma_twos_complement); + assert(get_mode_arithmetic(sm) == irma_twos_complement); /* integers are convertable if * - both have the same sign and lm is the larger one @@ -548,12 +542,12 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) { * (one for the sign, one for the highest bit of sm) * - sm & lm are two_complement and lm has greater or equal number of bits */ - if(mode_is_signed(sm)) { - if(!mode_is_signed(lm)) + if (mode_is_signed(sm)) { + if (!mode_is_signed(lm)) return 0; return sm_bits <= lm_bits; } else { - if(mode_is_signed(lm)) { + if (mode_is_signed(lm)) { return sm_bits < lm_bits; } return sm_bits <= lm_bits; @@ -592,6 +586,37 @@ int smaller_mode(const ir_mode *sm, const ir_mode *lm) { return 0; } +/* Returns true if a value of mode sm can be converted into mode lm + and backwards without loss. */ +int values_in_mode(const ir_mode *sm, const ir_mode *lm) { + int sm_bits, lm_bits; + ir_mode_arithmetic arith; + + assert(sm); + assert(lm); + + if (sm == lm) return 1; + + if (sm == mode_b) + return mode_is_int(lm); + + sm_bits = get_mode_size_bits(sm); + lm_bits = get_mode_size_bits(lm); + + arith = get_mode_arithmetic(sm); + if (arith != get_mode_arithmetic(lm)) + return 0; + + switch (arith) { + case irma_twos_complement: + case irma_ieee754: + return get_mode_size_bits(sm) <= get_mode_size_bits(lm); + + default: + return 0; + } +} + /* Return the signed integer equivalent mode for an reference mode. */ ir_mode *get_reference_mode_signed_eq(ir_mode *mode) { assert(mode_is_reference(mode)); @@ -720,7 +745,9 @@ void init_mode(void) { newmode.name = new_id_from_chars("E", 1); newmode.code = irm_E; newmode.sign = 1; - newmode.size = 80; + /* note that the tarval module is calculating with 80 bits, but we use + * 96 bits, as that is what will be stored to memory by most hardware */ + newmode.size = 96; mode_E = register_mode(&newmode); @@ -843,7 +870,11 @@ void init_mode(void) { ir_mode *find_unsigned_mode(const ir_mode *mode) { ir_mode n = *mode; - assert(mode->sort == irms_int_number); + /* allowed for reference mode */ + if (mode->sort == irms_reference) + n.sort = irms_int_number; + + assert(n.sort == irms_int_number); n.sign = 0; return find_mode(&n); } @@ -903,6 +934,25 @@ int mode_wrap_around(const ir_mode *mode) { return mode_is_int(mode); } +/* + * Returns non-zero if the cast from mode src to mode dst is a + * reinterpret cast (ie. only the bit pattern is reinterpreted, + * no conversion is done) + */ +int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst) { + ir_mode_arithmetic ma; + + if (src == dst) + return 1; + if (get_mode_size_bits(src) != get_mode_size_bits(dst)) + return 0; + ma = get_mode_arithmetic(src); + if (ma != get_mode_arithmetic(dst)) + return 0; + + return ma == irma_twos_complement || ma == irma_ones_complement; +} + void finish_mode(void) { obstack_free(&modes, 0); DEL_ARR_F(mode_list);