stage3/constant_folding.cc
author mjsousa
Thu, 08 Jan 2015 19:04:03 +0000
changeset 986 2064a22cc1f2
parent 985 837c6532a9b8
child 988 3b12a6cf9fbd
permissions -rw-r--r--
Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     1
/*
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     3
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     5
 *  Copyright (C) 2012       Manuele Conti (conti.ma@alice.it)
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     6
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    10
 *  (at your option) any later version.
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    11
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    15
 *  GNU General Public License for more details.
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    16
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    19
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    20
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    23
 */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    24
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    25
/*
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    26
 * An IEC 61131-3 compiler.
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    27
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    28
 * Based on the
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    29
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    30
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    31
 */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    32
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    33
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    34
640
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    35
/* TODO: 
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    36
 *         - Add support for comparison (= and !=) of enumeration literals!
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    37
 *              We will need to add another const_value entry to the symbol_c, containing the 
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    38
 *              possible enumeration value of the enum constant!
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    39
 *              Doing this will allow us to more easily implement a constant_propagation_c later on!
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    40
 *
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    41
 *         - Add support for comparison (= and !=) of the exact same variable
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    42
 *                (e.g. if (int_v = int_v) then ...)
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    43
 */
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
    44
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    45
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    46
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    47
/* Do constant folding...
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    48
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    49
 * I.e., Determine the value of all expressions in which only constant values (i.e. literals) are used.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    50
 * The (constant) result of each operation is stored (annotated) in the respective operation symbol 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    51
 * (e.g.: add_expression_c) in the abstract syntax tree,
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    52
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    53
 * For example:
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    54
 *       2 + 3         -> the constant value '5'    is stored in the add_expression_c symbol.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    55
 *       22.2 - 5.0    -> the constant value '17.2' is stored in the add_expression_c symbol.
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    56
 *       etc...
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    57
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    58
 *
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    59
 * NOTE 1 
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    60
 *      Some operations and constants can have multiple data types. For example,
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    61
 *        1 AND 0
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    62
 *      may be either a BOOL, BYTE, WORD or LWORD.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    63
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    64
 *      The same happens with 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    65
 *        1 + 2
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    66
 *      which may be signed (e.g. INT) or unsigned (UINT)
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    67
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    68
 *      For the above reason, instead of storing a single constant value, we actually store 4:
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    69
 *        - bool
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    70
 *        - uint64
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    71
 *        -  int64
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    72
 *        - real64
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    73
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    74
 *      Additionally, since the result of an operation may result in an overflow, we actually
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    75
 *      store the result inside a struct (defined in absyntax.hh)
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    76
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    77
 *             ** During stage 3 (semantic analysis/checking) we will be doing constant folding.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    78
 *              * That algorithm will anotate the abstract syntax tree with the result of operations
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    79
 *              * on literals (i.e. 44 + 55 will store the result 99).
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    80
 *              * Since the same source code (e.g. 1 + 0) may actually be a BOOL or an ANY_INT,
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    81
 *              * or an ANY_BIT, we need to handle all possibilities, and determine the result of the
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    82
 *              * operation assuming each type.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    83
 *              * For this reason, we have one entry for each possible type, with some expressions
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    84
 *              * having more than one entry filled in!
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    85
 *              **
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    86
 *             typedef enum { cs_undefined,   // not defined --> const_value is not valid!
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    87
 *                            cs_const_value, // const value is valid
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    88
 *                            cs_overflow     // result produced overflow or underflow --> const_value is not valid!
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    89
 *                          } const_status_t;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    90
 *    
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    91
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    92
 *                 const_status_t status;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    93
 *                 real64_t       value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    94
 *             } const_value_real64_t;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    95
 *             const_value_real64_t *const_value_real64; // when NULL --> UNDEFINED
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    96
 *             
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    97
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    98
 *                 const_status_t status;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    99
 *                 int64_t        value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   100
 *             } const_value_int64_t;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   101
 *             const_value_int64_t *const_value_int64; // when NULL --> UNDEFINED
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   102
 *             
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   103
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   104
 *                 const_status_t status;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   105
 *                 uint64_t       value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   106
 *             } const_value_uint64_t;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   107
 *             const_value_uint64_t *const_value_uint64; // when NULL --> UNDEFINED
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   108
 *             
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   109
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   110
 *                 const_status_t status;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   111
 *                 bool           value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   112
 *             } const_value_bool_t;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   113
 *             const_value_bool_t *const_value_bool; // when NULL --> UNDEFINED
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   114
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   115
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   116
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   117
 * NOTE 2 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   118
 *    This file does not print out any error messages!
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   119
 *    We cannot really print out error messages when we find an overflow. Since each operation
781
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   120
 *    (symbol in the abstract syntax tree for that operation) will have up to 4 constant results,
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   121
 *    it may happen that some of them overflow, while other do not.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   122
 *    We must wait for data type checking to determine the exact data type of each expression
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   123
 *    before we can decide whether or not we should print out an overflow error message.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   124
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   125
 *    For this reason, this visitor merely annotates the abstract syntax tree, and leaves the
781
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   126
 *    actually printing of errors for the print_datatype_errors_c class!
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   127
 *
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   128
 * NOTE 3
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   129
 *    Constant Folding class is extended with a implementation constant propagation algorithm
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   130
 *    by Mario de Sousa.
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   131
 *    Main idea is not to implement a general constant propagation algorithm but to reinterpret it
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   132
 *    for visitor classes.
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   133
 *    We declared a hash map, it contains a variables list linked with current constant values.
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   134
 *    During expression evaluation we can retrieve a constant value to symbolic variables getting it from the map.
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   135
 *    Also at join source points we use a meet semilattice rules to merge current values between a block
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   136
 *    and adjacent block.
577547327f67 Add note about How Constant Folding class is extended with a implementation constant propagation algorithm
Manuele Conti <conti.ma@alice.it>
parents: 780
diff changeset
   137
 *
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   138
 */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   139
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   140
#include "constant_folding.hh"
565
8acbddf75333 Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
   141
#include <stdlib.h> /* required for malloc() */
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   142
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   143
#include <string.h>  /* required for strlen() */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   144
// #include <stdlib.h>  /* required for atoi() */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   145
#include <errno.h>   /* required for errno */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   146
607
be9ba3531afb cleaning up code. Changing HUGE_VAL to INFINITY.
Mario de Sousa <msousa@fe.up.pt>
parents: 604
diff changeset
   147
#include "../main.hh" // required for uint8_t, real_64_t, ..., and the macros NAN, INFINITY, INT8_MAX, REAL32_MAX, ... */
579
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   148
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   149
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   150
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   151
568
5f79478142d7 make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents: 567
diff changeset
   152
5f79478142d7 make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents: 567
diff changeset
   153
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   154
#define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order)   ? (symbol1) : (symbol2))
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   155
#define  LAST_(symbol1, symbol2) (((symbol1)->last_order  > (symbol2)->last_order)    ? (symbol1) : (symbol2))
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   156
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   157
#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   158
  if (current_display_error_level >= error_level) {                                                                         \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   159
    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   160
            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   161
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   162
    fprintf(stderr, __VA_ARGS__);                                                                                           \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   163
    fprintf(stderr, "\n");                                                                                                  \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   164
    error_count++;                                                                                                     \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   165
  }                                                                                                                         \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   166
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   167
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   168
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   169
#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   170
    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   171
            FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   172
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   173
    fprintf(stderr, __VA_ARGS__);                                                                                           \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   174
    fprintf(stderr, "\n");                                                                                                  \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   175
    warning_found = true;                                                                                                   \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   176
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   177
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   178
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   179
567
e5deeb6d4d2f create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents: 566
diff changeset
   180
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   181
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   182
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   183
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   184
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   185
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   186
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   187
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   188
965
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   189
#define SET_CVALUE(dtype, symbol, new_value)  ((symbol)->const_value._##dtype.set(new_value))
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   190
#define GET_CVALUE(dtype, symbol)             ((symbol)->const_value._##dtype.get())
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   191
#define SET_OVFLOW(dtype, symbol)             ((symbol)->const_value._##dtype.set_overflow())
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   192
#define SET_NONCONST(dtype, symbol)           ((symbol)->const_value._##dtype.set_nonconst())
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   193
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   194
#define VALID_CVALUE(dtype, symbol)           ((symbol)->const_value._##dtype.is_valid())
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   195
#define IS_OVFLOW(dtype, symbol)              ((symbol)->const_value._##dtype.is_overflow())
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   196
#define IS_NONCONST(dtype, symbol)            ((symbol)->const_value._##dtype.is_nonconst())
983
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
   197
#define IS_UNDEFINED(dtype, symbol)           ((symbol)->const_value._##dtype.is_undefined())
965
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   198
#define ISZERO_CVALUE(dtype, symbol)          ((symbol)->const_value._##dtype.is_zero())
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   199
575
a1b63f776535 cleaning up the code...
Mario de Sousa <msousa@fe.up.pt>
parents: 574
diff changeset
   200
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   201
#define ISEQUAL_CVALUE(dtype, symbol1, symbol2) \
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   202
	(VALID_CVALUE(dtype, symbol1) && VALID_CVALUE(dtype, symbol2) && (GET_CVALUE(dtype, symbol1) == GET_CVALUE(dtype, symbol2))) 
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   203
648
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   204
#define DO_BINARY_OPER(oper_type, operation, res_type, operand1, operand2) {                                              \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   205
	if      (VALID_CVALUE(oper_type, operand1) && VALID_CVALUE(oper_type, operand2))                                  \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   206
		{SET_CVALUE(res_type, symbol, GET_CVALUE(oper_type, operand1) operation GET_CVALUE(oper_type, operand2));}\
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   207
	else if (IS_OVFLOW   (oper_type, operand1) || IS_OVFLOW   (oper_type, operand2))                                  \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   208
		{SET_OVFLOW(res_type, symbol);}  /* does it really make sense to set OVFLOW when restype is boolean??  */ \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   209
	else if (IS_NONCONST (oper_type, operand1) || IS_NONCONST (oper_type, operand2))                                  \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   210
		{SET_NONCONST(res_type, symbol);}                                                                         \
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   211
}
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   212
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   213
#define DO_UNARY_OPER(dtype, operation, operand) {                                                                        \
648
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   214
	if      (VALID_CVALUE(dtype, operand))                                                                            \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   215
		{SET_CVALUE(dtype, symbol, operation GET_CVALUE(dtype, operand));}                                        \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   216
	else if (IS_OVFLOW   (dtype, operand))                                                                            \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   217
		{SET_OVFLOW(dtype, symbol);}                                                                              \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   218
	else if (IS_NONCONST (dtype, operand))                                                                            \
5ca2aabb8bcb More precise handling of const value status.
Mario de Sousa <msousa@fe.up.pt>
parents: 643
diff changeset
   219
		{SET_NONCONST(dtype, symbol);}                                                                            \
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   220
}
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   221
776
96a1199d0739 Fix comment about meet semilattice rules.
Manuele Conti <conti.ma@alice.it>
parents: 774
diff changeset
   222
/* Constant Propagation: Rules for Meet from "Cooper K., Torczon L. - Engineering a Compiler, Second Edition - 2011"
96a1199d0739 Fix comment about meet semilattice rules.
Manuele Conti <conti.ma@alice.it>
parents: 774
diff changeset
   223
 * at 9.3 Static Single-Assignment Form  page 517
96a1199d0739 Fix comment about meet semilattice rules.
Manuele Conti <conti.ma@alice.it>
parents: 774
diff changeset
   224
 * - any * undefined = any
96a1199d0739 Fix comment about meet semilattice rules.
Manuele Conti <conti.ma@alice.it>
parents: 774
diff changeset
   225
 * - any * non_const = non_const
96a1199d0739 Fix comment about meet semilattice rules.
Manuele Conti <conti.ma@alice.it>
parents: 774
diff changeset
   226
 * - constant * constant = constant  (if equal)
96a1199d0739 Fix comment about meet semilattice rules.
Manuele Conti <conti.ma@alice.it>
parents: 774
diff changeset
   227
 * - constant * constant = non_const (if not equal)
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
   228
 */
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
   229
