[m-rev.] For Review: Refactor deep profiling module.

Paul Bone pbone at csse.unimelb.edu.au
Thu Nov 29 21:59:44 AEDT 2007


For review by anyone,

This is currently running through the bootcheck for the deep profiling
grade.  I have tested it by comparing HLDS dumps between this and a
known good compiler.

Estimated hours taken: 3
Branches: main

Refactor the deep profiler module to remove duplicated code.  This
change will make future changes to this module easier, many changes will
only have to be done in one place rather than three.

compiler/deep_profiling.m:
	+ Factored out common code between the transformations for det, semidet
      and nondet procedures.
	+ Replaced cases of 2-4 repeated 'create a variable' conjuncts with a
	  single call to generate_var.


Index: compiler/deep_profiling.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/deep_profiling.m,v
retrieving revision 1.68
diff -u -r1.68 deep_profiling.m
--- compiler/deep_profiling.m	23 Nov 2007 07:34:58 -0000	1.68
+++ compiler/deep_profiling.m	29 Nov 2007 10:49:37 -0000
@@ -486,7 +486,8 @@
         ->
             transform_inner_proc(ModuleInfo, PredProcId, !ProcInfo)
         ;
-            transform_det_proc(ModuleInfo, PredProcId, !ProcInfo)
+            transform_proc(transform_proc_det, ModuleInfo, PredProcId, 
+                !ProcInfo)
         )
     ;
         CodeModel = model_semi,
@@ -498,11 +499,13 @@
         ->
             transform_inner_proc(ModuleInfo, PredProcId, !ProcInfo)
         ;
-            transform_semi_proc(ModuleInfo, PredProcId, !ProcInfo)
+            transform_proc(transform_proc_semi, ModuleInfo, PredProcId, 
+                !ProcInfo)
         )
     ;
         CodeModel = model_non,
-        transform_non_proc(ModuleInfo, PredProcId, !ProcInfo)
+        transform_proc(transform_proc_non, ModuleInfo, PredProcId, 
+            !ProcInfo)
     ).
 
 %-----------------------------------------------------------------------------%
@@ -514,41 +517,39 @@
                 deep_current_csd        :: prog_var,
                 deep_site_num_counter   :: counter,
                 deep_call_sites         :: list(call_site_static_data),
-                deep_varset             :: prog_varset,
-                deep_var_types          :: vartypes,
+                deep_varinfos               :: var_info,
                 deep_proc_filename      :: string,
                 deep_maybe_rec_info     :: maybe(deep_recursion_info)
             ).
 
-:- pred transform_det_proc(module_info::in, pred_proc_id::in,
-    proc_info::in, proc_info::out) is det.
 
-transform_det_proc(ModuleInfo, PredProcId, !ProcInfo) :-
+:- type transform_proc_type
+    --->    transform_proc_det
+    ;       transform_proc_semi
+    ;       transform_proc_non.
+
+
+%
+% Transfrom a procedure.  The first argument is model specific, and is used to
+% wrap the transformed goal in port code.
+%
+:- pred transform_proc(transform_proc_type::in, module_info::in,
+    pred_proc_id::in, proc_info::in, proc_info::out) is det.
+
+transform_proc(TransformType, ModuleInfo, PredProcId, !ProcInfo) :-
     proc_info_get_goal(!.ProcInfo, Goal0),
     Goal0 = hlds_goal(_, GoalInfo0),
-    some [!VarSet, !VarTypes] (
-        proc_info_get_varset(!.ProcInfo, !:VarSet),
-        proc_info_get_vartypes(!.ProcInfo, !:VarTypes),
-        CPointerType = c_pointer_type,
-        svvarset.new_named_var("TopCSD", TopCSD, !VarSet),
-        svvarset.new_named_var("MiddleCSD", MiddleCSD, !VarSet),
-        svvarset.new_named_var("ProcStaticLayout", ProcStaticVar, !VarSet),
-        svmap.set(TopCSD, CPointerType, !VarTypes),
-        svmap.set(MiddleCSD, CPointerType, !VarTypes),
-        svmap.set(ProcStaticVar, CPointerType, !VarTypes),
-        module_info_get_globals(ModuleInfo, Globals),
-        globals.lookup_bool_option(Globals, use_activation_counts,
-            UseActivationCounts),
-        (
-            UseActivationCounts = no,
-            svvarset.new_named_var("ActivationPtr", ActivationPtr0, !VarSet),
-            svmap.set(ActivationPtr0, CPointerType, !VarTypes),
-            MaybeActivationPtr = yes(ActivationPtr0)
-        ;
-            UseActivationCounts = yes,
-            MaybeActivationPtr = no
-        ),
-        ExcpVars = hlds_deep_excp_vars(TopCSD, MiddleCSD, MaybeActivationPtr),
+    proc_info_get_varset(!.ProcInfo, VarSet0),
+    proc_info_get_vartypes(!.ProcInfo, VarTypes0),
+    some [!VarInfo] (
+        !:VarInfo = var_info(VarSet0, VarTypes0),
+        generate_var("TopCSD", c_pointer_type, TopCSD, !VarInfo),
+        generate_var("MiddleCSD", c_pointer_type, MiddleCSD, !VarInfo),
+        generate_var("ProcStaticLayout", c_pointer_type, ProcStaticVar, 
+            !VarInfo),
+        
         proc_info_get_context(!.ProcInfo, Context),
         FileName = term.context_file(Context),
         LineNumber = term.context_line(Context),
@@ -556,32 +557,76 @@
         proc_info_get_maybe_deep_profile_info(!.ProcInfo, MaybeDeepProfInfo),
         extract_deep_rec_info(MaybeDeepProfInfo, MaybeRecInfo),
         DeepInfo0 = deep_info(ModuleInfo, PredProcId, MiddleCSD,
-            counter.init(0), [], !.VarSet, !.VarTypes, FileName, MaybeRecInfo)
-    ),
+            counter.init(0), [], !.VarInfo, FileName, MaybeRecInfo),
 
-    deep_prof_transform_goal(empty, Goal0, TransformedGoal, _,
-        DeepInfo0, DeepInfo),
+        % This call transforms the goals of the procedure.
+        deep_prof_transform_goal(empty, Goal0, TransformedGoal, _,
+            DeepInfo0, DeepInfo),
 
-    Vars = DeepInfo ^ deep_varset,
-    VarTypes = DeepInfo ^ deep_var_types,
-    CallSites = DeepInfo ^ deep_call_sites,
+        !:VarInfo = DeepInfo ^ deep_varinfos,
+        CallSites = DeepInfo ^ deep_call_sites,
 
-    (
-        MaybeRecInfo = yes(RecInfo),
-        RecInfo ^ role = inner_proc(OuterPredProcId)
-    ->
-        OuterPredProcId = proc(PredId, ProcId)
-    ;
-        PredProcId = proc(PredId, ProcId)
+        (
+            MaybeRecInfo = yes(RecInfo),
+            RecInfo ^ role = inner_proc(OuterPredProcId)
+        ->
+            OuterPredProcId = proc(PredId, ProcId)
+        ;
+            PredProcId = proc(PredId, ProcId)
+        ),
+
+        module_info_get_globals(ModuleInfo, Globals),
+        globals.lookup_bool_option(Globals, use_activation_counts,
+            UseActivationCounts),
+        
+        IsInInterface = is_proc_in_interface(ModuleInfo, PredId, ProcId),
+        ProcStatic = hlds_proc_static(FileName, LineNumber, IsInInterface,
+            CallSites),
+        ShroudedPredProcId = shroud_pred_proc_id(proc(PredId, ProcId)),
+        ProcStaticConsId = deep_profiling_proc_layout(ShroudedPredProcId),
+        generate_unify(ProcStaticConsId, ProcStaticVar, BindProcStaticVarGoal),
+
+        % Wrap goal body inside more goals that run the port code as nessacary.
+        % This depends on the type of procedure being considered.
+        (
+            (
+                TransformType = transform_proc_det,
+                BuildProcBody = build_det_proc_body
+            ;
+                TransformType = transform_proc_semi,
+                BuildProcBody = build_semi_proc_body
+            ),
+            maybe_generate_activation_ptr(UseActivationCounts, TopCSD,
+                MiddleCSD, MaybeActivationPtr, ExcpVars, !VarInfo),
+            BuildProcBody(ModuleInfo, TopCSD, MiddleCSD, ProcStaticVar,
+                MaybeActivationPtr, GoalInfo0, BindProcStaticVarGoal,
+                TransformedGoal, Goal)
+        ;
+            TransformType = transform_proc_non,
+            generate_outermost_proc_dyns(UseActivationCounts,
+                TopCSD, MiddleCSD, MaybeOldActivationPtr, NewOutermostProcDyn, 
+                ExcpVars, !VarInfo),
+            build_non_proc_body(ModuleInfo, TopCSD, MiddleCSD, ProcStaticVar,
+                MaybeOldActivationPtr, NewOutermostProcDyn, GoalInfo0,
+                BindProcStaticVarGoal, TransformedGoal, Goal)
+        ),
+        
+        !.VarInfo = var_info(Vars, VarTypes)
     ),
+    proc_info_set_varset(Vars, !ProcInfo),
+    proc_info_set_vartypes(VarTypes, !ProcInfo),
+    proc_info_set_goal(Goal, !ProcInfo),
+    record_hlds_proc_static(ProcStatic, ExcpVars, !ProcInfo).
 
-    IsInInterface = is_proc_in_interface(ModuleInfo, PredId, ProcId),
-    ProcStatic = hlds_proc_static(FileName, LineNumber, IsInInterface,
-        CallSites),
-    ShroudedPredProcId = shroud_pred_proc_id(proc(PredId, ProcId)),
-    ProcStaticConsId = deep_profiling_proc_layout(ShroudedPredProcId),
-    generate_unify(ProcStaticConsId, ProcStaticVar, BindProcStaticVarGoal),
 
+% Wrap the procedure body in the deep profiling port goals.
+
+:- pred build_det_proc_body(module_info::in, prog_var::in, prog_var::in, 
+    prog_var::in, maybe(prog_var)::in, hlds_goal_info::in, hlds_goal::in,
+    hlds_goal::in, hlds_goal::out) is det.
+
+build_det_proc_body(ModuleInfo, TopCSD, MiddleCSD, ProcStaticVar,
+        MaybeActivationPtr, GoalInfo0, BindProcStaticVarGoal, Goal0, Goal) :-
     (
         MaybeActivationPtr = yes(ActivationPtr1),
         generate_deep_det_call(ModuleInfo, "det_call_port_code_sr", 4,
@@ -606,78 +651,18 @@
     GoalExpr = conj(plain_conj, [
         BindProcStaticVarGoal,
         CallPortCode,
-        TransformedGoal,
+        Goal0,
         ExitPortCode
     ]),
-    Goal = hlds_goal(GoalExpr, GoalInfo),
-    proc_info_set_varset(Vars, !ProcInfo),
-    proc_info_set_vartypes(VarTypes, !ProcInfo),
-    proc_info_set_goal(Goal, !ProcInfo),
-    record_hlds_proc_static(ProcStatic, ExcpVars, !ProcInfo).
-
-:- pred transform_semi_proc(module_info::in, pred_proc_id::in,
-    proc_info::in, proc_info::out) is det.
-
-transform_semi_proc(ModuleInfo, PredProcId, !ProcInfo) :-
-    proc_info_get_goal(!.ProcInfo, Goal0),
-    Goal0 = hlds_goal(_, GoalInfo0),
-    some [!VarSet, !VarTypes] (
-        proc_info_get_varset(!.ProcInfo, !:VarSet),
-        proc_info_get_vartypes(!.ProcInfo, !:VarTypes),
-        CPointerType = c_pointer_type,
-        svvarset.new_named_var("TopCSD", TopCSD, !VarSet),
-        svvarset.new_named_var("MiddleCSD", MiddleCSD, !VarSet),
-        svvarset.new_named_var("ProcStaticLayout", ProcStaticVar, !VarSet),
-        svmap.set(TopCSD, CPointerType, !VarTypes),
-        svmap.set(MiddleCSD, CPointerType, !VarTypes),
-        svmap.set(ProcStaticVar, CPointerType, !VarTypes),
-        module_info_get_globals(ModuleInfo, Globals),
-        globals.lookup_bool_option(Globals, use_activation_counts,
-            UseActivationCounts),
-        (
-            UseActivationCounts = no,
-            svvarset.new_named_var("ActivationPtr", ActivationPtr0, !VarSet),
-            svmap.set(ActivationPtr0, CPointerType, !VarTypes),
-            MaybeActivationPtr = yes(ActivationPtr0)
-        ;
-            UseActivationCounts = yes,
-            MaybeActivationPtr = no
-        ),
-        ExcpVars = hlds_deep_excp_vars(TopCSD, MiddleCSD, MaybeActivationPtr),
-        proc_info_get_context(!.ProcInfo, Context),
-        FileName = term.context_file(Context),
-        LineNumber = term.context_line(Context),
-
-        proc_info_get_maybe_deep_profile_info(!.ProcInfo, MaybeDeepProfInfo),
-        extract_deep_rec_info(MaybeDeepProfInfo, MaybeRecInfo),
-        DeepInfo0 = deep_info(ModuleInfo, PredProcId, MiddleCSD,
-            counter.init(0), [], !.VarSet, !.VarTypes,
-            FileName, MaybeRecInfo)
-    ),
-
-    deep_prof_transform_goal(empty, Goal0, TransformedGoal, _,
-        DeepInfo0, DeepInfo),
+    Goal = hlds_goal(GoalExpr, GoalInfo).
 
-    Vars = DeepInfo ^ deep_varset,
-    VarTypes = DeepInfo ^ deep_var_types,
-    CallSites = DeepInfo ^ deep_call_sites,
 
-    (
-        MaybeRecInfo = yes(RecInfo),
-        RecInfo ^ role = inner_proc(OuterPredProcId)
-    ->
-        OuterPredProcId = proc(PredId, ProcId)
-    ;
-        PredProcId = proc(PredId, ProcId)
-    ),
-
-    IsInInterface = is_proc_in_interface(ModuleInfo, PredId, ProcId),
-    ProcStatic = hlds_proc_static(FileName, LineNumber, IsInInterface,
-        CallSites),
-    ShroudedPredProcId = shroud_pred_proc_id(proc(PredId, ProcId)),
-    ProcStaticConsId = deep_profiling_proc_layout(ShroudedPredProcId),
-    generate_unify(ProcStaticConsId, ProcStaticVar, BindProcStaticVarGoal),
+:- pred build_semi_proc_body(module_info::in, prog_var::in, prog_var::in, 
+    prog_var::in, maybe(prog_var)::in, hlds_goal_info::in, hlds_goal::in,
+    hlds_goal::in, hlds_goal::out) is det.
 
+build_semi_proc_body(ModuleInfo, TopCSD, MiddleCSD, ProcStaticVar,
+        MaybeActivationPtr, GoalInfo0, BindProcStaticVarGoal, Goal0, Goal) :-
     (
         MaybeActivationPtr = yes(ActivationPtr1),
         generate_deep_det_call(ModuleInfo, "semi_call_port_code_sr", 4,
@@ -714,76 +699,22 @@
         CallPortCode,
         hlds_goal(
             disj([
-                hlds_goal(conj(plain_conj, [TransformedGoal, ExitPortCode]),
+                hlds_goal(conj(plain_conj, [Goal0, ExitPortCode]),
                     ExitConjGoalInfo),
                 FailPortCode
             ]),
             ExitConjGoalInfo)
     ]),
-    Goal = hlds_goal(GoalExpr, GoalInfo),
-    proc_info_set_varset(Vars, !ProcInfo),
-    proc_info_set_vartypes(VarTypes, !ProcInfo),
-    proc_info_set_goal(Goal, !ProcInfo),
-    record_hlds_proc_static(ProcStatic, ExcpVars, !ProcInfo).
-
-:- pred transform_non_proc(module_info::in, pred_proc_id::in,
-    proc_info::in, proc_info::out) is det.
-
-transform_non_proc(ModuleInfo, PredProcId, !ProcInfo) :-
-    proc_info_get_goal(!.ProcInfo, Goal0),
-    Goal0 = hlds_goal(_, GoalInfo0),
-    some [!VarSet, !VarTypes] (
-        proc_info_get_varset(!.ProcInfo, !:VarSet),
-        proc_info_get_vartypes(!.ProcInfo, !:VarTypes),
-        CPointerType = c_pointer_type,
-        svvarset.new_named_var("TopCSD", TopCSD, !VarSet),
-        svvarset.new_named_var("MiddleCSD", MiddleCSD, !VarSet),
-        svvarset.new_named_var("ProcStaticLayout", ProcStaticVar, !VarSet),
-        svmap.set(TopCSD, CPointerType, !VarTypes),
-        svmap.set(MiddleCSD, CPointerType, !VarTypes),
-        svmap.set(ProcStaticVar, CPointerType, !VarTypes),
-        module_info_get_globals(ModuleInfo, Globals),
-        globals.lookup_bool_option(Globals, use_activation_counts,
-            UseActivationCounts),
-        (
-            UseActivationCounts = no,
-            svvarset.new_named_var("OldOutermost", OldOutermostProcDyn0,
-                !VarSet),
-            svmap.set(OldOutermostProcDyn0, CPointerType, !VarTypes),
-            MaybeOldActivationPtr = yes(OldOutermostProcDyn0)
-        ;
-            UseActivationCounts = yes,
-            MaybeOldActivationPtr = no
-        ),
-        ExcpVars = hlds_deep_excp_vars(TopCSD, MiddleCSD,
-            MaybeOldActivationPtr),
-        svvarset.new_named_var("NewOutermost", NewOutermostProcDyn, !VarSet),
-        svmap.set(NewOutermostProcDyn, CPointerType, !VarTypes),
-        proc_info_get_context(!.ProcInfo, Context),
-        FileName = term.context_file(Context),
-        LineNumber = term.context_line(Context),
-
-        proc_info_get_maybe_deep_profile_info(!.ProcInfo, MaybeDeepProfInfo),
-        extract_deep_rec_info(MaybeDeepProfInfo, MaybeRecInfo),
-        DeepInfo0 = deep_info(ModuleInfo, PredProcId, MiddleCSD,
-            counter.init(0), [], !.VarSet, !.VarTypes, FileName, MaybeRecInfo)
-    ),
-
-    deep_prof_transform_goal(empty, Goal0, TransformedGoal, _,
-        DeepInfo0, DeepInfo),
+    Goal = hlds_goal(GoalExpr, GoalInfo).
 
-    Vars = DeepInfo ^ deep_varset,
-    VarTypes = DeepInfo ^ deep_var_types,
-    CallSites = DeepInfo ^ deep_call_sites,
-
-    PredProcId = proc(PredId, ProcId),
-    IsInInterface = is_proc_in_interface(ModuleInfo, PredId, ProcId),
-    ProcStatic = hlds_proc_static(FileName, LineNumber, IsInInterface,
-        CallSites),
-    ShroudedPredProcId = shroud_pred_proc_id(proc(PredId, ProcId)),
-    ProcStaticConsId = deep_profiling_proc_layout(ShroudedPredProcId),
-    generate_unify(ProcStaticConsId, ProcStaticVar, BindProcStaticVarGoal),
 
+:- pred build_non_proc_body(module_info::in, prog_var::in, prog_var::in, 
+    prog_var::in, maybe(prog_var)::in, prog_var::in, hlds_goal_info::in,
+    hlds_goal::in, hlds_goal::in, hlds_goal::out) is det.
+
+build_non_proc_body(ModuleInfo, TopCSD, MiddleCSD, ProcStaticVar,
+        MaybeOldActivationPtr, NewOutermostProcDyn, GoalInfo0,
+        BindProcStaticVarGoal, Goal0, Goal) :-
     (
         MaybeOldActivationPtr = yes(OldOutermostProcDyn2),
         generate_deep_det_call(ModuleInfo, "non_call_port_code_sr", 5,
@@ -853,7 +784,7 @@
             disj([
                 hlds_goal(
                     conj(plain_conj, [
-                        TransformedGoal,
+                        Goal0,
                         hlds_goal(
                             disj([
                                 ExitPortCode,
@@ -866,11 +797,8 @@
             ]),
             CallExitRedoGoalInfo)
     ]),
-    Goal = hlds_goal(GoalExpr, GoalInfo),
-    proc_info_set_varset(Vars, !ProcInfo),
-    proc_info_set_vartypes(VarTypes, !ProcInfo),
-    proc_info_set_goal(Goal, !ProcInfo),
-    record_hlds_proc_static(ProcStatic, ExcpVars, !ProcInfo).
+    Goal = hlds_goal(GoalExpr, GoalInfo).
+
 
 :- pred transform_inner_proc(module_info::in, pred_proc_id::in,
     proc_info::in, proc_info::out) is det.
@@ -880,10 +808,8 @@
     Goal0 = hlds_goal(_, GoalInfo0),
     proc_info_get_varset(!.ProcInfo, VarSet0),
     proc_info_get_vartypes(!.ProcInfo, VarTypes0),
-    CPointerType = c_pointer_type,
-    % MiddleCSD should be unused
-    varset.new_named_var(VarSet0, "MiddleCSD", MiddleCSD, VarSet1),
-    map.set(VarTypes0, MiddleCSD, CPointerType, VarTypes1),
+    VarInfo0 = var_info(VarSet0, VarTypes0),
+    generate_var("MiddleCSD", c_pointer_type, MiddleCSD, VarInfo0, VarInfo1),
 
     Context = goal_info_get_context(GoalInfo0),
     FileName = term.context_file(Context),
@@ -891,14 +817,14 @@
     proc_info_get_maybe_deep_profile_info(!.ProcInfo, MaybeDeepProfInfo),
     extract_deep_rec_info(MaybeDeepProfInfo, MaybeRecInfo),
     DeepInfo0 = deep_info(ModuleInfo, PredProcId, MiddleCSD,
-        counter.init(0), [], VarSet1, VarTypes1,
+        counter.init(0), [], VarInfo1,
         FileName, MaybeRecInfo),
 
     deep_prof_transform_goal(empty, Goal0, TransformedGoal, _,
         DeepInfo0, DeepInfo),
 
-    VarSet = DeepInfo ^ deep_varset,
-    VarTypes = DeepInfo ^ deep_var_types,
+    VarInfo = DeepInfo ^ deep_varinfos,
+    VarInfo = var_info(VarSet, VarTypes),
 
     proc_info_set_varset(VarSet, !ProcInfo),
     proc_info_set_vartypes(VarTypes, !ProcInfo),
@@ -985,7 +911,7 @@
     ;
         GoalExpr0 = switch(Var, CF, Cases0),
         ModuleInfo = !.DeepInfo ^ deep_module_info,
-        VarTypes = !.DeepInfo ^ deep_var_types,
+        VarTypes = !.DeepInfo ^ deep_varinfos ^ varinfo_vartypes,
         map.lookup(VarTypes, Var, Type),
         ( switch_type_num_functors(ModuleInfo, Type, NumFunctors) ->
             MaybeNumFunctors = yes(NumFunctors)
@@ -1133,13 +1059,10 @@
 
     SiteNumCounter0 = !.DeepInfo ^ deep_site_num_counter,
     counter.allocate(SiteNum, SiteNumCounter0, SiteNumCounter),
-    varset.new_named_var(!.DeepInfo ^ deep_varset, "SiteNum", SiteNumVar,
-        VarSet1),
-    IntType = int_type,
-    map.set(!.DeepInfo ^ deep_var_types, SiteNumVar, IntType, VarTypes1),
+    VarInfo0 = !.DeepInfo ^ deep_varinfos,
+    generate_var("SiteNum", int_type, SiteNumVar, VarInfo0, VarInfo1),
     generate_unify(int_const(SiteNum), SiteNumVar, SiteNumVarGoal),
-    !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet1,
-    !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes1,
+    !:DeepInfo = !.DeepInfo ^ deep_varinfos := VarInfo1,
     !:DeepInfo = !.DeepInfo ^ deep_site_num_counter := SiteNumCounter,
 
     Context = goal_info_get_context(GoalInfo0),
@@ -1197,14 +1120,12 @@
             CallSite = higher_order_call(FileName, LineNumber, GoalPath)
         ;
             Generic = class_method(TypeClassInfoVar, MethodNum, _, _),
-            varset.new_named_var(!.DeepInfo ^ deep_varset, "MethodNum",
-                MethodNumVar, VarSet2),
-            map.set(!.DeepInfo ^ deep_var_types, MethodNumVar, IntType,
-                VarTypes2),
+            VarInfo2 = !.DeepInfo ^ deep_varinfos,
+            generate_var("MethodNum", int_type, MethodNumVar, VarInfo2, 
+                VarInfo3),
+            !:DeepInfo = !.DeepInfo ^ deep_varinfos := VarInfo3,
             generate_unify(int_const(MethodNum), MethodNumVar,
                 MethodNumVarGoal),
-            !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet2,
-            !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes2,
             generate_deep_det_call(ModuleInfo, "prepare_for_method_call", 3,
                 [SiteNumVar, TypeClassInfoVar, MethodNumVar],
                 [], PrepareCallGoal),
@@ -1311,54 +1232,48 @@
 
 deep_prof_transform_higher_order_call(Globals, CodeModel, Goal0, Goal,
         !DeepInfo) :-
-    VarSet0 = !.DeepInfo ^ deep_varset,
-    VarTypes0 = !.DeepInfo ^ deep_var_types,
+    some [!VarInfo] (
+        !:VarInfo = !.DeepInfo ^ deep_varinfos,
 
-    CPointerType = c_pointer_type,
-    varset.new_named_var(VarSet0, "SavedPtr", SavedPtrVar, VarSet1),
-    map.set(VarTypes0, SavedPtrVar, CPointerType, VarTypes1),
+        generate_var("SavedPtr", c_pointer_type, SavedPtrVar, !VarInfo),
 
-    globals.lookup_bool_option(Globals, use_activation_counts,
-        UseActivationCounts),
-    (
-        UseActivationCounts = yes,
+        globals.lookup_bool_option(Globals, use_activation_counts,
+            UseActivationCounts),
+        (
+            UseActivationCounts = yes,
 
-        IntType = int_type,
-        varset.new_named_var(VarSet1, "SavedCounter", SavedCountVar, VarSet),
-        map.set(VarTypes1, SavedCountVar, IntType, VarTypes),
-
-        !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet,
-        !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes,
-        ExtraNonLocals = set.list_to_set([SavedCountVar, SavedPtrVar]),
-
-        generate_deep_det_call(!.DeepInfo ^ deep_module_info,
-            "save_and_zero_activation_info_ac", 2,
-            [SavedCountVar, SavedPtrVar],
-            [SavedCountVar, SavedPtrVar], SaveStuff),
-        generate_deep_det_call(!.DeepInfo ^ deep_module_info,
-            "reset_activation_info_ac", 2,
-            [SavedCountVar, SavedPtrVar], [], RestoreStuff),
-        generate_deep_det_call(!.DeepInfo ^ deep_module_info,
-            "rezero_activation_info_ac", 0,
-            [], [], ReZeroStuff)
-    ;
-        UseActivationCounts = no,
+            generate_var("SavedCounter", int_type, SavedCountVar, !VarInfo),
+            ExtraNonLocals = set.list_to_set([SavedCountVar, SavedPtrVar]),
 
-        !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet1,
-        !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes1,
-        ExtraNonLocals = set.list_to_set([SavedPtrVar]),
-
-        generate_deep_det_call(!.DeepInfo ^ deep_module_info,
-            "save_and_zero_activation_info_sr", 1,
-            [SavedPtrVar], [SavedPtrVar], SaveStuff),
-        generate_deep_det_call(!.DeepInfo ^ deep_module_info,
-            "reset_activation_info_sr", 1,
-            [SavedPtrVar], [], RestoreStuff),
-        generate_deep_det_call(!.DeepInfo ^ deep_module_info,
-            "rezero_activation_info_sr", 0,
-            [], [], ReZeroStuff)
-    ),
+            generate_deep_det_call(!.DeepInfo ^ deep_module_info,
+                "save_and_zero_activation_info_ac", 2,
+                [SavedCountVar, SavedPtrVar],
+                [SavedCountVar, SavedPtrVar], SaveStuff),
+            generate_deep_det_call(!.DeepInfo ^ deep_module_info,
+                "reset_activation_info_ac", 2,
+                [SavedCountVar, SavedPtrVar], [], RestoreStuff),
+            generate_deep_det_call(!.DeepInfo ^ deep_module_info,
+                "rezero_activation_info_ac", 0,
+                [], [], ReZeroStuff)
+        ;
+            UseActivationCounts = no,
+
+            ExtraNonLocals = set.list_to_set([SavedPtrVar]),
+
+            generate_deep_det_call(!.DeepInfo ^ deep_module_info,
+                "save_and_zero_activation_info_sr", 1,
+                [SavedPtrVar], [SavedPtrVar], SaveStuff),
+            generate_deep_det_call(!.DeepInfo ^ deep_module_info,
+                "reset_activation_info_sr", 1,
+                [SavedPtrVar], [], RestoreStuff),
+            generate_deep_det_call(!.DeepInfo ^ deep_module_info,
+                "rezero_activation_info_sr", 0,
+                [], [], ReZeroStuff)
+        ),
 
+        !:DeepInfo = !.DeepInfo ^ deep_varinfos := !.VarInfo
+    ),
+    
     Goal0 = hlds_goal(_, GoalInfo0),
     ExtGoalInfo = goal_info_add_nonlocals_make_impure(GoalInfo0,
         ExtraNonLocals),
@@ -1435,9 +1350,8 @@
 
     SiteNumCounter0 = !.DeepInfo ^ deep_site_num_counter,
     counter.allocate(SiteNum, SiteNumCounter0, SiteNumCounter),
-    varset.new_named_var(!.DeepInfo ^ deep_varset, "SiteNum", SiteNumVar,
-        VarSet),
-    map.set(!.DeepInfo ^ deep_var_types, SiteNumVar, int_type, VarTypes),
+    generate_var("SiteNum", int_type, SiteNumVar, !.DeepInfo ^ deep_varinfos, 
+        VarInfo),
     generate_unify(int_const(SiteNum), SiteNumVar, SiteNumVarGoal),
 
     generate_deep_det_call(ModuleInfo, "prepare_for_callback", 1,
@@ -1453,8 +1367,7 @@
     Goal = hlds_goal(conj(plain_conj, [SiteNumVarGoal, PrepareGoal, Goal0]),
         GoalInfo),
     !:DeepInfo = !.DeepInfo ^ deep_site_num_counter := SiteNumCounter,
-    !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet,
-    !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes,
+    !:DeepInfo = !.DeepInfo ^ deep_varinfos := VarInfo,
     !:DeepInfo = !.DeepInfo ^ deep_call_sites :=
         !.DeepInfo ^ deep_call_sites ++ [CallSite].
 
@@ -1598,14 +1511,11 @@
     deep_info::in, deep_info::out) is det.
 
 generate_depth_var(CSN, DepthVar, !DeepInfo) :-
-    VarSet0 = !.DeepInfo ^ deep_varset,
-    VarTypes0 = !.DeepInfo ^ deep_var_types,
-    IntType = int_type,
+    VarInfo0 = !.DeepInfo ^ deep_varinfos,
     VarName = string.format("Depth%d", [i(CSN)]),
-    varset.new_named_var(VarSet0, VarName, DepthVar, VarSet),
-    map.set(VarTypes0, DepthVar, IntType, VarTypes),
-    !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet,
-    !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes.
+    generate_var(VarName, int_type, DepthVar, VarInfo0, VarInfo),
+    !:DeepInfo = !.DeepInfo ^ deep_varinfos := VarInfo.
+
 
 :- pred generate_csn_vector(int::in, list(int)::in, list(prog_var)::out,
     list(hlds_goal)::out, prog_var::out,
@@ -1634,16 +1544,13 @@
     prog_var::out, hlds_goal::out, deep_info::in, deep_info::out) is det.
 
 generate_csn_vector_cell(Length, CSNVars, CellVar, CellGoal, !DeepInfo) :-
-    VarSet0 = !.DeepInfo ^ deep_varset,
-    VarTypes0 = !.DeepInfo ^ deep_var_types,
-    varset.new_named_var(VarSet0, "CSNCell", CellVar, VarSet),
+    VarInfo0 = !.DeepInfo ^ deep_varinfos,
     ProfilingBuiltin = mercury_profiling_builtin_module,
     CellTypeName = string.format("call_site_nums_%d", [i(Length)]),
     CellTypeId = type_ctor(qualified(ProfilingBuiltin, CellTypeName), Length),
     construct_type(CellTypeId, [], CellType),
-    map.set(VarTypes0, CellVar, CellType, VarTypes),
-    !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet,
-    !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes,
+    generate_var("CSNCell", CellType, CellVar, VarInfo0, VarInfo),
+    !:DeepInfo = !.DeepInfo ^ deep_varinfos := VarInfo,
     ConsId = cons(qualified(ProfilingBuiltin, CellTypeName), Length),
     generate_cell_unify(Length, ConsId, CSNVars, CellVar, CellGoal).
 
@@ -1651,13 +1558,10 @@
     pair(prog_var, hlds_goal)::out, deep_info::in, deep_info::out) is det.
 
 generate_single_csn_unify(CSN, CSNVar - UnifyGoal, !DeepInfo) :-
-    VarSet0 = !.DeepInfo ^ deep_varset,
-    VarTypes0 = !.DeepInfo ^ deep_var_types,
+    VarInfo0 = !.DeepInfo ^ deep_varinfos,
     VarName = string.format("CSN%d", [i(CSN)]),
-    varset.new_named_var(VarSet0, VarName, CSNVar, VarSet),
-    map.set(VarTypes0, CSNVar, int_type, VarTypes),
-    !:DeepInfo = !.DeepInfo ^ deep_varset := VarSet,
-    !:DeepInfo = !.DeepInfo ^ deep_var_types := VarTypes,
+    generate_var(VarName, int_type, CSNVar, VarInfo0, VarInfo),
+    !:DeepInfo = !.DeepInfo ^ deep_varinfos := VarInfo,
     generate_unify(int_const(CSN), CSNVar, UnifyGoal).
 
 :- pred generate_deep_det_call(module_info::in, string::in, int::in,
@@ -1726,6 +1630,72 @@
         unify_context(umc_explicit, [])),
     Goal = hlds_goal(GoalExpr, GoalInfo).
 
+
+:- type var_info
+    --->    var_info(
+                varinfo_varset      :: prog_varset,
+                varinfo_vartypes    :: vartypes). 
+
+
+:- pred generate_var(string::in, mer_type::in, prog_var::out, var_info::in,
+    var_info::out) is det.
+
+generate_var(Name, Type, Var, !VarInfo) :-
+    some [!VarSet, !VarTypes]
+    (
+        !.VarInfo = var_info(!:VarSet, !:VarTypes),
+        generate_var(Name, Type, Var, !VarSet, !VarTypes),
+        !:VarInfo = var_info(!.VarSet, !.VarTypes)
+    ).
+
+
+:- pred generate_var(string::in, mer_type::in, prog_var::out, prog_varset::in,
+    prog_varset::out, vartypes::in, vartypes::out) is det. 
+
+generate_var(Name, Type, Var, !VarSet, !VarTypes) :-
+    svvarset.new_named_var(Name, Var, !VarSet),
+    svmap.set(Var, Type, !VarTypes).
+
+
+:- pred maybe_generate_activation_ptr(bool::in, prog_var::in, prog_var::in,
+        maybe(prog_var)::out, hlds_deep_excp_vars::out, var_info::in, 
+        var_info::out) is det.
+
+maybe_generate_activation_ptr(UseActivationCounts, TopCSD, MiddleCSD,
+    MaybeActivationPtr, ExcpVars, !VarInfo) :-
+    (
+        UseActivationCounts = no,
+        generate_var("ActivationPtr", c_pointer_type, ActivationPtr0, 
+            !VarInfo),
+        MaybeActivationPtr = yes(ActivationPtr0)
+    ;
+        UseActivationCounts = yes,
+        MaybeActivationPtr = no
+    ),
+    ExcpVars = hlds_deep_excp_vars(TopCSD, MiddleCSD, MaybeActivationPtr).
+
+
+:- pred generate_outermost_proc_dyns(bool::in, prog_var::in, prog_var::in, 
+        maybe(prog_var)::out, prog_var::out, hlds_deep_excp_vars::out, 
+        var_info::in, var_info::out) is det.
+
+generate_outermost_proc_dyns(UseActivationCounts, TopCSD, MiddleCSD,
+        MaybeOldActivationPtr, NewOutermostProcDyn, ExcpVars, !VarInfo) :-
+    (   
+        UseActivationCounts = no,
+        generate_var("OldOutermost", c_pointer_type, OldOutermostProcDyn0,
+            !VarInfo),
+        MaybeOldActivationPtr = yes(OldOutermostProcDyn0)
+    ;   
+        UseActivationCounts = yes,
+        MaybeOldActivationPtr = no
+    ),
+    ExcpVars = hlds_deep_excp_vars(TopCSD, MiddleCSD,
+        MaybeOldActivationPtr),
+    generate_var("NewOutermost", c_pointer_type, NewOutermostProcDyn,
+        !VarInfo).
+
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list