stage4/generate_c/generate_location_list.cc
changeset 70 e1f0ebd2d9ec
child 185 39c23046dad6
equal deleted inserted replaced
69:41cb5b80416e 70:e1f0ebd2d9ec
       
     1 /*
       
     2  * (c) 2007 Mario de Sousa and Laurent Bessard
       
     3  *
       
     4  * Offered to the public under the terms of the GNU General Public License
       
     5  * as published by the Free Software Foundation; either version 2 of the
       
     6  * License, or (at your option) any later version.
       
     7  *
       
     8  * This program is distributed in the hope that it will be useful, but
       
     9  * WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
       
    11  * Public License for more details.
       
    12  *
       
    13  * This code is made available on the understanding that it will not be
       
    14  * used in safety-critical situations without a full and competent review.
       
    15  */
       
    16 
       
    17 /*
       
    18  * An IEC 61131-3 IL and ST compiler.
       
    19  *
       
    20  * Based on the
       
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    22  *
       
    23  */
       
    24 
       
    25 /*
       
    26  * This is one of the versions available for the 4th stage.
       
    27  *
       
    28  * This 4th stage generates a c++ source program equivalent
       
    29  * to the IL and ST code.
       
    30  */
       
    31 
       
    32 //#include <stdio.h>  /* required for NULL */
       
    33 //#include <string>
       
    34 //#include <iostream>
       
    35 
       
    36 //#include "../../util/symtable.hh"
       
    37 
       
    38 
       
    39 
       
    40 
       
    41 
       
    42 
       
    43 
       
    44 
       
    45 
       
    46 /***********************************************************************/
       
    47 /***********************************************************************/
       
    48 /***********************************************************************/
       
    49 /***********************************************************************/
       
    50 /***********************************************************************/
       
    51 /***********************************************************************/
       
    52 /***********************************************************************/
       
    53 /***********************************************************************/
       
    54 
       
    55 class generate_location_list_c: protected iterator_visitor_c {
       
    56 
       
    57   protected:
       
    58     stage4out_c &s4o;
       
    59 
       
    60   private:
       
    61     symbol_c *current_var_type_symbol;
       
    62     generate_c_base_c *generate_c_base;
       
    63     search_base_type_c search_base_type;
       
    64     
       
    65   public:
       
    66     generate_location_list_c(stage4out_c *s4o_ptr): s4o(*s4o_ptr) {
       
    67       generate_c_base = new generate_c_base_c(s4o_ptr);
       
    68       current_var_type_symbol = NULL;
       
    69     }
       
    70     ~generate_location_list_c(void) {
       
    71       delete generate_c_base;
       
    72     }
       
    73 
       
    74     bool test_location_type(symbol_c *direct_variable) {
       
    75       
       
    76       token_c *location = dynamic_cast<token_c *>(direct_variable);
       
    77       
       
    78       if (location == NULL)
       
    79         /* invalid identifiers... */
       
    80         return false;
       
    81       
       
    82       switch (location->value[2]) {
       
    83         case 'X': // bit
       
    84           if (typeid(*current_var_type_symbol) == typeid(bool_type_name_c)) return true;
       
    85           break;
       
    86         case 'B': // Byte, 8 bits
       
    87           if (typeid(*current_var_type_symbol) == typeid(sint_type_name_c)) return true;
       
    88           if (typeid(*current_var_type_symbol) == typeid(usint_type_name_c)) return true;
       
    89           if (typeid(*current_var_type_symbol) == typeid(string_type_name_c)) return true;
       
    90           if (typeid(*current_var_type_symbol) == typeid(byte_type_name_c)) return true;
       
    91           break;
       
    92         case 'W': // Word, 16 bits
       
    93           if (typeid(*current_var_type_symbol) == typeid(int_type_name_c)) return true;
       
    94           if (typeid(*current_var_type_symbol) == typeid(uint_type_name_c)) return true;
       
    95           if (typeid(*current_var_type_symbol) == typeid(word_type_name_c)) return true;
       
    96           if (typeid(*current_var_type_symbol) == typeid(wstring_type_name_c)) return true;
       
    97           break;
       
    98         case 'D': // Double, 32 bits
       
    99           if (typeid(*current_var_type_symbol) == typeid(dint_type_name_c)) return true;
       
   100           if (typeid(*current_var_type_symbol) == typeid(udint_type_name_c)) return true;
       
   101           if (typeid(*current_var_type_symbol) == typeid(real_type_name_c)) return true;
       
   102           if (typeid(*current_var_type_symbol) == typeid(dword_type_name_c)) return true;
       
   103           break;
       
   104         case 'L': // Long, 64 bits
       
   105           if (typeid(*current_var_type_symbol) == typeid(lint_type_name_c)) return true;
       
   106           if (typeid(*current_var_type_symbol) == typeid(ulint_type_name_c)) return true;
       
   107           if (typeid(*current_var_type_symbol) == typeid(lreal_type_name_c)) return true;
       
   108           if (typeid(*current_var_type_symbol) == typeid(lword_type_name_c)) return true;
       
   109           break;
       
   110         default:
       
   111           if (typeid(*current_var_type_symbol) == typeid(bool_type_name_c)) return true;
       
   112       }
       
   113       return false;
       
   114     }
       
   115 
       
   116 /********************************************/
       
   117 /* B.1.4.1   Directly Represented Variables */
       
   118 /********************************************/
       
   119 
       
   120     void *visit(direct_variable_c *symbol) {
       
   121       if (current_var_type_symbol) {
       
   122         s4o.print("__LOCATED_VAR(");
       
   123         current_var_type_symbol->accept(*generate_c_base);
       
   124         s4o.print(",");
       
   125         /* Do not use print_token() as it will change everything into uppercase */
       
   126         s4o.printlocation((symbol->value)+1);
       
   127         s4o.print(",");
       
   128         s4o.printlocation_comasep((symbol->value)+1);
       
   129         s4o.print(")\n");
       
   130       }
       
   131       return NULL;
       
   132     }
       
   133 
       
   134 
       
   135 /********************************************/
       
   136 /* B.1.4.3   Declaration and initilization  */
       
   137 /********************************************/
       
   138 
       
   139 /*  [variable_name] location ':' located_var_spec_init */
       
   140 /* variable_name -> may be NULL ! */
       
   141 //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
       
   142     void *visit(located_var_decl_c *symbol) {
       
   143         current_var_type_symbol = spec_init_sperator_c::get_spec(symbol->located_var_spec_init);
       
   144         if (current_var_type_symbol == NULL)
       
   145           ERROR;
       
   146         
       
   147         current_var_type_symbol = (symbol_c *)(current_var_type_symbol->accept(search_base_type));
       
   148         if (current_var_type_symbol == NULL)
       
   149           ERROR;
       
   150         
       
   151         symbol->location->accept(*this);
       
   152         
       
   153         current_var_type_symbol = NULL;
       
   154         return NULL;
       
   155     }
       
   156 
       
   157 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
       
   158 /* type_specification ->may be NULL ! */
       
   159 //SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
       
   160     void *visit(global_var_decl_c *symbol) {
       
   161         current_var_type_symbol = spec_init_sperator_c::get_spec(symbol->type_specification);
       
   162         if (current_var_type_symbol == NULL)
       
   163           ERROR;
       
   164         
       
   165         current_var_type_symbol = (symbol_c *)(current_var_type_symbol->accept(search_base_type));
       
   166         if (current_var_type_symbol == NULL)
       
   167           ERROR;
       
   168         
       
   169         symbol->global_var_spec->accept(*this);
       
   170         
       
   171         current_var_type_symbol = NULL;
       
   172         return NULL;
       
   173     }
       
   174 
       
   175 /*  AT direct_variable */
       
   176 //SYM_REF2(location_c, direct_variable, unused)
       
   177     void *visit(location_c *symbol) {
       
   178       if (test_location_type(symbol->direct_variable))
       
   179         symbol->direct_variable->accept(*this); 
       
   180       else
       
   181         ERROR;
       
   182       return NULL;
       
   183     }
       
   184 
       
   185 }; /* generate_location_list_c */