etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. 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: /* etisserant@0: * An IEC 61131-3 IL and ST 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: * Declare temporary variables to be later used as output parameters etisserant@0: * in function calls for which not all output parameters were etisserant@0: * defined in the original (st or il) source code. etisserant@0: * etisserant@0: * This is part of the 4th stage that generates etisserant@0: * a c++ source program equivalent to the IL and ST etisserant@0: * code. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: class temp_var_name_c { etisserant@0: private: etisserant@0: int counter; etisserant@0: etisserant@0: public: etisserant@0: void reset(void) {counter = 0;} etisserant@0: temp_var_name_c(void) {reset();} etisserant@0: etisserant@0: public: etisserant@0: std::string *new_name(void) { etisserant@0: std::string *new_str = new std::string(TEMP_VAR); etisserant@0: /* yikes!!! How to convert an int to a string elegantly??? etisserant@0: * Right now I (Mario) can only think of snprintf() etisserant@0: * C++ must have a more elegant method! etisserant@0: */ etisserant@0: int int_str_size = snprintf(NULL, 0, "%d", counter); etisserant@0: if (int_str_size <= 0) ERROR; etisserant@0: char *int_str = (char *)malloc(int_str_size+1); etisserant@0: if (snprintf(int_str, int_str_size+1, "%d", counter++) >= int_str_size+1) ERROR; etisserant@0: *new_str += int_str; etisserant@0: free(int_str); etisserant@0: return new_str; 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: /* Some function calls in the body of functions or function blocks etisserant@0: * may leave some parameters to their default values, and etisserant@0: * ignore some output parameters of the function being called. etisserant@0: * Our conversion of ST functions to C++ does not contemplate that, etisserant@0: * i.e. each called function must get all it's input and output etisserant@0: * parameters set correctly. etisserant@0: * For input parameters we merely need to call the function with etisserant@0: * the apropriate default value, but for output parameters etisserant@0: * we must create temporary variables to hold the output value. etisserant@0: * etisserant@0: * We declare all the temporary output variables at the begining of etisserant@0: * the body of each function or function block, and use them as etisserant@0: * in function calls later on as they become necessary... etisserant@0: * Note that we cannot create these variables just before a function etisserant@0: * call, as the function call itself may be integrated within an etisserant@0: * expression, or another function call! etisserant@0: * etisserant@0: * The variables are declared in the exact same order in which they etisserant@0: * will be used later on during the function calls, which allows us etisserant@0: * to simply re-create the name that was used for the temporary variable etisserant@0: * instead of keeping it in some list. etisserant@0: * The names are recreated by the temp_var_name_factory, after reset() etisserant@0: * has been called! etisserant@0: * etisserant@0: * This function will genertae code similar to... etisserant@0: * etisserant@0: * INT __TMP_0 = 23; etisserant@0: * REAL __TMP_1 = 45.5; etisserant@0: * ... etisserant@0: */ etisserant@0: etisserant@0: class generate_cc_tempvardecl_c: generate_cc_typedecl_c { etisserant@0: public: etisserant@0: generate_cc_tempvardecl_c(stage4out_c *s4o_ptr): generate_cc_typedecl_c(s4o_ptr) {} etisserant@0: etisserant@0: void generate(symbol_c *body, temp_var_name_c *temp_var_name_factory) { etisserant@0: temp_var_name_factory->reset(); etisserant@0: function_call_iterator_c fcall_iterator(body); etisserant@0: for(symbol_c *finvocation = NULL; (finvocation = fcall_iterator.next()) != NULL;) { etisserant@0: /* get the name of the next function that gets called */ etisserant@0: identifier_c *fcalled_name = fcall_iterator.fname(); etisserant@0: /* get that function's declaration... */ etisserant@0: function_declaration_c *fdecl = function_symtable.find_value(fcalled_name); etisserant@0: if (fdecl == function_symtable.end_value()) ERROR; etisserant@0: /* create iterator to iterate through each of the called function's parameters... */ etisserant@0: function_param_iterator_c fp_iterator(fdecl); etisserant@0: etisserant@0: /* iterate through each of the called function's parameters... */ etisserant@0: identifier_c *param_name = NULL; etisserant@0: function_call_param_iterator_c function_call_param_iterator(finvocation); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: if (param_direction == function_param_iterator_c::direction_in) etisserant@0: /* ignore input only parameters... etisserant@0: * we do not need to create temporary variables for these! etisserant@0: */ etisserant@0: continue; etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: symbol_c *param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: if (param_value != NULL) etisserant@0: /* ignore output parameters to which a variable is passed... etisserant@0: * we do not need to create temporary variables for these! etisserant@0: */ etisserant@0: continue; etisserant@0: etisserant@0: symbol_c *param_type = fp_iterator.param_type(); etisserant@0: etisserant@0: /* get the parameter's default value */ etisserant@0: param_value = fp_iterator.default_value(); etisserant@0: etisserant@0: /* If no default value specified in function declaration, etisserant@0: * get the default value of this variable's type etisserant@0: */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); etisserant@0: if (param_value == NULL) ERROR; etisserant@0: etisserant@0: /* now declare a temporary variable, with the correct default value... */ etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: param_type->accept(*this); etisserant@0: s4o.print(" "); etisserant@0: etisserant@0: std::string *temp_var_name = temp_var_name_factory->new_name(); etisserant@0: s4o.print(*temp_var_name); etisserant@0: delete temp_var_name; etisserant@0: etisserant@0: s4o.print(" = "); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(";\n"); etisserant@0: } etisserant@0: } etisserant@0: temp_var_name_factory->reset(); etisserant@0: s4o.print("\n"); etisserant@0: } etisserant@0: };