|
1 /* |
|
2 * matiec - a compiler for the programming languages defined in IEC 61131-3 |
|
3 * |
|
4 * Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) |
|
5 * Copyright (C) 2012 Manuele Conti (conti.ma@alice.it) |
|
6 * |
|
7 * |
|
8 * This program is free software: you can redistribute it and/or modify |
|
9 * it under the terms of the GNU General Public License as published by |
|
10 * the Free Software Foundation, either version 3 of the License, or |
|
11 * (at your option) any later version. |
|
12 * |
|
13 * This program is distributed in the hope that it will be useful, |
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 * GNU General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 * |
|
21 * |
|
22 * This code is made available on the understanding that it will not be |
|
23 * used in safety-critical situations without a full and competent review. |
|
24 */ |
|
25 |
|
26 /* |
|
27 * An IEC 61131-3 compiler. |
|
28 * |
|
29 * Based on the |
|
30 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
31 * |
|
32 */ |
|
33 |
|
34 |
|
35 /* |
|
36 * TODO: |
|
37 * - Check subscript values fall within allowed range. |
|
38 * For the checking of subscript values to work correctly, we would need to have constant folding working too: |
|
39 * array_var[8 + 99] can not be checked without constant folding. |
|
40 * However, even without constant folding range check may be usefull, |
|
41 * and later changing it to use the values coming out of constant folding should not be very difficult. |
|
42 * |
|
43 */ |
|
44 |
|
45 |
|
46 #include "range_check.hh" |
|
47 |
|
48 #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) |
|
49 #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) |
|
50 |
|
51 #define STAGE3_ERROR(error_level, symbol1, symbol2, ...) { \ |
|
52 if (current_display_error_level >= error_level) { \ |
|
53 fprintf(stderr, "%s:%d-%d..%d-%d: error: ", \ |
|
54 FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ |
|
55 LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ |
|
56 fprintf(stderr, __VA_ARGS__); \ |
|
57 fprintf(stderr, "\n"); \ |
|
58 error_count++; \ |
|
59 } \ |
|
60 } |
|
61 |
|
62 |
|
63 #define STAGE3_WARNING(symbol1, symbol2, ...) { \ |
|
64 fprintf(stderr, "%s:%d-%d..%d-%d: warning: ", \ |
|
65 FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column,\ |
|
66 LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column);\ |
|
67 fprintf(stderr, __VA_ARGS__); \ |
|
68 fprintf(stderr, "\n"); \ |
|
69 warning_found = true; \ |
|
70 } |
|
71 |
|
72 |
|
73 range_check_c::range_check_c(symbol_c *ignore) { |
|
74 error_count = 0; |
|
75 current_display_error_level = 0; |
|
76 } |
|
77 |
|
78 range_check_c::~range_check_c(void) { |
|
79 } |
|
80 |
|
81 int range_check_c::get_error_count() { |
|
82 return error_count; |
|
83 } |
|
84 |
|
85 void range_check_c::check_range_array_check(array_variable_c *symbol) { |
|
86 int dimension_count; |
|
87 symbol_c* dimension; |
|
88 var_declarations_c *var_decl; |
|
89 array_dimension_iterator_c* array_dimension_iterator; |
|
90 |
|
91 var_decl = (var_declarations_c *)search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable); |
|
92 array_dimension_iterator = new array_dimension_iterator_c(var_decl); |
|
93 for (dimension_count = 0; NULL != array_dimension_iterator->next(); dimension_count++); |
|
94 if (dimension_count != ((list_c *)symbol->subscript_list)->n) |
|
95 STAGE3_ERROR(0, symbol, symbol, "Number of dimensions does not match, Array have %d dimension(s)", dimension_count); |
|
96 } |
|
97 |
|
98 /*********************/ |
|
99 /* B 1.4 - Variables */ |
|
100 /*********************/ |
|
101 /*************************************/ |
|
102 /* B 1.4.2 - Multi-element variables */ |
|
103 /*************************************/ |
|
104 void *range_check_c::visit(array_variable_c *symbol) { |
|
105 check_range_array_check(symbol); |
|
106 return NULL; |
|
107 } |
|
108 |
|
109 |
|
110 /**************************************/ |
|
111 /* B 1.5 - Program organisation units */ |
|
112 /**************************************/ |
|
113 /***********************/ |
|
114 /* B 1.5.1 - Functions */ |
|
115 /***********************/ |
|
116 void *range_check_c::visit(function_declaration_c *symbol) { |
|
117 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
118 search_var_instance_decl = new search_var_instance_decl_c(symbol); |
|
119 symbol->function_body->accept(*this); |
|
120 delete search_varfb_instance_type; |
|
121 delete search_var_instance_decl; |
|
122 search_varfb_instance_type = NULL; |
|
123 search_var_instance_decl = NULL; |
|
124 return NULL; |
|
125 } |
|
126 |
|
127 /*****************************/ |
|
128 /* B 1.5.2 - Function blocks */ |
|
129 /*****************************/ |
|
130 void *range_check_c::visit(function_block_declaration_c *symbol) { |
|
131 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
132 search_var_instance_decl = new search_var_instance_decl_c(symbol); |
|
133 symbol->fblock_body->accept(*this); |
|
134 delete search_varfb_instance_type; |
|
135 delete search_var_instance_decl; |
|
136 search_varfb_instance_type = NULL; |
|
137 search_var_instance_decl = NULL; |
|
138 return NULL; |
|
139 } |
|
140 |
|
141 /**********************/ |
|
142 /* B 1.5.3 - Programs */ |
|
143 /**********************/ |
|
144 void *range_check_c::visit(program_declaration_c *symbol) { |
|
145 search_varfb_instance_type = new search_varfb_instance_type_c(symbol); |
|
146 search_var_instance_decl = new search_var_instance_decl_c(symbol); |
|
147 symbol->function_block_body->accept(*this); |
|
148 delete search_varfb_instance_type; |
|
149 delete search_var_instance_decl; |
|
150 search_varfb_instance_type = NULL; |
|
151 search_var_instance_decl = NULL; |
|
152 return NULL; |
|
153 } |
|
154 |
|
155 |
|
156 |
|
157 /***************************************/ |
|
158 /* B.3 - Language ST (Structured Text) */ |
|
159 /***************************************/ |
|
160 /*********************************/ |
|
161 /* B 3.2.1 Assignment Statements */ |
|
162 /*********************************/ |
|
163 void *range_check_c::visit(assignment_statement_c *symbol) { |
|
164 symbol->l_exp->accept(*this); |
|
165 symbol->r_exp->accept(*this); |
|
166 return NULL; |
|
167 } |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |