Start sematinc range check class.
authorManuele Conti <conti.ma@alice.it>
Tue, 15 May 2012 18:59:32 +0200
changeset 559 a3b8925e640c
parent 558 9273dfc5fa7c
child 560 13b5b7faa3d7
Start sematinc range check class.
stage3/Makefile.am
stage3/range_check.cc
stage3/range_check.hh
stage3/stage3.cc
--- a/stage3/Makefile.am	Tue May 15 15:50:43 2012 +0100
+++ b/stage3/Makefile.am	Tue May 15 18:59:32 2012 +0200
@@ -9,5 +9,6 @@
 	narrow_candidate_datatypes.cc \
 	print_datatypes_error.cc \
 	datatype_functions.cc \
-	lvalue_check.cc
+	lvalue_check.cc \
+        range_check.cc
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage3/range_check.cc	Tue May 15 18:59:32 2012 +0200
@@ -0,0 +1,175 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
+ *
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * TODO:
+ *   - Check subscript values fall within allowed range.
+ *     For the checking of subscript values to work correctly, we would need to have constant folding working too:
+ *     array_var[8 + 99] can not be checked without constant folding.
+ *     However, even without constant folding range check may be usefull,
+ *     and later changing it to use the values coming out of constant folding should not be very difficult.
+ *
+ */
+
+
+#include "range_check.hh"
+
+#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
+#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
+
+#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
+  if (current_display_error_level >= error_level) {                                                                         \
+    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
+            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
+                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
+    fprintf(stderr, __VA_ARGS__);                                                                                           \
+    fprintf(stderr, "\n");                                                                                                  \
+    error_count++;                                                                                                     \
+  }                                                                                                                         \
+}
+
+
+#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
+    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
+            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
+                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
+    fprintf(stderr, __VA_ARGS__);                                                                                           \
+    fprintf(stderr, "\n");                                                                                                  \
+    warning_found = true;                                                                                                   \
+}
+
+
+range_check_c::range_check_c(symbol_c *ignore) {
+	error_count = 0;
+	current_display_error_level = 0;
+}
+
+range_check_c::~range_check_c(void) {
+}
+
+int range_check_c::get_error_count() {
+	return error_count;
+}
+
+void range_check_c::check_range_array_check(array_variable_c *symbol) {
+	int dimension_count;
+	symbol_c* dimension;
+	var_declarations_c *var_decl;
+	array_dimension_iterator_c* array_dimension_iterator;
+
+	var_decl = (var_declarations_c *)search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
+	array_dimension_iterator = new array_dimension_iterator_c(var_decl);
+	for (dimension_count = 0; NULL != array_dimension_iterator->next(); dimension_count++);
+	if (dimension_count != ((list_c *)symbol->subscript_list)->n)
+		STAGE3_ERROR(0, symbol, symbol, "Number of dimensions does not match, Array have %d dimension(s)", dimension_count);
+}
+
+/*********************/
+/* B 1.4 - Variables */
+/*********************/
+/*************************************/
+/* B 1.4.2 - Multi-element variables */
+/*************************************/
+void *range_check_c::visit(array_variable_c *symbol) {
+	check_range_array_check(symbol);
+	return NULL;
+}
+
+
+/**************************************/
+/* B 1.5 - Program organisation units */
+/**************************************/
+/***********************/
+/* B 1.5.1 - Functions */
+/***********************/
+void *range_check_c::visit(function_declaration_c *symbol) {
+	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
+	search_var_instance_decl = new search_var_instance_decl_c(symbol);
+	symbol->function_body->accept(*this);
+	delete search_varfb_instance_type;
+	delete search_var_instance_decl;
+	search_varfb_instance_type = NULL;
+	search_var_instance_decl = NULL;
+	return NULL;
+}
+
+/*****************************/
+/* B 1.5.2 - Function blocks */
+/*****************************/
+void *range_check_c::visit(function_block_declaration_c *symbol) {
+	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
+	search_var_instance_decl = new search_var_instance_decl_c(symbol);
+	symbol->fblock_body->accept(*this);
+	delete search_varfb_instance_type;
+	delete search_var_instance_decl;
+	search_varfb_instance_type = NULL;
+	search_var_instance_decl = NULL;
+	return NULL;
+}
+
+/**********************/
+/* B 1.5.3 - Programs */
+/**********************/
+void *range_check_c::visit(program_declaration_c *symbol) {
+	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
+	search_var_instance_decl = new search_var_instance_decl_c(symbol);
+	symbol->function_block_body->accept(*this);
+	delete search_varfb_instance_type;
+	delete search_var_instance_decl;
+	search_varfb_instance_type = NULL;
+	search_var_instance_decl = NULL;
+	return NULL;
+}
+
+
+
+/***************************************/
+/* B.3 - Language ST (Structured Text) */
+/***************************************/
+/*********************************/
+/* B 3.2.1 Assignment Statements */
+/*********************************/
+void *range_check_c::visit(assignment_statement_c *symbol) {
+	symbol->l_exp->accept(*this);
+    symbol->r_exp->accept(*this);
+	return NULL;
+}
+
+
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage3/range_check.hh	Tue May 15 18:59:32 2012 +0200
@@ -0,0 +1,103 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2009-2012  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2012       Manuele Conti  (conti.ma@alice.it)
+ *
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+#include <vector>
+#include "../absyntax_utils/absyntax_utils.hh"
+#include "datatype_functions.hh"
+
+
+
+
+class range_check_c: public iterator_visitor_c {
+
+  private:
+    search_varfb_instance_type_c *search_varfb_instance_type;
+    search_var_instance_decl_c *search_var_instance_decl;
+    search_base_type_c search_base_type;
+    int error_count;
+    int current_display_error_level;
+
+    void check_range_array_check(array_variable_c *symbol);
+
+  public:
+    range_check_c(symbol_c *ignore);
+    virtual ~range_check_c(void);
+    int get_error_count();
+
+    /*********************/
+    /* B 1.4 - Variables */
+    /*********************/
+    /*************************************/
+    /* B 1.4.2 - Multi-element variables */
+    /*************************************/
+    void *visit(array_variable_c *symbol);
+
+    /**************************************/
+    /* B 1.5 - Program organisation units */
+    /**************************************/
+    /***********************/
+    /* B 1.5.1 - Functions */
+    /***********************/
+    void *visit(function_declaration_c *symbol);
+
+    /*****************************/
+    /* B 1.5.2 - Function blocks */
+    /*****************************/
+    void *visit(function_block_declaration_c *symbol);
+
+    /**********************/
+    /* B 1.5.3 - Programs */
+    /**********************/
+    void *visit(program_declaration_c *symbol);
+
+
+    /***************************************/
+    /* B.3 - Language ST (Structured Text) */
+    /***************************************/
+
+    /*********************************/
+    /* B 3.2.1 Assignment Statements */
+    /*********************************/
+    void *visit(assignment_statement_c *symbol);
+
+
+
+
+}; /* range_check_c */
+
+
+
+
+
+
+
--- a/stage3/stage3.cc	Tue May 15 15:50:43 2012 +0100
+++ b/stage3/stage3.cc	Tue May 15 18:59:32 2012 +0200
@@ -39,7 +39,7 @@
 #include "narrow_candidate_datatypes.hh"
 #include "print_datatypes_error.hh"
 #include "lvalue_check.hh"
-
+#include "range_check.hh"
 
 
 static int flow_control_analysis(symbol_c *tree_root){
@@ -72,12 +72,19 @@
 	return lvalue_check.get_error_count();
 }
 
+static int range_check(symbol_c *tree_root){
+	range_check_c range_check(tree_root);
+	tree_root->accept(range_check);
+	return range_check.get_error_count();
+}
+
 
 int stage3(symbol_c *tree_root){
 	int error_count = 0;
 	error_count += flow_control_analysis(tree_root);
 	error_count += type_safety(tree_root);
 	error_count += lvalue_check(tree_root);
+	error_count += range_check(tree_root);
 	
 	if (error_count > 0) {
 		fprintf(stderr, "%d error(s) found. Bailing out!\n", error_count);