stage4/generate_c/generate_c_inlinefcall.cc
author mjsousa
Sun, 16 Nov 2014 12:54:10 +0000
changeset 945 477393b00f95
parent 885 b2604fc6d25c
child 1001 f6ed7dcfc53e
permissions -rwxr-xr-x
Add support for relaxed datatype model for array datatypes.
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     1
/*
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 238
diff changeset
     2
 *  matiec - a compiler for the programming languages defined in IEC 61131-3
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     3
 *
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
     4
 *  Copyright (C) 2003-2012  Mario de Sousa (msousa@fe.up.pt)
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 238
diff changeset
     5
 *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
     6
 *
279
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 238
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: 238
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: 238
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: 238
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: 238
diff changeset
    11
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 238
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: 238
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: 238
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: 238
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: 238
diff changeset
    16
 *
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 238
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: 238
diff changeset
    18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
c0453b7f99df Re-generated std lib related code, with updated headers, updated all forgotten headers
Edouard Tisserant
parents: 238
diff changeset
    19
 *
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    20
 *
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    21
 * This code is made available on the understanding that it will not be
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    22
 * used in safety-critical situations without a full and competent review.
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    23
 */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    24
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    25
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    26
#define INLINE_RESULT_TEMP_VAR "__res"
380
b78e59ed4269 Fix bug in generate_c_inlinefcall.cc that prevent to use extendible standard functions
laurent
parents: 355
diff changeset
    27
#define INLINE_PARAM_COUNT "__PARAM_COUNT"
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    28
945
477393b00f95 Add support for relaxed datatype model for array datatypes.
mjsousa
parents: 885
diff changeset
    29
class generate_c_inlinefcall_c: public generate_c_base_and_typeid_c {
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    30
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    31
  public:
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    32
    typedef enum {
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    33
      expression_vg,
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
    34
      assignment_vg,
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
    35
      complextype_base_vg,
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
    36
      complextype_suffix_vg
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    37
    } variablegeneration_t;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    38
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    39
  private:
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    40
     /* The result of the comparison IL operations (GT, EQ, LT, ...)
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    41
     * is a boolean variable.
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    42
     * This class keeps track of the current data type stored in the
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    43
     * il default variable. This is usually done by keeping a reference
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    44
     * to the data type of the last operand. Nevertheless, in the case of
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    45
     * the comparison IL operators, the data type of the result (a boolean)
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    46
     * is not the data type of the operand. We therefore need an object
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    47
     * of the boolean data type to keep as a reference of the current
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    48
     * data type.
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    49
     * The following object is it...
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    50
     */
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    51
    bool_type_name_c bool_type;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    52
    lint_type_name_c lint_type;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    53
    lword_type_name_c lword_type;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    54
    lreal_type_name_c lreal_type;
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
    55
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    56
    /* The name of the IL default variable... */
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    57
    #define IL_DEFVAR   VAR_LEADER "IL_DEFVAR"
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    58
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    59
    /* The name of the variable used to pass the result of a
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    60
     * parenthesised instruction list to the immediately preceding
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    61
     * scope ...
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    62
     */
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
    63
    #define IL_DEFVAR_BACK   VAR_LEADER "IL_DEFVAR_BACK"
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
    64
    il_default_variable_c implicit_variable_current;
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    65
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    66
    symbol_c* current_array_type;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    67
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    68
    int fcall_number;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    69
    bool generating_inlinefunction;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
    70
    symbol_c *fbname;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    71
9bb38736f126 Adding support for declare, init, get and set macros
laurent
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: 500
diff changeset
    73
    search_var_instance_decl_c   *search_var_instance_decl;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    74
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    75
    variablegeneration_t wanted_variablegeneration;
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    76
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    77
  public:
234
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
    78
    generate_c_inlinefcall_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: 885
diff changeset
    79
    : generate_c_base_and_typeid_c(s4o_ptr),
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
    80
      implicit_variable_current(IL_DEFVAR, NULL)
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    81
    {
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    82
      search_varfb_instance_type = new search_varfb_instance_type_c(scope);
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
    83
      search_var_instance_decl   = new search_var_instance_decl_c  (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: 500
diff changeset
    84
      
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    85
      this->set_variable_prefix(variable_prefix);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    86
      fcall_number = 0;
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    87
      fbname = name;
221
c6aed7e5f070 Adding support for flags on Function Block variables for marking which variable must be debugged, retained or is forced
laurent
parents: 219
diff changeset
    88
      wanted_variablegeneration = expression_vg;
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
    89
      generating_inlinefunction = false;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    90
    }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    91
234
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
    92
    virtual ~generate_c_inlinefcall_c(void) {
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    93
      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: 500
diff changeset
    94
      delete search_var_instance_decl;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    95
    }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
    96
234
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
    97
    void print(symbol_c* symbol) {
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
    98
      function_call_iterator_c fc_iterator(symbol);
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
    99
      symbol_c* function_call;
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   100
      while ((function_call = fc_iterator.next()) != NULL) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   101
        function_call->accept(*this);
234
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   102
      }
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   103
    }
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   104
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   105
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   106
234
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   107
    void generate_inline(symbol_c *function_name,
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   108
            symbol_c *function_type_prefix,
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   109
            symbol_c *function_type_suffix,
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   110
            std::list<FUNCTION_PARAM*> param_list,
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   111
            function_declaration_c *f_decl = NULL) {
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   112
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   113
      std::list<FUNCTION_PARAM*>::iterator pt;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   114
      generating_inlinefunction = true;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   115
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   116
      fcall_number++;
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   117
      function_type_prefix = default_literal_type(function_type_prefix);
336
229eb3e29216 Fix bug in function calls with literal parameters
laurent
parents: 332
diff changeset
   118
      if (function_type_suffix) {
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   119
        function_type_suffix = default_literal_type(function_type_suffix);
332
0f1d91a37506 Fix bug in inline function calls when using function with undetermined type interface with literals as input
laurent
parents: 321
diff changeset
   120
      }
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   121
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   122
      s4o.print(s4o.indent_spaces);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   123
      s4o.print("inline ");
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   124
      function_type_prefix->accept(*this);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   125
      s4o.print(" __");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   126
      fbname->accept(*this);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   127
      s4o.print("_");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   128
      function_name->accept(*this);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   129
      if (f_decl != NULL) {
383
0b81b59f333b Fix bug generate_c_inlinefcall in inline function name generated
laurent
parents: 380
diff changeset
   130
        /* function being called is overloaded! */
0b81b59f333b Fix bug generate_c_inlinefcall in inline function name generated
laurent
parents: 380
diff changeset
   131
        s4o.print("__");
406
6381589697ff Fix bug with overloaded function due to literal input values
laurent
parents: 399
diff changeset
   132
        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
   133
        f_decl->accept(overloaded_func_suf);
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   134
      }
336
229eb3e29216 Fix bug in function calls with literal parameters
laurent
parents: 332
diff changeset
   135
      if (function_type_suffix) {
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   136
        function_type_suffix->accept(*this);
336
229eb3e29216 Fix bug in function calls with literal parameters
laurent
parents: 332
diff changeset
   137
      }
594
c8092e909886 Clean up code (remove parsing of integers in stage 4).
Mario de Sousa <msousa@fe.up.pt>
parents: 531
diff changeset
   138
      s4o.print(fcall_number);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   139
      s4o.print("(");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   140
      s4o.indent_right();
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   141
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   142
      PARAM_LIST_ITERATOR() {
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   143
        if (PARAM_DIRECTION == function_param_iterator_c::direction_in) {
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   144
          default_literal_type(PARAM_TYPE)->accept(*this);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   145
          s4o.print(" ");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   146
          PARAM_NAME->accept(*this);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   147
          s4o.print(",\n" + s4o.indent_spaces);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   148
        }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   149
      }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   150
      fbname->accept(*this);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   151
      s4o.print(" *");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   152
      s4o.print(FB_FUNCTION_PARAM);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   153
      s4o.indent_left();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   154
      s4o.print(")\n" + s4o.indent_spaces);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   155
      s4o.print("{\n");
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   156
      s4o.indent_right();
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   157
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   158
      s4o.print(s4o.indent_spaces);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   159
      function_type_prefix->accept(*this);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   160
      s4o.print(" "),
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   161
      s4o.print(INLINE_RESULT_TEMP_VAR);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   162
      s4o.print(";\n");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   163
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   164
      PARAM_LIST_ITERATOR() {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   165
        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
   166
             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
   167
             PARAM_VALUE != NULL) {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   168
          s4o.print(s4o.indent_spaces);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   169
          PARAM_TYPE->accept(*this);
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   170
          s4o.print(" ");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   171
          s4o.print(TEMP_VAR);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   172
          PARAM_NAME->accept(*this);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   173
          s4o.print(" = ");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   174
          print_check_function(PARAM_TYPE, PARAM_VALUE);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   175
          s4o.print(";\n");
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   176
          }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   177
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   178
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   179
      s4o.print(s4o.indent_spaces + INLINE_RESULT_TEMP_VAR),
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   180
      s4o.print(" = ");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   181
      function_name->accept(*this);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   182
      if (f_decl != NULL) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   183
        /* function being called is overloaded! */
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   184
        s4o.print("__");
406
6381589697ff Fix bug with overloaded function due to literal input values
laurent
parents: 399
diff changeset
   185
        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
   186
        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
   187
      }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   188
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   189
      if (function_type_suffix)
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   190
        function_type_suffix->accept(*this);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   191
      s4o.print("(");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   192
      s4o.indent_right();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   193
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   194
      PARAM_LIST_ITERATOR() {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   195
        if (pt != param_list.begin())
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   196
        s4o.print(",\n" + s4o.indent_spaces);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   197
        if (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
   198
          PARAM_NAME->accept(*this);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   199
        else if (PARAM_VALUE != NULL){
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   200
          s4o.print("&");
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   201
          s4o.print(TEMP_VAR);
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   202
          PARAM_NAME->accept(*this);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   203
        } else {
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   204
          s4o.print("NULL");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   205
         }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   206
      }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   207
      s4o.print(");\n");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   208
      s4o.indent_left();
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   209
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   210
      PARAM_LIST_ITERATOR() {
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   211
        if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   212
             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
   213
             PARAM_VALUE != NULL) {
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   214
          s4o.print(s4o.indent_spaces);
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   215
          print_setter(PARAM_VALUE, PARAM_TYPE, PARAM_NAME);
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   216
          s4o.print(";\n");
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   217
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   218
      }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   219
      s4o.print(s4o.indent_spaces + "return ");
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   220
      s4o.print(INLINE_RESULT_TEMP_VAR);
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   221
      s4o.print(";\n");
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   222
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   223
      s4o.indent_left();
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   224
      s4o.print(s4o.indent_spaces + "}\n\n");
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   225
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   226
      generating_inlinefunction = false;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   227
    }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   228
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   229
  private:
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   230
    /* a small helper function */
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   231
    symbol_c *default_literal_type(symbol_c *symbol) {
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   232
      if (get_datatype_info_c::is_ANY_INT_literal(symbol)) {
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: 690
diff changeset
   233
        return &get_datatype_info_c::lint_type_name;
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   234
      }
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   235
      else if (get_datatype_info_c::is_ANY_REAL_literal(symbol)) {
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: 690
diff changeset
   236
        return &get_datatype_info_c::lreal_type_name;
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   237
      }
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   238
      return symbol;
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   239
    }
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   240
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   241
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   242
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   243
    void *print_getter(symbol_c *symbol) {
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: 500
diff changeset
   244
      unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   245
      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
   246
        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
   247
        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
   248
          s4o.print(GET_EXTERNAL_FB);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   249
        else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   250
          s4o.print(GET_EXTERNAL);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   251
      }
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   252
      else if (vartype == search_var_instance_decl_c::located_vt)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   253
        s4o.print(GET_LOCATED);
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   254
      else
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   255
        s4o.print(GET_VAR);
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   256
      s4o.print("(");
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   257
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   258
      wanted_variablegeneration = complextype_base_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   259
      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: 844
diff changeset
   260
      s4o.print(",");
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   261
      wanted_variablegeneration = complextype_suffix_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   262
      symbol->accept(*this);
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   263
      s4o.print(")");
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   264
      wanted_variablegeneration = expression_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   265
      return NULL;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   266
    }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   267
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   268
    void *print_setter(symbol_c* symbol,
670
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   269
                       symbol_c* type,
400bf52a2691 Deprecate use of search_expression_type_c (prepare for complete removal!).
Mario de Sousa <msousa@fe.up.pt>
parents: 667
diff changeset
   270
                       symbol_c* value) {
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: 500
diff changeset
   271
      unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   272
      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
   273
        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
   274
        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
   275
          s4o.print(SET_EXTERNAL_FB);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   276
         else
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   277
          s4o.print(SET_EXTERNAL);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   278
      }
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 388
diff changeset
   279
      else if (vartype == search_var_instance_decl_c::located_vt)
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 388
diff changeset
   280
        s4o.print(SET_LOCATED);
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 388
diff changeset
   281
      else
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 388
diff changeset
   282
        s4o.print(SET_VAR);
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 388
diff changeset
   283
      s4o.print("(,");
885
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   284
/*
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   285
      wanted_variablegeneration = complextype_base_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   286
      symbol->accept(*this);
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   287
      s4o.print(",");
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   288
      wanted_variablegeneration = expression_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   289
      print_check_function(type, value, NULL, true);
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
   290
      if (analyse_variable_c::contains_complex_type(symbol)) {
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   291
        s4o.print(",");
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   292
        wanted_variablegeneration = complextype_suffix_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   293
        symbol->accept(*this);
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   294
      }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   295
      s4o.print(")");
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   296
      wanted_variablegeneration = expression_vg;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   297
      return NULL;
885
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   298
*/
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   299
      wanted_variablegeneration = complextype_base_vg;
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   300
      symbol->accept(*this);
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   301
      s4o.print(",");
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   302
      if (analyse_variable_c::contains_complex_type(symbol)) {
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   303
        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: 874
diff changeset
   304
        symbol->accept(*this);
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   305
      }
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   306
      s4o.print(",");
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   307
      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: 874
diff changeset
   308
      print_check_function(type, value, NULL, true);
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   309
      s4o.print(")");
b2604fc6d25c Change order of SET_xxx() macros. (this will allow me to simplify the print_setter() methods later on)
mjsousa
parents: 874
diff changeset
   310
      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: 874
diff changeset
   311
      return NULL;
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   312
    }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   313
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   314
    /*********************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   315
    /* B 1.4 - Variables */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   316
    /*********************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   317
    void *visit(symbolic_variable_c *symbol) {
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   318
      unsigned int vartype;
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   319
      if (generating_inlinefunction) {
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   320
        if (wanted_variablegeneration == complextype_base_vg)
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   321
          generate_c_base_c::visit(symbol);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   322
        else if (wanted_variablegeneration == complextype_suffix_vg)
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   323
          return NULL;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   324
        else
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   325
          print_getter(symbol);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   326
      }
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   327
      return NULL;
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   328
    }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   329
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   330
    /********************************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   331
    /* B.1.4.1   Directly Represented Variables */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   332
    /********************************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   333
    // direct_variable: direct_variable_token   {$$ = new direct_variable_c($1);};
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   334
    void *visit(direct_variable_c *symbol) {
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   335
      TRACE("direct_variable_c");
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   336
      if (generating_inlinefunction) {
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   337
        /* Do not use print_token() as it will change everything into uppercase */
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   338
        if (strlen(symbol->value) == 0) ERROR;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   339
        s4o.print(GET_LOCATED);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   340
        s4o.print("(");
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   341
        this->print_variable_prefix();
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   342
        s4o.printlocation(symbol->value + 1);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   343
        s4o.print(")");
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   344
      }
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   345
      return NULL;
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   346
    }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   347
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   348
    /*************************************/
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   349
    /* B.1.4.2   Multi-element Variables */
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   350
    /*************************************/
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   351
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   352
    // SYM_REF2(structured_variable_c, record_variable, field_selector)
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   353
    void *visit(structured_variable_c *symbol) {
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   354
      TRACE("structured_variable_c");
854
13d0b67de111 Code cleanup: move datatype analysis to get_datatype_info_c
Mario de Sousa <msousa@fe.up.pt>
parents: 852
diff changeset
   355
      bool type_is_complex = analyse_variable_c::is_complex_type(symbol->record_variable);
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   356
      if (generating_inlinefunction) {
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   357
        switch (wanted_variablegeneration) {
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   358
          case complextype_base_vg:
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   359
            symbol->record_variable->accept(*this);
410
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 408
diff changeset
   360
            if (!type_is_complex) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   361
                s4o.print(".");
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   362
                symbol->field_selector->accept(*this);
410
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 408
diff changeset
   363
            }
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   364
            break;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   365
          case complextype_suffix_vg:
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   366
            symbol->record_variable->accept(*this);
410
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 408
diff changeset
   367
            if (type_is_complex) {
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 408
diff changeset
   368
              s4o.print(".");
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 408
diff changeset
   369
              symbol->field_selector->accept(*this);
c6486bd97392 Fix bug while using complex type variables in Function and FunctionBlock interface
Laurent Bessard
parents: 408
diff changeset
   370
            }
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   371
            break;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   372
          default:
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   373
            print_getter(symbol);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   374
            break;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   375
        }
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   376
      }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   377
      return NULL;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   378
    }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   379
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   380
    /*  subscripted_variable '[' subscript_list ']' */
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   381
    //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   382
    void *visit(array_variable_c *symbol) {
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   383
      if (generating_inlinefunction) {
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   384
        switch (wanted_variablegeneration) {
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   385
          case complextype_base_vg:
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   386
            symbol->subscripted_variable->accept(*this);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   387
            break;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   388
          case complextype_suffix_vg:
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   389
            symbol->subscripted_variable->accept(*this);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   390
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   391
            current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   392
            if (current_array_type == NULL) ERROR;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   393
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   394
            s4o.print(".table");
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   395
            symbol->subscript_list->accept(*this);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   396
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   397
            current_array_type = NULL;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   398
            break;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   399
          default:
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   400
            print_getter(symbol);
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   401
            break;
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   402
        }
228
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   403
      }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   404
      return NULL;
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   405
    }
43831b683764 Adding support for setter and getter on complex datatype in IL
berem
parents: 221
diff changeset
   406
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   407
    /****************************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   408
    /* B.2 - Language IL (Instruction List) */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   409
    /****************************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   410
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   411
    /***********************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   412
    /* B 2.1 Instructions and Operands */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   413
    /***********************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   414
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   415
    /* | label ':' [il_incomplete_instruction] eol_list */
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   416
    // SYM_REF2(il_instruction_c, label, il_instruction)
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   417
    void *visit(il_instruction_c *symbol) {
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   418
      /* all previous IL instructions should have the same datatype (checked in stage3), so we get the datatype from the first previous IL instruction we find */
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   419
      implicit_variable_current.datatype = (symbol->prev_il_instruction.empty())? NULL : symbol->prev_il_instruction[0]->datatype;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   420
      if (NULL != symbol->il_instruction)  symbol->il_instruction->accept(*this); 
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   421
      implicit_variable_current.datatype = NULL;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   422
      return NULL;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   423
    }
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   424
392
9b88b8b6bccd Fixing generated code for global variables. Adding support for defining global variables with complex type
laurent
parents: 388
diff changeset
   425
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   426
    /* | il_simple_operator [il_operand] */
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   427
    //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   428
    void *visit(il_simple_operation_c *symbol) {
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   429
      symbol->il_simple_operator->accept(*this);
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   430
      return NULL;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   431
    }
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   432
874
b8fa360b376d Fix bug: do not print IL 'label' when generating inline functions (JMPxx label was generating erroneous C code)
mjsousa
parents: 861
diff changeset
   433
    /*  il_jump_operator label */
b8fa360b376d Fix bug: do not print IL 'label' when generating inline functions (JMPxx label was generating erroneous C code)
mjsousa
parents: 861
diff changeset
   434
    // SYM_REF2(il_jump_operation_c, il_jump_operator, label)    
b8fa360b376d Fix bug: do not print IL 'label' when generating inline functions (JMPxx label was generating erroneous C code)
mjsousa
parents: 861
diff changeset
   435
    void *visit(il_jump_operation_c *symbol) {
b8fa360b376d Fix bug: do not print IL 'label' when generating inline functions (JMPxx label was generating erroneous C code)
mjsousa
parents: 861
diff changeset
   436
      return NULL;
b8fa360b376d Fix bug: do not print IL 'label' when generating inline functions (JMPxx label was generating erroneous C code)
mjsousa
parents: 861
diff changeset
   437
    }
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   438
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   439
    void *visit(il_function_call_c *symbol) {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   440
      symbol_c* function_type_prefix = NULL;
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   441
      symbol_c* function_name = NULL;     
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   442
      symbol_c* function_type_suffix = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   443
      DECLARE_PARAM_LIST()
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   444
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   445
      function_call_param_iterator_c function_call_param_iterator(symbol);
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   446
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   447
      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
   448
      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
   449
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   450
      /* determine the base data type returned by the function being called... */
718
a9f8cc778444 Make search_base_type_c a singleton!
Mario de Sousa <msousa@fe.up.pt>
parents: 693
diff changeset
   451
      function_type_prefix = search_base_type_c::get_basetype_decl(f_decl->type_name);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   452
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   453
      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
   454
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   455
      /* 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
   456
       * 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
   457
       */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   458
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   459
      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
   460
      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
   461
        /* flag to remember whether we have already used the value stored in the default variable to pass to the first parameter */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   462
      bool used_defvar = false;       
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   463
        /* 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
   464
      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
   465
      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
   466
        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
   467
          /* 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
   468
           * 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
   469
           * 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
   470
           *
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   471
           * 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
   472
           * 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
   473
           * 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
   474
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   475
          /* 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
   476
           *       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
   477
           *       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
   478
           *       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
   479
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   480
          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
   481
          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
   482
          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
   483
          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
   484
          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
   485
          uint_type_name_c *param_type  = new uint_type_name_c();
380
b78e59ed4269 Fix bug in generate_c_inlinefcall.cc that prevent to use extendible standard functions
laurent
parents: 355
diff changeset
   486
          identifier_c *param_name = new identifier_c(INLINE_PARAM_COUNT);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   487
          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
   488
          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
   489
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   490
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   491
        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
   492
        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
   493
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   494
        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
   495
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   496
        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
   497
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   498
        /* 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
   499
        /* NOTE: the following line of code is not required in this case, but it doesn't
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   500
         * harm to leave it in, as in the case of a non-formal syntax function call,
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   501
         * it will always return NULL.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   502
         * We leave it in in case we later decide to merge this part of the code together
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   503
         * with the function calling code in generate_c_st_c, which does require
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   504
         * the following line...
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   505
         */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   506
        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
   507
          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
   508
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   509
        /* if it is the first parameter in a non-formal function call (which is the
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   510
         * case being handled!), semantics specifies that we should
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   511
         * get the value off the IL default variable!
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   512
         *
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   513
         * However, if the parameter is an implicitly defined EN or ENO parameter, we should not
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   514
         * use the default variable as a source of data to pass to those parameters!
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   515
         */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   516
        if ((param_value == NULL) && (!used_defvar) && !fp_iterator.is_en_eno_param_implicit()) {
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   517
	  if (NULL == implicit_variable_current.datatype) ERROR;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   518
          param_value = &this->implicit_variable_current;
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   519
          used_defvar = true;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   520
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   521
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   522
        /* 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
   523
        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
   524
          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
   525
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   526
        
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   527
        /* 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
   528
         * 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
   529
         * have reached the end, and should simply jump out of the for loop.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   530
         */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   531
        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
   532
          break;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   533
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   534
      
844
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   535
        /* We do not yet support embedded IL lists, so we abort the compiler if we find one */
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   536
        /* Note that in IL function calls the syntax does not allow embeded IL lists, so this check is not necessary here! */
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   537
        /*
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   538
        {simple_instr_list_c *instruction_list = dynamic_cast<simple_instr_list_c *>(param_value);
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   539
         if (NULL != instruction_list) STAGE4_ERROR(param_value, param_value, "The compiler does not yet support formal invocations in IL that contain embedded IL lists. Aborting!");
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   540
        }
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   541
        */
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   542
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   543
        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
   544
          /* 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
   545
          /* 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
   546
          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
   547
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   548
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   549
        ADD_PARAM_LIST(param_name, param_value, param_type, 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
   550
      } /* for(...) */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   551
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   552
      if (function_call_param_iterator.next_nf() != NULL) ERROR;
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   553
      if (NULL == function_type_prefix) ERROR;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   554
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   555
      bool has_output_params = false;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   556
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   557
      PARAM_LIST_ITERATOR() {
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   558
        if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   559
             PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   560
             PARAM_VALUE != NULL) {
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   561
          has_output_params = true;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   562
        }
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   563
      }
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   564
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   565
      /* 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
   566
      /* (fdecl_mutiplicity > 1)  => calling overloaded function */
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   567
      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
   568
      if (fdecl_mutiplicity == 0) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   569
      if (fdecl_mutiplicity == 1) 
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   570
        /* function being called is NOT overloaded! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   571
        f_decl = NULL; 
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   572
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   573
      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
   574
        generate_inline(function_name, function_type_prefix, function_type_suffix, param_list, f_decl);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   575
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   576
      CLEAR_PARAM_LIST()
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   577
      return NULL;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   578
    }
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   579
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   580
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   581
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   582
    /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   583
    //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused)
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   584
    void *visit(il_expression_c *symbol) {
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   585
      /* We will be recursevely interpreting an instruction list, so we store a backup of the implicit_variable_result/current.
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   586
       * Notice that they will be overwriten while processing the parenthsized instruction list.
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   587
       */
690
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   588
      // il_default_variable_c old_implicit_variable_current = this->implicit_variable_current;      // no longer needed as we do not call symbol->il_expr_operator->accept(*this);
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   589
      
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   590
      /* Stage2 will insert an artificial (and equivalent) LD <il_operand> to the simple_instr_list if necessary. We can therefore ignore the 'il_operand' entry! */
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   591
      //if (NULL != symbol->il_operand) { do nothing!! }
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   592
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   593
      /* Now do the parenthesised instructions... */
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   594
      /* NOTE: the following code line will get the variable this->implicit_variable_current.datatype updated!  */
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   595
      symbol->simple_instr_list->accept(*this);
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   596
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   597
      /* Now do the operation, using the previous result! */
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   598
      /* NOTE: Actually, we do not need to call this, as it can never be a function call, which is what we are handling here... */
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   599
      // this->implicit_variable_current.datatype = old_current_default_variable_data_type;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   600
      // symbol->il_expr_operator->accept(*this);
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   601
      return NULL;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   602
    }
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   603
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   604
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   605
    /* | function_name '(' eol_list [il_param_list] ')' */
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   606
    // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   607
    void *visit(il_formal_funct_call_c *symbol) {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   608
      symbol_c* function_type_prefix = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   609
      symbol_c* function_name = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   610
      symbol_c* function_type_suffix = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   611
      DECLARE_PARAM_LIST()
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   612
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   613
      function_call_param_iterator_c function_call_param_iterator(symbol);
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   614
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   615
      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
   616
      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
   617
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   618
      /* determine the base data type returned by the function being called... */
718
a9f8cc778444 Make search_base_type_c a singleton!
Mario de Sousa <msousa@fe.up.pt>
parents: 693
diff changeset
   619
      function_type_prefix = search_base_type_c::get_basetype_decl(f_decl->type_name);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   620
      if (NULL == function_type_prefix) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   621
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   622
      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
   623
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   624
      /* 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
   625
       * 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
   626
       */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   627
      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
   628
      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
   629
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   630
        /* 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
   631
      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
   632
      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
   633
        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
   634
          /* 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
   635
           * 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
   636
           * 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
   637
           *
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   638
           * 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
   639
           * 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
   640
           * 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
   641
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   642
          /* 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
   643
           *       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
   644
           *       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
   645
           *       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
   646
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   647
          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
   648
          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
   649
          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
   650
          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
   651
          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
   652
          uint_type_name_c *param_type  = new uint_type_name_c();
380
b78e59ed4269 Fix bug in generate_c_inlinefcall.cc that prevent to use extendible standard functions
laurent
parents: 355
diff changeset
   653
          identifier_c *param_name = new identifier_c(INLINE_PARAM_COUNT);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   654
          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
   655
          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
   656
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   657
        
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   658
        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
   659
          /* 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
   660
           * 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
   661
           * 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
   662
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   663
          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
   664
          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
   665
          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
   666
          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
   667
          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
   668
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   669
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   670
        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
   671
        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
   672
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   673
        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
   674
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   675
        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
   676
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   677
        /* 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
   678
        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
   679
          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
   680
      
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   681
        /* 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
   682
        /* NOTE: the following line of code is not required in this case, but it doesn't
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   683
         * harm to leave it in, as in the case of a formal syntax function call,
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   684
         * it will always return NULL.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   685
         * We leave it in in case we later decide to merge this part of the code together
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   686
         * with the function calling code in generate_c_st_c, which does require
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   687
         * the following line...
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   688
         */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   689
        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
   690
          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
   691
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   692
        
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   693
        /* 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
   694
         * 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
   695
         * have reached the end, and should simply jump out of the for loop.
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   696
         */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   697
        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
   698
          break;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   699
        }
844
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   700
        
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   701
        /* We do not yet support embedded IL lists, so we abort the compiler if we find one */
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   702
        {simple_instr_list_c *instruction_list = dynamic_cast<simple_instr_list_c *>(param_value);
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   703
         if (NULL != instruction_list) STAGE4_ERROR(param_value, param_value, "The compiler does not yet support formal invocations in IL that contain embedded IL lists. Aborting!");
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   704
        }
39c755f41ee8 Generate correct error message when encountering IL lists embedded in IL formal invocations.
mjsousa
parents: 793
diff changeset
   705
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   706
        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
   707
          /* 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
   708
          /* 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
   709
          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
   710
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   711
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   712
        ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   713
      }
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   714
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   715
      if (function_call_param_iterator.next_nf() != NULL) ERROR;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   716
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   717
      bool has_output_params = false;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   718
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   719
      PARAM_LIST_ITERATOR() {
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   720
        if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   721
             PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   722
             PARAM_VALUE != NULL) {
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   723
          has_output_params = true;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   724
        }
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   725
      }
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   726
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   727
      /* 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
   728
      /* (fdecl_mutiplicity > 1)  => calling overloaded function */
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   729
      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
   730
      if (fdecl_mutiplicity == 0) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   731
      if (fdecl_mutiplicity == 1) 
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   732
        /* function being called is NOT overloaded! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   733
        f_decl = NULL; 
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   734
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   735
      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
   736
        generate_inline(function_name, function_type_prefix, function_type_suffix, param_list, f_decl);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   737
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   738
      CLEAR_PARAM_LIST()
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   739
      return NULL;
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   740
    }
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   741
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   742
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   743
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   744
    /* | simple_instr_list il_simple_instruction */
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   745
    // SYM_LIST(simple_instr_list_c)
