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