generate_IEC_std.py
author laurent
Sat, 12 Dec 2009 20:37:35 +0100
changeset 484 acef952101a5
parent 458 6e4ad5af894d
child 486 0daecbcbfbf4
permissions -rwxr-xr-x
Adding support for excluding option when not available according to IEC 61131 standard
Disabling use of complex types (array and structure) for POU interface until matiec can generate code
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     1
#!/usr/bin/env python
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     2
# -*- coding: utf-8 -*-
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     3
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     5
#based on the plcopen standard. 
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     6
#
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     8
#
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
     9
#See COPYING file for copyrights details.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    10
#
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    11
#This library is free software; you can redistribute it and/or
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    12
#modify it under the terms of the GNU General Public
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    13
#License as published by the Free Software Foundation; either
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    15
#
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    16
#This library is distributed in the hope that it will be useful,
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    19
#General Public License for more details.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    20
#
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    21
#You should have received a copy of the GNU General Public
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    22
#License along with this library; if not, write to the Free Software
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    24
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    25
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    26
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
    27
 THIS CODE GENARATES C++ CODE FOR IEC2C COMPILER
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    28
 GENERATED CODE IS :
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    29
 
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    30
function_type_decl.h - the long enumeration of std function types
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    31
get_function_type_decl.c - the funct that return enumerated according function name
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
    32
st_code_gen.c - part of generate_c_st_c::visit(function_invocation)
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    33
                responsible to generate C code for std lib calls
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
    34
il_code_gen.c - part of generate_c_il_c::visit(il_function_call)
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    35
                responsible to generate C code for std lib calls
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    36
search_type_code.c - called by search_expression_type_c::visit(function_invocation) 
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    37
                     return type symbol for std function invocation
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    38
 
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    39
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    40
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    41
# Get definitions
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    42
from plcopen.structures import *
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    43
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
    44
if len(sys.argv) != 2 :
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
    45
    print "Usage: " + sys.argv[0] + " path_name\n -> create files in path_name"
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
    46
    sys.exit(0)
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
    47
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    48
#import pprint
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    49
#pp = pprint.PrettyPrinter(indent=4)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    50
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    51
matiec_header = """/*
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    52
 * (c) 2008 Edouard TISSERANT
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    53
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    54
 * Offered to the public under the terms of the GNU General Public License
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    55
 * as published by the Free Software Foundation; either version 2 of the
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    56
 * License, or (at your option) any later version.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    57
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    58
 * This program is distributed in the hope that it will be useful, but
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    59
 * WITHOUT ANY WARRANTY; without even the implied warranty of
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    60
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    61
 * Public License for more details.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    62
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    63
 * This code is made available on the understanding that it will not be
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    64
 * used in safety-critical situations without a full and competent review.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    65
 */
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    66
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    67
/*
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    68
 * An IEC 61131-3 IL and ST compiler.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    69
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    70
 * Based on the
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    71
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    72
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    73
 */
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    74
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    75
/****
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    76
 * IEC 61131-3 standard function library
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    77
 * generated code, do not edit by hand
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    78
 */
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    79
 
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    80
 """
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    81
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    82
matiec_lesser_header = """/*
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    83
 * (c) 2008 Edouard TISSERANT
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    84
 *
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    85
 * Offered to the public under the terms of the GNU Lesser General Public
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    86
 * License as published by the Free Software Foundation; either version 2
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    87
 * of the License, or (at your option) any later version.
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    88
 *
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    89
 * This program is distributed in the hope that it will be useful, but
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    90
 * WITHOUT ANY WARRANTY; without even the implied warranty of
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    91
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    92
 * General Public License for more details.
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    93
 *
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    94
 * This code is made available on the understanding that it will not be
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    95
 * used in safety-critical situations without a full and competent review.
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    96
 */
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    97
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    98
/****
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    99
 * IEC 61131-3 standard function library
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
   100
 * generated code, do not edit by hand
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
   101
 */
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
   102
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   103
 """
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   104
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   105
def ANY_to_compiler_test_type_GEN(typename, paramname):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   106
    """
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   107
    Convert ANY_XXX IEC type declaration into IEC2C's generated type test.
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   108
    This tests are defined in search_expression_type.cc 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   109
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   110
    return {"ANY" : "",
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   111
    "ANY_BIT" : "if(%(paramname)s_type_symbol == NULL || search_expression_type->is_binary_type(%(paramname)s_type_symbol))",
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   112
    "ANY_NBIT" : "if(%(paramname)s_type_symbol == NULL || search_expression_type->is_nbinary_type(%(paramname)s_type_symbol))",
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   113
    "ANY_NUM" : "if(%(paramname)s_type_symbol == NULL || search_expression_type->is_num_type(%(paramname)s_type_symbol))",
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   114
    "ANY_REAL" : "if(%(paramname)s_type_symbol == NULL || search_expression_type->is_real_type(%(paramname)s_type_symbol))",
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   115
    "ANY_INT" : "if(%(paramname)s_type_symbol == NULL || search_expression_type->is_integer_type(%(paramname)s_type_symbol))"
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   116
    }.get(typename,
40
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   117
        #"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   118
        "if(%(paramname)s_type_symbol == NULL || search_expression_type->is_same_type(&search_constant_type_c::%(typename)s_type_name, last_type_symbol))")%{
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   119
                "paramname" : paramname, "typename": typename.lower()}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   120
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   121
def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   122
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   123
    This function generate visit(function_invocation) code for 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   124
        - ST code generator
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   125
        - IL code generator
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   126
        - search_expression_type class for ST
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   127
        - search_expression_type class for IL
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   128
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   129
    Input data is a 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   130
    "{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   131
    nested dictionary structure.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   132
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   133
    if type(fdecls) != type(tuple()):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   134
        res = ""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   135
        for Paramname, ParamTypes in fdecls.iteritems():
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   136
            if do_il:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   137
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   138
{"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   139
                if not do_type_search_only:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   140
                    res += """
458
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   141
    identifier_c %(input_name)s_param_name("%(input_name)s");
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   142
    /* Get the value from a foo(<param_name> = <param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   143
    symbol_c *%(input_name)s_param_value = &this->default_variable_name;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   144
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   145
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   146
    symbol_c *%(input_name)s_type_symbol = param_data_type;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   147
    last_type_symbol = param_data_type;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   148
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   149
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   150
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   151
{
458
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   152
    identifier_c %(input_name)s_param_name("%(input_name)s");
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   153
    /* Get the value from a foo(<param_name> = <param_value>) style call */
458
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   154
    symbol_c *%(input_name)s_param_value = function_call_param_iterator.search_f(&%(input_name)s_param_name);
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   155
    symbol_c *%(input_name)s_type_symbol = NULL;
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   156
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   157
    /* Get the value from a foo(<param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   158
    if (%(input_name)s_param_value == NULL)
406
af07b091bbee Modify code generated to fit latest modifications in matiec
laurent
parents: 404
diff changeset
   159
      %(input_name)s_param_value = function_call_param_iterator.next_nf();
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   160
    if (%(input_name)s_param_value != NULL) {
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   161
      %(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value);
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   162
      last_type_symbol = last_type_symbol && %(input_name)s_type_symbol && search_expression_type->is_same_type(%(input_name)s_type_symbol, last_type_symbol) ? search_expression_type->common_type(%(input_name)s_type_symbol, last_type_symbol) : %(input_name)s_type_symbol ;
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   163
    }
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   164
"""%{"input_name":Paramname}
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   165
                                
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   166
            for ParamType,NextParamDecl in ParamTypes.iteritems():
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   167
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   168
                res += """    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   169
    %(type_test)s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   170
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   171
%(if_good_type_code)s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   172
    }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   173
"""%{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   174
    "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   175
    "if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n    ')}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   176
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   177
            res += """    
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   178
    
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   179
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   180
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   181
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   182
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   183
        return res.replace('\n','\n'+indent)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   184
    else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   185
        res = "\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   186
        fdecl=fdecls[0]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   187
        
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   188
        if not do_type_search_only:
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   189
            code_gen = eval(fdecl["python_eval_c_code_format"])
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   190
            
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   191
            if code_gen[1] is not None:
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   192
                res += "function_name = (symbol_c*)(new pragma_c(\"%s\"));\n"%code_gen[1]
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   193
            if fdecl["extensible"]:
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   194
                res += """
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   195
if (nb_param < %(min_nb_param)d)
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   196
  nb_param = %(min_nb_param)d;
291
e580c945c62a Bug with standard functions nb_params count on Windows fixed
lbessard
parents: 284
diff changeset
   197
char* nb_param_str = new char[10];
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   198
sprintf(nb_param_str, "%%d", nb_param);
458
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   199
identifier_c nb_param_name("nb_param");
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   200
ADD_PARAM_LIST(&nb_param_name, (symbol_c*)(new integer_c((const char *)nb_param_str)), (symbol_c*)(new int_type_name_c()), function_param_iterator_c::direction_in)
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   201
"""%{"min_nb_param" : len(fdecl["inputs"])}
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   202
            for paramname,paramtype,unused in fdecl["inputs"]:
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   203
                res += """
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   204
if (%(input_name)s_type_symbol == NULL)
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   205
  %(input_name)s_type_symbol = last_type_symbol;
458
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   206
ADD_PARAM_LIST(&%(input_name)s_param_name, %(input_name)s_param_value, %(input_name)s_type_symbol, function_param_iterator_c::direction_in)
307
fd1f6ae26d4f Adding support for cancelling code generation of function with no input connected
lbessard
parents: 291
diff changeset
   207
"""%{"input_name" : paramname}
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   208
            if fdecl["extensible"]:
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   209
                res += """
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   210
int base_num = %d;
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   211
symbol_c *param_value = NULL;
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   212
do{
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   213
    char my_name[10];
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   214
    sprintf(my_name, "IN%%d", base_num++);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   215
    identifier_c param_name(my_name);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   216
    
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   217
    /* Get the value from a foo(<param_name> = <param_value>) style call */
406
af07b091bbee Modify code generated to fit latest modifications in matiec
laurent
parents: 404
diff changeset
   218
    param_value = function_call_param_iterator.search_f(&param_name);
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   219
    
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   220
    /* Get the value from a foo(<param_value>) style call */
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   221
    if (param_value == NULL)
406
af07b091bbee Modify code generated to fit latest modifications in matiec
laurent
parents: 404
diff changeset
   222
      param_value = function_call_param_iterator.next_nf();
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   223
    if (param_value != NULL){
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   224
        symbol_c *current_type_symbol = search_expression_type->get_type(param_value);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   225
        last_type_symbol = last_type_symbol && search_expression_type->is_same_type(current_type_symbol, last_type_symbol) ? search_expression_type->common_type(current_type_symbol, last_type_symbol) : current_type_symbol ;
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   226
    
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   227
        /*Function specific CODE */
458
6e4ad5af894d Adding support for declare, init, get and set macros
laurent
parents: 410
diff changeset
   228
        ADD_PARAM_LIST(&param_name, param_value, current_type_symbol, function_param_iterator_c::direction_in)
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   229
    }
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   230
    
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   231
}while(param_value != NULL);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   232
"""%(fdecl["baseinputnumber"] + 2)
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   233
        
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   234
        result_type_rule = fdecl["return_type_rule"]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   235
        res += {
410
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   236
            "copy_input" : """symbol_c * return_type_symbol;
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   237
if (search_expression_type->is_literal_integer_type(last_type_symbol))
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   238
    return_type_symbol = &search_constant_type_c::%s_type_name;
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   239
else
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   240
    return_type_symbol = last_type_symbol;
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   241
"""%({True: "lword", False: """lint_type_name;
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   242
else if (search_expression_type->is_literal_real_type(last_type_symbol))
e1d4e9a93186 Bug when trying to use standard functions on literals only fixed
laurent
parents: 406
diff changeset
   243
    return_type_symbol = &search_constant_type_c::lreal"""}[reduce(lambda x, y: x or y, [paramtype == "ANY_BIT" for paramname,paramtype,unused in fdecl["inputs"]], False)]),
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   244
            "defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(),
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   245
            }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   246
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   247
        if not do_type_search_only:
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   248
            if code_gen[0] is not None:
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   249
                res += "function_type_prefix = %s;\n"%{"return_type" : "return_type_symbol"}.get(code_gen[0], "(symbol_c*)(new pragma_c(\"%s\"))"%code_gen[0])
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   250
            if code_gen[2] is not None:
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   251
                res += "function_type_suffix = %s_symbol;\n"%{"common_type" : "last_type"}.get(code_gen[2], code_gen[2])
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   252
            res += "break;\n"
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   253
        else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   254
            res += "return return_type_symbol;\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   255
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   256
        return res.replace('\n','\n'+indent)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   257
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   258
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   259
###                                                             ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   260
###                           MAIN                              ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   261
###                                                             ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   262
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   263
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   264
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   265
Reorganize std_decl from structure.py
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   266
into a nested dictionnary structure (i.e. a tree):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   267
"{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   268
Keep ptrack of original declaration order in a 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   269
separated list called official_order
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   270
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   271
std_fdecls = {}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   272
official_order = []
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   273
for section in std_decl:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   274
    for fdecl in section["list"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   275
        if len(official_order)==0 or official_order[-1] != fdecl["name"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   276
            official_order.append(fdecl["name"])
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   277
        # store all func by name in a dict
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   278
        std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   279
        current = std_fdecls_fdecl_name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   280
        for i in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   281
            current[i[0]] = current.get(i[0], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   282
            current = current[i[0]]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   283
            last = current
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   284
            current[i[1]] = current.get(i[1], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   285
            current = current[i[1]]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   286
        last[i[1]]=(fdecl,)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   287
        std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   288
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   289
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   290
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   291
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   292
Generate the long enumeration of std function types
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   293
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   294
function_type_decl =  matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   295
typedef enum {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   296
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   297
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   298
    function_type_decl += "    function_"+fname.lower()+",\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   299
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   300
function_type_decl += """    function_none
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   301
} function_type_t;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   302
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   303
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   304
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   305
Generate the funct that return enumerated according function name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   306
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   307
get_function_type_decl = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   308
function_type_t get_function_type(identifier_c *function_name) {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   309
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   310
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   311
    get_function_type_decl += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   312
if (!strcasecmp(function_name->value, "%s"))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   313
    return function_%s;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   314
"""%(fname,fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   315
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   316
get_function_type_decl += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   317
    else return function_none;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   318
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   319
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   320
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   321
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   322
"""
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   323
Generate the part of generate_c_st_c::visit(function_invocation)
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   324
that is responsible to generate C code for std lib calls.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   325
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   326
st_code_gen = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   327
switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   328
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   329
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   330
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   331
    st_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   332
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   333
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   334
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   335
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   336
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   337
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   338
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   339
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   340
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   341
    st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   342
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   343
    st_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   344
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   345
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   346
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   347
st_code_gen +=  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   348
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   349
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   350
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   351
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   352
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   353
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   354
"""
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   355
Generate the part of generate_c_il_c::visit(il_function_call)
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   356
that is responsible to generate C code for std lib calls.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   357
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   358
il_code_gen = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   359
switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   360
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   361
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   362
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   363
    il_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   364
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   365
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   366
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   367
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   368
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   369
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   370
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   371
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   372
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   373
    il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   374
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   375
    il_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   376
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   377
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   378
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   379
il_code_gen +=  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   380
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   381
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   382
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   383
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   384
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   385
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   386
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   387
Generate the part of search_expression_type_c::visit(function_invocation)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   388
that is responsible of returning type symbol for function invocation.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   389
"""
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   390
search_type_code = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   391
406
af07b091bbee Modify code generated to fit latest modifications in matiec
laurent
parents: 404
diff changeset
   392
void *search_expression_type_c::compute_standard_function_default(function_invocation_c *st_symbol = NULL, il_formal_funct_call_c *il_symbol = NULL) {
284
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   393
  function_type_t current_function_type;
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   394
  function_call_param_iterator_c *tmp_function_call_param_iterator;
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   395
  if (st_symbol != NULL && il_symbol == NULL) {
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   396
    current_function_type = get_function_type((identifier_c *)st_symbol->function_name);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   397
    tmp_function_call_param_iterator = new function_call_param_iterator_c(st_symbol);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   398
  }
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   399
  else if (st_symbol == NULL && il_symbol != NULL) {
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   400
    current_function_type = get_function_type((identifier_c *)il_symbol->function_name);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   401
    tmp_function_call_param_iterator = new function_call_param_iterator_c(il_symbol);
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   402
  }
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   403
  else
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   404
    ERROR;
6cf858411d3a Adding support for EN/ENO in standard functions
lbessard
parents: 167
diff changeset
   405
  function_call_param_iterator_c function_call_param_iterator(*tmp_function_call_param_iterator);
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   406
  search_expression_type_c* search_expression_type = this;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   407
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   408
  switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   409
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   410
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   411
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   412
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   413
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   414
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   415
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   416
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   417
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   418
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   419
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   420
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   421
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   422
    search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   423
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   424
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   425
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   426
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   427
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   428
search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   429
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   430
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   431
  }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   432
  return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   433
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   434
406
af07b091bbee Modify code generated to fit latest modifications in matiec
laurent
parents: 404
diff changeset
   435
void *search_expression_type_c::compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) {
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   436
  
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   437
  function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   438
  function_call_param_iterator_c function_call_param_iterator(symbol);  
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   439
  search_expression_type_c* search_expression_type = this;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   440
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   441
  switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   442
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   443
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   444
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   445
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   446
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   447
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   448
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   449
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   450
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   451
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   452
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   453
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   454
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   455
    search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   456
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   457
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   458
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   459
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   460
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   461
search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   462
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   463
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   464
  }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   465
  return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   466
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   467
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   468
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   469
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   470
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   471
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   472
"""
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   473
Generate the standard_function_names[] for inclusion in bizon generated code
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   474
"""
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   475
standard_function_names = matiec_header + """
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   476
const char *standard_function_names[] = {
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   477
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   478
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   479
    standard_function_names += "\""+fname+"\",\n"
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   480
standard_function_names += """
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   481
/* end of array marker! Do not remove! */
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   482
NULL
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   483
};
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   484
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   485
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   486
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   487
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   488
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   489
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   490
"""
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   491
Generate the C implementation of the IEC standard function library.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   492
"""
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   493
iec_std_lib_generated = matiec_lesser_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   494
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   495
/* Macro that expand to subtypes */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   496
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   497
for typename, parenttypename in TypeHierarchy_list:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   498
    if (typename.startswith("ANY")):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   499
        iec_std_lib_generated += "#define " + typename + "(DO)"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   500
        for typename2, parenttypename2 in TypeHierarchy_list:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   501
            if(parenttypename2 == typename):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   502
                if(typename2.startswith("ANY")):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   503
                    iec_std_lib_generated +=  " " + typename2 + "(DO)"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   504
                else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   505
                    iec_std_lib_generated +=  " DO(" + typename2 + ")"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   506
        iec_std_lib_generated +=  "\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   507
    else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   508
        break
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   509
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   510
# Now, print that out, or write to files from sys.argv
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   511
for path, name, ext in [
404
d5a5eaea7844 Bug with type conversion EN/ENO not evaluated fixed
laurent
parents: 307
diff changeset
   512
        ('absyntax_utils','function_type_decl','h'),
d5a5eaea7844 Bug with type conversion EN/ENO not evaluated fixed
laurent
parents: 307
diff changeset
   513
        ('absyntax_utils','get_function_type_decl','c'),
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   514
        ('stage4/generate_c','st_code_gen','c'),
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   515
        ('stage4/generate_c','il_code_gen','c'),
404
d5a5eaea7844 Bug with type conversion EN/ENO not evaluated fixed
laurent
parents: 307
diff changeset
   516
        ('absyntax_utils','search_type_code','c'),
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   517
        ('stage1_2','standard_function_names','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   518
        ('lib', 'iec_std_lib_generated','h')
26
36d378bd852e Stage4 changes according to new STD lib implementation
etisserant
parents: 25
diff changeset
   519
        ]:
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   520
    fd = open(os.path.join(sys.argv[1], path, name+'.'+ext),'w')
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   521
    fd.write(eval(name))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   522
    fd.close()
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   523
    
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   524
#print "/* Code to eventually paste in iec_std_lib.h if type hierarchy changed */"
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   525
#print "/* you also have to change iec_std_lib.h according to new types        */\n\n"
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   526
#print iec_std_lib_generated