stage4/generate_c/generate_c.cc
changeset 1071 7fd69f29320a
parent 1061 f762b43c8fad
child 1072 2dc67df52e59
equal deleted inserted replaced
1061:f762b43c8fad 1071:7fd69f29320a
  2146       s4o.print("void ");
  2146       s4o.print("void ");
  2147       symbol->resource_name->accept(*this);
  2147       symbol->resource_name->accept(*this);
  2148       s4o.print("_restore__" "(void **buffer, int *maxsize);\n");      
  2148       s4o.print("_restore__" "(void **buffer, int *maxsize);\n");      
  2149       return NULL;
  2149       return NULL;
  2150     }
  2150     }
       
  2151     
       
  2152     
       
  2153     void *visit(single_resource_declaration_c *symbol) {
       
  2154       /* __Must__ not insert any code! */
       
  2155       /* sinlge resources will not create a specific function for the resource */
       
  2156       /* backup and restore opertions will be inserted together with the configuration! */
       
  2157       return NULL;
       
  2158     }
       
  2159     
  2151 };
  2160 };
  2152 
  2161 
  2153 
  2162 
  2154 /* print out the begining of the generic backup/restore function */
  2163 /* print out the begining of the generic backup/restore function */
  2155 void print_backup_restore_function_beg(stage4out_c &s4o, const char *func_name, const char *operation) {
  2164 void print_backup_restore_function_beg(stage4out_c &s4o, const char *func_name, const char *operation) {
  2192   s4o.indent_left();
  2201   s4o.indent_left();
  2193   s4o.print("}\n");      
  2202   s4o.print("}\n");      
  2194 }
  2203 }
  2195 
  2204 
  2196 
  2205 
       
  2206 
       
  2207 
       
  2208 
       
  2209 /* generate the backup/restore function for a RESOURCE */
       
  2210 /* the backup/restore function generated here will be called by the backup/restore
       
  2211  * function generated for the configuration in which the resource is embedded
       
  2212  */
       
  2213 class generate_c_backup_resource_c: public generate_c_base_and_typeid_c {
       
  2214   public:    
       
  2215     const char *operation;
       
  2216     
       
  2217     generate_c_backup_resource_c(stage4out_c *s4o_ptr)
       
  2218       : generate_c_base_and_typeid_c(s4o_ptr) {
       
  2219       operation = NULL;
       
  2220     };
       
  2221 
       
  2222   
       
  2223     virtual ~generate_c_backup_resource_c(void) {}
       
  2224 
       
  2225     
       
  2226 private:
       
  2227     void print_forward_declarations(void) {
       
  2228       s4o.print("\n\n\n");
       
  2229   
       
  2230       s4o.print("void ");
       
  2231       s4o.print("_backup__");
       
  2232       s4o.print("(void *varptr, int varsize, void **buffer, int *maxsize);\n");
       
  2233       s4o.print("void ");
       
  2234       s4o.print("_restore__");
       
  2235       s4o.print("(void *varptr, int varsize, void **buffer, int *maxsize);\n");
       
  2236   
       
  2237       s4o.print("\n\n\n");
       
  2238       s4o.print("#undef " DECLARE_GLOBAL          "\n");
       
  2239       s4o.print("#undef " DECLARE_GLOBAL_FB       "\n");
       
  2240       s4o.print("#undef " DECLARE_GLOBAL_LOCATION "\n");
       
  2241       s4o.print("#undef " DECLARE_GLOBAL_LOCATED  "\n");
       
  2242     }
       
  2243 
       
  2244   public:
       
  2245     /********************/
       
  2246     /* 2.1.6 - Pragmas  */
       
  2247     /********************/
       
  2248     void *visit(enable_code_generation_pragma_c * symbol)   {s4o.enable_output(); return NULL;}
       
  2249     void *visit(disable_code_generation_pragma_c * symbol)  {s4o.disable_output();return NULL;}
       
  2250         
       
  2251         
       
  2252     /********************************/
       
  2253     /* B 1.7 Configuration elements */
       
  2254     /********************************/
       
  2255     void *visit(resource_declaration_c *symbol) {
       
  2256       char *resource_name = strdup(symbol->resource_name->token->value);
       
  2257       /* convert to upper case */
       
  2258       for (char *c = resource_name; *c != '\0'; *c = toupper(*c), c++);
       
  2259       
       
  2260       generate_c_vardecl_c vardecl = generate_c_vardecl_c(&s4o,
       
  2261                                          generate_c_vardecl_c::local_vf,
       
  2262                                          generate_c_vardecl_c::global_vt,
       
  2263                                          symbol->resource_name);
       
  2264       
       
  2265       print_forward_declarations();
       
  2266       
       
  2267       print_backup_restore_function_beg(s4o, resource_name, "_backup__");
       
  2268       if (symbol->global_var_declarations != NULL)   
       
  2269         vardecl.print(symbol->global_var_declarations);
       
  2270       if (symbol->resource_declaration != NULL) {
       
  2271         operation = "_backup__";
       
  2272         symbol->resource_declaration->accept(*this);  // will call visit(single_resource_declaration_c *)
       
  2273         operation = NULL;
       
  2274       }
       
  2275       print_backup_restore_function_end(s4o);      
       
  2276       
       
  2277       print_backup_restore_function_beg(s4o, resource_name, "_restore__");
       
  2278       if (symbol->global_var_declarations != NULL)
       
  2279         vardecl.print(symbol->global_var_declarations);
       
  2280       if (symbol->resource_declaration != NULL) {
       
  2281         operation = "_restore__";
       
  2282         symbol->resource_declaration->accept(*this);  // will call visit(single_resource_declaration_c *)
       
  2283         operation = NULL;
       
  2284       }
       
  2285       print_backup_restore_function_end(s4o);      
       
  2286 
       
  2287       return NULL;
       
  2288     }
       
  2289     
       
  2290     void *visit(single_resource_declaration_c *symbol) {
       
  2291       /* Must store the declared/instatiated PROGRAMS */
       
  2292       if (symbol->program_configuration_list != NULL)
       
  2293         symbol->program_configuration_list->accept(*this);
       
  2294       return NULL;
       
  2295     }
       
  2296     
       
  2297     /*  PROGRAM [RETAIN | NON_RETAIN] program_name [WITH task_name] ':' program_type_name ['(' prog_conf_elements ')'] */
       
  2298     // SYM_REF5(program_configuration_c, retain_option, program_name, task_name, program_type_name, prog_conf_elements)
       
  2299     void *visit(program_configuration_c *symbol) {
       
  2300       // generate the following source code:
       
  2301       // _xxxxxx__(&program_name, sizeof(program_name), buffer, maxsize);
       
  2302       s4o.print(s4o.indent_spaces);
       
  2303       s4o.print(operation); // call _restore__() or _backup__()
       
  2304       s4o.print("(&"); 
       
  2305       symbol->program_name->accept(*this);
       
  2306       s4o.print(", sizeof(");
       
  2307       symbol->program_name->accept(*this);
       
  2308       s4o.print("), buffer, maxsize);\n");
       
  2309       return NULL;
       
  2310     }
       
  2311 
       
  2312 
       
  2313 };
       
  2314 
       
  2315 
  2197 /* generate the backup/restore function for a CONFIGURATION */
  2316 /* generate the backup/restore function for a CONFIGURATION */
  2198 /* the generated function will backup/restore the global variables declared in the
  2317 /* the generated function will backup/restore the global variables declared in the
  2199  * configuration, and call the backup/restore functions of each embedded resource to do
  2318  * configuration, and call the backup/restore functions of each embedded resource to do
  2200  * the same for the global variables declared inside each resource.
  2319  * the same for the global variables declared inside each resource.
  2201  */
  2320  */
  2262       
  2381       
  2263       print_backup_restore_function_beg(s4o, "config", "_backup__");
  2382       print_backup_restore_function_beg(s4o, "config", "_backup__");
  2264       vardecl.print(symbol);
  2383       vardecl.print(symbol);
  2265       s4o.print("\n");
  2384       s4o.print("\n");
  2266       func_to_call = "_backup__";
  2385       func_to_call = "_backup__";
  2267       symbol->resource_declarations->accept(*this);
  2386       symbol->resource_declarations->accept(*this);  // will call resource_declaration_list_c or single_resource_declaration_c
  2268       func_to_call = NULL;
  2387       func_to_call = NULL;
  2269       print_backup_restore_function_end(s4o);      
  2388       print_backup_restore_function_end(s4o);      
  2270     
  2389     
  2271       print_backup_restore_function_beg(s4o, "config", "_restore__");
  2390       print_backup_restore_function_beg(s4o, "config", "_restore__");
  2272       vardecl.print(symbol);
  2391       vardecl.print(symbol);
  2273       s4o.print("\n");
  2392       s4o.print("\n");
  2274       func_to_call = "_restore__";
  2393       func_to_call = "_restore__";
  2275       symbol->resource_declarations->accept(*this);
  2394       symbol->resource_declarations->accept(*this);  // will call resource_declaration_list_c or single_resource_declaration_c
  2276       func_to_call = NULL;
  2395       func_to_call = NULL;
  2277       print_backup_restore_function_end(s4o);      
  2396       print_backup_restore_function_end(s4o);      
  2278       
  2397       
  2279       return NULL;
  2398       return NULL;
  2280     }
  2399     }
  2281     
  2400     
  2282     void *visit(resource_declaration_c *symbol) {
  2401     void *visit(resource_declaration_c *symbol) {
  2283       if (symbol->global_var_declarations == NULL)
       
  2284         return NULL;
       
  2285       s4o.print(s4o.indent_spaces);
  2402       s4o.print(s4o.indent_spaces);
  2286       symbol->resource_name->accept(*this);
  2403       symbol->resource_name->accept(*this);
  2287       s4o.print(func_to_call);
  2404       s4o.print(func_to_call);
  2288       s4o.print("(buffer, maxsize);\n");      
  2405       s4o.print("(buffer, maxsize);\n");      
  2289       return NULL;
  2406       return NULL;
  2290     }
  2407     }
  2291     
  2408     
  2292     void *visit(single_resource_declaration_c *symbol) {
  2409     void *visit(single_resource_declaration_c *symbol) {
  2293       /* A single resource never has global variables declared, so nothing to do! */
  2410       /* If the configuration does not have any resources, we must store/restore the declared program instances
       
  2411        * inside the backup() restore() functions created for the configuration.
       
  2412        */
       
  2413       generate_c_backup_resource_c handle_resource = generate_c_backup_resource_c(&s4o);
       
  2414       handle_resource.operation = func_to_call;
       
  2415       symbol->accept(handle_resource);
  2294       return NULL;
  2416       return NULL;
  2295     }
  2417     }
  2296 
  2418 
  2297 };
  2419 };
  2298 
  2420 
  2299 
       
  2300 
       
  2301 /* generate the backup/restore function for a RESOURCE */
       
  2302 /* the backup/restore function generated here will be called by the backup/restore
       
  2303  * function generated for the configuration in which the resource is embedded
       
  2304  */
       
  2305 class generate_c_backup_resource_c: public generate_c_base_and_typeid_c {
       
  2306   public:
       
  2307     generate_c_backup_resource_c(stage4out_c *s4o_ptr)
       
  2308       : generate_c_base_and_typeid_c(s4o_ptr) {
       
  2309     };
       
  2310 
       
  2311     virtual ~generate_c_backup_resource_c(void) {}
       
  2312 
       
  2313     
       
  2314   public:
       
  2315     /********************/
       
  2316     /* 2.1.6 - Pragmas  */
       
  2317     /********************/
       
  2318     void *visit(enable_code_generation_pragma_c * symbol)   {s4o.enable_output(); return NULL;}
       
  2319     void *visit(disable_code_generation_pragma_c * symbol)  {s4o.disable_output();return NULL;}
       
  2320         
       
  2321         
       
  2322     /********************************/
       
  2323     /* B 1.7 Configuration elements */
       
  2324     /********************************/
       
  2325     void *visit(resource_declaration_c *symbol) {
       
  2326       if (symbol->global_var_declarations == NULL)
       
  2327         return NULL;
       
  2328 
       
  2329       char *resource_name = strdup(symbol->resource_name->token->value);
       
  2330       /* convert to upper case */
       
  2331       for (char *c = resource_name; *c != '\0'; *c = toupper(*c), c++);
       
  2332       
       
  2333       generate_c_vardecl_c vardecl = generate_c_vardecl_c(&s4o,
       
  2334                                          generate_c_vardecl_c::local_vf,
       
  2335                                          generate_c_vardecl_c::global_vt,
       
  2336                                          symbol->resource_name);
       
  2337       s4o.print("\n\n\n");
       
  2338 
       
  2339       s4o.print("void ");
       
  2340       s4o.print("_backup__");
       
  2341       s4o.print("(void *varptr, int varsize, void **buffer, int *maxsize);\n");
       
  2342       s4o.print("void ");
       
  2343       s4o.print("_restore__");
       
  2344       s4o.print("(void *varptr, int varsize, void **buffer, int *maxsize);\n");
       
  2345 
       
  2346       s4o.print("\n\n\n");
       
  2347       s4o.print("#undef " DECLARE_GLOBAL          "\n");
       
  2348       s4o.print("#undef " DECLARE_GLOBAL_FB       "\n");
       
  2349       s4o.print("#undef " DECLARE_GLOBAL_LOCATION "\n");
       
  2350       s4o.print("#undef " DECLARE_GLOBAL_LOCATED  "\n");
       
  2351       
       
  2352       print_backup_restore_function_beg(s4o, resource_name, "_backup__");
       
  2353       vardecl.print(symbol->global_var_declarations);
       
  2354       print_backup_restore_function_end(s4o);      
       
  2355     
       
  2356       print_backup_restore_function_beg(s4o, resource_name, "_restore__");
       
  2357       vardecl.print(symbol->global_var_declarations);
       
  2358       print_backup_restore_function_end(s4o);      
       
  2359     
       
  2360       return NULL;
       
  2361     }
       
  2362     
       
  2363     void *visit(single_resource_declaration_c *symbol) {
       
  2364       /* A single resource never has global variables declared, so nothing to do! */
       
  2365       return NULL;
       
  2366     }
       
  2367 
       
  2368 };
       
  2369 
  2421 
  2370 
  2422 
  2371 /***********************************************************************/
  2423 /***********************************************************************/
  2372 /***********************************************************************/
  2424 /***********************************************************************/
  2373 /***********************************************************************/
  2425 /***********************************************************************/