laurent@219: /*
Edouard@279: * matiec - a compiler for the programming languages defined in IEC 61131-3
laurent@219: *
msousa@683: * Copyright (C) 2003-2012 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@380: #define INLINE_PARAM_COUNT "__PARAM_COUNT"
laurent@219:
mjsousa@861: class generate_c_inlinefcall_c: public generate_c_base_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@706: /* The result of the comparison IL operations (GT, EQ, LT, ...)
Laurent@706: * is a boolean variable.
Laurent@706: * This class keeps track of the current data type stored in the
Laurent@706: * il default variable. This is usually done by keeping a reference
Laurent@706: * to the data type of the last operand. Nevertheless, in the case of
Laurent@706: * the comparison IL operators, the data type of the result (a boolean)
Laurent@706: * is not the data type of the operand. We therefore need an object
Laurent@706: * of the boolean data type to keep as a reference of the current
Laurent@706: * data type.
Laurent@706: * The following object is it...
Laurent@706: */
Laurent@706: bool_type_name_c bool_type;
Laurent@706: lint_type_name_c lint_type;
Laurent@706: lword_type_name_c lword_type;
Laurent@706: lreal_type_name_c lreal_type;
laurent@337:
laurent@219: /* The name of the IL default variable... */
Laurent@706: #define IL_DEFVAR VAR_LEADER "IL_DEFVAR"
Laurent@706:
Laurent@706: /* The name of the variable used to pass the result of a
Laurent@706: * parenthesised instruction list to the immediately preceding
Laurent@706: * scope ...
Laurent@706: */
laurent@337: #define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK"
msousa@683: il_default_variable_c implicit_variable_current;
Laurent@706:
Laurent@706: symbol_c* current_array_type;
Laurent@706:
Laurent@706: int fcall_number;
Laurent@706: bool generating_inlinefunction;
Laurent@706: symbol_c *fbname;
laurent@219:
laurent@219: search_varfb_instance_type_c *search_varfb_instance_type;
msousa@505: search_var_instance_decl_c *search_var_instance_decl;
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)
mjsousa@861: : generate_c_base_c(s4o_ptr),
msousa@683: implicit_variable_current(IL_DEFVAR, NULL)
laurent@219: {
laurent@219: search_varfb_instance_type = new search_varfb_instance_type_c(scope);
msousa@670: search_var_instance_decl = new search_var_instance_decl_c (scope);
msousa@505:
laurent@219: this->set_variable_prefix(variable_prefix);
laurent@219: fcall_number = 0;
laurent@219: fbname = name;
laurent@221: wanted_variablegeneration = expression_vg;
laurent@388: generating_inlinefunction = false;
laurent@219: }
laurent@219:
laurent@234: virtual ~generate_c_inlinefcall_c(void) {
laurent@219: delete search_varfb_instance_type;
msousa@505: delete search_var_instance_decl;
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@706: 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@388:
laurent@388: std::list::iterator pt;
laurent@388: generating_inlinefunction = true;
laurent@219:
laurent@219: fcall_number++;
msousa@670: function_type_prefix = default_literal_type(function_type_prefix);
laurent@336: if (function_type_suffix) {
msousa@670: function_type_suffix = 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) {
laurent@383: /* function being called is overloaded! */
laurent@383: s4o.print("__");
laurent@406: print_function_parameter_data_types_c overloaded_func_suf(&s4o);
msousa@350: f_decl->accept(overloaded_func_suf);
Laurent@706: }
laurent@336: if (function_type_suffix) {
msousa@350: function_type_suffix->accept(*this);
laurent@336: }
msousa@594: s4o.print(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) {
msousa@670: 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) {
Laurent@706: /* function being called is overloaded! */
Laurent@706: 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@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@388:
laurent@388: generating_inlinefunction = false;
laurent@219: }
laurent@219:
laurent@219: private:
msousa@670: /* a small helper function */
msousa@670: symbol_c *default_literal_type(symbol_c *symbol) {
msousa@670: if (get_datatype_info_c::is_ANY_INT_literal(symbol)) {
msousa@693: return &get_datatype_info_c::lint_type_name;
msousa@670: }
msousa@670: else if (get_datatype_info_c::is_ANY_REAL_literal(symbol)) {
msousa@693: return &get_datatype_info_c::lreal_type_name;
msousa@670: }
msousa@670: return symbol;
msousa@670: }
laurent@219:
msousa@683:
laurent@337:
berem@228: void *print_getter(symbol_c *symbol) {
msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
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: }
berem@228: else if (vartype == search_var_instance_decl_c::located_vt)
Laurent@706: s4o.print(GET_LOCATED);
berem@228: else
Laurent@706: s4o.print(GET_VAR);
berem@228: s4o.print("(");
berem@228:
berem@228: wanted_variablegeneration = complextype_base_vg;
berem@228: symbol->accept(*this);
msousa@851: 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,
msousa@670: symbol_c* type,
msousa@670: symbol_c* value) {
msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
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(SET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(SET_EXTERNAL);
Laurent@706: }
laurent@392: else if (vartype == search_var_instance_decl_c::located_vt)
laurent@392: s4o.print(SET_LOCATED);
laurent@392: else
laurent@392: s4o.print(SET_VAR);
laurent@392: s4o.print("(,");
mjsousa@885: /*
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);
msousa@852: if (analyse_variable_c::contains_complex_type(symbol)) {
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;
mjsousa@885: */
mjsousa@885: wanted_variablegeneration = complextype_base_vg;
mjsousa@885: symbol->accept(*this);
mjsousa@885: s4o.print(",");
mjsousa@885: if (analyse_variable_c::contains_complex_type(symbol)) {
mjsousa@885: wanted_variablegeneration = complextype_suffix_vg;
mjsousa@885: symbol->accept(*this);
mjsousa@885: }
mjsousa@885: s4o.print(",");
mjsousa@885: wanted_variablegeneration = expression_vg;
mjsousa@885: print_check_function(type, value, NULL, true);
mjsousa@885: s4o.print(")");
mjsousa@885: wanted_variablegeneration = expression_vg;
mjsousa@885: 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;
laurent@388: if (generating_inlinefunction) {
laurent@388: if (wanted_variablegeneration == complextype_base_vg)
laurent@388: generate_c_base_c::visit(symbol);
laurent@388: else if (wanted_variablegeneration == complextype_suffix_vg)
laurent@388: return NULL;
laurent@388: else
laurent@388: print_getter(symbol);
laurent@388: }
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@388: if (generating_inlinefunction) {
laurent@388: /* Do not use print_token() as it will change everything into uppercase */
laurent@388: if (strlen(symbol->value) == 0) ERROR;
laurent@388: s4o.print(GET_LOCATED);
laurent@388: s4o.print("(");
laurent@388: this->print_variable_prefix();
laurent@388: s4o.printlocation(symbol->value + 1);
laurent@388: s4o.print(")");
laurent@388: }
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");
msousa@854: bool type_is_complex = analyse_variable_c::is_complex_type(symbol->record_variable);
laurent@388: if (generating_inlinefunction) {
laurent@388: switch (wanted_variablegeneration) {
laurent@388: case complextype_base_vg:
laurent@388: symbol->record_variable->accept(*this);
Laurent@410: if (!type_is_complex) {
Laurent@706: s4o.print(".");
Laurent@706: symbol->field_selector->accept(*this);
Laurent@410: }
laurent@388: break;
laurent@388: case complextype_suffix_vg:
laurent@388: symbol->record_variable->accept(*this);
Laurent@410: if (type_is_complex) {
Laurent@410: s4o.print(".");
Laurent@410: symbol->field_selector->accept(*this);
Laurent@410: }
laurent@388: break;
laurent@388: default:
laurent@388: print_getter(symbol);
laurent@388: break;
laurent@388: }
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) {
laurent@388: if (generating_inlinefunction) {
laurent@388: switch (wanted_variablegeneration) {
laurent@388: case complextype_base_vg:
laurent@388: symbol->subscripted_variable->accept(*this);
laurent@388: break;
laurent@388: case complextype_suffix_vg:
laurent@388: symbol->subscripted_variable->accept(*this);
laurent@388:
laurent@388: current_array_type = search_varfb_instance_type->get_type_id(symbol->subscripted_variable);
laurent@388: if (current_array_type == NULL) ERROR;
laurent@388:
laurent@388: s4o.print(".table");
laurent@388: symbol->subscript_list->accept(*this);
laurent@388:
laurent@388: current_array_type = NULL;
laurent@388: break;
laurent@388: default:
laurent@388: print_getter(symbol);
laurent@388: break;
laurent@388: }
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@337: /* | label ':' [il_incomplete_instruction] eol_list */
laurent@337: // SYM_REF2(il_instruction_c, label, il_instruction)
laurent@337: void *visit(il_instruction_c *symbol) {
msousa@683: /* all previous IL instructions should have the same datatype (checked in stage3), so we get the datatype from the first previous IL instruction we find */
msousa@683: implicit_variable_current.datatype = (symbol->prev_il_instruction.empty())? NULL : symbol->prev_il_instruction[0]->datatype;
msousa@683: if (NULL != symbol->il_instruction) symbol->il_instruction->accept(*this);
msousa@683: implicit_variable_current.datatype = NULL;
msousa@683: return NULL;
msousa@683: }
msousa@683:
laurent@392:
laurent@337: /* | il_simple_operator [il_operand] */
laurent@337: //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
laurent@337: void *visit(il_simple_operation_c *symbol) {
laurent@337: symbol->il_simple_operator->accept(*this);
msousa@683: return NULL;
msousa@683: }
msousa@683:
mjsousa@874: /* il_jump_operator label */
mjsousa@874: // SYM_REF2(il_jump_operation_c, il_jump_operator, label)
mjsousa@874: void *visit(il_jump_operation_c *symbol) {
mjsousa@874: return NULL;
mjsousa@874: }
laurent@337:
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: 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@718: function_type_prefix = search_base_type_c::get_basetype_decl(f_decl->type_name);
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();
laurent@380: identifier_c *param_name = new identifier_c(INLINE_PARAM_COUNT);
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@683: if (NULL == implicit_variable_current.datatype) ERROR;
msousa@683: param_value = &this->implicit_variable_current;
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:
mjsousa@844: /* We do not yet support embedded IL lists, so we abort the compiler if we find one */
mjsousa@844: /* Note that in IL function calls the syntax does not allow embeded IL lists, so this check is not necessary here! */
mjsousa@844: /*
mjsousa@844: {simple_instr_list_c *instruction_list = dynamic_cast(param_value);
mjsousa@844: if (NULL != instruction_list) STAGE4_ERROR(param_value, param_value, "The compiler does not yet support formal invocations in IL that contain embedded IL lists. Aborting!");
mjsousa@844: }
mjsousa@844: */
mjsousa@844:
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@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;
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()
msousa@683: return NULL;
msousa@683: }
msousa@683:
msousa@683:
laurent@337:
laurent@337: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
laurent@337: //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused)
laurent@337: void *visit(il_expression_c *symbol) {
msousa@683: /* We will be recursevely interpreting an instruction list, so we store a backup of the implicit_variable_result/current.
msousa@683: * Notice that they will be overwriten while processing the parenthsized instruction list.
laurent@337: */
msousa@690: // il_default_variable_c old_implicit_variable_current = this->implicit_variable_current; // no longer needed as we do not call symbol->il_expr_operator->accept(*this);
msousa@690:
msousa@690: /* Stage2 will insert an artificial (and equivalent) LD to the simple_instr_list if necessary. We can therefore ignore the 'il_operand' entry! */
msousa@690: //if (NULL != symbol->il_operand) { do nothing!! }
laurent@337:
laurent@337: /* Now do the parenthesised instructions... */
msousa@683: /* NOTE: the following code line will get the variable this->implicit_variable_current.datatype updated! */
laurent@337: symbol->simple_instr_list->accept(*this);
laurent@337:
laurent@337: /* Now do the operation, using the previous result! */
msousa@683: /* NOTE: Actually, we do not need to call this, as it can never be a function call, which is what we are handling here... */
msousa@683: // this->implicit_variable_current.datatype = old_current_default_variable_data_type;
msousa@683: // symbol->il_expr_operator->accept(*this);
msousa@683: return NULL;
msousa@683: }
msousa@683:
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@718: function_type_prefix = search_base_type_c::get_basetype_decl(f_decl->type_name);
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();
laurent@380: identifier_c *param_name = new identifier_c(INLINE_PARAM_COUNT);
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: }
mjsousa@844:
mjsousa@844: /* We do not yet support embedded IL lists, so we abort the compiler if we find one */
mjsousa@844: {simple_instr_list_c *instruction_list = dynamic_cast(param_value);
mjsousa@844: if (NULL != instruction_list) STAGE4_ERROR(param_value, param_value, "The compiler does not yet support formal invocations in IL that contain embedded IL lists. Aborting!");
mjsousa@844: }
mjsousa@844:
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@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;
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()
msousa@683: return NULL;
msousa@683: }
msousa@683:
msousa@683:
laurent@337:
laurent@337: /* | simple_instr_list il_simple_instruction */
laurent@337: // SYM_LIST(simple_instr_list_c)
msousa@690: void *visit(simple_instr_list_c *symbol) {return iterator_visitor_c::visit(symbol);}
msousa@683:
msousa@683:
msousa@453: // SYM_REF1(il_simple_instruction_c, il_simple_instruction, symbol_c *prev_il_instruction;)
Laurent@706: void *visit(il_simple_instruction_c *symbol) {
msousa@690: /* all previous IL instructions should have the same datatype (checked in stage3), so we get the datatype from the first previous IL instruction we find */
msousa@690: implicit_variable_current.datatype = (symbol->prev_il_instruction.empty())? NULL : symbol->prev_il_instruction[0]->datatype;
msousa@690: symbol->il_simple_instruction->accept(*this);
msousa@690: implicit_variable_current.datatype = NULL;
msousa@690: return NULL;
msousa@453: }
laurent@337:
msousa@683:
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@337: void *visit(statement_list_c *symbol) {
Laurent@706: function_call_iterator_c fc_iterator(symbol);
Laurent@706: symbol_c* function_call;
Laurent@706: while ((function_call = fc_iterator.next()) != NULL) {
Laurent@706: function_call->accept(*this);
Laurent@706: }
Laurent@706: return NULL;
Laurent@706: }
laurent@337:
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@718: function_type_prefix = search_base_type_c::get_basetype_decl(f_decl->type_name);
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();
laurent@380: identifier_c *param_name = new identifier_c(INLINE_PARAM_COUNT);
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@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;
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@337: return NULL;
laurent@219: }
laurent@219:
Laurent@706: /*********************************************/
Laurent@706: /* B.1.6 Sequential function chart elements */
Laurent@706: /*********************************************/
Laurent@706:
Laurent@706: void *visit(initial_step_c *symbol) {
Laurent@706: return NULL;
Laurent@706: }
Laurent@706:
Laurent@706: void *visit(step_c *symbol) {
Laurent@706: return NULL;
Laurent@706: }
Laurent@706:
Laurent@706: void *visit(transition_c *symbol) {
Laurent@706: return symbol->transition_condition->accept(*this);
Laurent@706: }
Laurent@706:
Laurent@706: void *visit(transition_condition_c *symbol) {
Laurent@706: // Transition condition is in IL
Laurent@706: if (symbol->transition_condition_il != NULL) {
Laurent@706: symbol->transition_condition_il->accept(*this);
Laurent@706: }
Laurent@706:
Laurent@706: // Transition condition is in ST
Laurent@706: if (symbol->transition_condition_st != NULL) {
Laurent@706: function_call_iterator_c fc_iterator(symbol->transition_condition_st);
Laurent@706: symbol_c* function_call;
Laurent@706: while ((function_call = fc_iterator.next()) != NULL) {
Laurent@706: function_call->accept(*this);
Laurent@706: }
Laurent@706: }
Laurent@706:
Laurent@706: return NULL;
Laurent@706: }
Laurent@706:
Laurent@706: void *visit(action_c *symbol) {
Laurent@706: return symbol->function_block_body->accept(*this);
Laurent@706: }
laurent@388:
laurent@234: }; /* generate_c_inlinefcall_c */
laurent@234: