stage4/generate_c/generate_c.cc
changeset 282 871edf12fe64
parent 279 c0453b7f99df
child 283 214d3fdee596
equal deleted inserted replaced
281:686fa44e3832 282:871edf12fe64
     1 /*
     1 /*
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
     2  *  matiec - a compiler for the programming languages defined in IEC 61131-3
     3  *
     3  *
     4  *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
     4  *  Copyright (C) 2003-2011  Mario de Sousa (msousa@fe.up.pt)
       
     5  *  Copyright (C) 2007-2011  Laurent Bessard and Edouard Tisserant
     5  *
     6  *
     6  *  This program is free software: you can redistribute it and/or modify
     7  *  This program is free software: you can redistribute it and/or modify
     7  *  it under the terms of the GNU General Public License as published by
     8  *  it under the terms of the GNU General Public License as published by
     8  *  the Free Software Foundation, either version 3 of the License, or
     9  *  the Free Software Foundation, either version 3 of the License, or
     9  *  (at your option) any later version.
    10  *  (at your option) any later version.
    19  *
    20  *
    20  * This code is made available on the understanding that it will not be
    21  * This code is made available on the understanding that it will not be
    21  * used in safety-critical situations without a full and competent review.
    22  * used in safety-critical situations without a full and competent review.
    22  */
    23  */
    23 
    24 
    24 /*
       
    25  * An IEC 61131-3 compiler.
       
    26  *
       
    27  * Based on the
       
    28  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
       
    29  *
       
    30  */
       
    31 
       
    32 
       
    33 /*
       
    34  * This is one of the versions available for the 4th stage.
       
    35  *
       
    36  * This 4th stage generates a c++ source program equivalent
       
    37  * to the IL and ST code.
       
    38  */
       
    39 
       
    40 
       
    41 
       
    42 
       
    43 
       
    44 
       
    45 // #include <stdio.h>  /* required for NULL */
       
    46 #include <string>
    25 #include <string>
    47 #include <iostream>
    26 #include <iostream>
    48 #include <sstream>
    27 #include <sstream>
    49 #include <typeinfo>
    28 #include <typeinfo>
    50 #include <list>
    29 #include <list>
    55 #include "../../absyntax/visitor.hh"
    34 #include "../../absyntax/visitor.hh"
    56 #include "../../absyntax_utils/absyntax_utils.hh"
    35 #include "../../absyntax_utils/absyntax_utils.hh"
    57 
    36 
    58 #include "../stage4.hh"
    37 #include "../stage4.hh"
    59 
    38 
    60 
       
    61 
       
    62 
       
    63 
       
    64 
       
    65 
       
    66 //#define DEBUG
    39 //#define DEBUG
    67 #ifdef DEBUG
    40 #ifdef DEBUG
    68 #define TRACE(classname) printf("\n____%s____\n",classname);
    41 #define TRACE(classname) printf("\n____%s____\n",classname);
    69 #else
    42 #else
    70 #define TRACE(classname)
    43 #define TRACE(classname)
    71 #endif
    44 #endif
    72 
    45 
    73 
       
    74 
       
    75 #define ERROR error_exit(__FILE__,__LINE__)
    46 #define ERROR error_exit(__FILE__,__LINE__)
    76 /* function defined in main.cc */
    47 /* function defined in main.cc */
    77 extern void error_exit(const char *file_name, int line_no);
    48 extern void error_exit(const char *file_name, int line_no);
    78 
    49 
    79 
       
    80 
       
    81 
       
    82 /***********************************************************************/
       
    83 /***********************************************************************/
       
    84 /***********************************************************************/
       
    85 /***********************************************************************/
    50 /***********************************************************************/
    86 
    51 
    87 /* Unlike Programs and Configurations which get mapped onto C++ classes,
    52 /* Unlike Programs and Configurations which get mapped onto C++ classes,
    88  * Function Blocks are mapped onto a C structure containing the variables, and
    53  * Function Blocks are mapped onto a C structure containing the variables, and
    89  * a C function containing the code in the FB's body. This is to allow direct allocation
    54  * a C function containing the code in the FB's body. This is to allow direct allocation
  1081   s4o.print(s4o.indent_spaces + "void config");
  1046   s4o.print(s4o.indent_spaces + "void config");
  1082   s4o.print(FB_INIT_SUFFIX);
  1047   s4o.print(FB_INIT_SUFFIX);
  1083   s4o.print("(void) {\n");
  1048   s4o.print("(void) {\n");
  1084   s4o.indent_right();
  1049   s4o.indent_right();
  1085   s4o.print(s4o.indent_spaces);
  1050   s4o.print(s4o.indent_spaces);
  1086   s4o.print("BOOL retain = 0;\n");
  1051   s4o.print("BOOL retain;\n");
       
  1052   s4o.print(s4o.indent_spaces);
       
  1053   s4o.print("retain = 0;\n");
  1087   
  1054   
  1088   /* (B.3) Global variables initializations... */
  1055   /* (B.3) Global variables initializations... */
  1089   s4o.print(s4o.indent_spaces);
  1056   s4o.print(s4o.indent_spaces);
  1090   vardecl = new generate_c_vardecl_c(&s4o,
  1057   vardecl = new generate_c_vardecl_c(&s4o,
  1091                                      generate_c_vardecl_c::constructorinit_vf,
  1058                                      generate_c_vardecl_c::constructorinit_vf,
  1369       current_resource_name->accept(*this);
  1336       current_resource_name->accept(*this);
  1370       s4o.print(FB_INIT_SUFFIX);
  1337       s4o.print(FB_INIT_SUFFIX);
  1371       s4o.print("(void) {\n");
  1338       s4o.print("(void) {\n");
  1372       s4o.indent_right();
  1339       s4o.indent_right();
  1373       s4o.print(s4o.indent_spaces);
  1340       s4o.print(s4o.indent_spaces);
  1374       s4o.print("BOOL retain = 0;\n");
  1341       s4o.print("BOOL retain;\n");
       
  1342       s4o.print(s4o.indent_spaces);
       
  1343       s4o.print("retain = 0;\n");
  1375       
  1344       
  1376       /* (B.2) Global variables initialisations... */
  1345       /* (B.2) Global variables initialisations... */
  1377       if (current_global_vars != NULL) {
  1346       if (current_global_vars != NULL) {
  1378         s4o.print(s4o.indent_spaces);
  1347         s4o.print(s4o.indent_spaces);
  1379         vardecl = new generate_c_vardecl_c(&s4o,
  1348         vardecl = new generate_c_vardecl_c(&s4o,