lbessard@70: /* lbessard@70: * (c) 2003 Mario de Sousa lbessard@70: * lbessard@70: * Offered to the public under the terms of the GNU General Public License lbessard@70: * as published by the Free Software Foundation; either version 2 of the lbessard@70: * License, or (at your option) any later version. lbessard@70: * lbessard@70: * This program is distributed in the hope that it will be useful, but lbessard@70: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@70: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General lbessard@70: * Public License for more details. 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: * An IEC 61131-3 IL and ST compiler. lbessard@70: * lbessard@70: * Based on the lbessard@70: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) lbessard@70: * lbessard@70: */ lbessard@70: lbessard@70: lbessard@70: /* lbessard@70: * Conversion of il statements (i.e. IL code). lbessard@70: * lbessard@70: * This is part of the 4th stage that generates lbessard@70: * a c++ source program equivalent to the IL and ST lbessard@70: * code. lbessard@70: */ lbessard@70: lbessard@70: 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: class search_il_operand_type_c { lbessard@70: lbessard@70: private: lbessard@70: search_varfb_instance_type_c search_varfb_instance_type; lbessard@70: search_constant_type_c search_constant_type; lbessard@70: lbessard@70: public: lbessard@70: search_il_operand_type_c(symbol_c *search_scope): search_varfb_instance_type(search_scope) {} lbessard@70: lbessard@70: public: lbessard@70: symbol_c *get_type(symbol_c *il_operand) { lbessard@70: symbol_c *res; lbessard@70: lbessard@70: /* We first assume that it is a constant... */ lbessard@70: res = search_constant_type.get_type(il_operand); lbessard@70: if (res != NULL) return res; lbessard@70: lbessard@70: /* Nope, now we assume it is a variable, and determine its type... */ lbessard@70: res = search_varfb_instance_type.get_type(il_operand); lbessard@70: if (NULL != res) return res; lbessard@70: lbessard@70: /* not found */ lbessard@70: return NULL; lbessard@70: } lbessard@70: }; lbessard@70: lbessard@70: lbessard@70: lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: /***********************************************************************/ lbessard@70: lbessard@70: lbessard@70: /* A new class to ouput the il default 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 lbessard@70: * the IL default 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 lbessard@70: * the value currently being stored in the default 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 lbessard@70: * keep track of the data type currently stored in the default 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: symbol_c *current_type; 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: lbessard@70: class generate_c_il_c: public generate_c_typedecl_c, il_default_variable_visitor_c { lbessard@70: lbessard@70: private: lbessard@70: /* When compiling il code, it becomes necessary to determine the lbessard@70: * data type of il operands. To do this, we must first find the lbessard@70: * il operand's declaration, within the scope of the function block lbessard@70: * or function currently being processed. lbessard@70: * The following object does just that... lbessard@70: * This object instance will then later be called while the lbessard@70: * remaining il code is being handled. lbessard@70: */ lbessard@70: //search_il_operand_type_c *search_il_operand_type; lbessard@70: search_expression_type_c *search_expression_type; lbessard@70: lbessard@70: /* The initial value that should be given to the IL default variable lbessard@70: * imediately after a parenthesis is opened. lbessard@70: * This variable is only used to pass data from the lbessard@70: * il_expression_c visitor to the simple_instr_list_c visitor. lbessard@70: * lbessard@70: * e.g.: lbessard@70: * LD var1 lbessard@70: * AND ( var2 lbessard@70: * OR var3 lbessard@70: * ) lbessard@70: * lbessard@70: * In the above code sample, the line 'AND ( var2' constitutes lbessard@70: * an il_expression_c, where var2 should be loaded into the lbessard@70: * il default variable before continuing with the expression lbessard@70: * inside the parenthesis. lbessard@70: * Unfortunately, only the simple_instr_list_c may do the lbessard@70: * initial laoding of the var2 bariable following the parenthesis, lbessard@70: * so the il_expression_c visitor will have to pass 'var2' as a lbessard@70: * parameter to the simple_instr_list_c visitor. lbessard@70: * Ergo, the existance of the following parameter...! lbessard@70: */ lbessard@70: symbol_c *il_default_variable_init_value; lbessard@70: 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: symbol_c *current_operand_type; lbessard@70: lbessard@70: /* Label to which the current IL jump operation should jump to... */ lbessard@70: /* This variable is used to pass data from the lbessard@70: * il_jump_operation_c visitor lbessard@70: * to the il jump operator visitors (i.e. JMP_operator_c, lbessard@70: * JMPC_operator_c, JMPCN_operator_c, ...) lbessard@70: */ lbessard@70: symbol_c *jump_label; lbessard@70: lbessard@70: /* The result of the comparison IL operations (GT, EQ, LT, ...) lbessard@70: * is a boolean variable. lbessard@70: * This class keeps track of the current data type stored in the lbessard@70: * il default variable. This is usually done by keeping a reference lbessard@70: * to the data type of the last operand. Nevertheless, in the case of lbessard@70: * the comparison IL operators, the data type of the result (a boolean) lbessard@70: * is not the data type of the operand. We therefore need an object lbessard@70: * of the boolean data type to keep as a reference of the current lbessard@70: * data type. lbessard@70: * The following object is it... lbessard@70: */ lbessard@70: bool_type_name_c bool_type; lbessard@70: lbessard@70: /* the data type of the IL default variable... */ lbessard@70: #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T" lbessard@70: /* The name of the IL default variable... */ lbessard@70: #define IL_DEFVAR VAR_LEADER "IL_DEFVAR" lbessard@70: /* The name of the variable used to pass the result of a lbessard@70: * parenthesised instruction list to the immediately preceding lbessard@70: * scope ... lbessard@70: */ lbessard@70: #define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK" lbessard@70: il_default_variable_c default_variable_name; lbessard@70: il_default_variable_c default_variable_back_name; lbessard@70: lbessard@70: /* Some function calls in the body of functions or function blocks lbessard@70: * may leave some parameters to their default values, and lbessard@70: * ignore some output parameters of the function being called. lbessard@70: * Our conversion of ST functions to C++ does not contemplate that, lbessard@70: * i.e. each called function must get all it's input and output lbessard@70: * parameters set correctly. lbessard@70: * For input parameters we merely need to call the function with lbessard@70: * the apropriate default value, but for output parameters lbessard@70: * we must create temporary variables to hold the output value. lbessard@70: * lbessard@70: * We declare all the temporary output variables at the begining of lbessard@70: * the body of each function or function block, and use them as lbessard@70: * in function calls later on as they become necessary... lbessard@70: * Note that we cannot create these variables just before a function lbessard@70: * call, as the function call itself may be integrated within an lbessard@70: * expression, or another function call! lbessard@70: * lbessard@70: * The variables are declared in the exact same order in which they lbessard@70: * will be used later on during the function calls, which allows us lbessard@70: * to simply re-create the name that was used for the temporary variable lbessard@70: * instead of keeping it in some list. lbessard@70: * The names are recreated by the temp_var_name_factory, after reset() lbessard@70: * has been called! lbessard@70: * lbessard@70: * This function will genertae code similar to... lbessard@70: * lbessard@70: * INT __TMP_0 = 23; lbessard@70: * REAL __TMP_1 = 45.5; lbessard@70: * ... lbessard@70: */ lbessard@70: temp_var_name_c temp_var_name_factory; 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; lbessard@98: lbessard@98: search_base_type_c search_base_type; lbessard@70: lbessard@146: bool current_param_is_pointer; lbessard@146: lbessard@70: public: lbessard@70: generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) lbessard@70: : generate_c_typedecl_c(s4o_ptr), lbessard@70: default_variable_name(IL_DEFVAR, NULL), lbessard@70: default_variable_back_name(IL_DEFVAR_BACK, NULL) lbessard@70: { lbessard@70: //search_il_operand_type = new search_il_operand_type_c(scope); lbessard@70: search_expression_type = new search_expression_type_c(scope); lbessard@70: search_fb_instance_decl = new search_fb_instance_decl_c(scope); lbessard@98: search_varfb_instance_type = new search_varfb_instance_type_c(scope); lbessard@70: current_operand = NULL; lbessard@70: current_operand_type = NULL; lbessard@70: il_default_variable_init_value = NULL; lbessard@146: current_param_is_pointer = false; 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@70: //delete search_il_operand_type; lbessard@70: delete search_expression_type; lbessard@98: delete search_varfb_instance_type; lbessard@70: } lbessard@70: lbessard@70: void generate(instruction_list_c *il) { lbessard@70: generate_c_tempvardecl_c generate_c_tempvardecl(&s4o); lbessard@70: generate_c_tempvardecl.generate(il, &temp_var_name_factory); lbessard@70: il->accept(*this); lbessard@70: } lbessard@70: lbessard@70: /* Declare the backup to the default variable, that will store the result lbessard@70: * of the IL operations executed inside a parenthesis... lbessard@70: */ lbessard@70: void declare_backup_variable(void) { lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print(IL_DEFVAR_T); lbessard@70: s4o.print(" "); lbessard@70: print_backup_variable(); lbessard@70: s4o.print(";\n"); lbessard@70: } lbessard@70: lbessard@70: void print_backup_variable(void) { lbessard@70: this->default_variable_back_name.accept(*this); lbessard@70: } lbessard@70: lbessard@70: private: lbessard@70: /* A helper function... */ lbessard@70: /* lbessard@70: bool is_bool_type(symbol_c *type_symbol) { lbessard@70: return (NULL != dynamic_cast(type_symbol)); lbessard@70: } lbessard@70: */ lbessard@70: lbessard@70: /* A helper function... */ lbessard@70: void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) { lbessard@70: if ((NULL == lo) || (NULL == ro)) ERROR; lbessard@70: if (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... */ lbessard@70: void *XXX_function(const char *func, symbol_c *lo, symbol_c *ro) { lbessard@70: if ((NULL == lo) || (NULL == ro)) ERROR; lbessard@70: if (NULL == func) ERROR; lbessard@70: lbessard@70: lo->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: lbessard@70: /* A helper function... */ lbessard@70: void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) { 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; lbessard@70: 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) lbessard@70: il_param_assignment_c il_param_assignment(¶m, &this->default_variable_name, NULL); lbessard@70: // SYM_LIST(il_param_list_c) lbessard@70: 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... */ lbessard@70: void *CMP_operator(symbol_c *o, const char *operation) { lbessard@70: if (NULL == o) ERROR; lbessard@70: if (NULL == this->default_variable_name.current_type) ERROR; lbessard@70: lbessard@70: symbol_c *backup = this->default_variable_name.current_type; lbessard@70: this->default_variable_name.current_type = &(this->bool_type); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: this->default_variable_name.current_type = backup; lbessard@70: lbessard@70: s4o.print(" = "); lbessard@70: s4o.print(operation); lbessard@70: this->default_variable_name.current_type->accept(*this); lbessard@70: s4o.print("(2, "); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(", "); lbessard@70: o->accept(*this); lbessard@70: s4o.print(")"); lbessard@70: lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = &(this->bool_type); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: /* A helper function... */ lbessard@70: void C_modifier(void) { lbessard@70: if (search_expression_type->is_bool_type(default_variable_name.current_type)) { lbessard@70: s4o.print("if ("); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(") "); lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: } lbessard@70: lbessard@70: /* A helper function... */ lbessard@70: void CN_modifier(void) { lbessard@70: if (search_expression_type->is_bool_type(default_variable_name.current_type)) { lbessard@70: s4o.print("if (!"); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(") "); lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: } lbessard@70: lbessard@70: lbessard@70: public: lbessard@70: void *visit(il_default_variable_c *symbol) { lbessard@70: //s4o.print("il_default_variable_c VISITOR!!\n"); lbessard@70: symbol->var_name->accept(*this); lbessard@70: if (NULL != symbol->current_type) { lbessard@70: s4o.print("."); lbessard@70: symbol->current_type->accept(*this); lbessard@70: s4o.print("var"); lbessard@70: } lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: lbessard@70: private: lbessard@70: lbessard@146: void *visit(eno_param_c *symbol) { lbessard@146: if (this->is_variable_prefix_null()) { lbessard@146: s4o.print("*"); lbessard@146: } lbessard@146: else { lbessard@146: this->print_variable_prefix(); lbessard@146: } lbessard@146: s4o.print("ENO"); lbessard@146: return NULL; lbessard@146: } lbessard@146: lbessard@146: /*********************/ lbessard@146: /* B 1.4 - Variables */ lbessard@146: /*********************/ lbessard@146: void *visit(symbolic_variable_c *symbol) { lbessard@146: unsigned int vartype = search_varfb_instance_type->get_vartype(symbol); lbessard@146: if (!current_param_is_pointer && (vartype == search_var_instance_decl_c::external_vt || vartype == search_var_instance_decl_c::located_vt)) { lbessard@146: s4o.print("*("); lbessard@146: generate_c_base_c::visit(symbol); lbessard@146: s4o.print(")"); lbessard@146: } lbessard@146: else if (current_param_is_pointer && vartype != search_var_instance_decl_c::external_vt && vartype != search_var_instance_decl_c::located_vt) { lbessard@146: s4o.print("&("); lbessard@146: generate_c_base_c::visit(symbol); lbessard@146: s4o.print(")"); lbessard@146: } lbessard@146: else { lbessard@146: generate_c_base_c::visit(symbol); lbessard@146: } 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; lbessard@146: if (!current_param_is_pointer) { lbessard@146: s4o.print("*("); lbessard@146: } lbessard@70: this->print_variable_prefix(); lbessard@70: s4o.printlocation(symbol->value + 1); lbessard@146: if (!current_param_is_pointer) { lbessard@146: s4o.print(")"); lbessard@146: } lbessard@70: return NULL; lbessard@70: } lbessard@70: 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: /* please see the comment before the RET_operator_c visitor for details... */ lbessard@70: #define END_LABEL VAR_LEADER "end" lbessard@70: lbessard@70: /*| instruction_list il_instruction */ lbessard@70: void *visit(instruction_list_c *symbol) { lbessard@70: lbessard@70: /* Declare the backup to the default variable, that will store the result lbessard@70: * of the IL operations executed inside a parenthesis... lbessard@70: */ lbessard@70: declare_backup_variable(); lbessard@70: lbessard@70: /* Declare the default variable, that will store the result of the IL operations... */ lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print(IL_DEFVAR_T); lbessard@70: s4o.print(" "); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(";\n\n"); lbessard@70: lbessard@70: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); lbessard@70: lbessard@70: /* write the label marking the end of the code block */ lbessard@70: /* please see the comment before the RET_operator_c visitor for details... */ lbessard@70: s4o.print("\n"); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print(END_LABEL); lbessard@70: s4o.print(":\n"); lbessard@70: s4o.indent_right(); lbessard@70: /* since every label must be followed by at least one statement, and lbessard@70: * only the functions will introduce the return statement after this label, lbessard@70: * function blocks written in IL would result in invalid C++ code. lbessard@70: * To work around this we introduce the equivalent of a 'nop' operation lbessard@70: * to humour the compiler... lbessard@70: */ lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print("/* to humour the compiler, we insert a nop */\n"); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: s4o.indent_left(); lbessard@70: 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) { 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: } lbessard@70: symbol->il_instruction->accept(*this); lbessard@70: return NULL; lbessard@70: } 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: if (NULL == this->current_operand) { lbessard@70: this->current_operand_type = NULL; lbessard@70: } else { lbessard@70: this->current_operand_type = search_expression_type->get_type(this->current_operand); lbessard@70: if (NULL == this->current_operand_type) ERROR; lbessard@70: } lbessard@70: lbessard@70: symbol->il_simple_operator->accept(*this); lbessard@70: lbessard@70: this->current_operand = NULL; lbessard@70: this->current_operand_type = 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@70: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); lbessard@70: lbessard@70: if (f_decl == function_symtable.end_value()) { lbessard@70: /* should never occur. The function being called MUST be in the symtable... */ lbessard@70: function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); lbessard@70: if (current_function_type == function_none) ERROR; lbessard@70: lbessard@70: symbol_c *param_data_type = default_variable_name.current_type; lbessard@70: symbol_c *return_data_type = (symbol_c *)search_expression_type->compute_standard_function_il(symbol, param_data_type); lbessard@70: if (NULL == return_data_type) ERROR; lbessard@70: lbessard@70: default_variable_name.current_type = return_data_type; lbessard@70: this->default_variable_name.accept(*this); lbessard@70: default_variable_name.current_type = param_data_type; lbessard@70: s4o.print(" = "); lbessard@70: lbessard@70: function_call_param_iterator_c function_call_param_iterator(symbol); lbessard@70: lbessard@70: int nb_param = 1; lbessard@70: if (symbol->il_operand_list != NULL) lbessard@70: nb_param += ((list_c *)symbol->il_operand_list)->n; lbessard@70: lbessard@146: #include "il_code_gen.c" lbessard@70: lbessard@70: /* the data type returned by the function, and stored in the il default variable... */ lbessard@70: default_variable_name.current_type = return_data_type; lbessard@70: } lbessard@70: else { lbessard@70: /* determine the base data type returned by the function being called... */ lbessard@70: search_base_type_c search_base_type; lbessard@70: symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); lbessard@70: symbol_c *param_data_type = default_variable_name.current_type; lbessard@70: if (NULL == return_data_type) ERROR; lbessard@70: lbessard@70: default_variable_name.current_type = return_data_type; lbessard@70: this->default_variable_name.accept(*this); lbessard@70: default_variable_name.current_type = param_data_type; lbessard@70: s4o.print(" = "); lbessard@70: lbessard@70: symbol->function_name->accept(*this); lbessard@70: s4o.print("("); lbessard@70: lbessard@70: /* loop through each function parameter, find the value we should pass lbessard@70: * to it, and then output the c equivalent... lbessard@70: */ lbessard@70: lbessard@70: function_param_iterator_c fp_iterator(f_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: if (i != 1) lbessard@70: s4o.print(", "); lbessard@70: lbessard@70: symbol_c *param_type = fp_iterator.param_type(); lbessard@70: if (param_type == NULL) ERROR; lbessard@70: lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); lbessard@70: lbessard@70: lbessard@70: symbol_c *param_value = NULL; lbessard@70: lbessard@70: /* if it is the first parameter, semantics specifies that we should lbessard@70: * get the value off the IL default variable! lbessard@70: */ lbessard@70: if (1 == i) lbessard@70: param_value = &this->default_variable_name; lbessard@70: lbessard@70: /* Get the value from a foo( = ) style call */ lbessard@70: /* NOTE: the following line of code is not required in this case, but it doesn't lbessard@70: * harm to leave it in, as in the case of a non-formal syntax function call, lbessard@70: * it will always return NULL. lbessard@70: * We leave it in in case we later decide to merge this part of the code together lbessard@70: * with the function calling code in generate_c_st_c, which does require lbessard@70: * the following line... lbessard@70: */ lbessard@70: if (param_value == NULL) lbessard@70: param_value = function_call_param_iterator.search(param_name); lbessard@70: lbessard@70: /* Get the value from a foo() style call */ lbessard@70: if (param_value == NULL) lbessard@70: param_value = function_call_param_iterator.next(); lbessard@70: lbessard@70: switch (param_direction) { lbessard@70: case function_param_iterator_c::direction_in: lbessard@70: if (param_value == NULL) { lbessard@70: /* No value given for parameter, so we must use the default... */ lbessard@70: /* First check whether default value specified in function declaration...*/ lbessard@70: param_value = fp_iterator.default_value(); lbessard@70: } lbessard@70: if (param_value == NULL) { lbessard@70: /* If not, get the default value of this variable's type */ lbessard@70: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); lbessard@70: } lbessard@70: if (param_value == NULL) ERROR; lbessard@70: param_value->accept(*this); lbessard@70: break; lbessard@70: case function_param_iterator_c::direction_out: lbessard@70: case function_param_iterator_c::direction_inout: lbessard@146: current_param_is_pointer = true; lbessard@70: if (param_value == NULL) { lbessard@146: s4o.print("NULL"); lbessard@70: } else { lbessard@70: param_value->accept(*this); lbessard@70: } lbessard@146: current_param_is_pointer = false; lbessard@70: break; lbessard@70: case function_param_iterator_c::direction_extref: lbessard@70: /* TODO! */ lbessard@70: ERROR; lbessard@70: break; lbessard@70: } /* switch */ lbessard@70: } /* for(...) */ lbessard@70: lbessard@70: s4o.print(")"); lbessard@70: /* the data type returned by the function, and stored in the il default variable... */ lbessard@70: default_variable_name.current_type = return_data_type; lbessard@70: } lbessard@70: 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) { lbessard@70: /* We will be recursevely interpreting an instruction list, lbessard@70: * so we store a backup of the data type of the value currently stored lbessard@70: * in the default variable, and set the current data type to NULL lbessard@70: */ lbessard@70: symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type; lbessard@70: this->default_variable_name.current_type = NULL; lbessard@70: lbessard@70: /* Pass the symbol->il_operand to the simple_instr_list visitor lbessard@70: * using the il_default_variable_init_value parameter... lbessard@70: * Note that the simple_instr_list_c visitor will set this parameter lbessard@70: * to NULL as soon as it does not require it any longer, lbessard@70: * so we don't do it here again after the lbessard@70: * symbol->simple_instr_list->accept(*this); lbessard@70: * returns... lbessard@70: */ lbessard@70: this->il_default_variable_init_value = symbol->il_operand; lbessard@70: lbessard@70: /* Now do the parenthesised instructions... */ lbessard@70: /* NOTE: the following code line will get the variable lbessard@70: * this->default_variable_name.current_type updated! lbessard@70: */ lbessard@70: symbol->simple_instr_list->accept(*this); lbessard@70: lbessard@70: /* Now do the operation, using the previous result! */ lbessard@70: /* NOTE: The result of the previous instruction list 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: */ lbessard@70: this->current_operand = &(this->default_variable_back_name); lbessard@70: this->current_operand_type = this->default_variable_back_name.current_type; lbessard@70: lbessard@70: this->default_variable_name.current_type = old_current_default_variable_data_type; lbessard@70: if (NULL == this->current_operand_type) ERROR; lbessard@70: lbessard@70: symbol->il_expr_operator->accept(*this); lbessard@70: lbessard@70: this->current_operand = NULL; lbessard@70: this->current_operand_type = NULL; lbessard@70: this->default_variable_back_name.current_type = NULL; lbessard@70: return NULL; lbessard@70: } 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) { lbessard@70: /* Pass the symbol->label to the il_jump_operation visitor lbessard@70: * using the jump_label parameter... lbessard@70: */ lbessard@70: this->jump_label = symbol->label; lbessard@70: symbol->il_jump_operator->accept(*this); lbessard@70: this->jump_label = NULL; lbessard@70: lbessard@70: return NULL; lbessard@70: } 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 */ lbessard@70: symbol_c *param_value = function_call_param_iterator.search(param_name); lbessard@70: lbessard@70: /* Get the value from a foo() style call */ lbessard@70: if (param_value == NULL) lbessard@70: param_value = function_call_param_iterator.next(); 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)) { lbessard@70: symbol->fb_name->accept(*this); lbessard@70: s4o.print("."); lbessard@70: param_name->accept(*this); lbessard@70: s4o.print(" = "); lbessard@123: if (search_base_type.type_is_subrange(param_type)) { lbessard@98: s4o.print("__CHECK_"); lbessard@98: param_type->accept(*this); lbessard@98: s4o.print("("); lbessard@98: } lbessard@70: param_value->accept(*this); lbessard@123: if (search_base_type.type_is_subrange(param_type)) lbessard@98: s4o.print(")"); 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("(&"); 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 */ lbessard@70: symbol_c *param_value = function_call_param_iterator.search(param_name); lbessard@70: lbessard@70: /* Get the value from a foo() style call */ lbessard@70: if (param_value == NULL) lbessard@70: param_value = function_call_param_iterator.next(); 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)) { lbessard@98: symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false); lbessard@98: lbessard@70: s4o.print(";\n"+ s4o.indent_spaces); lbessard@70: param_value->accept(*this); lbessard@70: s4o.print(" = "); lbessard@123: if (search_base_type.type_is_subrange(param_type)) { lbessard@98: s4o.print("__CHECK_"); lbessard@98: param_type->accept(*this); lbessard@98: s4o.print("("); lbessard@98: } lbessard@70: symbol->fb_name->accept(*this); lbessard@70: s4o.print("."); lbessard@70: param_name->accept(*this); lbessard@123: if (search_base_type.type_is_subrange(param_type)) lbessard@98: s4o.print(")"); 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@70: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); lbessard@70: lbessard@70: if (f_decl == function_symtable.end_value()) lbessard@70: /* should never occur. The function being called MUST be in the symtable... */ lbessard@70: ERROR; lbessard@70: lbessard@70: symbol->function_name->accept(*this); lbessard@70: s4o.print("("); lbessard@70: lbessard@70: /* loop through each function parameter, find the value we should pass lbessard@70: * to it, and then output the c equivalent... lbessard@70: */ lbessard@70: lbessard@70: function_param_iterator_c fp_iterator(f_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: if (i != 1) lbessard@70: s4o.print(", "); lbessard@70: lbessard@70: symbol_c *param_type = fp_iterator.param_type(); lbessard@70: if (param_type == NULL) ERROR; lbessard@70: lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); lbessard@70: lbessard@70: lbessard@70: symbol_c *param_value = NULL; lbessard@70: lbessard@70: /* Get the value from a foo( = ) style call */ lbessard@70: if (param_value == NULL) lbessard@70: param_value = function_call_param_iterator.search(param_name); lbessard@70: lbessard@70: /* Get the value from a foo() style call */ lbessard@70: /* NOTE: the following line of code is not required in this case, but it doesn't lbessard@70: * harm to leave it in, as in the case of a formal syntax function call, lbessard@70: * it will always return NULL. lbessard@70: * We leave it in in case we later decide to merge this part of the code together lbessard@70: * with the function calling code in generate_c_st_c, which does require lbessard@70: * the following line... lbessard@70: */ lbessard@70: if (param_value == NULL) lbessard@70: param_value = function_call_param_iterator.next(); lbessard@70: lbessard@70: switch (param_direction) { lbessard@70: case function_param_iterator_c::direction_in: lbessard@70: if (param_value == NULL) { lbessard@70: /* No value given for parameter, so we must use the default... */ lbessard@70: /* First check whether default value specified in function declaration...*/ lbessard@70: param_value = fp_iterator.default_value(); lbessard@70: } lbessard@70: if (param_value == NULL) { lbessard@70: /* If not, get the default value of this variable's type */ lbessard@70: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); lbessard@70: } lbessard@70: if (param_value == NULL) ERROR; lbessard@123: if (search_base_type.type_is_subrange(param_type)) { lbessard@98: s4o.print("__CHECK_"); lbessard@98: param_type->accept(*this); lbessard@98: s4o.print("("); lbessard@98: } lbessard@70: param_value->accept(*this); lbessard@123: if (search_base_type.type_is_subrange(param_type)) lbessard@98: s4o.print(")"); lbessard@70: break; lbessard@70: case function_param_iterator_c::direction_out: lbessard@70: case function_param_iterator_c::direction_inout: lbessard@70: if (param_value == NULL) { lbessard@70: /* no parameter value given, so we pass a previously declared temporary variable. */ lbessard@70: std::string *temp_var_name = temp_var_name_factory.new_name(); lbessard@70: s4o.print(*temp_var_name); lbessard@70: delete temp_var_name; lbessard@70: } else { lbessard@70: param_value->accept(*this); lbessard@70: } lbessard@70: break; lbessard@70: case function_param_iterator_c::direction_extref: lbessard@70: /* TODO! */ lbessard@70: ERROR; lbessard@70: break; lbessard@70: } /* switch */ lbessard@70: } /* for(...) */ lbessard@70: lbessard@70: // symbol->parameter_assignment->accept(*this); lbessard@70: s4o.print(")"); 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) { lbessard@70: /* A simple_instr_list_c is used to store a list of il operations lbessard@70: * 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: * lbessard@70: * This will be converted to C++ by defining a new scope lbessard@70: * with a new il default 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 lbessard@70: * in the il default 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: * lbessard@70: * The intial value of the il default variable (in the above lbessard@70: * example 'var2') is passed to this simple_instr_list_c visitor lbessard@70: * using the il_default_variable_init_value parameter. lbessard@70: * Since it is possible to have parenthesis inside other parenthesis lbessard@70: * recursively, we reset the il_default_variable_init_value to NULL lbessard@70: * as soon as we no longer require it, as it may be used once again lbessard@70: * in the line lbessard@70: * print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); lbessard@70: * lbessard@70: */ lbessard@70: lbessard@70: /* Declare the default variable, that will store the result of the IL operations... */ lbessard@70: s4o.print("{\n"); lbessard@70: s4o.indent_right(); lbessard@70: lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: s4o.print(IL_DEFVAR_T); lbessard@70: s4o.print(" "); lbessard@70: this->default_variable_name.accept(*this); lbessard@70: s4o.print(";\n\n"); lbessard@70: lbessard@70: /* Check whether we should initiliase the il default variable... */ lbessard@70: if (NULL != this->il_default_variable_init_value) { lbessard@70: /* Yes, we must... */ lbessard@70: /* We will do it by instatiating a LD operator, and having this lbessard@70: * same generate_c_il_c class visiting it! lbessard@70: */ lbessard@70: LD_operator_c ld_oper; lbessard@70: il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value); lbessard@70: lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: il_simple_oper.accept(*this); lbessard@70: s4o.print(";\n"); lbessard@70: } lbessard@70: lbessard@70: /* this parameter no longer required... */ lbessard@70: this->il_default_variable_init_value = NULL; lbessard@70: lbessard@70: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); lbessard@70: lbessard@70: /* copy the result in the default variable to the variable lbessard@70: * used to pass the data out to the scope enclosing lbessard@70: * the current scope! lbessard@70: * lbessard@70: * We also need to update the data type currently stored within lbessard@70: * the variable used to pass the data to the outside scope... lbessard@70: */ lbessard@70: this->default_variable_back_name.current_type = this->default_variable_name.current_type; lbessard@70: s4o.print("\n"); lbessard@70: s4o.print(s4o.indent_spaces); lbessard@70: this->default_variable_back_name.accept(*this); lbessard@70: s4o.print(" = "); lbessard@70: this->default_variable_name.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: lbessard@70: /* | il_initial_param_list il_param_instruction */ lbessard@70: // SYM_LIST(il_param_list_c) lbessard@70: 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) lbessard@70: 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); lbessard@70: void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called! lbessard@70: lbessard@70: /*******************/ lbessard@70: /* B 2.2 Operators */ lbessard@70: /*******************/ lbessard@70: lbessard@70: void *visit(LD_operator_c *symbol) { lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: XXX_operator(&(this->default_variable_name), " = ", this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(LDN_operator_c *symbol) { lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: XXX_operator(&(this->default_variable_name), lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~", lbessard@70: this->current_operand); lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(ST_operator_c *symbol) { lbessard@98: symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false); lbessard@123: lbessard@98: this->current_operand->accept(*this); lbessard@98: s4o.print(" = "); lbessard@123: if (search_base_type.type_is_subrange(operand_type)) { lbessard@98: s4o.print("__CHECK_"); lbessard@98: operand_type->accept(*this); lbessard@98: s4o.print("("); lbessard@98: } lbessard@98: this->default_variable_name.accept(*this); lbessard@123: if (search_base_type.type_is_subrange(operand_type)) lbessard@98: s4o.print(")"); lbessard@70: /* the data type resulting from this operation is unchamged. */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(STN_operator_c *symbol) { lbessard@98: symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false); lbessard@123: lbessard@98: this->current_operand->accept(*this); lbessard@98: s4o.print(" = "); lbessard@123: if (search_base_type.type_is_subrange(operand_type)) { lbessard@98: s4o.print("__CHECK_"); lbessard@98: operand_type->accept(*this); lbessard@98: s4o.print("("); lbessard@98: } lbessard@98: if (search_expression_type->is_bool_type(this->current_operand_type)) lbessard@98: s4o.print("!"); lbessard@98: else lbessard@98: s4o.print("~"); lbessard@98: this->default_variable_name.accept(*this); lbessard@123: if (search_base_type.type_is_subrange(operand_type)) lbessard@98: s4o.print(")"); lbessard@70: /* the data type resulting from this operation is unchamged. */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(NOT_operator_c *symbol) { lbessard@70: if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR; lbessard@70: XXX_operator(&(this->default_variable_name), lbessard@70: search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~", lbessard@70: &(this->default_variable_name)); lbessard@70: /* the data type resulting from this operation is unchanged. */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(S_operator_c *symbol) { lbessard@70: if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; lbessard@70: lbessard@70: C_modifier(); lbessard@70: this->current_operand->accept(*this); lbessard@70: s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = true":" = 1"); lbessard@70: /* the data type resulting from this operation is unchanged! */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(R_operator_c *symbol) { lbessard@70: if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; lbessard@70: lbessard@70: C_modifier(); lbessard@70: this->current_operand->accept(*this); lbessard@70: s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = false":" = 0"); lbessard@70: /* the data type resulting from this operation is unchanged! */ lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(S1_operator_c *symbol) {return XXX_CAL_operator("S1", this->current_operand);} lbessard@70: 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);} lbessard@70: void *visit(CU_operator_c *symbol) {return XXX_CAL_operator("CU", this->current_operand);} lbessard@70: void *visit(CD_operator_c *symbol) {return XXX_CAL_operator("CD", this->current_operand);} lbessard@70: void *visit(PV_operator_c *symbol) {return XXX_CAL_operator("PV", this->current_operand);} lbessard@70: void *visit(IN_operator_c *symbol) {return XXX_CAL_operator("IN", this->current_operand);} lbessard@70: void *visit(PT_operator_c *symbol) {return XXX_CAL_operator("PT", this->current_operand);} lbessard@70: lbessard@70: void *visit(AND_operator_c *symbol) { lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " &= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(OR_operator_c *symbol) { lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " |= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(XOR_operator_c *symbol) { lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types! lbessard@70: XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(ANDN_operator_c *symbol) { lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~", lbessard@70: this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(ORN_operator_c *symbol) { lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~", lbessard@70: this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(XORN_operator_c *symbol) { lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), lbessard@70: // bit by bit exclusive OR !! Also seems to work with boolean types! lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~", lbessard@70: this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(ADD_operator_c *symbol) { lbessard@70: if (search_expression_type->is_time_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_time_type(this->current_operand_type)) { lbessard@70: XXX_function("__time_add", &(this->default_variable_name), this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: return NULL; lbessard@70: } lbessard@70: if (search_expression_type->is_num_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " += ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: return NULL; lbessard@70: } lbessard@70: ERROR; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(SUB_operator_c *symbol) { lbessard@70: if (search_expression_type->is_time_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_time_type(this->current_operand_type)) { lbessard@70: XXX_function("__time_sub", &(this->default_variable_name), this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: return NULL; lbessard@70: } lbessard@70: if (search_expression_type->is_num_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " -= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: return NULL; lbessard@70: } lbessard@70: ERROR; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(MUL_operator_c *symbol) { lbessard@70: if (search_expression_type->is_time_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_integer_type(this->current_operand_type)) { lbessard@70: XXX_function("__time_mul", &(this->default_variable_name), this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: return NULL; lbessard@70: } lbessard@70: if (search_expression_type->is_num_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " *= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: return NULL; lbessard@70: } lbessard@70: ERROR; lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(DIV_operator_c *symbol) { lbessard@70: if (search_expression_type->is_num_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " /= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(MOD_operator_c *symbol) { lbessard@70: if (search_expression_type->is_num_type(this->default_variable_name.current_type) && lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) { lbessard@70: XXX_operator(&(this->default_variable_name), " %= ", this->current_operand); lbessard@70: /* the data type resulting from this operation... */ lbessard@70: this->default_variable_name.current_type = this->current_operand_type; lbessard@70: } lbessard@70: else {ERROR;} lbessard@70: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(GT_operator_c *symbol) { lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && lbessard@123: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) lbessard@123: return CMP_operator(this->current_operand, "__gt_"); lbessard@123: ERROR; lbessard@123: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(GE_operator_c *symbol) { lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && lbessard@123: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) lbessard@123: return CMP_operator(this->current_operand, "__ge_"); lbessard@123: ERROR; lbessard@123: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(EQ_operator_c *symbol) { lbessard@123: if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) lbessard@123: return CMP_operator(this->current_operand, "__eq_"); lbessard@123: ERROR; lbessard@123: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(LT_operator_c *symbol) { lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && lbessard@123: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) lbessard@123: return CMP_operator(this->current_operand, "__lt_"); lbessard@123: ERROR; lbessard@123: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(LE_operator_c *symbol) { lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) && lbessard@123: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) lbessard@123: return CMP_operator(this->current_operand, "__le_"); lbessard@123: ERROR; lbessard@123: return NULL; lbessard@70: } lbessard@70: lbessard@70: void *visit(NE_operator_c *symbol) { lbessard@123: if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) lbessard@123: return CMP_operator(this->current_operand, "__ne_"); lbessard@123: ERROR; lbessard@123: return NULL; lbessard@70: } 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: lbessard@70: s4o.print("goto "); lbessard@70: this->jump_label->accept(*this); lbessard@70: /* the data type resulting from this operation is unchanged! */ 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: lbessard@70: C_modifier(); lbessard@70: s4o.print("goto "); lbessard@70: this->jump_label->accept(*this); lbessard@70: /* the data type resulting from this operation is unchanged! */ 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: lbessard@70: CN_modifier(); lbessard@70: s4o.print("goto "); lbessard@70: this->jump_label->accept(*this); lbessard@70: /* the data type resulting from this operation is unchanged! */ 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: lbessard@70: this->current_type = current_type; lbessard@70: }