# HG changeset patch # User etisserant # Date 1183740952 -7200 # Node ID 8dc68e669d99c5780d399fb34a78c986ef9eb56a # Parent 364320323b4de2410765eaf664f3371674b3b21c Early implementation of STD library. diff -r 364320323b4d -r 8dc68e669d99 generate_IEC_std.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/generate_IEC_std.py Fri Jul 06 18:55:52 2007 +0200 @@ -0,0 +1,412 @@ +# Get definitions +from plcopen.structures import * + +#import pprint +#pp = pprint.PrettyPrinter(indent=4) + +def ANY_to_compiler_test_type_GEN(typename, paramname): + """ + Convert ANY_XXX IEC type declaration into IEC2CC's generated type test. + This tests are defined in search_expression_type.cc + """ + return {"ANY" : "", + "ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))", + "ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))", + "ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))", + "ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))" + }.get(typename, + "if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{ + "paramname" : paramname, "typename": typename.lower()} + +def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False): + """ + This function generate visit(function_invocation) code for + - ST code generator + - IL code generator + - search_expression_type class for ST + - search_expression_type class for IL + + Input data is a + "{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" + nested dictionary structure. + """ + if type(fdecls) != type(tuple()): + res = "" + for Paramname, ParamTypes in fdecls.iteritems(): + if do_il: + res += """ +{""" + if not do_type_search_only: + res += """ + /* Get the value from a foo( = ) style call */ + symbol_c *%(input_name)s_param_value = &this->default_variable_name; +"""%{"input_name":Paramname} + res += """ + symbol_c *%(input_name)s_type_symbol = param_data_type; + last_type_symbol = param_data_type; +"""%{"input_name":Paramname} + else: + res += """ +{ + identifier_c param_name("%(input_name)s"); + /* Get the value from a foo( = ) style call */ + symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); + + /* Get the value from a foo() style call */ + if (%(input_name)s_param_value == NULL) + %(input_name)s_param_value = function_call_param_iterator.next(); + symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value); + 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 ; +"""%{"input_name":Paramname} + + for ParamType,NextParamDecl in ParamTypes.iteritems(): + + res += """ + %(type_test)s + { +%(if_good_type_code)s + } +"""%{ + "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), + "if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n ')} + + res += """ + ERROR; +} +""" + + return res.replace('\n','\n'+indent) + else: + res = "\n" + fdecl=fdecls[0] + + result_type_rule = fdecl["return_type_rule"] + res += { + "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n", + "defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(), + }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule) + + if not do_type_search_only: + code_gen = eval(fdecl["python_eval_c_code_format"]) + + code_gen_dic_decl = {} + for paramname,paramtype,unused in fdecl["inputs"]: + code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname) + code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname) + code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("' + code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("' + code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n s4o.print("(' + 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("' + + if type(code_gen) == type(tuple()): + res += 's4o.print("%s");\n'%(code_gen[0]%code_gen_dic_decl) + static_param_accept_list = [] + for paramname,paramtype,unused in fdecl["inputs"]: + static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname)) + res += ('s4o.print("%s");\n'%(code_gen[1])).join(static_param_accept_list) + code = 's4o.print("%s");\nparam_value->accept(*this);\n'%(code_gen[1]) + end_code = 's4o.print("%s");\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl) + else: + code = '' + end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','') + + if fdecl["extensible"]: + res += (""" +int base_num = %d; +symbol_c *param_value = NULL; +do{ + char my_name[10]; + sprintf(my_name, "IN%%d", base_num++); + identifier_c param_name(my_name); + + /* Get the value from a foo( = ) style call */ + param_value = function_call_param_iterator.search(¶m_name); + + /* Get the value from a foo() style call */ + if (param_value == NULL) + param_value = function_call_param_iterator.next(); + if (param_value != NULL){ + symbol_c *current_type_symbol = search_expression_type->get_type(param_value); + 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 ; + + /*Function specific CODE */ + %s + } + +}while(param_value != NULL); +%s +"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n '), end_code)) + else: + #res += code + end_code + res += end_code + else: + res += "return return_type_symbol;\n" + + + return res.replace('\n','\n'+indent) + +################################################################### +### ### +### MAIN ### +### ### +################################################################### + +""" +Reorganize std_decl from structure.py +into a nested dictionnary structure (i.e. a tree): +"{fname : {IN[0]paramname : {IN[0]paramtype : {IN[1]paraname : {IN[1]paramtype : {... : {IN[N]paraname : {IN[N]paramtype : (fdecl,)}}}}}}" +Keep ptrack of original declaration order in a +separated list called official_order +""" +std_fdecls = {} +official_order = [] +for section in std_decl: + for fdecl in section["list"]: + if len(official_order)==0 or official_order[-1] != fdecl["name"]: + official_order.append(fdecl["name"]) + # store all func by name in a dict + std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {}) + current = std_fdecls_fdecl_name + for i in fdecl["inputs"]: + current[i[0]] = current.get(i[0], {}) + current = current[i[0]] + last = current + current[i[1]] = current.get(i[1], {}) + current = current[i[1]] + last[i[1]]=(fdecl,) + std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name + +################################################################### + +""" +Generate the long enumeration of std function types +""" +function_type_decl = """ +/**** + * IEC 61131-3 standard function lib + * generated code, do not edit by hand + */ +typedef enum { +""" +for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: + function_type_decl += " function_"+fname.lower()+",\n" + +function_type_decl += """ function_none +} function_type_t; +""" +################################################################### +""" +Generate the funct that return enumerated according function name +""" +get_function_type_decl = """ +/**** + * IEC 61131-3 standard function lib + * generated code, do not edit by hand + */ +function_type_t get_function_type(identifier_c *function_name) { +""" +for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: + get_function_type_decl += """ +if (!strcasecmp(function_name->value, "%s")) + return function_%s; +"""%(fname,fname.lower()) + +get_function_type_decl += """ + else return function_none; +} + +""" +################################################################### +""" +Generate the part of generate_cc_st_c::visit(function_invocation) +that is responsible to generate C code for std lib calls. +""" +st_code_gen = """ +/**** + * IEC 61131-3 standard function lib + * generated code, do not edit by hand + */ +switch(current_function_type){ +""" + +for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: + st_code_gen += """ +/**** + *%s + */ + case function_%s : + { + symbol_c *last_type_symbol = NULL; +""" %(fname, fname.lower()) + indent = " " + + st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n ') + + st_code_gen += """ + }/*function_%s*/ + break; +""" %(fname.lower()) +st_code_gen += """ + case function_none : + ERROR; +} +return NULL; +""" + +################################################################### +""" +Generate the part of generate_cc_il_c::visit(il_function_call) +that is responsible to generate C code for std lib calls. +""" +il_code_gen = """ +/**** + * IEC 61131-3 standard function lib + * generated code, do not edit by hand + */ +switch(current_function_type){ +""" + +for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: + il_code_gen += """ +/**** + *%s + */ + case function_%s : + { + symbol_c *last_type_symbol = NULL; +""" %(fname, fname.lower()) + indent = " " + + il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n ') + + il_code_gen += """ + }/*function_%s*/ + break; +""" %(fname.lower()) +il_code_gen += """ + case function_none : + ERROR; +} +return NULL; +""" + +################################################################### +""" +Generate the part of search_expression_type_c::visit(function_invocation) +that is responsible of returning type symbol for function invocation. +""" +search_type_code = """ +/**** + * IEC 61131-3 standard function lib + * generated code, do not edit by hand + */ + +void *compute_standard_function_st(function_invocation_c *symbol) { + + function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); + function_call_param_iterator_c function_call_param_iterator(symbol); + search_expression_type_c* search_expression_type = this; + + switch(current_function_type){ +""" + +for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: + search_type_code += """ +/**** + *%s + */ + case function_%s : + { + symbol_c *last_type_symbol = NULL; +""" %(fname, fname.lower()) + indent = " " + + search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n ') + + search_type_code += """ + }/*function_%s*/ + break; +""" %(fname.lower()) +search_type_code += """ + case function_none : + ERROR; + } + return NULL; +} + +void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) { + + function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); + function_call_param_iterator_c function_call_param_iterator(symbol); + search_expression_type_c* search_expression_type = this; + + switch(current_function_type){ +""" + +for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: + search_type_code += """ +/**** + *%s + */ + case function_%s : + { + symbol_c *last_type_symbol = NULL; +""" %(fname, fname.lower()) + indent = " " + + search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n ') + + search_type_code += """ + }/*function_%s*/ + break; +""" %(fname.lower()) +search_type_code += """ + case function_none : + ERROR; + } + return NULL; +} +""" + +################################################################### +################################################################### +################################################################### +""" +Generate the C implementation of the IEC standard function library. +""" +iec_std_lib_generated = """ +/**** + * IEC 61131-3 standard function lib + * generated code, do not edit by hand + */ + +/* Macro that expand to subtypes */ +""" +for typename, parenttypename in TypeHierarchy_list: + if (typename.startswith("ANY")): + iec_std_lib_generated += "#define " + typename + "(DO)" + for typename2, parenttypename2 in TypeHierarchy_list: + if(parenttypename2 == typename): + if(typename2.startswith("ANY")): + iec_std_lib_generated += " " + typename2 + "(DO)" + else: + iec_std_lib_generated += " DO(" + typename2 + ")" + iec_std_lib_generated += "\n" + else: + break + +if len(sys.argv) != 2 : + print "Usage: " + sys.argv[0] + "path_name\n -> create files in path_name" + sys.exit(0) + +# Now, print that out, or write to files from sys.argv +for name, ext in [ + ('function_type_decl','h'), + ('get_function_type_decl','c'), + ('st_code_gen','c'), + ('il_code_gen','c'), + ('search_type_code','c'), + ('iec_std_lib_generated','h')]: + fd = open(os.path.join(sys.argv[1],name+'.'+ext),'w') + fd.write(eval(name)) + fd.close() diff -r 364320323b4d -r 8dc68e669d99 plcopen/iec_std.csv --- a/plcopen/iec_std.csv Mon Jun 25 18:57:14 2007 +0200 +++ b/plcopen/iec_std.csv Fri Jul 06 18:55:52 2007 +0200 @@ -1,79 +1,79 @@ -Standard_functions_variables_types;name;comment;type;;;;;;;;; -;N;Number of bits to be shifted;UINT;;;;;;;;; -;L;Left position within character string;UINT;;;;;;;;; -;P;Position within character string;UINT;;;;;;;;; -;G;Selection out of 2 inputs (gate);BOOL;;;;;;;;; -;K;Selection out of n inputs;ANY_INT;;;;;;;;; -;MN;Minimum value for limitation;ANY;;;;;;;;; -;MX;Maximum value for limitation;ANY;;;;;;;;; -;;;;;;;;;;;; -;;;;;;;;;;;; -;;;;;;;;;;;; -;;;;;;;;;;;; -;;;;;;;;;;;; -;;;;;;;;;;;; -Standard_functions_type;name;baseinputnumber;inputs;outputs;comment;extensible;lib_typed_decl;python_eval_c_code_format;return_type_rule;;; -Type conversion;*_TO_**;1;(ANY);ANY;Data type conversion;no;;ANY_TO_ANY_FORMAT_GEN(fdecl);defined;;;0=defined -;TRUNC;1;(ANY_REAL);ANY_INT;Rounding up/down;no;;"(int)%(IN_value)s";&search_constant_type_c::constant_int_type_name;;; -;BCD_TO_**;1;(ANY_BIT);ANY_INT;Conversion from BCD;no;;"__bcd_to_something(sizeof(%(IN_type)s),&%(IN_value)s)";defined;;; -;*_TO_BCD;1;(ANY_INT);ANY_BIT;Conversion to BCD;no;;"__something_to_bcd(sizeof(%(IN_type)s),&%(IN_value)s)";&search_constant_type_c::constant_int_type_name;;; -;DATE_AND_TIME_TO_TIME_OF_DAY;1;(DT);TOD;Conversion to time-of-day;no;;"__date_and_time_to_time_of_day(&%(IN_value)s)";defined;;; -;DATE_AND_TIME_TO_DATE;1;(DT);DATE;Conversion to date;no;;"__date_and_time_to_time_of_day(&%(IN_value)s)";defined;;; -Numerical;ABS;1;(ANY_NUM);ANY_NUM;Absolute number;no;"__abs_%(return_type)s(%(return_type)s IN){ return IN > 0 ? IN : -IN {sc}}";"__abs_%(IN_type)s(%(IN_value)s)";IN_type_symbol;;; -;SQRT;1;(ANY_REAL);ANY_REAL;Square root (base 2);no;;"sqrt(%(IN_value)s)";IN_type_symbol;;; -;LN;1;(ANY_REAL);ANY_REAL;Natural logarithm;no;;"ln(%(IN_value)s)";IN_type_symbol;;; -;LOG;1;(ANY_REAL);ANY_REAL;Logarithm to base 10;no;;"log(%(IN_value)s)";IN_type_symbol;;; -;EXP;1;(ANY_REAL);ANY_REAL;Exponentiation;no;;"exp(%(IN_value)s)";IN_type_symbol;;; -;SIN;1;(ANY_REAL);ANY_REAL;Sine;no;;"sin(%(IN_value)s)";IN_type_symbol;;; -;COS;1;(ANY_REAL);ANY_REAL;Cosine;no;;"cos(%(IN_value)s)";IN_type_symbol;;; -;TAN;1;(ANY_REAL);ANY_REAL;Tangent;no;;"tan(%(IN_value)s)";IN_type_symbol;;; -;ASIN;1;(ANY_REAL);ANY_REAL;Arc sine;no;;"asin(%(IN_value)s)";IN_type_symbol;;; -;ACOS;1;(ANY_REAL);ANY_REAL;Arc cosine;no;;"acos(%(IN_value)s)";IN_type_symbol;;; -;ATAN;1;(ANY_REAL);ANY_REAL;Arc tangent;no;;"atan(%(IN_value)s)";IN_type_symbol;;; -Arithmetic;ADD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Addition;yes;;("(","+",")");copy_input;;; -;ADD;1;(TIME, TIME);TIME;Time addition;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;ADD;1;(TOD, TIME);TOD;Time-of-day addition;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;ADD;1;(DT, TIME);DT;Date addition;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;MUL;1;(ANY_NUM, ANY_NUM);ANY_NUM;Multiplication;yes;;("(","*",")");copy_input;;; -;MUL;1;(TIME, ANY_NUM);TIME;Time multiplication;no;;"__time_mul(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;SUB;1;(ANY_NUM, ANY_NUM);ANY_NUM;Subtraction;no;;("(","-",")");copy_input;;; -;SUB;1;(TIME, TIME);TIME;Time subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;SUB;1;(DATE, DATE);TIME;Date subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;SUB;1;(TOD, TIME);TOD;Time-of-day subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;SUB;1;(TOD, TOD);TIME;Time-of-day subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;SUB;1;(DT, TIME);DT;Date and time subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;SUB;1;(DT, DT);TIME;Date and time subtraction;no;;"__time_sub(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;DIV;1;(ANY_NUM, ANY_NUM);ANY_NUM;Division;no;;("(","/",")");copy_input;;; -;DIV;1;(TIME, ANY_NUM);TIME;Time division;no;;"__time_div(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;MOD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Remainder (modulo);no;;("(","%",")");copy_input;;; -;EXPT;1;(ANY_NUM, ANY_NUM);ANY_NUM;Exponent;no;;"pow(&%(IN1_value)s, &%(IN2_value)s)";copy_input;;; -;MOVE;1;(ANY_NUM);ANY_NUM;Assignment;no;;"%(IN_value)s";copy_input;;; -Bit-shift;SHL;1;(ANY_BIT, N);ANY_BIT;Shift left;no;;"%(IN_value)s<<%(N_value)s";IN_type_symbol;;; -;SHR;1;(ANY_BIT, N);ANY_BIT;Shift right;no;;"%(IN_value)s>>%(N_value)s";IN_type_symbol;;; -;ROR;1;(ANY_BIT, N);ANY_BIT;Rotate right;no;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}";"__ror(sizeof(%(IN_type)s), &%(IN_value)s, %(N_value)s)";IN_type_symbol;;; -;ROL;1;(ANY_BIT, N);ANY_BIT;Rotate left;no;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}";"__rol(sizeof(%(IN_type)s), &%(IN_value)s, %(N_value)s)";IN_type_symbol;;; -Bitwise;AND;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise AND;yes;;("(%(start_bool_filter)s","&",")%(end_bool_filter)s");copy_input;;; -;OR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise OR;yes;;("(%(start_bool_filter)s","|",")%(end_bool_filter)s");copy_input;;; -;XOR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise EXOR;yes;;("(%(start_bool_filter)s","^",")%(end_bool_filter)s");copy_input;;; -;NOT;1;(ANY_BIT);ANY_BIT;Bitwise inverting;no;;"~%(IN_value)s";IN_type_symbol;;; -Selection;SEL;0;(G, ANY, ANY);ANY;Binary selection (1 of 2);no;;"%(G_value)s ? %(IN1_value)s : %(IN0_value)s";copy_input;;; -;MAX;1;(ANY, ANY);ANY;Maximum;yes;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}";("__max_%(return_type)s(%(param_count)s,",",",")");copy_input;;; -;MIN;1;(ANY, ANY);ANY;Minimum;yes;"__min_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 < IN2 ? IN1 : IN2 {sc}}";("__min_%(return_type)s(%(param_count)s,",",",")");copy_input;;; -;LIMIT;1;(MN, ANY, MX);ANY;Limitation;no;;"__limit_%(IN_type)s(%(MN_value)s, %(IN_value)s, %(MX_value)s)";IN_type_symbol;;; -;MUX;0;(K, ANY, ANY);ANY;Multiplexer (select 1 of N);yes;;("__mux_%(return_type)s(%(param_count)s,",",",")");copy_input;;; -Comparison;GT;1;(ANY, ANY);BOOL;Greater than;yes;"__gt_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 0 ? IN : -IN {sc}}";("__gt_%(return_type)s(%(param_count)s,",",",")");defined;;; -;GE;1;(ANY, ANY);BOOL;Greater than or equal to;yes;;("__ge_%(return_type)s(%(param_count)s,",",",")");defined;;; -;EQ;1;(ANY, ANY);BOOL;Equal to;yes;;("__eq_%(return_type)s(%(param_count)s,",",",")");defined;;; -;LT;1;(ANY, ANY);BOOL;Less than;yes;;("__lt_%(return_type)s(%(param_count)s,",",",")");defined;;; -;LE;1;(ANY, ANY);BOOL;Less than or equal to;yes;;("__le_%(return_type)s(%(param_count)s,",",",")");defined;;; -;NE;1;(ANY, ANY);BOOL;Not equal to;yes;;("__ne_%(return_type)s(%(param_count)s,",",",")");defined;;; -Character string;LEN;1;(STRING);INT;Length of string;no;;"__len(&%(IN_value)s)";defined;;; -;LEFT;1;(STRING, L);STRING;string left of;no;;"__left(&%(IN_value)s, %(L_value)s)";defined;;; -;RIGHT;1;(STRING, L);STRING;string right of;no;;"__right(&%(IN_value)s, %(L_value)s)";defined;;; -;MID;1;(STRING, L, P);STRING;string from the middle;no;;"__mid(&%(IN_value)s, %(L_value)s, %(P_value)s)";defined;;; -;CONCAT;1;(STRING, STRING);STRING;Concatenation;yes;;("__concat(%(param_count)s,",",&",")");defined;;; -;CONCAT;1;(DATE, TOD);DT;Time concatenation;no;;"__time_add(&%(IN1_value)s, &%(IN2_value)s)";defined;;; -;INSERT;1;(STRING, STRING, P);STRING;Insertion (into);no;;"__insert(&%(IN1_value)s, &%(IN2_value)s, %(P_value)s)";defined;;; -;DELETE;1;(STRING, L, P);STRING;Deletion (within);no;;"__delete(&%(IN_value)s, %(L_value)s, %(P_value)s)";defined;;; -;REPLACE;1;(STRING, STRING, L, P);STRING;Replacement (within);no;;"__replace(&%(IN1_value)s, &%(IN2_value)s, %(L_value)s, %(P_value)s)";defined;;; -;FIND;1;(STRING, STRING);INT;Find position;no;;"__find(&%(IN1_value)s, &%(IN2_value)s)";defined;;; +Standard_functions_variables_types;name;type;comment;;;;;; +;N;UINT;Number of bits to be shifted;;;;;; +;L;UINT;Left position within character string;;;;;; +;P;UINT;Position within character string;;;;;; +;G;BOOL;Selection out of 2 inputs (gate);;;;;; +;K;ANY_INT;Selection out of n inputs;;;;;; +;MN;ANY;Minimum value for limitation;;;;;; +;MX;ANY;Maximum value for limitation;;;;;; +;;;;;;;;; +;;;;;;;;; +;;;;;;;;; +;;;;;;;;; +;;;;;;;;; +;;;;;;;;; +Standard_functions_type;name;baseinputnumber;inputs;outputs;comment;extensible;python_eval_c_code_format;return_type_rule;lib_typed_decl +Type conversion;*_TO_**;1;(ANY);ANY;Data type conversion;no;ANY_TO_ANY_FORMAT_GEN(ANY_TO_ANY_LIST,fdecl);defined; +;TRUNC;1;(ANY_REAL);ANY_INT;Rounding up/down;no;"(int)%(IN_value)s";&search_constant_type_c::constant_int_type_name; +;BCD_TO_**;1;(ANY_BIT);ANY_INT;Conversion from BCD;no;ANY_TO_ANY_FORMAT_GEN(BCD_TO_ANY_LIST,fdecl);defined; +;*_TO_BCD;1;(ANY_INT);ANY_BIT;Conversion to BCD;no;ANY_TO_ANY_FORMAT_GEN(ANY_TO_BCD_LIST,fdecl);&search_constant_type_c::constant_int_type_name; +;DATE_AND_TIME_TO_TIME_OF_DAY;1;(DT);TOD;Conversion to time-of-day;no;"__date_and_time_to_time_of_day(%(IN_value)s)";defined; +;DATE_AND_TIME_TO_DATE;1;(DT);DATE;Conversion to date;no;"__date_and_time_to_date(%(IN_value)s)";defined; +Numerical;ABS;1;(ANY_NUM);ANY_NUM;Absolute number;no;"__abs_%(IN_type)s(%(IN_value)s)";IN_type_symbol;"__abs_%(return_type)s(%(return_type)s IN){ return IN > 0 ? IN : -IN {sc}}" +;SQRT;1;(ANY_REAL);ANY_REAL;Square root (base 2);no;"sqrt(%(IN_value)s)";IN_type_symbol; +;LN;1;(ANY_REAL);ANY_REAL;Natural logarithm;no;"ln(%(IN_value)s)";IN_type_symbol; +;LOG;1;(ANY_REAL);ANY_REAL;Logarithm to base 10;no;"log(%(IN_value)s)";IN_type_symbol; +;EXP;1;(ANY_REAL);ANY_REAL;Exponentiation;no;"exp(%(IN_value)s)";IN_type_symbol; +;SIN;1;(ANY_REAL);ANY_REAL;Sine;no;"sin(%(IN_value)s)";IN_type_symbol; +;COS;1;(ANY_REAL);ANY_REAL;Cosine;no;"cos(%(IN_value)s)";IN_type_symbol; +;TAN;1;(ANY_REAL);ANY_REAL;Tangent;no;"tan(%(IN_value)s)";IN_type_symbol; +;ASIN;1;(ANY_REAL);ANY_REAL;Arc sine;no;"asin(%(IN_value)s)";IN_type_symbol; +;ACOS;1;(ANY_REAL);ANY_REAL;Arc cosine;no;"acos(%(IN_value)s)";IN_type_symbol; +;ATAN;1;(ANY_REAL);ANY_REAL;Arc tangent;no;"atan(%(IN_value)s)";IN_type_symbol; +Arithmetic;ADD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Addition;yes;("(","+",")");copy_input; +;ADD;1;(TIME, TIME);TIME;Time addition;no;"__time_add(%(IN1_value)s, %(IN2_value)s)";defined; +;ADD;1;(TOD, TIME);TOD;Time-of-day addition;no;"__time_add(%(IN1_value)s, %(IN2_value)s)";defined; +;ADD;1;(DT, TIME);DT;Date addition;no;"__time_add(%(IN1_value)s, %(IN2_value)s)";defined; +;MUL;1;(ANY_NUM, ANY_NUM);ANY_NUM;Multiplication;yes;("(","*",")");copy_input; +;MUL;1;(TIME, ANY_NUM);TIME;Time multiplication;no;"__time_mul(%(IN1_value)s, %(IN2_value)s)";defined; +;SUB;1;(ANY_NUM, ANY_NUM);ANY_NUM;Subtraction;no;("(","-",")");copy_input; +;SUB;1;(TIME, TIME);TIME;Time subtraction;no;"__time_sub(%(IN1_value)s, %(IN2_value)s)";defined; +;SUB;1;(DATE, DATE);TIME;Date subtraction;no;"__time_sub(%(IN1_value)s, %(IN2_value)s)";defined; +;SUB;1;(TOD, TIME);TOD;Time-of-day subtraction;no;"__time_sub(%(IN1_value)s, %(IN2_value)s)";defined; +;SUB;1;(TOD, TOD);TIME;Time-of-day subtraction;no;"__time_sub(%(IN1_value)s, %(IN2_value)s)";defined; +;SUB;1;(DT, TIME);DT;Date and time subtraction;no;"__time_sub(%(IN1_value)s, %(IN2_value)s)";defined; +;SUB;1;(DT, DT);TIME;Date and time subtraction;no;"__time_sub(%(IN1_value)s, %(IN2_value)s)";defined; +;DIV;1;(ANY_NUM, ANY_NUM);ANY_NUM;Division;no;("(","/",")");copy_input; +;DIV;1;(TIME, ANY_NUM);TIME;Time division;no;"__time_div(%(IN1_value)s, %(IN2_value)s)";defined; +;MOD;1;(ANY_NUM, ANY_NUM);ANY_NUM;Remainder (modulo);no;("(","%",")");copy_input; +;EXPT;1;(ANY_NUM, ANY_NUM);ANY_NUM;Exponent;no;"pow(%(IN1_value)s, %(IN2_value)s)";copy_input; +;MOVE;1;(ANY_NUM);ANY_NUM;Assignment;no;"%(IN_value)s";copy_input; +Bit-shift;SHL;1;(ANY_BIT, N);ANY_BIT;Shift left;no;"%(IN_value)s<<%(N_value)s";IN_type_symbol; +;SHR;1;(ANY_BIT, N);ANY_BIT;Shift right;no;"%(IN_value)s>>%(N_value)s";IN_type_symbol; +;ROR;1;(ANY_NBIT, N);ANY_NBIT;Rotate right;no;"__ror_%(IN_type)s(%(IN_value)s, %(N_value)s)";IN_type_symbol;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}" +;ROL;1;(ANY_NBIT, N);ANY_NBIT;Rotate left;no;"__rol_%(IN_type)s(%(IN_value)s, %(N_value)s)";IN_type_symbol;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}" +Bitwise;AND;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise AND;yes;("(%(start_bool_filter)s","&",")%(end_bool_filter)s");copy_input; +;OR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise OR;yes;("(%(start_bool_filter)s","|",")%(end_bool_filter)s");copy_input; +;XOR;1;(ANY_BIT, ANY_BIT);ANY_BIT;Bitwise EXOR;yes;("(%(start_bool_filter)s","^",")%(end_bool_filter)s");copy_input; +;NOT;1;(ANY_BIT);ANY_BIT;Bitwise inverting;no;"~%(IN_value)s";IN_type_symbol; +Selection;SEL;0;(G, ANY, ANY);ANY;Binary selection (1 of 2);no;"%(G_value)s ? %(IN1_value)s : %(IN0_value)s";copy_input; +;MAX;1;(ANY, ANY);ANY;Maximum;yes;("__max_%(return_type)s(%(param_count)s,",",",")");copy_input;"__max_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 > IN2 ? IN1 : IN2 {sc}}" +;MIN;1;(ANY, ANY);ANY;Minimum;yes;("__min_%(return_type)s(%(param_count)s,",",",")");copy_input;"__min_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 < IN2 ? IN1 : IN2 {sc}}" +;LIMIT;1;(MN, ANY, MX);ANY;Limitation;no;"__limit_%(IN_type)s(%(MN_value)s, %(IN_value)s, %(MX_value)s)";IN_type_symbol; +;MUX;0;(K, ANY, ANY);ANY;Multiplexer (select 1 of N);yes;("__mux_%(return_type)s(%(param_count)s,",",",")");copy_input; +Comparison;GT;1;(ANY, ANY);BOOL;Greater than;yes;("__gt_%(return_type)s(%(param_count)s,",",",")");defined;"__gt_%(return_type)s(%(return_type)s IN1, %(return_type)s IN2){ return IN1 0 ? IN : -IN {sc}}" +;GE;1;(ANY, ANY);BOOL;Greater than or equal to;yes;("__ge_%(return_type)s(%(param_count)s,",",",")");defined; +;EQ;1;(ANY, ANY);BOOL;Equal to;yes;("__eq_%(return_type)s(%(param_count)s,",",",")");defined; +;LT;1;(ANY, ANY);BOOL;Less than;yes;("__lt_%(return_type)s(%(param_count)s,",",",")");defined; +;LE;1;(ANY, ANY);BOOL;Less than or equal to;yes;("__le_%(return_type)s(%(param_count)s,",",",")");defined; +;NE;1;(ANY, ANY);BOOL;Not equal to;yes;("__ne_%(return_type)s(%(param_count)s,",",",")");defined; +Character string;LEN;1;(STRING);INT;Length of string;no;"__len(%(IN_value)s)";defined; +;LEFT;1;(STRING, L);STRING;string left of;no;"__left(%(IN_value)s, %(L_value)s)";defined; +;RIGHT;1;(STRING, L);STRING;string right of;no;"__right(%(IN_value)s, %(L_value)s)";defined; +;MID;1;(STRING, L, P);STRING;string from the middle;no;"__mid(%(IN_value)s, %(L_value)s, %(P_value)s)";defined; +;CONCAT;1;(STRING, STRING);STRING;Concatenation;yes;("__concat(%(param_count)s,",",&",")");defined; +;CONCAT;1;(DATE, TOD);DT;Time concatenation;no;"__time_add(%(IN1_value)s, %(IN2_value)s)";defined; +;INSERT;1;(STRING, STRING, P);STRING;Insertion (into);no;"__insert(%(IN1_value)s, %(IN2_value)s, %(P_value)s)";defined; +;DELETE;1;(STRING, L, P);STRING;Deletion (within);no;"__delete(%(IN_value)s, %(L_value)s, %(P_value)s)";defined; +;REPLACE;1;(STRING, STRING, L, P);STRING;Replacement (within);no;"__replace(%(IN1_value)s, %(IN2_value)s, %(L_value)s, %(P_value)s)";defined; +;FIND;1;(STRING, STRING);INT;Find position;no;"__find(%(IN1_value)s, %(IN2_value)s)";defined; diff -r 364320323b4d -r 8dc68e669d99 plcopen/structures.py --- a/plcopen/structures.py Mon Jun 25 18:57:14 2007 +0200 +++ b/plcopen/structures.py Fri Jul 06 18:55:52 2007 +0200 @@ -151,29 +151,32 @@ ("ANY_ELEMENTARY", "ANY"), ("ANY_MAGNITUDE", "ANY_ELEMENTARY"), ("ANY_BIT", "ANY_ELEMENTARY"), + ("ANY_NBIT", "ANY_BIT"), ("ANY_STRING", "ANY_ELEMENTARY"), ("ANY_DATE", "ANY_ELEMENTARY"), ("ANY_NUM", "ANY_MAGNITUDE"), ("ANY_REAL", "ANY_NUM"), ("ANY_INT", "ANY_NUM"), + ("ANY_SINT", "ANY_INT"), + ("ANY_UINT", "ANY_INT"), ("REAL", "ANY_REAL"), ("LREAL", "ANY_REAL"), - ("SINT", "ANY_INT"), - ("INT", "ANY_INT"), - ("DINT", "ANY_INT"), - ("LINT", "ANY_INT"), - ("USINT", "ANY_INT"), - ("UINT", "ANY_INT"), - ("UDINT", "ANY_INT"), - ("ULINT", "ANY_INT"), + ("SINT", "ANY_SINT"), + ("INT", "ANY_SINT"), + ("DINT", "ANY_SINT"), + ("LINT", "ANY_SINT"), + ("USINT", "ANY_UINT"), + ("UINT", "ANY_UINT"), + ("UDINT", "ANY_UINT"), + ("ULINT", "ANY_UINT"), ("TIME", "ANY_MAGNITUDE"), ("BOOL", "ANY_BIT"), - ("BYTE", "ANY_BIT"), - ("WORD", "ANY_BIT"), - ("DWORD", "ANY_BIT"), - ("LWORD", "ANY_BIT"), + ("BYTE", "ANY_NBIT"), + ("WORD", "ANY_NBIT"), + ("DWORD", "ANY_NBIT"), + ("LWORD", "ANY_NBIT"), ("STRING", "ANY_STRING"), - ("WSTRING", "ANY_STRING"), + #("WSTRING", "ANY_STRING"), # TODO ("DATE", "ANY_DATE"), ("TOD", "ANY_DATE"), ("DT", "ANY_DATE")] @@ -331,20 +334,51 @@ return params -ANY_T0_ANY_LIST=[ +ANY_TO_ANY_LIST=[ + # simple type conv are let as C cast (("ANY_NUM","ANY_BIT"),("ANY_NUM","ANY_BIT"), "(%(return_type)s)%(IN_value)s"), - (("ANY_NUM","ANY_BIT"),("ANY_DATE","TIME"), "(%(return_type)s)real_to_time(%(IN_value)s)"), - (("ANY_DATE","TIME"), ("ANY_NUM","ANY_BIT"), "(%(return_type)s)time_to_real(%(IN_value)s)"), - (("ANY_DATE","TIME"), ("ANY_STRING",), "(%(return_type)s)time_to_string(%(IN_value)s)"), - (("ANY_STRING",), ("ANY_DATE","TIME"), "(%(return_type)s)string_to_time(%(IN_value)s)"), - (("ANY_BIT",), ("ANY_STRING",), "(%(return_type)s)int_to_string(%(IN_value)s, 16)"), - (("ANY_NUM",), ("ANY_STRING",), "(%(return_type)s)int_to_string(%(IN_value)s, 10)"), - (("ANY_STRING",), ("ANY_BIT",), "(%(return_type)s)string_to_int(%(IN_value)s, 16)"), - (("ANY_STRING",), ("ANY_NUM",), "(%(return_type)s)string_to_int(%(IN_value)s, 10)")] - -def ANY_TO_ANY_FORMAT_GEN(fdecl): - - for (InTypes, OutTypes, Format) in ANY_T0_ANY_LIST: + # TO_TIME + (("ANY_INT","ANY_BIT"),("ANY_DATE","TIME"), "(%(return_type)s)__int_to_time(%(IN_value)s)"), + (("ANY_REAL",),("ANY_DATE","TIME"), "(%(return_type)s)__real_to_time(%(IN_value)s)"), + (("ANY_STRING",), ("ANY_DATE","TIME"), "(%(return_type)s)__string_to_time(%(IN_value)s)"), + # FROM_TIME + (("ANY_DATE","TIME"), ("ANY_REAL",), "(%(return_type)s)__time_to_real(%(IN_value)s)"), + (("ANY_DATE","TIME"), ("ANY_INT","ANY_BIT"), "(%(return_type)s)__time_to_int(%(IN_value)s)"), + (("TIME",), ("ANY_STRING",), "(%(return_type)s)__time_to_string(%(IN_value)s)"), + (("DATE",), ("ANY_STRING",), "(%(return_type)s)__date_to_string(%(IN_value)s)"), + (("TOD",), ("ANY_STRING",), "(%(return_type)s)__tod_to_string(%(IN_value)s)"), + (("DT",), ("ANY_STRING",), "(%(return_type)s)__dt_to_string(%(IN_value)s)"), + # TO_STRING + (("BOOL",), ("ANY_STRING",), "(%(return_type)s)__bool_to_string(%(IN_value)s)"), + (("ANY_BIT",), ("ANY_STRING",), "(%(return_type)s)__bit_to_string(%(IN_value)s)"), + (("ANY_REAL",), ("ANY_STRING",), "(%(return_type)s)__real_to_string(%(IN_value)s)"), + (("ANY_SINT",), ("ANY_STRING",), "(%(return_type)s)__sint_to_string(%(IN_value)s)"), + (("ANY_UINT",), ("ANY_STRING",), "(%(return_type)s)__uint_to_string(%(IN_value)s)"), + # FROM_STRING + (("ANY_STRING",), ("BOOL",), "(%(return_type)s)__string_to_bool(%(IN_value)s)"), + (("ANY_STRING",), ("ANY_BIT",), "(%(return_type)s)__string_to_bit(%(IN_value)s)"), + (("ANY_STRING",), ("ANY_SINT",), "(%(return_type)s)__string_to_sint(%(IN_value)s)"), + (("ANY_STRING",), ("ANY_UINT",), "(%(return_type)s)__string_to_uint(%(IN_value)s)"), + (("ANY_STRING",), ("ANY_REAL",), "(%(return_type)s)__string_to_real(%(IN_value)s)")] + + +BCD_TO_ANY_LIST=[ + (("BYTE",),("USINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)"), + (("WORD",),("UINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)"), + (("DWORD",),("UDINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)"), + (("LWORD",),("ULINT",), "(%(return_type)s)__bcd_to_uint(%(IN_value)s)")] + + +ANY_TO_BCD_LIST=[ + (("USINT",),("BYTE",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)"), + (("UINT",),("WORD",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)"), + (("UDINT",),("DWORD",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)"), + (("ULINT",),("LWORD",), "(%(return_type)s)__uint_to_bcd(%(IN_value)s)")] + + +def ANY_TO_ANY_FORMAT_GEN(any_to_any_list, fdecl): + + for (InTypes, OutTypes, Format) in any_to_any_list: outs = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["outputs"][0][1],testtype), OutTypes)) inps = reduce(lambda a,b: a or b, map(lambda testtype : IsOfType(fdecl["inputs"][0][1],testtype), InTypes)) if inps and outs and fdecl["outputs"][0][1] != fdecl["inputs"][0][1]: @@ -396,14 +430,11 @@ Function_decl[param] = translate[param](value) Function_decl["type"] = "function" - if Function_decl["name"].startswith('*') : + if Function_decl["name"].startswith('*') or Function_decl["name"].endswith('*') : input_ovrloading_types = GetSubTypes(Function_decl["inputs"][0][1]) + output_types = GetSubTypes(Function_decl["outputs"][0][1]) else: input_ovrloading_types = [None] - - if Function_decl["name"].endswith('*') : - output_types = GetSubTypes(Function_decl["outputs"][0][1]) - else: output_types = [None] funcdeclname_orig = Function_decl["name"] @@ -458,359 +489,7 @@ return Standard_Functions_Decl - -if __name__ == '__main__': - - import pprint - pp = pprint.PrettyPrinter(indent=4) - - def ANY_to_compiler_test_type_GEN(typename, paramname): - return {"ANY" : "", - "ANY_BIT" : "if(search_expression_type->is_binary_type(%(paramname)s_type_symbol))", - "ANY_NUM" : "if(search_expression_type->is_num_type(%(paramname)s_type_symbol))", - "ANY_REAL" : "if(search_expression_type->is_real_type(%(paramname)s_type_symbol))", - "ANY_INT" : "if(search_expression_type->is_integer_type(%(paramname)s_type_symbol))" - }.get(typename, - "if (typeid(*last_type_symbol) == typeid(%(typename)s_type_name_c))")%{ - "paramname" : paramname, "typename": typename.lower()} - - def recurse_and_indent(fdecls, indent, do_type_search_only = False, do_il = False): - if type(fdecls) != type(tuple()): - res = "" - for Paramname, ParamTypes in fdecls.iteritems(): - if do_il: - res += """ -{""" - if not do_type_search_only: - res += """ - /* Get the value from a foo( = ) style call */ - symbol_c *%(input_name)s_param_value = &this->default_variable_name; -"""%{"input_name":Paramname} - res += """ - symbol_c *%(input_name)s_type_symbol = param_data_type; - last_type_symbol = param_data_type; -"""%{"input_name":Paramname} - else: - res += """ -{ - identifier_c param_name("%(input_name)s"); - /* Get the value from a foo( = ) style call */ - symbol_c *%(input_name)s_param_value = function_call_param_iterator.search(¶m_name); - - /* Get the value from a foo() style call */ - if (%(input_name)s_param_value == NULL) - %(input_name)s_param_value = function_call_param_iterator.next(); - symbol_c *%(input_name)s_type_symbol = search_expression_type->get_type(%(input_name)s_param_value); - 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 ; -"""%{"input_name":Paramname} - - for ParamType,NextParamDecl in ParamTypes.iteritems(): - - res += """ - %(type_test)s - { -%(if_good_type_code)s - } -"""%{ - "type_test":ANY_to_compiler_test_type_GEN(ParamType,Paramname), - "if_good_type_code":recurse_and_indent(NextParamDecl,indent,do_type_search_only).replace('\n','\n ')} - - res += """ - ERROR; -} -""" - - return res.replace('\n','\n'+indent) - else: - res = "\n" - fdecl=fdecls[0] - - result_type_rule = fdecl["return_type_rule"] - res += { - "copy_input" : "symbol_c * return_type_symbol = last_type_symbol;\n", - "defined" : "symbol_c * return_type_symbol = &search_constant_type_c::%s_type_name;\n"%fdecl["outputs"][0][1].lower(), - }.get(result_type_rule, "symbol_c * return_type_symbol = %s;\n"%result_type_rule) - - if not do_type_search_only: - code_gen = eval(fdecl["python_eval_c_code_format"]) - - code_gen_dic_decl = {} - for paramname,paramtype,unused in fdecl["inputs"]: - code_gen_dic_decl[paramname+"_value"] = '");\n%s_param_value->accept(*this);\ns4o.print("'%(paramname) - code_gen_dic_decl[paramname+"_type"] = '");\n%s_type_symbol->accept(*this);\ns4o.print("'%(paramname) - code_gen_dic_decl["return_type"] = '");\nreturn_type_symbol->accept(*this);\ns4o.print("' - code_gen_dic_decl["param_count"] = '");\ns4o.print_integer(nb_param);\ns4o.print("' - code_gen_dic_decl["start_bool_filter"] = '");\nif (search_expression_type->is_bool_type(last_type_symbol))\n s4o.print("(' - 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("' - - if type(code_gen) == type(tuple()): - res += 's4o.print("%s");\n'%(code_gen[0]%code_gen_dic_decl) - static_param_accept_list = [] - for paramname,paramtype,unused in fdecl["inputs"]: - static_param_accept_list.append("%s_param_value->accept(*this);\n"%(paramname)) - res += ('s4o.print("%s");\n'%(code_gen[1])).join(static_param_accept_list) - code = 's4o.print("%s");\nparam_value->accept(*this);\n'%(code_gen[1]) - end_code = 's4o.print("%s");\nreturn NULL;\n'%(code_gen[2]%code_gen_dic_decl) - else: - code = '' - end_code = ('s4o.print("' + code_gen%code_gen_dic_decl + '");\nreturn NULL;\n').replace('s4o.print("");\n','') - - if fdecl["extensible"]: - res += (""" -int base_num = %d; -symbol_c *param_value = NULL; -do{ - char my_name[10]; - sprintf(my_name, "IN%%d", base_num++); - identifier_c param_name(my_name); - - /* Get the value from a foo( = ) style call */ - param_value = function_call_param_iterator.search(¶m_name); - - /* Get the value from a foo() style call */ - if (param_value == NULL) - param_value = function_call_param_iterator.next(); - if (param_value != NULL){ - symbol_c *current_type_symbol = search_expression_type->get_type(param_value); - 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 ; - - /*Function specific CODE */ - %s - } - -}while(param_value != NULL); -%s -"""%(fdecl["baseinputnumber"]+2, code.replace('\n','\n '), end_code)) - else: - #res += code + end_code - res += end_code - else: - res += "return return_type_symbol;\n" - - - return res.replace('\n','\n'+indent) - -################################################################### -### ### -### MAIN ### -### ### -################################################################### - - # Get definitions - std_decl = get_standard_funtions(csv_file_to_table(open("iec_std.csv")))#, True) - - # Reorganize into a dict of dict, according - # fname : paramname : paramtype : paraname : paramtype... - # Keep ptrack of original order in a separated list - std_fdecls = {} - official_order = [] - for section in std_decl: - for fdecl in section["list"]: - if len(official_order)==0 or official_order[-1] != fdecl["name"]: - official_order.append(fdecl["name"]) - # store all func by name in a dict - std_fdecls_fdecl_name = std_fdecls.get(fdecl["name"], {}) - current = std_fdecls_fdecl_name - for i in fdecl["inputs"]: - current[i[0]] = current.get(i[0], {}) - current = current[i[0]] - last = current - current[i[1]] = current.get(i[1], {}) - current = current[i[1]] - last[i[1]]=(fdecl,) - std_fdecls[fdecl["name"]] = std_fdecls_fdecl_name - - # Generate the long enumeration of std function types - function_type_decl = """ -/**** - * IEC 61131-3 standard function lib - * generated code, do not edit by hand - */ -typedef enum { -""" - for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: - function_type_decl += " function_"+fname.lower()+",\n" - - function_type_decl += """ function_none -} function_type_t; -""" - - # Generate the funct thaat return enumerated according function name - get_function_type_decl = """ -/**** - * IEC 61131-3 standard function lib - * generated code, do not edit by hand - */ -function_type_t get_function_type(identifier_c *function_name) { -""" - for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: - get_function_type_decl += """ - if (!strcasecmp(function_name->value, "%s")) - return function_%s; -"""%(fname,fname.lower()) - - get_function_type_decl += """ - else return function_none; -} - -""" - - # Generate the part of generate_cc_st_c::visit(function_invocation) - # that is responsible to generate C code for std lib calls. - st_code_gen = """ -/**** - * IEC 61131-3 standard function lib - * generated code, do not edit by hand - */ -switch(current_function_type){ -""" - - for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: - st_code_gen += """ -/**** - *%s - */ - case function_%s : - { - symbol_c *last_type_symbol = NULL; -""" %(fname, fname.lower()) - indent = " " - - st_code_gen += recurse_and_indent(fdecls, indent).replace('\n','\n ') - - st_code_gen += """ - }/*function_%s*/ - break; -""" %(fname.lower()) - st_code_gen += """ - case function_none : - ERROR; -} -return NULL; -""" - - # Generate the part of generate_cc_il_c::visit(il_function_call) - # that is responsible to generate C code for std lib calls. - il_code_gen = """ -/**** - * IEC 61131-3 standard function lib - * generated code, do not edit by hand - */ -switch(current_function_type){ -""" - - for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: - il_code_gen += """ -/**** - *%s - */ - case function_%s : - { - symbol_c *last_type_symbol = NULL; -""" %(fname, fname.lower()) - indent = " " - - il_code_gen += recurse_and_indent(fdecls, indent, do_il=True).replace('\n','\n ') - - il_code_gen += """ - }/*function_%s*/ - break; -""" %(fname.lower()) - il_code_gen += """ - case function_none : - ERROR; -} -return NULL; -""" - - # Generate the part of search_expression_type_c::visit(function_invocation) - # that is responsible of returning type symbol for function invocation. - search_type_code = """ -/**** - * IEC 61131-3 standard function lib - * generated code, do not edit by hand - */ - -void *compute_standard_function_st(function_invocation_c *symbol) { - - function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); - function_call_param_iterator_c function_call_param_iterator(symbol); - search_expression_type_c* search_expression_type = this; - - switch(current_function_type){ -""" - - for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: - search_type_code += """ -/**** - *%s - */ - case function_%s : - { - symbol_c *last_type_symbol = NULL; -""" %(fname, fname.lower()) - indent = " " - - search_type_code += recurse_and_indent(fdecls, indent, True).replace('\n','\n ') - - search_type_code += """ - }/*function_%s*/ - break; -""" %(fname.lower()) - search_type_code += """ - case function_none : - ERROR; - } - return NULL; -} - -void *compute_standard_function_il(il_function_call_c *symbol, symbol_c *param_data_type) { - - function_type_t current_function_type = get_function_type((identifier_c *)symbol->function_name); - function_call_param_iterator_c function_call_param_iterator(symbol); - search_expression_type_c* search_expression_type = this; - - switch(current_function_type){ -""" - - for fname, fdecls in [ (fname,std_fdecls[fname]) for fname in official_order ]: - search_type_code += """ -/**** - *%s - */ - case function_%s : - { - symbol_c *last_type_symbol = NULL; -""" %(fname, fname.lower()) - indent = " " - - search_type_code += recurse_and_indent(fdecls, indent, True, True).replace('\n','\n ') - - search_type_code += """ - }/*function_%s*/ - break; -""" %(fname.lower()) - search_type_code += """ - case function_none : - ERROR; - } - return NULL; -} -""" - - - # Now, print that out, or write to files from sys.argv - for name, ext in [ - ('function_type_decl','h'), - ('get_function_type_decl','c'), - ('st_code_gen','c'), - ('il_code_gen','c'), - ('search_type_code','c')]: - fd = open(os.path.join(sys.argv[1],name+'.'+ext),'w') - fd.write(eval(name)) - fd.close() -else: - # Put standard functions declaration in Bloktypes - BlockTypes.extend(get_standard_funtions(csv_file_to_table(open(os.path.join(sys.path[0], "plcopen/iec_std.csv"))))) - - \ No newline at end of file +std_decl = get_standard_funtions(csv_file_to_table(open(os.path.join(os.path.split(__file__)[0],"iec_std.csv"))))#, True) + +BlockTypes.extend(std_decl) +