Adding support for derived data types.
Already tested:
- Directly
- Enumerated
- Subrange
- Array
--- a/stage1_2/iec.y Sat Nov 17 10:08:38 2007 +0100
+++ b/stage1_2/iec.y Thu Dec 06 18:01:58 2007 +0100
@@ -2243,6 +2243,7 @@
integer_type_name '(' subrange')'
{$$ = new subrange_specification_c($1, $3, locloc(@$));}
| prev_declared_subrange_type_name
+ {$$ = new subrange_specification_c($1, NULL, locloc(@$));}
;
@@ -2324,7 +2325,7 @@
subrange
{$$ = new array_subrange_list_c(locloc(@$)); $$->add_element($1);}
| array_subrange_list ',' subrange
- {$$ = $1; $$->add_element($1);}
+ {$$ = $1; $$->add_element($3);}
;
--- a/stage4/generate_c/decompose_var_instance_name.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/decompose_var_instance_name.cc Thu Dec 06 18:01:58 2007 +0100
@@ -116,10 +116,10 @@
/* The correct code, is therefore more complex... */
if (next_variable_name == symbol) {
/* NOTE: field_selector is always an identifier_c,
- * so we do not have to recursevily visit it again...
- * return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!!
- */
- return (void *)symbol->field_selector;
+ * so we do not have to recursevily visit it again...
+ * return (void *)symbol->field_selector->accept(*this); -> NOT REQUIRED!!
+ */
+ return (void *)symbol->field_selector;
}
current_recursive_variable_name = symbol;
--- a/stage4/generate_c/generate_c.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/generate_c.cc Thu Dec 06 18:01:58 2007 +0100
@@ -1457,12 +1457,11 @@
/* B 0 - Programming Model */
/***************************/
void *visit(library_c *symbol) {
- generate_location_list_c generate_location_list(&located_variables_s4o);
- symbol->accept(generate_location_list);
-
for(int i = 0; i < symbol->n; i++) {
symbol->elements[i]->accept(*this);
}
+ generate_location_list_c generate_location_list(&located_variables_s4o);
+ symbol->accept(generate_location_list);
return NULL;
}
@@ -1477,6 +1476,15 @@
return NULL;
}
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+ /* TYPE type_declaration_list END_TYPE */
+ void *visit(data_type_declaration_c *symbol) {
+ symbol->accept(generate_c_pous);
+ return NULL;
+ }
+
/**************************************/
/* B.1.5 - Program organization units */
/**************************************/
--- a/stage4/generate_c/generate_c_base.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/generate_c_base.cc Thu Dec 06 18:01:58 2007 +0100
@@ -254,6 +254,12 @@
void *visit(boolean_true_c *symbol) {s4o.print("TRUE"); return NULL;}
void *visit(boolean_false_c *symbol) {s4o.print("FALSE"); return NULL;}
+ void *visit(neg_expression_c *symbol) {
+ s4o.print("-");
+ symbol->exp->accept(*this);
+ return NULL;
+ }
+
/*******************************/
/* B.1.2.2 Character Strings */
/*******************************/
@@ -552,8 +558,6 @@
/* subscripted_variable '[' subscript_list ']' */
SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
-/* subscript_list ',' subscript */
-SYM_LIST(subscript_list_c)
#endif
/* record_variable '.' field_selector */
--- a/stage4/generate_c/generate_c_il.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/generate_c_il.cc Thu Dec 06 18:01:58 2007 +0100
@@ -291,6 +291,9 @@
*/
search_fb_instance_decl_c *search_fb_instance_decl;
+ search_varfb_instance_type_c *search_varfb_instance_type;
+
+ search_base_type_c search_base_type;
public:
generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
@@ -301,6 +304,7 @@
//search_il_operand_type = new search_il_operand_type_c(scope);
search_expression_type = new search_expression_type_c(scope);
search_fb_instance_decl = new search_fb_instance_decl_c(scope);
+ search_varfb_instance_type = new search_varfb_instance_type_c(scope);
current_operand = NULL;
current_operand_type = NULL;
il_default_variable_init_value = NULL;
@@ -311,6 +315,7 @@
delete search_fb_instance_decl;
//delete search_il_operand_type;
delete search_expression_type;
+ delete search_varfb_instance_type;
}
void generate(instruction_list_c *il) {
@@ -813,6 +818,11 @@
if (param_value == NULL)
param_value = function_call_param_iterator.next();
+ symbol_c *param_type = fp_iterator.param_type();
+ if (param_type == NULL) ERROR;
+
+ search_base_type.explore_type(param_type);
+
/* now output the value assignment */
if (param_value != NULL)
if ((param_direction == function_param_iterator_c::direction_in) ||
@@ -821,7 +831,14 @@
s4o.print(".");
param_name->accept(*this);
s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ param_type->accept(*this);
+ s4o.print("(");
+ }
param_value->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
s4o.print(";\n" + s4o.indent_spaces);
}
} /* for(...) */
@@ -852,12 +869,22 @@
if (param_value != NULL)
if ((param_direction == function_param_iterator_c::direction_out) ||
(param_direction == function_param_iterator_c::direction_inout)) {
+ symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
+ search_base_type.explore_type(param_type);
+
s4o.print(";\n"+ s4o.indent_spaces);
param_value->accept(*this);
s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ param_type->accept(*this);
+ s4o.print("(");
+ }
symbol->fb_name->accept(*this);
s4o.print(".");
param_name->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
}
} /* for(...) */
@@ -917,6 +944,8 @@
if (param_value == NULL)
param_value = function_call_param_iterator.next();
+ search_base_type.explore_type(param_type);
+
switch (param_direction) {
case function_param_iterator_c::direction_in:
if (param_value == NULL) {
@@ -929,7 +958,14 @@
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
}
if (param_value == NULL) ERROR;
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ param_type->accept(*this);
+ s4o.print("(");
+ }
param_value->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
break;
case function_param_iterator_c::direction_out:
case function_param_iterator_c::direction_inout:
@@ -1095,15 +1131,41 @@
}
void *visit(ST_operator_c *symbol) {
- XXX_operator(this->current_operand, " = ",&(this->default_variable_name));
+ symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
+ search_base_type.explore_type(operand_type);
+
+ this->current_operand->accept(*this);
+ s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ operand_type->accept(*this);
+ s4o.print("(");
+ }
+ this->default_variable_name.accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
/* the data type resulting from this operation is unchamged. */
return NULL;
}
void *visit(STN_operator_c *symbol) {
- XXX_operator(this->current_operand,
- search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
- &(this->default_variable_name));
+ symbol_c *operand_type = search_varfb_instance_type->get_type(this->current_operand, false);
+ search_base_type.explore_type(operand_type);
+
+ this->current_operand->accept(*this);
+ s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ operand_type->accept(*this);
+ s4o.print("(");
+ }
+ if (search_expression_type->is_bool_type(this->current_operand_type))
+ s4o.print("!");
+ else
+ s4o.print("~");
+ this->default_variable_name.accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
/* the data type resulting from this operation is unchamged. */
return NULL;
}
--- a/stage4/generate_c/generate_c_st.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/generate_c_st.cc Thu Dec 06 18:01:58 2007 +0100
@@ -68,6 +68,10 @@
search_varfb_instance_type_c *search_varfb_instance_type;
+ search_base_type_c search_base_type;
+
+ symbol_c* current_array_type;
+
public:
generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
: generate_c_typedecl_c(s4o_ptr) {
@@ -75,6 +79,7 @@
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_array_type = NULL;
}
virtual ~generate_c_st_c(void) {
@@ -157,6 +162,37 @@
return NULL;
}
+/*************************************/
+/* B.1.4.2 Multi-element Variables */
+/*************************************/
+
+/* subscripted_variable '[' subscript_list ']' */
+//SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
+void *visit(array_variable_c *symbol) {
+ current_array_type = search_varfb_instance_type->get_type(symbol->subscripted_variable, false);
+ symbol->subscripted_variable->accept(*this);
+ if (current_array_type != NULL) {
+ symbol->subscript_list->accept(*this);
+ current_array_type = NULL;
+ }
+ return NULL;
+}
+
+/* subscript_list ',' subscript */
+void *visit(subscript_list_c *symbol) {
+ for (int i = 0; i < symbol->n; i++) {
+ s4o.print("[__");
+ current_array_type->accept(*this);
+ s4o.print("_TRANSIDX");
+ print_integer(i);
+ s4o.print("(");
+ symbol->elements[i]->accept(*this);
+ s4o.print(")]");
+ }
+ return NULL;
+}
+
+
/***************************************/
/* B.3 - Language ST (Structured Text) */
/***************************************/
@@ -510,6 +546,8 @@
symbol_c *param_type = fp_iterator.param_type();
if (param_type == NULL) ERROR;
+ search_base_type.explore_type(param_type);
+
switch (param_direction) {
case function_param_iterator_c::direction_in:
if (param_value == NULL) {
@@ -522,7 +560,14 @@
param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
}
if (param_value == NULL) ERROR;
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ param_type->accept(*this);
+ s4o.print("(");
+ }
param_value->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
break;
case function_param_iterator_c::direction_out:
case function_param_iterator_c::direction_inout:
@@ -560,9 +605,23 @@
/* B 3.2.1 Assignment Statements */
/*********************************/
void *visit(assignment_statement_c *symbol) {
+ symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false);
+ symbol_c *base_type = (symbol_c *)search_base_type.explore_type(left_type);
+
symbol->l_exp->accept(*this);
s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ left_type->accept(*this);
+ s4o.print("(");
+ }
symbol->r_exp->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
+ s4o.print("; // ");
+ left_type->accept(*this);
+ s4o.print(" ");
+ base_type->accept(*this);
return NULL;
}
@@ -601,6 +660,11 @@
if (param_value == NULL)
param_value = function_call_param_iterator.next();
+ symbol_c *param_type = fp_iterator.param_type();
+ if (param_type == NULL) ERROR;
+
+ search_base_type.explore_type(param_type);
+
/* now output the value assignment */
if (param_value != NULL)
if ((param_direction == function_param_iterator_c::direction_in) ||
@@ -610,7 +674,14 @@
s4o.print(".");
param_name->accept(*this);
s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ param_type->accept(*this);
+ s4o.print("(");
+ }
param_value->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
s4o.print(";\n" + s4o.indent_spaces);
}
} /* for(...) */
@@ -642,13 +713,23 @@
if (param_value != NULL)
if ((param_direction == function_param_iterator_c::direction_out) ||
(param_direction == function_param_iterator_c::direction_inout)) {
+ symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
+ search_base_type.explore_type(param_type);
+
s4o.print(";\n"+ s4o.indent_spaces);
param_value->accept(*this);
s4o.print(" = ");
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("__CHECK_");
+ param_type->accept(*this);
+ s4o.print("(");
+ }
print_variable_prefix();
symbol->fb_name->accept(*this);
s4o.print(".");
param_name->accept(*this);
+ if (search_base_type.base_is_subrange())
+ s4o.print(")");
}
} /* for(...) */
--- a/stage4/generate_c/generate_c_typedecl.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/generate_c_typedecl.cc Thu Dec 06 18:01:58 2007 +0100
@@ -48,10 +48,46 @@
class generate_c_typedecl_c: public generate_c_base_c {
+ private:
+ symbol_c* current_type_name;
+ search_base_type_c search_base_type;
+
public:
- generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr) {}
+ generate_c_typedecl_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr) {
+ current_typedefinition = none_td;
+ current_basetypedeclaration = none_bd;
+ }
~generate_c_typedecl_c(void) {}
+ typedef enum {
+ none_td,
+ subrange_td,
+ array_td
+ } typedefinition_t;
+
+ typedefinition_t current_typedefinition;
+
+ typedef enum {
+ none_bd,
+ subrangebasetype_bd,
+ subrangebasetypeexploration_bd,
+ subrangetest_bd,
+ arraybasetype_bd,
+ arraysubrange_bd,
+ arraytranslateindex_bd
+ } basetypedeclaration_t;
+
+ basetypedeclaration_t current_basetypedeclaration;
+
+ int extract_integer(symbol_c *integer) {
+ return atoi(((integer_c *)integer)->value);
+ }
+
+ void print_integer(unsigned int integer) {
+ char str[10];
+ sprintf(str, "%d", integer);
+ s4o.print(str);
+ }
/***************************/
/* B 0 - Programming Model */
@@ -110,17 +146,241 @@
/********************************/
/* B 1.3.3 - Derived data types */
/********************************/
+/* subrange_type_name ':' subrange_spec_init */
+void *visit(subrange_type_declaration_c *symbol) {
+ TRACE("subrange_type_declaration_c");
+ /* add this type declaration to the type symbol table... */
+ type_symtable.insert(symbol->subrange_type_name, symbol->subrange_spec_init);
+
+ s4o.print("typedef ");
+ current_basetypedeclaration = subrangebasetype_bd;
+ symbol->subrange_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+ s4o.print(" ");
+ symbol->subrange_type_name->accept(*this);
+ s4o.print(";\n\n");
+
+ current_basetypedeclaration = subrangebasetypeexploration_bd;
+ symbol->subrange_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+
+ current_type_name = symbol->subrange_type_name;
+
+ current_basetypedeclaration = subrangetest_bd;
+ symbol->subrange_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+
+ return NULL;
+}
+
+/* subrange_specification ASSIGN signed_integer */
void *visit(subrange_spec_init_c *symbol) {
TRACE("subrange_spec_init_c");
- // TODO...
- ERROR;
+ current_typedefinition = subrange_td;
+ symbol->subrange_specification->accept(*this);
+ current_typedefinition = none_td;
+ return NULL;
+}
+
+/* integer_type_name '(' subrange')' */
+void *visit(subrange_specification_c *symbol) {
+ switch (current_basetypedeclaration) {
+ case subrangebasetype_bd:
+ symbol->integer_type_name->accept(*this);
+ break;
+ case subrangebasetypeexploration_bd:
+ search_base_type.explore_type(symbol->integer_type_name);
+ break;
+ case subrangetest_bd:
+ if (symbol->subrange != NULL) {
+ current_type_name->accept(*this);
+ s4o.print(" __CHECK_");
+ current_type_name->accept(*this);
+ s4o.print("(");
+ current_type_name->accept(*this);
+ s4o.print(" value) {\n");
+ s4o.indent_right();
+
+ if (search_base_type.base_is_subrange()) {
+ s4o.print(s4o.indent_spaces + "value = __CHECK_");
+ symbol->integer_type_name->accept(*this);
+ s4o.print("(value);\n");
+ }
+
+ symbol->subrange->accept(*this);
+
+ s4o.indent_left();
+ s4o.print("}\n");
+ }
+ else {
+ s4o.print("#define __CHECK_");
+ current_type_name->accept(*this);
+ s4o.print(" __CHECK_");
+ symbol->integer_type_name->accept(*this);
+ s4o.print("\n");
+ }
+ break;
+ default:
+ break;
+ }
+ return NULL;
+}
+
+/* signed_integer DOTDOT signed_integer */
+void *visit(subrange_c *symbol) {
+ int dimension;
+ switch (current_typedefinition) {
+ case array_td:
+ if (current_basetypedeclaration == arraysubrange_bd) {
+ s4o.print("[");
+ dimension = extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1;
+ print_integer(dimension);
+ s4o.print("]");
+ }
+ else
+ symbol->lower_limit->accept(*this);
+ break;
+ case subrange_td:
+ s4o.print(s4o.indent_spaces + "if (value < ");
+ symbol->lower_limit->accept(*this);
+ s4o.print(")\n");
+ s4o.indent_right();
+ s4o.print(s4o.indent_spaces + "return ");
+ symbol->lower_limit->accept(*this);
+ s4o.print(";\n");
+ s4o.indent_left();
+ s4o.print(s4o.indent_spaces + "else if (value > ");
+ symbol->upper_limit->accept(*this);
+ s4o.print(")\n");
+ s4o.indent_right();
+ s4o.print(s4o.indent_spaces + "return ");
+ symbol->upper_limit->accept(*this);
+ s4o.print(";\n");
+ s4o.indent_left();
+ s4o.print(s4o.indent_spaces + "else\n");
+ s4o.indent_right();
+ s4o.print(s4o.indent_spaces + "return value;\n");
+ s4o.indent_left();
+ default:
+ break;
+ }
+ return NULL;
+}
+
+/* enumerated_type_name ':' enumerated_spec_init */
+void *visit(enumerated_type_declaration_c *symbol) {
+ TRACE("enumerated_type_declaration_c");
+ /* add this type declaration to the type symbol table... */
+ type_symtable.insert(symbol->enumerated_type_name, symbol->enumerated_spec_init);
+
+ s4o.print("typedef enum {\n");
+ s4o.indent_right();
+ symbol->enumerated_spec_init->accept(*this);
+ s4o.indent_left();
+ s4o.print("} ");
+ symbol->enumerated_type_name->accept(*this);
+ s4o.print(";\n");
return NULL;
}
void *visit(enumerated_spec_init_c *symbol) {
TRACE("enumerated_spec_init_c");
- // TODO...
- ERROR;
+ symbol->enumerated_specification->accept(*this);
+ return NULL;
+}
+
+/* helper symbol for enumerated_specification->enumerated_spec_init */
+/* enumerated_value_list ',' enumerated_value */
+void *visit(enumerated_value_list_c *symbol) {
+ print_list(symbol, s4o.indent_spaces, ",\n"+s4o.indent_spaces, "\n");
+ return NULL;
+}
+
+/* enumerated_type_name '#' identifier */
+void *visit(enumerated_value_c *symbol) {
+ symbol->value->accept(*this);
+ return NULL;
+}
+
+/* identifier ':' array_spec_init */
+void *visit(array_type_declaration_c *symbol) {
+ TRACE("array_type_declaration_c");
+ /* add this type declaration to the type symbol table... */
+ type_symtable.insert(symbol->identifier, symbol->array_spec_init);
+
+ s4o.print("typedef ");
+ current_basetypedeclaration = arraybasetype_bd;
+ symbol->array_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+ s4o.print(" ");
+ symbol->identifier->accept(*this);
+ current_basetypedeclaration = arraysubrange_bd;
+ symbol->array_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+ s4o.print(";\n");
+
+ search_base_type.explore_type(symbol->array_spec_init);
+ if (search_base_type.base_is_subrange()) {
+ s4o.print("#define __CHECK_");
+ symbol->identifier->accept(*this);
+ s4o.print(" __CHECK_");
+ current_basetypedeclaration = arraybasetype_bd;
+ symbol->array_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+ s4o.print("\n");
+ }
+
+ current_type_name = symbol->identifier;
+ current_basetypedeclaration = arraytranslateindex_bd;
+ symbol->array_spec_init->accept(*this);
+ current_basetypedeclaration = none_bd;
+ s4o.print("\n");
+
+ return NULL;
+}
+
+/* array_specification [ASSIGN array_initialization} */
+/* array_initialization may be NULL ! */
+void *visit(array_spec_init_c *symbol) {
+ TRACE("array_spec_init_c");
+ current_typedefinition = array_td;
+ symbol->array_specification->accept(*this);
+ current_typedefinition = none_td;
+ return NULL;
+}
+
+/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
+void *visit(array_specification_c *symbol) {
+ switch (current_basetypedeclaration) {
+ case arraybasetype_bd:
+ symbol->non_generic_type_name->accept(*this);
+ break;
+ case arraysubrange_bd:
+ case arraytranslateindex_bd:
+ symbol->array_subrange_list->accept(*this);
+ break;
+ default:
+ break;
+ }
+ return NULL;
+}
+
+/* helper symbol for array_specification */
+/* array_subrange_list ',' subrange */
+void *visit(array_subrange_list_c *symbol) {
+ if (current_basetypedeclaration == arraytranslateindex_bd) {
+ for (int i = 0; i < symbol->n; i++) {
+ s4o.print("#define __");
+ current_type_name->accept(*this);
+ s4o.print("_TRANSIDX");
+ print_integer(i);
+ s4o.print("(index) (index) - ");
+ symbol->elements[i]->accept(*this);
+ s4o.print("\n");
+ }
+ }
+ else
+ print_list(symbol);
return NULL;
}
@@ -135,7 +395,7 @@
/* helper symbol for data_type_declaration */
void *visit(type_declaration_list_c *symbol) {
TRACE("type_declaration_list_c");
- return print_list(symbol);
+ return print_list(symbol, "", "\n");
}
/* simple_type_name ':' simple_spec_init */
--- a/stage4/generate_c/generate_c_vardecl.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/generate_c_vardecl.cc Thu Dec 06 18:01:58 2007 +0100
@@ -43,10 +43,212 @@
-
-
-
-
+class generate_c_array_initialization_c: public generate_c_typedecl_c {
+
+ public:
+ typedef enum {
+ none_am,
+ dimensioncount_am,
+ initializationvalue_am,
+ arrayassignment_am,
+ varlistparse_am
+ } arrayinitialization_mode_t;
+
+ arrayinitialization_mode_t current_mode;
+
+ symbol_c* array_default_value;
+
+ private:
+ int dimension_number, current_dimension, array_size;
+
+ public:
+ generate_c_array_initialization_c(stage4out_c *s4o_ptr): generate_c_typedecl_c(s4o_ptr) {}
+ ~generate_c_array_initialization_c(void) {}
+
+ void init_array(symbol_c *var1_list, symbol_c *array_specification, symbol_c *array_initialization) {
+ int i;
+
+ dimension_number = 0;
+ current_dimension = 0;
+ array_size = 1;
+ array_default_value = NULL;
+
+ current_mode = dimensioncount_am;
+ array_specification->accept(*this);
+
+ current_mode = initializationvalue_am;
+ s4o.print("\n");
+ s4o.print(s4o.indent_spaces + "{\n");
+ s4o.indent_right();
+ s4o.print(s4o.indent_spaces + "int index[");
+ print_integer(dimension_number);
+ s4o.print("];\n");
+ s4o.print(s4o.indent_spaces);
+ array_specification->accept(*this);
+ s4o.print(" temp = ");
+ array_initialization->accept(*this);
+ s4o.print(";\n");
+
+ current_mode = arrayassignment_am;
+ array_specification->accept(*this);
+
+ current_mode = varlistparse_am;
+ var1_list->accept(*this);
+
+ current_mode = arrayassignment_am;
+ for (i = 0; i < dimension_number; i++) {
+ s4o.indent_left();
+ s4o.print(s4o.indent_spaces + "}\n");
+ }
+ s4o.indent_left();
+ s4o.print(s4o.indent_spaces + "}");
+ }
+
+ void *visit(identifier_c *type_name) {
+ symbol_c *type_decl;
+ switch (current_mode) {
+ case dimensioncount_am:
+ case arrayassignment_am:
+ /* look up the type declaration... */
+ type_decl = type_symtable.find_value(type_name);
+ if (type_decl == type_symtable.end_value())
+ /* Type declaration not found!! */
+ ERROR;
+ type_decl->accept(*this);
+ break;
+ default:
+ print_token(type_name);
+ break;
+ }
+ return NULL;
+ }
+
+ void *visit(var1_list_c *symbol) {
+ int i, j;
+
+ for (i = 0; i < symbol->n; i++) {
+ s4o.print(s4o.indent_spaces);
+ print_variable_prefix();
+ symbol->elements[i]->accept(*this);
+ for (j = 0; j < dimension_number; j++) {
+ s4o.print("[index[");
+ print_integer(j);
+ s4o.print("]]");
+ }
+ s4o.print(" = temp");
+ for (j = 0; j < dimension_number; j++) {
+ s4o.print("[index[");
+ print_integer(j);
+ s4o.print("]]");
+ }
+ s4o.print(";\n");
+ }
+ return NULL;
+ }
+
+/********************************/
+/* B 1.3.3 - Derived data types */
+/********************************/
+
+ /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
+ void *visit(array_specification_c *symbol) {
+ switch (current_mode) {
+ case dimensioncount_am:
+ symbol->array_subrange_list->accept(*this);
+ array_default_value = (symbol_c *)symbol->non_generic_type_name->accept(*type_initial_value_c::instance());;
+ break;
+ default:
+ symbol->array_subrange_list->accept(*this);
+ break;
+ }
+ return NULL;
+ }
+
+ /* signed_integer DOTDOT signed_integer */
+ //SYM_REF2(subrange_c, lower_limit, upper_limit)
+ void *visit(subrange_c *symbol) {
+ switch (current_mode) {
+ case dimensioncount_am:
+ dimension_number++;
+ array_size *= extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit) + 1;
+ break;
+ case arrayassignment_am:
+ s4o.print(s4o.indent_spaces + "for (index[");
+ print_integer(current_dimension);
+ s4o.print("] = 0; index[");
+ print_integer(current_dimension);
+ s4o.print("] <= ");
+ print_integer(extract_integer(symbol->upper_limit) - extract_integer(symbol->lower_limit));
+ s4o.print("; index[");
+ print_integer(current_dimension);
+ s4o.print("]++) {\n");
+ s4o.indent_right();
+ current_dimension++;
+ break;
+ default:
+ break;
+ }
+ return NULL;
+ }
+
+ /* helper symbol for array_initialization */
+ /* array_initial_elements_list ',' array_initial_elements */
+ void *visit(array_initial_elements_list_c *symbol) {
+ switch (current_mode) {
+ case initializationvalue_am:
+ int i;
+
+ s4o.print("{");
+ for (i = 0; i < symbol->n; i++) {
+ if (i > 0)
+ s4o.print(", ");
+ symbol->elements[i]->accept(*this);
+ array_size--;
+ }
+ if (array_size > 0) {
+ if (symbol->n > 0)
+ s4o.print(", ");
+ for (i = 0; i < array_size; i++) {
+ if (i > 0)
+ s4o.print(", ");
+ array_default_value->accept(*this);
+ }
+ }
+ s4o.print("}");
+ break;
+ default:
+ break;
+ }
+ return NULL;
+ }
+
+ /* integer '(' [array_initial_element] ')' */
+ /* array_initial_element may be NULL ! */
+ void *visit(array_initial_elements_c *symbol) {
+ int initial_element_number;
+
+ switch (current_mode) {
+ case initializationvalue_am:
+ initial_element_number = extract_integer(symbol->integer);
+
+ for (int i = 0; i < initial_element_number; i++) {
+ if (i > 0)
+ s4o.print(", ");
+ if (symbol->array_initial_element != NULL)
+ symbol->array_initial_element->accept(*this);
+ else if (array_default_value != NULL)
+ array_default_value->accept(*this);
+ }
+ if (initial_element_number > 1)
+ array_size -= initial_element_number - 1;
+ break;
+ default:
+ break;
+ }
+ return NULL;
+ }
+
+};
/***********************************************************************/
/***********************************************************************/
@@ -209,6 +411,8 @@
private:
+ generate_c_array_initialization_c *generate_c_array_initialization;
+
/* variable used to store the types of variables that need to be processed... */
/* Only set in the constructor...! */
/* Will contain a set of values of generate_c_vardecl_c::XXXX_vt */
@@ -353,6 +557,7 @@
public:
generate_c_vardecl_c(stage4out_c *s4o_ptr, varformat_t varformat, unsigned int vartype)
: generate_c_typedecl_c(s4o_ptr) {
+ generate_c_array_initialization = new generate_c_array_initialization_c(s4o_ptr);
wanted_varformat = varformat;
wanted_vartype = vartype;
current_vartype = none_vt;
@@ -362,11 +567,14 @@
nv = NULL;
}
- ~generate_c_vardecl_c(void) {}
+ ~generate_c_vardecl_c(void) {
+ delete generate_c_array_initialization;
+ }
void print(symbol_c *symbol, symbol_c *scope = NULL, const char *variable_prefix = NULL) {
this->set_variable_prefix(variable_prefix);
+ this->generate_c_array_initialization->set_variable_prefix(variable_prefix);
if (globalinit_vf == wanted_varformat)
globalnamespace = scope;
@@ -514,11 +722,33 @@
return NULL;
}
-
-#if 0
-/* var1_list ':' array_spec_init */
-SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
-#endif
+/* var1_list ':' array_spec_init */
+// SYM_REF2(array_var_init_decl_c, var1_list, array_spec_init)
+void *visit(array_var_init_decl_c *symbol) {
+ TRACE("array_var_init_decl_c");
+ /* Please read the comments inside the var1_init_decl_c
+ * visitor, as they apply here too.
+ */
+
+ /* Start off by setting the current_var_type_symbol and
+ * current_var_init_symbol private variables...
+ */
+ update_type_init(symbol->array_spec_init);
+
+ /* now to produce the c equivalent... */
+ if (wanted_varformat == constructorinit_vf)
+ generate_c_array_initialization->init_array(symbol->var1_list, this->current_var_type_symbol, this->current_var_init_symbol);
+ else
+ symbol->var1_list->accept(*this);
+
+ /* Values no longer in scope, and therefore no longer used.
+ * Make an effort to keep them set to NULL when not in use
+ * in order to catch bugs as soon as possible...
+ */
+ void_type_init();
+
+ return NULL;
+}
/* var1_list ':' initialized_structure */
// SYM_REF2(structured_var_init_decl_c, var1_list, initialized_structure)
@@ -623,6 +853,11 @@
SYM_REF2(array_var_declaration_c, var1_list, array_specification)
#endif
+void *visit(array_initial_elements_list_c *symbol) {
+ s4o.print(";// array initialisation");
+ return NULL;
+}
+
/* var1_list ':' structure_type_name */
//SYM_REF2(structured_var_declaration_c, var1_list, structure_type_name)
void *visit(structured_var_declaration_c *symbol) {
--- a/stage4/generate_c/search_base_type.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/search_base_type.cc Thu Dec 06 18:01:58 2007 +0100
@@ -38,9 +38,11 @@
class search_base_type_c: public null_visitor_c {
private:
symbol_c *current_type_name;
+ bool is_subrange;
public:
search_base_type_c(void) {current_type_name = NULL;}
+ bool base_is_subrange() {return this->is_subrange;}
public:
void *visit(identifier_c *type_name) {
@@ -53,6 +55,11 @@
return type_decl->accept(*this);
}
+
+ void *explore_type(symbol_c* type_decl) {
+ this->is_subrange = false;
+ return type_decl->accept(*this);
+ }
/***********************************/
/* B 1.3.1 - Elementary Data Types */
@@ -100,6 +107,7 @@
/* subrange_specification ASSIGN signed_integer */
void *visit(subrange_spec_init_c *symbol) {
+ this->is_subrange = true;
return symbol->subrange_specification->accept(*this);
}
@@ -148,7 +156,7 @@
/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
void *visit(array_specification_c *symbol) {
if (NULL == this->current_type_name) ERROR;
- return (void *)this->current_type_name;
+ return symbol->non_generic_type_name->accept(*this);
}
/* helper symbol for array_specification */
--- a/stage4/generate_c/search_varfb_instance_type.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/search_varfb_instance_type.cc Thu Dec 06 18:01:58 2007 +0100
@@ -51,17 +51,19 @@
search_var_instance_decl_c search_var_instance_decl;
decompose_var_instance_name_c *decompose_var_instance_name;
symbol_c *current_structelement_name;
+ bool search_base_type;
public:
search_varfb_instance_type_c(symbol_c *search_scope): search_var_instance_decl(search_scope) {
this->decompose_var_instance_name = NULL;
this->current_structelement_name = NULL;
- }
-
-
- symbol_c *get_type(symbol_c *variable_name) {
+ this->search_base_type = false;
+ }
+
+ symbol_c *get_type(symbol_c *variable_name, bool base_type = true) {
this->current_structelement_name = NULL;
this->decompose_var_instance_name = new decompose_var_instance_name_c(variable_name);
+ this->search_base_type = base_type;
if (NULL == decompose_var_instance_name) ERROR;
/* find the part of the variable name that will appear in the
@@ -165,12 +167,32 @@
/* No. It is not a function block, so we let
* the base class take care of it...
*/
- return search_base_type_c::visit(type_name);
+ if (this->search_base_type)
+ return search_base_type_c::visit(type_name);
+ else
+ return type_name;
}
/********************************/
/* B 1.3.3 - Derived data types */
/********************************/
+
+/* identifier ':' array_spec_init */
+ void *visit(array_type_declaration_c *symbol) {
+ return symbol->array_spec_init->accept(*this);
+ }
+
+/* array_specification [ASSIGN array_initialization} */
+/* array_initialization may be NULL ! */
+ void *visit(array_spec_init_c *symbol) {
+ return symbol->array_specification->accept(*this);
+ }
+
+/* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
+ void *visit(array_specification_c *symbol) {
+ return symbol->non_generic_type_name->accept(*this);
+ }
+
/* structure_type_name ':' structure_specification */
void *visit(structure_type_declaration_c *symbol) {
return symbol->structure_specification->accept(*this);
--- a/stage4/generate_c/spec_init_separator.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/spec_init_separator.cc Thu Dec 06 18:01:58 2007 +0100
@@ -113,13 +113,35 @@
void *visit(subrange_spec_init_c *symbol) {
TRACE("spec_init_sperator_c::subrange_spec_init_c");
switch (search_what) {
- case search_spec: return symbol->subrange_specification;
+ case search_spec: return symbol->subrange_specification->accept(*this);
case search_init: return symbol->signed_integer;
}
ERROR; /* should never occur */
return NULL;
}
+/* integer_type_name '(' subrange')' */
+void *visit(subrange_specification_c *symbol) {
+ TRACE("spec_init_sperator_c::subrange_specification_c");
+ switch (search_what) {
+ case search_spec: return symbol->integer_type_name;
+ case search_init: return NULL; /* should never occur */
+ }
+ ERROR; /* should never occur */
+ return NULL;
+}
+
+/* array_specification [ASSIGN array_initialization} */
+/* array_initialization may be NULL ! */
+void *visit(array_spec_init_c *symbol) {
+ TRACE("spec_init_sperator_c::array_spec_init_c");
+ switch (search_what) {
+ case search_spec: return symbol->array_specification;
+ case search_init: return symbol->array_initialization;
+ }
+ return NULL;
+}
+
/* enumerated_specification ASSIGN enumerated_value */
void *visit(enumerated_spec_init_c *symbol) {
TRACE("spec_init_sperator_c::enumerated_spec_init_c");
--- a/stage4/generate_c/type_initial_value.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_c/type_initial_value.cc Thu Dec 06 18:01:58 2007 +0100
@@ -192,7 +192,10 @@
/* integer_type_name '(' subrange')' */
void *visit(subrange_specification_c *symbol) {
/* if no initial value explicitly given, then use the lowest value of the subrange */
- return symbol->subrange->accept(*this);
+ if (symbol->subrange != NULL)
+ return symbol->subrange->accept(*this);
+ else
+ return symbol->integer_type_name->accept(*this);
}
/* signed_integer DOTDOT signed_integer */
void *visit(subrange_c *symbol) {return symbol->lower_limit;}
@@ -253,7 +256,7 @@
// NOTE: We are leaking memory, as the array_initial_elements_list will never get free'd!!
array_initial_elements_list_c *array_initial_elements_list = new array_initial_elements_list_c();
array_initial_elements_list->add_element(array_initial_elements);
- return (void *)array_initial_elements_list;
+ return array_initial_elements_list;
}
/* helper symbol for array_specification */
/* array_subrange_list ',' subrange */
--- a/stage4/generate_iec/generate_iec.cc Sat Nov 17 10:08:38 2007 +0100
+++ b/stage4/generate_iec/generate_iec.cc Thu Dec 06 18:01:58 2007 +0100
@@ -356,9 +356,11 @@
/* integer_type_name '(' subrange')' */
void *visit(subrange_specification_c *symbol) {
symbol->integer_type_name->accept(*this);
- s4o.print("(");
- symbol->subrange->accept(*this);
- s4o.print(")");
+ if (symbol->subrange != NULL) {
+ s4o.print("(");
+ symbol->subrange->accept(*this);
+ s4o.print(")");
+ }
return NULL;
}