--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/accessor.h Mon Nov 30 15:36:12 2009 +0100
@@ -0,0 +1,65 @@
+#ifndef __ACCESSOR_H
+#define __ACCESSOR_H
+
+// variable declaration macros
+#define __DECLARE_VAR(type, name)\
+ type name;
+#define __DECLARE_GLOBAL(type, resource, name)\
+ type resource##__##name;\
+ static type *GLOBAL__##name = &resource##__##name;\
+ type __GET_GLOBAL_##name(void) {return *GLOBAL__##name;}\
+ void __SET_GLOBAL_##name(type value) {*GLOBAL__##name = value;}
+#define __DECLARE_GLOBAL_LOCATION(type, location)\
+ extern type *location;
+#define __DECLARE_GLOBAL_LOCATED(type, resource, name)\
+ type *resource##__##name;\
+ static type *GLOBAL__##name;\
+ type __GET_GLOBAL_##name(void) {return *GLOBAL__##name;}\
+ void __SET_GLOBAL_##name(type value) {*GLOBAL__##name = value;}
+#define __DECLARE_EXTERNAL(type, name)\
+ type *name;
+#define __DECLARE_LOCATED(type, name)\
+ type *name;
+
+
+// variable initialization macros
+#define __INIT_VAR(name, initial)\
+ name = initial;
+#define __INIT_GLOBAL(name, initial)\
+ *GLOBAL__##name = initial;
+#define __INIT_GLOBAL_LOCATED(resource, name, location)\
+ resource##__##name = location;\
+ GLOBAL__##name = location;
+#define __INIT_EXTERNAL(type, global, name)\
+ {extern type *GLOBAL__##global;\
+ name = GLOBAL__##global;}
+#define __INIT_LOCATED(type, location, name)\
+ {extern type *location;\
+ name = location;}
+#define __INIT_LOCATED_VALUE(name, initial)\
+ *name = initial;
+
+
+// variable getting macros
+#define __GET_VAR(name)\
+ name
+#define __GET_EXTERNAL(name)\
+ __GET_GLOBAL_##name()
+#define __GET_LOCATED(name)\
+ *(name)
+#define __GET_VAR_BY_REF(name)\
+ &(name)
+#define __GET_EXTERNAL_BY_REF(name)\
+ GLOBAL__##name
+#define __GET_LOCATED_BY_REF(name)\
+ name
+
+// variable setting macros
+#define __SET_VAR(name, new_value)\
+ name = new_value
+#define __SET_EXTERNAL(name, new_value)\
+ __SET_GLOBAL_##name(value)
+#define __SET_LOCATED(name, new_value)\
+ *(name) = value
+
+#endif //__ACCESSOR_H
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/stage4/generate_c/generate_c_inlinefcall.cc Mon Nov 30 15:36:12 2009 +0100
@@ -0,0 +1,480 @@
+/*
+ * (c) 2007 Mario de Sousa and Laurent Bessard
+ *
+ * Offered to the public under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2 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.
+ *
+ * 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 IL and ST compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+/*
+ * This is one of the versions available for the 4th stage.
+ *
+ * This 4th stage generates a c++ source program equivalent
+ * to the IL and ST code.
+ */
+
+
+#define INLINE_RESULT_TEMP_VAR "__res"
+
+class generate_c_inline_c: public generate_c_typedecl_c {
+
+ private:
+
+ /* The name of the IL default variable... */
+ #define IL_DEFVAR VAR_LEADER "IL_DEFVAR"
+ /* The name of the variable used to pass the result of a
+ * parenthesised instruction list to the immediately preceding
+ * scope ...
+ */
+ il_default_variable_c default_variable_name;
+
+ int fcall_number;
+ symbol_c *fbname;
+
+ search_expression_type_c *search_expression_type;
+
+ search_varfb_instance_type_c *search_varfb_instance_type;
+
+ search_base_type_c search_base_type;
+
+ public:
+ generate_c_inline_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
+ : generate_c_typedecl_c(s4o_ptr),
+ default_variable_name(IL_DEFVAR, NULL)
+ {
+ search_expression_type = new search_expression_type_c(scope);
+ search_varfb_instance_type = new search_varfb_instance_type_c(scope);
+ this->set_variable_prefix(variable_prefix);
+ fcall_number = 0;
+ fbname = name;
+ }
+
+ virtual ~generate_c_inline_c(void) {
+ delete search_varfb_instance_type;
+ }
+
+ void *generate_inline(symbol_c *function_name,
+ symbol_c *return_data_type,
+ std::list<FUNCTION_PARAM*> param_list) {
+ std::list<FUNCTION_PARAM*>::iterator pt;
+
+ fcall_number++;
+
+ s4o.print(s4o.indent_spaces);
+ s4o.print("inline ");
+ return_data_type->accept(*this);
+ s4o.print(" __");
+ fbname->accept(*this);
+ s4o.print("_");
+ function_name->accept(*this);
+ s4o.print_integer(fcall_number);
+ s4o.print("(");
+ s4o.indent_right();
+
+ PARAM_LIST_ITERATOR() {
+ if (PARAM_DIRECTION == function_param_iterator_c::direction_in) {
+ PARAM_TYPE->accept(*this);
+ s4o.print(" ");
+ PARAM_NAME->accept(*this);
+ s4o.print(",\n" + s4o.indent_spaces);
+ }
+ }
+ fbname->accept(*this);
+ s4o.print(" *");
+ s4o.print(FB_FUNCTION_PARAM);
+ s4o.indent_left();
+ s4o.print(")\n" + s4o.indent_spaces);
+ s4o.print("{\n");
+ s4o.indent_right();
+
+ s4o.print(s4o.indent_spaces);
+ return_data_type->accept(*this);
+ s4o.print(" "),
+ s4o.print(INLINE_RESULT_TEMP_VAR);
+ s4o.print(";\n");
+
+ PARAM_LIST_ITERATOR() {
+ if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
+ PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
+ PARAM_VALUE != NULL) {
+ s4o.print(s4o.indent_spaces);
+ PARAM_TYPE->accept(*this);
+ s4o.print(" ");
+ s4o.print(TEMP_VAR);
+ PARAM_NAME->accept(*this);
+ s4o.print(" = ");
+ print_check_function(PARAM_TYPE, PARAM_VALUE);
+ s4o.print(";\n");
+ }
+ }
+
+ s4o.print(s4o.indent_spaces + INLINE_RESULT_TEMP_VAR),
+ s4o.print(" = ");
+ function_name->accept(*this);
+ s4o.print("(");
+ s4o.indent_right();
+
+ PARAM_LIST_ITERATOR() {
+ if (pt != param_list.begin())
+ s4o.print(",\n" + s4o.indent_spaces);
+ if (PARAM_DIRECTION == function_param_iterator_c::direction_in)
+ PARAM_NAME->accept(*this);
+ else if (PARAM_VALUE != NULL){
+ s4o.print("&");
+ s4o.print(TEMP_VAR);
+ PARAM_NAME->accept(*this);
+ }
+ else {
+ s4o.print("NULL");
+ }
+ }
+ s4o.print(");\n");
+ s4o.indent_left();
+
+ PARAM_LIST_ITERATOR() {
+ if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
+ PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
+ PARAM_VALUE != NULL) {
+
+ s4o.print(s4o.indent_spaces);
+
+ unsigned int vartype = search_varfb_instance_type->get_vartype(PARAM_VALUE);
+ if (vartype == search_var_instance_decl_c::external_vt)
+ s4o.print(SET_EXTERNAL);
+ else if (vartype == search_var_instance_decl_c::located_vt)
+ s4o.print(SET_LOCATED);
+ else
+ s4o.print(SET_VAR);
+ s4o.print("(");
+
+ PARAM_VALUE->accept(*this);
+ s4o.print(", ");
+ print_check_function(PARAM_TYPE, PARAM_NAME, NULL, true);
+ s4o.print(");\n");
+ }
+ }
+ s4o.print(s4o.indent_spaces + "return ");
+ s4o.print(INLINE_RESULT_TEMP_VAR);
+ s4o.print(";\n");
+
+ s4o.indent_left();
+ s4o.print(s4o.indent_spaces + "}\n\n");
+
+ return NULL;
+ }
+
+ private:
+
+ /*********************/
+ /* B 1.4 - Variables */
+ /*********************/
+ void *visit(symbolic_variable_c *symbol) {
+ unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
+ if (vartype == search_var_instance_decl_c::external_vt) {
+ s4o.print(GET_EXTERNAL);
+ s4o.print("(");
+ symbol->var_name->accept(*this);
+ }
+ else {
+ if (vartype == search_var_instance_decl_c::located_vt)
+ s4o.print(GET_LOCATED);
+ else
+ s4o.print(GET_VAR);
+ s4o.print("(");
+ generate_c_base_c::visit(symbol);
+ }
+ s4o.print(")");
+ return NULL;
+ }
+
+ /********************************************/
+ /* B.1.4.1 Directly Represented Variables */
+ /********************************************/
+ // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);};
+ void *visit(direct_variable_c *symbol) {
+ TRACE("direct_variable_c");
+ /* Do not use print_token() as it will change everything into uppercase */
+ if (strlen(symbol->value) == 0) ERROR;
+ s4o.print(GET_LOCATED);
+ s4o.print("(");
+ this->print_variable_prefix();
+ s4o.printlocation(symbol->value + 1);
+ s4o.print(")");
+ return NULL;
+ }
+
+ /****************************************/
+ /* B.2 - Language IL (Instruction List) */
+ /****************************************/
+
+ /***********************************/
+ /* B 2.1 Instructions and Operands */
+ /***********************************/
+
+ void *visit(il_function_call_c *symbol) {
+ function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
+
+ if (f_decl != function_symtable.end_value()) {
+ DECLARE_PARAM_LIST()
+ bool has_output_params = false;
+
+ /* determine the base data type returned by the function being called... */
+ search_base_type_c search_base_type;
+ symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
+ if (NULL == return_data_type) ERROR;
+
+ /* loop through each function parameter, find the value we should pass
+ * to it, and then output the c equivalent...
+ */
+
+ function_param_iterator_c fp_iterator(f_decl);
+ identifier_c *param_name;
+ function_call_param_iterator_c function_call_param_iterator(symbol);
+ for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+ symbol_c *param_type = fp_iterator.param_type();
+ if (param_type == NULL) ERROR;
+
+ function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+ symbol_c *param_value = NULL;
+
+ /* if it is the first parameter, semantics specifies that we should
+ * get the value off the IL default variable!
+ */
+ if (1 == i)
+ param_value = &this->default_variable_name;
+
+ /* Get the value from a foo(<param_name> = <param_value>) style call */
+ /* NOTE: the following line of code is not required in this case, but it doesn't
+ * harm to leave it in, as in the case of a non-formal syntax function call,
+ * it will always return NULL.
+ * We leave it in in case we later decide to merge this part of the code together
+ * with the function calling code in generate_c_st_c, which does require
+ * the following line...
+ */
+ if (param_value == NULL)
+ param_value = function_call_param_iterator.search_f(param_name);
+
+ /* Get the value from a foo(<param_value>) style call */
+ if (param_value == NULL)
+ param_value = function_call_param_iterator.next_nf();
+
+ if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) {
+ /* No value given for parameter, so we must use the default... */
+ /* First check whether default value specified in function declaration...*/
+ param_value = fp_iterator.default_value();
+ }
+
+ ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
+ } /* for(...) */
+
+ PARAM_LIST_ITERATOR() {
+ if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
+ PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
+ PARAM_VALUE != NULL) {
+ has_output_params = true;
+ }
+ }
+
+ if (has_output_params)
+ generate_inline(symbol->function_name, return_data_type, param_list);
+
+ CLEAR_PARAM_LIST()
+ }
+ return NULL;
+ }
+
+ void *visit(il_formal_funct_call_c *symbol) {
+ function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
+
+ if (f_decl != function_symtable.end_value()) {
+ DECLARE_PARAM_LIST()
+ bool has_output_params = false;
+
+ /* determine the base data type returned by the function being called... */
+ search_base_type_c search_base_type;
+ symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
+ if (NULL == return_data_type) ERROR;
+
+ /* loop through each function parameter, find the value we should pass
+ * to it, and then output the c equivalent...
+ */
+
+ function_param_iterator_c fp_iterator(f_decl);
+ identifier_c *param_name;
+ function_call_param_iterator_c function_call_param_iterator(symbol);
+ for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+ symbol_c *param_type = fp_iterator.param_type();
+ if (param_type == NULL) ERROR;
+
+ function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+
+ symbol_c *param_value = NULL;
+
+ /* Get the value from a foo(<param_name> = <param_value>) style call */
+ if (param_value == NULL)
+ param_value = function_call_param_iterator.search_f(param_name);
+
+ /* Get the value from a foo(<param_value>) style call */
+ /* NOTE: the following line of code is not required in this case, but it doesn't
+ * harm to leave it in, as in the case of a formal syntax function call,
+ * it will always return NULL.
+ * We leave it in in case we later decide to merge this part of the code together
+ * with the function calling code in generate_c_st_c, which does require
+ * the following line...
+ */
+ if (param_value == NULL)
+ param_value = function_call_param_iterator.next_nf();
+
+ if (param_value == NULL) {
+ /* No value given for parameter, so we must use the default... */
+ /* First check whether default value specified in function declaration...*/
+ param_value = fp_iterator.default_value();
+ }
+
+ ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
+ }
+
+ PARAM_LIST_ITERATOR() {
+ if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
+ PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
+ PARAM_VALUE != NULL) {
+ has_output_params = true;
+ }
+ }
+
+ if (has_output_params)
+ generate_inline(symbol->function_name, return_data_type, param_list);
+
+ CLEAR_PARAM_LIST()
+ }
+ return NULL;
+ }
+
+ /***************************************/
+ /* B.3 - Language ST (Structured Text) */
+ /***************************************/
+ /***********************/
+ /* B 3.1 - Expressions */
+ /***********************/
+
+ void *visit(function_invocation_c *symbol) {
+ function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
+
+ if (f_decl != function_symtable.end_value()) {
+ DECLARE_PARAM_LIST()
+ bool has_output_params = false;
+
+ symbol_c *parameter_assignment_list = NULL;
+ if (NULL != symbol-> formal_param_list) parameter_assignment_list = symbol-> formal_param_list;
+ if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list;
+ if (NULL == parameter_assignment_list) ERROR;
+
+ /* determine the base data type returned by the function being called... */
+ search_base_type_c search_base_type;
+ symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
+ if (NULL == return_data_type) ERROR;
+
+ /* loop through each function parameter, find the value we should pass
+ * to it, and then output the c equivalent...
+ */
+ function_param_iterator_c fp_iterator(f_decl);
+ identifier_c *param_name;
+ function_call_param_iterator_c function_call_param_iterator(symbol);
+ for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
+
+ function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
+
+ /* Get the value from a foo(<param_name> = <param_value>) style call */
+ symbol_c *param_value = function_call_param_iterator.search_f(param_name);
+
+ /* Get the value from a foo(<param_value>) style call */
+ if (param_value == NULL)
+ param_value = function_call_param_iterator.next_nf();
+
+ if (param_value == NULL && param_direction == function_param_iterator_c::direction_in) {
+ /* No value given for parameter, so we must use the default... */
+ /* First check whether default value specified in function declaration...*/
+ param_value = fp_iterator.default_value();
+ }
+
+ symbol_c *param_type = fp_iterator.param_type();
+ if (param_type == NULL) ERROR;
+
+ ADD_PARAM_LIST(param_name, param_value, param_type, param_direction)
+ } /* for(...) */
+ // symbol->parameter_assignment->accept(*this);
+
+ PARAM_LIST_ITERATOR() {
+ if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
+ PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
+ PARAM_VALUE != NULL) {
+ has_output_params = true;
+ }
+ }
+
+ if (has_output_params)
+ generate_inline(symbol->function_name, return_data_type, param_list);
+
+ CLEAR_PARAM_LIST()
+ }
+ return NULL;
+ }
+
+}; /* generate_c_inline_c */
+
+
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+/***********************************************************************/
+
+
+class generate_c_inlinefcall_c: public iterator_visitor_c {
+
+ private:
+ generate_c_inline_c *generate_c_inline;
+
+ public:
+ generate_c_inlinefcall_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL) {
+ generate_c_inline = new generate_c_inline_c(s4o_ptr, name, scope, variable_prefix);
+ }
+
+ virtual ~generate_c_inlinefcall_c(void) {
+ delete generate_c_inline;
+ }
+
+ private:
+
+ void *visit(function_invocation_c *symbol) {
+ return symbol->accept(*generate_c_inline);
+ }
+
+ void *visit(il_function_call_c *symbol) {
+ return symbol->accept(*generate_c_inline);
+ }
+
+ void *visit(il_formal_funct_call_c *symbol) {
+ return symbol->accept(*generate_c_inline);
+ }
+}; /* generate_c_inlinefcall_c */
+
+
+