lbessard@70: /*
Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3
lbessard@70: *
Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
lbessard@70: *
Edouard@279: * This program is free software: you can redistribute it and/or modify
Edouard@279: * it under the terms of the GNU General Public License as published by
Edouard@279: * the Free Software Foundation, either version 3 of the License, or
Edouard@279: * (at your option) any later version.
Edouard@279: *
Edouard@279: * This program is distributed in the hope that it will be useful,
Edouard@279: * but WITHOUT ANY WARRANTY; without even the implied warranty of
Edouard@279: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Edouard@279: * GNU General Public License for more details.
Edouard@279: *
Edouard@279: * You should have received a copy of the GNU General Public License
Edouard@279: * along with this program. If not, see .
lbessard@70: *
lbessard@70: * This code is made available on the understanding that it will not be
lbessard@70: * used in safety-critical situations without a full and competent review.
lbessard@70: *
lbessard@70: * Based on the
lbessard@70: * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
lbessard@70: *
lbessard@70: */
lbessard@70:
lbessard@70:
lbessard@70: /*
lbessard@70: * Conversion of st statements (i.e. ST code).
lbessard@70: *
lbessard@70: * This is part of the 4th stage that generates
Edouard@279: * a C source program equivalent to the IL and ST, or SFC
lbessard@70: * code.
lbessard@70: */
lbessard@70:
lbessard@70:
msousa@350:
msousa@350: #include "../../util/strdup.hh"
msousa@350:
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70:
lbessard@70:
lbessard@70: class generate_c_st_c: public generate_c_typedecl_c {
lbessard@70:
laurent@217: public:
laurent@217: typedef enum {
laurent@217: expression_vg,
laurent@217: assignment_vg,
laurent@226: complextype_base_vg,
laurent@392: complextype_base_assignment_vg,
laurent@226: complextype_suffix_vg,
laurent@217: fparam_output_vg
laurent@217: } variablegeneration_t;
laurent@217:
laurent@347: typedef enum {
laurent@347: single_cg,
laurent@347: subrange_cg,
laurent@347: none_cg
laurent@347: } casegeneration_t;
laurent@347:
lbessard@70: private:
lbessard@70: /* When calling a function block, we must first find it's type,
lbessard@70: * by searching through the declarations of the variables currently
lbessard@70: * in scope.
lbessard@70: * This class does just that...
lbessard@70: * A new class is instantiated whenever we begin generating the code
lbessard@70: * for a function block type declaration, or a program declaration.
lbessard@70: * This object instance will then later be called while the
lbessard@70: * function block's or the program's body is being handled.
lbessard@70: *
lbessard@70: * Note that functions cannot contain calls to function blocks,
lbessard@70: * so we do not create an object instance when handling
lbessard@70: * a function declaration.
lbessard@70: */
lbessard@70: search_fb_instance_decl_c *search_fb_instance_decl;
lbessard@70:
lbessard@70: search_varfb_instance_type_c *search_varfb_instance_type;
msousa@505: search_var_instance_decl_c *search_var_instance_decl;
lbessard@70:
lbessard@98: symbol_c* current_array_type;
laurent@235: symbol_c* current_param_type;
lbessard@98:
laurent@217: int fcall_number;
laurent@217: symbol_c *fbname;
laurent@217:
laurent@347: bool first_subrange_case_list;
laurent@347:
laurent@217: variablegeneration_t wanted_variablegeneration;
laurent@347: casegeneration_t wanted_casegeneration;
lbessard@145:
lbessard@70: public:
laurent@217: generate_c_st_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
lbessard@70: : generate_c_typedecl_c(s4o_ptr) {
msousa@668: search_fb_instance_decl = new search_fb_instance_decl_c (scope);
lbessard@70: search_varfb_instance_type = new search_varfb_instance_type_c(scope);
msousa@668: search_var_instance_decl = new search_var_instance_decl_c (scope);
msousa@505:
lbessard@70: this->set_variable_prefix(variable_prefix);
lbessard@98: current_array_type = NULL;
laurent@235: current_param_type = NULL;
laurent@217: fcall_number = 0;
laurent@217: fbname = name;
laurent@217: wanted_variablegeneration = expression_vg;
laurent@347: wanted_casegeneration = none_cg;
lbessard@70: }
lbessard@70:
lbessard@70: virtual ~generate_c_st_c(void) {
lbessard@70: delete search_fb_instance_decl;
lbessard@70: delete search_varfb_instance_type;
msousa@505: delete search_var_instance_decl;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: public:
lbessard@70: void generate(statement_list_c *stl) {
lbessard@70: stl->accept(*this);
lbessard@70: }
lbessard@70:
lbessard@70: private:
msousa@531:
msousa@531:
msousa@531:
msousa@531:
msousa@531:
lbessard@70:
laurent@226: void *print_getter(symbol_c *symbol) {
msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
laurent@235: if (wanted_variablegeneration == fparam_output_vg) {
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) {
Laurent@706: if (search_var_instance_decl->type_is_fb(symbol))
Laurent@706: s4o.print(GET_EXTERNAL_FB_BY_REF);
Laurent@706: else
Laurent@706: s4o.print(GET_EXTERNAL_BY_REF);
Laurent@706: }
laurent@235: else if (vartype == search_var_instance_decl_c::located_vt)
laurent@235: s4o.print(GET_LOCATED_BY_REF);
laurent@235: else
laurent@235: s4o.print(GET_VAR_BY_REF);
laurent@235: }
laurent@235: else {
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) {
Laurent@706: if (search_var_instance_decl->type_is_fb(symbol))
Laurent@706: s4o.print(GET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(GET_EXTERNAL);
Laurent@706: }
msousa@503: else if (vartype == search_var_instance_decl_c::located_vt)
msousa@503: s4o.print(GET_LOCATED);
msousa@503: else
msousa@503: s4o.print(GET_VAR);
laurent@235: }
laurent@226: s4o.print("(");
laurent@226:
laurent@235: variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
laurent@226: wanted_variablegeneration = complextype_base_vg;
laurent@226: symbol->accept(*this);
msousa@531: if (search_var_instance_decl->type_is_complex(symbol))
laurent@235: s4o.print(",");
laurent@226: wanted_variablegeneration = complextype_suffix_vg;
laurent@226: symbol->accept(*this);
laurent@226: s4o.print(")");
laurent@235: wanted_variablegeneration = old_wanted_variablegeneration;
laurent@226: return NULL;
laurent@226: }
laurent@226:
laurent@226: void *print_setter(symbol_c* symbol,
Laurent@706: symbol_c* type,
Laurent@706: symbol_c* value,
Laurent@706: symbol_c* fb_symbol = NULL,
Laurent@706: symbol_c* fb_value = NULL) {
msousa@302:
laurent@405: bool type_is_complex = false;
laurent@405: if (fb_symbol == NULL) {
msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
msousa@531: type_is_complex = search_var_instance_decl->type_is_complex(symbol);
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) {
Laurent@706: if (search_var_instance_decl->type_is_fb(symbol))
Laurent@706: s4o.print(SET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(SET_EXTERNAL);
Laurent@706: }
laurent@405: else if (vartype == search_var_instance_decl_c::located_vt)
laurent@405: s4o.print(SET_LOCATED);
laurent@405: else
laurent@405: s4o.print(SET_VAR);
laurent@405: }
Laurent@706: else {
Laurent@706: unsigned int vartype = search_var_instance_decl->get_vartype(fb_symbol);
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt)
Laurent@706: s4o.print(SET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(SET_VAR);
Laurent@706: }
laurent@392: s4o.print("(");
laurent@226:
laurent@226: if (fb_symbol != NULL) {
laurent@226: print_variable_prefix();
laurent@226: fb_symbol->accept(*this);
laurent@392: s4o.print(".,");
laurent@226: }
laurent@382: else if (type_is_complex)
laurent@392: wanted_variablegeneration = complextype_base_assignment_vg;
laurent@226: else
laurent@382: wanted_variablegeneration = assignment_vg;
msousa@302:
laurent@226: symbol->accept(*this);
laurent@226: s4o.print(",");
laurent@226: wanted_variablegeneration = expression_vg;
laurent@226: print_check_function(type, value, fb_value);
laurent@382: if (type_is_complex) {
laurent@226: s4o.print(",");
laurent@226: wanted_variablegeneration = complextype_suffix_vg;
laurent@226: symbol->accept(*this);
laurent@226: }
laurent@226: s4o.print(")");
laurent@226: wanted_variablegeneration = expression_vg;
laurent@226: return NULL;
laurent@226: }
lbessard@146:
laurent@347: /********************************/
laurent@347: /* B 1.3.3 - Derived data types */
laurent@347: /********************************/
laurent@377:
laurent@347: /* signed_integer DOTDOT signed_integer */
laurent@347: void *visit(subrange_c *symbol) {
laurent@347: switch (wanted_casegeneration) {
laurent@347: case subrange_cg:
laurent@376: s4o.print("__case_expression >= ");
laurent@347: symbol->lower_limit->accept(*this);
laurent@376: s4o.print(" && __case_expression <= ");
laurent@347: symbol->upper_limit->accept(*this);
laurent@347: break;
laurent@347: default:
laurent@377: symbol->lower_limit->accept(*this);
laurent@347: break;
laurent@347: }
laurent@347: return NULL;
laurent@347: }
laurent@347:
laurent@377: /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
laurent@377: void *visit(array_specification_c *symbol) {
laurent@377: symbol->non_generic_type_name->accept(*this);
laurent@377: return NULL;
laurent@377: }
laurent@377:
Laurent@800: /* enumerated_type_name ':' enumerated_spec_init */
Laurent@800: void *visit(enumerated_type_declaration_c *symbol) {
Laurent@800: symbol->enumerated_type_name->accept(*this);
Laurent@800: return NULL;
Laurent@800: }
Laurent@800:
lbessard@70: /*********************/
lbessard@70: /* B 1.4 - Variables */
lbessard@70: /*********************/
lbessard@70: void *visit(symbolic_variable_c *symbol) {
laurent@382: switch (wanted_variablegeneration) {
laurent@392: case complextype_base_assignment_vg:
laurent@392: case assignment_vg:
laurent@392: this->print_variable_prefix();
laurent@392: s4o.print(",");
laurent@392: symbol->var_name->accept(*this);
laurent@392: break;
laurent@382: case complextype_base_vg:
laurent@382: generate_c_base_c::visit(symbol);
laurent@382: break;
laurent@382: case complextype_suffix_vg:
laurent@382: break;
laurent@382: default:
laurent@382: if (this->is_variable_prefix_null()) {
laurent@382: if (wanted_variablegeneration == fparam_output_vg) {
laurent@382: s4o.print("&(");
laurent@382: generate_c_base_c::visit(symbol);
laurent@382: s4o.print(")");
laurent@382: }
laurent@382: else {
laurent@382: generate_c_base_c::visit(symbol);
laurent@382: }
laurent@382: }
laurent@382: else
laurent@382: print_getter(symbol);
laurent@382: break;
laurent@382: }
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /********************************************/
lbessard@70: /* B.1.4.1 Directly Represented Variables */
lbessard@70: /********************************************/
lbessard@70: // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);};
lbessard@70: void *visit(direct_variable_c *symbol) {
lbessard@70: TRACE("direct_variable_c");
lbessard@70: /* Do not use print_token() as it will change everything into uppercase */
lbessard@70: if (strlen(symbol->value) == 0) ERROR;
laurent@217: if (this->is_variable_prefix_null()) {
laurent@217: if (wanted_variablegeneration != fparam_output_vg)
Laurent@706: s4o.print("*(");
laurent@217: }
laurent@217: else {
laurent@217: switch (wanted_variablegeneration) {
laurent@217: case expression_vg:
Laurent@706: s4o.print(GET_LOCATED);
Laurent@706: s4o.print("(");
Laurent@706: break;
laurent@217: case fparam_output_vg:
laurent@217: s4o.print(GET_LOCATED_BY_REF);
laurent@217: s4o.print("(");
laurent@217: break;
laurent@217: default:
laurent@217: break;
laurent@217: }
lbessard@145: }
lbessard@70: this->print_variable_prefix();
lbessard@70: s4o.printlocation(symbol->value + 1);
laurent@217: if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) ||
Laurent@706: wanted_variablegeneration != assignment_vg)
lbessard@145: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@98: /*************************************/
lbessard@98: /* B.1.4.2 Multi-element Variables */
lbessard@98: /*************************************/
lbessard@98:
laurent@221: // SYM_REF2(structured_variable_c, record_variable, field_selector)
laurent@221: void *visit(structured_variable_c *symbol) {
laurent@221: TRACE("structured_variable_c");
Laurent@625: bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable);
laurent@226: switch (wanted_variablegeneration) {
laurent@226: case complextype_base_vg:
laurent@392: case complextype_base_assignment_vg:
laurent@226: symbol->record_variable->accept(*this);
Laurent@410: if (!type_is_complex) {
Laurent@706: s4o.print(".");
Laurent@706: symbol->field_selector->accept(*this);
Laurent@410: }
laurent@226: break;
laurent@226: case complextype_suffix_vg:
laurent@226: symbol->record_variable->accept(*this);
Laurent@410: if (type_is_complex) {
Laurent@410: s4o.print(".");
Laurent@410: symbol->field_selector->accept(*this);
Laurent@410: }
laurent@226: break;
laurent@382: case assignment_vg:
laurent@226: symbol->record_variable->accept(*this);
laurent@226: s4o.print(".");
laurent@226: symbol->field_selector->accept(*this);
laurent@226: break;
laurent@226: default:
laurent@226: if (this->is_variable_prefix_null()) {
Laurent@706: symbol->record_variable->accept(*this);
Laurent@706: s4o.print(".");
Laurent@706: symbol->field_selector->accept(*this);
laurent@226: }
laurent@226: else
Laurent@706: print_getter(symbol);
laurent@226: break;
laurent@221: }
laurent@221: return NULL;
laurent@221: }
laurent@221:
lbessard@98: /* subscripted_variable '[' subscript_list ']' */
lbessard@98: //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
lbessard@98: void *visit(array_variable_c *symbol) {
laurent@226: switch (wanted_variablegeneration) {
laurent@226: case complextype_base_vg:
laurent@392: case complextype_base_assignment_vg:
laurent@226: symbol->subscripted_variable->accept(*this);
laurent@226: break;
laurent@226: case complextype_suffix_vg:
laurent@238: symbol->subscripted_variable->accept(*this);
laurent@238:
msousa@417: current_array_type = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
laurent@238: if (current_array_type == NULL) ERROR;
laurent@238:
laurent@238: s4o.print(".table");
msousa@298: wanted_variablegeneration = expression_vg;
laurent@238: symbol->subscript_list->accept(*this);
msousa@298: wanted_variablegeneration = complextype_suffix_vg;
laurent@238:
laurent@238: current_array_type = NULL;
laurent@226: break;
laurent@226: default:
laurent@226: if (this->is_variable_prefix_null()) {
Laurent@706: symbol->subscripted_variable->accept(*this);
Laurent@706:
Laurent@706: current_array_type = search_varfb_instance_type->get_basetype_decl(symbol->subscripted_variable);
Laurent@706: if (current_array_type == NULL) ERROR;
Laurent@706:
Laurent@706: s4o.print(".table");
laurent@238: symbol->subscript_list->accept(*this);
laurent@238:
laurent@238: current_array_type = NULL;
laurent@226: }
laurent@226: else
Laurent@706: print_getter(symbol);
laurent@226: break;
lbessard@98: }
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
lbessard@98: /* subscript_list ',' subscript */
lbessard@98: void *visit(subscript_list_c *symbol) {
laurent@377: array_dimension_iterator_c* array_dimension_iterator = new array_dimension_iterator_c(current_array_type);
lbessard@98: for (int i = 0; i < symbol->n; i++) {
laurent@377: symbol_c* dimension = array_dimension_iterator->next();
Laurent@706: if (dimension == NULL) ERROR;
Laurent@706:
Laurent@706: s4o.print("[(");
lbessard@98: symbol->elements[i]->accept(*this);
laurent@377: s4o.print(") - (");
laurent@377: dimension->accept(*this);
lbessard@98: s4o.print(")]");
lbessard@98: }
laurent@377: delete array_dimension_iterator;
lbessard@98: return NULL;
lbessard@98: }
lbessard@98:
laurent@235: /******************************************/
laurent@235: /* B 1.4.3 - Declaration & Initialisation */
laurent@235: /******************************************/
laurent@237:
laurent@237: /* helper symbol for structure_initialization */
laurent@237: /* structure_element_initialization_list ',' structure_element_initialization */
laurent@235: void *visit(structure_element_initialization_list_c *symbol) {
laurent@235: generate_c_structure_initialization_c *structure_initialization = new generate_c_structure_initialization_c(&s4o);
laurent@235: structure_initialization->init_structure_default(this->current_param_type);
laurent@237: structure_initialization->init_structure_values(symbol);
laurent@235: delete structure_initialization;
laurent@235: return NULL;
laurent@235: }
lbessard@98:
laurent@237: /* helper symbol for array_initialization */
laurent@237: /* array_initial_elements_list ',' array_initial_elements */
laurent@237: void *visit(array_initial_elements_list_c *symbol) {
laurent@237: generate_c_array_initialization_c *array_initialization = new generate_c_array_initialization_c(&s4o);
laurent@237: array_initialization->init_array_size(this->current_param_type);
laurent@237: array_initialization->init_array_values(symbol);
laurent@237: delete array_initialization;
laurent@237: return NULL;
laurent@237: }
laurent@237:
lbessard@70: /***************************************/
lbessard@70: /* B.3 - Language ST (Structured Text) */
lbessard@70: /***************************************/
lbessard@70: /***********************/
lbessard@70: /* B 3.1 - Expressions */
lbessard@70: /***********************/
lbessard@70: void *visit(or_expression_c *symbol) {
msousa@668: if (get_datatype_info_c::is_BOOL_compatible(symbol->datatype))
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " || ");
msousa@668: if (get_datatype_info_c::is_ANY_nBIT_compatible(symbol->datatype))
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " | ");
lbessard@70: ERROR;
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(xor_expression_c *symbol) {
msousa@668: if (get_datatype_info_c::is_BOOL_compatible(symbol->datatype)) {
lbessard@70: s4o.print("(");
lbessard@70: symbol->l_exp->accept(*this);
lbessard@70: s4o.print(" && !");
lbessard@70: symbol->r_exp->accept(*this);
lbessard@70: s4o.print(") || (!");
lbessard@70: symbol->l_exp->accept(*this);
lbessard@70: s4o.print(" && ");
lbessard@70: symbol->r_exp->accept(*this);
lbessard@70: s4o.print(")");
etisserant@130: return NULL;
lbessard@70: }
msousa@668: if (get_datatype_info_c::is_ANY_nBIT_compatible(symbol->datatype))
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ ");
lbessard@70: ERROR;
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(and_expression_c *symbol) {
msousa@668: if (get_datatype_info_c::is_BOOL_compatible(symbol->datatype))
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");
msousa@668: if (get_datatype_info_c::is_ANY_nBIT_compatible(symbol->datatype))
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " & ");
lbessard@70: ERROR;
msousa@668: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(equ_expression_c *symbol) {
conti@694: if (get_datatype_info_c::is_TIME_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_DATE_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
conti@694: return print_compare_function("EQ_", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(notequ_expression_c *symbol) {
conti@694: if (get_datatype_info_c::is_TIME_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_DATE_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
conti@694: return print_compare_function("NE_", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
lbessard@70: return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(lt_expression_c *symbol) {
conti@694: if (get_datatype_info_c::is_TIME_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_DATE_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
conti@694: return print_compare_function("LT_", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
msousa@653: return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(gt_expression_c *symbol) {
conti@694: if (get_datatype_info_c::is_TIME_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_DATE_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
conti@694: return print_compare_function("GT_", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
msousa@653: return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(le_expression_c *symbol) {
conti@694: if (get_datatype_info_c::is_TIME_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_DATE_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
conti@694: return print_compare_function("LE_", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
msousa@653: return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(ge_expression_c *symbol) {
conti@694: if (get_datatype_info_c::is_TIME_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_DATE_compatible (symbol->l_exp->datatype) ||
conti@694: get_datatype_info_c::is_ANY_STRING_compatible(symbol->l_exp->datatype))
conti@694: return print_compare_function("GE_", symbol->l_exp->datatype, symbol->l_exp, symbol->r_exp);
msousa@653: return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(add_expression_c *symbol) {
msousa@668: /*
msousa@668: symbol_c *left_type = symbol->l_exp->datatype;
msousa@668: symbol_c *right_type = symbol->r_exp->datatype;
Laurent@706: if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
msousa@668: (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
msousa@668: (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)))
msousa@350: return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
msousa@668: */
msousa@668: if (get_datatype_info_c::is_TIME_compatible (symbol->datatype) ||
msousa@668: get_datatype_info_c::is_ANY_DATE_compatible (symbol->datatype))
msousa@668: return print_binary_function("__time_add", symbol->l_exp, symbol->r_exp);
msousa@668: return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(sub_expression_c *symbol) {
msousa@668: /*
msousa@668: symbol_c *left_type = symbol->l_exp->datatype;
msousa@668: symbol_c *right_type = symbol->r_exp->datatype;
lbessard@70: if ((typeid(*left_type) == typeid(time_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
lbessard@70: (typeid(*left_type) == typeid(date_type_name_c) && typeid(*right_type) == typeid(date_type_name_c)) ||
msousa@668: (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
msousa@668: (typeid(*left_type) == typeid(tod_type_name_c) && typeid(*right_type) == typeid(tod_type_name_c)) ||
msousa@668: (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(time_type_name_c)) ||
msousa@668: (typeid(*left_type) == typeid(dt_type_name_c) && typeid(*right_type) == typeid(dt_type_name_c)))
msousa@350: return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp);
msousa@668: */
msousa@668: if (get_datatype_info_c::is_TIME_compatible (symbol->datatype) ||
msousa@668: get_datatype_info_c::is_ANY_DATE_compatible (symbol->datatype))
msousa@668: return print_binary_function("__time_sub", symbol->l_exp, symbol->r_exp);
msousa@668: return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(mul_expression_c *symbol) {
msousa@668: /*
msousa@668: symbol_c *left_type = symbol->l_exp->datatype;
msousa@668: symbol_c *right_type = symbol->r_exp->datatype;
msousa@668: if ((typeid(*left_type) == typeid(time_type_name_c) && get_datatype_info_c::is_ANY_INT_compatible (right_type)) ||
msousa@668: (typeid(*left_type) == typeid(time_type_name_c) && get_datatype_info_c::is_ANY_REAL_compatible(right_type)))
msousa@350: return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp);
msousa@668: */
msousa@668: if (get_datatype_info_c::is_TIME_compatible (symbol->datatype))
msousa@668: return print_binary_function("__time_mul", symbol->l_exp, symbol->r_exp);
msousa@668: return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(div_expression_c *symbol) {
msousa@668: /*
msousa@668: symbol_c *left_type = symbol->l_exp->datatype;
msousa@668: symbol_c *right_type = symbol->r_exp->datatype;
msousa@668: if ((typeid(*left_type) == typeid(time_type_name_c) && get_datatype_info_c::is_ANY_INT_compatible (right_type)) ||
msousa@668: (typeid(*left_type) == typeid(time_type_name_c) && get_datatype_info_c::is_ANY_REAL_compatible(right_type)))
msousa@350: return print_binary_function("__time_div", symbol->l_exp, symbol->r_exp);
msousa@668: */
msousa@668: if (get_datatype_info_c::is_TIME_compatible (symbol->datatype))
msousa@668: return print_binary_function("__time_div", symbol->l_exp, symbol->r_exp);
msousa@668: return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(mod_expression_c *symbol) {
msousa@668: s4o.print("((");
msousa@668: symbol->r_exp->accept(*this);
msousa@668: s4o.print(" == 0)?0:");
msousa@668: print_binary_expression(symbol->l_exp, symbol->r_exp, " % ");
msousa@668: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
msousa@257: void *visit(power_expression_c *symbol) {
msousa@668: s4o.print("EXPT__LREAL__LREAL__LREAL((BOOL)__BOOL_LITERAL(TRUE),\n");
msousa@668: s4o.indent_right();
msousa@668: s4o.print(s4o.indent_spaces + "NULL,\n");
msousa@668: s4o.print(s4o.indent_spaces + "(LREAL)(");
msousa@668: symbol->l_exp->accept(*this);
msousa@668: s4o.print("),\n");
msousa@668: s4o.print(s4o.indent_spaces + "(LREAL)(");
msousa@668: symbol->r_exp->accept(*this);
msousa@668: s4o.print("))");
msousa@358: return NULL;
msousa@257: }
msousa@257:
lbessard@123: void *visit(neg_expression_c *symbol) {
msousa@257: return print_unary_expression(symbol->exp, " -");
lbessard@123: }
lbessard@70:
lbessard@70: void *visit(not_expression_c *symbol) {
msousa@668: return print_unary_expression(symbol->exp, get_datatype_info_c::is_BOOL_compatible(symbol->datatype)?"!":"~");
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(function_invocation_c *symbol) {
lbessard@149: symbol_c* function_name = NULL;
laurent@217: DECLARE_PARAM_LIST()
ccb@202:
ccb@202: symbol_c *parameter_assignment_list = NULL;
ccb@202: if (NULL != symbol-> formal_param_list) parameter_assignment_list = symbol-> formal_param_list;
ccb@202: if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list;
ccb@202: if (NULL == parameter_assignment_list) ERROR;
ccb@202:
laurent@233: function_call_param_iterator_c function_call_param_iterator(symbol);
laurent@233:
msousa@350: function_declaration_c *f_decl = (function_declaration_c *)symbol->called_function_declaration;
msousa@350: if (f_decl == NULL) ERROR;
msousa@350:
msousa@350: function_name = symbol->function_name;
msousa@350:
msousa@350: /* loop through each function parameter, find the value we should pass
msousa@350: * to it, and then output the c equivalent...
msousa@350: */
msousa@350: function_param_iterator_c fp_iterator(f_decl);
msousa@350: identifier_c *param_name;
msousa@350: /* flag to cirreclty handle calls to extensible standard functions (i.e. functions with variable number of input parameters) */
msousa@350: bool found_first_extensible_parameter = false;
msousa@350: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
msousa@350: if (fp_iterator.is_extensible_param() && (!found_first_extensible_parameter)) {
msousa@350: /* We are calling an extensible function. Before passing the extensible
msousa@350: * parameters, we must add a dummy paramater value to tell the called
msousa@350: * function how many extensible parameters we will be passing.
msousa@350: *
msousa@350: * Note that stage 3 has already determined the number of extensible
msousa@350: * paramters, and stored that info in the abstract syntax tree. We simply
msousa@350: * re-use that value.
msousa@350: */
msousa@350: /* NOTE: we are not freeing the malloc'd memory. This is not really a bug.
msousa@350: * Since we are writing a compiler, which runs to termination quickly,
msousa@350: * we can consider this as just memory required for the compilation process
msousa@350: * that will be free'd when the program terminates.
msousa@350: */
msousa@350: char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
msousa@350: if (tmp == NULL) ERROR;
msousa@350: int res = snprintf(tmp, 32, "%d", symbol->extensible_param_count);
msousa@350: if ((res >= 32) || (res < 0)) ERROR;
msousa@350: identifier_c *param_value = new identifier_c(tmp);
msousa@350: uint_type_name_c *param_type = new uint_type_name_c();
msousa@350: identifier_c *param_name = new identifier_c("");
msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, function_param_iterator_c::direction_in)
msousa@350: found_first_extensible_parameter = true;
msousa@350: }
msousa@350:
msousa@350: if (fp_iterator.is_extensible_param()) {
msousa@350: /* since we are handling an extensible parameter, we must add the index to the
msousa@350: * parameter name so we can go looking for the value passed to the correct
msousa@350: * extended parameter (e.g. IN1, IN2, IN3, IN4, ...)
msousa@350: */
msousa@350: char *tmp = (char *)malloc(32); /* enough space for a call with 10^31 (larger than 2^64) input parameters! */
msousa@350: int res = snprintf(tmp, 32, "%d", fp_iterator.extensible_param_index());
msousa@350: if ((res >= 32) || (res < 0)) ERROR;
msousa@350: param_name = new identifier_c(strdup2(param_name->value, tmp));
msousa@350: if (param_name->value == NULL) ERROR;
msousa@350: }
msousa@350:
msousa@350: symbol_c *param_type = fp_iterator.param_type();
msousa@350: if (param_type == NULL) ERROR;
msousa@350:
msousa@350: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
msousa@350:
msousa@350: symbol_c *param_value = NULL;
msousa@350:
msousa@350: /* Get the value from a foo( = ) style call */
msousa@350: if (param_value == NULL)
msousa@350: param_value = function_call_param_iterator.search_f(param_name);
msousa@350:
msousa@350: /* Get the value from a foo() style call */
msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit()) {
msousa@350: param_value = function_call_param_iterator.next_nf();
msousa@350: }
msousa@350:
msousa@350: /* if no more parameter values in function call, and the current parameter
msousa@350: * of the function declaration is an extensible parameter, we
msousa@350: * have reached the end, and should simply jump out of the for loop.
lbessard@70: */
msousa@350: if ((param_value == NULL) && (fp_iterator.is_extensible_param())) {
msousa@350: break;
msousa@350: }
msousa@350:
msousa@350: if ((param_value == NULL) && (param_direction == function_param_iterator_c::direction_in)) {
msousa@350: /* No value given for parameter, so we must use the default... */
msousa@350: /* First check whether default value specified in function declaration...*/
msousa@350: param_value = fp_iterator.default_value();
msousa@350: }
msousa@350:
msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, param_direction)
msousa@350: } /* for(...) */
msousa@350: // symbol->parameter_assignment->accept(*this);
lbessard@149:
laurent@233: if (function_call_param_iterator.next_nf() != NULL) ERROR;
laurent@233:
laurent@217: bool has_output_params = false;
laurent@217:
laurent@217: if (!this->is_variable_prefix_null()) {
laurent@217: PARAM_LIST_ITERATOR() {
msousa@350: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
msousa@350: PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
msousa@350: PARAM_VALUE != NULL) {
msousa@350: has_output_params = true;
msousa@350: }
msousa@350: }
msousa@350: }
msousa@350:
msousa@350: /* Check whether we are calling an overloaded function! */
msousa@721: /* (fdecl_mutiplicity > 1) => calling overloaded function */
msousa@721: int fdecl_mutiplicity = function_symtable.count(symbol->function_name);
msousa@350: if (fdecl_mutiplicity == 0) ERROR;
laurent@217:
laurent@217: if (has_output_params) {
msousa@350: fcall_number++;
msousa@350: s4o.print("__");
laurent@217: fbname->accept(*this);
laurent@217: s4o.print("_");
lbessard@149: function_name->accept(*this);
msousa@721: if (fdecl_mutiplicity > 1) {
msousa@350: /* function being called is overloaded! */
msousa@350: s4o.print("__");
laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o);
msousa@350: f_decl->accept(overloaded_func_suf);
msousa@350: }
msousa@594: s4o.print(fcall_number);
laurent@217: }
laurent@217: else {
laurent@217: function_name->accept(*this);
msousa@721: if (fdecl_mutiplicity > 1) {
msousa@350: /* function being called is overloaded! */
msousa@350: s4o.print("__");
laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o);
msousa@350: f_decl->accept(overloaded_func_suf);
msousa@350: }
laurent@217: }
lbessard@149: s4o.print("(");
lbessard@149: s4o.indent_right();
lbessard@149:
laurent@217: int nb_param = 0;
laurent@217: PARAM_LIST_ITERATOR() {
laurent@217: symbol_c *param_value = PARAM_VALUE;
laurent@235: current_param_type = PARAM_TYPE;
lbessard@149:
laurent@217: switch (PARAM_DIRECTION) {
lbessard@149: case function_param_iterator_c::direction_in:
Laurent@706: if (nb_param > 0)
Laurent@706: s4o.print(",\n"+s4o.indent_spaces);
Laurent@706: if (param_value == NULL) {
lbessard@149: /* If not, get the default value of this variable's type */
msousa@762: param_value = type_initial_value_c::get(current_param_type);
lbessard@149: }
lbessard@149: if (param_value == NULL) ERROR;
laurent@216: s4o.print("(");
msousa@668: if (get_datatype_info_c::is_ANY_INT_literal(current_param_type))
msousa@693: get_datatype_info_c::lint_type_name.accept(*this);
msousa@668: else if (get_datatype_info_c::is_ANY_REAL_literal(current_param_type))
msousa@693: get_datatype_info_c::lreal_type_name.accept(*this);
laurent@216: else
laurent@235: current_param_type->accept(*this);
laurent@216: s4o.print(")");
laurent@235: print_check_function(current_param_type, param_value);
laurent@217: nb_param++;
lbessard@149: break;
lbessard@149: case function_param_iterator_c::direction_out:
lbessard@149: case function_param_iterator_c::direction_inout:
Laurent@706: if (!has_output_params) {
laurent@217: if (nb_param > 0)
Laurent@706: s4o.print(",\n"+s4o.indent_spaces);
Laurent@706: if (param_value == NULL)
laurent@217: s4o.print("NULL");
laurent@217: else {
laurent@217: wanted_variablegeneration = fparam_output_vg;
laurent@217: param_value->accept(*this);
laurent@217: wanted_variablegeneration = expression_vg;
laurent@217: }
Laurent@706: nb_param++;
Laurent@706: }
lbessard@149: break;
lbessard@149: case function_param_iterator_c::direction_extref:
lbessard@149: /* TODO! */
lbessard@149: ERROR;
lbessard@149: break;
lbessard@149: } /* switch */
laurent@217: }
laurent@217: if (has_output_params) {
laurent@217: if (nb_param > 0)
laurent@217: s4o.print(",\n"+s4o.indent_spaces);
laurent@217: s4o.print(FB_FUNCTION_PARAM);
laurent@217: }
lbessard@149: s4o.print(")");
lbessard@149: s4o.indent_left();
lbessard@70:
laurent@217: CLEAR_PARAM_LIST()
laurent@217:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /********************/
lbessard@70: /* B 3.2 Statements */
lbessard@70: /********************/
lbessard@70: void *visit(statement_list_c *symbol) {
lbessard@70: return print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
lbessard@70: }
lbessard@70:
lbessard@70: /*********************************/
lbessard@70: /* B 3.2.1 Assignment Statements */
lbessard@70: /*********************************/
lbessard@70: void *visit(assignment_statement_c *symbol) {
msousa@321: symbol_c *left_type = search_varfb_instance_type->get_type_id(symbol->l_exp);
lbessard@98:
laurent@226: if (this->is_variable_prefix_null()) {
laurent@217: symbol->l_exp->accept(*this);
laurent@226: s4o.print(" = ");
laurent@226: print_check_function(left_type, symbol->r_exp);
laurent@217: }
laurent@217: else {
Laurent@706: print_setter(symbol->l_exp, left_type, symbol->r_exp);
laurent@226: }
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /*****************************************/
lbessard@70: /* B 3.2.2 Subprogram Control Statements */
lbessard@70: /*****************************************/
lbessard@70:
msousa@283: void *visit(return_statement_c *symbol) {
msousa@283: s4o.print("goto "); s4o.print(END_LABEL);
msousa@283: return NULL;
msousa@283: }
msousa@283:
msousa@283:
lbessard@70: /* fb_name '(' [param_assignment_list] ')' */
lbessard@70: /* param_assignment_list -> may be NULL ! */
lbessard@70: //SYM_REF2(fb_invocation_c, fb_name, param_assignment_list)
lbessard@70: void *visit(fb_invocation_c *symbol) {
lbessard@70: TRACE("fb_invocation_c");
lbessard@70: /* first figure out what is the name of the function block type of the function block being called... */
lbessard@70: symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name);
lbessard@70: /* should never occur. The function block instance MUST have been declared... */
lbessard@70: if (function_block_type_name == NULL) ERROR;
lbessard@70:
lbessard@70: /* Now find the declaration of the function block type being called... */
lbessard@70: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name);
lbessard@70: /* should never occur. The function block type being called MUST be in the symtable... */
lbessard@70: if (fb_decl == function_block_type_symtable.end_value()) ERROR;
lbessard@70:
lbessard@70: /* loop through each function block parameter, find the value we should pass
lbessard@70: * to it, and then output the c equivalent...
lbessard@70: */
lbessard@70: function_param_iterator_c fp_iterator(fb_decl);
lbessard@70: identifier_c *param_name;
lbessard@70: function_call_param_iterator_c function_call_param_iterator(symbol);
lbessard@70: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
lbessard@160:
etisserant@162: /*fprintf(stderr, "param : %s\n", param_name->value);*/
lbessard@160:
lbessard@70: /* Get the value from a foo( = ) style call */
ccb@202: symbol_c *param_value = function_call_param_iterator.search_f(param_name);
lbessard@70:
lbessard@70: /* Get the value from a foo() style call */
msousa@350: /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these
msousa@350: * were implicitly defined!
msousa@350: */
msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit())
ccb@202: param_value = function_call_param_iterator.next_nf();
lbessard@70:
lbessard@98: symbol_c *param_type = fp_iterator.param_type();
lbessard@98: if (param_type == NULL) ERROR;
lbessard@98:
lbessard@70: /* now output the value assignment */
lbessard@70: if (param_value != NULL)
lbessard@70: if ((param_direction == function_param_iterator_c::direction_in) ||
lbessard@70: (param_direction == function_param_iterator_c::direction_inout)) {
laurent@226: if (this->is_variable_prefix_null()) {
laurent@226: symbol->fb_name->accept(*this);
laurent@226: s4o.print(".");
laurent@226: param_name->accept(*this);
laurent@226: s4o.print(" = ");
laurent@226: print_check_function(param_type, param_value);
laurent@217: }
laurent@226: else {
berem@228: print_setter(param_name, param_type, param_value, symbol->fb_name);
laurent@226: }
lbessard@70: s4o.print(";\n" + s4o.indent_spaces);
lbessard@70: }
lbessard@70: } /* for(...) */
lbessard@70:
lbessard@70: /* now call the function... */
lbessard@70: function_block_type_name->accept(*this);
lbessard@70: s4o.print(FB_FUNCTION_SUFFIX);
Laurent@706: s4o.print("(");
Laurent@706: if (search_var_instance_decl->get_vartype(symbol->fb_name) != search_var_instance_decl_c::external_vt)
Laurent@706: s4o.print("&");
lbessard@70: print_variable_prefix();
lbessard@70: symbol->fb_name->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70:
lbessard@70: /* loop through each function parameter, find the variable to which
lbessard@70: * we should atribute the value of all output or inoutput parameters.
lbessard@70: */
lbessard@70: fp_iterator.reset();
lbessard@70: function_call_param_iterator.reset();
lbessard@70: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
lbessard@70:
lbessard@70: /* Get the value from a foo( = ) style call */
ccb@202: symbol_c *param_value = function_call_param_iterator.search_f(param_name);
lbessard@70:
lbessard@70: /* Get the value from a foo() style call */
msousa@350: /* When using the informal invocation style, user can not pass values to EN or ENO parameters if these
msousa@350: * were implicitly defined!
msousa@350: */
msousa@350: if ((param_value == NULL) && !fp_iterator.is_en_eno_param_implicit())
ccb@202: param_value = function_call_param_iterator.next_nf();
lbessard@70:
lbessard@70: /* now output the value assignment */
lbessard@70: if (param_value != NULL)
lbessard@70: if ((param_direction == function_param_iterator_c::direction_out) ||
lbessard@70: (param_direction == function_param_iterator_c::direction_inout)) {
msousa@321: symbol_c *param_type = search_varfb_instance_type->get_type_id(param_value);
laurent@240: s4o.print(";\n" + s4o.indent_spaces);
laurent@226: if (this->is_variable_prefix_null()) {
laurent@217: param_value->accept(*this);
laurent@226: s4o.print(" = ");
laurent@226: print_check_function(param_type, param_name, symbol->fb_name);
lbessard@98: }
laurent@217: else {
laurent@226: print_setter(param_value, param_type, param_name, NULL, symbol->fb_name);
laurent@217: }
lbessard@70: }
lbessard@70: } /* for(...) */
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: /* helper symbol for fb_invocation */
lbessard@70: /* param_assignment_list ',' param_assignment */
lbessard@70: void *visit(param_assignment_list_c *symbol) {
lbessard@70: TRACE("param_assignment_list_c");
lbessard@70: /* this should never be called... */
lbessard@70: ERROR;
lbessard@70: return NULL;
lbessard@70: // return print_list(symbol, "", ", ");
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: void *visit(input_variable_param_assignment_c *symbol) {
lbessard@70: TRACE("input_variable_param_assignment_c");
lbessard@70: /* this should never be called... */
lbessard@70: ERROR;
lbessard@70: return NULL;
lbessard@70: /*
lbessard@70: symbol->variable_name->accept(*this);
lbessard@70: s4o.print(" = ");
lbessard@70: symbol->expression->accept(*this);
lbessard@70: return NULL;
lbessard@70: */
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(output_variable_param_assignment_c *symbol) {
lbessard@70: TRACE("output_variable_param_assignment_c");
lbessard@70: /* this should never be called... */
lbessard@70: ERROR;
lbessard@70: return NULL;
lbessard@70: /*
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: if (symbol->not_param != NULL)
lbessard@70: symbol->not_param->accept(*this);
lbessard@70: symbol->variable_name->accept(*this);
lbessard@70: s4o.print(" => ");
lbessard@70: symbol->variable->accept(*this);
lbessard@70: return NULL;
lbessard@70: */
lbessard@70: }
lbessard@70:
lbessard@70: // TODO: the NOT symbol in function (block) calls...
lbessard@70: void *visit(not_paramassign_c *symbol) {
lbessard@70: TRACE("not_paramassign_c");
lbessard@70: /* this should never be called... */
lbessard@70: ERROR;
lbessard@70: return NULL;
lbessard@70: /*
lbessard@70: s4o.print("NOT ");
lbessard@70: return NULL;
lbessard@70: */
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 3.2.3 Selection Statements */
lbessard@70: /********************************/
lbessard@70: void *visit(if_statement_c *symbol) {
lbessard@70: s4o.print("if (");
lbessard@70: symbol->expression->accept(*this);
lbessard@70: s4o.print(") {\n");
lbessard@70: s4o.indent_right();
lbessard@70: symbol->statement_list->accept(*this);
lbessard@70: s4o.indent_left();
lbessard@70: symbol->elseif_statement_list->accept(*this);
lbessard@70:
lbessard@70: if (symbol->else_statement_list != NULL) {
lbessard@70: s4o.print(s4o.indent_spaces); s4o.print("} else {\n");
lbessard@70: s4o.indent_right();
lbessard@70: symbol->else_statement_list->accept(*this);
lbessard@70: s4o.indent_left();
lbessard@70: }
lbessard@70: s4o.print(s4o.indent_spaces); s4o.print("}");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* helper symbol for if_statement */
lbessard@70: void *visit(elseif_statement_list_c *symbol) {return print_list(symbol);}
lbessard@70:
lbessard@70: /* helper symbol for elseif_statement_list */
lbessard@70: void *visit(elseif_statement_c *symbol) {
lbessard@70: s4o.print(s4o.indent_spaces); s4o.print("} else if (");
lbessard@70: symbol->expression->accept(*this);
lbessard@70: s4o.print(") {\n");
lbessard@70: s4o.indent_right();
lbessard@70: symbol->statement_list->accept(*this);
lbessard@70: s4o.indent_left();
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(case_statement_c *symbol) {
msousa@668: symbol_c *expression_type = symbol->expression->datatype;
laurent@347: s4o.print("{\n");
laurent@347: s4o.indent_right();
laurent@376: s4o.print(s4o.indent_spaces);
msousa@668: if (get_datatype_info_c::is_ANY_INT_literal(expression_type))
msousa@693: get_datatype_info_c::lint_type_name.accept(*this);
msousa@668: else if (get_datatype_info_c::is_ANY_REAL_literal(expression_type))
msousa@693: get_datatype_info_c::lreal_type_name.accept(*this);
laurent@376: else
laurent@376: expression_type->accept(*this);
laurent@376: s4o.print(" __case_expression = ");
lbessard@70: symbol->expression->accept(*this);
laurent@376: s4o.print(";\n" + s4o.indent_spaces + "switch (__case_expression) {\n");
lbessard@70: s4o.indent_right();
laurent@347: wanted_casegeneration = single_cg;
laurent@347: symbol->case_element_list->accept(*this);
laurent@347: wanted_casegeneration = subrange_cg;
laurent@347: s4o.print(s4o.indent_spaces + "default:\n");
laurent@347: s4o.indent_right();
laurent@347: first_subrange_case_list = true;
lbessard@70: symbol->case_element_list->accept(*this);
lbessard@70: if (symbol->statement_list != NULL) {
Laurent@706: if (!first_subrange_case_list) {
laurent@347: s4o.print(s4o.indent_spaces + "else {\n");
laurent@347: s4o.indent_right();
Laurent@706: }
laurent@347: symbol->statement_list->accept(*this);
laurent@347: if (!first_subrange_case_list) {
laurent@347: s4o.indent_left();
laurent@347: s4o.print(s4o.indent_spaces + "}\n");
laurent@347: }
laurent@347: }
laurent@347: s4o.print(s4o.indent_spaces + "break;\n");
laurent@347: s4o.indent_left();
laurent@347: wanted_casegeneration = none_cg;
laurent@347: s4o.indent_left();
laurent@347: s4o.print(s4o.indent_spaces + "}\n");
laurent@347: s4o.indent_left();
laurent@347: s4o.print(s4o.indent_spaces + "}");
laurent@347: return NULL;
laurent@347: }
laurent@347:
laurent@347: /* helper symbol for case_statement */
laurent@347: void *visit(case_element_list_c *symbol) {return print_list(symbol);}
laurent@347:
laurent@347: void *visit(case_element_c *symbol) {
laurent@347: case_element_iterator_c *case_element_iterator;
laurent@347: symbol_c* element = NULL;
laurent@347: bool first_element = true;
laurent@347:
laurent@347: switch (wanted_casegeneration) {
laurent@347: case single_cg:
laurent@347: case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_single);
laurent@347: for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) {
laurent@347: if (first_element) first_element = false;
Laurent@706: s4o.print(s4o.indent_spaces + "case ");
laurent@347: element->accept(*this);
laurent@347: s4o.print(":\n");
laurent@347: }
laurent@347: delete case_element_iterator;
laurent@347: break;
laurent@347:
laurent@347: case subrange_cg:
laurent@347: case_element_iterator = new case_element_iterator_c(symbol->case_list, case_element_iterator_c::element_subrange);
laurent@347: for (element = case_element_iterator->next(); element != NULL; element = case_element_iterator->next()) {
laurent@347: if (first_element) {
laurent@347: if (first_subrange_case_list) {
laurent@347: s4o.print(s4o.indent_spaces + "if (");
laurent@347: first_subrange_case_list = false;
laurent@347: }
laurent@347: else {
Laurent@706: s4o.print(s4o.indent_spaces + "else if (");
laurent@347: }
laurent@347: first_element = false;
laurent@347: }
laurent@347: else {
laurent@347: s4o.print(" && ");
laurent@347: }
laurent@347: element->accept(*this);
laurent@347: }
laurent@347: delete case_element_iterator;
laurent@347: if (!first_element) {
laurent@347: s4o.print(") {\n");
laurent@347: }
laurent@347: break;
laurent@347:
laurent@347: default:
laurent@347: break;
laurent@347: }
laurent@347:
laurent@347: if (!first_element) {
lbessard@70: s4o.indent_right();
lbessard@70: symbol->statement_list->accept(*this);
laurent@347: switch (wanted_casegeneration) {
laurent@347: case single_cg:
laurent@347: s4o.print(s4o.indent_spaces + "break;\n");
laurent@347: s4o.indent_left();
laurent@347: break;
laurent@347: case subrange_cg:
Laurent@706: s4o.indent_left();
Laurent@706: s4o.print(s4o.indent_spaces + "}\n");
Laurent@706: break;
laurent@347: default:
laurent@347: break;
laurent@347: }
laurent@347: }
laurent@347: return NULL;
laurent@347: }
lbessard@70:
lbessard@70: /********************************/
lbessard@70: /* B 3.2.4 Iteration Statements */
lbessard@70: /********************************/
lbessard@70: void *visit(for_statement_c *symbol) {
lbessard@70: s4o.print("for(");
lbessard@70: symbol->control_variable->accept(*this);
lbessard@70: s4o.print(" = ");
lbessard@70: symbol->beg_expression->accept(*this);
lbessard@70: s4o.print("; ");
msousa@387: if (symbol->by_expression == NULL) {
msousa@387: /* increment by 1 */
msousa@387: symbol->control_variable->accept(*this);
msousa@387: s4o.print(" <= ");
msousa@387: symbol->end_expression->accept(*this);
msousa@387: s4o.print("; ");
msousa@387: symbol->control_variable->accept(*this);
msousa@387: s4o.print("++");
msousa@387: } else {
msousa@387: /* increment by user defined value */
msousa@387: /* The user defined increment value may be negative, in which case
msousa@387: * the expression to determine whether we have reached the end of the loop
msousa@387: * changes from a '<=' to a '>='.
msousa@387: * Since the increment value may change during runtime (remember, it is
msousa@387: * an expression, so may contain variables), choosing which test
msousa@387: * to use has to be done at runtime.
msousa@387: */
msousa@387: s4o.print("((");
lbessard@70: symbol->by_expression->accept(*this);
msousa@387: s4o.print(") > 0)? (");
msousa@387: symbol->control_variable->accept(*this);
msousa@387: s4o.print(" <= (");
msousa@387: symbol->end_expression->accept(*this);
msousa@387: s4o.print(")) : (");
msousa@387: symbol->control_variable->accept(*this);
msousa@387: s4o.print(" >= (");
msousa@387: symbol->end_expression->accept(*this);
msousa@387: s4o.print(")); ");
msousa@387: symbol->control_variable->accept(*this);
msousa@387: s4o.print(" += (");
msousa@387: symbol->by_expression->accept(*this);
msousa@387: s4o.print(")");
msousa@387: }
msousa@387: s4o.print(")");
msousa@387:
msousa@387: s4o.print(" {\n");
lbessard@70: s4o.indent_right();
lbessard@70: symbol->statement_list->accept(*this);
lbessard@70: s4o.indent_left();
lbessard@70: s4o.print(s4o.indent_spaces); s4o.print("}");
lbessard@70: return NULL;
lbessard@70: }
msousa@387:
lbessard@70: void *visit(while_statement_c *symbol) {
lbessard@70: s4o.print("while (");
lbessard@70: symbol->expression->accept(*this);
lbessard@70: s4o.print(") {\n");
lbessard@70: s4o.indent_right();
lbessard@70: symbol->statement_list->accept(*this);
lbessard@70: s4o.indent_left();
lbessard@70: s4o.print(s4o.indent_spaces); s4o.print("}");
lbessard@70: return NULL;
lbessard@70: }
msousa@387:
lbessard@70: void *visit(repeat_statement_c *symbol) {
lbessard@70: s4o.print("do {\n");
lbessard@70: s4o.indent_right();
lbessard@70: symbol->statement_list->accept(*this);
lbessard@70: s4o.indent_left();
lbessard@70: s4o.print(s4o.indent_spaces); s4o.print("} while(");
lbessard@70: symbol->expression->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
msousa@387:
lbessard@70: void *visit(exit_statement_c *symbol) {
msousa@281: s4o.print("break");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: }; /* generate_c_st_c */
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: