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: mjsousa@945: class generate_c_st_c: public generate_c_base_and_typeid_c { lbessard@70: laurent@217: public: laurent@217: typedef enum { laurent@217: expression_vg, laurent@226: complextype_base_vg, laurent@226: complextype_suffix_vg, laurent@217: fparam_output_vg laurent@217: } variablegeneration_t; laurent@217: 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: */ msousa@863: search_fb_instance_decl_c *search_fb_instance_decl; lbessard@70: search_varfb_instance_type_c *search_varfb_instance_type; msousa@505: search_var_instance_decl_c *search_var_instance_decl; msousa@863: msousa@863: symbol_c *scope_; 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; 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) mjsousa@945: : generate_c_base_and_typeid_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@863: scope_ = 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; 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@863: unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol, scope_); laurent@235: if (wanted_variablegeneration == fparam_output_vg) { Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) { msousa@854: if (!get_datatype_info_c::is_type_valid (symbol->datatype)) ERROR; msousa@854: if ( get_datatype_info_c::is_function_block(symbol->datatype)) 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) { msousa@854: if (!get_datatype_info_c::is_type_valid (symbol->datatype)) ERROR; msousa@854: if ( get_datatype_info_c::is_function_block(symbol->datatype)) 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: } mjsousa@887: mjsousa@887: variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; laurent@226: s4o.print("("); mjsousa@887: print_variable_prefix(); laurent@226: wanted_variablegeneration = complextype_base_vg; laurent@226: symbol->accept(*this); msousa@851: 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: msousa@852: msousa@852: 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) { mjsousa@888: laurent@405: if (fb_symbol == NULL) { msousa@863: unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol, scope_); mjsousa@889: symbol_c *first_nonfb = analyse_variable_c::find_first_nonfb(symbol); mjsousa@889: if (first_nonfb == NULL) ERROR; Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) { mjsousa@889: if (!get_datatype_info_c::is_type_valid (first_nonfb->datatype)) ERROR; mjsousa@889: if ( get_datatype_info_c::is_function_block(first_nonfb->datatype)) // handle situation where we are copying a complete fb -> fb1.fb2.fb3 := fb4 (and fb3 is external!) 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("("); mjsousa@887: laurent@226: if (fb_symbol != NULL) { laurent@226: print_variable_prefix(); mjsousa@888: // It is my (MJS) conviction that by this time the following will always be true... mjsousa@888: // wanted_variablegeneration == expression_vg; laurent@226: fb_symbol->accept(*this); laurent@392: s4o.print(".,"); mjsousa@888: symbol->accept(*this); mjsousa@888: s4o.print(","); mjsousa@888: s4o.print(","); mjsousa@897: } else { mjsousa@887: print_variable_prefix(); mjsousa@887: s4o.print(","); mjsousa@888: wanted_variablegeneration = complextype_base_vg; mjsousa@888: symbol->accept(*this); mjsousa@888: s4o.print(","); mjsousa@885: wanted_variablegeneration = complextype_suffix_vg; mjsousa@885: symbol->accept(*this); mjsousa@888: s4o.print(","); mjsousa@888: } mjsousa@885: wanted_variablegeneration = expression_vg; mjsousa@885: print_check_function(type, value, fb_value); mjsousa@885: s4o.print(")"); mjsousa@885: wanted_variablegeneration = expression_vg; mjsousa@885: 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) { mjsousa@999: symbol->lower_limit->accept(*this); 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@382: case complextype_base_vg: mjsousa@887: symbol->var_name->accept(*this); //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); mjsousa@887: if (( this->is_variable_prefix_null() && (wanted_variablegeneration != fparam_output_vg)) || mjsousa@887: (!this->is_variable_prefix_null() && (wanted_variablegeneration == expression_vg )) || mjsousa@887: (!this->is_variable_prefix_null() && (wanted_variablegeneration == fparam_output_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@226: switch (wanted_variablegeneration) { laurent@226: case complextype_base_vg: laurent@226: symbol->record_variable->accept(*this); mjsousa@897: /* NOTE: The following test includes a special case for SFC Steps. They are currently mapped onto a C data structure mjsousa@897: * that does not follow the standard IEC_ pattern used for user defined structure datatypes mjsousa@897: * (i.e. it does not include the 'values' and 'flag' structure mjsousa@897: * elements that are tested by __GET_VAR and __SET_VAR acessor macros defined in acessor.h). However, the mjsousa@897: * STEP.T and STEP.X elements of this step structure are of the IEC_BOOL and IEC_TIME datatypes, and are mjsousa@897: * actually structures that do have the 'value' and 'flags' elements. So, it is safe to say that any variable mjsousa@897: * that is a STEPname is not of a complex type, as its .T and .X elements are and can later be safely accessed mjsousa@897: * using the __SET_VAR and __GET_VAR macros. mjsousa@897: * mjsousa@897: * For the above reason, a STEP must be handled as a FB, i.e. it does NOT contain the 'flags' and 'value' elements! mjsousa@897: */ mjsousa@897: if ( get_datatype_info_c::is_function_block(symbol->record_variable->datatype) mjsousa@897: || get_datatype_info_c::is_sfc_step (symbol->record_variable->datatype)) { mjsousa@889: if (NULL == symbol->record_variable->scope) ERROR; mjsousa@889: search_var_instance_decl_c search_var_instance_decl(symbol->record_variable->scope); mjsousa@936: if (search_var_instance_decl_c::external_vt == search_var_instance_decl.get_vartype(get_var_name_c::get_last_field(symbol->record_variable))) mjsousa@943: s4o.print("->"); mjsousa@936: else if (dynamic_cast(symbol->record_variable) != NULL) mjsousa@936: s4o.print("->"); /* please read the comment in visit(deref_operator_c *) tio understand what this line is doing! */ mjsousa@889: else mjsousa@889: 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); mjsousa@897: // the following condition MUST be a negation of the above condition used in the 'case complextype_base_vg:' mjsousa@897: if (!( get_datatype_info_c::is_function_block(symbol->record_variable->datatype) // if the record variable is not a FB... mjsousa@897: || get_datatype_info_c::is_sfc_step (symbol->record_variable->datatype))) { // ...nor an SFC step name, then it will certainly be a structure! mjsousa@936: if (dynamic_cast(symbol->record_variable) != NULL) mjsousa@936: s4o.print("->"); /* please read the comment in visit(deref_operator_c *) tio understand what this line is doing! */ mjsousa@936: else mjsousa@936: s4o.print("."); Laurent@410: symbol->field_selector->accept(*this); Laurent@410: } laurent@226: break; laurent@226: default: laurent@226: if (this->is_variable_prefix_null()) { mjsousa@936: /* We are writing code for a FUNCTION. In this case, deref_operator_c are not transformed into the C pointer derefence syntax '->' (e.g. ptr->elem). mjsousa@936: * We use instead the '*' syntax (e.g. (*ptr).elem) mjsousa@936: * While in FB the '->' is generated by this structured_variable_c visitor, in Functions the '*' syntax is generated by the deref_operator_c visitor mjsousa@936: * This is why here we do NOT have --> {if (dynamic_cast(symbol->record_variable) != NULL) ..} mjsousa@936: * mjsousa@936: * please read the comment in visit(deref_operator_c *) for more information! mjsousa@936: */ 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@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: mjsousa@943: if (dynamic_cast(symbol->subscripted_variable) != NULL) mjsousa@943: s4o.print("->"); /* please read the comment in visit(deref_operator_c *) tio understand what this line is doing! */ mjsousa@943: else mjsousa@943: s4o.print("."); mjsousa@943: 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: /***********************/ mjsousa@936: void *visit(deref_operator_c *symbol) { mjsousa@936: /* When producing C code for FUNCTION_BLOCKS, we use the '*' syntax (e.g. (*ptr).elem) mjsousa@936: * When producing C code for a FUNCTION_BLOCK, the deref_operator_c are transformed in two ways, depending on where they occur. mjsousa@936: * - deref_operator between a struct and its elem (e.g. struct_ref^.elem1) mjsousa@936: * are transformed into C using the C pointer derefence syntax '->' (e.g. struct_ref->elem1). mjsousa@936: * (In this case, '->' is generated by this structured_variable_c visitor) mjsousa@936: * - deref_operator at the end of a struct variable (e.g. struct.elem_ptr^) mjsousa@936: * are transformed using the '*' syntax for C pointer dereferencing (e.g. *(struct.elem_ptr) ) mjsousa@936: * mjsousa@936: * NOTE: Ideally, we should always use the '*' C pointer dereferencing syntax. However, due to the mjsousa@936: * was the GET_VAR and SET_VAR are transformed into C, this does not work for '^' between a struct and its mjsousa@936: * element (e.g. struct_ref^.elem), which is whey in this case only we use the '->' syntax. mjsousa@936: * NOTE: The use of the -> syntax means that pointers to pointers are not supported betweem a struct and its elem mjsousa@936: * (e.g. . struct_ref_ref^^.elem) as this would be transformed into the invalid C code struct_ref_ref->->elem. mjsousa@936: * This is why we add a test for this case, and bug out with an error if we encounter it!! mjsousa@936: */ mjsousa@936: if (this->is_variable_prefix_null()) { mjsousa@936: /* For code in FUNCTIONs */ mjsousa@936: s4o.print("(*"); mjsousa@936: symbol->exp->accept(*this); mjsousa@936: s4o.print(")"); mjsousa@936: } else { mjsousa@936: /* For code in FBs, and PROGRAMS... */ mjsousa@943: if ( (NULL == dynamic_cast(symbol->parent)) mjsousa@943: && (NULL == dynamic_cast< array_variable_c *>(symbol->parent))) { mjsousa@936: s4o.print("(*"); mjsousa@936: symbol->exp->accept(*this); mjsousa@936: s4o.print(")"); mjsousa@936: } else { mjsousa@943: /* We are in a structured variable - the structured_variable_c or the array_variable_c will already have printed out the '->' !! */ mjsousa@936: if (NULL != dynamic_cast(symbol->exp)) mjsousa@941: STAGE4_ERROR(symbol, symbol->exp, "The use of two or more consecutive derefencing operators between a struct variable and its record elem (ex: struct_ref_ref^^.elem) is currently not supported for code inside a Function_Block."); mjsousa@936: symbol->exp->accept(*this); mjsousa@936: } mjsousa@936: } mjsousa@936: mjsousa@936: return NULL; mjsousa@936: } mjsousa@936: mjsousa@936: mjsousa@933: void *visit(deref_expression_c *symbol) { mjsousa@933: s4o.print("("); mjsousa@933: if (this->is_variable_prefix_null()) { mjsousa@933: /* For code in FUNCTIONs */ mjsousa@933: s4o.print("*"); mjsousa@933: symbol->exp->accept(*this); mjsousa@933: s4o.print(""); mjsousa@933: } else { mjsousa@933: /* For code in FBs, and PROGRAMS... */ mjsousa@933: unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol->exp, scope_); mjsousa@933: if (vartype == search_var_instance_decl_c::external_vt) { mjsousa@933: if (!get_datatype_info_c::is_type_valid (symbol->exp->datatype)) ERROR; mjsousa@933: if ( get_datatype_info_c::is_function_block(symbol->exp->datatype)) mjsousa@933: s4o.print(GET_EXTERNAL_FB_DREF); mjsousa@933: else mjsousa@933: s4o.print(GET_EXTERNAL_DREF); mjsousa@933: } mjsousa@933: else if (vartype == search_var_instance_decl_c::located_vt) mjsousa@933: s4o.print(GET_LOCATED_DREF); mjsousa@933: else mjsousa@933: s4o.print(GET_VAR_DREF); mjsousa@933: mjsousa@933: variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; mjsousa@933: s4o.print("("); mjsousa@933: wanted_variablegeneration = complextype_base_vg; mjsousa@933: print_variable_prefix(); mjsousa@933: symbol->exp->accept(*this); mjsousa@933: s4o.print(","); mjsousa@933: wanted_variablegeneration = complextype_suffix_vg; mjsousa@933: symbol->exp->accept(*this); mjsousa@933: s4o.print(")"); mjsousa@933: wanted_variablegeneration = old_wanted_variablegeneration; mjsousa@933: } mjsousa@933: s4o.print(")"); mjsousa@933: mjsousa@933: return NULL; mjsousa@933: } mjsousa@933: mjsousa@933: mjsousa@873: void *visit(ref_expression_c *symbol) { mjsousa@909: s4o.print("("); mjsousa@873: if (this->is_variable_prefix_null()) { mjsousa@873: /* For code in FUNCTIONs */ mjsousa@873: s4o.print("&("); mjsousa@873: symbol->exp->accept(*this); mjsousa@873: s4o.print(")"); mjsousa@873: } else { mjsousa@873: /* For code in FBs, and PROGRAMS... */ mjsousa@873: s4o.print("("); mjsousa@873: unsigned int vartype = analyse_variable_c::first_nonfb_vardecltype(symbol->exp, scope_); mjsousa@873: if (vartype == search_var_instance_decl_c::external_vt) { mjsousa@873: if (!get_datatype_info_c::is_type_valid (symbol->exp->datatype)) ERROR; mjsousa@873: if ( get_datatype_info_c::is_function_block(symbol->exp->datatype)) mjsousa@873: s4o.print(GET_EXTERNAL_FB_REF); mjsousa@873: else mjsousa@873: s4o.print(GET_EXTERNAL_REF); mjsousa@873: } mjsousa@873: else if (vartype == search_var_instance_decl_c::located_vt) mjsousa@873: s4o.print(GET_LOCATED_REF); mjsousa@873: else mjsousa@873: s4o.print(GET_VAR_REF); mjsousa@873: mjsousa@873: variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration; mjsousa@873: s4o.print("("); mjsousa@873: wanted_variablegeneration = complextype_base_vg; mjsousa@887: print_variable_prefix(); mjsousa@873: symbol->exp->accept(*this); mjsousa@873: s4o.print(","); mjsousa@873: wanted_variablegeneration = complextype_suffix_vg; mjsousa@873: symbol->exp->accept(*this); mjsousa@873: s4o.print(")"); mjsousa@873: wanted_variablegeneration = old_wanted_variablegeneration; mjsousa@873: mjsousa@873: s4o.print(")"); mjsousa@873: } mjsousa@873: s4o.print(")"); mjsousa@873: mjsousa@873: return NULL; mjsousa@873: } mjsousa@873: mjsousa@873: 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)) mjsousa@1011: 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)) mjsousa@1011: 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)) mjsousa@1011: 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)) mjsousa@1011: 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)) mjsousa@1011: 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)) mjsousa@1011: 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: 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: 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: 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: 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) { mjsousa@1011: s4o.print("__expt((LREAL)("); msousa@668: symbol->l_exp->accept(*this); mjsousa@1011: s4o.print("), (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(); mjsousa@1011: s4o.print("\n"+s4o.indent_spaces); 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) { mjsousa@877: for(int i = 0; i < symbol->n; i++) { mjsousa@877: print_line_directive(symbol->elements[i]); mjsousa@877: s4o.print(s4o.indent_spaces); mjsousa@877: symbol->elements[i]->accept(*this); mjsousa@877: s4o.print(";\n"); mjsousa@877: } mjsousa@877: return NULL; 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] ')' */ mjsousa@855: /* formal_param_list -> may be NULL ! */ mjsousa@855: /* nonformal_param_list -> may be NULL ! */ mjsousa@855: /* NOTE: The parameter 'called_fb_declaration'is used to pass data between stage 3 and stage4 (although currently it is not used in stage 4 */ mjsousa@855: // SYM_REF3(fb_invocation_c, fb_name, formal_param_list, nonformal_param_list, symbol_c *called_fb_declaration;) lbessard@70: void *visit(fb_invocation_c *symbol) { lbessard@70: TRACE("fb_invocation_c"); mjsousa@855: mjsousa@855: /* find the declaration of the function block type being called... */ mjsousa@855: symbol_c *fb_decl = symbol->called_fb_declaration; mjsousa@855: if (fb_decl == NULL) ERROR; mjsousa@855: /* figure out the name of the function block type of the function block being called... */ mjsousa@855: symbol_c *function_block_type_name = get_datatype_info_c::get_id(fb_decl); mjsousa@855: if (NULL == function_block_type_name) ERROR; mjsousa@855: 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); mjsousa@999: s4o.print(";\n"); lbessard@70: symbol->case_element_list->accept(*this); lbessard@70: if (symbol->statement_list != NULL) { mjsousa@999: s4o.print(s4o.indent_spaces + "else {\n"); mjsousa@999: s4o.indent_right(); laurent@347: symbol->statement_list->accept(*this); mjsousa@999: s4o.indent_left(); mjsousa@999: s4o.print(s4o.indent_spaces + "}\n"); mjsousa@999: } laurent@347: s4o.indent_left(); mjsousa@999: s4o.print(s4o.indent_spaces + "}"); mjsousa@999: return NULL; mjsousa@999: } mjsousa@999: mjsousa@999: mjsousa@999: /* helper symbol for case_statement */ mjsousa@999: void *visit(case_element_list_c *symbol) {return print_list(symbol, s4o.indent_spaces+"if ", s4o.indent_spaces+"else if ");} mjsousa@999: mjsousa@999: void *visit(case_element_c *symbol) { mjsousa@999: symbol->case_list->accept(*this); mjsousa@999: s4o.print(" {\n"); mjsousa@999: s4o.indent_right(); mjsousa@999: symbol->statement_list->accept(*this); laurent@347: s4o.indent_left(); laurent@347: s4o.print(s4o.indent_spaces + "}\n"); mjsousa@999: return NULL; mjsousa@999: } mjsousa@999: mjsousa@999: mjsousa@999: void *visit(case_list_c *symbol) { mjsousa@999: s4o.print("("); mjsousa@999: for (int i = 0; i < symbol->n; i++) { mjsousa@999: /* if not the first element, then add an '||', a '\n', and add some spaces to get nice alignment */ mjsousa@999: /* example of generated C code (codition) for mjsousa@999: * case XX of mjsousa@999: * 10..20,42,15..99: <---- C code is for this line! mjsousa@999: * mjsousa@999: * else if ((__case_expression >= 10 && __case_expression <= 20) || mjsousa@999: * (__case_expression == 42) || mjsousa@999: * (__case_expression >= 15 && __case_expression <= 99)) { mjsousa@999: */ mjsousa@999: if (0 != i) s4o.print(" ||\n" + s4o.indent_spaces + " "); mjsousa@999: s4o.print("("); mjsousa@999: subrange_c *subrange = dynamic_cast(symbol->elements[i]); mjsousa@999: if (NULL == subrange) { mjsousa@999: s4o.print("__case_expression == "); mjsousa@999: symbol->elements[i]->accept(*this); mjsousa@999: } else { mjsousa@999: s4o.print("__case_expression >= "); mjsousa@999: subrange->lower_limit->accept(*this); mjsousa@999: s4o.print(" && __case_expression <= "); mjsousa@999: subrange->upper_limit->accept(*this); mjsousa@999: } mjsousa@999: s4o.print(")"); mjsousa@999: } mjsousa@999: s4o.print(")"); 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: