stage4/generate_c/generate_c_st.cc
changeset 123 a9b4af71cfa4
parent 101 fd9abc64c43c
child 130 a8263f33123f
equal deleted inserted replaced
122:9e57c6d79398 123:a9b4af71cfa4
   250 void *visit(equ_expression_c *symbol) {
   250 void *visit(equ_expression_c *symbol) {
   251   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   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);
   252   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   253   if (!search_expression_type->is_same_type(left_type, right_type))
   253   if (!search_expression_type->is_same_type(left_type, right_type))
   254       ERROR;
   254       ERROR;
   255   if (search_expression_type->is_time_type(left_type))
   255   if (search_expression_type->is_time_type(left_type) ||
       
   256       search_expression_type->is_string_type(left_type))
   256     return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp);
   257     return print_compare_function("__eq_", left_type, symbol->l_exp, symbol->r_exp);
   257   return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
   258   return print_binary_expression(symbol->l_exp, symbol->r_exp, " == ");
   258 }
   259 }
   259 
   260 
   260 void *visit(notequ_expression_c *symbol) {
   261 void *visit(notequ_expression_c *symbol) {
   261   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   262   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   262   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   263   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   263   if (!search_expression_type->is_same_type(left_type, right_type))
   264   if (!search_expression_type->is_same_type(left_type, right_type))
   264       ERROR;
   265       ERROR;
   265   if (search_expression_type->is_time_type(left_type))
   266   if (search_expression_type->is_time_type(left_type) ||
       
   267       search_expression_type->is_string_type(left_type))
   266     return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp);
   268     return print_compare_function("__ne_", left_type, symbol->l_exp, symbol->r_exp);
   267   return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
   269   return print_binary_expression(symbol->l_exp, symbol->r_exp, " != ");
   268 }
   270 }
   269 
   271 
   270 void *visit(lt_expression_c *symbol) {
   272 void *visit(lt_expression_c *symbol) {
   271   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   273   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   272   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   274   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   273   if (!search_expression_type->is_same_type(left_type, right_type))
   275   if (!search_expression_type->is_same_type(left_type, right_type))
   274       ERROR;
   276       ERROR;
   275   if (search_expression_type->is_time_type(left_type))
   277   if (search_expression_type->is_time_type(left_type) ||
       
   278       search_expression_type->is_string_type(left_type))
   276     return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp);
   279     return print_compare_function("__lt_", left_type, symbol->l_exp, symbol->r_exp);
   277   return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
   280   if (!search_base_type.type_is_enumerated(left_type))
       
   281     return print_binary_expression(symbol->l_exp, symbol->r_exp, " < ");
       
   282   ERROR;
       
   283   return NULL;
   278 }
   284 }
   279 
   285 
   280 void *visit(gt_expression_c *symbol) {
   286 void *visit(gt_expression_c *symbol) {
   281   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   287   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   282   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   288   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   283   if (!search_expression_type->is_same_type(left_type, right_type))
   289   if (!search_expression_type->is_same_type(left_type, right_type))
   284       ERROR;
   290       ERROR;
   285   if (search_expression_type->is_time_type(left_type))
   291   if (search_expression_type->is_time_type(left_type) ||
       
   292       search_expression_type->is_string_type(left_type))
   286     return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp);
   293     return print_compare_function("__gt_", left_type, symbol->l_exp, symbol->r_exp);
   287   return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
   294   if (!search_base_type.type_is_enumerated(left_type))
       
   295     return print_binary_expression(symbol->l_exp, symbol->r_exp, " > ");
       
   296   ERROR;
       
   297   return NULL;
   288 }
   298 }
   289 
   299 
   290 void *visit(le_expression_c *symbol) {
   300 void *visit(le_expression_c *symbol) {
   291   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   301   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   292   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   302   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   293   if (!search_expression_type->is_same_type(left_type, right_type))
   303   if (!search_expression_type->is_same_type(left_type, right_type))
   294       ERROR;
   304       ERROR;
   295   if (search_expression_type->is_time_type(left_type))
   305   if (search_expression_type->is_time_type(left_type) ||
       
   306       search_expression_type->is_string_type(left_type))
   296     return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp);
   307     return print_compare_function("__le_", left_type, symbol->l_exp, symbol->r_exp);
   297   return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
   308   if (!search_base_type.type_is_enumerated(left_type))
       
   309     return print_binary_expression(symbol->l_exp, symbol->r_exp, " <= ");
       
   310   ERROR;
       
   311   return NULL;
   298 }
   312 }
   299 
   313 
   300 void *visit(ge_expression_c *symbol) {
   314 void *visit(ge_expression_c *symbol) {
   301   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   315   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   302   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   316   symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   303   if (!search_expression_type->is_same_type(left_type, right_type))
   317   if (!search_expression_type->is_same_type(left_type, right_type))
   304       ERROR;
   318       ERROR;
   305   if (search_expression_type->is_time_type(left_type))
   319   if (search_expression_type->is_time_type(left_type) ||
       
   320       search_expression_type->is_string_type(left_type))
   306     return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp);
   321     return print_compare_function("__ge_", left_type, symbol->l_exp, symbol->r_exp);
   307   return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
   322   if (!search_base_type.type_is_enumerated(left_type))
       
   323     return print_binary_expression(symbol->l_exp, symbol->r_exp, " >= ");
       
   324   ERROR;
       
   325   return NULL;
   308 }
   326 }
   309 
   327 
   310 void *visit(add_expression_c *symbol) {
   328 void *visit(add_expression_c *symbol) {
   311   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   329   symbol_c *left_type = search_expression_type->get_type(symbol->l_exp);
   312 	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   330 	symbol_c *right_type = search_expression_type->get_type(symbol->r_exp);
   382   return NULL;
   400   return NULL;
   383 }
   401 }
   384 
   402 
   385 /* TODO: power expression... */
   403 /* TODO: power expression... */
   386 void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");}
   404 void *visit(power_expression_c *symbol) {ERROR; return print_binary_expression(symbol->l_exp, symbol->r_exp, " ** ");}
   387 void *visit(neg_expression_c *symbol) {return print_unary_expression(symbol->exp, " -");}
   405 void *visit(neg_expression_c *symbol) {
       
   406   symbol_c *exp_type = search_expression_type->get_type(symbol->exp);
       
   407   if (search_expression_type->is_integer_type(exp_type) || search_expression_type->is_real_type(exp_type))
       
   408     return print_unary_expression(symbol->exp, " -");
       
   409   ERROR;
       
   410   return NULL;
       
   411 }
   388 
   412 
   389 void *visit(not_expression_c *symbol) {
   413 void *visit(not_expression_c *symbol) {
   390   symbol_c *exp_type = search_expression_type->get_type(symbol->exp);
   414   symbol_c *exp_type = search_expression_type->get_type(symbol->exp);
   391   if (!search_expression_type->is_binary_type(exp_type))
   415   if (search_expression_type->is_binary_type(exp_type))
   392      ERROR;
   416     return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~");
   393   return print_unary_expression(symbol->exp, search_expression_type->is_bool_type(exp_type)?"!":"~");
   417   ERROR;
       
   418   return NULL;
   394 }
   419 }
   395 
   420 
   396 void *visit(function_invocation_c *symbol) {
   421 void *visit(function_invocation_c *symbol) {
   397   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
   422   function_declaration_c *f_decl = function_symtable.find_value(symbol->function_name);
   398   
   423   
   543       if (param_value == NULL)
   568       if (param_value == NULL)
   544         param_value = function_call_param_iterator.next();
   569         param_value = function_call_param_iterator.next();
   545   
   570   
   546       symbol_c *param_type = fp_iterator.param_type();
   571       symbol_c *param_type = fp_iterator.param_type();
   547       if (param_type == NULL) ERROR;
   572       if (param_type == NULL) ERROR;
   548   
       
   549       search_base_type.explore_type(param_type);
       
   550         
   573         
   551       switch (param_direction) {
   574       switch (param_direction) {
   552         case function_param_iterator_c::direction_in:
   575         case function_param_iterator_c::direction_in:
   553           if (param_value == NULL) {
   576           if (param_value == NULL) {
   554             /* No value given for parameter, so we must use the default... */
   577             /* No value given for parameter, so we must use the default... */
   558           if (param_value == NULL) {
   581           if (param_value == NULL) {
   559             /* If not, get the default value of this variable's type */
   582             /* If not, get the default value of this variable's type */
   560             param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
   583             param_value = (symbol_c *)param_type->accept(*type_initial_value_c::instance());
   561           }
   584           }
   562           if (param_value == NULL) ERROR;
   585           if (param_value == NULL) ERROR;
   563           if (search_base_type.base_is_subrange()) {
   586           if (search_base_type.type_is_subrange(param_type)) {
   564             s4o.print("__CHECK_");
   587             s4o.print("__CHECK_");
   565             param_type->accept(*this);
   588             param_type->accept(*this);
   566             s4o.print("(");
   589             s4o.print("(");
   567           }
   590           }
   568           param_value->accept(*this);
   591           param_value->accept(*this);
   569           if (search_base_type.base_is_subrange())
   592           if (search_base_type.type_is_subrange(param_type))
   570             s4o.print(")");
   593             s4o.print(")");
   571           break;
   594           break;
   572         case function_param_iterator_c::direction_out:
   595         case function_param_iterator_c::direction_out:
   573         case function_param_iterator_c::direction_inout:
   596         case function_param_iterator_c::direction_inout:
   574           if (param_value == NULL) {
   597           if (param_value == NULL) {
   604 /*********************************/
   627 /*********************************/
   605 /* B 3.2.1 Assignment Statements */
   628 /* B 3.2.1 Assignment Statements */
   606 /*********************************/
   629 /*********************************/
   607 void *visit(assignment_statement_c *symbol) {
   630 void *visit(assignment_statement_c *symbol) {
   608   symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false);
   631   symbol_c *left_type = search_varfb_instance_type->get_type(symbol->l_exp, false);
   609   symbol_c *base_type = (symbol_c *)search_base_type.explore_type(left_type);
       
   610   
   632   
   611   symbol->l_exp->accept(*this);
   633   symbol->l_exp->accept(*this);
   612   s4o.print(" = ");
   634   s4o.print(" = ");
   613   if (search_base_type.base_is_subrange()) {
   635   if (search_base_type.type_is_subrange(left_type)) {
   614     s4o.print("__CHECK_");
   636     s4o.print("__CHECK_");
   615     left_type->accept(*this);
   637     left_type->accept(*this);
   616     s4o.print("(");
   638     s4o.print("(");
   617   }
   639   }
   618   symbol->r_exp->accept(*this);
   640   symbol->r_exp->accept(*this);
   619   if (search_base_type.base_is_subrange())
   641   if (search_base_type.type_is_subrange(left_type))
   620     s4o.print(")");
   642     s4o.print(")");
   621   return NULL;
   643   return NULL;
   622 }
   644 }
   623 
   645 
   624 /*****************************************/
   646 /*****************************************/
   657       param_value = function_call_param_iterator.next();
   679       param_value = function_call_param_iterator.next();
   658 
   680 
   659     symbol_c *param_type = fp_iterator.param_type();
   681     symbol_c *param_type = fp_iterator.param_type();
   660     if (param_type == NULL) ERROR;
   682     if (param_type == NULL) ERROR;
   661     
   683     
   662     search_base_type.explore_type(param_type);
       
   663 
       
   664     /* now output the value assignment */
   684     /* now output the value assignment */
   665     if (param_value != NULL)
   685     if (param_value != NULL)
   666       if ((param_direction == function_param_iterator_c::direction_in) ||
   686       if ((param_direction == function_param_iterator_c::direction_in) ||
   667           (param_direction == function_param_iterator_c::direction_inout)) {
   687           (param_direction == function_param_iterator_c::direction_inout)) {
   668         print_variable_prefix();
   688         print_variable_prefix();
   669         symbol->fb_name->accept(*this);
   689         symbol->fb_name->accept(*this);
   670         s4o.print(".");
   690         s4o.print(".");
   671         param_name->accept(*this);
   691         param_name->accept(*this);
   672         s4o.print(" = ");
   692         s4o.print(" = ");
   673         if (search_base_type.base_is_subrange()) {
   693         if (search_base_type.type_is_subrange(param_type)) {
   674           s4o.print("__CHECK_");
   694           s4o.print("__CHECK_");
   675           param_type->accept(*this);
   695           param_type->accept(*this);
   676           s4o.print("(");
   696           s4o.print("(");
   677         }
   697         }
   678         param_value->accept(*this);
   698         param_value->accept(*this);
   679         if (search_base_type.base_is_subrange())
   699         if (search_base_type.type_is_subrange(param_type))
   680           s4o.print(")");
   700           s4o.print(")");
   681         s4o.print(";\n" + s4o.indent_spaces);
   701         s4o.print(";\n" + s4o.indent_spaces);
   682       }
   702       }
   683   } /* for(...) */
   703   } /* for(...) */
   684 
   704 
   708     /* now output the value assignment */
   728     /* now output the value assignment */
   709     if (param_value != NULL)
   729     if (param_value != NULL)
   710       if ((param_direction == function_param_iterator_c::direction_out) ||
   730       if ((param_direction == function_param_iterator_c::direction_out) ||
   711           (param_direction == function_param_iterator_c::direction_inout)) {
   731           (param_direction == function_param_iterator_c::direction_inout)) {
   712         symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
   732         symbol_c *param_type = search_varfb_instance_type->get_type(param_value, false);
   713         search_base_type.explore_type(param_type);
       
   714         
   733         
   715         s4o.print(";\n"+ s4o.indent_spaces);
   734         s4o.print(";\n"+ s4o.indent_spaces);
   716         param_value->accept(*this);
   735         param_value->accept(*this);
   717         s4o.print(" = ");
   736         s4o.print(" = ");
   718         if (search_base_type.base_is_subrange()) {
   737         if (search_base_type.type_is_subrange(param_type)) {
   719           s4o.print("__CHECK_");
   738           s4o.print("__CHECK_");
   720           param_type->accept(*this);
   739           param_type->accept(*this);
   721           s4o.print("(");
   740           s4o.print("(");
   722         }
   741         }
   723         print_variable_prefix();
   742         print_variable_prefix();
   724         symbol->fb_name->accept(*this);
   743         symbol->fb_name->accept(*this);
   725         s4o.print(".");
   744         s4o.print(".");
   726         param_name->accept(*this);
   745         param_name->accept(*this);
   727         if (search_base_type.base_is_subrange())
   746         if (search_base_type.type_is_subrange(param_type))
   728           s4o.print(")");
   747           s4o.print(")");
   729       }
   748       }
   730   } /* for(...) */
   749   } /* for(...) */
   731 
   750 
   732   return NULL;
   751   return NULL;