mjsousa@1000: /*
mjsousa@1000: * matiec - a compiler for the programming languages defined in IEC 61131-3
mjsousa@1000: *
mjsousa@1000: * Copyright (C) 2015 Mario de Sousa (msousa@fe.up.pt)
mjsousa@1000: *
mjsousa@1000: *
mjsousa@1000: * This program is free software: you can redistribute it and/or modify
mjsousa@1000: * it under the terms of the GNU General Public License as published by
mjsousa@1000: * the Free Software Foundation, either version 3 of the License, or
mjsousa@1000: * (at your option) any later version.
mjsousa@1000: *
mjsousa@1000: * This program is distributed in the hope that it will be useful,
mjsousa@1000: * but WITHOUT ANY WARRANTY; without even the implied warranty of
mjsousa@1000: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mjsousa@1000: * GNU General Public License for more details.
mjsousa@1000: *
mjsousa@1000: * You should have received a copy of the GNU General Public License
mjsousa@1000: * along with this program. If not, see .
mjsousa@1000: *
mjsousa@1000: *
mjsousa@1000: * This code is made available on the understanding that it will not be
mjsousa@1000: * used in safety-critical situations without a full and competent review.
mjsousa@1000: */
mjsousa@1000:
mjsousa@1000: /*
mjsousa@1000: * An IEC 61131-3 compiler.
mjsousa@1000: *
mjsousa@1000: * Based on the
mjsousa@1000: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
mjsousa@1000: *
mjsousa@1000: */
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: /*
mjsousa@1000: * Case Options Checking:
mjsousa@1000: * - Check whether the options in a case statement are repeated, either directly, or in a range.
mjsousa@1000: * For example:
mjsousa@1000: * case var of
mjsousa@1000: * 1: ... <- OK
mjsousa@1000: * 2: ... <- OK
mjsousa@1000: * 1: ... <- OK (not an error), but produce a warning due to repeated '1'!
mjsousa@1000: * 0..8: ...<- OK (not an error), but produce a warning cue to repeated '1' and '2'!
mjsousa@1000: */
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: #include "case_elements_check.hh"
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2))
mjsousa@1000: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2))
mjsousa@1000:
mjsousa@1000: #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \
mjsousa@1000: if (current_display_error_level >= error_level) { \
mjsousa@1000: fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \
mjsousa@1000: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
mjsousa@1000: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\
mjsousa@1000: fprintf(stderr, __VA_ARGS__); \
mjsousa@1000: fprintf(stderr, "\n"); \
mjsousa@1000: error_count++; \
mjsousa@1000: } \
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: #define STAGE3_WARNING(symbol1, symbol2, ...) { \
mjsousa@1000: fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \
mjsousa@1000: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
mjsousa@1000: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\
mjsousa@1000: fprintf(stderr, __VA_ARGS__); \
mjsousa@1000: fprintf(stderr, "\n"); \
mjsousa@1000: warning_found = true; \
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: #define GET_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.get())
mjsousa@1000: #define VALID_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.is_valid())
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: case_elements_check_c::case_elements_check_c(symbol_c *ignore) {
mjsousa@1000: warning_found = false;
mjsousa@1000: error_count = 0;
mjsousa@1000: current_display_error_level = 0;
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: case_elements_check_c::~case_elements_check_c(void) {
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: int case_elements_check_c::get_error_count() {
mjsousa@1000: return error_count;
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: /* compare two integer constants, and determins if s1 < s2 */
mjsousa@1000: static bool less_than(symbol_c *s1, symbol_c *s2) {
mjsousa@1000: if ( (VALID_CVALUE( int64, s1))
mjsousa@1000: && (VALID_CVALUE( int64, s2))
mjsousa@1000: && ( GET_CVALUE( int64, s1) < GET_CVALUE( int64, s2)))
mjsousa@1000: return true;
mjsousa@1000:
mjsousa@1000: if ( (VALID_CVALUE(uint64, s1))
mjsousa@1000: && (VALID_CVALUE(uint64, s2))
mjsousa@1000: && ( GET_CVALUE(uint64, s1) < GET_CVALUE(uint64, s2)))
mjsousa@1000: return true;
mjsousa@1000:
mjsousa@1000: if ( (VALID_CVALUE( int64, s1))
mjsousa@1000: && (VALID_CVALUE(uint64, s2))
mjsousa@1000: && ( GET_CVALUE( int64, s1) < 0))
mjsousa@1000: return true;
mjsousa@1000:
mjsousa@1000: return false;
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: void case_elements_check_c::check_subr_subr(symbol_c *s1, symbol_c *s2) {
mjsousa@1000: subrange_c *sub1 = dynamic_cast(s1);
mjsousa@1000: subrange_c *sub2 = dynamic_cast(s2);
mjsousa@1000:
mjsousa@1000: if ((NULL == sub1) || (NULL == sub2)) return;
mjsousa@1000: symbol_c *l1 = sub1->lower_limit;
mjsousa@1000: symbol_c *u1 = sub1->upper_limit;
mjsousa@1000: symbol_c *l2 = sub2->lower_limit;
mjsousa@1000: symbol_c *u2 = sub2->upper_limit;
mjsousa@1000:
mjsousa@1000: if (less_than(u1, l2)) return; // no overlap!
mjsousa@1000: if (less_than(u2, l1)) return; // no overlap!
mjsousa@1000:
mjsousa@1000: if ( (VALID_CVALUE( int64, l1) || (VALID_CVALUE(uint64, l1)))
mjsousa@1000: && (VALID_CVALUE( int64, l2) || (VALID_CVALUE(uint64, l2)))
mjsousa@1000: && (VALID_CVALUE( int64, u1) || (VALID_CVALUE(uint64, u1)))
mjsousa@1000: && (VALID_CVALUE( int64, u2) || (VALID_CVALUE(uint64, u2))))
mjsousa@1000: STAGE3_WARNING(s1, s2, "Elements in CASE options have overlapping ranges.");
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: void case_elements_check_c::check_subr_symb(symbol_c *s1, symbol_c *s2) {
mjsousa@1000: subrange_c *subr = NULL;
mjsousa@1000: symbol_c *symb = NULL;
mjsousa@1000: if ((subr = dynamic_cast(s1)) != NULL) {symb = s2;}
mjsousa@1000: if ((subr = dynamic_cast(s2)) != NULL) {symb = s1;}
mjsousa@1000:
mjsousa@1000: if ((NULL == subr) || (NULL == symb)) return;
mjsousa@1000: symbol_c *lowl = subr->lower_limit;
mjsousa@1000: symbol_c *uppl = subr->upper_limit;
mjsousa@1000:
mjsousa@1000: if ( (VALID_CVALUE(int64, symb))
mjsousa@1000: && (VALID_CVALUE(int64, lowl))
mjsousa@1000: && (VALID_CVALUE(int64, uppl))
mjsousa@1000: && ( GET_CVALUE(int64, symb) >= GET_CVALUE(int64, lowl))
mjsousa@1000: && ( GET_CVALUE(int64, symb) <= GET_CVALUE(int64, uppl)))
mjsousa@1000: {STAGE3_WARNING(s1, s2, "Element in CASE option falls within range of another element."); return;}
mjsousa@1000:
mjsousa@1000: if ( (VALID_CVALUE(uint64, symb))
mjsousa@1000: && (VALID_CVALUE( int64, lowl))
mjsousa@1000: && (VALID_CVALUE(uint64, uppl))
mjsousa@1000: && ( GET_CVALUE( int64, lowl) < 0)
mjsousa@1000: && ( GET_CVALUE(uint64, symb) <= GET_CVALUE(uint64, uppl)))
mjsousa@1000: {STAGE3_WARNING(s1, s2, "Element in CASE option falls within range of another element."); return;}
mjsousa@1000:
mjsousa@1000: if ( (VALID_CVALUE(uint64, symb))
mjsousa@1000: && (VALID_CVALUE(uint64, lowl))
mjsousa@1000: && (VALID_CVALUE(uint64, uppl))
mjsousa@1000: && ( GET_CVALUE(uint64, symb) >= GET_CVALUE(uint64, lowl))
mjsousa@1000: && ( GET_CVALUE(uint64, symb) <= GET_CVALUE(uint64, uppl)))
mjsousa@1000: {STAGE3_WARNING(s1, s2, "Element in CASE option falls within range of another element."); return;}
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: #include
mjsousa@1000: void case_elements_check_c::check_symb_symb(symbol_c *s1, symbol_c *s2) {
mjsousa@1000: if ( (dynamic_cast(s1) != NULL)
mjsousa@1000: || (dynamic_cast(s2) != NULL))
mjsousa@1000: return; // only run this test if neither s1 nor s2 are subranges!
mjsousa@1000:
mjsousa@1000: if ( (s1->const_value.is_const() && s2->const_value.is_const() && (s1->const_value == s2->const_value)) // if const, then compare const values (using overloaded '==' operator!)
mjsousa@1000: || (compare_identifiers(s1, s2) == 0)) // if token_c, compare tokens! (compare_identifiers() returns 0 when equal tokens!, -1 when either is not token_c)
mjsousa@1000: STAGE3_WARNING(s1, s2, "Duplicate element found in CASE options.");
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: /***************************************/
mjsousa@1000: /* B.3 - Language ST (Structured Text) */
mjsousa@1000: /***************************************/
mjsousa@1000: /********************/
mjsousa@1000: /* B 3.2 Statements */
mjsousa@1000: /********************/
mjsousa@1000: /********************************/
mjsousa@1000: /* B 3.2.3 Selection Statements */
mjsousa@1000: /********************************/
mjsousa@1000: /* CASE expression OF case_element_list ELSE statement_list END_CASE */
mjsousa@1000: // SYM_REF3(case_statement_c, expression, case_element_list, statement_list)
mjsousa@1000: void *case_elements_check_c::visit(case_statement_c *symbol) {
mjsousa@1000: std::vector case_elements_list_local = case_elements_list; // Required when source code contains CASE inside another CASE !
mjsousa@1000:
mjsousa@1000: case_elements_list.clear();
mjsousa@1000: symbol->case_element_list->accept(*this); // will fill up the case_elements_list with all the elements in the case!
mjsousa@1000:
mjsousa@1000: // OK, now check whether we have any overlappings...
mjsousa@1000: std::vector::iterator s1 = case_elements_list.begin();
mjsousa@1000: for ( ; s1 != case_elements_list.end(); s1++) {
mjsousa@1000: std::vector::iterator s2 = s1;
mjsousa@1000: s2++; // do not compare the value with itself!
mjsousa@1000: for (; s2 != case_elements_list.end(); s2++) {
mjsousa@1000: // Check for overlapping elements
mjsousa@1000: check_subr_subr(*s1, *s2);
mjsousa@1000: check_subr_symb(*s1, *s2);
mjsousa@1000: check_symb_symb(*s2, *s1);
mjsousa@1000: }
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000: case_elements_list = case_elements_list_local;
mjsousa@1000: return NULL;
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000: /* helper symbol for case_statement */
mjsousa@1000: // SYM_LIST(case_element_list_c)
mjsousa@1000: // void *case_elements_check_c::visit(case_element_list_c *symbol) // not needed! We inherit from iterator_visitor_c
mjsousa@1000:
mjsousa@1000: /* case_list ':' statement_list */
mjsousa@1000: // SYM_REF2(case_element_c, case_list, statement_list)
mjsousa@1000: // void *case_elements_check_c::visit(case_element_c *symbol) // not needed! We inherit from iterator_visitor_c
mjsousa@1000:
mjsousa@1000:
mjsousa@1000: // SYM_LIST(case_list_c)
mjsousa@1000: void *case_elements_check_c::visit(case_list_c *symbol) {
mjsousa@1000: for (int i = 0; i < symbol->n; i++)
msousa@1041: case_elements_list.push_back(symbol->get_element(i));
mjsousa@1000: return NULL;
mjsousa@1000: }
mjsousa@1000:
mjsousa@1000: