generate_IEC_std.py
author etisserant
Wed, 18 Jul 2007 11:57:36 +0200
changeset 43 4a7e80513edd
parent 40 49c8ebc1ee25
child 58 39cd981ff242
permissions -rw-r--r--
Test IEC_LIB and fix bugs (EQ, GT et all)
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     1
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     2
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     3
 THIS CODE GENARATES C++ CODE FOR IEC2CC COMPILER
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     4
 GENERATED CODE IS :
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     5
 
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     6
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
     7
get_function_type_decl.c - the funct that return enumerated according function name
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     8
st_code_gen.c - part of generate_cc_st_c::visit(function_invocation)
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
     9
                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
    10
il_code_gen.c - part of generate_cc_il_c::visit(il_function_call)
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    11
                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
    12
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
    13
                     return type symbol for std function invocation
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    14
 
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    15
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
    16
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    17
# Get definitions
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    18
from plcopen.structures import *
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    19
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    20
#import pprint
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    21
#pp = pprint.PrettyPrinter(indent=4)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    22
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    23
def ANY_to_compiler_test_type_GEN(typename, paramname):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    24
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    25
    Convert ANY_XXX IEC type declaration into IEC2CC's generated type test.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    26
    This tests are defined in search_expression_type.cc 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    27
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    28
    return {"ANY" : "",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    29
    "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
    30
    "ANY_NBIT" : "if(search_expression_type->is_nbinary_type(%(paramname)s_type_symbol))",
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    31
    "ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    32
    "ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    33
    "ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    34
    }.get(typename,
40
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
    35
        #"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
    36
        "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
    37
                "paramname" : paramname, "typename": typename.lower()}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    38
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    39
def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    40
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    41
    This function generate visit(function_invocation) code for 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    42
        - ST code generator
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    43
        - IL code generator
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    44
        - search_expression_type class for ST
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    45
        - search_expression_type class for IL
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    46
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    47
    Input data is a 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    48
    "{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
    49
    nested dictionary structure.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    50
    """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    51
    if type(fdecls) != type(tuple()):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    52
        res = ""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    53
        for Paramname, ParamTypes in fdecls.iteritems():
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    54
            if do_il:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    55
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    56
{"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    57
                if not do_type_search_only:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    58
                    res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    59
    /* Get the value from a foo(<param_name> = <param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    60
    symbol_c *%(input_name)s_param_value = &this->default_variable_name;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    61
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    62
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    63
    symbol_c *%(input_name)s_type_symbol = param_data_type;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    64
    last_type_symbol = param_data_type;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    65
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    66
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    67
                res += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    68
{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    69
    identifier_c param_name("%(input_name)s");
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    70
    /* Get the value from a foo(<param_name> = <param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    71
    symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(&param_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    72
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    73
    /* Get the value from a foo(<param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    74
    if (%(input_name)s_param_value == NULL)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    75
      %(input_name)s_param_value = function_call_param_iterator.next();
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    76
    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
    77
    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
    78
"""%{"input_name":Paramname}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    79
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    80
            for ParamType,NextParamDecl in ParamTypes.iteritems():
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    81
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    82
                res += """    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    83
    %(type_test)s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    84
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    85
%(if_good_type_code)s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    86
    }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    87
"""%{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    88
    "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    89
    "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
    90
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    91
            res += """    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    92
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    93
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    94
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    95
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    96
        return res.replace('\n','\n'+indent)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    97
    else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    98
        res = "\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
    99
        fdecl=fdecls[0]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   100
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   101
        result_type_rule = fdecl["return_type_rule"]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   102
        res += {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   103
            "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n",
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   104
            "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
   105
            }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   106
        
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   107
        if not do_type_search_only:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   108
            code_gen = eval(fdecl["python_eval_c_code_format"])
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   109
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   110
            code_gen_dic_decl = {}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   111
            for paramname,paramtype,unused in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   112
                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
   113
                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
   114
            code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("'
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   115
            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
   116
            code_gen_dic_decl["common_type"] = '");\nlast_type_symbol->accept(*this);\ns4o.print("'
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   117
            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
   118
            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
   119
            
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   120
            if type(code_gen) == type(tuple()):
40
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   121
                res += 's4o.indent_right();\n'
49c8ebc1ee25 Cleanup in generated C code for STD lib
etisserant
parents: 36
diff changeset
   122
                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
   123
                static_param_accept_list = []
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   124
                for paramname,paramtype,unused in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   125
                    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
   126
                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
   127
                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
   128
                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
   129
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   130
                code = ''
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   131
                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
   132
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   133
            if fdecl["extensible"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   134
                res += ("""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   135
int base_num = %d;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   136
symbol_c *param_value = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   137
do{
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   138
    char my_name[10];
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   139
    sprintf(my_name, "IN%%d", base_num++);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   140
    identifier_c param_name(my_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   141
    
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
    param_value = function_call_param_iterator.search(&param_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   144
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   145
    /* Get the value from a foo(<param_value>) style call */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   146
    if (param_value == NULL)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   147
      param_value = function_call_param_iterator.next();
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   148
    if (param_value != NULL){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   149
        symbol_c *current_type_symbol = search_expression_type->get_type(param_value);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   150
        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
   151
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   152
        /*Function specific CODE */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   153
        %s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   154
    }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   155
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   156
}while(param_value != NULL);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   157
%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   158
"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n        '), end_code))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   159
            else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   160
                #res += code + end_code
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   161
                res += end_code
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   162
        else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   163
            res += "return return_type_symbol;\n"
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
        return res.replace('\n','\n'+indent)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   167
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   168
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   169
###                                                             ###
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   170
###                           MAIN                              ###
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
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   175
Reorganize std_decl from structure.py
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   176
into a nested dictionnary structure (i.e. a tree):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   177
"{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
   178
Keep ptrack of original declaration order in a 
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   179
separated list called official_order
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   180
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   181
std_fdecls = {}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   182
official_order = []
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   183
for section in std_decl:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   184
    for fdecl in section["list"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   185
        if len(official_order)==0 or official_order[-1] != fdecl["name"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   186
            official_order.append(fdecl["name"])
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   187
        # store all func by name in a dict
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   188
        std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   189
        current = std_fdecls_fdecl_name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   190
        for i in fdecl["inputs"]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   191
            current[i[0]] = current.get(i[0], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   192
            current = current[i[0]]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   193
            last = current
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   194
            current[i[1]] = current.get(i[1], {})
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   195
            current = current[i[1]]
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   196
        last[i[1]]=(fdecl,)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   197
        std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   198
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   199
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   200
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   201
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   202
Generate the long enumeration of std function types
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   203
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   204
function_type_decl =  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   205
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   206
 * IEC 61131-3 standard function lib
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   207
 * generated code, do not edit by hand
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   208
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   209
typedef enum {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   210
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   211
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   212
    function_type_decl += "    function_"+fname.lower()+",\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   213
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   214
function_type_decl += """    function_none
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   215
} function_type_t;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   216
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   217
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   218
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   219
Generate the funct that return enumerated according function name
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   220
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   221
get_function_type_decl = """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   222
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   223
 * IEC 61131-3 standard function lib
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   224
 * generated code, do not edit by hand
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   225
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   226
function_type_t get_function_type(identifier_c *function_name) {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   227
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   228
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   229
    get_function_type_decl += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   230
if (!strcasecmp(function_name->value, "%s"))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   231
    return function_%s;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   232
"""%(fname,fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   233
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   234
get_function_type_decl += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   235
    else return function_none;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   236
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   237
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   238
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   239
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   240
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   241
Generate the part of generate_cc_st_c::visit(function_invocation)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   242
that is responsible to generate C code for std lib calls.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   243
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   244
st_code_gen = """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   245
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   246
 * IEC 61131-3 standard function lib
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   247
 * generated code, do not edit by hand
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   248
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   249
switch(current_function_type){
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
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   253
    st_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   254
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   255
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   256
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   257
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   258
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   259
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   260
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   261
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   262
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   263
    st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   264
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   265
    st_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   266
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   267
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   268
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   269
st_code_gen +=  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   270
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   271
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   272
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   273
return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   274
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   275
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
Generate the part of generate_cc_il_c::visit(il_function_call)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   279
that is responsible to generate C code for std lib calls.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   280
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   281
il_code_gen = """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   282
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   283
 * IEC 61131-3 standard function lib
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   284
 * generated code, do not edit by hand
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   285
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   286
switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   287
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   288
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   289
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   290
    il_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   291
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   292
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   293
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   294
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   295
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   296
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   297
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   298
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   299
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   300
    il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   301
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   302
    il_code_gen += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   303
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   304
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   305
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   306
il_code_gen +=  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   307
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   308
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   309
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   310
return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   311
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   312
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   313
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   314
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   315
Generate the part of search_expression_type_c::visit(function_invocation)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   316
that is responsible of returning type symbol for function invocation.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   317
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   318
search_type_code =  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   319
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   320
 * IEC 61131-3 standard function lib
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   321
 * generated code, do not edit by hand
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   322
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   323
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   324
void *compute_standard_function_st(function_invocation_c *symbol) {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   325
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   326
  function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   327
  function_call_param_iterator_c function_call_param_iterator(symbol);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   328
  search_expression_type_c* search_expression_type = this;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   329
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   330
  switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   331
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   332
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   333
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   334
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   335
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   336
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   337
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   338
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   339
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   340
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   341
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   342
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   343
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   344
    search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   345
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   346
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   347
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   348
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   349
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   350
search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   351
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   352
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   353
  }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   354
  return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   355
}
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   356
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   357
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
   358
  
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   359
  function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name);
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   360
  function_call_param_iterator_c function_call_param_iterator(symbol);  
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   361
  search_expression_type_c* search_expression_type = this;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   362
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   363
  switch(current_function_type){
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   364
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   365
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   366
for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   367
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   368
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   369
 *%s
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   370
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   371
    case function_%s :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   372
    {
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   373
        symbol_c *last_type_symbol = NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   374
"""    %(fname, fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   375
    indent =  "    "
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   376
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   377
    search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n    ')
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   378
    
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   379
    search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   380
    }/*function_%s*/
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   381
    break;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   382
"""    %(fname.lower())
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   383
search_type_code += """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   384
    case function_none :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   385
    ERROR;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   386
  }
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   387
  return NULL;
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   388
}
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
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   392
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   393
###################################################################
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   394
"""
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   395
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
   396
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   397
standard_function_names = """
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   398
/****
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   399
 * IEC 61131-3 standard function lib
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   400
 * generated code, do not edit by hand
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   401
 */
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   402
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   403
const char *standard_function_names[] = {
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   404
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   405
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
   406
    standard_function_names += "\""+fname+"\",\n"
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   407
standard_function_names += """
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   408
/* end of array marker! Do not remove! */
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   409
NULL
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   410
};
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   411
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   412
"""
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   413
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   414
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   415
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   416
###################################################################
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   417
"""
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   418
Generate the C implementation of the IEC standard function library.
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   419
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   420
iec_std_lib_generated =  """
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   421
/****
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   422
 * IEC 61131-3 standard function lib
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   423
 * generated code, do not edit by hand
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   424
 */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   425
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   426
/* Macro that expand to subtypes */
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   427
"""
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   428
for typename, parenttypename in TypeHierarchy_list:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   429
    if (typename.startswith("ANY")):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   430
        iec_std_lib_generated += "#define " + typename + "(DO)"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   431
        for typename2, parenttypename2 in TypeHierarchy_list:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   432
            if(parenttypename2 == typename):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   433
                if(typename2.startswith("ANY")):
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   434
                    iec_std_lib_generated +=  " " + typename2 + "(DO)"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   435
                else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   436
                    iec_std_lib_generated +=  " DO(" + typename2 + ")"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   437
        iec_std_lib_generated +=  "\n"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   438
    else:
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   439
        break
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   440
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   441
if len(sys.argv) != 2 :
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   442
    print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name"
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   443
    sys.exit(0)
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   444
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   445
# 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
   446
for path, name, ext in [
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   447
        ('stage4/generate_cc','function_type_decl','h'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   448
        ('stage4/generate_cc','get_function_type_decl','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   449
        ('stage4/generate_cc','st_code_gen','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   450
        ('stage4/generate_cc','il_code_gen','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   451
        ('stage4/generate_cc','search_type_code','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   452
        ('stage1_2','standard_function_names','c'),
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   453
        ('lib', 'iec_std_lib_generated','h')
26
36d378bd852e Stage4 changes according to new STD lib implementation
etisserant
parents: 25
diff changeset
   454
        ]:
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   455
    fd = open(os.path.join(sys.argv[1], path, name+'.'+ext),'w')
25
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   456
    fd.write(eval(name))
8dc68e669d99 Early implementation of STD library.
etisserant
parents:
diff changeset
   457
    fd.close()
36
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   458
    
e7d67b27877f Changes in the std library + std lib PLCOpen test (broken)
etisserant
parents: 26
diff changeset
   459
#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
   460
#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
   461
#print iec_std_lib_generated