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: * Conversion of st statements (i.e. ST 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: lbessard@16: /***********************************************************************/ lbessard@16: /***********************************************************************/ lbessard@16: /***********************************************************************/ lbessard@16: /***********************************************************************/ etisserant@0: etisserant@0: etisserant@0: class generate_cc_st_c: public generate_cc_typedecl_c { etisserant@0: etisserant@0: private: etisserant@0: /* When calling a function block, we must first find it's type, etisserant@0: * by searching through the declarations of the variables currently etisserant@0: * in scope. etisserant@0: * This class does just that... etisserant@0: * A new class is instantiated whenever we begin generating the code etisserant@0: * for a function block type declaration, or a program declaration. etisserant@0: * This object instance will then later be called while the etisserant@0: * function block's or the program's body is being handled. etisserant@0: * etisserant@0: * Note that functions cannot contain calls to function blocks, etisserant@0: * so we do not create an object instance when handling etisserant@0: * a function declaration. etisserant@0: */ etisserant@0: search_fb_instance_decl_c *search_fb_instance_decl; etisserant@0: lbessard@16: /* When compiling st code, it becomes necessary to determine the lbessard@16: * data type of st expressions. To do this, we must first find the lbessard@16: * st operand's declaration, within the scope of the function block lbessard@16: * or function currently being processed. lbessard@16: * The following object does just that... lbessard@16: * This object instance will then later be called while the lbessard@16: * remaining st code is being handled. lbessard@16: */ lbessard@16: search_expression_type_c *search_expression_type; etisserant@0: lbessard@25: search_varfb_instance_type_c *search_varfb_instance_type; lbessard@25: etisserant@0: public: etisserant@0: generate_cc_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) etisserant@0: : generate_cc_typedecl_c(s4o_ptr) { etisserant@0: search_fb_instance_decl = new search_fb_instance_decl_c(scope); lbessard@16: search_expression_type = new search_expression_type_c(scope); lbessard@25: search_varfb_instance_type = new search_varfb_instance_type_c(scope); etisserant@0: this->set_variable_prefix(variable_prefix); etisserant@0: } etisserant@0: etisserant@0: virtual ~generate_cc_st_c(void) { etisserant@0: delete search_fb_instance_decl; lbessard@16: delete search_expression_type; lbessard@25: delete search_varfb_instance_type; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: private: 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: temp_var_name_c temp_var_name_factory; etisserant@0: etisserant@0: public: etisserant@0: void generate(statement_list_c *stl) { etisserant@0: generate_cc_tempvardecl_c generate_cc_tempvardecl(&s4o); etisserant@0: generate_cc_tempvardecl.generate(stl, &temp_var_name_factory); etisserant@0: stl->accept(*this); etisserant@0: } etisserant@0: etisserant@0: private: lbessard@25: lbessard@25: /*********************/ lbessard@25: /* B 1.4 - Variables */ lbessard@25: /*********************/ lbessard@25: void *visit(symbolic_variable_c *symbol) { lbessard@25: unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); etisserant@26: if (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt) { lbessard@25: s4o.print("*("); lbessard@25: generate_cc_base_c::visit(symbol); lbessard@25: s4o.print(")"); lbessard@25: } lbessard@25: else { lbessard@25: generate_cc_base_c::visit(symbol); lbessard@25: } lbessard@25: return NULL; lbessard@25: } lbessard@25: etisserant@62: /********************************************/ etisserant@62: /* B.1.4.1 Directly Represented Variables */ etisserant@62: /********************************************/ etisserant@62: // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; etisserant@62: void *visit(direct_variable_c *symbol) { etisserant@62: TRACE("direct_variable_c"); etisserant@62: /* Do not use print_token() as it will change everything into uppercase */ etisserant@62: if (strlen(symbol->value) == 0) ERROR; etisserant@62: s4o.print("*("); etisserant@62: this->print_variable_prefix(); etisserant@62: s4o.printlocation(symbol->value + 1); etisserant@62: s4o.print(")"); etisserant@62: return NULL; etisserant@62: } etisserant@62: etisserant@0: /***************************************/ etisserant@0: /* B.3 - Language ST (Structured Text) */ etisserant@0: /***************************************/ etisserant@0: /***********************/ etisserant@0: /* B 3.1 - Expressions */ etisserant@0: /***********************/ lbessard@16: void *visit(or_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_bool_type(left_type)) lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " || "); lbessard@32: if (search_expression_type->is_binary_type(left_type)) lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " | "); lbessard@16: ERROR; lbessard@16: return NULL; lbessard@16: } lbessard@16: lbessard@16: void *visit(xor_expression_c *symbol) { lbessard@16: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@16: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_bool_type(left_type)) { lbessard@16: s4o.print("("); lbessard@16: symbol->l_exp->accept(*this); lbessard@16: s4o.print(" && !"); lbessard@16: symbol->r_exp->accept(*this); lbessard@16: s4o.print(") || (!"); lbessard@16: symbol->l_exp->accept(*this); lbessard@16: s4o.print(" && "); lbessard@16: symbol->r_exp->accept(*this); lbessard@16: s4o.print(")"); lbessard@16: } lbessard@32: if (search_expression_type->is_binary_type(left_type)) lbessard@16: return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ "); lbessard@32: ERROR; lbessard@32: return NULL; lbessard@32: } lbessard@32: lbessard@32: void *visit(and_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_bool_type(left_type)) lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " && "); lbessard@32: if (search_expression_type->is_binary_type(left_type)) lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " & "); lbessard@32: ERROR; lbessard@32: return NULL; lbessard@32: } lbessard@32: lbessard@32: void *visit(equ_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_time_type(left_type)) etisserant@41: return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp); lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " == "); lbessard@32: } lbessard@32: lbessard@32: void *visit(notequ_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_time_type(left_type)) etisserant@41: return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp); lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " != "); lbessard@32: } lbessard@32: lbessard@32: void *visit(lt_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_time_type(left_type)) etisserant@41: return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp); lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " < "); lbessard@32: } lbessard@32: lbessard@32: void *visit(gt_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_time_type(left_type)) etisserant@41: return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp); lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " > "); lbessard@32: } lbessard@32: lbessard@32: void *visit(le_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_time_type(left_type)) etisserant@41: return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp); lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= "); lbessard@32: } lbessard@32: lbessard@32: void *visit(ge_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_time_type(left_type)) etisserant@41: return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp); lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= "); lbessard@32: } lbessard@32: lbessard@32: void *visit(add_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c))) etisserant@41: return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " + "); lbessard@32: ERROR; lbessard@32: return NULL; lbessard@32: } lbessard@32: lbessard@32: void *visit(sub_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) || lbessard@32: (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c))) etisserant@41: return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " - "); lbessard@32: ERROR; lbessard@32: return NULL; lbessard@32: } lbessard@32: lbessard@32: void *visit(mul_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if ((typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_integer_type(right_type)) || lbessard@32: (typeid(*left_type) == typeid(time_type_name_c) && search_expression_type->is_real_type(right_type))) etisserant@41: return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " * "); lbessard@32: ERROR; lbessard@32: return NULL; lbessard@32: } lbessard@32: lbessard@32: void *visit(div_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) lbessard@32: return print_binary_expression(symbol->l_exp, symbol->r_exp, " / "); lbessard@32: ERROR; lbessard@32: return NULL; lbessard@32: } lbessard@32: lbessard@32: void *visit(mod_expression_c *symbol) { lbessard@32: symbol_c *left_type = search_expression_type->get_type(symbol->l_exp); lbessard@32: symbol_c *right_type = search_expression_type->get_type(symbol->r_exp); lbessard@32: if (!search_expression_type->is_same_type(left_type, right_type)) lbessard@32: ERROR; lbessard@32: if (search_expression_type->is_integer_type(left_type) || search_expression_type->is_real_type(left_type)) { lbessard@32: s4o.print("(("); lbessard@32: symbol->r_exp->accept(*this); lbessard@32: s4o.print(" == 0)?0:"); lbessard@32: print_binary_expression(symbol->l_exp, symbol->r_exp, " % "); lbessard@32: s4o.print(")"); lbessard@32: return NULL; lbessard@16: } lbessard@16: ERROR; lbessard@16: return NULL; lbessard@16: } lbessard@16: etisserant@0: /* TODO: power expression... */ etisserant@0: void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");} etisserant@0: void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, " -");} etisserant@64: void *visit(not_expression_c *symbol) {return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(symbol->exp)?"!":"~");} etisserant@0: etisserant@0: void *visit(function_invocation_c *symbol) { etisserant@0: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); lbessard@32: lbessard@32: if (f_decl == function_symtable.end_value()) { lbessard@32: /* The function called is not in the symtable, so we test if it is a lbessard@32: * standard function defined in standard */ lbessard@32: lbessard@32: function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); lbessard@32: if (current_function_type == function_none) ERROR; lbessard@32: lbessard@32: symbol_c *function_return_type = search_expression_type->get_type(symbol); lbessard@32: lbessard@32: function_call_param_iterator_c function_call_param_iterator(symbol); lbessard@32: lbessard@32: int nb_param = ((list_c *)symbol->parameter_assignment_list)->n; etisserant@33: etisserant@33: #include "st_code_gen.c" etisserant@33: etisserant@33: #if 0 lbessard@32: for(int current_param = 0; current_param < nb_param; current_param++) { lbessard@32: symbol_c *param_name = NULL; lbessard@32: switch (current_function_type) { lbessard@32: case function_add: lbessard@32: case function_and: lbessard@32: case function_or: lbessard@32: param_name = generate_param_name("IN%d", current_param + 1); lbessard@32: break; lbessard@32: case function_sub: lbessard@32: if (current_param < 2) lbessard@32: param_name = generate_param_name("IN%d", current_param + 1); lbessard@32: else lbessard@32: ERROR; lbessard@32: break; lbessard@32: default: ERROR; lbessard@32: } lbessard@32: lbessard@32: /* Get the value from a foo( = ) style call */ lbessard@32: symbol_c *param_value = function_call_param_iterator.search(param_name); lbessard@32: delete param_name; lbessard@32: lbessard@32: /* Get the value from a foo() style call */ lbessard@32: if (param_value == NULL) lbessard@32: param_value = function_call_param_iterator.next(); lbessard@32: lbessard@32: if (param_value == NULL) ERROR; lbessard@32: lbessard@32: switch (current_function_type) { lbessard@32: case function_add: lbessard@32: if (search_expression_type->is_time_type(function_return_type)) { lbessard@32: if (current_param == 0) { etisserant@41: s4o.print("__time_add("); lbessard@32: param_value->accept(*this); lbessard@32: } lbessard@32: else if (current_param == 1) { lbessard@32: s4o.print(", "); lbessard@32: param_value->accept(*this); lbessard@32: s4o.print(")"); lbessard@32: } lbessard@32: else ERROR; lbessard@32: } lbessard@32: else { lbessard@32: if (current_param == 0) lbessard@32: s4o.print("("); lbessard@32: else lbessard@32: s4o.print(" + "); lbessard@32: param_value->accept(*this); lbessard@32: if (current_param == nb_param - 1) lbessard@32: s4o.print(")"); lbessard@32: } lbessard@32: break; lbessard@32: case function_sub: lbessard@32: if (search_expression_type->is_time_type(function_return_type)) { lbessard@32: if (current_param == 0) { etisserant@41: s4o.print("__time_sub("); lbessard@32: param_value->accept(*this); lbessard@32: } lbessard@32: else if (current_param == 1) { lbessard@32: s4o.print(", "); lbessard@32: param_value->accept(*this); lbessard@32: s4o.print(")"); lbessard@32: } lbessard@32: else ERROR; lbessard@32: } lbessard@32: else { lbessard@32: if (current_param == 0) { lbessard@32: s4o.print("("); lbessard@32: param_value->accept(*this); lbessard@32: } lbessard@32: else if (current_param == 1) { lbessard@32: s4o.print(" - "); lbessard@32: param_value->accept(*this); lbessard@32: s4o.print(")"); lbessard@32: } lbessard@32: else ERROR; lbessard@32: } lbessard@32: break; lbessard@32: case function_and: lbessard@32: if (current_param == 0) lbessard@32: s4o.print("("); lbessard@32: else lbessard@32: if (search_expression_type->is_bool_type(function_return_type)) lbessard@32: s4o.print(" && "); lbessard@32: else lbessard@32: s4o.print(" & "); etisserant@0: param_value->accept(*this); lbessard@32: if (current_param == nb_param - 1) lbessard@32: s4o.print(")"); lbessard@32: break; lbessard@32: case function_or: lbessard@32: if (current_param == 0) lbessard@32: s4o.print("("); lbessard@32: else lbessard@32: if (search_expression_type->is_bool_type(function_return_type)) lbessard@32: s4o.print(" || "); lbessard@32: else lbessard@32: s4o.print(" | "); lbessard@32: param_value->accept(*this); lbessard@32: if (current_param == nb_param - 1) lbessard@32: s4o.print(")"); lbessard@32: break; lbessard@32: default: ERROR; lbessard@32: } lbessard@32: } /* for(...) */ etisserant@33: #endif lbessard@32: } lbessard@32: else { lbessard@32: /* loop through each function parameter, find the value we should pass lbessard@32: * to it, and then output the c equivalent... lbessard@32: */ lbessard@32: function_param_iterator_c fp_iterator(f_decl); lbessard@32: lbessard@32: symbol->function_name->accept(*this); lbessard@32: s4o.print("("); etisserant@42: s4o.indent_right(); lbessard@32: lbessard@32: identifier_c *param_name; lbessard@32: function_call_param_iterator_c function_call_param_iterator(symbol); lbessard@32: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { lbessard@32: if (i != 1) etisserant@42: s4o.print(",\n"+s4o.indent_spaces); lbessard@32: lbessard@32: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); lbessard@32: lbessard@32: /* Get the value from a foo( = ) style call */ lbessard@32: symbol_c *param_value = function_call_param_iterator.search(param_name); lbessard@32: lbessard@32: /* Get the value from a foo() style call */ lbessard@32: if (param_value == NULL) lbessard@32: param_value = function_call_param_iterator.next(); lbessard@32: lbessard@32: symbol_c *param_type = fp_iterator.param_type(); lbessard@32: if (param_type == NULL) ERROR; lbessard@32: lbessard@32: switch (param_direction) { lbessard@32: case function_param_iterator_c::direction_in: lbessard@32: if (param_value == NULL) { lbessard@32: /* No value given for parameter, so we must use the default... */ lbessard@32: /* First check whether default value specified in function declaration...*/ lbessard@32: param_value = fp_iterator.default_value(); lbessard@32: } lbessard@32: if (param_value == NULL) { lbessard@32: /* If not, get the default value of this variable's type */ lbessard@32: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); lbessard@32: } lbessard@32: if (param_value == NULL) ERROR; lbessard@32: param_value->accept(*this); lbessard@32: break; lbessard@32: case function_param_iterator_c::direction_out: lbessard@32: case function_param_iterator_c::direction_inout: lbessard@32: if (param_value == NULL) { lbessard@32: /* no parameter value given, so we pass a previously declared temporary variable. */ lbessard@32: std::string *temp_var_name = temp_var_name_factory.new_name(); lbessard@32: s4o.print(*temp_var_name); lbessard@32: delete temp_var_name; lbessard@32: } else { lbessard@32: param_value->accept(*this); lbessard@32: } lbessard@32: break; lbessard@32: case function_param_iterator_c::direction_extref: lbessard@32: /* TODO! */ lbessard@32: ERROR; lbessard@32: break; lbessard@32: } /* switch */ lbessard@32: } /* for(...) */ lbessard@32: // symbol->parameter_assignment->accept(*this); lbessard@32: s4o.print(")"); etisserant@42: s4o.indent_left(); lbessard@32: } lbessard@32: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /********************/ etisserant@0: /* B 3.2 Statements */ etisserant@0: /********************/ etisserant@0: void *visit(statement_list_c *symbol) { etisserant@0: return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); etisserant@0: } etisserant@0: etisserant@0: /*********************************/ etisserant@0: /* B 3.2.1 Assignment Statements */ etisserant@0: /*********************************/ etisserant@0: void *visit(assignment_statement_c *symbol) { etisserant@0: symbol->l_exp->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->r_exp->accept(*this); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /*****************************************/ etisserant@0: /* B 3.2.2 Subprogram Control Statements */ etisserant@0: /*****************************************/ etisserant@0: etisserant@0: /* fb_name '(' [param_assignment_list] ')' */ etisserant@0: /* param_assignment_list -> may be NULL ! */ etisserant@0: //SYM_REF2(fb_invocation_c, fb_name, param_assignment_list) etisserant@0: void *visit(fb_invocation_c *symbol) { etisserant@0: TRACE("fb_invocation_c"); etisserant@0: /* first figure out what is the name of the function block type of the function block being called... */ etisserant@0: symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); etisserant@0: /* should never occur. The function block instance MUST have been declared... */ etisserant@0: if (function_block_type_name == NULL) ERROR; etisserant@0: etisserant@0: /* Now find the declaration of the function block type being called... */ etisserant@0: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); etisserant@0: /* should never occur. The function block type being called MUST be in the symtable... */ etisserant@0: if (fb_decl == function_block_type_symtable.end_value()) ERROR; etisserant@0: etisserant@0: /* loop through each function block parameter, find the value we should pass etisserant@0: * to it, and then output the c equivalent... etisserant@0: */ etisserant@0: function_param_iterator_c fp_iterator(fb_decl); etisserant@0: identifier_c *param_name; etisserant@0: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); 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: /* now output the value assignment */ etisserant@0: if (param_value != NULL) etisserant@0: if ((param_direction == function_param_iterator_c::direction_in) || etisserant@0: (param_direction == function_param_iterator_c::direction_inout)) { etisserant@24: print_variable_prefix(); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print("."); etisserant@0: param_name->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(";\n" + s4o.indent_spaces); etisserant@0: } etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: /* now call the function... */ etisserant@0: function_block_type_name->accept(*this); etisserant@0: s4o.print(FB_FUNCTION_SUFFIX); etisserant@41: s4o.print("(&"); etisserant@24: print_variable_prefix(); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: etisserant@0: /* loop through each function parameter, find the variable to which etisserant@0: * we should atribute the value of all output or inoutput parameters. etisserant@0: */ etisserant@0: fp_iterator.reset(); etisserant@0: function_call_param_iterator.reset(); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); 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: /* now output the value assignment */ etisserant@0: if (param_value != NULL) etisserant@0: if ((param_direction == function_param_iterator_c::direction_out) || etisserant@0: (param_direction == function_param_iterator_c::direction_inout)) { etisserant@0: s4o.print(";\n"+ s4o.indent_spaces); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(" = "); etisserant@24: print_variable_prefix(); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print("."); etisserant@0: param_name->accept(*this); etisserant@0: } etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* helper symbol for fb_invocation */ etisserant@0: /* param_assignment_list ',' param_assignment */ etisserant@0: void *visit(param_assignment_list_c *symbol) { etisserant@0: TRACE("param_assignment_list_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: // return print_list(symbol, "", ", "); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: void *visit(input_variable_param_assignment_c *symbol) { etisserant@0: TRACE("input_variable_param_assignment_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: /* etisserant@0: symbol->variable_name->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->expression->accept(*this); etisserant@0: return NULL; etisserant@0: */ etisserant@0: } etisserant@0: etisserant@0: void *visit(output_variable_param_assignment_c *symbol) { etisserant@0: TRACE("output_variable_param_assignment_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: /* etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: if (symbol->not_param != NULL) etisserant@0: symbol->not_param->accept(*this); etisserant@0: symbol->variable_name->accept(*this); etisserant@0: s4o.print(" => "); etisserant@0: symbol->variable->accept(*this); etisserant@0: return NULL; etisserant@0: */ etisserant@0: } etisserant@0: etisserant@0: // TODO: the NOT symbol in function (block) calls... etisserant@0: void *visit(not_paramassign_c *symbol) { etisserant@0: TRACE("not_paramassign_c"); etisserant@0: /* this should never be called... */ etisserant@0: ERROR; etisserant@0: return NULL; etisserant@0: /* etisserant@0: s4o.print("NOT "); etisserant@0: return NULL; etisserant@0: */ etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 3.2.3 Selection Statements */ etisserant@0: /********************************/ etisserant@0: void *visit(if_statement_c *symbol) { etisserant@0: s4o.print("if ("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: symbol->elseif_statement_list->accept(*this); etisserant@0: etisserant@0: if (symbol->else_statement_list != NULL) { etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("} else {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->else_statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: } etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for if_statement */ etisserant@0: void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);} etisserant@0: etisserant@0: /* helper symbol for elseif_statement_list */ etisserant@0: void *visit(elseif_statement_c *symbol) { etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("} else if ("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(case_statement_c *symbol) { etisserant@0: s4o.print("switch("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->case_element_list->accept(*this); etisserant@0: if (symbol->statement_list != NULL) { etisserant@0: s4o.print(s4o.indent_spaces + "default:\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.print(s4o.indent_spaces + "break;\n"); etisserant@0: s4o.indent_left(); etisserant@0: } etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces + "}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* helper symbol for case_statement */ etisserant@0: void *visit(case_element_list_c *symbol) {return print_list(symbol);} etisserant@0: etisserant@0: void *visit(case_element_c *symbol) { etisserant@0: s4o.print(s4o.indent_spaces + "case "); etisserant@0: symbol->case_list->accept(*this); etisserant@0: s4o.print(" :\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.print(s4o.indent_spaces + "break;\n"); etisserant@0: s4o.indent_left(); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(case_list_c *symbol) {return print_list(symbol, "", ", ");} etisserant@0: etisserant@0: /********************************/ etisserant@0: /* B 3.2.4 Iteration Statements */ etisserant@0: /********************************/ etisserant@0: void *visit(for_statement_c *symbol) { etisserant@0: s4o.print("for("); etisserant@0: symbol->control_variable->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->beg_expression->accept(*this); etisserant@0: s4o.print("; "); etisserant@0: symbol->control_variable->accept(*this); etisserant@0: s4o.print(" != "); etisserant@0: symbol->end_expression->accept(*this); etisserant@0: s4o.print("; "); etisserant@0: symbol->control_variable->accept(*this); etisserant@0: if (symbol->by_expression != NULL) { etisserant@0: s4o.print(" += "); etisserant@0: symbol->by_expression->accept(*this); etisserant@0: } else { etisserant@0: s4o.print("++"); etisserant@0: } etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: void *visit(while_statement_c *symbol) { etisserant@0: s4o.print("while ("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(") {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("}"); etisserant@0: return NULL; etisserant@0: } etisserant@0: void *visit(repeat_statement_c *symbol) { etisserant@0: s4o.print("do {\n"); etisserant@0: s4o.indent_right(); etisserant@0: symbol->statement_list->accept(*this); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); s4o.print("} while("); etisserant@0: symbol->expression->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: return NULL; etisserant@0: } etisserant@0: void *visit(exit_statement_c *symbol) { etisserant@0: s4o.print("exit(0)"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: }; /* generate_cc_st_c */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: