Fix case statement in ST, adding support for enumerated types
authorlaurent
Fri, 02 Sep 2011 18:15:55 +0200
changeset 347 44ff2a6fcadc
parent 346 620fd98a021d
child 348 990ecb883731
Fix case statement in ST, adding support for enumerated types
Makefile
absyntax_utils/Makefile
absyntax_utils/absyntax_utils.hh
absyntax_utils/case_element_iterator.cc
absyntax_utils/case_element_iterator.hh
stage4/generate_c/generate_c_st.cc
--- a/Makefile	Wed Jul 13 13:57:29 2011 +0200
+++ b/Makefile	Fri Sep 02 18:15:55 2011 +0200
@@ -53,6 +53,7 @@
 LIBS += absyntax_utils/absyntax_utils.o
 LIBS += absyntax_utils/search_expression_type.o
 LIBS += absyntax_utils/decompose_var_instance_name.o
+LIBS += absyntax_utils/case_element_iterator.o
 LIBS += absyntax_utils/function_call_iterator.o
 LIBS += absyntax_utils/function_call_param_iterator.o
 LIBS += absyntax_utils/function_param_iterator.o
--- a/absyntax_utils/Makefile	Wed Jul 13 13:57:29 2011 +0200
+++ b/absyntax_utils/Makefile	Fri Sep 02 18:15:55 2011 +0200
@@ -22,6 +22,7 @@
 SEARCH_UTIL_FILES  = absyntax_utils.o
 SEARCH_UTIL_FILES += search_expression_type.o
 SEARCH_UTIL_FILES += decompose_var_instance_name.o
+SEARCH_UTIL_FILES += case_element_iterator.o
 SEARCH_UTIL_FILES += function_call_iterator.o
 SEARCH_UTIL_FILES += function_call_param_iterator.o
 SEARCH_UTIL_FILES += function_param_iterator.o
--- a/absyntax_utils/absyntax_utils.hh	Wed Jul 13 13:57:29 2011 +0200
+++ b/absyntax_utils/absyntax_utils.hh	Fri Sep 02 18:15:55 2011 +0200
@@ -98,6 +98,7 @@
 /***********************************************************************/
 
 #include "spec_init_separator.hh"
+#include "case_element_iterator.hh"
 #include "function_param_iterator.hh"
 #include "function_call_iterator.hh"
 #include "function_call_param_iterator.hh"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/absyntax_utils/case_element_iterator.cc	Fri Sep 02 18:15:55 2011 +0200
