etisserant@0: /* etisserant@0: * (c) 2003 Mario de Sousa etisserant@0: * etisserant@0: * Offered to the public under the terms of the GNU General Public License etisserant@0: * as published by the Free Software Foundation; either version 2 of the etisserant@0: * License, or (at your option) any later version. etisserant@0: * etisserant@0: * This program is distributed in the hope that it will be useful, but etisserant@0: * WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General etisserant@0: * Public License for more details. etisserant@0: * etisserant@0: * This code is made available on the understanding that it will not be etisserant@0: * used in safety-critical situations without a full and competent review. etisserant@0: */ etisserant@0: etisserant@0: /* etisserant@0: * An IEC 61131-3 IL and ST compiler. etisserant@0: * etisserant@0: * Based on the etisserant@0: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: * Conversion of il statements (i.e. IL code). etisserant@0: * etisserant@0: * This is part of the 4th stage that generates etisserant@0: * a c++ source program equivalent to the IL and ST etisserant@0: * code. etisserant@0: */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: etisserant@0: /* Returns the data type of an il_operand. etisserant@0: * etisserant@0: * Note that the il_operand may be a variable, in which case etisserant@0: * we return the type of the variable instance. etisserant@0: * The il_operand may also be a constant, in which case etisserant@0: * we return the data type of that constant. etisserant@0: * etisserant@0: * The variable instance may be a member of a structured variable, etisserant@0: * or an element in an array, or any combination of the two. etisserant@0: * etisserant@0: * The class constructor must be given the search scope etisserant@0: * (function, function block or program within which etisserant@0: * the possible il_operand variable instance was declared). etisserant@0: */ etisserant@0: class search_il_operand_type_c { etisserant@0: etisserant@0: private: etisserant@0: search_varfb_instance_type_c search_varfb_instance_type; etisserant@0: search_constant_type_c search_constant_type; etisserant@0: etisserant@0: public: etisserant@0: search_il_operand_type_c(symbol_c *search_scope): search_varfb_instance_type(search_scope) {} etisserant@0: etisserant@0: public: etisserant@0: symbol_c *get_type(symbol_c *il_operand) { etisserant@0: symbol_c *res; etisserant@0: etisserant@0: /* We first assume that it is a constant... */ etisserant@0: res = search_constant_type.get_type(il_operand); etisserant@0: if (res != NULL) return res; etisserant@0: etisserant@0: /* Nope, now we assume it is a variable, and determine its type... */ etisserant@0: res = search_varfb_instance_type.get_type(il_operand); etisserant@0: if (NULL != res) return res; etisserant@0: etisserant@0: /* not found */ etisserant@0: return NULL; etisserant@0: } etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: etisserant@0: etisserant@0: /* A new class to ouput the il default variable to c++ code etisserant@0: * We use this class, inheriting from symbol_c, so it may be used etisserant@0: * as any other symbol_c object in the intermediate parse tree, etisserant@0: * more specifically, so it can be used as any other il operand. etisserant@0: * This makes the rest of the code much easier... etisserant@0: * etisserant@0: * Nevertheless, the basic visitor class visitor_c does not know etisserant@0: * how to visit this new il_default_variable_c class, so we have etisserant@0: * to extend that too. etisserant@0: * In reality extending the basic symbols doesn't quite work out etisserant@0: * as cleanly as desired (we need to use dynamic_cast in the etisserant@0: * accept method of the il_default_variable_c), but it is cleaner etisserant@0: * than the alternative... etisserant@0: */ etisserant@0: class il_default_variable_c; etisserant@0: etisserant@0: /* This visitor class is not really required, we could place the etisserant@0: * visit() method directly in genertae_cc_il_c, but doing it in etisserant@0: * a seperate class makes the architecture more evident... etisserant@0: */ etisserant@0: class il_default_variable_visitor_c { etisserant@0: public: etisserant@0: virtual void *visit(il_default_variable_c *symbol) = 0; etisserant@0: etisserant@0: virtual ~il_default_variable_visitor_c(void) {return;} etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: /* A class to print out to the resulting C++ code etisserant@0: * the IL default variable name. etisserant@0: * etisserant@0: * It includes a reference to its name, etisserant@0: * and the data type of the data currently stored etisserant@0: * in this C++ variable... This is required because the etisserant@0: * C++ variable is a union, and we must know which member etisserant@0: * of the union top reference!! etisserant@0: * etisserant@0: * Note that we also need to keep track of the data type of etisserant@0: * the value currently being stored in the default variable. etisserant@0: * This is required so we can process parenthesis, etisserant@0: * etisserant@0: * e.g. : etisserant@0: * LD var1 etisserant@0: * AND ( etisserant@0: * LD var2 etisserant@0: * OR var3 etisserant@0: * ) etisserant@0: * etisserant@0: * Note that we only execute the 'AND (' operation when we come across etisserant@0: * the ')', i.e. once we have evaluated the result of the etisserant@0: * instructions inside the parenthesis. etisserant@0: * When we do execute the 'AND (' operation, we need to know the data type etisserant@0: * of the operand, which in this case is the result of the evaluation of the etisserant@0: * instruction list inside the parenthesis. We can only know this if we etisserant@0: * keep track of the data type currently stored in the default variable! etisserant@0: * etisserant@0: * We use the current_type inside the generate_cc_il::default_variable_name variable etisserant@0: * to track this! etisserant@0: */ etisserant@0: class il_default_variable_c: public symbol_c { etisserant@0: public: etisserant@0: symbol_c *var_name; /* in principle, this should point to an indentifier_c */ etisserant@0: symbol_c *current_type; etisserant@0: etisserant@0: public: etisserant@0: il_default_variable_c(const char *var_name_str, symbol_c *current_type); etisserant@0: virtual void *accept(visitor_c &visitor); etisserant@0: }; etisserant@0: etisserant@0: etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: /***********************************************************************/ etisserant@0: etisserant@0: etisserant@0: etisserant@0: class generate_cc_il_c: public generate_cc_typedecl_c, il_default_variable_visitor_c { etisserant@0: etisserant@0: private: etisserant@0: /* When compiling il code, it becomes necessary to determine the etisserant@0: * data type of il operands. To do this, we must first find the etisserant@0: * il operand's declaration, within the scope of the function block etisserant@0: * or function currently being processed. etisserant@0: * The following object does just that... etisserant@0: * This object instance will then later be called while the etisserant@0: * remaining il code is being handled. etisserant@0: */ etisserant@0: search_il_operand_type_c *search_il_operand_type; etisserant@0: etisserant@0: /* The initial value that should be given to the IL default variable etisserant@0: * imediately after a parenthesis is opened. etisserant@0: * This variable is only used to pass data from the etisserant@0: * il_expression_c visitor to the simple_instr_list_c visitor. etisserant@0: * etisserant@0: * e.g.: etisserant@0: * LD var1 etisserant@0: * AND ( var2 etisserant@0: * OR var3 etisserant@0: * ) etisserant@0: * etisserant@0: * In the above code sample, the line 'AND ( var2' constitutes etisserant@0: * an il_expression_c, where var2 should be loaded into the etisserant@0: * il default variable before continuing with the expression etisserant@0: * inside the parenthesis. etisserant@0: * Unfortunately, only the simple_instr_list_c may do the etisserant@0: * initial laoding of the var2 bariable following the parenthesis, etisserant@0: * so the il_expression_c visitor will have to pass 'var2' as a etisserant@0: * parameter to the simple_instr_list_c visitor. etisserant@0: * Ergo, the existance of the following parameter...! etisserant@0: */ etisserant@0: symbol_c *il_default_variable_init_value; etisserant@0: etisserant@0: /* Operand to the IL operation currently being processed... */ etisserant@0: /* These variables are used to pass data from the etisserant@0: * il_simple_operation_c and il_expression_c visitors etisserant@0: * to the il operator visitors (i.e. LD_operator_c, etisserant@0: * LDN_operator_c, ST_operator_c, STN_operator_c, ...) etisserant@0: */ etisserant@0: symbol_c *current_operand; etisserant@0: symbol_c *current_operand_type; etisserant@0: etisserant@0: /* Label to which the current IL jump operation should jump to... */ etisserant@0: /* This variable is used to pass data from the etisserant@0: * il_jump_operation_c visitor etisserant@0: * to the il jump operator visitors (i.e. JMP_operator_c, etisserant@0: * JMPC_operator_c, JMPCN_operator_c, ...) etisserant@0: */ etisserant@0: symbol_c *jump_label; etisserant@0: etisserant@0: /* The result of the comparison IL operations (GT, EQ, LT, ...) etisserant@0: * is a boolean variable. etisserant@0: * This class keeps track of the current data type stored in the etisserant@0: * il default variable. This is usually done by keeping a reference etisserant@0: * to the data type of the last operand. Nevertheless, in the case of etisserant@0: * the comparison IL operators, the data type of the result (a boolean) etisserant@0: * is not the data type of the operand. We therefore need an object etisserant@0: * of the boolean data type to keep as a reference of the current etisserant@0: * data type. etisserant@0: * The following object is it... etisserant@0: */ etisserant@0: bool_type_name_c bool_type; etisserant@0: etisserant@0: /* the data type of the IL default variable... */ etisserant@0: #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T" etisserant@0: /* The name of the IL default variable... */ etisserant@0: #define IL_DEFVAR VAR_LEADER "IL_DEFVAR" etisserant@0: /* The name of the variable used to pass the result of a etisserant@0: * parenthesised instruction list to the immediately preceding etisserant@0: * scope ... etisserant@0: */ etisserant@0: #define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK" etisserant@0: il_default_variable_c default_variable_name; etisserant@0: il_default_variable_c default_variable_back_name; etisserant@0: etisserant@0: /* Some function calls in the body of functions or function blocks etisserant@0: * may leave some parameters to their default values, and etisserant@0: * ignore some output parameters of the function being called. etisserant@0: * Our conversion of ST functions to C++ does not contemplate that, etisserant@0: * i.e. each called function must get all it's input and output etisserant@0: * parameters set correctly. etisserant@0: * For input parameters we merely need to call the function with etisserant@0: * the apropriate default value, but for output parameters etisserant@0: * we must create temporary variables to hold the output value. etisserant@0: * etisserant@0: * We declare all the temporary output variables at the begining of etisserant@0: * the body of each function or function block, and use them as etisserant@0: * in function calls later on as they become necessary... etisserant@0: * Note that we cannot create these variables just before a function etisserant@0: * call, as the function call itself may be integrated within an etisserant@0: * expression, or another function call! etisserant@0: * etisserant@0: * The variables are declared in the exact same order in which they etisserant@0: * will be used later on during the function calls, which allows us etisserant@0: * to simply re-create the name that was used for the temporary variable etisserant@0: * instead of keeping it in some list. etisserant@0: * The names are recreated by the temp_var_name_factory, after reset() etisserant@0: * has been called! etisserant@0: * etisserant@0: * This function will genertae code similar to... etisserant@0: * etisserant@0: * INT __TMP_0 = 23; etisserant@0: * REAL __TMP_1 = 45.5; etisserant@0: * ... etisserant@0: */ etisserant@0: temp_var_name_c temp_var_name_factory; etisserant@0: etisserant@0: /* When calling a function block, we must first find it's type, etisserant@0: * by searching through the declarations of the variables currently etisserant@0: * in scope. etisserant@0: * This class does just that... etisserant@0: * A new class is instantiated whenever we begin generating the code etisserant@0: * for a function block type declaration, or a program declaration. etisserant@0: * This object instance will then later be called while the etisserant@0: * function block's or the program's body is being handled. etisserant@0: * etisserant@0: * Note that functions cannot contain calls to function blocks, etisserant@0: * so we do not create an object instance when handling etisserant@0: * a function declaration. etisserant@0: */ etisserant@0: search_fb_instance_decl_c *search_fb_instance_decl; etisserant@0: etisserant@0: etisserant@0: public: etisserant@0: generate_cc_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL) etisserant@0: : generate_cc_typedecl_c(s4o_ptr), etisserant@0: default_variable_name(IL_DEFVAR, NULL), etisserant@0: default_variable_back_name(IL_DEFVAR_BACK, NULL) etisserant@0: { etisserant@0: search_il_operand_type = new search_il_operand_type_c(scope); etisserant@0: search_fb_instance_decl = new search_fb_instance_decl_c(scope); etisserant@0: current_operand = NULL; etisserant@0: current_operand_type = NULL; etisserant@0: il_default_variable_init_value = NULL; etisserant@0: this->set_variable_prefix(variable_prefix); etisserant@0: } etisserant@0: etisserant@0: virtual ~generate_cc_il_c(void) { etisserant@0: delete search_fb_instance_decl; etisserant@0: delete search_il_operand_type; etisserant@0: } etisserant@0: etisserant@0: void generate(instruction_list_c *il) { etisserant@0: generate_cc_tempvardecl_c generate_cc_tempvardecl(&s4o); etisserant@0: generate_cc_tempvardecl.generate(il, &temp_var_name_factory); etisserant@0: il->accept(*this); etisserant@0: } etisserant@0: etisserant@0: private: etisserant@0: /* A helper function... */ etisserant@0: bool is_bool_type(symbol_c *type_symbol) { etisserant@0: return (NULL != dynamic_cast(type_symbol)); etisserant@0: } etisserant@0: etisserant@0: /* A helper function... */ etisserant@0: void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) { etisserant@0: if ((NULL == lo) || (NULL == ro)) ERROR; etisserant@0: if (NULL == op) ERROR; etisserant@0: etisserant@0: lo->accept(*this); etisserant@0: s4o.print(op); etisserant@0: ro->accept(*this); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* A helper function... */ etisserant@0: void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) { etisserant@0: if (NULL == fb_name) ERROR; etisserant@0: symbolic_variable_c *sv = dynamic_cast(fb_name); etisserant@0: if (NULL == sv) ERROR; etisserant@0: identifier_c *id = dynamic_cast(sv->var_name); etisserant@0: if (NULL == id) ERROR; etisserant@0: etisserant@0: identifier_c param(param_name); etisserant@0: etisserant@0: //SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) etisserant@0: il_param_assignment_c il_param_assignment(¶m, &this->default_variable_name, NULL, NULL); etisserant@0: // SYM_LIST(il_param_list_c) etisserant@0: il_param_list_c il_param_list; etisserant@0: il_param_list.add_element(&il_param_assignment); etisserant@0: CAL_operator_c CAL_operator; etisserant@0: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) etisserant@0: il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list); etisserant@0: etisserant@0: il_fb_call.accept(*this); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* A helper function... */ etisserant@0: void *CMP_operator(symbol_c *o, const char *operation) { etisserant@0: if (NULL == o) ERROR; etisserant@0: if (NULL == this->default_variable_name.current_type) ERROR; etisserant@0: etisserant@0: symbol_c *backup = this->default_variable_name.current_type; etisserant@0: this->default_variable_name.current_type = &(this->bool_type); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: this->default_variable_name.current_type = backup; etisserant@0: etisserant@0: s4o.print(" = ("); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(operation); etisserant@0: o->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = &(this->bool_type); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* A helper function... */ etisserant@0: void C_modifier(void) { etisserant@0: if (NULL == this->default_variable_name.current_type) ERROR; etisserant@0: etisserant@0: s4o.print("if ("); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(") "); etisserant@0: } etisserant@0: etisserant@0: /* A helper function... */ etisserant@0: void CN_modifier(void) { etisserant@0: if (NULL == this->default_variable_name.current_type) ERROR; etisserant@0: etisserant@0: s4o.print("if (!"); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(") "); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: public: etisserant@0: void *visit(il_default_variable_c *symbol) { etisserant@0: //s4o.print("il_default_variable_c VISITOR!!\n"); etisserant@0: symbol->var_name->accept(*this); etisserant@0: if (NULL != symbol->current_type) { etisserant@0: s4o.print("."); etisserant@0: symbol->current_type->accept(*this); etisserant@0: s4o.print("var"); etisserant@0: } etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: private: etisserant@0: /****************************************/ etisserant@0: /* B.2 - Language IL (Instruction List) */ etisserant@0: /****************************************/ etisserant@0: etisserant@0: /***********************************/ etisserant@0: /* B 2.1 Instructions and Operands */ etisserant@0: /***********************************/ etisserant@0: etisserant@0: /* please see the comment before the RET_operator_c visitor for details... */ etisserant@0: #define END_LABEL VAR_LEADER "end" etisserant@0: etisserant@0: /*| instruction_list il_instruction */ etisserant@0: void *visit(instruction_list_c *symbol) { etisserant@0: /* Declare the backup to the default variable, that will store the result etisserant@0: * of the IL operations executed inside a parenthesis... etisserant@0: */ etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print(IL_DEFVAR_T); etisserant@0: s4o.print(" "); etisserant@0: this->default_variable_back_name.accept(*this); etisserant@0: s4o.print(";\n"); etisserant@0: etisserant@0: /* Declare the default variable, that will store the result of the IL operations... */ etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print(IL_DEFVAR_T); etisserant@0: s4o.print(" "); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(";\n\n"); etisserant@0: etisserant@0: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); etisserant@0: etisserant@0: /* write the label marking the end of the code block */ etisserant@0: /* please see the comment before the RET_operator_c visitor for details... */ etisserant@0: s4o.print("\n"); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print(END_LABEL); etisserant@0: s4o.print(":\n"); etisserant@0: s4o.indent_right(); etisserant@0: /* since every label must be followed by at least one statement, and etisserant@0: * only the functions will introduce the return statement after this label, etisserant@0: * function blocks written in IL would result in invalid C++ code. etisserant@0: * To work around this we introduce the equivalent of a 'nop' operation etisserant@0: * to humour the compiler... etisserant@0: */ etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print("/* to humour the compiler, we insert a nop */\n"); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(";\n"); etisserant@0: s4o.indent_left(); etisserant@0: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* | label ':' [il_incomplete_instruction] eol_list */ etisserant@0: // SYM_REF2(il_instruction_c, label, il_instruction) etisserant@0: void *visit(il_instruction_c *symbol) { etisserant@0: if (NULL != symbol->label) { etisserant@0: symbol->label->accept(*this); etisserant@0: s4o.print(":\n"); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: } etisserant@0: symbol->il_instruction->accept(*this); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* | il_simple_operator [il_operand] */ etisserant@0: //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand) etisserant@0: void *visit(il_simple_operation_c *symbol) { etisserant@0: this->current_operand = symbol->il_operand; etisserant@0: if (NULL == this->current_operand) { etisserant@0: this->current_operand_type = NULL; etisserant@0: } else { etisserant@0: this->current_operand_type = search_il_operand_type->get_type(this->current_operand); etisserant@0: if (NULL == this->current_operand_type) ERROR; etisserant@0: } etisserant@0: etisserant@0: symbol->il_simple_operator->accept(*this); etisserant@0: etisserant@0: this->current_operand = NULL; etisserant@0: this->current_operand_type = NULL; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* | function_name [il_operand_list] */ etisserant@0: // SYM_REF2(il_function_call_c, function_name, il_operand_list) etisserant@0: void *visit(il_function_call_c *symbol) { etisserant@0: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); etisserant@0: etisserant@0: if (f_decl == function_symtable.end_value()) etisserant@0: /* should never occur. The function being called MUST be in the symtable... */ etisserant@0: ERROR; etisserant@0: etisserant@0: /* determine the base data type returned by the function being called... */ etisserant@0: search_base_type_c search_base_type; etisserant@0: symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type); etisserant@0: symbol_c *param_data_type = default_variable_name.current_type; etisserant@0: if (NULL == return_data_type) ERROR; etisserant@0: etisserant@0: default_variable_name.current_type = return_data_type; etisserant@0: this->default_variable_name.accept(*this); etisserant@0: default_variable_name.current_type = param_data_type; etisserant@0: s4o.print(" = "); etisserant@0: etisserant@0: symbol->function_name->accept(*this); etisserant@0: s4o.print("("); etisserant@0: etisserant@0: /* loop through each function parameter, find the value we should pass etisserant@0: * to it, and then output the c equivalent... etisserant@0: */ etisserant@0: etisserant@0: function_param_iterator_c fp_iterator(f_decl); etisserant@0: identifier_c *param_name; etisserant@0: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: if (i != 1) etisserant@0: s4o.print(", "); etisserant@0: etisserant@0: symbol_c *param_type = fp_iterator.param_type(); etisserant@0: if (param_type == NULL) ERROR; etisserant@0: etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: etisserant@0: symbol_c *param_value = NULL; etisserant@0: etisserant@0: /* if it is the first parameter, semantics specifies that we should etisserant@0: * get the value off the IL default variable! etisserant@0: */ etisserant@0: if (1 == i) etisserant@0: param_value = &this->default_variable_name; etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: /* NOTE: the following line of code is not required in this case, but it doesn't etisserant@0: * harm to leave it in, as in the case of a non-formal syntax function call, etisserant@0: * it will always return NULL. etisserant@0: * We leave it in in case we later decide to merge this part of the code together etisserant@0: * with the function calling code in generate_cc_st_c, which does require etisserant@0: * the following line... etisserant@0: */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: switch (param_direction) { etisserant@0: case function_param_iterator_c::direction_in: etisserant@0: if (param_value == NULL) { etisserant@0: /* No value given for parameter, so we must use the default... */ etisserant@0: /* First check whether default value specified in function declaration...*/ etisserant@0: param_value = fp_iterator.default_value(); etisserant@0: } etisserant@0: if (param_value == NULL) { etisserant@0: /* If not, get the default value of this variable's type */ etisserant@0: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); etisserant@0: } etisserant@0: if (param_value == NULL) ERROR; etisserant@0: param_value->accept(*this); etisserant@0: break; etisserant@0: case function_param_iterator_c::direction_out: etisserant@0: case function_param_iterator_c::direction_inout: etisserant@0: if (param_value == NULL) { etisserant@0: /* no parameter value given, so we pass a previously declared temporary variable. */ etisserant@0: std::string *temp_var_name = temp_var_name_factory.new_name(); etisserant@0: s4o.print(*temp_var_name); etisserant@0: delete temp_var_name; etisserant@0: } else { etisserant@0: param_value->accept(*this); etisserant@0: } etisserant@0: break; etisserant@0: case function_param_iterator_c::direction_extref: etisserant@0: /* TODO! */ etisserant@0: ERROR; etisserant@0: break; etisserant@0: } /* switch */ etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: s4o.print(")"); etisserant@0: etisserant@0: /* the data type returned by the function, and stored in the il default variable... */ etisserant@0: default_variable_name.current_type = return_data_type; etisserant@0: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */ etisserant@0: //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused) etisserant@0: void *visit(il_expression_c *symbol) { etisserant@0: /* We will be recursevely interpreting an instruction list, etisserant@0: * so we store a backup of the data type of the value currently stored etisserant@0: * in the default variable, and set the current data type to NULL etisserant@0: */ etisserant@0: symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type; etisserant@0: this->default_variable_name.current_type = NULL; etisserant@0: etisserant@0: /* Pass the symbol->il_operand to the simple_instr_list visitor etisserant@0: * using the il_default_variable_init_value parameter... etisserant@0: * Note that the simple_instr_list_c visitor will set this parameter etisserant@0: * to NULL as soon as it does not require it any longer, etisserant@0: * so we don't do it here again after the etisserant@0: * symbol->simple_instr_list->accept(*this); etisserant@0: * returns... etisserant@0: */ etisserant@0: this->il_default_variable_init_value = symbol->il_operand; etisserant@0: etisserant@0: /* Now do the parenthesised instructions... */ etisserant@0: /* NOTE: the following code line will get the variable etisserant@0: * this->default_variable_name.current_type updated! etisserant@0: */ etisserant@0: symbol->simple_instr_list->accept(*this); etisserant@0: etisserant@0: /* Now do the operation, using the previous result! */ etisserant@0: /* NOTE: The result of the previous instruction list will be stored etisserant@0: * in a variable named IL_DEFVAR_BACK. This is done in the visitor etisserant@0: * to instruction_list_c objects... etisserant@0: */ etisserant@0: this->current_operand = &(this->default_variable_back_name); etisserant@0: this->current_operand_type = this->default_variable_back_name.current_type; etisserant@0: etisserant@0: this->default_variable_name.current_type = old_current_default_variable_data_type; etisserant@0: if (NULL == this->current_operand_type) ERROR; etisserant@0: etisserant@0: symbol->il_expr_operator->accept(*this); etisserant@0: etisserant@0: this->current_operand = NULL; etisserant@0: this->current_operand_type = NULL; etisserant@0: this->default_variable_back_name.current_type = NULL; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* il_jump_operator label */ etisserant@0: // SYM_REF2(il_jump_operation_c, il_jump_operator, label) etisserant@0: void *visit(il_jump_operation_c *symbol) { etisserant@0: /* Pass the symbol->label to the il_jump_operation visitor etisserant@0: * using the jump_label parameter... etisserant@0: */ etisserant@0: this->jump_label = symbol->label; etisserant@0: symbol->il_jump_operator->accept(*this); etisserant@0: this->jump_label = NULL; etisserant@0: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* il_call_operator prev_declared_fb_name etisserant@0: * | il_call_operator prev_declared_fb_name '(' ')' etisserant@0: * | il_call_operator prev_declared_fb_name '(' eol_list ')' etisserant@0: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')' etisserant@0: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')' etisserant@0: */ etisserant@0: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list) etisserant@0: void *visit(il_fb_call_c *symbol) { etisserant@0: symbol->il_call_operator->accept(*this); etisserant@0: s4o.print("{\n"); etisserant@0: s4o.indent_right(); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: etisserant@0: /* first figure out what is the name of the function block type of the function block being called... */ etisserant@0: symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name); etisserant@0: /* should never occur. The function block instance MUST have been declared... */ etisserant@0: if (function_block_type_name == NULL) ERROR; etisserant@0: etisserant@0: /* Now find the declaration of the function block type being called... */ etisserant@0: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name); etisserant@0: /* should never occur. The function block type being called MUST be in the symtable... */ etisserant@0: if (fb_decl == function_block_type_symtable.end_value()) ERROR; etisserant@0: etisserant@0: /* loop through each function block parameter, find the value we should pass etisserant@0: * to it, and then output the c equivalent... etisserant@0: */ etisserant@0: function_param_iterator_c fp_iterator(fb_decl); etisserant@0: identifier_c *param_name; etisserant@0: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: symbol_c *param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: /* now output the value assignment */ etisserant@0: if (param_value != NULL) etisserant@0: if ((param_direction == function_param_iterator_c::direction_in) || etisserant@0: (param_direction == function_param_iterator_c::direction_inout)) { etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print("."); etisserant@0: param_name->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(";\n" + s4o.indent_spaces); etisserant@0: } etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: /* now call the function... */ etisserant@0: function_block_type_name->accept(*this); etisserant@0: s4o.print(FB_FUNCTION_SUFFIX); etisserant@0: s4o.print("(&"); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: etisserant@0: /* loop through each function parameter, find the variable to which etisserant@0: * we should atribute the value of all output or inoutput parameters. etisserant@0: */ etisserant@0: fp_iterator.reset(); etisserant@0: function_call_param_iterator.reset(); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: symbol_c *param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: /* now output the value assignment */ etisserant@0: if (param_value != NULL) etisserant@0: if ((param_direction == function_param_iterator_c::direction_out) || etisserant@0: (param_direction == function_param_iterator_c::direction_inout)) { etisserant@0: s4o.print(";\n"+ s4o.indent_spaces); etisserant@0: param_value->accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: symbol->fb_name->accept(*this); etisserant@0: s4o.print("."); etisserant@0: param_name->accept(*this); etisserant@0: } etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: s4o.print(";\n"); etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print("}"); etisserant@0: etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* | function_name '(' eol_list [il_param_list] ')' */ etisserant@0: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list) etisserant@0: void *visit(il_formal_funct_call_c *symbol) { etisserant@0: function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name); etisserant@0: etisserant@0: if (f_decl == function_symtable.end_value()) etisserant@0: /* should never occur. The function being called MUST be in the symtable... */ etisserant@0: ERROR; etisserant@0: etisserant@0: symbol->function_name->accept(*this); etisserant@0: s4o.print("("); etisserant@0: etisserant@0: /* loop through each function parameter, find the value we should pass etisserant@0: * to it, and then output the c equivalent... etisserant@0: */ etisserant@0: etisserant@0: function_param_iterator_c fp_iterator(f_decl); etisserant@0: identifier_c *param_name; etisserant@0: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@0: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) { etisserant@0: if (i != 1) etisserant@0: s4o.print(", "); etisserant@0: etisserant@0: symbol_c *param_type = fp_iterator.param_type(); etisserant@0: if (param_type == NULL) ERROR; etisserant@0: etisserant@0: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction(); etisserant@0: etisserant@0: etisserant@0: symbol_c *param_value = NULL; etisserant@0: etisserant@0: /* Get the value from a foo( = ) style call */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.search(param_name); etisserant@0: etisserant@0: /* Get the value from a foo() style call */ etisserant@0: /* NOTE: the following line of code is not required in this case, but it doesn't etisserant@0: * harm to leave it in, as in the case of a formal syntax function call, etisserant@0: * it will always return NULL. etisserant@0: * We leave it in in case we later decide to merge this part of the code together etisserant@0: * with the function calling code in generate_cc_st_c, which does require etisserant@0: * the following line... etisserant@0: */ etisserant@0: if (param_value == NULL) etisserant@0: param_value = function_call_param_iterator.next(); etisserant@0: etisserant@0: switch (param_direction) { etisserant@0: case function_param_iterator_c::direction_in: etisserant@0: if (param_value == NULL) { etisserant@0: /* No value given for parameter, so we must use the default... */ etisserant@0: /* First check whether default value specified in function declaration...*/ etisserant@0: param_value = fp_iterator.default_value(); etisserant@0: } etisserant@0: if (param_value == NULL) { etisserant@0: /* If not, get the default value of this variable's type */ etisserant@0: param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance()); etisserant@0: } etisserant@0: if (param_value == NULL) ERROR; etisserant@0: param_value->accept(*this); etisserant@0: break; etisserant@0: case function_param_iterator_c::direction_out: etisserant@0: case function_param_iterator_c::direction_inout: etisserant@0: if (param_value == NULL) { etisserant@0: /* no parameter value given, so we pass a previously declared temporary variable. */ etisserant@0: std::string *temp_var_name = temp_var_name_factory.new_name(); etisserant@0: s4o.print(*temp_var_name); etisserant@0: delete temp_var_name; etisserant@0: } else { etisserant@0: param_value->accept(*this); etisserant@0: } etisserant@0: break; etisserant@0: case function_param_iterator_c::direction_extref: etisserant@0: /* TODO! */ etisserant@0: ERROR; etisserant@0: break; etisserant@0: } /* switch */ etisserant@0: } /* for(...) */ etisserant@0: etisserant@0: // symbol->parameter_assignment->accept(*this); etisserant@0: s4o.print(")"); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: etisserant@0: /* | il_operand_list ',' il_operand */ etisserant@0: // SYM_LIST(il_operand_list_c) etisserant@0: void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called! etisserant@0: etisserant@0: etisserant@0: /* | simple_instr_list il_simple_instruction */ etisserant@0: // SYM_LIST(simple_instr_list_c) etisserant@0: void *visit(simple_instr_list_c *symbol) { etisserant@0: /* A simple_instr_list_c is used to store a list of il operations etisserant@0: * being done within parenthesis... etisserant@0: * etisserant@0: * e.g.: etisserant@0: * LD var1 etisserant@0: * AND ( var2 etisserant@0: * OR var3 etisserant@0: * OR var4 etisserant@0: * ) etisserant@0: * etisserant@0: * This will be converted to C++ by defining a new scope etisserant@0: * with a new il default variable, and executing the il operands etisserant@0: * within this new scope. etisserant@0: * At the end of the scope the result, i.e. the value currently stored etisserant@0: * in the il default variable is copied to the variable used to take this etisserant@0: * value to the outside scope... etisserant@0: * etisserant@0: * The above example will result in the following C++ code: etisserant@0: * {__IL_DEFVAR_T __IL_DEFVAR_BACK; etisserant@0: * __IL_DEFVAR_T __IL_DEFVAR; etisserant@0: * etisserant@0: * __IL_DEFVAR.INTvar = var1; etisserant@0: * { etisserant@0: * __IL_DEFVAR_T __IL_DEFVAR; etisserant@0: * etisserant@0: * __IL_DEFVAR.INTvar = var2; etisserant@0: * __IL_DEFVAR.INTvar |= var3; etisserant@0: * __IL_DEFVAR.INTvar |= var4; etisserant@0: * etisserant@0: * __IL_DEFVAR_BACK = __IL_DEFVAR; etisserant@0: * } etisserant@0: * __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar; etisserant@0: * etisserant@0: * } etisserant@0: * etisserant@0: * The intial value of the il default variable (in the above etisserant@0: * example 'var2') is passed to this simple_instr_list_c visitor etisserant@0: * using the il_default_variable_init_value parameter. etisserant@0: * Since it is possible to have parenthesis inside other parenthesis etisserant@0: * recursively, we reset the il_default_variable_init_value to NULL etisserant@0: * as soon as we no longer require it, as it may be used once again etisserant@0: * in the line etisserant@0: * print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); etisserant@0: * etisserant@0: */ etisserant@0: etisserant@0: /* Declare the default variable, that will store the result of the IL operations... */ etisserant@0: s4o.print("{\n"); etisserant@0: s4o.indent_right(); etisserant@0: etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print(IL_DEFVAR_T); etisserant@0: s4o.print(" "); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(";\n\n"); etisserant@0: etisserant@0: /* Check whether we should initiliase the il default variable... */ etisserant@0: if (NULL != this->il_default_variable_init_value) { etisserant@0: /* Yes, we must... */ etisserant@0: /* We will do it by instatiating a LD operator, and having this etisserant@0: * same generate_cc_il_c class visiting it! etisserant@0: */ etisserant@0: LD_operator_c ld_oper; etisserant@0: il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value); etisserant@0: etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: il_simple_oper.accept(*this); etisserant@0: s4o.print(";\n"); etisserant@0: } etisserant@0: etisserant@0: /* this parameter no longer required... */ etisserant@0: this->il_default_variable_init_value = NULL; etisserant@0: etisserant@0: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n"); etisserant@0: etisserant@0: /* copy the result in the default variable to the variable etisserant@0: * used to pass the data out to the scope enclosing etisserant@0: * the current scope! etisserant@0: * etisserant@0: * We also need to update the data type currently stored within etisserant@0: * the variable used to pass the data to the outside scope... etisserant@0: */ etisserant@0: this->default_variable_back_name.current_type = this->default_variable_name.current_type; etisserant@0: s4o.print("\n"); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: this->default_variable_back_name.accept(*this); etisserant@0: s4o.print(" = "); etisserant@0: this->default_variable_name.accept(*this); etisserant@0: s4o.print(";\n"); etisserant@0: etisserant@0: s4o.indent_left(); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: s4o.print("}\n"); etisserant@0: s4o.print(s4o.indent_spaces); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: /* | il_initial_param_list il_param_instruction */ etisserant@0: // SYM_LIST(il_param_list_c) etisserant@0: void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called! etisserant@0: etisserant@0: /* il_assign_operator il_operand etisserant@0: * | il_assign_operator '(' eol_list simple_instr_list ')' etisserant@0: */ etisserant@0: // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused) etisserant@0: void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called! etisserant@0: etisserant@0: /* il_assign_out_operator variable */ etisserant@0: // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable); etisserant@0: void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called! etisserant@0: etisserant@0: /*******************/ etisserant@0: /* B 2.2 Operators */ etisserant@0: /*******************/ etisserant@0: etisserant@0: void *visit(LD_operator_c *symbol) { etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: XXX_operator(&(this->default_variable_name), " = ", this->current_operand); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(LDN_operator_c *symbol) { etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: XXX_operator(&(this->default_variable_name), etisserant@0: is_bool_type(this->current_operand_type)?" = !":" = ~", etisserant@0: this->current_operand); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(ST_operator_c *symbol) { etisserant@0: XXX_operator(this->current_operand, " = ",&(this->default_variable_name)); etisserant@0: /* the data type resulting from this operation is unchamged. */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(STN_operator_c *symbol) { etisserant@0: XXX_operator(this->current_operand, etisserant@0: is_bool_type(this->current_operand_type)?" = !":" = ~", etisserant@0: &(this->default_variable_name)); etisserant@0: /* the data type resulting from this operation is unchamged. */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(NOT_operator_c *symbol) { etisserant@0: if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR; etisserant@0: XXX_operator(&(this->default_variable_name), etisserant@0: is_bool_type(this->default_variable_name.current_type)?" = !":" = ~", etisserant@0: &(this->default_variable_name)); etisserant@0: /* the data type resulting from this operation is unchanged. */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(S_operator_c *symbol) { etisserant@0: if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; etisserant@0: etisserant@0: C_modifier(); etisserant@0: this->current_operand->accept(*this); etisserant@0: s4o.print(is_bool_type(this->current_operand_type)?" = true":" = 1"); etisserant@0: /* the data type resulting from this operation is unchanged! */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(R_operator_c *symbol) { etisserant@0: if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR; etisserant@0: etisserant@0: C_modifier(); etisserant@0: this->current_operand->accept(*this); etisserant@0: s4o.print(is_bool_type(this->current_operand_type)?" = false":" = 0"); etisserant@0: /* the data type resulting from this operation is unchanged! */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(S1_operator_c *symbol) {return XXX_CAL_operator("S1", this->current_operand);} etisserant@0: void *visit(R1_operator_c *symbol) {return XXX_CAL_operator("R1", this->current_operand);} etisserant@0: void *visit(CLK_operator_c *symbol) {return XXX_CAL_operator("CLK", this->current_operand);} etisserant@0: void *visit(CU_operator_c *symbol) {return XXX_CAL_operator("CU", this->current_operand);} etisserant@0: void *visit(CD_operator_c *symbol) {return XXX_CAL_operator("CD", this->current_operand);} etisserant@0: void *visit(PV_operator_c *symbol) {return XXX_CAL_operator("PV", this->current_operand);} etisserant@0: void *visit(IN_operator_c *symbol) {return XXX_CAL_operator("IN", this->current_operand);} etisserant@0: void *visit(PT_operator_c *symbol) {return XXX_CAL_operator("PT", this->current_operand);} etisserant@0: etisserant@0: void *visit(AND_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " &= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(OR_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " |= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(XOR_operator_c *symbol) { etisserant@0: // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types! etisserant@0: XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(ANDN_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), etisserant@0: is_bool_type(this->current_operand_type)?" &= !":" &= ~", etisserant@0: this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(ORN_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), etisserant@0: is_bool_type(this->current_operand_type)?" |= !":" |= ~", etisserant@0: this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(XORN_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), etisserant@0: // bit by bit exclusive OR !! Also seems to work with boolean types! etisserant@0: is_bool_type(this->current_operand_type)?" ^= !":" ^= ~", etisserant@0: this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(ADD_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " += ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(SUB_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " -= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(MUL_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " *= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(DIV_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " /= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(MOD_operator_c *symbol) { etisserant@0: XXX_operator(&(this->default_variable_name), " %= ", this->current_operand); etisserant@0: /* the data type resulting from this operation... */ etisserant@0: this->default_variable_name.current_type = this->current_operand_type; etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: void *visit(GT_operator_c *symbol) { etisserant@0: return CMP_operator(this->current_operand, " > "); etisserant@0: } etisserant@0: etisserant@0: void *visit(GE_operator_c *symbol) { etisserant@0: return CMP_operator(this->current_operand, " >= "); etisserant@0: } etisserant@0: etisserant@0: void *visit(EQ_operator_c *symbol) { etisserant@0: return CMP_operator(this->current_operand, " == "); etisserant@0: } etisserant@0: etisserant@0: void *visit(LT_operator_c *symbol) { etisserant@0: return CMP_operator(this->current_operand, " < "); etisserant@0: } etisserant@0: etisserant@0: void *visit(LE_operator_c *symbol) { etisserant@0: return CMP_operator(this->current_operand, " <= "); etisserant@0: } etisserant@0: etisserant@0: void *visit(NE_operator_c *symbol) { etisserant@0: return CMP_operator(this->current_operand, " != "); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: //SYM_REF0(CAL_operator_c) etisserant@0: // This method will be called from within the il_fb_call_c visitor method etisserant@0: void *visit(CAL_operator_c *symbol) {return NULL;} etisserant@0: etisserant@0: //SYM_REF0(CALC_operator_c) etisserant@0: // This method will be called from within the il_fb_call_c visitor method etisserant@0: void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;} etisserant@0: etisserant@0: //SYM_REF0(CALCN_operator_c) etisserant@0: // This method will be called from within the il_fb_call_c visitor method etisserant@0: void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;} etisserant@0: etisserant@0: /* NOTE: The semantics of the RET operator requires us to return a value etisserant@0: * if the IL code is inside a function, but simply return no value if etisserant@0: * the IL code is inside a function block or program! etisserant@0: * Nevertheless, it is the generate_cc_c class itself that etisserant@0: * introduces the 'reaturn ' into the c++ code at the end etisserant@0: * of every function. This class does not know whether the IL code etisserant@0: * is inside a function or a function block. etisserant@0: * We work around this by jumping to the end of the code, etisserant@0: * that will be marked by the END_LABEL label in the etisserant@0: * instruction_list_c visitor... etisserant@0: */ etisserant@0: // SYM_REF0(RET_operator_c) etisserant@0: void *visit(RET_operator_c *symbol) { etisserant@0: s4o.print("goto ");s4o.print(END_LABEL); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: // SYM_REF0(RETC_operator_c) etisserant@0: void *visit(RETC_operator_c *symbol) { etisserant@0: C_modifier(); etisserant@0: s4o.print("goto ");s4o.print(END_LABEL); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: // SYM_REF0(RETCN_operator_c) etisserant@0: void *visit(RETCN_operator_c *symbol) { etisserant@0: CN_modifier(); etisserant@0: s4o.print("goto ");s4o.print(END_LABEL); etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: //SYM_REF0(JMP_operator_c) etisserant@0: void *visit(JMP_operator_c *symbol) { etisserant@0: if (NULL == this->jump_label) ERROR; etisserant@0: etisserant@0: s4o.print("goto "); etisserant@0: this->jump_label->accept(*this); etisserant@0: /* the data type resulting from this operation is unchanged! */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: // SYM_REF0(JMPC_operator_c) etisserant@0: void *visit(JMPC_operator_c *symbol) { etisserant@0: if (NULL == this->jump_label) ERROR; etisserant@0: etisserant@0: C_modifier(); etisserant@0: s4o.print("goto "); etisserant@0: this->jump_label->accept(*this); etisserant@0: /* the data type resulting from this operation is unchanged! */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: // SYM_REF0(JMPCN_operator_c) etisserant@0: void *visit(JMPCN_operator_c *symbol) { etisserant@0: if (NULL == this->jump_label) ERROR; etisserant@0: etisserant@0: CN_modifier(); etisserant@0: s4o.print("goto "); etisserant@0: this->jump_label->accept(*this); etisserant@0: /* the data type resulting from this operation is unchanged! */ etisserant@0: return NULL; etisserant@0: } etisserant@0: etisserant@0: #if 0 etisserant@0: /*| [NOT] any_identifier SENDTO */ etisserant@0: SYM_REF2(il_assign_out_operator_c, option, variable_name) etisserant@0: #endif etisserant@0: etisserant@0: }; /* generate_cc_il_c */ etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* The implementation of the single visit() member function etisserant@0: * of il_default_variable_c. etisserant@0: * It can only come after the full declaration of etisserant@0: * generate_cc_il_c. Since we define and declare etisserant@0: * generate_cc_il_c simultaneously, it can only come etisserant@0: * after the definition... etisserant@0: */ etisserant@0: void *il_default_variable_c::accept(visitor_c &visitor) { etisserant@0: /* An ugly hack!! */ etisserant@0: /* This is required because we need to over-ride the base etisserant@0: * accept(visitor_c &) method of the class symbol_c, etisserant@0: * so this method may be called through a symbol_c * etisserant@0: * reference! etisserant@0: * etisserant@0: * But, the visitor_c does not include a visitor to etisserant@0: * an il_default_variable_c, which means that we couldn't etisserant@0: * simply call visitor.visit(this); etisserant@0: * etisserant@0: * We therefore need to use the dynamic_cast hack!! etisserant@0: * etisserant@0: * Note too that we can't cast a visitor_c to a etisserant@0: * il_default_variable_visitor_c, since they are not related. etisserant@0: * Nor may the il_default_variable_visitor_c inherit from etisserant@0: * visitor_c, because then generate_cc_il_c would contain etisserant@0: * two visitor_c base classes, one each through etisserant@0: * il_default_variable_visitor_c and generate_cc_type_c etisserant@0: * etisserant@0: * We could use virtual inheritance of the visitor_c, but it etisserant@0: * would probably create more problems than it is worth! etisserant@0: */ etisserant@0: generate_cc_il_c *v; etisserant@0: v = dynamic_cast(&visitor); etisserant@0: if (v == NULL) ERROR; etisserant@0: etisserant@0: return v->visit(this); etisserant@0: } etisserant@0: etisserant@0: etisserant@0: etisserant@0: etisserant@0: il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) { etisserant@0: if (NULL == var_name_str) ERROR; etisserant@0: /* Note: current_type may start off with NULL */ etisserant@0: etisserant@0: this->var_name = new identifier_c(var_name_str); etisserant@0: if (NULL == this->var_name) ERROR; etisserant@0: etisserant@0: this->current_type = current_type; etisserant@0: }