stage4/generate_c/generate_c_il.cc
changeset 70 e1f0ebd2d9ec
child 98 d0cdf1d00b74
equal deleted inserted replaced
69:41cb5b80416e 70:e1f0ebd2d9ec
       
     1 /*
       
     2  * (c) 2003 Mario de Sousa
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 
       
    26 /*
       
    27  * Conversion of il statements (i.e. IL code).
       
    28  *
       
    29  * This is part of the 4th stage that generates
       
    30  * a c++ source program equivalent to the IL and ST
       
    31  * code.
       
    32  */
       
    33 
       
    34 
       
    35 
       
    36 
       
    37 /***********************************************************************/
       
    38 /***********************************************************************/
       
    39 /***********************************************************************/
       
    40 /***********************************************************************/
       
    41 
       
    42 /* Returns the data type of an il_operand.
       
    43  *
       
    44  * Note that the il_operand may be a variable, in which case
       
    45  * we return the type of the variable instance.
       
    46  * The il_operand may also be a constant, in which case
       
    47  * we return the data type of that constant.
       
    48  *
       
    49  * The variable instance may be a member of a structured variable,
       
    50  * or an element in an array, or any combination of the two.
       
    51  *
       
    52  * The class constructor must be given the search scope
       
    53  * (function, function block or program within which
       
    54  * the possible il_operand variable instance was declared).
       
    55  */
       
    56 
       
    57 class search_il_operand_type_c {
       
    58 
       
    59   private:
       
    60     search_varfb_instance_type_c search_varfb_instance_type;
       
    61     search_constant_type_c search_constant_type;
       
    62 
       
    63   public:
       
    64     search_il_operand_type_c(symbol_c *search_scope): search_varfb_instance_type(search_scope) {}
       
    65 
       
    66   public:
       
    67     symbol_c *get_type(symbol_c *il_operand) {
       
    68       symbol_c *res;
       
    69 
       
    70       /* We first assume that it is a constant... */
       
    71       res = search_constant_type.get_type(il_operand);
       
    72       if (res != NULL) return res;
       
    73 
       
    74       /* Nope, now we assume it is a variable, and determine its type... */
       
    75       res = search_varfb_instance_type.get_type(il_operand);
       
    76       if (NULL != res) return res;
       
    77 
       
    78       /* not found */
       
    79       return NULL;
       
    80     }
       
    81 };
       
    82 
       
    83 
       
    84 
       
    85 /***********************************************************************/
       
    86 /***********************************************************************/
       
    87 /***********************************************************************/
       
    88 /***********************************************************************/
       
    89 
       
    90 
       
    91 /* A new class to ouput the il default variable to c++ code
       
    92  * We use this class, inheriting from symbol_c, so it may be used
       
    93  * as any other symbol_c object in the intermediate parse tree,
       
    94  * more specifically, so it can be used as any other il operand.
       
    95  * This makes the rest of the code much easier...
       
    96  *
       
    97  * Nevertheless, the basic visitor class visitor_c does not know
       
    98  * how to visit this new il_default_variable_c class, so we have
       
    99  * to extend that too.
       
   100  * In reality extending the basic symbols doesn't quite work out
       
   101  * as cleanly as desired (we need to use dynamic_cast in the
       
   102  * accept method of the il_default_variable_c), but it is cleaner
       
   103  * than the alternative...
       
   104  */
       
   105 class il_default_variable_c;
       
   106 
       
   107 /* This visitor class is not really required, we could place the
       
   108  * visit() method directly in genertae_cc_il_c, but doing it in
       
   109  * a seperate class makes the architecture more evident...
       
   110  */
       
   111 class il_default_variable_visitor_c {
       
   112   public:
       
   113     virtual void *visit(il_default_variable_c *symbol) = 0;
       
   114 
       
   115     virtual ~il_default_variable_visitor_c(void) {return;}
       
   116 };
       
   117 
       
   118 
       
   119 /* A class to print out to the resulting C++ code
       
   120  * the IL default variable name.
       
   121  *
       
   122  * It includes a reference to its name,
       
   123  * and the data type of the data currently stored
       
   124  * in this C++ variable... This is required because the
       
   125  * C++ variable is a union, and we must know which member
       
   126  * of the union top reference!!
       
   127  *
       
   128  * Note that we also need to keep track of the data type of
       
   129  * the value currently being stored in the default variable.
       
   130  * This is required so we can process parenthesis,
       
   131  *
       
   132  * e.g. :
       
   133  *         LD var1
       
   134  *         AND (
       
   135  *         LD var2
       
   136  *         OR var3
       
   137  *         )
       
   138  *
       
   139  * Note that we only execute the 'AND (' operation when we come across
       
   140  * the ')', i.e. once we have evaluated the result of the
       
   141  * instructions inside the parenthesis.
       
   142  * When we do execute the 'AND (' operation, we need to know the data type
       
   143  * of the operand, which in this case is the result of the evaluation of the
       
   144  * instruction list inside the parenthesis. We can only know this if we
       
   145  * keep track of the data type currently stored in the default variable!
       
   146  *
       
   147  * We use the current_type inside the generate_c_il::default_variable_name variable
       
   148  * to track this!
       
   149  */
       
   150 class il_default_variable_c: public symbol_c {
       
   151   public:
       
   152     symbol_c *var_name;  /* in principle, this should point to an indentifier_c */
       
   153     symbol_c *current_type;
       
   154 
       
   155   public:
       
   156     il_default_variable_c(const char *var_name_str, symbol_c *current_type);
       
   157     virtual void *accept(visitor_c &visitor);
       
   158 };
       
   159 
       
   160 
       
   161 /***********************************************************************/
       
   162 /***********************************************************************/
       
   163 /***********************************************************************/
       
   164 /***********************************************************************/
       
   165 
       
   166 
       
   167 
       
   168 class generate_c_il_c: public generate_c_typedecl_c, il_default_variable_visitor_c {
       
   169 
       
   170   private:
       
   171     /* When compiling il code, it becomes necessary to determine the
       
   172      * data type of il operands. To do this, we must first find the
       
   173      * il operand's declaration, within the scope of the function block
       
   174      * or function currently being processed.
       
   175      * The following object does just that...
       
   176      * This object instance will then later be called while the
       
   177      * remaining il code is being handled.
       
   178      */
       
   179     //search_il_operand_type_c *search_il_operand_type;
       
   180     search_expression_type_c *search_expression_type;
       
   181 
       
   182     /* The initial value that should be given to the IL default variable
       
   183      * imediately after a parenthesis is opened.
       
   184      * This variable is only used to pass data from the
       
   185      * il_expression_c visitor to the simple_instr_list_c visitor.
       
   186      *
       
   187      * e.g.:
       
   188      *         LD var1
       
   189      *         AND ( var2
       
   190      *         OR var3
       
   191      *         )
       
   192      *
       
   193      * In the above code sample, the line 'AND ( var2' constitutes
       
   194      * an il_expression_c, where var2 should be loaded into the
       
   195      * il default variable before continuing with the expression
       
   196      * inside the parenthesis.
       
   197      * Unfortunately, only the simple_instr_list_c may do the
       
   198      * initial laoding of the var2 bariable following the parenthesis,
       
   199      * so the il_expression_c visitor will have to pass 'var2' as a
       
   200      * parameter to the simple_instr_list_c visitor.
       
   201      * Ergo, the existance of the following parameter...!
       
   202      */
       
   203     symbol_c *il_default_variable_init_value;
       
   204 
       
   205     /* Operand to the IL operation currently being processed... */
       
   206     /* These variables are used to pass data from the
       
   207      * il_simple_operation_c and il_expression_c visitors
       
   208      * to the il operator visitors (i.e. LD_operator_c,
       
   209      * LDN_operator_c, ST_operator_c, STN_operator_c, ...)
       
   210      */
       
   211     symbol_c *current_operand;
       
   212     symbol_c *current_operand_type;
       
   213 
       
   214     /* Label to which the current IL jump operation should jump to... */
       
   215     /* This variable is used to pass data from the
       
   216      * il_jump_operation_c visitor
       
   217      * to the il jump operator visitors (i.e. JMP_operator_c,
       
   218      * JMPC_operator_c, JMPCN_operator_c, ...)
       
   219      */
       
   220     symbol_c *jump_label;
       
   221 
       
   222     /* The result of the comparison IL operations (GT, EQ, LT, ...)
       
   223      * is a boolean variable.
       
   224      * This class keeps track of the current data type stored in the
       
   225      * il default variable. This is usually done by keeping a reference
       
   226      * to the data type of the last operand. Nevertheless, in the case of
       
   227      * the comparison IL operators, the data type of the result (a boolean)
       
   228      * is not the data type of the operand. We therefore need an object
       
   229      * of the boolean data type to keep as a reference of the current
       
   230      * data type.
       
   231      * The following object is it...
       
   232      */
       
   233     bool_type_name_c bool_type;
       
   234 
       
   235     /* the data type of the IL default variable... */
       
   236     #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T"
       
   237     /* The name of the IL default variable... */
       
   238     #define IL_DEFVAR   VAR_LEADER "IL_DEFVAR"
       
   239     /* The name of the variable used to pass the result of a
       
   240      * parenthesised instruction list to the immediately preceding
       
   241      * scope ...
       
   242      */
       
   243     #define IL_DEFVAR_BACK   VAR_LEADER "IL_DEFVAR_BACK"
       
   244     il_default_variable_c default_variable_name;
       
   245     il_default_variable_c default_variable_back_name;
       
   246 
       
   247     /* Some function calls in the body of functions or function blocks
       
   248      * may leave some parameters to their default values, and
       
   249      * ignore some output parameters of the function being called.
       
   250      * Our conversion of ST functions to C++ does not contemplate that,
       
   251      * i.e. each called function must get all it's input and output
       
   252      * parameters set correctly.
       
   253      * For input parameters we merely need to call the function with
       
   254      * the apropriate default value, but for output parameters
       
   255      * we must create temporary variables to hold the output value.
       
   256      *
       
   257      * We declare all the temporary output variables at the begining of
       
   258      * the body of each function or function block, and use them as
       
   259      * in function calls later on as they become necessary...
       
   260      * Note that we cannot create these variables just before a function
       
   261      * call, as the function call itself may be integrated within an
       
   262      * expression, or another function call!
       
   263      *
       
   264      * The variables are declared in the exact same order in which they
       
   265      * will be used later on during the function calls, which allows us
       
   266      * to simply re-create the name that was used for the temporary variable
       
   267      * instead of keeping it in some list.
       
   268      * The names are recreated by the temp_var_name_factory, after reset()
       
   269      * has been called!
       
   270      *
       
   271      * This function will genertae code similar to...
       
   272      *
       
   273      *     INT __TMP_0 = 23;
       
   274      *     REAL __TMP_1 = 45.5;
       
   275      *     ...
       
   276      */
       
   277     temp_var_name_c temp_var_name_factory;
       
   278 
       
   279     /* When calling a function block, we must first find it's type,
       
   280      * by searching through the declarations of the variables currently
       
   281      * in scope.
       
   282      * This class does just that...
       
   283      * A new class is instantiated whenever we begin generating the code
       
   284      * for a function block type declaration, or a program declaration.
       
   285      * This object instance will then later be called while the
       
   286      * function block's or the program's body is being handled.
       
   287      *
       
   288      * Note that functions cannot contain calls to function blocks,
       
   289      * so we do not create an object instance when handling
       
   290      * a function declaration.
       
   291      */
       
   292     search_fb_instance_decl_c *search_fb_instance_decl;
       
   293 
       
   294 
       
   295   public:
       
   296     generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
       
   297     : generate_c_typedecl_c(s4o_ptr),
       
   298       default_variable_name(IL_DEFVAR, NULL),
       
   299       default_variable_back_name(IL_DEFVAR_BACK, NULL)
       
   300     {
       
   301       //search_il_operand_type  = new search_il_operand_type_c(scope);
       
   302       search_expression_type = new search_expression_type_c(scope);
       
   303       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
       
   304       current_operand = NULL;
       
   305       current_operand_type = NULL;
       
   306       il_default_variable_init_value = NULL;
       
   307       this->set_variable_prefix(variable_prefix);
       
   308     }
       
   309 
       
   310     virtual ~generate_c_il_c(void) {
       
   311       delete search_fb_instance_decl;
       
   312       //delete search_il_operand_type;
       
   313       delete search_expression_type;
       
   314     }
       
   315 
       
   316     void generate(instruction_list_c *il) {
       
   317       generate_c_tempvardecl_c generate_c_tempvardecl(&s4o);
       
   318       generate_c_tempvardecl.generate(il, &temp_var_name_factory);
       
   319       il->accept(*this);
       
   320     }
       
   321 
       
   322     /* Declare the backup to the default variable, that will store the result
       
   323      * of the IL operations executed inside a parenthesis...
       
   324      */
       
   325     void declare_backup_variable(void) {
       
   326       s4o.print(s4o.indent_spaces);
       
   327       s4o.print(IL_DEFVAR_T);
       
   328       s4o.print(" ");
       
   329       print_backup_variable();
       
   330       s4o.print(";\n");
       
   331     }
       
   332     
       
   333     void print_backup_variable(void) {
       
   334       this->default_variable_back_name.accept(*this);
       
   335     }
       
   336 
       
   337   private:
       
   338     /* A helper function... */
       
   339     /*
       
   340     bool is_bool_type(symbol_c *type_symbol) {
       
   341       return (NULL != dynamic_cast<bool_type_name_c *>(type_symbol));
       
   342     }
       
   343     */
       
   344 
       
   345     /* A helper function... */
       
   346     void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) {
       
   347       if ((NULL == lo) || (NULL == ro)) ERROR;
       
   348       if (NULL == op) ERROR;
       
   349 
       
   350       lo->accept(*this);
       
   351       s4o.print(op);
       
   352       ro->accept(*this);
       
   353       return NULL;
       
   354     }
       
   355 
       
   356     /* A helper function... */
       
   357     void *XXX_function(const char *func, symbol_c *lo, symbol_c *ro) {
       
   358       if ((NULL == lo) || (NULL == ro)) ERROR;
       
   359       if (NULL == func) ERROR;
       
   360 
       
   361       lo->accept(*this);
       
   362       s4o.print(" = ");
       
   363       s4o.print(func);
       
   364       s4o.print("(");
       
   365       lo->accept(*this);
       
   366       s4o.print(", ");
       
   367       ro->accept(*this);
       
   368       s4o.print(")");
       
   369       return NULL;
       
   370     }
       
   371 
       
   372     /* A helper function... */
       
   373     void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) {
       
   374       if (NULL == fb_name) ERROR;
       
   375       symbolic_variable_c *sv = dynamic_cast<symbolic_variable_c *>(fb_name);
       
   376       if (NULL == sv) ERROR;
       
   377       identifier_c *id = dynamic_cast<identifier_c *>(sv->var_name);
       
   378       if (NULL == id) ERROR;
       
   379 
       
   380       identifier_c param(param_name);
       
   381 
       
   382       //SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list)
       
   383       il_param_assignment_c il_param_assignment(&param, &this->default_variable_name, NULL);
       
   384       // SYM_LIST(il_param_list_c)
       
   385       il_param_list_c il_param_list;
       
   386       il_param_list.add_element(&il_param_assignment);
       
   387       CAL_operator_c CAL_operator;
       
   388       // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
       
   389       il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list);
       
   390 
       
   391       il_fb_call.accept(*this);
       
   392       return NULL;
       
   393     }
       
   394 
       
   395     /* A helper function... */
       
   396     void *CMP_operator(symbol_c *o, const char *operation) {
       
   397       if (NULL == o) ERROR;
       
   398       if (NULL == this->default_variable_name.current_type) ERROR;
       
   399 
       
   400       symbol_c *backup = this->default_variable_name.current_type;
       
   401       this->default_variable_name.current_type = &(this->bool_type);
       
   402       this->default_variable_name.accept(*this);
       
   403       this->default_variable_name.current_type = backup;
       
   404 
       
   405       s4o.print(" = ");
       
   406       s4o.print(operation);
       
   407       this->default_variable_name.current_type->accept(*this);
       
   408       s4o.print("(2, ");
       
   409       this->default_variable_name.accept(*this);
       
   410       s4o.print(", ");
       
   411       o->accept(*this);
       
   412       s4o.print(")");
       
   413 
       
   414       /* the data type resulting from this operation... */
       
   415       this->default_variable_name.current_type = &(this->bool_type);
       
   416       return NULL;
       
   417     }
       
   418 
       
   419 
       
   420     /* A helper function... */
       
   421     void C_modifier(void) {
       
   422       if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
       
   423         s4o.print("if (");
       
   424         this->default_variable_name.accept(*this);
       
   425         s4o.print(") ");
       
   426       }
       
   427       else {ERROR;}
       
   428     }
       
   429 
       
   430     /* A helper function... */
       
   431     void CN_modifier(void) {
       
   432       if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
       
   433         s4o.print("if (!");
       
   434         this->default_variable_name.accept(*this);
       
   435         s4o.print(") ");
       
   436       }
       
   437       else {ERROR;}
       
   438     }
       
   439 
       
   440 
       
   441 public:
       
   442 void *visit(il_default_variable_c *symbol) {
       
   443   //s4o.print("il_default_variable_c VISITOR!!\n");
       
   444   symbol->var_name->accept(*this);
       
   445   if (NULL != symbol->current_type) {
       
   446     s4o.print(".");
       
   447     symbol->current_type->accept(*this);
       
   448     s4o.print("var");
       
   449   }
       
   450   return NULL;
       
   451 }
       
   452 
       
   453 
       
   454 private:
       
   455 
       
   456 /********************************************/
       
   457 /* B.1.4.1   Directly Represented Variables */
       
   458 /********************************************/
       
   459 // direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
       
   460 void *visit(direct_variable_c *symbol) {
       
   461   TRACE("direct_variable_c");
       
   462   /* Do not use print_token() as it will change everything into uppercase */
       
   463   if (strlen(symbol->value) == 0) ERROR;
       
   464   s4o.print("*(");
       
   465   this->print_variable_prefix();
       
   466   s4o.printlocation(symbol->value + 1);
       
   467   s4o.print(")");
       
   468   return NULL;
       
   469 }
       
   470 
       
   471 /****************************************/
       
   472 /* B.2 - Language IL (Instruction List) */
       
   473 /****************************************/
       
   474 
       
   475 /***********************************/
       
   476 /* B 2.1 Instructions and Operands */
       
   477 /***********************************/
       
   478 
       
   479 /* please see the comment before the RET_operator_c visitor for details... */
       
   480 #define END_LABEL VAR_LEADER "end"
       
   481 
       
   482 /*| instruction_list il_instruction */
       
   483 void *visit(instruction_list_c *symbol) {
       
   484   
       
   485   /* Declare the backup to the default variable, that will store the result
       
   486    * of the IL operations executed inside a parenthesis...
       
   487    */
       
   488   declare_backup_variable();
       
   489   
       
   490   /* Declare the default variable, that will store the result of the IL operations... */
       
   491   s4o.print(s4o.indent_spaces);
       
   492   s4o.print(IL_DEFVAR_T);
       
   493   s4o.print(" ");
       
   494   this->default_variable_name.accept(*this);
       
   495   s4o.print(";\n\n");
       
   496 
       
   497   print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
       
   498 
       
   499   /* write the label marking the end of the code block */
       
   500   /* please see the comment before the RET_operator_c visitor for details... */
       
   501   s4o.print("\n");
       
   502   s4o.print(s4o.indent_spaces);
       
   503   s4o.print(END_LABEL);
       
   504   s4o.print(":\n");
       
   505   s4o.indent_right();
       
   506   /* since every label must be followed by at least one statement, and
       
   507    * only the functions will introduce the return statement after this label,
       
   508    * function blocks written in IL would result in invalid C++ code.
       
   509    * To work around this we introduce the equivalent of a 'nop' operation
       
   510    * to humour the compiler...
       
   511    */
       
   512   s4o.print(s4o.indent_spaces);
       
   513   s4o.print("/* to humour the compiler, we insert a nop */\n");
       
   514   s4o.print(s4o.indent_spaces);
       
   515   this->default_variable_name.accept(*this);
       
   516   s4o.print(" = ");
       
   517   this->default_variable_name.accept(*this);
       
   518   s4o.print(";\n");
       
   519   s4o.indent_left();
       
   520 
       
   521   return NULL;
       
   522 }
       
   523 
       
   524 
       
   525 /* | label ':' [il_incomplete_instruction] eol_list */
       
   526 // SYM_REF2(il_instruction_c, label, il_instruction)
       
   527 void *visit(il_instruction_c *symbol) {
       
   528   if (NULL != symbol->label) {
       
   529     symbol->label->accept(*this);
       
   530     s4o.print(":\n");
       
   531     s4o.print(s4o.indent_spaces);
       
   532   }
       
   533   symbol->il_instruction->accept(*this);
       
   534   return NULL;
       
   535 }
       
   536 
       
   537 /* | il_simple_operator [il_operand] */
       
   538 //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
       
   539 void *visit(il_simple_operation_c *symbol) {
       
   540   this->current_operand = symbol->il_operand;
       
   541   if (NULL == this->current_operand) {
       
   542     this->current_operand_type = NULL;
       
   543   } else {
       
   544     this->current_operand_type = search_expression_type->get_type(this->current_operand);
       
   545     if (NULL == this->current_operand_type) ERROR;
       
   546   }
       
   547 
       
   548   symbol->il_simple_operator->accept(*this);
       
   549 
       
   550   this->current_operand = NULL;
       
   551   this->current_operand_type = NULL;
       
   552   return NULL;
       
   553 }
       
   554 
       
   555 
       
   556 /* | function_name [il_operand_list] */
       
   557 // SYM_REF2(il_function_call_c, function_name, il_operand_list)
       
   558 void *visit(il_function_call_c *symbol) {
       
   559   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
       
   560 
       
   561   if (f_decl == function_symtable.end_value()) {
       
   562     /* should never occur. The function being called MUST be in the symtable... */
       
   563     function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
       
   564     if (current_function_type == function_none) ERROR;
       
   565     
       
   566     symbol_c *param_data_type = default_variable_name.current_type;
       
   567     symbol_c *return_data_type = (symbol_c *)search_expression_type->compute_standard_function_il(symbol, param_data_type);
       
   568     if (NULL == return_data_type) ERROR;
       
   569     
       
   570     default_variable_name.current_type = return_data_type;
       
   571     this->default_variable_name.accept(*this);
       
   572     default_variable_name.current_type = param_data_type;
       
   573     s4o.print(" = ");
       
   574     
       
   575     function_call_param_iterator_c function_call_param_iterator(symbol);
       
   576     
       
   577     int nb_param = 1;
       
   578     if (symbol->il_operand_list != NULL)
       
   579       nb_param += ((list_c *)symbol->il_operand_list)->n;
       
   580 
       
   581 #include "il_code_gen.c"
       
   582 
       
   583 #if 0
       
   584     for(int current_param = 0; current_param < nb_param; current_param++) {
       
   585       symbol_c *param_value;
       
   586       if (current_param == 0)
       
   587         param_value = &this->default_variable_name;
       
   588       else {
       
   589         symbol_c *param_name = NULL;
       
   590         switch (current_function_type) {
       
   591           default: ERROR;
       
   592         }
       
   593         
       
   594          
       
   595         /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   596         param_value = function_call_param_iterator.search(param_name);
       
   597         delete param_name;
       
   598         
       
   599         /* Get the value from a foo(<param_value>) style call */
       
   600         if (param_value == NULL)
       
   601           param_value = function_call_param_iterator.next();
       
   602         
       
   603         if (param_value == NULL) ERROR;
       
   604       }
       
   605       
       
   606       switch (current_function_type) {
       
   607         case (function_sqrt):
       
   608           if (current_param == 0) {
       
   609             s4o.print("sqrt(");
       
   610             param_value->accept(*this);
       
   611             s4o.print(")");
       
   612           }
       
   613           else ERROR;
       
   614           break;
       
   615         default: ERROR;
       
   616       }
       
   617     } /* for(...) */
       
   618 #endif
       
   619 
       
   620     /* the data type returned by the function, and stored in the il default variable... */
       
   621     default_variable_name.current_type = return_data_type;
       
   622   }
       
   623   else {
       
   624     /* determine the base data type returned by the function being called... */
       
   625     search_base_type_c search_base_type;
       
   626     symbol_c *return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
       
   627     symbol_c *param_data_type = default_variable_name.current_type;
       
   628     if (NULL == return_data_type) ERROR;
       
   629   
       
   630     default_variable_name.current_type = return_data_type;
       
   631     this->default_variable_name.accept(*this);
       
   632     default_variable_name.current_type = param_data_type;
       
   633     s4o.print(" = ");
       
   634   
       
   635     symbol->function_name->accept(*this);
       
   636     s4o.print("(");
       
   637   
       
   638     /* loop through each function parameter, find the value we should pass
       
   639      * to it, and then output the c equivalent...
       
   640      */
       
   641   
       
   642     function_param_iterator_c fp_iterator(f_decl);
       
   643     identifier_c *param_name;
       
   644     function_call_param_iterator_c function_call_param_iterator(symbol);
       
   645     for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
   646       if (i != 1)
       
   647         s4o.print(", ");
       
   648   
       
   649       symbol_c *param_type = fp_iterator.param_type();
       
   650       if (param_type == NULL) ERROR;
       
   651   
       
   652       function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
   653   
       
   654   
       
   655       symbol_c *param_value = NULL;
       
   656   
       
   657       /* if it is the first parameter, semantics specifies that we should
       
   658        * get the value off the IL default variable!
       
   659        */
       
   660      if (1 == i)
       
   661        param_value = &this->default_variable_name;
       
   662   
       
   663       /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   664       /* NOTE: the following line of code is not required in this case, but it doesn't
       
   665        * harm to leave it in, as in the case of a non-formal syntax function call,
       
   666        * it will always return NULL.
       
   667        * We leave it in in case we later decide to merge this part of the code together
       
   668        * with the function calling code in generate_c_st_c, which does require
       
   669        * the following line...
       
   670        */
       
   671       if (param_value == NULL)
       
   672         param_value = function_call_param_iterator.search(param_name);
       
   673   
       
   674       /* Get the value from a foo(<param_value>) style call */
       
   675       if (param_value == NULL)
       
   676         param_value = function_call_param_iterator.next();
       
   677   
       
   678       switch (param_direction) {
       
   679         case function_param_iterator_c::direction_in:
       
   680           if (param_value == NULL) {
       
   681             /* No value given for parameter, so we must use the default... */
       
   682             /* First check whether default value specified in function declaration...*/
       
   683             param_value = fp_iterator.default_value();
       
   684           }
       
   685           if (param_value == NULL) {
       
   686             /* If not, get the default value of this variable's type */
       
   687             param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
       
   688           }
       
   689           if (param_value == NULL) ERROR;
       
   690           param_value->accept(*this);
       
   691           break;
       
   692         case function_param_iterator_c::direction_out:
       
   693         case function_param_iterator_c::direction_inout:
       
   694           if (param_value == NULL) {
       
   695   	  /* no parameter value given, so we pass a previously declared temporary variable. */
       
   696             std::string *temp_var_name = temp_var_name_factory.new_name();
       
   697             s4o.print(*temp_var_name);
       
   698             delete temp_var_name;
       
   699           } else {
       
   700             param_value->accept(*this);
       
   701           }
       
   702           break;
       
   703         case function_param_iterator_c::direction_extref:
       
   704           /* TODO! */
       
   705           ERROR;
       
   706           break;
       
   707       } /* switch */
       
   708     } /* for(...) */
       
   709   
       
   710     s4o.print(")");
       
   711     /* the data type returned by the function, and stored in the il default variable... */
       
   712     default_variable_name.current_type = return_data_type;
       
   713   }
       
   714   
       
   715   return NULL;
       
   716 }
       
   717 
       
   718 
       
   719 /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
       
   720 //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused)
       
   721 void *visit(il_expression_c *symbol) {
       
   722   /* We will be recursevely interpreting an instruction list,
       
   723    * so we store a backup of the data type of the value currently stored
       
   724    * in the default variable, and set the current data type to NULL
       
   725    */
       
   726   symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type;
       
   727   this->default_variable_name.current_type = NULL;
       
   728 
       
   729  /* Pass the symbol->il_operand to the simple_instr_list visitor
       
   730   * using the il_default_variable_init_value parameter...
       
   731   * Note that the simple_instr_list_c visitor will set this parameter
       
   732   * to NULL as soon as it does not require it any longer,
       
   733   * so we don't do it here again after the
       
   734   *   symbol->simple_instr_list->accept(*this);
       
   735   * returns...
       
   736   */
       
   737   this->il_default_variable_init_value = symbol->il_operand;
       
   738 
       
   739   /* Now do the parenthesised instructions... */
       
   740   /* NOTE: the following code line will get the variable
       
   741    * this->default_variable_name.current_type updated!
       
   742    */
       
   743   symbol->simple_instr_list->accept(*this);
       
   744 
       
   745   /* Now do the operation, using the previous result! */
       
   746   /* NOTE: The result of the previous instruction list will be stored
       
   747    * in a variable named IL_DEFVAR_BACK. This is done in the visitor
       
   748    * to instruction_list_c objects...
       
   749    */
       
   750   this->current_operand = &(this->default_variable_back_name);
       
   751   this->current_operand_type = this->default_variable_back_name.current_type;
       
   752 
       
   753   this->default_variable_name.current_type = old_current_default_variable_data_type;
       
   754   if (NULL == this->current_operand_type) ERROR;
       
   755 
       
   756   symbol->il_expr_operator->accept(*this);
       
   757 
       
   758   this->current_operand = NULL;
       
   759   this->current_operand_type = NULL;
       
   760   this->default_variable_back_name.current_type = NULL;
       
   761   return NULL;
       
   762 }
       
   763 
       
   764 /*  il_jump_operator label */
       
   765 // SYM_REF2(il_jump_operation_c, il_jump_operator, label)
       
   766 void *visit(il_jump_operation_c *symbol) {
       
   767  /* Pass the symbol->label to the il_jump_operation visitor
       
   768   * using the jump_label parameter...
       
   769   */
       
   770   this->jump_label = symbol->label;
       
   771   symbol->il_jump_operator->accept(*this);
       
   772   this->jump_label = NULL;
       
   773 
       
   774   return NULL;
       
   775 }
       
   776 
       
   777 /*   il_call_operator prev_declared_fb_name
       
   778  * | il_call_operator prev_declared_fb_name '(' ')'
       
   779  * | il_call_operator prev_declared_fb_name '(' eol_list ')'
       
   780  * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
       
   781  * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
       
   782  */
       
   783 // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
       
   784 void *visit(il_fb_call_c *symbol) {
       
   785   symbol->il_call_operator->accept(*this);
       
   786   s4o.print("{\n");
       
   787   s4o.indent_right();
       
   788   s4o.print(s4o.indent_spaces);
       
   789 
       
   790   /* first figure out what is the name of the function block type of the function block being called... */
       
   791   symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name);
       
   792     /* should never occur. The function block instance MUST have been declared... */
       
   793   if (function_block_type_name == NULL) ERROR;
       
   794 
       
   795   /* Now find the declaration of the function block type being called... */
       
   796   function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name);
       
   797     /* should never occur. The function block type being called MUST be in the symtable... */
       
   798   if (fb_decl == function_block_type_symtable.end_value()) ERROR;
       
   799 
       
   800   /* loop through each function block parameter, find the value we should pass
       
   801    * to it, and then output the c equivalent...
       
   802    */
       
   803   function_param_iterator_c fp_iterator(fb_decl);
       
   804   identifier_c *param_name;
       
   805   function_call_param_iterator_c function_call_param_iterator(symbol);
       
   806   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
   807     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
   808 
       
   809     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   810     symbol_c *param_value = function_call_param_iterator.search(param_name);
       
   811 
       
   812     /* Get the value from a foo(<param_value>) style call */
       
   813     if (param_value == NULL)
       
   814       param_value = function_call_param_iterator.next();
       
   815 
       
   816     /* now output the value assignment */
       
   817     if (param_value != NULL)
       
   818       if ((param_direction == function_param_iterator_c::direction_in) ||
       
   819           (param_direction == function_param_iterator_c::direction_inout)) {
       
   820         symbol->fb_name->accept(*this);
       
   821         s4o.print(".");
       
   822         param_name->accept(*this);
       
   823         s4o.print(" = ");
       
   824         param_value->accept(*this);
       
   825         s4o.print(";\n" + s4o.indent_spaces);
       
   826       }
       
   827   } /* for(...) */
       
   828 
       
   829   /* now call the function... */
       
   830   function_block_type_name->accept(*this);
       
   831   s4o.print(FB_FUNCTION_SUFFIX);
       
   832   s4o.print("(&");
       
   833   symbol->fb_name->accept(*this);
       
   834   s4o.print(")");
       
   835 
       
   836   /* loop through each function parameter, find the variable to which
       
   837    * we should atribute the value of all output or inoutput parameters.
       
   838    */
       
   839   fp_iterator.reset();
       
   840   function_call_param_iterator.reset();
       
   841   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
   842     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
   843 
       
   844     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   845     symbol_c *param_value = function_call_param_iterator.search(param_name);
       
   846 
       
   847     /* Get the value from a foo(<param_value>) style call */
       
   848     if (param_value == NULL)
       
   849       param_value = function_call_param_iterator.next();
       
   850 
       
   851     /* now output the value assignment */
       
   852     if (param_value != NULL)
       
   853       if ((param_direction == function_param_iterator_c::direction_out) ||
       
   854           (param_direction == function_param_iterator_c::direction_inout)) {
       
   855         s4o.print(";\n"+ s4o.indent_spaces);
       
   856         param_value->accept(*this);
       
   857         s4o.print(" = ");
       
   858         symbol->fb_name->accept(*this);
       
   859         s4o.print(".");
       
   860         param_name->accept(*this);
       
   861       }
       
   862   } /* for(...) */
       
   863 
       
   864   s4o.print(";\n");
       
   865   s4o.indent_left();
       
   866   s4o.print(s4o.indent_spaces);
       
   867   s4o.print("}");
       
   868 
       
   869   return NULL;
       
   870 }
       
   871 
       
   872 
       
   873 
       
   874 /* | function_name '(' eol_list [il_param_list] ')' */
       
   875 // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
       
   876 void *visit(il_formal_funct_call_c *symbol) {
       
   877   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
       
   878 
       
   879   if (f_decl == function_symtable.end_value())
       
   880     /* should never occur. The function being called MUST be in the symtable... */
       
   881     ERROR;
       
   882 
       
   883   symbol->function_name->accept(*this);
       
   884   s4o.print("(");
       
   885 
       
   886   /* loop through each function parameter, find the value we should pass
       
   887    * to it, and then output the c equivalent...
       
   888    */
       
   889 
       
   890   function_param_iterator_c fp_iterator(f_decl);
       
   891   identifier_c *param_name;
       
   892   function_call_param_iterator_c function_call_param_iterator(symbol);
       
   893   for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
       
   894     if (i != 1)
       
   895       s4o.print(", ");
       
   896 
       
   897     symbol_c *param_type = fp_iterator.param_type();
       
   898     if (param_type == NULL) ERROR;
       
   899 
       
   900     function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
       
   901 
       
   902 
       
   903     symbol_c *param_value = NULL;
       
   904 
       
   905     /* Get the value from a foo(<param_name> = <param_value>) style call */
       
   906     if (param_value == NULL)
       
   907       param_value = function_call_param_iterator.search(param_name);
       
   908 
       
   909     /* Get the value from a foo(<param_value>) style call */
       
   910     /* NOTE: the following line of code is not required in this case, but it doesn't
       
   911      * harm to leave it in, as in the case of a formal syntax function call,
       
   912      * it will always return NULL.
       
   913      * We leave it in in case we later decide to merge this part of the code together
       
   914      * with the function calling code in generate_c_st_c, which does require
       
   915      * the following line...
       
   916      */
       
   917     if (param_value == NULL)
       
   918       param_value = function_call_param_iterator.next();
       
   919 
       
   920     switch (param_direction) {
       
   921       case function_param_iterator_c::direction_in:
       
   922         if (param_value == NULL) {
       
   923           /* No value given for parameter, so we must use the default... */
       
   924           /* First check whether default value specified in function declaration...*/
       
   925           param_value = fp_iterator.default_value();
       
   926         }
       
   927         if (param_value == NULL) {
       
   928           /* If not, get the default value of this variable's type */
       
   929           param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
       
   930         }
       
   931         if (param_value == NULL) ERROR;
       
   932         param_value->accept(*this);
       
   933 	break;
       
   934       case function_param_iterator_c::direction_out:
       
   935       case function_param_iterator_c::direction_inout:
       
   936         if (param_value == NULL) {
       
   937 	  /* no parameter value given, so we pass a previously declared temporary variable. */
       
   938           std::string *temp_var_name = temp_var_name_factory.new_name();
       
   939           s4o.print(*temp_var_name);
       
   940           delete temp_var_name;
       
   941 	} else {
       
   942           param_value->accept(*this);
       
   943 	}
       
   944 	break;
       
   945       case function_param_iterator_c::direction_extref:
       
   946         /* TODO! */
       
   947         ERROR;
       
   948 	break;
       
   949     } /* switch */
       
   950   } /* for(...) */
       
   951 
       
   952   // symbol->parameter_assignment->accept(*this);
       
   953   s4o.print(")");
       
   954   return NULL;
       
   955 }
       
   956 
       
   957 
       
   958 /* | il_operand_list ',' il_operand */
       
   959 // SYM_LIST(il_operand_list_c)
       
   960 void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called!
       
   961 
       
   962 
       
   963 /* | simple_instr_list il_simple_instruction */
       
   964 // SYM_LIST(simple_instr_list_c)
       
   965 void *visit(simple_instr_list_c *symbol) {
       
   966   /* A simple_instr_list_c is used to store a list of il operations
       
   967    * being done within parenthesis...
       
   968    *
       
   969    * e.g.:
       
   970    *         LD var1
       
   971    *         AND ( var2
       
   972    *         OR var3
       
   973    *         OR var4
       
   974    *         )
       
   975    *
       
   976    * This will be converted to C++ by defining a new scope
       
   977    * with a new il default variable, and executing the il operands
       
   978    * within this new scope.
       
   979    * At the end of the scope the result, i.e. the value currently stored
       
   980    * in the il default variable is copied to the variable used to take this
       
   981    * value to the outside scope...
       
   982    *
       
   983    * The above example will result in the following C++ code:
       
   984    * {__IL_DEFVAR_T __IL_DEFVAR_BACK;
       
   985    *  __IL_DEFVAR_T __IL_DEFVAR;
       
   986    *
       
   987    *  __IL_DEFVAR.INTvar = var1;
       
   988    *  {
       
   989    *    __IL_DEFVAR_T __IL_DEFVAR;
       
   990    *
       
   991    *    __IL_DEFVAR.INTvar = var2;
       
   992    *    __IL_DEFVAR.INTvar |= var3;
       
   993    *    __IL_DEFVAR.INTvar |= var4;
       
   994    *
       
   995    *    __IL_DEFVAR_BACK = __IL_DEFVAR;
       
   996    *  }
       
   997    *  __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar;
       
   998    *
       
   999    * }
       
  1000    *
       
  1001    *  The intial value of the il default variable (in the above
       
  1002    * example 'var2') is passed to this simple_instr_list_c visitor
       
  1003    * using the il_default_variable_init_value parameter.
       
  1004    * Since it is possible to have parenthesis inside other parenthesis
       
  1005    * recursively, we reset the il_default_variable_init_value to NULL
       
  1006    * as soon as we no longer require it, as it may be used once again
       
  1007    * in the line
       
  1008    *  print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
       
  1009    *
       
  1010    */
       
  1011 
       
  1012   /* Declare the default variable, that will store the result of the IL operations... */
       
  1013   s4o.print("{\n");
       
  1014   s4o.indent_right();
       
  1015 
       
  1016   s4o.print(s4o.indent_spaces);
       
  1017   s4o.print(IL_DEFVAR_T);
       
  1018   s4o.print(" ");
       
  1019   this->default_variable_name.accept(*this);
       
  1020   s4o.print(";\n\n");
       
  1021 
       
  1022   /* Check whether we should initiliase the il default variable... */
       
  1023   if (NULL != this->il_default_variable_init_value) {
       
  1024     /* Yes, we must... */
       
  1025     /* We will do it by instatiating a LD operator, and having this
       
  1026      * same generate_c_il_c class visiting it!
       
  1027      */
       
  1028     LD_operator_c ld_oper;
       
  1029     il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value);
       
  1030 
       
  1031     s4o.print(s4o.indent_spaces);
       
  1032     il_simple_oper.accept(*this);
       
  1033     s4o.print(";\n");
       
  1034   }
       
  1035 
       
  1036   /* this parameter no longer required... */
       
  1037   this->il_default_variable_init_value = NULL;
       
  1038 
       
  1039   print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
       
  1040 
       
  1041   /* copy the result in the default variable to the variable
       
  1042    * used to pass the data out to the scope enclosing
       
  1043    * the current scope!
       
  1044    *
       
  1045    * We also need to update the data type currently stored within
       
  1046    * the variable used to pass the data to the outside scope...
       
  1047    */
       
  1048   this->default_variable_back_name.current_type = this->default_variable_name.current_type;
       
  1049   s4o.print("\n");
       
  1050   s4o.print(s4o.indent_spaces);
       
  1051   this->default_variable_back_name.accept(*this);
       
  1052   s4o.print(" = ");
       
  1053   this->default_variable_name.accept(*this);
       
  1054   s4o.print(";\n");
       
  1055 
       
  1056   s4o.indent_left();
       
  1057   s4o.print(s4o.indent_spaces);
       
  1058   s4o.print("}\n");
       
  1059   s4o.print(s4o.indent_spaces);
       
  1060   return NULL;
       
  1061 }
       
  1062 
       
  1063 /* | il_initial_param_list il_param_instruction */
       
  1064 // SYM_LIST(il_param_list_c)
       
  1065 void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called!
       
  1066 
       
  1067 /*  il_assign_operator il_operand
       
  1068  * | il_assign_operator '(' eol_list simple_instr_list ')'
       
  1069  */
       
  1070 // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused)
       
  1071 void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called!
       
  1072 
       
  1073 /*  il_assign_out_operator variable */
       
  1074 // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable);
       
  1075 void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called!
       
  1076 
       
  1077 /*******************/
       
  1078 /* B 2.2 Operators */
       
  1079 /*******************/
       
  1080 
       
  1081 void *visit(LD_operator_c *symbol)	{
       
  1082   /* the data type resulting from this operation... */
       
  1083   this->default_variable_name.current_type = this->current_operand_type;
       
  1084   XXX_operator(&(this->default_variable_name), " = ", this->current_operand);
       
  1085   return NULL;
       
  1086 }
       
  1087 
       
  1088 void *visit(LDN_operator_c *symbol)	{
       
  1089   /* the data type resulting from this operation... */
       
  1090   this->default_variable_name.current_type = this->current_operand_type;
       
  1091   XXX_operator(&(this->default_variable_name),
       
  1092                search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
       
  1093                this->current_operand);
       
  1094   return NULL;
       
  1095 }
       
  1096 
       
  1097 void *visit(ST_operator_c *symbol)	{
       
  1098   XXX_operator(this->current_operand, " = ",&(this->default_variable_name));
       
  1099   /* the data type resulting from this operation is unchamged. */
       
  1100   return NULL;
       
  1101 }
       
  1102 
       
  1103 void *visit(STN_operator_c *symbol)	{
       
  1104   XXX_operator(this->current_operand,
       
  1105                search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
       
  1106                &(this->default_variable_name));
       
  1107   /* the data type resulting from this operation is unchamged. */
       
  1108   return NULL;
       
  1109 }
       
  1110 
       
  1111 void *visit(NOT_operator_c *symbol)	{
       
  1112   if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR;
       
  1113   XXX_operator(&(this->default_variable_name),
       
  1114                search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~",
       
  1115                &(this->default_variable_name));
       
  1116   /* the data type resulting from this operation is unchanged. */
       
  1117   return NULL;
       
  1118 }
       
  1119 
       
  1120 void *visit(S_operator_c *symbol)	{
       
  1121   if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR;
       
  1122 
       
  1123   C_modifier();
       
  1124   this->current_operand->accept(*this);
       
  1125   s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = true":" = 1");
       
  1126   /* the data type resulting from this operation is unchanged! */
       
  1127   return NULL;
       
  1128 }
       
  1129 
       
  1130 void *visit(R_operator_c *symbol)	{
       
  1131   if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR;
       
  1132 
       
  1133   C_modifier();
       
  1134   this->current_operand->accept(*this);
       
  1135   s4o.print(search_expression_type->is_bool_type(this->current_operand_type)?" = false":" = 0");
       
  1136   /* the data type resulting from this operation is unchanged! */
       
  1137   return NULL;
       
  1138 }
       
  1139 
       
  1140 void *visit(S1_operator_c *symbol)	{return XXX_CAL_operator("S1", this->current_operand);}
       
  1141 void *visit(R1_operator_c *symbol)	{return XXX_CAL_operator("R1", this->current_operand);}
       
  1142 void *visit(CLK_operator_c *symbol)	{return XXX_CAL_operator("CLK", this->current_operand);}
       
  1143 void *visit(CU_operator_c *symbol)	{return XXX_CAL_operator("CU", this->current_operand);}
       
  1144 void *visit(CD_operator_c *symbol)	{return XXX_CAL_operator("CD", this->current_operand);}
       
  1145 void *visit(PV_operator_c *symbol)	{return XXX_CAL_operator("PV", this->current_operand);}
       
  1146 void *visit(IN_operator_c *symbol)	{return XXX_CAL_operator("IN", this->current_operand);}
       
  1147 void *visit(PT_operator_c *symbol)	{return XXX_CAL_operator("PT", this->current_operand);}
       
  1148 
       
  1149 void *visit(AND_operator_c *symbol)	{
       
  1150   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
       
  1151       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1152     XXX_operator(&(this->default_variable_name), " &= ", this->current_operand);
       
  1153     /* the data type resulting from this operation... */
       
  1154     this->default_variable_name.current_type = this->current_operand_type;
       
  1155   }
       
  1156   else {ERROR;}
       
  1157   return NULL;
       
  1158 }
       
  1159 
       
  1160 void *visit(OR_operator_c *symbol)	{
       
  1161   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
       
  1162       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1163     XXX_operator(&(this->default_variable_name), " |= ", this->current_operand);
       
  1164     /* the data type resulting from this operation... */
       
  1165     this->default_variable_name.current_type = this->current_operand_type;
       
  1166   }
       
  1167   else {ERROR;}
       
  1168   return NULL;
       
  1169 }
       
  1170 
       
  1171 void *visit(XOR_operator_c *symbol)	{
       
  1172   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
       
  1173       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1174     // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types!
       
  1175     XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand);
       
  1176     /* the data type resulting from this operation... */
       
  1177     this->default_variable_name.current_type = this->current_operand_type;
       
  1178   }
       
  1179   else {ERROR;}
       
  1180   return NULL;
       
  1181 }
       
  1182 
       
  1183 void *visit(ANDN_operator_c *symbol)	{
       
  1184   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
       
  1185       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1186     XXX_operator(&(this->default_variable_name),
       
  1187                  search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~",
       
  1188                  this->current_operand);
       
  1189     /* the data type resulting from this operation... */
       
  1190     this->default_variable_name.current_type = this->current_operand_type;
       
  1191   }
       
  1192   else {ERROR;}
       
  1193   return NULL;
       
  1194 }
       
  1195 
       
  1196 void *visit(ORN_operator_c *symbol)	{
       
  1197   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
       
  1198       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1199     XXX_operator(&(this->default_variable_name),
       
  1200                  search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~",
       
  1201                  this->current_operand);
       
  1202     /* the data type resulting from this operation... */
       
  1203     this->default_variable_name.current_type = this->current_operand_type;
       
  1204   }
       
  1205   else {ERROR;}
       
  1206   return NULL;
       
  1207 }
       
  1208 
       
  1209 void *visit(XORN_operator_c *symbol)	{
       
  1210   if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
       
  1211       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1212     XXX_operator(&(this->default_variable_name),
       
  1213                  // bit by bit exclusive OR !! Also seems to work with boolean types!
       
  1214                  search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~",
       
  1215                  this->current_operand);
       
  1216     /* the data type resulting from this operation... */
       
  1217     this->default_variable_name.current_type = this->current_operand_type;
       
  1218   }
       
  1219   else {ERROR;}
       
  1220   return NULL;
       
  1221 }
       
  1222 
       
  1223 void *visit(ADD_operator_c *symbol)	{
       
  1224   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
       
  1225       search_expression_type->is_time_type(this->current_operand_type)) {
       
  1226     XXX_function("__time_add", &(this->default_variable_name), this->current_operand);
       
  1227     /* the data type resulting from this operation... */
       
  1228     this->default_variable_name.current_type = this->current_operand_type;
       
  1229     return NULL;
       
  1230   }
       
  1231   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
  1232       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1233     XXX_operator(&(this->default_variable_name), " += ", this->current_operand);
       
  1234     /* the data type resulting from this operation... */
       
  1235     this->default_variable_name.current_type = this->current_operand_type;
       
  1236     return NULL;
       
  1237   }
       
  1238   ERROR;
       
  1239   return NULL;
       
  1240 }
       
  1241 
       
  1242 void *visit(SUB_operator_c *symbol)	{
       
  1243   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
       
  1244       search_expression_type->is_time_type(this->current_operand_type)) {
       
  1245     XXX_function("__time_sub", &(this->default_variable_name), this->current_operand);
       
  1246     /* the data type resulting from this operation... */
       
  1247     this->default_variable_name.current_type = this->current_operand_type;
       
  1248     return NULL;
       
  1249   }
       
  1250   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
  1251       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1252     XXX_operator(&(this->default_variable_name), " -= ", this->current_operand);
       
  1253     /* the data type resulting from this operation... */
       
  1254     this->default_variable_name.current_type = this->current_operand_type;
       
  1255     return NULL;
       
  1256   }
       
  1257   ERROR;
       
  1258   return NULL;
       
  1259 }
       
  1260 
       
  1261 void *visit(MUL_operator_c *symbol)	{
       
  1262   if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
       
  1263       search_expression_type->is_integer_type(this->current_operand_type)) {
       
  1264     XXX_function("__time_mul", &(this->default_variable_name), this->current_operand);
       
  1265     /* the data type resulting from this operation... */
       
  1266     this->default_variable_name.current_type = this->current_operand_type;
       
  1267     return NULL;
       
  1268   }
       
  1269   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
  1270       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1271     XXX_operator(&(this->default_variable_name), " *= ", this->current_operand);
       
  1272     /* the data type resulting from this operation... */
       
  1273     this->default_variable_name.current_type = this->current_operand_type;
       
  1274     return NULL;
       
  1275   }
       
  1276   ERROR;
       
  1277   return NULL;
       
  1278 }
       
  1279 
       
  1280 void *visit(DIV_operator_c *symbol)	{
       
  1281   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
  1282       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1283     XXX_operator(&(this->default_variable_name), " /= ", this->current_operand);
       
  1284     /* the data type resulting from this operation... */
       
  1285     this->default_variable_name.current_type = this->current_operand_type;
       
  1286   }
       
  1287   else {ERROR;}
       
  1288   return NULL;
       
  1289 }
       
  1290 
       
  1291 void *visit(MOD_operator_c *symbol)	{
       
  1292   if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
       
  1293       search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
       
  1294     XXX_operator(&(this->default_variable_name), " %= ", this->current_operand);
       
  1295     /* the data type resulting from this operation... */
       
  1296     this->default_variable_name.current_type = this->current_operand_type;
       
  1297   }
       
  1298   else {ERROR;}
       
  1299   return NULL;
       
  1300 }
       
  1301 
       
  1302 void *visit(GT_operator_c *symbol)	{
       
  1303   return CMP_operator(this->current_operand, "__gt_");
       
  1304 }
       
  1305 
       
  1306 void *visit(GE_operator_c *symbol)	{
       
  1307   return CMP_operator(this->current_operand, "__ge_");
       
  1308 }
       
  1309 
       
  1310 void *visit(EQ_operator_c *symbol)	{
       
  1311   return CMP_operator(this->current_operand, "__eq_");
       
  1312 }
       
  1313 
       
  1314 void *visit(LT_operator_c *symbol)	{
       
  1315   return CMP_operator(this->current_operand, "__lt_");
       
  1316 }
       
  1317 
       
  1318 void *visit(LE_operator_c *symbol)	{
       
  1319   return CMP_operator(this->current_operand, "__le_");
       
  1320 }
       
  1321 
       
  1322 void *visit(NE_operator_c *symbol)	{
       
  1323   return CMP_operator(this->current_operand, "__ne_");
       
  1324 }
       
  1325 
       
  1326 
       
  1327 //SYM_REF0(CAL_operator_c)
       
  1328 // This method will be called from within the il_fb_call_c visitor method
       
  1329 void *visit(CAL_operator_c *symbol) {return NULL;}
       
  1330 
       
  1331 //SYM_REF0(CALC_operator_c)
       
  1332 // This method will be called from within the il_fb_call_c visitor method
       
  1333 void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;}
       
  1334 
       
  1335 //SYM_REF0(CALCN_operator_c)
       
  1336 // This method will be called from within the il_fb_call_c visitor method
       
  1337 void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;}
       
  1338 
       
  1339 /* NOTE: The semantics of the RET operator requires us to return a value
       
  1340  *       if the IL code is inside a function, but simply return no value if
       
  1341  *       the IL code is inside a function block or program!
       
  1342  *       Nevertheless, it is the generate_c_c class itself that
       
  1343  *       introduces the 'reaturn <value>' into the c++ code at the end
       
  1344  *       of every function. This class does not know whether the IL code
       
  1345  *       is inside a function or a function block.
       
  1346  *       We work around this by jumping to the end of the code,
       
  1347  *       that will be marked by the END_LABEL label in the
       
  1348  *       instruction_list_c visitor...
       
  1349  */
       
  1350 // SYM_REF0(RET_operator_c)
       
  1351 void *visit(RET_operator_c *symbol) {
       
  1352   s4o.print("goto ");s4o.print(END_LABEL);
       
  1353   return NULL;
       
  1354 }
       
  1355 
       
  1356 // SYM_REF0(RETC_operator_c)
       
  1357 void *visit(RETC_operator_c *symbol) {
       
  1358   C_modifier();
       
  1359   s4o.print("goto ");s4o.print(END_LABEL);
       
  1360   return NULL;
       
  1361 }
       
  1362 
       
  1363 // SYM_REF0(RETCN_operator_c)
       
  1364 void *visit(RETCN_operator_c *symbol) {
       
  1365   CN_modifier();
       
  1366   s4o.print("goto ");s4o.print(END_LABEL);
       
  1367   return NULL;
       
  1368 }
       
  1369 
       
  1370 //SYM_REF0(JMP_operator_c)
       
  1371 void *visit(JMP_operator_c *symbol)	{
       
  1372   if (NULL == this->jump_label) ERROR;
       
  1373 
       
  1374   s4o.print("goto ");
       
  1375   this->jump_label->accept(*this);
       
  1376   /* the data type resulting from this operation is unchanged! */
       
  1377   return NULL;
       
  1378 }
       
  1379 
       
  1380 // SYM_REF0(JMPC_operator_c)
       
  1381 void *visit(JMPC_operator_c *symbol)	{
       
  1382   if (NULL == this->jump_label) ERROR;
       
  1383 
       
  1384   C_modifier();
       
  1385   s4o.print("goto ");
       
  1386   this->jump_label->accept(*this);
       
  1387   /* the data type resulting from this operation is unchanged! */
       
  1388   return NULL;
       
  1389 }
       
  1390 
       
  1391 // SYM_REF0(JMPCN_operator_c)
       
  1392 void *visit(JMPCN_operator_c *symbol)	{
       
  1393   if (NULL == this->jump_label) ERROR;
       
  1394 
       
  1395   CN_modifier();
       
  1396   s4o.print("goto ");
       
  1397   this->jump_label->accept(*this);
       
  1398   /* the data type resulting from this operation is unchanged! */
       
  1399   return NULL;
       
  1400 }
       
  1401 
       
  1402 #if 0
       
  1403 /*| [NOT] any_identifier SENDTO */
       
  1404 SYM_REF2(il_assign_out_operator_c, option, variable_name)
       
  1405 #endif
       
  1406 
       
  1407 }; /* generate_c_il_c */
       
  1408 
       
  1409 
       
  1410 
       
  1411 
       
  1412 
       
  1413 
       
  1414 
       
  1415 
       
  1416 
       
  1417 /* The implementation of the single visit() member function
       
  1418  * of il_default_variable_c.
       
  1419  * It can only come after the full declaration of
       
  1420  * generate_c_il_c. Since we define and declare
       
  1421  * generate_c_il_c simultaneously, it can only come
       
  1422  * after the definition...
       
  1423  */
       
  1424 void *il_default_variable_c::accept(visitor_c &visitor) {
       
  1425   /* An ugly hack!! */
       
  1426   /* This is required because we need to over-ride the base
       
  1427    * accept(visitor_c &) method of the class symbol_c,
       
  1428    * so this method may be called through a symbol_c *
       
  1429    * reference!
       
  1430    *
       
  1431    * But, the visitor_c does not include a visitor to
       
  1432    * an il_default_variable_c, which means that we couldn't
       
  1433    * simply call visitor.visit(this);
       
  1434    *
       
  1435    * We therefore need to use the dynamic_cast hack!!
       
  1436    *
       
  1437    * Note too that we can't cast a visitor_c to a
       
  1438    * il_default_variable_visitor_c, since they are not related.
       
  1439    * Nor may the il_default_variable_visitor_c inherit from
       
  1440    * visitor_c, because then generate_c_il_c would contain
       
  1441    * two visitor_c base classes, one each through
       
  1442    * il_default_variable_visitor_c and generate_c_type_c
       
  1443    *
       
  1444    * We could use virtual inheritance of the visitor_c, but it
       
  1445    * would probably create more problems than it is worth!
       
  1446    */
       
  1447   generate_c_il_c *v;
       
  1448   v = dynamic_cast<generate_c_il_c *>(&visitor);
       
  1449   if (v == NULL) ERROR;
       
  1450 
       
  1451   return v->visit(this);
       
  1452 }
       
  1453 
       
  1454 
       
  1455 
       
  1456 
       
  1457 il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) {
       
  1458   if (NULL == var_name_str) ERROR;
       
  1459   /* Note: current_type may start off with NULL */
       
  1460 
       
  1461   this->var_name = new identifier_c(var_name_str);
       
  1462   if (NULL == this->var_name) ERROR;
       
  1463 
       
  1464   this->current_type = current_type;
       
  1465 }