msousa@678: /* msousa@678: * matiec - a compiler for the programming languages defined in IEC 61131-3 msousa@678: * Copyright (C) 2012 Mario de Sousa (msousa@fe.up.pt) msousa@678: * msousa@678: * This program is free software: you can redistribute it and/or modify msousa@678: * it under the terms of the GNU General Public License as published by msousa@678: * the Free Software Foundation, either version 3 of the License, or msousa@678: * (at your option) any later version. msousa@678: * msousa@678: * This program is distributed in the hope that it will be useful, msousa@678: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@678: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@678: * GNU General Public License for more details. msousa@678: * msousa@678: * You should have received a copy of the GNU General Public License msousa@678: * along with this program. If not, see . msousa@678: * msousa@678: * msousa@678: * This code is made available on the understanding that it will not be msousa@678: * used in safety-critical situations without a full and competent review. msousa@678: */ msousa@678: msousa@678: /* msousa@678: * An IEC 61131-3 compiler. msousa@678: * msousa@678: * Based on the msousa@678: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) msousa@678: * msousa@678: */ msousa@678: msousa@678: msousa@678: /* msousa@678: * Some classes to help with debuging. msousa@678: * msousa@678: * These classes will print out the current state of a symbol or a portion of the Abstract Syntax Tree. msousa@678: */ msousa@678: msousa@678: /* TODO: Use a class similar to stage4out_c so that we can have nice indentation when printing an AST msousa@678: * Create a template so that we can TRACE the execution of other visitor classes doing usefull work! msousa@678: */ msousa@678: msousa@678: msousa@678: msousa@678: #include msousa@678: #include /* required for NULL */ msousa@678: #include "absyntax_utils.hh" msousa@725: #include "../absyntax/visitor.hh" msousa@725: msousa@725: msousa@725: msousa@725: mjsousa@979: static void dump_cvalue(const_value_c const_value) { mjsousa@979: fprintf(stderr, "constv{"); mjsousa@979: if (const_value._real64.is_valid ()) fprintf(stderr, "%f", const_value._real64.get()); mjsousa@979: else if (const_value._real64.is_overflow()) fprintf(stderr, "ov"); mjsousa@979: else if (const_value._real64.is_nonconst()) fprintf(stderr, "nc"); mjsousa@979: else fprintf(stderr, "?"); mjsousa@979: fprintf(stderr, ", i="); mjsousa@979: if (const_value. _int64.is_valid ()) fprintf(stderr, "%"PRId64"", const_value. _int64.get()); mjsousa@979: else if (const_value. _int64.is_overflow()) fprintf(stderr, "ov"); mjsousa@979: else if (const_value. _int64.is_nonconst()) fprintf(stderr, "nc"); mjsousa@979: else fprintf(stderr, "?"); mjsousa@979: fprintf(stderr, ", u="); mjsousa@979: if (const_value._uint64.is_valid ()) fprintf(stderr, "%"PRIu64"", const_value._uint64.get()); mjsousa@979: else if (const_value._uint64.is_overflow()) fprintf(stderr, "ov"); mjsousa@979: else if (const_value._uint64.is_nonconst()) fprintf(stderr, "nc"); mjsousa@979: else fprintf(stderr, "?"); mjsousa@979: fprintf(stderr, ", b="); mjsousa@979: if (const_value. _bool.is_valid ()) fprintf(stderr, "%d", const_value. _bool.get()?1:0); mjsousa@979: else if (const_value. _bool.is_overflow()) fprintf(stderr, "ov"); mjsousa@979: else if (const_value. _bool.is_nonconst()) fprintf(stderr, "nc"); mjsousa@979: else fprintf(stderr, "?"); mjsousa@979: fprintf(stderr, "}"); mjsousa@979: } mjsousa@979: msousa@725: msousa@725: msousa@725: /*********************************/ msousa@725: /* Class to print a symbol */ msousa@725: /*********************************/ msousa@725: msousa@725: msousa@725: class print_symbol_c: public fcall_visitor_c { msousa@725: public: msousa@725: static void print(symbol_c *symbol); msousa@725: msousa@725: protected: msousa@725: void fcall(symbol_c *symbol); msousa@725: /* AST symbols with extra data have their own specialised methods for printing that data */ msousa@725: void *visit(il_instruction_c *symbol); msousa@725: msousa@725: private: msousa@725: static print_symbol_c *singleton; msousa@725: msousa@725: void dump_symbol(symbol_c* symbol); msousa@725: }; msousa@678: msousa@678: msousa@678: msousa@678: msousa@678: print_symbol_c *print_symbol_c::singleton = NULL; msousa@678: msousa@678: msousa@678: void print_symbol_c::print(symbol_c* symbol) { msousa@678: if (NULL == singleton) singleton = new print_symbol_c(); msousa@678: if (NULL == singleton) ERROR; msousa@678: msousa@678: symbol->accept(*singleton); msousa@678: } msousa@678: msousa@678: msousa@678: msousa@678: msousa@678: msousa@678: void print_symbol_c::fcall(symbol_c* symbol) { msousa@678: dump_symbol(symbol); msousa@678: fprintf(stderr, "\n"); msousa@678: } msousa@678: msousa@678: mjsousa@979: msousa@678: void print_symbol_c::dump_symbol(symbol_c* symbol) { msousa@809: fprintf(stderr, "(%s->%03d:%03d..%03d:%03d) \t%s\t", symbol->first_file, symbol->first_line, symbol->first_column, symbol->last_line, symbol->last_column, symbol->absyntax_cname()); msousa@678: msousa@678: fprintf(stderr, " datatype="); msousa@678: if (NULL == symbol->datatype) msousa@678: fprintf(stderr, "NULL\t\t"); conti@735: else { conti@735: fprintf(stderr, "%s", symbol->datatype->absyntax_cname()); conti@735: } msousa@678: fprintf(stderr, "\t<-{"); msousa@678: if (symbol->candidate_datatypes.size() == 0) { msousa@678: fprintf(stderr, "\t\t\t\t\t"); msousa@678: } else if (symbol->candidate_datatypes.size() <= 2) { msousa@678: for (unsigned int i = 0; i < 2; i++) msousa@678: if (i < symbol->candidate_datatypes.size()) msousa@678: fprintf(stderr, " %s,", symbol->candidate_datatypes[i]->absyntax_cname()); msousa@678: else msousa@678: fprintf(stderr, "\t\t\t"); msousa@678: } else { msousa@736: fprintf(stderr, "(%lu)\t\t\t\t\t", (unsigned long int)symbol->candidate_datatypes.size()); msousa@678: } mjsousa@979: fprintf(stderr, "}\t "); msousa@731: msousa@731: /* print the const values... */ mjsousa@979: dump_cvalue(symbol->const_value); mjsousa@979: fprintf(stderr, "\t"); msousa@678: } msousa@678: msousa@678: msousa@678: msousa@678: void *print_symbol_c::visit(il_instruction_c *symbol) { msousa@678: dump_symbol(symbol); msousa@678: msousa@736: /* NOTE: std::map.size() returns a size_type, whose type is dependent on compiler/platform. To be portable, we need to do an explicit type cast. */ msousa@736: fprintf(stderr, " next_il_=%lu ", (unsigned long int)symbol->next_il_instruction.size()); msousa@736: fprintf(stderr, " prev_il_=%lu ", (unsigned long int)symbol->prev_il_instruction.size()); msousa@678: msousa@678: if (symbol->prev_il_instruction.size() == 0) msousa@678: fprintf(stderr, "(----,"); msousa@678: else if (symbol->prev_il_instruction[0]->datatype == NULL) msousa@678: fprintf(stderr, "(NULL,"); msousa@678: else if (!get_datatype_info_c::is_type_valid(symbol->prev_il_instruction[0]->datatype)) msousa@678: fprintf(stderr, "(****,"); msousa@678: else msousa@678: fprintf(stderr, "( ,"); msousa@678: msousa@678: if (symbol->next_il_instruction.size() == 0) msousa@678: fprintf(stderr, "----)"); msousa@678: else if (symbol->next_il_instruction[0]->datatype == NULL) msousa@678: fprintf(stderr, "NULL)"); msousa@678: else if (!get_datatype_info_c::is_type_valid(symbol->next_il_instruction[0]->datatype)) msousa@678: fprintf(stderr, "****)"); msousa@678: else msousa@678: fprintf(stderr, " )"); msousa@678: msousa@678: fprintf(stderr, "\n"); msousa@678: msousa@678: return NULL; msousa@678: }; msousa@678: msousa@678: msousa@678: msousa@678: msousa@678: msousa@725: /*********************************/ msousa@725: /* Class to print an AST */ msousa@725: /*********************************/ msousa@725: msousa@725: class print_ast_c: public fcall_iterator_visitor_c { msousa@725: public: msousa@725: static void print(symbol_c *symbol); msousa@725: static void print(const char *str); msousa@725: msousa@725: protected: msousa@725: void prefix_fcall(symbol_c *symbol); msousa@725: void suffix_fcall(symbol_c *symbol); msousa@725: msousa@725: private: msousa@725: static print_ast_c *singleton; msousa@725: }; msousa@678: msousa@678: msousa@678: msousa@678: msousa@678: print_ast_c *print_ast_c::singleton = NULL; msousa@678: msousa@678: msousa@678: void print_ast_c::print(symbol_c* symbol) { msousa@678: if (NULL == singleton) singleton = new print_ast_c(); msousa@678: if (NULL == singleton) ERROR; msousa@678: msousa@678: symbol->accept(*singleton); msousa@678: } msousa@678: msousa@725: msousa@725: void print_ast_c::print(const char *str) { conti@767: fprintf(stderr, "%s", str); msousa@725: } msousa@725: msousa@678: msousa@678: void print_ast_c::prefix_fcall(symbol_c* symbol) {print_symbol_c::print(symbol);} msousa@678: void print_ast_c::suffix_fcall(symbol_c* symbol) {} msousa@678: msousa@678: msousa@678: msousa@678: msousa@678: msousa@725: /*********************************/ msousa@725: /* The DEBUG class */ msousa@725: /*********************************/ msousa@725: msousa@725: msousa@725: msousa@725: msousa@725: void debug_c::print(const char *str) { conti@767: fprintf(stderr, "%s", str); msousa@725: } msousa@725: mjsousa@979: void debug_c::print(const_value_c cvalue) { mjsousa@979: dump_cvalue(cvalue); mjsousa@979: } mjsousa@979: msousa@725: void debug_c::print(symbol_c *symbol) { msousa@725: print_symbol_c::print(symbol); msousa@725: } msousa@725: msousa@725: void debug_c::print_ast(symbol_c *symbol) { msousa@725: print_ast_c::print(symbol); msousa@725: } msousa@725: msousa@725: msousa@725: msousa@725: msousa@725: