lbessard@58: #!/usr/bin/env python lbessard@58: # -*- coding: utf-8 -*- lbessard@58: lbessard@58: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor lbessard@58: #based on the plcopen standard. lbessard@58: # lbessard@58: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD lbessard@58: # lbessard@58: #See COPYING file for copyrights details. lbessard@58: # lbessard@58: #This library is free software; you can redistribute it and/or lbessard@58: #modify it under the terms of the GNU General Public lbessard@58: #License as published by the Free Software Foundation; either lbessard@58: #version 2.1 of the License, or (at your option) any later version. lbessard@58: # lbessard@58: #This library is distributed in the hope that it will be useful, lbessard@58: #but WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@58: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU lbessard@58: #General Public License for more details. lbessard@58: # lbessard@58: #You should have received a copy of the GNU General Public lbessard@58: #License along with this library; if not, write to the Free Software lbessard@58: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA lbessard@58: etisserant@36: """ etisserant@36: lbessard@115: THIS CODE GENARATES C++ CODE FOR IEC2C COMPILER etisserant@36: GENERATED CODE IS : etisserant@36: etisserant@36: function_type_decl.h - the long enumeration of std function types etisserant@36: get_function_type_decl.c - the funct that return enumerated according function name lbessard@115: st_code_gen.c - part of generate_c_st_c::visit(function_invocation) etisserant@36: responsible to generate C code for std lib calls lbessard@115: il_code_gen.c - part of generate_c_il_c::visit(il_function_call) etisserant@36: responsible to generate C code for std lib calls etisserant@36: search_type_code.c - called by search_expression_type_c::visit(function_invocation) etisserant@36: return type symbol for std function invocation etisserant@36: etisserant@36: """ etisserant@36: etisserant@25: # Get definitions etisserant@25: from plcopen.structures import * etisserant@25: etisserant@25: #import pprint etisserant@25: #pp = pprint.PrettyPrinter(indent=4) etisserant@25: lbessard@58: matiec_header = """/* etisserant@167: * (c) 2008 Edouard TISSERANT lbessard@58: * lbessard@58: * Offered to the public under the terms of the GNU General Public License lbessard@58: * as published by the Free Software Foundation; either version 2 of the lbessard@58: * License, or (at your option) any later version. lbessard@58: * lbessard@58: * This program is distributed in the hope that it will be useful, but lbessard@58: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@58: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General lbessard@58: * Public License for more details. lbessard@58: * lbessard@58: * This code is made available on the understanding that it will not be lbessard@58: * used in safety-critical situations without a full and competent review. lbessard@58: */ lbessard@58: lbessard@58: /* lbessard@58: * An IEC 61131-3 IL and ST compiler. lbessard@58: * lbessard@58: * Based on the lbessard@58: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) lbessard@58: * lbessard@58: */ etisserant@167: etisserant@167: /**** etisserant@167: * IEC 61131-3 standard function library etisserant@167: * generated code, do not edit by hand etisserant@167: */ lbessard@58: lbessard@58: """ lbessard@58: lbessard@59: matiec_lesser_header = """/* etisserant@167: * (c) 2008 Edouard TISSERANT lbessard@59: * lbessard@59: * Offered to the public under the terms of the GNU Lesser General Public lbessard@59: * License as published by the Free Software Foundation; either version 2 lbessard@59: * of the License, or (at your option) any later version. lbessard@59: * lbessard@59: * This program is distributed in the hope that it will be useful, but lbessard@59: * WITHOUT ANY WARRANTY; without even the implied warranty of lbessard@59: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser lbessard@59: * General Public License for more details. lbessard@59: * lbessard@59: * This code is made available on the understanding that it will not be lbessard@59: * used in safety-critical situations without a full and competent review. lbessard@59: */ lbessard@59: etisserant@167: /**** etisserant@167: * IEC 61131-3 standard function library etisserant@167: * generated code, do not edit by hand etisserant@167: */ etisserant@167: lbessard@59: """ lbessard@59: etisserant@25: def ANY_to_compiler_test_type_GEN(typename, paramname): etisserant@25: """ lbessard@115: Convert ANY_XXX IEC type declaration into IEC2C's generated type test. etisserant@25: This tests are defined in search_expression_type.cc etisserant@25: """ etisserant@25: return {"ANY" : "", etisserant@25: "ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))", etisserant@26: "ANY_NBIT" : "if(search_expression_type->is_nbinary_type(%(paramname)s_type_symbol))", etisserant@25: "ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))", etisserant@25: "ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))", etisserant@25: "ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))" etisserant@25: }.get(typename, etisserant@40: #"if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{ etisserant@40: "if(search_expression_type->is_same_type(&search_constant_type_c::%(typename)s_type_name, last_type_symbol))")%{ etisserant@25: "paramname" : paramname, "typename": typename.lower()} etisserant@25: etisserant@25: def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False): etisserant@25: """ etisserant@25: This function generate visit(function_invocation) code for etisserant@25: - ST code generator etisserant@25: - IL code generator etisserant@25: - search_expression_type class for ST etisserant@25: - search_expression_type class for IL etisserant@25: etisserant@25: Input data is a etisserant@25: "{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" etisserant@25: nested dictionary structure. etisserant@25: """ etisserant@25: if type(fdecls) != type(tuple()): etisserant@25: res = "" etisserant@25: for Paramname, ParamTypes in fdecls.iteritems(): etisserant@25: if do_il: etisserant@25: res += """ etisserant@25: {""" etisserant@25: if not do_type_search_only: etisserant@25: res += """ etisserant@25: /* Get the value from a foo( = ) style call */ etisserant@25: symbol_c *%(input_name)s_param_value = &this->default_variable_name; etisserant@25: """%{"input_name":Paramname} etisserant@25: res += """ etisserant@25: symbol_c *%(input_name)s_type_symbol = param_data_type; etisserant@25: last_type_symbol = param_data_type; etisserant@25: """%{"input_name":Paramname} etisserant@25: else: etisserant@25: res += """ etisserant@25: { etisserant@25: identifier_c param_name("%(input_name)s"); etisserant@25: /* Get the value from a foo( = ) style call */ etisserant@25: symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); etisserant@25: etisserant@25: /* Get the value from a foo() style call */ etisserant@25: if (%(input_name)s_param_value == NULL) etisserant@25: %(input_name)s_param_value = function_call_param_iterator.next(); etisserant@25: symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value); etisserant@25: 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 ; etisserant@25: """%{"input_name":Paramname} etisserant@25: etisserant@25: for ParamType,NextParamDecl in ParamTypes.iteritems(): etisserant@25: etisserant@25: res += """ etisserant@25: %(type_test)s etisserant@25: { etisserant@25: %(if_good_type_code)s etisserant@25: } etisserant@25: """%{ etisserant@25: "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), etisserant@25: "if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n ')} etisserant@25: etisserant@25: res += """ etisserant@25: ERROR; etisserant@25: } etisserant@25: """ etisserant@25: etisserant@25: return res.replace('\n','\n'+indent) etisserant@25: else: etisserant@25: res = "\n" etisserant@25: fdecl=fdecls[0] etisserant@25: etisserant@25: result_type_rule = fdecl["return_type_rule"] etisserant@25: res += { etisserant@25: "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n", etisserant@25: "defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(), etisserant@25: }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule) etisserant@25: etisserant@25: if not do_type_search_only: etisserant@25: code_gen = eval(fdecl["python_eval_c_code_format"]) etisserant@25: etisserant@25: code_gen_dic_decl = {} etisserant@25: for paramname,paramtype,unused in fdecl["inputs"]: etisserant@25: code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname) etisserant@25: code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname) etisserant@25: code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("' etisserant@25: code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("' etisserant@43: code_gen_dic_decl["common_type"] = '");\nlast_type_symbol->accept(*this);\ns4o.print("' etisserant@25: code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n s4o.print("(' etisserant@25: 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("' etisserant@25: etisserant@25: if type(code_gen) == type(tuple()): etisserant@40: res += 's4o.indent_right();\n' etisserant@40: res += 's4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[0]%code_gen_dic_decl) etisserant@25: static_param_accept_list = [] etisserant@25: for paramname,paramtype,unused in fdecl["inputs"]: etisserant@25: static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname)) etisserant@40: res += ('s4o.print("%s\\n" + s4o.indent_spaces);\n'%(code_gen[1])).join(static_param_accept_list) etisserant@40: code = 's4o.print("%s\\n" + s4o.indent_spaces);\nparam_value->accept(*this);\n'%(code_gen[1]) etisserant@40: end_code = 's4o.print("%s");\ns4o.indent_left();\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl) etisserant@25: else: etisserant@25: code = '' etisserant@25: end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','') etisserant@25: etisserant@25: if fdecl["extensible"]: etisserant@25: res += (""" etisserant@25: int base_num = %d; etisserant@25: symbol_c *param_value = NULL; etisserant@25: do{ etisserant@25: char my_name[10]; etisserant@25: sprintf(my_name, "IN%%d", base_num++); etisserant@25: identifier_c param_name(my_name); etisserant@25: etisserant@25: /* Get the value from a foo( = ) style call */ etisserant@25: param_value = function_call_param_iterator.search(¶m_name); etisserant@25: etisserant@25: /* Get the value from a foo() style call */ etisserant@25: if (param_value == NULL) etisserant@25: param_value = function_call_param_iterator.next(); etisserant@25: if (param_value != NULL){ etisserant@25: symbol_c *current_type_symbol = search_expression_type->get_type(param_value); etisserant@25: 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 ; etisserant@25: etisserant@25: /*Function specific CODE */ etisserant@25: %s etisserant@25: } etisserant@25: etisserant@25: }while(param_value != NULL); etisserant@25: %s etisserant@25: """%(fdecl["baseinputnumber"]+2, code.replace('\n','\n '), end_code)) etisserant@25: else: etisserant@25: #res += code + end_code etisserant@25: res += end_code etisserant@25: else: etisserant@25: res += "return return_type_symbol;\n" etisserant@25: etisserant@25: etisserant@25: return res.replace('\n','\n'+indent) etisserant@25: etisserant@25: ################################################################### etisserant@25: ### ### etisserant@25: ### MAIN ### etisserant@25: ### ### etisserant@25: ################################################################### etisserant@25: etisserant@25: """ etisserant@25: Reorganize std_decl from structure.py etisserant@25: into a nested dictionnary structure (i.e. a tree): etisserant@25: "{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" etisserant@25: Keep ptrack of original declaration order in a etisserant@25: separated list called official_order etisserant@25: """ etisserant@25: std_fdecls = {} etisserant@25: official_order = [] etisserant@25: for section in std_decl: etisserant@25: for fdecl in section["list"]: etisserant@25: if len(official_order)==0 or official_order[-1] != fdecl["name"]: etisserant@25: official_order.append(fdecl["name"]) etisserant@25: # store all func by name in a dict etisserant@25: std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {}) etisserant@25: current = std_fdecls_fdecl_name etisserant@25: for i in fdecl["inputs"]: etisserant@25: current[i[0]] = current.get(i[0], {}) etisserant@25: current = current[i[0]] etisserant@25: last = current etisserant@25: current[i[1]] = current.get(i[1], {}) etisserant@25: current = current[i[1]] etisserant@25: last[i[1]]=(fdecl,) etisserant@25: std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name etisserant@25: etisserant@25: ################################################################### etisserant@25: etisserant@25: """ etisserant@25: Generate the long enumeration of std function types etisserant@25: """ lbessard@58: function_type_decl = matiec_header + """ etisserant@25: typedef enum { etisserant@25: """ etisserant@25: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@25: function_type_decl += " function_"+fname.lower()+",\n" etisserant@25: etisserant@25: function_type_decl += """ function_none etisserant@25: } function_type_t; etisserant@25: """ etisserant@25: ################################################################### etisserant@25: """ etisserant@25: Generate the funct that return enumerated according function name etisserant@25: """ lbessard@58: get_function_type_decl = matiec_header + """ etisserant@25: function_type_t get_function_type(identifier_c *function_name) { etisserant@25: """ etisserant@25: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@25: get_function_type_decl += """ etisserant@25: if (!strcasecmp(function_name->value, "%s")) etisserant@25: return function_%s; etisserant@25: """%(fname,fname.lower()) etisserant@25: etisserant@25: get_function_type_decl += """ etisserant@25: else return function_none; etisserant@25: } etisserant@25: etisserant@25: """ etisserant@25: ################################################################### etisserant@25: """ lbessard@115: Generate the part of generate_c_st_c::visit(function_invocation) etisserant@25: that is responsible to generate C code for std lib calls. etisserant@25: """ lbessard@58: st_code_gen = matiec_header + """ etisserant@25: switch(current_function_type){ etisserant@25: """ etisserant@25: etisserant@25: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@25: st_code_gen += """ etisserant@25: /**** etisserant@25: *%s etisserant@25: */ etisserant@25: case function_%s : etisserant@25: { etisserant@25: symbol_c *last_type_symbol = NULL; etisserant@25: """ %(fname, fname.lower()) etisserant@25: indent = " " etisserant@25: etisserant@25: st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n ') etisserant@25: etisserant@25: st_code_gen += """ etisserant@25: }/*function_%s*/ etisserant@25: break; etisserant@25: """ %(fname.lower()) etisserant@25: st_code_gen += """ etisserant@25: case function_none : etisserant@25: ERROR; etisserant@25: } etisserant@25: return NULL; etisserant@25: """ etisserant@25: etisserant@25: ################################################################### etisserant@25: """ lbessard@115: Generate the part of generate_c_il_c::visit(il_function_call) etisserant@25: that is responsible to generate C code for std lib calls. etisserant@25: """ lbessard@58: il_code_gen = matiec_header + """ etisserant@25: switch(current_function_type){ etisserant@25: """ etisserant@25: etisserant@25: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@25: il_code_gen += """ etisserant@25: /**** etisserant@25: *%s etisserant@25: */ etisserant@25: case function_%s : etisserant@25: { etisserant@25: symbol_c *last_type_symbol = NULL; etisserant@25: """ %(fname, fname.lower()) etisserant@25: indent = " " etisserant@25: etisserant@25: il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n ') etisserant@25: etisserant@25: il_code_gen += """ etisserant@25: }/*function_%s*/ etisserant@25: break; etisserant@25: """ %(fname.lower()) etisserant@25: il_code_gen += """ etisserant@25: case function_none : etisserant@25: ERROR; etisserant@25: } etisserant@25: return NULL; etisserant@25: """ etisserant@25: etisserant@25: ################################################################### etisserant@25: """ etisserant@25: Generate the part of search_expression_type_c::visit(function_invocation) etisserant@25: that is responsible of returning type symbol for function invocation. etisserant@25: """ lbessard@59: search_type_code = matiec_header + """ etisserant@25: etisserant@25: void *compute_standard_function_st(function_invocation_c *symbol) { etisserant@25: etisserant@25: function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); etisserant@25: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@25: search_expression_type_c* search_expression_type = this; etisserant@25: etisserant@25: switch(current_function_type){ etisserant@25: """ etisserant@25: etisserant@25: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@25: search_type_code += """ etisserant@25: /**** etisserant@25: *%s etisserant@25: */ etisserant@25: case function_%s : etisserant@25: { etisserant@25: symbol_c *last_type_symbol = NULL; etisserant@25: """ %(fname, fname.lower()) etisserant@25: indent = " " etisserant@25: etisserant@25: search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n ') etisserant@25: etisserant@25: search_type_code += """ etisserant@25: }/*function_%s*/ etisserant@25: break; etisserant@25: """ %(fname.lower()) etisserant@25: search_type_code += """ etisserant@25: case function_none : etisserant@25: ERROR; etisserant@25: } etisserant@25: return NULL; etisserant@25: } etisserant@25: etisserant@25: void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) { etisserant@25: etisserant@25: function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); etisserant@25: function_call_param_iterator_c function_call_param_iterator(symbol); etisserant@25: search_expression_type_c* search_expression_type = this; etisserant@25: etisserant@25: switch(current_function_type){ etisserant@25: """ etisserant@25: etisserant@25: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@25: search_type_code += """ etisserant@25: /**** etisserant@25: *%s etisserant@25: */ etisserant@25: case function_%s : etisserant@25: { etisserant@25: symbol_c *last_type_symbol = NULL; etisserant@25: """ %(fname, fname.lower()) etisserant@25: indent = " " etisserant@25: etisserant@25: search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n ') etisserant@25: etisserant@25: search_type_code += """ etisserant@25: }/*function_%s*/ etisserant@25: break; etisserant@25: """ %(fname.lower()) etisserant@25: search_type_code += """ etisserant@25: case function_none : etisserant@25: ERROR; etisserant@25: } etisserant@25: return NULL; etisserant@25: } etisserant@25: """ etisserant@25: etisserant@25: ################################################################### etisserant@25: ################################################################### etisserant@25: ################################################################### etisserant@25: """ etisserant@36: Generate the standard_function_names[] for inclusion in bizon generated code etisserant@36: """ lbessard@59: standard_function_names = matiec_header + """ etisserant@36: const char *standard_function_names[] = { etisserant@36: """ etisserant@36: for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: etisserant@36: standard_function_names += "\""+fname+"\",\n" etisserant@36: standard_function_names += """ etisserant@36: /* end of array marker! Do not remove! */ etisserant@36: NULL etisserant@36: }; etisserant@36: etisserant@36: """ etisserant@36: etisserant@36: ################################################################### etisserant@36: ################################################################### etisserant@36: ################################################################### etisserant@36: """ etisserant@25: Generate the C implementation of the IEC standard function library. etisserant@25: """ lbessard@59: iec_std_lib_generated = matiec_lesser_header + """ etisserant@25: etisserant@25: /* Macro that expand to subtypes */ etisserant@25: """ etisserant@25: for typename, parenttypename in TypeHierarchy_list: etisserant@25: if (typename.startswith("ANY")): etisserant@25: iec_std_lib_generated += "#define " + typename + "(DO)" etisserant@25: for typename2, parenttypename2 in TypeHierarchy_list: etisserant@25: if(parenttypename2 == typename): etisserant@25: if(typename2.startswith("ANY")): etisserant@25: iec_std_lib_generated += " " + typename2 + "(DO)" etisserant@25: else: etisserant@25: iec_std_lib_generated += " DO(" + typename2 + ")" etisserant@25: iec_std_lib_generated += "\n" etisserant@25: else: etisserant@25: break etisserant@25: etisserant@25: if len(sys.argv) != 2 : etisserant@25: print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name" etisserant@25: sys.exit(0) etisserant@25: etisserant@25: # Now, print that out, or write to files from sys.argv etisserant@36: for path, name, ext in [ lbessard@115: ('stage4/generate_c','function_type_decl','h'), lbessard@115: ('stage4/generate_c','get_function_type_decl','c'), lbessard@115: ('stage4/generate_c','st_code_gen','c'), lbessard@115: ('stage4/generate_c','il_code_gen','c'), lbessard@115: ('stage4/generate_c','search_type_code','c'), etisserant@36: ('stage1_2','standard_function_names','c'), etisserant@36: ('lib', 'iec_std_lib_generated','h') etisserant@26: ]: etisserant@36: fd = open(os.path.join(sys.argv[1], path, name+'.'+ext),'w') etisserant@25: fd.write(eval(name)) etisserant@25: fd.close() etisserant@36: etisserant@36: #print "/* Code to eventually paste in iec_std_lib.h if type hierarchy changed */" etisserant@36: #print "/* you also have to change iec_std_lib.h according to new types */\n\n" etisserant@36: #print iec_std_lib_generated