absyntax_utils/get_sizeof_datatype.cc
changeset 417 d48f53715f77
parent 306 82c1f453cd07
child 586 b602f0459f17
equal deleted inserted replaced
416:0c2ef191b22a 417:d48f53715f77
    78 #include <string.h>
    78 #include <string.h>
    79 #include <limits.h>  // get definition of ULLONG_MAX
    79 #include <limits.h>  // get definition of ULLONG_MAX
    80 /* tell stdint.h we want the definition of UINT64_MAX */
    80 /* tell stdint.h we want the definition of UINT64_MAX */
    81 #define __STDC_LIMIT_MACROS
    81 #define __STDC_LIMIT_MACROS
    82 #include <stdint.h>  // get definition of uint64_t and UINT64_MAX
    82 #include <stdint.h>  // get definition of uint64_t and UINT64_MAX
       
    83 #include <errno.h>
    83 
    84 
    84 
    85 
    85 #define ERROR error_exit(__FILE__,__LINE__)
    86 #define ERROR error_exit(__FILE__,__LINE__)
    86 /* function defined in main.cc */
    87 /* function defined in main.cc */
    87 extern void error_exit(const char *file_name, int line_no);
    88 extern void error_exit(const char *file_name, int line_no);
   156   */
   157   */
   157 
   158 
   158 /* NOTE: all integer_c and real_c tokens will always be positive (i.e. no leading '-')
   159 /* NOTE: all integer_c and real_c tokens will always be positive (i.e. no leading '-')
   159  * due to the way the source code is parsed by iec.flex.
   160  * due to the way the source code is parsed by iec.flex.
   160  */
   161  */
       
   162 
       
   163 /*
       
   164  * IEC6113-3 and C++ use IEC 60559 to rappresent floating point data types
       
   165  * REAL  => float       => single precision 	32 bit
       
   166  * LREAL => double      => double precision 	64 bit
       
   167  * ????? => long double => quadruple precision 128 bit
       
   168  */
   161 void *get_sizeof_datatype_c::visit(real_c *symbol) {
   169 void *get_sizeof_datatype_c::visit(real_c *symbol) {
   162   return _encode_int(32);
   170   char *endp;
       
   171   long double ld_test;
       
   172   double d_test;
       
   173   float  f_test;
       
   174 
       
   175   /* copy the original string, but leave out any underscores... */
       
   176   char *sval, *oval;
       
   177   const char *pval;
       
   178   oval = sval = (char *)malloc(strlen(symbol->value)+1);
       
   179   if (NULL ==  sval) ERROR;
       
   180   
       
   181   for (pval = symbol->value, sval = oval; *pval != '\0'; pval++) {
       
   182     if ('_' != *pval) {*sval = *pval; sval++;}
       
   183   }  
       
   184   *sval = '\0';  
       
   185   
       
   186   sval = oval;
       
   187   if ('\0' == *sval) ERROR;
       
   188 
       
   189   /* now do the conversion using the new string... */
       
   190   f_test = strtof(sval, &endp);
       
   191   if (*endp != '\0') ERROR;
       
   192   if (ERANGE != errno) {
       
   193     /* No overflow/underflow! => It fits in a float! */
       
   194     free(oval);
       
   195     return _encode_int(32);
       
   196   }
       
   197   
       
   198   d_test = strtod(sval, &endp);
       
   199   if (*endp != '\0') ERROR;
       
   200   if (ERANGE != errno) {
       
   201     /* No overflow/underflow! => It fits in a double! */
       
   202     free(oval);
       
   203     return _encode_int(64);
       
   204   }
       
   205   
       
   206   ld_test = strtold(sval, &endp);
       
   207   if (*endp != '\0') ERROR;
       
   208   if (ERANGE != errno) {
       
   209     /* No overflow/underflow! => It fits in a long double! */
       
   210     free(oval);
       
   211     return _encode_int(128);
       
   212   }
       
   213 
       
   214   free(oval);
       
   215   return _encode_int(65535); /* a very large number!!! */
   163 }
   216 }
   164 
   217 
   165 void *get_sizeof_datatype_c::visit(neg_real_c *symbol) {
   218 void *get_sizeof_datatype_c::visit(neg_real_c *symbol) {
   166   return symbol->exp->accept(*this);
   219   return symbol->exp->accept(*this);
   167 }
   220 }
   280   for (bitsize = 0; '\0' != *sval; sval++) {
   333   for (bitsize = 0; '\0' != *sval; sval++) {
   281     /* consistency check: make sure we only have binary digits! */
   334     /* consistency check: make sure we only have binary digits! */
   282     if (('0' != *sval) && ('1' != *sval) && ('_' != *sval))
   335     if (('0' != *sval) && ('1' != *sval) && ('_' != *sval))
   283       ERROR;
   336       ERROR;
   284 
   337 
   285     if ('_' != *sval) bitsize ++; /* 1 bits per binary digit */
   338     if ('_' != *sval) bitsize++; /* 1 bits per binary digit */
   286   }
   339   }
   287 
   340 
   288   /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */
   341   /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */
   289   if (0 == bitsize) bitsize = 1;
   342   if (0 == bitsize) bitsize = 1;
   290 
   343