stage4/generate_c/generate_c.cc
changeset 184 16495e10f69a
parent 181 38d6eb056260
child 189 7ca02034c4f6
equal deleted inserted replaced
183:fb240c8d1f66 184:16495e10f69a
  1127   search_var_instance_decl_c *search_resource_instance;
  1127   search_var_instance_decl_c *search_resource_instance;
  1128 
  1128 
  1129   private:
  1129   private:
  1130     /* The name of the resource curretnly being processed... */
  1130     /* The name of the resource curretnly being processed... */
  1131     symbol_c *current_resource_name;
  1131     symbol_c *current_resource_name;
       
  1132     symbol_c *current_task_name;
  1132     symbol_c *current_global_vars;
  1133     symbol_c *current_global_vars;
  1133 
  1134 
  1134   public:
  1135   public:
  1135     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
  1136     generate_c_resources_c(stage4out_c *s4o_ptr, symbol_c *config_scope, symbol_c *resource_scope, unsigned long time)
  1136       : generate_c_typedecl_c(s4o_ptr) {
  1137       : generate_c_typedecl_c(s4o_ptr) {
  1137       search_config_instance = new search_var_instance_decl_c(config_scope);
  1138       search_config_instance = new search_var_instance_decl_c(config_scope);
  1138       search_resource_instance = new search_var_instance_decl_c(resource_scope);
  1139       search_resource_instance = new search_var_instance_decl_c(resource_scope);
  1139       common_ticktime = time;
  1140       common_ticktime = time;
  1140       current_resource_name = NULL;
  1141       current_resource_name = NULL;
       
  1142       current_task_name = NULL;
  1141       current_global_vars = NULL;
  1143       current_global_vars = NULL;
  1142     };
  1144     };
  1143     virtual ~generate_c_resources_c(void) {
  1145     virtual ~generate_c_resources_c(void) {
  1144       delete search_config_instance;
  1146       delete search_config_instance;
  1145       delete search_resource_instance;
  1147       delete search_resource_instance;
  1222       
  1224       
  1223       /* (A.3) POUs inclusion */
  1225       /* (A.3) POUs inclusion */
  1224       s4o.print("#include \"POUS.h\"\n\n");
  1226       s4o.print("#include \"POUS.h\"\n\n");
  1225       s4o.print("#include \"POUS.c\"\n\n");
  1227       s4o.print("#include \"POUS.c\"\n\n");
  1226       
  1228       
       
  1229       wanted_declaretype = declare_dt;
       
  1230       
  1227       /* (A.4) Resource programs declaration... */
  1231       /* (A.4) Resource programs declaration... */
  1228       wanted_declaretype = declare_dt;
  1232       symbol->task_configuration_list->accept(*this);
       
  1233       
       
  1234       /* (A.5) Resource programs declaration... */
  1229       symbol->program_configuration_list->accept(*this);
  1235       symbol->program_configuration_list->accept(*this);
       
  1236       
  1230       s4o.print("\n");
  1237       s4o.print("\n");
  1231       
  1238       
  1232       /* (B) resource initialisation function... */
  1239       /* (B) resource initialisation function... */
  1233       /* (B.1) initialisation function name... */
  1240       /* (B.1) initialisation function name... */
  1234       s4o.print("void ");
  1241       s4o.print("void ");
  1246         vardecl->print(current_global_vars);
  1253         vardecl->print(current_global_vars);
  1247         delete vardecl;
  1254         delete vardecl;
  1248       }
  1255       }
  1249       s4o.print("\n");
  1256       s4o.print("\n");
  1250       
  1257       
  1251       /* (B.3) Resource programs initialisations... */
       
  1252       wanted_declaretype = init_dt;
  1258       wanted_declaretype = init_dt;
       
  1259       
       
  1260       /* (B.3) Tasks initialisations... */
       
  1261       symbol->task_configuration_list->accept(*this);
       
  1262       
       
  1263       /* (B.4) Resource programs initialisations... */
  1253       symbol->program_configuration_list->accept(*this);
  1264       symbol->program_configuration_list->accept(*this);
  1254       
  1265       
  1255       s4o.indent_left();
  1266       s4o.indent_left();
  1256       s4o.print("}\n\n");
  1267       s4o.print("}\n\n");
  1257       
  1268       
  1261       current_resource_name->accept(*this);
  1272       current_resource_name->accept(*this);
  1262       s4o.print(FB_RUN_SUFFIX);
  1273       s4o.print(FB_RUN_SUFFIX);
  1263       s4o.print("(int tick) {\n");
  1274       s4o.print("(int tick) {\n");
  1264       s4o.indent_right();
  1275       s4o.indent_right();
  1265       
  1276       
       
  1277       wanted_declaretype = run_dt;
       
  1278       
  1266       /* (C.2) Task management... */
  1279       /* (C.2) Task management... */
  1267       symbol->task_configuration_list->accept(*this);
  1280       symbol->task_configuration_list->accept(*this);
  1268       
  1281       
  1269       /* (C.3) Program run declaration... */
  1282       /* (C.3) Program run declaration... */
  1270       wanted_declaretype = run_dt;
       
  1271       symbol->program_configuration_list->accept(*this);
  1283       symbol->program_configuration_list->accept(*this);
  1272       
  1284       
  1273       s4o.indent_left();
  1285       s4o.indent_left();
  1274       s4o.print("}\n\n");
  1286       s4o.print("}\n\n");
  1275       
  1287       
  1281     }
  1293     }
  1282     
  1294     
  1283 /*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
  1295 /*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
  1284 //SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
  1296 //SYM_REF6(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements, unused)
  1285     void *visit(program_configuration_c *symbol) {
  1297     void *visit(program_configuration_c *symbol) {
  1286       if (wanted_declaretype == declare_dt) {
  1298       switch (wanted_declaretype) {
  1287         s4o.print(s4o.indent_spaces);
  1299         case declare_dt:
  1288         symbol->program_type_name->accept(*this);
       
  1289         s4o.print(" ");
       
  1290         current_resource_name->accept(*this);
       
  1291         s4o.print("__");
       
  1292         symbol->program_name->accept(*this);
       
  1293         s4o.print(";\n#define ");
       
  1294         symbol->program_name->accept(*this);
       
  1295         s4o.print(" ");
       
  1296         current_resource_name->accept(*this);
       
  1297         s4o.print("__");
       
  1298         symbol->program_name->accept(*this);
       
  1299         s4o.print("\n");
       
  1300       }
       
  1301       if (wanted_declaretype == init_dt) {
       
  1302         s4o.print(s4o.indent_spaces);
       
  1303         symbol->program_type_name->accept(*this);
       
  1304         s4o.print(FB_INIT_SUFFIX);
       
  1305         s4o.print("(&");
       
  1306         symbol->program_name->accept(*this);
       
  1307         s4o.print(");\n");
       
  1308       }
       
  1309       if (wanted_declaretype == run_dt) {
       
  1310         current_program_name = ((identifier_c*)(symbol->program_name))->value;
       
  1311         if (symbol->task_name != NULL) {
       
  1312           s4o.print(s4o.indent_spaces);
  1300           s4o.print(s4o.indent_spaces);
  1313           s4o.print("if (!");
  1301           symbol->program_type_name->accept(*this);
  1314           symbol->task_name->accept(*this);
  1302           s4o.print(" ");
  1315           s4o.print(") {\n");
  1303           current_resource_name->accept(*this);
  1316           s4o.indent_right(); 
  1304           s4o.print("__");
  1317         }
  1305           symbol->program_name->accept(*this);
       
  1306           s4o.print(";\n#define ");
       
  1307           symbol->program_name->accept(*this);
       
  1308           s4o.print(" ");
       
  1309           current_resource_name->accept(*this);
       
  1310           s4o.print("__");
       
  1311           symbol->program_name->accept(*this);
       
  1312           s4o.print("\n");
       
  1313           break;
       
  1314         case init_dt:
       
  1315           s4o.print(s4o.indent_spaces);
       
  1316           symbol->program_type_name->accept(*this);
       
  1317           s4o.print(FB_INIT_SUFFIX);
       
  1318           s4o.print("(&");
       
  1319           symbol->program_name->accept(*this);
       
  1320           s4o.print(");\n");
       
  1321           break;
       
  1322         case run_dt:
       
  1323           current_program_name = ((identifier_c*)(symbol->program_name))->value;
       
  1324           if (symbol->task_name != NULL) {
       
  1325             s4o.print(s4o.indent_spaces);
       
  1326             s4o.print("if (");
       
  1327             symbol->task_name->accept(*this);
       
  1328             s4o.print(") {\n");
       
  1329             s4o.indent_right(); 
       
  1330           }
  1318         
  1331         
  1319         wanted_assigntype = assign_at;
  1332           wanted_assigntype = assign_at;
  1320         if (symbol->prog_conf_elements != NULL)
  1333           if (symbol->prog_conf_elements != NULL)
  1321           symbol->prog_conf_elements->accept(*this);
  1334             symbol->prog_conf_elements->accept(*this);
  1322         
  1335           
  1323         s4o.print(s4o.indent_spaces);
  1336           s4o.print(s4o.indent_spaces);
  1324         symbol->program_type_name->accept(*this);
  1337           symbol->program_type_name->accept(*this);
  1325         s4o.print(FB_FUNCTION_SUFFIX);
  1338           s4o.print(FB_FUNCTION_SUFFIX);
  1326         s4o.print("(&");
  1339           s4o.print("(&");
  1327         symbol->program_name->accept(*this);
  1340           symbol->program_name->accept(*this);
  1328         s4o.print(");\n");
  1341           s4o.print(");\n");
  1329         
  1342           
  1330         wanted_assigntype = send_at;
  1343           wanted_assigntype = send_at;
  1331         if (symbol->prog_conf_elements != NULL)
  1344           if (symbol->prog_conf_elements != NULL)
  1332           symbol->prog_conf_elements->accept(*this);
  1345             symbol->prog_conf_elements->accept(*this);
  1333         
  1346           
  1334         if (symbol->task_name != NULL) {
  1347           if (symbol->task_name != NULL) {
  1335           s4o.indent_left();
  1348             s4o.indent_left();
  1336           s4o.print(s4o.indent_spaces + "}\n");
  1349             s4o.print(s4o.indent_spaces + "}\n");
  1337         }
  1350           }
       
  1351           break;
       
  1352         default:
       
  1353           break;
  1338       }
  1354       }
  1339       return NULL;
  1355       return NULL;
  1340     }
  1356     }
  1341     
  1357     
  1342 /*  TASK task_name task_initialization */
  1358 /*  TASK task_name task_initialization */
  1343 //SYM_REF2(task_configuration_c, task_name, task_initialization)
  1359 //SYM_REF2(task_configuration_c, task_name, task_initialization)
  1344     void *visit(task_configuration_c *symbol) {
  1360     void *visit(task_configuration_c *symbol) {
  1345       s4o.print(s4o.indent_spaces + "int ");
  1361       current_task_name = symbol->task_name;
  1346       symbol->task_name->accept(*this);
  1362       switch (wanted_declaretype) {
  1347       s4o.print(" = ");
  1363         case declare_dt:
  1348       symbol->task_initialization->accept(*this);
  1364           s4o.print(s4o.indent_spaces + "BOOL ");
  1349       s4o.print(";\n");
  1365           current_task_name->accept(*this);
       
  1366           s4o.print(";\n");
       
  1367           symbol->task_initialization->accept(*this);
       
  1368           break;
       
  1369         case init_dt:
       
  1370           s4o.print(s4o.indent_spaces);
       
  1371           current_task_name->accept(*this);
       
  1372           s4o.print(" = __BOOL_LITERAL(FALSE);\n");
       
  1373           symbol->task_initialization->accept(*this);
       
  1374           break;
       
  1375         case run_dt:
       
  1376           symbol->task_initialization->accept(*this);
       
  1377           break;
       
  1378         default:
       
  1379           break;
       
  1380       }
       
  1381       current_task_name = NULL;
  1350       return NULL;
  1382       return NULL;
  1351     }
  1383     }
  1352     
  1384     
  1353 /*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
  1385 /*  '(' [SINGLE ASSIGN data_source ','] [INTERVAL ASSIGN data_source ','] PRIORITY ASSIGN integer ')' */
  1354 //SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
  1386 //SYM_REF4(task_initialization_c, single_data_source, interval_data_source, priority_data_source, unused)
  1355     void *visit(task_initialization_c *symbol) {
  1387     void *visit(task_initialization_c *symbol) {
  1356       if (symbol->interval_data_source != NULL) {
  1388       switch (wanted_declaretype) {
  1357         calculate_time_c calculate_time;
  1389         case declare_dt:
  1358         symbol->interval_data_source->accept(calculate_time);
  1390           if (symbol->single_data_source != NULL) {
  1359         unsigned long time = calculate_time.get_time();
  1391             s4o.print(s4o.indent_spaces + "R_TRIG ");
  1360         if (time != 0) {
  1392             current_task_name->accept(*this);
  1361           s4o.print("tick % ");
  1393             s4o.print("_R_TRIG;\n");
  1362           s4o.print_integer((int)(time / common_ticktime));
  1394           }
  1363         }
  1395           break;
  1364         else
  1396         case init_dt:
  1365           s4o.print("1");
  1397           if (symbol->single_data_source != NULL) {
       
  1398             s4o.print(s4o.indent_spaces + "R_TRIG");
       
  1399             s4o.print(FB_INIT_SUFFIX);
       
  1400             s4o.print("(&");
       
  1401             current_task_name->accept(*this);
       
  1402             s4o.print("_R_TRIG);\n");
       
  1403           }
       
  1404           break;
       
  1405         case run_dt:
       
  1406           if (symbol->single_data_source != NULL) {
       
  1407             symbol_c *config_var_decl = NULL;
       
  1408             symbol_c *res_var_decl = NULL;
       
  1409             symbol_c *current_var_reference = ((global_var_reference_c *)(symbol->single_data_source))->global_var_name;
       
  1410             res_var_decl = search_resource_instance->get_decl(current_var_reference);
       
  1411             if (res_var_decl == NULL) {
       
  1412               config_var_decl = search_config_instance->get_decl(current_var_reference);
       
  1413               if (config_var_decl == NULL)
       
  1414                 ERROR;
       
  1415               s4o.print(s4o.indent_spaces + "{extern ");
       
  1416               config_var_decl->accept(*this);
       
  1417               s4o.print(" *");
       
  1418               symbol->single_data_source->accept(*this);
       
  1419               s4o.print("; ");
       
  1420             }
       
  1421             else
       
  1422               s4o.print(s4o.indent_spaces);
       
  1423             current_task_name->accept(*this);
       
  1424             s4o.print("_R_TRIG.CLK = *");
       
  1425             symbol->single_data_source->accept(*this);
       
  1426             s4o.print(";");
       
  1427             if (config_var_decl != NULL)
       
  1428               s4o.print("}");
       
  1429             s4o.print("\n");
       
  1430             s4o.print(s4o.indent_spaces + "R_TRIG");
       
  1431             s4o.print(FB_FUNCTION_SUFFIX);
       
  1432             s4o.print("(&");
       
  1433             current_task_name->accept(*this);
       
  1434             s4o.print("_R_TRIG);\n");
       
  1435             s4o.print(s4o.indent_spaces);
       
  1436             current_task_name->accept(*this);
       
  1437             s4o.print(" = ");
       
  1438             current_task_name->accept(*this);
       
  1439             s4o.print("_R_TRIG.Q");
       
  1440           }
       
  1441           else {
       
  1442             s4o.print(s4o.indent_spaces);
       
  1443             current_task_name->accept(*this);
       
  1444             s4o.print(" = ");
       
  1445             if (symbol->interval_data_source != NULL) {
       
  1446               calculate_time_c calculate_time;
       
  1447               symbol->interval_data_source->accept(calculate_time);
       
  1448               unsigned long time = calculate_time.get_time();
       
  1449               if (time != 0) {
       
  1450                 s4o.print("!(tick % ");
       
  1451                 s4o.print_integer((int)(time / common_ticktime));
       
  1452                 s4o.print(")");
       
  1453               }
       
  1454               else
       
  1455                 s4o.print("1");
       
  1456             }
       
  1457             else 
       
  1458               s4o.print("1");
       
  1459           }
       
  1460           s4o.print(";\n");
       
  1461           break;
       
  1462         default:
       
  1463           break;
  1366       }
  1464       }
  1367       else
       
  1368         s4o.print("1");
       
  1369       return NULL;
  1465       return NULL;
  1370     }
  1466     }
  1371 
  1467 
  1372 /*  any_symbolic_variable ASSIGN prog_data_source */
  1468 /*  any_symbolic_variable ASSIGN prog_data_source */
  1373 //SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)
  1469 //SYM_REF2(prog_cnxn_assign_c, symbolic_variable, prog_data_source)