stage4/generate_c/generate_c.cc
changeset 1011 76175defb87b
parent 965 c9eeb67ba939
child 1016 91bef6704b44
equal deleted inserted replaced
1010:242907849850 1011:76175defb87b
   891             break;
   891             break;
   892         }
   892         }
   893       }
   893       }
   894       s4o.print(";\n\n");
   894       s4o.print(";\n\n");
   895       
   895       
   896       s4o.print(s4o.indent_spaces + "// Control execution\n");
   896       
   897       s4o.print(s4o.indent_spaces + "if (!EN) {\n");
   897       // Only generate the code that controls the execution of the function's body if the
   898       s4o.indent_right();
   898       // function contains a declaration of both the EN and ENO variables
   899       s4o.print(s4o.indent_spaces + "if (__ENO != NULL) {\n");
   899       search_var_instance_decl_c search_var(symbol);
   900       s4o.indent_right();
   900       identifier_c  en_var("EN");
   901       s4o.print(s4o.indent_spaces + "*__ENO = __BOOL_LITERAL(FALSE);\n");
   901       identifier_c eno_var("ENO");
   902       s4o.indent_left();
   902       if (   (search_var.get_vartype(& en_var) == search_var_instance_decl_c::input_vt)
   903       s4o.print(s4o.indent_spaces + "}\n");
   903           && (search_var.get_vartype(&eno_var) == search_var_instance_decl_c::output_vt)) {
   904       s4o.print(s4o.indent_spaces + "return ");
   904         s4o.print(s4o.indent_spaces + "// Control execution\n");
   905       symbol->derived_function_name->accept(print_base);
   905         s4o.print(s4o.indent_spaces + "if (!EN) {\n");
   906       s4o.print(";\n");
   906         s4o.indent_right();
   907       s4o.indent_left();
   907         s4o.print(s4o.indent_spaces + "if (__ENO != NULL) {\n");
   908       s4o.print(s4o.indent_spaces + "}\n");
   908         s4o.indent_right();
       
   909         s4o.print(s4o.indent_spaces + "*__ENO = __BOOL_LITERAL(FALSE);\n");
       
   910         s4o.indent_left();
       
   911         s4o.print(s4o.indent_spaces + "}\n");
       
   912         s4o.print(s4o.indent_spaces + "return ");
       
   913         symbol->derived_function_name->accept(print_base);
       
   914         s4o.print(";\n");
       
   915         s4o.indent_left();
       
   916         s4o.print(s4o.indent_spaces + "}\n");
       
   917       }
   909     
   918     
   910       /* (C) Function body */
   919       /* (C) Function body */
   911       generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->derived_function_name, symbol);
   920       generate_c_SFC_IL_ST_c generate_c_code(&s4o, symbol->derived_function_name, symbol);
   912       symbol->function_body->accept(generate_c_code);
   921       symbol->function_body->accept(generate_c_code);
   913       
   922       
  1069       if (print_declaration) {
  1078       if (print_declaration) {
  1070         s4o.print(";\n");
  1079         s4o.print(";\n");
  1071       } else {
  1080       } else {
  1072         s4o.print(" {\n");
  1081         s4o.print(" {\n");
  1073         s4o.indent_right();
  1082         s4o.indent_right();
  1074       
  1083 
  1075         s4o.print(s4o.indent_spaces + "// Control execution\n");
  1084         // Only generate the code that controls the execution of the function's body if the
  1076         s4o.print(s4o.indent_spaces + "if (!");
  1085         // function contains a declaration of both the EN and ENO variables
  1077         s4o.print(GET_VAR);
  1086         search_var_instance_decl_c search_var(symbol);
  1078         s4o.print("(");
  1087         identifier_c  en_var("EN");
  1079         s4o.print(FB_FUNCTION_PARAM);
  1088         identifier_c eno_var("ENO");
  1080         s4o.print("->EN)) {\n");
  1089         if (   (search_var.get_vartype(& en_var) == search_var_instance_decl_c::input_vt)
  1081         s4o.indent_right();
  1090             && (search_var.get_vartype(&eno_var) == search_var_instance_decl_c::output_vt)) {
  1082         s4o.print(s4o.indent_spaces);
  1091 
  1083         s4o.print(SET_VAR);
  1092           s4o.print(s4o.indent_spaces + "// Control execution\n");
  1084         s4o.print("(");
  1093           s4o.print(s4o.indent_spaces + "if (!");
  1085         s4o.print(FB_FUNCTION_PARAM);
  1094           s4o.print(GET_VAR);
  1086         s4o.print("->,ENO,,__BOOL_LITERAL(FALSE));\n");
  1095           s4o.print("(");
  1087         s4o.print(s4o.indent_spaces + "return;\n");
  1096           s4o.print(FB_FUNCTION_PARAM);
  1088         s4o.indent_left();
  1097           s4o.print("->EN)) {\n");
  1089         s4o.print(s4o.indent_spaces + "}\n");
  1098           s4o.indent_right();
  1090         s4o.print(s4o.indent_spaces + "else {\n");
  1099           s4o.print(s4o.indent_spaces);
  1091         s4o.indent_right();
  1100           s4o.print(SET_VAR);
  1092         s4o.print(s4o.indent_spaces);
  1101           s4o.print("(");
  1093         s4o.print(SET_VAR);
  1102           s4o.print(FB_FUNCTION_PARAM);
  1094         s4o.print("(");
  1103           s4o.print("->,ENO,,__BOOL_LITERAL(FALSE));\n");
  1095         s4o.print(FB_FUNCTION_PARAM);
  1104           s4o.print(s4o.indent_spaces + "return;\n");
  1096         s4o.print("->,ENO,,__BOOL_LITERAL(TRUE));\n");
  1105           s4o.indent_left();
  1097         s4o.indent_left();
  1106           s4o.print(s4o.indent_spaces + "}\n");
  1098         s4o.print(s4o.indent_spaces + "}\n");
  1107           s4o.print(s4o.indent_spaces + "else {\n");
       
  1108           s4o.indent_right();
       
  1109           s4o.print(s4o.indent_spaces);
       
  1110           s4o.print(SET_VAR);
       
  1111           s4o.print("(");
       
  1112           s4o.print(FB_FUNCTION_PARAM);
       
  1113           s4o.print("->,ENO,,__BOOL_LITERAL(TRUE));\n");
       
  1114           s4o.indent_left();
       
  1115           s4o.print(s4o.indent_spaces + "}\n");
       
  1116         }
  1099       
  1117       
  1100         /* (C.4) Initialize TEMP variables */
  1118         /* (C.4) Initialize TEMP variables */
  1101         /* function body */
  1119         /* function body */
  1102         s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n");
  1120         s4o.print(s4o.indent_spaces + "// Initialise TEMP variables\n");
  1103         vardecl = new generate_c_vardecl_c(&s4o,
  1121         vardecl = new generate_c_vardecl_c(&s4o,
  1378   /* Insert the header... */
  1396   /* Insert the header... */
  1379   s4o.print("/*******************************************/\n");
  1397   s4o.print("/*******************************************/\n");
  1380   s4o.print("/*     FILE GENERATED BY iec2c             */\n");
  1398   s4o.print("/*     FILE GENERATED BY iec2c             */\n");
  1381   s4o.print("/* Editing this file is not recommended... */\n");
  1399   s4o.print("/* Editing this file is not recommended... */\n");
  1382   s4o.print("/*******************************************/\n\n");
  1400   s4o.print("/*******************************************/\n\n");
       
  1401   
       
  1402   if (runtime_options.disable_implicit_en_eno) {
       
  1403     // If we are not generating the EN and ENO parameters for functions and FB,
       
  1404     //   then make sure we use the standard library version compiled without these parameters too!
       
  1405     s4o.print("#ifndef DISABLE_EN_ENO_PARAMETERS\n");
       
  1406     s4o.print("#define DISABLE_EN_ENO_PARAMETERS\n");
       
  1407     s4o.print("#endif\n");
       
  1408   }
       
  1409   
  1383   s4o.print("#include \"iec_std_lib.h\"\n\n");
  1410   s4o.print("#include \"iec_std_lib.h\"\n\n");
  1384   s4o.print("#include \"accessor.h\"\n\n"); 
  1411   s4o.print("#include \"accessor.h\"\n\n"); 
  1385   s4o.print("#include \"POUS.h\"\n\n");
  1412   s4o.print("#include \"POUS.h\"\n\n");
  1386 
  1413 
  1387   /* (A) configuration declaration... */
  1414   /* (A) configuration declaration... */
  1683       /* Insert the header... */
  1710       /* Insert the header... */
  1684       s4o.print("/*******************************************/\n");
  1711       s4o.print("/*******************************************/\n");
  1685       s4o.print("/*     FILE GENERATED BY iec2c             */\n");
  1712       s4o.print("/*     FILE GENERATED BY iec2c             */\n");
  1686       s4o.print("/* Editing this file is not recommended... */\n");
  1713       s4o.print("/* Editing this file is not recommended... */\n");
  1687       s4o.print("/*******************************************/\n\n");
  1714       s4o.print("/*******************************************/\n\n");
       
  1715   
       
  1716       if (runtime_options.disable_implicit_en_eno) {
       
  1717         // If we are not generating the EN and ENO parameters for functions and FB,
       
  1718         //   then make sure we use the standard library version compiled without these parameters too!
       
  1719         s4o.print("#ifndef DISABLE_EN_ENO_PARAMETERS\n");
       
  1720         s4o.print("#define DISABLE_EN_ENO_PARAMETERS\n");
       
  1721         s4o.print("#endif\n");
       
  1722       }
       
  1723       
  1688       s4o.print("#include \"iec_std_lib.h\"\n\n");
  1724       s4o.print("#include \"iec_std_lib.h\"\n\n");
  1689       
  1725       
  1690       /* (A) resource declaration... */
  1726       /* (A) resource declaration... */
  1691       /* (A.1) resource name in comment */
  1727       /* (A.1) resource name in comment */
  1692       s4o.print("// RESOURCE ");
  1728       s4o.print("// RESOURCE ");
  2116 
  2152 
  2117 /***************************/
  2153 /***************************/
  2118 /* B 0 - Programming Model */
  2154 /* B 0 - Programming Model */
  2119 /***************************/
  2155 /***************************/
  2120     void *visit(library_c *symbol) {
  2156     void *visit(library_c *symbol) {
  2121       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n#include \"accessor.h\"\n#include \"iec_std_lib.h\"\n\n");
  2157       pous_incl_s4o.print("#ifndef __POUS_H\n#define __POUS_H\n\n");
       
  2158       
       
  2159       if (runtime_options.disable_implicit_en_eno) {
       
  2160         // If we are not generating the EN and ENO parameters for functions and FB,
       
  2161         //   then make sure we use the standard library version compiled without these parameters too!
       
  2162         pous_incl_s4o.print("#ifndef DISABLE_EN_ENO_PARAMETERS\n");
       
  2163         pous_incl_s4o.print("#define DISABLE_EN_ENO_PARAMETERS\n");
       
  2164         pous_incl_s4o.print("#endif\n");
       
  2165       }
       
  2166       
       
  2167       pous_incl_s4o.print("#include \"accessor.h\"\n#include \"iec_std_lib.h\"\n\n");
  2122 
  2168 
  2123       for(int i = 0; i < symbol->n; i++) {
  2169       for(int i = 0; i < symbol->n; i++) {
  2124         symbol->elements[i]->accept(*this);
  2170         symbol->elements[i]->accept(*this);
  2125       }
  2171       }
  2126 
  2172