ccb@202: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@265: * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant msousa@265: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * ccb@202: * ccb@202: * This code is made available on the understanding that it will not be ccb@202: * used in safety-critical situations without a full and competent review. ccb@202: */ ccb@202: ccb@202: /* msousa@265: * An IEC 61131-3 compiler. ccb@202: * ccb@202: * Based on the ccb@202: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) ccb@202: * ccb@202: */ ccb@202: ccb@202: /* Determine the size, in bits, of the data type. ccb@202: * ccb@202: * NOTE: Currently, only elementary data types with well defined sizes (in the standard) are supported. ccb@202: * - derived data types are not supported, and these will return 0 ccb@202: * - TIME, DATE, TIME_OF_DAY, and DATE_AND_TIME are not supported, and will return 0 ccb@202: * - STRING and WSTRING are not supported, and the standard merely defines bit per character, ccb@202: * and not the maximum number of characters, so these will return 0 ccb@202: * ccb@202: * We also support the 'Numeric Literals' Data types. ccb@202: * i.e., numeric literals are considerd basic data types ccb@202: * as their data type is undefined (e.g. the datat type of '30' ccb@202: * could be 'INT' or 'SINT' or 'LINT' or 'USINT' or ... ccb@202: * NOTE: for base 10 numeric literals, any number taking up more than 64 bits ccb@202: * will only return a bitsize of 1024! ccb@202: * msousa@608: * NOTE: The code that does the following has been commented out, since we no longer need it! msousa@608: * It has been superceded by the constant_folding.cc class. msousa@608: * // For numeric literals, we return the minimum number of bits msousa@608: * // required to store the value. ccb@202: * ccb@202: * E.g. TYPE new_int_t : INT; END_TYPE; ccb@202: * TYPE new_int2_t : INT = 2; END_TYPE; ccb@202: * TYPE new_subr_t : INT (4..5); END_TYPE; ccb@202: * ccb@202: * sizeof(SINT) -> 8 ccb@202: * sizeof(INT) -> 16 ccb@202: * sizeof(DINT) -> 32 ccb@202: * sizeof(LINT) -> 64 ccb@202: * msousa@608: * NOTE: The code that does the following has been commented out, since we no longer need it! msousa@608: * It has been superceded by the constant_folding.cc class. msousa@608: * // sizeof('1') -> 1 msousa@608: * // sizeof('015') -> 4 # Leading zeros are ignored! msousa@608: * // sizeof('0') -> 1 # This is a special case! Even the value 0 needs at least 1 bit to store! msousa@608: * // sizeof('16') -> 5 msousa@608: * // sizeof('2#00101') -> 3 msousa@608: * // sizeof('8#334') -> 9 msousa@608: * // sizeof('16#2A') -> 8 msousa@608: * msousa@608: * // sizeof('7.4') -> 32 # all real literals return 32 bits, the size of a 'REAL' msousa@608: * // # TODO: study IEC 60559 for the range of values that may be msousa@608: * // # stored in a REAL (basic single width floating point format) msousa@608: * // # and in a LREAL (basic double width floating point format) msousa@608: * // # and see if some real literals need to return 64 instead! ccb@202: */ ccb@202: ccb@202: #include "get_sizeof_datatype.hh" ccb@202: ccb@202: #include ccb@202: #include ccb@202: #include // get definition of ULLONG_MAX msousa@417: #include ccb@202: msousa@613: #include "../main.hh" // required for ERROR() and ERROR_MSG() macros, and uint64_t and UINT64_MAX msousa@596: ccb@202: ccb@202: ccb@202: /* This class is a singleton. ccb@202: * So we need a pointer to the singe instance... ccb@202: */ ccb@202: get_sizeof_datatype_c *get_sizeof_datatype_c::singleton = NULL; ccb@202: ccb@202: ccb@202: #define _encode_int(value) ((void *)(((char *)NULL) + value)) ccb@202: #define _decode_int(ptr) (((char *)ptr) - ((char *)NULL)) ccb@202: ccb@202: msousa@608: #if 0 /* We no longer need the code for handling numeric literals. But keep it around for a little while longer... */ ccb@202: /* divide a base 10 literal in a string by 2 */ ccb@202: /* returns remainder of division (0 or 1) */ ccb@202: static int strdivby2(char **strptr) { ccb@202: char *str = *strptr; ccb@202: int carry = 0; ccb@202: ccb@202: while (*str != '\0') { ccb@202: /* Assumes ASCII */ ccb@202: int newcarry; ccb@202: // newcarry = ((*str-'0') mod 2); ccb@202: newcarry = ((*str-'0') - ((*str-'0')/2)*2); ccb@202: *str = (((*str-'0') + 10*carry)/2) + '0'; ccb@202: carry = newcarry; ccb@202: str++; ccb@202: } ccb@202: ccb@202: /* ignore leading zeros in result... */ ccb@202: while (**strptr == '0') ccb@202: (*strptr)++; ccb@202: ccb@202: return carry; ccb@202: } msousa@608: #endif ccb@202: ccb@202: /* Constructor for the singleton class */ ccb@202: int get_sizeof_datatype_c::getsize(symbol_c *data_type_symbol) { ccb@202: if (NULL == singleton) { ccb@202: singleton = new get_sizeof_datatype_c; ccb@202: if (NULL == singleton) ccb@202: ERROR; ccb@202: } ccb@202: return _decode_int(data_type_symbol->accept(*singleton)); ccb@202: } ccb@202: ccb@202: /* Destructor for the singleton class */ ccb@202: get_sizeof_datatype_c::~get_sizeof_datatype_c(void) { ccb@202: if (NULL != singleton) delete singleton; ccb@202: singleton = NULL; ccb@202: } ccb@202: msousa@608: #if 0 /* We no longer need the code for handling numeric literals. But keep it around for a little while longer... */ ccb@202: /*********************/ ccb@202: /* B 1.2 - Constants */ ccb@202: /*********************/ ccb@202: ccb@202: /******************************/ ccb@202: /* B 1.2.1 - Numeric Literals */ ccb@202: /******************************/ ccb@202: /* Numeric literals without any explicit type cast have unknown data type, ccb@202: * so we continue considering them as their own basic data types until ccb@202: * they can be resolved (for example, when using '30+x' where 'x' is a LINT variable, the ccb@202: * numeric literal '30' must then be considered a LINT so the ADD function may be called ccb@202: * with all inputs of the same data type. ccb@202: * If 'x' were a SINT, then the '30' would have to be a SINT too! ccb@202: */ ccb@202: msousa@257: /* NOTE: all integer_c and real_c tokens will always be positive (i.e. no leading '-') ccb@202: * due to the way the source code is parsed by iec.flex. ccb@202: */ msousa@417: msousa@417: /* msousa@417: * IEC6113-3 and C++ use IEC 60559 to rappresent floating point data types msousa@417: * REAL => float => single precision 32 bit msousa@417: * LREAL => double => double precision 64 bit msousa@417: * ????? => long double => quadruple precision 128 bit msousa@417: */ ccb@202: void *get_sizeof_datatype_c::visit(real_c *symbol) { msousa@417: char *endp; msousa@417: long double ld_test; msousa@417: double d_test; msousa@417: float f_test; msousa@417: msousa@417: /* copy the original string, but leave out any underscores... */ msousa@417: char *sval, *oval; msousa@417: const char *pval; msousa@417: oval = sval = (char *)malloc(strlen(symbol->value)+1); msousa@417: if (NULL == sval) ERROR; msousa@417: msousa@417: for (pval = symbol->value, sval = oval; *pval != '\0'; pval++) { msousa@417: if ('_' != *pval) {*sval = *pval; sval++;} msousa@417: } msousa@417: *sval = '\0'; msousa@417: msousa@417: sval = oval; msousa@417: if ('\0' == *sval) ERROR; msousa@417: msousa@417: /* now do the conversion using the new string... */ msousa@417: f_test = strtof(sval, &endp); msousa@417: if (*endp != '\0') ERROR; msousa@417: if (ERANGE != errno) { msousa@417: /* No overflow/underflow! => It fits in a float! */ msousa@417: free(oval); msousa@417: return _encode_int(32); msousa@417: } msousa@417: msousa@417: d_test = strtod(sval, &endp); msousa@417: if (*endp != '\0') ERROR; msousa@417: if (ERANGE != errno) { msousa@417: /* No overflow/underflow! => It fits in a double! */ msousa@417: free(oval); msousa@417: return _encode_int(64); msousa@417: } msousa@417: msousa@417: ld_test = strtold(sval, &endp); msousa@417: if (*endp != '\0') ERROR; msousa@417: if (ERANGE != errno) { msousa@417: /* No overflow/underflow! => It fits in a long double! */ msousa@417: free(oval); msousa@417: return _encode_int(128); msousa@417: } msousa@417: msousa@417: free(oval); msousa@417: return _encode_int(65535); /* a very large number!!! */ ccb@202: } ccb@202: msousa@257: void *get_sizeof_datatype_c::visit(neg_real_c *symbol) { msousa@257: return symbol->exp->accept(*this); msousa@257: } msousa@257: msousa@257: msousa@257: /* NOTE: all integer_c and real_c literal tokens will always be positive (i.e. no leading '-') ccb@202: * due to the way the source code is parsed by iec.flex. ccb@202: */ ccb@202: void *get_sizeof_datatype_c::visit(integer_c *symbol) { ccb@202: int bitsize = 0; ccb@202: ccb@202: if (NULL == symbol->value ) ERROR; ccb@202: if ('\0' == *(symbol->value)) ERROR; ccb@202: ccb@202: #if 0 ccb@202: char *endptr; ccb@202: /* Convert the string to an unsigned 64 bit integer */ ccb@202: /* We can use strtoull(), but we are not guaranteed that an unsigned long long int ccb@202: * is 64 bits wide. First make sure that it is... ccb@202: * ccb@202: * We could also use the strtouq() instead, which converts ccb@202: * to a quad word (64 bits). However, this requires either GCC or BSD extensions. ccb@202: */ ccb@202: #ifdef strtoull // this ifdef does not work!! ccb@202: /* we have long long int, use it... */ ccb@202: #define ival_MAX ULLONG_MAX ccb@202: unsigned long long int ival = 0; ccb@202: ival = strtoull (symbol->value, &endptr, 10 /* base */); ccb@202: #else ccb@202: /* use long int ... */ ccb@202: #define ival_MAX ULONG_MAX ccb@202: unsigned long int ival = 0; ccb@202: ival = strtoul (symbol->value, &endptr, 10 /* base */); ccb@202: #endif ccb@202: ccb@202: #if (ival_MAX < UINT64_MAX) ccb@202: #error Largest strtoint() conversion function converts to an int less than 64 bits wide! ccb@202: #endif ccb@202: ccb@202: if (NULL == endptr) ERROR; ccb@202: if ('\0' != *endptr) ERROR; ccb@202: // TODO: return _encode_int(1024) if value takes up more than 64 bits! ccb@202: ccb@202: /* determine the number of bits used... */ ccb@202: for (bitsize = 0; ival > 0; ival /= 2, bitsize++); ccb@202: ccb@202: /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */ ccb@202: if (0 == bitsize) bitsize = 1; ccb@202: ccb@202: return _encode_int(bitsize); ccb@202: #endif ccb@202: /* We could first convert from string to some kind of integer, and then ccb@202: * determine the the bitsize using integer aritmetic. ccb@202: * However, we are then limited to the bit size of the widest available integer ccb@202: * (usually 64 bits), which is not good at all! ccb@202: */ ccb@202: /* Let's try to determine bitsize by converting directly from the string!! */ msousa@306: char *sval, *oval; msousa@306: const char *pval; msousa@306: oval = sval = (char *)malloc(strlen(symbol->value)+1); msousa@306: if (NULL == sval) ERROR; msousa@306: msousa@306: /* copy the original string, but leave out any underscores... */ msousa@306: for (pval = symbol->value, sval = oval; *pval != '\0'; pval++) { msousa@306: if ('_' != *pval) {*sval = *pval; sval++;} msousa@306: } msousa@306: *sval = '\0'; msousa@306: msousa@306: sval = oval; msousa@306: if ('\0' == *sval) ERROR; msousa@306: msousa@306: for (bitsize = 0; *sval != '\0'; strdivby2(&sval), bitsize ++); msousa@306: msousa@306: /* ccb@202: char *sval = strdup(symbol->value); ccb@202: char *oval = sval; ccb@202: if (NULL == sval) ERROR; ccb@202: if ('\0' == *sval) ERROR; ccb@202: for (bitsize = 0; *sval != '\0'; strdivby2(&sval), bitsize ++); msousa@306: */ ccb@202: ccb@202: /* Even for (value == 0), the above loop will return bitsize == 1!, ccb@202: * so we don't need to handle the special case... ccb@202: */ ccb@202: /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */ ccb@202: // if (0 == bitsize) bitsize = 1; ccb@202: ccb@202: free(oval); ccb@202: return _encode_int(bitsize); ccb@202: } ccb@202: ccb@202: msousa@257: void *get_sizeof_datatype_c::visit(neg_integer_c *symbol) { msousa@257: return symbol->exp->accept(*this); msousa@257: } msousa@257: msousa@257: msousa@257: /* NOTE: all binary_integer_c tokens will always be positive (i.e. no leading '-') msousa@257: * due to the syntax definition of IEC 61131-3. ccb@202: */ ccb@202: void *get_sizeof_datatype_c::visit(binary_integer_c *symbol) { ccb@202: const char *sval = symbol->value; ccb@202: int bitsize = 0; ccb@202: ccb@202: /* first 2 characters had better be "2#" ! */ ccb@202: if (NULL == sval) ERROR; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '2' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '#' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: ccb@202: /* determine the number of bits used... */ msousa@304: for (bitsize = 0; '\0' != *sval; sval++) { ccb@202: /* consistency check: make sure we only have binary digits! */ msousa@304: if (('0' != *sval) && ('1' != *sval) && ('_' != *sval)) ccb@202: ERROR; msousa@304: msousa@417: if ('_' != *sval) bitsize++; /* 1 bits per binary digit */ ccb@202: } ccb@202: ccb@202: /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */ ccb@202: if (0 == bitsize) bitsize = 1; ccb@202: ccb@202: return _encode_int(bitsize); ccb@202: } ccb@202: ccb@202: msousa@257: /* NOTE: all octal_integer_c tokens will always be positive (i.e. no leading '-') msousa@257: * due to the syntax definition of IEC 61131-3. ccb@202: */ ccb@202: void *get_sizeof_datatype_c::visit(octal_integer_c *symbol) { ccb@202: const char *sval = symbol->value; ccb@202: int bitsize = 0; ccb@202: ccb@202: /* first 2 characters had better be "8#" ! */ ccb@202: if (NULL == sval) ERROR; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '8' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '#' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: ccb@202: /* determine the number of bits used... */ msousa@304: for (bitsize = 0; '\0' != *sval; sval++) { ccb@202: /* consistency check: make sure we only have octal digits! */ ccb@202: /* Assumes ASCII */ msousa@304: if ((('0' > *sval) || ('7' < *sval)) && ('_' != *sval)) ccb@202: ERROR; msousa@304: msousa@304: if ('_' != *sval) bitsize += 3; /* 3 bits per octal digit */ ccb@202: } ccb@202: ccb@202: /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */ ccb@202: if (0 == bitsize) bitsize = 1; ccb@202: ccb@202: return _encode_int(bitsize); ccb@202: } ccb@202: ccb@202: msousa@257: /* NOTE: all hex_integer_c tokens will always be positive (i.e. no leading '-') msousa@257: * due to the syntax definition of IEC 61131-3. ccb@202: */ ccb@202: void *get_sizeof_datatype_c::visit(hex_integer_c *symbol) { ccb@202: const char *sval = symbol->value; ccb@202: int bitsize = 0; ccb@202: ccb@202: /* first 3 characters had better be "16#" ! */ ccb@202: if (NULL == sval) ERROR; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '1' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '6' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: if ( '#' != *sval) ERROR; ccb@202: sval++; ccb@202: if ('\0' == *sval) ERROR; ccb@202: ccb@202: /* determine the number of bits used... */ msousa@304: for (bitsize = 0; '\0' != *sval; sval++) { msousa@304: /* consistency check: make sure we only have hex digits or underscores! */ ccb@202: /* Assumes ASCII */ ccb@202: if (!(('0' <= *sval) && ('9' >= *sval)) && ccb@202: !(('A' <= *sval) && ('F' >= *sval)) && msousa@586: !(('a' <= *sval) && ('f' >= *sval)) && msousa@304: ! ('_' == *sval)) ccb@202: ERROR; msousa@304: msousa@304: if ('_' != *sval) bitsize += 4; /* 4 bits per hex digit */ ccb@202: } ccb@202: ccb@202: /* special case... if (value == 0) <=> (bitsize == 0), return bit size of 1 ! */ ccb@202: if (0 == bitsize) bitsize = 1; ccb@202: ccb@202: return _encode_int(bitsize); ccb@202: } msousa@608: #endif ccb@202: ccb@202: /***********************************/ ccb@202: /* B 1.3.1 - Elementary Data Types */ ccb@202: /***********************************/ msousa@257: // void *get_sizeof_datatype_c::visit(time_type_name_c *symbol) {return _encode_int(0); } msousa@257: void *get_sizeof_datatype_c::visit(bool_type_name_c *symbol) {return _encode_int(1); } msousa@257: void *get_sizeof_datatype_c::visit(sint_type_name_c *symbol) {return _encode_int(8); } msousa@257: void *get_sizeof_datatype_c::visit(int_type_name_c *symbol) {return _encode_int(16);} msousa@257: void *get_sizeof_datatype_c::visit(dint_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(lint_type_name_c *symbol) {return _encode_int(64);} msousa@257: void *get_sizeof_datatype_c::visit(usint_type_name_c *symbol) {return _encode_int(8); } msousa@257: void *get_sizeof_datatype_c::visit(uint_type_name_c *symbol) {return _encode_int(16);} msousa@257: void *get_sizeof_datatype_c::visit(udint_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(ulint_type_name_c *symbol) {return _encode_int(64);} msousa@257: void *get_sizeof_datatype_c::visit(real_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(lreal_type_name_c *symbol) {return _encode_int(64);} msousa@257: // void *get_sizeof_datatype_c::visit(date_type_name_c *symbol) {return _encode_int(0); } msousa@257: // void *get_sizeof_datatype_c::visit(tod_type_name_c *symbol) {return _encode_int(0); } msousa@257: // void *get_sizeof_datatype_c::visit(dt_type_name_c *symbol) {return _encode_int(0); } msousa@257: void *get_sizeof_datatype_c::visit(byte_type_name_c *symbol) {return _encode_int(8); } msousa@257: void *get_sizeof_datatype_c::visit(word_type_name_c *symbol) {return _encode_int(16);} msousa@257: void *get_sizeof_datatype_c::visit(dword_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(lword_type_name_c *symbol) {return _encode_int(64);} msousa@257: // void *get_sizeof_datatype_c::visit(string_type_name_c *symbol) {return _encode_int(0); } msousa@257: // void *get_sizeof_datatype_c::visit(wstring_type_name_c *symbol) {return _encode_int(0); } ccb@202: /******************************************************/ ccb@202: /* Extensions to the base standard as defined in */ ccb@202: /* "Safety Software Technical Specification, */ ccb@202: /* Part 1: Concepts and Function Blocks, */ ccb@202: /* Version 1.0 – Official Release" */ ccb@202: /* by PLCopen - Technical Committee 5 - 2006-01-31 */ ccb@202: /******************************************************/ msousa@257: // void *get_sizeof_datatype_c::visit(safetime_type_name_c *symbol); {return _encode_int(0); } msousa@257: void *get_sizeof_datatype_c::visit(safebool_type_name_c *symbol) {return _encode_int(1); } msousa@257: void *get_sizeof_datatype_c::visit(safesint_type_name_c *symbol) {return _encode_int(8); } msousa@257: void *get_sizeof_datatype_c::visit(safeint_type_name_c *symbol) {return _encode_int(16);} msousa@257: void *get_sizeof_datatype_c::visit(safedint_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(safelint_type_name_c *symbol) {return _encode_int(64);} msousa@257: void *get_sizeof_datatype_c::visit(safeusint_type_name_c *symbol) {return _encode_int(8); } msousa@257: void *get_sizeof_datatype_c::visit(safeuint_type_name_c *symbol) {return _encode_int(16);} msousa@257: void *get_sizeof_datatype_c::visit(safeudint_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(safeulint_type_name_c *symbol) {return _encode_int(64);} msousa@257: void *get_sizeof_datatype_c::visit(safereal_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(safelreal_type_name_c *symbol) {return _encode_int(64);} msousa@257: // void *get_sizeof_datatype_c::visit(safedate_type_name_c *symbol); {return _encode_int(0); } msousa@257: // void *get_sizeof_datatype_c::visit(safetod_type_name_c *symbol); {return _encode_int(0); } msousa@257: // void *get_sizeof_datatype_c::visit(safedt_type_name_c *symbol); {return _encode_int(0); } msousa@257: void *get_sizeof_datatype_c::visit(safebyte_type_name_c *symbol) {return _encode_int(8); } msousa@257: void *get_sizeof_datatype_c::visit(safeword_type_name_c *symbol) {return _encode_int(16);} msousa@257: void *get_sizeof_datatype_c::visit(safedword_type_name_c *symbol) {return _encode_int(32);} msousa@257: void *get_sizeof_datatype_c::visit(safelword_type_name_c *symbol) {return _encode_int(64);} msousa@257: // void *get_sizeof_datatype_c::visit(safestring_type_name_c *symbol); {return _encode_int(0); } msousa@257: // void *get_sizeof_datatype_c::visit(safewstring_type_name_c *symbol); {return _encode_int(0); } msousa@257: ccb@202: ccb@202: /********************************/ ccb@202: /* B 1.3.3 - Derived data types */ ccb@202: /********************************/ ccb@202: // Not yet supported...