msousa@677: /*
msousa@677: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@677: *
msousa@677: * Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt)
msousa@677: *
msousa@677: * This program is free software: you can redistribute it and/or modify
msousa@677: * it under the terms of the GNU General Public License as published by
msousa@677: * the Free Software Foundation, either version 3 of the License, or
msousa@677: * (at your option) any later version.
msousa@677: *
msousa@677: * This program is distributed in the hope that it will be useful,
msousa@677: * but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@677: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
msousa@677: * GNU General Public License for more details.
msousa@677: *
msousa@677: * You should have received a copy of the GNU General Public License
msousa@677: * along with this program. If not, see .
msousa@677: *
msousa@677: *
msousa@677: * This code is made available on the understanding that it will not be
msousa@677: * used in safety-critical situations without a full and competent review.
msousa@677: */
msousa@677:
msousa@677: /*
msousa@677: * An IEC 61131-3 compiler.
msousa@677: *
msousa@677: * Based on the
msousa@677: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
msousa@677: *
msousa@677: */
msousa@677:
msousa@677:
msousa@677: /*
msousa@677: * Data type analysis of IL code may leave some IL instructions with an undefined datatype.
msousa@677: * This visitor will set the datatype for all these symbols, so that all symbols have a well
msousa@677: * defined datatype when we reach stage4.
msousa@677: *
msousa@677: * Example:
msousa@677: * =========
msousa@677: *
msousa@677: * VAR
msousa@677: * N : INT := 99 ;
msousa@677: * tonv: TON;
msousa@677: * byte_var: BYTE;
msousa@677: * tonv : TON;
msousa@677: * a : BYTE;
msousa@677: * t : time;
msousa@677: * tod1: tod;
msousa@677: * END_VAR
msousa@677: *
msousa@677: * (0) --> Data type before executing forced_narrow_candidate_datatypes_c
msousa@677: * (1) --> Data type after executing 1st pass of forced_narrow_candidate_datatypes_c
msousa@677: * (2) --> Data type after executing 2nd pass of forced_narrow_candidate_datatypes_c
msousa@677: *
msousa@677: * --- --> NULL (undefined datatype)
msousa@677: * *** --> invalid_type_name_c (invalid datatype)
msousa@677: *
msousa@681: * (0) PASS1 (1) PASS2 (2)
msousa@677: *
msousa@681: * --- (e) *** *** CAL tonv (
msousa@681: * PT := T#1s
msousa@681: * )
msousa@681: * --- (e) *** *** JMP l4
msousa@681: *
msousa@681: * --- (e) sint sint l0: LD 1
msousa@681: * --- (e) sint sint ADD 2
msousa@681: * --- (c) sint sint CAL tonv (
msousa@681: * PT := T#1s
msousa@681: * )
msousa@681: *
msousa@681: * --- (e) sint sint LD 45
msousa@681: * --- (c) sint sint ADD 45
msousa@681: *
msousa@681: *
msousa@681: * --- (e) sint sint LD 3
msousa@681: * --- (e) sint sint l1:
msousa@681: * --- (c) sint sint l2: ADD 4
msousa@681: * int int int LD 5
msousa@681: * int int int ST n
msousa@681: * int int int JMP l3
msousa@681: *
msousa@681: * --- (d) --- (e) sint LD 5
msousa@681: * --- (d) --- (e) sint SUB 6
msousa@681: * --- (d)(e) sint sint JMP l1
msousa@681: *
msousa@681: * --- (e) bool bool LD FALSE
msousa@681: * --- (e) bool bool NOT
msousa@681: * --- (b) bool bool RET
msousa@681: *
msousa@681: * int int int l3:
msousa@681: * int int int ST n
msousa@681: * --- (b) int int RET
msousa@681: *
msousa@681: * --- (e) *** *** l4:
msousa@681: * --- (e) *** *** CAL tonv (
msousa@681: * PT := T#1s
msousa@681: * )
msousa@681: * --- (a) *** *** JMP l0
msousa@681: * --- (b) byte byte LD 88
msousa@677: *
msousa@677: *
msousa@677: *
msousa@677: */
msousa@677:
msousa@677:
msousa@677:
msousa@677: #include "forced_narrow_candidate_datatypes.hh"
msousa@677: #include "datatype_functions.hh"
msousa@677:
msousa@677:
msousa@677: /* set to 1 to see debug info during execution */
msousa@677: static int debug = 0;
msousa@677:
msousa@677: forced_narrow_candidate_datatypes_c::forced_narrow_candidate_datatypes_c(symbol_c *ignore)
msousa@677: :narrow_candidate_datatypes_c(ignore) {
msousa@677: }
msousa@677:
msousa@677: forced_narrow_candidate_datatypes_c::~forced_narrow_candidate_datatypes_c(void) {
msousa@677: }
msousa@677:
msousa@677:
msousa@677:
msousa@691: void forced_narrow_candidate_datatypes_c::forced_narrow_il_instruction(symbol_c *symbol, std::vector &next_il_instruction) {
msousa@691: if (NULL == symbol->datatype) {
msousa@691: if (symbol->candidate_datatypes.empty()) {
msousa@693: symbol->datatype = &(get_datatype_info_c::invalid_type_name); // This will occur in the situations (a) in the above example
msousa@691: // return NULL; // No need to return control to the visit() method of the base class... But we do so, just to be safe (called at the end of this function)!
msousa@691: } else {
msousa@691: if (next_il_instruction.empty()) {
msousa@691: symbol->datatype = symbol->candidate_datatypes[0]; // This will occur in the situations (b) in the above example
msousa@691: } else {
msousa@691: symbol_c *next_datatype = NULL;
msousa@691:
msousa@691: /* find the datatype of the following IL instructions (they should all be identical by now, but we don't have an assertion checking for this. */
msousa@691: for (unsigned int i=0; i < next_il_instruction.size(); i++)
msousa@691: if (NULL != next_il_instruction[i]->datatype)
msousa@691: next_datatype = next_il_instruction[i]->datatype;
msousa@691: if (get_datatype_info_c::is_type_valid(next_datatype)) {
msousa@691: // This will occur in the situations (c) in the above example
msousa@691: symbol->datatype = symbol->candidate_datatypes[0];
msousa@691: } else {
msousa@691: // This will occur in the situations (d) in the above example
msousa@691: // it is not possible to determine the exact situation in the current pass, so we can't do anything just yet. Leave it for the next time around!
msousa@691: }
msousa@691: }
msousa@691: }
msousa@691: }
msousa@691: }
msousa@677:
msousa@677:
msousa@681:
msousa@677: /****************************************/
msousa@677: /* B.2 - Language IL (Instruction List) */
msousa@677: /****************************************/
msousa@677: /***********************************/
msousa@677: /* B 2.1 Instructions and Operands */
msousa@677: /***********************************/
msousa@677:
msousa@677: /*| instruction_list il_instruction */
msousa@677: // SYM_LIST(instruction_list_c)
msousa@677: void *forced_narrow_candidate_datatypes_c::visit(instruction_list_c *symbol) {
msousa@677: for(int j = 0; j < 2; j++) {
msousa@677: for(int i = symbol->n-1; i >= 0; i--) {
msousa@677: symbol->elements[i]->accept(*this);
msousa@677: }
msousa@677: }
msousa@677:
msousa@677: /* Assert that this algorithm managed to remove all NULL datatypes! */
msousa@684: /* NOTE: The forced_narrow_candidate_datatypes_c assumes that the original IEC 61131-3 source code does not have any bugs!
msousa@684: * This means we cannot run this assertion here, as the compiler will bork in the presence of bug in the code being compiled! Not good!!
msousa@684: */
msousa@684: /*
msousa@677: for(int i = symbol->n-1; i >= 0; i--) {
msousa@677: if (NULL == symbol->elements[i]->datatype)
msousa@677: ERROR;
msousa@677: }
msousa@684: */
msousa@686:
msousa@677: return NULL;
msousa@677: }
msousa@677:
msousa@677:
msousa@677:
msousa@677: /* | label ':' [il_incomplete_instruction] eol_list */
msousa@677: // SYM_REF2(il_instruction_c, label, il_instruction)
msousa@677: // void *visit(instruction_list_c *symbol);
msousa@677: void *forced_narrow_candidate_datatypes_c::visit(il_instruction_c *symbol) {
msousa@691: forced_narrow_il_instruction(symbol, symbol->next_il_instruction);
msousa@677:
msousa@677: /* return control to the visit() method of the base class! */
msousa@691: return narrow_candidate_datatypes_c::visit(symbol); // This handles the situations (e) in the above example
msousa@677: }
msousa@677:
msousa@677:
msousa@677:
msousa@677:
msousa@677:
msousa@677: /* | il_simple_operator [il_operand] */
msousa@677: // SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(il_simple_operation_c *symbol)
msousa@677:
msousa@677: /* | function_name [il_operand_list] */
msousa@677: /* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */
msousa@677: // SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;)
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(il_function_call_c *symbol)
msousa@677:
msousa@677: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
msousa@677: // SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(il_expression_c *symbol)
msousa@677:
msousa@677: /* il_jump_operator label */
msousa@677: // SYM_REF2(il_jump_operation_c, il_jump_operator, label)
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(il_jump_operation_c *symbol)
msousa@677:
msousa@677: /* il_call_operator prev_declared_fb_name
msousa@677: * | il_call_operator prev_declared_fb_name '(' ')'
msousa@677: * | il_call_operator prev_declared_fb_name '(' eol_list ')'
msousa@677: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
msousa@677: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
msousa@677: */
msousa@677: /* 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 */
msousa@677: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(il_fb_call_c *symbol)
msousa@677:
msousa@677: /* | function_name '(' eol_list [il_param_list] ')' */
msousa@677: /* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */
msousa@677: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;)
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(il_formal_funct_call_c *symbol)
msousa@677:
msousa@677: // void *visit(il_operand_list_c *symbol);
msousa@677: // void *forced_narrow_candidate_datatypes_c::visit(simple_instr_list_c *symbol)
msousa@677:
msousa@677: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
msousa@691: void *forced_narrow_candidate_datatypes_c::visit(il_simple_instruction_c*symbol) {
msousa@691: forced_narrow_il_instruction(symbol, symbol->next_il_instruction);
msousa@691:
msousa@691: /* return control to the visit() method of the base class! */
msousa@691: return narrow_candidate_datatypes_c::visit(symbol); // This handle the situations (e) in the above example
msousa@691: }
msousa@691:
msousa@677:
msousa@677: /*
msousa@677: void *visit(il_param_list_c *symbol);
msousa@677: void *visit(il_param_assignment_c *symbol);
msousa@677: void *visit(il_param_out_assignment_c *symbol);
msousa@677: */
msousa@677:
msousa@681:
msousa@681:
msousa@681: /***************************************/
msousa@681: /* B.3 - Language ST (Structured Text) */
msousa@681: /***************************************/
msousa@681: // SYM_LIST(statement_list_c)
msousa@681: /* The normal narrow_candidate_datatypes_c algorithm does not leave any symbol, in an ST code, with an undefined datatype.
msousa@681: * There is therefore no need to re-run the narrow algorithm here, so we overide the narrow_candidate_datatypes_c visitor,
msousa@681: * and simply bug out!
msousa@681: */
msousa@681: void *forced_narrow_candidate_datatypes_c::visit(statement_list_c *symbol) {return NULL;}
msousa@681: