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: /* conti@559: * TODO: conti@559: * - Check subscript values fall within allowed range. conti@559: * For the checking of subscript values to work correctly, we would need to have constant folding working too: conti@559: * array_var[8 + 99] can not be checked without constant folding. conti@559: * However, even without constant folding range check may be usefull, conti@559: * and later changing it to use the values coming out of constant folding should not be very difficult. conti@559: * conti@559: */ conti@559: conti@559: msousa@560: #include "array_range_check.hh" 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: conti@581: #define GET_CVALUE(dtype, symbol) ((symbol)->const_value_##dtype->value) conti@581: #define VALID_CVALUE(dtype, symbol) ((NULL != (symbol)->const_value_##dtype) && (symbol_c::cs_const_value == (symbol)->const_value_##dtype->status)) conti@581: conti@581: 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@559: } conti@559: msousa@560: array_range_check_c::~array_range_check_c(void) { conti@559: } conti@559: msousa@560: int array_range_check_c::get_error_count() { conti@559: return error_count; conti@559: } conti@559: 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: 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@583: msousa@583: if ( VALID_CVALUE( int64, l->elements[i]) && VALID_CVALUE( int64, dimension->lower_limit)) msousa@583: if ( GET_CVALUE( int64, l->elements[i]) < GET_CVALUE( int64, dimension->lower_limit)) msousa@583: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;} conti@581: 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)) msousa@583: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;} msousa@583: msousa@583: if ( VALID_CVALUE(uint64, l->elements[i]) && VALID_CVALUE(uint64, dimension->lower_limit)) msousa@583: if ( GET_CVALUE(uint64, l->elements[i]) < GET_CVALUE(uint64, dimension->lower_limit)) msousa@583: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); 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)) msousa@583: {STAGE3_ERROR(0, symbol, symbol, "Array access out of bounds."); continue;} conti@581: } conti@581: } conti@581: 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@560: void *array_range_check_c::visit(function_declaration_c *symbol) { 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@560: void *array_range_check_c::visit(function_block_declaration_c *symbol) { 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@560: void *array_range_check_c::visit(program_declaration_c *symbol) { 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: