author | laurent |
Wed, 06 Jul 2011 12:19:30 +0200 | |
changeset 334 | 63b52a8a12f3 |
parent 328 | 66cd5d9893dd |
child 338 | 3037bb7e8a82 |
permissions | -rwxr-xr-x |
181 | 1 |
/* |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
2 |
* matiec - a compiler for the programming languages defined in IEC 61131-3 |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
3 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
4 |
* Copyright (C) 2009-2011 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:
265
diff
changeset
|
5 |
* Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant |
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
6 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
7 |
* This program is free software: you can redistribute it and/or modify |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
8 |
* it under the terms of the GNU General Public License as published by |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
9 |
* the Free Software Foundation, either version 3 of the License, or |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
10 |
* (at your option) any later version. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
11 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
12 |
* This program is distributed in the hope that it will be useful, |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
15 |
* GNU General Public License for more details. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
16 |
* |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
19 |
* |
181 | 20 |
* |
21 |
* This code is made available on the understanding that it will not be |
|
22 |
* used in safety-critical situations without a full and competent review. |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
265
4d222f46f8cc
Updating license info (with Edouard's permission for relevant files).
Mario de Sousa <msousa@fe.up.pt>
parents:
181
diff
changeset
|
26 |
* An IEC 61131-3 compiler. |
181 | 27 |
* |
28 |
* Based on the |
|
29 |
* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) |
|
30 |
* |
|
31 |
*/ |
|
32 |
||
33 |
||
34 |
/* |
|
35 |
* This is the main stage 3a file. |
|
36 |
* |
|
37 |
* In stage 3a some helpful symbol tables are instanciated and populated. |
|
38 |
* These symbol tables wll then be used by stage3b and atage4 code generators. |
|
39 |
*/ |
|
40 |
||
41 |
||
42 |
||
43 |
||
44 |
||
45 |
||
46 |
// #include <stdio.h> /* required for NULL */ |
|
47 |
#include <string> |
|
48 |
#include <iostream> |
|
49 |
#include <sstream> |
|
50 |
#include <typeinfo> |
|
51 |
#include <list> |
|
52 |
#include <strings.h> |
|
53 |
||
54 |
#include "../util/symtable.hh" |
|
55 |
#include "../util/dsymtable.hh" |
|
56 |
#include "../absyntax/visitor.hh" |
|
57 |
||
58 |
||
59 |
||
60 |
//#define DEBUG |
|
61 |
#ifdef DEBUG |
|
62 |
#define TRACE(classname) printf("\n____%s____\n",classname); |
|
63 |
#else |
|
64 |
#define TRACE(classname) |
|
65 |
#endif |
|
66 |
||
67 |
#define ERROR error_exit(__FILE__,__LINE__) |
|
68 |
/* function defined in main.cc */ |
|
69 |
extern void error_exit(const char *file_name, int line_no); |
|
70 |
||
71 |
||
72 |
/***********************************************************************/ |
|
73 |
/***********************************************************************/ |
|
74 |
/***********************************************************************/ |
|
75 |
/***********************************************************************/ |
|
76 |
||
77 |
||
78 |
/* returns 0 if the names are equal!! */ |
|
79 |
/* NOTE: it must ignore case!! */ |
|
80 |
int compare_identifiers(symbol_c *ident1, symbol_c *ident2) { |
|
81 |
||
82 |
token_c *name1 = dynamic_cast<token_c *>(ident1); |
|
83 |
token_c *name2 = dynamic_cast<token_c *>(ident2); |
|
84 |
||
85 |
if ((name1 == NULL) || (name2 == NULL)) |
|
86 |
/* invalid identifiers... */ |
|
87 |
return -1; |
|
88 |
||
89 |
if (strcasecmp(name1->value, name2->value) == 0) |
|
90 |
return 0; |
|
91 |
||
92 |
/* identifiers do not match! */ |
|
93 |
return 1; |
|
94 |
} |
|
95 |
||
96 |
||
97 |
||
98 |
/***********************************************************************/ |
|
99 |
/***********************************************************************/ |
|
100 |
/***********************************************************************/ |
|
101 |
/***********************************************************************/ |
|
102 |
||
103 |
||
104 |
||
105 |
/* A symbol table with all globally declared functions... */ |
|
106 |
function_declaration_c null_symbol1(NULL,NULL,NULL,NULL); |
|
107 |
dsymtable_c<function_declaration_c *, &null_symbol1> function_symtable; |
|
108 |
||
109 |
/* A symbol table with all globally declared functions block types... */ |
|
110 |
function_block_declaration_c null_symbol2(NULL,NULL,NULL); |
|
111 |
symtable_c<function_block_declaration_c *, &null_symbol2> function_block_type_symtable; |
|
112 |
||
113 |
/* A symbol table with all globally declared program types... */ |
|
114 |
program_declaration_c null_symbol3(NULL,NULL,NULL); |
|
115 |
symtable_c<program_declaration_c *, &null_symbol3> program_type_symtable; |
|
116 |
||
117 |
/* A symbol table with all user declared type definitions... */ |
|
118 |
/* Note that function block types and program types have their |
|
119 |
* own symbol tables, so do not get placed in this symbol table! |
|
120 |
*/ |
|
121 |
symbol_c null_symbol4; |
|
122 |
symtable_c<symbol_c *, &null_symbol4> type_symtable; |
|
123 |
||
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
124 |
/* A symbol table with all values declared for enumerated type... */ |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
125 |
/* Note that if the value is defined multiple times the value |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
126 |
* is the null pointer. |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
127 |
*/ |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
128 |
symtable_c<symbol_c *, &null_symbol4> enumerated_value_symtable; |
181 | 129 |
|
130 |
||
131 |
/***********************************************************************/ |
|
132 |
/***********************************************************************/ |
|
133 |
/***********************************************************************/ |
|
134 |
/***********************************************************************/ |
|
135 |
||
136 |
||
137 |
class populate_symtables_c: public iterator_visitor_c { |
|
138 |
||
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
139 |
private: |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
140 |
symbol_c *current_enumerated_type; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
141 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
142 |
public: |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
143 |
populate_symtables_c(void) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
144 |
current_enumerated_type = NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
145 |
}; |
181 | 146 |
virtual ~populate_symtables_c(void) {} |
147 |
||
148 |
||
149 |
public: |
|
150 |
||
151 |
/*************************/ |
|
152 |
/* B.1 - Common elements */ |
|
153 |
/*************************/ |
|
154 |
/*******************************************/ |
|
155 |
/* B 1.1 - Letters, digits and identifiers */ |
|
156 |
/*******************************************/ |
|
157 |
/*********************/ |
|
158 |
/* B 1.2 - Constants */ |
|
159 |
/*********************/ |
|
160 |
/******************************/ |
|
161 |
/* B 1.2.1 - Numeric Literals */ |
|
162 |
/******************************/ |
|
163 |
/*******************************/ |
|
164 |
/* B.1.2.2 Character Strings */ |
|
165 |
/*******************************/ |
|
166 |
/***************************/ |
|
167 |
/* B 1.2.3 - Time Literals */ |
|
168 |
/***************************/ |
|
169 |
/************************/ |
|
170 |
/* B 1.2.3.1 - Duration */ |
|
171 |
/************************/ |
|
172 |
/************************************/ |
|
173 |
/* B 1.2.3.2 - Time of day and Date */ |
|
174 |
/************************************/ |
|
175 |
/**********************/ |
|
176 |
/* B.1.3 - Data types */ |
|
177 |
/**********************/ |
|
178 |
/***********************************/ |
|
179 |
/* B 1.3.1 - Elementary Data Types */ |
|
180 |
/***********************************/ |
|
181 |
/********************************/ |
|
182 |
/* B.1.3.2 - Generic data types */ |
|
183 |
/********************************/ |
|
184 |
/********************************/ |
|
185 |
/* B 1.3.3 - Derived data types */ |
|
186 |
/********************************/ |
|
187 |
||
188 |
/* subrange_type_name ':' subrange_spec_init */ |
|
189 |
void *visit(subrange_type_declaration_c *symbol) { |
|
190 |
TRACE("subrange_type_declaration_c"); |
|
191 |
type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init); |
|
192 |
return NULL; |
|
193 |
} |
|
194 |
||
195 |
||
196 |
/* enumerated_type_name ':' enumerated_spec_init */ |
|
197 |
void *visit(enumerated_type_declaration_c *symbol) { |
|
198 |
TRACE("enumerated_type_declaration_c"); |
|
199 |
type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init); |
|
328
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
200 |
current_enumerated_type = symbol->enumerated_type_name; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
201 |
symbol->enumerated_spec_init->accept(*this); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
202 |
current_enumerated_type = NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
203 |
return NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
204 |
} |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
205 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
206 |
/* enumerated_specification ASSIGN enumerated_value */ |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
207 |
void *visit(enumerated_spec_init_c *symbol) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
208 |
return symbol->enumerated_specification->accept(*this); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
209 |
} |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
210 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
211 |
/* [enumerated_type_name '#'] identifier */ |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
212 |
void *visit(enumerated_value_c *symbol) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
213 |
if (current_enumerated_type != NULL) { |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
214 |
if (symbol->type != NULL) ERROR; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
215 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
216 |
symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
217 |
if (value_type == current_enumerated_type) ERROR; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
218 |
|
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
219 |
if (value_type == enumerated_value_symtable.end_value()) |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
220 |
enumerated_value_symtable.insert(symbol->value, current_enumerated_type); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
221 |
else if (value_type != NULL) |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
222 |
enumerated_value_symtable.set(symbol->value, NULL); |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
223 |
} |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
224 |
return NULL; |
66cd5d9893dd
Add support for finding type of a constant enumerated value and managing conflict between same identifiers defined in different enumerated data types
laurent
parents:
279
diff
changeset
|
225 |
} |
181 | 226 |
|
227 |
/* identifier ':' array_spec_init */ |
|
228 |
void *visit(array_type_declaration_c *symbol) { |
|
229 |
TRACE("array_type_declaration_c"); |
|
230 |
type_symtable.insert(symbol->identifier, symbol->array_spec_init); |
|
231 |
return NULL; |
|
232 |
} |
|
233 |
||
234 |
||
235 |
/* simple_type_name ':' simple_spec_init */ |
|
236 |
void *visit(simple_type_declaration_c *symbol) { |
|
237 |
TRACE("simple_type_declaration_c"); |
|
238 |
type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init); |
|
239 |
return NULL; |
|
240 |
} |
|
241 |
||
242 |
||
243 |
/* structure_type_name ':' structure_specification */ |
|
244 |
void *visit(structure_type_declaration_c *symbol) { |
|
245 |
TRACE("structure_type_declaration_c"); |
|
246 |
type_symtable.insert(symbol->structure_type_name, symbol->structure_specification); |
|
247 |
return NULL; |
|
248 |
} |
|
249 |
||
250 |
||
251 |
||
252 |
/*********************/ |
|
253 |
/* B 1.4 - Variables */ |
|
254 |
/*********************/ |
|
255 |
/********************************************/ |
|
256 |
/* B.1.4.1 Directly Represented Variables */ |
|
257 |
/********************************************/ |
|
258 |
/*************************************/ |
|
259 |
/* B.1.4.2 Multi-element Variables */ |
|
260 |
/*************************************/ |
|
261 |
/******************************************/ |
|
262 |
/* B 1.4.3 - Declaration & Initialisation */ |
|
263 |
/******************************************/ |
|
264 |
/**************************************/ |
|
265 |
/* B.1.5 - Program organization units */ |
|
266 |
/**************************************/ |
|
267 |
/***********************/ |
|
268 |
/* B 1.5.1 - Functions */ |
|
269 |
/***********************/ |
|
270 |
public: |
|
271 |
/* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
272 |
/* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */ |
|
273 |
void *visit(function_declaration_c *symbol) { |
|
274 |
TRACE("function_declaration_c"); |
|
275 |
function_symtable.insert(symbol->derived_function_name, symbol); |
|
276 |
||
277 |
/* symbol->derived_function_name->accept(*this); */ /* Function name */ |
|
278 |
/* symbol->type_name->accept(*this); */ /* return data type */ |
|
279 |
/* symbol->var_declarations_list->accept(*this); */ /* Function parameters and variables */ |
|
280 |
/* symbol->function_body->accept(*this); */ /* Function body */ |
|
281 |
return NULL; |
|
282 |
} |
|
283 |
||
284 |
||
285 |
/*****************************/ |
|
286 |
/* B 1.5.2 - Function Blocks */ |
|
287 |
/*****************************/ |
|
288 |
public: |
|
289 |
/* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */ |
|
290 |
//SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused) |
|
291 |
void *visit(function_block_declaration_c *symbol) { |
|
292 |
TRACE("function_block_declaration_c"); |
|
293 |
function_block_type_symtable.insert(symbol->fblock_name, symbol); |
|
294 |
/* |
|
295 |
symbol->fblock_name->accept(*this); |
|
296 |
symbol->var_declarations->accept(*this); |
|
297 |
symbol->fblock_body->accept(*this); |
|
298 |
*/ |
|
299 |
return NULL; |
|
300 |
} |
|
301 |
||
302 |
||
303 |
/**********************/ |
|
304 |
/* B 1.5.3 - Programs */ |
|
305 |
/**********************/ |
|
306 |
public: |
|
307 |
/* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */ |
|
308 |
//SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused) |
|
309 |
void *visit(program_declaration_c *symbol) { |
|
310 |
TRACE("program_declaration_c"); |
|
311 |
program_type_symtable.insert(symbol->program_type_name, symbol); |
|
312 |
/* |
|
313 |
symbol->program_type_name->accept(*this); |
|
314 |
symbol->var_declarations->accept(*this); |
|
315 |
symbol->function_block_body->accept(*this); |
|
316 |
*/ |
|
317 |
return NULL; |
|
318 |
} |
|
319 |
||
320 |
}; /* populate_symtables_c */ |
|
321 |
||
322 |
||
323 |
||
324 |
||
325 |
||
326 |
void absyntax_utils_init(symbol_c *tree_root) { |
|
327 |
populate_symtables_c populate_symbols; |
|
328 |
||
329 |
tree_root->accept(populate_symbols); |
|
330 |
} |
|
331 |