stage3/constant_folding.cc
author Mario de Sousa <msousa@fe.up.pt>
Sun, 10 Jun 2012 15:38:24 +0100
changeset 574 d291a942899b
parent 573 e28b47911c19
child 575 a1b63f776535
permissions -rw-r--r--
Fix remaining overflow checks.
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
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    35
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    36
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    37
/* Do constant folding...
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    38
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    39
 * 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
    40
 * 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
    41
 * (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
    42
 *
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
    43
 * For example:
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    44
 *       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
    45
 *       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
    46
 *       etc...
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    47
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    48
 *
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
    49
 * 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
    50
 *      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
    51
 *        1 AND 0
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    52
 *      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
    53
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    54
 *      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
    55
 *        1 + 2
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    56
 *      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
    57
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    58
 *      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
    59
 *        - bool
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    60
 *        - uint64
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    61
 *        -  int64
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    62
 *        - real64
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
 *      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
    65
 *      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
    66
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    67
 *             ** 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
    68
 *              * 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
    69
 *              * 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
    70
 *              * 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
    71
 *              * 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
    72
 *              * 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
    73
 *              * 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
    74
 *              * 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
    75
 *              **
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    76
 *             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
    77
 *                            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
    78
 *                            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
    79
 *                          } const_status_t;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    80
 *    
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    81
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    82
 *                 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
    83
 *                 real64_t       value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    84
 *             } 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
    85
 *             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
    86
 *             
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    87
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    88
 *                 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
    89
 *                 int64_t        value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    90
 *             } 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
    91
 *             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
    92
 *             
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    93
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    94
 *                 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
    95
 *                 uint64_t       value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    96
 *             } 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
    97
 *             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
    98
 *             
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
    99
 *             typedef struct {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   100
 *                 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
   101
 *                 bool           value; 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   102
 *             } 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
   103
 *             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
   104
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   105
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   106
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   107
 * NOTE 2 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   108
 *    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
   109
 *    We cannot really print out error messages when we find an overflow. Since each operation
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   110
 *    (symbol in the absract syntax tree for that operation) will have up to 4 constant results,
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   111
 *    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
   112
 *    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
   113
 *    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
   114
 *
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   115
 *    For this reason, this visitor merely annotates the abstract syntax tree, and leaves the
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   116
 *    actuall printing of errors for the print_datatype_errors_c class!
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   117
 */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   118
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   119
#include "constant_folding.hh"
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   120
#include <limits>
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   121
#include <math.h> /* required for pow function */
565
8acbddf75333 Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
   122
#include <stdlib.h> /* required for malloc() */
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   123
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   124
#if 1
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   125
#define UINT64_MAX (std::numeric_limits< uint64_t >::max())
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   126
#define  INT64_MAX (std::numeric_limits<  int64_t >::max())
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   127
#define  INT64_MIN (std::numeric_limits<  int64_t >::min()) 
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   128
#else
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   129
#define __STDC_LIMIT_MACROS /* required for UINT64_MAX, INT64_MAX, INT64_MIN, ... */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   130
#include <stdint.h>         /* required for UINT64_MAX, INT64_MAX, INT64_MIN, ... */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   131
#endif
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
   132
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
   133
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   134
#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
   135
