Fixing generated code for global variables. Adding support for defining global variables with complex type
Fixing bug preventing to generate SFC transition with IL body
--- a/lib/accessor.h Fri Nov 18 17:21:16 2011 +0100
+++ b/lib/accessor.h Fri Nov 18 17:28:37 2011 +0100
@@ -6,15 +6,14 @@
// variable declaration macros
#define __DECLARE_VAR(type, name)\
__IEC_##type##_t name;
-#define __DECLARE_GLOBAL(type, resource, name)\
- __IEC_##type##_t resource##__##name;\
- static __IEC_##type##_t *GLOBAL__##name = &resource##__##name;\
+#define __DECLARE_GLOBAL(type, domain, name)\
+ __IEC_##type##_t domain##__##name;\
+ static __IEC_##type##_t *GLOBAL__##name = &(domain##__##name);\
void __INIT_GLOBAL_##name(type value) {\
(*GLOBAL__##name).value = value;\
}\
- void __SET_GLOBAL_##name(type value) {\
- if (!((*GLOBAL__##name).flags & __IEC_FORCE_FLAG))\
- (*GLOBAL__##name).value = value;\
+ IEC_BYTE __IS_GLOBAL_##name##_FORCED(void) {\
+ return (*GLOBAL__##name).flags & __IEC_FORCE_FLAG;\
}\
type* __GET_GLOBAL_##name(void) {\
return &((*GLOBAL__##name).value);\
@@ -23,13 +22,12 @@
extern type *location;
#define __DECLARE_GLOBAL_LOCATED(type, resource, name)\
__IEC_##type##_p resource##__##name;\
- static __IEC_##type##_p *GLOBAL__##name = &resource##__##name;\
+ static __IEC_##type##_p *GLOBAL__##name = &(resource##__##name);\
void __INIT_GLOBAL_##name(type value) {\
*((*GLOBAL__##name).value) = value;\
}\
- void __SET_GLOBAL_##name(type value) {\
- if (!((*GLOBAL__##name).flags & __IEC_FORCE_FLAG))\
- *((*GLOBAL__##name).value) = value;\
+ IEC_BYTE __IS_GLOBAL_##name##_FORCED(void) {\
+ return (*GLOBAL__##name).flags & __IEC_FORCE_FLAG;\
}\
type* __GET_GLOBAL_##name(void) {\
return (*GLOBAL__##name).value;\
@@ -52,16 +50,21 @@
__INIT_GLOBAL_##name(temp);\
__INIT_RETAIN((*GLOBAL__##name), retained)\
}
-#define __INIT_GLOBAL_LOCATED(resource, name, location, retained)\
- resource##__##name.value = location;\
- __INIT_RETAIN(resource##__##name, retained)
+#define __INIT_GLOBAL_LOCATED(domain, name, location, retained)\
+ domain##__##name.value = location;\
+ __INIT_RETAIN(domain##__##name, retained)
#define __INIT_EXTERNAL(type, global, name, retained)\
- name.value = __GET_GLOBAL_##global();\
- __INIT_RETAIN(name, retained)
+ {\
+ extern type* __GET_GLOBAL_##global();\
+ name.value = __GET_GLOBAL_##global();\
+ __INIT_RETAIN(name, retained)\
+ }
#define __INIT_LOCATED(type, location, name, retained)\
- {extern type *location;\
- name.value = location;\
- __INIT_RETAIN(name, retained)}
+ {\
+ extern type *location;\
+ name.value = location;\
+ __INIT_RETAIN(name, retained)\
+ }
#define __INIT_LOCATED_VALUE(name, initial)\
*(name.value) = initial;
@@ -81,14 +84,12 @@
((name.flags & __IEC_FORCE_FLAG) ? &(name.fvalue __VA_ARGS__) : &(*(name.value) __VA_ARGS__))
// variable setting macros
-#define __SET_VAR(name, new_value, ...)\
- if (!(name.flags & __IEC_FORCE_FLAG)) name.value __VA_ARGS__ = new_value
-#define __SET_EXTERNAL(global, name, new_value)\
- if (!(name.flags & __IEC_FORCE_FLAG))\
- __SET_GLOBAL_##global(new_value)
-#define __SET_COMPLEX_EXTERNAL(name, new_value, ...)\
- *(name.value) __VA_ARGS__ = new_value
-#define __SET_LOCATED(name, new_value, ...)\
- if (!(name.flags & __IEC_FORCE_FLAG)) *(name.value) __VA_ARGS__ = new_value
+#define __SET_VAR(prefix, name, new_value, ...)\
+ if (!(prefix name.flags & __IEC_FORCE_FLAG)) prefix name.value __VA_ARGS__ = new_value
+#define __SET_EXTERNAL(prefix, name, new_value, ...)\
+ if (!(prefix name.flags & __IEC_FORCE_FLAG || __IS_GLOBAL_##name##_FORCED()))\
+ (*(prefix name.value)) __VA_ARGS__ = new_value
+#define __SET_LOCATED(prefix, name, new_value, ...)\
+ if (!(prefix name.flags & __IEC_FORCE_FLAG)) *(prefix name.value) __VA_ARGS__ = new_value
#endif //__ACCESSOR_H
--- a/lib/timer.txt Fri Nov 18 17:21:16 2011 +0100
+++ b/lib/timer.txt Fri Nov 18 17:28:37 2011 +0100
@@ -90,7 +90,7 @@
CURRENT_TIME, START_TIME : TIME;
END_VAR
- {__SET_VAR(data__->CURRENT_TIME,__CURRENT_TIME)}
+ {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
IF ((STATE = 0) AND NOT(PREV_IN) AND IN) (* found rising edge on IN *)
THEN
@@ -171,7 +171,7 @@
CURRENT_TIME, START_TIME : TIME;
END_VAR
- {__SET_VAR(data__->CURRENT_TIME,__CURRENT_TIME)}
+ {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
IF ((STATE = 0) AND NOT(PREV_IN) AND IN) (* found rising edge on IN *)
THEN
@@ -255,7 +255,7 @@
CURRENT_TIME, START_TIME : TIME;
END_VAR
- {__SET_VAR(data__->CURRENT_TIME,__CURRENT_TIME)}
+ {__SET_VAR(data__->,CURRENT_TIME,__CURRENT_TIME)}
IF ((STATE = 0) AND PREV_IN AND NOT(IN)) (* found falling edge on IN *)
THEN
--- a/stage4/generate_c/generate_c.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c.cc Fri Nov 18 17:28:37 2011 +0100
@@ -124,7 +124,6 @@
/* Variable setter symbol for accessor macros */
#define SET_VAR "__SET_VAR"
#define SET_EXTERNAL "__SET_EXTERNAL"
-#define SET_COMPLEX_EXTERNAL "__SET_COMPLEX_EXTERNAL"
#define SET_LOCATED "__SET_LOCATED"
@@ -1334,7 +1333,7 @@
s4o.print(SET_VAR);
s4o.print("(");
s4o.print(FB_FUNCTION_PARAM);
- s4o.print("->ENO,__BOOL_LITERAL(FALSE));\n");
+ s4o.print("->,ENO,__BOOL_LITERAL(FALSE));\n");
s4o.print(s4o.indent_spaces + "return;\n");
s4o.indent_left();
s4o.print(s4o.indent_spaces + "}\n");
@@ -1344,7 +1343,7 @@
s4o.print(SET_VAR);
s4o.print("(");
s4o.print(FB_FUNCTION_PARAM);
- s4o.print("->ENO,__BOOL_LITERAL(TRUE));\n");
+ s4o.print("->,ENO,__BOOL_LITERAL(TRUE));\n");
s4o.indent_left();
s4o.print(s4o.indent_spaces + "}\n");
@@ -1621,7 +1620,8 @@
/* (A.2) Global variables */
vardecl = new generate_c_vardecl_c(&s4o,
generate_c_vardecl_c::local_vf,
- generate_c_vardecl_c::global_vt);
+ generate_c_vardecl_c::global_vt,
+ symbol->configuration_name);
vardecl->print(symbol);
delete vardecl;
s4o.print("\n");
@@ -1894,6 +1894,7 @@
s4o.print("extern unsigned long long common_ticktime__;\n\n");
s4o.print("#include \"accessor.h\"\n\n");
+ s4o.print("#include \"POUS.h\"\n\n");
/* (A.2) Global variables... */
if (current_global_vars != NULL) {
@@ -1907,7 +1908,6 @@
}
/* (A.3) POUs inclusion */
- s4o.print("#include \"POUS.h\"\n\n");
s4o.print("#include \"POUS.c\"\n\n");
wanted_declaretype = declare_dt;
@@ -2098,33 +2098,29 @@
if (symbol->single_data_source != NULL) {
symbol_c *config_var_decl = NULL;
symbol_c *res_var_decl = NULL;
- unsigned int vartype;
+ s4o.print(s4o.indent_spaces + "{");
symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->single_data_source))->global_var_name;
res_var_decl = search_resource_instance->get_decl(current_var_reference);
if (res_var_decl == NULL) {
config_var_decl = search_config_instance->get_decl(current_var_reference);
if (config_var_decl == NULL)
ERROR;
- vartype = search_config_instance->get_vartype();
- s4o.print(s4o.indent_spaces + "{extern ");
config_var_decl->accept(*this);
- s4o.print(" *");
- symbol->single_data_source->accept(*this);
- s4o.print("; ");
}
else {
- vartype = search_resource_instance->get_vartype();
- s4o.print(s4o.indent_spaces);
+ res_var_decl->accept(*this);
}
+ s4o.print("* ");
+ symbol->single_data_source->accept(*this);
+ s4o.print(" = __GET_GLOBAL_");
+ symbol->single_data_source->accept(*this);
+ s4o.print("();");
s4o.print(SET_VAR);
s4o.print("(");
current_task_name->accept(*this);
- s4o.print("_R_TRIG.CLK, *__GET_GLOBAL_");
+ s4o.print("_R_TRIG.,CLK, *");
symbol->single_data_source->accept(*this);
- s4o.print("());");
- if (config_var_decl != NULL)
- s4o.print("}");
- s4o.print("\n");
+ s4o.print(");}\n");
s4o.print(s4o.indent_spaces + "R_TRIG");
s4o.print(FB_FUNCTION_SUFFIX);
s4o.print("(&");
--- a/stage4/generate_c/generate_c_il.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c_il.cc Fri Nov 18 17:28:37 2011 +0100
@@ -132,6 +132,7 @@
expression_vg,
assignment_vg,
complextype_base_vg,
+ complextype_base_assignment_vg,
complextype_suffix_vg,
fparam_output_vg
} variablegeneration_t;
@@ -287,6 +288,11 @@
this->default_variable_back_name.accept(*this);
}
+ void reset_default_variable_name(void) {
+ this->default_variable_name.current_type = NULL;
+ this->default_variable_back_name.current_type = NULL;
+ }
+
private:
/* A helper function... */
/*
@@ -468,35 +474,21 @@
bool negative = false) {
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
bool type_is_complex = search_varfb_instance_type->type_is_complex();
- if (vartype == search_var_instance_decl_c::external_vt) {
- symbolic_variable_c *variable = dynamic_cast<symbolic_variable_c *>(symbol);
- /* TODO Find a solution for forcing global complex variables */
- if (variable != NULL) {
- s4o.print(SET_EXTERNAL);
- s4o.print("(");
- variable->var_name->accept(*this);
- s4o.print(",");
- }
- else {
- s4o.print(SET_COMPLEX_EXTERNAL);
- s4o.print("(");
- }
- }
- else {
- if (vartype == search_var_instance_decl_c::located_vt)
- s4o.print(SET_LOCATED);
- else
- s4o.print(SET_VAR);
- s4o.print("(");
- }
+ if (vartype == search_var_instance_decl_c::external_vt)
+ s4o.print(SET_EXTERNAL);
+ else if (vartype == search_var_instance_decl_c::located_vt)
+ s4o.print(SET_LOCATED);
+ else
+ s4o.print(SET_VAR);
+ s4o.print("(");
if (fb_symbol != NULL) {
print_variable_prefix();
fb_symbol->accept(*this);
- s4o.print(".");
+ s4o.print(".,");
}
else if (type_is_complex)
- wanted_variablegeneration = complextype_base_vg;
+ wanted_variablegeneration = complextype_base_assignment_vg;
else
wanted_variablegeneration = assignment_vg;
@@ -580,8 +572,13 @@
void *visit(symbolic_variable_c *symbol) {
unsigned int vartype;
switch (wanted_variablegeneration) {
+ case complextype_base_assignment_vg:
+ case assignment_vg:
+ this->print_variable_prefix();
+ s4o.print(",");
+ symbol->var_name->accept(*this);
+ break;
case complextype_base_vg:
- case assignment_vg:
generate_c_base_c::visit(symbol);
break;
case complextype_suffix_vg:
@@ -648,6 +645,7 @@
TRACE("structured_variable_c");
switch (wanted_variablegeneration) {
case complextype_base_vg:
+ case complextype_base_assignment_vg:
symbol->record_variable->accept(*this);
break;
case complextype_suffix_vg:
@@ -674,6 +672,7 @@
void *visit(array_variable_c *symbol) {
switch (wanted_variablegeneration) {
case complextype_base_vg:
+ case complextype_base_assignment_vg:
symbol->subscripted_variable->accept(*this);
break;
case complextype_suffix_vg:
--- a/stage4/generate_c/generate_c_inlinefcall.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c_inlinefcall.cc Fri Nov 18 17:28:37 2011 +0100
@@ -120,6 +120,9 @@
search_expression_type = new search_expression_type_c(scope);
search_varfb_instance_type = new search_varfb_instance_type_c(scope);
this->set_variable_prefix(variable_prefix);
+ current_operand = NULL;
+ current_operand_type = NULL;
+ il_default_variable_init_value = NULL;
fcall_number = 0;
fbname = name;
wanted_variablegeneration = expression_vg;
@@ -330,27 +333,13 @@
symbol_c* type,
symbol_c* value) {
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
- if (vartype == search_var_instance_decl_c::external_vt) {
- symbolic_variable_c *variable = dynamic_cast<symbolic_variable_c *>(symbol);
- /* TODO Find a solution for forcing global complex variables */
- if (variable != NULL) {
- s4o.print(SET_EXTERNAL);
- s4o.print("(");
- variable->var_name->accept(*this);
- s4o.print(",");
- }
- else {
- s4o.print(SET_COMPLEX_EXTERNAL);
- s4o.print("(");
- }
- }
- else {
- if (vartype == search_var_instance_decl_c::located_vt)
- s4o.print(SET_LOCATED);
- else
- s4o.print(SET_VAR);
- s4o.print("(");
- }
+ if (vartype == search_var_instance_decl_c::external_vt)
+ s4o.print(SET_EXTERNAL);
+ else if (vartype == search_var_instance_decl_c::located_vt)
+ s4o.print(SET_LOCATED);
+ else
+ s4o.print(SET_VAR);
+ s4o.print("(,");
wanted_variablegeneration = complextype_base_vg;
symbol->accept(*this);
@@ -469,6 +458,7 @@
}
return NULL;
}
+
/* | il_simple_operator [il_operand] */
//SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
void *visit(il_simple_operation_c *symbol) {
--- a/stage4/generate_c/generate_c_sfc.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c_sfc.cc Fri Nov 18 17:28:37 2011 +0100
@@ -96,8 +96,9 @@
}
}
- void print_step_argument(symbol_c *step_name, const char* argument) {
- print_variable_prefix();
+ void print_step_argument(symbol_c *step_name, const char* argument, bool setter=false) {
+ print_variable_prefix();
+ if (setter) s4o.print(",");
s4o.print("__step_list[");
s4o.print(SFC_STEP_ACTION_PREFIX);
step_name->accept(*this);
@@ -105,8 +106,9 @@
s4o.print(argument);
}
- void print_action_argument(symbol_c *action_name, const char* argument) {
- print_variable_prefix();
+ void print_action_argument(symbol_c *action_name, const char* argument, bool setter=false) {
+ print_variable_prefix();
+ if (setter) s4o.print(",");
s4o.print("__action_list[");
s4o.print(SFC_STEP_ACTION_PREFIX);
action_name->accept(*this);
@@ -122,7 +124,7 @@
s4o.print(s4o.indent_spaces);
s4o.print(SET_VAR);
s4o.print("(");
- print_step_argument(step_name, "state");
+ print_step_argument(step_name, "state", true);
s4o.print(",0);\n");
}
@@ -130,7 +132,7 @@
s4o.print(s4o.indent_spaces);
s4o.print(SET_VAR);
s4o.print("(");
- print_step_argument(step_name, "state");
+ print_step_argument(step_name, "state", true);
s4o.print(",1);\n" + s4o.indent_spaces);
print_step_argument(step_name, "elapsed_time");
s4o.print(" = __time_to_timespec(1, 0, 0, 0, 0, 0);\n");
@@ -283,7 +285,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
- s4o.print("__transition_list[");
+ s4o.print(",__transition_list[");
print_transition_number();
s4o.print("],0);\n");
s4o.indent_left();
@@ -337,6 +339,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
+ s4o.print(",");
if (wanted_sfcgeneration == transitiontestdebug_sg)
s4o.print("__debug_");
else
@@ -345,6 +348,7 @@
print_transition_number();
s4o.print("],");
generate_c_il->print_backup_variable();
+ generate_c_il->reset_default_variable_name();
s4o.print(");\n");
}
// Transition condition is in ST
@@ -353,6 +357,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
+ s4o.print(",");
if (wanted_sfcgeneration == transitiontestdebug_sg)
s4o.print("__debug_");
else
@@ -370,7 +375,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
- s4o.print("__debug_transition_list[");
+ s4o.print(",__debug_transition_list[");
print_transition_number();
s4o.print("],");
s4o.print(GET_VAR);
@@ -842,21 +847,16 @@
unsigned int vartype = search_var_instance_decl->get_vartype();
s4o.print(s4o.indent_spaces);
- if (vartype == search_var_instance_decl_c::external_vt) {
+ if (vartype == search_var_instance_decl_c::external_vt)
s4o.print(SET_EXTERNAL);
- s4o.print("(");
- pt->symbol->accept(*this);
- s4o.print(",");
- }
- else {
- if (vartype == search_var_instance_decl_c::located_vt)
- s4o.print(SET_LOCATED);
- else
- s4o.print(SET_VAR);
- s4o.print("(");
- }
- print_variable_prefix();
- pt->symbol->accept(*this);
+ else if (vartype == search_var_instance_decl_c::located_vt)
+ s4o.print(SET_LOCATED);
+ else
+ s4o.print(SET_VAR);
+ s4o.print("(");
+ print_variable_prefix();
+ s4o.print(",");
+ pt->symbol->accept(*this);
s4o.print(",");
print_variable_prefix();
s4o.print("__action_list[");
--- a/stage4/generate_c/generate_c_sfcdecl.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c_sfcdecl.cc Fri Nov 18 17:28:37 2011 +0100
@@ -250,7 +250,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
- s4o.print("__step_list[");
+ s4o.print(",__step_list[");
s4o.print_integer(step_number);
s4o.print("].state,1);\n");
step_number++;
--- a/stage4/generate_c/generate_c_st.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c_st.cc Fri Nov 18 17:28:37 2011 +0100
@@ -51,6 +51,7 @@
expression_vg,
assignment_vg,
complextype_base_vg,
+ complextype_base_assignment_vg,
complextype_suffix_vg,
fparam_output_vg
} variablegeneration_t;
@@ -171,35 +172,21 @@
unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
bool type_is_complex = search_varfb_instance_type->type_is_complex();
- if (vartype == search_var_instance_decl_c::external_vt) {
- symbolic_variable_c *variable = dynamic_cast<symbolic_variable_c *>(symbol);
- /* TODO Find a solution for forcing global complex variables */
- if (variable != NULL) {
- s4o.print(SET_EXTERNAL);
- s4o.print("(");
- variable->var_name->accept(*this);
- s4o.print(",");
- }
- else {
- s4o.print(SET_COMPLEX_EXTERNAL);
- s4o.print("(");
- }
- }
- else {
- if (vartype == search_var_instance_decl_c::located_vt)
- s4o.print(SET_LOCATED);
- else
- s4o.print(SET_VAR);
- s4o.print("(");
- }
+ if (vartype == search_var_instance_decl_c::external_vt)
+ s4o.print(SET_EXTERNAL);
+ else if (vartype == search_var_instance_decl_c::located_vt)
+ s4o.print(SET_LOCATED);
+ else
+ s4o.print(SET_VAR);
+ s4o.print("(");
if (fb_symbol != NULL) {
print_variable_prefix();
fb_symbol->accept(*this);
- s4o.print(".");
+ s4o.print(".,");
}
else if (type_is_complex)
- wanted_variablegeneration = complextype_base_vg;
+ wanted_variablegeneration = complextype_base_assignment_vg;
else
wanted_variablegeneration = assignment_vg;
@@ -249,8 +236,13 @@
void *visit(symbolic_variable_c *symbol) {
unsigned int vartype;
switch (wanted_variablegeneration) {
+ case complextype_base_assignment_vg:
+ case assignment_vg:
+ this->print_variable_prefix();
+ s4o.print(",");
+ symbol->var_name->accept(*this);
+ break;
case complextype_base_vg:
- case assignment_vg:
generate_c_base_c::visit(symbol);
break;
case complextype_suffix_vg:
@@ -317,6 +309,7 @@
TRACE("structured_variable_c");
switch (wanted_variablegeneration) {
case complextype_base_vg:
+ case complextype_base_assignment_vg:
symbol->record_variable->accept(*this);
break;
case complextype_suffix_vg:
@@ -343,6 +336,7 @@
void *visit(array_variable_c *symbol) {
switch (wanted_variablegeneration) {
case complextype_base_vg:
+ case complextype_base_assignment_vg:
symbol->subscripted_variable->accept(*this);
break;
case complextype_suffix_vg:
--- a/stage4/generate_c/generate_c_vardecl.cc Fri Nov 18 17:21:16 2011 +0100
+++ b/stage4/generate_c/generate_c_vardecl.cc Fri Nov 18 17:28:37 2011 +0100
@@ -161,6 +161,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
+ s4o.print(",");
symbol->elements[i]->accept(*this);
s4o.print(",temp);\n");
}
@@ -553,6 +554,7 @@
s4o.print(SET_VAR);
s4o.print("(");
print_variable_prefix();
+ s4o.print(",");
symbol->elements[i]->accept(*this);
s4o.print(",temp);\n");
}
@@ -924,16 +926,19 @@
s4o.print(" ");
else
s4o.print(",");
+ print_variable_prefix();
}
else if (wanted_varformat == localinit_vf) {
this->current_var_type_symbol->accept(*this);
s4o.print(" ");
+ print_variable_prefix();
}
else if (wanted_varformat == init_vf) {
s4o.print(SET_VAR);
s4o.print("(");
+ print_variable_prefix();
+ s4o.print(",");
}
- print_variable_prefix();
list->elements[i]->accept(*this);
if (wanted_varformat != local_vf) {
if (wanted_varformat == localinit_vf &&