author | Manuele Conti <conti.ma@alice.it> |
Thu, 16 Aug 2012 22:44:38 +0200 | |
changeset 641 | 5681f600ac18 |
parent 612 | c062ff18d04f |
child 656 | 45a796bce487 |
permissions | -rwxr-xr-x |
204 | 1 |
/* |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
204 | 3 |
* |
417
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
4 |
* Copyright (C) 2009-2012 Mario de Sousa (msousa@fe.up.pt) |
279
c0453b7f99df
Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents:
267
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
417
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
6 |
* Copyright (C) 2012 Manuele Conti (manuele.conti@sirius-es.it) |
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
7 |
* Copyright (C) 2012 Matteo Facchinetti (matteo.facchinetti@sirius-es.it) |
204 | 8 |
* |
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
9 |
* This program is free software: you can redistribute it and/or modify |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
10 |
* it under the terms of the GNU General Public License as published by |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
11 |
* the Free Software Foundation, either version 3 of the License, or |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
12 |
* (at your option) any later version. |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
13 |
* |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
14 |
* This program is distributed in the hope that it will be useful, |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
17 |
* GNU General Public License for more details. |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
18 |
* |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
19 |
* You should have received a copy of the GNU General Public License |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
21 |
* |
204 | 22 |
* |
23 |
* This code is made available on the understanding that it will not be |
|
24 |
* used in safety-critical situations without a full and competent review. |
|
25 |
*/ |
|
26 |
||
27 |
/* |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
28 |
* An IEC 61131-3 compiler. |
204 | 29 |
* |
30 |
* Based on the |
|
267
0a1204bcc9af
starting to add proper header file with declaration of standard functions
Mario de Sousa <msousa@fe.up.pt>
parents:
262
diff
changeset
|
31 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
204 | 32 |
* |
33 |
*/ |
|
34 |
||
35 |
#include "stage3.hh" |
|
36 |
||
443
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
37 |
#include "flow_control_analysis.hh" |
417
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
38 |
#include "fill_candidate_datatypes.hh" |
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
39 |
#include "narrow_candidate_datatypes.hh" |
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
40 |
#include "print_datatypes_error.hh" |
508 | 41 |
#include "lvalue_check.hh" |
560
13b5b7faa3d7
Renamed class, remove unused code, delete allocated objects.
mjsousa <msousa@fe.up.pt>
parents:
559
diff
changeset
|
42 |
#include "array_range_check.hh" |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
560
diff
changeset
|
43 |
#include "constant_folding.hh" |
530
1eacd7afcab1
Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents:
516
diff
changeset
|
44 |
|
612 | 45 |
|
530
1eacd7afcab1
Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents:
516
diff
changeset
|
46 |
|
443
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
47 |
static int flow_control_analysis(symbol_c *tree_root){ |
564
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
560
diff
changeset
|
48 |
flow_control_analysis_c flow_control_analysis(tree_root); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
560
diff
changeset
|
49 |
tree_root->accept(flow_control_analysis); |
dabffc3086dc
Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents:
560
diff
changeset
|
50 |
return 0; |
443
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
51 |
} |
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
52 |
|
530
1eacd7afcab1
Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents:
516
diff
changeset
|
53 |
|
612 | 54 |
/* Constant folding assumes that flow control analysis has been completed! |
55 |
* so be sure to call flow_control_analysis() before calling this function! |
|
56 |
*/ |
|
57 |
static int constant_folding(symbol_c *tree_root){ |
|
58 |
constant_folding_c constant_folding(tree_root); |
|
59 |
tree_root->accept(constant_folding); |
|
60 |
return constant_folding.get_error_count(); |
|
61 |
} |
|
62 |
||
63 |
||
611 | 64 |
/* Type safety analysis assumes that |
65 |
* - flow control analysis |
|
66 |
* - constant folding (constant check) |
|
67 |
* has already been completed, so be sure to call those semantic checkers |
|
68 |
* before calling this function |
|
443
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
69 |
*/ |
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
70 |
static int type_safety(symbol_c *tree_root){ |
417
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
71 |
fill_candidate_datatypes_c fill_candidate_datatypes(tree_root); |
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
72 |
tree_root->accept(fill_candidate_datatypes); |
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
73 |
narrow_candidate_datatypes_c narrow_candidate_datatypes(tree_root); |
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
74 |
tree_root->accept(narrow_candidate_datatypes); |
444
92d40d2a7adc
Update comments on general architecture.
Mario de Sousa <msousa@fe.up.pt>
parents:
443
diff
changeset
|
75 |
print_datatypes_error_c print_datatypes_error(tree_root); |
417
d48f53715f77
Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents:
279
diff
changeset
|
76 |
tree_root->accept(print_datatypes_error); |
510
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
77 |
return print_datatypes_error.get_error_count(); |
204 | 78 |
} |
79 |
||
443
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
80 |
|
612 | 81 |
/* Left value checking assumes that data type analysis has already been completed, |
530
1eacd7afcab1
Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents:
516
diff
changeset
|
82 |
* so be sure to call type_safety() before calling this function |
510
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
83 |
*/ |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
84 |
static int lvalue_check(symbol_c *tree_root){ |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
85 |
lvalue_check_c lvalue_check(tree_root); |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
86 |
tree_root->accept(lvalue_check); |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
87 |
return lvalue_check.get_error_count(); |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
88 |
} |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
89 |
|
612 | 90 |
/* Array range check assumes that constant folding has been completed! |
91 |
* so be sure to call constant_folding() before calling this function! |
|
92 |
*/ |
|
93 |
static int array_range_check(symbol_c *tree_root){ |
|
560
13b5b7faa3d7
Renamed class, remove unused code, delete allocated objects.
mjsousa <msousa@fe.up.pt>
parents:
559
diff
changeset
|
94 |
array_range_check_c array_range_check(tree_root); |
13b5b7faa3d7
Renamed class, remove unused code, delete allocated objects.
mjsousa <msousa@fe.up.pt>
parents:
559
diff
changeset
|
95 |
tree_root->accept(array_range_check); |
13b5b7faa3d7
Renamed class, remove unused code, delete allocated objects.
mjsousa <msousa@fe.up.pt>
parents:
559
diff
changeset
|
96 |
return array_range_check.get_error_count(); |
559
a3b8925e640c
Start sematinc range check class.
Manuele Conti <conti.ma@alice.it>
parents:
530
diff
changeset
|
97 |
} |
a3b8925e640c
Start sematinc range check class.
Manuele Conti <conti.ma@alice.it>
parents:
530
diff
changeset
|
98 |
|
443
ff4d26b7e51d
Narrow IL code backwords, so as to correctly handle invocations to overloaded functions from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents:
417
diff
changeset
|
99 |
|
204 | 100 |
int stage3(symbol_c *tree_root){ |
510
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
101 |
int error_count = 0; |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
102 |
error_count += flow_control_analysis(tree_root); |
612 | 103 |
error_count += constant_folding(tree_root); |
510
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
104 |
error_count += type_safety(tree_root); |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
105 |
error_count += lvalue_check(tree_root); |
612 | 106 |
error_count += array_range_check(tree_root); |
510
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
107 |
|
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
108 |
if (error_count > 0) { |
516
8591746fa134
Produce similar error messages in stage2 and stage3.
Mario de Sousa <msousa@fe.up.pt>
parents:
510
diff
changeset
|
109 |
fprintf(stderr, "%d error(s) found. Bailing out!\n", error_count); |
510
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
110 |
return -1; |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
111 |
} |
9317e04c1dde
Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents:
508
diff
changeset
|
112 |
return 0; |
204 | 113 |
} |