stage3/flow_control_analysis.cc
changeset 443 ff4d26b7e51d
child 448 1bd18fc06911
equal deleted inserted replaced
442:bd5998ee8876 443:ff4d26b7e51d
       
     1 /*
       
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
       
     3  *
       
     4  *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
       
     5  *
       
     6  *  This program is free software: you can redistribute it and/or modify
       
     7  *  it under the terms of the GNU General Public License as published by
       
     8  *  the Free Software Foundation, either version 3 of the License, or
       
     9  *  (at your option) any later version.
       
    10  *
       
    11  *  This program is distributed in the hope that it will be useful,
       
    12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14  *  GNU General Public License for more details.
       
    15  *
       
    16  *  You should have received a copy of the GNU General Public License
       
    17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18  *
       
    19  *
       
    20  * This code is made available on the understanding that it will not be
       
    21  * used in safety-critical situations without a full and competent review.
       
    22  */
       
    23 
       
    24 /*
       
    25  * An IEC 61131-3 compiler.
       
    26  *
       
    27  * Based on the
       
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    29  *
       
    30  */
       
    31 
       
    32 
       
    33 /*
       
    34  *  Do flow control analysis of the IEC 61131-3 code.
       
    35  *
       
    36  *  We currently only do this for IL code.
       
    37  *  This class will annotate the abstract syntax tree, by filling in the
       
    38  *  prev_il_instruction variable in the il_instruction_c, so it points to
       
    39  *  the previous il_instruction_c object in the instruction list instruction_list_c.
       
    40  */
       
    41 
       
    42 #include "flow_control_analysis.hh"
       
    43 
       
    44 
       
    45 
       
    46 /* set to 1 to see debug info during execution */
       
    47 static int debug = 0;
       
    48 
       
    49 flow_control_analysis_c::flow_control_analysis_c(symbol_c *ignore) {
       
    50 }
       
    51 
       
    52 flow_control_analysis_c::~flow_control_analysis_c(void) {
       
    53 }
       
    54 
       
    55 
       
    56 
       
    57 /************************************/
       
    58 /* B 1.5 Program organization units */
       
    59 /************************************/
       
    60 /*********************/
       
    61 /* B 1.5.1 Functions */
       
    62 /*********************/
       
    63 void *flow_control_analysis_c::visit(function_declaration_c *symbol) {
       
    64 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
    65 	if (debug) printf("Doing flow control analysis in body of function %s\n", ((token_c *)(symbol->derived_function_name))->value);
       
    66 	symbol->function_body->accept(*this);
       
    67 	delete search_varfb_instance_type;
       
    68 	search_varfb_instance_type = NULL;
       
    69 	return NULL;
       
    70 }
       
    71 
       
    72 /***************************/
       
    73 /* B 1.5.2 Function blocks */
       
    74 /***************************/
       
    75 void *flow_control_analysis_c::visit(function_block_declaration_c *symbol) {
       
    76 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
    77 	if (debug) printf("Doing flow control analysis in body of FB %s\n", ((token_c *)(symbol->fblock_name))->value);
       
    78 	symbol->fblock_body->accept(*this);
       
    79 	delete search_varfb_instance_type;
       
    80 	search_varfb_instance_type = NULL;
       
    81 	return NULL;
       
    82 }
       
    83 
       
    84 /********************/
       
    85 /* B 1.5.3 Programs */
       
    86 /********************/
       
    87 void *flow_control_analysis_c::visit(program_declaration_c *symbol) {
       
    88 	search_varfb_instance_type = new search_varfb_instance_type_c(symbol);
       
    89 	if (debug) printf("Doing flow control analysis in body of program %s\n", ((token_c *)(symbol->program_type_name))->value);
       
    90 	symbol->function_block_body->accept(*this);
       
    91 	delete search_varfb_instance_type;
       
    92 	search_varfb_instance_type = NULL;
       
    93 	return NULL;
       
    94 }
       
    95 
       
    96 
       
    97 /********************************/
       
    98 /* B 1.7 Configuration elements */
       
    99 /********************************/
       
   100 void *flow_control_analysis_c::visit(configuration_declaration_c *symbol) {
       
   101 	return NULL;
       
   102 }
       
   103 
       
   104 
       
   105 /****************************************/
       
   106 /* B.2 - Language IL (Instruction List) */
       
   107 /****************************************/
       
   108 /***********************************/
       
   109 /* B 2.1 Instructions and Operands */
       
   110 /***********************************/
       
   111 
       
   112 /*| instruction_list il_instruction */
       
   113 // SYM_LIST(instruction_list_c)
       
   114 void *flow_control_analysis_c::visit(instruction_list_c *symbol) {
       
   115 	for(int i = 1; i < symbol->n; i++) {
       
   116 		prev_il_instruction = symbol->elements[i-1];
       
   117 		symbol->elements[i]->accept(*this);
       
   118 	}
       
   119 	return NULL;
       
   120 }
       
   121 
       
   122 /* | label ':' [il_incomplete_instruction] eol_list */
       
   123 // SYM_REF2(il_instruction_c, label, il_instruction)
       
   124 // void *visit(instruction_list_c *symbol);
       
   125 void *flow_control_analysis_c::visit(il_instruction_c *symbol) {
       
   126 	symbol->prev_il_instruction = prev_il_instruction;
       
   127 	/* TODO: handle labels correctly!
       
   128 	 *
       
   129 	 *      Don't forget to handle multiple consecutive lables too!
       
   130 	 * 	  label2:
       
   131 	 *        label3:
       
   132 	 *        label4:
       
   133 	 *                LD I
       
   134 	 */
       
   135 
       
   136 return NULL;
       
   137 }
       
   138 
       
   139 
       
   140 #if 0
       
   141 /* | function_name [il_operand_list] */
       
   142 /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */
       
   143 // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;)
       
   144 void *flow_control_analysis_c::visit(il_function_call_c *symbol) {
       
   145 	return NULL;
       
   146 }
       
   147 
       
   148 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
       
   149 // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
       
   150 void *flow_control_analysis_c::visit(il_expression_c *symbol) {
       
   151 return NULL;
       
   152 }
       
   153 
       
   154 /*   il_call_operator prev_declared_fb_name
       
   155  * | il_call_operator prev_declared_fb_name '(' ')'
       
   156  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
       
   157  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
       
   158  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
       
   159  */
       
   160 /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */
       
   161 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
       
   162 void *flow_control_analysis_c::visit(il_fb_call_c *symbol) {
       
   163 	return NULL;
       
   164 }
       
   165 
       
   166 
       
   167 /* | function_name '(' eol_list [il_param_list] ')' */
       
   168 /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */
       
   169 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;)
       
   170 void *flow_control_analysis_c::visit(il_formal_funct_call_c *symbol) {
       
   171 	return NULL;
       
   172 }
       
   173 
       
   174 
       
   175 /*
       
   176     void *visit(il_operand_list_c *symbol);
       
   177     void *visit(simple_instr_list_c *symbol);
       
   178     void *visit(il_param_list_c *symbol);
       
   179     void *visit(il_param_assignment_c *symbol);
       
   180     void *visit(il_param_out_assignment_c *symbol);
       
   181  */
       
   182 
       
   183 
       
   184 #endif
       
   185