690
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   746
    void *visit(simple_instr_list_c *symbol) {return iterator_visitor_c::visit(symbol);}
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   747
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   748
453
4733f662362a More changes for support of semantic verification of il_expressions
Mario de Sousa <msousa@fe.up.pt>
parents: 392
diff changeset
   749
    // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   750
    void *visit(il_simple_instruction_c *symbol) {
690
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   751
      /* all previous IL instructions should have the same datatype (checked in stage3), so we get the datatype from the first previous IL instruction we find */
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   752
      implicit_variable_current.datatype = (symbol->prev_il_instruction.empty())? NULL : symbol->prev_il_instruction[0]->datatype;
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   753
      symbol->il_simple_instruction->accept(*this);
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   754
      implicit_variable_current.datatype = NULL;
6156ee2b4e32 Correctly generate C code for IL expressions, i.e. IL instructions inside parenthesis.
Mario de Sousa <msousa@fe.up.pt>
parents: 683
diff changeset
   755
      return NULL;      
453
4733f662362a More changes for support of semantic verification of il_expressions
Mario de Sousa <msousa@fe.up.pt>
parents: 392
diff changeset
   756
    }
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   757
683
2d96a47a75b1 Code cleanup: get the datatype of each entity off symbol->datatype (set in stage3 datatype checking), instead of determining it again in stage4. (NOTE: handling of IL parenthesized expressions is not working! Will be fixed later.)
Mario de Sousa <msousa@fe.up.pt>
parents: 670
diff changeset
   758
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   759
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   760
    /***************************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   761
    /* B.3 - Language ST (Structured Text) */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   762
    /***************************************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   763
    /***********************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   764
    /* B 3.1 - Expressions */
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   765
    /***********************/
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   766
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   767
    void *visit(statement_list_c *symbol) {
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   768
      function_call_iterator_c fc_iterator(symbol);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   769
      symbol_c* function_call;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   770
      while ((function_call = fc_iterator.next()) != NULL) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   771
        function_call->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   772
      }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   773
      return NULL;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   774
    }
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   775
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   776
    void *visit(function_invocation_c *symbol) {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   777
      symbol_c* function_type_prefix = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   778
      symbol_c* function_name = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   779
      symbol_c* function_type_suffix = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   780
      DECLARE_PARAM_LIST()
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   781
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   782
      symbol_c *parameter_assignment_list = NULL;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   783
      if (NULL != symbol->   formal_param_list) parameter_assignment_list = symbol->   formal_param_list;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   784
      if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   785
      if (NULL == parameter_assignment_list) ERROR;
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   786
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   787
      function_call_param_iterator_c function_call_param_iterator(symbol);
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   788
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   789
      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
   790
      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
   791
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   792
      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
   793
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   794
      /* determine the base data type returned by the function being called... */
718
a9f8cc778444 Make search_base_type_c a singleton!
Mario de Sousa <msousa@fe.up.pt>
parents: 693
diff changeset
   795
      function_type_prefix = search_base_type_c::get_basetype_decl(f_decl->type_name);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   796
      if (NULL == function_type_prefix) ERROR;
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
      /* 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
   799
       * 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
   800
       */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   801
      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
   802
      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
   803
        /* 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
   804
      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
   805
      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
   806
        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
   807
          /* 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
   808
           * 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
   809
           * 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
   810
           *
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   811
           * 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
   812
           * 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
   813
           * 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
   814
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   815
          /* 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
   816
           *       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
   817
           *       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
   818
           *       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
   819
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   820
          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
   821
          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
   822
          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
   823
          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
   824
          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
   825
          uint_type_name_c *param_type  = new uint_type_name_c();
380
b78e59ed4269 Fix bug in generate_c_inlinefcall.cc that prevent to use extendible standard functions
laurent
parents: 355
diff changeset
   826
          identifier_c *param_name = new identifier_c(INLINE_PARAM_COUNT);
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   827
          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
   828
          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
   829
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   830
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   831
        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
   832
          /* 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
   833
           * 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
   834
           * 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
   835
           */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   836
          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
   837
          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
   838
          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
   839
          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
   840
          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
   841
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   842
        
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   843
        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
   844
        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
   845
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   846
        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
   847
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   848
        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
   849
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   850
        /* 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
   851
        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
   852
          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
   853
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   854
        /* 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
   855
        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
   856
          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
   857
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   858
        
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   859
        /* 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
   860
         * 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
   861
         * have reached the end, and should simply jump out of the for loop.
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   862
         */
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   863
        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
   864
          break;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   865
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   866
    
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   867
        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
   868
          /* 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
   869
          /* 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
   870
          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
   871
        }
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   872
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   873
        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
   874
      } /* for(...) */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   875
      // symbol->parameter_assignment->accept(*this);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   876
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   877
      if (function_call_param_iterator.next_nf() != NULL) ERROR;
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   878
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   879
      bool has_output_params = false;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   880
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   881
      PARAM_LIST_ITERATOR() {
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   882
        if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   883
             PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   884
             PARAM_VALUE != NULL) {
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   885
          has_output_params = true;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   886
        }
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   887
      }
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   888
350
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   889
      /* 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
   890
      /* (fdecl_mutiplicity > 1)  => calling overloaded function */
5dc33058e041 Fix counting bug in dsymbtable_c
Mario de Sousa <msousa@fe.up.pt>
parents: 718
diff changeset
   891
      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
   892
      if (fdecl_mutiplicity == 0) ERROR;
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   893
      if (fdecl_mutiplicity == 1) 
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   894
        /* function being called is NOT overloaded! */
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   895
        f_decl = NULL; 
2c3c4dc34979 Support for semantic verification of calls to standard functions.
Mario de Sousa <msousa@fe.up.pt>
parents: 336
diff changeset
   896
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   897
      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
   898
        generate_inline(function_name, function_type_prefix, function_type_suffix, param_list, f_decl);
235
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   899
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   900
      CLEAR_PARAM_LIST()
ed66dc50f31a Bug on generate_c_inlinefcall fixed
laurent
parents: 234
diff changeset
   901
337
9a2262fb7994 Fix bug with inline function call in POU programmed in IL
laurent
parents: 336
diff changeset
   902
      return NULL;
219
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   903
    }
9bb38736f126 Adding support for declare, init, get and set macros
laurent
parents:
diff changeset
   904
706
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   905
    /*********************************************/
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   906
    /* B.1.6  Sequential function chart elements */
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   907
    /*********************************************/
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   908
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   909
    void *visit(initial_step_c *symbol) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   910
        return NULL;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   911
    }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   912
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   913
    void *visit(step_c *symbol) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   914
        return NULL;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   915
    }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   916
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   917
    void *visit(transition_c *symbol) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   918
        return symbol->transition_condition->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   919
    }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   920
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   921
    void *visit(transition_condition_c *symbol) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   922
        // Transition condition is in IL
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   923
        if (symbol->transition_condition_il != NULL) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   924
            symbol->transition_condition_il->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   925
        }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   926
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   927
        // Transition condition is in ST
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   928
        if (symbol->transition_condition_st != NULL) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   929
            function_call_iterator_c fc_iterator(symbol->transition_condition_st);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   930
            symbol_c* function_call;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   931
            while ((function_call = fc_iterator.next()) != NULL) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   932
                function_call->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   933
            }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   934
        }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   935
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   936
        return NULL;
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   937
    }
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   938
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   939
    void *visit(action_c *symbol) {
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   940
        return symbol->function_block_body->accept(*this);
31553c22f318 Adding support for declaring function block instances as global and external variables
Laurent Bessard
parents: 625
diff changeset
   941
    }
388
17eaad42ba88 Fix bug in generate_c_inlinefcall. Was not handling textual SFC elements
laurent
parents: 383
diff changeset
   942
234
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   943
};  /* generate_c_inlinefcall_c */
e4d31cd0e6d8 Using function_call_iterator in generate_c_inlinefcall instead of creating a new visitor
laurent
parents: 233
diff changeset
   944