generate_IEC_std.py
author lbessard
Sun, 07 Sep 2008 15:27:53 +0200
changeset 252 166ee9d2e233
parent 167 893ff98ab3be
child 284 6cf858411d3a
permissions -rw-r--r--
Fix bug in popup menu and function block types in Variable Panel
Adding support for get value from project debug copy only when program transferred
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
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    44
#import pprint
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    45
#pp = pprint.PrettyPrinter(indent=4)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    46
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    47
matiec_header = """/*
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    48
 * (c) 2008 Edouard TISSERANT
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    49
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    50
 * Offered to the public under the terms of the GNU General Public License
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    51
 * as published by the Free Software Foundation; either version 2 of the
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    52
 * License, or (at your option) any later version.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    53
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    54
 * This program is distributed in the hope that it will be useful, but
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    55
 * WITHOUT ANY WARRANTY; without even the implied warranty of
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    56
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    57
 * Public License for more details.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    58
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    59
 * This code is made available on the understanding that it will not be
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    60
 * used in safety-critical situations without a full and competent review.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    61
 */
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    62
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    63
/*
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    64
 * An IEC 61131-3 IL and ST compiler.
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    65
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    66
 * Based on the
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    67
 * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    68
 *
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    69
 */
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    70
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    71
/****
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    72
 * IEC 61131-3 standard function library
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    73
 * generated code, do not edit by hand
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    74
 */
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    75
 
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    76
 """
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
    77
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    78
matiec_lesser_header = """/*
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    79
 * (c) 2008 Edouard TISSERANT
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    80
 *
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    81
 * 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
    82
 * License as published by the Free Software Foundation; either version 2
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    83
 * of the License, or (at your option) any later version.
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
 * 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
    86
 * WITHOUT ANY WARRANTY; without even the implied warranty of
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    87
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    88
 * General Public License for more details.
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    89
 *
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    90
 * 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
    91
 * used in safety-critical situations without a full and competent review.
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    92
 */
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    93
167
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    94
/****
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    95
 * IEC 61131-3 standard function library
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    96
 * generated code, do not edit by hand
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    97
 */
893ff98ab3be Fixed std code for matiec generation header
etisserant
parents: 115
diff changeset
    98
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
    99
 """
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   100
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   101
def ANY_to_compiler_test_type_GEN(typename, paramname):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   102
    """
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   103
    Convert ANY_XXX IEC type declaration into IEC2C's generated type test.
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   104
    This tests are defined in search_expression_type.cc 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   105
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   106
    return {"ANY" : "",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   107
    "ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))",
26
36d378bd852e Stage4 changes according to new STD lib implementation
etisserant
parents: 25
diff changeset
   108
    "ANY_NBIT" : "if(search_expression_type->is_nbinary_type(%(paramname)s_type_symbol))",
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   109
    "ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   110
    "ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   111
    "ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   112
    }.get(typename,
40
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   113
        #"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   114
        "if(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
   115
                "paramname" : paramname, "typename": typename.lower()}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   116
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   117
def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   118
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   119
    This function generate visit(function_invocation) code for 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   120
        - ST code generator
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   121
        - IL code generator
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   122
        - search_expression_type class for ST
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   123
        - search_expression_type class for IL
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   124
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   125
    Input data is a 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   126
    "{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
   127
    nested dictionary structure.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   128
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   129
    if type(fdecls) != type(tuple()):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   130
        res = ""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   131
        for Paramname, ParamTypes in fdecls.iteritems():
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   132
            if do_il:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   133
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   134
{"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   135
                if not do_type_search_only:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   136
                    res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   137
    /* Get the value from a foo(<param_name> = <param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   138
    symbol_c *%(input_name)s_param_value = &this->default_variable_name;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   139
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   140
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   141
    symbol_c *%(input_name)s_type_symbol = param_data_type;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   142
    last_type_symbol = param_data_type;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   143
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   144
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   145
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   146
{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   147
    identifier_c param_name("%(input_name)s");
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   148
    /* Get the value from a foo(<param_name> = <param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   149
    symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(&param_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   150
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   151
    /* Get the value from a foo(<param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   152
    if (%(input_name)s_param_value == NULL)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   153
      %(input_name)s_param_value = function_call_param_iterator.next();
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   154
    symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   155
    last_type_symbol = last_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 ;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   156
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   157
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   158
            for ParamType,NextParamDecl in ParamTypes.iteritems():
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   159
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   160
                res += """    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   161
    %(type_test)s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   162
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   163
%(if_good_type_code)s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   164
    }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   165
"""%{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   166
    "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   167
    "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
   168
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   169
            res += """    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   170
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   171
}
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
        return res.replace('\n','\n'+indent)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   175
    else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   176
        res = "\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   177
        fdecl=fdecls[0]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   178
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   179
        result_type_rule = fdecl["return_type_rule"]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   180
        res += {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   181
            "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   182
            "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
   183
            }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   184
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   185
        if not do_type_search_only:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   186
            code_gen = eval(fdecl["python_eval_c_code_format"])
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   187
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   188
            code_gen_dic_decl = {}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   189
            for paramname,paramtype,unused in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   190
                code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   191
                code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   192
            code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("'
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   193
            code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("'
43
4a7e80513edd Test IEC_LIB and fix bugs (EQ, GT et all)
etisserant
parents: 40
diff changeset
   194
            code_gen_dic_decl["common_type"] = '");\nlast_type_symbol->accept(*this);\ns4o.print("'
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   195
            code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n  s4o.print("('
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   196
            code_gen_dic_decl["end_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol)) {\n  s4o.print("&1");\n  s4o.print(")");\n}\ns4o.print("'
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   197
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   198
            if type(code_gen) == type(tuple()):
40
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   199
                res += 's4o.indent_right();\n'
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   200
                res += 's4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[0]%code_gen_dic_decl)
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   201
                static_param_accept_list = []
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   202
                for paramname,paramtype,unused in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   203
                    static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname))
40
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   204
                res += ('s4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[1])).join(static_param_accept_list)
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   205
                code = 's4o.print("%s\\n" + s4o.indent_spaces);\nparam_value->accept(*this);\n'%(code_gen[1])
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   206
                end_code = 's4o.print("%s");\ns4o.indent_left();\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl)
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   207
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   208
                code = ''
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   209
                end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   210
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   211
            if fdecl["extensible"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   212
                res += ("""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   213
int base_num = %d;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   214
symbol_c *param_value = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   215
do{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   216
    char my_name[10];
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   217
    sprintf(my_name, "IN%%d", base_num++);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   218
    identifier_c param_name(my_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   219
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   220
    /* Get the value from a foo(<param_name> = <param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   221
    param_value = function_call_param_iterator.search(&param_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   222
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   223
    /* Get the value from a foo(<param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   224
    if (param_value == NULL)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   225
      param_value = function_call_param_iterator.next();
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   226
    if (param_value != NULL){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   227
        symbol_c *current_type_symbol = search_expression_type->get_type(param_value);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   228
        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 ;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   229
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   230
        /*Function specific CODE */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   231
        %s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   232
    }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   233
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   234
}while(param_value != NULL);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   235
%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   236
"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n        '), end_code))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   237
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   238
                #res += code + end_code
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   239
                res += end_code
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   240
        else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   241
            res += "return return_type_symbol;\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   242
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   243
                
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   244
        return res.replace('\n','\n'+indent)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   245
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   246
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   247
###                                                             ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   248
###                           MAIN                              ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   249
###                                                             ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   250
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   251
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   252
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   253
Reorganize std_decl from structure.py
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   254
into a nested dictionnary structure (i.e. a tree):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   255
"{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
   256
Keep ptrack of original declaration order in a 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   257
separated list called official_order
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   258
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   259
std_fdecls = {}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   260
official_order = []
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   261
for section in std_decl:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   262
    for fdecl in section["list"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   263
        if len(official_order)==0 or official_order[-1] != fdecl["name"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   264
            official_order.append(fdecl["name"])
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   265
        # store all func by name in a dict
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   266
        std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   267
        current = std_fdecls_fdecl_name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   268
        for i in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   269
            current[i[0]] = current.get(i[0], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   270
            current = current[i[0]]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   271
            last = current
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   272
            current[i[1]] = current.get(i[1], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   273
            current = current[i[1]]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   274
        last[i[1]]=(fdecl,)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   275
        std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   276
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   277
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   278
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   279
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   280
Generate the long enumeration of std function types
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   281
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   282
function_type_decl =  matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   283
typedef enum {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   284
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   285
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   286
    function_type_decl += "    function_"+fname.lower()+",\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   287
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   288
function_type_decl += """    function_none
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   289
} function_type_t;
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
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   293
Generate the funct that return enumerated according function name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   294
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   295
get_function_type_decl = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   296
function_type_t get_function_type(identifier_c *function_name) {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   297
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   298
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   299
    get_function_type_decl += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   300
if (!strcasecmp(function_name->value, "%s"))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   301
    return function_%s;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   302
"""%(fname,fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   303
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   304
get_function_type_decl += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   305
    else return function_none;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   306
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   307
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   308
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   309
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   310
"""
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   311
Generate the part of generate_c_st_c::visit(function_invocation)
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   312
that is responsible to generate C code for std lib calls.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   313
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   314
st_code_gen = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   315
switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   316
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   317
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   318
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   319
    st_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   320
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   321
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   322
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   323
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   324
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   325
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   326
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   327
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   328
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   329
    st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   330
    
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
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   333
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   334
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   335
st_code_gen +=  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   336
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   337
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   338
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   339
return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   340
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   341
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   342
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   343
"""
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   344
Generate the part of generate_c_il_c::visit(il_function_call)
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   345
that is responsible to generate C code for std lib calls.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   346
"""
58
39cd981ff242 Changing file headers
lbessard
parents: 43
diff changeset
   347
il_code_gen = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   348
switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   349
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   350
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   351
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   352
    il_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   353
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   354
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   355
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   356
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   357
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   358
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   359
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   360
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   361
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   362
    il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   363
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   364
    il_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   365
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   366
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   367
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   368
il_code_gen +=  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   369
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   370
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   371
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   372
return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   373
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   374
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   375
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   376
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   377
Generate the part of search_expression_type_c::visit(function_invocation)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   378
that is responsible of returning type symbol for function invocation.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   379
"""
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   380
search_type_code = matiec_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   381
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   382
void *compute_standard_function_st(function_invocation_c *symbol) {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   383
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   384
  function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   385
  function_call_param_iterator_c function_call_param_iterator(symbol);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   386
  search_expression_type_c* search_expression_type = this;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   387
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   388
  switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   389
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   390
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   391
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   392
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   393
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   394
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   395
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   396
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   397
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   398
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   399
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   400
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   401
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   402
    search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   403
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   404
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   405
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   406
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   407
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   408
search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   409
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   410
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   411
  }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   412
  return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   413
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   414
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   415
void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   416
  
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   417
  function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   418
  function_call_param_iterator_c function_call_param_iterator(symbol);  
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   419
  search_expression_type_c* search_expression_type = this;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   420
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   421
  switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   422
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   423
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   424
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   425
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   426
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   427
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   428
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   429
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   430
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   431
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   432
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   433
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   434
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   435
    search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   436
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   437
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   438
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   439
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   440
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   441
search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   442
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   443
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   444
  }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   445
  return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   446
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   447
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   448
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   449
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   450
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   451
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   452
"""
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   453
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
   454
"""
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   455
standard_function_names = matiec_header + """
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   456
const char *standard_function_names[] = {
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   457
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   458
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
   459
    standard_function_names += "\""+fname+"\",\n"
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   460
standard_function_names += """
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   461
/* end of array marker! Do not remove! */
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   462
NULL
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   463
};
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   464
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   465
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   466
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   467
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   468
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   469
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   470
"""
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   471
Generate the C implementation of the IEC standard function library.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   472
"""
59
af079a11837b Adding header for files published under LGPL
lbessard
parents: 58
diff changeset
   473
iec_std_lib_generated = matiec_lesser_header + """
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   474
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   475
/* Macro that expand to subtypes */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   476
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   477
for typename, parenttypename in TypeHierarchy_list:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   478
    if (typename.startswith("ANY")):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   479
        iec_std_lib_generated += "#define " + typename + "(DO)"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   480
        for typename2, parenttypename2 in TypeHierarchy_list:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   481
            if(parenttypename2 == typename):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   482
                if(typename2.startswith("ANY")):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   483
                    iec_std_lib_generated +=  " " + typename2 + "(DO)"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   484
                else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   485
                    iec_std_lib_generated +=  " DO(" + typename2 + ")"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   486
        iec_std_lib_generated +=  "\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   487
    else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   488
        break
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   489
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   490
if len(sys.argv) != 2 :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   491
    print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   492
    sys.exit(0)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   493
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   494
# 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
   495
for path, name, ext in [
115
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   496
        ('stage4/generate_c','function_type_decl','h'),
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   497
        ('stage4/generate_c','get_function_type_decl','c'),
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   498
        ('stage4/generate_c','st_code_gen','c'),
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   499
        ('stage4/generate_c','il_code_gen','c'),
017a65259f07 Change generate_cc to generate_c
lbessard
parents: 59
diff changeset
   500
        ('stage4/generate_c','search_type_code','c'),
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   501
        ('stage1_2','standard_function_names','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   502
        ('lib', 'iec_std_lib_generated','h')
26
36d378bd852e Stage4 changes according to new STD lib implementation
etisserant
parents: 25
diff changeset
   503
        ]:
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   504
    fd = open(os.path.join(sys.argv[1], path, name+'.'+ext),'w')
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   505
    fd.write(eval(name))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   506
    fd.close()
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   507
    
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   508
#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
   509
#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
   510
#print iec_std_lib_generated