stage3/stage3.cc
author Mario de Sousa <msousa@fe.up.pt>
Sun, 16 Apr 2017 08:46:58 +0100
changeset 1049 4d7183013481
parent 1000 556b74055518
permissions -rw-r--r--
merge
204
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
     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
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
     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
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
     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
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    22
 *
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    23
 * This code is made available on the understanding that it will not be
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    24
 * used in safety-critical situations without a full and competent review.
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    25
 */
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    26
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    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
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    29
 *
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    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
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    32
 *
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    33
 */
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    34
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    35
#include "stage3.hh"
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
    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"
677
740da3255d9d Remove all remaining NULL (undefined) datatypes from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
    40
#include "forced_narrow_candidate_datatypes.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
    41
#include "print_datatypes_error.hh"
508
dc6906338042 Add lvalue check class.
Manuele Conti <conti.ma@alice.it>
parents: 444
diff changeset
    42
#include "lvalue_check.hh"
560
13b5b7faa3d7 Renamed class, remove unused code, delete allocated objects.
mjsousa <msousa@fe.up.pt>
parents: 559
diff changeset
    43
#include "array_range_check.hh"
1000
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
    44
#include "case_elements_check.hh"
564
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents: 560
diff changeset
    45
#include "constant_folding.hh"
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents: 612
diff changeset
    46
#include "declaration_check.hh"
738
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    47
#include "enum_declaration_check.hh"
959
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
    48
#include "remove_forward_dependencies.hh"
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
    49
738
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    50
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    51
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    52
static int enum_declaration_check(symbol_c *tree_root){
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    53
    enum_declaration_check_c enum_declaration_check(NULL);
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    54
    tree_root->accept(enum_declaration_check);
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    55
    return enum_declaration_check.get_error_count();
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    56
}
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
    57
530
1eacd7afcab1 Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents: 516
diff changeset
    58
986
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    59
/* In order to correctly handle variable sized arrays
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    60
 * declaration_safety() must only be run after constant folding!
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    61
 *   NOTE that the dependency does not resides directly in declaration_check_c,
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    62
 *        but rather indirectly in the call to get_datatype_info_c::is_type_equal()
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    63
 *        which may in turn call get_datatype_info_c::is_arraytype_equal_relaxed()
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    64
 *
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    65
 * Example of a variable sized array:
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    66
 *   VAR_EXTERN CONSTANT max: INT; END_VAR;
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    67
 *   VAR_EXTERN xx: ARRAY [1..max] OF INT; END_VAR;
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
    68
 */
656
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents: 612
diff changeset
    69
static int declaration_safety(symbol_c *tree_root){
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents: 612
diff changeset
    70
    declaration_check_c declaration_check(tree_root);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents: 612
diff changeset
    71
    tree_root->accept(declaration_check);
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents: 612
diff changeset
    72
    return declaration_check.get_error_count();
45a796bce487 Start declaration check class.
Manuele Conti <conti.ma@alice.it>
parents: 612
diff changeset
    73
}
530
1eacd7afcab1 Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents: 516
diff changeset
    74
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
    75
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
    76
    flow_control_analysis_c flow_control_analysis(tree_root);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents: 560
diff changeset
    77
    tree_root->accept(flow_control_analysis);
dabffc3086dc Start constant_folding class.
Manuele Conti <conti.ma@alice.it>
parents: 560
diff changeset
    78
    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
    79
}
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
530
1eacd7afcab1 Fix comment. (thanks go to Manuele for pointing it put).
Mario de Sousa <msousa@fe.up.pt>
parents: 516
diff changeset
    81
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
    82
/* Constant folding assumes that flow control analysis has been completed!
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
    83
 * so be sure to call flow_control_analysis() before calling this function!
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
    84
 */
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 959
diff changeset
    85
static int constant_propagation(symbol_c *tree_root){
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 959
diff changeset
    86
    constant_propagation_c constant_propagation(tree_root);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 959
diff changeset
    87
    tree_root->accept(constant_propagation);
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 959
diff changeset
    88
    return constant_propagation.get_error_count();
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
    89
}
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
    90
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
    91
611
8e81d7db54be update comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
    92
/* Type safety analysis assumes that 
8e81d7db54be update comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
    93
 *    - flow control analysis 
8e81d7db54be update comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
    94
 *    - constant folding (constant check)
8e81d7db54be update comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
    95
 * has already been completed, so be sure to call those semantic checkers
8e81d7db54be update comments.
Mario de Sousa <msousa@fe.up.pt>
parents: 564
diff changeset
    96
 * 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
    97
 */
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
    98
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
    99
	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
   100
	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
   101
	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
   102
	tree_root->accept(narrow_candidate_datatypes);
444
92d40d2a7adc Update comments on general architecture.
Mario de Sousa <msousa@fe.up.pt>
parents: 443
diff changeset
   103
	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
   104
	tree_root->accept(print_datatypes_error);
677
740da3255d9d Remove all remaining NULL (undefined) datatypes from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   105
	forced_narrow_candidate_datatypes_c forced_narrow_candidate_datatypes(tree_root);
740da3255d9d Remove all remaining NULL (undefined) datatypes from IL code.
Mario de Sousa <msousa@fe.up.pt>
parents: 656
diff changeset
   106
	tree_root->accept(forced_narrow_candidate_datatypes);
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   107
	return print_datatypes_error.get_error_count();
204
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
   108
}
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
   109
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
   110
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
   111
/* 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
   112
 * 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
   113
 */
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   114
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
   115
	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
   116
	tree_root->accept(lvalue_check);
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   117
	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
   118
}
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   119
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
   120
/* Array range check assumes that constant folding has been completed!
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
   121
 * so be sure to call constant_folding() before calling this function!
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
   122
 */
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
   123
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
   124
	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
   125
	tree_root->accept(array_range_check);
13b5b7faa3d7 Renamed class, remove unused code, delete allocated objects.
mjsousa <msousa@fe.up.pt>
parents: 559
diff changeset
   126
	return array_range_check.get_error_count();
559
a3b8925e640c Start sematinc range check class.
Manuele Conti <conti.ma@alice.it>
parents: 530
diff changeset
   127
}
a3b8925e640c Start sematinc range check class.
Manuele Conti <conti.ma@alice.it>
parents: 530
diff changeset
   128
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
   129
1000
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   130
/* Case options check assumes that constant folding has been completed!
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   131
 * so be sure to call constant_folding() before calling this function!
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   132
 */
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   133
static int case_elements_check(symbol_c *tree_root){
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   134
	case_elements_check_c case_elements_check(tree_root);
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   135
	tree_root->accept(case_elements_check);
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   136
	return case_elements_check.get_error_count();
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   137
}
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   138
959
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   139
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   140
/* Removing forward dependencies only makes sense when stage1_2 is run with the pre-parsing option.
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   141
 * This algorithm has no dependencies on other stage 3 algorithms.
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   142
 * Typically this is run last, just to show that the remaining algorithms also do not depend on the fact that 
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   143
 * the library_c (i.e. the source code) does not contain forward dependencies.
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   144
 */
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   145
static int remove_forward_dependencies(symbol_c *tree_root, symbol_c **ordered_tree_root) {
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   146
	if (NULL != ordered_tree_root)    *ordered_tree_root = tree_root; // by default, consider tree_root already ordered
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   147
	if (!runtime_options.pre_parsing)  return 0;                      // No re-ordering necessary, just return
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   148
	  
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   149
	/* We need to re-order the elements in the library, to fix any forward references! */
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   150
	remove_forward_dependencies_c remove_forward_dependencies;
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   151
	symbol_c *new_tree_root = remove_forward_dependencies.create_new_tree(tree_root);
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   152
	if (NULL ==     new_tree_root)   ERROR;
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   153
	if (NULL != ordered_tree_root)   *ordered_tree_root = new_tree_root;
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   154
	return remove_forward_dependencies.get_error_count();
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   155
}
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   156
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   157
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   158
int stage3(symbol_c *tree_root, symbol_c **ordered_tree_root) {
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   159
	int error_count = 0;
738
e47cc8c954db Start to move enumeration declaration checking to its own class.
Mario de Sousa <msousa@fe.up.pt>
parents: 677
diff changeset
   160
	error_count += enum_declaration_check(tree_root);
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   161
	error_count += flow_control_analysis(tree_root);
984
634269b0f104 Break constant_folding_c in two classes: constant_folding_c and constant_propagation_c
mjsousa
parents: 959
diff changeset
   162
	error_count += constant_propagation(tree_root);
986
2064a22cc1f2 Correctly analyse external<->global var compatibility when using variable sized arrays (TODO: does not yet work for FBs!)
mjsousa
parents: 984
diff changeset
   163
	error_count += declaration_safety(tree_root);
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   164
	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
   165
	error_count += lvalue_check(tree_root);
612
c062ff18d04f Constant folding for IL.
Mario de Sousa <msousa@fe.up.pt>
parents: 611
diff changeset
   166
	error_count += array_range_check(tree_root);
1000
556b74055518 Add check for repeated elements in a CASE statement. Emit warnings (and not errors) if found.
mjsousa
parents: 986
diff changeset
   167
	error_count += case_elements_check(tree_root);
959
8bfcc8e62bd6 Fix C code generation when forward references (-p command line option) is on.
mjsousa
parents: 738
diff changeset
   168
	error_count += remove_forward_dependencies(tree_root, ordered_tree_root);
510
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   169
	
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   170
	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
   171
		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
   172
		return -1;
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   173
	}
9317e04c1dde Fixing check for assignment to output variables.
Mario de Sousa <msousa@fe.up.pt>
parents: 508
diff changeset
   174
	return 0;
204
8ffa211b7f9a Adding missing Stage3 files.
Catarina Boucinha <ccb@fe.up.pt>
parents:
diff changeset
   175
}