#define COMPUTE_MEET_SEMILATTICE(dtype, c1, c2, resValue) {\
965
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   230
		if (( c1._##dtype.get()  != c2._##dtype.get() && c2._##dtype.is_valid() && c1._##dtype.is_valid()) ||\
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   231
		    ( c1._##dtype.is_nonconst() && c2._##dtype.is_valid() ) ||\
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   232
		    ( c2._##dtype.is_nonconst() && c1._##dtype.is_valid() )) {\
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   233
			resValue._##dtype.set_nonconst();\
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
   234
		} else {\
965
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
   235
			resValue._##dtype.set(c1._##dtype.get());\
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
   236
		}\
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
   237
}
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
   238
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
   239
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   240
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   241
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   242
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   243
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   244
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   245
/***            convert string to numerical value                    ***/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   246
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   247
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   248
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   249
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   250
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   251
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   252
  /* To allow the compiler to be portable, we cannot assume that int64_t is mapped onto long long int,
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   253
   * so we cannot call strtoll() and strtoull() in extract_int64() and extract_uint64().
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   254
   *
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   255
   * So, we create our own strtouint64() and strtoint64() functions.
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   256
   * (We actually call them matiec_strtoint64() so they will not clash with any function
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   257
   *  that may be added to the standard library in the future).
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   258
   * We actually create several of each, and let the compiler choose which is the correct one,
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   259
   * by having it resolve the call to the overloaded function. For the C++ compiler to be able
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   260
   * to resolve this ambiguity, we need to add a dummy parameter to each function!
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   261
   *
640
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
   262
   * TODO: support platforms (where the compiler will run) in which int64_t is mapped onto int !!
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
   263
   *       Is this really needed?
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
   264
   *       Currently, when trying to compile matiec on sych a platform, the C++ compiler will not
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
   265
   *       find any apropriate matiec_strtoint64() to call, so matiec will not be able to be compiled.
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
   266
   *       If you need this, you are welcome to fix it yourself...
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   267
   */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   268
static  int64_t matiec_strtoint64 (         long      int *dummy, const char *nptr, char **endptr, int base) {return strtol  (nptr, endptr, base);}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   269
static  int64_t matiec_strtoint64 (         long long int *dummy, const char *nptr, char **endptr, int base) {return strtoll (nptr, endptr, base);}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   270
  
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   271
static uint64_t matiec_strtouint64(unsigned long      int *dummy, const char *nptr, char **endptr, int base) {return strtoul (nptr, endptr, base);}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   272
static uint64_t matiec_strtouint64(unsigned long long int *dummy, const char *nptr, char **endptr, int base) {return strtoull(nptr, endptr, base);}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   273
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   274
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   275
/* extract the value of an integer from an integer_c object !! */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   276
/* NOTE: it must ignore underscores! */
601
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   277
/* NOTE: To follow the basic structure used throughout the compiler's code, we should really be
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   278
 * writing this as a visitor_c (and do away with the dynamic casts!), but since we only have 3 distinct 
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   279
 * symbol class types to handle, it is probably easier to read if we write it as a standard function... 
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   280
 */
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   281
int64_t extract_int64_value(symbol_c *sym, bool *overflow) {
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   282
  int64_t      ret;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   283
  std::string  str = "";
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   284
  char        *endptr;
735
0304ff59fd7f Fix some important clang++ warnings.
Manuele Conti <conti.ma@alice.it>
parents: 690
diff changeset
   285
  const char  *value = NULL;
601
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   286
  int          base;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   287
  integer_c         *integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   288
  hex_integer_c     *hex_integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   289
  octal_integer_c   *octal_integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   290
  binary_integer_c  *binary_integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   291
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   292
   if       ((integer        = dynamic_cast<integer_c *>(sym))        != NULL) {value = integer       ->value + 0; base = 10;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   293
   else  if ((hex_integer    = dynamic_cast<hex_integer_c *>(sym))    != NULL) {value = hex_integer   ->value + 3; base = 16;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   294
   else  if ((octal_integer  = dynamic_cast<octal_integer_c *>(sym))  != NULL) {value = octal_integer ->value + 2; base =  8;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   295
   else  if ((binary_integer = dynamic_cast<binary_integer_c *>(sym)) != NULL) {value = binary_integer->value + 2; base =  2;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   296
   else  ERROR;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   297
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   298
  for(unsigned int i = 0; i < strlen(value); i++)
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   299
    if (value[i] != '_')  str += value[i];
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   300
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   301
  errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   302
  ret = matiec_strtoint64((int64_t *)NULL, str.c_str(), &endptr, base);
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   303
  if (overflow != NULL)
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   304
    *overflow = (errno == ERANGE);
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   305
  if (((errno != 0) && (errno != ERANGE)) || (*endptr != '\0'))
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   306
    ERROR;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   307
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   308
  return ret;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   309
}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   310
601
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   311
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   312
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   313
uint64_t extract_uint64_value(symbol_c *sym, bool *overflow) {
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   314
  uint64_t     ret;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   315
  std::string  str = "";
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   316
  char        *endptr;
735
0304ff59fd7f Fix some important clang++ warnings.
Manuele Conti <conti.ma@alice.it>
parents: 690
diff changeset
   317
  const char  *value = NULL;
601
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   318
  int          base;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   319
  integer_c         *integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   320
  hex_integer_c     *hex_integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   321
  octal_integer_c   *octal_integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   322
  binary_integer_c  *binary_integer;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   323
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   324
   if       ((integer        = dynamic_cast<integer_c *>(sym))        != NULL) {value = integer       ->value + 0; base = 10;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   325
   else  if ((hex_integer    = dynamic_cast<hex_integer_c *>(sym))    != NULL) {value = hex_integer   ->value + 3; base = 16;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   326
   else  if ((octal_integer  = dynamic_cast<octal_integer_c *>(sym))  != NULL) {value = octal_integer ->value + 2; base =  8;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   327
   else  if ((binary_integer = dynamic_cast<binary_integer_c *>(sym)) != NULL) {value = binary_integer->value + 2; base =  2;}
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   328
   else  ERROR;
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   329
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   330
  for(unsigned int i = 0; i < strlen(value); i++)
683ae5444237 fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 600
diff changeset
   331
    if (value[i] != '_')  str += value[i];
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   332
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   333
  errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   334
  ret = matiec_strtouint64((uint64_t *)NULL, str.c_str(), &endptr, base);
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   335
  if (overflow != NULL)
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   336
    *overflow = (errno == ERANGE);
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   337
  if (((errno != 0) && (errno != ERANGE)) || (*endptr != '\0'))
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   338
    ERROR;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   339
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   340
  return ret;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   341
}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   342
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   343
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   344
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   345
/* extract the value of a real from an real_c object !! */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   346
/* NOTE: it must ignore underscores! */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   347
/* From iec_bison.yy
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   348
 *  real:
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   349
 *   real_token		{$$ = new real_c($1, locloc(@$));}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   350
 * | fixed_point_token	{$$ = new real_c($1, locloc(@$));}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   351
 *
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   352
 * From iec_flex.ll
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   353
 * {real}			{yylval.ID=strdup(yytext); return real_token;}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   354
 * {fixed_point}		{yylval.ID=strdup(yytext); return fixed_point_token;}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   355
 *
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   356
 * real		{integer}\.{integer}{exponent}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   357
 * fixed_point		{integer}\.{integer}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   358
 * exponent        [Ee]([+-]?){integer}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   359
 * integer         {digit}((_?{digit})*)
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   360
 */
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   361
real64_t extract_real_value(symbol_c *sym, bool *overflow) {
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   362
  std::string str = "";
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   363
  real_c *real_sym;
633
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   364
  fixed_point_c *fixed_point_sym;
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   365
  char   *endptr;
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   366
  real64_t ret;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   367
633
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   368
  if ((real_sym = dynamic_cast<real_c *>(sym)) != NULL) {
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   369
	for(unsigned int i = 0; i < strlen(real_sym->value); i++)
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   370
      if (real_sym->value[i] != '_') str += real_sym->value[i];
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   371
  }
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   372
  else if ((fixed_point_sym = dynamic_cast<fixed_point_c *>(sym)) != NULL) {
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   373
    for(unsigned int i = 0; i < strlen(fixed_point_sym->value); i++)
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   374
      if (fixed_point_sym->value[i] != '_') str += fixed_point_sym->value[i];
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   375
  }
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
   376
  else ERROR;
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   377
    
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   378
  errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
890
499486ece119 Fix compilation error on platforms where real64_t is mapped onto 'long double'
mjsousa
parents: 792
diff changeset
   379
  #if    (real64_tX  == float)
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   380
    ret = strtof(str.c_str(),  &endptr);
890
499486ece119 Fix compilation error on platforms where real64_t is mapped onto 'long double'
mjsousa
parents: 792
diff changeset
   381
  #elif  (real64_tX  == double)
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   382
    ret = strtod(str.c_str(),  &endptr);
890
499486ece119 Fix compilation error on platforms where real64_t is mapped onto 'long double'
mjsousa
parents: 792
diff changeset
   383
  #elif  (real64_tX  == long_double)
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   384
    ret = strtold(str.c_str(), &endptr);
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   385
  #else 
890
499486ece119 Fix compilation error on platforms where real64_t is mapped onto 'long double'
mjsousa
parents: 792
diff changeset
   386
    #error Could not determine which data type is being used for real64_t (defined in main.hh). Aborting!
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   387
  #endif
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   388
  if (overflow != NULL)
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   389
    *overflow = (errno == ERANGE);
600
f5d4e9f91513 determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 596
diff changeset
   390
  if (((errno != 0) && (errno != ERANGE)) || (*endptr != '\0'))
596
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   391
    ERROR;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   392
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   393
  return ret;
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   394
}
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   395
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   396
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   397
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   398
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   399
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   400
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   401
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   402
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   403
/***        Functions to check for overflow situation                ***/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   404
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   405
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   406
/***********************************************************************/
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   407
4efb11e44065 Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents: 592
diff changeset
   408
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   409
/* NOTE:
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   410
 *   Most of the conditions to detect overflows on signed and unsigned integer operations were adapted from
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   411
 *   https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   412
 *   https://www.securecoding.cert.org/confluence/display/seccode/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   413
 */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   414
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   415
/* NOTE: If at all possible, all overflow tests are done by pre-condition tests, i.e. tests that 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   416
 *       can be run _before_ the operation is executed, and therefore without accessing the result!
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   417
 *
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   418
 *       The exception is for real/floating point values, that simply test if the result is NaN (not a number).
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   419
 */
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   420
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   421
/* res = a + b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   422
static void CHECK_OVERFLOW_uint64_SUM(symbol_c *res, symbol_c *a, symbol_c *b) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   423
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   424
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   425
	/* Test by post-condition: If sum is smaller than either operand => overflow! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   426
	// if (GET_CVALUE(uint64, res) < GET_CVALUE(uint64, a))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   427
	/* Test by pre-condition: If (UINT64_MAX - a) < b => overflow! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   428
	if ((UINT64_MAX - GET_CVALUE(uint64, a)) < GET_CVALUE(uint64, b))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   429
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   430
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   431
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   432
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   433
/* res = a - b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   434
static void CHECK_OVERFLOW_uint64_SUB(symbol_c *res, symbol_c *a, symbol_c *b) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   435
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   436
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   437
	/* Test by post-condition: If diference is larger than a => overflow! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   438
	// if (GET_CVALUE(uint64, res) > GET_CVALUE(uint64, a))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   439
	/* Test by pre-condition: if b > a => overflow! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   440
	if (GET_CVALUE(uint64, b) > GET_CVALUE(uint64, a))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   441
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   442
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   443
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   444
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   445
/* res = a * b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   446
static void CHECK_OVERFLOW_uint64_MUL(symbol_c *res, symbol_c *a, symbol_c *b) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   447
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   448
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   449
	/* Test by pre-condition: If (UINT64_MAX / a) < b => overflow! */
786
c370918ca7fb Fix divide by 0 bug.
Mario de Sousa <msousa@fe.up.pt>
parents: 783
diff changeset
   450
	if (0 == GET_CVALUE(uint64, a))
c370918ca7fb Fix divide by 0 bug.
Mario de Sousa <msousa@fe.up.pt>
parents: 783
diff changeset
   451
		return; // multiplying by 0 will always result in 0, a valid result!	  
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   452
	if ((UINT64_MAX / GET_CVALUE(uint64, a)) < GET_CVALUE(uint64, b))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   453
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   454
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   455
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   456
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   457
/* res = a / b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   458
static void CHECK_OVERFLOW_uint64_DIV(symbol_c *res, symbol_c *a, symbol_c *b) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   459
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   460
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   461
	if (GET_CVALUE(uint64, b) == 0) /* division by zero! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   462
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   463
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   464
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   465
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   466
/* res = a MOD b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   467
static void CHECK_OVERFLOW_uint64_MOD(symbol_c *res, symbol_c *a, symbol_c *b) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   468
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   469
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   470
	/* no overflow condition exists, including division by zero, which IEC 61131-3 considers legal for MOD operation! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   471
	if (false) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   472
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   473
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   474
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   475
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   476
/* res = - a */
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   477
static void CHECK_OVERFLOW_uint64_NEG(symbol_c *res, symbol_c *a) {
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   478
	/* The only legal operation is res = -0, everything else is an overflow! */
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   479
	if (VALID_CVALUE(uint64, a) && (GET_CVALUE(uint64, a) != 0))
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   480
		SET_OVFLOW(uint64, res);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   481
}
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   482
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   483
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   484
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   485
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   486
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   487
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   488
/* res = a + b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   489
static void CHECK_OVERFLOW_int64_SUM(symbol_c *res, symbol_c *a_ptr, symbol_c *b_ptr) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   490
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   491
		return;
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   492
	int64_t a = GET_CVALUE(int64, a_ptr);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   493
	int64_t b = GET_CVALUE(int64, b_ptr);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   494
	/* The following test is valid no matter what representation is being used (e.g. two's complement, etc...) */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   495
	if (((b > 0) && (a > (INT64_MAX - b)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   496
	 || ((b < 0) && (a < (INT64_MIN - b))))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   497
		SET_OVFLOW(int64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   498
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   499
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   500
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   501
/* res = a - b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   502
static void CHECK_OVERFLOW_int64_SUB(symbol_c *res, symbol_c *a_ptr, symbol_c *b_ptr) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   503
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   504
		return;
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   505
	int64_t a = GET_CVALUE(int64, a_ptr);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   506
	int64_t b = GET_CVALUE(int64, b_ptr);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   507
	/* The following test is valid no matter what representation is being used (e.g. two's complement, etc...) */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   508
	if (((b > 0) && (a < (INT64_MIN + b)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   509
	 || ((b < 0) && (a > (INT64_MAX + b))))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   510
		SET_OVFLOW(int64, res);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   511
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   512
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   513
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   514
/* res = a * b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   515
static void CHECK_OVERFLOW_int64_MUL(symbol_c *res, symbol_c *a_ptr, symbol_c *b_ptr) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   516
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   517
		return;
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   518
	int64_t a = GET_CVALUE(int64, a_ptr);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   519
	int64_t b = GET_CVALUE(int64, b_ptr);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   520
	if (   ( (a > 0) &&  (b > 0) &&             (a > (INT64_MAX / b))) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   521
	    || ( (a > 0) && !(b > 0) &&             (b < (INT64_MIN / a))) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   522
	    || (!(a > 0) &&  (b > 0) &&             (a < (INT64_MIN / b))) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   523
	    || (!(a > 0) && !(b > 0) && (a != 0) && (b < (INT64_MAX / a))))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   524
		SET_OVFLOW(int64, res);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   525
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   526
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   527
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   528
/* res = a / b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   529
static void CHECK_OVERFLOW_int64_DIV(symbol_c *res, symbol_c *a_ptr, symbol_c *b_ptr) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   530
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   531
		return;
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   532
	int64_t a = GET_CVALUE(int64, a_ptr);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   533
	int64_t b = GET_CVALUE(int64, b_ptr);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   534
	if ((b == 0) || ((a == INT64_MIN) && (b == -1)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   535
		SET_OVFLOW(int64, res);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   536
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   537
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   538
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   539
/* res = a MOD b */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   540
static void CHECK_OVERFLOW_int64_MOD(symbol_c *res, symbol_c *a_ptr, symbol_c *b_ptr) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   541
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   542
		return;
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   543
	int64_t a = GET_CVALUE(int64, a_ptr);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   544
	int64_t b = GET_CVALUE(int64, b_ptr);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   545
	/* IEC 61131-3 standard says IN1 MOD IN2 must be equivalent to
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   546
	 *  IF (IN2 = 0) THEN OUT:=0 ; ELSE OUT:=IN1 - (IN1/IN2)*IN2 ; END_IF
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   547
	 *
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   548
	 * Note that, when IN1 = INT64_MIN, and IN2 = -1, an overflow occurs in the division,
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   549
	 * so although the MOD operation should be OK, acording to the above definition, we actually have an overflow!!
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   550
	 *
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   551
	 * On the other hand, division by 0 is OK!!
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   552
	 */
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   553
	if ((a == INT64_MIN) && (b == -1))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   554
		SET_OVFLOW(int64, res);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   555
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   556
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   557
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   558
/* res = - a */
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   559
static void CHECK_OVERFLOW_int64_NEG(symbol_c *res, symbol_c *a) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   560
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   561
		return;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   562
	if (GET_CVALUE(int64, a) == INT64_MIN)
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   563
		SET_OVFLOW(int64, res);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   564
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   565
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   566
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   567
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   568
579
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   569
static void CHECK_OVERFLOW_real64(symbol_c *res_ptr) {
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   570
	if (!VALID_CVALUE(real64, res_ptr))
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   571
		return;
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   572
	real64_t res = GET_CVALUE(real64, res_ptr);
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   573
	/* NaN => underflow, overflow, number is a higher precision format, is a complex number (IEEE standard) */
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   574
	/* The IEC 61131-3 clearly states in section '2.5.1.5.2 Numerical functions':
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   575
	 * "It is an error if the result of evaluation of one of these [numerical] functions exceeds the range of values
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   576
	 *  specified for the data type of the function output, or if division by zero is attempted."
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   577
	 * For this reason, any operation that has as a result a positive or negative inifinity, is also an error!
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   578
	 */
607
be9ba3531afb cleaning up code. Changing HUGE_VAL to INFINITY.
Mario de Sousa <msousa@fe.up.pt>
parents: 604
diff changeset
   579
	if ((isnan(res)) || (res == INFINITY) || (res == -INFINITY))
579
983a3b743085 Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents: 578
diff changeset
   580
		SET_OVFLOW(real64, res_ptr);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   581
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   582
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   583
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   584
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   585
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   586
/***********************************************************************/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   587
/***********************************************************************/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   588
/***********************************************************************/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   589
/***        Functions to execute operations on the const values      ***/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   590
/***********************************************************************/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   591
/***********************************************************************/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   592
/***********************************************************************/
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   593
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   594
985
837c6532a9b8 Add comment about bug that needs to be fixed later on.
mjsousa
parents: 984
diff changeset
   595
/* TODO: FIXME !!!!!
837c6532a9b8 Add comment about bug that needs to be fixed later on.
mjsousa
parents: 984
diff changeset
   596
 *   The following operation is wrong - it does not handle the comparisons of all possible datatypes correctly.
837c6532a9b8 Add comment about bug that needs to be fixed later on.
mjsousa
parents: 984
diff changeset
   597
 *   The result of comparig the bool, int64, and uint64 are overwritten by the comparison of the real64 type!
837c6532a9b8 Add comment about bug that needs to be fixed later on.
mjsousa
parents: 984
diff changeset
   598
 */
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   599
/* static void *handle_cmp(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2, OPERATION) */
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   600
#define handle_cmp(symbol, oper1, oper2, operation) {               \
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   601
	if ((NULL == oper1) || (NULL == oper2)) return NULL;        \
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   602
	DO_BINARY_OPER(  bool, operation, bool, oper1, oper2);     \
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   603
	DO_BINARY_OPER(uint64, operation, bool, oper1, oper2);     \
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   604
	DO_BINARY_OPER( int64, operation, bool, oper1, oper2);     \
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   605
	DO_BINARY_OPER(real64, operation, bool, oper1, oper2);     \
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   606
	return NULL;                                                \
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   607
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   608
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   609
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   610
/* NOTE: the MOVE standard function is equivalent to the ':=' in ST syntax */
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   611
static void *handle_move(symbol_c *to, symbol_c *from) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   612
	if (NULL == from) return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   613
	to->const_value = from->const_value;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   614
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   615
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   616
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   617
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   618
/* unary negation (multiply by -1) */
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   619
static void *handle_neg(symbol_c *symbol, symbol_c *oper) {
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   620
	if (NULL == oper) return NULL;
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   621
	/* NOTE: The oper may never be an integer/real literal, '-1' and '-2.2' are stored as an neg_integer_c/neg_real_c instead.
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   622
	 *       Because of this, we MUST NOT handle the INT_MIN special situation that is handled in neg_integer_c visitor!
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   623
	 *
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   624
	 *       VAR v1, v2, v3 : UINT; END_VAR;
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   625
	 *       v1 =  9223372036854775808 ; (* |INT64_MIN| == -INT64_MIN *)   <------ LEGAL
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   626
	 *       v2 =  -(-v1);                                                 <------ ILLEGAL (since it -v1 is overflow!)
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   627
	 *       v2 =  -(-9223372036854775808 );                               <------ MUST also be ILLEGAL 
640
ffa02cf2b335 Add some comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 621
diff changeset
   628
	 */
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   629
	DO_UNARY_OPER(uint64, -, oper);	CHECK_OVERFLOW_uint64_NEG(symbol, oper);  /* handle the uint_v := -0 situation! */
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   630
	DO_UNARY_OPER( int64, -, oper);	CHECK_OVERFLOW_int64_NEG (symbol, oper);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   631
	DO_UNARY_OPER(real64, -, oper);	CHECK_OVERFLOW_real64(symbol);
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   632
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   633
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   634
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   635
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   636
/* unary boolean negation (NOT) */
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   637
static void *handle_not(symbol_c *symbol, symbol_c *oper) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   638
	if (NULL == oper) return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   639
	DO_UNARY_OPER(  bool, !, oper);
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   640
	DO_UNARY_OPER(uint64, ~, oper);
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   641
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   642
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   643
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   644
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   645
static void *handle_or (symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   646
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   647
	DO_BINARY_OPER(  bool, ||, bool  , oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   648
	DO_BINARY_OPER(uint64, | , uint64, oper1, oper2);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   649
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   650
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   651
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   652
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   653
static void *handle_xor(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   654
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   655
	DO_BINARY_OPER(  bool, ^, bool  , oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   656
	DO_BINARY_OPER(uint64, ^, uint64, oper1, oper2);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   657
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   658
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   659
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   660
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   661
static void *handle_and(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   662
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   663
	DO_BINARY_OPER(  bool, &&, bool, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   664
	DO_BINARY_OPER(uint64, & , uint64, oper1, oper2);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   665
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   666
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   667
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   668
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   669
static void *handle_add(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   670
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   671
	DO_BINARY_OPER(uint64, +, uint64, oper1, oper2);   CHECK_OVERFLOW_uint64_SUM(symbol, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   672
	DO_BINARY_OPER( int64, +,  int64, oper1, oper2);   CHECK_OVERFLOW_int64_SUM (symbol, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   673
	DO_BINARY_OPER(real64, +, real64, oper1, oper2);   CHECK_OVERFLOW_real64    (symbol);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   674
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   675
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   676
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   677
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   678
static void *handle_sub(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   679
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   680
	DO_BINARY_OPER(uint64, -, uint64, oper1, oper2);   CHECK_OVERFLOW_uint64_SUB(symbol, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   681
	DO_BINARY_OPER( int64, -,  int64, oper1, oper2);   CHECK_OVERFLOW_int64_SUB (symbol, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   682
	DO_BINARY_OPER(real64, -, real64, oper1, oper2);   CHECK_OVERFLOW_real64    (symbol);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   683
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   684
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   685
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   686
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   687
static void *handle_mul(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   688
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   689
	DO_BINARY_OPER(uint64, *, uint64, oper1, oper2);   CHECK_OVERFLOW_uint64_MUL(symbol, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   690
	DO_BINARY_OPER( int64, *,  int64, oper1, oper2);   CHECK_OVERFLOW_int64_MUL (symbol, oper1, oper2);
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   691
	DO_BINARY_OPER(real64, *, real64, oper1, oper2);   CHECK_OVERFLOW_real64    (symbol);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   692
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   693
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   694
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   695
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   696
static void *handle_div(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   697
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   698
	if (ISZERO_CVALUE(uint64, oper2))  {SET_OVFLOW(uint64, symbol);} else {DO_BINARY_OPER(uint64, /, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_DIV(symbol, oper1, oper2);};
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   699
	if (ISZERO_CVALUE( int64, oper2))  {SET_OVFLOW( int64, symbol);} else {DO_BINARY_OPER( int64, /,  int64, oper1, oper2); CHECK_OVERFLOW_int64_DIV (symbol, oper1, oper2);};
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   700
	if (ISZERO_CVALUE(real64, oper2))  {SET_OVFLOW(real64, symbol);} else {DO_BINARY_OPER(real64, /, real64, oper1, oper2); CHECK_OVERFLOW_real64(symbol);};
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   701
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   702
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   703
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   704
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   705
static void *handle_mod(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   706
	if ((NULL == oper1) || (NULL == oper2)) return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   707
	/* IEC 61131-3 standard says IN1 MOD IN2 must be equivalent to
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   708
	 *  IF (IN2 = 0) THEN OUT:=0 ; ELSE OUT:=IN1 - (IN1/IN2)*IN2 ; END_IF
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   709
	 *
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   710
	 * Note that, when IN1 = INT64_MIN, and IN2 = -1, an overflow occurs in the division,
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   711
	 * so although the MOD operation should be OK, acording to the above definition, we actually have an overflow!!
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   712
	 */
643
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   713
	if (ISZERO_CVALUE(uint64, oper2))  {SET_CVALUE(uint64, symbol, 0);} else {DO_BINARY_OPER(uint64, %, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_MOD(symbol, oper1, oper2);};
1cc0e1ca2aad Fix constant folding: now handles INT_MIN and neg_integer_c correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 640
diff changeset
   714
	if (ISZERO_CVALUE( int64, oper2))  {SET_CVALUE( int64, symbol, 0);} else {DO_BINARY_OPER( int64, %,  int64, oper1, oper2); CHECK_OVERFLOW_int64_MOD (symbol, oper1, oper2);};
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   715
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   716
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   717
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   718
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   719
static void *handle_pow(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) {
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   720
	/* NOTE: If the const_value in symbol->r_exp is within the limits of both int64 and uint64, then we do both operations.
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   721
	 *       That is OK, as the result should be identicial (we do create an unnecessary CVALUE variable, but who cares?).
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   722
	 *       If only one is valid, then that is the oper we will do!
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   723
	 */
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   724
	if (VALID_CVALUE(real64, oper1) && VALID_CVALUE( int64, oper2))
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   725
		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE( int64, oper2)));
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   726
	if (VALID_CVALUE(real64, oper1) && VALID_CVALUE(uint64, oper2))
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   727
		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE(uint64, oper2)));
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   728
	CHECK_OVERFLOW_real64(symbol);
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   729
	return NULL;
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   730
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
   731
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   732
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   733
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   734
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   735
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   736
/***        Helper functions for handling IL instruction lists.      ***/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   737
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   738
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   739
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   740
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   741
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   742
/* If the cvalues of all the prev_il_intructions have the same VALID value, then set the local cvalue to that value, otherwise, set it to NONCONST! */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   743
#define intersect_prev_CVALUE_(dtype, symbol) {                                                                   \
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   744
	symbol->const_value._##dtype = symbol->prev_il_instruction[0]->const_value._##dtype;                      \
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   745
	for (unsigned int i = 1; i < symbol->prev_il_instruction.size(); i++) {                                   \
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   746
		if (!ISEQUAL_CVALUE(dtype, symbol, symbol->prev_il_instruction[i]))                               \
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   747
			{SET_NONCONST(dtype, symbol); break;}                                                     \
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   748
	}                                                                                                         \
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   749
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   750
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   751
static void intersect_prev_cvalues(il_instruction_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   752
	if (symbol->prev_il_instruction.empty())
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   753
		return;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   754
	intersect_prev_CVALUE_(real64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   755
	intersect_prev_CVALUE_(uint64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   756
	intersect_prev_CVALUE_( int64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   757
	intersect_prev_CVALUE_(  bool, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   758
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   759
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   760
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   761
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   762
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   763
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   764
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   765
/***        The constant_folding_c                                   ***/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   766
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   767
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   768
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   769
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   770
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   771
constant_folding_c::constant_folding_c(symbol_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   772
    error_count = 0;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   773
    warning_found = false;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   774
    current_display_error_level = 0;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   775
    il_operand = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   776
    prev_il_instruction = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   777
    
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   778
    /* check whether the platform on which the compiler is being run implements IEC 559 floating point data types. */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   779
    symbol_c null_symbol;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   780
    if (! (std::numeric_limits<real64_t>::is_iec559) )
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   781
        STAGE3_WARNING(&null_symbol, &null_symbol, "The platform running the compiler does not implement IEC 60559 floating point numbers. "
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   782
                                                   "Any error and/or warning messages related to overflow/underflow of the result of operations on REAL/LREAL literals "
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   783
                                                   "(i.e. constant folding) may themselves be erroneous, although are most probably correct."
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   784
                                                   "However, more likely is the possible existance of overflow/underflow errors that are not detected.");
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   785
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   786
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   787
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   788
constant_folding_c::~constant_folding_c(void) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   789
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   790
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   791
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   792
int constant_folding_c::get_error_count() {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   793
	return error_count;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   794
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   795
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   796
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   797
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   798
/*********************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   799
/* B 1.2 - Constants */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   800
/*********************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   801
/******************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   802
/* B 1.2.1 - Numeric Literals */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   803
/******************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   804
void *constant_folding_c::visit(real_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   805
	bool overflow;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   806
	SET_CVALUE(real64, symbol, extract_real_value(symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   807
	if (overflow) SET_OVFLOW(real64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   808
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   809
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   810
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   811
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   812
void *constant_folding_c::visit(integer_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   813
	bool overflow;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   814
	SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   815
	if (overflow) SET_OVFLOW(int64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   816
	SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   817
	if (overflow) SET_OVFLOW(uint64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   818
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   819
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   820
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   821
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   822
void *constant_folding_c::visit(neg_real_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   823
	symbol->exp->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   824
	DO_UNARY_OPER(real64, -, symbol->exp); CHECK_OVERFLOW_real64(symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   825
	if (IS_OVFLOW(real64, symbol->exp)) SET_OVFLOW(real64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   826
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   827
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   828
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   829
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   830
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   831
/* | '-' integer	{$$ = new neg_integer_c($2, locloc(@$));} */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   832
void *constant_folding_c::visit(neg_integer_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   833
	symbol->exp->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   834
	/* Note that due to syntax restrictions, the value of symbol->exp will always be positive. 
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   835
	 * However, the following code does not depend on that restriction.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   836
	 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   837
	/* The remainder of the code (for example, data type checking) considers the neg_integer_c as a leaf of the
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   838
	 * abstract syntax tree, and therefore simply ignores the values of neg_integer_c->exp.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   839
	 * For this reason only, and in only this situation, we must guarantee that any 'overflow' situation in 
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   840
	 * the cvalue of neg_integer_c->exp is also reflected back to this neg_integer_c symbol.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   841
	 * For the rest of the code we do NOT do this, as it would gurantee that a single overflow deep inside
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   842
	 * an expression would imply that the expression itself would also be set to 'overflow' condition.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   843
	 * This in turn would then have the compiler produce a whole load of error messages where they are not wanted!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   844
	 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   845
	DO_UNARY_OPER(uint64, -, symbol->exp); CHECK_OVERFLOW_uint64_NEG(symbol, symbol->exp);  /* handle the uintv := -0 situation */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   846
	if (IS_OVFLOW(uint64, symbol->exp)) SET_OVFLOW(uint64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   847
	DO_UNARY_OPER( int64, -, symbol->exp); CHECK_OVERFLOW_int64_NEG (symbol, symbol->exp);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   848
	if (IS_OVFLOW( int64, symbol->exp)) SET_OVFLOW( int64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   849
	/* NOTE 1: INT64_MIN = -(INT64_MAX + 1)   ---> assuming two's complement representation!!!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   850
	 * NOTE 2: if the user happens to want INT_MIN, that value will first be parsed as a positive integer, before being negated here.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   851
	 * However, the positive value cannot be stored inside an int64! So, in this case, we will get the value from the uint64 cvalue.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   852
	 *
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   853
	 * This same situation is usually considered an overflow (check handle_neg() function). However, here we have a special
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   854
	 * situation. If we do not allow this, then the user would never the able to use the following code:
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   855
	 *  VAR v : LINT; END_VAR
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   856
	 *    v := -9223372036854775809 ; (* - |INT64_MIN| == INT64_MIN *)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   857
	 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   858
	// if (INT64_MIN == -INT64_MAX - 1) // We do not really need to check that the platform uses two's complement
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   859
	if (VALID_CVALUE(uint64, symbol->exp) && (GET_CVALUE(uint64, symbol->exp) == (uint64_t)INT64_MAX+1)) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   860
		SET_CVALUE(int64, symbol, INT64_MIN);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   861
	}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   862
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   863
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   864
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   865
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   866
void *constant_folding_c::visit(binary_integer_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   867
	bool overflow;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   868
	SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   869
	if (overflow) SET_OVFLOW(int64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   870
	SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   871
	if (overflow) SET_OVFLOW(uint64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   872
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   873
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   874
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   875
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   876
void *constant_folding_c::visit(octal_integer_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   877
	bool overflow;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   878
	SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   879
	if (overflow) SET_OVFLOW(int64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   880
	SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   881
	if (overflow) SET_OVFLOW(uint64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   882
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   883
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   884
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   885
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   886
void *constant_folding_c::visit(hex_integer_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   887
	bool overflow;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   888
	SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   889
	if (overflow) SET_OVFLOW(int64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   890
	SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   891
	if (overflow) SET_OVFLOW(uint64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   892
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   893
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   894
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   895
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   896
/*
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   897
integer_literal:
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   898
  integer_type_name '#' signed_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   899
| integer_type_name '#' binary_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   900
| integer_type_name '#' octal_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   901
| integer_type_name '#' hex_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   902
*/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   903
// SYM_REF2(integer_literal_c, type, value)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   904
void *constant_folding_c::visit(integer_literal_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   905
	symbol->value->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   906
	DO_UNARY_OPER( int64, /* none */, symbol->value);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   907
	DO_UNARY_OPER(uint64, /* none */, symbol->value);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   908
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   909
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   910
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   911
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   912
void *constant_folding_c::visit(real_literal_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   913
	symbol->value->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   914
	DO_UNARY_OPER(real64, /* none */, symbol->value);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   915
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   916
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   917
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   918
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   919
void *constant_folding_c::visit(bit_string_literal_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   920
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   921
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   922
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   923
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   924
void *constant_folding_c::visit(boolean_literal_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   925
	symbol->value->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   926
	DO_UNARY_OPER(bool, /* none */, symbol->value);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   927
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   928
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   929
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   930
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   931
void *constant_folding_c::visit(boolean_true_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   932
	SET_CVALUE(bool, symbol, true);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   933
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   934
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   935
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   936
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   937
void *constant_folding_c::visit(boolean_false_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   938
	SET_CVALUE(bool, symbol, false);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   939
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   940
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   941
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   942
/************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   943
/* B 1.2.3.1 - Duration */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   944
/********* **************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   945
void *constant_folding_c::visit(fixed_point_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   946
	bool overflow;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   947
	SET_CVALUE(real64, symbol, extract_real_value(symbol, &overflow));
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   948
	if (overflow) SET_OVFLOW(real64, symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   949
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   950
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   951
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   952
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   953
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   954
/****************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   955
/* B.2 - Language IL (Instruction List) */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   956
/****************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   957
/***********************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   958
/* B 2.1 Instructions and Operands */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   959
/***********************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   960
/* Not needed, since we inherit from iterator_visitor_c */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   961
/*| instruction_list il_instruction */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   962
// SYM_LIST(instruction_list_c)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   963
// void *constant_folding_c::visit(instruction_list_c *symbol) {}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   964
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   965
/* | label ':' [il_incomplete_instruction] eol_list */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   966
// SYM_REF2(il_instruction_c, label, il_instruction)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   967
// void *visit(instruction_list_c *symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   968
void *constant_folding_c::visit(il_instruction_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   969
	if (NULL == symbol->il_instruction) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   970
		/* This empty/null il_instruction does not change the value of the current/default IL variable.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   971
		 * So it inherits the candidate_datatypes from it's previous IL instructions!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   972
		 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   973
		intersect_prev_cvalues(symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   974
	} else {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   975
		il_instruction_c fake_prev_il_instruction = *symbol;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   976
		intersect_prev_cvalues(&fake_prev_il_instruction);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   977
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   978
		if (symbol->prev_il_instruction.size() == 0)  prev_il_instruction = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   979
		else                                          prev_il_instruction = &fake_prev_il_instruction;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   980
		symbol->il_instruction->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   981
		prev_il_instruction = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   982
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   983
		/* This object has (inherits) the same cvalues as the il_instruction */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   984
		symbol->const_value = symbol->il_instruction->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   985
	}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   986
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   987
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   988
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   989
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   990
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   991
void *constant_folding_c::visit(il_simple_operation_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   992
	/* determine the cvalue of the operand */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   993
	if (NULL != symbol->il_operand) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   994
		symbol->il_operand->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   995
	}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   996
	/* determine the cvalue resulting from executing the il_operator... */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   997
	il_operand = symbol->il_operand;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   998
	symbol->il_simple_operator->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
   999
	il_operand = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1000
	/* This object has (inherits) the same cvalues as the il_instruction */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1001
	symbol->const_value = symbol->il_simple_operator->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1002
	return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1003
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1004
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1005
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1006
/* TODO: handle function invocations... */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1007
/* | function_name [il_operand_list] */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1008
/* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1009
// SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1010
// void *constant_folding_c::visit(il_function_call_c *symbol) {}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1011
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1012
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1013
/* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1014
// SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1015
void *constant_folding_c::visit(il_expression_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1016
  symbol_c *prev_il_instruction_backup = prev_il_instruction;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1017
  
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1018
  /* Stage2 will insert an artificial (and equivalent) LD <il_operand> to the simple_instr_list if necessary. We can therefore ignore the 'il_operand' entry! */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1019
  // if (NULL != symbol->il_operand)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1020
  //   symbol->il_operand->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1021
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1022
  if(symbol->simple_instr_list != NULL)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1023
    symbol->simple_instr_list->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1024
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1025
  /* Now do the operation,  */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1026
  il_operand = symbol->simple_instr_list;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1027
  prev_il_instruction = prev_il_instruction_backup;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1028
  symbol->il_expr_operator->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1029
  il_operand = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1030
  
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1031
  /* This object has (inherits) the same cvalues as the il_instruction */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1032
  symbol->const_value = symbol->il_expr_operator->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1033
  
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1034
  /* Since stage2 will insert an artificial (and equivalent) LD <il_operand> to the simple_instr_list when an 'il_operand' exists, we know
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1035
   * that if (symbol->il_operand != NULL), then the first IL instruction in the simple_instr_list will be the equivalent and artificial
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1036
   * 'LD <il_operand>' IL instruction.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1037
   * Just to be cosistent, we will copy the constant info back into the il_operand, even though this should not be necessary!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1038
   */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1039
  if ((NULL != symbol->il_operand) && ((NULL == symbol->simple_instr_list) || (0 == ((list_c *)symbol->simple_instr_list)->n))) ERROR; // stage2 is not behaving as we expect it to!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1040
  if  (NULL != symbol->il_operand)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1041
    symbol->il_operand->const_value = ((list_c *)symbol->simple_instr_list)->elements[0]->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1042
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1043
  return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1044
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1045
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1046
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1047
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1048
void *constant_folding_c::visit(il_jump_operation_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1049
  /* recursive call to fill const values... */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1050
  il_operand = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1051
  symbol->il_jump_operator->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1052
  il_operand = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1053
  /* This object has (inherits) the same cvalues as the il_jump_operator */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1054
  symbol->const_value = symbol->il_jump_operator->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1055
  return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1056
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1057
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1058
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1059
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1060
/* FB calls leave the value in the accumulator unchanged */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1061
/*   il_call_operator prev_declared_fb_name
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1062
 * | il_call_operator prev_declared_fb_name '(' ')'
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1063
 * | il_call_operator prev_declared_fb_name '(' eol_list ')'
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1064
 * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1065
 * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1066
 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1067
/* 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 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1068
// SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1069
void *constant_folding_c::visit(il_fb_call_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1070
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1071
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1072
/* TODO: handle function invocations... */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1073
/* | function_name '(' eol_list [il_param_list] ')' */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1074
/* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1075
// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1076
// void *constant_folding_c::visit(il_formal_funct_call_c *symbol) {return NULL;}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1077
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1078
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1079
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1080
/* Not needed, since we inherit from iterator_visitor_c */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1081
//  void *constant_folding_c::visit(il_operand_list_c *symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1082
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1083
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1084
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1085
/* | simple_instr_list il_simple_instruction */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1086
/* This object is referenced by il_expression_c objects */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1087
void *constant_folding_c::visit(simple_instr_list_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1088
  if (symbol->n <= 0)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1089
    return NULL;  /* List is empty! Nothing to do. */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1090
    
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1091
  for(int i = 0; i < symbol->n; i++)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1092
    symbol->elements[i]->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1093
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1094
  /* This object has (inherits) the same cvalues as the il_jump_operator */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1095
  symbol->const_value = symbol->elements[symbol->n-1]->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1096
  return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1097
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1098
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1099
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1100
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1101
// SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1102
void *constant_folding_c::visit(il_simple_instruction_c *symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1103
  if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1104
  if (symbol->prev_il_instruction.size() == 0)  prev_il_instruction = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1105
  else                                          prev_il_instruction = symbol->prev_il_instruction[0];
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1106
  symbol->il_simple_instruction->accept(*this);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1107
  prev_il_instruction = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1108
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1109
  /* This object has (inherits) the same cvalues as the il_jump_operator */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1110
  symbol->const_value = symbol->il_simple_instruction->const_value;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1111
  return NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1112
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1113
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1114
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1115
/*
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1116
    void *visit(il_param_list_c *symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1117
    void *visit(il_param_assignment_c *symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1118
    void *visit(il_param_out_assignment_c *symbol);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1119
*/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1120
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1121
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1122
/*******************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1123
/* B 2.2 Operators */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1124
/*******************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1125
void *constant_folding_c::visit(   LD_operator_c *symbol) {return handle_move(symbol, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1126
void *constant_folding_c::visit(  LDN_operator_c *symbol) {return handle_not (symbol, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1127
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1128
/* NOTE: we are implementing a constant folding algorithm, not a constant propagation algorithm.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1129
 *       For the constant propagation algorithm, the correct implementation of ST(N)_operator_c would be...
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1130
 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1131
//void *constant_folding_c::visit(   ST_operator_c *symbol) {return handle_move(il_operand, symbol);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1132
//void *constant_folding_c::visit(  STN_operator_c *symbol) {return handle_not (il_operand, symbol);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1133
void *constant_folding_c::visit(   ST_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1134
void *constant_folding_c::visit(  STN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1135
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1136
/* NOTE: the standard allows syntax in which the NOT operator is followed by an optional <il_operand>
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1137
 *              NOT [<il_operand>]
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1138
 *       However, it does not define the semantic of the NOT operation when the <il_operand> is specified.
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1139
 *       We therefore consider it an error if an il_operand is specified! This error will be caught elsewhere!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1140
 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1141
void *constant_folding_c::visit(  NOT_operator_c *symbol) {return handle_not(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1142
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1143
/* NOTE: Since we are only implementing a constant folding algorithm, and not a constant propagation algorithm,
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1144
 *       the following IL instructions do not change/set the value of the il_operand!
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1145
 */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1146
void *constant_folding_c::visit(    S_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1147
void *constant_folding_c::visit(    R_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1148
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1149
/* FB calls leave the value in the accumulator unchanged */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1150
void *constant_folding_c::visit(   S1_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1151
void *constant_folding_c::visit(   R1_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1152
void *constant_folding_c::visit(  CLK_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1153
void *constant_folding_c::visit(   CU_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1154
void *constant_folding_c::visit(   CD_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1155
void *constant_folding_c::visit(   PV_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1156
void *constant_folding_c::visit(   IN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1157
void *constant_folding_c::visit(   PT_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1158
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1159
void *constant_folding_c::visit(  AND_operator_c *symbol) {return handle_and (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1160
void *constant_folding_c::visit(   OR_operator_c *symbol) {return handle_or  (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1161
void *constant_folding_c::visit(  XOR_operator_c *symbol) {return handle_xor (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1162
void *constant_folding_c::visit( ANDN_operator_c *symbol) {       handle_and (symbol, prev_il_instruction, il_operand); return handle_not(symbol, symbol);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1163
void *constant_folding_c::visit(  ORN_operator_c *symbol) {       handle_or  (symbol, prev_il_instruction, il_operand); return handle_not(symbol, symbol);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1164
void *constant_folding_c::visit( XORN_operator_c *symbol) {       handle_xor (symbol, prev_il_instruction, il_operand); return handle_not(symbol, symbol);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1165
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1166
void *constant_folding_c::visit(  ADD_operator_c *symbol) {return handle_add (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1167
void *constant_folding_c::visit(  SUB_operator_c *symbol) {return handle_sub (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1168
void *constant_folding_c::visit(  MUL_operator_c *symbol) {return handle_mul (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1169
void *constant_folding_c::visit(  DIV_operator_c *symbol) {return handle_div (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1170
void *constant_folding_c::visit(  MOD_operator_c *symbol) {return handle_mod (symbol, prev_il_instruction, il_operand);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1171
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1172
void *constant_folding_c::visit(   GT_operator_c *symbol) {       handle_cmp (symbol, prev_il_instruction, il_operand, > );}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1173
void *constant_folding_c::visit(   GE_operator_c *symbol) {       handle_cmp (symbol, prev_il_instruction, il_operand, >=);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1174
void *constant_folding_c::visit(   EQ_operator_c *symbol) {       handle_cmp (symbol, prev_il_instruction, il_operand, ==);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1175
void *constant_folding_c::visit(   LT_operator_c *symbol) {       handle_cmp (symbol, prev_il_instruction, il_operand, < );}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1176
void *constant_folding_c::visit(   LE_operator_c *symbol) {       handle_cmp (symbol, prev_il_instruction, il_operand, <=);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1177
void *constant_folding_c::visit(   NE_operator_c *symbol) {       handle_cmp (symbol, prev_il_instruction, il_operand, !=);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1178
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1179
void *constant_folding_c::visit(  CAL_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1180
void *constant_folding_c::visit(  RET_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1181
void *constant_folding_c::visit(  JMP_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1182
void *constant_folding_c::visit( CALC_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1183
void *constant_folding_c::visit(CALCN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1184
void *constant_folding_c::visit( RETC_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1185
void *constant_folding_c::visit(RETCN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1186
void *constant_folding_c::visit( JMPC_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1187
void *constant_folding_c::visit(JMPCN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1188
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1189
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1190
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1191
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1192
/***************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1193
/* B.3 - Language ST (Structured Text) */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1194
/***************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1195
/***********************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1196
/* B 3.1 - Expressions */
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1197
/***********************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1198
void *constant_folding_c::visit(    or_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_or (symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1199
void *constant_folding_c::visit(   xor_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_xor(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1200
void *constant_folding_c::visit(   and_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_and(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1201
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1202
void *constant_folding_c::visit(   equ_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this);        handle_cmp (symbol, symbol->l_exp, symbol->r_exp, ==);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1203
void *constant_folding_c::visit(notequ_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this);        handle_cmp (symbol, symbol->l_exp, symbol->r_exp, !=);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1204
void *constant_folding_c::visit(    lt_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this);        handle_cmp (symbol, symbol->l_exp, symbol->r_exp, < );}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1205
void *constant_folding_c::visit(    gt_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this);        handle_cmp (symbol, symbol->l_exp, symbol->r_exp, > );}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1206
void *constant_folding_c::visit(    le_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this);        handle_cmp (symbol, symbol->l_exp, symbol->r_exp, <=);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1207
void *constant_folding_c::visit(    ge_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this);        handle_cmp (symbol, symbol->l_exp, symbol->r_exp, >=);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1208
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1209
void *constant_folding_c::visit(   add_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_add(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1210
void *constant_folding_c::visit(   sub_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_sub(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1211
void *constant_folding_c::visit(   mul_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_mul(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1212
void *constant_folding_c::visit(   div_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_div(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1213
void *constant_folding_c::visit(   mod_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_mod(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1214
void *constant_folding_c::visit( power_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_pow(symbol, symbol->l_exp, symbol->r_exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1215
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1216
void *constant_folding_c::visit(   neg_expression_c *symbol) {symbol->  exp->accept(*this); return handle_neg(symbol, symbol->exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1217
void *constant_folding_c::visit(   not_expression_c *symbol) {symbol->  exp->accept(*this); return handle_not(symbol, symbol->exp);}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1218
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1219
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1220
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1221
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1222
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1223
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1224
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1225
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1226
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1227
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1228
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1229
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1230
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1231
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1232
/***        The constant_propagation_c                               ***/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1233
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1234
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1235
/***********************************************************************/
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1236
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1237
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1238
constant_propagation_c::constant_propagation_c(symbol_c *symbol)
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1239
  : constant_folding_c(symbol) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1240
    current_resource = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1241
    current_configuration = NULL;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1242
    fixed_init_value_ = false;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1243
    function_pou_ = false;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1244
}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1245
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1246
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1247
constant_propagation_c::~constant_propagation_c(void) {}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1248
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1249
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1250
static constant_propagation_c::map_values_t inner_left_join_values(constant_propagation_c::map_values_t m1, constant_propagation_c::map_values_t m2) {
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1251
	constant_propagation_c::map_values_t::const_iterator itr;
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1252
	constant_propagation_c::map_values_t ret;
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1253
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1254
	itr = m1.begin();
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1255
	for ( ; itr != m1.end(); ++itr) {
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1256
		std::string name = itr->first;
965
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
  1257
		const_value_c value;
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1258
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1259
		if (m2.count(name) > 0) {
965
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
  1260
			const_value_c c1 = itr->second;
c9eeb67ba939 Small code re-organization of how const values are stored in symbol_c (can now be accessed through member functions, instead of macros)
mjsousa
parents: 964
diff changeset
  1261
			const_value_c c2 = m2[name];
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1262
			COMPUTE_MEET_SEMILATTICE (real64, c1, c2, value);
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1263
			COMPUTE_MEET_SEMILATTICE (uint64, c1, c2, value);
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1264
			COMPUTE_MEET_SEMILATTICE ( int64, c1, c2, value);
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1265
			COMPUTE_MEET_SEMILATTICE (  bool, c1, c2, value);
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1266
		} else
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1267
			value = m1[name];
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1268
		ret[name] = value;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1269
	}
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1270
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1271
	return ret;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1272
}
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1273
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1274
/***************************/
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1275
/* B 0 - Programming Model */
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1276
/***************************/
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1277
/* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1278
// SYM_LIST(library_c, enumvalue_symtable_t enumvalue_symtable;)
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1279
  /* The constant propagation algorithm propagates the constant values to all the locations
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1280
   * in the code where expressions can be determined to have a fixed value.
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1281
   *  e.g.:   var1 := 99;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1282
   *          var2[var1] := 42; <-- with constant propagation, we know we are accessing var2[99] here!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1283
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1284
   * An important question in constant propagation is whether we should use the values of 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1285
   * VAR_GLOBAL CONSTANT variables as a constant. The problem here is that the 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1286
   * constant value of each global variable can only be declared in a configuration,
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1287
   * but a POU may eventually be used from different configurations.
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1288
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1289
   * For example:
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1290
   *     CONFIGURATION conf1
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1291
   *       VAR_GLOBAL CONSTANT global_const : INT := 42; END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1292
   *       PROGRAM prog1 WITH TaskX : Prog1_t;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1293
   *     END_CONFIGURATION
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1294
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1295
   *     CONFIGURATION conf2
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1296
   *       VAR_GLOBAL CONSTANT global_const : INT := 18; END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1297
   *       PROGRAM prog1 WITH TaskX : Prog1_t;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1298
   *     END_CONFIGURATION
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1299
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1300
   *     PROGRAM Prog1_t
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1301
   *      VAR_EXTERN COSNTANT  global_const : INT; END_VAR  <--- NOTE: 61131-3 syntax does not allow const value to be set here!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1302
   *      VAR .... END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1303
   *       array_var[global_const] := 0;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1304
   *     END_PROGRAM
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1305
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1306
   *  Considering the above code, where Prog1_t is instanciated in both conf1 and conf2,
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1307
   *  and therefore where the 'constant' var_global may actually take two possible values
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1308
   *  (42 and 18), it would be incorrect to consider the var_global variable a constant in
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1309
   *  the constant propagation algorithm.
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1310
   *  This means that when doing constant propagation of the Prog1_t POU, we should consider
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1311
   *  all global variables (including the 'constant') as non-constant - this actually makes
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1312
   *  the algorithm much easier!).
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1313
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1314
   *  However, matiec has implemented an extension where we allow arrays whose size may be 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1315
   *  defined using symbolic variables, whose value can be determined at compile time 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1316
   *  (typically VAR CONSTANT variables). To really become usefull, this extension must allow
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1317
   *  the same var_global constant to be used in declaring arrays in both configuration as
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1318
   *  well as in some other POUs.
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1319
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1320
   *   e.g.:
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1321
   *     CONFIGURATION conf2
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1322
   *       VAR_GLOBAL CONSTANT global_const : INT := 18; END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1323
   *       VAR_GLOBAL          global_array : ARRAY [1..global_const] OF INT; END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1324
   *       PROGRAM prog1 WITH TaskX : Prog1_t;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1325
   *     END_CONFIGURATION
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1326
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1327
   *     PROGRAM Prog1_t
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1328
   *      VAR_EXTERN COSNTANT  global_const : INT; END_VAR  <--- NOTE: 61131-3 syntax does not allow const value to be set here!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1329
   *      VAR_EXTERN           global_array : ARRAY [1..global_const] OF INT; END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1330
   *      VAR .... END_VAR
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1331
   *       global_array[global_const] := 0;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1332
   *     END_PROGRAM
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1333
   * 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1334
   * The above requirement means that we MUST therefore do the propagation of constant values
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1335
   * from a var_global constant to the corresponding var_external in the POUs. 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1336
   * This implies 3 things:
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1337
   *   1) We must detect when two configurations use the same POU and set distinct values
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1338
   *       to the same var_global - we emit a warning/error (which should we emit?)
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1339
   *   2) Even if each POU is used by only one configuration, we must warn the user that
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1340
   *       the generated code may not be safely turned into a library to be later linked
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1341
   *       to other configurations
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1342
   *   3) To do the propagation of the var_global const value to the var_external,
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1343
   *       we must first analyse all the configurations in the library. 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1344
   *       - When analysing a configuration, we store all the constant values in the 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1345
   *         global_values[] map, and call from within the configuration context all the POUs
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1346
   *         instantiated inside this configuration (basically, we do the constant propagation 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1347
   *         of each POU with the global_values[] map preloaded with all the constant values).
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1348
   *         This means that we may evetually do constant folding of the same POU type multiple
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1349
   *         times (if it is instantiated multiple times in the same configuration, or once
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1350
   *         in several configurations). This should not be a problem because the constant
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1351
   *         propagation algorithm is idem-potent (assuming the same constant values in the 
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1352
   *         beginning), and we can use these multiple calls to the same POU to detect if
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1353
   *         the situation mentioned in (1) is ocurring.
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1354
   *       - After analysing all the configurations, we analyse all the other POUs that have
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1355
   *         not yet been called (because they are not instantiated directly from within
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1356
   *         any configuration - e.g. functions, and most FBs!).
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1357
   *       It is for this reason (3) why we have the two loops on the following code!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1358
   */
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1359
void *constant_propagation_c::visit(library_c *symbol) {
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1360
  int i;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1361
  
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1362
  for (i = 0; i < symbol->n; i++) {
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1363
    // first analyse the configurations
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1364
    if (NULL != dynamic_cast<configuration_declaration_c *>(symbol->elements[i]))
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1365
      symbol->elements[i]->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1366
  }
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1367
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1368
  for (i = 0; i < symbol->n; i++) {
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1369
    /* NOTE: we will be re-visiting all the POUs that were already called indirectly through the
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1370
     *       visit(program_configuration_c) of vist(fb_task_c) visitors during the previous for
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1371
     *       loop. However, this is OK as the only difference would be how the VAR_EXTERN are handled,
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1372
     *       and that is taken care of in the visit(external_declaration_c) visitor!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1373
     */
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1374
    if (NULL == dynamic_cast<configuration_declaration_c *>(symbol->elements[i]))
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1375
      symbol->elements[i]->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1376
  }
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1377
  
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1378
  return NULL;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1379
}
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1380
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
  1381
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1382
/*********************/
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1383
/* B 1.4 - Variables */
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1384
/*********************/
981
aad6aa35ce60 Disable constant propagation again (still not working correctly)
mjsousa
parents: 974
diff changeset
  1385
#if DO_CONSTANT_PROPAGATION__
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1386
void *constant_propagation_c::visit(symbolic_variable_c *symbol) {
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1387
	std::string varName = get_var_name_c::get_name(symbol->var_name)->value;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1388
	if (values.count(varName) > 0) 
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1389
		symbol->const_value = values[varName];
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1390
	return NULL;
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1391
}
981
aad6aa35ce60 Disable constant propagation again (still not working correctly)
mjsousa
parents: 974
diff changeset
  1392
#endif  // DO_CONSTANT_PROPAGATION__
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1393
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1394
void *constant_propagation_c::visit(symbolic_constant_c *symbol) {
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1395
	std::string varName = get_var_name_c::get_name(symbol->var_name)->value;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1396
	if (values.count(varName) > 0) 
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1397
		symbol->const_value = values[varName];
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1398
	return NULL;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1399
}
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1400
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1401
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1402
/******************************************/
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1403
/* B 1.4.3 - Declaration & Initialisation */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1404
/******************************************/
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1405
  
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1406
void *constant_propagation_c::handle_var_decl(symbol_c *var_list, bool fixed_init_value) {
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1407
  fixed_init_value_ = fixed_init_value;
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1408
  var_list->accept(*this); 
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1409
  fixed_init_value_ = false; 
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1410
  return NULL;
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1411
}
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1412
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1413
void *constant_propagation_c::handle_var_list_decl(symbol_c *var_list, symbol_c *type_decl) {
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1414
  type_decl->accept(*this);  // Do constant folding of the initial value, and literals in subranges! (we will probably be doing this multiple times for the same init value, but this is safe as the cvalue is idem-potent)
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1415
  symbol_c *init_value = type_initial_value_c::get(type_decl);  
970
0ede7ca157e2 Remove debugging code left in by mistake.
mjsousa
parents: 969
diff changeset
  1416
  if (NULL == init_value)   {return NULL;} // this is probably a FB datatype, for which no initial value exists! Do nothing and return.
968
649e03abbfc1 Do constant folding of default initial values of datatypes.
mjsousa
parents: 967
diff changeset
  1417
  init_value->accept(*this); // necessary when handling default initial values, that were not constant folded in the call type_decl->accept(*this)
649e03abbfc1 Do constant folding of default initial values of datatypes.
mjsousa
parents: 967
diff changeset
  1418
  
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1419
  list_c *list = dynamic_cast<list_c *>(var_list);
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1420
  if (NULL == list) ERROR;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1421
  for (int i = 0; i < list->n; i++) {
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1422
    token_c *var_name = dynamic_cast<token_c *>(list->elements[i]);
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1423
    if (NULL == var_name) {
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1424
      if (NULL != dynamic_cast<extensible_input_parameter_c *>(list->elements[i]))
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1425
        continue; // this is an extensible standard function. Ignore this variable, and continue!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1426
      // debug_c::print(list->elements[i]);
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1427
      ERROR;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1428
    }
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1429
    list->elements[i]->const_value = init_value->const_value;
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1430
    if (fixed_init_value_) {
974
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
  1431
      values[var_name->value] = init_value->const_value;
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1432
    }
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1433
  }
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1434
  return NULL;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1435
}
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1436
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1437
//SYM_REF0(constant_option_c)     // Not needed!
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1438
//SYM_REF0(retain_option_c)       // Not needed!
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1439
//SYM_REF0(non_retain_option_c)   // Not needed!
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1440
bool constant_propagation_c::is_constant(symbol_c *option) {return (NULL != dynamic_cast<constant_option_c *>(option));}
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1441
bool constant_propagation_c::is_retain  (symbol_c *option) {return (NULL != dynamic_cast<  retain_option_c *>(option));}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1442
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1443
/* | var1_list ',' variable_name */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1444
//SYM_LIST(var1_list_c)           // Not needed!
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1445
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1446
/* spec_init is one of the following...
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1447
 *    simple_spec_init_c *
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1448
 *    subrange_spec_init_c *
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1449
 *    enumerated_spec_init_c *
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1450
 */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1451
// SYM_REF2(var1_init_decl_c, var1_list, spec_init)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1452
void *constant_propagation_c::visit(var1_init_decl_c *symbol) {return handle_var_list_decl(symbol->var1_list, symbol->spec_init);}
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1453
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1454
/* | [var1_list ','] variable_name integer '..' */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1455
/* NOTE: This is an extension to the standard!!! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1456
//SYM_REF2(extensible_input_parameter_c, var_name, first_index)          // Not needed!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1457
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1458
/* var1_list ':' array_spec_init */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1459
//SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)            // We do not yet handle arrays!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1460
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1461
/*  var1_list ':' initialized_structure */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1462
//SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure) // We do not yet handle structures!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1463
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1464
/* fb_name_list ':' function_block_type_name ASSIGN structure_initialization */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1465
//SYM_REF2(fb_name_decl_c, fb_name_list, fb_spec_init)                   // We do not yet handle FBs!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1466
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1467
/* fb_name_list ',' fb_name */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1468
//SYM_LIST(fb_name_list_c)                                               // Not needed!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1469
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1470
/* VAR_INPUT [option] input_declaration_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1471
/* option -> the RETAIN/NON_RETAIN/<NULL> directive... */
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1472
//SYM_REF3(input_declarations_c, option, input_declaration_list, method) // Not needed since we inherit from iterator_visitor_c!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1473
// NOTE: Input variables can take any initial value, so we can not set the const_value annotation => we set fixed_init_value to false !!!
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1474
//       We must still visit it iteratively, to set the const_value of all literals in the type declarations.
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1475
void *constant_propagation_c::visit(input_declarations_c *symbol) {return handle_var_decl(symbol->input_declaration_list, false);}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1476
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1477
/* helper symbol for input_declarations */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1478
//SYM_LIST(input_declaration_list_c)                                     // Not needed!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1479
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1480
/* VAR_OUTPUT [RETAIN | NON_RETAIN] var_init_decl_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1481
/* option -> may be NULL ! */
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1482
//SYM_REF3(output_declarations_c, option, var_init_decl_list, method) 
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1483
void *constant_propagation_c::visit(output_declarations_c *symbol) {return handle_var_decl(symbol->var_init_decl_list, !is_retain(symbol->option) && function_pou_);}
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1484
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1485
/*  VAR_IN_OUT var_declaration_list END_VAR */
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1486
//SYM_REF1(input_output_declarations_c, var_declaration_list)
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1487
// NOTE: Input variables can take any initial value, so we can not set the const_value annotation => we set fixed_init_value to false !!!
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1488
//       We must still visit it iteratively, to set the const_value of all literals in the type declarations.
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1489
void *constant_propagation_c::visit(input_output_declarations_c *symbol) {return handle_var_decl(symbol->var_declaration_list, false);}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1490
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1491
/* helper symbol for input_output_declarations */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1492
/* var_declaration_list var_declaration ';' */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1493
//SYM_LIST(var_declaration_list_c)                                       // Not needed since we inherit from iterator_visitor_c!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1494
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1495
/*  var1_list ':' array_specification */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1496
//SYM_REF2(array_var_declaration_c, var1_list, array_specification)      // We do not yet handle arrays! 
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1497
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1498
/*  var1_list ':' structure_type_name */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1499
//SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name) // We do not yet handle structures!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1500
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1501
/* VAR [CONSTANT] var_init_decl_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1502
/* option -> may be NULL ! */
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1503
//SYM_REF2(var_declarations_c, option, var_init_decl_list)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1504
void *constant_propagation_c::visit(var_declarations_c *symbol) {return handle_var_decl(symbol->var_init_decl_list, false);}
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1505
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1506
/*  VAR RETAIN var_init_decl_list END_VAR */
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1507
//SYM_REF1(retentive_var_declarations_c, var_init_decl_list)             // Not needed since we inherit from iterator_visitor_c!
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1508
// NOTE: Retentive variables can take any initial value, so we can not set the const_value annotation => we set fixed_init_value to false !!!
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1509
//       We must still visit it iteratively, to set the const_value of all literals in the type declarations.
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1510
void *constant_propagation_c::visit(retentive_var_declarations_c *symbol) {return handle_var_decl(symbol->var_init_decl_list, false);}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1511
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1512
#if 0  
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1513
// TODO
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1514
/*  VAR [CONSTANT|RETAIN|NON_RETAIN] located_var_decl_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1515
/* option -> may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1516
SYM_REF2(located_var_declarations_c, option, located_var_decl_list)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1517
/* helper symbol for located_var_declarations */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1518
/* located_var_decl_list located_var_decl ';' */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1519
SYM_LIST(located_var_decl_list_c)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1520
/*  [variable_name] location ':' located_var_spec_init */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1521
/* variable_name -> may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1522
SYM_REF3(located_var_decl_c, variable_name, location, located_var_spec_init)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1523
#endif
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1524
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1525
/*| VAR_EXTERNAL [CONSTANT] external_declaration_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1526
/* option -> may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1527
// SYM_REF2(external_var_declarations_c, option, external_declaration_list)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1528
void *constant_propagation_c::visit(external_var_declarations_c *symbol) {return handle_var_decl(symbol->external_declaration_list, is_constant(symbol->option));}
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1529
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1530
/* helper symbol for external_var_declarations */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1531
/*| external_declaration_list external_declaration';' */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1532
// SYM_LIST(external_declaration_list_c)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1533
// void *constant_propagation_c::visit(external_declaration_list_c *symbol) {} // Not needed: we inherit from iterator_c
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1534
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1535
/*  global_var_name ':' (simple_specification|subrange_specification|enumerated_specification|array_specification|prev_declared_structure_type_name|function_block_type_name */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1536
//SYM_REF2(external_declaration_c, global_var_name, specification)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1537
void *constant_propagation_c::visit(external_declaration_c *symbol) {
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1538
  // The syntax does not allow VAR_EXTERN to be initialized. We must get the initial value from the corresponding VAR_GLOBAL declaration
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1539
  /* However, we only do this is if the visit() method for the Program/FB in which this VAR_EXTERN is found was called from the visit(configurtion/resource) visitor!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1540
   * When we are called from the visit(library_c) visitor, we do not have the required information to do this!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1541
   */ 
983
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1542
  if (NULL != current_configuration) {
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1543
    /* before copying the constant value from the VAR_GLOBAL declaration (which is stored in the var_global_values[] map)
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1544
     * we check to see if the VAR_EXTERN constant values has already been set by a previous call to constant fold the POU
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1545
     * in which this VAR_EXTERN is found we simply check to see if any const value of this VAR_EXTERN variable has already
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1546
     * been set previously!
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1547
     */
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1548
    if (   !IS_UNDEFINED( int64, symbol->specification) || !IS_UNDEFINED(uint64, symbol->specification) 
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1549
        || !IS_UNDEFINED(real64, symbol->specification) || !IS_UNDEFINED(  bool, symbol->specification)) {
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1550
      /* The const_value for this VAR_EXTERN has already been previously set. Lets check to see if it is the exact
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1551
       * same const value - if not we produce an error message!
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1552
       * 
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1553
       * NOTE: comparison is inverted with '!'
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1554
       */
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1555
      if (! (symbol->specification->const_value == var_global_values[get_var_name_c::get_name(symbol->global_var_name)->value]))
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1556
        STAGE3_ERROR(0, symbol, symbol, "The initial value of this external variable is ambiguous (the Program/FB in which "
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1557
                                        "this external variable is declared has been used to instantiate a Program/FB in more "
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1558
                                        "than one configuration and/or resource - and each resource sets the corresponding global "
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1559
                                        "variable to a distinct constant initial value).");
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1560
    }
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1561
    
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1562
    // only now do we copy the const value from the var_global to the var_external.
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1563
    symbol->specification->const_value = var_global_values[get_var_name_c::get_name(symbol->global_var_name)->value];
983
ead554e12195 Add check whether the same constant var_external variable is initialised with two (or more) different constant values.
mjsousa
parents: 982
diff changeset
  1564
  }
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1565
  
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1566
  symbol->global_var_name->const_value = symbol->specification->const_value;
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1567
  if (fixed_init_value_) {
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1568
//  values[symbol->global_var_name->get_value()] = symbol->specification->const_value;
974
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
  1569
    values[get_var_name_c::get_name(symbol->global_var_name)->value] = symbol->specification->const_value;
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1570
  }
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1571
  // If the datatype specification is a subrange or array, do constant folding of all the literals in that type declaration... (ex: literals in array subrange limits)
964
5f4dfe6670da Fix bugs introduced in previous commit (once again do constant folding of all literals in type declarations)
mjsousa
parents: 963
diff changeset
  1572
  symbol->specification->accept(*this);  // should never get to change the const_value of the symbol->specification symbol (only its children!).
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1573
  return NULL;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1574
}
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1575
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1576
/* NOTE that the constant folding of GLOBAL variables is already handled by handle_var_extern_global_pair, which 
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1577
 * is called from declaration_check_c,
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1578
 * This is done like this because we need to know the pairing of external<->global variables to get the cvalue
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1579
 * from the global variable. Since the external<->global pairing information is available in the declaration_check_c,
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1580
 * we have that class call the constant_propagation_c::handle_var_extern_global_pair(), which will actually do the 
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1581
 * constant folding of the global and the external variable declarations!
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1582
 */
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1583
/* NOTE: The constant propagation portion of this algorithm must still be done here!!
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1584
 * Even though the constant folding of GLOBAL variables are already handled by handle_var_extern_global_pair(),
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1585
 * we must still visit them here, since when doing constant propagation of a Configuration or a Resource we need the
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1586
 * values of constant variables to be placed in the values[] map, as these same variables may be used to declare
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1587
 * arrays of a variable size
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1588
 *    VAR_GLOBAL CONSTANT max     : INT := 42;             END_VAR
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1589
 *    VAR_GLOBAL          array_v : ARRAY [1..max] of INT; END_VAR  <---- NOTE the use of 'max' in the subrange!
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1590
 */
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1591
/*| VAR_GLOBAL [CONSTANT|RETAIN] global_var_decl_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1592
/* option -> may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1593
// SYM_REF2(global_var_declarations_c, option, global_var_decl_list)
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1594
/* Note that calling handle_var_decl() will result in doing constant folding of literals (of datatype initial values)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1595
 * that were already constant folded by the method handle_var_extern_global_pair()
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1596
 * Nevertheless, since constant folding is idem-potent, it is simpler to just call handle_var_decl() instead
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1597
 * of writing some code specific for this situation!
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1598
 */
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1599
void *constant_propagation_c::visit(global_var_declarations_c *symbol) {return handle_var_decl(symbol->global_var_decl_list, is_constant(symbol->option));}
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1600
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1601
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1602
/* helper symbol for global_var_declarations */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1603
/*| global_var_decl_list global_var_decl ';' */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1604
// SYM_LIST(global_var_decl_list_c)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1605
// void *constant_propagation_c::visit(global_var_decl_list_c *symbol) {} // Not needed: we inherit from iterator_c
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1606
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1607
/*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1608
/* type_specification ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1609
//SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1610
void *constant_propagation_c::visit(global_var_decl_c *symbol) {
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1611
  /* global_var_spec may be either a global_var_spec_c or a global_var_list_c.
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1612
   * Since we already have a nice method that handles var lists (handle_var_list_decl() )
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1613
   * if it is a global_var_spec_c we will create a temporary list so we can call that method!
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1614
   */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1615
  global_var_spec_c *var_spec = dynamic_cast<global_var_spec_c *>(symbol->global_var_spec);
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1616
  if (NULL == var_spec) {
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1617
    // global_var_spec is a global_var_list_c
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1618
    return handle_var_list_decl(symbol->global_var_spec, symbol->type_specification);
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1619
  } else {
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1620
    global_var_list_c var_list;
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1621
    var_list.add_element(var_spec->global_var_name);
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1622
    return handle_var_list_decl(&var_list, symbol->type_specification);
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1623
  }
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1624
}
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1625
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1626
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1627
/*| global_var_name location */
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1628
//SYM_REF2(global_var_spec_c, global_var_name, location)              // Not needed!
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1629
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1630
/*  AT direct_variable */
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1631
//SYM_REF1(location_c, direct_variable)                               // Not needed!
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1632
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1633
/*| global_var_list ',' global_var_name */
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1634
//SYM_LIST(global_var_list_c)                                         // Not needed!
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1635
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1636
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1637
#if 0  
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1638
// TODO
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1639
// We do not do constant folding of strings yet, so there is no need to implement this now!
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1640
/*  var1_list ':' single_byte_string_spec */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1641
SYM_REF2(single_byte_string_var_declaration_c, var1_list, single_byte_string_spec)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1642
/*  STRING ['[' integer ']'] [ASSIGN single_byte_character_string] */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1643
/* integer ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1644
/* single_byte_character_string ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1645
SYM_REF2(single_byte_string_spec_c, string_spec, single_byte_character_string)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1646
/*   STRING ['[' integer ']'] */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1647
/* integer ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1648
SYM_REF2(single_byte_limited_len_string_spec_c, string_type_name, character_string_len)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1649
/*  WSTRING ['[' integer ']'] */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1650
/* integer ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1651
SYM_REF2(double_byte_limited_len_string_spec_c, string_type_name, character_string_len)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1652
/*  var1_list ':' double_byte_string_spec */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1653
SYM_REF2(double_byte_string_var_declaration_c, var1_list, double_byte_string_spec)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1654
/*  WSTRING ['[' integer ']'] [ASSIGN double_byte_character_string] */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1655
/* integer ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1656
/* double_byte_character_string ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1657
SYM_REF2(double_byte_string_spec_c, string_spec, double_byte_character_string)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1658
/*| VAR [RETAIN|NON_RETAIN] incompl_located_var_decl_list END_VAR */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1659
/* option ->may be NULL ! */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1660
SYM_REF2(incompl_located_var_declarations_c, option, incompl_located_var_decl_list)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1661
/* helper symbol for incompl_located_var_declarations */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1662
/*| incompl_located_var_decl_list incompl_located_var_decl ';' */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1663
SYM_LIST(incompl_located_var_decl_list_c)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1664
/*  variable_name incompl_location ':' var_spec */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1665
SYM_REF3(incompl_located_var_decl_c, variable_name, incompl_location, var_spec)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1666
/*  AT incompl_location_token */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1667
SYM_TOKEN(incompl_location_c)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1668
/* intermediate helper symbol for:
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1669
 *  - non_retentive_var_decls
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1670
 *  - output_declarations
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1671
 */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1672
SYM_LIST(var_init_decl_list_c)
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1673
#endif
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1674
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1675
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1676
/***********************/
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1677
/* B 1.5.1 - Functions */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1678
/***********************/
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1679
/* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1680
//SYM_REF4(function_declaration_c, derived_function_name, type_name, var_declarations_list, function_body, enumvalue_symtable_t enumvalue_symtable;)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1681
void *constant_propagation_c::visit(function_declaration_c *symbol) {
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1682
	values.clear(); /* Clear global map */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1683
	/* Add initial value of all declared variables into Values map. */
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1684
	function_pou_ = true;
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1685
	symbol->var_declarations_list->accept(*this);
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1686
	function_pou_ = false;
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1687
	symbol->function_body->accept(*this);
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1688
	return NULL;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1689
}
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1690
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1691
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1692
/* intermediate helper symbol for
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1693
 * - function_declaration
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1694
 * - function_block_declaration
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1695
 * - program_declaration
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1696
 */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1697
// SYM_LIST(var_declarations_list_c) // Not needed since we inherit from iterator_c
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1698
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1699
/* option -> storage method, CONSTANT or <null> */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1700
// SYM_REF2(function_var_decls_c, option, decl_list)
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1701
// NOTE: function_var_decls_c is only used inside Functions, so it is safe to call with fixed_init_value_ = true 
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1702
void *constant_propagation_c::visit(function_var_decls_c *symbol) {return handle_var_decl(symbol->decl_list, true);}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1703
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1704
/* intermediate helper symbol for function_var_decls */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1705
// SYM_LIST(var2_init_decl_list_c) // Not needed since we inherit from iterator_c
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1706
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1707
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1708
/*****************************/
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1709
/* B 1.5.2 - Function Blocks */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1710
/*****************************/
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1711
/*  FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1712
/* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1713
//SYM_REF3(function_block_declaration_c, fblock_name, var_declarations, fblock_body, enumvalue_symtable_t enumvalue_symtable;)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1714
void *constant_propagation_c::visit(function_block_declaration_c *symbol) {
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1715
	values.clear(); /* Clear global map */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1716
	/* Add initial value of all declared variables into Values map. */
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1717
	function_pou_ = false;
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1718
	symbol->var_declarations->accept(*this);
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1719
	symbol->fblock_body->accept(*this);
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1720
	return NULL;
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1721
}
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1722
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1723
/*  VAR_TEMP temp_var_decl_list END_VAR */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1724
// SYM_REF1(temp_var_decls_c, var_decl_list)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1725
void *constant_propagation_c::visit(temp_var_decls_c *symbol) {return handle_var_decl(symbol->var_decl_list, true);}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1726
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1727
/* intermediate helper symbol for temp_var_decls */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1728
// SYM_LIST(temp_var_decls_list_c)
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1729
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1730
/*  VAR NON_RETAIN var_init_decl_list END_VAR */
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1731
// SYM_REF1(non_retentive_var_decls_c, var_decl_list)
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1732
// NOTE: non_retentive_var_decls_c is only used inside FBs and Programs, so it is safe to call with fixed_init_value_ = false 
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1733
void *constant_propagation_c::visit(non_retentive_var_decls_c *symbol) {return handle_var_decl(symbol->var_decl_list, false);}
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1734
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1735
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1736
/**********************/
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1737
/* B 1.5.3 - Programs */
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1738
/**********************/
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1739
/*  PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1740
//SYM_REF3(program_declaration_c, program_type_name, var_declarations, function_block_body, enumvalue_symtable_t enumvalue_symtable;)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1741
void *constant_propagation_c::visit(program_declaration_c *symbol) {
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1742
	values.clear(); /* Clear global map */
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  1743
	/* Add initial value of all declared variables into Values map. */
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1744
	function_pou_ = false;
787
6e2671e0f1a8 Fix constant_folding missing call. [Bug found by Mario.]
Manuele Conti <conti.ma@alice.it>
parents: 783
diff changeset
  1745
	symbol->var_declarations->accept(*this);
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1746
	symbol->function_block_body->accept(*this);
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1747
	return NULL;
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1748
}
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
  1749
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
  1750
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1751
/********************************/
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1752
/* B 1.7 Configuration elements */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1753
/********************************/
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1754
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1755
/*
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1756
CONFIGURATION configuration_name
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1757
   optional_global_var_declarations
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1758
   (resource_declaration_list | single_resource_declaration)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1759
   optional_access_declarations
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1760
   optional_instance_specific_initializations
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1761
END_CONFIGURATION
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1762
*/
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1763
/* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1764
// SYM_REF5(configuration_declaration_c, configuration_name, global_var_declarations, resource_declarations, access_declarations, instance_specific_initializations, 
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1765
//          enumvalue_symtable_t enumvalue_symtable; localvar_symbmap_t localvar_symbmap; localvar_symbvec_t localvar_symbvec;)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1766
void *constant_propagation_c::visit(configuration_declaration_c *symbol) {
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1767
	values.clear(); /* Clear global map */
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1768
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1769
	/* Add initial value of all declared variables into Values map. */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1770
	function_pou_ = false;
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1771
	current_configuration = symbol;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1772
	iterator_visitor_c::visit(symbol); // let the base iterator class handle the rest (basically iterate through the whole configuration and do the constant folding!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1773
	current_configuration = NULL;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1774
	return NULL;
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1775
}
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1776
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1777
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1778
/* helper symbol for configuration_declaration */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1779
// SYM_LIST(resource_declaration_list_c)           // Not needed: we inherit from iterator_c
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1780
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1781
/*
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1782
RESOURCE resource_name ON resource_type_name
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1783
   optional_global_var_declarations
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1784
   single_resource_declaration
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1785
END_RESOURCE
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1786
*/
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1787
/* enumvalue_symtable is filled in by enum_declaration_check_c, during stage3 semantic verification, with a list of all enumerated constants declared inside this POU */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1788
// SYM_REF4(resource_declaration_c, resource_name, resource_type_name, global_var_declarations, resource_declaration, 
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1789
//          enumvalue_symtable_t enumvalue_symtable; localvar_symbmap_t localvar_symbmap; localvar_symbvec_t localvar_symbvec;)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1790
void *constant_propagation_c::visit(resource_declaration_c *symbol) {
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 970
diff changeset
  1791
	values.push(); /* Create inner scope */
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1792
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1793
	/* Add initial value of all declared variables into Values map. */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1794
	function_pou_ = false;
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1795
	symbol->global_var_declarations->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1796
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1797
	var_global_values = values;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1798
	values.clear();
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1799
	current_resource = symbol;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1800
	symbol->resource_declaration->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1801
	current_resource = NULL;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1802
	values = var_global_values;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1803
// 	iterator_visitor_c::visit(symbol); // let the base iterator class handle the rest (basically iterate through the whole configuration and do the constant folding!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1804
973
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 970
diff changeset
  1805
	values.pop(); /* Delete inner scope */
f86d5d6bb04e Do constant propagation of configuration/resource variables, taking into account scope of variables.
mjsousa
parents: 970
diff changeset
  1806
	return NULL;
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1807
}
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1808
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1809
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1810
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1811
/* task_configuration_list program_configuration_list */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1812
// SYM_REF2(single_resource_declaration_c, task_configuration_list, program_configuration_list)
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1813
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1814
/* helper symbol for single_resource_declaration */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1815
// SYM_LIST(task_configuration_list_c)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1816
/* helper symbol for single_resource_declaration */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1817
// SYM_LIST(program_configuration_list_c)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1818
/* helper symbol for: (access_path, instance_specific_init) */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1819
// SYM_LIST(any_fb_name_list_c)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1820
/*  [resource_name '.'] global_var_name ['.' structure_element_name] */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1821
// SYM_REF3(global_var_reference_c, resource_name, global_var_name, structure_element_name)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1822
/*  prev_declared_program_name '.' symbolic_variable */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1823
// SYM_REF2(program_output_reference_c, program_name, symbolic_variable)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1824
/*  TASK task_name task_initialization */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1825
// SYM_REF2(task_configuration_c, task_name, task_initialization)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1826
/*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1827
// SYM_REF3(task_initialization_c, single_data_source, interval_data_source, priority_data_source)
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1828
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1829
/*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1830
/* NOTE: The parameter 'called_prog_declaration'is used to pass data between stage 3 and stage4 */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1831
// SYM_REF5(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements,
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1832
//          symbol_c *called_prog_declaration;)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1833
void *constant_propagation_c::visit(program_configuration_c *symbol) {
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1834
	/* find the declaration (i.e. the datatype) of the program being instantiated */
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1835
	// NOTE: we do not use symbol->datatype so this cost propagation algorithm will not depend on the fill/narrow datatypes algorithm!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1836
	program_type_symtable_t::iterator itr = program_type_symtable.find(symbol->program_type_name);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1837
	if (itr == program_type_symtable.end()) ERROR; // syntax parsing should not allow this!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1838
	program_declaration_c *prog_type = itr->second;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1839
	if (NULL == prog_type) ERROR; // syntax parsing should not allow this!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1840
	prog_type->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1841
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1842
	if (NULL != symbol->prog_conf_elements)
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1843
		symbol->prog_conf_elements->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1844
	return NULL;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1845
}
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1846
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1847
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1848
/* prog_conf_elements ',' prog_conf_element */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1849
// SYM_LIST(prog_conf_elements_c)
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1850
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1851
/*  fb_name WITH task_name */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1852
// SYM_REF2(fb_task_c, fb_name, task_name)
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1853
void *constant_propagation_c::visit(fb_task_c *symbol) {
982
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1854
	/* find the declaration (i.e. the datatype) of the FB being instantiated */
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1855
	// NOTE: we do not use symbol->datatype so this cost propagation algorithm will not depend on the fill/narrow datatypes algorithm!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1856
	symbol_c *fb_type_name = NULL;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1857
	
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1858
	if ((NULL == fb_type_name) && (NULL != current_configuration)) {
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1859
		search_var_instance_decl_c search_scope(current_configuration);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1860
		fb_type_name = search_scope.get_decl(symbol->fb_name);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1861
	}
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1862
	if ((NULL == fb_type_name) && (NULL != current_resource)) {
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1863
		search_var_instance_decl_c search_scope(current_resource);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1864
		fb_type_name = search_scope.get_decl(symbol->fb_name);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1865
	}
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1866
	if (NULL == fb_type_name) ERROR;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1867
	
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1868
	function_block_type_symtable_t::iterator itr = function_block_type_symtable.find(fb_type_name);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1869
	if (itr == function_block_type_symtable.end()) ERROR; // syntax parsing should not allow this!
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1870
	function_block_declaration_c *fb_type_decl = itr->second;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1871
	if (NULL == fb_type_decl) ERROR;	
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1872
	fb_type_decl->accept(*this);
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1873
	return NULL;
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1874
}
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1875
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1876
760b26477193 Change algorithm for propagating VAR_GLOBAL constant values to corresponding VAR_EXTERNAL.
mjsousa
parents: 981
diff changeset
  1877
969
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1878
/*  any_symbolic_variable ASSIGN prog_data_source */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1879
// SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1880
/* any_symbolic_variable SENDTO data_sink */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1881
// SYM_REF2(prog_cnxn_sendto_c, symbolic_variable, data_sink)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1882
/* VAR_CONFIG instance_specific_init_list END_VAR */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1883
// SYM_REF1(instance_specific_initializations_c, instance_specific_init_list)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1884
/* helper symbol for instance_specific_initializations */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1885
// SYM_LIST(instance_specific_init_list_c)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1886
/* resource_name '.' program_name '.' {fb_name '.'}
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1887
    ((variable_name [location] ':' located_var_spec_init) | (fb_name ':' fb_initialization))
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1888
*/
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1889
// SYM_REF6(instance_specific_init_c, resource_name, program_name, any_fb_name_list, variable_name, location, initialization)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1890
/* helper symbol for instance_specific_init */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1891
/* function_block_type_name ':=' structure_initialization */
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1892
// SYM_REF2(fb_initialization_c, function_block_type_name, structure_initialization)
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1893
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1894
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1895
706a152731ab Do constant propagation inside Configurations and Resources (required for support of extension: array [1..max] OF int); (Resources is still buggy)
mjsousa
parents: 968
diff changeset
  1896
567
e5deeb6d4d2f create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents: 566
diff changeset
  1897
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
  1898
/***************************************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
  1899
/* B.3 - Language ST (Structured Text) */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
  1900
/***************************************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
  1901
/***********************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
  1902
/* B 3.1 - Expressions */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
  1903
/***********************/
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1904
#if DO_CONSTANT_PROPAGATION__
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 607
diff changeset
  1905
/* TODO: handle function invocations... */
633
73b56dc69e61 Fix bug with task interval using fixed_point value for duration items
Laurent Bessard
parents: 621
diff changeset
  1906
// void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) {}
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1907
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1908
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1909
/*********************************/
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1910
/* B 3.2.1 Assignment Statements */
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1911
/*********************************/
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1912
void *constant_propagation_c::visit(assignment_statement_c *symbol) {
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1913
	std::string varName;
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1914
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1915
	symbol->r_exp->accept(*this);
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1916
	symbol->l_exp->accept(*this); // if the lvalue has an array, do contant folding of the array indexes!
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1917
	symbol->l_exp->const_value = symbol->r_exp->const_value;
974
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
  1918
	values[get_var_name_c::get_name(symbol->l_exp)->value] = symbol->l_exp->const_value;
967
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1919
	return NULL;
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1920
}
544ff4dff04f Do constant propagation to symbolic_variables, and correctly handle constant folding and propagation of variable declarations.
mjsousa
parents: 965
diff changeset
  1921
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1922
/********************************/
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1923
/* B 3.2.3 Selection Statements */
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1924
/********************************/
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1925
void *constant_propagation_c::visit(if_statement_c *symbol) {
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1926
	map_values_t values_incoming;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1927
	map_values_t values_statement_result;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1928
	map_values_t values_elsestatement_result;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1929
	map_values_t::iterator itr;
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1930
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1931
	/* Optimize dead code */
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1932
	symbol->expression->accept(*this);
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1933
	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1934
		return NULL;
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1935
774
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1936
	values_incoming = values; /* save incoming status */
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1937
	symbol->statement_list->accept(*this);
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1938
	values_statement_result = values;
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1939
	if (NULL != symbol->else_statement_list) {
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1940
		values = values_incoming;
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1941
		symbol->else_statement_list->accept(*this);
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1942
		values_elsestatement_result = values;
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1943
	} else
979af2009d88 Start to implement constant propagation algorithm.
Manuele Conti <conti.ma@alice.it>
parents: 735
diff changeset
  1944
		values_elsestatement_result = values_incoming;
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1945
	values = inner_left_join_values(values_statement_result, values_elsestatement_result);
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1946
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1947
	return NULL;
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1948
}
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1949
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1950
/********************************/
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1951
/* B 3.2.4 Iteration Statements */
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1952
/********************************/
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1953
void *constant_propagation_c::visit(for_statement_c *symbol) {
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1954
	map_values_t values_incoming;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1955
	map_values_t values_statement_result;
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1956
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1957
	values_incoming = values; /* save incoming status */
782
c8cd69801b7e Fix constant propagation alg. in for statement like Mario suggestion.
Manuele Conti <conti.ma@alice.it>
parents: 781
diff changeset
  1958
	symbol->beg_expression->accept(*this);
c8cd69801b7e Fix constant propagation alg. in for statement like Mario suggestion.
Manuele Conti <conti.ma@alice.it>
parents: 781
diff changeset
  1959
	symbol->end_expression->accept(*this);
974
a47c2df5ae3d Fix previous commit (symtable operator[] must return a reference to the stored value!)
mjsousa
parents: 973
diff changeset
  1960
	values[get_var_name_c::get_name(symbol->control_variable)->value]._int64.status = const_value_c::cs_non_const;
782
c8cd69801b7e Fix constant propagation alg. in for statement like Mario suggestion.
Manuele Conti <conti.ma@alice.it>
parents: 781
diff changeset
  1961
c8cd69801b7e Fix constant propagation alg. in for statement like Mario suggestion.
Manuele Conti <conti.ma@alice.it>
parents: 781
diff changeset
  1962
	/* Optimize dead code */
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1963
	if (NULL != symbol->by_expression) {
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1964
		symbol->by_expression->accept(*this);
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1965
		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) > 0 &&
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1966
			VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)     &&
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1967
			  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1968
			return NULL;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1969
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1970
		if (VALID_CVALUE(int64, symbol->by_expression ) &&   GET_CVALUE(int64, symbol->by_expression ) < 0 &&
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1971
			VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)    &&
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1972
			  GET_CVALUE(int64, symbol->beg_expression) <    GET_CVALUE(int64, symbol->end_expression))
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1973
			return NULL;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1974
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1975
	} else {
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1976
		if (VALID_CVALUE(int64, symbol->beg_expression) && VALID_CVALUE(int64, symbol->end_expression)     &&
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1977
			  GET_CVALUE(int64, symbol->beg_expression) >    GET_CVALUE(int64, symbol->end_expression))
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1978
			return NULL;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1979
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1980
	}
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1981
782
c8cd69801b7e Fix constant propagation alg. in for statement like Mario suggestion.
Manuele Conti <conti.ma@alice.it>
parents: 781
diff changeset
  1982
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1983
	symbol->statement_list->accept(*this);
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1984
	values_statement_result = values;
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1985
	values = inner_left_join_values(values_statement_result, values_incoming);
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1986
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1987
	return NULL;
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1988
}
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1989
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  1990
void *constant_propagation_c::visit(while_statement_c *symbol) {
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1991
	map_values_t values_incoming;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  1992
	map_values_t values_statement_result;
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1993
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1994
	/* Optimize dead code */
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1995
	symbol->expression->accept(*this);
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1996
	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == false)
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1997
		return NULL;
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1998
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  1999
	values_incoming = values; /* save incoming status */
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2000
	symbol->statement_list->accept(*this);
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2001
	values_statement_result = values;
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2002
	values = inner_left_join_values(values_statement_result, values_incoming);
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2003
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2004
	return NULL;
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2005
}
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2006
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 983
diff changeset
  2007
void *constant_propagation_c::visit(repeat_statement_c *symbol) {
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2008
	map_values_t values_incoming;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2009
	map_values_t values_statement_result;
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2010
790
a722594dcd64 Fix constant propagation for repeat_statement_c class.
Manuele Conti <conti.ma@alice.it>
parents: 789
diff changeset
  2011
	values_incoming = values; /* save incoming status */
a722594dcd64 Fix constant propagation for repeat_statement_c class.
Manuele Conti <conti.ma@alice.it>
parents: 789
diff changeset
  2012
	symbol->statement_list->accept(*this);
a722594dcd64 Fix constant propagation for repeat_statement_c class.
Manuele Conti <conti.ma@alice.it>
parents: 789
diff changeset
  2013
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2014
	/* Optimize dead code */
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2015
	symbol->expression->accept(*this);
790
a722594dcd64 Fix constant propagation for repeat_statement_c class.
Manuele Conti <conti.ma@alice.it>
parents: 789
diff changeset
  2016
	if (VALID_CVALUE(bool, symbol->expression) && GET_CVALUE(bool, symbol->expression) == true)
780
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2017
		return NULL;
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2018
9fbdf8a7430e Add constant propagation algorithm for loop cycles.
Manuele Conti <conti.ma@alice.it>
parents: 776
diff changeset
  2019
	values_statement_result = values;
788
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2020
	values = inner_left_join_values(values_statement_result, values_incoming);
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2021
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2022
	return NULL;
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2023
}
aa56031e5cb3 Implement Mario's suggestions:
Manuele Conti <conti.ma@alice.it>
parents: 787
diff changeset
  2024
792
78083edf93d5 disable not yet complete constant propagation algorithm (currently brocken and producing incorrect results),
Mario de Sousa <msousa@fe.up.pt>
parents: 791
diff changeset
  2025
#endif  // DO_CONSTANT_PROPAGATION__
78083edf93d5 disable not yet complete constant propagation algorithm (currently brocken and producing incorrect results),
Mario de Sousa <msousa@fe.up.pt>
parents: 791
diff changeset
  2026
78083edf93d5 disable not yet complete constant propagation algorithm (currently brocken and producing incorrect results),
Mario de Sousa <msousa@fe.up.pt>
parents: 791
diff changeset
  2027
963
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  2028
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  2029
e3d4dca7520b Do constant folding of variable's initial value (allows correct C code generation with variables in the subrange of an array declaration: ARRAY [1..max] of INT).
mjsousa
parents: 945
diff changeset
  2030