absyntax_utils/search_il_label.cc
author mjsousa
Tue, 20 Aug 2013 11:15:40 +0100
changeset 834 783ef40344dd
parent 460 00f39a4b25fb
permissions -rw-r--r--
Add support for FB call semantics of 'S' and 'R' IL operators!
Remove segfaults when analysing buggy IL code (IL operators with no operands).
460
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     1
/*
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     3
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     4
 *  Copyright (C) 2012  Mario de Sousa (msousa@fe.up.pt)
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     5
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     6
 *  This program is free software: you can redistribute it and/or modify
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     7
 *  it under the terms of the GNU General Public License as published by
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     8
 *  the Free Software Foundation, either version 3 of the License, or
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
     9
 *  (at your option) any later version.
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    10
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    11
 *  This program is distributed in the hope that it will be useful,
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    14
 *  GNU General Public License for more details.
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    15
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    16
 *  You should have received a copy of the GNU General Public License
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    18
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    19
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    20
 * This code is made available on the understanding that it will not be
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    21
 * used in safety-critical situations without a full and competent review.
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    22
 */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    23
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    24
/*
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    25
 * An IEC 61131-3 compiler.
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    26
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    27
 * Based on the
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    28
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    29
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    30
 */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    31
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    32
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    33
/*
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    34
 *  Search for a specific label in an IL list. 
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    35
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    36
 *  when instantiated, must be given a pointer to one of the following
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    37
 *     - function_declaration_c
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    38
 *     - function_block_declaration_c
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    39
 *     - program_declaration_c
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    40
 *     - instruction_list_c
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    41
 *
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    42
 * which is where all calls to search for a specific label will look for said label.
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    43
 */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    44
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    45
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    46
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    47
#include "absyntax_utils.hh"
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    48
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    49
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    50
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    51
/* set to 1 to see debug info during execution */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    52
static int debug = 0;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    53
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    54
search_il_label_c::search_il_label_c(symbol_c *search_scope) {
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    55
  this->search_scope = search_scope;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    56
  this->search_label = NULL;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    57
}
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    58
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    59
search_il_label_c::~search_il_label_c(void) {
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    60
}
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    61
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    62
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    63
il_instruction_c *search_il_label_c::find_label(const char *label) {
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    64
  return find_label(new identifier_c(label));
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    65
}
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    66
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    67
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    68
il_instruction_c *search_il_label_c::find_label(symbol_c *label) {
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    69
  search_label = label;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    70
  il_instruction_c *res = (il_instruction_c *)search_scope->accept(*this);
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    71
  search_label = NULL;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    72
  return res;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    73
}
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    74
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    75
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    76
/****************************************/
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    77
/* B.2 - Language IL (Instruction List) */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    78
/****************************************/
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    79
/***********************************/
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    80
/* B 2.1 Instructions and Operands */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    81
/***********************************/
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    82
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    83
/* | label ':' [il_incomplete_instruction] eol_list */
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    84
// SYM_REF2(il_instruction_c, label, il_instruction)
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    85
// void *visit(instruction_list_c *symbol);
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    86
void *search_il_label_c::visit(il_instruction_c *symbol) {
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    87
// printf("search_il_label_c::visit(il_instruction_c *symbol): searching for %s\n", ((identifier_c *)search_label)->value);
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    88
	if (NULL != symbol->label)
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    89
		if (compare_identifiers(search_label, symbol->label) == 0)
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    90
			return symbol;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    91
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    92
	return NULL;
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    93
}
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    94
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    95
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    96
00f39a4b25fb Add missing files.
Mario de Sousa <msousa@fe.up.pt>
parents:
diff changeset
    97