stage4/generate_c/generate_var_list.cc
changeset 232 29ab33687333
parent 229 ca7bc1324540
child 279 c0453b7f99df
equal deleted inserted replaced
231:b8527b0abe75 232:29ab33687333
    41 typedef struct
    41 typedef struct
    42 {
    42 {
    43   symbol_c *symbol;
    43   symbol_c *symbol;
    44 } SYMBOL;
    44 } SYMBOL;
    45 
    45 
    46 
    46 typedef enum {
       
    47   none_lt,
       
    48   input_lt,
       
    49   output_lt,
       
    50   memory_lt
       
    51 } locationtype_t;
       
    52 
       
    53 
       
    54 /***********************************************************************/
       
    55 /***********************************************************************/
       
    56 /***********************************************************************/
       
    57 /***********************************************************************/
       
    58 
       
    59 
       
    60 class search_location_type_c: public iterator_visitor_c {
       
    61 
       
    62   public:
       
    63     locationtype_t current_location_type;
       
    64 
       
    65   public:
       
    66 	search_location_type_c(void) {}
       
    67 
       
    68 	virtual ~search_location_type_c(void) {}
       
    69 
       
    70 	locationtype_t get_location_type(symbol_c *symbol) {
       
    71       current_location_type = none_lt;
       
    72       symbol->accept(*this);
       
    73       if (current_location_type == none_lt) ERROR;
       
    74       return current_location_type;
       
    75 	}
       
    76 
       
    77   private:
       
    78 
       
    79 	void *visit(incompl_location_c* symbol) {
       
    80       if (symbol->value[1] == 'I')
       
    81         current_location_type = input_lt;
       
    82 	  else if (symbol->value[1] == 'Q')
       
    83         current_location_type = output_lt;
       
    84 	  else if (symbol->value[1] == 'M')
       
    85         current_location_type = memory_lt;
       
    86       return NULL;
       
    87 	}
       
    88 
       
    89 	void *visit(direct_variable_c *symbol) {
       
    90       if (symbol->value[1] == 'I')
       
    91         current_location_type = input_lt;
       
    92 	  else if (symbol->value[1] == 'Q')
       
    93         current_location_type = output_lt;
       
    94 	  else if (symbol->value[1] == 'M')
       
    95         current_location_type = memory_lt;
       
    96       return NULL;
       
    97 	}
       
    98 };
    47 
    99 
    48 
   100 
    49 /***********************************************************************/
   101 /***********************************************************************/
    50 /***********************************************************************/
   102 /***********************************************************************/
    51 /***********************************************************************/
   103 /***********************************************************************/
    67     declarationtype_t current_declarationtype;
   119     declarationtype_t current_declarationtype;
    68     
   120     
    69     typedef enum {
   121     typedef enum {
    70       none_vtc,
   122       none_vtc,
    71       variable_vtc,
   123       variable_vtc,
    72       pointer_vtc,
   124       external_vtc,
       
   125       located_input_vtc,
       
   126       located_output_vtc,
    73       array_vtc,
   127       array_vtc,
    74       structure_vtc,
   128       structure_vtc,
    75       function_block_vtc
   129       function_block_vtc
    76     } vartypecategory_t;
   130     } vartypecategory_t;
    77     
   131     
   167     
   221     
   168     void declare_variable(symbol_c *symbol) {
   222     void declare_variable(symbol_c *symbol) {
   169       print_var_number();
   223       print_var_number();
   170       s4o.print(";");
   224       s4o.print(";");
   171       switch (this->current_var_type_category) {
   225       switch (this->current_var_type_category) {
   172         case pointer_vtc:
   226         case external_vtc:
   173           s4o.print("PT");
   227           s4o.print("EXT");
       
   228           break;
       
   229         case located_input_vtc:
       
   230           s4o.print("IN");
       
   231           break;
       
   232         case located_output_vtc:
       
   233           s4o.print("OUT");
   174           break;
   234           break;
   175         case array_vtc:
   235         case array_vtc:
   176           s4o.print("ARRAY");
   236           s4o.print("ARRAY");
   177           break;
   237           break;
   178         case structure_vtc:
   238         case structure_vtc:
   259         /* Start off by setting the current_var_type_symbol and
   319         /* Start off by setting the current_var_type_symbol and
   260          * current_var_init_symbol private variables...
   320          * current_var_init_symbol private variables...
   261          */
   321          */
   262         update_var_type_symbol(symbol->located_var_spec_init);
   322         update_var_type_symbol(symbol->located_var_spec_init);
   263         
   323         
   264         if (symbol->variable_name != NULL) {
   324         search_location_type_c search_location_type;
   265           this->current_var_type_category = pointer_vtc;
   325         locationtype_t location_type = search_location_type.get_location_type(symbol->location);
       
   326         if (location_type == input_lt)
       
   327           this->current_var_type_category = located_input_vtc;
       
   328         else if (location_type == output_lt)
       
   329           this->current_var_type_category = located_output_vtc;
       
   330 
       
   331         if (symbol->variable_name != NULL)
   266           declare_variable(symbol->variable_name);
   332           declare_variable(symbol->variable_name);
   267         }
   333         else
       
   334           declare_variable(symbol->location);
   268         
   335         
       
   336         current_var_type_symbol = NULL;
       
   337         return NULL;
       
   338     }
       
   339 
       
   340     /* variable_name incompl_location ':' var_spec */
       
   341     /* variable_name -> may be NULL ! */
       
   342     //SYM_REF3(incompl_located_var_decl_c, variable_name, incompl_location, var_spec)
       
   343     void *visit(incompl_located_var_decl_c *symbol) {
       
   344         /* Start off by setting the current_var_type_symbol and
       
   345          * current_var_init_symbol private variables...
       
   346          */
       
   347         update_var_type_symbol(symbol->var_spec);
       
   348 
       
   349         search_location_type_c search_location_type;
       
   350         locationtype_t location_type = search_location_type.get_location_type(symbol->incompl_location);
       
   351         if (location_type == input_lt)
       
   352           this->current_var_type_category = located_input_vtc;
       
   353         else if (location_type == output_lt)
       
   354           this->current_var_type_category = located_output_vtc;
       
   355 
       
   356         if (symbol->variable_name != NULL)
       
   357           declare_variable(symbol->variable_name);
       
   358         else
       
   359           declare_variable(symbol->incompl_location);
       
   360 
   269         current_var_type_symbol = NULL;
   361         current_var_type_symbol = NULL;
   270         return NULL;
   362         return NULL;
   271     }
   363     }
   272     
   364     
   273     /*  var1_list ':' array_spec_init */
   365     /*  var1_list ':' array_spec_init */
   353        * current_var_init_symbol private variables...
   445        * current_var_init_symbol private variables...
   354        */
   446        */
   355       update_var_type_symbol(symbol->specification);
   447       update_var_type_symbol(symbol->specification);
   356       
   448       
   357       /* now to produce the c equivalent... */
   449       /* now to produce the c equivalent... */
   358       if (this->current_var_type_category == variable_vtc)
   450       this->current_var_type_category = external_vtc;
   359         this->current_var_type_category = pointer_vtc;
       
   360       declare_variable(symbol->global_var_name);
   451       declare_variable(symbol->global_var_name);
   361       
   452       
   362       /* Values no longer in scope, and therefore no longer used.
   453       /* Values no longer in scope, and therefore no longer used.
   363        * Make an effort to keep them set to NULL when not in use
   454        * Make an effort to keep them set to NULL when not in use
   364        * in order to catch bugs as soon as possible...
   455        * in order to catch bugs as soon as possible...
   400     }
   491     }
   401     
   492     
   402     /*| global_var_name location */
   493     /*| global_var_name location */
   403     // SYM_REF2(global_var_spec_c, global_var_name, location)
   494     // SYM_REF2(global_var_spec_c, global_var_name, location)
   404     void *visit(global_var_spec_c *symbol) {
   495     void *visit(global_var_spec_c *symbol) {
   405       if (symbol->global_var_name != NULL) {
   496       search_location_type_c search_location_type;
   406         this->current_var_type_category = pointer_vtc;
   497 	  locationtype_t location_type = search_location_type.get_location_type(symbol->location);
       
   498 	  if (location_type == input_lt)
       
   499         this->current_var_type_category = located_input_vtc;
       
   500       else if (location_type == output_lt)
       
   501         this->current_var_type_category = located_output_vtc;
       
   502 
       
   503       if (symbol->global_var_name != NULL)
   407         declare_variable(symbol->global_var_name);
   504         declare_variable(symbol->global_var_name);
   408       }
   505       else
       
   506         declare_variable(symbol->location);
   409       return NULL;
   507       return NULL;
   410     }
   508     }
   411     
   509     
   412     void *visit(var1_init_decl_c *symbol) {
   510     void *visit(var1_init_decl_c *symbol) {
   413       TRACE("var1_init_decl_c");
   511       TRACE("var1_init_decl_c");