# HG changeset patch
# User laurent
# Date 1316033919 -7200
# Node ID 60b012b7793f129e640208d1d8e876329c4b28a0
# Parent 7dcbd84187719495b927a1b306240359f5652aab
Adding support for compiling direct array specification inside variable declaration
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/Makefile.am
--- a/absyntax_utils/Makefile.am Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/Makefile.am Wed Sep 14 22:58:39 2011 +0200
@@ -6,6 +6,7 @@
absyntax_utils.cc \
add_en_eno_param_decl.cc \
decompose_var_instance_name.cc \
+ array_dimension_iterator.cc \
case_element_iterator.cc \
function_call_iterator.cc \
function_call_param_iterator.cc \
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/Makefile.in
--- a/absyntax_utils/Makefile.in Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/Makefile.in Wed Sep 14 22:58:39 2011 +0200
@@ -73,6 +73,7 @@
am_libabsyntax_utils_a_OBJECTS = absyntax_utils.$(OBJEXT) \
add_en_eno_param_decl.$(OBJEXT) \
decompose_var_instance_name.$(OBJEXT) \
+ array_dimension_iterator.$(OBJEXT) \
case_element_iterator.$(OBJEXT) \
function_call_iterator.$(OBJEXT) \
function_call_param_iterator.$(OBJEXT) \
@@ -205,6 +206,7 @@
absyntax_utils.cc \
add_en_eno_param_decl.cc \
decompose_var_instance_name.cc \
+ array_dimension_iterator.cc \
case_element_iterator.cc \
function_call_iterator.cc \
function_call_param_iterator.cc \
@@ -304,6 +306,7 @@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/absyntax_utils.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/add_en_eno_param_decl.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/array_dimension_iterator.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/case_element_iterator.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/decompose_var_instance_name.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/function_call_iterator.Po@am__quote@
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/absyntax_utils.hh
--- a/absyntax_utils/absyntax_utils.hh Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/absyntax_utils.hh Wed Sep 14 22:58:39 2011 +0200
@@ -102,6 +102,7 @@
/***********************************************************************/
#include "spec_init_separator.hh"
+#include "array_dimension_iterator.hh"
#include "case_element_iterator.hh"
#include "function_param_iterator.hh"
#include "function_call_iterator.hh"
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/array_dimension_iterator.cc
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/absyntax_utils/array_dimension_iterator.cc Wed Sep 14 22:58:39 2011 +0200
@@ -0,0 +1,139 @@
+/*
+ * 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 .
+ *
+ *
+ * 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)
+ *
+ */
+
+
+/*
+ * Array dimension iterator.
+ * Iterate through the dimensions of array specification.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+/* Given a array_specification_c, iterate through
+ * each subrange, returning the symbol of each subrange
+ * ...array_dimension_iterator_c
+ */
+
+
+
+
+#include "array_dimension_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* array_dimension_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 array_dimension_iterator_c::reset(void) {
+ current_array_dimension = NULL;
+}
+
+
+/* initialize the iterator object.
+ * We must be given a reference to a array_specification_c that will be analyzed...
+ */
+array_dimension_iterator_c::array_dimension_iterator_c(symbol_c *symbol) {
+ /* do some consistency check... */
+ array_specification_c* array_spec = dynamic_cast(symbol);
+
+ if (NULL == array_spec) ERROR;
+
+ /* OK. Now initialize this object... */
+ this->array_specification = symbol;
+ reset();
+}
+
+
+
+/* Skip to the next subrange. After object creation,
+ * the object references on subrange _before_ the first, so
+ * this function must be called once to get the object to
+ * reference the first subrange...
+ *
+ * Returns the subrange symbol!
+ */
+symbol_c *array_dimension_iterator_c::next(void) {
+ void *res = array_specification->accept(*this);
+ if (res == NULL)
+ return NULL;
+
+ return current_array_dimension;
+}
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+/* signed_integer DOTDOT signed_integer */
+void *array_dimension_iterator_c::visit(subrange_c *symbol) {
+ if (current_array_dimension == symbol) {
+ current_array_dimension = NULL;
+ }
+ else if (current_array_dimension == NULL) {
+ current_array_dimension = symbol;
+ return symbol;
+ }
+
+ /* Not found! */
+ return NULL;
+}
+
+/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
+void *array_dimension_iterator_c::visit(array_specification_c *symbol) {
+ return symbol->array_subrange_list->accept(*this);
+}
+
+/* array_subrange_list ',' subrange */
+void *array_dimension_iterator_c::visit(array_subrange_list_c *symbol) {
+ return iterate_list(symbol);
+}
+
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/array_dimension_iterator.hh
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/absyntax_utils/array_dimension_iterator.hh Wed Sep 14 22:58:39 2011 +0200
@@ -0,0 +1,101 @@
+/*
+ * 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 .
+ *
+ *
+ * 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)
+ *
+ */
+
+
+/*
+ * Array dimension iterator.
+ * Iterate through the dimensions of array specification.
+ *
+ * This is part of the 4th stage that generates
+ * a c++ source program equivalent to the IL and ST
+ * code.
+ */
+
+/* Given a array_specification_c, iterate through
+ * each subrange, returning the symbol of each subrange
+ * ...array_dimension_iterator_c
+ */
+
+
+#include "../absyntax/visitor.hh"
+
+
+class array_dimension_iterator_c : public null_visitor_c {
+ private:
+ /* a pointer to the array_specification_c currently being analyzed */
+ symbol_c *array_specification;
+ /* used when called to iterate() for a parameter */
+ symbol_c *current_array_dimension;
+
+ private:
+ void* iterate_list(list_c *list);
+
+ public:
+ /* start off at the first dimension once again... */
+ void reset(void);
+
+ /* initialize the iterator object.
+ * We must be given a reference to a case_list_c that will be analyzed...
+ */
+ array_dimension_iterator_c(symbol_c *symbol);
+
+ /* Skip to the next subrange. After object creation,
+ * the object references on subrange _before_ the first, so
+ * this function must be called once to get the object to
+ * reference the first subrange...
+ *
+ * Returns the subrange symbol!
+ */
+ symbol_c *next(void);
+
+ private:
+
+ /********************************/
+ /* B 1.3.3 - Derived data types */
+ /********************************/
+ /* signed_integer DOTDOT signed_integer */
+ void *visit(subrange_c *symbol);
+
+ /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
+ void *visit(array_specification_c *symbol);
+
+ /* array_subrange_list ',' subrange */
+ void *visit(array_subrange_list_c *symbol);
+
+}; // function_param_iterator_c
+
+
+
+
+
+
+
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/case_element_iterator.cc
--- a/absyntax_utils/case_element_iterator.cc Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/case_element_iterator.cc Wed Sep 14 22:58:39 2011 +0200
@@ -93,8 +93,8 @@
}
-/* initialise the iterator object.
- * We must be given a reference to a case_list_c that will be analysed...
+/* initialize the iterator object.
+ * We must be given a reference to a case_list_c that will be analyzed...
*/
case_element_iterator_c::case_element_iterator_c(symbol_c *list, case_element_t element_type) {
/* do some consistency check... */
@@ -102,7 +102,7 @@
if (NULL == case_list) ERROR;
- /* OK. Now initialise this object... */
+ /* OK. Now initialize this object... */
this->case_list = list;
this->wanted_element_type = element_type;
reset();
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/case_element_iterator.hh
--- a/absyntax_utils/case_element_iterator.hh Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/case_element_iterator.hh Wed Sep 14 22:58:39 2011 +0200
@@ -60,9 +60,7 @@
private:
- /* a pointer to the function_block_declaration_c
- * or function_declaration_c currently being analysed.
- */
+ /* a pointer to the case_list_c currently being analyzed */
symbol_c *case_list;
/* used when called to iterate() for a parameter */
symbol_c *current_case_element;
@@ -79,8 +77,8 @@
/* 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...
+ /* initialize the iterator object.
+ * We must be given a reference to a case_list_c that will be analyzed...
*/
case_element_iterator_c(symbol_c *list, case_element_t element_type);
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/search_base_type.cc
--- a/absyntax_utils/search_base_type.cc Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/search_base_type.cc Wed Sep 14 22:58:39 2011 +0200
@@ -227,7 +227,8 @@
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
void *search_base_type_c::visit(array_specification_c *symbol) {
- if (NULL == this->current_type_name) ERROR;
+ if (NULL == this->current_type_name)
+ this->current_type_name = symbol->non_generic_type_name;
return symbol->non_generic_type_name->accept(*this);
}
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/search_base_type.hh
--- a/absyntax_utils/search_base_type.hh Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/search_base_type.hh Wed Sep 14 22:58:39 2011 +0200
@@ -52,6 +52,7 @@
private:
symbol_c *current_type_name;
+ bool is_array;
bool is_subrange;
bool is_enumerated;
diff -r 7dcbd8418771 -r 60b012b7793f absyntax_utils/search_varfb_instance_type.cc
--- a/absyntax_utils/search_varfb_instance_type.cc Fri Sep 09 12:03:15 2011 +0200
+++ b/absyntax_utils/search_varfb_instance_type.cc Wed Sep 14 22:58:39 2011 +0200
@@ -269,20 +269,19 @@
/* identifier ':' array_spec_init */
void *search_varfb_instance_type_c::visit(array_type_declaration_c *symbol) {
- this->is_complex = true;
return symbol->array_spec_init->accept(*this);
}
/* array_specification [ASSIGN array_initialization] */
/* array_initialization may be NULL ! */
void *search_varfb_instance_type_c::visit(array_spec_init_c *symbol) {
- this->is_complex = true;
return symbol->array_specification->accept(*this);
}
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
void *search_varfb_instance_type_c::visit(array_specification_c *symbol) {
this->is_complex = true;
+ this->current_typeid = symbol;
return symbol->non_generic_type_name->accept(*this);
}
diff -r 7dcbd8418771 -r 60b012b7793f lib/accessor.h
--- a/lib/accessor.h Fri Sep 09 12:03:15 2011 +0200
+++ b/lib/accessor.h Wed Sep 14 22:58:39 2011 +0200
@@ -1,6 +1,7 @@
#ifndef __ACCESSOR_H
#define __ACCESSOR_H
+#define __INITIAL_VALUE(...) __VA_ARGS__
// variable declaration macros
#define __DECLARE_VAR(type, name)\
@@ -45,9 +46,12 @@
#define __INIT_VAR(name, initial, retained)\
name.value = initial;\
__INIT_RETAIN(name, retained)
-#define __INIT_GLOBAL(name, initial, retained)\
- __INIT_GLOBAL_##name(initial);\
- __INIT_RETAIN((*GLOBAL__##name), retained)
+#define __INIT_GLOBAL(type, name, initial, retained)\
+ {\
+ static const type temp = initial;\
+ __INIT_GLOBAL_##name(temp);\
+ __INIT_RETAIN((*GLOBAL__##name), retained)\
+ }
#define __INIT_GLOBAL_LOCATED(resource, name, location, retained)\
resource##__##name.value = location;\
__INIT_RETAIN(resource##__##name, retained)
diff -r 7dcbd8418771 -r 60b012b7793f stage1_2/iec_bison.yy
--- a/stage1_2/iec_bison.yy Fri Sep 09 12:03:15 2011 +0200
+++ b/stage1_2/iec_bison.yy Wed Sep 14 22:58:39 2011 +0200
@@ -3004,6 +3004,7 @@
array_initial_elements:
array_initial_element
| integer '(' ')'
+ {$$ = new array_initial_elements_c($1, NULL, locloc(@$));}
| integer '(' array_initial_element ')'
{$$ = new array_initial_elements_c($1, $3, locloc(@$));}
/* ERROR_CHECK_BEGIN */
diff -r 7dcbd8418771 -r 60b012b7793f stage4/generate_c/generate_c.cc
--- a/stage4/generate_c/generate_c.cc Fri Sep 09 12:03:15 2011 +0200
+++ b/stage4/generate_c/generate_c.cc Wed Sep 14 22:58:39 2011 +0200
@@ -27,6 +27,8 @@
#include
#include
#include
+#include