#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
   136
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   137
#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) {                                                                  \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   138
  if (current_display_error_level >= error_level) {                                                                         \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   139
    fprintf(stderr, "%s:%d-%d..%d-%d: error: ",                                                                             \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   140
            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
   141
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   142
    fprintf(stderr, __VA_ARGS__);                                                                                           \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   143
    fprintf(stderr, "\n");                                                                                                  \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   144
    error_count++;                                                                                                     \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   145
  }                                                                                                                         \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   146
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   147
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   148
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   149
#define STAGE3_WARNING(symbol1, symbol2, ...) {                                                                             \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   150
    fprintf(stderr, "%s:%d-%d..%d-%d: warning: ",                                                                           \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   151
            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
   152
                                                 LAST_(symbol1,symbol2) ->last_line,  LAST_(symbol1,symbol2) ->last_column);\
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   153
    fprintf(stderr, __VA_ARGS__);                                                                                           \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   154
    fprintf(stderr, "\n");                                                                                                  \
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   155
    warning_found = true;                                                                                                   \
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
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   158
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   159
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
   160
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   161
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   162
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   163
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   164
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   165
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   166
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   167
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   168
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   169
#define NEW_CVALUE(dtype, symbol) \
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   170
 (symbol->const_value_##dtype) = new(symbol_c::const_value_##dtype##_t); \
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   171
 if ((symbol->const_value_##dtype) == NULL) ERROR; \
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   172
 (symbol->const_value_##dtype)->status = symbol_c::cs_undefined;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   173
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   174
#define SET_CVALUE(dtype, symbol, new_value)  ((symbol)->const_value_##dtype->value) = new_value; ((symbol)->const_value_##dtype->status) = symbol_c::cs_const_value;  
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   175
#define GET_CVALUE(dtype, symbol)             ((symbol)->const_value_##dtype->value) 
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   176
#define SET_OVFLOW(dtype, symbol)             ((symbol)->const_value_##dtype->status) = symbol_c::cs_overflow
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   177
    /* The following test is correct in the presence of a NULL pointer, as the logical evaluation will be suspended as soon as the first condition is false! */
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   178
#define VALID_CVALUE(dtype, symbol)           ((NULL != (symbol)->const_value_##dtype) && (symbol_c::cs_const_value == (symbol)->const_value_##dtype->status))
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   179
#define ISZERO_CVALUE(dtype, symbol)          ((VALID_CVALUE(dtype, symbol)) && (GET_CVALUE(dtype, symbol) == 0))
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   180
#define DO_BINARY_OPER(dtype, oper)\
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   181
	if (VALID_CVALUE(dtype, symbol->r_exp) && VALID_CVALUE(dtype, symbol->l_exp)) {                                \
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   182
		NEW_CVALUE(dtype, symbol);                                                                             \
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   183
		SET_CVALUE(dtype, symbol, GET_CVALUE(dtype, symbol->l_exp) oper GET_CVALUE(dtype, symbol->r_exp));     \
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
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   186
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   187
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   188
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   189
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   190
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   191
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   192
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   193
/* NOTE:
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   194
 *   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
   195
 *   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
   196
 *   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
   197
 */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   198
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   199
/* 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
   200
 *       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
   201
 *
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   202
 *       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
   203
 */
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   204
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   205
/* 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
   206
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
   207
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   208
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   209
	/* 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
   210
	// 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
   211
	/* 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
   212
	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
   213
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   214
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   215
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   216
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   217
/* 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
   218
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
   219
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   220
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   221
	/* 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
   222
	// 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
   223
	/* Test by pre-condition: if b > a => overflow! */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   224
	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
   225
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   226
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   227
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   228
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   229
/* 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
   230
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
   231
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   232
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   233
	/* 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
   234
	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
   235
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   236
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   237
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   238
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   239
/* 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
   240
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
   241
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   242
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   243
	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
   244
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   245
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   246
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   247
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   248
/* 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
   249
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
   250
	if (!VALID_CVALUE(uint64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   251
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   252
	/* 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
   253
	if (false) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   254
		SET_OVFLOW(uint64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   255
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   256
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   257
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   258
/* 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
   259
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
   260
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   261
		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
   262
	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
   263
	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
   264
	/* 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
   265
	if (((b > 0) && (a > (INT64_MAX - b)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   266
	 || ((b < 0) && (a < (INT64_MIN - b))))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   267
		SET_OVFLOW(int64, res);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   268
}
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   269
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   270
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   271
/* 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
   272
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
   273
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   274
		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
   275
	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
   276
	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
   277
	/* 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
   278
	if (((b > 0) && (a < (INT64_MIN + b)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   279
	 || ((b < 0) && (a > (INT64_MAX + b))))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   280
		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
   281
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   282
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   283
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   284
/* 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
   285
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
   286
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   287
		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
   288
	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
   289
	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
   290
	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
   291
	    || ( (a > 0) && !(b > 0) &&             (b < (INT64_MIN / a))) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   292
	    || (!(a > 0) &&  (b > 0) &&             (a < (INT64_MIN / b))) 
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   293
	    || (!(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
   294
		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
   295
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   296
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   297
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   298
/* 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
   299
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
   300
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   301
		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
   302
	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
   303
	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
   304
	if ((b == 0) || ((a == INT64_MIN) && (b == -1)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   305
		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
   306
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   307
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   308
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   309
/* 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
   310
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
   311
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   312
		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
   313
	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
   314
	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
   315
	/* 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
   316
	 *  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
   317
	 *
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   318
	 * 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
   319
	 * 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
   320
	 *
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   321
	 * 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
   322
	 */
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   323
	if ((a == INT64_MIN) && (b == -1))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   324
		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
   325
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   326
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   327
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   328
/* res = - a */
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   329
static void CHECK_OVERFLOW_int64_NEG(symbol_c *res, symbol_c *a_ptr) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   330
	if (!VALID_CVALUE(int64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   331
		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
   332
	int64_t a = GET_CVALUE(int64, a_ptr);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   333
	if (a == INT64_MIN)
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   334
		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
   335
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   336
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   337
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   338
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   339
static void CHECK_OVERFLOW_real64(symbol_c *res) {
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   340
	if (!VALID_CVALUE(real64, res))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   341
		return;
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   342
       	/* NaN => underflow, overflow, number is a higher precision format, is a complex number (IEEE standard) */
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   343
	if (isnan(GET_CVALUE(real64, res)))
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   344
		SET_OVFLOW(real64, 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
   345
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   346
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   347
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   348
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   349
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   350
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   351
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   352
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   353
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   354
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   355
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   356
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   357
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   358
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   359
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   360
constant_folding_c::constant_folding_c(symbol_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   361
    error_count = 0;
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
   362
    warning_found = false;
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   363
    current_display_error_level = 0;
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
   364
    
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
   365
    /* check whether the platform on which the compiler is being run implements IEC 559 floating point data types. */
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
   366
    symbol_c null_symbol;
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
   367
    if (! (std::numeric_limits<real64_t>::is_iec559) )
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
   368
        STAGE3_WARNING(&null_symbol, &null_symbol, "The platform running the compiler does not implement IEC 559 floating point numbers. "
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
   369
                                                   "Any error and/or warning messages related to overflow/underflow of the result of operations on REAL/LREAL literals "
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   370
                                                   " (i.e. constant folding) may themselves be erroneous, although are most probably correct.");
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   371
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   372
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
   373
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   374
constant_folding_c::~constant_folding_c(void) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   375
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   376
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
   377
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   378
int constant_folding_c::get_error_count() {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   379
	return error_count;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   380
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   381
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   382
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   383
/*********************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   384
/* B 1.2 - Constants */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   385
/*********************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   386
/******************************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   387
/* B 1.2.1 - Numeric Literals */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   388
/******************************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   389
void *constant_folding_c::visit(real_c *symbol) {
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   390
	NEW_CVALUE(real64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   391
	SET_CVALUE(real64, symbol, extract_real_value(symbol));
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
   392
	return NULL;
e5deeb6d4d2f create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents: 566
diff changeset
   393
}
e5deeb6d4d2f create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents: 566
diff changeset
   394
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   395
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   396
void *constant_folding_c::visit(integer_c *symbol) {
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   397
	NEW_CVALUE( int64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   398
	SET_CVALUE( int64, symbol, extract_integer_value(symbol));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   399
	NEW_CVALUE(uint64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   400
	SET_CVALUE(uint64, symbol, extract_integer_value(symbol));
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   401
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   402
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   403
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
   404
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   405
void *constant_folding_c::visit(neg_real_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   406
	symbol->exp->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   407
	if (!VALID_CVALUE(real64, symbol->exp))
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   408
		return NULL;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   409
	NEW_CVALUE(real64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   410
	SET_CVALUE(real64, symbol, - GET_CVALUE( real64, symbol->exp));
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   411
	CHECK_OVERFLOW_real64(symbol);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   412
	return NULL;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   413
}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   414
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   415
/* | '-' integer	{$$ = new neg_integer_c($2, locloc(@$));} */
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   416
void *constant_folding_c::visit(neg_integer_c *symbol) {
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   417
	symbol->exp->accept(*this);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   418
	if (VALID_CVALUE(int64, symbol->exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   419
		NEW_CVALUE( int64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   420
		SET_CVALUE( int64, symbol, - GET_CVALUE( int64, symbol->exp));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   421
	}
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   422
	CHECK_OVERFLOW_int64_NEG(symbol, symbol->exp);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   423
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   424
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   425
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
   426
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   427
void *constant_folding_c::visit(binary_integer_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   428
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   429
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   430
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
   431
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   432
void *constant_folding_c::visit(octal_integer_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   433
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   434
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   435
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
   436
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   437
void *constant_folding_c::visit(hex_integer_c *symbol) {
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   438
	NEW_CVALUE( int64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   439
	SET_CVALUE( int64, symbol, extract_hex_value(symbol));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   440
	NEW_CVALUE(uint64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   441
	SET_CVALUE(uint64, symbol, extract_hex_value(symbol));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   442
	return NULL;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   443
}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   444
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   445
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   446
/*
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   447
integer_literal:
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   448
  integer_type_name '#' signed_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   449
| integer_type_name '#' binary_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   450
| integer_type_name '#' octal_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   451
| integer_type_name '#' hex_integer	{$$ = new integer_literal_c($1, $3, locloc(@$));}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   452
*/
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   453
// SYM_REF2(integer_literal_c, type, value)
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   454
void *constant_folding_c::visit(integer_literal_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   455
	symbol->value->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   456
	if (VALID_CVALUE( int64, symbol->value)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   457
		NEW_CVALUE( int64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   458
		SET_CVALUE( int64, symbol, GET_CVALUE( int64, symbol->value));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   459
	}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   460
	if (VALID_CVALUE(uint64, symbol->value)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   461
		NEW_CVALUE(uint64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   462
		SET_CVALUE(uint64, symbol, GET_CVALUE(uint64, symbol->value));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   463
	}
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   464
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   465
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   466
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
   467
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   468
void *constant_folding_c::visit(real_literal_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   469
	symbol->value->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   470
	if (VALID_CVALUE(real64, symbol->value)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   471
		NEW_CVALUE(real64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   472
		SET_CVALUE(real64, symbol,  GET_CVALUE(real64, symbol->value));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   473
	}
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   474
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   475
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   476
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
   477
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   478
void *constant_folding_c::visit(bit_string_literal_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   479
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   480
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   481
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
   482
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   483
void *constant_folding_c::visit(boolean_literal_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   484
	symbol->value->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   485
	if (VALID_CVALUE(bool, symbol->value)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   486
		NEW_CVALUE(bool, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   487
		SET_CVALUE(bool, symbol,  GET_CVALUE(  bool, symbol->value));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   488
	}
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   489
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   490
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   491
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
   492
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   493
void *constant_folding_c::visit(boolean_true_c *symbol) {
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   494
	NEW_CVALUE(bool, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   495
	SET_CVALUE(bool, symbol, true);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   496
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   497
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   498
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
   499
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   500
void *constant_folding_c::visit(boolean_false_c *symbol) {
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   501
	NEW_CVALUE(bool, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   502
	SET_CVALUE(bool, symbol, false);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   503
	return NULL;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   504
}
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
   505
e5deeb6d4d2f create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents: 566
diff changeset
   506
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   507
/***************************************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   508
/* B.3 - Language ST (Structured Text) */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   509
/***************************************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   510
/***********************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   511
/* B 3.1 - Expressions */
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   512
/***********************/
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   513
void *constant_folding_c::visit(or_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   514
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   515
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   516
	DO_BINARY_OPER(bool,   ||);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   517
	DO_BINARY_OPER(uint64, | );
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   518
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   519
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   520
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
   521
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   522
void *constant_folding_c::visit(xor_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   523
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   524
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   525
	DO_BINARY_OPER(bool,   ^);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   526
	DO_BINARY_OPER(uint64, ^);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   527
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   528
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   529
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
   530
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   531
void *constant_folding_c::visit(and_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   532
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   533
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   534
	DO_BINARY_OPER(bool,   &&);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   535
	DO_BINARY_OPER(uint64, & );
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   536
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   537
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   538
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
   539
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   540
void *constant_folding_c::visit(equ_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   541
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   542
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   543
	DO_BINARY_OPER(bool,   ==);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   544
	DO_BINARY_OPER(uint64, ==);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   545
	DO_BINARY_OPER( int64, ==);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   546
	DO_BINARY_OPER(real64, ==);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   547
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   548
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   549
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
   550
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   551
void *constant_folding_c::visit(notequ_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   552
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   553
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   554
	DO_BINARY_OPER(bool,   !=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   555
	DO_BINARY_OPER(uint64, !=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   556
	DO_BINARY_OPER( int64, !=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   557
	DO_BINARY_OPER(real64, !=);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   558
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   559
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   560
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
   561
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   562
void *constant_folding_c::visit(lt_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   563
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   564
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   565
	DO_BINARY_OPER(bool,   <);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   566
	DO_BINARY_OPER(uint64, <);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   567
	DO_BINARY_OPER( int64, <);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   568
	DO_BINARY_OPER(real64, <);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   569
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   570
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   571
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
   572
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   573
void *constant_folding_c::visit(gt_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   574
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   575
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   576
	DO_BINARY_OPER(bool,   >);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   577
	DO_BINARY_OPER(uint64, >);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   578
	DO_BINARY_OPER( int64, >);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   579
	DO_BINARY_OPER(real64, >);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   580
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   581
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   582
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
   583
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   584
void *constant_folding_c::visit(le_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   585
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   586
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   587
	DO_BINARY_OPER(bool,   <=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   588
	DO_BINARY_OPER(uint64, <=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   589
	DO_BINARY_OPER( int64, <=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   590
	DO_BINARY_OPER(real64, <=);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   591
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   592
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   593
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
   594
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   595
void *constant_folding_c::visit(ge_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   596
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   597
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   598
	DO_BINARY_OPER(bool,   >=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   599
	DO_BINARY_OPER(uint64, >=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   600
	DO_BINARY_OPER( int64, >=);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   601
	DO_BINARY_OPER(real64, >=);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   602
	return NULL;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   603
}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   604
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   605
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   606
void *constant_folding_c::visit(add_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   607
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   608
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   609
	DO_BINARY_OPER(uint64, +);   CHECK_OVERFLOW_uint64_SUM(symbol, symbol->l_exp, symbol->r_exp);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   610
	DO_BINARY_OPER( int64, +);   CHECK_OVERFLOW_int64_SUM (symbol, symbol->l_exp, symbol->r_exp);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   611
	DO_BINARY_OPER(real64, +);   CHECK_OVERFLOW_real64    (symbol);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   612
	return NULL;
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   613
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   614
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   615
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   616
void *constant_folding_c::visit(sub_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   617
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   618
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   619
	DO_BINARY_OPER(uint64, -);   CHECK_OVERFLOW_uint64_SUB(symbol, symbol->l_exp, symbol->r_exp);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   620
	DO_BINARY_OPER( int64, -);   CHECK_OVERFLOW_int64_SUB (symbol, symbol->l_exp, symbol->r_exp);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   621
	DO_BINARY_OPER(real64, -);   CHECK_OVERFLOW_real64    (symbol);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   622
	return NULL;
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   623
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   624
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   625
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   626
void *constant_folding_c::visit(mul_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   627
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   628
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   629
	DO_BINARY_OPER(uint64, *);   CHECK_OVERFLOW_uint64_MUL(symbol, symbol->l_exp, symbol->r_exp);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   630
	DO_BINARY_OPER( int64, *);   CHECK_OVERFLOW_int64_MUL (symbol, symbol->l_exp, symbol->r_exp);
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   631
	DO_BINARY_OPER(real64, *);   CHECK_OVERFLOW_real64    (symbol);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   632
	return NULL;
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   633
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   634
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   635
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   636
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   637
void *constant_folding_c::visit(div_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   638
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   639
	symbol->r_exp->accept(*this);
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   640
	if (ISZERO_CVALUE(uint64, symbol->r_exp))  {NEW_CVALUE(uint64, symbol); SET_OVFLOW(uint64, symbol);} else {DO_BINARY_OPER(uint64, /); CHECK_OVERFLOW_uint64_DIV(symbol, symbol->l_exp, symbol->r_exp);};
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   641
	if (ISZERO_CVALUE( int64, symbol->r_exp))  {NEW_CVALUE( int64, symbol); SET_OVFLOW( int64, symbol);} else {DO_BINARY_OPER( int64, /); CHECK_OVERFLOW_int64_DIV(symbol, symbol->l_exp, symbol->r_exp);};
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   642
	if (ISZERO_CVALUE(real64, symbol->r_exp))  {NEW_CVALUE(real64, symbol); SET_OVFLOW(real64, symbol);} else {DO_BINARY_OPER(real64, /); CHECK_OVERFLOW_real64(symbol);};
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   643
	return NULL;
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   644
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   645
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   646
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   647
void *constant_folding_c::visit(mod_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   648
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   649
	symbol->r_exp->accept(*this);
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   650
	/* 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
   651
	 *  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
   652
	 *
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   653
	 * 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
   654
	 * 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
   655
	 */
574
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   656
	if (ISZERO_CVALUE(uint64, symbol->r_exp))  {NEW_CVALUE(uint64, symbol); SET_CVALUE(uint64, symbol, 0);} else {DO_BINARY_OPER(uint64, %); CHECK_OVERFLOW_uint64_MOD(symbol, symbol->l_exp, symbol->r_exp);};
d291a942899b Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents: 573
diff changeset
   657
	if (ISZERO_CVALUE( int64, symbol->r_exp))  {NEW_CVALUE( int64, symbol); SET_CVALUE( int64, symbol, 0);} else {DO_BINARY_OPER( int64, %); CHECK_OVERFLOW_int64_MOD(symbol, symbol->l_exp, symbol->r_exp);};
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   658
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   659
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   660
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
   661
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   662
void *constant_folding_c::visit(power_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   663
	symbol->l_exp->accept(*this);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   664
	symbol->r_exp->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   665
	/* NOTE: If the const_value in symbol->r_exp is within the limits of both int64 and uint64, then we do both operations.
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   666
	 *       That is OK, as the result should be identicial (we do create an unnecessary CVALUE variable, but who cares?).
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   667
	 *       If only one is valid, then that is the oper we will do!
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   668
	 */
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   669
	if (VALID_CVALUE(real64, symbol->l_exp) && VALID_CVALUE( int64, symbol->r_exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   670
		NEW_CVALUE(real64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   671
		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, symbol->l_exp), GET_CVALUE( int64, symbol->r_exp)));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   672
	}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   673
	if (VALID_CVALUE(real64, symbol->l_exp) && VALID_CVALUE(uint64, symbol->r_exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   674
		NEW_CVALUE(real64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   675
		SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, symbol->l_exp), GET_CVALUE(uint64, symbol->r_exp)));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   676
	}
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   677
	CHECK_OVERFLOW_real64(symbol);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   678
	return NULL;
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   679
}
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   680
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   681
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   682
void *constant_folding_c::visit(neg_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   683
	symbol->exp->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   684
	if (VALID_CVALUE( int64, symbol->exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   685
		NEW_CVALUE( int64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   686
		SET_CVALUE( int64, symbol, - GET_CVALUE( int64, symbol->exp));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   687
	}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   688
	if (VALID_CVALUE(real64, symbol->exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   689
		NEW_CVALUE(real64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   690
		SET_CVALUE(real64, symbol, - GET_CVALUE(real64, symbol->exp));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   691
	}
572
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   692
	CHECK_OVERFLOW_int64_NEG(symbol, symbol->exp);
c353bc67bf91 Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents: 570
diff changeset
   693
	CHECK_OVERFLOW_real64(symbol);
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   694
	return NULL;
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   695
}
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   696
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
   697
e5deeb6d4d2f create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents: 566
diff changeset
   698
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   699
void *constant_folding_c::visit(not_expression_c *symbol) {
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff changeset
   700
	symbol->exp->accept(*this);
569
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   701
	if (VALID_CVALUE(  bool, symbol->exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   702
		NEW_CVALUE(  bool, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   703
		SET_CVALUE(  bool, symbol,  ! GET_CVALUE(  bool, symbol->exp));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   704
	}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   705
	if (VALID_CVALUE(uint64, symbol->exp)) {
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   706
		NEW_CVALUE(uint64, symbol);
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   707
		SET_CVALUE(uint64, symbol,  ~ GET_CVALUE(uint64, symbol->exp));
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   708
	}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   709
	return NULL;
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   710
}
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   711
0d1ab9e78574 Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents: 568
diff changeset
   712