conti@564: /*
conti@564: * matiec - a compiler for the programming languages defined in IEC 61131-3
conti@564: *
conti@564: * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt)
conti@564: * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it)
conti@564: *
conti@564: * This program is free software: you can redistribute it and/or modify
conti@564: * it under the terms of the GNU General Public License as published by
conti@564: * the Free Software Foundation, either version 3 of the License, or
conti@564: * (at your option) any later version.
conti@564: *
conti@564: * This program is distributed in the hope that it will be useful,
conti@564: * but WITHOUT ANY WARRANTY; without even the implied warranty of
conti@564: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
conti@564: * GNU General Public License for more details.
conti@564: *
conti@564: * You should have received a copy of the GNU General Public License
conti@564: * along with this program. If not, see .
conti@564: *
conti@564: *
conti@564: * This code is made available on the understanding that it will not be
conti@564: * used in safety-critical situations without a full and competent review.
conti@564: */
conti@564:
conti@564: /*
conti@564: * An IEC 61131-3 compiler.
conti@564: *
conti@564: * Based on the
conti@564: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
conti@564: *
conti@564: */
conti@564:
conti@781: /* Determine the value of an constant expression.
conti@564: * A reference to the relevant type definition is returned.
conti@564: *
conti@564: * For example:
conti@564: * 2 + 3 -> returns reference
conti@564: * 22.2 - 5 -> returns reference
conti@564: * etc...
conti@564: */
conti@564:
conti@564: #include
conti@564: #include "../absyntax_utils/absyntax_utils.hh"
conti@564:
conti@564:
msousa@792:
msousa@792: /* For the moment we disable constant propagation algorithm as it is not yet complete,
msousa@792: * and due to this is currently brocken and producing incorrect results!
msousa@792: */
msousa@792: #define DO_CONSTANT_PROPAGATION__ 0
msousa@792:
msousa@792:
msousa@792:
conti@564: class constant_folding_c : public iterator_visitor_c {
conti@564: search_varfb_instance_type_c *search_varfb_instance_type;
conti@564: int error_count;
msousa@568: bool warning_found;
conti@564: int current_display_error_level;
msousa@612: /* Pointer to the previous IL instruction, which contains the current cvalue of the data stored in the IL stack, i.e. the default variable, a.k.a. accumulator */
msousa@612: symbol_c *prev_il_instruction;
msousa@612: /* the current IL operand being analyzed */
msousa@612: symbol_c *il_operand;
conti@564:
msousa@612: public:
conti@564: constant_folding_c(symbol_c *symbol = NULL);
conti@564: virtual ~constant_folding_c(void);
conti@564: int get_error_count();
mjsousa@963: int handle_var_extern_global_pair(symbol_c *extern_var_name, symbol_c *extern_var_decl, symbol_c *global_var_name, symbol_c *global_var_decl);
mjsousa@963: private:
mjsousa@963: void *handle_var_list_decl(symbol_c *var_list, symbol_c *type_decl);
mjsousa@963: public:
mjsousa@945: #if 0
mjsousa@945: // not currently needed, so comment it out!...
mjsousa@945: /* utility functions for other stage3 algorithms to access the contant folded values */
mjsousa@945: /* written as static since we do not need to iteratively visit the symbols! */
mjsousa@945: // returns true if both symbols have the same value in all the cvalues
mjsousa@945: static bool is_equal_cvalue(symbol_c *symbol_1, symbol_c *symbol_2);
mjsousa@945: #endif
mjsousa@945:
msousa@612: private:
conti@564: /*********************/
conti@564: /* B 1.2 - Constants */
conti@564: /*********************/
conti@564: /******************************/
conti@564: /* B 1.2.1 - Numeric Literals */
conti@564: /******************************/
conti@564: void *visit(real_c *symbol);
conti@564: void *visit(integer_c *symbol);
conti@564: void *visit(neg_real_c *symbol);
conti@564: void *visit(neg_integer_c *symbol);
conti@564: void *visit(binary_integer_c *symbol);
conti@564: void *visit(octal_integer_c *symbol);
conti@564: void *visit(hex_integer_c *symbol);
conti@564: void *visit(integer_literal_c *symbol);
conti@564: void *visit(real_literal_c *symbol);
conti@564: void *visit(bit_string_literal_c *symbol);
conti@564: void *visit(boolean_literal_c *symbol);
conti@564: void *visit(boolean_true_c *symbol);
conti@564: void *visit(boolean_false_c *symbol);
conti@564:
Laurent@633: /************************/
Laurent@633: /* B 1.2.3.1 - Duration */
Laurent@633: /********* **************/
Laurent@633: void *visit(fixed_point_c *symbol);
Laurent@633:
conti@774: /*********************/
conti@774: /* B 1.4 - Variables */
conti@774: /*********************/
msousa@792: #if DO_CONSTANT_PROPAGATION__
conti@774: void *visit(symbolic_variable_c *symbol);
mjsousa@963: #endif // DO_CONSTANT_PROPAGATION__
mjsousa@963: void *visit(symbolic_constant_c *symbol);
mjsousa@963:
mjsousa@963: /******************************************/
mjsousa@963: /* B 1.4.3 - Declaration & Initialisation */
mjsousa@963: /******************************************/
mjsousa@963: void *visit( var1_init_decl_c *symbol);
mjsousa@963: void *visit( external_declaration_c *symbol);
mjsousa@963:
mjsousa@963: /**************************************/
mjsousa@963: /* B.1.5 - Program organization units */
mjsousa@963: /**************************************/
mjsousa@963: /***********************/
mjsousa@963: /* B 1.5.1 - Functions */
mjsousa@963: /***********************/
mjsousa@963: void *visit(function_declaration_c *symbol);
mjsousa@963:
mjsousa@963: /*****************************/
mjsousa@963: /* B 1.5.2 - Function Blocks */
mjsousa@963: /*****************************/
mjsousa@963: void *visit(function_block_declaration_c *symbol);
conti@774:
conti@774: /**********************/
conti@774: /* B 1.5.3 - Programs */
conti@774: /**********************/
conti@774: void *visit(program_declaration_c *symbol);
mjsousa@963:
conti@774:
msousa@612: /****************************************/
msousa@612: /* B.2 - Language IL (Instruction List) */
msousa@612: /****************************************/
msousa@612: /***********************************/
msousa@612: /* B 2.1 Instructions and Operands */
msousa@612: /***********************************/
msousa@612: // void *visit(instruction_list_c *symbol); /* Not needed, since we inherit from iterator_visitor_c */
msousa@612: void *visit(il_instruction_c *symbol);
msousa@612: void *visit(il_simple_operation_c *symbol);
msousa@612: //void *visit(il_function_call_c *symbol); /* TODO */
msousa@612: void *visit(il_expression_c *symbol);
msousa@612: void *visit(il_jump_operation_c *symbol);
msousa@612: void *visit(il_fb_call_c *symbol);
msousa@612: //void *visit(il_formal_funct_call_c *symbol); /* TODO */
msousa@612: //void *visit(il_operand_list_c *symbol); /* Not needed, since we inherit from iterator_visitor_c */
msousa@612: void *visit(simple_instr_list_c *symbol);
msousa@612: void *visit(il_simple_instruction_c *symbol);
msousa@612:
msousa@612:
msousa@612: /*******************/
msousa@612: /* B 2.2 Operators */
msousa@612: /*******************/
msousa@612: void *visit( LD_operator_c *symbol);
msousa@612: void *visit( LDN_operator_c *symbol);
msousa@612: void *visit( ST_operator_c *symbol);
msousa@612: void *visit( STN_operator_c *symbol);
msousa@612: void *visit( NOT_operator_c *symbol);
msousa@612: void *visit( S_operator_c *symbol);
msousa@612: void *visit( R_operator_c *symbol);
msousa@612: void *visit( S1_operator_c *symbol);
msousa@612: void *visit( R1_operator_c *symbol);
msousa@612: void *visit( CLK_operator_c *symbol);
msousa@612: void *visit( CU_operator_c *symbol);
msousa@612: void *visit( CD_operator_c *symbol);
msousa@612: void *visit( PV_operator_c *symbol);
msousa@612: void *visit( IN_operator_c *symbol);
msousa@612: void *visit( PT_operator_c *symbol);
msousa@612: void *visit( AND_operator_c *symbol);
msousa@612: void *visit( OR_operator_c *symbol);
msousa@612: void *visit( XOR_operator_c *symbol);
msousa@612: void *visit( ANDN_operator_c *symbol);
msousa@612: void *visit( ORN_operator_c *symbol);
msousa@612: void *visit( XORN_operator_c *symbol);
msousa@612: void *visit( ADD_operator_c *symbol);
msousa@612: void *visit( SUB_operator_c *symbol);
msousa@612: void *visit( MUL_operator_c *symbol);
msousa@612: void *visit( DIV_operator_c *symbol);
msousa@612: void *visit( MOD_operator_c *symbol);
msousa@612: void *visit( GT_operator_c *symbol);
msousa@612: void *visit( GE_operator_c *symbol);
msousa@612: void *visit( EQ_operator_c *symbol);
msousa@612: void *visit( LT_operator_c *symbol);
msousa@612: void *visit( LE_operator_c *symbol);
msousa@612: void *visit( NE_operator_c *symbol);
msousa@612: void *visit( CAL_operator_c *symbol);
msousa@612: void *visit( CALC_operator_c *symbol);
msousa@612: void *visit(CALCN_operator_c *symbol);
msousa@612: void *visit( RET_operator_c *symbol);
msousa@612: void *visit( RETC_operator_c *symbol);
msousa@612: void *visit(RETCN_operator_c *symbol);
msousa@612: void *visit( JMP_operator_c *symbol);
msousa@612: void *visit( JMPC_operator_c *symbol);
msousa@612: void *visit(JMPCN_operator_c *symbol);
msousa@612: /* Symbol class handled together with function call checks */
msousa@612: // void *visit(il_assign_operator_c *symbol, variable_name);
msousa@612: /* Symbol class handled together with function call checks */
msousa@612: // void *visit(il_assign_operator_c *symbol, option, variable_name);
msousa@612:
conti@564: /***************************************/
conti@564: /* B.3 - Language ST (Structured Text) */
conti@564: /***************************************/
conti@564: /***********************/
conti@564: /* B 3.1 - Expressions */
conti@564: /***********************/
msousa@612: void *visit( or_expression_c *symbol);
msousa@612: void *visit( xor_expression_c *symbol);
msousa@612: void *visit( and_expression_c *symbol);
msousa@612: void *visit( equ_expression_c *symbol);
conti@564: void *visit(notequ_expression_c *symbol);
msousa@612: void *visit( lt_expression_c *symbol);
msousa@612: void *visit( gt_expression_c *symbol);
msousa@612: void *visit( le_expression_c *symbol);
msousa@612: void *visit( ge_expression_c *symbol);
msousa@612: void *visit( add_expression_c *symbol);
msousa@612: void *visit( sub_expression_c *symbol);
msousa@612: void *visit( mul_expression_c *symbol);
msousa@612: void *visit( div_expression_c *symbol);
msousa@612: void *visit( mod_expression_c *symbol);
msousa@612: void *visit( power_expression_c *symbol);
msousa@612: void *visit( neg_expression_c *symbol);
msousa@612: void *visit( not_expression_c *symbol);
msousa@612: //void *visit(function_invocation_c *symbol); /* TODO */
conti@774:
msousa@612:
msousa@792: #if DO_CONSTANT_PROPAGATION__
conti@774: /*********************************/
conti@774: /* B 3.2.1 Assignment Statements */
conti@774: /*********************************/
conti@774: void *visit(assignment_statement_c *symbol);
conti@774:
conti@774: /********************************/
conti@774: /* B 3.2.3 Selection Statements */
conti@774: /********************************/
conti@774: void *visit(if_statement_c *symbol);
conti@780:
conti@780: /********************************/
conti@780: /* B 3.2.4 Iteration Statements */
conti@780: /********************************/
conti@780: void *visit(for_statement_c *symbol);
conti@780: void *visit(while_statement_c *symbol);
conti@780: void *visit(repeat_statement_c *symbol);
msousa@792: #endif // DO_CONSTANT_PROPAGATION__
conti@564: };
conti@564: