etisserant@0: /* msousa@267: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant msousa@267: * msousa@267: * This program is free software: you can redistribute it and/or modify msousa@267: * it under the terms of the GNU General Public License as published by msousa@267: * the Free Software Foundation, either version 3 of the License, or msousa@267: * (at your option) any later version. msousa@267: * msousa@267: * This program is distributed in the hope that it will be useful, msousa@267: * but WITHOUT ANY WARRANTY; without even the implied warranty of msousa@267: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the msousa@267: * GNU General Public License for more details. msousa@267: * msousa@267: * You should have received a copy of the GNU General Public License msousa@267: * along with this program. If not, see . msousa@267: * etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* msousa@267: * An IEC 61131-3 compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * This file contains the code that stores the output generated etisserant@0: * by each specific version of the 4th stage. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: // #include /* required for NULL */ etisserant@0: #include etisserant@0: #include etisserant@27: #include etisserant@139: #include etisserant@0: etisserant@0: #include "stage4.hh" etisserant@0: etisserant@0: msousa@522: #define FIRST_(symbol1, symbol2) (((symbol1)->first_order < (symbol2)->first_order) ? (symbol1) : (symbol2)) msousa@522: #define LAST_(symbol1, symbol2) (((symbol1)->last_order > (symbol2)->last_order) ? (symbol1) : (symbol2)) msousa@522: #include msousa@522: msousa@522: void stage4err(const char *stage4_generator_id, symbol_c *symbol1, symbol_c *symbol2, const char *errmsg, ...) { msousa@522: va_list argptr; msousa@522: va_start(argptr, errmsg); /* second argument is last fixed pamater of stage4err() */ msousa@522: msousa@522: fprintf(stderr, "%s:%d-%d..%d-%d: error %s: ", msousa@522: FIRST_(symbol1,symbol2)->first_file, FIRST_(symbol1,symbol2)->first_line, FIRST_(symbol1,symbol2)->first_column, msousa@522: LAST_(symbol1,symbol2) ->last_line, LAST_(symbol1,symbol2) ->last_column, msousa@522: stage4_generator_id); msousa@522: vfprintf(stderr, errmsg, argptr); msousa@522: fprintf(stderr, "\n"); msousa@522: // error_count++; msousa@522: va_end(argptr); msousa@522: } etisserant@0: etisserant@0: etisserant@0: etisserant@27: stage4out_c::stage4out_c(std::string indent_level): etisserant@27: m_file(NULL) { etisserant@27: out = &std::cout; etisserant@0: this->indent_level = indent_level; etisserant@0: this->indent_spaces = ""; etisserant@0: } etisserant@0: lbessard@46: stage4out_c::stage4out_c(const char *dir, const char *radix, const char *extension, std::string indent_level) { etisserant@27: std::string filename(radix); etisserant@27: filename += "."; etisserant@27: filename += extension; lbessard@46: std::string filepath(""); lbessard@46: if (dir != NULL) { lbessard@46: filepath += dir; lbessard@46: filepath += "/"; lbessard@46: } lbessard@46: filepath += filename; lbessard@46: std::fstream *file = new std::fstream(filepath.c_str(), std::fstream::out); etisserant@41: if(file->fail()){ etisserant@41: std::cerr << "Cannot open " << filename << " for write access \n"; etisserant@41: exit(EXIT_FAILURE); etisserant@41: }else{ etisserant@41: std::cout << filename << "\n"; etisserant@41: } etisserant@27: out = file; etisserant@27: m_file = file; etisserant@27: this->indent_level = indent_level; etisserant@27: this->indent_spaces = ""; msousa@267: allow_output = true; etisserant@27: } etisserant@27: etisserant@27: stage4out_c::~stage4out_c(void) { etisserant@27: if(m_file) etisserant@27: { etisserant@27: m_file->close(); etisserant@27: delete m_file; etisserant@27: } etisserant@27: } etisserant@0: msousa@296: void stage4out_c::flush(void) { msousa@296: out->flush(); msousa@296: } msousa@296: msousa@267: void stage4out_c::enable_output(void) { msousa@267: allow_output = true; msousa@267: } msousa@267: msousa@267: void stage4out_c::disable_output(void) { msousa@267: allow_output = false; msousa@267: } etisserant@0: etisserant@0: void stage4out_c::indent_right(void) { etisserant@0: indent_spaces+=indent_level; etisserant@0: } etisserant@0: etisserant@0: void stage4out_c::indent_left(void) { etisserant@0: if (indent_spaces.length() >= indent_level.length()) etisserant@0: indent_spaces.erase(indent_spaces.length() - indent_level.length(), indent_level.length()); etisserant@0: else etisserant@0: indent_spaces.erase(); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *stage4out_c::print(const char *str) { msousa@267: if (!allow_output) return NULL; etisserant@27: *out << str; etisserant@0: return NULL; etisserant@0: } etisserant@0: lbessard@28: void *stage4out_c::print_integer(int integer) { msousa@267: if (!allow_output) return NULL; lbessard@28: *out << integer; lbessard@28: return NULL; lbessard@28: } etisserant@0: laurent@397: void *stage4out_c::print_long_integer(unsigned long l_integer, bool suffix) { laurent@397: if (!allow_output) return NULL; laurent@397: *out << l_integer; laurent@397: if (suffix) *out << "UL"; laurent@397: return NULL; laurent@397: } laurent@397: laurent@397: void *stage4out_c::print_long_long_integer(unsigned long long ll_integer, bool suffix) { laurent@397: if (!allow_output) return NULL; laurent@397: *out << ll_integer; laurent@397: if (suffix) *out << "ULL"; laurent@210: return NULL; laurent@210: } laurent@210: etisserant@0: void *stage4out_c::printupper(const char *str) { msousa@267: if (!allow_output) return NULL; etisserant@0: for (int i = 0; str[i] != '\0'; i++) etisserant@27: *out << (unsigned char)toupper(str[i]); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@26: void *stage4out_c::printlocation(const char *str) { msousa@267: if (!allow_output) return NULL; etisserant@27: *out << "__"; etisserant@26: for (int i = 0; str[i] != '\0'; i++) etisserant@26: if(str[i] == '.') etisserant@27: *out << '_'; etisserant@26: else etisserant@27: *out << (unsigned char)toupper(str[i]); etisserant@26: return NULL; etisserant@26: } etisserant@0: etisserant@55: void *stage4out_c::printlocation_comasep(const char *str) { msousa@267: if (!allow_output) return NULL; etisserant@55: *out << (unsigned char)toupper(str[0]); etisserant@55: *out << ','; etisserant@55: *out << (unsigned char)toupper(str[1]); etisserant@55: *out << ','; etisserant@55: for (int i = 2; str[i] != '\0'; i++) etisserant@55: if(str[i] == '.') etisserant@55: *out << ','; etisserant@55: else etisserant@55: *out << (unsigned char)toupper(str[i]); etisserant@55: return NULL; etisserant@55: } etisserant@55: etisserant@55: etisserant@0: void *stage4out_c::print(std::string str) { msousa@267: if (!allow_output) return NULL; etisserant@27: *out << str; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *stage4out_c::printupper(std::string str) { msousa@267: if (!allow_output) return NULL; etisserant@0: /* The string standard class does not have a converter member function to upper case. etisserant@0: * We have to do it ourselves, a character at a time... etisserant@0: */ etisserant@0: #if 0 etisserant@0: /* The C++ way of doint things... */ etisserant@0: for (string::const_iterator p = str.begin(); p != str.end(); ++p) etisserant@27: *out << (unsigned char)toupper(*p); etisserant@0: #else etisserant@0: /* Or more simply... */ etisserant@0: printupper(str.c_str()); etisserant@0: #endif etisserant@0: return NULL; etisserant@0: } ccb@202: ccb@202: etisserant@26: void *stage4out_c::printlocation(std::string str) { msousa@267: if (!allow_output) return NULL; etisserant@27: return printlocation(str.c_str()); etisserant@26: } etisserant@0: etisserant@0: etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* forward declarations... */ etisserant@0: /* These functions will be implemented in generate_XXX.cc */ lbessard@46: visitor_c *new_code_generator(stage4out_c *s4o, const char *builddir); etisserant@0: void delete_code_generator(visitor_c *code_generator); etisserant@0: etisserant@0: lbessard@46: int stage4(symbol_c *tree_root, const char *builddir) { etisserant@0: stage4out_c s4o; lbessard@46: visitor_c *generate_code = new_code_generator(&s4o, builddir); etisserant@0: etisserant@0: if (NULL == generate_code) etisserant@0: return -1; etisserant@0: etisserant@0: tree_root->accept(*generate_code); etisserant@0: etisserant@0: delete_code_generator(generate_code); etisserant@0: etisserant@0: return 0; etisserant@0: } etisserant@0: