stage4/generate_cc/generate_cc_st.cc
changeset 16 e8b99f896416
parent 0 fb772792efd1
child 22 08bcc40be1fa
equal deleted inserted replaced
15:0b472e25eb16 16:e8b99f896416
    30  * a c++ source program equivalent to the IL and ST
    30  * a c++ source program equivalent to the IL and ST
    31  * code.
    31  * code.
    32  */
    32  */
    33 
    33 
    34 
    34 
    35 
    35 /***********************************************************************/
       
    36 /***********************************************************************/
       
    37 /***********************************************************************/
       
    38 /***********************************************************************/
    36 
    39 
    37 
    40 
    38 class generate_cc_st_c: public generate_cc_typedecl_c {
    41 class generate_cc_st_c: public generate_cc_typedecl_c {
    39 
    42 
    40   private:
    43   private:
    51      * so we do not create an object instance when handling
    54      * so we do not create an object instance when handling
    52      * a function declaration.
    55      * a function declaration.
    53      */
    56      */
    54     search_fb_instance_decl_c *search_fb_instance_decl;
    57     search_fb_instance_decl_c *search_fb_instance_decl;
    55 
    58 
       
    59     /* When compiling st code, it becomes necessary to determine the
       
    60      * data type of st expressions. To do this, we must first find the
       
    61      * st operand's declaration, within the scope of the function block
       
    62      * or function currently being processed.
       
    63      * The following object does just that...
       
    64      * This object instance will then later be called while the
       
    65      * remaining st code is being handled.
       
    66      */
       
    67     search_expression_type_c *search_expression_type;
    56 
    68 
    57   public:
    69   public:
    58     generate_cc_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
    70     generate_cc_st_c(stage4out_c *s4o_ptr, symbol_c *scope, const char *variable_prefix = NULL)
    59     : generate_cc_typedecl_c(s4o_ptr) {
    71     : generate_cc_typedecl_c(s4o_ptr) {
    60       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
    72       search_fb_instance_decl = new search_fb_instance_decl_c(scope);
       
    73       search_expression_type = new search_expression_type_c(scope);
    61       this->set_variable_prefix(variable_prefix);
    74       this->set_variable_prefix(variable_prefix);
    62     }
    75     }
    63 
    76 
    64     virtual ~generate_cc_st_c(void) {
    77     virtual ~generate_cc_st_c(void) {
    65       delete search_fb_instance_decl;
    78       delete search_fb_instance_decl;
       
    79       delete search_expression_type;
    66     }
    80     }
    67 
    81 
    68 
    82 
    69   private:
    83   private:
    70     /* Some function calls in the body of functions or function blocks
    84     /* Some function calls in the body of functions or function blocks
   105       generate_cc_tempvardecl.generate(stl, &temp_var_name_factory);
   119       generate_cc_tempvardecl.generate(stl, &temp_var_name_factory);
   106       stl->accept(*this);
   120       stl->accept(*this);
   107     }
   121     }
   108 
   122 
   109   private:
   123   private:
       
   124   
   110 /***************************************/
   125 /***************************************/
   111 /* B.3 - Language ST (Structured Text) */
   126 /* B.3 - Language ST (Structured Text) */
   112 /***************************************/
   127 /***************************************/
   113 /***********************/
   128 /***********************/
   114 /* B 3.1 - Expressions */
   129 /* B 3.1 - Expressions */
   115 /***********************/
   130 /***********************/
   116 void *visit(or_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " || ");}
   131 void *visit(or_expression_c *symbol) {
   117 /* TODO ...  XOR expression... */
   132   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   118 void *visit(xor_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " XOR ");}
   133   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   119 void *visit(and_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");}
   134   if (typeid(*left_type) == typeid(bool_type_name_c) && typeid(*right_type) == typeid(bool_type_name_c)) {
   120 void *visit(equ_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");}
   135     return print_binary_expression(symbol->l_exp, symbol->r_exp, " || ");
   121 void *visit(notequ_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");}
   136   }
   122 void *visit(lt_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");}
   137   if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) {
   123 void *visit(gt_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");}
   138     return print_binary_expression(symbol->l_exp, symbol->r_exp, " | ");
   124 void *visit(le_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");}
   139   }
   125 void *visit(ge_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");}
   140   ERROR;
   126 void *visit(add_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");}
   141   return NULL;
   127 void *visit(sub_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");}
   142 }
   128 void *visit(mul_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");}
   143 
       
   144 void *visit(xor_expression_c *symbol) {
       
   145   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   146   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   147   if (typeid(*left_type) == typeid(bool_type_name_c) && typeid(*right_type) == typeid(bool_type_name_c)) {
       
   148     s4o.print("(");
       
   149     symbol->l_exp->accept(*this);
       
   150     s4o.print(" && !");
       
   151     symbol->r_exp->accept(*this);
       
   152     s4o.print(") || (!");
       
   153     symbol->l_exp->accept(*this);
       
   154     s4o.print(" && ");
       
   155     symbol->r_exp->accept(*this);
       
   156     s4o.print(")");
       
   157   }
       
   158   if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) {
       
   159     return print_binary_expression(symbol->l_exp, symbol->r_exp, " ^ ");
       
   160   }
       
   161   ERROR;
       
   162   return NULL;
       
   163 }
       
   164 
       
   165 void *visit(and_expression_c *symbol) {
       
   166   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   167   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   168   if (typeid(*left_type) == typeid(bool_type_name_c) && typeid(*right_type) == typeid(bool_type_name_c)) {
       
   169     return print_binary_expression(symbol->l_exp, symbol->r_exp, " && ");
       
   170   }
       
   171   if (search_expression_type->is_numeric_compatible(left_type) && search_expression_type->is_numeric_compatible(right_type)) {
       
   172     return print_binary_expression(symbol->l_exp, symbol->r_exp, " & ");
       
   173   }
       
   174   ERROR;
       
   175   return NULL;
       
   176 }
       
   177 
       
   178 void *visit(equ_expression_c *symbol) {
       
   179   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   180   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   181   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   182     return print_compare_function("__compare_timespec", "==", symbol->l_exp, symbol->r_exp);
       
   183   }
       
   184   return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
       
   185 }
       
   186 
       
   187 void *visit(notequ_expression_c *symbol) {
       
   188   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   189   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   190   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   191     return print_compare_function("__compare_timespec", "!=", symbol->l_exp, symbol->r_exp);
       
   192   }
       
   193   return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
       
   194 }
       
   195 
       
   196 void *visit(lt_expression_c *symbol) {
       
   197   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   198   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   199   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   200     return print_compare_function("__compare_timespec", "<", symbol->l_exp, symbol->r_exp);
       
   201   }
       
   202   return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
       
   203 }
       
   204 
       
   205 void *visit(gt_expression_c *symbol) {
       
   206   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   207   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   208   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   209     return print_compare_function("__compare_timespec", ">", symbol->l_exp, symbol->r_exp);
       
   210   }
       
   211   return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
       
   212 }
       
   213 
       
   214 void *visit(le_expression_c *symbol) {
       
   215   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   216   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   217   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   218     return print_compare_function("__compare_timespec", "<=", symbol->l_exp, symbol->r_exp);
       
   219   }
       
   220   return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
       
   221 }
       
   222 
       
   223 void *visit(ge_expression_c *symbol) {
       
   224   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   225   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   226   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   227     return print_compare_function("__compare_timespec", ">=", symbol->l_exp, symbol->r_exp);
       
   228   }
       
   229   return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
       
   230 }
       
   231 
       
   232 void *visit(add_expression_c *symbol) {
       
   233   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   234 	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   235 	if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   236 		return print_binary_function("__add_timespec", symbol->l_exp, symbol->r_exp);
       
   237 	}
       
   238 	return print_binary_expression(symbol->l_exp, symbol->r_exp, " + ");
       
   239 }
       
   240 
       
   241 void *visit(sub_expression_c *symbol) {
       
   242   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   243   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   244   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   245     return print_binary_function("__sub_timespec", symbol->l_exp, symbol->r_exp);
       
   246   }
       
   247   return print_binary_expression(symbol->l_exp, symbol->r_exp, " - ");
       
   248 }
       
   249 
       
   250 void *visit(mul_expression_c *symbol) {
       
   251   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
       
   252   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
       
   253   if (search_expression_type->is_time_compatible(left_type) && search_expression_type->is_time_compatible(right_type)) {
       
   254     return print_binary_function("__mul_timespec", symbol->l_exp, symbol->r_exp);
       
   255   }
       
   256   return print_binary_expression(symbol->l_exp, symbol->r_exp, " * ");
       
   257 }
       
   258 
   129 void *visit(div_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");}
   259 void *visit(div_expression_c *symbol) {return print_binary_expression(symbol->l_exp, symbol->r_exp, " / ");}
   130 void *visit(mod_expression_c *symbol) {
   260 void *visit(mod_expression_c *symbol) {
   131   s4o.print("((");
   261   s4o.print("((");
   132   symbol->r_exp->accept(*this);
   262   symbol->r_exp->accept(*this);
   133   s4o.print(" == 0)?0:");
   263   s4o.print(" == 0)?0:");