lbessard@70: /* Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3 lbessard@70: * Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt) Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant lbessard@70: * Edouard@279: * This program is free software: you can redistribute it and/or modify Edouard@279: * it under the terms of the GNU General Public License as published by Edouard@279: * the Free Software Foundation, either version 3 of the License, or Edouard@279: * (at your option) any later version. Edouard@279: * Edouard@279: * This program is distributed in the hope that it will be useful, Edouard@279: * but WITHOUT ANY WARRANTY; without even the implied warranty of Edouard@279: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Edouard@279: * GNU General Public License for more details. Edouard@279: * Edouard@279: * You should have received a copy of the GNU General Public License Edouard@279: * along with this program. If not, see . Edouard@279: * lbessard@70: * lbessard@70: * This code is made available on the understanding that it will not be lbessard@70: * used in safety-critical situations without a full and competent review. lbessard@70: */ lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: /* Returns the data type of an il_operand. lbessard@70: * lbessard@70: * Note that the il_operand may be a variable, in which case lbessard@70: * we return the type of the variable instance. lbessard@70: * The il_operand may also be a constant, in which case lbessard@70: * we return the data type of that constant. lbessard@70: * lbessard@70: * The variable instance may be a member of a structured variable, lbessard@70: * or an element in an array, or any combination of the two. lbessard@70: * lbessard@70: * The class constructor must be given the search scope lbessard@70: * (function, function block or program within which lbessard@70: * the possible il_operand variable instance was declared). lbessard@70: */ lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: msousa@682: /* A new class to ouput the IL implicit variable to c++ code lbessard@70: * We use this class, inheriting from symbol_c, so it may be used lbessard@70: * as any other symbol_c object in the intermediate parse tree, lbessard@70: * more specifically, so it can be used as any other il operand. lbessard@70: * This makes the rest of the code much easier... lbessard@70: * lbessard@70: * Nevertheless, the basic visitor class visitor_c does not know lbessard@70: * how to visit this new il_default_variable_c class, so we have lbessard@70: * to extend that too. lbessard@70: * In reality extending the basic symbols doesn't quite work out lbessard@70: * as cleanly as desired (we need to use dynamic_cast in the lbessard@70: * accept method of the il_default_variable_c), but it is cleaner lbessard@70: * than the alternative... lbessard@70: */ lbessard@70: class il_default_variable_c; lbessard@70: lbessard@70: /* This visitor class is not really required, we could place the lbessard@70: * visit() method directly in genertae_cc_il_c, but doing it in lbessard@70: * a seperate class makes the architecture more evident... lbessard@70: */ lbessard@70: class il_default_variable_visitor_c { lbessard@70: public: lbessard@70: virtual void *visit(il_default_variable_c *symbol) = 0; lbessard@70: lbessard@70: virtual ~il_default_variable_visitor_c(void) {return;} lbessard@70: }; lbessard@70: lbessard@70: lbessard@70: /* A class to print out to the resulting C++ code msousa@682: * the IL implicit variable name. lbessard@70: * lbessard@70: * It includes a reference to its name, lbessard@70: * and the data type of the data currently stored lbessard@70: * in this C++ variable... This is required because the lbessard@70: * C++ variable is a union, and we must know which member lbessard@70: * of the union top reference!! lbessard@70: * lbessard@70: * Note that we also need to keep track of the data type of msousa@682: * the value currently being stored in the IL implicit variable. lbessard@70: * This is required so we can process parenthesis, lbessard@70: * lbessard@70: * e.g. : lbessard@70: * LD var1 lbessard@70: * AND ( lbessard@70: * LD var2 lbessard@70: * OR var3 lbessard@70: * ) lbessard@70: * lbessard@70: * Note that we only execute the 'AND (' operation when we come across lbessard@70: * the ')', i.e. once we have evaluated the result of the lbessard@70: * instructions inside the parenthesis. lbessard@70: * When we do execute the 'AND (' operation, we need to know the data type lbessard@70: * of the operand, which in this case is the result of the evaluation of the lbessard@70: * instruction list inside the parenthesis. We can only know this if we msousa@682: * keep track of the data type currently stored in the IL implicit variable! lbessard@70: * lbessard@70: * We use the current_type inside the generate_c_il::default_variable_name variable lbessard@70: * to track this! lbessard@70: */ lbessard@70: class il_default_variable_c: public symbol_c { lbessard@70: public: lbessard@70: symbol_c *var_name; /* in principle, this should point to an indentifier_c */ lbessard@70: lbessard@70: public: lbessard@70: il_default_variable_c(const char *var_name_str, symbol_c *current_type); lbessard@70: virtual void *accept(visitor_c &visitor); lbessard@70: }; lbessard@70: lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: msousa@669: msousa@669: lbessard@70: class generate_c_il_c: public generate_c_typedecl_c, il_default_variable_visitor_c { lbessard@70: laurent@217: public: laurent@217: typedef enum { laurent@217: expression_vg, laurent@217: assignment_vg, berem@228: complextype_base_vg, laurent@392: complextype_base_assignment_vg, berem@228: complextype_suffix_vg, laurent@217: fparam_output_vg laurent@217: } variablegeneration_t; laurent@217: lbessard@70: private: msousa@682: /* Label to which the current IL jump operation should jump to... */ msousa@682: /* This variable is used to pass data from the msousa@682: * il_jump_operation_c visitor msousa@682: * to the il jump operator visitors (i.e. JMP_operator_c, msousa@682: * JMPC_operator_c, JMPCN_operator_c, ...) lbessard@70: */ msousa@682: symbol_c *jump_label; msousa@682: msousa@682: /* the data type of the IL implicit variable... */ msousa@682: #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T" msousa@682: /* The name of the IL implicit variable... */ msousa@682: #define IL_DEFVAR VAR_LEADER "IL_DEFVAR" msousa@682: /* The name of the variable used to pass the result of a msousa@682: * parenthesised instruction list to the immediately preceding msousa@682: * scope ... msousa@682: */ msousa@682: #define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK" msousa@682: msousa@682: il_default_variable_c implicit_variable_current; /* the current implicit variable, with the datatype resulting from the previous IL operation */ msousa@682: il_default_variable_c implicit_variable_result; /* the resulting implicit variable, with the datatype resulting from the current IL operation */ msousa@682: il_default_variable_c implicit_variable_result_back; msousa@682: lbessard@70: /* Operand to the IL operation currently being processed... */ lbessard@70: /* These variables are used to pass data from the lbessard@70: * il_simple_operation_c and il_expression_c visitors lbessard@70: * to the il operator visitors (i.e. LD_operator_c, lbessard@70: * LDN_operator_c, ST_operator_c, STN_operator_c, ...) lbessard@70: */ lbessard@70: symbol_c *current_operand; lbessard@70: lbessard@70: /* When calling a function block, we must first find it's type, lbessard@70: * by searching through the declarations of the variables currently lbessard@70: * in scope. lbessard@70: * This class does just that... lbessard@70: * A new class is instantiated whenever we begin generating the code lbessard@70: * for a function block type declaration, or a program declaration. lbessard@70: * This object instance will then later be called while the lbessard@70: * function block's or the program's body is being handled. lbessard@70: * lbessard@70: * Note that functions cannot contain calls to function blocks, lbessard@70: * so we do not create an object instance when handling lbessard@70: * a function declaration. lbessard@70: */ lbessard@70: search_fb_instance_decl_c *search_fb_instance_decl; lbessard@70: lbessard@98: search_varfb_instance_type_c *search_varfb_instance_type; msousa@505: search_var_instance_decl_c *search_var_instance_decl; lbessard@98: berem@228: symbol_c* current_array_type; laurent@235: symbol_c* current_param_type; berem@228: laurent@217: int fcall_number; laurent@217: symbol_c *fbname; laurent@217: laurent@217: variablegeneration_t wanted_variablegeneration; lbessard@146: lbessard@70: public: laurent@217: generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL) lbessard@70: : generate_c_typedecl_c(s4o_ptr), msousa@682: implicit_variable_current (IL_DEFVAR, NULL), msousa@682: implicit_variable_result (IL_DEFVAR, NULL), msousa@682: implicit_variable_result_back(IL_DEFVAR_BACK, NULL) lbessard@70: { msousa@669: search_fb_instance_decl = new search_fb_instance_decl_c (scope); lbessard@98: search_varfb_instance_type = new search_varfb_instance_type_c(scope); msousa@669: search_var_instance_decl = new search_var_instance_decl_c (scope); msousa@505: lbessard@70: current_operand = NULL; berem@228: current_array_type = NULL; laurent@235: current_param_type = NULL; laurent@217: fcall_number = 0; laurent@217: fbname = name; laurent@217: wanted_variablegeneration = expression_vg; lbessard@70: this->set_variable_prefix(variable_prefix); lbessard@70: } lbessard@70: lbessard@70: virtual ~generate_c_il_c(void) { lbessard@70: delete search_fb_instance_decl; lbessard@98: delete search_varfb_instance_type; msousa@505: delete search_var_instance_decl; lbessard@70: } lbessard@70: lbessard@70: void generate(instruction_list_c *il) { lbessard@70: il->accept(*this); lbessard@70: } lbessard@70: msousa@682: private: msousa@682: /* Declare an implicit IL variable... */ msousa@682: void declare_implicit_variable(il_default_variable_c *implicit_var) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print(IL_DEFVAR_T); lbessard@70: s4o.print(" "); msousa@682: implicit_var->datatype = NULL; msousa@682: implicit_var->accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: } lbessard@70: msousa@682: public: msousa@682: /* Declare the default variable, that will store the result of the IL operations */ msousa@682: void declare_implicit_variable(void) { msousa@682: declare_implicit_variable(&this->implicit_variable_result); msousa@682: } msousa@682: msousa@682: /* Declare the backup to the default variable, that will store the result of the IL operations executed inside a parenthesis... */ msousa@682: void declare_implicit_variable_back(void) { msousa@682: declare_implicit_variable(&this->implicit_variable_result_back); msousa@682: } msousa@682: msousa@682: void print_implicit_variable_back(void) { msousa@682: this->implicit_variable_result_back.accept(*this); msousa@682: } msousa@682: laurent@392: lbessard@70: private: msousa@669: /* a small helper function */ msousa@669: symbol_c *default_literal_type(symbol_c *symbol) { msousa@669: if (get_datatype_info_c::is_ANY_INT_literal(symbol)) { msousa@669: return &search_constant_type_c::lint_type_name; msousa@669: } msousa@669: else if (get_datatype_info_c::is_ANY_REAL_literal(symbol)) { msousa@669: return &search_constant_type_c::lreal_type_name; msousa@669: } msousa@669: return symbol; msousa@669: } lbessard@70: lbessard@70: /* A helper function... */ lbessard@70: void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) { msousa@669: if ((NULL == lo) || (NULL == ro) || (NULL == op)) ERROR; lbessard@70: lbessard@70: lo->accept(*this); lbessard@70: s4o.print(op); lbessard@70: ro->accept(*this); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* A helper function... */ msousa@682: void *XXX_function(symbol_c *res, const char *func, symbol_c *lo, symbol_c *ro) { msousa@682: if ((NULL == res) || (NULL == lo) || (NULL == ro)) ERROR; msousa@682: if (NULL == func) ERROR; msousa@682: msousa@682: res->accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: s4o.print(func); lbessard@70: s4o.print("("); lbessard@70: lo->accept(*this); lbessard@70: s4o.print(", "); lbessard@70: ro->accept(*this); lbessard@70: s4o.print(")"); lbessard@70: return NULL; lbessard@70: } lbessard@70: msousa@682: /* A helper function... used for implicit FB calls: S1, R1, CLK, CU, CD, PV, IN, and PT */ lbessard@70: void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) { laurent@382: if (wanted_variablegeneration != expression_vg) { laurent@382: s4o.print(param_name); laurent@382: return NULL; laurent@382: } laurent@382: lbessard@70: if (NULL == fb_name) ERROR; lbessard@70: symbolic_variable_c *sv = dynamic_cast(fb_name); lbessard@70: if (NULL == sv) ERROR; lbessard@70: identifier_c *id = dynamic_cast(sv->var_name); lbessard@70: if (NULL == id) ERROR; ccb@202: lbessard@70: identifier_c param(param_name); lbessard@70: lbessard@70: //SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list) ccb@202: il_assign_operator_c il_assign_operator(¶m); msousa@682: il_param_assignment_c il_param_assignment(&il_assign_operator, &this->implicit_variable_current, NULL); lbessard@70: // SYM_LIST(il_param_list_c) ccb@202: il_param_list_c il_param_list; lbessard@70: il_param_list.add_element(&il_param_assignment); lbessard@70: CAL_operator_c CAL_operator; lbessard@70: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) lbessard@70: il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list); lbessard@70: lbessard@70: il_fb_call.accept(*this); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: /* A helper function... */ msousa@682: void *CMP_operator(symbol_c *operand, const char *operation) { msousa@682: if (NULL == operand) ERROR; msousa@682: if (NULL == operand->datatype) ERROR; msousa@682: if (NULL == this->implicit_variable_current.datatype) ERROR; msousa@682: msousa@682: this->implicit_variable_result.accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: s4o.print(operation); msousa@682: operand->datatype->accept(*this); msousa@682: /* NOTE: we are calling a standard Function: msousa@682: * 1st parameter: EN (enable) msousa@682: * 2nd parameter: ENO (enable output) msousa@682: * 3rd parameter: number of operands we will be passing (required because we are calling an extensible standard function!) msousa@682: * 4th parameter: the left hand side of the comparison expression (in out case, the IL implicit variable) msousa@682: * 4th parameter: the right hand side of the comparison expression (in out case, current operand) msousa@682: */ lbessard@149: s4o.print("(__BOOL_LITERAL(TRUE), NULL, 2, "); msousa@682: this->implicit_variable_current.accept(*this); lbessard@70: s4o.print(", "); msousa@682: operand->accept(*this); lbessard@70: s4o.print(")"); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* A helper function... */ lbessard@70: void C_modifier(void) { msousa@682: if (!get_datatype_info_c::is_BOOL_compatible(implicit_variable_current.datatype)) ERROR; msousa@682: s4o.print("if ("); msousa@682: this->implicit_variable_current.accept(*this); msousa@682: s4o.print(") "); lbessard@70: } lbessard@70: lbessard@70: /* A helper function... */ lbessard@70: void CN_modifier(void) { msousa@682: if (!get_datatype_info_c::is_BOOL_compatible(implicit_variable_current.datatype)) ERROR; msousa@682: s4o.print("if (!"); msousa@682: this->implicit_variable_current.accept(*this); msousa@682: s4o.print(") "); msousa@682: } msousa@682: laurent@210: berem@228: void *print_getter(symbol_c *symbol) { msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol); laurent@235: if (wanted_variablegeneration == fparam_output_vg) { laurent@235: if (vartype == search_var_instance_decl_c::external_vt) laurent@235: s4o.print(GET_EXTERNAL_BY_REF); laurent@235: else if (vartype == search_var_instance_decl_c::located_vt) laurent@235: s4o.print(GET_LOCATED_BY_REF); laurent@235: else laurent@235: s4o.print(GET_VAR_BY_REF); laurent@235: } laurent@235: else { laurent@235: if (vartype == search_var_instance_decl_c::external_vt) laurent@235: s4o.print(GET_EXTERNAL); laurent@235: else if (vartype == search_var_instance_decl_c::located_vt) laurent@235: s4o.print(GET_LOCATED); laurent@235: else laurent@235: s4o.print(GET_VAR); laurent@235: } berem@228: s4o.print("("); berem@228: laurent@235: variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; berem@228: wanted_variablegeneration = complextype_base_vg; berem@228: symbol->accept(*this); msousa@531: if (search_var_instance_decl->type_is_complex(symbol)) laurent@235: s4o.print(","); berem@228: wanted_variablegeneration = complextype_suffix_vg; berem@228: symbol->accept(*this); berem@228: s4o.print(")"); laurent@235: wanted_variablegeneration = old_wanted_variablegeneration; berem@228: return NULL; berem@228: } berem@228: berem@228: void *print_setter(symbol_c* symbol, berem@228: symbol_c* type, berem@228: symbol_c* value, berem@228: symbol_c* fb_symbol = NULL, berem@228: symbol_c* fb_value = NULL, berem@228: bool negative = false) { laurent@405: laurent@405: bool type_is_complex = false; laurent@405: if (fb_symbol == NULL) { msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol); msousa@531: type_is_complex = search_var_instance_decl->type_is_complex(symbol); laurent@405: if (vartype == search_var_instance_decl_c::external_vt) laurent@405: s4o.print(SET_EXTERNAL); laurent@405: else if (vartype == search_var_instance_decl_c::located_vt) laurent@405: s4o.print(SET_LOCATED); laurent@405: else laurent@405: s4o.print(SET_VAR); laurent@405: } laurent@392: else laurent@392: s4o.print(SET_VAR); laurent@392: s4o.print("("); berem@228: berem@228: if (fb_symbol != NULL) { berem@228: print_variable_prefix(); berem@228: fb_symbol->accept(*this); laurent@392: s4o.print(".,"); berem@228: } laurent@382: else if (type_is_complex) laurent@392: wanted_variablegeneration = complextype_base_assignment_vg; berem@228: else laurent@382: wanted_variablegeneration = assignment_vg; laurent@382: berem@228: symbol->accept(*this); berem@228: s4o.print(","); berem@228: if (negative) { msousa@682: if (get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)) berem@228: s4o.print("!"); berem@228: else berem@228: s4o.print("~"); berem@228: } berem@228: wanted_variablegeneration = expression_vg; berem@228: print_check_function(type, value, fb_value); laurent@382: if (type_is_complex) { berem@228: s4o.print(","); berem@228: wanted_variablegeneration = complextype_suffix_vg; berem@228: symbol->accept(*this); berem@228: } berem@228: s4o.print(")"); berem@228: wanted_variablegeneration = expression_vg; berem@228: return NULL; berem@228: } lbessard@70: lbessard@70: public: lbessard@70: void *visit(il_default_variable_c *symbol) { lbessard@70: symbol->var_name->accept(*this); msousa@682: if (NULL != symbol->datatype) { lbessard@70: s4o.print("."); msousa@682: symbol->datatype->accept(*this); lbessard@70: s4o.print("var"); agraeper@563: } return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: private: lbessard@70: ccb@202: lbessard@146: laurent@377: /********************************/ laurent@377: /* B 1.3.3 - Derived data types */ laurent@377: /********************************/ laurent@377: laurent@382: /* signed_integer DOTDOT signed_integer */ laurent@382: void *visit(subrange_c *symbol) { laurent@382: symbol->lower_limit->accept(*this); laurent@382: return NULL; laurent@382: } laurent@382: laurent@377: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */ laurent@377: void *visit(array_specification_c *symbol) { laurent@377: symbol->non_generic_type_name->accept(*this); laurent@377: return NULL; laurent@377: } laurent@377: lbessard@146: /*********************/ lbessard@146: /* B 1.4 - Variables */ lbessard@146: /*********************/ laurent@221: lbessard@146: void *visit(symbolic_variable_c *symbol) { berem@228: unsigned int vartype; laurent@382: switch (wanted_variablegeneration) { laurent@392: case complextype_base_assignment_vg: laurent@392: case assignment_vg: laurent@392: this->print_variable_prefix(); laurent@392: s4o.print(","); laurent@392: symbol->var_name->accept(*this); laurent@392: break; laurent@382: case complextype_base_vg: laurent@221: generate_c_base_c::visit(symbol); laurent@382: break; laurent@382: case complextype_suffix_vg: laurent@382: break; laurent@382: default: laurent@382: if (this->is_variable_prefix_null()) { msousa@505: vartype = search_var_instance_decl->get_vartype(symbol); laurent@382: if (wanted_variablegeneration == fparam_output_vg) { laurent@382: s4o.print("&("); laurent@382: generate_c_base_c::visit(symbol); laurent@382: s4o.print(")"); laurent@382: } laurent@382: else { laurent@382: generate_c_base_c::visit(symbol); laurent@382: } laurent@382: } laurent@382: else laurent@382: print_getter(symbol); laurent@382: break; laurent@382: } lbessard@146: return NULL; lbessard@146: } lbessard@146: lbessard@70: /********************************************/ lbessard@70: /* B.1.4.1 Directly Represented Variables */ lbessard@70: /********************************************/ lbessard@70: // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);}; lbessard@70: void *visit(direct_variable_c *symbol) { lbessard@70: TRACE("direct_variable_c"); lbessard@70: /* Do not use print_token() as it will change everything into uppercase */ lbessard@70: if (strlen(symbol->value) == 0) ERROR; laurent@217: if (this->is_variable_prefix_null()) { laurent@217: if (wanted_variablegeneration != fparam_output_vg) laurent@217: s4o.print("*("); laurent@217: } laurent@217: else { laurent@217: switch (wanted_variablegeneration) { laurent@217: case expression_vg: laurent@217: s4o.print(GET_LOCATED); laurent@217: s4o.print("("); laurent@217: break; laurent@217: case fparam_output_vg: laurent@217: s4o.print(GET_LOCATED_BY_REF); laurent@217: s4o.print("("); laurent@217: break; laurent@217: default: laurent@217: break; laurent@217: } lbessard@146: } lbessard@70: this->print_variable_prefix(); lbessard@70: s4o.printlocation(symbol->value + 1); laurent@217: if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) || laurent@217: wanted_variablegeneration != assignment_vg) lbessard@146: s4o.print(")"); lbessard@70: return NULL; lbessard@70: } lbessard@70: berem@228: /*************************************/ berem@228: /* B.1.4.2 Multi-element Variables */ berem@228: /*************************************/ berem@228: berem@228: // SYM_REF2(structured_variable_c, record_variable, field_selector) berem@228: void *visit(structured_variable_c *symbol) { berem@228: TRACE("structured_variable_c"); Laurent@625: bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable); berem@228: switch (wanted_variablegeneration) { berem@228: case complextype_base_vg: laurent@392: case complextype_base_assignment_vg: berem@228: symbol->record_variable->accept(*this); Laurent@410: if (!type_is_complex) { Laurent@410: s4o.print("."); Laurent@410: symbol->field_selector->accept(*this); Laurent@410: } berem@228: break; berem@228: case complextype_suffix_vg: Laurent@411: symbol->record_variable->accept(*this); Laurent@411: if (type_is_complex) { Laurent@411: s4o.print("."); Laurent@411: symbol->field_selector->accept(*this); Laurent@411: } Laurent@411: break; Laurent@411: case assignment_vg: Laurent@411: symbol->record_variable->accept(*this); Laurent@411: s4o.print("."); Laurent@411: symbol->field_selector->accept(*this); Laurent@411: break; berem@228: default: berem@228: if (this->is_variable_prefix_null()) { berem@228: symbol->record_variable->accept(*this); berem@228: s4o.print("."); berem@228: symbol->field_selector->accept(*this); berem@228: } berem@228: else berem@228: print_getter(symbol); berem@228: break; berem@228: } berem@228: return NULL; berem@228: } berem@228: berem@228: /* subscripted_variable '[' subscript_list ']' */ berem@228: //SYM_REF2(array_variable_c, subscripted_variable, subscript_list) berem@228: void *visit(array_variable_c *symbol) { berem@228: switch (wanted_variablegeneration) { berem@228: case complextype_base_vg: laurent@392: case complextype_base_assignment_vg: berem@228: symbol->subscripted_variable->accept(*this); berem@228: break; berem@228: case complextype_suffix_vg: laurent@238: symbol->subscripted_variable->accept(*this); laurent@238: msousa@321: current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable); laurent@238: if (current_array_type == NULL) ERROR; laurent@238: laurent@238: s4o.print(".table"); laurent@238: symbol->subscript_list->accept(*this); laurent@238: laurent@238: current_array_type = NULL; berem@228: break; berem@228: default: berem@228: if (this->is_variable_prefix_null()) { laurent@238: symbol->subscripted_variable->accept(*this); laurent@238: msousa@321: current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable); laurent@238: if (current_array_type == NULL) ERROR; laurent@238: laurent@238: s4o.print(".table"); laurent@238: symbol->subscript_list->accept(*this); laurent@238: laurent@238: current_array_type = NULL; berem@228: } berem@228: else berem@228: print_getter(symbol); berem@228: break; berem@228: } berem@228: return NULL; berem@228: } berem@228: berem@228: /* subscript_list ',' subscript */ berem@228: void *visit(subscript_list_c *symbol) { laurent@377: array_dimension_iterator_c* array_dimension_iterator = new array_dimension_iterator_c(current_array_type); berem@228: for (int i = 0; i < symbol->n; i++) { laurent@377: symbol_c* dimension = array_dimension_iterator->next(); laurent@377: if (dimension == NULL) ERROR; laurent@377: laurent@377: s4o.print("[("); berem@228: symbol->elements[i]->accept(*this); laurent@377: s4o.print(") - ("); laurent@377: dimension->accept(*this); berem@228: s4o.print(")]"); berem@228: } laurent@377: delete array_dimension_iterator; berem@228: return NULL; berem@228: } berem@228: laurent@235: /******************************************/ laurent@235: /* B 1.4.3 - Declaration & Initialisation */ laurent@235: /******************************************/ laurent@237: laurent@237: /* helper symbol for structure_initialization */ laurent@237: /* structure_element_initialization_list ',' structure_element_initialization */ laurent@235: void *visit(structure_element_initialization_list_c *symbol) { laurent@235: generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o); laurent@235: structure_initialization->init_structure_default(this->current_param_type); laurent@237: structure_initialization->init_structure_values(symbol); laurent@235: delete structure_initialization; laurent@235: return NULL; laurent@235: } laurent@235: laurent@237: /* helper symbol for array_initialization */ laurent@237: /* array_initial_elements_list ',' array_initial_elements */ laurent@237: void *visit(array_initial_elements_list_c *symbol) { laurent@237: generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o); laurent@237: array_initialization->init_array_size(this->current_param_type); laurent@237: array_initialization->init_array_values(symbol); laurent@237: delete array_initialization; laurent@237: return NULL; laurent@237: } lbessard@70: /****************************************/ lbessard@70: /* B.2 - Language IL (Instruction List) */ lbessard@70: /****************************************/ lbessard@70: lbessard@70: /***********************************/ lbessard@70: /* B 2.1 Instructions and Operands */ lbessard@70: /***********************************/ lbessard@70: lbessard@70: /*| instruction_list il_instruction */ lbessard@70: void *visit(instruction_list_c *symbol) { lbessard@70: msousa@682: /* Declare the IL implicit variable, that will store the result of the IL operations... */ msousa@682: declare_implicit_variable(); msousa@682: msousa@682: /* Declare the backup to the IL implicit variable, that will store the result of the IL operations executed inside a parenthesis... */ msousa@682: declare_implicit_variable_back(); msousa@682: /* lbessard@70: s4o.print(s4o.indent_spaces); msousa@682: this->implicit_variable_result_back.accept(*this); laurent@211: s4o.print(".INTvar = 0;\n\n"); msousa@682: */ lbessard@70: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* | label ':' [il_incomplete_instruction] eol_list */ lbessard@70: // SYM_REF2(il_instruction_c, label, il_instruction) lbessard@70: void *visit(il_instruction_c *symbol) { msousa@682: /* all previous IL instructions should have the same datatype (checked in stage3), so we get the datatype from the first previous IL instruction we find */ msousa@682: implicit_variable_current.datatype = (symbol->prev_il_instruction.empty())? NULL : symbol->prev_il_instruction[0]->datatype; msousa@682: implicit_variable_result .datatype = symbol->datatype; msousa@682: lbessard@70: if (NULL != symbol->label) { lbessard@70: symbol->label->accept(*this); lbessard@70: s4o.print(":\n"); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: } msousa@682: msousa@311: if (NULL != symbol->il_instruction) { msousa@311: symbol->il_instruction->accept(*this); msousa@311: } msousa@682: msousa@682: implicit_variable_result .datatype = NULL; msousa@682: implicit_variable_current.datatype = NULL; msousa@682: return NULL; msousa@682: } msousa@682: lbessard@70: lbessard@70: /* | il_simple_operator [il_operand] */ lbessard@70: //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) lbessard@70: void *visit(il_simple_operation_c *symbol) { lbessard@70: this->current_operand = symbol->il_operand; lbessard@70: symbol->il_simple_operator->accept(*this); lbessard@70: this->current_operand = NULL; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* | function_name [il_operand_list] */ lbessard@70: // SYM_REF2(il_function_call_c, function_name, il_operand_list) lbessard@70: void *visit(il_function_call_c *symbol) { lbessard@149: symbol_c* function_type_prefix = NULL; lbessard@149: symbol_c* function_name = NULL; lbessard@149: symbol_c* function_type_suffix = NULL; laurent@217: DECLARE_PARAM_LIST() lbessard@149: laurent@233: function_call_param_iterator_c function_call_param_iterator(symbol); laurent@233: msousa@350: function_declaration_c *f_decl = (function_declaration_c *)symbol->called_function_declaration; msousa@350: if (f_decl == NULL) ERROR; msousa@350: msousa@350: function_name = symbol->function_name; msousa@350: msousa@350: /* loop through each function parameter, find the value we should pass msousa@350: * to it, and then output the c equivalent... msousa@350: */ msousa@350: function_param_iterator_c fp_iterator(f_decl); msousa@350: identifier_c *param_name; msousa@682: /* flag to remember whether we have already used the value stored in the implicit variable to pass to the first parameter */ msousa@350: bool used_defvar = false; msousa@350: /* flag to cirreclty handle calls to extensible standard functions (i.e. functions with variable number of input parameters) */ msousa@350: bool found_first_extensible_parameter = false; msousa@350: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { msousa@350: if (fp_iterator.is_extensible_param() && (!found_first_extensible_parameter)) { msousa@350: /* We are calling an extensible function. Before passing the extensible msousa@350: * parameters, we must add a dummy paramater value to tell the called msousa@350: * function how many extensible parameters we will be passing. msousa@350: * msousa@350: * Note that stage 3 has already determined the number of extensible msousa@350: * paramters, and stored that info in the abstract syntax tree. We simply msousa@350: * re-use that value. msousa@350: */ msousa@350: /* NOTE: we are not freeing the malloc'd memory. This is not really a bug. msousa@350: * Since we are writing a compiler, which runs to termination quickly, msousa@350: * we can consider this as just memory required for the compilation process msousa@350: * that will be free'd when the program terminates. msousa@350: */ msousa@350: char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */ msousa@350: if (tmp == NULL) ERROR; msousa@350: int res = snprintf(tmp, 32, "%d", symbol->extensible_param_count); msousa@350: if ((res >= 32) || (res < 0)) ERROR; msousa@350: identifier_c *param_value = new identifier_c(tmp); msousa@350: uint_type_name_c *param_type = new uint_type_name_c(); msousa@350: identifier_c *param_name = new identifier_c(""); msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, function_param_iterator_c::direction_in) msousa@350: found_first_extensible_parameter = true; msousa@350: } lbessard@70: msousa@350: symbol_c *param_type = fp_iterator.param_type(); msousa@350: if (param_type == NULL) ERROR; lbessard@70: msousa@350: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); lbessard@149: msousa@350: symbol_c *param_value = NULL; msousa@350: msousa@350: /* Get the value from a foo( = ) style call */ msousa@350: /* NOTE: Since the class il_function_call_c only references a non.formal function call, msousa@350: * the following line of code is not required in this case. However, it doesn't msousa@350: * harm to leave it in, as in the case of a non-formal syntax function call, msousa@350: * it will always return NULL. msousa@350: * We leave it in in case we later decide to merge this part of the code together msousa@350: * with the function calling code in generate_c_st_c, which does require msousa@350: * the following line... msousa@350: */ msousa@350: if (param_value == NULL) msousa@350: param_value = function_call_param_iterator.search_f(param_name); msousa@350: msousa@350: /* if it is the first parameter in a non-formal function call (which is the msousa@350: * case being handled!), semantics specifies that we should msousa@682: * get the value off the IL implicit variable! msousa@350: * msousa@350: * However, if the parameter is an implicitly defined EN or ENO parameter, we should not msousa@682: * use the IL implicit variable as a source of data to pass to those parameters! msousa@350: */ msousa@350: if ((param_value == NULL) && (!used_defvar) && !fp_iterator.is_en_eno_param_implicit()) { msousa@683: if (NULL == implicit_variable_current.datatype) ERROR; msousa@682: param_value = &this->implicit_variable_current; msousa@350: used_defvar = true; msousa@350: } msousa@350: msousa@350: /* Get the value from a foo() style call */ msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) { msousa@350: param_value = function_call_param_iterator.next_nf(); msousa@350: } lbessard@149: msousa@350: /* if no more parameter values in function call, and the current parameter msousa@350: * of the function declaration is an extensible parameter, we msousa@350: * have reached the end, and should simply jump out of the for loop. msousa@350: */ msousa@350: if ((param_value == NULL) && (fp_iterator.is_extensible_param())) { msousa@350: break; msousa@350: } lbessard@149: msousa@350: if ((param_value == NULL) && (param_direction == function_param_iterator_c::direction_in)) { msousa@350: /* No value given for parameter, so we must use the default... */ msousa@350: /* First check whether default value specified in function declaration...*/ msousa@350: param_value = fp_iterator.default_value(); msousa@350: } msousa@350: msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction()) msousa@350: } /* for(...) */ msousa@350: laurent@233: if (function_call_param_iterator.next_nf() != NULL) ERROR; laurent@233: laurent@217: bool has_output_params = false; laurent@217: laurent@217: if (!this->is_variable_prefix_null()) { laurent@217: PARAM_LIST_ITERATOR() { msousa@350: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || msousa@350: PARAM_DIRECTION == function_param_iterator_c::direction_inout) && msousa@350: PARAM_VALUE != NULL) { msousa@350: has_output_params = true; msousa@350: } msousa@350: } msousa@350: } msousa@350: msousa@350: /* Check whether we are calling an overloaded function! */ msousa@350: /* (fdecl_mutiplicity==2) => calling overloaded function */ msousa@350: int fdecl_mutiplicity = function_symtable.multiplicity(symbol->function_name); msousa@350: if (fdecl_mutiplicity == 0) ERROR; laurent@217: msousa@682: this->implicit_variable_result.accept(*this); lbessard@149: s4o.print(" = "); lbessard@149: lbessard@149: if (function_type_prefix != NULL) { lbessard@149: s4o.print("("); msousa@669: default_literal_type(function_type_prefix)->accept(*this); lbessard@149: s4o.print(")"); lbessard@149: } laurent@336: if (function_type_suffix != NULL) { msousa@669: function_type_suffix = default_literal_type(function_type_suffix); laurent@336: } laurent@217: if (has_output_params) { msousa@350: fcall_number++; msousa@350: s4o.print("__"); laurent@217: fbname->accept(*this); laurent@217: s4o.print("_"); lbessard@149: function_name->accept(*this); msousa@350: if (fdecl_mutiplicity == 2) { msousa@350: /* function being called is overloaded! */ msousa@350: s4o.print("__"); laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o); msousa@350: f_decl->accept(overloaded_func_suf); msousa@350: } msousa@594: s4o.print(fcall_number); laurent@217: } laurent@217: else { msousa@350: if (function_name != NULL) { msousa@350: function_name->accept(*this); msousa@350: if (fdecl_mutiplicity == 2) { msousa@350: /* function being called is overloaded! */ msousa@350: s4o.print("__"); laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o); msousa@350: f_decl->accept(overloaded_func_suf); msousa@350: } msousa@350: } laurent@217: if (function_type_suffix != NULL) msousa@350: function_type_suffix->accept(*this); laurent@217: } lbessard@149: s4o.print("("); lbessard@149: s4o.indent_right(); lbessard@149: laurent@217: int nb_param = 0; laurent@217: PARAM_LIST_ITERATOR() { laurent@217: symbol_c *param_value = PARAM_VALUE; laurent@235: current_param_type = PARAM_TYPE; lbessard@149: laurent@217: switch (PARAM_DIRECTION) { lbessard@149: case function_param_iterator_c::direction_in: msousa@350: if (nb_param > 0) msousa@350: s4o.print(",\n"+s4o.indent_spaces); lbessard@149: if (param_value == NULL) { lbessard@149: /* If not, get the default value of this variable's type */ laurent@235: param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance()); lbessard@149: } lbessard@149: if (param_value == NULL) ERROR; laurent@217: s4o.print("("); msousa@669: if (get_datatype_info_c::is_ANY_INT_literal(current_param_type)) msousa@669: search_constant_type_c::lint_type_name.accept(*this); msousa@669: else if (get_datatype_info_c::is_ANY_REAL_literal(current_param_type)) msousa@669: search_constant_type_c::lreal_type_name.accept(*this); laurent@220: else laurent@235: current_param_type->accept(*this); laurent@217: s4o.print(")"); laurent@235: print_check_function(current_param_type, param_value); laurent@217: nb_param++; lbessard@149: break; lbessard@149: case function_param_iterator_c::direction_out: lbessard@149: case function_param_iterator_c::direction_inout: msousa@350: if (!has_output_params) { laurent@217: if (nb_param > 0) msousa@350: s4o.print(",\n"+s4o.indent_spaces); msousa@350: if (param_value == NULL) { msousa@350: s4o.print("NULL"); msousa@350: } else { msousa@350: wanted_variablegeneration = fparam_output_vg; msousa@350: param_value->accept(*this); msousa@350: wanted_variablegeneration = expression_vg; msousa@350: } msousa@350: nb_param++; msousa@350: } lbessard@149: break; lbessard@149: case function_param_iterator_c::direction_extref: lbessard@149: /* TODO! */ lbessard@149: ERROR; lbessard@149: break; lbessard@149: } /* switch */ lbessard@149: } laurent@217: if (has_output_params) { laurent@217: if (nb_param > 0) msousa@350: s4o.print(",\n"+s4o.indent_spaces); laurent@217: s4o.print(FB_FUNCTION_PARAM); laurent@217: } lbessard@149: lbessard@149: s4o.print(")"); laurent@217: laurent@217: CLEAR_PARAM_LIST() laurent@217: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ lbessard@70: //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused) lbessard@70: void *visit(il_expression_c *symbol) { msousa@682: LD_operator_c *tmp_LD_operator = NULL; msousa@682: il_simple_operation_c *tmp_il_simple_operation = NULL; msousa@682: il_simple_instruction_c *tmp_il_simple_instruction = NULL; msousa@682: msousa@682: /* We will be recursevely interpreting an instruction list, so we store a backup of the implicit_variable_result/current. msousa@682: * Notice that they will be overwriten while processing the parenthsized instruction list. lbessard@70: */ msousa@682: il_default_variable_c old_implicit_variable_current = this->implicit_variable_current; msousa@682: il_default_variable_c old_implicit_variable_result = this->implicit_variable_result; msousa@682: msousa@682: /* If the symbol->il_operand is not NULL, then we instantiate a 'LD' operation and insert it into the simple_instr_list msousa@682: * (i.e. add an equivalent LD operation to the Abstract Syntax Tree), and let it be handled like any other LD operation! msousa@682: */ msousa@682: if (NULL != symbol->il_operand) { msousa@682: tmp_LD_operator = new LD_operator_c(); msousa@682: if (NULL == tmp_LD_operator) ERROR; msousa@682: /* copy all the location, datatpe, etc.. data from symbol->il_operand to the new object! */ msousa@682: *((symbol_c *)tmp_LD_operator) = *(symbol->il_operand); msousa@682: msousa@682: tmp_il_simple_operation = new il_simple_operation_c(tmp_LD_operator, symbol->il_operand); msousa@682: if (NULL == tmp_il_simple_operation) ERROR; msousa@682: /* copy all the location, datatpe, etc.. data from symbol->il_operand to the new object! */ msousa@682: *((symbol_c *)tmp_il_simple_operation) = *(symbol->il_operand); msousa@682: msousa@682: tmp_il_simple_instruction = new il_simple_instruction_c(tmp_il_simple_operation); msousa@682: if (NULL == tmp_il_simple_instruction) ERROR; msousa@682: /* copy all the location, datatpe, etc.. data from symbol->il_operand to the new object! */ msousa@682: *((symbol_c *)tmp_il_simple_instruction) = *(symbol->il_operand); msousa@682: msousa@682: if (NULL == symbol->simple_instr_list) { msousa@682: symbol->simple_instr_list = new simple_instr_list_c(); msousa@682: if (NULL == symbol->simple_instr_list) ERROR; msousa@682: /* copy all the location, datatpe, etc.. data from symbol->il_operand to the new object! */ msousa@682: *((symbol_c *)symbol->simple_instr_list) = *(symbol->il_operand); msousa@682: } msousa@682: list_c *tmp_list = dynamic_cast (symbol->simple_instr_list); msousa@682: if (NULL == tmp_list) ERROR; msousa@682: tmp_list->insert_element(tmp_il_simple_instruction, 0); msousa@682: } lbessard@70: lbessard@70: /* Now do the parenthesised instructions... */ msousa@682: /* NOTE: the following code line will overwrite the variables implicit_variable_current and implicit_variable_result */ lbessard@70: symbol->simple_instr_list->accept(*this); lbessard@70: msousa@682: /* delete/undo any changes made to the AST above */ msousa@682: if (NULL != symbol->il_operand) { msousa@682: delete tmp_LD_operator; msousa@682: delete tmp_il_simple_operation; msousa@682: delete tmp_il_simple_instruction; msousa@682: list_c *tmp_list = dynamic_cast (symbol->simple_instr_list); msousa@682: if (NULL == tmp_list) ERROR; msousa@682: delete tmp_list->elements[0]; msousa@682: tmp_list->remove_element(0); msousa@682: if (0 == tmp_list->n) { msousa@682: delete symbol->simple_instr_list; msousa@682: symbol->simple_instr_list = NULL; msousa@682: } msousa@682: } msousa@682: lbessard@70: /* Now do the operation, using the previous result! */ msousa@682: /* NOTE: The result of the previous instruction list in the parenthesis will be stored lbessard@70: * in a variable named IL_DEFVAR_BACK. This is done in the visitor lbessard@70: * to instruction_list_c objects... lbessard@70: */ msousa@682: this->implicit_variable_result_back.datatype = symbol->simple_instr_list->datatype; msousa@682: this->current_operand = &(this->implicit_variable_result_back); msousa@682: msousa@682: this->implicit_variable_current = old_implicit_variable_current; msousa@682: this->implicit_variable_result = old_implicit_variable_result; lbessard@70: lbessard@70: symbol->il_expr_operator->accept(*this); lbessard@70: lbessard@70: this->current_operand = NULL; msousa@682: this->implicit_variable_result_back.datatype = NULL; msousa@682: return NULL; msousa@682: } msousa@682: lbessard@70: lbessard@70: /* il_jump_operator label */ lbessard@70: // SYM_REF2(il_jump_operation_c, il_jump_operator, label) lbessard@70: void *visit(il_jump_operation_c *symbol) { msousa@682: /* Pass the symbol->label to the il_jump_operation visitor using the jump_label parameter... */ lbessard@70: this->jump_label = symbol->label; lbessard@70: symbol->il_jump_operator->accept(*this); lbessard@70: this->jump_label = NULL; msousa@682: return NULL; msousa@682: } msousa@682: lbessard@70: lbessard@70: /* il_call_operator prev_declared_fb_name lbessard@70: * | il_call_operator prev_declared_fb_name '(' ')' lbessard@70: * | il_call_operator prev_declared_fb_name '(' eol_list ')' lbessard@70: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' lbessard@70: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' lbessard@70: */ lbessard@70: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) lbessard@70: void *visit(il_fb_call_c *symbol) { lbessard@70: symbol->il_call_operator->accept(*this); lbessard@70: s4o.print("{\n"); lbessard@70: s4o.indent_right(); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: lbessard@70: /* first figure out what is the name of the function block type of the function block being called... */ lbessard@70: symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); lbessard@70: /* should never occur. The function block instance MUST have been declared... */ lbessard@70: if (function_block_type_name == NULL) ERROR; lbessard@70: lbessard@70: /* Now find the declaration of the function block type being called... */ lbessard@70: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); lbessard@70: /* should never occur. The function block type being called MUST be in the symtable... */ lbessard@70: if (fb_decl == function_block_type_symtable.end_value()) ERROR; lbessard@70: lbessard@70: /* loop through each function block parameter, find the value we should pass lbessard@70: * to it, and then output the c equivalent... lbessard@70: */ lbessard@70: function_param_iterator_c fp_iterator(fb_decl); lbessard@70: identifier_c *param_name; lbessard@70: function_call_param_iterator_c function_call_param_iterator(symbol); lbessard@70: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); lbessard@70: lbessard@70: /* Get the value from a foo( = ) style call */ ccb@202: symbol_c *param_value = function_call_param_iterator.search_f(param_name); lbessard@70: lbessard@70: /* Get the value from a foo() style call */ msousa@350: /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these msousa@350: * were implicitly defined! msousa@350: */ msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) ccb@202: param_value = function_call_param_iterator.next_nf(); lbessard@70: lbessard@98: symbol_c *param_type = fp_iterator.param_type(); lbessard@98: if (param_type == NULL) ERROR; lbessard@98: lbessard@123: /* now output the value assignment */ lbessard@70: if (param_value != NULL) lbessard@70: if ((param_direction == function_param_iterator_c::direction_in) || lbessard@70: (param_direction == function_param_iterator_c::direction_inout)) { berem@228: if (this->is_variable_prefix_null()) { berem@228: symbol->fb_name->accept(*this); berem@228: s4o.print("."); berem@228: param_name->accept(*this); berem@228: s4o.print(" = "); berem@228: print_check_function(param_type, param_value); laurent@217: } berem@228: else { berem@228: print_setter(param_name, param_type, param_value, symbol->fb_name); berem@228: } lbessard@70: s4o.print(";\n" + s4o.indent_spaces); lbessard@70: } lbessard@70: } /* for(...) */ lbessard@70: lbessard@70: /* now call the function... */ lbessard@70: function_block_type_name->accept(*this); lbessard@70: s4o.print(FB_FUNCTION_SUFFIX); lbessard@70: s4o.print("(&"); laurent@240: print_variable_prefix(); lbessard@70: symbol->fb_name->accept(*this); lbessard@70: s4o.print(")"); lbessard@70: lbessard@70: /* loop through each function parameter, find the variable to which lbessard@70: * we should atribute the value of all output or inoutput parameters. lbessard@70: */ lbessard@70: fp_iterator.reset(); lbessard@70: function_call_param_iterator.reset(); lbessard@70: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); lbessard@70: lbessard@70: /* Get the value from a foo( = ) style call */ ccb@202: symbol_c *param_value = function_call_param_iterator.search_f(param_name); lbessard@70: lbessard@70: /* Get the value from a foo() style call */ msousa@350: /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these msousa@350: * were implicitly defined! msousa@350: */ msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) ccb@202: param_value = function_call_param_iterator.next_nf(); lbessard@70: lbessard@70: /* now output the value assignment */ lbessard@70: if (param_value != NULL) lbessard@70: if ((param_direction == function_param_iterator_c::direction_out) || lbessard@70: (param_direction == function_param_iterator_c::direction_inout)) { msousa@321: symbol_c *param_type = search_varfb_instance_type->get_type_id(param_value); laurent@240: s4o.print(";\n" + s4o.indent_spaces); berem@228: if (this->is_variable_prefix_null()) { laurent@240: param_value->accept(*this); berem@228: s4o.print(" = "); berem@228: print_check_function(param_type, param_name, symbol->fb_name); berem@228: } berem@228: else { berem@228: print_setter(param_value, param_type, param_name, NULL, symbol->fb_name); berem@228: } lbessard@70: } lbessard@70: } /* for(...) */ lbessard@70: lbessard@70: s4o.print(";\n"); lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print("}"); lbessard@70: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: lbessard@70: /* | function_name '(' eol_list [il_param_list] ')' */ lbessard@70: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) lbessard@70: void *visit(il_formal_funct_call_c *symbol) { lbessard@149: symbol_c* function_type_prefix = NULL; lbessard@149: symbol_c* function_name = NULL; lbessard@149: symbol_c* function_type_suffix = NULL; laurent@217: DECLARE_PARAM_LIST() lbessard@149: laurent@233: function_call_param_iterator_c function_call_param_iterator(symbol); laurent@233: msousa@350: function_declaration_c *f_decl = (function_declaration_c *)symbol->called_function_declaration; msousa@350: if (f_decl == NULL) ERROR; msousa@350: msousa@350: function_name = symbol->function_name; msousa@350: msousa@350: /* loop through each function parameter, find the value we should pass msousa@350: * to it, and then output the c equivalent... msousa@350: */ msousa@350: function_param_iterator_c fp_iterator(f_decl); msousa@350: identifier_c *param_name; msousa@350: msousa@350: /* flag to cirreclty handle calls to extensible standard functions (i.e. functions with variable number of input parameters) */ msousa@350: bool found_first_extensible_parameter = false; msousa@350: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { msousa@350: if (fp_iterator.is_extensible_param() && (!found_first_extensible_parameter)) { msousa@350: /* We are calling an extensible function. Before passing the extensible msousa@350: * parameters, we must add a dummy paramater value to tell the called msousa@350: * function how many extensible parameters we will be passing. msousa@350: * msousa@350: * Note that stage 3 has already determined the number of extensible msousa@350: * paramters, and stored that info in the abstract syntax tree. We simply msousa@350: * re-use that value. msousa@350: */ msousa@350: /* NOTE: we are not freeing the malloc'd memory. This is not really a bug. msousa@350: * Since we are writing a compiler, which runs to termination quickly, msousa@350: * we can consider this as just memory required for the compilation process msousa@350: * that will be free'd when the program terminates. msousa@350: */ msousa@350: char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */ msousa@350: if (tmp == NULL) ERROR; msousa@350: int res = snprintf(tmp, 32, "%d", symbol->extensible_param_count); msousa@350: if ((res >= 32) || (res < 0)) ERROR; msousa@350: identifier_c *param_value = new identifier_c(tmp); msousa@350: uint_type_name_c *param_type = new uint_type_name_c(); msousa@350: identifier_c *param_name = new identifier_c(""); msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, function_param_iterator_c::direction_in) msousa@350: found_first_extensible_parameter = true; msousa@350: } lbessard@149: msousa@350: if (fp_iterator.is_extensible_param()) { msousa@350: /* since we are handling an extensible parameter, we must add the index to the msousa@350: * parameter name so we can go looking for the value passed to the correct msousa@350: * extended parameter (e.g. IN1, IN2, IN3, IN4, ...) msousa@350: */ msousa@350: char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */ msousa@350: int res = snprintf(tmp, 32, "%d", fp_iterator.extensible_param_index()); msousa@350: if ((res >= 32) || (res < 0)) ERROR; msousa@350: param_name = new identifier_c(strdup2(param_name->value, tmp)); msousa@350: if (param_name->value == NULL) ERROR; msousa@350: } msousa@350: msousa@350: symbol_c *param_type = fp_iterator.param_type(); msousa@350: if (param_type == NULL) ERROR; msousa@350: msousa@350: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); msousa@350: msousa@350: symbol_c *param_value = NULL; msousa@350: msousa@350: /* Get the value from a foo( = ) style call */ msousa@350: if (param_value == NULL) msousa@350: param_value = function_call_param_iterator.search_f(param_name); msousa@350: msousa@350: /* Get the value from a foo() style call */ msousa@350: /* NOTE: the following line of code is not required in this case, but it doesn't msousa@350: * harm to leave it in, as in the case of a formal syntax function call, msousa@350: * it will always return NULL. msousa@350: * We leave it in in case we later decide to merge this part of the code together msousa@350: * with the function calling code in generate_c_st_c, which does require msousa@350: * the following line... msousa@350: */ msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) { msousa@350: param_value = function_call_param_iterator.next_nf(); msousa@350: } lbessard@149: msousa@350: /* if no more parameter values in function call, and the current parameter msousa@350: * of the function declaration is an extensible parameter, we msousa@350: * have reached the end, and should simply jump out of the for loop. msousa@350: */ msousa@350: if ((param_value == NULL) && (fp_iterator.is_extensible_param())) { msousa@350: break; msousa@350: } lbessard@169: msousa@350: if ((param_value == NULL) && (param_direction == function_param_iterator_c::direction_in)) { msousa@350: /* No value given for parameter, so we must use the default... */ msousa@350: /* First check whether default value specified in function declaration...*/ msousa@350: param_value = fp_iterator.default_value(); msousa@350: } lbessard@149: msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction()) laurent@217: } laurent@217: laurent@233: if (function_call_param_iterator.next_nf() != NULL) ERROR; laurent@233: laurent@217: bool has_output_params = false; laurent@217: laurent@217: if (!this->is_variable_prefix_null()) { laurent@217: PARAM_LIST_ITERATOR() { msousa@350: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out || msousa@350: PARAM_DIRECTION == function_param_iterator_c::direction_inout) && msousa@350: PARAM_VALUE != NULL) { msousa@350: has_output_params = true; msousa@350: } msousa@350: } msousa@350: } msousa@350: msousa@350: /* Check whether we are calling an overloaded function! */ msousa@350: /* (fdecl_mutiplicity==2) => calling overloaded function */ msousa@350: int fdecl_mutiplicity = function_symtable.multiplicity(symbol->function_name); msousa@350: if (fdecl_mutiplicity == 0) ERROR; msousa@350: if (fdecl_mutiplicity == 1) msousa@350: /* function being called is NOT overloaded! */ msousa@350: f_decl = NULL; laurent@217: msousa@682: this->implicit_variable_result.accept(*this); lbessard@149: s4o.print(" = "); lbessard@149: lbessard@149: if (function_type_prefix != NULL) { lbessard@149: s4o.print("("); msousa@669: default_literal_type(function_type_prefix)->accept(*this); lbessard@149: s4o.print(")"); lbessard@149: } laurent@336: if (function_type_suffix != NULL) { msousa@669: function_type_suffix = default_literal_type(function_type_suffix); laurent@336: } laurent@217: if (has_output_params) { msousa@350: fcall_number++; msousa@350: s4o.print("__"); laurent@217: fbname->accept(*this); laurent@217: s4o.print("_"); lbessard@149: function_name->accept(*this); msousa@350: if (fdecl_mutiplicity == 2) { msousa@350: /* function being called is overloaded! */ msousa@350: s4o.print("__"); laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o); msousa@350: f_decl->accept(overloaded_func_suf); msousa@350: } msousa@594: s4o.print(fcall_number); laurent@217: } laurent@217: else { msousa@350: if (function_name != NULL) { laurent@217: function_name->accept(*this); msousa@350: if (fdecl_mutiplicity == 2) { msousa@350: /* function being called is overloaded! */ msousa@350: s4o.print("__"); laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o); msousa@350: f_decl->accept(overloaded_func_suf); msousa@350: } msousa@350: } laurent@217: if (function_type_suffix != NULL) laurent@217: function_type_suffix->accept(*this); laurent@217: } lbessard@70: s4o.print("("); lbessard@149: s4o.indent_right(); lbessard@149: laurent@217: int nb_param = 0; laurent@217: PARAM_LIST_ITERATOR() { msousa@350: symbol_c *param_value = PARAM_VALUE; msousa@350: current_param_type = PARAM_TYPE; laurent@217: switch (PARAM_DIRECTION) { lbessard@70: case function_param_iterator_c::direction_in: msousa@350: if (nb_param > 0) msousa@350: s4o.print(",\n"+s4o.indent_spaces); msousa@350: if (param_value == NULL) { lbessard@70: /* If not, get the default value of this variable's type */ laurent@235: param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance()); lbessard@70: } lbessard@70: if (param_value == NULL) ERROR; laurent@217: s4o.print("("); msousa@669: if (get_datatype_info_c::is_ANY_INT_literal(current_param_type)) msousa@669: search_constant_type_c::lint_type_name.accept(*this); msousa@669: else if (get_datatype_info_c::is_ANY_REAL_literal(current_param_type)) msousa@669: search_constant_type_c::lreal_type_name.accept(*this); laurent@220: else laurent@235: current_param_type->accept(*this); laurent@217: s4o.print(")"); laurent@235: print_check_function(current_param_type, param_value); msousa@350: nb_param++; laurent@217: break; lbessard@70: case function_param_iterator_c::direction_out: lbessard@70: case function_param_iterator_c::direction_inout: msousa@350: if (!has_output_params) { laurent@217: if (nb_param > 0) msousa@350: s4o.print(",\n"+s4o.indent_spaces); msousa@350: if (param_value == NULL) { msousa@350: s4o.print("NULL"); msousa@350: } else { msousa@350: wanted_variablegeneration = fparam_output_vg; msousa@350: param_value->accept(*this); msousa@350: wanted_variablegeneration = expression_vg; msousa@350: } msousa@350: } msousa@350: break; lbessard@70: case function_param_iterator_c::direction_extref: lbessard@70: /* TODO! */ lbessard@70: ERROR; msousa@350: break; lbessard@70: } /* switch */ lbessard@70: } /* for(...) */ laurent@217: if (has_output_params) { laurent@217: if (nb_param > 0) laurent@217: s4o.print(",\n"+s4o.indent_spaces); laurent@217: s4o.print(FB_FUNCTION_PARAM); laurent@217: } lbessard@70: lbessard@70: // symbol->parameter_assignment->accept(*this); lbessard@70: s4o.print(")"); laurent@217: laurent@217: CLEAR_PARAM_LIST() laurent@217: lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* | il_operand_list ',' il_operand */ lbessard@70: // SYM_LIST(il_operand_list_c) lbessard@70: void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called! lbessard@70: lbessard@70: lbessard@70: /* | simple_instr_list il_simple_instruction */ lbessard@70: // SYM_LIST(simple_instr_list_c) lbessard@70: void *visit(simple_instr_list_c *symbol) { msousa@682: /* A simple_instr_list_c is used to store a list of il operations being done within parenthesis... lbessard@70: * lbessard@70: * e.g.: lbessard@70: * LD var1 lbessard@70: * AND ( var2 lbessard@70: * OR var3 lbessard@70: * OR var4 lbessard@70: * ) lbessard@70: * msousa@682: * NOTE 1: lbessard@70: * This will be converted to C++ by defining a new scope msousa@682: * with a new il implicit variable, and executing the il operands lbessard@70: * within this new scope. lbessard@70: * At the end of the scope the result, i.e. the value currently stored msousa@682: * in the il implicit variable is copied to the variable used to take this lbessard@70: * value to the outside scope... lbessard@70: * lbessard@70: * The above example will result in the following C++ code: lbessard@70: * {__IL_DEFVAR_T __IL_DEFVAR_BACK; lbessard@70: * __IL_DEFVAR_T __IL_DEFVAR; lbessard@70: * lbessard@70: * __IL_DEFVAR.INTvar = var1; lbessard@70: * { lbessard@70: * __IL_DEFVAR_T __IL_DEFVAR; lbessard@70: * lbessard@70: * __IL_DEFVAR.INTvar = var2; lbessard@70: * __IL_DEFVAR.INTvar |= var3; lbessard@70: * __IL_DEFVAR.INTvar |= var4; lbessard@70: * lbessard@70: * __IL_DEFVAR_BACK = __IL_DEFVAR; lbessard@70: * } lbessard@70: * __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar; lbessard@70: * lbessard@70: * } lbessard@70: * msousa@682: * NOTE 2: msousa@682: * If the intial value of the il implicit variable (in the above msousa@682: * example 'var2') exists, then the il_expression_c will insert an equivalent msousa@682: * LD operation into the parenthesized instruction list- This means we do not msousa@682: * need to do anything here to handle this special situation! lbessard@70: */ lbessard@70: msousa@682: /* Declare the IL implicit variable, that will store the result of the IL operations... */ lbessard@70: s4o.print("{\n"); lbessard@70: s4o.indent_right(); msousa@682: declare_implicit_variable(); msousa@682: lbessard@70: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); lbessard@70: msousa@682: /* copy the result in the IL implicit variable to the variable msousa@682: * used to pass the data out to the scope enclosing the current scope! lbessard@70: */ msousa@682: this->implicit_variable_result_back.datatype = symbol->datatype; msousa@682: this->implicit_variable_result .datatype = symbol->datatype; msousa@682: lbessard@70: s4o.print("\n"); lbessard@70: s4o.print(s4o.indent_spaces); msousa@682: this->implicit_variable_result_back.accept(*this); lbessard@70: s4o.print(" = "); msousa@682: this->implicit_variable_result.accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: lbessard@70: s4o.indent_left(); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print("}\n"); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: return NULL; lbessard@70: } lbessard@70: msousa@682: msousa@453: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;) msousa@682: void *visit(il_simple_instruction_c *symbol) {return symbol->il_simple_instruction->accept(*this);} msousa@453: lbessard@70: /* | il_initial_param_list il_param_instruction */ lbessard@70: // SYM_LIST(il_param_list_c) msousa@682: void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called! lbessard@70: lbessard@70: /* il_assign_operator il_operand lbessard@70: * | il_assign_operator '(' eol_list simple_instr_list ')' lbessard@70: */ lbessard@70: // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) msousa@682: void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called! lbessard@70: lbessard@70: /* il_assign_out_operator variable */ lbessard@70: // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); msousa@682: void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called! msousa@682: lbessard@70: lbessard@70: /*******************/ lbessard@70: /* B 2.2 Operators */ lbessard@70: /*******************/ lbessard@70: msousa@682: void *visit(LD_operator_c *symbol) { laurent@382: if (wanted_variablegeneration != expression_vg) { laurent@382: s4o.print("LD"); laurent@382: return NULL; laurent@382: } msousa@682: XXX_operator(&(this->implicit_variable_result), " = ", this->current_operand); msousa@682: return NULL; msousa@682: } msousa@682: msousa@682: msousa@682: void *visit(LDN_operator_c *symbol) { msousa@682: XXX_operator(&(this->implicit_variable_result), get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)?" = !":" = ~", this->current_operand); msousa@682: return NULL; msousa@682: } msousa@682: msousa@682: msousa@682: void *visit(ST_operator_c *symbol) { berem@228: if (this->is_variable_prefix_null()) { laurent@217: this->current_operand->accept(*this); laurent@217: s4o.print(" = "); msousa@682: print_check_function(this->current_operand->datatype, (symbol_c*)&(this->implicit_variable_current)); berem@228: } berem@228: else { msousa@682: print_setter(this->current_operand, this->current_operand->datatype, (symbol_c*)&(this->implicit_variable_current)); msousa@682: } msousa@682: return NULL; msousa@682: } msousa@682: msousa@682: msousa@682: void *visit(STN_operator_c *symbol) { berem@228: if (this->is_variable_prefix_null()) { laurent@217: this->current_operand->accept(*this); laurent@217: s4o.print(" = "); msousa@682: if (get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)) berem@228: s4o.print("!"); berem@228: else msousa@682: s4o.print("~"); msousa@682: this->implicit_variable_current.accept(*this); berem@228: } berem@228: else { msousa@682: print_setter(this->current_operand, this->current_operand->datatype, (symbol_c*)&(this->implicit_variable_current), NULL, NULL, true); msousa@682: } msousa@682: return NULL; msousa@682: } msousa@682: msousa@682: msousa@682: void *visit(NOT_operator_c *symbol) { msousa@470: /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional msousa@470: * NOT [] msousa@470: * However, it does not define the semantic of the NOT operation when the is specified. msousa@470: * We therefore consider it an error if an il_operand is specified! msousa@470: * The error is caught in stage 3! msousa@470: */ msousa@682: if (NULL != this->current_operand) ERROR; msousa@682: XXX_operator(&(this->implicit_variable_result), get_datatype_info_c::is_BOOL_compatible(symbol->datatype)?" = !":" = ~", &(this->implicit_variable_current)); msousa@682: return NULL; msousa@682: } msousa@682: lbessard@70: lbessard@70: void *visit(S_operator_c *symbol) { laurent@382: if (wanted_variablegeneration != expression_vg) { laurent@382: s4o.print("LD"); laurent@382: return NULL; laurent@382: } laurent@382: msousa@682: if ((NULL == this->current_operand) || (NULL == this->current_operand->datatype)) ERROR; lbessard@70: lbessard@70: C_modifier(); lbessard@70: this->current_operand->accept(*this); laurent@240: s4o.print(" = __"); msousa@682: if (get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)) { laurent@240: s4o.print("BOOL_LITERAL(TRUE)"); msousa@682: } else if (get_datatype_info_c::is_ANY_INT_compatible(this->current_operand->datatype)) { msousa@682: this->current_operand->datatype->accept(*this); laurent@240: s4o.print("_LITERAL(1)"); msousa@682: } else laurent@240: ERROR; msousa@682: return NULL; msousa@682: } msousa@682: lbessard@70: lbessard@70: void *visit(R_operator_c *symbol) { laurent@382: if (wanted_variablegeneration != expression_vg) { laurent@382: s4o.print("LD"); laurent@382: return NULL; laurent@382: } laurent@382: msousa@682: if ((NULL == this->current_operand) || (NULL == this->current_operand->datatype)) ERROR; lbessard@70: lbessard@70: C_modifier(); lbessard@70: this->current_operand->accept(*this); laurent@240: s4o.print(" = __"); msousa@682: if (get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)) { laurent@240: s4o.print("BOOL_LITERAL(FALSE)"); msousa@682: } else if (get_datatype_info_c::is_ANY_INT_compatible(this->current_operand->datatype)) { msousa@682: this->current_operand->datatype->accept(*this); laurent@240: s4o.print("_LITERAL(0)"); msousa@682: } else laurent@240: ERROR; lbessard@70: /* the data type resulting from this operation is unchanged! */ lbessard@70: return NULL; lbessard@70: } lbessard@70: msousa@682: msousa@682: void *visit( S1_operator_c *symbol) {return XXX_CAL_operator( "S1", this->current_operand);} msousa@682: void *visit( R1_operator_c *symbol) {return XXX_CAL_operator( "R1", this->current_operand);} lbessard@70: void *visit(CLK_operator_c *symbol) {return XXX_CAL_operator("CLK", this->current_operand);} msousa@682: void *visit( CU_operator_c *symbol) {return XXX_CAL_operator( "CU", this->current_operand);} msousa@682: void *visit( CD_operator_c *symbol) {return XXX_CAL_operator( "CD", this->current_operand);} msousa@682: void *visit( PV_operator_c *symbol) {return XXX_CAL_operator( "PV", this->current_operand);} msousa@682: void *visit( IN_operator_c *symbol) {return XXX_CAL_operator( "IN", this->current_operand);} msousa@682: void *visit( PT_operator_c *symbol) {return XXX_CAL_operator( "PT", this->current_operand);} lbessard@70: lbessard@70: void *visit(AND_operator_c *symbol) { msousa@682: if (!get_datatype_info_c::is_ANY_BIT_compatible(symbol->datatype)) ERROR; msousa@682: XXX_operator(&(this->implicit_variable_result), " &= ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(OR_operator_c *symbol) { msousa@682: if (!get_datatype_info_c::is_ANY_BIT_compatible(symbol->datatype)) ERROR; msousa@682: XXX_operator(&(this->implicit_variable_result), " |= ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(XOR_operator_c *symbol) { msousa@682: if (!get_datatype_info_c::is_ANY_BIT_compatible(symbol->datatype)) ERROR; msousa@682: // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types! msousa@682: XXX_operator(&(this->implicit_variable_result), " ^= ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(ANDN_operator_c *symbol) { msousa@682: if (!get_datatype_info_c::is_ANY_BIT_compatible(symbol->datatype)) ERROR; msousa@682: XXX_operator(&(this->implicit_variable_result), get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)?" &= !":" &= ~", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(ORN_operator_c *symbol) { msousa@682: if (!get_datatype_info_c::is_ANY_BIT_compatible(symbol->datatype)) ERROR; msousa@682: XXX_operator(&(this->implicit_variable_result), get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)?" |= !":" |= ~", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(XORN_operator_c *symbol) { msousa@682: if (!get_datatype_info_c::is_ANY_BIT_compatible(symbol->datatype)) ERROR; msousa@682: // bit by bit exclusive OR !! Also seems to work with boolean types! msousa@682: XXX_operator(&(this->implicit_variable_result), get_datatype_info_c::is_BOOL_compatible(this->current_operand->datatype)?" ^= !":" ^= ~", this->current_operand); msousa@682: return NULL; msousa@682: } msousa@669: lbessard@70: void *visit(ADD_operator_c *symbol) { msousa@682: if (get_datatype_info_c::is_TIME_compatible(symbol->datatype) || get_datatype_info_c::is_ANY_DATE_compatible (symbol->datatype)) msousa@682: XXX_function(&(this->implicit_variable_result), "__time_add", &(this->implicit_variable_current), this->current_operand); msousa@682: else XXX_operator(&(this->implicit_variable_result), " += ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(SUB_operator_c *symbol) { msousa@682: if (get_datatype_info_c::is_TIME_compatible(symbol->datatype) || get_datatype_info_c::is_ANY_DATE_compatible (symbol->datatype)) msousa@682: XXX_function(&(this->implicit_variable_result), "__time_sub", &(this->implicit_variable_current), this->current_operand); msousa@682: else XXX_operator(&(this->implicit_variable_result), " -= ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(MUL_operator_c *symbol) { msousa@682: if (get_datatype_info_c::is_TIME_compatible(symbol->datatype)) msousa@682: XXX_function(&(this->implicit_variable_result), "__time_mul", &(this->implicit_variable_current), this->current_operand); msousa@682: else XXX_operator(&(this->implicit_variable_result), " *= ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(DIV_operator_c *symbol) { msousa@682: if (get_datatype_info_c::is_TIME_compatible(symbol->datatype)) msousa@682: XXX_function(&(this->implicit_variable_result), "__time_div", &(this->implicit_variable_current), this->current_operand); msousa@682: else XXX_operator(&(this->implicit_variable_result), " /= ", this->current_operand); msousa@682: return NULL; msousa@682: } msousa@682: msousa@682: void *visit(MOD_operator_c *symbol) {XXX_operator(&(this->implicit_variable_result), " %= ", this->current_operand); return NULL;} lbessard@70: msousa@653: void *visit(GT_operator_c *symbol) {CMP_operator(this->current_operand, "GT_"); return NULL;} msousa@653: void *visit(GE_operator_c *symbol) {CMP_operator(this->current_operand, "GE_"); return NULL;} msousa@653: void *visit(EQ_operator_c *symbol) {CMP_operator(this->current_operand, "EQ_"); return NULL;} msousa@653: void *visit(LT_operator_c *symbol) {CMP_operator(this->current_operand, "LT_"); return NULL;} msousa@653: void *visit(LE_operator_c *symbol) {CMP_operator(this->current_operand, "LE_"); return NULL;} msousa@653: void *visit(NE_operator_c *symbol) {CMP_operator(this->current_operand, "NE_"); return NULL;} lbessard@70: lbessard@70: lbessard@70: //SYM_REF0(CAL_operator_c) lbessard@70: // This method will be called from within the il_fb_call_c visitor method lbessard@70: void *visit(CAL_operator_c *symbol) {return NULL;} lbessard@70: lbessard@70: //SYM_REF0(CALC_operator_c) lbessard@70: // This method will be called from within the il_fb_call_c visitor method lbessard@70: void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;} lbessard@70: lbessard@70: //SYM_REF0(CALCN_operator_c) lbessard@70: // This method will be called from within the il_fb_call_c visitor method lbessard@70: void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;} lbessard@70: lbessard@70: /* NOTE: The semantics of the RET operator requires us to return a value lbessard@70: * if the IL code is inside a function, but simply return no value if lbessard@70: * the IL code is inside a function block or program! lbessard@70: * Nevertheless, it is the generate_c_c class itself that lbessard@70: * introduces the 'reaturn ' into the c++ code at the end lbessard@70: * of every function. This class does not know whether the IL code lbessard@70: * is inside a function or a function block. lbessard@70: * We work around this by jumping to the end of the code, lbessard@70: * that will be marked by the END_LABEL label in the lbessard@70: * instruction_list_c visitor... lbessard@70: */ lbessard@70: // SYM_REF0(RET_operator_c) lbessard@70: void *visit(RET_operator_c *symbol) { lbessard@70: s4o.print("goto ");s4o.print(END_LABEL); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: // SYM_REF0(RETC_operator_c) lbessard@70: void *visit(RETC_operator_c *symbol) { lbessard@70: C_modifier(); lbessard@70: s4o.print("goto ");s4o.print(END_LABEL); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: // SYM_REF0(RETCN_operator_c) lbessard@70: void *visit(RETCN_operator_c *symbol) { lbessard@70: CN_modifier(); lbessard@70: s4o.print("goto ");s4o.print(END_LABEL); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: //SYM_REF0(JMP_operator_c) lbessard@70: void *visit(JMP_operator_c *symbol) { lbessard@70: if (NULL == this->jump_label) ERROR; lbessard@70: s4o.print("goto "); lbessard@70: this->jump_label->accept(*this); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: // SYM_REF0(JMPC_operator_c) lbessard@70: void *visit(JMPC_operator_c *symbol) { lbessard@70: if (NULL == this->jump_label) ERROR; lbessard@70: C_modifier(); lbessard@70: s4o.print("goto "); lbessard@70: this->jump_label->accept(*this); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: // SYM_REF0(JMPCN_operator_c) lbessard@70: void *visit(JMPCN_operator_c *symbol) { lbessard@70: if (NULL == this->jump_label) ERROR; lbessard@70: CN_modifier(); lbessard@70: s4o.print("goto "); lbessard@70: this->jump_label->accept(*this); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: #if 0 lbessard@70: /*| [NOT] any_identifier SENDTO */ lbessard@70: SYM_REF2(il_assign_out_operator_c, option, variable_name) lbessard@70: #endif lbessard@70: lbessard@70: }; /* generate_c_il_c */ lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: /* The implementation of the single visit() member function lbessard@70: * of il_default_variable_c. lbessard@70: * It can only come after the full declaration of lbessard@70: * generate_c_il_c. Since we define and declare lbessard@70: * generate_c_il_c simultaneously, it can only come lbessard@70: * after the definition... lbessard@70: */ lbessard@70: void *il_default_variable_c::accept(visitor_c &visitor) { lbessard@70: /* An ugly hack!! */ lbessard@70: /* This is required because we need to over-ride the base lbessard@70: * accept(visitor_c &) method of the class symbol_c, lbessard@70: * so this method may be called through a symbol_c * lbessard@70: * reference! lbessard@70: * lbessard@70: * But, the visitor_c does not include a visitor to lbessard@70: * an il_default_variable_c, which means that we couldn't lbessard@70: * simply call visitor.visit(this); lbessard@70: * lbessard@70: * We therefore need to use the dynamic_cast hack!! lbessard@70: * lbessard@70: * Note too that we can't cast a visitor_c to a lbessard@70: * il_default_variable_visitor_c, since they are not related. lbessard@70: * Nor may the il_default_variable_visitor_c inherit from lbessard@70: * visitor_c, because then generate_c_il_c would contain lbessard@70: * two visitor_c base classes, one each through lbessard@70: * il_default_variable_visitor_c and generate_c_type_c lbessard@70: * lbessard@70: * We could use virtual inheritance of the visitor_c, but it lbessard@70: * would probably create more problems than it is worth! lbessard@70: */ lbessard@70: generate_c_il_c *v; lbessard@70: v = dynamic_cast(&visitor); lbessard@70: if (v == NULL) ERROR; lbessard@70: lbessard@70: return v->visit(this); lbessard@70: } lbessard@70: lbessard@70: lbessard@70: lbessard@70: lbessard@70: il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) { lbessard@70: if (NULL == var_name_str) ERROR; lbessard@70: /* Note: current_type may start off with NULL */ lbessard@70: lbessard@70: this->var_name = new identifier_c(var_name_str); lbessard@70: if (NULL == this->var_name) ERROR; lbessard@70: msousa@682: this->datatype = current_type; msousa@682: }