mario@181: /* msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@265: * msousa@265: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) msousa@265: * msousa@265: * This program is free software: you can redistribute it and/or modify msousa@265: * it under the terms of the GNU General Public License as published by msousa@265: * the Free Software Foundation, either version 3 of the License, or msousa@265: * (at your option) any later version. msousa@265: * msousa@265: * This program is distributed in the hope that it will be useful, msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@265: * GNU General Public License for more details. msousa@265: * msousa@265: * You should have received a copy of the GNU General Public License msousa@265: * along with this program. If not, see . msousa@265: * mario@181: * mario@181: * This code is made available on the understanding that it will not be mario@181: * used in safety-critical situations without a full and competent review. mario@181: */ mario@181: mario@181: /* msousa@265: * An IEC 61131-3 compiler. mario@181: * mario@181: * Based on the mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) mario@181: * mario@181: */ mario@181: mario@181: /* mario@181: * Function call parameter iterator. ccb@202: * It will iterate through the non-formal parameters of a function call mario@181: * (i.e. function calls using the foo(, , ...) syntax). ccb@202: * and/or search through the formal parameters of a function call mario@181: * (i.e. function calls using the foo( = , = , ...) syntax). mario@181: * mario@181: * Calls to function blocks and programs are also supported. mario@181: * ccb@202: * Note that calls to next_nf() will only iterate through non-formal parameters, ccb@202: * calls to next_f() will only iterate through formal parameters, ccb@202: * and calls to search_f() will only serach through formal parameters. mario@181: */ mario@181: mario@181: mario@181: mario@181: #include "function_call_param_iterator.hh" mario@181: #include mario@181: mario@181: mario@181: //#define DEBUG mario@181: #ifdef DEBUG mario@181: #define TRACE(classname) printf("\n____%s____\n",classname); mario@181: #else mario@181: #define TRACE(classname) mario@181: #endif mario@181: mario@181: #define ERROR error_exit(__FILE__,__LINE__) mario@181: /* function defined in main.cc */ mario@181: extern void error_exit(const char *file_name, int line_no); mario@181: mario@181: mario@181: mario@181: mario@181: mario@181: void *function_call_param_iterator_c::search_list(list_c *list) { mario@181: switch (current_operation) { ccb@202: case iterate_nf_op: mario@181: for(int i = 0; i < list->n; i++) { mario@181: void *res = list->elements[i]->accept(*this); mario@181: if (NULL != res) { mario@181: /* It went through the handle_parameter_assignment() function, mario@181: * and is therefore a parameter assignment ( = ), mario@181: * and not a simple expression (). mario@181: */ mario@181: /* we do nothing... */ mario@181: } else { mario@181: param_count++; ccb@202: if (param_count == iterate_nf_next_param) { mario@181: return list->elements[i]; mario@181: } mario@181: } mario@181: } mario@181: return NULL; mario@181: break; mario@181: ccb@202: case iterate_f_op: ccb@202: for(int i = 0; i < list->n; i++) { ccb@202: void *res = list->elements[i]->accept(*this); ccb@202: if (NULL != res) { ccb@202: /* It went through the handle_parameter_assignment() function, ccb@202: * and is therefore a parameter assignment ( = ), ccb@202: * and not a simple expression (). ccb@202: */ ccb@202: param_count++; ccb@202: if (param_count == iterate_f_next_param) { ccb@202: return res; ccb@202: } ccb@202: } else { ccb@202: /* we do nothing... */ ccb@202: } ccb@202: } ccb@202: return NULL; ccb@202: break; ccb@202: ccb@202: case search_f_op: mario@181: for(int i = 0; i < list->n; i++) { mario@181: void *res = list->elements[i]->accept(*this); mario@181: if (res != NULL) mario@181: return res; mario@181: } mario@181: return NULL; mario@181: break; mario@181: } /* switch */ mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: mario@181: void *function_call_param_iterator_c::handle_parameter_assignment(symbol_c *variable_name, symbol_c *expression) { mario@181: switch (current_operation) { ccb@202: case iterate_nf_op: mario@181: /* UGLY HACK -> this will be detected in the search_list() function */ ccb@202: return (void *)variable_name; /* anything, as long as it is not NULL!! */ mario@181: break; mario@181: ccb@202: case iterate_f_op: ccb@202: current_value = expression; ccb@202: return (void *)variable_name; ccb@202: break; ccb@202: ccb@202: case search_f_op: mario@181: identifier_c *variable_name2 = dynamic_cast(variable_name); ccb@202: mario@181: if (variable_name2 == NULL) ERROR; ccb@202: mario@181: if (strcasecmp(search_param_name->value, variable_name2->value) == 0) mario@181: /* FOUND! This is the same parameter!! */ mario@181: return (void *)expression; mario@181: return NULL; mario@181: break; mario@181: } mario@181: mario@181: ERROR; mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /* start off at the first parameter once again... */ mario@181: void function_call_param_iterator_c::reset(void) { ccb@202: iterate_nf_next_param = 0; ccb@202: iterate_f_next_param = 0; ccb@202: param_count = 0; mario@181: } mario@181: mario@181: /* initialise the iterator object. mario@181: * We must be given a reference to the function/program/function block call mario@181: * that will be analysed... mario@181: */ mario@181: function_call_param_iterator_c::function_call_param_iterator_c(symbol_c *f_call) { mario@181: /* It is expected that f_call will reference one of the following: mario@181: * program_configuration_c mario@181: * function_invocation_c mario@181: * fb_invocation_c mario@181: * il_function_call_c mario@181: * il_formal_funct_call_c mario@181: * ... (have I missed any?) mario@181: */ mario@181: this->f_call = f_call; mario@181: search_param_name = NULL; mario@181: reset(); mario@181: } mario@181: ccb@202: /* Skip to the next formal parameter. After object creation, mario@181: * the object references on parameter _before_ the first, so mario@181: * this function must be called once to get the object to mario@181: * reference the first parameter... mario@181: * ccb@202: * Returns the paramater name to which a value is being passed! ccb@202: * You can determine the value being passed by calling ccb@202: * function_call_param_iterator_c::search_f() ccb@202: */ ccb@202: symbol_c *function_call_param_iterator_c::next_f(void) { ccb@202: current_value = NULL; mario@181: param_count = 0; ccb@202: iterate_f_next_param++; ccb@202: current_operation = function_call_param_iterator_c::iterate_f_op; mario@181: void *res = f_call->accept(*this); mario@181: return (symbol_c *)res; mario@181: } mario@181: ccb@202: ccb@202: /* Skip to the next non-formal parameter. After object creation, ccb@202: * the object references on parameter _before_ the first, so ccb@202: * this function must be called once to get the object to ccb@202: * reference the first parameter... ccb@202: * ccb@202: * Returns whatever is being passed to the parameter! ccb@202: */ ccb@202: symbol_c *function_call_param_iterator_c::next_nf(void) { ccb@202: current_value = NULL; ccb@202: param_count = 0; ccb@202: iterate_nf_next_param++; ccb@202: current_operation = function_call_param_iterator_c::iterate_nf_op; ccb@202: void *res = f_call->accept(*this); ccb@202: current_value = (symbol_c *)res; ccb@202: return (symbol_c *)res; ccb@202: } ccb@202: mario@181: /* Search for the value passed to the parameter named ... */ ccb@202: symbol_c *function_call_param_iterator_c::search_f(symbol_c *param_name) { ccb@202: current_value = NULL; mario@181: if (NULL == param_name) ERROR; mario@181: search_param_name = dynamic_cast(param_name); mario@181: if (NULL == search_param_name) ERROR; ccb@202: current_operation = function_call_param_iterator_c::search_f_op; mario@181: void *res = f_call->accept(*this); ccb@202: current_value = (symbol_c *)res; mario@181: return (symbol_c *)res; mario@181: } mario@181: ccb@202: /* Returns the value being passed to the current parameter. */ ccb@202: symbol_c *function_call_param_iterator_c::get_current_value(void) { ccb@202: return current_value; ccb@202: } mario@181: mario@181: /********************************/ mario@181: /* B 1.7 Configuration elements */ mario@181: /********************************/ mario@181: mario@181: /* mario@181: CONFIGURATION configuration_name mario@181: optional_global_var_declarations mario@181: (resource_declaration_list | single_resource_declaration) mario@181: optional_access_declarations mario@181: optional_instance_specific_initializations mario@181: END_CONFIGURATION mario@181: */ mario@181: /* mario@181: SYM_REF6(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, unused) mario@181: */ mario@181: mario@181: /* helper symbol for configuration_declaration */ mario@181: /* mario@181: SYM_LIST(resource_declaration_list_c) mario@181: */ mario@181: mario@181: /* mario@181: RESOURCE resource_name ON resource_type_name mario@181: optional_global_var_declarations mario@181: single_resource_declaration mario@181: END_RESOURCE mario@181: */ mario@181: /* mario@181: SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration) mario@181: */ mario@181: mario@181: /* task_configuration_list program_configuration_list */ mario@181: /* mario@181: SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list) mario@181: */ mario@181: mario@181: /* helper symbol for single_resource_declaration */ mario@181: /* mario@181: SYM_LIST(task_configuration_list_c) mario@181: */ mario@181: mario@181: /* helper symbol for single_resource_declaration */ mario@181: /* mario@181: SYM_LIST(program_configuration_list_c) mario@181: */ mario@181: mario@181: /* helper symbol for mario@181: * - access_path mario@181: * - instance_specific_init mario@181: */ mario@181: /* mario@181: SYM_LIST(any_fb_name_list_c) mario@181: */ mario@181: mario@181: /* [resource_name '.'] global_var_name ['.' structure_element_name] */ mario@181: /* mario@181: SYM_REF4(global_var_reference_c, resource_name, global_var_name, structure_element_name, unused) mario@181: */ mario@181: mario@181: /* prev_declared_program_name '.' symbolic_variable */ mario@181: /* mario@181: SYM_REF2(program_output_reference_c, program_name, symbolic_variable) mario@181: */ mario@181: mario@181: /* TASK task_name task_initialization */ mario@181: /* mario@181: SYM_REF2(task_configuration_c, task_name, task_initialization) mario@181: */ mario@181: mario@181: /* '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */ mario@181: /* mario@181: SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused) mario@181: */ mario@181: mario@181: /* PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */ mario@181: // SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused) mario@181: void *function_call_param_iterator_c::visit(program_configuration_c *symbol) { mario@181: TRACE("program_configuration_c"); mario@181: return symbol->prog_conf_elements->accept(*this); mario@181: } mario@181: mario@181: /* prog_conf_elements ',' prog_conf_element */ mario@181: // SYM_LIST(prog_conf_elements_c) mario@181: void *function_call_param_iterator_c::visit(prog_conf_elements_c *symbol) { mario@181: TRACE("prog_conf_elements_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: /* fb_name WITH task_name */ mario@181: /* mario@181: SYM_REF2(fb_task_c, fb_name, task_name) mario@181: */ mario@181: mario@181: /* any_symbolic_variable ASSIGN prog_data_source */ mario@181: // SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source) mario@181: void *function_call_param_iterator_c::visit(prog_cnxn_assign_c *symbol) { mario@181: TRACE("prog_cnxn_assign_c"); mario@181: mario@181: /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario) mario@181: * do not understand the semantics that should be implmeneted if it is not a mario@181: * symbolic_variable, so for the moment we simply give up! mario@181: */ mario@181: symbolic_variable_c *symb_var = dynamic_cast(symbol->symbolic_variable); mario@181: if (NULL == symb_var) mario@181: ERROR; mario@181: mario@181: return handle_parameter_assignment(symb_var->var_name, symbol->prog_data_source); mario@181: } mario@181: mario@181: /* any_symbolic_variable SENDTO data_sink */ mario@181: // SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, prog_data_source) mario@181: void *function_call_param_iterator_c::visit(prog_cnxn_sendto_c *symbol) { mario@181: TRACE("prog_cnxn_sendto_c"); mario@181: mario@181: /* NOTE: symbolic_variable may be something other than a symbolic_variable_c, but I (Mario) mario@181: * do not understand the semantics that should be implmeneted if it is not a mario@181: * symbolic_variable, so for the moment we simply give up! mario@181: */ mario@181: symbolic_variable_c *symb_var = dynamic_cast(symbol->symbolic_variable); mario@181: if (NULL == symb_var) mario@181: ERROR; mario@181: mario@181: return handle_parameter_assignment(symb_var->var_name, symbol->data_sink); mario@181: } mario@181: mario@181: /* VAR_CONFIG instance_specific_init_list END_VAR */ mario@181: /* mario@181: SYM_REF2(instance_specific_initializations_c, instance_specific_init_list, unused) mario@181: */ mario@181: mario@181: /* helper symbol for instance_specific_initializations */ mario@181: /* mario@181: SYM_LIST(instance_specific_init_list_c) mario@181: */ mario@181: mario@181: /* resource_name '.' program_name '.' {fb_name '.'} mario@181: ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization)) mario@181: */ mario@181: /* mario@181: SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization) mario@181: */ mario@181: mario@181: /* helper symbol for instance_specific_init */ mario@181: /* function_block_type_name ':=' structure_initialization */ mario@181: /* mario@181: SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization) mario@181: */ mario@181: mario@181: mario@181: mario@181: /****************************************/ mario@181: /* B.2 - Language IL (Instruction List) */ mario@181: /****************************************/ mario@181: /***********************************/ mario@181: /* B 2.1 Instructions and Operands */ mario@181: /***********************************/ mario@181: mario@181: /* | function_name [il_operand_list] */ mario@181: // SYM_REF2(il_function_call_c, function_name, il_operand_list) mario@181: void *function_call_param_iterator_c::visit(il_function_call_c *symbol) { mario@181: TRACE("il_function_call_c"); mario@181: if (NULL != symbol->il_operand_list) mario@181: return symbol->il_operand_list->accept(*this); mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /* | function_name '(' eol_list [il_param_list] ')' */ mario@181: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) mario@181: void *function_call_param_iterator_c::visit(il_formal_funct_call_c *symbol) { mario@181: TRACE("il_formal_funct_call_c"); mario@181: if (NULL != symbol->il_param_list) mario@181: return symbol->il_param_list->accept(*this); mario@181: return NULL; mario@181: } mario@181: mario@181: mario@181: /* il_call_operator prev_declared_fb_name mario@181: * | il_call_operator prev_declared_fb_name '(' ')' mario@181: * | il_call_operator prev_declared_fb_name '(' eol_list ')' mario@181: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' mario@181: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' mario@181: */ mario@181: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) mario@181: void *function_call_param_iterator_c::visit(il_fb_call_c *symbol) { mario@181: TRACE("il_fb_call_c"); mario@181: /* the following should never occur. In reality the syntax parser mario@181: * will guarantee that they never occur, but it makes it easier to mario@181: * understand the remaining code :-) mario@181: */ mario@181: //if ((NULL == symbol->il_operand_list) && (NULL == symbol->il_param_list)) ERROR; mario@181: //if ((NULL != symbol->il_operand_list) && (NULL != symbol->il_param_list)) ERROR; mario@181: mario@181: if (NULL != symbol->il_operand_list) mario@181: return symbol->il_operand_list->accept(*this); mario@181: if (NULL != symbol->il_param_list) mario@181: return symbol->il_param_list->accept(*this); mario@181: return NULL; mario@181: } mario@181: mario@181: /* | il_operand_list ',' il_operand */ mario@181: // SYM_LIST(il_operand_list_c) mario@181: void *function_call_param_iterator_c::visit(il_operand_list_c *symbol) { mario@181: TRACE("il_operand_list_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: mario@181: /* | il_initial_param_list il_param_instruction */ mario@181: // SYM_LIST(il_param_list_c) mario@181: void *function_call_param_iterator_c::visit(il_param_list_c *symbol) { mario@181: TRACE("il_param_list_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: /* il_assign_operator il_operand mario@181: * | il_assign_operator '(' eol_list simple_instr_list ')' mario@181: */ mario@181: // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) mario@181: void *function_call_param_iterator_c::visit(il_param_assignment_c *symbol) { mario@181: TRACE("il_param_assignment_c"); mario@181: mario@181: // TODO : We do not yet handle a instruction list passed as parameter !!! mario@181: // since we do not yet support it, it is best to simply stop than to fail silently... mario@181: if (NULL != symbol->simple_instr_list) ERROR; mario@181: ccb@202: return handle_parameter_assignment((symbol_c *)symbol->il_assign_operator->accept(*this), symbol->il_operand); mario@181: } mario@181: mario@181: /* il_assign_out_operator variable */ mario@181: // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); mario@181: void *function_call_param_iterator_c::visit(il_param_out_assignment_c *symbol) { mario@181: TRACE("il_param_out_assignment_c"); mario@181: return handle_parameter_assignment((symbol_c *)symbol->il_assign_out_operator->accept(*this), symbol->variable); mario@181: } mario@181: mario@181: mario@181: /*******************/ mario@181: /* B 2.2 Operators */ mario@181: /*******************/ ccb@202: /* any_identifier ASSIGN */ ccb@202: // SYM_REF1(il_assign_operator_c, variable_name) ccb@202: void *function_call_param_iterator_c::visit(il_assign_operator_c *symbol) { ccb@202: TRACE("il_assign_operator_c"); ccb@202: return (void *)symbol->variable_name; ccb@202: } ccb@202: mario@181: /*| [NOT] any_identifier SENDTO */ mario@181: // SYM_REF2(il_assign_out_operator_c, option, variable_name) mario@181: void *function_call_param_iterator_c::visit(il_assign_out_operator_c *symbol) { mario@181: TRACE("il_assign_out_operator_c"); mario@181: mario@181: // TODO : Handle not_param !!! mario@181: // we do not yet support it, so it is best to simply stop than to fail silently... ccb@202: // if (NULL != symbol->option) ERROR; mario@181: mario@181: return (void *)symbol->variable_name; mario@181: } mario@181: mario@181: mario@181: mario@181: mario@181: /***************************************/ mario@181: /* B.3 - Language ST (Structured Text) */ mario@181: /***************************************/ mario@181: /***********************/ mario@181: /* B 3.1 - Expressions */ mario@181: /***********************/ mario@181: mario@181: /* mario@181: SYM_REF2(function_invocation_c, function_name, parameter_assignment_list) mario@181: */ mario@181: void *function_call_param_iterator_c::visit(function_invocation_c *symbol) { mario@181: TRACE("function_invocation_c"); ccb@202: /* If the syntax parser is working correctly, exactly one of the ccb@202: * following two symbols will be NULL, while the other is != NULL. ccb@202: */ laurent@247: if (symbol == (function_invocation_c *)f_call) { laurent@247: if (symbol-> formal_param_list != NULL) return symbol-> formal_param_list->accept(*this); laurent@247: if (symbol->nonformal_param_list != NULL) return symbol->nonformal_param_list->accept(*this); laurent@247: } ccb@202: ccb@202: return NULL; mario@181: } mario@181: mario@181: mario@181: /********************/ mario@181: /* B 3.2 Statements */ mario@181: /********************/ mario@181: mario@181: /*********************************/ mario@181: /* B 3.2.1 Assignment Statements */ mario@181: /*********************************/ mario@181: /* mario@181: SYM_REF2(assignment_statement_c, l_exp, r_exp) mario@181: */ mario@181: mario@181: /*****************************************/ mario@181: /* B 3.2.2 Subprogram Control Statements */ mario@181: /*****************************************/ mario@181: /* RETURN */ mario@181: // SYM_REF0(return_statement_c) mario@181: mario@181: mario@181: /* fb_name '(' [param_assignment_list] ')' */ mario@181: /* param_assignment_list -> may be NULL ! */ mario@181: // SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) mario@181: void *function_call_param_iterator_c::visit(fb_invocation_c *symbol) { mario@181: TRACE("fb_invocation_c"); ccb@202: /* If the syntax parser is working correctly, only one of the ccb@202: * following two symbols will be != NULL. ccb@202: * However, both may be NULL simultaneously! ccb@202: */ ccb@202: if (symbol-> formal_param_list != NULL) return symbol-> formal_param_list->accept(*this); ccb@202: if (symbol->nonformal_param_list != NULL) return symbol->nonformal_param_list->accept(*this); ccb@202: ccb@202: return NULL; mario@181: } mario@181: mario@181: /* helper symbol for fb_invocation */ mario@181: /* param_assignment_list ',' param_assignment */ mario@181: // SYM_LIST(param_assignment_list_c) mario@181: void *function_call_param_iterator_c::visit(param_assignment_list_c *symbol) { mario@181: TRACE("param_assignment_list_c"); mario@181: return search_list(symbol); mario@181: } mario@181: mario@181: /* variable_name ASSIGN expression */ mario@181: // SYM_REF2(input_variable_param_assignment_c, variable_name, expression) mario@181: void *function_call_param_iterator_c::visit(input_variable_param_assignment_c *symbol) { mario@181: TRACE("input_variable_param_assignment_c"); mario@181: return handle_parameter_assignment(symbol->variable_name, symbol->expression); mario@181: } mario@181: mario@181: /* [NOT] variable_name '=>' variable */ mario@181: // SYM_REF4(output_variable_param_assignment_c, not_param, variable_name, variable, unused) mario@181: void *function_call_param_iterator_c::visit(output_variable_param_assignment_c *symbol) { mario@181: TRACE("output_variable_param_assignment_c"); mario@181: // TODO : Handle not_param !!! mario@181: if (NULL != symbol->not_param) ERROR; // we do not yet support it, so it is best to simply stop than to fail silently... mario@181: mario@181: return handle_parameter_assignment(symbol->variable_name, symbol->variable); mario@181: } mario@181: mario@181: /* helper CLASS for output_variable_param_assignment */ mario@181: // SYM_REF0(not_paramassign_c) mario@181: // TODO... ??? mario@181: mario@181: