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: 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: * ccb@202: * For numeric literals, we return the minimum number of bits ccb@202: * 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: * ccb@202: * sizeof('1') -> 1 ccb@202: * sizeof('015') -> 4 # Leading zeros are ignored! ccb@202: * sizeof('0') -> 1 # This is a special case! Even the value 0 needs at least 1 bit to store! ccb@202: * sizeof('16') -> 5 ccb@202: * sizeof('2#00101') -> 3 ccb@202: * sizeof('8#334') -> 9 ccb@202: * sizeof('16#2A') -> 8 ccb@202: * ccb@202: * sizeof('7.4') -> 32 # all real literals return 32 bits, the size of a 'REAL' ccb@202: * # TODO: study IEC 60559 for the range of values that may be ccb@202: * # stored in a REAL (basic single width floating point format) ccb@202: * # and in a LREAL (basic double width floating point format) ccb@202: * # and see if some real literals need to return 64 instead! ccb@202: */ ccb@202: ccb@202: #include "../absyntax/visitor.hh" ccb@202: ccb@202: class get_sizeof_datatype_c: public null_visitor_c { ccb@202: ccb@202: public: ccb@202: static int getsize(symbol_c *data_type_symbol); ccb@202: ~get_sizeof_datatype_c(void); ccb@202: ccb@202: private: ccb@202: /* this class is a singleton. So we need a pointer to the single instance... */ ccb@202: static get_sizeof_datatype_c *singleton; ccb@202: ccb@202: private: 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: void *visit(real_c *symbol); msousa@257: void *visit(neg_real_c *symbol); ccb@202: void *visit(integer_c *symbol); msousa@257: void *visit(neg_integer_c *symbol); ccb@202: void *visit(binary_integer_c *symbol); ccb@202: void *visit(octal_integer_c *symbol); ccb@202: void *visit(hex_integer_c *symbol); msousa@608: #endif ccb@202: /***********************************/ ccb@202: /* B 1.3.1 - Elementary Data Types */ ccb@202: /***********************************/ ccb@202: // void *visit(time_type_name_c *symbol); ccb@202: void *visit(bool_type_name_c *symbol); ccb@202: void *visit(sint_type_name_c *symbol); ccb@202: void *visit(int_type_name_c *symbol); ccb@202: void *visit(dint_type_name_c *symbol); ccb@202: void *visit(lint_type_name_c *symbol); ccb@202: void *visit(usint_type_name_c *symbol); ccb@202: void *visit(uint_type_name_c *symbol); ccb@202: void *visit(udint_type_name_c *symbol); ccb@202: void *visit(ulint_type_name_c *symbol); ccb@202: void *visit(real_type_name_c *symbol); ccb@202: void *visit(lreal_type_name_c *symbol); ccb@202: // void *visit(date_type_name_c *symbol); ccb@202: // void *visit(tod_type_name_c *symbol); ccb@202: // void *visit(dt_type_name_c *symbol) ; ccb@202: void *visit(byte_type_name_c *symbol); ccb@202: void *visit(word_type_name_c *symbol); ccb@202: void *visit(dword_type_name_c *symbol); ccb@202: void *visit(lword_type_name_c *symbol); ccb@202: // void *visit(string_type_name_c *symbol); ccb@202: // void *visit(wstring_type_name_c *symbol); ccb@202: 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 *visit(safetime_type_name_c *symbol); ccb@202: void *visit(safebool_type_name_c *symbol); msousa@257: void *visit(safesint_type_name_c *symbol); msousa@257: void *visit(safeint_type_name_c *symbol); msousa@257: void *visit(safedint_type_name_c *symbol); msousa@257: void *visit(safelint_type_name_c *symbol); msousa@257: void *visit(safeusint_type_name_c *symbol); msousa@257: void *visit(safeuint_type_name_c *symbol); msousa@257: void *visit(safeudint_type_name_c *symbol); msousa@257: void *visit(safeulint_type_name_c *symbol); msousa@257: void *visit(safereal_type_name_c *symbol); msousa@257: void *visit(safelreal_type_name_c *symbol); msousa@257: // void *visit(safedate_type_name_c *symbol); msousa@257: // void *visit(safetod_type_name_c *symbol); msousa@257: // void *visit(safedt_type_name_c *symbol) ; msousa@257: void *visit(safebyte_type_name_c *symbol); msousa@257: void *visit(safeword_type_name_c *symbol); msousa@257: void *visit(safedword_type_name_c *symbol); msousa@257: void *visit(safelword_type_name_c *symbol); msousa@257: // void *visit(safestring_type_name_c *symbol); msousa@257: // void *visit(safewstring_type_name_c *symbol); ccb@202: ccb@202: /********************************/ ccb@202: /* B 1.3.3 - Derived data types */ ccb@202: /********************************/ ccb@202: #if 0 ccb@202: /* simple_type_name ':' simple_spec_init */ ccb@202: void *visit(simple_type_declaration_c *symbol); ccb@202: /* simple_specification ASSIGN constant */ ccb@202: void *visit(simple_spec_init_c *symbol); ccb@202: /* subrange_type_name ':' subrange_spec_init */ ccb@202: void *visit(subrange_type_declaration_c *symbol); ccb@202: /* subrange_specification ASSIGN signed_integer */ ccb@202: void *visit(subrange_spec_init_c *symbol); ccb@202: /* integer_type_name '(' subrange')' */ ccb@202: void *visit(subrange_specification_c *symbol); ccb@202: /* signed_integer DOTDOT signed_integer */ ccb@202: void *visit(subrange_c *symbol); ccb@202: ccb@202: /* enumerated_type_name ':' enumerated_spec_init */ ccb@202: void *visit(enumerated_type_declaration_c *symbol); ccb@202: /* enumerated_specification ASSIGN enumerated_value */ ccb@202: void *visit(enumerated_spec_init_c *symbol); ccb@202: /* helper symbol for enumerated_specification->enumerated_spec_init */ ccb@202: /* enumerated_value_list ',' enumerated_value */ ccb@202: void *visit(enumerated_value_list_c *symbol); ccb@202: /* enumerated_type_name '#' identifier */ ccb@202: // SYM_REF2(enumerated_value_c, type, value) ccb@202: void *visit(enumerated_value_c *symbol); ccb@202: /* identifier ':' array_spec_init */ ccb@202: void *visit(array_type_declaration_c *symbol); ccb@202: /* array_specification [ASSIGN array_initialization} */ ccb@202: /* array_initialization may be NULL ! */ ccb@202: void *visit(array_spec_init_c *symbol); ccb@202: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ ccb@202: void *visit(array_specification_c *symbol); ccb@202: /* helper symbol for array_specification */ ccb@202: /* array_subrange_list ',' subrange */ ccb@202: void *visit(array_subrange_list_c *symbol); ccb@202: /* array_initialization: '[' array_initial_elements_list ']' */ ccb@202: /* helper symbol for array_initialization */ ccb@202: /* array_initial_elements_list ',' array_initial_elements */ ccb@202: void *visit(array_initial_elements_list_c *symbol); ccb@202: /* integer '(' [array_initial_element] ')' */ ccb@202: /* array_initial_element may be NULL ! */ ccb@202: void *visit(array_initial_elements_c *symbol); ccb@202: /* structure_type_name ':' structure_specification */ ccb@202: /* NOTE: structure_specification will point to either a ccb@202: * initialized_structure_c ccb@202: * OR A ccb@202: * structure_element_declaration_list_c ccb@202: */ ccb@202: void *visit(structure_type_declaration_c *symbol); ccb@202: /* structure_type_name ASSIGN structure_initialization */ ccb@202: /* structure_initialization may be NULL ! */ ccb@202: void *visit(initialized_structure_c *symbol); ccb@202: /* helper symbol for structure_declaration */ ccb@202: /* structure_declaration: STRUCT structure_element_declaration_list END_STRUCT */ ccb@202: /* structure_element_declaration_list structure_element_declaration ';' */ ccb@202: void *visit(structure_element_declaration_list_c *symbol); ccb@202: /* structure_element_name ':' *_spec_init */ ccb@202: void *visit(structure_element_declaration_c *symbol); ccb@202: /* helper symbol for structure_initialization */ ccb@202: /* structure_initialization: '(' structure_element_initialization_list ')' */ ccb@202: /* structure_element_initialization_list ',' structure_element_initialization */ ccb@202: void *visit(structure_element_initialization_list_c *symbol); ccb@202: /* structure_element_name ASSIGN value */ ccb@202: void *visit(structure_element_initialization_c *symbol); ccb@202: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */ ccb@202: /* ccb@202: SYM_REF4(string_type_declaration_c, string_type_name, ccb@202: elementary_string_type_name, ccb@202: string_type_declaration_size, ccb@202: string_type_declaration_init) // may be == NULL! ccb@202: */ ccb@202: void *visit(string_type_declaration_c *symbol); ccb@202: #endif ccb@202: }; // get_sizeof_datatype_c ccb@202: ccb@202: ccb@202: ccb@202: