stage4/generate_cc/generate_location_list.cc
changeset 30 83201ec94ef4
child 31 c6959b0f539d
equal deleted inserted replaced
29:3ba8ef691003 30:83201ec94ef4
       
     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_cc_base_c *generate_cc_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_cc_base = new generate_cc_base_c(s4o_ptr);
       
    68       current_var_type_symbol = NULL;
       
    69     }
       
    70     ~generate_location_list_c(void) {
       
    71       delete generate_cc_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       current_var_type_symbol->accept(*generate_cc_base);
       
   122       s4o.print(" ");
       
   123       /* Do not use print_token() as it will change everything into uppercase */
       
   124       s4o.printlocation(symbol->value);
       
   125       s4o.print("\n");
       
   126       return NULL;
       
   127     }
       
   128 
       
   129 
       
   130 /********************************************/
       
   131 /* B.1.4.3   Declaration and initilization  */
       
   132 /********************************************/
       
   133 
       
   134 /*  [variable_name] location ':' located_var_spec_init */
       
   135 /* variable_name -> may be NULL ! */
       
   136 //SYM_REF4(located_var_decl_c, variable_name, location, located_var_spec_init, unused)
       
   137     void *visit(located_var_decl_c *symbol) {
       
   138         current_var_type_symbol = spec_init_sperator_c::get_spec(symbol->located_var_spec_init);
       
   139         if (current_var_type_symbol == NULL)
       
   140           ERROR;
       
   141         
       
   142         current_var_type_symbol = (symbol_c *)(current_var_type_symbol->accept(search_base_type));
       
   143         if (current_var_type_symbol == NULL)
       
   144           ERROR;
       
   145         
       
   146         symbol->location->accept(*this);
       
   147         
       
   148         current_var_type_symbol = NULL;
       
   149         return NULL;
       
   150     }
       
   151 
       
   152 /*| global_var_spec ':' [located_var_spec_init|function_block_type_name] */
       
   153 /* type_specification ->may be NULL ! */
       
   154 //SYM_REF2(global_var_decl_c, global_var_spec, type_specification)
       
   155     void *visit(global_var_decl_c *symbol) {
       
   156         current_var_type_symbol = spec_init_sperator_c::get_spec(symbol->type_specification);
       
   157         if (current_var_type_symbol == NULL)
       
   158           ERROR;
       
   159         
       
   160         current_var_type_symbol = (symbol_c *)(current_var_type_symbol->accept(search_base_type));
       
   161         if (current_var_type_symbol == NULL)
       
   162           ERROR;
       
   163         
       
   164         symbol->global_var_spec->accept(*this);
       
   165         
       
   166         current_var_type_symbol = NULL;
       
   167         return NULL;
       
   168     }
       
   169 
       
   170 /*  AT direct_variable */
       
   171 //SYM_REF2(location_c, direct_variable, unused)
       
   172     void *visit(location_c *symbol) {
       
   173       if (test_location_type(symbol->direct_variable))
       
   174         symbol->direct_variable->accept(*this); 
       
   175       else
       
   176         ERROR;
       
   177       return NULL;
       
   178     }
       
   179 
       
   180 }; /* generate_location_list_c */