changeset 567 | e5deeb6d4d2f |
parent 566 | 5688fa07f89a |
child 568 | 5f79478142d7 |
566:5688fa07f89a | 567:e5deeb6d4d2f |
---|---|
70 warning_found = true; \ |
70 warning_found = true; \ |
71 } |
71 } |
72 |
72 |
73 |
73 |
74 |
74 |
75 |
|
76 #define MALLOC(variable, data_type) \ |
|
77 variable = (data_type *)malloc(sizeof(data_type)); \ |
|
78 if (variable == NULL) ERROR; |
|
79 |
|
80 |
|
75 #define DO_OPER(dtype)\ |
81 #define DO_OPER(dtype)\ |
76 (NULL != symbol->r_exp->const_value_##dtype ) && \ |
82 (NULL != symbol->r_exp->const_value_##dtype ) && \ |
77 (NULL != symbol->l_exp->const_value_##dtype ) |
83 (NULL != symbol->l_exp->const_value_##dtype ) |
78 |
84 |
79 |
85 |
94 constant_folding_c::constant_folding_c(symbol_c *symbol) { |
100 constant_folding_c::constant_folding_c(symbol_c *symbol) { |
95 error_count = 0; |
101 error_count = 0; |
96 current_display_error_level = 0; |
102 current_display_error_level = 0; |
97 } |
103 } |
98 |
104 |
105 |
|
99 constant_folding_c::~constant_folding_c(void) { |
106 constant_folding_c::~constant_folding_c(void) { |
100 } |
107 } |
108 |
|
101 |
109 |
102 int constant_folding_c::get_error_count() { |
110 int constant_folding_c::get_error_count() { |
103 return error_count; |
111 return error_count; |
104 } |
112 } |
105 |
113 |
109 /*********************/ |
117 /*********************/ |
110 /******************************/ |
118 /******************************/ |
111 /* B 1.2.1 - Numeric Literals */ |
119 /* B 1.2.1 - Numeric Literals */ |
112 /******************************/ |
120 /******************************/ |
113 void *constant_folding_c::visit(real_c *symbol) { |
121 void *constant_folding_c::visit(real_c *symbol) { |
114 double *real_value; |
122 MALLOC(symbol->const_value_real, double); |
115 |
123 *symbol->const_value_real = extract_real_value(symbol); |
116 real_value = (double *)malloc(sizeof(double)); |
124 |
117 sscanf(symbol->value, "%lf", real_value); |
125 return NULL; |
118 symbol->const_value_real = real_value; |
126 } |
119 |
127 |
120 return NULL; |
|
121 } |
|
122 |
128 |
123 void *constant_folding_c::visit(integer_c *symbol) { |
129 void *constant_folding_c::visit(integer_c *symbol) { |
124 int64_t *integer_value; |
130 int64_t *integer_value; |
125 |
131 |
126 integer_value = (int64_t *)malloc(sizeof(int64_t)); |
132 integer_value = (int64_t *)malloc(sizeof(int64_t)); |
127 *integer_value = extract_integer_value(symbol); |
133 *integer_value = extract_integer_value(symbol); |
128 symbol->const_value_integer = integer_value; |
134 symbol->const_value_integer = integer_value; |
129 |
135 |
130 return NULL; |
136 return NULL; |
131 } |
137 } |
138 |
|
132 |
139 |
133 void *constant_folding_c::visit(neg_real_c *symbol) { |
140 void *constant_folding_c::visit(neg_real_c *symbol) { |
134 symbol->exp->accept(*this); |
141 symbol->exp->accept(*this); |
135 if (NULL == symbol->exp->const_value_real) |
142 if (NULL == symbol->exp->const_value_real) |
136 ERROR; |
143 ERROR; |
137 symbol->const_value_real = (double*) malloc(sizeof(double)); |
144 symbol->const_value_real = (double*) malloc(sizeof(double)); |
138 *symbol->const_value_real = - *(symbol->exp->const_value_real); |
145 *symbol->const_value_real = - *(symbol->exp->const_value_real); |
139 return NULL; |
146 return NULL; |
140 } |
147 } |
141 |
148 |
149 |
|
142 void *constant_folding_c::visit(neg_integer_c *symbol) { |
150 void *constant_folding_c::visit(neg_integer_c *symbol) { |
143 int64_t *integer_value; |
151 int64_t *integer_value; |
144 |
152 |
145 integer_value = (int64_t *)malloc(sizeof(int64_t)); |
153 integer_value = (int64_t *)malloc(sizeof(int64_t)); |
146 *integer_value = extract_integer_value(symbol); |
154 *integer_value = extract_integer_value(symbol); |
147 symbol->const_value_integer = integer_value; |
155 symbol->const_value_integer = integer_value; |
148 |
156 |
149 return NULL; |
157 return NULL; |
150 } |
158 } |
151 |
159 |
160 |
|
152 void *constant_folding_c::visit(binary_integer_c *symbol) { |
161 void *constant_folding_c::visit(binary_integer_c *symbol) { |
153 |
162 |
154 return NULL; |
163 return NULL; |
155 } |
164 } |
156 |
165 |
166 |
|
157 void *constant_folding_c::visit(octal_integer_c *symbol) { |
167 void *constant_folding_c::visit(octal_integer_c *symbol) { |
158 |
168 |
159 return NULL; |
169 return NULL; |
160 } |
170 } |
171 |
|
161 |
172 |
162 void *constant_folding_c::visit(hex_integer_c *symbol) { |
173 void *constant_folding_c::visit(hex_integer_c *symbol) { |
163 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
174 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
164 *(symbol->const_value_integer) = extract_hex_value(symbol); |
175 *(symbol->const_value_integer) = extract_hex_value(symbol); |
165 |
176 |
166 return NULL; |
177 return NULL; |
167 } |
178 } |
179 |
|
168 |
180 |
169 void *constant_folding_c::visit(integer_literal_c *symbol) { |
181 void *constant_folding_c::visit(integer_literal_c *symbol) { |
170 symbol->value->accept(*this); |
182 symbol->value->accept(*this); |
171 if (NULL == symbol->value->const_value_integer) ERROR; |
183 if (NULL == symbol->value->const_value_integer) ERROR; |
172 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
184 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
173 *(symbol->const_value_integer) = *(symbol->value->const_value_integer); |
185 *(symbol->const_value_integer) = *(symbol->value->const_value_integer); |
174 |
186 |
175 return NULL; |
187 return NULL; |
176 } |
188 } |
177 |
189 |
190 |
|
178 void *constant_folding_c::visit(real_literal_c *symbol) { |
191 void *constant_folding_c::visit(real_literal_c *symbol) { |
179 symbol->value->accept(*this); |
192 symbol->value->accept(*this); |
180 if (NULL == symbol->value->const_value_real) ERROR; |
193 if (NULL == symbol->value->const_value_real) ERROR; |
181 symbol->const_value_real = (double*) malloc(sizeof(double)); |
194 symbol->const_value_real = (double*) malloc(sizeof(double)); |
182 *symbol->const_value_real = *(symbol->value->const_value_real); |
195 *symbol->const_value_real = *(symbol->value->const_value_real); |
183 |
196 |
184 return NULL; |
197 return NULL; |
185 } |
198 } |
186 |
199 |
200 |
|
187 void *constant_folding_c::visit(bit_string_literal_c *symbol) { |
201 void *constant_folding_c::visit(bit_string_literal_c *symbol) { |
188 |
202 |
189 return NULL; |
203 return NULL; |
190 } |
204 } |
205 |
|
191 |
206 |
192 void *constant_folding_c::visit(boolean_literal_c *symbol) { |
207 void *constant_folding_c::visit(boolean_literal_c *symbol) { |
193 symbol->value->accept(*this); |
208 symbol->value->accept(*this); |
194 if (NULL == symbol->value->const_value_bool) ERROR; |
209 if (NULL == symbol->value->const_value_bool) ERROR; |
195 symbol->const_value_bool = (bool *)malloc(sizeof(bool)); |
210 symbol->const_value_bool = (bool *)malloc(sizeof(bool)); |
196 *(symbol->const_value_bool) = *(symbol->value->const_value_bool); |
211 *(symbol->const_value_bool) = *(symbol->value->const_value_bool); |
197 |
212 |
198 return NULL; |
213 return NULL; |
199 } |
214 } |
200 |
215 |
216 |
|
201 void *constant_folding_c::visit(boolean_true_c *symbol) { |
217 void *constant_folding_c::visit(boolean_true_c *symbol) { |
202 symbol->const_value_bool = (bool *)malloc(sizeof(bool)); |
218 symbol->const_value_bool = (bool *)malloc(sizeof(bool)); |
203 *(symbol->const_value_bool) = true; |
219 *(symbol->const_value_bool) = true; |
204 |
220 |
205 return NULL; |
221 return NULL; |
206 } |
222 } |
207 |
223 |
224 |
|
208 void *constant_folding_c::visit(boolean_false_c *symbol) { |
225 void *constant_folding_c::visit(boolean_false_c *symbol) { |
209 symbol->const_value_bool = (bool *)malloc(sizeof(bool)); |
226 symbol->const_value_bool = (bool *)malloc(sizeof(bool)); |
210 *(symbol->const_value_bool) = false; |
227 *(symbol->const_value_bool) = false; |
211 |
228 |
212 return NULL; |
229 return NULL; |
213 } |
230 } |
231 |
|
232 |
|
214 |
233 |
215 /***************************************/ |
234 /***************************************/ |
216 /* B.3 - Language ST (Structured Text) */ |
235 /* B.3 - Language ST (Structured Text) */ |
217 /***************************************/ |
236 /***************************************/ |
218 /***********************/ |
237 /***********************/ |
231 } |
250 } |
232 |
251 |
233 return NULL; |
252 return NULL; |
234 } |
253 } |
235 |
254 |
255 |
|
236 void *constant_folding_c::visit(xor_expression_c *symbol) { |
256 void *constant_folding_c::visit(xor_expression_c *symbol) { |
237 symbol->l_exp->accept(*this); |
257 symbol->l_exp->accept(*this); |
238 symbol->r_exp->accept(*this); |
258 symbol->r_exp->accept(*this); |
239 if (DO_OPER(integer)) { |
259 if (DO_OPER(integer)) { |
240 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
260 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
242 } |
262 } |
243 |
263 |
244 return NULL; |
264 return NULL; |
245 } |
265 } |
246 |
266 |
267 |
|
247 void *constant_folding_c::visit(and_expression_c *symbol) { |
268 void *constant_folding_c::visit(and_expression_c *symbol) { |
248 symbol->l_exp->accept(*this); |
269 symbol->l_exp->accept(*this); |
249 symbol->r_exp->accept(*this); |
270 symbol->r_exp->accept(*this); |
250 if (DO_OPER(bool)) { |
271 if (DO_OPER(bool)) { |
251 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
272 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
257 } |
278 } |
258 |
279 |
259 return NULL; |
280 return NULL; |
260 } |
281 } |
261 |
282 |
283 |
|
262 void *constant_folding_c::visit(equ_expression_c *symbol) { |
284 void *constant_folding_c::visit(equ_expression_c *symbol) { |
263 symbol->l_exp->accept(*this); |
285 symbol->l_exp->accept(*this); |
264 symbol->r_exp->accept(*this); |
286 symbol->r_exp->accept(*this); |
265 if (DO_OPER(bool)) { |
287 if (DO_OPER(bool)) { |
266 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
288 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
276 } |
298 } |
277 |
299 |
278 return NULL; |
300 return NULL; |
279 } |
301 } |
280 |
302 |
303 |
|
281 void *constant_folding_c::visit(notequ_expression_c *symbol) { |
304 void *constant_folding_c::visit(notequ_expression_c *symbol) { |
282 symbol->l_exp->accept(*this); |
305 symbol->l_exp->accept(*this); |
283 symbol->r_exp->accept(*this); |
306 symbol->r_exp->accept(*this); |
284 if (DO_OPER(bool)) { |
307 if (DO_OPER(bool)) { |
285 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
308 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
295 } |
318 } |
296 |
319 |
297 return NULL; |
320 return NULL; |
298 } |
321 } |
299 |
322 |
323 |
|
300 void *constant_folding_c::visit(lt_expression_c *symbol) { |
324 void *constant_folding_c::visit(lt_expression_c *symbol) { |
301 symbol->l_exp->accept(*this); |
325 symbol->l_exp->accept(*this); |
302 symbol->r_exp->accept(*this); |
326 symbol->r_exp->accept(*this); |
303 if (DO_OPER(integer)) { |
327 if (DO_OPER(integer)) { |
304 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
328 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
310 } |
334 } |
311 |
335 |
312 return NULL; |
336 return NULL; |
313 } |
337 } |
314 |
338 |
339 |
|
315 void *constant_folding_c::visit(gt_expression_c *symbol) { |
340 void *constant_folding_c::visit(gt_expression_c *symbol) { |
316 symbol->l_exp->accept(*this); |
341 symbol->l_exp->accept(*this); |
317 symbol->r_exp->accept(*this); |
342 symbol->r_exp->accept(*this); |
318 if (DO_OPER(integer)) { |
343 if (DO_OPER(integer)) { |
319 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
344 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
325 } |
350 } |
326 |
351 |
327 return NULL; |
352 return NULL; |
328 } |
353 } |
329 |
354 |
355 |
|
330 void *constant_folding_c::visit(le_expression_c *symbol) { |
356 void *constant_folding_c::visit(le_expression_c *symbol) { |
331 symbol->l_exp->accept(*this); |
357 symbol->l_exp->accept(*this); |
332 symbol->r_exp->accept(*this); |
358 symbol->r_exp->accept(*this); |
333 if (DO_OPER(integer)) { |
359 if (DO_OPER(integer)) { |
334 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
360 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
340 } |
366 } |
341 |
367 |
342 return NULL; |
368 return NULL; |
343 } |
369 } |
344 |
370 |
371 |
|
345 void *constant_folding_c::visit(ge_expression_c *symbol) { |
372 void *constant_folding_c::visit(ge_expression_c *symbol) { |
346 symbol->l_exp->accept(*this); |
373 symbol->l_exp->accept(*this); |
347 symbol->r_exp->accept(*this); |
374 symbol->r_exp->accept(*this); |
348 if (DO_OPER(integer)) { |
375 if (DO_OPER(integer)) { |
349 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
376 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
354 *(symbol->const_value_bool) = *(symbol->l_exp->const_value_real) >= *(symbol->r_exp->const_value_real); |
381 *(symbol->const_value_bool) = *(symbol->l_exp->const_value_real) >= *(symbol->r_exp->const_value_real); |
355 } |
382 } |
356 |
383 |
357 return NULL; |
384 return NULL; |
358 } |
385 } |
386 |
|
359 |
387 |
360 void *constant_folding_c::visit(add_expression_c *symbol) { |
388 void *constant_folding_c::visit(add_expression_c *symbol) { |
361 symbol->l_exp->accept(*this); |
389 symbol->l_exp->accept(*this); |
362 symbol->r_exp->accept(*this); |
390 symbol->r_exp->accept(*this); |
363 if (DO_OPER(integer)) { |
391 if (DO_OPER(integer)) { |
381 } |
409 } |
382 |
410 |
383 return NULL; |
411 return NULL; |
384 } |
412 } |
385 |
413 |
414 |
|
386 void *constant_folding_c::visit(sub_expression_c *symbol) { |
415 void *constant_folding_c::visit(sub_expression_c *symbol) { |
387 symbol->l_exp->accept(*this); |
416 symbol->l_exp->accept(*this); |
388 symbol->r_exp->accept(*this); |
417 symbol->r_exp->accept(*this); |
389 if (DO_OPER(integer)) { |
418 if (DO_OPER(integer)) { |
390 if (CHECK_OVERFLOW_SUB(*(symbol->l_exp->const_value_integer), *(symbol->r_exp->const_value_integer), int64_t)) |
419 if (CHECK_OVERFLOW_SUB(*(symbol->l_exp->const_value_integer), *(symbol->r_exp->const_value_integer), int64_t)) |
407 } |
436 } |
408 |
437 |
409 return NULL; |
438 return NULL; |
410 } |
439 } |
411 |
440 |
441 |
|
412 void *constant_folding_c::visit(mul_expression_c *symbol) { |
442 void *constant_folding_c::visit(mul_expression_c *symbol) { |
413 symbol->l_exp->accept(*this); |
443 symbol->l_exp->accept(*this); |
414 symbol->r_exp->accept(*this); |
444 symbol->r_exp->accept(*this); |
415 if (DO_OPER(integer)) { |
445 if (DO_OPER(integer)) { |
416 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
446 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
430 STAGE3_ERROR(0, symbol, symbol, "Overflow in constant expression."); |
460 STAGE3_ERROR(0, symbol, symbol, "Overflow in constant expression."); |
431 } |
461 } |
432 |
462 |
433 return NULL; |
463 return NULL; |
434 } |
464 } |
465 |
|
435 |
466 |
436 void *constant_folding_c::visit(div_expression_c *symbol) { |
467 void *constant_folding_c::visit(div_expression_c *symbol) { |
437 symbol->l_exp->accept(*this); |
468 symbol->l_exp->accept(*this); |
438 symbol->r_exp->accept(*this); |
469 symbol->r_exp->accept(*this); |
439 if (DO_OPER(integer)) { |
470 if (DO_OPER(integer)) { |
459 } |
490 } |
460 |
491 |
461 return NULL; |
492 return NULL; |
462 } |
493 } |
463 |
494 |
495 |
|
464 void *constant_folding_c::visit(mod_expression_c *symbol) { |
496 void *constant_folding_c::visit(mod_expression_c *symbol) { |
465 symbol->l_exp->accept(*this); |
497 symbol->l_exp->accept(*this); |
466 symbol->r_exp->accept(*this); |
498 symbol->r_exp->accept(*this); |
467 if (DO_OPER(integer)) { |
499 if (DO_OPER(integer)) { |
468 if (*(symbol->r_exp->const_value_integer) == 0) |
500 if (*(symbol->r_exp->const_value_integer) == 0) |
472 |
504 |
473 } |
505 } |
474 |
506 |
475 return NULL; |
507 return NULL; |
476 } |
508 } |
509 |
|
477 |
510 |
478 void *constant_folding_c::visit(power_expression_c *symbol) { |
511 void *constant_folding_c::visit(power_expression_c *symbol) { |
479 symbol->l_exp->accept(*this); |
512 symbol->l_exp->accept(*this); |
480 symbol->r_exp->accept(*this); |
513 symbol->r_exp->accept(*this); |
481 if ((NULL != symbol->l_exp->const_value_real) && (NULL != symbol->r_exp->const_value_integer)) { |
514 if ((NULL != symbol->l_exp->const_value_real) && (NULL != symbol->r_exp->const_value_integer)) { |
492 STAGE3_ERROR(0, symbol, symbol, "Overflow in constant expression."); |
525 STAGE3_ERROR(0, symbol, symbol, "Overflow in constant expression."); |
493 } |
526 } |
494 |
527 |
495 return NULL; |
528 return NULL; |
496 } |
529 } |
530 |
|
531 |
|
497 void *constant_folding_c::visit(neg_expression_c *symbol) { |
532 void *constant_folding_c::visit(neg_expression_c *symbol) { |
498 symbol->exp->accept(*this); |
533 symbol->exp->accept(*this); |
499 if (NULL != symbol->exp->const_value_integer) { |
534 if (NULL != symbol->exp->const_value_integer) { |
500 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
535 symbol->const_value_integer = (int64_t*) malloc(sizeof(int64_t)); |
501 *(symbol->const_value_integer) = - *(symbol->exp->const_value_integer); |
536 *(symbol->const_value_integer) = - *(symbol->exp->const_value_integer); |
505 *(symbol->const_value_real) = - *(symbol->exp->const_value_real); |
540 *(symbol->const_value_real) = - *(symbol->exp->const_value_real); |
506 } |
541 } |
507 return NULL; |
542 return NULL; |
508 } |
543 } |
509 |
544 |
545 |
|
546 |
|
510 void *constant_folding_c::visit(not_expression_c *symbol) { |
547 void *constant_folding_c::visit(not_expression_c *symbol) { |
511 symbol->exp->accept(*this); |
548 symbol->exp->accept(*this); |
512 if (NULL != symbol->exp->const_value_bool) { |
549 if (NULL != symbol->exp->const_value_bool) { |
513 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
550 symbol->const_value_bool = (bool*) malloc(sizeof(bool)); |
514 *(symbol->const_value_bool) = !*(symbol->exp->const_value_bool); |
551 *(symbol->const_value_bool) = !*(symbol->exp->const_value_bool); |