[m-rev.] refactor nondet stack traversal code

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Nov 12 21:49:44 AEDT 2003


Here is a relative and full diff for the other approach that
I mentioned in my earlier mail.  This one adds about 50 lines
of code, but keeps the external interface simple.  That's compared
with the one I posted earlier, which adds about 25 lines of code,
but also complicates the external interface.

My inclination is to just leave it as is, and not commit either of
these changes.  But I could be easily persuaded otherwise.

RELATIVE DIFF (relative to the one that I posted earlier)

diff -u runtime/mercury_stack_trace.c runtime/mercury_stack_trace.c
--- runtime/mercury_stack_trace.c	11 Nov 2003 10:03:52 -0000
+++ runtime/mercury_stack_trace.c	12 Nov 2003 10:38:06 -0000
@@ -33,10 +33,28 @@
 
 #ifndef MR_HIGHLEVEL_CODE
 
-static MR_Traverse_Nondet_Frame_Func MR_dump_nondet_stack_frame;
+typedef enum {
+    MR_FRAME_ON_MAIN_BRANCH,
+    MR_INTERNAL_FRAME_ON_SIDE_BRANCH,
+    MR_TOP_FRAME_ON_SIDE_BRANCH,
+    MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH
+} MR_Nondet_Frame_Category;
+
+typedef struct {
+    MR_Traverse_Nondet_Frame_Func *func;
+    void *func_data;
+} MR_Traverse_Nondet_Frame_Func_Info;
+
+typedef void MR_Dump_Or_Traverse_Nondet_Frame_Func(void *user_data,
+                MR_Nondet_Frame_Category category, MR_Word *top_fr,
+                const MR_Label_Layout *layout, MR_Word *base_sp,
+                MR_Word *base_curfr, int level_number);
+
+static MR_Dump_Or_Traverse_Nondet_Frame_Func MR_dump_nondet_stack_frame;
+static MR_Dump_Or_Traverse_Nondet_Frame_Func MR_traverse_nondet_stack_frame;
 static  const char  *MR_step_over_nondet_frame(
-                        MR_Traverse_Nondet_Frame_Func *func, void *func_data,
-                        int level_number, MR_Word *fr);
+                        MR_Dump_Or_Traverse_Nondet_Frame_Func *func,
+                        void *func_data, int level_number, MR_Word *fr);
 static void         MR_init_nondet_branch_infos(MR_Word *base_maxfr,
                     	const MR_Label_Layout *top_layout, MR_Word *base_sp,
                         MR_Word *base_curfr);
@@ -536,7 +554,11 @@
         } else {
             level_number++;
             if (base_maxfr > MR_nondet_stack_trace_bottom) {
-                problem = MR_step_over_nondet_frame(func, func_data,
+                MR_Traverse_Nondet_Frame_Func_Info func_info;
+                func_info.func = func;
+                func_info.func_data = func_data;
+                problem = MR_step_over_nondet_frame(
+                        MR_traverse_nondet_stack_frame, &func_info,
                         level_number, base_maxfr);
                 if (problem != NULL) {
                     MR_fatal_error(problem);
@@ -550,6 +572,18 @@
 }
 
 static void
+MR_traverse_nondet_stack_frame(void *info, MR_Nondet_Frame_Category category,
+    MR_Word *top_fr, const MR_Label_Layout *top_layout, MR_Word *base_sp,
+    MR_Word *base_curfr, int level_number)
+{
+    MR_Traverse_Nondet_Frame_Func_Info *func_info = info;
+    if (category != MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH) {
+        func_info->func(func_info->func_data, top_layout, base_sp, base_curfr,
+            level_number);
+    }
+}
+
+static void
 MR_init_nondet_branch_infos(MR_Word *base_maxfr,
 	const MR_Label_Layout *top_layout, MR_Word *base_sp, MR_Word *base_curfr)
 {
@@ -564,7 +598,7 @@
 }
 
 static const char *
-MR_step_over_nondet_frame(MR_Traverse_Nondet_Frame_Func *func,
+MR_step_over_nondet_frame(MR_Dump_Or_Traverse_Nondet_Frame_Func *func,
     void *func_data, int level_number, MR_Word *fr)
 {
     MR_Stack_Walk_Step_Result       result;
reverted:
--- runtime/mercury_stack_trace.h	11 Nov 2003 09:59:58 -0000
+++ runtime/mercury_stack_trace.h	11 Nov 2003 09:12:26 -0000	1.30
@@ -100,15 +100,7 @@
 **	function for each frame.
 */
 
-typedef enum {
-        MR_FRAME_ON_MAIN_BRANCH,
-        MR_INTERNAL_FRAME_ON_SIDE_BRANCH,
-        MR_TOP_FRAME_ON_SIDE_BRANCH,
-        MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH
-} MR_Nondet_Frame_Category;
-
 typedef void MR_Traverse_Nondet_Frame_Func(void *user_data,
-			MR_Nondet_Frame_Category category, MR_Word *top_fr,
 			const MR_Label_Layout *layout, MR_Word *base_sp,
 			MR_Word *base_curfr, int level_number);
 
FULL DIFF

/home/jupiter/fjh/ws-jupiter/mercury
cvs diff runtime/mercury_stack_trace.c
Index: runtime/mercury_stack_trace.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_stack_trace.c,v
retrieving revision 1.57
diff -u -d -r1.57 mercury_stack_trace.c
--- runtime/mercury_stack_trace.c	11 Nov 2003 09:12:26 -0000	1.57
+++ runtime/mercury_stack_trace.c	12 Nov 2003 10:38:06 -0000
@@ -33,10 +33,28 @@
 
 #ifndef MR_HIGHLEVEL_CODE
 
-static MR_Traverse_Nondet_Frame_Func MR_dump_nondet_stack_frame;
+typedef enum {
+    MR_FRAME_ON_MAIN_BRANCH,
+    MR_INTERNAL_FRAME_ON_SIDE_BRANCH,
+    MR_TOP_FRAME_ON_SIDE_BRANCH,
+    MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH
+} MR_Nondet_Frame_Category;
+
+typedef struct {
+    MR_Traverse_Nondet_Frame_Func *func;
+    void *func_data;
+} MR_Traverse_Nondet_Frame_Func_Info;
+
+typedef void MR_Dump_Or_Traverse_Nondet_Frame_Func(void *user_data,
+                MR_Nondet_Frame_Category category, MR_Word *top_fr,
+                const MR_Label_Layout *layout, MR_Word *base_sp,
+                MR_Word *base_curfr, int level_number);
+
+static MR_Dump_Or_Traverse_Nondet_Frame_Func MR_dump_nondet_stack_frame;
+static MR_Dump_Or_Traverse_Nondet_Frame_Func MR_traverse_nondet_stack_frame;
 static  const char  *MR_step_over_nondet_frame(
-                        MR_Traverse_Nondet_Frame_Func *func, void *func_data,
-                        FILE *fp, int level_number, MR_Word *fr);
+                        MR_Dump_Or_Traverse_Nondet_Frame_Func *func,
+                        void *func_data, int level_number, MR_Word *fr);
 static void         MR_init_nondet_branch_infos(MR_Word *base_maxfr,
                     	const MR_Label_Layout *top_layout, MR_Word *base_sp,
                         MR_Word *base_curfr);
@@ -449,7 +467,7 @@
             if (print_vars && base_maxfr > MR_nondet_stack_trace_bottom) {
                 problem = MR_step_over_nondet_frame(
                         MR_dump_nondet_stack_frame, fp,
-                        fp, level_number, base_maxfr);
+                        level_number, base_maxfr);
                 if (problem != NULL) {
                     fprintf(fp, "%s\n", problem);
                     return;
@@ -463,13 +481,42 @@
 }
 
 static void
-MR_dump_nondet_stack_frame(void *fp,
-    const MR_Label_Layout *top_layout, MR_Word *base_sp, MR_Word *base_curfr,
-    int level_number)
+MR_dump_nondet_stack_frame(void *fp, MR_Nondet_Frame_Category category,
+    MR_Word *top_fr, const MR_Label_Layout *top_layout, MR_Word *base_sp,
+    MR_Word *base_curfr, int level_number)
 {
-    /* XXX we ignore the return value */
-    (*MR_address_of_trace_browse_all_on_level) (fp, top_layout, base_sp,
-        base_curfr, level_number, MR_TRUE);
+    FILE *dump_fp = fp;
+
+    switch (category) {
+    case MR_INTERNAL_FRAME_ON_SIDE_BRANCH:
+        fprintf(dump_fp, " internal frame on nondet side branch ");
+        MR_printnondstackptr(top_fr);
+        fprintf(dump_fp, "\n");
+        break;
+    case MR_FRAME_ON_MAIN_BRANCH:
+        fprintf(dump_fp, " on main nondet branch ");
+        MR_printnondstackptr(top_fr);
+        fprintf(dump_fp, "\n");
+        break;
+    case MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH:
+        fprintf(dump_fp, " terminal top frame of a nondet side branch ");
+        MR_printnondstackptr(base_curfr);
+        fprintf(dump_fp, "\n");
+        break;
+    case MR_TOP_FRAME_ON_SIDE_BRANCH:
+        fprintf(dump_fp, " top frame of a nondet side branch ");
+        MR_printnondstackptr(base_curfr);
+        fprintf(dump_fp, "\n");
+        break;
+    default:
+        MR_fatal_error("invalid MR_Nondet_Frame_Category");
+    }
+
+    if (category != MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH) {
+        /* XXX we ignore the return value */
+        (*MR_address_of_trace_browse_all_on_level) (dump_fp, top_layout,
+                base_sp, base_curfr, level_number, MR_TRUE);
+    }
 }
 
 void
@@ -507,8 +554,12 @@
         } else {
             level_number++;
             if (base_maxfr > MR_nondet_stack_trace_bottom) {
-                problem = MR_step_over_nondet_frame(func, func_data,
-                        NULL, level_number, base_maxfr);
+                MR_Traverse_Nondet_Frame_Func_Info func_info;
+                func_info.func = func;
+                func_info.func_data = func_data;
+                problem = MR_step_over_nondet_frame(
+                        MR_traverse_nondet_stack_frame, &func_info,
+                        level_number, base_maxfr);
                 if (problem != NULL) {
                     MR_fatal_error(problem);
                 }
@@ -521,6 +572,18 @@
 }
 
 static void
+MR_traverse_nondet_stack_frame(void *info, MR_Nondet_Frame_Category category,
+    MR_Word *top_fr, const MR_Label_Layout *top_layout, MR_Word *base_sp,
+    MR_Word *base_curfr, int level_number)
+{
+    MR_Traverse_Nondet_Frame_Func_Info *func_info = info;
+    if (category != MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH) {
+        func_info->func(func_info->func_data, top_layout, base_sp, base_curfr,
+            level_number);
+    }
+}
+
+static void
 MR_init_nondet_branch_infos(MR_Word *base_maxfr,
 	const MR_Label_Layout *top_layout, MR_Word *base_sp, MR_Word *base_curfr)
 {
@@ -535,8 +598,8 @@
 }
 
 static const char *
-MR_step_over_nondet_frame(MR_Traverse_Nondet_Frame_Func *func,
-    void *func_data, FILE *dump_fp, int level_number, MR_Word *fr)
+MR_step_over_nondet_frame(MR_Dump_Or_Traverse_Nondet_Frame_Func *func,
+    void *func_data, int level_number, MR_Word *fr)
 {
     MR_Stack_Walk_Step_Result       result;
     MR_Determinism                  determinism;
@@ -551,24 +614,19 @@
     MR_Code                         *redoip;
     MR_Code                         *success;
     const char                      *problem;
+    MR_Nondet_Frame_Category        category;
 
     if (MR_find_matching_branch(fr, &branch)) {
         base_sp = MR_nondet_branch_infos[branch].branch_sp;
         base_curfr = MR_nondet_branch_infos[branch].branch_curfr;
         label_layout = MR_nondet_branch_infos[branch].branch_layout;
         topfr = MR_nondet_branch_infos[branch].branch_topfr;
-        if (dump_fp) {
-            if (base_sp == NULL) {
-                fprintf(dump_fp, " internal frame on nondet side branch ");
-                MR_printnondstackptr(topfr);
-                fprintf(dump_fp, "\n");
-            } else {
-                fprintf(dump_fp, " on main nondet branch ");
-                MR_printnondstackptr(topfr);
-                fprintf(dump_fp, "\n");
-            }
+        if (base_sp == NULL) {
+            category = MR_INTERNAL_FRAME_ON_SIDE_BRANCH;
+        } else {
+            category = MR_FRAME_ON_MAIN_BRANCH;
         }
-        (*func)(func_data, label_layout, base_sp, base_curfr,
+        (*func)(func_data, category, topfr, label_layout, base_sp, base_curfr,
                 level_number);
         MR_erase_temp_redoip(fr);
         proc_layout = label_layout->MR_sll_entry;
@@ -621,12 +679,8 @@
         }
 
         if (redoip == NULL) {
-            if (dump_fp) {
-                fprintf(dump_fp,
-                        " terminal top frame of a nondet side branch ");
-                MR_printnondstackptr(fr);
-                fprintf(dump_fp, "\n");
-            }
+            (*func)(func_data, MR_TERMINAL_TOP_FRAME_ON_SIDE_BRANCH,
+                    NULL, NULL, NULL, fr, level_number);
             MR_erase_temp_redoip(fr);
 
             success = MR_succip_slot(fr);
@@ -642,12 +696,8 @@
             }
 
             label_layout = internal->i_layout;
-            if (dump_fp) {
-                fprintf(dump_fp, " top frame of a nondet side branch ");
-                MR_printnondstackptr(fr);
-                fprintf(dump_fp, "\n");
-            }
-            (*func)(func_data, label_layout, NULL, fr, level_number);
+            (*func)(func_data, MR_TOP_FRAME_ON_SIDE_BRANCH, NULL, label_layout,
+                    NULL, fr, level_number);
             MR_erase_temp_redoip(fr);
 
             /*
-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list