stage4/generate_c/generate_c_st.cc
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 26 Apr 2024 09:27:21 +0200
changeset 1105 8128cad6a6e3
parent 1098 84bbafb4fb26
permissions -rw-r--r--
merge
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
     1
/*
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
     3
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
     4
 *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
     6
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
     7
 *  This program is free software: you can redistribute it and/or modify
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
     8
 *  it under the terms of the GNU General Public License as published by
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
     9
 *  the Free Software Foundation, either version 3 of the License, or
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    10
 *  (at your option) any later version.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    11
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    12
 *  This program is distributed in the hope that it will be useful,
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    15
 *  GNU General Public License for more details.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    16
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    17
 *  You should have received a copy of the GNU General Public License
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    19
 *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    20
 * This code is made available on the understanding that it will not be
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    21
 * used in safety-critical situations without a full and competent review.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    22
 *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    23
 * Based on the
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    24
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    25
 *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    26
 */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    27
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    28
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    29
/*
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    30
 * Conversion of st statements (i.e. ST code).
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    31
 *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    32
 * This is part of the 4th stage that generates
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 258
diff changeset
    33
 * a C source program equivalent to the IL and ST, or SFC
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    34
 * code.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    35
 */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    36
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    37
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
    38
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
    39
#include "../../util/strdup.hh"
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
    40
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    41
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    42
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    43
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    44
/***********************************************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    45
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    46
945
477393b00f95 Add support for relaxed datatype model for array datatypes.
mjsousa
parents: 943
diff changeset
    47
class generate_c_st_c: public generate_c_base_and_typeid_c {
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    48
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    49
  public:
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    50
    typedef enum {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    51
      expression_vg,
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
    52
      complextype_base_vg,
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
    53
      complextype_suffix_vg,
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    54
      fparam_output_vg
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    55
    } variablegeneration_t;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    56
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    57
  private:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    58
    /* When calling a function block, we must first find it's type,
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    59
     * by searching through the declarations of the variables currently
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    60
     * in scope.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    61
     * This class does just that...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    62
     * A new class is instantiated whenever we begin generating the code
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    63
     * for a function block type declaration, or a program declaration.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    64
     * This object instance will then later be called while the
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    65
     * function block's or the program's body is being handled.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    66
     *
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    67
     * Note that functions cannot contain calls to function blocks,
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    68
     * so we do not create an object instance when handling
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    69
     * a function declaration.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    70
     */
863
06820d03a433 Fix bug: correctly generate code when accessing external variables declared inside FBs as a structured variable (realvar := fb1.fb2.extvar1.realvar)
Mario de Sousa <msousa@fe.up.pt>
parents: 854
diff changeset
    71
    search_fb_instance_decl_c    *search_fb_instance_decl;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    72
    search_varfb_instance_type_c *search_varfb_instance_type;
505
21be0f2f242d Removing code from search_varfb_instance_type_c (use search_var_instance_decl_c instead).
Mario de Sousa <msousa@fe.up.pt>
parents: 503
diff changeset
    73
    search_var_instance_decl_c   *search_var_instance_decl;
863
06820d03a433 Fix bug: correctly generate code when accessing external variables declared inside FBs as a structured variable (realvar := fb1.fb2.extvar1.realvar)
Mario de Sousa <msousa@fe.up.pt>
parents: 854
diff changeset
    74
    
06820d03a433 Fix bug: correctly generate code when accessing external variables declared inside FBs as a structured variable (realvar := fb1.fb2.extvar1.realvar)
Mario de Sousa <msousa@fe.up.pt>
parents: 854
diff changeset
    75
    symbol_c *scope_;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    76
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    77
    symbol_c* current_array_type;
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
    78
    symbol_c* current_param_type;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    79
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    80
    int fcall_number;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    81
    symbol_c *fbname;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    82
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
    83
    bool first_subrange_case_list;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
    84
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    85
    variablegeneration_t wanted_variablegeneration;
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 130
diff changeset
    86
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    87
  public:
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    88
    generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
945
477393b00f95 Add support for relaxed datatype model for array datatypes.
mjsousa
parents: 943
diff changeset
    89
    : generate_c_base_and_typeid_c(s4o_ptr) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
    90
      search_fb_instance_decl    = new search_fb_instance_decl_c   (scope);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    91
      search_varfb_instance_type = new search_varfb_instance_type_c(scope);
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
    92
      search_var_instance_decl   = new search_var_instance_decl_c  (scope);
863
06820d03a433 Fix bug: correctly generate code when accessing external variables declared inside FBs as a structured variable (realvar := fb1.fb2.extvar1.realvar)
Mario de Sousa <msousa@fe.up.pt>
parents: 854
diff changeset
    93
      scope_ = scope;
505
21be0f2f242d Removing code from search_varfb_instance_type_c (use search_var_instance_decl_c instead).
Mario de Sousa <msousa@fe.up.pt>
parents: 503
diff changeset
    94
      
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
    95
      this->set_variable_prefix(variable_prefix);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
    96
      current_array_type = NULL;
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
    97
      current_param_type = NULL;
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    98
      fcall_number = 0;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
    99
      fbname = name;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   100
      wanted_variablegeneration = expression_vg;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   101
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   102
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   103
    virtual ~generate_c_st_c(void) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   104
      delete search_fb_instance_decl;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   105
      delete search_varfb_instance_type;
505
21be0f2f242d Removing code from search_varfb_instance_type_c (use search_var_instance_decl_c instead).
Mario de Sousa <msousa@fe.up.pt>
parents: 503
diff changeset
   106
      delete search_var_instance_decl;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   107
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   108
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   109
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   110
  public:
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   111
    void generate(statement_list_c *stl) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   112
      stl->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   113
    }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   114
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   115
  private:
531
e7d6f28fc882 Make temporary fix to is_complex() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 505
diff changeset
   116
    
e7d6f28fc882 Make temporary fix to is_complex() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 505
diff changeset
   117
    
e7d6f28fc882 Make temporary fix to is_complex() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 505
diff changeset
   118
e7d6f28fc882 Make temporary fix to is_complex() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 505
diff changeset
   119
e7d6f28fc882 Make temporary fix to is_complex() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 505
diff changeset
   120
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   121
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   122
void *print_getter(symbol_c *symbol) {
863
06820d03a433 Fix bug: correctly generate code when accessing external variables declared inside FBs as a structured variable (realvar := fb1.fb2.extvar1.realvar)
Mario de Sousa <msousa@fe.up.pt>
parents: 854
diff changeset
   123
  unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol, scope_);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   124
  if (wanted_variablegeneration == fparam_output_vg) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   125
    if (vartype == search_var_instance_decl_c::external_vt) {
854
13d0b67de111 Code cleanup: move datatype analysis to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 852
diff changeset
   126
      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
13d0b67de111 Code cleanup: move datatype analysis to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 852
diff changeset
   127
      if ( get_datatype_info_c::is_function_block(symbol->datatype))
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   128
        s4o.print(GET_EXTERNAL_FB_BY_REF);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   129
      else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   130
        s4o.print(GET_EXTERNAL_BY_REF);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   131
    }
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   132
    else if (vartype == search_var_instance_decl_c::located_vt)
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   133
      s4o.print(GET_LOCATED_BY_REF);
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   134
    else
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   135
      s4o.print(GET_VAR_BY_REF);
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   136
  }
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   137
  else {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   138
    if (vartype == search_var_instance_decl_c::external_vt) {
854
13d0b67de111 Code cleanup: move datatype analysis to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 852
diff changeset
   139
      if (!get_datatype_info_c::is_type_valid    (symbol->datatype)) ERROR;
13d0b67de111 Code cleanup: move datatype analysis to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 852
diff changeset
   140
      if ( get_datatype_info_c::is_function_block(symbol->datatype))
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   141
        s4o.print(GET_EXTERNAL_FB);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   142
      else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   143
        s4o.print(GET_EXTERNAL);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   144
    }
503
e1e7c7678c44 Edit formating and delete unused code.
Mario de Sousa <msousa@fe.up.pt>
parents: 498
diff changeset
   145
    else if (vartype == search_var_instance_decl_c::located_vt)
e1e7c7678c44 Edit formating and delete unused code.
Mario de Sousa <msousa@fe.up.pt>
parents: 498
diff changeset
   146
      s4o.print(GET_LOCATED);
e1e7c7678c44 Edit formating and delete unused code.
Mario de Sousa <msousa@fe.up.pt>
parents: 498
diff changeset
   147
    else
e1e7c7678c44 Edit formating and delete unused code.
Mario de Sousa <msousa@fe.up.pt>
parents: 498
diff changeset
   148
      s4o.print(GET_VAR);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   149
  }
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   150
  
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   151
  variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   152
  s4o.print("(");
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   153
  print_variable_prefix();  
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   154
  wanted_variablegeneration = complextype_base_vg;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   155
  symbol->accept(*this);
851
2c59c2b8fca4 Fix bug/issue #33 (correctly access struct/array variables declared inside a FB -> r:=FB1.FB2.struct1.array1[3] )
Mario de Sousa <msousa@fe.up.pt>
parents: 800
diff changeset
   156
  s4o.print(",");
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   157
  wanted_variablegeneration = complextype_suffix_vg;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   158
  symbol->accept(*this);
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   159
  s4o.print(")");
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   160
  wanted_variablegeneration = old_wanted_variablegeneration;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   161
  return NULL;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   162
}
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   163
852
efb44e892582 Fix bug: allow use, as lvalues, structures/arrays inside FBs (e.g. fb1.struct1.r := 33.3).
Mario de Sousa <msousa@fe.up.pt>
parents: 851
diff changeset
   164
efb44e892582 Fix bug: allow use, as lvalues, structures/arrays inside FBs (e.g. fb1.struct1.r := 33.3).
Mario de Sousa <msousa@fe.up.pt>
parents: 851
diff changeset
   165
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   166
void *print_setter(symbol_c* symbol,
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   167
        symbol_c* type,
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   168
        symbol_c* value,
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   169
        symbol_c* fb_symbol = NULL,
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   170
        symbol_c* fb_value = NULL) {
888
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   171
 
1095
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   172
  unsigned int vartype;
405
7b5d67d1aeef Fix bug wrong code generated when, in a pou, a located variable has the same name than a function block instance input variable
laurent
parents: 399
diff changeset
   173
  if (fb_symbol == NULL) {
1095
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   174
    vartype = analyse_variable_c::first_nonfb_vardecltype(symbol, scope_);
889
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   175
    symbol_c *first_nonfb = analyse_variable_c::find_first_nonfb(symbol);
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   176
    if (first_nonfb == NULL) ERROR;
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   177
    if (vartype == search_var_instance_decl_c::external_vt) {
889
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   178
      if (!get_datatype_info_c::is_type_valid    (first_nonfb->datatype)) ERROR;
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   179
      if ( get_datatype_info_c::is_function_block(first_nonfb->datatype)) // handle situation where we are copying a complete fb -> fb1.fb2.fb3 := fb4 (and fb3 is external!)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   180
        s4o.print(SET_EXTERNAL_FB);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   181
      else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   182
        s4o.print(SET_EXTERNAL);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   183
    }
405
7b5d67d1aeef Fix bug wrong code generated when, in a pou, a located variable has the same name than a function block instance input variable
laurent
parents: 399
diff changeset
   184
    else if (vartype == search_var_instance_decl_c::located_vt)
7b5d67d1aeef Fix bug wrong code generated when, in a pou, a located variable has the same name than a function block instance input variable
laurent
parents: 399
diff changeset
   185
      s4o.print(SET_LOCATED);
7b5d67d1aeef Fix bug wrong code generated when, in a pou, a located variable has the same name than a function block instance input variable
laurent
parents: 399
diff changeset
   186
    else
7b5d67d1aeef Fix bug wrong code generated when, in a pou, a located variable has the same name than a function block instance input variable
laurent
parents: 399
diff changeset
   187
      s4o.print(SET_VAR);
7b5d67d1aeef Fix bug wrong code generated when, in a pou, a located variable has the same name than a function block instance input variable
laurent
parents: 399
diff changeset
   188
  }
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   189
  else {
1095
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   190
    vartype = search_var_instance_decl->get_vartype(fb_symbol);
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   191
    if (vartype == search_var_instance_decl_c::external_vt)
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   192
      s4o.print(SET_EXTERNAL_FB);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   193
    else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   194
      s4o.print(SET_VAR);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   195
  }
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 387
diff changeset
   196
  s4o.print("(");
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   197
  
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   198
  if (fb_symbol != NULL) {
1095
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   199
    if (vartype == search_var_instance_decl_c::external_vt){
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   200
        print_variable_prefix();
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   201
        s4o.print(",");
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   202
        fb_symbol->accept(*this);
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   203
        s4o.print("->");
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   204
        symbol->accept(*this);
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   205
    }else{
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   206
        print_variable_prefix();
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   207
        // It is my (MJS) conviction that by this time the following will always be true...
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   208
        //   wanted_variablegeneration == expression_vg;
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   209
        fb_symbol->accept(*this);
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   210
        s4o.print(".,");
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   211
        symbol->accept(*this);
36bbd0b64816 Fixed __SET_EXTERNAL_FB declaration in accessor.h, invoked when accessing EXTERNAL_BLOCK.VARIABLE. Macro was invoking __SET_VAR with wrong parameter count. Fixed corresponding code generator for ST. Seems similar codegen for IL is also broken, but fix would affect other (unknown) behviours -> didn't touch it.
Edouard Tisserant
parents: 1076
diff changeset
   212
    }
888
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   213
    s4o.print(",");
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   214
    s4o.print(",");    
897
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   215
  } else {
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   216
    print_variable_prefix();
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   217
    s4o.print(",");    
888
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   218
    wanted_variablegeneration = complextype_base_vg;
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   219
    symbol->accept(*this);
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   220
    s4o.print(",");
885
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   221
    wanted_variablegeneration = complextype_suffix_vg;
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   222
    symbol->accept(*this);
888
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   223
    s4o.print(",");
4893e6b11b25 Remove call to type_is_complex() in ST code generation.
mjsousa
parents: 887
diff changeset
   224
  }
885
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   225
  wanted_variablegeneration = expression_vg;
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   226
  print_check_function(type, value, fb_value);
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   227
  s4o.print(")");
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   228
  wanted_variablegeneration = expression_vg;
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 877
diff changeset
   229
  return NULL;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   230
}
146
eef5e62048c7 Adding support for EN/ENO params in function and function blocks (standard function not supported yet)
lbessard
parents: 145
diff changeset
   231
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   232
/********************************/
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   233
/* B 1.3.3 - Derived data types */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   234
/********************************/
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   235
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   236
/*  signed_integer DOTDOT signed_integer */
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   237
void *visit(subrange_c *symbol) {
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
   238
  symbol->lower_limit->accept(*this);
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   239
  return NULL;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   240
}
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
   241
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   242
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   243
void *visit(array_specification_c *symbol) {
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   244
  symbol->non_generic_type_name->accept(*this);
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   245
  return NULL;
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   246
}
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   247
800
2481fffe6e58 Fixed bug in case statement with enumerated type variable as case expression
Laurent Bessard
parents: 793
diff changeset
   248
/*  enumerated_type_name ':' enumerated_spec_init */
2481fffe6e58 Fixed bug in case statement with enumerated type variable as case expression
Laurent Bessard
parents: 793
diff changeset
   249
void *visit(enumerated_type_declaration_c *symbol) {
2481fffe6e58 Fixed bug in case statement with enumerated type variable as case expression
Laurent Bessard
parents: 793
diff changeset
   250
  symbol->enumerated_type_name->accept(*this);
2481fffe6e58 Fixed bug in case statement with enumerated type variable as case expression
Laurent Bessard
parents: 793
diff changeset
   251
  return NULL;
2481fffe6e58 Fixed bug in case statement with enumerated type variable as case expression
Laurent Bessard
parents: 793
diff changeset
   252
}
2481fffe6e58 Fixed bug in case statement with enumerated type variable as case expression
Laurent Bessard
parents: 793
diff changeset
   253
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   254
/*********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   255
/* B 1.4 - Variables */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   256
/*********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   257
void *visit(symbolic_variable_c *symbol) {
382
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   258
  switch (wanted_variablegeneration) {
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   259
    case complextype_base_vg:
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   260
      symbol->var_name->accept(*this); //generate_c_base_c::visit(symbol);
382
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   261
      break;
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   262
    case complextype_suffix_vg:
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   263
      break;
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   264
    default:
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   265
      if (this->is_variable_prefix_null()) {
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   266
        if (wanted_variablegeneration == fparam_output_vg) {
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   267
          s4o.print("&(");
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   268
          generate_c_base_c::visit(symbol);
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   269
          s4o.print(")");
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   270
        }
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   271
        else {
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   272
          generate_c_base_c::visit(symbol);
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   273
        }
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   274
      }
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   275
      else
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   276
        print_getter(symbol);
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   277
      break;
ac6dfec701c9 Fix bug in parser while trying to use IL operator like S1, R1, etc... as standard function block interface variable in structured_variable syntax and bug in code generator while generating code for assignment of function block interface variable using structured_variable syntax
laurent
parents: 377
diff changeset
   278
  }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   279
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   280
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   281
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   282
/********************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   283
/* B.1.4.1   Directly Represented Variables */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   284
/********************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   285
// direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   286
void *visit(direct_variable_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   287
  TRACE("direct_variable_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   288
  /* Do not use print_token() as it will change everything into uppercase */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   289
  if (strlen(symbol->value) == 0) ERROR;
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   290
  if (this->is_variable_prefix_null()) {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   291
    if (wanted_variablegeneration != fparam_output_vg)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   292
      s4o.print("*(");
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   293
  }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   294
  else {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   295
    switch (wanted_variablegeneration) {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   296
      case expression_vg:
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   297
        s4o.print(GET_LOCATED);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   298
        s4o.print("(");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   299
        break;
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   300
      case fparam_output_vg:
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   301
        s4o.print(GET_LOCATED_BY_REF);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   302
        s4o.print("(");
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   303
        break;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   304
      default:
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   305
        break;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   306
    }
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 130
diff changeset
   307
  }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   308
  this->print_variable_prefix();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   309
  s4o.printlocation(symbol->value + 1);
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   310
  if (( this->is_variable_prefix_null() && (wanted_variablegeneration != fparam_output_vg)) ||
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   311
      (!this->is_variable_prefix_null() && (wanted_variablegeneration == expression_vg   )) ||
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   312
      (!this->is_variable_prefix_null() && (wanted_variablegeneration == fparam_output_vg)))
145
72ae82e65dbc Adding support for ouput variables in functions
lbessard
parents: 130
diff changeset
   313
    s4o.print(")");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   314
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   315
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   316
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   317
/*************************************/
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   318
/* B.1.4.2   Multi-element Variables */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   319
/*************************************/
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   320
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   321
// SYM_REF2(structured_variable_c, record_variable, field_selector)
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   322
void *visit(structured_variable_c *symbol) {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   323
  TRACE("structured_variable_c");
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   324
  switch (wanted_variablegeneration) {
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   325
    case complextype_base_vg:
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   326
      symbol->record_variable->accept(*this);
897
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   327
      /* NOTE: The following test includes a special case for SFC Steps. They are currently mapped onto a C data structure
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   328
       *        that does not follow the standard IEC_<typename> pattern used for user defined structure datatypes 
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   329
       *        (i.e. it does not include the 'values' and 'flag' structure 
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   330
       *        elements that are tested by __GET_VAR and __SET_VAR acessor macros defined in acessor.h). However, the
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   331
       *        STEP.T and STEP.X elements of this step structure are of the IEC_BOOL and IEC_TIME datatypes, and are 
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   332
       *        actually structures that do have the 'value' and 'flags' elements. So, it is safe to say that any  variable
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   333
       *        that is a STEPname is not of a complex type, as its .T and .X elements are and can later be safely accessed
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   334
       *        using the __SET_VAR and __GET_VAR macros.
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   335
       * 
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   336
       *        For the above reason, a STEP must be handled as a FB, i.e. it does NOT contain the 'flags' and 'value' elements!
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   337
       */
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   338
      if (   get_datatype_info_c::is_function_block(symbol->record_variable->datatype)
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   339
          || get_datatype_info_c::is_sfc_step      (symbol->record_variable->datatype)) {
889
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   340
        if (NULL == symbol->record_variable->scope) ERROR;
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   341
        search_var_instance_decl_c search_var_instance_decl(symbol->record_variable->scope);
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   342
        if      (search_var_instance_decl_c::external_vt == search_var_instance_decl.get_vartype(get_var_name_c::get_last_field(symbol->record_variable)))
943
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   343
          s4o.print("->");
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   344
        else if (dynamic_cast<deref_operator_c *>(symbol->record_variable) != NULL)
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   345
          s4o.print("->"); /* please read the comment in visit(deref_operator_c *) tio understand what this line is doing! */
889
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   346
        else  
5f380b99e95e Correctly handle structured variables that include FB and external FBs (example: FB1.FB2.extFB3.FB4.extFB5.extFB6.structvar.field1 := 42;) (this commit fixes only ST code. TODO: IL and SFC)
mjsousa
parents: 888
diff changeset
   347
          s4o.print(".");
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   348
        symbol->field_selector->accept(*this);
410
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 406
diff changeset
   349
      }
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   350
      break;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   351
    case complextype_suffix_vg:
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   352
      symbol->record_variable->accept(*this);
897
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   353
      // the following condition MUST be a negation of the above condition used in the 'case complextype_base_vg:'
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   354
      if (!(   get_datatype_info_c::is_function_block(symbol->record_variable->datatype)     // if the record variable is not a FB... 
8c3e91c385f5 Add support for Stepname.X syntax.
mjsousa
parents: 889
diff changeset
   355
            || get_datatype_info_c::is_sfc_step      (symbol->record_variable->datatype))) { // ...nor an SFC step name, then it will certainly be a structure!
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   356
        if (dynamic_cast<deref_operator_c *>(symbol->record_variable) != NULL)
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   357
          s4o.print("->"); /* please read the comment in visit(deref_operator_c *) tio understand what this line is doing! */
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   358
        else
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   359
          s4o.print(".");
410
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 406
diff changeset
   360
        symbol->field_selector->accept(*this);
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 406
diff changeset
   361
      }
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   362
      break;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   363
    default:
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   364
      if (this->is_variable_prefix_null()) {
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   365
        /* We are writing code for a FUNCTION. In this case, deref_operator_c are not transformed into the C pointer derefence syntax '->' (e.g. ptr->elem).
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   366
         * We use instead the '*' syntax (e.g. (*ptr).elem)
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   367
         * While in FB the '->' is generated by this structured_variable_c visitor, in Functions the '*' syntax is generated by the deref_operator_c visitor
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   368
         * This is why here we do NOT have --> {if (dynamic_cast<deref_operator_c *>(symbol->record_variable) != NULL)  ..}
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   369
	 *  
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   370
	 * please read the comment in visit(deref_operator_c *) for more information!
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   371
         */
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   372
        symbol->record_variable->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   373
        s4o.print(".");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   374
        symbol->field_selector->accept(*this);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   375
      }
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   376
      else
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   377
        print_getter(symbol);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   378
      break;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   379
  }
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   380
  return NULL;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   381
}
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 220
diff changeset
   382
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   383
/*  subscripted_variable '[' subscript_list ']' */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   384
//SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   385
void *visit(array_variable_c *symbol) {
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   386
  switch (wanted_variablegeneration) {
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   387
    case complextype_base_vg:
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   388
      symbol->subscripted_variable->accept(*this);
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   389
      break;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   390
    case complextype_suffix_vg:
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   391
      symbol->subscripted_variable->accept(*this);
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   392
417
d48f53715f77 Re-write of stage3 code (done by Manuele and I), re-write of search_varfb_instance_type_c (done by myself), and several other bug fixes.
Mario de Sousa <msousa@fe.up.pt>
parents: 392
diff changeset
   393
      current_array_type = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   394
      if (current_array_type == NULL) ERROR;
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   395
943
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   396
      if (dynamic_cast<deref_operator_c *>(symbol->subscripted_variable) != NULL)
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   397
        s4o.print("->"); /* please read the comment in visit(deref_operator_c *) tio understand what this line is doing! */
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   398
      else
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   399
        s4o.print(".");
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   400
      s4o.print("table");
298
022a874149ce Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents: 283
diff changeset
   401
      wanted_variablegeneration = expression_vg;
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   402
      symbol->subscript_list->accept(*this);
298
022a874149ce Fixing code generation of array indexes in Function Blocks.
Mario de Sousa <msousa@fe.up.pt>
parents: 283
diff changeset
   403
      wanted_variablegeneration = complextype_suffix_vg;
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   404
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   405
      current_array_type = NULL;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   406
      break;
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   407
    default:
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   408
      if (this->is_variable_prefix_null()) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   409
        symbol->subscripted_variable->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   410
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   411
        current_array_type = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   412
        if (current_array_type == NULL) ERROR;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   413
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   414
        s4o.print(".table");
238
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   415
        symbol->subscript_list->accept(*this);
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   416
0919986a5c98 Bug when trying to get type of elements in a big complex structure fixed
laurent
parents: 237
diff changeset
   417
        current_array_type = NULL;
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   418
      }
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   419
      else
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   420
        print_getter(symbol);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   421
      break;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   422
  }
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   423
  return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   424
}
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   425
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   426
/* subscript_list ',' subscript */
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   427
void *visit(subscript_list_c *symbol) {
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   428
  array_dimension_iterator_c* array_dimension_iterator = new array_dimension_iterator_c(current_array_type);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   429
  for (int i =  0; i < symbol->n; i++) {
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   430
    symbol_c* dimension = array_dimension_iterator->next();
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   431
    if (dimension == NULL) ERROR;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   432
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   433
    s4o.print("[(");
1041
56ebe2a31b5b Access elements[] in list_c through a new get_element() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
   434
    symbol->get_element(i)->accept(*this);
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   435
    s4o.print(") - (");
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   436
    dimension->accept(*this);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   437
    s4o.print(")]");
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   438
  }
377
60b012b7793f Adding support for compiling direct array specification inside variable declaration
laurent
parents: 376
diff changeset
   439
  delete array_dimension_iterator;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   440
  return NULL;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   441
}
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   442
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   443
/******************************************/
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   444
/* B 1.4.3 - Declaration & Initialisation */
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   445
/******************************************/
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   446
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   447
/* helper symbol for structure_initialization */
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   448
/* structure_element_initialization_list ',' structure_element_initialization */
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   449
void *visit(structure_element_initialization_list_c *symbol) {
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   450
  generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   451
  structure_initialization->init_structure_default(this->current_param_type);
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   452
  structure_initialization->init_structure_values(symbol);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   453
  delete structure_initialization;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   454
  return NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   455
}
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   456
237
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   457
/* helper symbol for array_initialization */
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   458
/* array_initial_elements_list ',' array_initial_elements */
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   459
void *visit(array_initial_elements_list_c *symbol) {
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   460
  generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   461
  array_initialization->init_array_size(this->current_param_type);
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   462
  array_initialization->init_array_values(symbol);
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   463
  delete array_initialization;
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   464
  return NULL;
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   465
}
cece842c7417 Adding support for using arrays in POU interface
laurent
parents: 236
diff changeset
   466
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   467
/***************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   468
/* B.3 - Language ST (Structured Text) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   469
/***************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   470
/***********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   471
/* B 3.1 - Expressions */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   472
/***********************/
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   473
void *visit(deref_operator_c *symbol) {
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   474
  /* When producing C code for FUNCTION_BLOCKS, we use the '*' syntax (e.g. (*ptr).elem)
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   475
   * When producing C code for a FUNCTION_BLOCK, the deref_operator_c are transformed in two ways, depending on where they occur.
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   476
   *    - deref_operator between a struct and its elem (e.g. struct_ref^.elem1) 
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   477
   *        are transformed into C using the C pointer derefence syntax '->' (e.g. struct_ref->elem1).
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   478
   *        (In this case, '->' is generated by this structured_variable_c visitor)
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   479
   *    - deref_operator at the end of a struct variable (e.g. struct.elem_ptr^)
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   480
   *        are transformed using the '*' syntax for C pointer dereferencing (e.g. *(struct.elem_ptr) )
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   481
   * 
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   482
   *   NOTE: Ideally, we should always use the '*' C pointer dereferencing syntax. However, due to the
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   483
   *         was the GET_VAR and SET_VAR are transformed into C, this does not work for '^' between a struct and its 
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   484
   *         element (e.g. struct_ref^.elem), which is whey in this case only we use the '->' syntax.
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   485
   *   NOTE: The use of the -> syntax means that pointers to pointers are not supported betweem a struct and its elem
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   486
   *         (e.g. . struct_ref_ref^^.elem) as this would be transformed into the invalid C code struct_ref_ref->->elem.
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   487
   *         This is why we add a test for this case, and bug out with an error if we encounter it!!
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   488
   */
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   489
  if (this->is_variable_prefix_null()) {  
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   490
    /* For code in FUNCTIONs */
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   491
    s4o.print("(*");  
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   492
    symbol->exp->accept(*this);    
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   493
    s4o.print(")");  
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   494
  } else {
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   495
    /* For code in FBs, and PROGRAMS... */
943
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   496
    if (   (NULL == dynamic_cast<structured_variable_c *>(symbol->parent)) 
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   497
        && (NULL == dynamic_cast<     array_variable_c *>(symbol->parent))) {
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   498
      s4o.print("(*");  
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   499
      symbol->exp->accept(*this);    
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   500
      s4o.print(")");  
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   501
    } else {
943
566414d7ba1f fix code generation when dereferencing pointers (ref_to) to arrays (ref_to_array^[5])
mjsousa
parents: 941
diff changeset
   502
      /* We are in a structured variable - the structured_variable_c or the array_variable_c will already have printed out the '->' !! */ 
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   503
      if (NULL != dynamic_cast<deref_operator_c *>(symbol->exp))
941
c2ef93412407 Editing of error message to make it more clear.
mjsousa
parents: 936
diff changeset
   504
        STAGE4_ERROR(symbol, symbol->exp, "The use of two or more consecutive derefencing operators between a struct variable and its record elem (ex: struct_ref_ref^^.elem) is currently not supported for code inside a Function_Block.");
936
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   505
      symbol->exp->accept(*this);
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   506
    }
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   507
  }
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   508
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   509
  return NULL;
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   510
}
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   511
0f7bcc160568 Fix bug: Add support for de-referencing pointers to struct (struct_ptr^.elem1) when used inside FB.
mjsousa
parents: 933
diff changeset
   512
933
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   513
void *visit(deref_expression_c *symbol) {
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   514
  s4o.print("(");  
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   515
  if (this->is_variable_prefix_null()) {  
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   516
    /* For code in FUNCTIONs */
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   517
    s4o.print("*");  
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   518
    symbol->exp->accept(*this);    
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   519
    s4o.print("");  
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   520
  } else {
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   521
    /* For code in FBs, and PROGRAMS... */
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   522
    unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol->exp, scope_);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   523
    if (vartype == search_var_instance_decl_c::external_vt) {
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   524
      if (!get_datatype_info_c::is_type_valid    (symbol->exp->datatype)) ERROR;
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   525
      if ( get_datatype_info_c::is_function_block(symbol->exp->datatype))
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   526
        s4o.print(GET_EXTERNAL_FB_DREF);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   527
      else
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   528
        s4o.print(GET_EXTERNAL_DREF);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   529
    }
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   530
    else if (vartype == search_var_instance_decl_c::located_vt)
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   531
      s4o.print(GET_LOCATED_DREF);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   532
    else
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   533
      s4o.print(GET_VAR_DREF);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   534
    
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   535
    variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; 
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   536
    s4o.print("(");
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   537
    wanted_variablegeneration = complextype_base_vg;
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   538
    print_variable_prefix();
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   539
    symbol->exp->accept(*this);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   540
    s4o.print(",");
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   541
    wanted_variablegeneration = complextype_suffix_vg;
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   542
    symbol->exp->accept(*this);
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   543
    s4o.print(")");
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   544
    wanted_variablegeneration = old_wanted_variablegeneration;
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   545
  }
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   546
  s4o.print(")");  
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   547
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   548
  return NULL;
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   549
}
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   550
76324f461aed Add support for de-referencing of REF_TO datatypes (Note: dereferencing of arrays and structs not yet supported!)
mjsousa
parents: 911
diff changeset
   551
873
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   552
void *visit(ref_expression_c *symbol) {
909
8b2a31dea131 Add support for REF_TO derived datatypes (along with datatype verification)
mjsousa
parents: 897
diff changeset
   553
  s4o.print("(");  
873
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   554
  if (this->is_variable_prefix_null()) {  
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   555
    /* For code in FUNCTIONs */
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   556
    s4o.print("&(");  
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   557
    symbol->exp->accept(*this);    
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   558
    s4o.print(")");  
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   559
  } else {
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   560
    /* For code in FBs, and PROGRAMS... */
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   561
    s4o.print("(");  
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   562
    unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol->exp, scope_);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   563
    if (vartype == search_var_instance_decl_c::external_vt) {
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   564
      if (!get_datatype_info_c::is_type_valid    (symbol->exp->datatype)) ERROR;
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   565
      if ( get_datatype_info_c::is_function_block(symbol->exp->datatype))
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   566
        s4o.print(GET_EXTERNAL_FB_REF);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   567
      else
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   568
        s4o.print(GET_EXTERNAL_REF);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   569
    }
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   570
    else if (vartype == search_var_instance_decl_c::located_vt)
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   571
      s4o.print(GET_LOCATED_REF);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   572
    else
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   573
      s4o.print(GET_VAR_REF);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   574
    
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   575
    variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; 
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   576
    s4o.print("(");
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   577
    wanted_variablegeneration = complextype_base_vg;
887
a5e2eedeef53 A little code cleanup (reduce number of possible variable generation states)
mjsousa
parents: 885
diff changeset
   578
    print_variable_prefix();
873
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   579
    symbol->exp->accept(*this);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   580
    s4o.print(",");
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   581
    wanted_variablegeneration = complextype_suffix_vg;
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   582
    symbol->exp->accept(*this);
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   583
    s4o.print(")");
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   584
    wanted_variablegeneration = old_wanted_variablegeneration;
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   585
    
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   586
    s4o.print(")");  
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   587
  }
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   588
  s4o.print(")");  
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   589
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   590
  return NULL;
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   591
}
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   592
dea39ef02847 Add limited support for the REF() operator (defined in v3 of IEC 61131-3)
mjsousa
parents: 864
diff changeset
   593
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   594
void *visit(or_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   595
  if (get_datatype_info_c::is_BOOL_compatible(symbol->datatype))
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   596
    return print_binary_expression(symbol->l_exp, symbol->r_exp, " || ");
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   597
  if (get_datatype_info_c::is_ANY_nBIT_compatible(symbol->datatype))
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   598
    return print_binary_expression(symbol->l_exp, symbol->r_exp, " | ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   599
  ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   600
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   601
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   602
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   603
void *visit(xor_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   604
  if (get_datatype_info_c::is_BOOL_compatible(symbol->datatype)) {
1026
fe580c0c1911 Fix code generation for XOR expressions in ST (add parenthesis)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1012
diff changeset
   605
    s4o.print("((");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   606
    symbol->l_exp->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   607
    s4o.print(" && !");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   608
    symbol->r_exp->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   609
    s4o.print(") || (!");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   610
    symbol->l_exp->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   611
    s4o.print(" && ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   612
    symbol->r_exp->accept(*this);
1026
fe580c0c1911 Fix code generation for XOR expressions in ST (add parenthesis)
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1012
diff changeset
   613
    s4o.print("))");
130
a8263f33123f Fixed missing return in XOR operator code gen.
etisserant
parents: 123
diff changeset
   614
    return NULL;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   615
  }
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   616
  if (get_datatype_info_c::is_ANY_nBIT_compatible(symbol->datatype))
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   617
    return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   618
  ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   619
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   620
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   621
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   622
void *visit(and_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   623
  if (get_datatype_info_c::is_BOOL_compatible(symbol->datatype))
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   624
    return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   625
  if (get_datatype_info_c::is_ANY_nBIT_compatible(symbol->datatype))
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   626
    return print_binary_expression(symbol->l_exp, symbol->r_exp, " & ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   627
  ERROR;
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   628
return NULL;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   629
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   630
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   631
void *visit(equ_expression_c *symbol) {
694
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   632
  if (get_datatype_info_c::is_TIME_compatible      (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   633
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   634
      get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   635
    return print_compare_function("EQ", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   636
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   637
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   638
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   639
void *visit(notequ_expression_c *symbol) {
694
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   640
  if (get_datatype_info_c::is_TIME_compatible      (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   641
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   642
      get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   643
    return print_compare_function("NE", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   644
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   645
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   646
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   647
void *visit(lt_expression_c *symbol) {
694
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   648
  if (get_datatype_info_c::is_TIME_compatible      (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   649
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   650
      get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   651
    return print_compare_function("LT", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
653
ea78924a1f60 Deleting datatype checks no longer needed (with a view of making search_expression_type_c redundant).
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   652
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   653
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   654
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   655
void *visit(gt_expression_c *symbol) {
694
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   656
  if (get_datatype_info_c::is_TIME_compatible      (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   657
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   658
      get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   659
    return print_compare_function("GT", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
653
ea78924a1f60 Deleting datatype checks no longer needed (with a view of making search_expression_type_c redundant).
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   660
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   661
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   662
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   663
void *visit(le_expression_c *symbol) {
694
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   664
  if (get_datatype_info_c::is_TIME_compatible      (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   665
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   666
      get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   667
    return print_compare_function("LE", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
653
ea78924a1f60 Deleting datatype checks no longer needed (with a view of making search_expression_type_c redundant).
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   668
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   669
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   670
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   671
void *visit(ge_expression_c *symbol) {
694
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   672
  if (get_datatype_info_c::is_TIME_compatible      (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   673
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->l_exp->datatype) ||
a010763dd4cb Fix stage4 for compare expressions.
Manuele Conti <conti.ma@alice.it>
parents: 693
diff changeset
   674
      get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   675
    return print_compare_function("GE", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
653
ea78924a1f60 Deleting datatype checks no longer needed (with a view of making search_expression_type_c redundant).
Mario de Sousa <msousa@fe.up.pt>
parents: 594
diff changeset
   676
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   677
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   678
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   679
void *visit(add_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   680
  if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype) ||
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   681
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->datatype))
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   682
    return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   683
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   684
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   685
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   686
void *visit(sub_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   687
  if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype) ||
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   688
      get_datatype_info_c::is_ANY_DATE_compatible  (symbol->datatype))
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   689
    return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp);
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   690
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   691
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   692
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   693
void *visit(mul_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   694
  if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype))
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   695
    return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp);
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   696
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   697
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   698
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   699
void *visit(div_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   700
  if (get_datatype_info_c::is_TIME_compatible      (symbol->datatype))
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   701
    return print_binary_function("__time_div", symbol->l_exp, symbol->r_exp);
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   702
  return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   703
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   704
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   705
void *visit(mod_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   706
  s4o.print("((");
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   707
  symbol->r_exp->accept(*this);
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   708
  s4o.print(" == 0)?0:");
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   709
  print_binary_expression(symbol->l_exp, symbol->r_exp, " % ");
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   710
  s4o.print(")");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   711
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   712
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   713
257
90782e241346 Huge change.
Mario de Sousa <msousa@fe.up.pt>
parents: 202
diff changeset
   714
void *visit(power_expression_c *symbol) {
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   715
  s4o.print("__expt((LREAL)(");
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   716
  symbol->l_exp->accept(*this);
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   717
  s4o.print("), (LREAL)(");
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   718
  symbol->r_exp->accept(*this);
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   719
  s4o.print("))");
358
a2c4af8f0c18 Add support for '**' (power) operator in ST expressions
Mario de Sousa <msousa@fe.up.pt>
parents: 355
diff changeset
   720
  return NULL;
257
90782e241346 Huge change.
Mario de Sousa <msousa@fe.up.pt>
parents: 202
diff changeset
   721
}
90782e241346 Huge change.
Mario de Sousa <msousa@fe.up.pt>
parents: 202
diff changeset
   722
123
a9b4af71cfa4 Fixed some issues on:
lbessard
parents: 101
diff changeset
   723
void *visit(neg_expression_c *symbol) {
257
90782e241346 Huge change.
Mario de Sousa <msousa@fe.up.pt>
parents: 202
diff changeset
   724
  return print_unary_expression(symbol->exp, " -");
123
a9b4af71cfa4 Fixed some issues on:
lbessard
parents: 101
diff changeset
   725
}
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   726
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   727
void *visit(not_expression_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   728
  return print_unary_expression(symbol->exp, get_datatype_info_c::is_BOOL_compatible(symbol->datatype)?"!":"~");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   729
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   730
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   731
void *visit(function_invocation_c *symbol) {
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   732
  symbol_c* function_name = NULL;
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   733
  DECLARE_PARAM_LIST()
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
   734
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
   735
  symbol_c *parameter_assignment_list = NULL;
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
   736
  if (NULL != symbol->   formal_param_list) parameter_assignment_list = symbol->   formal_param_list;
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
   737
  if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list;
1012
1f2af384fb1f Add support for non standard feature: allow POUs with no in, out and inout parameters
mjsousa
parents: 1011
diff changeset
   738
  // NOTE-> We support the non-standard feature of POUS with no in, out and inout parameters, so this is no longer an internal error!
1f2af384fb1f Add support for non standard feature: allow POUs with no in, out and inout parameters
mjsousa
parents: 1011
diff changeset
   739
  //if (NULL == parameter_assignment_list) ERROR;
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
   740
233
3d23a68183d3 Bug on standard functions called with literals fixed
laurent
parents: 231
diff changeset
   741
  function_call_param_iterator_c function_call_param_iterator(symbol);
3d23a68183d3 Bug on standard functions called with literals fixed
laurent
parents: 231
diff changeset
   742
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   743
  function_declaration_c *f_decl = (function_declaration_c *)symbol->called_function_declaration;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   744
  if (f_decl == NULL) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   745
  
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   746
  function_name = symbol->function_name;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   747
  
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   748
  /* loop through each function parameter, find the value we should pass
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   749
   * to it, and then output the c equivalent...
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   750
   */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   751
  function_param_iterator_c fp_iterator(f_decl);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   752
  identifier_c *param_name;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   753
    /* flag to cirreclty handle calls to extensible standard functions (i.e. functions with variable number of input parameters) */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   754
  bool found_first_extensible_parameter = false;  
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   755
  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   756
    if (fp_iterator.is_extensible_param() && (!found_first_extensible_parameter)) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   757
      /* We are calling an extensible function. Before passing the extensible
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   758
       * parameters, we must add a dummy paramater value to tell the called
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   759
       * function how many extensible parameters we will be passing.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   760
       *
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   761
       * Note that stage 3 has already determined the number of extensible
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   762
       * paramters, and stored that info in the abstract syntax tree. We simply
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   763
       * re-use that value.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   764
       */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   765
      /* NOTE: we are not freeing the malloc'd memory. This is not really a bug.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   766
       *       Since we are writing a compiler, which runs to termination quickly,
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   767
       *       we can consider this as just memory required for the compilation process
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   768
       *       that will be free'd when the program terminates.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   769
       */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   770
      char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   771
      if (tmp == NULL) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   772
      int res = snprintf(tmp, 32, "%d", symbol->extensible_param_count);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   773
      if ((res >= 32) || (res < 0)) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   774
      identifier_c *param_value = new identifier_c(tmp);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   775
      uint_type_name_c *param_type  = new uint_type_name_c();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   776
      identifier_c *param_name = new identifier_c("");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   777
      ADD_PARAM_LIST(param_name, param_value, param_type, function_param_iterator_c::direction_in)
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   778
      found_first_extensible_parameter = true;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   779
    }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   780
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   781
    if (fp_iterator.is_extensible_param()) {      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   782
      /* since we are handling an extensible parameter, we must add the index to the
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   783
       * parameter name so we can go looking for the value passed to the correct
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   784
       * extended parameter (e.g. IN1, IN2, IN3, IN4, ...)
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   785
       */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   786
      char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   787
      int res = snprintf(tmp, 32, "%d", fp_iterator.extensible_param_index());
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   788
      if ((res >= 32) || (res < 0)) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   789
      param_name = new identifier_c(strdup2(param_name->value, tmp));
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   790
      if (param_name->value == NULL) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   791
    }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   792
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   793
    symbol_c *param_type = fp_iterator.param_type();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   794
    if (param_type == NULL) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   795
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   796
    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   797
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   798
    symbol_c *param_value = NULL;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   799
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   800
    /* Get the value from a foo(<param_name> = <param_value>) style call */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   801
    if (param_value == NULL)
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   802
      param_value = function_call_param_iterator.search_f(param_name);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   803
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   804
    /* Get the value from a foo(<param_value>) style call */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   805
    if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   806
      param_value = function_call_param_iterator.next_nf();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   807
    }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   808
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   809
    /* if no more parameter values in function call, and the current parameter
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   810
     * of the function declaration is an extensible parameter, we
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   811
     * have reached the end, and should simply jump out of the for loop.
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   812
     */
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   813
    if ((param_value == NULL) && (fp_iterator.is_extensible_param())) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   814
      break;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   815
    }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   816
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   817
    if ((param_value == NULL) && (param_direction == function_param_iterator_c::direction_in)) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   818
      /* No value given for parameter, so we must use the default... */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   819
      /* First check whether default value specified in function declaration...*/
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   820
      param_value = fp_iterator.default_value();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   821
    }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   822
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   823
    ADD_PARAM_LIST(param_name, param_value, param_type, param_direction)
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   824
  } /* for(...) */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   825
  // symbol->parameter_assignment->accept(*this);
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   826
  
233
3d23a68183d3 Bug on standard functions called with literals fixed
laurent
parents: 231
diff changeset
   827
  if (function_call_param_iterator.next_nf() != NULL) ERROR;
3d23a68183d3 Bug on standard functions called with literals fixed
laurent
parents: 231
diff changeset
   828
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   829
  bool has_output_params = false;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   830
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   831
  if (!this->is_variable_prefix_null()) {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   832
    PARAM_LIST_ITERATOR() {
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   833
      if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   834
           PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   835
           PARAM_VALUE != NULL) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   836
        has_output_params = true;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   837
      }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   838
    }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   839
  }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   840
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   841
  /* Check whether we are calling an overloaded function! */
721
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   842
  /* (fdecl_mutiplicity > 1)  => calling overloaded function */
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   843
  int fdecl_mutiplicity =  function_symtable.count(symbol->function_name);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   844
  if (fdecl_mutiplicity == 0) ERROR;
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   845
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   846
  if (has_output_params) {
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   847
    fcall_number++;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   848
    s4o.print("__");
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   849
    fbname->accept(*this);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   850
    s4o.print("_");
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   851
    function_name->accept(*this);
721
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   852
    if (fdecl_mutiplicity > 1) {
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   853
      /* function being called is overloaded! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   854
      s4o.print("__");
406
6381589697ff Fix bug with overloaded function due to literal input values
laurent
parents: 405
diff changeset
   855
      print_function_parameter_data_types_c overloaded_func_suf(&s4o);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   856
      f_decl->accept(overloaded_func_suf);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   857
    }
594
c8092e909886 Clean up code (remove parsing of integers in stage 4).
Mario de Sousa <msousa@fe.up.pt>
parents: 531
diff changeset
   858
    s4o.print(fcall_number);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   859
  }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   860
  else {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   861
    function_name->accept(*this);
721
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   862
    if (fdecl_mutiplicity > 1) {
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   863
      /* function being called is overloaded! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   864
      s4o.print("__");
406
6381589697ff Fix bug with overloaded function due to literal input values
laurent
parents: 405
diff changeset
   865
      print_function_parameter_data_types_c overloaded_func_suf(&s4o);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   866
      f_decl->accept(overloaded_func_suf);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   867
    }
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   868
  }
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   869
  s4o.print("(");
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   870
  s4o.indent_right();
1011
76175defb87b Add option to not add the EN and ENO parameters to every function and FB (generated C code becomes smaller, and uses less memory for function invocations)
mjsousa
parents: 999
diff changeset
   871
  s4o.print("\n"+s4o.indent_spaces);
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   872
  
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   873
  int nb_param = 0;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   874
  PARAM_LIST_ITERATOR() {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   875
    symbol_c *param_value = PARAM_VALUE;
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   876
    current_param_type = PARAM_TYPE;
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   877
          
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   878
    switch (PARAM_DIRECTION) {
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   879
      case function_param_iterator_c::direction_in:
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   880
        if (nb_param > 0)
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   881
          s4o.print(",\n"+s4o.indent_spaces);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   882
        if (param_value == NULL) {
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   883
          /* If not, get the default value of this variable's type */
762
a3d917474ae4 Small code cleanup.
Mario de Sousa <msousa@fe.up.pt>
parents: 721
diff changeset
   884
          param_value = type_initial_value_c::get(current_param_type);
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   885
        }
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   886
        if (param_value == NULL) ERROR;
216
136d6ae70745 Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents: 208
diff changeset
   887
        s4o.print("(");
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   888
        if      (get_datatype_info_c::is_ANY_INT_literal(current_param_type))
693
51a2fa6441b9 Prepare to delete search_constant_type_c -> Move the static variables from search_constant_type_c to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 668
diff changeset
   889
          get_datatype_info_c::lint_type_name.accept(*this);
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   890
        else if (get_datatype_info_c::is_ANY_REAL_literal(current_param_type))
693
51a2fa6441b9 Prepare to delete search_constant_type_c -> Move the static variables from search_constant_type_c to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 668
diff changeset
   891
          get_datatype_info_c::lreal_type_name.accept(*this);
216
136d6ae70745 Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents: 208
diff changeset
   892
        else
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   893
          current_param_type->accept(*this);
216
136d6ae70745 Bug with using non explicit literals as standard library functions input parameters fixed
laurent
parents: 208
diff changeset
   894
        s4o.print(")");
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 233
diff changeset
   895
        print_check_function(current_param_type, param_value);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   896
        nb_param++;
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   897
        break;
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   898
      case function_param_iterator_c::direction_out:
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   899
      case function_param_iterator_c::direction_inout:
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   900
        if (!has_output_params) {
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   901
          if (nb_param > 0)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   902
            s4o.print(",\n"+s4o.indent_spaces);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   903
          if (param_value == NULL)
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   904
            s4o.print("NULL");
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   905
          else {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   906
            wanted_variablegeneration = fparam_output_vg;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   907
            param_value->accept(*this);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   908
            wanted_variablegeneration = expression_vg;
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   909
          }
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   910
          nb_param++;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   911
        }
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   912
        break;
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   913
      case function_param_iterator_c::direction_extref:
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   914
        /* TODO! */
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   915
        ERROR;
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   916
        break;
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   917
    } /* switch */
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   918
  }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   919
  if (has_output_params) {
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   920
    if (nb_param > 0)
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   921
      s4o.print(",\n"+s4o.indent_spaces);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   922
    s4o.print(FB_FUNCTION_PARAM);
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   923
  }
149
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   924
  s4o.print(")");
05ca171a3d57 Adding support for EN/ENO in standard functions
lbessard
parents: 146
diff changeset
   925
  s4o.indent_left();
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   926
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   927
  CLEAR_PARAM_LIST()
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   928
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   929
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   930
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   931
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   932
/********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   933
/* B 3.2 Statements */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   934
/********************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   935
void *visit(statement_list_c *symbol) {
877
c25346eac788 Insert #line directives in generated C code.
mjsousa
parents: 873
diff changeset
   936
  for(int i = 0; i < symbol->n; i++) {
1041
56ebe2a31b5b Access elements[] in list_c through a new get_element() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
   937
    print_line_directive(symbol->get_element(i));
877
c25346eac788 Insert #line directives in generated C code.
mjsousa
parents: 873
diff changeset
   938
    s4o.print(s4o.indent_spaces);
1041
56ebe2a31b5b Access elements[] in list_c through a new get_element() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
   939
    symbol->get_element(i)->accept(*this);
877
c25346eac788 Insert #line directives in generated C code.
mjsousa
parents: 873
diff changeset
   940
    s4o.print(";\n");
c25346eac788 Insert #line directives in generated C code.
mjsousa
parents: 873
diff changeset
   941
  }
c25346eac788 Insert #line directives in generated C code.
mjsousa
parents: 873
diff changeset
   942
  return NULL;
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   943
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   944
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   945
/*********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   946
/* B 3.2.1 Assignment Statements */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   947
/*********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   948
void *visit(assignment_statement_c *symbol) {
1076
efaa818ef637 partial fix for issue 74: allow deref of REF in left hand side of assignment (only works in FUNCTIONS for now)
Mario de Sousa <msousa@fe.up.pt>
parents: 1049
diff changeset
   949
  symbol_c *left_type = symbol->l_exp->datatype;
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
   950
  
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   951
  if (this->is_variable_prefix_null()) {
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   952
    symbol->l_exp->accept(*this);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   953
    s4o.print(" = ");
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   954
    print_check_function(left_type, symbol->r_exp);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   955
  }
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
   956
  else {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   957
    print_setter(symbol->l_exp, left_type, symbol->r_exp);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
   958
  }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   959
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   960
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   961
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   962
/*****************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   963
/* B 3.2.2 Subprogram Control Statements */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   964
/*****************************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   965
283
214d3fdee596 fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents: 282
diff changeset
   966
void *visit(return_statement_c *symbol) {
214d3fdee596 fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents: 282
diff changeset
   967
  s4o.print("goto "); s4o.print(END_LABEL);
214d3fdee596 fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents: 282
diff changeset
   968
  return NULL;
214d3fdee596 fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents: 282
diff changeset
   969
}
214d3fdee596 fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents: 282
diff changeset
   970
214d3fdee596 fixing C code generation of return_statement_c and exit_statement_c
Mario de Sousa <msousa@fe.up.pt>
parents: 282
diff changeset
   971
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   972
/* fb_name '(' [param_assignment_list] ')' */
855
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   973
/*    formal_param_list -> may be NULL ! */
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   974
/* nonformal_param_list -> may be NULL ! */
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   975
/* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   976
// SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list, symbol_c *called_fb_declaration;)
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   977
void *visit(fb_invocation_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   978
  TRACE("fb_invocation_c");
855
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   979
  
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   980
  /* find the declaration of the function block type being called... */
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   981
  symbol_c *fb_decl = symbol->called_fb_declaration;
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   982
  if (fb_decl == NULL) ERROR;
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   983
  /* figure out the name of the function block type of the function block being called... */
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   984
  symbol_c *function_block_type_name = get_datatype_info_c::get_id(fb_decl);
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   985
  if (NULL == function_block_type_name) ERROR;
a3109442e166 Start using the called_fb_declaration annotation when generating C code from FB calls in ST.
mjsousa
parents: 854
diff changeset
   986
  
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   987
  /* loop through each function block parameter, find the value we should pass
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   988
   * to it, and then output the c equivalent...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   989
   */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   990
  function_param_iterator_c fp_iterator(fb_decl);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   991
  identifier_c *param_name;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   992
  function_call_param_iterator_c function_call_param_iterator(symbol);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   993
  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   994
    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 149
diff changeset
   995
    
162
60a124678842 Fixed typo in EN/ENO parameters handling for std library functions
etisserant
parents: 160
diff changeset
   996
    /*fprintf(stderr, "param : %s\n", param_name->value);*/
160
59d58f5e6caa Adding support for compiling struct data types
lbessard
parents: 149
diff changeset
   997
    
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
   998
    /* Get the value from a foo(<param_name> = <param_value>) style call */
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
   999
    symbol_c *param_value = function_call_param_iterator.search_f(param_name);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1000
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1001
    /* Get the value from a foo(<param_value>) style call */
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1002
    /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1003
     * were implicitly defined!
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1004
     */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1005
    if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit())
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
  1006
      param_value = function_call_param_iterator.next_nf();
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1007
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
  1008
    symbol_c *param_type = fp_iterator.param_type();
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
  1009
    if (param_type == NULL) ERROR;
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
  1010
    
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1011
    /* now output the value assignment */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1012
    if (param_value != NULL)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1013
      if ((param_direction == function_param_iterator_c::direction_in) ||
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1014
          (param_direction == function_param_iterator_c::direction_inout)) {
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1015
        if (this->is_variable_prefix_null()) {
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1016
          symbol->fb_name->accept(*this);
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1017
          s4o.print(".");
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1018
          param_name->accept(*this);
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1019
          s4o.print(" = ");
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1020
          print_check_function(param_type, param_value);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
  1021
        }
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1022
        else {
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 226
diff changeset
  1023
          print_setter(param_name, param_type, param_value, symbol->fb_name);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1024
        }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1025
        s4o.print(";\n" + s4o.indent_spaces);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1026
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1027
  } /* for(...) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1028
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1029
  /* now call the function... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1030
  function_block_type_name->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1031
  s4o.print(FB_FUNCTION_SUFFIX);
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
  1032
  s4o.print("(");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
  1033
  if (search_var_instance_decl->get_vartype(symbol->fb_name) != search_var_instance_decl_c::external_vt)
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
  1034
    s4o.print("&");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1035
  print_variable_prefix();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1036
  symbol->fb_name->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1037
  s4o.print(")");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1038
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1039
  /* loop through each function parameter, find the variable to which
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1040
   * we should atribute the value of all output or inoutput parameters.
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1041
   */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1042
  fp_iterator.reset();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1043
  function_call_param_iterator.reset();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1044
  for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1045
    function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1046
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1047
    /* Get the value from a foo(<param_name> = <param_value>) style call */
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
  1048
    symbol_c *param_value = function_call_param_iterator.search_f(param_name);
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1049
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1050
    /* Get the value from a foo(<param_value>) style call */
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1051
    /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1052
     * were implicitly defined!
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1053
     */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
  1054
    if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit())
202
da1a8186f86f Initial (very rough) version of semantic checker (stage3)
Catarina Boucinha <ccb@fe.up.pt>
parents: 169
diff changeset
  1055
      param_value = function_call_param_iterator.next_nf();
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1056
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1057
    /* now output the value assignment */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1058
    if (param_value != NULL)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1059
      if ((param_direction == function_param_iterator_c::direction_out) ||
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1060
          (param_direction == function_param_iterator_c::direction_inout)) {
321
a96399ab57c2 Add better comments, and change name of functions to clarify what they do.
Mario de Sousa <msousa@fe.up.pt>
parents: 309
diff changeset
  1061
        symbol_c *param_type = search_varfb_instance_type->get_type_id(param_value);
240
f78fa87bb4cb Bugs fixed:
laurent
parents: 238
diff changeset
  1062
        s4o.print(";\n" + s4o.indent_spaces);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1063
        if (this->is_variable_prefix_null()) {
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
  1064
          param_value->accept(*this);
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1065
          s4o.print(" = ");
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1066
          print_check_function(param_type, param_name, symbol->fb_name);
98
d0cdf1d00b74 Adding support for derived data types.
lbessard
parents: 70
diff changeset
  1067
        }
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
  1068
        else {
226
29f8ffc203c1 Adding support for setter and getter on complex datatype in ST
laurent
parents: 221
diff changeset
  1069
          print_setter(param_value, param_type, param_name, NULL, symbol->fb_name);
217
f5dfadf5de54 Adding support for declare, init, get and set macros
laurent
parents: 216
diff changeset
  1070
        }
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1071
      }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1072
  } /* for(...) */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1073
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1074
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1075
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1076
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1077
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1078
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1079
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1080
/* helper symbol for fb_invocation */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1081
/* param_assignment_list ',' param_assignment */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1082
void *visit(param_assignment_list_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1083
  TRACE("param_assignment_list_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1084
  /* this should never be called... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1085
  ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1086
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1087
//  return print_list(symbol, "", ", ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1088
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1089
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1090
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1091
void *visit(input_variable_param_assignment_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1092
  TRACE("input_variable_param_assignment_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1093
  /* this should never be called... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1094
  ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1095
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1096
/*
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1097
  symbol->variable_name->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1098
  s4o.print(" = ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1099
  symbol->expression->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1100
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1101
*/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1102
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1103
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1104
void *visit(output_variable_param_assignment_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1105
  TRACE("output_variable_param_assignment_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1106
  /* this should never be called... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1107
  ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1108
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1109
/*
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1110
  s4o.print(s4o.indent_spaces);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1111
  if (symbol->not_param != NULL)
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1112
    symbol->not_param->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1113
  symbol->variable_name->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1114
  s4o.print(" => ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1115
  symbol->variable->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1116
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1117
*/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1118
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1119
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1120
// TODO: the NOT symbol in function (block) calls...
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1121
void *visit(not_paramassign_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1122
  TRACE("not_paramassign_c");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1123
  /* this should never be called... */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1124
  ERROR;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1125
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1126
/*
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1127
  s4o.print("NOT ");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1128
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1129
*/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1130
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1131
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1132
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1133
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1134
/* B 3.2.3 Selection Statements */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1135
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1136
void *visit(if_statement_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1137
  s4o.print("if (");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1138
  symbol->expression->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1139
  s4o.print(") {\n");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1140
  s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1141
  symbol->statement_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1142
  s4o.indent_left();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1143
  symbol->elseif_statement_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1144
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1145
  if (symbol->else_statement_list != NULL) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1146
    s4o.print(s4o.indent_spaces); s4o.print("} else {\n");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1147
    s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1148
    symbol->else_statement_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1149
    s4o.indent_left();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1150
  }
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1151
  s4o.print(s4o.indent_spaces); s4o.print("}");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1152
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1153
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1154
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1155
/* helper symbol for if_statement */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1156
void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1157
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1158
/* helper symbol for elseif_statement_list */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1159
void *visit(elseif_statement_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1160
  s4o.print(s4o.indent_spaces); s4o.print("} else if (");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1161
  symbol->expression->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1162
  s4o.print(") {\n");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1163
  s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1164
  symbol->statement_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1165
  s4o.indent_left();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1166
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1167
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1168
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1169
void *visit(case_statement_c *symbol) {
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
  1170
  symbol_c *expression_type = symbol->expression->datatype;
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1171
  s4o.print("{\n");
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1172
  s4o.indent_right();
376
7dcbd8418771 Making case statement code generated safer
laurent
parents: 375
diff changeset
  1173
  s4o.print(s4o.indent_spaces);
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
  1174
  if      (get_datatype_info_c::is_ANY_INT_literal(expression_type))
693
51a2fa6441b9 Prepare to delete search_constant_type_c -> Move the static variables from search_constant_type_c to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 668
diff changeset
  1175
           get_datatype_info_c::lint_type_name.accept(*this);
668
90b6eb7f1775 Deprecating use of search_expression_type_c, so it may later be deleted!
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
  1176
  else if (get_datatype_info_c::is_ANY_REAL_literal(expression_type))
693
51a2fa6441b9 Prepare to delete search_constant_type_c -> Move the static variables from search_constant_type_c to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 668
diff changeset
  1177
           get_datatype_info_c::lreal_type_name.accept(*this);
376
7dcbd8418771 Making case statement code generated safer
laurent
parents: 375
diff changeset
  1178
  else
7dcbd8418771 Making case statement code generated safer
laurent
parents: 375
diff changeset
  1179
    expression_type->accept(*this);
7dcbd8418771 Making case statement code generated safer
laurent
parents: 375
diff changeset
  1180
  s4o.print(" __case_expression = ");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1181
  symbol->expression->accept(*this);
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1182
  s4o.print(";\n");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1183
  symbol->case_element_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1184
  if (symbol->statement_list != NULL) {
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1185
    s4o.print(s4o.indent_spaces + "else {\n");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1186
    s4o.indent_right();
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1187
    symbol->statement_list->accept(*this);
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1188
    s4o.indent_left();
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1189
    s4o.print(s4o.indent_spaces + "}\n");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1190
  }
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1191
  s4o.indent_left();
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1192
  s4o.print(s4o.indent_spaces + "}");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1193
  return NULL;
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1194
}
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1195
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1196
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1197
/* helper symbol for case_statement */
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1198
void *visit(case_element_list_c *symbol) {return print_list(symbol, s4o.indent_spaces+"if ", s4o.indent_spaces+"else if ");}
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1199
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1200
void *visit(case_element_c *symbol) {
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1201
  symbol->case_list->accept(*this);
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1202
  s4o.print(" {\n");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1203
  s4o.indent_right();
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1204
  symbol->statement_list->accept(*this);
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1205
  s4o.indent_left();
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1206
  s4o.print(s4o.indent_spaces + "}\n");
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1207
  return NULL;
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1208
}
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1209
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1210
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1211
void *visit(case_list_c *symbol) {
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1212
  s4o.print("(");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1213
  for (int i =  0; i < symbol->n; i++) {
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1214
    /* if not the first element, then add an '||', a '\n', and add some spaces to get nice alignment */
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1215
    /* example of generated C code (codition) for 
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1216
     *   case XX of
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1217
     *     10..20,42,15..99:  <---- C code is for this line!
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1218
     * 
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1219
     *    else if ((__case_expression >= 10 && __case_expression <= 20) ||
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1220
     *             (__case_expression == 42) ||
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1221
     *             (__case_expression >= 15 && __case_expression <= 99)) {
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1222
     */
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1223
    if (0 != i)  s4o.print(" ||\n" + s4o.indent_spaces + "         ");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1224
    s4o.print("(");
1041
56ebe2a31b5b Access elements[] in list_c through a new get_element() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1225
    subrange_c *subrange = dynamic_cast<subrange_c *>(symbol->get_element(i));
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1226
    if (NULL == subrange) {
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1227
      s4o.print("__case_expression == ");
1041
56ebe2a31b5b Access elements[] in list_c through a new get_element() method.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1228
      symbol->get_element(i)->accept(*this);
999
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1229
    } else {
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1230
      s4o.print("__case_expression >= ");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1231
      subrange->lower_limit->accept(*this);
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1232
      s4o.print(" && __case_expression <= ");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1233
      subrange->upper_limit->accept(*this);
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1234
    }
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1235
    s4o.print(")");
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1236
  }
dd50a82ae8da transform CASE into if()..else if().. -> fixes 2 bugs (1)case within case statements, and (2)repeated elements in case options
mjsousa
parents: 945
diff changeset
  1237
  s4o.print(")");
347
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1238
  return NULL;
44ff2a6fcadc Fix case statement in ST, adding support for enumerated types
laurent
parents: 344
diff changeset
  1239
}
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1240
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1241
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1242
/* B 3.2.4 Iteration Statements */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1243
/********************************/
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1244
void *visit(for_statement_c *symbol) {
1038
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1245
  /* Due to the way the GET/SET_GLOBAL accessor macros access VAR_GLOBAL variables,
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1246
   * these varibles cannot be used within a C for(;;) loop.
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1247
   * We must therefore implemnt the FOR END_FOR loop as a C while() loop
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1248
   */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1249
  s4o.print("/* FOR ... */\n" + s4o.indent_spaces);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1250
  /* For the initialization part, we create an assignment_statement_c   */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1251
  /* and have this visitor visit it!                                    */ 
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1252
  assignment_statement_c ini_assignment(symbol->control_variable, symbol->beg_expression);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1253
  ini_assignment.accept(*this);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1254
  //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1255
  //s4o.print(" = ");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1256
  //symbol->beg_expression->accept(*this);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1257
  
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1258
  /* comparison // check for end of loop */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1259
  s4o.print(";\n" + s4o.indent_spaces + "while( ");
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1260
  if (symbol->by_expression == NULL) {
1038
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1261
    /* increment by 1 */    
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1262
    symbol->control_variable->accept(*this);
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1263
    s4o.print(" <= ");
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1264
    symbol->end_expression->accept(*this);
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1265
  } else {
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1266
    /* increment by user defined value  */
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1267
    /* The user defined increment value may be negative, in which case
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1268
     * the expression to determine whether we have reached the end of the loop
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1269
     * changes from a '<=' to a '>='.
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1270
     * Since the increment value may change during runtime (remember, it is
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1271
     * an expression, so may contain variables), choosing which test
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1272
     * to use has to be done at runtime.
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1273
     */
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1274
    s4o.print("((");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1275
    symbol->by_expression->accept(*this);
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1276
    s4o.print(") > 0)? (");
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1277
    symbol->control_variable->accept(*this);
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1278
    s4o.print(" <= (");
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1279
    symbol->end_expression->accept(*this);
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1280
    s4o.print(")) : (");
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1281
    symbol->control_variable->accept(*this);
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1282
    s4o.print(" >= (");
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1283
    symbol->end_expression->accept(*this);
1038
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1284
    s4o.print(")) ");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1285
  }
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1286
  s4o.print(" ) {\n");
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1287
  
1038
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1288
  /* the body part */
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1289
  s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1290
  symbol->statement_list->accept(*this);
1038
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1291
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1292
  /* increment part */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1293
  s4o.print(s4o.indent_spaces + "/* BY ... (of FOR loop) */\n");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1294
  s4o.print(s4o.indent_spaces); 
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1295
  if (symbol->by_expression == NULL) {
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1296
    /* increment by 1 */    
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1297
    /* For the increment part, we create an add_expression_c and assignment_statement_c   */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1298
    /* and have this visitor vist the latter!                                             */ 
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1299
    integer_c              integer_oneval("1");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1300
    add_expression_c       add_expression(symbol->control_variable, &integer_oneval);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1301
    assignment_statement_c inc_assignment(symbol->control_variable, &add_expression);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1302
    integer_oneval.const_value._int64 .set(1);                    // set the stage3 anottation we need 
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1303
    integer_oneval.const_value._uint64.set(1);                    // set the stage3 anottation we need
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1304
    integer_oneval.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1305
    add_expression.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1306
    inc_assignment.accept(*this);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1307
    //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1308
    //s4o.print("++");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1309
  } else {
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1310
    /* increment by user defined value  */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1311
    /* For the increment part, we create an add_expression_c and assignment_statement_c   */
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1312
    /* and have this visitor vist the latter!                                             */ 
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1313
    add_expression_c       add_expression(symbol->control_variable, symbol->by_expression);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1314
    assignment_statement_c inc_assignment(symbol->control_variable, &add_expression);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1315
    add_expression.datatype = symbol->control_variable->datatype; // set the stage3 anottation we need
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1316
    inc_assignment.accept(*this);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1317
    //symbol->control_variable->accept(*this);  // this does not work for VAR_GLOBAL variables
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1318
    //s4o.print(" += (");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1319
    //symbol->by_expression->accept(*this);
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1320
    //s4o.print(")");
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1321
  }  
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1322
  
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1323
  s4o.indent_left();
1038
036f15e4041d Allow use of GLOBAL/EXTERNAL variables as control variable of FOR loop.
Mario de Sousa <msousa@fe.up.pt>
parents: 1026
diff changeset
  1324
  s4o.print(";\n" + s4o.indent_spaces + "} /* END_FOR */");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1325
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1326
}
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1327
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1328
void *visit(while_statement_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1329
  s4o.print("while (");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1330
  symbol->expression->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1331
  s4o.print(") {\n");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1332
  s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1333
  symbol->statement_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1334
  s4o.indent_left();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1335
  s4o.print(s4o.indent_spaces); s4o.print("}");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1336
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1337
}
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1338
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1339
void *visit(repeat_statement_c *symbol) {
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1340
  s4o.print("do {\n");
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1341
  s4o.indent_right();
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1342
  symbol->statement_list->accept(*this);
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1343
  s4o.indent_left();
1098
84bbafb4fb26 Fixed logic for REPEAT..UNTIL
Edouard Tisserant
parents: 1095
diff changeset
  1344
  s4o.print(s4o.indent_spaces); s4o.print("} while(!(");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1345
  symbol->expression->accept(*this);
1098
84bbafb4fb26 Fixed logic for REPEAT..UNTIL
Edouard Tisserant
parents: 1095
diff changeset
  1346
  s4o.print("))");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1347
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1348
}
387
db368e53133c Fix code generation of FOR loops. Now handles negative values of BY correctly.
Mario de Sousa <msousa@fe.up.pt>
parents: 382
diff changeset
  1349
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1350
void *visit(exit_statement_c *symbol) {
281
686fa44e3832 fixing exit_statement_c: produce 'break' instead of 'exit(0); (Thanks to Manuele)
Mario de Sousa <msousa@fe.up.pt>
parents: 258
diff changeset
  1351
  s4o.print("break");
70
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1352
  return NULL;
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1353
}
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1354
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1355
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1356
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1357
}; /* generate_c_st_c */
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1358
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1359
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1360
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1361
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1362
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1363
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1364
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1365
e1f0ebd2d9ec Change generate_cc to generate_c
lbessard
parents:
diff changeset
  1366