author | Mario de Sousa <msousa@fe.up.pt> |
Sat, 28 Jul 2012 09:07:55 +0100 | |
changeset 615 | 509b79602f7c |
parent 612 | c062ff18d04f |
child 621 | e3616f6b6959 |
permissions | -rw-r--r-- |
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" |
565
8acbddf75333
Fix a few bugs of previous commit.
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
120 |
#include <stdlib.h> /* required for malloc() */ |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
121 |
|
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
122 |
#include <string.h> /* required for strlen() */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
123 |
// #include <stdlib.h> /* required for atoi() */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
124 |
#include <errno.h> /* required for errno */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
125 |
|
607
be9ba3531afb
cleaning up code. Changing HUGE_VAL to INFINITY.
Mario de Sousa <msousa@fe.up.pt>
parents:
604
diff
changeset
|
126 |
#include "../main.hh" // required for uint8_t, real_64_t, ..., and the macros NAN, INFINITY, INT8_MAX, REAL32_MAX, ... */ |
579
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
127 |
|
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
128 |
|
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
129 |
|
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
130 |
|
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
|
131 |
|
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 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
133 |
#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
|
134 |
#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
|
135 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
136 |
#define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
137 |
if (current_display_error_level >= error_level) { \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
138 |
fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
139 |
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
|
140 |
LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
141 |
fprintf(stderr, __VA_ARGS__); \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
142 |
fprintf(stderr, "\n"); \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
143 |
error_count++; \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
144 |
} \ |
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 |
#define STAGE3_WARNING(symbol1, symbol2, ...) { \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
149 |
fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
150 |
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
|
151 |
LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
152 |
fprintf(stderr, __VA_ARGS__); \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
153 |
fprintf(stderr, "\n"); \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
154 |
warning_found = true; \ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
155 |
} |
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 |
|
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
|
159 |
|
569
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
160 |
|
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 |
|
612 | 168 |
#define SET_CVALUE(dtype, symbol, new_value) ((symbol)->const_value._##dtype.value) = new_value; ((symbol)->const_value._##dtype.status) = symbol_c::cs_const_value; |
169 |
#define GET_CVALUE(dtype, symbol) ((symbol)->const_value._##dtype.value) |
|
170 |
#define SET_OVFLOW(dtype, symbol) ((symbol)->const_value._##dtype.status) = symbol_c::cs_overflow |
|
171 |
#define SET_NONCONST(dtype, symbol) ((symbol)->const_value._##dtype.status) = symbol_c::cs_non_const |
|
172 |
||
173 |
#define VALID_CVALUE(dtype, symbol) (symbol_c::cs_const_value == (symbol)->const_value._##dtype.status) |
|
569
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
174 |
#define ISZERO_CVALUE(dtype, symbol) ((VALID_CVALUE(dtype, symbol)) && (GET_CVALUE(dtype, symbol) == 0)) |
575 | 175 |
|
612 | 176 |
#define ISEQUAL_CVALUE(dtype, symbol1, symbol2) \ |
177 |
(VALID_CVALUE(dtype, symbol1) && VALID_CVALUE(dtype, symbol2) && (GET_CVALUE(dtype, symbol1) == GET_CVALUE(dtype, symbol2))) |
|
178 |
||
577
f578f14cb97f
Fix compare expression in constat folding class.
Manuele Conti <conti.ma@alice.it>
parents:
576
diff
changeset
|
179 |
#define DO_BINARY_OPER(dtype, oper, otype)\ |
569
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
180 |
if (VALID_CVALUE(dtype, symbol->r_exp) && VALID_CVALUE(dtype, symbol->l_exp)) { \ |
577
f578f14cb97f
Fix compare expression in constat folding class.
Manuele Conti <conti.ma@alice.it>
parents:
576
diff
changeset
|
181 |
SET_CVALUE(otype, symbol, GET_CVALUE(dtype, symbol->l_exp) oper GET_CVALUE(dtype, symbol->r_exp)); \ |
569
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
182 |
} |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
183 |
|
612 | 184 |
#define DO_BINARY_OPER_(oper_type, operation, res_type, operand1, operand2)\ |
185 |
if (VALID_CVALUE(oper_type, operand1) && VALID_CVALUE(oper_type, operand2)) { \ |
|
186 |
SET_CVALUE(res_type, symbol, GET_CVALUE(oper_type, operand1) operation GET_CVALUE(oper_type, operand2)); \ |
|
187 |
} |
|
188 |
||
189 |
#define DO_UNARY_OPER(dtype, operation, operand)\ |
|
190 |
if (VALID_CVALUE(dtype, operand)) { \ |
|
191 |
SET_CVALUE(dtype, symbol, operation GET_CVALUE(dtype, operand)); \ |
|
575 | 192 |
} |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
193 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
194 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
195 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
196 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
197 |
|
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
198 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
199 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
200 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
201 |
/*** convert string to numerical value ***/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
202 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
203 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
204 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
205 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
206 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
207 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
208 |
/* To allow the compiler to be portable, we cannot assume that int64_t is mapped onto long long int, |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
209 |
* so we cannot call strtoll() and strtoull() in extract_int64() and extract_uint64(). |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
210 |
* |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
211 |
* So, we create our own strtouint64() and strtoint64() functions. |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
212 |
* (We actually call them matiec_strtoint64() so they will not clash with any function |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
213 |
* that may be added to the standard library in the future). |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
214 |
* We actually create several of each, and let the compiler choose which is the correct one, |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
215 |
* by having it resolve the call to the overloaded function. For the C++ compiler to be able |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
216 |
* to resolve this ambiguity, we need to add a dummy parameter to each function! |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
217 |
* |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
218 |
* TODO: support platforms in which int64_t is mapped onto int !! Is this really needed? |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
219 |
*/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
220 |
static int64_t matiec_strtoint64 ( long int *dummy, const char *nptr, char **endptr, int base) {return strtol (nptr, endptr, base);} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
221 |
static int64_t matiec_strtoint64 ( long long int *dummy, const char *nptr, char **endptr, int base) {return strtoll (nptr, endptr, base);} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
222 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
223 |
static uint64_t matiec_strtouint64(unsigned long int *dummy, const char *nptr, char **endptr, int base) {return strtoul (nptr, endptr, base);} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
224 |
static uint64_t matiec_strtouint64(unsigned long long int *dummy, const char *nptr, char **endptr, int base) {return strtoull(nptr, endptr, base);} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
225 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
226 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
227 |
/* extract the value of an integer from an integer_c object !! */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
228 |
/* NOTE: it must ignore underscores! */ |
601
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
229 |
/* NOTE: To follow the basic structure used throughout the compiler's code, we should really be |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
230 |
* writing this as a visitor_c (and do away with the dynamic casts!), but since we only have 3 distinct |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
231 |
* symbol class types to handle, it is probably easier to read if we write it as a standard function... |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
232 |
*/ |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
233 |
int64_t extract_int64_value(symbol_c *sym, bool *overflow) { |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
234 |
int64_t ret; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
235 |
std::string str = ""; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
236 |
char *endptr; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
237 |
const char *value; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
238 |
int base; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
239 |
integer_c *integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
240 |
hex_integer_c *hex_integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
241 |
octal_integer_c *octal_integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
242 |
binary_integer_c *binary_integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
243 |
|
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
244 |
if ((integer = dynamic_cast<integer_c *>(sym)) != NULL) {value = integer ->value + 0; base = 10;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
245 |
else if ((hex_integer = dynamic_cast<hex_integer_c *>(sym)) != NULL) {value = hex_integer ->value + 3; base = 16;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
246 |
else if ((octal_integer = dynamic_cast<octal_integer_c *>(sym)) != NULL) {value = octal_integer ->value + 2; base = 8;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
247 |
else if ((binary_integer = dynamic_cast<binary_integer_c *>(sym)) != NULL) {value = binary_integer->value + 2; base = 2;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
248 |
else ERROR; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
249 |
|
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
250 |
for(unsigned int i = 0; i < strlen(value); i++) |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
251 |
if (value[i] != '_') str += value[i]; |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
252 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
253 |
errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly! |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
254 |
ret = matiec_strtoint64((int64_t *)NULL, str.c_str(), &endptr, base); |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
255 |
if (overflow != NULL) |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
256 |
*overflow = (errno == ERANGE); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
257 |
if (((errno != 0) && (errno != ERANGE)) || (*endptr != '\0')) |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
258 |
ERROR; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
259 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
260 |
return ret; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
261 |
} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
262 |
|
601
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
263 |
|
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
264 |
|
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
265 |
uint64_t extract_uint64_value(symbol_c *sym, bool *overflow) { |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
266 |
uint64_t ret; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
267 |
std::string str = ""; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
268 |
char *endptr; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
269 |
const char *value; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
270 |
int base; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
271 |
integer_c *integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
272 |
hex_integer_c *hex_integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
273 |
octal_integer_c *octal_integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
274 |
binary_integer_c *binary_integer; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
275 |
|
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
276 |
if ((integer = dynamic_cast<integer_c *>(sym)) != NULL) {value = integer ->value + 0; base = 10;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
277 |
else if ((hex_integer = dynamic_cast<hex_integer_c *>(sym)) != NULL) {value = hex_integer ->value + 3; base = 16;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
278 |
else if ((octal_integer = dynamic_cast<octal_integer_c *>(sym)) != NULL) {value = octal_integer ->value + 2; base = 8;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
279 |
else if ((binary_integer = dynamic_cast<binary_integer_c *>(sym)) != NULL) {value = binary_integer->value + 2; base = 2;} |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
280 |
else ERROR; |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
281 |
|
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
282 |
for(unsigned int i = 0; i < strlen(value); i++) |
683ae5444237
fix parsing of non base 10 constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
600
diff
changeset
|
283 |
if (value[i] != '_') str += value[i]; |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
284 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
285 |
errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly! |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
286 |
ret = matiec_strtouint64((uint64_t *)NULL, str.c_str(), &endptr, base); |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
287 |
if (overflow != NULL) |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
288 |
*overflow = (errno == ERANGE); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
289 |
if (((errno != 0) && (errno != ERANGE)) || (*endptr != '\0')) |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
290 |
ERROR; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
291 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
292 |
return ret; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
293 |
} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
294 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
295 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
296 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
297 |
/* extract the value of a real from an real_c object !! */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
298 |
/* NOTE: it must ignore underscores! */ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
299 |
/* From iec_bison.yy |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
300 |
* real: |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
301 |
* real_token {$$ = new real_c($1, locloc(@$));} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
302 |
* | fixed_point_token {$$ = new real_c($1, locloc(@$));} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
303 |
* |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
304 |
* From iec_flex.ll |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
305 |
* {real} {yylval.ID=strdup(yytext); return real_token;} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
306 |
* {fixed_point} {yylval.ID=strdup(yytext); return fixed_point_token;} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
307 |
* |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
308 |
* real {integer}\.{integer}{exponent} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
309 |
* fixed_point {integer}\.{integer} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
310 |
* exponent [Ee]([+-]?){integer} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
311 |
* integer {digit}((_?{digit})*) |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
312 |
*/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
313 |
real64_t extract_real_value(symbol_c *sym, bool *overflow) { |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
314 |
std::string str = ""; |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
315 |
real_c *real_sym; |
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
316 |
char *endptr; |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
317 |
real64_t ret; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
318 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
319 |
if ((real_sym = dynamic_cast<real_c *>(sym)) == NULL) ERROR; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
320 |
for(unsigned int i = 0; i < strlen(real_sym->value); i++) |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
321 |
if (real_sym->value[i] != '_') str += real_sym->value[i]; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
322 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
323 |
errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly! |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
324 |
#if (real64_t == float) |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
325 |
ret = strtof(str.c_str(), &endptr); |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
326 |
#elif (real64_t == double) |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
327 |
ret = strtod(str.c_str(), &endptr); |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
328 |
#elif (real64_t == long_double) |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
329 |
ret = strtold(str.c_str(), &endptr); |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
330 |
#else |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
331 |
#error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting! |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
332 |
#endif |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
333 |
if (overflow != NULL) |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
334 |
*overflow = (errno == ERANGE); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
335 |
if (((errno != 0) && (errno != ERANGE)) || (*endptr != '\0')) |
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
336 |
ERROR; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
337 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
338 |
return ret; |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
339 |
} |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
340 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
341 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
342 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
343 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
344 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
345 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
346 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
347 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
348 |
/*** Functions to check for overflow situation ***/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
349 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
350 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
351 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
352 |
|
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
353 |
|
574
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
354 |
/* NOTE: |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
355 |
* 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
|
356 |
* 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
|
357 |
* 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
|
358 |
*/ |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
359 |
|
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
360 |
/* 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
|
361 |
* 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
|
362 |
* |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
363 |
* 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
|
364 |
*/ |
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
365 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
366 |
/* 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
|
367 |
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
|
368 |
if (!VALID_CVALUE(uint64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
369 |
return; |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
370 |
/* 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
|
371 |
// 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
|
372 |
/* 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
|
373 |
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
|
374 |
SET_OVFLOW(uint64, res); |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
375 |
} |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
376 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
377 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
378 |
/* 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
|
379 |
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
|
380 |
if (!VALID_CVALUE(uint64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
381 |
return; |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
382 |
/* 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
|
383 |
// 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
|
384 |
/* Test by pre-condition: if b > a => overflow! */ |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
385 |
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
|
386 |
SET_OVFLOW(uint64, res); |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
387 |
} |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
388 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
389 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
390 |
/* 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
|
391 |
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
|
392 |
if (!VALID_CVALUE(uint64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
393 |
return; |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
394 |
/* 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
|
395 |
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
|
396 |
SET_OVFLOW(uint64, res); |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
397 |
} |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
398 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
399 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
400 |
/* 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
|
401 |
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
|
402 |
if (!VALID_CVALUE(uint64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
403 |
return; |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
404 |
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
|
405 |
SET_OVFLOW(uint64, res); |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
406 |
} |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
407 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
408 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
409 |
/* 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
|
410 |
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
|
411 |
if (!VALID_CVALUE(uint64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
412 |
return; |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
413 |
/* 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
|
414 |
if (false) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
415 |
SET_OVFLOW(uint64, res); |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
416 |
} |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
417 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
418 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
419 |
/* 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
|
420 |
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
|
421 |
if (!VALID_CVALUE(int64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
422 |
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
|
423 |
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
|
424 |
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
|
425 |
/* 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
|
426 |
if (((b > 0) && (a > (INT64_MAX - b))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
427 |
|| ((b < 0) && (a < (INT64_MIN - b)))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
428 |
SET_OVFLOW(int64, res); |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
429 |
} |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
430 |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
431 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
432 |
/* 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
|
433 |
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
|
434 |
if (!VALID_CVALUE(int64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
435 |
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
|
436 |
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
|
437 |
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
|
438 |
/* 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
|
439 |
if (((b > 0) && (a < (INT64_MIN + b))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
440 |
|| ((b < 0) && (a > (INT64_MAX + b)))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
441 |
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
|
442 |
} |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
443 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
444 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
445 |
/* res = a * b */ |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
446 |
static void CHECK_OVERFLOW_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
|
447 |
if (!VALID_CVALUE(int64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
448 |
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
|
449 |
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
|
450 |
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
|
451 |
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
|
452 |
|| ( (a > 0) && !(b > 0) && (b < (INT64_MIN / a))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
453 |
|| (!(a > 0) && (b > 0) && (a < (INT64_MIN / b))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
454 |
|| (!(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
|
455 |
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
|
456 |
} |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
457 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
458 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
459 |
/* 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
|
460 |
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
|
461 |
if (!VALID_CVALUE(int64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
462 |
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
|
463 |
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
|
464 |
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
|
465 |
if ((b == 0) || ((a == INT64_MIN) && (b == -1))) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
466 |
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
|
467 |
} |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
468 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
469 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
470 |
/* 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
|
471 |
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
|
472 |
if (!VALID_CVALUE(int64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
473 |
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
|
474 |
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
|
475 |
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
|
476 |
/* 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
|
477 |
* 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
|
478 |
* |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
479 |
* 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
|
480 |
* 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
|
481 |
* |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
482 |
* 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
|
483 |
*/ |
574
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
484 |
if ((a == INT64_MIN) && (b == -1)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
485 |
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
|
486 |
} |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
487 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
488 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
489 |
/* 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
|
490 |
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
|
491 |
if (!VALID_CVALUE(int64, res)) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
492 |
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
|
493 |
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
|
494 |
if (a == INT64_MIN) |
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
495 |
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
|
496 |
} |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
497 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
498 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
499 |
|
579
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
500 |
static void CHECK_OVERFLOW_real64(symbol_c *res_ptr) { |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
501 |
if (!VALID_CVALUE(real64, res_ptr)) |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
502 |
return; |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
503 |
real64_t res = GET_CVALUE(real64, res_ptr); |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
504 |
/* NaN => underflow, overflow, number is a higher precision format, is a complex number (IEEE standard) */ |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
505 |
/* The IEC 61131-3 clearly states in section '2.5.1.5.2 Numerical functions': |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
506 |
* "It is an error if the result of evaluation of one of these [numerical] functions exceeds the range of values |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
507 |
* specified for the data type of the function output, or if division by zero is attempted." |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
508 |
* For this reason, any operation that has as a result a positive or negative inifinity, is also an error! |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
509 |
*/ |
607
be9ba3531afb
cleaning up code. Changing HUGE_VAL to INFINITY.
Mario de Sousa <msousa@fe.up.pt>
parents:
604
diff
changeset
|
510 |
if ((isnan(res)) || (res == INFINITY) || (res == -INFINITY)) |
579
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
511 |
SET_OVFLOW(real64, res_ptr); |
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
512 |
} |
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
513 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
514 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
515 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
516 |
|
612 | 517 |
/***********************************************************************/ |
518 |
/***********************************************************************/ |
|
519 |
/***********************************************************************/ |
|
520 |
/*** Functions to execute operations on the const values ***/ |
|
521 |
/***********************************************************************/ |
|
522 |
/***********************************************************************/ |
|
523 |
/***********************************************************************/ |
|
524 |
||
525 |
/* static void *handle_cmp(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2, OPERATION) */ |
|
526 |
#define handle_cmp(symbol, oper1, oper2, operation) { \ |
|
527 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; \ |
|
528 |
DO_BINARY_OPER_( bool, operation, bool, oper1, oper2); \ |
|
529 |
DO_BINARY_OPER_(uint64, operation, bool, oper1, oper2); \ |
|
530 |
DO_BINARY_OPER_( int64, operation, bool, oper1, oper2); \ |
|
531 |
DO_BINARY_OPER_(real64, operation, bool, oper1, oper2); \ |
|
532 |
return NULL; \ |
|
533 |
} |
|
534 |
||
535 |
||
536 |
/* NOTE: the MOVE standard function is equivalent to the ':=' in ST syntax */ |
|
537 |
static void *handle_move(symbol_c *to, symbol_c *from) { |
|
538 |
if (NULL == from) return NULL; |
|
539 |
to->const_value = from->const_value; |
|
540 |
return NULL; |
|
541 |
} |
|
542 |
||
543 |
||
544 |
/* unary negation (multiply by -1) */ |
|
545 |
static void *handle_neg(symbol_c *symbol, symbol_c *oper) { |
|
546 |
DO_UNARY_OPER( int64, -, oper); CHECK_OVERFLOW_int64_NEG(symbol, oper); |
|
547 |
DO_UNARY_OPER(real64, -, oper); CHECK_OVERFLOW_real64(symbol); |
|
548 |
return NULL; |
|
549 |
} |
|
550 |
||
551 |
||
552 |
/* unary boolean negation (NOT) */ |
|
553 |
static void *handle_not(symbol_c *symbol, symbol_c *oper) { |
|
554 |
if (NULL == oper) return NULL; |
|
555 |
DO_UNARY_OPER( bool, !, oper); |
|
556 |
DO_UNARY_OPER(uint64, ~, oper); |
|
557 |
return NULL; |
|
558 |
} |
|
559 |
||
560 |
||
561 |
static void *handle_or (symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
562 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
563 |
DO_BINARY_OPER_( bool, ||, bool , oper1, oper2); |
|
564 |
DO_BINARY_OPER_(uint64, | , uint64, oper1, oper2); |
|
565 |
return NULL; |
|
566 |
} |
|
567 |
||
568 |
||
569 |
static void *handle_xor(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
570 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
571 |
DO_BINARY_OPER_( bool, ^, bool , oper1, oper2); |
|
572 |
DO_BINARY_OPER_(uint64, ^, uint64, oper1, oper2); |
|
573 |
return NULL; |
|
574 |
} |
|
575 |
||
576 |
||
577 |
static void *handle_and(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
578 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
579 |
DO_BINARY_OPER_( bool, &&, bool, oper1, oper2); |
|
580 |
DO_BINARY_OPER_(uint64, & , uint64, oper1, oper2); |
|
581 |
return NULL; |
|
582 |
} |
|
583 |
||
584 |
||
585 |
static void *handle_add(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
586 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
587 |
DO_BINARY_OPER_(uint64, +, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_SUM(symbol, oper1, oper2); |
|
588 |
DO_BINARY_OPER_( int64, +, int64, oper1, oper2); CHECK_OVERFLOW_int64_SUM (symbol, oper1, oper2); |
|
589 |
DO_BINARY_OPER_(real64, +, real64, oper1, oper2); CHECK_OVERFLOW_real64 (symbol); |
|
590 |
return NULL; |
|
591 |
} |
|
592 |
||
593 |
||
594 |
static void *handle_sub(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
595 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
596 |
DO_BINARY_OPER_(uint64, -, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_SUB(symbol, oper1, oper2); |
|
597 |
DO_BINARY_OPER_( int64, -, int64, oper1, oper2); CHECK_OVERFLOW_int64_SUB (symbol, oper1, oper2); |
|
598 |
DO_BINARY_OPER_(real64, -, real64, oper1, oper2); CHECK_OVERFLOW_real64 (symbol); |
|
599 |
return NULL; |
|
600 |
} |
|
601 |
||
602 |
||
603 |
static void *handle_mul(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
604 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
605 |
DO_BINARY_OPER_(uint64, *, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_MUL(symbol, oper1, oper2); |
|
606 |
DO_BINARY_OPER_( int64, *, int64, oper1, oper2); CHECK_OVERFLOW_int64_MUL (symbol, oper1, oper2); |
|
607 |
DO_BINARY_OPER_(real64, *, real64, oper1, oper2); CHECK_OVERFLOW_real64 (symbol); |
|
608 |
return NULL; |
|
609 |
} |
|
610 |
||
611 |
||
612 |
static void *handle_div(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
613 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
614 |
if (ISZERO_CVALUE(uint64, oper2)) {SET_OVFLOW(uint64, symbol);} else {DO_BINARY_OPER_(uint64, /, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_DIV(symbol, oper1, oper2);}; |
|
615 |
if (ISZERO_CVALUE( int64, oper2)) {SET_OVFLOW( int64, symbol);} else {DO_BINARY_OPER_( int64, /, int64, oper1, oper2); CHECK_OVERFLOW_int64_DIV (symbol, oper1, oper2);}; |
|
616 |
if (ISZERO_CVALUE(real64, oper2)) {SET_OVFLOW(real64, symbol);} else {DO_BINARY_OPER_(real64, /, real64, oper1, oper2); CHECK_OVERFLOW_real64(symbol);}; |
|
617 |
return NULL; |
|
618 |
} |
|
619 |
||
620 |
||
621 |
static void *handle_mod(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
622 |
if ((NULL == oper1) || (NULL == oper2)) return NULL; |
|
623 |
/* IEC 61131-3 standard says IN1 MOD IN2 must be equivalent to |
|
624 |
* IF (IN2 = 0) THEN OUT:=0 ; ELSE OUT:=IN1 - (IN1/IN2)*IN2 ; END_IF |
|
625 |
* |
|
626 |
* Note that, when IN1 = INT64_MIN, and IN2 = -1, an overflow occurs in the division, |
|
627 |
* so although the MOD operation should be OK, acording to the above definition, we actually have an overflow!! |
|
628 |
*/ |
|
629 |
if (ISZERO_CVALUE(uint64, oper2)) {SET_CVALUE(uint64, symbol, 0);} else {DO_BINARY_OPER_(uint64, %, uint64, oper1, oper2); CHECK_OVERFLOW_uint64_MOD(symbol, oper1, oper2);}; |
|
630 |
if (ISZERO_CVALUE( int64, oper2)) {SET_CVALUE( int64, symbol, 0);} else {DO_BINARY_OPER_( int64, %, int64, oper1, oper2); CHECK_OVERFLOW_int64_MOD (symbol, oper1, oper2);}; |
|
631 |
return NULL; |
|
632 |
} |
|
633 |
||
634 |
||
635 |
static void *handle_pow(symbol_c *symbol, symbol_c *oper1, symbol_c *oper2) { |
|
636 |
/* NOTE: If the const_value in symbol->r_exp is within the limits of both int64 and uint64, then we do both operations. |
|
637 |
* That is OK, as the result should be identicial (we do create an unnecessary CVALUE variable, but who cares?). |
|
638 |
* If only one is valid, then that is the oper we will do! |
|
639 |
*/ |
|
640 |
if (VALID_CVALUE(real64, oper1) && VALID_CVALUE( int64, oper2)) |
|
641 |
SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE( int64, oper2))); |
|
642 |
if (VALID_CVALUE(real64, oper1) && VALID_CVALUE(uint64, oper2)) |
|
643 |
SET_CVALUE(real64, symbol, pow(GET_CVALUE(real64, oper1), GET_CVALUE(uint64, oper2))); |
|
644 |
CHECK_OVERFLOW_real64(symbol); |
|
645 |
return NULL; |
|
646 |
} |
|
647 |
||
648 |
/***********************************************************************/ |
|
649 |
/***********************************************************************/ |
|
650 |
/***********************************************************************/ |
|
651 |
/*** Helper functions for handling IL instruction lists. ***/ |
|
652 |
/***********************************************************************/ |
|
653 |
/***********************************************************************/ |
|
654 |
/***********************************************************************/ |
|
655 |
||
656 |
||
657 |
/* If the cvalues of all the prev_il_intructions have the same VALID value, then set the local cvalue to that value, otherwise, set it to NONCONST! */ |
|
658 |
#define intersect_prev_CVALUE_(dtype, symbol) { \ |
|
659 |
symbol->const_value._##dtype = symbol->prev_il_instruction[0]->const_value._##dtype; \ |
|
660 |
for (unsigned int i = 1; i < symbol->prev_il_instruction.size(); i++) { \ |
|
661 |
if (!ISEQUAL_CVALUE(dtype, symbol, symbol->prev_il_instruction[i])) \ |
|
662 |
{SET_NONCONST(dtype, symbol); break;} \ |
|
663 |
} \ |
|
664 |
} |
|
665 |
||
666 |
static void intersect_prev_cvalues(il_instruction_c *symbol) { |
|
667 |
if (symbol->prev_il_instruction.empty()) |
|
668 |
return; |
|
669 |
intersect_prev_CVALUE_(real64, symbol); |
|
670 |
intersect_prev_CVALUE_(uint64, symbol); |
|
671 |
intersect_prev_CVALUE_( int64, symbol); |
|
672 |
intersect_prev_CVALUE_( bool, symbol); |
|
673 |
} |
|
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
674 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
675 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
676 |
|
596
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
677 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
678 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
679 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
680 |
/*** The constant_folding_c ***/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
681 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
682 |
/***********************************************************************/ |
4efb11e44065
Add ERROR_MSG macro && move extract_XXX() functions to constant_folding.cc
Mario de Sousa <msousa@fe.up.pt>
parents:
592
diff
changeset
|
683 |
/***********************************************************************/ |
572
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
684 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
685 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
686 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
687 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
688 |
|
c353bc67bf91
Correctly implement MOD operation and overflow checks (still missing uint * / MOD).
Mario de Sousa <msousa@fe.up.pt>
parents:
570
diff
changeset
|
689 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
690 |
constant_folding_c::constant_folding_c(symbol_c *symbol) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
691 |
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
|
692 |
warning_found = false; |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
693 |
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
|
694 |
|
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
|
695 |
/* 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
|
696 |
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
|
697 |
if (! (std::numeric_limits<real64_t>::is_iec559) ) |
579
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
698 |
STAGE3_WARNING(&null_symbol, &null_symbol, "The platform running the compiler does not implement IEC 60559 floating point numbers. " |
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
|
699 |
"Any error and/or warning messages related to overflow/underflow of the result of operations on REAL/LREAL literals " |
579
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
700 |
"(i.e. constant folding) may themselves be erroneous, although are most probably correct." |
983a3b743085
Fix 'overflow' detection for real64.
Mario de Sousa <msousa@fe.up.pt>
parents:
578
diff
changeset
|
701 |
"However, more likely is the possible existance of overflow/underflow errors that are not detected."); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
702 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
703 |
|
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
|
704 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
705 |
constant_folding_c::~constant_folding_c(void) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
706 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
707 |
|
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
|
708 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
709 |
int constant_folding_c::get_error_count() { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
710 |
return error_count; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
711 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
712 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
713 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
714 |
/*********************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
715 |
/* B 1.2 - Constants */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
716 |
/*********************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
717 |
/******************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
718 |
/* B 1.2.1 - Numeric Literals */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
719 |
/******************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
720 |
void *constant_folding_c::visit(real_c *symbol) { |
576
8368ec909825
Fix extract_real (thanks Manuele), and add check for overflow.
Mario de Sousa <msousa@fe.up.pt>
parents:
575
diff
changeset
|
721 |
bool overflow; |
612 | 722 |
SET_CVALUE(real64, symbol, extract_real_value(symbol, &overflow)); |
576
8368ec909825
Fix extract_real (thanks Manuele), and add check for overflow.
Mario de Sousa <msousa@fe.up.pt>
parents:
575
diff
changeset
|
723 |
if (overflow) SET_OVFLOW(real64, 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
|
724 |
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
|
725 |
} |
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
566
diff
changeset
|
726 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
727 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
728 |
void *constant_folding_c::visit(integer_c *symbol) { |
587
1ecf916cc397
Fix extract_integer_value.
Manuele Conti <conti.ma@alice.it>
parents:
579
diff
changeset
|
729 |
bool overflow; |
612 | 730 |
SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow)); |
588
3d72d09bd40f
Add missing set overflow flag.
Manuele Conti <conti.ma@alice.it>
parents:
587
diff
changeset
|
731 |
if (overflow) SET_OVFLOW(int64, symbol); |
612 | 732 |
SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow)); |
588
3d72d09bd40f
Add missing set overflow flag.
Manuele Conti <conti.ma@alice.it>
parents:
587
diff
changeset
|
733 |
if (overflow) SET_OVFLOW(uint64, symbol); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
734 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
735 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
736 |
|
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
|
737 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
738 |
void *constant_folding_c::visit(neg_real_c *symbol) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
739 |
symbol->exp->accept(*this); |
575 | 740 |
DO_UNARY_OPER(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
|
741 |
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
|
742 |
return NULL; |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
743 |
} |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
744 |
|
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
745 |
/* | '-' integer {$$ = new neg_integer_c($2, locloc(@$));} */ |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
746 |
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
|
747 |
symbol->exp->accept(*this); |
575 | 748 |
DO_UNARY_OPER(int64, -, symbol->exp); |
574
d291a942899b
Fix remaining overflow checks.
Mario de Sousa <msousa@fe.up.pt>
parents:
573
diff
changeset
|
749 |
CHECK_OVERFLOW_int64_NEG(symbol, symbol->exp); |
590
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
750 |
/* NOTE 1: INT64_MIN = -(INT64_MAX + 1) ---> assuming two's complement representation!!! |
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
751 |
* NOTE 2: if the user happens to want INT_MIN, that value will first be parsed as a positive integer, before being negated here. |
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
752 |
* However, the positive value cannot be stored inside an int64! So, in this case, we will get the value from the uint64 cvalue. |
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
753 |
*/ |
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
754 |
// if (INT64_MIN == -INT64_MAX - 1) // We do not really need to check that the platform uses two's complement |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
755 |
if (VALID_CVALUE(uint64, symbol->exp) && (GET_CVALUE(uint64, symbol->exp) == -INT64_MIN)) { // How do we stop the compiler from complaining about a comparison between int and unsigned int? |
590
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
756 |
SET_CVALUE(int64, symbol, INT64_MIN); |
649667749171
Add support for -INT64_MIN values in literals.
Mario de Sousa <msousa@fe.up.pt>
parents:
588
diff
changeset
|
757 |
} |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
758 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
759 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
760 |
|
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
|
761 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
762 |
void *constant_folding_c::visit(binary_integer_c *symbol) { |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
763 |
bool overflow; |
612 | 764 |
SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow)); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
765 |
if (overflow) SET_OVFLOW(int64, symbol); |
612 | 766 |
SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow)); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
767 |
if (overflow) SET_OVFLOW(uint64, symbol); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
768 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
769 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
770 |
|
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
|
771 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
772 |
void *constant_folding_c::visit(octal_integer_c *symbol) { |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
773 |
bool overflow; |
612 | 774 |
SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow)); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
775 |
if (overflow) SET_OVFLOW(int64, symbol); |
612 | 776 |
SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow)); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
777 |
if (overflow) SET_OVFLOW(uint64, symbol); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
778 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
779 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
780 |
|
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
|
781 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
782 |
void *constant_folding_c::visit(hex_integer_c *symbol) { |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
783 |
bool overflow; |
612 | 784 |
SET_CVALUE( int64, symbol, extract_int64_value (symbol, &overflow)); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
785 |
if (overflow) SET_OVFLOW(int64, symbol); |
612 | 786 |
SET_CVALUE(uint64, symbol, extract_uint64_value(symbol, &overflow)); |
600
f5d4e9f91513
determine const value of hex, octal and bin literals correctly.
Mario de Sousa <msousa@fe.up.pt>
parents:
596
diff
changeset
|
787 |
if (overflow) SET_OVFLOW(uint64, symbol); |
569
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
788 |
return NULL; |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
789 |
} |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
790 |
|
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
791 |
|
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
792 |
/* |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
793 |
integer_literal: |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
794 |
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
|
795 |
| 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
|
796 |
| 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
|
797 |
| 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
|
798 |
*/ |
0d1ab9e78574
Add data structure for storing overflow of constant values.
Mario de Sousa <msousa@fe.up.pt>
parents:
568
diff
changeset
|
799 |
// SYM_REF2(integer_literal_c, type, value) |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
800 |
void *constant_folding_c::visit(integer_literal_c *symbol) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
801 |
symbol->value->accept(*this); |
575 | 802 |
DO_UNARY_OPER( int64, /* none */, symbol->value); |
803 |
DO_UNARY_OPER(uint64, /* none */, symbol->value); |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
804 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
805 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
806 |
|
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
|
807 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
808 |
void *constant_folding_c::visit(real_literal_c *symbol) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
809 |
symbol->value->accept(*this); |
575 | 810 |
DO_UNARY_OPER(real64, /* none */, symbol->value); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
811 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
812 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
813 |
|
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
|
814 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
815 |
void *constant_folding_c::visit(bit_string_literal_c *symbol) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
816 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
817 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
818 |
|
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
|
819 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
820 |
void *constant_folding_c::visit(boolean_literal_c *symbol) { |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
821 |
symbol->value->accept(*this); |
575 | 822 |
DO_UNARY_OPER(bool, /* none */, symbol->value); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
823 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
824 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
825 |
|
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
|
826 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
827 |
void *constant_folding_c::visit(boolean_true_c *symbol) { |
612 | 828 |
SET_CVALUE(bool, symbol, true); |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
829 |
return NULL; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
830 |
} |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
831 |
|
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
|
832 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
833 |
void *constant_folding_c::visit(boolean_false_c *symbol) { |
612 | 834 |
SET_CVALUE(bool, symbol, false); |
835 |
return NULL; |
|
836 |
} |
|
837 |
||
838 |
||
839 |
||
840 |
||
841 |
||
842 |
/****************************************/ |
|
843 |
/* B.2 - Language IL (Instruction List) */ |
|
844 |
/****************************************/ |
|
845 |
/***********************************/ |
|
846 |
/* B 2.1 Instructions and Operands */ |
|
847 |
/***********************************/ |
|
848 |
/* Not needed, since we inherit from iterator_visitor_c */ |
|
849 |
/*| instruction_list il_instruction */ |
|
850 |
// SYM_LIST(instruction_list_c) |
|
851 |
// void *constant_folding_c::visit(instruction_list_c *symbol) {} |
|
852 |
||
853 |
/* | label ':' [il_incomplete_instruction] eol_list */ |
|
854 |
// SYM_REF2(il_instruction_c, label, il_instruction) |
|
855 |
// void *visit(instruction_list_c *symbol); |
|
856 |
void *constant_folding_c::visit(il_instruction_c *symbol) { |
|
857 |
if (NULL == symbol->il_instruction) { |
|
858 |
/* This empty/null il_instruction does not change the value of the current/default IL variable. |
|
859 |
* So it inherits the candidate_datatypes from it's previous IL instructions! |
|
860 |
*/ |
|
861 |
intersect_prev_cvalues(symbol); |
|
862 |
} else { |
|
863 |
il_instruction_c fake_prev_il_instruction = *symbol; |
|
864 |
intersect_prev_cvalues(&fake_prev_il_instruction); |
|
865 |
||
866 |
if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; |
|
867 |
else prev_il_instruction = &fake_prev_il_instruction; |
|
868 |
symbol->il_instruction->accept(*this); |
|
869 |
prev_il_instruction = NULL; |
|
870 |
||
871 |
/* This object has (inherits) the same cvalues as the il_instruction */ |
|
872 |
symbol->const_value = symbol->il_instruction->const_value; |
|
873 |
} |
|
874 |
||
875 |
return NULL; |
|
876 |
} |
|
877 |
||
878 |
||
879 |
void *constant_folding_c::visit(il_simple_operation_c *symbol) { |
|
880 |
/* determine the cvalue of the operand */ |
|
881 |
if (NULL != symbol->il_operand) { |
|
882 |
symbol->il_operand->accept(*this); |
|
883 |
} |
|
884 |
/* determine the cvalue resulting from executing the il_operator... */ |
|
885 |
il_operand = symbol->il_operand; |
|
886 |
symbol->il_simple_operator->accept(*this); |
|
887 |
il_operand = NULL; |
|
888 |
/* This object has (inherits) the same cvalues as the il_instruction */ |
|
889 |
symbol->const_value = symbol->il_simple_operator->const_value; |
|
890 |
return NULL; |
|
891 |
} |
|
892 |
||
893 |
||
894 |
/* TODO: handle function invocations... */ |
|
895 |
/* | function_name [il_operand_list] */ |
|
896 |
/* NOTE: The parameters 'called_function_declaration' and 'extensible_param_count' are used to pass data between the stage 3 and stage 4. */ |
|
897 |
// SYM_REF2(il_function_call_c, function_name, il_operand_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
898 |
// void *constant_folding_c::visit(il_function_call_c *symbol) {} |
|
899 |
||
900 |
||
901 |
/* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ |
|
902 |
// SYM_REF3(il_expression_c, il_expr_operator, il_operand, simple_instr_list); |
|
903 |
void *constant_folding_c::visit(il_expression_c *symbol) { |
|
904 |
symbol_c *prev_il_instruction_backup = prev_il_instruction; |
|
905 |
||
906 |
if (NULL != symbol->il_operand) |
|
907 |
symbol->il_operand->accept(*this); |
|
908 |
||
909 |
if(symbol->simple_instr_list != NULL) |
|
910 |
symbol->simple_instr_list->accept(*this); |
|
911 |
||
912 |
/* Now do the operation, */ |
|
913 |
il_operand = symbol->simple_instr_list; |
|
914 |
prev_il_instruction = prev_il_instruction_backup; |
|
915 |
symbol->il_expr_operator->accept(*this); |
|
916 |
il_operand = NULL; |
|
917 |
||
918 |
/* This object has (inherits) the same cvalues as the il_instruction */ |
|
919 |
symbol->const_value = symbol->il_expr_operator->const_value; |
|
920 |
return NULL; |
|
921 |
} |
|
922 |
||
923 |
||
924 |
||
925 |
void *constant_folding_c::visit(il_jump_operation_c *symbol) { |
|
926 |
/* recursive call to fill const values... */ |
|
927 |
il_operand = NULL; |
|
928 |
symbol->il_jump_operator->accept(*this); |
|
929 |
il_operand = NULL; |
|
930 |
/* This object has (inherits) the same cvalues as the il_jump_operator */ |
|
931 |
symbol->const_value = symbol->il_jump_operator->const_value; |
|
932 |
return NULL; |
|
933 |
} |
|
934 |
||
935 |
||
936 |
||
937 |
/* FB calls leave the value in the accumulator unchanged */ |
|
938 |
/* il_call_operator prev_declared_fb_name |
|
939 |
* | il_call_operator prev_declared_fb_name '(' ')' |
|
940 |
* | il_call_operator prev_declared_fb_name '(' eol_list ')' |
|
941 |
* | il_call_operator prev_declared_fb_name '(' il_operand_list ')' |
|
942 |
* | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' |
|
943 |
*/ |
|
944 |
/* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ |
|
945 |
// SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list, symbol_c *called_fb_declaration) |
|
946 |
void *constant_folding_c::visit(il_fb_call_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
947 |
||
948 |
||
949 |
/* TODO: handle function invocations... */ |
|
950 |
/* | function_name '(' eol_list [il_param_list] ')' */ |
|
951 |
/* NOTE: The parameter 'called_function_declaration' is used to pass data between the stage 3 and stage 4. */ |
|
952 |
// SYM_REF2(il_formal_funct_call_c, function_name, il_param_list, symbol_c *called_function_declaration; int extensible_param_count;) |
|
953 |
// void *constant_folding_c::visit(il_formal_funct_call_c *symbol) {return NULL;} |
|
954 |
||
955 |
||
956 |
||
957 |
/* Not needed, since we inherit from iterator_visitor_c */ |
|
958 |
// void *constant_folding_c::visit(il_operand_list_c *symbol); |
|
959 |
||
960 |
||
961 |
||
962 |
/* | simple_instr_list il_simple_instruction */ |
|
963 |
/* This object is referenced by il_expression_c objects */ |
|
964 |
void *constant_folding_c::visit(simple_instr_list_c *symbol) { |
|
965 |
if (symbol->n <= 0) |
|
966 |
return NULL; /* List is empty! Nothing to do. */ |
|
967 |
||
968 |
for(int i = 0; i < symbol->n; i++) |
|
969 |
symbol->elements[i]->accept(*this); |
|
970 |
||
971 |
/* This object has (inherits) the same cvalues as the il_jump_operator */ |
|
972 |
symbol->const_value = symbol->elements[symbol->n-1]->const_value; |
|
973 |
return NULL; |
|
974 |
} |
|
975 |
||
976 |
||
977 |
||
978 |
// SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) |
|
979 |
void *constant_folding_c::visit(il_simple_instruction_c *symbol) { |
|
980 |
if (symbol->prev_il_instruction.size() > 1) ERROR; /* There should be no labeled insructions inside an IL expression! */ |
|
981 |
if (symbol->prev_il_instruction.size() == 0) prev_il_instruction = NULL; |
|
982 |
else prev_il_instruction = symbol->prev_il_instruction[0]; |
|
983 |
symbol->il_simple_instruction->accept(*this); |
|
984 |
prev_il_instruction = NULL; |
|
985 |
||
986 |
/* This object has (inherits) the same cvalues as the il_jump_operator */ |
|
987 |
symbol->const_value = symbol->il_simple_instruction->const_value; |
|
988 |
return NULL; |
|
989 |
} |
|
990 |
||
991 |
||
992 |
/* |
|
993 |
void *visit(il_param_list_c *symbol); |
|
994 |
void *visit(il_param_assignment_c *symbol); |
|
995 |
void *visit(il_param_out_assignment_c *symbol); |
|
996 |
*/ |
|
997 |
||
998 |
||
999 |
/*******************/ |
|
1000 |
/* B 2.2 Operators */ |
|
1001 |
/*******************/ |
|
1002 |
void *constant_folding_c::visit( LD_operator_c *symbol) {return handle_move(symbol, il_operand);} |
|
1003 |
void *constant_folding_c::visit( LDN_operator_c *symbol) {return handle_not (symbol, il_operand);} |
|
1004 |
||
1005 |
/* NOTE: we are implementing a constant folding algorithm, not a constant propagation algorithm. |
|
1006 |
* For the constant propagation algorithm, the correct implementation of ST(N)_operator_c would be... |
|
1007 |
*/ |
|
1008 |
//void *constant_folding_c::visit( ST_operator_c *symbol) {return handle_move(il_operand, symbol);} |
|
1009 |
//void *constant_folding_c::visit( STN_operator_c *symbol) {return handle_not (il_operand, symbol);} |
|
1010 |
void *constant_folding_c::visit( ST_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1011 |
void *constant_folding_c::visit( STN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1012 |
||
1013 |
/* NOTE: the standard allows syntax in which the NOT operator is followed by an optional <il_operand> |
|
1014 |
* NOT [<il_operand>] |
|
1015 |
* However, it does not define the semantic of the NOT operation when the <il_operand> is specified. |
|
1016 |
* We therefore consider it an error if an il_operand is specified! This error will be caught elsewhere! |
|
1017 |
*/ |
|
1018 |
void *constant_folding_c::visit( NOT_operator_c *symbol) {return handle_not(symbol, prev_il_instruction);} |
|
1019 |
||
1020 |
/* NOTE: Since we are only implementing a constant folding algorithm, and not a constant propagation algorithm, |
|
1021 |
* the following IL instructions do not change/set the value of the il_operand! |
|
1022 |
*/ |
|
1023 |
void *constant_folding_c::visit( S_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1024 |
void *constant_folding_c::visit( R_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1025 |
||
1026 |
/* FB calls leave the value in the accumulator unchanged */ |
|
1027 |
void *constant_folding_c::visit( S1_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1028 |
void *constant_folding_c::visit( R1_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1029 |
void *constant_folding_c::visit( CLK_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1030 |
void *constant_folding_c::visit( CU_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1031 |
void *constant_folding_c::visit( CD_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1032 |
void *constant_folding_c::visit( PV_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1033 |
void *constant_folding_c::visit( IN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1034 |
void *constant_folding_c::visit( PT_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1035 |
||
1036 |
void *constant_folding_c::visit( AND_operator_c *symbol) {return handle_and (symbol, prev_il_instruction, il_operand);} |
|
1037 |
void *constant_folding_c::visit( OR_operator_c *symbol) {return handle_or (symbol, prev_il_instruction, il_operand);} |
|
1038 |
void *constant_folding_c::visit( XOR_operator_c *symbol) {return handle_xor (symbol, prev_il_instruction, il_operand);} |
|
1039 |
void *constant_folding_c::visit( ANDN_operator_c *symbol) { handle_and (symbol, prev_il_instruction, il_operand); return handle_not(symbol, symbol);} |
|
1040 |
void *constant_folding_c::visit( ORN_operator_c *symbol) { handle_or (symbol, prev_il_instruction, il_operand); return handle_not(symbol, symbol);} |
|
1041 |
void *constant_folding_c::visit( XORN_operator_c *symbol) { handle_xor (symbol, prev_il_instruction, il_operand); return handle_not(symbol, symbol);} |
|
1042 |
||
1043 |
void *constant_folding_c::visit( ADD_operator_c *symbol) {return handle_add (symbol, prev_il_instruction, il_operand);} |
|
1044 |
void *constant_folding_c::visit( SUB_operator_c *symbol) {return handle_sub (symbol, prev_il_instruction, il_operand);} |
|
1045 |
void *constant_folding_c::visit( MUL_operator_c *symbol) {return handle_mul (symbol, prev_il_instruction, il_operand);} |
|
1046 |
void *constant_folding_c::visit( DIV_operator_c *symbol) {return handle_div (symbol, prev_il_instruction, il_operand);} |
|
1047 |
void *constant_folding_c::visit( MOD_operator_c *symbol) {return handle_mod (symbol, prev_il_instruction, il_operand);} |
|
1048 |
||
1049 |
void *constant_folding_c::visit( GT_operator_c *symbol) { handle_cmp (symbol, prev_il_instruction, il_operand, > );} |
|
1050 |
void *constant_folding_c::visit( GE_operator_c *symbol) { handle_cmp (symbol, prev_il_instruction, il_operand, >=);} |
|
1051 |
void *constant_folding_c::visit( EQ_operator_c *symbol) { handle_cmp (symbol, prev_il_instruction, il_operand, ==);} |
|
1052 |
void *constant_folding_c::visit( LT_operator_c *symbol) { handle_cmp (symbol, prev_il_instruction, il_operand, < );} |
|
1053 |
void *constant_folding_c::visit( LE_operator_c *symbol) { handle_cmp (symbol, prev_il_instruction, il_operand, <=);} |
|
1054 |
void *constant_folding_c::visit( NE_operator_c *symbol) { handle_cmp (symbol, prev_il_instruction, il_operand, !=);} |
|
1055 |
||
1056 |
void *constant_folding_c::visit( CAL_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1057 |
void *constant_folding_c::visit( RET_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1058 |
void *constant_folding_c::visit( JMP_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1059 |
void *constant_folding_c::visit( CALC_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1060 |
void *constant_folding_c::visit(CALCN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1061 |
void *constant_folding_c::visit( RETC_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1062 |
void *constant_folding_c::visit(RETCN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1063 |
void *constant_folding_c::visit( JMPC_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1064 |
void *constant_folding_c::visit(JMPCN_operator_c *symbol) {return handle_move(symbol, prev_il_instruction);} |
|
1065 |
||
1066 |
||
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
|
1067 |
|
e5deeb6d4d2f
create extract_real_value() in absyntax_utils. NOTE: overflows not yet handled!
Mario de Sousa <msousa@fe.up.pt>
parents:
566
diff
changeset
|
1068 |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
1069 |
/***************************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
1070 |
/* B.3 - Language ST (Structured Text) */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
1071 |
/***************************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
1072 |
/***********************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
1073 |
/* B 3.1 - Expressions */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
1074 |
/***********************/ |
612 | 1075 |
void *constant_folding_c::visit( or_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_or (symbol, symbol->l_exp, symbol->r_exp);} |
1076 |
void *constant_folding_c::visit( xor_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_xor(symbol, symbol->l_exp, symbol->r_exp);} |
|
1077 |
void *constant_folding_c::visit( and_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_and(symbol, symbol->l_exp, symbol->r_exp);} |
|
1078 |
||
1079 |
void *constant_folding_c::visit( equ_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); handle_cmp (symbol, symbol->l_exp, symbol->r_exp, ==);} |
|
1080 |
void *constant_folding_c::visit(notequ_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); handle_cmp (symbol, symbol->l_exp, symbol->r_exp, !=);} |
|
1081 |
void *constant_folding_c::visit( lt_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); handle_cmp (symbol, symbol->l_exp, symbol->r_exp, < );} |
|
1082 |
void *constant_folding_c::visit( gt_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); handle_cmp (symbol, symbol->l_exp, symbol->r_exp, > );} |
|
1083 |
void *constant_folding_c::visit( le_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); handle_cmp (symbol, symbol->l_exp, symbol->r_exp, <=);} |
|
1084 |
void *constant_folding_c::visit( ge_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); handle_cmp (symbol, symbol->l_exp, symbol->r_exp, >=);} |
|
1085 |
||
1086 |
void *constant_folding_c::visit( add_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_add(symbol, symbol->l_exp, symbol->r_exp);} |
|
1087 |
void *constant_folding_c::visit( sub_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_sub(symbol, symbol->l_exp, symbol->r_exp);} |
|
1088 |
void *constant_folding_c::visit( mul_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_mul(symbol, symbol->l_exp, symbol->r_exp);} |
|
1089 |
void *constant_folding_c::visit( div_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_div(symbol, symbol->l_exp, symbol->r_exp);} |
|
1090 |
void *constant_folding_c::visit( mod_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_mod(symbol, symbol->l_exp, symbol->r_exp);} |
|
1091 |
void *constant_folding_c::visit( power_expression_c *symbol) {symbol->l_exp->accept(*this); symbol->r_exp->accept(*this); return handle_pow(symbol, symbol->l_exp, symbol->r_exp);} |
|
1092 |
||
1093 |
void *constant_folding_c::visit( neg_expression_c *symbol) {symbol-> exp->accept(*this); return handle_neg(symbol, symbol->exp);} |
|
1094 |
void *constant_folding_c::visit( not_expression_c *symbol) {symbol-> exp->accept(*this); return handle_not(symbol, symbol->exp);} |
|
1095 |
||
1096 |
/* TODO: handle function invocations... */ |
|
1097 |
// void *fill_candidate_datatypes_c::visit(function_invocation_c *symbol) {} |