cleaning up code. Changing HUGE_VAL to INFINITY.
authorMario de Sousa <msousa@fe.up.pt>
Wed, 18 Jul 2012 22:34:51 +0100
changeset 607 be9ba3531afb
parent 606 d2122a32ec86
child 608 f5d942be991b
cleaning up code. Changing HUGE_VAL to INFINITY.
absyntax/absyntax.hh
main.hh
stage3/constant_folding.cc
stage3/fill_candidate_datatypes.cc
--- a/absyntax/absyntax.hh	Wed Jul 18 17:10:57 2012 +0100
+++ b/absyntax/absyntax.hh	Wed Jul 18 22:34:51 2012 +0100
@@ -50,29 +50,9 @@
 #include <vector>
 #include <string>
 #include <stdint.h>  // required for uint64_t, etc...
-
-
-/* Determine, for the current platform, which data type (float, double or long double) uses 64 bits. */
-/* NOTE: We cant use sizeof() in pre-processor directives, so we have to do it another way... */
-/* CURIOSITY: We can use sizeof() and offsetof() inside static_assert() but:
- *          - this only allows us to make assertions, and not #define new macros
- *          - is only available in the C standard [ISO/IEC 9899:2011] and the C++ 0X draft standard [Becker 2008]. It is not available in C99.
- *          https://www.securecoding.cert.org/confluence/display/seccode/DCL03-C.+Use+a+static+assertion+to+test+the+value+of+a+constant+expression
- *         struct {int a, b, c, d} header_t;
- *  e.g.:  static_assert(offsetof(struct header_t, c) == 8, "Compile time error message.");
- */
-
-#include <float.h>
-#if    (LDBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
-  #define long_double long double
-  #define real64_t long_double /* so we can later use #if (real64_t == long_double) directives in the code! */
-#elif  ( DBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
-  #define real64_t double
-#elif  ( FLT_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
-  #define real64_t float
-#else 
-  #error Could not find a 64 bit floating point data type on this platform. Aborting...
-#endif
+#include "../main.hh" // required for uint8_t, real_64_t, ..., and the macros INT8_MAX, REAL32_MAX, ... */
+
+
 
 
 /* Forward declaration of the visitor interface
--- a/main.hh	Wed Jul 18 17:10:57 2012 +0100
+++ b/main.hh	Wed Jul 18 22:34:51 2012 +0100
@@ -35,33 +35,6 @@
 #define _MAIN_HH
 
 
- /* Get the definition of INT16_MAX, INT16_MIN, UINT64_MAX, INT64_MAX, INT64_MIN, ... */
-
-#define __STDC_LIMIT_MACROS /* required when including from C++ source code. */
-#include <stdint.h>         
-
-#ifndef   UINT64_MAX 
-  #define UINT64_MAX (std::numeric_limits< uint64_t >::max())
-#endif
-#ifndef    INT64_MAX 
-  #define  INT64_MAX (std::numeric_limits<  int64_t >::max())
-#endif
-#ifndef    INT64_MIN
-  #define  INT64_MIN (std::numeric_limits<  int64_t >::min()) 
-#endif
-
-#if    (real64_t  == float)
-  #define HUGE_VAL64  HUGE_VALF
-#elif  (real64_t  == double)
-  #define HUGE_VAL64  HUGE_VAL
-#elif  (real64_t  == long_double)
-  #define HUGE_VAL64  HUGE_VALL
-#else 
-  #error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting!
-#endif
-
-
-
 
  /* Function used throughout the code --> used to report failed assertions (i.e. internal compiler errors)! */
 #include <stddef.h>  /* required for NULL */
@@ -75,4 +48,80 @@
 
 
 
+
+
+ /* Get the definition of INT16_MAX, INT16_MIN, UINT64_MAX, INT64_MAX, INT64_MIN, ... */
+
+#define __STDC_LIMIT_MACROS /* required to have UINTxx_MAX defined when including stdint.h from C++ source code. */
+#include <stdint.h>         
+#include <limits>
+
+#ifndef   UINT64_MAX 
+  #define UINT64_MAX (std::numeric_limits< uint64_t >::max())
+#endif
+#ifndef    INT64_MAX 
+  #define  INT64_MAX (std::numeric_limits<  int64_t >::max())
+#endif
+#ifndef    INT64_MIN
+  #define  INT64_MIN (std::numeric_limits<  int64_t >::min()) 
+#endif
+
+
+
+/* Determine, for the current platform, which datas types (float, double or long double) use 64 and 32 bits. */
+/* NOTE: We cant use sizeof() in pre-processor directives, so we have to do it another way... */
+/* CURIOSITY: We can use sizeof() and offsetof() inside static_assert() but:
+ *          - this only allows us to make assertions, and not #define new macros
+ *          - is only available in the C standard [ISO/IEC 9899:2011] and the C++ 0X draft standard [Becker 2008]. It is not available in C99.
+ *          https://www.securecoding.cert.org/confluence/display/seccode/DCL03-C.+Use+a+static+assertion+to+test+the+value+of+a+constant+expression
+ *         struct {int a, b, c, d} header_t;
+ *  e.g.:  static_assert(offsetof(struct header_t, c) == 8, "Compile time error message.");
+ */
+
+#include <float.h>
+#if    (LDBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
+  #define long_double long double
+  #define real64_t long_double /* so we can later use #if (real64_t == long_double) directives in the code! */
+  #define REAL64_MAX  LDBL_MAX
+#elif  ( DBL_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
+  #define real64_t double
+  #define REAL64_MAX  DBL_MAX
+#elif  ( FLT_MANT_DIG == 53) /* NOTE: 64 bit IEC559 real has 53 bits for mantissa! */
+  #define real64_t float
+  #define REAL64_MAX  FLT_MAX
+#else 
+  #error Could not find a 64 bit floating point data type on this platform. Aborting...
+#endif
+
+
+#if    (LDBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */
+  #ifndef long_double 
+    #define long_double long double
+  #endif  
+  #define real32_t long_double /* so we can later use #if (real32_t == long_double) directives in the code! */
+  #define REAL32_MAX  LDBL_MAX
+#elif  ( DBL_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */
+  #define real32_t double
+  #define REAL32_MAX  DBL_MAX
+#elif  ( FLT_MANT_DIG == 24) /* NOTE: 32 bit IEC559 real has 24 bits for mantissa! */
+  #define real32_t float
+  #define REAL32_MAX  FLT_MAX
+#else 
+  #error Could not find a 32 bit floating point data type on this platform. Aborting...
+#endif
+
+
+
+#include <math.h>
+#ifndef INFINITY
+  #error Could not find the macro that defines the value for INFINITY in the current platform.
+#endif
+#ifndef NAN
+  #error Could not find the macro that defines the value for NAN in the current platform.
+#endif
+
+
+
+
+
 #endif // #ifndef _MAIN_HH
\ No newline at end of file
--- a/stage3/constant_folding.cc	Wed Jul 18 17:10:57 2012 +0100
+++ b/stage3/constant_folding.cc	Wed Jul 18 22:34:51 2012 +0100
@@ -117,16 +117,13 @@
  */
 
 #include "constant_folding.hh"
-#include <limits>
-#include <math.h> /* required for pow function, and HUGE_VAL, HUGE_VALF, HUGE_VALL */
 #include <stdlib.h> /* required for malloc() */
 
-
 #include <string.h>  /* required for strlen() */
 // #include <stdlib.h>  /* required for atoi() */
 #include <errno.h>   /* required for errno */
 
-#include <../main.hh>         /* required for UINT64_MAX, INT64_MAX, INT64_MIN, ... */
+#include "../main.hh" // required for uint8_t, real_64_t, ..., and the macros NAN, INFINITY, INT8_MAX, REAL32_MAX, ... */
 
 
 
@@ -508,7 +505,7 @@
 	 *  specified for the data type of the function output, or if division by zero is attempted."
 	 * For this reason, any operation that has as a result a positive or negative inifinity, is also an error!
 	 */
-	if ((isnan(res)) || (res == HUGE_VAL64) || (res == -HUGE_VAL64))
+	if ((isnan(res)) || (res == INFINITY) || (res == -INFINITY))
 		SET_OVFLOW(real64, res_ptr);
 }
 
--- a/stage3/fill_candidate_datatypes.cc	Wed Jul 18 17:10:57 2012 +0100
+++ b/stage3/fill_candidate_datatypes.cc	Wed Jul 18 22:34:51 2012 +0100
@@ -65,9 +65,8 @@
 #include <strings.h>
 
 #define GET_CVALUE(dtype, symbol)             ((symbol)->const_value_##dtype->value)
-#define IS_UNDEF(dtype, symbol)               (NULL == (symbol)->const_value_##dtype)
 #define VALID_CVALUE(dtype, symbol)           ((NULL != (symbol)->const_value_##dtype) && (symbol_c::cs_const_value == (symbol)->const_value_##dtype->status))
-#define IS_OVERFLOW(dtype, symbol)            (symbol_c::cs_overflow == (symbol)->const_value_##dtype->status)
+#define IS_OVERFLOW(dtype, symbol)            ((NULL != (symbol)->const_value_##dtype) && (symbol_c::cs_overflow == (symbol)->const_value_##dtype->status))
 
 /* set to 1 to see debug info during execution */
 static int debug = 0;
@@ -113,118 +112,48 @@
   return true;
 }
 
+
+
 void fill_candidate_datatypes_c::remove_incompatible_datatypes(symbol_c *symbol) {
-  /* Remove unsigned data types */
-  if (! IS_UNDEF( uint64, symbol)) {
-    if (VALID_CVALUE( uint64, symbol)) {
-      uint64_t value = GET_CVALUE(uint64, symbol);
-      if (value > 1) {
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::bool_type_name,       symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safebool_type_name,   symbol->candidate_datatypes);
-      }
-      if (value > UINT8_MAX  ) {
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::usint_type_name,      symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safeusint_type_name,  symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::byte_type_name,       symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::safebyte_type_name,   symbol->candidate_datatypes);
-      }
-      if (value > UINT16_MAX ) {
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::uint_type_name,       symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safeuint_type_name,   symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::word_type_name,       symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safeword_type_name,   symbol->candidate_datatypes);
-      }
-      if (value > UINT32_MAX ) {
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::udint_type_name,      symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safeudint_type_name,  symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::dword_type_name,      symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safedword_type_name,  symbol->candidate_datatypes);
-      }
-    }
-    if (IS_OVERFLOW( uint64, symbol)) {
-	remove_from_candidate_datatype_list(&search_constant_type_c::bool_type_name,       symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safebool_type_name,   symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::usint_type_name,      symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safeusint_type_name,  symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::byte_type_name,       symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safebyte_type_name,   symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::uint_type_name,       symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safeuint_type_name,   symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::word_type_name,       symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safeword_type_name,   symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::udint_type_name,      symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safeudint_type_name,  symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::dword_type_name,      symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safedword_type_name,  symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::ulint_type_name,      symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safeulint_type_name,  symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::lword_type_name,      symbol->candidate_datatypes);
-	remove_from_candidate_datatype_list(&search_constant_type_c::safelword_type_name,  symbol->candidate_datatypes);
-    }
+  #ifdef __REMOVE__
+    #error __REMOVE__ macro already exists. Choose another name!
+  #endif
+  #define __REMOVE__(datatype)\
+      remove_from_candidate_datatype_list(&search_constant_type_c::datatype,       symbol->candidate_datatypes);\
+      remove_from_candidate_datatype_list(&search_constant_type_c::safe##datatype, symbol->candidate_datatypes);
+  
+  {/* Remove unsigned data types */
+    uint64_t value = 0;
+    if (VALID_CVALUE( uint64, symbol)) value = GET_CVALUE(uint64, symbol);
+    if (IS_OVERFLOW ( uint64, symbol)) value = (uint64_t)UINT32_MAX + (uint64_t)1;
+    
+    if (value > 1          )          {__REMOVE__(bool_type_name);}
+    if (value > UINT8_MAX  )          {__REMOVE__(usint_type_name);  __REMOVE__( byte_type_name);}
+    if (value > UINT16_MAX )          {__REMOVE__( uint_type_name);  __REMOVE__( word_type_name);}
+    if (value > UINT32_MAX )          {__REMOVE__(udint_type_name);  __REMOVE__(dword_type_name);}
+    if (IS_OVERFLOW( uint64, symbol)) {__REMOVE__(ulint_type_name);  __REMOVE__(lword_type_name);}
   }
-  /* Remove signed data types */
-  if (! IS_UNDEF( int64, symbol)) {
- 	if (VALID_CVALUE( int64, symbol)) {
-      int64_t value = GET_CVALUE(int64, symbol);
-      if (value < 0 || value > 1) {
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::bool_type_name,       symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safebool_type_name,   symbol->candidate_datatypes);
-      }
- 	  if  ((value < INT8_MIN  ) || (value > INT8_MAX )) {
- 	      remove_from_candidate_datatype_list(&search_constant_type_c::sint_type_name,       symbol->candidate_datatypes);
- 	      remove_from_candidate_datatype_list(&search_constant_type_c::safesint_type_name,   symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::byte_type_name,       symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::safebyte_type_name,   symbol->candidate_datatypes);
- 	  }
-      if  ((value < INT16_MIN ) || (value > INT16_MAX)) {
-          remove_from_candidate_datatype_list(&search_constant_type_c::int_type_name,        symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safeint_type_name,    symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::word_type_name,       symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safeword_type_name,   symbol->candidate_datatypes);
-      }
-      if  ((value < INT32_MIN ) || (value > INT32_MAX)) {
-          remove_from_candidate_datatype_list(&search_constant_type_c::dint_type_name,       symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safedint_type_name,   symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::dword_type_name,      symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safedword_type_name,  symbol->candidate_datatypes);
-      }
- 	}
-    if  (IS_OVERFLOW( int64, symbol)) {
-    	  /* Not exist a valid signed integer data types it can represent the current value */
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::sint_type_name,       symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safesint_type_name,   symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::int_type_name,        symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safeint_type_name,    symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::dint_type_name,       symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safedint_type_name,   symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::lint_type_name,       symbol->candidate_datatypes);
-    	  remove_from_candidate_datatype_list(&search_constant_type_c::safelint_type_name,   symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::byte_type_name,       symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::safebyte_type_name,   symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::word_type_name,       symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safeword_type_name,   symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::dword_type_name,      symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safedword_type_name,  symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::lword_type_name,      symbol->candidate_datatypes);
-          remove_from_candidate_datatype_list(&search_constant_type_c::safelword_type_name,  symbol->candidate_datatypes);
-    }
+
+  {/* Remove signed data types */
+    int64_t value = 0;
+    if (VALID_CVALUE(  int64, symbol)) value = GET_CVALUE(int64, symbol);
+    if (IS_OVERFLOW (  int64, symbol)) value = (int64_t)INT32_MAX + (int64_t)1;
+    
+    if ((value < 0) || (value > 1))                 {__REMOVE__( bool_type_name);}
+    if ((value <  INT8_MIN) || (value >  INT8_MAX)) {__REMOVE__(sint_type_name);  __REMOVE__( byte_type_name);}
+    if ((value < INT16_MIN) || (value > INT16_MAX)) {__REMOVE__( int_type_name);  __REMOVE__( word_type_name);}
+    if ((value < INT32_MIN) || (value > INT32_MAX)) {__REMOVE__(dint_type_name);  __REMOVE__(dword_type_name);}
+    if (IS_OVERFLOW( int64, symbol))                {__REMOVE__(lint_type_name);  __REMOVE__(lword_type_name);}
   }
-  /* Remove floating point data types */
-  if (! IS_UNDEF( real64, symbol)) {
-	  if (VALID_CVALUE( real64, symbol)) {
-		  real64_t value = GET_CVALUE(real64, symbol);
-		  /*  We need a way to understand when lost precision happen and overflow for single precision.
-		   *  In this way we can remove REAL data type when a value has a mantissa or exponent too large.
-		   */
-	  }
-	  if  (IS_OVERFLOW( real64, symbol)) {
-		  /* Not exist a valid real data types that it can represent the current value */
-	      remove_from_candidate_datatype_list(&search_constant_type_c::real_type_name,      symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::safereal_type_name,  symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::lreal_type_name,     symbol->candidate_datatypes);
-	      remove_from_candidate_datatype_list(&search_constant_type_c::safelreal_type_name, symbol->candidate_datatypes);
-	  }
+    
+  {/* Remove floating point data types */
+    real64_t value = 0;
+    if (VALID_CVALUE( real64, symbol)) value = GET_CVALUE(real64, symbol);
+    if (value >  REAL32_MAX )         {__REMOVE__( real_type_name);}
+    if (value < -REAL32_MAX )         {__REMOVE__( real_type_name);}
+    if (IS_OVERFLOW( real64, symbol)) {__REMOVE__(lreal_type_name);}
   }
+  #undef __REMOVE__
 }