[Pvfs2-developers] state machine compiler tweaks

Pete Wyckoff pw at osc.edu
Mon May 29 11:19:38 EDT 2006


bradles at parl.clemson.edu wrote on Mon, 29 May 2006 10:33 -0400:
> Pete Wyckoff wrote:
> 
> >Another possible way to fix this is by using named initializers,
> >like
> >
> >    static union PINT_state_array_values ST_setup_msgpairs[] = {
> >	{ .state_name = "setup_msgpairs" },
> >	{ .parent_machine = &pvfs2_client_small_io_sm },
> >	{ .flag = SM_NONE },
> >	...
> >
> >which would be just dandy, but not all compilers have caught up with
> >this nice C99-ism.  I didn't want to exclude old compilers, or
> >support two versions and add a configure check, so rejected this
> >idea.
> >
> 
> I wondered when this became legal -- I love it!  It is already being 
> used in the distribution code (that is where I learned about it), so I 
> guess it is okay to begin using everywhere.

Right you are, I had totally forgotten about that usage.  Thus it
seems we have implicitly decided that this feature in a compiler is
mandatory.  I think that rules out ancient things like gcc 2.95 and
some vendor compilers, but that's fine by me.

This new version has my vote:

    struct PINT_state_machine_s pvfs2_client_small_io_sm = {
	    .name = "pvfs2_client_small_io_sm",
	    .state_machine = ST_setup_msgpairs
    };

    static union PINT_state_array_values ST_setup_msgpairs[] = {
	    { .state_name = "setup_msgpairs" },
	    { .parent_machine = &pvfs2_client_small_io_sm },
	    { .flag = SM_NONE },
	    { .state_action = small_io_setup_msgpairs },
	    { .return_value = 0 },
	    { .next_state = ST_xfer_msgpairs },
	    { .return_value = -1 },
	    { .flag = SM_RETURN }
    };  

Thanks for pointing this out Brad.  I'll commit it in a day or so
unless there are any strong objections.  Diff attached.

		-- Pete
-------------- next part --------------
Index: src/common/misc/state-machine-fns.h
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/common/misc/state-machine-fns.h,v
retrieving revision 1.18
diff -u -p -r1.18 state-machine-fns.h
--- src/common/misc/state-machine-fns.h	14 Dec 2005 21:50:19 -0000	1.18
+++ src/common/misc/state-machine-fns.h	29 May 2006 15:16:30 -0000
@@ -78,8 +78,8 @@ static inline int PINT_state_machine_nex
     int code_val = r->error_code;       /* temp to hold the return code */
     int retval;            /* temp to hold return value of state action */
     union PINT_state_array_values *loc; /* temp pointer into state memory */
-    char * state_name;
-    char * machine_name;
+    const char * state_name;
+    const char * machine_name;
 
     do {
 	/* skip over the current state action to get to the return code list */
@@ -135,10 +135,7 @@ static inline int PINT_state_machine_nex
 	s->current_state += 1; /* skip state flag; now we point to the state
 				* machine */
 
-	/* NOTE: nested_machine is defined as a void * to eliminate a nasty
-	 * cross-structure dependency that I couldn't handle any more.  -- Rob
-	 */
-	s->current_state = ((struct PINT_state_machine_s *) s->current_state->nested_machine)->state_machine;
+	s->current_state = s->current_state->nested_machine->state_machine;
         s->current_state += 2;
     }
 
@@ -170,8 +167,8 @@ static inline int PINT_state_machine_inv
     struct PINT_OP_STATE *s, job_status_s *r)
 {
     int retval;
-    char * state_name;
-    char * machine_name;
+    const char * state_name;
+    const char * machine_name;
 
     state_name = PINT_state_machine_current_state_name(s);
     machine_name = PINT_state_machine_current_machine_name(s);
Index: src/common/misc/state-machine.h
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/common/misc/state-machine.h,v
retrieving revision 1.11
diff -u -p -r1.11 state-machine.h
--- src/common/misc/state-machine.h	14 Dec 2005 21:50:19 -0000	1.11
+++ src/common/misc/state-machine.h	29 May 2006 15:16:30 -0000
@@ -45,19 +45,19 @@
 
 union PINT_state_array_values
 {
-    char *state_name;
+    const char *state_name;
     struct PINT_state_machine_s *parent_machine;
     int (*state_action)(struct PINT_OP_STATE *, job_status_s *);
     int return_value;
     int flag;
-    struct PINT_state_machine_s *nested_machine; /* NOTE: this is really a PINT_state_machine * (void *)*/
+    struct PINT_state_machine_s *nested_machine;
     union PINT_state_array_values *next_state;
 };
 
 struct PINT_state_machine_s
 {
+    const char *name;
     union PINT_state_array_values *state_machine;
-    char *name;
 };
 
 enum {
Index: src/common/statecomp/codegen.c
===================================================================
RCS file: /projects/cvsroot/pvfs2/src/common/statecomp/codegen.c,v
retrieving revision 1.19
diff -u -p -r1.19 codegen.c
--- src/common/statecomp/codegen.c	14 Dec 2005 21:50:20 -0000	1.19
+++ src/common/statecomp/codegen.c	29 May 2006 15:16:30 -0000
@@ -17,7 +17,7 @@
 
 extern FILE *out_file;
 extern int terminate_path_flag;
-static char * current_machine;
+static char *current_machine;
 
 void gen_init(void);
 void gen_state_decl(char *state_name);
@@ -35,24 +35,26 @@ void gen_init(void)
 
 void gen_state_decl(char *state_name)
 {
-    fprintf(out_file,"static union PINT_state_array_values ST_%s[];\n", state_name);
+    fprintf(out_file, "static union PINT_state_array_values ST_%s[];\n",
+                      state_name);
 }
 
 void gen_machine(char *machine_name,
 		 char *first_state_name)
 {
     current_machine = machine_name;
-    fprintf(out_file, "\nstruct PINT_state_machine_s %s =\n{\n\t", machine_name);
-    fprintf(out_file, "ST_%s,\n\t\"%s\"\n", first_state_name, machine_name);
-    fprintf(out_file, "};\n");
+    fprintf(out_file, "\nstruct PINT_state_machine_s %s = {\n", machine_name);
+    fprintf(out_file, "\t.name = \"%s\",\n", machine_name);
+    fprintf(out_file, "\t.state_machine = ST_%s\n", first_state_name);
+    fprintf(out_file, "};\n\n");
 }
 
 void gen_state_start(char *state_name)
 {
     fprintf(out_file,
             "static union PINT_state_array_values ST_%s[] = {\n"
-            "(union PINT_state_array_values) \"%s\",\n"
-            "(union PINT_state_array_values) &%s,\n", 
+            "\t{ .state_name = \"%s\" },\n"
+            "\t{ .parent_machine = &%s },\n", 
             state_name, state_name, current_machine);
 }
 
@@ -65,20 +67,12 @@ void gen_state_action(char *run_func, in
 {
     switch (flag) {
 	case SM_NONE:
-	    fprintf(out_file,
-		    "(union PINT_state_array_values) %d",
-		    flag);
-	    fprintf(out_file,
-		    ",\n(union PINT_state_array_values) %s",
-		    run_func);
+	    fprintf(out_file, "\t{ .flag = SM_NONE },\n");
+            fprintf(out_file, "\t{ .state_action = %s }", run_func);
 	    break;
 	case SM_JUMP:
-	    fprintf(out_file,
-		    "(union PINT_state_array_values) %d",
-		    flag);
-	    fprintf(out_file,
-		    ",\n(union PINT_state_array_values) &%s",
-		    run_func);
+	    fprintf(out_file, "\t{ .flag = SM_JUMP },\n");
+            fprintf(out_file, "\t{ .nested_machine = &%s }", run_func);
 	    break;
 	default:
 	    fprintf(stderr,
@@ -90,30 +84,22 @@ void gen_state_action(char *run_func, in
 
 void gen_return_code(char *return_code)
 {
-    fprintf(out_file,
-	    ",\n(union PINT_state_array_values) %s",
-	    return_code);
+    fprintf(out_file, ",\n\t{ .return_value = %s }", return_code);
 }
 
 void gen_next_state(int flag, char *new_state)
 {
     switch (flag) {
 	case SM_NEXT:
-	    fprintf(out_file,
-		    ",\n(union PINT_state_array_values) ST_%s",
-		    new_state);
+	    fprintf(out_file, ",\n\t{ .next_state = ST_%s }", new_state);
 	    break;
 	case SM_RETURN:
 	    terminate_path_flag = 1;
-	    fprintf(out_file,
-		    ",\n(union PINT_state_array_values) %d",
-		    flag);
+	    fprintf(out_file, ",\n\t{ .flag = SM_RETURN }");
 	    break;
 	case SM_TERMINATE:
 	    terminate_path_flag = 1;
-	    fprintf(out_file,
-		    ",\n(union PINT_state_array_values) %d",
-		    flag);
+	    fprintf(out_file, ",\n\t{ .flag = SM_TERMINATE }");
 	    break;
 	default:
 	    fprintf(stderr,


More information about the Pvfs2-developers mailing list