stage4/generate_c/generate_c_base.cc
changeset 927 39b596670e33
parent 923 0ad7843d3790
child 928 fa7a6800503d
equal deleted inserted replaced
926:b8538c5d0aeb 927:39b596670e33
   114 
   114 
   115     void *print_striped_token(token_c *token, int offset = 0) {
   115     void *print_striped_token(token_c *token, int offset = 0) {
   116       std::string str = "";
   116       std::string str = "";
   117       bool leading_zero = true;
   117       bool leading_zero = true;
   118       for (unsigned int i = offset; i < strlen(token->value); i++) {
   118       for (unsigned int i = offset; i < strlen(token->value); i++) {
   119         if (leading_zero &&
   119         if (leading_zero
   120         	(token->value[i] != '0' ||
   120             && (   token->value[i] != '0'
   121         	 i == strlen(token->value) - 1 ||
   121                 || i == strlen(token->value) - 1
   122         	 token->value[i + 1] == '.'
   122                 || token->value[i + 1] == '.'
   123         	))
   123                 )
       
   124             )
   124           leading_zero = false;
   125           leading_zero = false;
   125     	if (!leading_zero && token->value[i] != '_')
   126             if (!leading_zero && token->value[i] != '_')
   126           str += token->value[i];
   127           str += token->value[i];
   127       }
   128       }
   128       return s4o.printupper(str);
   129       return s4o.printupper(str);
   129     }
   130     }
   130 
   131 
   138       s4o.print("0x");
   139       s4o.print("0x");
   139 
   140 
   140       total_bits = 0;
   141       total_bits = 0;
   141       for (i = offset; i < strlen(token->value); i++)
   142       for (i = offset; i < strlen(token->value); i++)
   142         if (token->value[i] != '_')
   143         if (token->value[i] != '_')
   143 	  total_bits++;
   144           total_bits++;
   144 
   145 
   145       value = 0;
   146       value = 0;
   146       bit_mult = (unsigned char)1 << (((total_bits+3)%4)+1);
   147       bit_mult = (unsigned char)1 << (((total_bits+3)%4)+1);
   147       for (i = offset; i < strlen(token->value); i++) {
   148       for (i = offset; i < strlen(token->value); i++) {
   148         if (token->value[i] != '_') {
   149         if (token->value[i] != '_') {
   149 	  bit_mult /= 2;
   150           bit_mult /= 2;
   150 	  value += bit_mult * ((token->value[i] == '0')? 0:1);
   151           value += bit_mult * ((token->value[i] == '0')? 0:1);
   151 	  if (bit_mult == 1) {
   152           if (bit_mult == 1) {
   152 	    str[0] = (value <= 9)? (char)'0' + value : (char)'A' + value - 10;
   153             str[0] = (value <= 9)? (char)'0' + value : (char)'A' + value - 10;
   153 	    s4o.print(str);
   154             s4o.print(str);
   154             bit_mult = 0x10;
   155             bit_mult = 0x10;
   155             value = 0;
   156             value = 0;
   156 	  }
   157           }
   157 	}
   158         }
   158       }
   159       }
   159 
   160 
   160       return NULL;
   161       return NULL;
   161     }
   162     }
   162 
   163 
   163     void *print_list(list_c *list,
   164     void *print_list(list_c *list,
   164 		     std::string pre_elem_str = "",
   165                      std::string pre_elem_str = "",
   165 		     std::string inter_elem_str = "",
   166                      std::string inter_elem_str = "",
   166 		     std::string post_elem_str = "",
   167                      std::string post_elem_str = "",
   167 		     visitor_c *visitor = NULL) {
   168                      visitor_c *visitor = NULL) {
   168       if (visitor == NULL) visitor = this;
   169       if (visitor == NULL) visitor = this;
   169 
   170 
   170       if (list->n > 0) {
   171       if (list->n > 0) {
   171 //std::cout << "generate_c_base_c::print_list(n = " << list->n << ")   000\n";
   172 //std::cout << "generate_c_base_c::print_list(n = " << list->n << ")   000\n";
   172         s4o.print(pre_elem_str);
   173         s4o.print(pre_elem_str);
   185       return NULL;
   186       return NULL;
   186     }
   187     }
   187 
   188 
   188 
   189 
   189     void *print_binary_expression(symbol_c *l_exp,
   190     void *print_binary_expression(symbol_c *l_exp,
   190 				  symbol_c *r_exp,
   191                                   symbol_c *r_exp,
   191 				  const char *operation) {
   192                                   const char *operation) {
   192       s4o.print("(");
   193       s4o.print("(");
   193       l_exp->accept(*this);
   194       l_exp->accept(*this);
   194       s4o.print(operation);
   195       s4o.print(operation);
   195       r_exp->accept(*this);
   196       r_exp->accept(*this);
   196       s4o.print(")");
   197       s4o.print(")");
   197       return NULL;
   198       return NULL;
   198     }
   199     }
   199 
   200 
   200     void *print_unary_expression(symbol_c *exp,
   201     void *print_unary_expression(symbol_c *exp,
   201 				 const char *operation) {
   202                                  const char *operation) {
   202       s4o.print(operation);
   203       s4o.print(operation);
   203       s4o.print("(");
   204       s4o.print("(");
   204       exp->accept(*this);
   205       exp->accept(*this);
   205       s4o.print(")");
   206       s4o.print(")");
   206       return NULL;
   207       return NULL;
   207     }
   208     }
   208 
   209 
   209     void *print_binary_function(const char *function,
   210     void *print_binary_function(const char *function,
   210           symbol_c *l_exp,
   211           symbol_c *l_exp,
   211 				  symbol_c *r_exp) {
   212           symbol_c *r_exp) {
   212       s4o.print(function);
   213       s4o.print(function);
   213       s4o.print("(");
   214       s4o.print("(");
   214       l_exp->accept(*this);
   215       l_exp->accept(*this);
   215       s4o.print(", ");
   216       s4o.print(", ");
   216       r_exp->accept(*this);
   217       r_exp->accept(*this);
   217       s4o.print(")");
   218       s4o.print(")");
   218       return NULL;
   219       return NULL;
   219    	}
   220     }
   220 
   221 
   221     void *print_compare_function(const char *function,
   222     void *print_compare_function(const char *function,
   222           symbol_c *compare_type,
   223           symbol_c *compare_type,
   223           symbol_c *l_exp,
   224           symbol_c *l_exp,
   224           symbol_c *r_exp) {
   225           symbol_c *r_exp) {
   237           symbol_c *fb_name = NULL,
   238           symbol_c *fb_name = NULL,
   238           bool temp = false) {
   239           bool temp = false) {
   239       if (!get_datatype_info_c::is_type_valid(type)) ERROR;
   240       if (!get_datatype_info_c::is_type_valid(type)) ERROR;
   240       bool is_subrange = get_datatype_info_c::is_subrange(type);
   241       bool is_subrange = get_datatype_info_c::is_subrange(type);
   241       if (is_subrange) {
   242       if (is_subrange) {
   242 		s4o.print("__CHECK_");
   243         s4o.print("__CHECK_");
   243 		type->accept(*this);
   244         type->accept(*this);
   244 		s4o.print("(");
   245         s4o.print("(");
   245       }
   246       }
   246       if (fb_name != NULL) {
   247       if (fb_name != NULL) {
   247         s4o.print(GET_VAR);
   248         s4o.print(GET_VAR);
   248         s4o.print("(");
   249         s4o.print("(");
   249         print_variable_prefix();
   250         print_variable_prefix();
   252         value->accept(*this);
   253         value->accept(*this);
   253         s4o.print(")");
   254         s4o.print(")");
   254       }
   255       }
   255       else {
   256       else {
   256         if (temp)
   257         if (temp)
   257     	  s4o.print(TEMP_VAR);
   258         s4o.print(TEMP_VAR);
   258         value->accept(*this);
   259         value->accept(*this);
   259       }
   260       }
   260       if (is_subrange)
   261       if (is_subrange)
   261         s4o.print(")");
   262         s4o.print(")");
   262       return NULL;
   263       return NULL;
   335     void *visit(bit_string_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
   336     void *visit(bit_string_literal_c *symbol) {return print_literal(symbol->type, symbol->value);}
   336     void *visit(boolean_literal_c *symbol) {
   337     void *visit(boolean_literal_c *symbol) {
   337       if (NULL != symbol->type)
   338       if (NULL != symbol->type)
   338         return print_literal(symbol->type, symbol->value);
   339         return print_literal(symbol->type, symbol->value);
   339       else {
   340       else {
   340 	bool_type_name_c bool_type;
   341         bool_type_name_c bool_type;
   341 	return print_literal(&bool_type, symbol->value);
   342         return print_literal(&bool_type, symbol->value);
   342       }
   343       }
   343     }
   344     }
   344 
   345 
   345     /* helper class for boolean_literal_c */
   346     /* helper class for boolean_literal_c */
   346     void *visit(boolean_true_c *symbol) {s4o.print("TRUE"); return NULL;}
   347     void *visit(boolean_true_c *symbol) {s4o.print("TRUE"); return NULL;}
   394           case 't':
   395           case 't':
   395             {str += "\\t"; /* tab */; count++; continue;}
   396             {str += "\\t"; /* tab */; count++; continue;}
   396           default: {
   397           default: {
   397             if (isxdigit(c)) {
   398             if (isxdigit(c)) {
   398               /* this should be safe, since the code has passed the syntax parser!! */
   399               /* this should be safe, since the code has passed the syntax parser!! */
   399 	      char c2 = symbol->value[++i];
   400               char c2 = symbol->value[++i];
   400 	      if (isxdigit(c2)) {
   401               if (isxdigit(c2)) {
   401 	        str += '\\'; str += 'x'; str += c; str += c2;
   402                 str += '\\'; str += 'x'; str += c; str += c2;
   402 	        count++; continue;
   403                 count++; continue;
   403 	      }
   404               }
   404 	    }
   405             }
   405           }
   406           }
   406           /* otherwise we have an invalid string!! */
   407           /* otherwise we have an invalid string!! */
   407           /* This should not have got through the syntax parser! */
   408           /* This should not have got through the syntax parser! */
   408           ERROR;
   409           ERROR;
   409         } /* switch() */
   410         } /* switch() */