mario@181: /*
msousa@265: * matiec - a compiler for the programming languages defined in IEC 61131-3
msousa@265: *
msousa@265: * Copyright (C) 2009-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
msousa@265: *
msousa@265: * This program is free software: you can redistribute it and/or modify
msousa@265: * it under the terms of the GNU General Public License as published by
msousa@265: * the Free Software Foundation, either version 3 of the License, or
msousa@265: * (at your option) any later version.
msousa@265: *
msousa@265: * This program is distributed in the hope that it will be useful,
msousa@265: * but WITHOUT ANY WARRANTY; without even the implied warranty of
msousa@265: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
msousa@265: * GNU General Public License for more details.
msousa@265: *
msousa@265: * You should have received a copy of the GNU General Public License
msousa@265: * along with this program. If not, see .
msousa@265: *
mario@181: *
mario@181: * This code is made available on the understanding that it will not be
mario@181: * used in safety-critical situations without a full and competent review.
mario@181: */
mario@181:
mario@181: /*
msousa@265: * An IEC 61131-3 compiler.
mario@181: *
mario@181: * Based on the
mario@181: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
mario@181: *
mario@181: */
mario@181:
mario@181:
mario@181: /*
mario@181: * This is the main stage 3a file.
mario@181: *
mario@181: * In stage 3a some helpful symbol tables are instanciated and populated.
mario@181: * These symbol tables wll then be used by stage3b and atage4 code generators.
mario@181: */
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181: // #include /* required for NULL */
mario@181: #include
mario@181: #include
mario@181: #include
mario@181: #include
mario@181: #include
mario@181: #include
msousa@366: #include /* required for strlen() */
msousa@350: #include /* required for atoi() */
conti@564: #include /* required for errno */
mario@181:
mario@181: #include "../util/symtable.hh"
mario@181: #include "../util/dsymtable.hh"
mario@181: #include "../absyntax/visitor.hh"
mario@181:
mario@181:
mario@181:
mario@181: //#define DEBUG
mario@181: #ifdef DEBUG
mario@181: #define TRACE(classname) printf("\n____%s____\n",classname);
mario@181: #else
mario@181: #define TRACE(classname)
mario@181: #endif
mario@181:
mario@181: #define ERROR error_exit(__FILE__,__LINE__)
mario@181: /* function defined in main.cc */
mario@181: extern void error_exit(const char *file_name, int line_no);
mario@181:
mario@181:
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181:
mario@181:
mario@181: /* returns 0 if the names are equal!! */
mario@181: /* NOTE: it must ignore case!! */
mario@181: int compare_identifiers(symbol_c *ident1, symbol_c *ident2) {
mario@181:
mario@181: token_c *name1 = dynamic_cast(ident1);
mario@181: token_c *name2 = dynamic_cast(ident2);
mario@181:
mario@181: if ((name1 == NULL) || (name2 == NULL))
mario@181: /* invalid identifiers... */
mario@181: return -1;
mario@181:
mario@181: if (strcasecmp(name1->value, name2->value) == 0)
mario@181: return 0;
mario@181:
mario@181: /* identifiers do not match! */
mario@181: return 1;
mario@181: }
mario@181:
mario@181:
msousa@350: /* extract the value of an integer from an integer_c object !! */
msousa@350: /* NOTE: it must ignore underscores! */
conti@587: int64_t extract_int64_value(symbol_c *sym, bool *overflow) {
msousa@366: std::string str = "";
msousa@366: integer_c *integer;
msousa@366: neg_integer_c * neg_integer;
conti@587: char *endptr;
conti@587: int64_t ret;
msousa@366:
msousa@366: if ((neg_integer = dynamic_cast(sym)) != NULL)
conti@587: return - extract_int64_value(neg_integer->exp, overflow);
conti@587:
msousa@366: if ((integer = dynamic_cast(sym)) == NULL) ERROR;
msousa@366:
msousa@366: for(unsigned int i = 0; i < strlen(integer->value); i++)
msousa@366: if (integer->value[i] != '_') str += integer->value[i];
msousa@366:
conti@587: errno = 0;
conti@587: ret = strtoll(str.c_str(), &endptr, 10);
conti@587: if (overflow != NULL)
conti@587: *overflow = (errno == ERANGE);
conti@587: if ((errno != 0) && (errno != ERANGE))
conti@587: ERROR;
conti@587:
conti@587: return ret;
conti@587: }
conti@587:
conti@587: uint64_t extract_uint64_value(symbol_c *sym, bool *overflow) {
conti@587: std::string str = "";
conti@587: integer_c *integer;
conti@587: neg_integer_c * neg_integer;
conti@587: char *endptr;
conti@587: uint64_t ret;
conti@587:
conti@587: if ((integer = dynamic_cast(sym)) == NULL) ERROR;
conti@587:
conti@587: for(unsigned int i = 0; i < strlen(integer->value); i++)
conti@587: if (integer->value[i] != '_') str += integer->value[i];
conti@587:
conti@587: errno = 0;
conti@587: ret = strtoull(str.c_str(), &endptr, 10);
conti@587: if (overflow != NULL)
conti@587: *overflow = (errno == ERANGE);
conti@587: if ((errno != 0) && (errno != ERANGE))
conti@587: ERROR;
conti@587:
conti@587: return ret;
msousa@350: }
msousa@350:
msousa@567:
msousa@567: /* extract the value of an hex integer from an hex_integer_c object !! */
msousa@567: /* NOTE: it must ignore underscores! */
conti@564: uint64_t extract_hex_value(symbol_c *sym) {
msousa@565: std::string str = "";
msousa@565: char *endptr;
msousa@565: hex_integer_c * hex_integer;
msousa@565: uint64_t ret;
msousa@565:
msousa@565: if ((hex_integer = dynamic_cast(sym)) == NULL) ERROR;
msousa@565: for(unsigned int i = 3; i < strlen(hex_integer->value); i++)
msousa@565: if (hex_integer->value[i] != '_') str += hex_integer->value[i];
msousa@567:
msousa@571: errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
msousa@565: ret = strtoull(str.c_str(), &endptr, 16);
msousa@565: if (errno != 0) ERROR;
msousa@565:
msousa@565: return ret;
conti@564: }
conti@564:
mario@181:
msousa@567: /* extract the value of a real from an real_c object !! */
msousa@567: /* NOTE: it must ignore underscores! */
msousa@576: /* From iec_bison.yy
msousa@576: * real:
msousa@576: * real_token {$$ = new real_c($1, locloc(@$));}
msousa@576: * | fixed_point_token {$$ = new real_c($1, locloc(@$));}
msousa@576: *
msousa@576: * From iec_flex.ll
msousa@576: * {real} {yylval.ID=strdup(yytext); return real_token;}
msousa@576: * {fixed_point} {yylval.ID=strdup(yytext); return fixed_point_token;}
msousa@576: *
msousa@576: * real {integer}\.{integer}{exponent}
msousa@576: * fixed_point {integer}\.{integer}
msousa@576: * exponent [Ee]([+-]?){integer}
msousa@576: * integer {digit}((_?{digit})*)
msousa@576: */
conti@587: real64_t extract_real_value(symbol_c *sym, bool *overflow) {
msousa@567: std::string str = "";
msousa@567: real_c * real_sym;
msousa@576: real64_t ret;
msousa@567:
msousa@567: if ((real_sym = dynamic_cast(sym)) == NULL) ERROR;
msousa@576: for(unsigned int i = 0; i < strlen(real_sym->value); i++)
msousa@567: if (real_sym->value[i] != '_') str += real_sym->value[i];
msousa@567:
msousa@571: errno = 0; // since strtoXX() may legally return 0, we must set errno to 0 to detect errors correctly!
msousa@568: #if (real64_t == float)
msousa@568: ret = strtof(str.c_str(), NULL);
msousa@568: #elif (real64_t == double)
msousa@568: ret = strtod(str.c_str(), NULL);
msousa@568: #elif (real64_t == long_double)
msousa@568: ret = strtold(str.c_str(), NULL);
msousa@568: #else
msousa@568: #error Could not determine which data type is being used for real64_t (defined in absyntax.hh). Aborting!
msousa@568: #endif
msousa@576: if (overflow != NULL)
msousa@576: *overflow = (errno == ERANGE);
msousa@576: if ((errno != 0) && (errno != ERANGE))
msousa@576: ERROR;
msousa@567:
msousa@567: return ret;
msousa@567: }
msousa@567:
msousa@567:
msousa@366:
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181:
mario@181:
mario@181:
mario@181: /* A symbol table with all globally declared functions... */
mario@181: function_declaration_c null_symbol1(NULL,NULL,NULL,NULL);
mario@181: dsymtable_c function_symtable;
mario@181:
mario@181: /* A symbol table with all globally declared functions block types... */
mario@181: function_block_declaration_c null_symbol2(NULL,NULL,NULL);
mario@181: symtable_c function_block_type_symtable;
mario@181:
mario@181: /* A symbol table with all globally declared program types... */
mario@181: program_declaration_c null_symbol3(NULL,NULL,NULL);
mario@181: symtable_c program_type_symtable;
mario@181:
mario@181: /* A symbol table with all user declared type definitions... */
mario@181: /* Note that function block types and program types have their
mario@181: * own symbol tables, so do not get placed in this symbol table!
msousa@338: *
msousa@338: * The symbol_c * associated to the value will point to the data type declaration.
mario@181: */
mario@181: symbol_c null_symbol4;
mario@181: symtable_c type_symtable;
mario@181:
laurent@328: /* A symbol table with all values declared for enumerated type... */
msousa@338: /* Notes:
msousa@338: * - if the value is defined multiple times the value
laurent@328: * is the null pointer.
msousa@338: *
msousa@338: * - The stored symbol_c * associated to the value points to the enumerated_type_name
msousa@338: * (i.e. the name of the enumerated data type) in which the the value/identifier
msousa@338: * is used/declared.
msousa@338: *
msousa@338: * - We could re-use the null_symbol4 object, but it is safer to use a distinct object
msousa@338: * (i.e. it might make it easier to find strange bugs).
msousa@338: */
msousa@338: symbol_c null_symbol5;
msousa@338: symtable_c enumerated_value_symtable;
mario@181:
mario@181:
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181: /***********************************************************************/
mario@181:
mario@181:
mario@181: class populate_symtables_c: public iterator_visitor_c {
mario@181:
laurent@328: private:
laurent@328: symbol_c *current_enumerated_type;
laurent@328:
laurent@328: public:
laurent@328: populate_symtables_c(void) {
laurent@328: current_enumerated_type = NULL;
laurent@328: };
mario@181: virtual ~populate_symtables_c(void) {}
mario@181:
mario@181:
mario@181: public:
mario@181:
mario@181: /*************************/
mario@181: /* B.1 - Common elements */
mario@181: /*************************/
mario@181: /*******************************************/
mario@181: /* B 1.1 - Letters, digits and identifiers */
mario@181: /*******************************************/
mario@181: /*********************/
mario@181: /* B 1.2 - Constants */
mario@181: /*********************/
mario@181: /******************************/
mario@181: /* B 1.2.1 - Numeric Literals */
mario@181: /******************************/
mario@181: /*******************************/
mario@181: /* B.1.2.2 Character Strings */
mario@181: /*******************************/
mario@181: /***************************/
mario@181: /* B 1.2.3 - Time Literals */
mario@181: /***************************/
mario@181: /************************/
mario@181: /* B 1.2.3.1 - Duration */
mario@181: /************************/
mario@181: /************************************/
mario@181: /* B 1.2.3.2 - Time of day and Date */
mario@181: /************************************/
mario@181: /**********************/
mario@181: /* B.1.3 - Data types */
mario@181: /**********************/
mario@181: /***********************************/
mario@181: /* B 1.3.1 - Elementary Data Types */
mario@181: /***********************************/
mario@181: /********************************/
mario@181: /* B.1.3.2 - Generic data types */
mario@181: /********************************/
mario@181: /********************************/
mario@181: /* B 1.3.3 - Derived data types */
mario@181: /********************************/
mario@181:
mario@181: /* subrange_type_name ':' subrange_spec_init */
mario@181: void *visit(subrange_type_declaration_c *symbol) {
mario@181: TRACE("subrange_type_declaration_c");
mario@181: type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* enumerated_type_name ':' enumerated_spec_init */
mario@181: void *visit(enumerated_type_declaration_c *symbol) {
mario@181: TRACE("enumerated_type_declaration_c");
mario@181: type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init);
laurent@328: current_enumerated_type = symbol->enumerated_type_name;
laurent@328: symbol->enumerated_spec_init->accept(*this);
laurent@328: current_enumerated_type = NULL;
laurent@328: return NULL;
laurent@328: }
laurent@328:
laurent@328: /* enumerated_specification ASSIGN enumerated_value */
laurent@328: void *visit(enumerated_spec_init_c *symbol) {
laurent@328: return symbol->enumerated_specification->accept(*this);
laurent@328: }
laurent@328:
laurent@328: /* [enumerated_type_name '#'] identifier */
laurent@328: void *visit(enumerated_value_c *symbol) {
laurent@328: if (current_enumerated_type != NULL) {
laurent@328: if (symbol->type != NULL) ERROR;
laurent@328:
laurent@328: symbol_c *value_type = enumerated_value_symtable.find_value(symbol->value);
msousa@338: /* NOTE: The following condition checks whether the same identifier is used more than once
msousa@338: * when defining the enumerated values of the type declaration of the new enumerated type.
msousa@338: * If this occurs, then the program beeing compiled contains a semantic error, which
msousa@338: * must be caught and reported by the semantic analyser. However, since
msousa@338: * this code is run before the semantic analyser, we must not yet raise the ERROR (internal
msousa@338: * compiler error message).
msousa@338: * For this reason, the follosing check is commented out.
msousa@338: */
msousa@338: /* if (value_type == current_enumerated_type) ERROR; */
laurent@328:
laurent@328: if (value_type == enumerated_value_symtable.end_value())
msousa@338: /* This identifier has not yet been used in any previous declaration of an enumeration data type.
msousa@338: * so we add it to the symbol table.
msousa@338: */
laurent@328: enumerated_value_symtable.insert(symbol->value, current_enumerated_type);
laurent@328: else if (value_type != NULL)
msousa@338: /* This identifier has already been used in a previous declaration of an enumeration data type.
msousa@338: * so we set the symbol in symbol table pointing to NULL.
msousa@338: */
laurent@328: enumerated_value_symtable.set(symbol->value, NULL);
laurent@328: }
laurent@328: return NULL;
laurent@328: }
mario@181:
mario@181: /* identifier ':' array_spec_init */
mario@181: void *visit(array_type_declaration_c *symbol) {
mario@181: TRACE("array_type_declaration_c");
mario@181: type_symtable.insert(symbol->identifier, symbol->array_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* simple_type_name ':' simple_spec_init */
mario@181: void *visit(simple_type_declaration_c *symbol) {
mario@181: TRACE("simple_type_declaration_c");
mario@181: type_symtable.insert(symbol->simple_type_name, symbol->simple_spec_init);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /* structure_type_name ':' structure_specification */
mario@181: void *visit(structure_type_declaration_c *symbol) {
mario@181: TRACE("structure_type_declaration_c");
mario@181: type_symtable.insert(symbol->structure_type_name, symbol->structure_specification);
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
msousa@433: /* string_type_name ':' elementary_string_type_name string_type_declaration_size string_type_declaration_init */
msousa@433: // SYM_REF4(string_type_declaration_c, string_type_name,
msousa@433: // elementary_string_type_name,
msousa@433: // string_type_declaration_size,
msousa@433: // string_type_declaration_init) /* may be == NULL! */
msousa@433: void *visit(string_type_declaration_c *symbol) {
msousa@433: TRACE("string_type_declaration_c");
msousa@433: type_symtable.insert(symbol->string_type_name, symbol);
msousa@433: return NULL;
msousa@433: }
msousa@433:
mario@181: /*********************/
mario@181: /* B 1.4 - Variables */
mario@181: /*********************/
mario@181: /********************************************/
mario@181: /* B.1.4.1 Directly Represented Variables */
mario@181: /********************************************/
mario@181: /*************************************/
mario@181: /* B.1.4.2 Multi-element Variables */
mario@181: /*************************************/
mario@181: /******************************************/
mario@181: /* B 1.4.3 - Declaration & Initialisation */
mario@181: /******************************************/
mario@181: /**************************************/
mario@181: /* B.1.5 - Program organization units */
mario@181: /**************************************/
mario@181: /***********************/
mario@181: /* B 1.5.1 - Functions */
mario@181: /***********************/
mario@181: public:
mario@181: /* FUNCTION derived_function_name ':' elementary_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
mario@181: /* | FUNCTION derived_function_name ':' derived_type_name io_OR_function_var_declarations_list function_body END_FUNCTION */
mario@181: void *visit(function_declaration_c *symbol) {
mario@181: TRACE("function_declaration_c");
mario@181: function_symtable.insert(symbol->derived_function_name, symbol);
mario@181:
mario@181: /* symbol->derived_function_name->accept(*this); */ /* Function name */
mario@181: /* symbol->type_name->accept(*this); */ /* return data type */
mario@181: /* symbol->var_declarations_list->accept(*this); */ /* Function parameters and variables */
mario@181: /* symbol->function_body->accept(*this); */ /* Function body */
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /*****************************/
mario@181: /* B 1.5.2 - Function Blocks */
mario@181: /*****************************/
mario@181: public:
mario@181: /* FUNCTION_BLOCK derived_function_block_name io_OR_other_var_declarations function_block_body END_FUNCTION_BLOCK */
mario@181: //SYM_REF4(function_block_declaration_c, fblock_name, var_declarations, fblock_body, unused)
mario@181: void *visit(function_block_declaration_c *symbol) {
mario@181: TRACE("function_block_declaration_c");
mario@181: function_block_type_symtable.insert(symbol->fblock_name, symbol);
mario@181: /*
mario@181: symbol->fblock_name->accept(*this);
mario@181: symbol->var_declarations->accept(*this);
mario@181: symbol->fblock_body->accept(*this);
mario@181: */
mario@181: return NULL;
mario@181: }
mario@181:
mario@181:
mario@181: /**********************/
mario@181: /* B 1.5.3 - Programs */
mario@181: /**********************/
mario@181: public:
mario@181: /* PROGRAM program_type_name program_var_declarations_list function_block_body END_PROGRAM */
mario@181: //SYM_REF4(program_declaration_c, program_type_name, var_declarations, function_block_body, unused)
mario@181: void *visit(program_declaration_c *symbol) {
mario@181: TRACE("program_declaration_c");
mario@181: program_type_symtable.insert(symbol->program_type_name, symbol);
mario@181: /*
mario@181: symbol->program_type_name->accept(*this);
mario@181: symbol->var_declarations->accept(*this);
mario@181: symbol->function_block_body->accept(*this);
mario@181: */
mario@181: return NULL;
mario@181: }
mario@181:
mario@181: }; /* populate_symtables_c */
mario@181:
mario@181:
mario@181:
mario@181:
mario@181:
mario@181: void absyntax_utils_init(symbol_c *tree_root) {
mario@181: populate_symtables_c populate_symbols;
mario@181:
mario@181: tree_root->accept(populate_symbols);
mario@181: }
mario@181: