conti@559: /* conti@559: * matiec - a compiler for the programming languages defined in IEC 61131-3 conti@559: * conti@559: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) conti@559: * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) conti@559: * conti@559: * conti@559: * This program is free software: you can redistribute it and/or modify conti@559: * it under the terms of the GNU General Public License as published by conti@559: * the Free Software Foundation, either version 3 of the License, or conti@559: * (at your option) any later version. conti@559: * conti@559: * This program is distributed in the hope that it will be useful, conti@559: * but WITHOUT ANY WARRANTY; without even the implied warranty of conti@559: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the conti@559: * GNU General Public License for more details. conti@559: * conti@559: * You should have received a copy of the GNU General Public License conti@559: * along with this program. If not, see . conti@559: * conti@559: * conti@559: * This code is made available on the understanding that it will not be conti@559: * used in safety-critical situations without a full and competent review. conti@559: */ conti@559: conti@559: /* conti@559: * An IEC 61131-3 compiler. conti@559: * conti@559: * Based on the conti@559: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) conti@559: * conti@559: */ conti@559: conti@559: conti@559: /* msousa@614: * Array Range Checking: msousa@614: * - Check whether array subscript values fall within the allowed range. msousa@614: * Note that for the checking of subscript values to work correctly, we need to have constant folding working too: conti@559: * array_var[8 + 99] can not be checked without constant folding. conti@559: */ conti@559: conti@559: msousa@560: #include "array_range_check.hh" msousa@592: #include // required for std::numeric_limits msousa@592: conti@559: conti@559: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) conti@559: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) conti@559: conti@559: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ conti@559: if (current_display_error_level >= error_level) { \ conti@559: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ conti@559: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@559: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@559: fprintf(stderr, __VA_ARGS__); \ conti@559: fprintf(stderr, "\n"); \ conti@559: error_count++; \ conti@559: } \ conti@559: } conti@559: conti@559: conti@559: #define STAGE3_WARNING(symbol1, symbol2, ...) { \ conti@559: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ conti@559: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ conti@559: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ conti@559: fprintf(stderr, __VA_ARGS__); \ conti@559: fprintf(stderr, "\n"); \ conti@559: warning_found = true; \ conti@559: } conti@559: mjsousa@965: mjsousa@965: #define GET_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.get()) mjsousa@965: #define VALID_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.is_valid()) conti@581: conti@620: /* The cmp_unsigned_signed function compares two numbers u and s. conti@620: * It returns an integer indicating the relationship between the numbers: conti@620: * - A zero value indicates that both numbers are equal. conti@620: * - A value greater than zero indicates that numbers does not match and conti@620: * first has a greater value. conti@620: * - A value less than zero indicates that numbers does not match and conti@620: * first has a lesser value. conti@620: */ conti@620: static inline int cmp_unsigned_signed(const uint64_t u, const int64_t s) { msousa@621: const uint64_t INT64_MAX_uvar = INT64_MAX; msousa@621: if (u <= INT64_MAX_uvar) conti@620: return ((int64_t)u - s); conti@620: return -1; conti@620: } conti@559: msousa@560: array_range_check_c::array_range_check_c(symbol_c *ignore) { conti@559: error_count = 0; conti@559: current_display_error_level = 0; conti@661: search_varfb_instance_type = NULL; conti@559: } conti@559: msousa@592: msousa@592: msousa@560: array_range_check_c::~array_range_check_c(void) { conti@559: } conti@559: msousa@592: msousa@592: msousa@560: int array_range_check_c::get_error_count() { conti@559: return error_count; conti@559: } conti@559: msousa@592: msousa@592: msousa@560: void array_range_check_c::check_dimension_count(array_variable_c *symbol) { conti@559: int dimension_count; msousa@560: symbol_c *var_decl; conti@559: msousa@560: var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable); msousa@560: array_dimension_iterator_c array_dimension_iterator(var_decl); msousa@560: for (dimension_count = 0; NULL != array_dimension_iterator.next(); dimension_count++); conti@559: if (dimension_count != ((list_c *)symbol->subscript_list)->n) msousa@583: STAGE3_ERROR(0, symbol, symbol, "Number of subscripts/indexes does not match the number of subscripts/indexes in the array's declaration (array has %d indexes)", dimension_count); conti@559: } conti@559: msousa@592: msousa@592: conti@581: void array_range_check_c::check_bounds(array_variable_c *symbol) { msousa@583: list_c *l; /* the subscript_list */ conti@581: symbol_c *var_decl; conti@581: conti@581: l = (list_c *)symbol->subscript_list; conti@581: var_decl = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable); conti@581: array_dimension_iterator_c array_dimension_iterator(var_decl); conti@581: for (int i = 0; i < l->n; i++) { conti@581: subrange_c *dimension = array_dimension_iterator.next(); msousa@585: /* mismatch between number of indexes/subscripts. This error will be caught in check_dimension_count() so we ignore it. */ msousa@585: if (NULL == dimension) msousa@585: return; msousa@599: msousa@599: /* Check lower limit */ msousa@583: if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit)) conti@620: if ( GET_CVALUE( int64, l->elements[i]) < GET_CVALUE( int64, dimension->lower_limit) ) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRId64", should be >= %"PRId64").", GET_CVALUE( int64, l->elements[i]), GET_CVALUE( int64, dimension->lower_limit)); continue;} conti@581: msousa@599: if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit)) conti@620: if ( cmp_unsigned_signed( GET_CVALUE(uint64, dimension->lower_limit), GET_CVALUE( int64, l->elements[i])) > 0 ) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRId64", should be >= %"PRIu64").", GET_CVALUE( int64, l->elements[i]), GET_CVALUE(uint64, dimension->lower_limit)); continue;} msousa@599: msousa@599: if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit)) msousa@599: if ( GET_CVALUE(uint64, l->elements[i]) < GET_CVALUE(uint64, dimension->lower_limit)) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRIu64", should be >= %"PRIu64").", GET_CVALUE(uint64, l->elements[i]), GET_CVALUE(uint64, dimension->lower_limit)); continue;} msousa@599: msousa@599: if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit)) conti@620: if ( cmp_unsigned_signed(GET_CVALUE(uint64, l->elements[i]), GET_CVALUE( int64, dimension->lower_limit)) < 0 ) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRIu64", should be >= %"PRId64").", GET_CVALUE(uint64, l->elements[i]), GET_CVALUE( int64, dimension->lower_limit)); continue;} msousa@599: msousa@599: /* Repeat the same check, now for upper limit */ msousa@583: if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit)) msousa@583: if ( GET_CVALUE( int64, l->elements[i]) > GET_CVALUE( int64, dimension->upper_limit)) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRId64", should be <= %"PRId64").", GET_CVALUE( int64, l->elements[i]), GET_CVALUE( int64, dimension->upper_limit)); continue;} msousa@583: msousa@599: if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit)) conti@620: if ( cmp_unsigned_signed( GET_CVALUE(uint64, dimension->upper_limit), GET_CVALUE( int64, l->elements[i])) < 0 ) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRId64", should be <= %"PRIu64").", GET_CVALUE( int64, l->elements[i]), GET_CVALUE(uint64, dimension->upper_limit)); continue;} msousa@583: msousa@583: if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->upper_limit)) msousa@583: if ( GET_CVALUE(uint64, l->elements[i]) > GET_CVALUE(uint64, dimension->upper_limit)) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRIu64", should be <= %"PRIu64").", GET_CVALUE(uint64, l->elements[i]), GET_CVALUE(uint64, dimension->upper_limit)); continue;} msousa@594: msousa@599: if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE( int64, dimension->upper_limit)) Laurent@625: if ( cmp_unsigned_signed(GET_CVALUE(uint64, l->elements[i]), GET_CVALUE( int64, dimension->upper_limit)) > 0 ) mjsousa@977: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds (using constant value of %"PRIu64", should be <= %"PRId64").", GET_CVALUE(uint64, l->elements[i]), GET_CVALUE( int64, dimension->upper_limit)); continue;} msousa@599: conti@581: } conti@581: } conti@581: msousa@592: msousa@592: msousa@592: msousa@592: msousa@592: msousa@592: msousa@592: msousa@592: msousa@592: /*************************/ msousa@592: /* B.1 - Common elements */ msousa@592: /*************************/ msousa@592: /**********************/ msousa@592: /* B.1.3 - Data types */ msousa@592: /**********************/ msousa@592: /********************************/ msousa@592: /* B 1.3.3 - Derived data types */ msousa@592: /********************************/ msousa@592: msousa@592: /* signed_integer DOTDOT signed_integer */ msousa@592: /* dimension will be filled in during stage 3 (array_range_check_c) with the number of elements in this subrange */ msousa@592: // SYM_REF2(subrange_c, lower_limit, upper_limit, unsigned long long int dimension) msousa@592: void *array_range_check_c::visit(subrange_c *symbol) { msousa@592: unsigned long long int dimension = 0; // we use unsigned long long instead of uint64_t since it might just happen to be larger than uint64_t in the platform used for compiling this code!! msousa@592: msousa@598: /* Determine the size of the array... */ msousa@592: if (VALID_CVALUE( int64, symbol->upper_limit) && VALID_CVALUE( int64, symbol->lower_limit)) { msousa@592: // do the sums in such a way that no overflow is possible... even during intermediate steps used by compiler! msousa@592: // remember that the result (dimension) is unsigned, while the operands are signed!! msousa@592: // dimension = GET_CVALUE( int64, symbol->upper_limit) - VALID_CVALUE( int64, symbol->lower_limit); mjsousa@975: if (GET_CVALUE( int64, symbol->lower_limit) > GET_CVALUE( int64, symbol->upper_limit)) { mjsousa@975: STAGE3_ERROR(0, symbol, symbol, "Subrange has lower limit (%"PRId64") larger than upper limit (%"PRId64").", GET_CVALUE( int64, symbol->lower_limit), GET_CVALUE( int64, symbol->upper_limit)); mjsousa@975: dimension = std::numeric_limits< unsigned long long int >::max() - 1; // -1 because it will be incremented at the end of this function!! mjsousa@975: } else if (GET_CVALUE( int64, symbol->lower_limit) >= 0) { msousa@592: dimension = GET_CVALUE( int64, symbol->upper_limit) - GET_CVALUE( int64, symbol->lower_limit); msousa@592: } else { msousa@592: dimension = -GET_CVALUE( int64, symbol->lower_limit); msousa@592: dimension += GET_CVALUE( int64, symbol->upper_limit); msousa@592: } msousa@592: } else if (VALID_CVALUE(uint64, symbol->upper_limit) && VALID_CVALUE(uint64, symbol->lower_limit)) { mjsousa@975: if (GET_CVALUE(uint64, symbol->lower_limit) > GET_CVALUE(uint64, symbol->upper_limit)) { mjsousa@975: STAGE3_ERROR(0, symbol, symbol, "Subrange has lower limit (%"PRIu64") larger than upper limit (%"PRIu64").", GET_CVALUE(uint64, symbol->lower_limit), GET_CVALUE(uint64, symbol->upper_limit)); mjsousa@975: dimension = std::numeric_limits< unsigned long long int >::max() - 1; // -1 because it will be incremented at the end of this function!! mjsousa@975: } else mjsousa@975: dimension = GET_CVALUE(uint64, symbol->upper_limit) - GET_CVALUE(uint64, symbol->lower_limit); msousa@592: } else if (VALID_CVALUE(uint64, symbol->upper_limit) && VALID_CVALUE( int64, symbol->lower_limit)) { mjsousa@975: // No need to check whether lower_limit > upper_limit, as we only reach this point if lower_limit < 0 (and upper_limit must be >= 0!) msousa@598: if (GET_CVALUE( int64, symbol->lower_limit) >= 0) { msousa@598: dimension = GET_CVALUE(uint64, symbol->upper_limit) - GET_CVALUE( int64, symbol->lower_limit); msousa@592: } else { msousa@598: unsigned long long int lower_ull; msousa@598: lower_ull = -GET_CVALUE( int64, symbol->lower_limit); msousa@598: dimension = GET_CVALUE(uint64, symbol->upper_limit) + lower_ull; msousa@598: if (dimension < lower_ull) msousa@598: STAGE3_ERROR(0, symbol, symbol, "Number of elements in array subrange exceeds maximum number of elements (%llu).", std::numeric_limits< unsigned long long int >::max()); msousa@592: } mjsousa@975: } else if (!VALID_CVALUE(uint64, symbol->upper_limit) && !VALID_CVALUE( int64, symbol->upper_limit)) { mjsousa@975: STAGE3_ERROR(0, symbol->upper_limit, symbol->upper_limit, "Subrange upper limit is not a constant value."); mjsousa@975: // set dimension to largest possible value so we do not get any further related error messages. mjsousa@975: dimension = std::numeric_limits< unsigned long long int >::max() - 1; // -1 because it will be incremented at the end of this function!! mjsousa@975: } else if (!VALID_CVALUE(uint64, symbol->lower_limit) && !VALID_CVALUE( int64, symbol->lower_limit)) { mjsousa@975: STAGE3_ERROR(0, symbol->lower_limit, symbol->lower_limit, "Subrange lower limit is not a constant value."); mjsousa@975: // set dimension to largest possible value so we do not get any further related error messages. mjsousa@975: dimension = std::numeric_limits< unsigned long long int >::max() - 1; // -1 because it will be incremented at the end of this function!! mjsousa@976: /* NOTE: Note that both the "subrange *** limit is not a constant value" error messages are only necessary due to an extension mjsousa@976: * that matiec supports by allowing non-literals in subrange declarations (currently only subranges in array declarations). mjsousa@976: * e.g.: array_var: ARRAY [1..max] of INT; <--- illegal according to IEC 61131-1 due to the 'max' non literal mjsousa@976: * Matiec will allow the above syntax, as long as the 'max' variable can be determined to be constant throughout mjsousa@976: * the program execution at runtime (and not only constant when program initiates) - for example, a VAR CONSTANT mjsousa@976: * variable. mjsousa@976: * These two checks will verify if we were able to fold the variable into a constant value, or not. mjsousa@976: */ mjsousa@970: } else {ERROR;} msousa@592: msousa@592: /* correct value for dimension is actually ---> dimension = upper_limit - lower_limit + 1 msousa@592: * Up to now, we have only determined dimension = upper_limit - lower_limit msousa@592: * We must first check whether this last increment will cause an overflow! msousa@592: */ msousa@592: if (dimension == std::numeric_limits< unsigned long long int >::max()) msousa@592: STAGE3_ERROR(0, symbol, symbol, "Number of elements in array subrange exceeds maximum number of elements (%llu).", std::numeric_limits< unsigned long long int >::max()); msousa@592: msousa@592: /* correct value for dimension is actually ---> dimension = upper_limit - lower_limit + 1 */ msousa@592: dimension++; msousa@592: msousa@592: symbol->dimension = dimension; msousa@592: return NULL; msousa@592: } msousa@592: msousa@592: msousa@598: msousa@598: msousa@598: msousa@598: /* integer '(' [array_initial_element] ')' */ msousa@598: /* array_initial_element may be NULL ! */ msousa@598: // SYM_REF2(array_initial_elements_c, integer, array_initial_element) msousa@598: void *array_range_check_c::visit(array_initial_elements_c *symbol) { msousa@598: if (VALID_CVALUE( int64, symbol->integer) && (GET_CVALUE( int64, symbol->integer) < 0)) msousa@598: ERROR; /* the IEC 61131-3 syntax guarantees that this value will never be negative! */ msousa@598: msousa@598: /* TODO: check that the total number of 'initial values' does not exceed the size of the array! */ msousa@598: msousa@598: return NULL; msousa@598: } msousa@598: msousa@598: msousa@598: msousa@598: msousa@598: msousa@598: msousa@598: msousa@598: msousa@598: conti@559: /*********************/ conti@559: /* B 1.4 - Variables */ conti@559: /*********************/ conti@559: /*************************************/ conti@559: /* B 1.4.2 - Multi-element variables */ conti@559: /*************************************/ msousa@560: void *array_range_check_c::visit(array_variable_c *symbol) { msousa@560: check_dimension_count(symbol); conti@581: check_bounds(symbol); conti@559: return NULL; conti@559: } conti@559: conti@559: conti@559: /**************************************/ conti@559: /* B 1.5 - Program organisation units */ conti@559: /**************************************/ conti@559: /***********************/ conti@559: /* B 1.5.1 - Functions */ conti@559: /***********************/ msousa@592: // SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body) msousa@560: void *array_range_check_c::visit(function_declaration_c *symbol) { msousa@592: symbol->var_declarations_list->accept(*this); // required for visiting subrange_c conti@559: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@560: // search_var_instance_decl = new search_var_instance_decl_c(symbol); conti@559: symbol->function_body->accept(*this); conti@559: delete search_varfb_instance_type; msousa@560: // delete search_var_instance_decl; conti@559: search_varfb_instance_type = NULL; msousa@560: // search_var_instance_decl = NULL; conti@559: return NULL; conti@559: } conti@559: conti@559: /*****************************/ conti@559: /* B 1.5.2 - Function blocks */ conti@559: /*****************************/ msousa@592: // SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body) msousa@560: void *array_range_check_c::visit(function_block_declaration_c *symbol) { msousa@592: symbol->var_declarations->accept(*this); // required for visiting subrange_c conti@559: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@560: // search_var_instance_decl = new search_var_instance_decl_c(symbol); conti@559: symbol->fblock_body->accept(*this); conti@559: delete search_varfb_instance_type; msousa@560: // delete search_var_instance_decl; conti@559: search_varfb_instance_type = NULL; msousa@560: // search_var_instance_decl = NULL; conti@559: return NULL; conti@559: } conti@559: conti@559: /**********************/ conti@559: /* B 1.5.3 - Programs */ conti@559: /**********************/ msousa@592: // SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body) msousa@560: void *array_range_check_c::visit(program_declaration_c *symbol) { msousa@592: symbol->var_declarations->accept(*this); // required for visiting subrange_c conti@559: search_varfb_instance_type = new search_varfb_instance_type_c(symbol); msousa@560: // search_var_instance_decl = new search_var_instance_decl_c(symbol); conti@559: symbol->function_block_body->accept(*this); conti@559: delete search_varfb_instance_type; msousa@560: // delete search_var_instance_decl; conti@559: search_varfb_instance_type = NULL; msousa@560: // search_var_instance_decl = NULL; conti@559: return NULL; conti@559: } conti@559: conti@559: conti@559: