laurent@219: /*
Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3
laurent@219: *
Edouard@279: * Copyright (C) 2003-2011 Mario de Sousa (msousa@fe.up.pt)
Edouard@279: * Copyright (C) 2007-2011 Laurent Bessard and Edouard Tisserant
laurent@219: *
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 .
Edouard@279: *
laurent@219: *
laurent@219: * This code is made available on the understanding that it will not be
laurent@219: * used in safety-critical situations without a full and competent review.
laurent@219: */
laurent@219:
laurent@219:
laurent@219: #define INLINE_RESULT_TEMP_VAR "__res"
laurent@219:
laurent@234: class generate_c_inlinefcall_c: public generate_c_typedecl_c {
laurent@219:
laurent@221: public:
laurent@221: typedef enum {
laurent@221: expression_vg,
berem@228: assignment_vg,
berem@228: complextype_base_vg,
berem@228: complextype_suffix_vg
laurent@221: } variablegeneration_t;
laurent@221:
laurent@219: private:
laurent@219:
laurent@219: /* The name of the IL default variable... */
laurent@219: #define IL_DEFVAR VAR_LEADER "IL_DEFVAR"
laurent@219: /* The name of the variable used to pass the result of a
laurent@219: * parenthesised instruction list to the immediately preceding
laurent@219: * scope ...
laurent@219: */
laurent@219: il_default_variable_c default_variable_name;
laurent@219:
berem@228: symbol_c* current_array_type;
berem@228:
laurent@219: int fcall_number;
laurent@219: symbol_c *fbname;
laurent@219:
laurent@219: search_expression_type_c *search_expression_type;
laurent@219:
laurent@219: search_varfb_instance_type_c *search_varfb_instance_type;
laurent@219:
laurent@219: search_base_type_c search_base_type;
laurent@219:
laurent@221: variablegeneration_t wanted_variablegeneration;
laurent@221:
laurent@219: public:
laurent@234: generate_c_inlinefcall_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
laurent@219: : generate_c_typedecl_c(s4o_ptr),
laurent@219: default_variable_name(IL_DEFVAR, NULL)
laurent@219: {
laurent@219: search_expression_type = new search_expression_type_c(scope);
laurent@219: search_varfb_instance_type = new search_varfb_instance_type_c(scope);
laurent@219: this->set_variable_prefix(variable_prefix);
laurent@219: fcall_number = 0;
laurent@219: fbname = name;
laurent@221: wanted_variablegeneration = expression_vg;
laurent@219: }
laurent@219:
laurent@234: virtual ~generate_c_inlinefcall_c(void) {
laurent@234: delete search_expression_type;
laurent@219: delete search_varfb_instance_type;
laurent@219: }
laurent@219:
laurent@234: void print(symbol_c* symbol) {
laurent@234: function_call_iterator_c fc_iterator(symbol);
laurent@234: symbol_c* function_call;
laurent@234: while ((function_call = fc_iterator.next()) != NULL) {
laurent@234: function_call->accept(*this);
laurent@234: }
laurent@234: }
laurent@234:
msousa@350:
msousa@350:
laurent@234: void generate_inline(symbol_c *function_name,
laurent@235: symbol_c *function_type_prefix,
laurent@235: symbol_c *function_type_suffix,
msousa@350: std::list param_list,
msousa@350: function_declaration_c *f_decl = NULL) {
laurent@234: std::list::iterator pt;
laurent@219:
laurent@219: fcall_number++;
laurent@336: function_type_prefix = search_expression_type->default_literal_type(function_type_prefix);
laurent@336: if (function_type_suffix) {
laurent@336: function_type_suffix = search_expression_type->default_literal_type(function_type_suffix);
laurent@332: }
laurent@219:
laurent@219: s4o.print(s4o.indent_spaces);
laurent@219: s4o.print("inline ");
laurent@235: function_type_prefix->accept(*this);
laurent@219: s4o.print(" __");
laurent@219: fbname->accept(*this);
laurent@219: s4o.print("_");
laurent@219: function_name->accept(*this);
msousa@350: if (f_decl != NULL) {
msousa@350: printf("generate_inline(): calling print_function_parameter_data_types_c !!!!!!!!!!!!!!!!!!!!!!\n");
msousa@350: print_function_parameter_data_types_c overloaded_func_suf(&s4o);
msousa@350: f_decl->accept(overloaded_func_suf);
msousa@350: }
laurent@336: if (function_type_suffix) {
msousa@350: function_type_suffix->accept(*this);
laurent@336: }
laurent@219: s4o.print_integer(fcall_number);
laurent@219: s4o.print("(");
laurent@219: s4o.indent_right();
laurent@219:
laurent@219: PARAM_LIST_ITERATOR() {
laurent@219: if (PARAM_DIRECTION == function_param_iterator_c::direction_in) {
laurent@336: search_expression_type->default_literal_type(PARAM_TYPE)->accept(*this);
laurent@219: s4o.print(" ");
laurent@219: PARAM_NAME->accept(*this);
laurent@219: s4o.print(",\n" + s4o.indent_spaces);
laurent@219: }
laurent@219: }
laurent@219: fbname->accept(*this);
msousa@350: s4o.print(" *");
msousa@350: s4o.print(FB_FUNCTION_PARAM);
msousa@350: s4o.indent_left();
msousa@350: s4o.print(")\n" + s4o.indent_spaces);
msousa@350: s4o.print("{\n");
laurent@219: s4o.indent_right();
laurent@219:
laurent@219: s4o.print(s4o.indent_spaces);
laurent@235: function_type_prefix->accept(*this);
laurent@219: s4o.print(" "),
laurent@219: s4o.print(INLINE_RESULT_TEMP_VAR);
laurent@219: s4o.print(";\n");
laurent@219:
msousa@350: 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: s4o.print(s4o.indent_spaces);
msousa@350: PARAM_TYPE->accept(*this);
laurent@219: s4o.print(" ");
laurent@219: s4o.print(TEMP_VAR);
laurent@219: PARAM_NAME->accept(*this);
laurent@219: s4o.print(" = ");
laurent@219: print_check_function(PARAM_TYPE, PARAM_VALUE);
laurent@219: s4o.print(";\n");
msousa@350: }
msousa@350: }
msousa@350:
msousa@350: s4o.print(s4o.indent_spaces + INLINE_RESULT_TEMP_VAR),
msousa@350: s4o.print(" = ");
msousa@350: function_name->accept(*this);
msousa@350: if (f_decl != NULL) {
msousa@350: printf("generate_inline(): calling print_function_parameter_data_types_c !!!!!!!!!!!!!!!!!!!!!!\n");
msousa@350: print_function_parameter_data_types_c overloaded_func_suf(&s4o);
msousa@350: f_decl->accept(overloaded_func_suf);
msousa@350: }
msousa@350:
msousa@350: if (function_type_suffix)
laurent@235: function_type_suffix->accept(*this);
msousa@350: s4o.print("(");
msousa@350: s4o.indent_right();
msousa@350:
msousa@350: PARAM_LIST_ITERATOR() {
msousa@350: if (pt != param_list.begin())
msousa@350: s4o.print(",\n" + s4o.indent_spaces);
msousa@350: if (PARAM_DIRECTION == function_param_iterator_c::direction_in)
msousa@350: PARAM_NAME->accept(*this);
msousa@350: else if (PARAM_VALUE != NULL){
laurent@219: s4o.print("&");
laurent@219: s4o.print(TEMP_VAR);
laurent@219: PARAM_NAME->accept(*this);
msousa@350: } else {
msousa@350: s4o.print("NULL");
msousa@350: }
msousa@350: }
msousa@350: s4o.print(");\n");
msousa@350: s4o.indent_left();
msousa@350:
msousa@350: PARAM_LIST_ITERATOR() {
laurent@219: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
msousa@350: PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
msousa@350: PARAM_VALUE != NULL) {
laurent@219: s4o.print(s4o.indent_spaces);
berem@228: print_setter(PARAM_VALUE, PARAM_TYPE, PARAM_NAME);
berem@228: s4o.print(";\n");
msousa@350: }
msousa@350: }
msousa@350: s4o.print(s4o.indent_spaces + "return ");
msousa@350: s4o.print(INLINE_RESULT_TEMP_VAR);
msousa@350: s4o.print(";\n");
laurent@219:
laurent@219: s4o.indent_left();
laurent@219: s4o.print(s4o.indent_spaces + "}\n\n");
laurent@219: }
laurent@219:
laurent@219: private:
laurent@219:
berem@228: void *print_getter(symbol_c *symbol) {
berem@228: unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
berem@228: if (vartype == search_var_instance_decl_c::external_vt)
berem@228: s4o.print(GET_EXTERNAL);
berem@228: else if (vartype == search_var_instance_decl_c::located_vt)
berem@228: s4o.print(GET_LOCATED);
berem@228: else
berem@228: s4o.print(GET_VAR);
berem@228: s4o.print("(");
berem@228:
berem@228: wanted_variablegeneration = complextype_base_vg;
berem@228: symbol->accept(*this);
berem@228: if (search_varfb_instance_type->type_is_complex())
berem@228: s4o.print(",");
berem@228: wanted_variablegeneration = complextype_suffix_vg;
berem@228: symbol->accept(*this);
berem@228: s4o.print(")");
berem@228: wanted_variablegeneration = expression_vg;
berem@228: return NULL;
berem@228: }
berem@228:
berem@228: void *print_setter(symbol_c* symbol,
berem@228: symbol_c* type,
berem@228: symbol_c* value) {
berem@228: unsigned int vartype = search_varfb_instance_type->get_vartype(symbol);
laurent@231: if (vartype == search_var_instance_decl_c::external_vt) {
laurent@231: symbolic_variable_c *variable = dynamic_cast(symbol);
laurent@231: /* TODO Find a solution for forcing global complex variables */
laurent@231: if (variable != NULL) {
laurent@231: s4o.print(SET_EXTERNAL);
laurent@231: s4o.print("(");
laurent@231: variable->var_name->accept(*this);
laurent@231: s4o.print(",");
laurent@231: }
laurent@231: else {
laurent@231: s4o.print(SET_COMPLEX_EXTERNAL);
laurent@231: s4o.print("(");
laurent@231: }
laurent@231: }
laurent@231: else {
laurent@231: if (vartype == search_var_instance_decl_c::located_vt)
laurent@231: s4o.print(SET_LOCATED);
laurent@231: else
laurent@231: s4o.print(SET_VAR);
laurent@231: s4o.print("(");
laurent@231: }
berem@228:
berem@228: wanted_variablegeneration = complextype_base_vg;
berem@228: symbol->accept(*this);
berem@228: s4o.print(",");
berem@228: wanted_variablegeneration = expression_vg;
berem@228: print_check_function(type, value, NULL, true);
berem@228: if (search_varfb_instance_type->type_is_complex()) {
berem@228: s4o.print(",");
berem@228: wanted_variablegeneration = complextype_suffix_vg;
berem@228: symbol->accept(*this);
berem@228: }
berem@228: s4o.print(")");
berem@228: wanted_variablegeneration = expression_vg;
berem@228: return NULL;
berem@228: }
berem@228:
laurent@219: /*********************/
laurent@219: /* B 1.4 - Variables */
laurent@219: /*********************/
laurent@219: void *visit(symbolic_variable_c *symbol) {
berem@228: unsigned int vartype;
berem@228: if (wanted_variablegeneration == complextype_base_vg)
berem@228: generate_c_base_c::visit(symbol);
berem@228: else if (wanted_variablegeneration == complextype_suffix_vg)
berem@228: return NULL;
laurent@221: else
laurent@235: print_getter(symbol);
laurent@219: return NULL;
laurent@219: }
laurent@219:
laurent@219: /********************************************/
laurent@219: /* B.1.4.1 Directly Represented Variables */
laurent@219: /********************************************/
laurent@219: // direct_variable: direct_variable_token {$$ = new direct_variable_c($1);};
laurent@219: void *visit(direct_variable_c *symbol) {
laurent@219: TRACE("direct_variable_c");
laurent@219: /* Do not use print_token() as it will change everything into uppercase */
laurent@219: if (strlen(symbol->value) == 0) ERROR;
laurent@219: s4o.print(GET_LOCATED);
laurent@219: s4o.print("(");
laurent@219: this->print_variable_prefix();
laurent@219: s4o.printlocation(symbol->value + 1);
laurent@219: s4o.print(")");
laurent@219: return NULL;
laurent@219: }
laurent@219:
berem@228: /*************************************/
berem@228: /* B.1.4.2 Multi-element Variables */
berem@228: /*************************************/
berem@228:
berem@228: // SYM_REF2(structured_variable_c, record_variable, field_selector)
berem@228: void *visit(structured_variable_c *symbol) {
berem@228: TRACE("structured_variable_c");
berem@228: switch (wanted_variablegeneration) {
berem@228: case complextype_base_vg:
berem@228: symbol->record_variable->accept(*this);
berem@228: break;
berem@228: case complextype_suffix_vg:
berem@228: symbol->record_variable->accept(*this);
berem@228: s4o.print(".");
berem@228: symbol->field_selector->accept(*this);
berem@228: break;
berem@228: default:
laurent@237: print_getter(symbol);
berem@228: break;
berem@228: }
berem@228: return NULL;
berem@228: }
berem@228:
berem@228: /* subscripted_variable '[' subscript_list ']' */
berem@228: //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
berem@228: void *visit(array_variable_c *symbol) {
berem@228: switch (wanted_variablegeneration) {
berem@228: case complextype_base_vg:
berem@228: symbol->subscripted_variable->accept(*this);
berem@228: break;
berem@228: case complextype_suffix_vg:
laurent@238: symbol->subscripted_variable->accept(*this);
laurent@238:
msousa@321: current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable);
laurent@238: if (current_array_type == NULL) ERROR;
laurent@238:
laurent@238: s4o.print(".table");
laurent@238: symbol->subscript_list->accept(*this);
laurent@238:
laurent@238: current_array_type = NULL;
berem@228: break;
berem@228: default:
laurent@237: print_getter(symbol);
berem@228: break;
berem@228: }
berem@228: return NULL;
berem@228: }
berem@228:
laurent@219: /****************************************/
laurent@219: /* B.2 - Language IL (Instruction List) */
laurent@219: /****************************************/
laurent@219:
laurent@219: /***********************************/
laurent@219: /* B 2.1 Instructions and Operands */
laurent@219: /***********************************/
laurent@219:
laurent@219: void *visit(il_function_call_c *symbol) {
laurent@235: symbol_c* function_type_prefix = NULL;
msousa@350: symbol_c* function_name = NULL;
laurent@235: symbol_c* function_type_suffix = NULL;
laurent@235: DECLARE_PARAM_LIST()
laurent@235:
laurent@235: symbol_c *param_data_type = default_variable_name.current_type;
laurent@235:
laurent@235: function_call_param_iterator_c function_call_param_iterator(symbol);
laurent@235:
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: /* determine the base data type returned by the function being called... */
msousa@350: search_base_type_c search_base_type;
msousa@350: function_type_prefix = (symbol_c *)f_decl->type_name->accept(search_base_type);
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:
msousa@350: function_param_iterator_c fp_iterator(f_decl);
msousa@350: identifier_c *param_name;
msousa@350: /* flag to remember whether we have already used the value stored in the default variable to pass to the first parameter */
msousa@350: bool used_defvar = false;
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: 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: /* NOTE: the following line of code is not required in this case, but it doesn't
msousa@350: * harm to leave it in, as in the case of a non-formal syntax function call,
msousa@350: * it will always return NULL.
msousa@350: * We leave it in in case we later decide to merge this part of the code together
msousa@350: * with the function calling code in generate_c_st_c, which does require
msousa@350: * the following line...
msousa@350: */
msousa@350: if (param_value == NULL)
msousa@350: param_value = function_call_param_iterator.search_f(param_name);
msousa@350:
msousa@350: /* if it is the first parameter in a non-formal function call (which is the
msousa@350: * case being handled!), semantics specifies that we should
msousa@350: * get the value off the IL default variable!
msousa@350: *
msousa@350: * However, if the parameter is an implicitly defined EN or ENO parameter, we should not
msousa@350: * use the default variable as a source of data to pass to those parameters!
msousa@350: */
msousa@350: if ((param_value == NULL) && (!used_defvar) && !fp_iterator.is_en_eno_param_implicit()) {
msousa@350: param_value = &this->default_variable_name;
msousa@350: used_defvar = true;
msousa@350: }
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.
msousa@350: */
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, fp_iterator.param_direction())
msousa@350: } /* for(...) */
msousa@350:
msousa@350: if (function_call_param_iterator.next_nf() != NULL) ERROR;
laurent@235: if (NULL == function_type_prefix) ERROR;
laurent@235:
laurent@235: bool has_output_params = false;
laurent@235:
laurent@235: PARAM_LIST_ITERATOR() {
laurent@235: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
laurent@235: PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
laurent@235: PARAM_VALUE != NULL) {
laurent@235: has_output_params = true;
laurent@235: }
laurent@235: }
laurent@235:
msousa@350: /* Check whether we are calling an overloaded function! */
msousa@350: /* (fdecl_mutiplicity==2) => calling overloaded function */
msousa@350: int fdecl_mutiplicity = function_symtable.multiplicity(symbol->function_name);
msousa@350: if (fdecl_mutiplicity == 0) ERROR;
msousa@350: if (fdecl_mutiplicity == 1)
msousa@350: /* function being called is NOT overloaded! */
msousa@350: f_decl = NULL;
msousa@350:
laurent@235: if (has_output_params)
msousa@350: generate_inline(function_name, function_type_prefix, function_type_suffix, param_list, f_decl);
laurent@235:
laurent@235: CLEAR_PARAM_LIST()
laurent@235:
laurent@235: return NULL;
laurent@235: }
laurent@235:
laurent@235: /* | function_name '(' eol_list [il_param_list] ')' */
laurent@235: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
laurent@219: void *visit(il_formal_funct_call_c *symbol) {
laurent@235: symbol_c* function_type_prefix = NULL;
laurent@235: symbol_c* function_name = NULL;
laurent@235: symbol_c* function_type_suffix = NULL;
laurent@235: DECLARE_PARAM_LIST()
laurent@235:
laurent@235: function_call_param_iterator_c function_call_param_iterator(symbol);
laurent@235:
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: /* determine the base data type returned by the function being called... */
msousa@350: search_base_type_c search_base_type;
msousa@350: function_type_prefix = (symbol_c *)f_decl->type_name->accept(search_base_type);
msousa@350: if (NULL == function_type_prefix) 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:
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: /* NOTE: the following line of code is not required in this case, but it doesn't
msousa@350: * harm to leave it in, as in the case of a formal syntax function call,
msousa@350: * it will always return NULL.
msousa@350: * We leave it in in case we later decide to merge this part of the code together
msousa@350: * with the function calling code in generate_c_st_c, which does require
msousa@350: * the following line...
msousa@350: */
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.
msousa@350: */
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, fp_iterator.param_direction())
laurent@235: }
laurent@235:
laurent@235: if (function_call_param_iterator.next_nf() != NULL) ERROR;
laurent@235:
laurent@235: bool has_output_params = false;
laurent@235:
laurent@235: PARAM_LIST_ITERATOR() {
laurent@235: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
laurent@235: PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
laurent@235: PARAM_VALUE != NULL) {
laurent@235: has_output_params = true;
laurent@219: }
laurent@235: }
laurent@235:
msousa@350: /* Check whether we are calling an overloaded function! */
msousa@350: /* (fdecl_mutiplicity==2) => calling overloaded function */
msousa@350: int fdecl_mutiplicity = function_symtable.multiplicity(symbol->function_name);
msousa@350: if (fdecl_mutiplicity == 0) ERROR;
msousa@350: if (fdecl_mutiplicity == 1)
msousa@350: /* function being called is NOT overloaded! */
msousa@350: f_decl = NULL;
msousa@350:
laurent@235: if (has_output_params)
msousa@350: generate_inline(function_name, function_type_prefix, function_type_suffix, param_list, f_decl);
laurent@235:
laurent@235: CLEAR_PARAM_LIST()
laurent@235:
laurent@219: return NULL;
laurent@219: }
laurent@219:
laurent@219: /***************************************/
laurent@219: /* B.3 - Language ST (Structured Text) */
laurent@219: /***************************************/
laurent@219: /***********************/
laurent@219: /* B 3.1 - Expressions */
laurent@219: /***********************/
laurent@219:
laurent@219: void *visit(function_invocation_c *symbol) {
laurent@235: symbol_c* function_type_prefix = NULL;
laurent@235: symbol_c* function_name = NULL;
laurent@235: symbol_c* function_type_suffix = NULL;
laurent@235: DECLARE_PARAM_LIST()
laurent@235:
laurent@235: symbol_c *parameter_assignment_list = NULL;
laurent@235: if (NULL != symbol-> formal_param_list) parameter_assignment_list = symbol-> formal_param_list;
laurent@235: if (NULL != symbol->nonformal_param_list) parameter_assignment_list = symbol->nonformal_param_list;
laurent@235: if (NULL == parameter_assignment_list) ERROR;
laurent@235:
laurent@235: function_call_param_iterator_c function_call_param_iterator(symbol);
laurent@235:
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: /* determine the base data type returned by the function being called... */
msousa@350: search_base_type_c search_base_type;
msousa@350: function_type_prefix = (symbol_c *)f_decl->type_name->accept(search_base_type);
msousa@350: if (NULL == function_type_prefix) ERROR;
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.
laurent@219: */
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);
laurent@235:
laurent@235: if (function_call_param_iterator.next_nf() != NULL) ERROR;
msousa@350:
msousa@350: bool has_output_params = false;
msousa@350:
msousa@350: PARAM_LIST_ITERATOR() {
laurent@235: if ((PARAM_DIRECTION == function_param_iterator_c::direction_out ||
laurent@235: PARAM_DIRECTION == function_param_iterator_c::direction_inout) &&
laurent@235: PARAM_VALUE != NULL) {
laurent@235: has_output_params = true;
laurent@219: }
laurent@235: }
laurent@235:
msousa@350: /* Check whether we are calling an overloaded function! */
msousa@350: /* (fdecl_mutiplicity==2) => calling overloaded function */
msousa@350: int fdecl_mutiplicity = function_symtable.multiplicity(symbol->function_name);
msousa@350: if (fdecl_mutiplicity == 0) ERROR;
msousa@350: if (fdecl_mutiplicity == 1)
msousa@350: /* function being called is NOT overloaded! */
msousa@350: f_decl = NULL;
msousa@350:
laurent@235: if (has_output_params)
msousa@350: generate_inline(function_name, function_type_prefix, function_type_suffix, param_list, f_decl);
laurent@235:
laurent@235: CLEAR_PARAM_LIST()
laurent@235:
msousa@350: return NULL;
laurent@219: }
laurent@219:
laurent@234: }; /* generate_c_inlinefcall_c */
laurent@234:
laurent@234: