stage4/generate_c/generate_c_base.cc
changeset 945 477393b00f95
parent 936 0f7bcc160568
child 947 aca1ab9fcb6d
equal deleted inserted replaced
943:566414d7ba1f 945:477393b00f95
    24 
    24 
    25 #include <string.h>
    25 #include <string.h>
    26 
    26 
    27 
    27 
    28 
    28 
       
    29 /*  This file cotains two main classes:
       
    30  *   - generate_c_base_c
       
    31  *   - generate_c_base_and_typeid_c
       
    32  * 
       
    33  *  generate_c_base_c
       
    34  *  -----------------
       
    35  *   This class generates C code for all literals and varables. In short, all the basic stuff
       
    36  *   that will probably be needed in all other code generating classes.
       
    37  *   It does not however handle derived datatypes.
       
    38  *
       
    39  * generate_c_base_and_typeid_c
       
    40  * ----------------------------
       
    41  *   This is similar to the generate_c_base_c (from which it inherits), but it also handles
       
    42  *   all the derived datatypes. Note that it does not generate C code for the declaration of
       
    43  *   those datatypes (that is what generate_c_typedecl_c is for), but rather it merely
       
    44  *   generates the name/id of a derived datatype.
       
    45  *   Note too that not all derived datatypes in the IEC 61131-3 have a name (for example, 
       
    46  *   VAR a: ARRAY [3..5] of INT END_VAR), in which case an alias for this datatype should 
       
    47  *   have been previously generated by either generate_c_typedecl_c or generate_implicit_typedecl_c.
       
    48  */ 
       
    49 
       
    50 
       
    51 
       
    52 
    29 
    53 
    30 typedef struct
    54 typedef struct
    31 {
    55 {
    32   symbol_c *param_name;
    56   symbol_c *param_name;
    33   symbol_c *param_value;
    57   symbol_c *param_value;
    59   PARAM_LIST_ITERATOR()\
    83   PARAM_LIST_ITERATOR()\
    60     delete *pt;\
    84     delete *pt;\
    61   param_list.clear();
    85   param_list.clear();
    62 
    86 
    63 
    87 
       
    88 /*  generate_c_base_c
       
    89  *  -----------------
       
    90  *   This class generates C code for all literals and varables. In short, all the basic stuff
       
    91  *   that will probably be needed in all other code generating classes.
       
    92  *   It does not however handle derived datatypes.
       
    93  */
    64 class generate_c_base_c: public iterator_visitor_c {
    94 class generate_c_base_c: public iterator_visitor_c {
    65 
    95 
    66   protected:
    96   protected:
    67     stage4out_c &s4o;
    97     stage4out_c &s4o;
    68 
    98 
   296 /*************************/
   326 /*************************/
   297 /*******************************************/
   327 /*******************************************/
   298 /* B 1.1 - Letters, digits and identifiers */
   328 /* B 1.1 - Letters, digits and identifiers */
   299 /*******************************************/
   329 /*******************************************/
   300     void *visit(identifier_c *symbol) {return print_token(symbol);}
   330     void *visit(identifier_c *symbol) {return print_token(symbol);}
       
   331     /* If you need the derived_datatype_identifier_c visitor, then you should probably be 
       
   332      * inheriting from generate_c_base_and_typeid_c and not generate_c_base_c !!
       
   333      */
       
   334     void *visit(derived_datatype_identifier_c *symbol) {ERROR; return NULL;}
   301 
   335 
   302 /*********************/
   336 /*********************/
   303 /* B 1.2 - Constants */
   337 /* B 1.2 - Constants */
   304 /*********************/
   338 /*********************/
   305   /* originally empty... */
   339   /* originally empty... */
   592 /* B.1.3.2 - Generic data types */
   626 /* B.1.3.2 - Generic data types */
   593 /********************************/
   627 /********************************/
   594     /* Currently only used in REF_TO ANY, which is mapped onto (void *) */
   628     /* Currently only used in REF_TO ANY, which is mapped onto (void *) */
   595     void *visit(generic_type_any_c *symbol)      {s4o.print("void");    return NULL;}
   629     void *visit(generic_type_any_c *symbol)      {s4o.print("void");    return NULL;}
   596 
   630 
       
   631 
   597 /********************************/
   632 /********************************/
   598 /* B 1.3.3 - Derived data types */
   633 /* B 1.3.3 - Derived data types */
   599 /********************************/
   634 /********************************/
   600   /* leave for derived classes... */
   635 
   601 
       
   602   
       
   603 /* enumerated_type_name '#' identifier */
   636 /* enumerated_type_name '#' identifier */
   604 void *visit(enumerated_value_c *symbol) {
   637 void *visit(enumerated_value_c *symbol) {
   605   if (NULL == symbol->datatype) {
   638   if (NULL == symbol->datatype) {
   606     debug_c::print(symbol);
   639     debug_c::print(symbol);
   607     ERROR;
   640     ERROR;
   617   symbol->value->accept(*this);
   650   symbol->value->accept(*this);
   618   return NULL;
   651   return NULL;
   619 }
   652 }
   620 
   653 
   621 
   654 
   622 /*  identifier ':' array_spec_init */
       
   623 void *visit(array_type_declaration_c *symbol) {ERROR;/* Should never get called! */ return NULL;}
       
   624 
       
   625 /* array_specification [ASSIGN array_initialization] */
       
   626 /* array_initialization may be NULL ! */
       
   627 void *visit(array_spec_init_c *symbol) {
       
   628   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   629   if (implicit_id_count != 1) ERROR;
       
   630   /* this is part of an implicitly declared datatype (i.e. inside a variable decaration), for which an equivalent C datatype
       
   631    * has already been defined. So, we simly print out the id of that C datatpe...
       
   632    */
       
   633   return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   634 }
       
   635 
       
   636 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   637 void *visit(array_specification_c *symbol) {
       
   638   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   639   if (implicit_id_count != 1) ERROR;
       
   640   /* this is part of an implicitly declared datatype (i.e. inside a variable decaration), for which an equivalent C datatype
       
   641    * has already been defined. So, we simly print out the id of that C datatpe...
       
   642    */
       
   643   return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   644 }
       
   645 
       
   646 
       
   647 /* ref_spec:  REF_TO (non_generic_type_name | function_block_type_name) */
       
   648 void *visit(ref_spec_c *symbol) {
       
   649   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   650   if (implicit_id_count != 1) ERROR;
       
   651   /* this is part of an implicitly declared datatype (i.e. inside a variable decaration), for which an equivalent C datatype
       
   652    * has already been defined. So, we simly print out the id of that C datatpe...
       
   653    */
       
   654   return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   655 }
       
   656 
       
   657 /* For the moment, we do not support initialising reference data types */
       
   658 /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */ 
       
   659 /* NOTE: ref_initialization may be NULL!! */
       
   660 // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
       
   661 void *visit(ref_spec_init_c *symbol) {
       
   662   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   663   if (implicit_id_count != 1) ERROR;
       
   664   /* this is part of an implicitly declared datatype (i.e. inside a variable decaration), for which an equivalent C datatype
       
   665    * has already been defined. So, we simly print out the id of that C datatpe...
       
   666    */
       
   667   return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   668 }
       
   669 
       
   670 /* ref_type_decl: identifier ':' ref_spec_init */
       
   671 void *visit(ref_type_decl_c *symbol) {ERROR;/* Should never get called! */ return NULL;}
       
   672 
       
   673 
       
   674 
       
   675 
       
   676 
       
   677 /* NOTE:     visit(subrange_spec_init_c *)
       
   678  *      and  visit(subrange_specification_c *)
       
   679  *      together simply print out the integer datatype
       
   680  *      on which the subrange is based.
       
   681  *
       
   682  *      Future code clean-ups should delete these two
       
   683  *      visit mehotds, and make sure whoever calls them 
       
   684  *      uses symbol->datatype instead!
       
   685  */ 
       
   686 /* subrange_specification ASSIGN signed_integer */
       
   687 void *visit(subrange_spec_init_c *symbol) {
       
   688   TRACE("subrange_spec_init_c");
       
   689   symbol->subrange_specification->accept(*this);
       
   690   return NULL;
       
   691 }
       
   692 
       
   693 /*  integer_type_name '(' subrange')' */
       
   694 void *visit(subrange_specification_c *symbol) {
       
   695   TRACE("subrange_specification_c");
       
   696   symbol->integer_type_name->accept(*this);
       
   697   return NULL;
       
   698 }
       
   699   
       
   700 
       
   701 /* NOTE: Why is this here? This visit() method should not be here!!
       
   702  * TODO: Figure out who is dependent on this method, and move it to its correct location!
       
   703  */
       
   704 /* helper symbol for array_specification */
       
   705 /* array_subrange_list ',' subrange */
       
   706 void *visit(array_subrange_list_c *symbol) {
       
   707   TRACE("array_subrange_list_c");
       
   708   print_list(symbol);
       
   709   return NULL;
       
   710 }  
       
   711   
       
   712   
       
   713 /*********************/
   655 /*********************/
   714 /* B 1.4 - Variables */
   656 /* B 1.4 - Variables */
   715 /*********************/
   657 /*********************/
   716 void *visit(symbolic_variable_c *symbol) {
   658 void *visit(symbolic_variable_c *symbol) {
   717   TRACE("symbolic_variable_c");
   659   TRACE("symbolic_variable_c");
   718   this->print_variable_prefix();
   660   this->print_variable_prefix();
   719   symbol->var_name->accept(*this);
   661   symbol->var_name->accept(*this);
   720   return NULL;
   662   return NULL;
   721 }
   663 }
   722 
   664 
       
   665 
   723 /********************************************/
   666 /********************************************/
   724 /* B.1.4.1   Directly Represented Variables */
   667 /* B.1.4.1   Directly Represented Variables */
   725 /********************************************/
   668 /********************************************/
   726 void *visit(direct_variable_c *symbol) {
   669 void *visit(direct_variable_c *symbol) {
   727   TRACE("direct_variable_c");
   670   TRACE("direct_variable_c");
   728   /* Do not use print_token() as it will change everything into uppercase */
   671   /* Do not use print_token() as it will change everything into uppercase */
   729   return s4o.printlocation(symbol->value);
   672   return s4o.printlocation(symbol->value);
   730 }
   673 }
   731 
   674 
   732 
   675 
   733 
       
   734 /*************************************/
   676 /*************************************/
   735 /* B.1.4.2   Multi-element Variables */
   677 /* B.1.4.2   Multi-element Variables */
   736 /*************************************/
   678 /*************************************/
   737 #if 0
       
   738 /*  subscripted_variable '[' subscript_list ']' */
   679 /*  subscripted_variable '[' subscript_list ']' */
   739 SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   680 //SYM_REF2(array_variable_c, subscripted_variable, subscript_list)
   740 
   681 
   741 #endif
       
   742 
   682 
   743 /*  record_variable '.' field_selector */
   683 /*  record_variable '.' field_selector */
   744 /*  WARNING: input and/or output variables of function blocks
   684 /*  WARNING: input and/or output variables of function blocks
   745  *           may be accessed as fields of a structured variable!
   685  *           may be accessed as fields of a structured variable!
   746  *           Code handling a structured_variable_c must take
   686  *           Code handling a structured_variable_c must take
   843 
   783 
   844 
   784 
   845 
   785 
   846 
   786 
   847 
   787 
   848 
   788 /************************************************************************************************/
   849 
   789 /************************************************************************************************/
   850 
   790 /************************************************************************************************/
   851 
   791 /************************************************************************************************/
   852 
   792 /************************************************************************************************/
   853 
   793 /************************************************************************************************/
   854 
   794 /************************************************************************************************/
       
   795 /************************************************************************************************/
       
   796 /************************************************************************************************/
       
   797 /************************************************************************************************/
       
   798 
       
   799 
       
   800 
       
   801 /* generate_c_base_and_typeid_c
       
   802  * ----------------------------
       
   803  *   This is similar to the generate_c_base_c (from which it inherits), but it also handles
       
   804  *   all the derived datatypes. Note that it does not generate C code for the declaration of
       
   805  *   those datatypes (that is what generate_c_typedecl_c is for), but rather it merely
       
   806  *   generates the name/id of a derived datatype.
       
   807  *   Note too that not all derived datatypes in the IEC 61131-3 have a name (for example, 
       
   808  *   VAR a: ARRAY [3..5] of INT END_VAR), in which case an alias for this datatype should 
       
   809  *   have been previously generated by either generate_c_typedecl_c or generate_implicit_typedecl_c.
       
   810  */
       
   811 class generate_c_base_and_typeid_c: public generate_c_base_c {
       
   812 
       
   813   public:
       
   814      generate_c_base_and_typeid_c(stage4out_c *s4o_ptr): generate_c_base_c(s4o_ptr) {}
       
   815     ~generate_c_base_and_typeid_c(void) {}
       
   816 
       
   817 
       
   818 /*************************/
       
   819 /* B.1 - Common elements */
       
   820 /*************************/
       
   821 /*******************************************/
       
   822 /* B 1.1 - Letters, digits and identifiers */
       
   823 /*******************************************/
       
   824     void *visit(derived_datatype_identifier_c *symbol) {
       
   825       if (get_datatype_info_c::is_array(symbol->datatype)) {
       
   826         return symbol->datatype->accept(*this);
       
   827       }
       
   828       return print_token(symbol);
       
   829     }
       
   830 
       
   831 /*********************/
       
   832 /* B 1.2 - Constants */
       
   833 /*********************/
       
   834 /**********************/
       
   835 /* B.1.3 - Data types */
       
   836 /**********************/
       
   837 /***********************************/
       
   838 /* B 1.3.1 - Elementary Data Types */
       
   839 /***********************************/
       
   840 /********************************/
       
   841 /* B.1.3.2 - Generic data types */
       
   842 /********************************/
       
   843     /* Currently only used in REF_TO ANY, which is mapped onto (void *) */
       
   844     void *visit(generic_type_any_c *symbol)      {s4o.print("void");    return NULL;}
       
   845 
       
   846 /********************************/
       
   847 /* B 1.3.3 - Derived data types */
       
   848 /********************************/
       
   849 
       
   850 /*  subrange_type_name ':' subrange_spec_init */
       
   851 void *visit(subrange_type_declaration_c *symbol) {return symbol->subrange_type_name->accept(*this);}
       
   852 
       
   853 /* subrange_specification ASSIGN signed_integer */
       
   854 void *visit(subrange_spec_init_c *symbol) {return symbol->subrange_specification->accept(*this);}
       
   855 
       
   856 /*  integer_type_name '(' subrange')' */
       
   857 void *visit(subrange_specification_c *symbol) {return symbol->integer_type_name->accept(*this);}
       
   858 
       
   859 /*  enumerated_type_name ':' enumerated_spec_init */
       
   860 void *visit(enumerated_type_declaration_c *symbol) {return symbol->enumerated_type_name->accept(*this);}
       
   861 
       
   862 /* enumerated_specification ASSIGN enumerated_value */
       
   863 void *visit(enumerated_spec_init_c *symbol) {return symbol->enumerated_specification->accept(*this);}
       
   864 
       
   865 
       
   866 /* enumerated_type_name '#' identifier */
       
   867 /* Handled by generate_c_base_c class!!
       
   868 void *visit(enumerated_value_c *symbol) {}
       
   869 */
       
   870 
       
   871 /*  identifier ':' array_spec_init */
       
   872 void *visit(array_type_declaration_c *symbol) {
       
   873   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   874   if (1 != implicit_id_count) ERROR;
       
   875   return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   876 }
       
   877 
       
   878 
       
   879 
       
   880 /* array_specification [ASSIGN array_initialization] */
       
   881 /* array_initialization may be NULL ! */
       
   882 void *visit(array_spec_init_c *symbol) {
       
   883   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   884   if (1 == implicit_id_count) return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   885   if (0 == implicit_id_count) return symbol->datatype->accept(*this);
       
   886   return NULL;
       
   887 }
       
   888 
       
   889 /* ARRAY '[' array_subrange_list ']' OF non_generic_type_name */
       
   890 void *visit(array_specification_c *symbol) {
       
   891   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   892   if (1 != implicit_id_count) ERROR;
       
   893   return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   894 }
       
   895 
       
   896 
       
   897 
       
   898 /*  simple_type_name ':' simple_spec_init */
       
   899 void *visit(simple_type_declaration_c *symbol) {return symbol->simple_type_name->accept(*this);}
       
   900 
       
   901 /* simple_specification [ASSIGN constant] */
       
   902 //SYM_REF2(simple_spec_init_c, simple_specification, constant)
       
   903 // <constant> may be NULL
       
   904 void *visit(simple_spec_init_c *symbol) {return symbol->simple_specification->accept(*this);}
       
   905 
       
   906 /*  structure_type_name ':' structure_specification */
       
   907 //SYM_REF2(structure_type_declaration_c, structure_type_name, structure_specification)
       
   908 void *visit(structure_type_declaration_c *symbol) {return symbol->structure_type_name->accept(*this);}
       
   909 
       
   910 /* structure_type_name ASSIGN structure_initialization */
       
   911 /* structure_initialization may be NULL ! */
       
   912 //SYM_REF2(initialized_structure_c, structure_type_name, structure_initialization)
       
   913 void *visit(initialized_structure_c *symbol) {return symbol->structure_type_name->accept(*this);}
       
   914 
       
   915 
       
   916 
       
   917 /* ref_spec:  REF_TO (non_generic_type_name | function_block_type_name) */
       
   918 // SYM_REF1(ref_spec_c, type_name)
       
   919 void *visit(ref_spec_c *symbol) { 
       
   920   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   921   if (implicit_id_count  > 1) ERROR;
       
   922   if (implicit_id_count == 1) {
       
   923       /* this is part of an implicitly declared datatype (i.e. inside a variable decaration), for which an equivalent C datatype
       
   924        * has already been defined. So, we simly print out the id of that C datatpe...
       
   925        */
       
   926     return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   927   }
       
   928   /* This is NOT part of an implicitly declared datatype (i.e. we are being called from an visit(ref_type_decl_c *),
       
   929    * through the visit(ref_spec_init_c*)), so we need to simply print out the name of the datatype we reference to.
       
   930    */
       
   931   //debug_c::print(symbol); ERROR;
       
   932   symbol->type_name->accept(*this);
       
   933   s4o.print("*");
       
   934   return NULL;
       
   935 }
       
   936 
       
   937 /* For the moment, we do not support initialising reference data types */
       
   938 /* ref_spec_init: ref_spec [ ASSIGN ref_initialization ] */ 
       
   939 /* NOTE: ref_initialization may be NULL!! */
       
   940 // SYM_REF2(ref_spec_init_c, ref_spec, ref_initialization)
       
   941 void *visit(ref_spec_init_c *symbol) {
       
   942   /* NOTE  An ref_type_decl_c will be created in stage4 for each implicitly defined REF_TO datatype,
       
   943    *       and this generate_c_typedecl_c will be called to define that REF_TO datatype in C.
       
   944    *       However, every implictly defined REF_TO datatype with the exact same parameters will be mapped
       
   945    *       to the same identifier (e.g: __REF_TO_INT).
       
   946    *       In order for the C compiler not to find the same datatype being defined two or more times, 
       
   947    *       we will keep track of the datatypes that have already been declared, and henceforth
       
   948    *       only declare the datatypes that have not been previously defined.
       
   949    */
       
   950   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   951   if (1  < implicit_id_count) ERROR;
       
   952   if (1 == implicit_id_count)
       
   953     return symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(*this);
       
   954   return symbol->ref_spec->accept(*this); // this is probably pointing to an identifier_c !!
       
   955 }
       
   956 
       
   957 /* ref_type_decl: identifier ':' ref_spec_init */
       
   958 // SYM_REF2(ref_type_decl_c, ref_type_name, ref_spec_init)
       
   959 void *visit(ref_type_decl_c *symbol) {
       
   960   TRACE("ref_type_decl_c");
       
   961   /* NOTE  An ref_type_decl_c will be created in stage4 for each implicitly defined REF_TO datatype,
       
   962    *       and this generate_c_typedecl_c will be called to define that REF_TO datatype in C.
       
   963    *       However, every implictly defined REF_TO datatype with the exact same parameters will be mapped
       
   964    *       to the same identifier (e.g: __REF_TO_INT).
       
   965    *       In order for the C compiler not to find the same datatype being defined two or more times, 
       
   966    *       we will keep track of the datatypes that have already been declared, and henceforth
       
   967    *       only declare the datatypes that have not been previously defined.
       
   968    */
       
   969   int implicit_id_count = symbol->anotations_map.count("generate_c_annotaton__implicit_type_id");
       
   970   if (0 != implicit_id_count) ERROR;
       
   971   //symbol->anotations_map["generate_c_annotaton__implicit_type_id"]->accept(generate_c_base);
       
   972   return symbol->ref_type_name->accept(*this);
       
   973 }
       
   974 
       
   975 
       
   976 }; /* class generate_c_base_and_typeid_c */
       
   977 
       
   978 
       
   979 
       
   980 
       
   981 
       
   982 
       
   983 
       
   984 
       
   985 
       
   986 
       
   987 
       
   988 
       
   989