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 .
Edouard@279: *
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:
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70:
lbessard@70: /* Returns the data type of an il_operand.
lbessard@70: *
lbessard@70: * Note that the il_operand may be a variable, in which case
lbessard@70: * we return the type of the variable instance.
lbessard@70: * The il_operand may also be a constant, in which case
lbessard@70: * we return the data type of that constant.
lbessard@70: *
lbessard@70: * The variable instance may be a member of a structured variable,
lbessard@70: * or an element in an array, or any combination of the two.
lbessard@70: *
lbessard@70: * The class constructor must be given the search scope
lbessard@70: * (function, function block or program within which
lbessard@70: * the possible il_operand variable instance was declared).
lbessard@70: */
lbessard@70:
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70:
lbessard@70:
lbessard@70: /* A new class to ouput the il default variable to c++ code
lbessard@70: * We use this class, inheriting from symbol_c, so it may be used
lbessard@70: * as any other symbol_c object in the intermediate parse tree,
lbessard@70: * more specifically, so it can be used as any other il operand.
lbessard@70: * This makes the rest of the code much easier...
lbessard@70: *
lbessard@70: * Nevertheless, the basic visitor class visitor_c does not know
lbessard@70: * how to visit this new il_default_variable_c class, so we have
lbessard@70: * to extend that too.
lbessard@70: * In reality extending the basic symbols doesn't quite work out
lbessard@70: * as cleanly as desired (we need to use dynamic_cast in the
lbessard@70: * accept method of the il_default_variable_c), but it is cleaner
lbessard@70: * than the alternative...
lbessard@70: */
lbessard@70: class il_default_variable_c;
lbessard@70:
lbessard@70: /* This visitor class is not really required, we could place the
lbessard@70: * visit() method directly in genertae_cc_il_c, but doing it in
lbessard@70: * a seperate class makes the architecture more evident...
lbessard@70: */
lbessard@70: class il_default_variable_visitor_c {
lbessard@70: public:
lbessard@70: virtual void *visit(il_default_variable_c *symbol) = 0;
lbessard@70:
lbessard@70: virtual ~il_default_variable_visitor_c(void) {return;}
lbessard@70: };
lbessard@70:
lbessard@70:
lbessard@70: /* A class to print out to the resulting C++ code
lbessard@70: * the IL default variable name.
lbessard@70: *
lbessard@70: * It includes a reference to its name,
lbessard@70: * and the data type of the data currently stored
lbessard@70: * in this C++ variable... This is required because the
lbessard@70: * C++ variable is a union, and we must know which member
lbessard@70: * of the union top reference!!
lbessard@70: *
lbessard@70: * Note that we also need to keep track of the data type of
lbessard@70: * the value currently being stored in the default variable.
lbessard@70: * This is required so we can process parenthesis,
lbessard@70: *
lbessard@70: * e.g. :
lbessard@70: * LD var1
lbessard@70: * AND (
lbessard@70: * LD var2
lbessard@70: * OR var3
lbessard@70: * )
lbessard@70: *
lbessard@70: * Note that we only execute the 'AND (' operation when we come across
lbessard@70: * the ')', i.e. once we have evaluated the result of the
lbessard@70: * instructions inside the parenthesis.
lbessard@70: * When we do execute the 'AND (' operation, we need to know the data type
lbessard@70: * of the operand, which in this case is the result of the evaluation of the
lbessard@70: * instruction list inside the parenthesis. We can only know this if we
lbessard@70: * keep track of the data type currently stored in the default variable!
lbessard@70: *
lbessard@70: * We use the current_type inside the generate_c_il::default_variable_name variable
lbessard@70: * to track this!
lbessard@70: */
lbessard@70: class il_default_variable_c: public symbol_c {
lbessard@70: public:
lbessard@70: symbol_c *var_name; /* in principle, this should point to an indentifier_c */
lbessard@70: symbol_c *current_type;
lbessard@70:
lbessard@70: public:
lbessard@70: il_default_variable_c(const char *var_name_str, symbol_c *current_type);
lbessard@70: virtual void *accept(visitor_c &visitor);
lbessard@70: };
lbessard@70:
lbessard@70:
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70: /***********************************************************************/
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: class generate_c_il_c: public generate_c_typedecl_c, il_default_variable_visitor_c {
lbessard@70:
laurent@217: public:
laurent@217: typedef enum {
laurent@217: expression_vg,
laurent@217: assignment_vg,
berem@228: complextype_base_vg,
laurent@392: complextype_base_assignment_vg,
berem@228: complextype_suffix_vg,
laurent@217: fparam_output_vg
laurent@217: } variablegeneration_t;
laurent@217:
lbessard@70: private:
lbessard@70: /* When compiling il code, it becomes necessary to determine the
lbessard@70: * data type of il operands. To do this, we must first find the
lbessard@70: * il operand's declaration, within the scope of the function block
lbessard@70: * or function currently being processed.
lbessard@70: * The following object does just that...
lbessard@70: * This object instance will then later be called while the
lbessard@70: * remaining il code is being handled.
lbessard@70: */
lbessard@70: search_expression_type_c *search_expression_type;
lbessard@70:
lbessard@70: /* The initial value that should be given to the IL default variable
lbessard@70: * imediately after a parenthesis is opened.
lbessard@70: * This variable is only used to pass data from the
lbessard@70: * il_expression_c visitor to the simple_instr_list_c visitor.
lbessard@70: *
lbessard@70: * e.g.:
lbessard@70: * LD var1
lbessard@70: * AND ( var2
lbessard@70: * OR var3
lbessard@70: * )
lbessard@70: *
lbessard@70: * In the above code sample, the line 'AND ( var2' constitutes
lbessard@70: * an il_expression_c, where var2 should be loaded into the
lbessard@70: * il default variable before continuing with the expression
lbessard@70: * inside the parenthesis.
lbessard@70: * Unfortunately, only the simple_instr_list_c may do the
lbessard@70: * initial laoding of the var2 bariable following the parenthesis,
lbessard@70: * so the il_expression_c visitor will have to pass 'var2' as a
lbessard@70: * parameter to the simple_instr_list_c visitor.
lbessard@70: * Ergo, the existance of the following parameter...!
lbessard@70: */
lbessard@70: symbol_c *il_default_variable_init_value;
lbessard@70:
lbessard@70: /* Operand to the IL operation currently being processed... */
lbessard@70: /* These variables are used to pass data from the
lbessard@70: * il_simple_operation_c and il_expression_c visitors
lbessard@70: * to the il operator visitors (i.e. LD_operator_c,
lbessard@70: * LDN_operator_c, ST_operator_c, STN_operator_c, ...)
lbessard@70: */
lbessard@70: symbol_c *current_operand;
lbessard@70: symbol_c *current_operand_type;
lbessard@70:
lbessard@70: /* Label to which the current IL jump operation should jump to... */
lbessard@70: /* This variable is used to pass data from the
lbessard@70: * il_jump_operation_c visitor
lbessard@70: * to the il jump operator visitors (i.e. JMP_operator_c,
lbessard@70: * JMPC_operator_c, JMPCN_operator_c, ...)
lbessard@70: */
lbessard@70: symbol_c *jump_label;
lbessard@70:
lbessard@70: /* The result of the comparison IL operations (GT, EQ, LT, ...)
lbessard@70: * is a boolean variable.
lbessard@70: * This class keeps track of the current data type stored in the
lbessard@70: * il default variable. This is usually done by keeping a reference
lbessard@70: * to the data type of the last operand. Nevertheless, in the case of
lbessard@70: * the comparison IL operators, the data type of the result (a boolean)
lbessard@70: * is not the data type of the operand. We therefore need an object
lbessard@70: * of the boolean data type to keep as a reference of the current
lbessard@70: * data type.
lbessard@70: * The following object is it...
lbessard@70: */
lbessard@70: bool_type_name_c bool_type;
laurent@210: lint_type_name_c lint_type;
laurent@210: lword_type_name_c lword_type;
laurent@210: lreal_type_name_c lreal_type;
lbessard@70:
lbessard@70: /* the data type of the IL default variable... */
lbessard@70: #define IL_DEFVAR_T VAR_LEADER "IL_DEFVAR_T"
lbessard@70: /* The name of the IL default variable... */
lbessard@70: #define IL_DEFVAR VAR_LEADER "IL_DEFVAR"
lbessard@70: /* The name of the variable used to pass the result of a
lbessard@70: * parenthesised instruction list to the immediately preceding
lbessard@70: * scope ...
lbessard@70: */
lbessard@70: #define IL_DEFVAR_BACK VAR_LEADER "IL_DEFVAR_BACK"
lbessard@70: il_default_variable_c default_variable_name;
lbessard@70: il_default_variable_c default_variable_back_name;
lbessard@70:
lbessard@70: /* When calling a function block, we must first find it's type,
lbessard@70: * by searching through the declarations of the variables currently
lbessard@70: * in scope.
lbessard@70: * This class does just that...
lbessard@70: * A new class is instantiated whenever we begin generating the code
lbessard@70: * for a function block type declaration, or a program declaration.
lbessard@70: * This object instance will then later be called while the
lbessard@70: * function block's or the program's body is being handled.
lbessard@70: *
lbessard@70: * Note that functions cannot contain calls to function blocks,
lbessard@70: * so we do not create an object instance when handling
lbessard@70: * a function declaration.
lbessard@70: */
lbessard@70: search_fb_instance_decl_c *search_fb_instance_decl;
lbessard@70:
lbessard@98: search_varfb_instance_type_c *search_varfb_instance_type;
msousa@505: search_var_instance_decl_c *search_var_instance_decl;
lbessard@98:
lbessard@98: search_base_type_c search_base_type;
lbessard@70:
berem@228: symbol_c* current_array_type;
laurent@235: symbol_c* current_param_type;
berem@228:
laurent@217: int fcall_number;
laurent@217: symbol_c *fbname;
laurent@217:
laurent@217: variablegeneration_t wanted_variablegeneration;
lbessard@146:
lbessard@70: public:
laurent@217: generate_c_il_c(stage4out_c *s4o_ptr, symbol_c *name, symbol_c *scope, const char *variable_prefix = NULL)
lbessard@70: : generate_c_typedecl_c(s4o_ptr),
lbessard@70: default_variable_name(IL_DEFVAR, NULL),
lbessard@70: default_variable_back_name(IL_DEFVAR_BACK, NULL)
lbessard@70: {
lbessard@70: search_expression_type = new search_expression_type_c(scope);
lbessard@70: search_fb_instance_decl = new search_fb_instance_decl_c(scope);
lbessard@98: search_varfb_instance_type = new search_varfb_instance_type_c(scope);
msousa@505: search_var_instance_decl = new search_var_instance_decl_c(scope);
msousa@505:
lbessard@70: current_operand = NULL;
lbessard@70: current_operand_type = NULL;
lbessard@70: il_default_variable_init_value = NULL;
berem@228: 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: this->set_variable_prefix(variable_prefix);
lbessard@70: }
lbessard@70:
lbessard@70: virtual ~generate_c_il_c(void) {
lbessard@70: delete search_fb_instance_decl;
lbessard@70: delete search_expression_type;
lbessard@98: delete search_varfb_instance_type;
msousa@505: delete search_var_instance_decl;
lbessard@70: }
lbessard@70:
lbessard@70: void generate(instruction_list_c *il) {
lbessard@70: il->accept(*this);
lbessard@70: }
lbessard@70:
lbessard@70: /* Declare the backup to the default variable, that will store the result
lbessard@70: * of the IL operations executed inside a parenthesis...
lbessard@70: */
lbessard@70: void declare_backup_variable(void) {
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: s4o.print(IL_DEFVAR_T);
lbessard@70: s4o.print(" ");
lbessard@70: print_backup_variable();
lbessard@70: s4o.print(";\n");
lbessard@70: }
lbessard@70:
lbessard@70: void print_backup_variable(void) {
lbessard@70: this->default_variable_back_name.accept(*this);
lbessard@70: }
lbessard@70:
laurent@392: void reset_default_variable_name(void) {
laurent@392: this->default_variable_name.current_type = NULL;
laurent@392: this->default_variable_back_name.current_type = NULL;
laurent@392: }
laurent@392:
lbessard@70: private:
lbessard@70: /* A helper function... */
lbessard@70: /*
lbessard@70: bool is_bool_type(symbol_c *type_symbol) {
lbessard@70: return (NULL != dynamic_cast(type_symbol));
lbessard@70: }
lbessard@70: */
lbessard@70:
lbessard@70: /* A helper function... */
lbessard@70: void *XXX_operator(symbol_c *lo, const char *op, symbol_c *ro) {
lbessard@70: if ((NULL == lo) || (NULL == ro)) ERROR;
lbessard@70: if (NULL == op) ERROR;
lbessard@70:
lbessard@70: lo->accept(*this);
lbessard@70: s4o.print(op);
lbessard@70: ro->accept(*this);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* A helper function... */
lbessard@70: void *XXX_function(const char *func, symbol_c *lo, symbol_c *ro) {
lbessard@70: if ((NULL == lo) || (NULL == ro)) ERROR;
lbessard@70: if (NULL == func) ERROR;
lbessard@70:
lbessard@70: lo->accept(*this);
lbessard@70: s4o.print(" = ");
lbessard@70: s4o.print(func);
lbessard@70: s4o.print("(");
lbessard@70: lo->accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: ro->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* A helper function... */
lbessard@70: void *XXX_CAL_operator(const char *param_name, symbol_c *fb_name) {
laurent@382: if (wanted_variablegeneration != expression_vg) {
laurent@382: s4o.print(param_name);
laurent@382: return NULL;
laurent@382: }
laurent@382:
lbessard@70: if (NULL == fb_name) ERROR;
lbessard@70: symbolic_variable_c *sv = dynamic_cast(fb_name);
lbessard@70: if (NULL == sv) ERROR;
lbessard@70: identifier_c *id = dynamic_cast(sv->var_name);
lbessard@70: if (NULL == id) ERROR;
ccb@202:
lbessard@70: identifier_c param(param_name);
lbessard@70:
lbessard@70: //SYM_REF3(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list)
ccb@202: il_assign_operator_c il_assign_operator(¶m);
ccb@202: il_param_assignment_c il_param_assignment(&il_assign_operator, &this->default_variable_name, NULL);
lbessard@70: // SYM_LIST(il_param_list_c)
ccb@202: il_param_list_c il_param_list;
lbessard@70: il_param_list.add_element(&il_param_assignment);
lbessard@70: CAL_operator_c CAL_operator;
lbessard@70: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
lbessard@70: il_fb_call_c il_fb_call(&CAL_operator, id, NULL, &il_param_list);
lbessard@70:
lbessard@70: il_fb_call.accept(*this);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* A helper function... */
lbessard@70: void *CMP_operator(symbol_c *o, const char *operation) {
lbessard@70: if (NULL == o) ERROR;
lbessard@70: if (NULL == this->default_variable_name.current_type) ERROR;
lbessard@70:
lbessard@70: symbol_c *backup = this->default_variable_name.current_type;
lbessard@70: this->default_variable_name.current_type = &(this->bool_type);
lbessard@70: this->default_variable_name.accept(*this);
lbessard@70: this->default_variable_name.current_type = backup;
lbessard@70:
lbessard@70: s4o.print(" = ");
lbessard@70: s4o.print(operation);
lbessard@70: this->default_variable_name.current_type->accept(*this);
lbessard@149: s4o.print("(__BOOL_LITERAL(TRUE), NULL, 2, ");
lbessard@70: this->default_variable_name.accept(*this);
lbessard@70: s4o.print(", ");
lbessard@70: o->accept(*this);
lbessard@70: s4o.print(")");
lbessard@70:
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = &(this->bool_type);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* A helper function... */
lbessard@70: void C_modifier(void) {
lbessard@70: if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
lbessard@70: s4o.print("if (");
lbessard@70: this->default_variable_name.accept(*this);
lbessard@70: s4o.print(") ");
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: }
lbessard@70:
lbessard@70: /* A helper function... */
lbessard@70: void CN_modifier(void) {
lbessard@70: if (search_expression_type->is_bool_type(default_variable_name.current_type)) {
lbessard@70: s4o.print("if (!");
lbessard@70: this->default_variable_name.accept(*this);
lbessard@70: s4o.print(") ");
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: }
lbessard@70:
laurent@210: void BYTE_operator_result_type(void) {
laurent@210: if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type)) {
laurent@210: if (search_expression_type->is_literal_integer_type(this->current_operand_type))
laurent@210: this->default_variable_name.current_type = &(this->lword_type);
laurent@210: else
laurent@210: this->default_variable_name.current_type = this->current_operand_type;
laurent@210: }
laurent@210: else if (search_expression_type->is_literal_integer_type(this->current_operand_type))
laurent@210: this->current_operand_type = this->default_variable_name.current_type;
laurent@210: }
laurent@210:
laurent@210: void NUM_operator_result_type(void) {
laurent@210: if (search_expression_type->is_literal_real_type(this->default_variable_name.current_type)) {
laurent@210: if (search_expression_type->is_literal_integer_type(this->current_operand_type) ||
laurent@210: search_expression_type->is_literal_real_type(this->current_operand_type))
laurent@210: this->default_variable_name.current_type = &(this->lreal_type);
laurent@210: else
laurent@210: this->default_variable_name.current_type = this->current_operand_type;
laurent@210: }
laurent@210: else if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type)) {
laurent@210: if (search_expression_type->is_literal_integer_type(this->current_operand_type))
laurent@210: this->default_variable_name.current_type = &(this->lint_type);
laurent@210: else if (search_expression_type->is_literal_real_type(this->current_operand_type))
laurent@210: this->default_variable_name.current_type = &(this->lreal_type);
laurent@210: else
laurent@210: this->default_variable_name.current_type = this->current_operand_type;
laurent@210: }
laurent@210: else if (search_expression_type->is_literal_integer_type(this->current_operand_type) ||
laurent@210: search_expression_type->is_literal_real_type(this->current_operand_type))
laurent@210: this->current_operand_type = this->default_variable_name.current_type;
laurent@210: }
laurent@210:
berem@228: void *print_getter(symbol_c *symbol) {
msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
laurent@235: if (wanted_variablegeneration == fparam_output_vg) {
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) {
Laurent@706: if (search_var_instance_decl->type_is_fb(symbol))
Laurent@706: s4o.print(GET_EXTERNAL_FB_BY_REF);
Laurent@706: else
Laurent@706: s4o.print(GET_EXTERNAL_BY_REF);
Laurent@706: }
laurent@235: else if (vartype == search_var_instance_decl_c::located_vt)
laurent@235: s4o.print(GET_LOCATED_BY_REF);
laurent@235: else
laurent@235: s4o.print(GET_VAR_BY_REF);
laurent@235: }
laurent@235: else {
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) {
Laurent@706: if (search_var_instance_decl->type_is_fb(symbol))
Laurent@706: s4o.print(GET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(GET_EXTERNAL);
Laurent@706: }
Laurent@706: else if (vartype == search_var_instance_decl_c::located_vt)
Laurent@706: s4o.print(GET_LOCATED);
Laurent@706: else
Laurent@706: s4o.print(GET_VAR);
laurent@235: }
berem@228: s4o.print("(");
berem@228:
laurent@235: variablegeneration_t old_wanted_variablegeneration = wanted_variablegeneration;
berem@228: wanted_variablegeneration = complextype_base_vg;
berem@228: symbol->accept(*this);
msousa@531: if (search_var_instance_decl->type_is_complex(symbol))
laurent@235: s4o.print(",");
berem@228: wanted_variablegeneration = complextype_suffix_vg;
berem@228: symbol->accept(*this);
berem@228: s4o.print(")");
laurent@235: wanted_variablegeneration = old_wanted_variablegeneration;
berem@228: return NULL;
berem@228: }
berem@228:
berem@228: 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,
Laurent@706: bool negative = false) {
Laurent@706:
Laurent@706: bool type_is_complex = search_var_instance_decl->type_is_complex(symbol);
laurent@405: if (fb_symbol == NULL) {
msousa@505: unsigned int vartype = search_var_instance_decl->get_vartype(symbol);
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt) {
Laurent@706: if (search_var_instance_decl->type_is_fb(symbol))
Laurent@706: s4o.print(SET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(SET_EXTERNAL);
Laurent@706: }
laurent@405: else if (vartype == search_var_instance_decl_c::located_vt)
laurent@405: s4o.print(SET_LOCATED);
laurent@405: else
laurent@405: s4o.print(SET_VAR);
laurent@405: }
Laurent@706: else {
Laurent@706: unsigned int vartype = search_var_instance_decl->get_vartype(fb_symbol);
Laurent@706: if (vartype == search_var_instance_decl_c::external_vt)
Laurent@706: s4o.print(SET_EXTERNAL_FB);
Laurent@706: else
Laurent@706: s4o.print(SET_VAR);
Laurent@706: }
laurent@392: s4o.print("(");
berem@228:
berem@228: if (fb_symbol != NULL) {
berem@228: print_variable_prefix();
berem@228: fb_symbol->accept(*this);
laurent@392: s4o.print(".,");
berem@228: }
laurent@382: else if (type_is_complex)
laurent@392: wanted_variablegeneration = complextype_base_assignment_vg;
berem@228: else
laurent@382: wanted_variablegeneration = assignment_vg;
laurent@382:
berem@228: symbol->accept(*this);
berem@228: s4o.print(",");
berem@228: if (negative) {
Laurent@706: if (search_expression_type->is_bool_type(this->current_operand_type))
Laurent@706: s4o.print("!");
Laurent@706: else
Laurent@706: s4o.print("~");
berem@228: }
berem@228: wanted_variablegeneration = expression_vg;
berem@228: print_check_function(type, value, fb_value);
laurent@382: if (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: }
lbessard@70:
lbessard@70: public:
lbessard@70: void *visit(il_default_variable_c *symbol) {
lbessard@70: symbol->var_name->accept(*this);
lbessard@70: if (NULL != symbol->current_type) {
lbessard@70: s4o.print(".");
agraeper@563: if ( search_expression_type->is_literal_integer_type(symbol->current_type)) this->lint_type.accept(*this);
agraeper@563: else if ( search_expression_type->is_literal_real_type(this->default_variable_name.current_type)) this->lreal_type.accept(*this);
agraeper@563: else if ( search_expression_type->is_bool_type(this->default_variable_name.current_type)) this->bool_type.accept(*this);
agraeper@563: else symbol->current_type->accept(*this);
lbessard@70: s4o.print("var");
agraeper@563: } return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: private:
lbessard@70:
ccb@202: #if 0
ccb@202: I NEED TO FIX THIS!!!
ccb@202: TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
lbessard@146: void *visit(eno_param_c *symbol) {
lbessard@146: if (this->is_variable_prefix_null()) {
lbessard@146: s4o.print("*");
lbessard@146: }
lbessard@146: else {
lbessard@146: this->print_variable_prefix();
lbessard@146: }
lbessard@146: s4o.print("ENO");
lbessard@146: return NULL;
lbessard@146: }
ccb@202: TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
ccb@202: #endif
ccb@202:
lbessard@146:
laurent@377: /********************************/
laurent@377: /* B 1.3.3 - Derived data types */
laurent@377: /********************************/
laurent@377:
laurent@382: /* signed_integer DOTDOT signed_integer */
laurent@382: void *visit(subrange_c *symbol) {
laurent@382: symbol->lower_limit->accept(*this);
laurent@382: return NULL;
laurent@382: }
laurent@382:
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:
lbessard@146: /*********************/
lbessard@146: /* B 1.4 - Variables */
lbessard@146: /*********************/
laurent@221:
lbessard@146: void *visit(symbolic_variable_c *symbol) {
berem@228: unsigned int vartype;
laurent@382: switch (wanted_variablegeneration) {
laurent@392: case complextype_base_assignment_vg:
laurent@392: case assignment_vg:
laurent@392: this->print_variable_prefix();
laurent@392: s4o.print(",");
laurent@392: symbol->var_name->accept(*this);
laurent@392: break;
laurent@382: case complextype_base_vg:
laurent@221: generate_c_base_c::visit(symbol);
laurent@382: break;
laurent@382: case complextype_suffix_vg:
Laurent@706: break;
laurent@382: default:
laurent@382: if (this->is_variable_prefix_null()) {
Laurent@706: vartype = search_var_instance_decl->get_vartype(symbol);
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@146: return NULL;
lbessard@146: }
lbessard@146:
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@146: }
lbessard@70: this->print_variable_prefix();
lbessard@70: s4o.printlocation(symbol->value + 1);
laurent@217: if ((this->is_variable_prefix_null() && wanted_variablegeneration != fparam_output_vg) ||
Laurent@706: wanted_variablegeneration != assignment_vg)
lbessard@146: s4o.print(")");
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
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");
Laurent@625: bool type_is_complex = search_var_instance_decl->type_is_complex(symbol->record_variable);
berem@228: switch (wanted_variablegeneration) {
berem@228: case complextype_base_vg:
laurent@392: case complextype_base_assignment_vg:
berem@228: symbol->record_variable->accept(*this);
Laurent@410: if (!type_is_complex) {
Laurent@410: s4o.print(".");
Laurent@410: symbol->field_selector->accept(*this);
Laurent@410: }
berem@228: break;
berem@228: case complextype_suffix_vg:
Laurent@706: symbol->record_variable->accept(*this);
Laurent@706: if (type_is_complex) {
Laurent@706: s4o.print(".");
Laurent@706: symbol->field_selector->accept(*this);
Laurent@706: }
Laurent@706: break;
Laurent@706: case assignment_vg:
Laurent@706: symbol->record_variable->accept(*this);
Laurent@706: s4o.print(".");
Laurent@706: symbol->field_selector->accept(*this);
Laurent@706: break;
berem@228: default:
berem@228: if (this->is_variable_prefix_null()) {
Laurent@706: symbol->record_variable->accept(*this);
Laurent@706: s4o.print(".");
Laurent@706: symbol->field_selector->accept(*this);
berem@228: }
berem@228: else
Laurent@706: 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:
laurent@392: case complextype_base_assignment_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:
berem@228: if (this->is_variable_prefix_null()) {
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: }
berem@228: else
Laurent@706: print_getter(symbol);
berem@228: break;
berem@228: }
berem@228: return NULL;
berem@228: }
berem@228:
berem@228: /* subscript_list ',' subscript */
berem@228: void *visit(subscript_list_c *symbol) {
laurent@377: array_dimension_iterator_c* array_dimension_iterator = new array_dimension_iterator_c(current_array_type);
berem@228: 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("[(");
berem@228: symbol->elements[i]->accept(*this);
laurent@377: s4o.print(") - (");
laurent@377: dimension->accept(*this);
berem@228: s4o.print(")]");
berem@228: }
laurent@377: delete array_dimension_iterator;
berem@228: return NULL;
berem@228: }
berem@228:
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: }
laurent@235:
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: }
lbessard@70: /****************************************/
lbessard@70: /* B.2 - Language IL (Instruction List) */
lbessard@70: /****************************************/
lbessard@70:
lbessard@70: /***********************************/
lbessard@70: /* B 2.1 Instructions and Operands */
lbessard@70: /***********************************/
lbessard@70:
lbessard@70: /*| instruction_list il_instruction */
lbessard@70: void *visit(instruction_list_c *symbol) {
lbessard@70:
lbessard@70: /* Declare the backup to the default variable, that will store the result
lbessard@70: * of the IL operations executed inside a parenthesis...
lbessard@70: */
lbessard@70: declare_backup_variable();
lbessard@70:
lbessard@70: /* Declare the default variable, that will store the result of the IL operations... */
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: s4o.print(IL_DEFVAR_T);
lbessard@70: s4o.print(" ");
lbessard@70: this->default_variable_name.accept(*this);
laurent@211: s4o.print(";\n");
laurent@211: s4o.print(s4o.indent_spaces);
laurent@211: print_backup_variable();
laurent@211: s4o.print(".INTvar = 0;\n\n");
lbessard@70:
lbessard@70: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* | label ':' [il_incomplete_instruction] eol_list */
lbessard@70: // SYM_REF2(il_instruction_c, label, il_instruction)
lbessard@70: void *visit(il_instruction_c *symbol) {
lbessard@70: if (NULL != symbol->label) {
lbessard@70: symbol->label->accept(*this);
lbessard@70: s4o.print(":\n");
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: }
msousa@311: if (NULL != symbol->il_instruction) {
msousa@311: symbol->il_instruction->accept(*this);
msousa@311: }
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* | il_simple_operator [il_operand] */
lbessard@70: //SYM_REF2(il_simple_operation_c, il_simple_operator, il_operand)
lbessard@70: void *visit(il_simple_operation_c *symbol) {
lbessard@70: this->current_operand = symbol->il_operand;
lbessard@70: if (NULL == this->current_operand) {
lbessard@70: this->current_operand_type = NULL;
lbessard@70: } else {
lbessard@70: this->current_operand_type = search_expression_type->get_type(this->current_operand);
lbessard@70: if (NULL == this->current_operand_type) ERROR;
lbessard@70: }
lbessard@70:
lbessard@70: symbol->il_simple_operator->accept(*this);
lbessard@70:
lbessard@70: this->current_operand = NULL;
lbessard@70: this->current_operand_type = NULL;
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* | function_name [il_operand_list] */
lbessard@70: // SYM_REF2(il_function_call_c, function_name, il_operand_list)
lbessard@70: void *visit(il_function_call_c *symbol) {
lbessard@149: symbol_c* function_type_prefix = NULL;
lbessard@149: symbol_c* function_name = NULL;
lbessard@149: symbol_c* function_type_suffix = NULL;
laurent@217: DECLARE_PARAM_LIST()
lbessard@149:
lbessard@149: symbol_c *param_data_type = default_variable_name.current_type;
lbessard@149: symbol_c *return_data_type = NULL;
lbessard@149:
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: /* determine the base data type returned by the function being called... */
msousa@350: search_base_type_c search_base_type;
msousa@350: return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
msousa@350: if (NULL == return_data_type) 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 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: }
lbessard@70:
msousa@350: symbol_c *param_type = fp_iterator.param_type();
msousa@350: if (param_type == NULL) ERROR;
lbessard@70:
msousa@350: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
lbessard@149:
msousa@350: symbol_c *param_value = NULL;
msousa@350:
msousa@350: /* Get the value from a foo( = ) style call */
msousa@350: /* NOTE: Since the class il_function_call_c only references a non.formal function call,
msousa@350: * the following line of code is not required in this case. However, 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: }
lbessard@149:
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: }
lbessard@149:
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:
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@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;
laurent@217:
lbessard@149: default_variable_name.current_type = return_data_type;
lbessard@149: this->default_variable_name.accept(*this);
lbessard@149: default_variable_name.current_type = param_data_type;
lbessard@149: s4o.print(" = ");
lbessard@149:
lbessard@149: if (function_type_prefix != NULL) {
lbessard@149: s4o.print("(");
laurent@336: search_expression_type->default_literal_type(function_type_prefix)->accept(*this);
lbessard@149: s4o.print(")");
lbessard@149: }
laurent@336: if (function_type_suffix != NULL) {
Laurent@706: function_type_suffix = search_expression_type->default_literal_type(function_type_suffix);
laurent@336: }
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@350: if (fdecl_mutiplicity == 2) {
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 {
msousa@350: if (function_name != NULL) {
msousa@350: function_name->accept(*this);
msousa@350: if (fdecl_mutiplicity == 2) {
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@706: }
laurent@217: if (function_type_suffix != NULL)
msousa@350: function_type_suffix->accept(*this);
laurent@217: }
lbessard@149: s4o.print("(");
lbessard@149: s4o.indent_right();
lbessard@149:
laurent@217: int nb_param = 0;
laurent@217: PARAM_LIST_ITERATOR() {
laurent@217: symbol_c *param_value = PARAM_VALUE;
laurent@235: current_param_type = PARAM_TYPE;
lbessard@149:
laurent@217: switch (PARAM_DIRECTION) {
lbessard@149: case function_param_iterator_c::direction_in:
msousa@350: if (nb_param > 0)
msousa@350: s4o.print(",\n"+s4o.indent_spaces);
lbessard@149: if (param_value == NULL) {
lbessard@149: /* If not, get the default value of this variable's type */
laurent@235: param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
lbessard@149: }
lbessard@149: if (param_value == NULL) ERROR;
laurent@217: s4o.print("(");
laurent@235: if (search_expression_type->is_literal_integer_type(current_param_type))
laurent@220: search_expression_type->lint_type_name.accept(*this);
laurent@235: else if (search_expression_type->is_literal_real_type(current_param_type))
laurent@220: search_expression_type->lreal_type_name.accept(*this);
laurent@220: else
laurent@235: current_param_type->accept(*this);
laurent@217: 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:
msousa@350: if (!has_output_params) {
laurent@217: if (nb_param > 0)
msousa@350: s4o.print(",\n"+s4o.indent_spaces);
msousa@350: if (param_value == NULL) {
msousa@350: s4o.print("NULL");
msousa@350: } else {
msousa@350: wanted_variablegeneration = fparam_output_vg;
msousa@350: param_value->accept(*this);
msousa@350: wanted_variablegeneration = expression_vg;
msousa@350: }
msousa@350: nb_param++;
msousa@350: }
lbessard@149: break;
lbessard@149: case function_param_iterator_c::direction_extref:
lbessard@149: /* TODO! */
lbessard@149: ERROR;
lbessard@149: break;
lbessard@149: } /* switch */
lbessard@149: }
laurent@217: if (has_output_params) {
laurent@217: if (nb_param > 0)
msousa@350: s4o.print(",\n"+s4o.indent_spaces);
laurent@217: s4o.print(FB_FUNCTION_PARAM);
laurent@217: }
lbessard@149:
lbessard@149: s4o.print(")");
lbessard@149: /* the data type returned by the function, and stored in the il default variable... */
lbessard@149: default_variable_name.current_type = return_data_type;
laurent@217:
laurent@217: CLEAR_PARAM_LIST()
laurent@217:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* | il_expr_operator '(' [il_operand] eol_list [simple_instr_list] ')' */
lbessard@70: //SYM_REF4(il_expression_c, il_expr_operator, il_operand, simple_instr_list, unused)
lbessard@70: void *visit(il_expression_c *symbol) {
lbessard@70: /* We will be recursevely interpreting an instruction list,
lbessard@70: * so we store a backup of the data type of the value currently stored
lbessard@70: * in the default variable, and set the current data type to NULL
lbessard@70: */
lbessard@70: symbol_c *old_current_default_variable_data_type = this->default_variable_name.current_type;
lbessard@70: this->default_variable_name.current_type = NULL;
lbessard@70:
lbessard@70: /* Pass the symbol->il_operand to the simple_instr_list visitor
lbessard@70: * using the il_default_variable_init_value parameter...
lbessard@70: * Note that the simple_instr_list_c visitor will set this parameter
lbessard@70: * to NULL as soon as it does not require it any longer,
lbessard@70: * so we don't do it here again after the
lbessard@70: * symbol->simple_instr_list->accept(*this);
lbessard@70: * returns...
lbessard@70: */
lbessard@70: this->il_default_variable_init_value = symbol->il_operand;
lbessard@70:
lbessard@70: /* Now do the parenthesised instructions... */
lbessard@70: /* NOTE: the following code line will get the variable
lbessard@70: * this->default_variable_name.current_type updated!
lbessard@70: */
lbessard@70: symbol->simple_instr_list->accept(*this);
lbessard@70:
lbessard@70: /* Now do the operation, using the previous result! */
lbessard@70: /* NOTE: The result of the previous instruction list will be stored
lbessard@70: * in a variable named IL_DEFVAR_BACK. This is done in the visitor
lbessard@70: * to instruction_list_c objects...
lbessard@70: */
lbessard@70: this->current_operand = &(this->default_variable_back_name);
lbessard@70: this->current_operand_type = this->default_variable_back_name.current_type;
lbessard@70:
lbessard@70: this->default_variable_name.current_type = old_current_default_variable_data_type;
lbessard@70: if (NULL == this->current_operand_type) ERROR;
lbessard@70:
lbessard@70: symbol->il_expr_operator->accept(*this);
lbessard@70:
lbessard@70: this->current_operand = NULL;
lbessard@70: this->current_operand_type = NULL;
lbessard@70: this->default_variable_back_name.current_type = NULL;
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* il_jump_operator label */
lbessard@70: // SYM_REF2(il_jump_operation_c, il_jump_operator, label)
lbessard@70: void *visit(il_jump_operation_c *symbol) {
lbessard@70: /* Pass the symbol->label to the il_jump_operation visitor
lbessard@70: * using the jump_label parameter...
lbessard@70: */
lbessard@70: this->jump_label = symbol->label;
lbessard@70: symbol->il_jump_operator->accept(*this);
lbessard@70: this->jump_label = NULL;
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: /* il_call_operator prev_declared_fb_name
lbessard@70: * | il_call_operator prev_declared_fb_name '(' ')'
lbessard@70: * | il_call_operator prev_declared_fb_name '(' eol_list ')'
lbessard@70: * | il_call_operator prev_declared_fb_name '(' il_operand_list ')'
lbessard@70: * | il_call_operator prev_declared_fb_name '(' eol_list il_param_list ')'
lbessard@70: */
lbessard@70: // SYM_REF4(il_fb_call_c, il_call_operator, fb_name, il_operand_list, il_param_list)
lbessard@70: void *visit(il_fb_call_c *symbol) {
lbessard@70: symbol->il_call_operator->accept(*this);
lbessard@70: s4o.print("{\n");
lbessard@70: s4o.indent_right();
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70:
lbessard@70: /* first figure out what is the name of the function block type of the function block being called... */
lbessard@70: symbol_c *function_block_type_name = this->search_fb_instance_decl->get_type_name(symbol->fb_name);
lbessard@70: /* should never occur. The function block instance MUST have been declared... */
lbessard@70: if (function_block_type_name == NULL) ERROR;
lbessard@70:
lbessard@70: /* Now find the declaration of the function block type being called... */
lbessard@70: function_block_declaration_c *fb_decl = function_block_type_symtable.find_value(function_block_type_name);
lbessard@70: /* should never occur. The function block type being called MUST be in the symtable... */
lbessard@70: if (fb_decl == function_block_type_symtable.end_value()) ERROR;
lbessard@70:
lbessard@70: /* loop through each function block parameter, find the value we should pass
lbessard@70: * to it, and then output the c equivalent...
lbessard@70: */
lbessard@70: function_param_iterator_c fp_iterator(fb_decl);
lbessard@70: identifier_c *param_name;
lbessard@70: function_call_param_iterator_c function_call_param_iterator(symbol);
lbessard@70: for(int i = 1; (param_name = fp_iterator.next()) != NULL; i++) {
lbessard@70: function_param_iterator_c::param_direction_t param_direction = fp_iterator.param_direction();
lbessard@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@98: symbol_c *param_type = fp_iterator.param_type();
lbessard@98: if (param_type == NULL) ERROR;
lbessard@98:
lbessard@123: /* 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@706: if (this->is_variable_prefix_null()) {
Laurent@706: symbol->fb_name->accept(*this);
berem@228: s4o.print(".");
berem@228: param_name->accept(*this);
berem@228: s4o.print(" = ");
berem@228: print_check_function(param_type, param_value);
Laurent@706: }
berem@228: else {
berem@228: print_setter(param_name, param_type, param_value, symbol->fb_name);
berem@228: }
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("&");
laurent@240: 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);
berem@228: if (this->is_variable_prefix_null()) {
laurent@240: param_value->accept(*this);
Laurent@706: s4o.print(" = ");
Laurent@706: print_check_function(param_type, param_name, symbol->fb_name);
Laurent@706: }
Laurent@706: else {
Laurent@706: print_setter(param_value, param_type, param_name, NULL, symbol->fb_name);
Laurent@706: }
lbessard@70: }
lbessard@70: } /* for(...) */
lbessard@70:
lbessard@70: s4o.print(";\n");
lbessard@70: s4o.indent_left();
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: s4o.print("}");
lbessard@70:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: /* | function_name '(' eol_list [il_param_list] ')' */
lbessard@70: // SYM_REF2(il_formal_funct_call_c, function_name, il_param_list)
lbessard@70: void *visit(il_formal_funct_call_c *symbol) {
lbessard@149: symbol_c* function_type_prefix = NULL;
lbessard@149: symbol_c* function_name = NULL;
lbessard@149: symbol_c* function_type_suffix = NULL;
laurent@217: DECLARE_PARAM_LIST()
lbessard@149:
lbessard@149: symbol_c *return_data_type = NULL;
lbessard@149:
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: /* determine the base data type returned by the function being called... */
msousa@350: search_base_type_c search_base_type;
msousa@350: return_data_type = (symbol_c *)f_decl->type_name->accept(search_base_type);
msousa@350: if (NULL == return_data_type) 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: }
lbessard@149:
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: }
lbessard@149:
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: }
lbessard@169:
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: }
lbessard@149:
msousa@350: ADD_PARAM_LIST(param_name, param_value, param_type, fp_iterator.param_direction())
laurent@217: }
laurent@217:
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@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;
laurent@217:
lbessard@149: default_variable_name.current_type = return_data_type;
lbessard@149: this->default_variable_name.accept(*this);
lbessard@149: s4o.print(" = ");
lbessard@149:
lbessard@149: if (function_type_prefix != NULL) {
lbessard@149: s4o.print("(");
laurent@336: search_expression_type->default_literal_type(function_type_prefix)->accept(*this);
lbessard@149: s4o.print(")");
lbessard@149: }
laurent@336: if (function_type_suffix != NULL) {
Laurent@706: function_type_suffix = search_expression_type->default_literal_type(function_type_suffix);
laurent@336: }
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@350: if (fdecl_mutiplicity == 2) {
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 {
msousa@350: if (function_name != NULL) {
laurent@217: function_name->accept(*this);
msousa@350: if (fdecl_mutiplicity == 2) {
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@350: }
laurent@217: if (function_type_suffix != NULL)
laurent@217: function_type_suffix->accept(*this);
laurent@217: }
lbessard@70: s4o.print("(");
lbessard@149: s4o.indent_right();
lbessard@149:
laurent@217: int nb_param = 0;
laurent@217: PARAM_LIST_ITERATOR() {
msousa@350: symbol_c *param_value = PARAM_VALUE;
msousa@350: current_param_type = PARAM_TYPE;
laurent@217: switch (PARAM_DIRECTION) {
lbessard@70: case function_param_iterator_c::direction_in:
msousa@350: if (nb_param > 0)
msousa@350: s4o.print(",\n"+s4o.indent_spaces);
msousa@350: if (param_value == NULL) {
lbessard@70: /* If not, get the default value of this variable's type */
laurent@235: param_value = (symbol_c *)current_param_type->accept(*type_initial_value_c::instance());
lbessard@70: }
lbessard@70: if (param_value == NULL) ERROR;
laurent@217: s4o.print("(");
laurent@235: if (search_expression_type->is_literal_integer_type(current_param_type))
laurent@220: search_expression_type->lint_type_name.accept(*this);
laurent@235: else if (search_expression_type->is_literal_real_type(current_param_type))
laurent@220: search_expression_type->lreal_type_name.accept(*this);
laurent@220: else
laurent@235: current_param_type->accept(*this);
laurent@217: s4o.print(")");
laurent@235: print_check_function(current_param_type, param_value);
msousa@350: nb_param++;
laurent@217: break;
lbessard@70: case function_param_iterator_c::direction_out:
lbessard@70: case function_param_iterator_c::direction_inout:
msousa@350: if (!has_output_params) {
laurent@217: if (nb_param > 0)
msousa@350: s4o.print(",\n"+s4o.indent_spaces);
msousa@350: if (param_value == NULL) {
msousa@350: s4o.print("NULL");
msousa@350: } else {
msousa@350: wanted_variablegeneration = fparam_output_vg;
msousa@350: param_value->accept(*this);
msousa@350: wanted_variablegeneration = expression_vg;
msousa@350: }
msousa@350: }
msousa@350: break;
lbessard@70: case function_param_iterator_c::direction_extref:
lbessard@70: /* TODO! */
lbessard@70: ERROR;
msousa@350: break;
lbessard@70: } /* switch */
lbessard@70: } /* for(...) */
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@70:
lbessard@70: // symbol->parameter_assignment->accept(*this);
lbessard@70: s4o.print(")");
lbessard@149: /* the data type returned by the function, and stored in the il default variable... */
laurent@217:
laurent@217: CLEAR_PARAM_LIST()
laurent@217:
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: /* | il_operand_list ',' il_operand */
lbessard@70: // SYM_LIST(il_operand_list_c)
lbessard@70: void *visit(il_operand_list_c *symbol) {ERROR; return NULL;} // should never get called!
lbessard@70:
lbessard@70:
lbessard@70: /* | simple_instr_list il_simple_instruction */
lbessard@70: // SYM_LIST(simple_instr_list_c)
lbessard@70: void *visit(simple_instr_list_c *symbol) {
lbessard@70: /* A simple_instr_list_c is used to store a list of il operations
lbessard@70: * being done within parenthesis...
lbessard@70: *
lbessard@70: * e.g.:
lbessard@70: * LD var1
lbessard@70: * AND ( var2
lbessard@70: * OR var3
lbessard@70: * OR var4
lbessard@70: * )
lbessard@70: *
lbessard@70: * This will be converted to C++ by defining a new scope
lbessard@70: * with a new il default variable, and executing the il operands
lbessard@70: * within this new scope.
lbessard@70: * At the end of the scope the result, i.e. the value currently stored
lbessard@70: * in the il default variable is copied to the variable used to take this
lbessard@70: * value to the outside scope...
lbessard@70: *
lbessard@70: * The above example will result in the following C++ code:
lbessard@70: * {__IL_DEFVAR_T __IL_DEFVAR_BACK;
lbessard@70: * __IL_DEFVAR_T __IL_DEFVAR;
lbessard@70: *
lbessard@70: * __IL_DEFVAR.INTvar = var1;
lbessard@70: * {
lbessard@70: * __IL_DEFVAR_T __IL_DEFVAR;
lbessard@70: *
lbessard@70: * __IL_DEFVAR.INTvar = var2;
lbessard@70: * __IL_DEFVAR.INTvar |= var3;
lbessard@70: * __IL_DEFVAR.INTvar |= var4;
lbessard@70: *
lbessard@70: * __IL_DEFVAR_BACK = __IL_DEFVAR;
lbessard@70: * }
lbessard@70: * __IL_DEFVAR.INTvar &= __IL_DEFVAR_BACK.INTvar;
lbessard@70: *
lbessard@70: * }
lbessard@70: *
lbessard@70: * The intial value of the il default variable (in the above
lbessard@70: * example 'var2') is passed to this simple_instr_list_c visitor
lbessard@70: * using the il_default_variable_init_value parameter.
lbessard@70: * Since it is possible to have parenthesis inside other parenthesis
lbessard@70: * recursively, we reset the il_default_variable_init_value to NULL
lbessard@70: * as soon as we no longer require it, as it may be used once again
lbessard@70: * in the line
lbessard@70: * print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
lbessard@70: *
lbessard@70: */
lbessard@70:
lbessard@70: /* Declare the default variable, that will store the result of the IL operations... */
lbessard@70: s4o.print("{\n");
lbessard@70: s4o.indent_right();
lbessard@70:
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: s4o.print(IL_DEFVAR_T);
lbessard@70: s4o.print(" ");
lbessard@70: this->default_variable_name.accept(*this);
lbessard@70: s4o.print(";\n\n");
lbessard@70:
lbessard@70: /* Check whether we should initiliase the il default variable... */
lbessard@70: if (NULL != this->il_default_variable_init_value) {
lbessard@70: /* Yes, we must... */
lbessard@70: /* We will do it by instatiating a LD operator, and having this
lbessard@70: * same generate_c_il_c class visiting it!
lbessard@70: */
lbessard@70: LD_operator_c ld_oper;
lbessard@70: il_simple_operation_c il_simple_oper(&ld_oper, this->il_default_variable_init_value);
lbessard@70:
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: il_simple_oper.accept(*this);
lbessard@70: s4o.print(";\n");
lbessard@70: }
lbessard@70:
lbessard@70: /* this parameter no longer required... */
lbessard@70: this->il_default_variable_init_value = NULL;
lbessard@70:
lbessard@70: print_list(symbol, s4o.indent_spaces, ";\n" + s4o.indent_spaces, ";\n");
lbessard@70:
lbessard@70: /* copy the result in the default variable to the variable
lbessard@70: * used to pass the data out to the scope enclosing
lbessard@70: * the current scope!
lbessard@70: *
lbessard@70: * We also need to update the data type currently stored within
lbessard@70: * the variable used to pass the data to the outside scope...
lbessard@70: */
lbessard@70: this->default_variable_back_name.current_type = this->default_variable_name.current_type;
lbessard@70: s4o.print("\n");
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: this->default_variable_back_name.accept(*this);
lbessard@70: s4o.print(" = ");
lbessard@70: this->default_variable_name.accept(*this);
lbessard@70: s4o.print(";\n");
lbessard@70:
lbessard@70: s4o.indent_left();
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: s4o.print("}\n");
lbessard@70: s4o.print(s4o.indent_spaces);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
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@453: return symbol->il_simple_instruction->accept(*this);
msousa@453: }
msousa@453:
msousa@453:
lbessard@70: /* | il_initial_param_list il_param_instruction */
lbessard@70: // SYM_LIST(il_param_list_c)
lbessard@70: void *visit(il_param_list_c *symbol) {ERROR; return NULL;} // should never get called!
lbessard@70:
lbessard@70: /* il_assign_operator il_operand
lbessard@70: * | il_assign_operator '(' eol_list simple_instr_list ')'
lbessard@70: */
lbessard@70: // SYM_REF4(il_param_assignment_c, il_assign_operator, il_operand, simple_instr_list, unused)
lbessard@70: void *visit(il_param_assignment_c *symbol) {ERROR; return NULL;} // should never get called!
lbessard@70:
lbessard@70: /* il_assign_out_operator variable */
lbessard@70: // SYM_REF2(il_param_out_assignment_c, il_assign_out_operator, variable);
lbessard@70: void *visit(il_param_out_assignment_c *symbol) {ERROR; return NULL;} // should never get called!
lbessard@70:
lbessard@70: /*******************/
lbessard@70: /* B 2.2 Operators */
lbessard@70: /*******************/
lbessard@70:
Laurent@706: void *visit(LD_operator_c *symbol) {
laurent@382: if (wanted_variablegeneration != expression_vg) {
laurent@382: s4o.print("LD");
laurent@382: return NULL;
laurent@382: }
laurent@382:
lbessard@70: /* the data type resulting from this operation... */
laurent@217: this->default_variable_name.current_type = this->current_operand_type;
laurent@210: XXX_operator(&(this->default_variable_name), " = ", this->current_operand);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(LDN_operator_c *symbol) {
lbessard@70: /* the data type resulting from this operation... */
laurent@217: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: XXX_operator(&(this->default_variable_name),
lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" = !":" = ~",
lbessard@70: this->current_operand);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(ST_operator_c *symbol) {
msousa@321: symbol_c *operand_type = search_varfb_instance_type->get_type_id(this->current_operand);
berem@228: if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type) ||
Laurent@706: search_expression_type->is_literal_real_type(this->default_variable_name.current_type))
berem@228: this->default_variable_name.current_type = this->current_operand_type;
berem@228: if (this->is_variable_prefix_null()) {
laurent@217: this->current_operand->accept(*this);
laurent@217: s4o.print(" = ");
berem@228: print_check_function(operand_type, (symbol_c*)&(this->default_variable_name));
berem@228: }
berem@228: else {
Laurent@706: print_setter(this->current_operand, operand_type, (symbol_c*)&(this->default_variable_name));
berem@228: }
laurent@217: /* the data type resulting from this operation is unchanged. */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(STN_operator_c *symbol) {
msousa@321: symbol_c *operand_type = search_varfb_instance_type->get_type_id(this->current_operand);
berem@228: if (search_expression_type->is_literal_integer_type(this->default_variable_name.current_type))
Laurent@706: this->default_variable_name.current_type = this->current_operand_type;
berem@228:
berem@228: if (this->is_variable_prefix_null()) {
laurent@217: this->current_operand->accept(*this);
laurent@217: s4o.print(" = ");
berem@228: if (search_expression_type->is_bool_type(this->current_operand_type))
berem@228: s4o.print("!");
berem@228: else
Laurent@706: s4o.print("~");
berem@228: this->default_variable_name.accept(*this);
berem@228: }
berem@228: else {
Laurent@706: print_setter(this->current_operand, operand_type, (symbol_c*)&(this->default_variable_name), NULL, NULL, true);
berem@228: }
berem@228: /* the data type resulting from this operation is unchanged. */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(NOT_operator_c *symbol) {
msousa@470: /* NOTE: the standard allows syntax in which the NOT operator is followed by an optional
msousa@470: * NOT []
msousa@470: * However, it does not define the semantic of the NOT operation when the is specified.
msousa@470: * We therefore consider it an error if an il_operand is specified!
msousa@470: * The error is caught in stage 3!
msousa@470: */
lbessard@70: if ((NULL != this->current_operand) || (NULL != this->current_operand_type)) ERROR;
lbessard@70: XXX_operator(&(this->default_variable_name),
lbessard@70: search_expression_type->is_bool_type(this->default_variable_name.current_type)?" = !":" = ~",
lbessard@70: &(this->default_variable_name));
lbessard@70: /* the data type resulting from this operation is unchanged. */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(S_operator_c *symbol) {
laurent@382: if (wanted_variablegeneration != expression_vg) {
laurent@382: s4o.print("LD");
laurent@382: return NULL;
laurent@382: }
laurent@382:
lbessard@70: if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR;
lbessard@70:
lbessard@70: C_modifier();
lbessard@70: this->current_operand->accept(*this);
laurent@240: s4o.print(" = __");
laurent@240: if (search_expression_type->is_bool_type(this->current_operand_type))
laurent@240: s4o.print("BOOL_LITERAL(TRUE)");
laurent@240: else if (search_expression_type->is_integer_type(this->current_operand_type)) {
laurent@240: this->current_operand_type->accept(*this);
laurent@240: s4o.print("_LITERAL(1)");
laurent@240: }
laurent@240: else
laurent@240: ERROR;
lbessard@70: /* the data type resulting from this operation is unchanged! */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(R_operator_c *symbol) {
laurent@382: if (wanted_variablegeneration != expression_vg) {
laurent@382: s4o.print("LD");
laurent@382: return NULL;
laurent@382: }
laurent@382:
lbessard@70: if ((NULL == this->current_operand) || (NULL == this->current_operand_type)) ERROR;
lbessard@70:
lbessard@70: C_modifier();
lbessard@70: this->current_operand->accept(*this);
laurent@240: s4o.print(" = __");
laurent@240: if (search_expression_type->is_bool_type(this->current_operand_type))
laurent@240: s4o.print("BOOL_LITERAL(FALSE)");
laurent@240: else if (search_expression_type->is_integer_type(this->current_operand_type)) {
laurent@240: this->current_operand_type->accept(*this);
laurent@240: s4o.print("_LITERAL(0)");
laurent@240: }
laurent@240: else
laurent@240: ERROR;
lbessard@70: /* the data type resulting from this operation is unchanged! */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: void *visit(S1_operator_c *symbol) {return XXX_CAL_operator("S1", this->current_operand);}
lbessard@70: void *visit(R1_operator_c *symbol) {return XXX_CAL_operator("R1", this->current_operand);}
lbessard@70: void *visit(CLK_operator_c *symbol) {return XXX_CAL_operator("CLK", this->current_operand);}
lbessard@70: void *visit(CU_operator_c *symbol) {return XXX_CAL_operator("CU", this->current_operand);}
lbessard@70: void *visit(CD_operator_c *symbol) {return XXX_CAL_operator("CD", this->current_operand);}
lbessard@70: void *visit(PV_operator_c *symbol) {return XXX_CAL_operator("PV", this->current_operand);}
lbessard@70: void *visit(IN_operator_c *symbol) {return XXX_CAL_operator("IN", this->current_operand);}
lbessard@70: void *visit(PT_operator_c *symbol) {return XXX_CAL_operator("PT", this->current_operand);}
lbessard@70:
Laurent@706: void *visit(AND_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: BYTE_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name), " &= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(OR_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: BYTE_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name), " |= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(XOR_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: BYTE_operator_result_type();
Laurent@706: // '^' is a bit by bit exclusive OR !! Also seems to work with boolean types!
lbessard@70: XXX_operator(&(this->default_variable_name), " ^= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(ANDN_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: BYTE_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name),
lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" &= !":" &= ~",
lbessard@70: this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(ORN_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: BYTE_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name),
lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" |= !":" |= ~",
lbessard@70: this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(XORN_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_binary_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: BYTE_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name),
lbessard@70: // bit by bit exclusive OR !! Also seems to work with boolean types!
lbessard@70: search_expression_type->is_bool_type(this->current_operand_type)?" ^= !":" ^= ~",
lbessard@70: this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
lbessard@70: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(ADD_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_time_type(this->current_operand_type)) {
msousa@350: XXX_function("__time_add", &(this->default_variable_name), this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
laurent@345: }
laurent@345: else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: NUM_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name), " += ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
laurent@345: }
laurent@345: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(SUB_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_time_type(this->current_operand_type)) {
msousa@350: XXX_function("__time_sub", &(this->default_variable_name), this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
laurent@345: }
laurent@345: else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: NUM_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name), " -= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
laurent@345: }
laurent@345: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(MUL_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_integer_type(this->current_operand_type)) {
msousa@350: XXX_function("__time_mul", &(this->default_variable_name), this->current_operand);
laurent@345: /* the data type resulting from this operation is unchanged! */
laurent@345: }
laurent@345: else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: NUM_operator_result_type();
lbessard@70: XXX_operator(&(this->default_variable_name), " *= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
laurent@345: }
laurent@345: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(DIV_operator_c *symbol) {
lbessard@149: if (search_expression_type->is_time_type(this->default_variable_name.current_type) &&
lbessard@149: search_expression_type->is_integer_type(this->current_operand_type)) {
msousa@350: XXX_function("__time_div", &(this->default_variable_name), this->current_operand);
laurent@345: /* the data type resulting from this operation is unchanged! */
laurent@345: }
laurent@345: else if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: NUM_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name), " /= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
laurent@345: return NULL;
laurent@345: }
laurent@345: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(MOD_operator_c *symbol) {
lbessard@70: if (search_expression_type->is_num_type(this->default_variable_name.current_type) &&
lbessard@70: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
Laurent@706: NUM_operator_result_type();
Laurent@706: XXX_operator(&(this->default_variable_name), " %= ", this->current_operand);
lbessard@70: /* the data type resulting from this operation... */
lbessard@70: this->default_variable_name.current_type = this->current_operand_type;
lbessard@70: }
laurent@345: else {ERROR;}
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(GT_operator_c *symbol) {
lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
laurent@345: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
msousa@355: CMP_operator(this->current_operand, "GT_");
msousa@355: } else {
msousa@355: ERROR;
msousa@355: }
lbessard@123: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(GE_operator_c *symbol) {
lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
laurent@345: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
msousa@355: CMP_operator(this->current_operand, "GE_");
msousa@355: } else {
msousa@355: ERROR;
msousa@355: }
lbessard@123: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(EQ_operator_c *symbol) {
laurent@345: if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
msousa@355: CMP_operator(this->current_operand, "EQ_");
msousa@355: } else {
msousa@355: ERROR;
msousa@355: }
lbessard@123: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(LT_operator_c *symbol) {
lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
laurent@345: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
msousa@355: CMP_operator(this->current_operand, "LT_");
msousa@355: } else {
msousa@355: ERROR;
msousa@355: }
lbessard@123: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(LE_operator_c *symbol) {
lbessard@123: if (!search_base_type.type_is_enumerated(this->default_variable_name.current_type) &&
laurent@345: search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
msousa@355: CMP_operator(this->current_operand, "LE_");
msousa@355: } else {
msousa@355: ERROR;
msousa@355: }
lbessard@123: return NULL;
lbessard@70: }
lbessard@70:
Laurent@706: void *visit(NE_operator_c *symbol) {
laurent@345: if (search_expression_type->is_same_type(this->default_variable_name.current_type, this->current_operand_type)) {
msousa@355: CMP_operator(this->current_operand, "NE_");
msousa@355: } else {
msousa@355: ERROR;
msousa@355: }
lbessard@123: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70: //SYM_REF0(CAL_operator_c)
lbessard@70: // This method will be called from within the il_fb_call_c visitor method
lbessard@70: void *visit(CAL_operator_c *symbol) {return NULL;}
lbessard@70:
lbessard@70: //SYM_REF0(CALC_operator_c)
lbessard@70: // This method will be called from within the il_fb_call_c visitor method
lbessard@70: void *visit(CALC_operator_c *symbol) {C_modifier(); return NULL;}
lbessard@70:
lbessard@70: //SYM_REF0(CALCN_operator_c)
lbessard@70: // This method will be called from within the il_fb_call_c visitor method
lbessard@70: void *visit(CALCN_operator_c *symbol) {CN_modifier(); return NULL;}
lbessard@70:
lbessard@70: /* NOTE: The semantics of the RET operator requires us to return a value
lbessard@70: * if the IL code is inside a function, but simply return no value if
lbessard@70: * the IL code is inside a function block or program!
lbessard@70: * Nevertheless, it is the generate_c_c class itself that
lbessard@70: * introduces the 'reaturn ' into the c++ code at the end
lbessard@70: * of every function. This class does not know whether the IL code
lbessard@70: * is inside a function or a function block.
lbessard@70: * We work around this by jumping to the end of the code,
lbessard@70: * that will be marked by the END_LABEL label in the
lbessard@70: * instruction_list_c visitor...
lbessard@70: */
lbessard@70: // SYM_REF0(RET_operator_c)
lbessard@70: void *visit(RET_operator_c *symbol) {
lbessard@70: s4o.print("goto ");s4o.print(END_LABEL);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: // SYM_REF0(RETC_operator_c)
lbessard@70: void *visit(RETC_operator_c *symbol) {
lbessard@70: C_modifier();
lbessard@70: s4o.print("goto ");s4o.print(END_LABEL);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: // SYM_REF0(RETCN_operator_c)
lbessard@70: void *visit(RETCN_operator_c *symbol) {
lbessard@70: CN_modifier();
lbessard@70: s4o.print("goto ");s4o.print(END_LABEL);
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: //SYM_REF0(JMP_operator_c)
Laurent@706: void *visit(JMP_operator_c *symbol) {
lbessard@70: if (NULL == this->jump_label) ERROR;
lbessard@70:
lbessard@70: s4o.print("goto ");
lbessard@70: this->jump_label->accept(*this);
lbessard@70: /* the data type resulting from this operation is unchanged! */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: // SYM_REF0(JMPC_operator_c)
Laurent@706: void *visit(JMPC_operator_c *symbol) {
lbessard@70: if (NULL == this->jump_label) ERROR;
lbessard@70:
lbessard@70: C_modifier();
lbessard@70: s4o.print("goto ");
lbessard@70: this->jump_label->accept(*this);
lbessard@70: /* the data type resulting from this operation is unchanged! */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: // SYM_REF0(JMPCN_operator_c)
Laurent@706: void *visit(JMPCN_operator_c *symbol) {
lbessard@70: if (NULL == this->jump_label) ERROR;
lbessard@70:
lbessard@70: CN_modifier();
lbessard@70: s4o.print("goto ");
lbessard@70: this->jump_label->accept(*this);
lbessard@70: /* the data type resulting from this operation is unchanged! */
lbessard@70: return NULL;
lbessard@70: }
lbessard@70:
lbessard@70: #if 0
lbessard@70: /*| [NOT] any_identifier SENDTO */
lbessard@70: SYM_REF2(il_assign_out_operator_c, option, variable_name)
lbessard@70: #endif
lbessard@70:
lbessard@70: }; /* generate_c_il_c */
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: /* The implementation of the single visit() member function
lbessard@70: * of il_default_variable_c.
lbessard@70: * It can only come after the full declaration of
lbessard@70: * generate_c_il_c. Since we define and declare
lbessard@70: * generate_c_il_c simultaneously, it can only come
lbessard@70: * after the definition...
lbessard@70: */
lbessard@70: void *il_default_variable_c::accept(visitor_c &visitor) {
lbessard@70: /* An ugly hack!! */
lbessard@70: /* This is required because we need to over-ride the base
lbessard@70: * accept(visitor_c &) method of the class symbol_c,
lbessard@70: * so this method may be called through a symbol_c *
lbessard@70: * reference!
lbessard@70: *
lbessard@70: * But, the visitor_c does not include a visitor to
lbessard@70: * an il_default_variable_c, which means that we couldn't
lbessard@70: * simply call visitor.visit(this);
lbessard@70: *
lbessard@70: * We therefore need to use the dynamic_cast hack!!
lbessard@70: *
lbessard@70: * Note too that we can't cast a visitor_c to a
lbessard@70: * il_default_variable_visitor_c, since they are not related.
lbessard@70: * Nor may the il_default_variable_visitor_c inherit from
lbessard@70: * visitor_c, because then generate_c_il_c would contain
lbessard@70: * two visitor_c base classes, one each through
lbessard@70: * il_default_variable_visitor_c and generate_c_type_c
lbessard@70: *
lbessard@70: * We could use virtual inheritance of the visitor_c, but it
lbessard@70: * would probably create more problems than it is worth!
lbessard@70: */
lbessard@70: generate_c_il_c *v;
lbessard@70: v = dynamic_cast(&visitor);
lbessard@70: if (v == NULL) ERROR;
lbessard@70:
lbessard@70: return v->visit(this);
lbessard@70: }
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70:
lbessard@70: il_default_variable_c::il_default_variable_c(const char *var_name_str, symbol_c *current_type) {
lbessard@70: if (NULL == var_name_str) ERROR;
lbessard@70: /* Note: current_type may start off with NULL */
lbessard@70:
lbessard@70: this->var_name = new identifier_c(var_name_str);
lbessard@70: if (NULL == this->var_name) ERROR;
lbessard@70:
lbessard@70: this->current_type = current_type;
lbessard@70: }