@@ -0,0 +1,186 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
+ *
+ *  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)
+ *
+ */
+
+
+/*
+ * Case element iterator.
+ * Iterate through the elements of a case list.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+/* Given a case_list_c and a type of element, iterate through
+ * each element, returning the symbol of each element if from
+ * the good type...case_element_iterator_c
+ */
+
+
+
+
+#include "case_element_iterator.hh"
+
+
+//#define DEBUG
+#ifdef DEBUG
+#define TRACE(classname) printf("\n____%s____\n",classname);
+#else
+#define TRACE(classname)
+#endif
+
+
+#define ERROR error_exit(__FILE__,__LINE__)
+/* function defined in main.cc */
+extern void error_exit(const char *file_name, int line_no);
+
+
+void* case_element_iterator_c::handle_case_element(symbol_c *case_element) {
+  if (current_case_element == case_element) {
+    current_case_element = NULL;
+  }
+  else if (current_case_element == NULL) {
+	current_case_element = case_element;
+	return case_element;
+  }
+
+  /* Not found! */
+  return NULL;
+}
+
+void* case_element_iterator_c::iterate_list(list_c *list) {
+  void *res;
+  for (int i = 0; i < list->n; i++) {
+    res = list->elements[i]->accept(*this);
+    if (res != NULL)
+        return res;
+  }
+  return NULL;
+}
+
+/* start off at the first case element once again... */
+void case_element_iterator_c::reset(void) {
+  current_case_element = NULL;
+}
+
+
+/* initialise the iterator object.
+ * We must be given a reference to a case_list_c that will be analysed...
+ */
+case_element_iterator_c::case_element_iterator_c(symbol_c *list, case_element_t element_type) {
+  /* do some consistency check... */
+  case_list_c* case_list = dynamic_cast<case_list_c*>(list);
+
+  if (NULL == case_list) ERROR;
+
+  /* OK. Now initialise this object... */
+  this->case_list = list;
+  this->wanted_element_type = element_type;
+  reset();
+}
+
+
+
+/* Skip to the next case element of type chosen. After object creation,
+ * the object references on case element _before_ the first, so
+ * this function must be called once to get the object to
+ * reference the first element...
+ *
+ * Returns the case element's symbol!
+ */
+symbol_c *case_element_iterator_c::next(void) {
+  void *res = case_list->accept(*this);
+  if (res == NULL) 
+    return NULL;
+
+  return current_case_element;
+}
+
+/******************************/
+/* B 1.2.1 - Numeric Literals */
+/******************************/
+void *case_element_iterator_c::visit(integer_c *symbol) {
+  switch (wanted_element_type) {
+	case element_single:
+	  return handle_case_element(symbol);
+	  break;
+	default:
+	  break;
+  }
+  return NULL;
+}
+
+void *case_element_iterator_c::visit(neg_integer_c *symbol) {
+  switch (wanted_element_type) {
+	case element_single:
+	  return handle_case_element(symbol);
+	  break;
+	default:
+	  break;
+  }
+  return NULL;
+}
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+/*  signed_integer DOTDOT signed_integer */
+void *case_element_iterator_c::visit(subrange_c *symbol) {
+  switch (wanted_element_type) {
+    case element_subrange:
+      return handle_case_element(symbol);
+      break;
+    default:
+      break;
+  }
+  return NULL;
+}
+
+/* enumerated_type_name '#' identifier */
+void *case_element_iterator_c::visit(enumerated_value_c *symbol) {
+  switch (wanted_element_type) {
+    case element_single:
+      return handle_case_element(symbol);
+      break;
+    default:
+      break;
+  }
+  return NULL;
+}
+
+/********************************/
+/* B 3.2.3 Selection Statements */
+/********************************/
+void *case_element_iterator_c::visit(case_list_c *symbol) {
+  return iterate_list(symbol);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/absyntax_utils/case_element_iterator.hh	Fri Sep 02 18:15:55 2011 +0200
@@ -0,0 +1,127 @@
+/*
+ *  matiec - a compiler for the programming languages defined in IEC 61131-3
+ *
+ *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
+ *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
+ *
+ *  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)
+ *
+ */
+
+
+/*
+ * Case element iterator.
+ * Iterate through the elements of a case list.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+/* Given a case_list_c and a type of element, iterate through
+ * each element, returning the symbol of each element if from
+ * the good type...case_element_iterator_c
+ */
+
+
+#include "../absyntax/visitor.hh"
+
+
+class case_element_iterator_c : public null_visitor_c {
+  public:
+    /* A type to specify the type of element.
+     */
+    typedef enum {
+      element_single,
+      element_subrange,
+    } case_element_t ;
+
+
+  private:
+    /* a pointer to the function_block_declaration_c
+     * or function_declaration_c currently being analysed.
+     */
+    symbol_c *case_list;
+    /* used when called to iterate() for a parameter */
+    symbol_c *current_case_element;
+
+    /* used to indicate the type of case element on which iterate */
+    case_element_t wanted_element_type;
+
+  private:
+    void* handle_case_element(symbol_c *case_element);
+
+    void* iterate_list(list_c *list);
+
+  public:
+    /* start off at the first case element once again... */
+    void reset(void);
+
+    /* initialise the iterator object.
+     * We must be given a reference to a case_list_c that will be analysed...
+     */
+    case_element_iterator_c(symbol_c *list, case_element_t element_type);
+
+    /* Skip to the next case element of type chosen. After object creation,
+     * the object references on case element _before_ the first, so
+     * this function must be called once to get the object to
+     * reference the first element...
+     *
+     * Returns the case element's symbol!
+     */
+    symbol_c *next(void);
+
+    private:
+    
+    /******************************/
+    /* B 1.2.1 - Numeric Literals */
+    /******************************/
+    void *visit(integer_c *symbol);
+    void *visit(neg_integer_c *symbol);
+
+    /********************************/
+    /* B 1.3.3 - Derived data types */
+    /********************************/
+    /*  signed_integer DOTDOT signed_integer */
+    void *visit(subrange_c *symbol);
+
+    /* enumerated_type_name '#' identifier */
+    void *visit(enumerated_value_c *symbol);
+
+    /********************************/
+    /* B 3.2.3 Selection Statements */
+    /********************************/
+    void *visit(case_list_c *symbol);
+
+    
+
+}; // function_param_iterator_c
+
+
+
+
+
+
+
--- a/stage4/generate_c/generate_c_st.cc	Wed Jul 13 13:57:29 2011 +0200
+++ b/stage4/generate_c/generate_c_st.cc	Fri Sep 02 18:15:55 2011 +0200
@@ -52,6 +52,12 @@
       fparam_output_vg
     } variablegeneration_t;
 
+    typedef enum {
+      single_cg,
+      subrange_cg,
+      none_cg
+    } casegeneration_t;
+
   private:
     /* When calling a function block, we must first find it's type,
      * by searching through the declarations of the variables currently
@@ -88,7 +94,10 @@
     int fcall_number;
     symbol_c *fbname;
 
+    bool first_subrange_case_list;
+
     variablegeneration_t wanted_variablegeneration;
+    casegeneration_t wanted_casegeneration;
 
   public:
     generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
@@ -102,6 +111,7 @@
       fcall_number = 0;
       fbname = name;
       wanted_variablegeneration = expression_vg;
+      wanted_casegeneration = none_cg;
     }
 
     virtual ~generate_c_st_c(void) {
@@ -207,6 +217,24 @@
   return NULL;
 }
 
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+/*  signed_integer DOTDOT signed_integer */
+void *visit(subrange_c *symbol) {
+  switch (wanted_casegeneration) {
+    case subrange_cg:
+      s4o.print("case_expression >= ");
+      symbol->lower_limit->accept(*this);
+      s4o.print(" && case_expression <= ");
+      symbol->upper_limit->accept(*this);
+      break;
+    default:
+      break;
+  }
+  return NULL;
+}
+
 /*********************/
 /* B 1.4 - Variables */
 /*********************/
@@ -1005,38 +1033,114 @@
 }
 
 void *visit(case_statement_c *symbol) {
-  s4o.print("switch(");
+  symbol_c *expression_type = search_expression_type->get_type(symbol->expression);
+  s4o.print("{\n");
+  s4o.indent_right();
+  if (search_base_type.type_is_enumerated(expression_type)) {
+	s4o.print(s4o.indent_spaces);
+	expression_type->accept(*this);
+	s4o.print(" case_expression = ");
+  }
+  else {
+	s4o.print(s4o.indent_spaces + "IEC_LINT case_expression = (IEC_LINT)");
+  }
   symbol->expression->accept(*this);
-  s4o.print(") {\n");
+  s4o.print(";\n" + s4o.indent_spaces + "switch (case_expression) {\n");
   s4o.indent_right();
+  wanted_casegeneration = single_cg;
+  symbol->case_element_list->accept(*this);
+  wanted_casegeneration = subrange_cg;
+  s4o.print(s4o.indent_spaces + "default:\n");
+  s4o.indent_right();
+  first_subrange_case_list = true;
   symbol->case_element_list->accept(*this);
   if (symbol->statement_list != NULL) {
-    s4o.print(s4o.indent_spaces + "default:\n");
+	if (!first_subrange_case_list) {
+      s4o.print(s4o.indent_spaces + "else {\n");
+      s4o.indent_right();
+	}
+    symbol->statement_list->accept(*this);
+    if (!first_subrange_case_list) {
+      s4o.indent_left();
+      s4o.print(s4o.indent_spaces + "}\n");
+    }
+  }
+  s4o.print(s4o.indent_spaces + "break;\n");
+  s4o.indent_left();
+  wanted_casegeneration = none_cg;
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}\n");
+  s4o.indent_left();
+  s4o.print(s4o.indent_spaces + "}");
+  return NULL;
+}
+
+/* helper symbol for case_statement */
+void *visit(case_element_list_c *symbol) {return print_list(symbol);}
+
+void *visit(case_element_c *symbol) {
+  case_element_iterator_c *case_element_iterator;
+  symbol_c* element = NULL;
+  bool first_element = true;
+
+  switch (wanted_casegeneration) {
+    case single_cg:
+      case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_single);
+      for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) {
+        if (first_element) first_element = false;
+    	s4o.print(s4o.indent_spaces + "case ");
+        element->accept(*this);
+        s4o.print(":\n");
+      }
+      delete case_element_iterator;
+      break;
+
+    case subrange_cg:
+      case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_subrange);
+      for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) {
+        if (first_element) {
+          if (first_subrange_case_list) {
+            s4o.print(s4o.indent_spaces + "if (");
+            first_subrange_case_list = false;
+          }
+          else {
+        	  s4o.print(s4o.indent_spaces + "else if (");
+          }
+          first_element = false;
+        }
+        else {
+          s4o.print(" && ");
+        }
+        element->accept(*this);
+      }
+      delete case_element_iterator;
+      if (!first_element) {
+        s4o.print(") {\n");
+      }
+      break;
+
+    default:
+      break;
+  }
+
+  if (!first_element) {
     s4o.indent_right();
     symbol->statement_list->accept(*this);
-    s4o.print(s4o.indent_spaces + "break;\n");
-    s4o.indent_left();
-  }
-  s4o.indent_left();
-  s4o.print(s4o.indent_spaces + "}");
-  return NULL;
-}
-
-/* helper symbol for case_statement */
-void *visit(case_element_list_c *symbol) {return print_list(symbol);}
-
-void *visit(case_element_c *symbol) {
-  s4o.print(s4o.indent_spaces + "case ");
-  symbol->case_list->accept(*this);
-  s4o.print(" :\n");
-  s4o.indent_right();
-  symbol->statement_list->accept(*this);
-  s4o.print(s4o.indent_spaces + "break;\n");
-  s4o.indent_left();
-  return NULL;
-}
-
-void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");}
+    switch (wanted_casegeneration) {
+      case single_cg:
+        s4o.print(s4o.indent_spaces + "break;\n");
+        s4o.indent_left();
+        break;
+      case subrange_cg:
+    	s4o.indent_left();
+  	    s4o.print(s4o.indent_spaces + "}\n");
+  	    break;
+      default:
+        break;
+    }
+  }
+  return NULL;
+}
 
 /********************************/
 /* B 3.2.4 Iteration Statements */