absyntax_utils/get_sizeof_datatype.cc
changeset 608 f5d942be991b
parent 596 4efb11e44065
child 613 65a2e5ac2576
equal deleted inserted replaced
607:be9ba3531afb 608:f5d942be991b
    43  *       as their data type is undefined (e.g. the datat type of '30'
    43  *       as their data type is undefined (e.g. the datat type of '30'
    44  *       could be 'INT' or 'SINT' or 'LINT' or 'USINT' or ...
    44  *       could be 'INT' or 'SINT' or 'LINT' or 'USINT' or ...
    45  *       NOTE: for base 10 numeric literals, any number taking up more than 64 bits
    45  *       NOTE: for base 10 numeric literals, any number taking up more than 64 bits
    46  *             will only return a bitsize of 1024!
    46  *             will only return a bitsize of 1024!
    47  *
    47  *
    48  *       For numeric literals, we return the minimum number of bits
    48  *       NOTE: The code that does the following has been commented out, since we no longer need it!
    49  *       required to store the value.
    49  *             It has been superceded by the constant_folding.cc class.
       
    50  *       // For numeric literals, we return the minimum number of bits
       
    51  *       // required to store the value.
    50  *
    52  *
    51  * E.g. TYPE new_int_t : INT; END_TYPE;
    53  * E.g. TYPE new_int_t : INT; END_TYPE;
    52  *      TYPE new_int2_t : INT = 2; END_TYPE;
    54  *      TYPE new_int2_t : INT = 2; END_TYPE;
    53  *      TYPE new_subr_t : INT (4..5); END_TYPE;
    55  *      TYPE new_subr_t : INT (4..5); END_TYPE;
    54  *
    56  *
    55  *    sizeof(SINT) ->  8
    57  *    sizeof(SINT) ->  8
    56  *    sizeof(INT)  -> 16
    58  *    sizeof(INT)  -> 16
    57  *    sizeof(DINT) -> 32
    59  *    sizeof(DINT) -> 32
    58  *    sizeof(LINT) -> 64
    60  *    sizeof(LINT) -> 64
    59  *
    61  *
    60  *    sizeof('1')       ->  1
    62  *       NOTE: The code that does the following has been commented out, since we no longer need it!
    61  *    sizeof('015')     ->  4    # Leading zeros are ignored!
    63  *             It has been superceded by the constant_folding.cc class.
    62  *    sizeof('0')       ->  1    # This is a special case! Even the value 0 needs at least 1 bit to store!
    64  *    // sizeof('1')       ->  1
    63  *    sizeof('16')      ->  5
    65  *    // sizeof('015')     ->  4    # Leading zeros are ignored!
    64  *    sizeof('2#00101') ->  3
    66  *    // sizeof('0')       ->  1    # This is a special case! Even the value 0 needs at least 1 bit to store!
    65  *    sizeof('8#334')   ->  9
    67  *    // sizeof('16')      ->  5
    66  *    sizeof('16#2A')   ->  8
    68  *    // sizeof('2#00101') ->  3
    67  *
    69  *    // sizeof('8#334')   ->  9
    68  *    sizeof('7.4')     ->  32   # all real literals return 32 bits, the size of a 'REAL'
    70  *    // sizeof('16#2A')   ->  8
    69  *                               # TODO: study IEC 60559 for the range of values that may be
    71  *
    70  *                               #       stored in a REAL (basic single width floating point format)
    72  *    // sizeof('7.4')     ->  32   # all real literals return 32 bits, the size of a 'REAL'
    71  *                               #       and in a LREAL (basic double width floating point format)
    73  *    //                            # TODO: study IEC 60559 for the range of values that may be
    72  *                               #       and see if some real literals need to return 64 instead!
    74  *    //                            #       stored in a REAL (basic single width floating point format)
       
    75  *    //                            #       and in a LREAL (basic double width floating point format)
       
    76  *    //                            #       and see if some real literals need to return 64 instead!
    73  */
    77  */
    74 
    78 
    75 #include "get_sizeof_datatype.hh"
    79 #include "get_sizeof_datatype.hh"
    76 
    80 
    77 #include <stdlib.h>
    81 #include <stdlib.h>
    94 
    98 
    95 #define _encode_int(value)   ((void *)(((char *)NULL) + value))
    99 #define _encode_int(value)   ((void *)(((char *)NULL) + value))
    96 #define _decode_int(ptr)     (((char *)ptr) - ((char *)NULL))
   100 #define _decode_int(ptr)     (((char *)ptr) - ((char *)NULL))
    97 
   101 
    98 
   102 
    99 
   103 #if 0   /* We no longer need the code for handling numeric literals. But keep it around for a little while longer... */
   100 
       
   101 /* divide a base 10 literal in a string by 2 */
   104 /* divide a base 10 literal in a string by 2 */
   102 /* returns remainder of division (0 or 1)    */
   105 /* returns remainder of division (0 or 1)    */
   103 static int strdivby2(char **strptr) {
   106 static int strdivby2(char **strptr) {
   104   char *str = *strptr;
   107   char *str = *strptr;
   105   int carry = 0;
   108   int carry = 0;
   118   while (**strptr == '0') 
   121   while (**strptr == '0') 
   119     (*strptr)++;
   122     (*strptr)++;
   120 
   123 
   121   return carry;
   124   return carry;
   122 }
   125 }
   123 
   126 #endif
   124 
   127 
   125 /* Constructor for the singleton class */
   128 /* Constructor for the singleton class */
   126 int get_sizeof_datatype_c::getsize(symbol_c *data_type_symbol) {
   129 int get_sizeof_datatype_c::getsize(symbol_c *data_type_symbol) {
   127       if (NULL == singleton) {
   130       if (NULL == singleton) {
   128         singleton = new get_sizeof_datatype_c;
   131         singleton = new get_sizeof_datatype_c;
   136 get_sizeof_datatype_c::~get_sizeof_datatype_c(void) {
   139 get_sizeof_datatype_c::~get_sizeof_datatype_c(void) {
   137       if (NULL != singleton) delete singleton;
   140       if (NULL != singleton) delete singleton;
   138       singleton = NULL;
   141       singleton = NULL;
   139     }
   142     }
   140 
   143 
   141 
   144 #if 0   /* We no longer need the code for handling numeric literals. But keep it around for a little while longer... */
   142 /*********************/
   145 /*********************/
   143 /* B 1.2 - Constants */
   146 /* B 1.2 - Constants */
   144 /*********************/
   147 /*********************/
   145 
   148 
   146 /******************************/
   149 /******************************/
   413   /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */
   416   /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */
   414   if (0 == bitsize) bitsize = 1;
   417   if (0 == bitsize) bitsize = 1;
   415 
   418 
   416   return _encode_int(bitsize);
   419   return _encode_int(bitsize);
   417 }
   420 }
   418 
   421 #endif
   419 
   422 
   420 /***********************************/
   423 /***********************************/
   421 /* B 1.3.1 - Elementary Data Types */
   424 /* B 1.3.1 - Elementary Data Types */
   422 /***********************************/
   425 /***********************************/
   423 // void *get_sizeof_datatype_c::visit(time_type_name_c *symbol) {return _encode_int(0); }
   426 // void *get_sizeof_datatype_c::visit(time_type_name_c *symbol) {return _encode_int(0); }