author | Laurent Bessard |
Tue, 11 Sep 2012 16:27:33 +0200 | |
changeset 629 | 06abc9867095 |
parent 612 | c062ff18d04f |
child 633 | 73b56dc69e61 |
child 664 | 199a9f84e64c |
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) 2009-2012 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 |
/* Determine the data type of an constant expression. |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
34 |
* A reference to the relevant type definition is returned. |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
35 |
* |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
36 |
* For example: |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
37 |
* 2 + 3 -> returns reference |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
38 |
* 22.2 - 5 -> returns reference |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
39 |
* etc... |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
40 |
*/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
41 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
42 |
#include <vector> |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
43 |
#include "../absyntax_utils/absyntax_utils.hh" |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
44 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
45 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
46 |
|
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
47 |
class constant_folding_c : public iterator_visitor_c { |
612 | 48 |
private: |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
49 |
search_varfb_instance_type_c *search_varfb_instance_type; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
50 |
search_base_type_c search_base_type; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
51 |
int error_count; |
568
5f79478142d7
make the compiler platform independent (i.e. no longer assume sizeof(double)==8).
Mario de Sousa <msousa@fe.up.pt>
parents:
564
diff
changeset
|
52 |
bool warning_found; |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
53 |
int current_display_error_level; |
612 | 54 |
/* Pointer to the previous IL instruction, which contains the current cvalue of the data stored in the IL stack, i.e. the default variable, a.k.a. accumulator */ |
55 |
symbol_c *prev_il_instruction; |
|
56 |
/* the current IL operand being analyzed */ |
|
57 |
symbol_c *il_operand; |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
58 |
|
612 | 59 |
public: |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
60 |
constant_folding_c(symbol_c *symbol = NULL); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
61 |
virtual ~constant_folding_c(void); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
62 |
int get_error_count(); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
63 |
|
612 | 64 |
private: |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
65 |
/*********************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
66 |
/* B 1.2 - Constants */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
67 |
/*********************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
68 |
/******************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
69 |
/* B 1.2.1 - Numeric Literals */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
70 |
/******************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
71 |
void *visit(real_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
72 |
void *visit(integer_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
73 |
void *visit(neg_real_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
74 |
void *visit(neg_integer_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
75 |
void *visit(binary_integer_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
76 |
void *visit(octal_integer_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
77 |
void *visit(hex_integer_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
78 |
void *visit(integer_literal_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
79 |
void *visit(real_literal_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
80 |
void *visit(bit_string_literal_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
81 |
void *visit(boolean_literal_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
82 |
void *visit(boolean_true_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
83 |
void *visit(boolean_false_c *symbol); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
84 |
|
612 | 85 |
/****************************************/ |
86 |
/* B.2 - Language IL (Instruction List) */ |
|
87 |
/****************************************/ |
|
88 |
/***********************************/ |
|
89 |
/* B 2.1 Instructions and Operands */ |
|
90 |
/***********************************/ |
|
91 |
// void *visit(instruction_list_c *symbol); /* Not needed, since we inherit from iterator_visitor_c */ |
|
92 |
void *visit(il_instruction_c *symbol); |
|
93 |
void *visit(il_simple_operation_c *symbol); |
|
94 |
//void *visit(il_function_call_c *symbol); /* TODO */ |
|
95 |
void *visit(il_expression_c *symbol); |
|
96 |
void *visit(il_jump_operation_c *symbol); |
|
97 |
void *visit(il_fb_call_c *symbol); |
|
98 |
//void *visit(il_formal_funct_call_c *symbol); /* TODO */ |
|
99 |
//void *visit(il_operand_list_c *symbol); /* Not needed, since we inherit from iterator_visitor_c */ |
|
100 |
void *visit(simple_instr_list_c *symbol); |
|
101 |
void *visit(il_simple_instruction_c *symbol); |
|
102 |
||
103 |
||
104 |
/*******************/ |
|
105 |
/* B 2.2 Operators */ |
|
106 |
/*******************/ |
|
107 |
void *visit( LD_operator_c *symbol); |
|
108 |
void *visit( LDN_operator_c *symbol); |
|
109 |
void *visit( ST_operator_c *symbol); |
|
110 |
void *visit( STN_operator_c *symbol); |
|
111 |
void *visit( NOT_operator_c *symbol); |
|
112 |
void *visit( S_operator_c *symbol); |
|
113 |
void *visit( R_operator_c *symbol); |
|
114 |
void *visit( S1_operator_c *symbol); |
|
115 |
void *visit( R1_operator_c *symbol); |
|
116 |
void *visit( CLK_operator_c *symbol); |
|
117 |
void *visit( CU_operator_c *symbol); |
|
118 |
void *visit( CD_operator_c *symbol); |
|
119 |
void *visit( PV_operator_c *symbol); |
|
120 |
void *visit( IN_operator_c *symbol); |
|
121 |
void *visit( PT_operator_c *symbol); |
|
122 |
void *visit( AND_operator_c *symbol); |
|
123 |
void *visit( OR_operator_c *symbol); |
|
124 |
void *visit( XOR_operator_c *symbol); |
|
125 |
void *visit( ANDN_operator_c *symbol); |
|
126 |
void *visit( ORN_operator_c *symbol); |
|
127 |
void *visit( XORN_operator_c *symbol); |
|
128 |
void *visit( ADD_operator_c *symbol); |
|
129 |
void *visit( SUB_operator_c *symbol); |
|
130 |
void *visit( MUL_operator_c *symbol); |
|
131 |
void *visit( DIV_operator_c *symbol); |
|
132 |
void *visit( MOD_operator_c *symbol); |
|
133 |
void *visit( GT_operator_c *symbol); |
|
134 |
void *visit( GE_operator_c *symbol); |
|
135 |
void *visit( EQ_operator_c *symbol); |
|
136 |
void *visit( LT_operator_c *symbol); |
|
137 |
void *visit( LE_operator_c *symbol); |
|
138 |
void *visit( NE_operator_c *symbol); |
|
139 |
void *visit( CAL_operator_c *symbol); |
|
140 |
void *visit( CALC_operator_c *symbol); |
|
141 |
void *visit(CALCN_operator_c *symbol); |
|
142 |
void *visit( RET_operator_c *symbol); |
|
143 |
void *visit( RETC_operator_c *symbol); |
|
144 |
void *visit(RETCN_operator_c *symbol); |
|
145 |
void *visit( JMP_operator_c *symbol); |
|
146 |
void *visit( JMPC_operator_c *symbol); |
|
147 |
void *visit(JMPCN_operator_c *symbol); |
|
148 |
/* Symbol class handled together with function call checks */ |
|
149 |
// void *visit(il_assign_operator_c *symbol, variable_name); |
|
150 |
/* Symbol class handled together with function call checks */ |
|
151 |
// void *visit(il_assign_operator_c *symbol, option, variable_name); |
|
152 |
||
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
153 |
/***************************************/ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
154 |
/* B.3 - Language ST (Structured Text) */ |
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 |
/* B 3.1 - Expressions */ |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
158 |
/***********************/ |
612 | 159 |
void *visit( or_expression_c *symbol); |
160 |
void *visit( xor_expression_c *symbol); |
|
161 |
void *visit( and_expression_c *symbol); |
|
162 |
void *visit( equ_expression_c *symbol); |
|
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
163 |
void *visit(notequ_expression_c *symbol); |
612 | 164 |
void *visit( lt_expression_c *symbol); |
165 |
void *visit( gt_expression_c *symbol); |
|
166 |
void *visit( le_expression_c *symbol); |
|
167 |
void *visit( ge_expression_c *symbol); |
|
168 |
void *visit( add_expression_c *symbol); |
|
169 |
void *visit( sub_expression_c *symbol); |
|
170 |
void *visit( mul_expression_c *symbol); |
|
171 |
void *visit( div_expression_c *symbol); |
|
172 |
void *visit( mod_expression_c *symbol); |
|
173 |
void *visit( power_expression_c *symbol); |
|
174 |
void *visit( neg_expression_c *symbol); |
|
175 |
void *visit( not_expression_c *symbol); |
|
176 |
//void *visit(function_invocation_c *symbol); /* TODO */ |
|
177 |
||
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
178 |
}; |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
diff
changeset
|
179 |