[m-rev.] For review: Annotate construction unifications with regions

Quan Phan Quan.Phan at cs.kuleuven.be
Tue Jun 12 15:20:58 AEST 2007


Hi,

Estimated hours taken: 6.
Branch: main.

Annotate the construction unifications with regions so that terms can be
constructed in regions.

compiler/hlds_goal.m:
        Add construct_in_region constructor to type how_to_construct.

compiler/goal_util.m:
        Change to work with the above addition.

compiler/hlds_out.m:
        Change to work with the above addition.

compiler/ml_unify_gen.m:
        Change so that the compiler is compileable after the addition.

compiler/constructions_in_region.m:
        New file.
        Provide the functionality to annotate construction unifications
        with region.

compiler/rbmm.m:
        Include the above new module.

Regards,
Quan

Index: goal_util.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/goal_util.m,v
retrieving revision 1.147
diff -u -u -r1.147 goal_util.m
--- goal_util.m	13 Apr 2007 04:56:38 -0000	1.147
+++ goal_util.m	12 Jun 2007 00:53:08 -0000
@@ -709,6 +709,10 @@
     ;
         How0 = construct_statically(_),
         How = How0
+    ;
+        How0 = construct_in_region(RegVar0),
+        rename_var(Must, Subn, RegVar0, RegVar),
+        How = construct_in_region(RegVar)
     ),
     (
         SubInfo0 = construct_sub_info(MTA, MaybeSize0),
Index: hlds_goal.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_goal.m,v
retrieving revision 1.177
diff -u -u -r1.177 hlds_goal.m
--- hlds_goal.m	13 Apr 2007 04:56:39 -0000	1.177
+++ hlds_goal.m	8 Jun 2007 06:51:38 -0000
@@ -764,12 +764,15 @@
 :- type how_to_construct
     --->    construct_statically(
                 % Use a statically initialized constant.
-
                 args :: list(static_cons)
             )
+
     ;       construct_dynamically
             % Allocate a new term on the heap
 
+    ;
+            construct_in_region(prog_var)
+
     ;       reuse_cell(cell_to_reuse).
             % Reuse an existing heap cell.
 
Index: hlds_out.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/hlds_out.m,v
retrieving revision 1.426
diff -u -u -r1.426 hlds_out.m
--- hlds_out.m	17 May 2007 03:52:43 -0000	1.426
+++ hlds_out.m	12 Jun 2007 01:02:13 -0000
@@ -2367,6 +2367,12 @@
         io.write_string("% reuse cell: ", !IO),
         mercury_output_var(ProgVarSet, AppendVarNums, ReuseVar, !IO),
         io.write_string("\n", !IO)
+    ;
+        ConstructHow = construct_in_region(RegVar),
+        write_indent(Indent, !IO),
+        io.write_string(" construct in region: ", !IO),
+        mercury_output_var(ProgVarSet, AppendVarNums, RegVar, !IO),
+        io.write_string("\n", !IO)
     ).
 
 write_unification(deconstruct(Var, ConsId, ArgVars, ArgModes, CanFail, CanCGC),
Index: ml_unify_gen.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/ml_unify_gen.m,v
retrieving revision 1.112
diff -u -u -r1.112 ml_unify_gen.m
--- ml_unify_gen.m	17 May 2007 01:09:58 -0000	1.112
+++ ml_unify_gen.m	12 Jun 2007 01:43:18 -0000
@@ -787,6 +787,10 @@
 
         Decls = [],
         Statements = [Statement | Statements0]
+    ;
+        HowToConstruct = construct_in_region(_RegVar),
+        sorry(this_file, "ml_gen_new_object: " ++
+            "implementation for construct_in_region is not available")
     ).
 
 :- pred ml_gen_field_take_address_assigns(list(take_addr_info)::in,
Index: rbmm.constructions_in_region.m
===================================================================
RCS file: rbmm.constructions_in_region.m
diff -N rbmm.constructions_in_region.m
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ rbmm.constructions_in_region.m	12 Jun 2007 04:16:49 -0000
@@ -0,0 +1,300 @@
+%-----------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4
+%-----------------------------------------------------------------------------%
+% Copyright (C) 2007 The University of Melbourne.
+% This file may only be copied under the terms of the GNU General
+% Public License - see the file COPYING in the Mercury distribution.
+%-----------------------------------------------------------------------------%
+% 
+% File: rbmm.constructions_in_region.m
+% Main author: quan.
+%
+% This module annotates construction unifications with regions.
+% 
+%-----------------------------------------------------------------------------%
+
+:- module transform_hlds.rbmm.constructions_in_region.
+:- interface.
+
+:- import_module hlds.
+:- import_module hlds.hlds_module.
+:- import_module hlds.hlds_pred.
+:- import_module parse_tree.
+:- import_module parse_tree.prog_data.
+:- import_module transform_hlds.rbmm.points_to_info.
+:- import_module transform_hlds.rbmm.region_resurrection_renaming.
+
+:- import_module map.
+:- import_module string.
+%-----------------------------------------------------------------------------%
+
+:- type name_to_prog_var_table == map(pred_proc_id, name_to_prog_var).
+:- type name_to_prog_var == map(string, prog_var).
+
+	% Note: We will apply renaming for resurrection before applying
+	% renaming for if-then-else. This means that the renaming for
+	% if-then-else will only be applicable to the reverse renaming
+	% annotations introduced by the former renaming. That is the
+	% reason why when annotating construction unifications we only
+	% need to apply the renaming for resurrection.
+	%
+	% Besides changing the HLDS, this predicate also returns a mapping
+	% from a region name to a program variable which represents the
+	% region. We will only create a new program variable for a region
+	% name which is not yet in the map. This map is supposed to be used
+	% in other annotation processes such as adding region instructions,
+	% extending the argument lists of procedures and calls.
+	%
+:- pred annotate_constructions(rpta_info_table::in, renaming_table::in,
+	name_to_prog_var_table::in, name_to_prog_var_table::out,
+	module_info::in, module_info::out) is det.
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module check_hlds.
+:- import_module check_hlds.goal_path.
+:- import_module hlds.hlds_goal.
+:- import_module libs.
+:- import_module libs.compiler_util.
+:- import_module mdbcomp.prim_data.
+:- import_module transform_hlds.rbmm.points_to_graph.
+:- import_module transform_hlds.smm_common.
+
+:- import_module list.
+:- import_module svmap.
+:- import_module varset.
+%-----------------------------------------------------------------------------%
+
+annotate_constructions(RptaInfoTable, ResurRenamingTable, !NameToVarTable,
+		!ModuleInfo) :-
+	module_info_predids(PredIds, !ModuleInfo),
+	list.foldl2(annotate_constructions_pred(RptaInfoTable,
+		ResurRenamingTable), PredIds, !NameToVarTable, !ModuleInfo).
+
+:- pred annotate_constructions_pred(rpta_info_table::in, renaming_table::in,
+	pred_id::in, name_to_prog_var_table::in, name_to_prog_var_table::out,
+	module_info::in, module_info::out) is det.
+
+annotate_constructions_pred(RptaInfoTable, ResurRenamingTable, PredId,
+		!NameToVarTable, !ModuleInfo) :-
+    module_info_pred_info(!.ModuleInfo, PredId, PredInfo),
+    ProcIds = pred_info_non_imported_procids(PredInfo),
+    list.foldl2(annotate_constructions_proc(RptaInfoTable, ResurRenamingTable,
+		PredId), ProcIds, !NameToVarTable, !ModuleInfo).
+
+:- pred annotate_constructions_proc(rpta_info_table::in, renaming_table::in,
+	pred_id::in, proc_id::in, name_to_prog_var_table::in,
+	name_to_prog_var_table::out, module_info::in, module_info::out) is det.
+
+annotate_constructions_proc(RptaInfoTable, ResurRenamingTable, PredId,
+		ProcId, !NameToVarTable, !ModuleInfo) :-
+    PPId = proc(PredId, ProcId),
+    ( if    some_are_special_preds([PPId], !.ModuleInfo)
+      then  true
+      else
+            module_info_pred_proc_info(!.ModuleInfo, PPId,
+				PredInfo, ProcInfo0),
+			fill_goal_path_slots(!.ModuleInfo, ProcInfo0,
+				ProcInfo1),
+            proc_info_get_goal(ProcInfo1, Goal0),
+			proc_info_get_varset(ProcInfo1, VarSet0),
+			proc_info_get_vartypes(ProcInfo1, VarTypes0),
+			map.lookup(RptaInfoTable, PPId, rpta_info(Graph, _)),
+			( if	map.search(ResurRenamingTable, PPId,
+						ResurRenamingProc0)
+			  then
+			  		ResurRenamingProc = ResurRenamingProc0
+			  else
+			  		ResurRenamingProc = map.init
+			),
+			( if	map.search(!.NameToVarTable, PPId, NameToVar0)
+			  then
+			  		NameToVar1 = NameToVar0
+			  else
+			  		NameToVar1 = map.init
+			),
+            annotate_constructions_goal(Graph, ResurRenamingProc,
+				Goal0, Goal, NameToVar1, NameToVar,
+				VarSet0, VarSet, VarTypes0, VarTypes),
+			proc_info_set_varset(VarSet, ProcInfo0, ProcInfo2),
+			proc_info_set_goal(Goal, ProcInfo2, ProcInfo3),
+			proc_info_set_vartypes(VarTypes, ProcInfo3, ProcInfo),
+            module_info_set_pred_proc_info(PPId, PredInfo, ProcInfo,
+				!ModuleInfo),
+			svmap.det_insert(PPId, NameToVar, !NameToVarTable)
+    ).
+
+:- pred annotate_constructions_goal(rpt_graph::in, renaming_proc::in,
+	hlds_goal::in, hlds_goal::out,
+	name_to_prog_var::in, name_to_prog_var::out,
+	prog_varset::in, prog_varset::out, vartypes::in, vartypes::out) is det.
+
+annotate_constructions_goal(Graph, ResurRenamingProc, !Goal, !NameToVar,
+		!VarSet, !VarTypes) :-
+    !.Goal = hlds_goal(GoalExpr0, Info), 
+    annotate_constructions_goal_expr(Graph, ResurRenamingProc, Info,
+		!NameToVar, !VarSet, !VarTypes, GoalExpr0, GoalExpr),
+	!:Goal = hlds_goal(GoalExpr, Info).	
+	
+:- pred annotate_constructions_goal_expr(rpt_graph::in, renaming_proc::in,
+	hlds_goal_info::in, name_to_prog_var::in, name_to_prog_var::out,
+	prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
+	hlds_goal_expr::in, hlds_goal_expr::out) is det.
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, _,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :- 
+	!.GoalExpr = conj(ConjType, Conjs0),
+    list.map_foldl3(annotate_constructions_goal(Graph, ResurRenamingProc),
+		Conjs0, Conjs, !NameToVar, !VarSet, !VarTypes),
+	!:GoalExpr = conj(ConjType, Conjs). 
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, _,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :-
+	!.GoalExpr = disj(Disjs0),
+    list.map_foldl3(annotate_constructions_goal(Graph, ResurRenamingProc),
+		Disjs0, Disjs, !NameToVar, !VarSet, !VarTypes),
+	!:GoalExpr = disj(Disjs).
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, _,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :-
+	!.GoalExpr = switch(A, B, Cases0), 
+    list.map_foldl3(annotate_constructions_case(Graph, ResurRenamingProc),
+		Cases0, Cases, !NameToVar, !VarSet, !VarTypes),
+	!:GoalExpr = switch(A, B, Cases).
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, _,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :-
+	!.GoalExpr = negation(Goal0),
+    annotate_constructions_goal(Graph, ResurRenamingProc, Goal0, Goal,
+		!NameToVar, !VarSet, !VarTypes),
+	!:GoalExpr = negation(Goal). 
+
+annotate_constructions_goal_expr(_, _, _, !NameToVar, !VarSet, !VarTypes,
+		!GoalExpr) :-
+	!.GoalExpr = plain_call(_, _, _, _, _, _).
+
+annotate_constructions_goal_expr(_, _, _, !NameToVar, !VarSet, !VarTypes,
+		!GoalExpr) :-
+	!.GoalExpr = generic_call(_, _, _, _).
+
+annotate_constructions_goal_expr(_, _, _, !NameToVar, !VarSet, !VarTypes,
+		!GoalExpr) :-
+	!.GoalExpr = call_foreign_proc(_, _, _, _, _, _, _).
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, Info,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :- 
+	!.GoalExpr = unify(LHS, RHS, Mode, Unification0, Context),
+    annotate_constructions_unification(Graph, ResurRenamingProc, Info,
+		!NameToVar, !VarSet, !VarTypes, Unification0, Unification),
+	!:GoalExpr = unify(LHS, RHS, Mode, Unification, Context).
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, _,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :-
+	!.GoalExpr = scope(Reason, Goal0),
+	annotate_constructions_goal(Graph, ResurRenamingProc, Goal0, Goal,
+		!NameToVar, !VarSet, !VarTypes),
+	!:GoalExpr = scope(Reason, Goal).
+
+annotate_constructions_goal_expr(Graph, ResurRenamingProc, _,
+		!NameToVar, !VarSet, !VarTypes, !GoalExpr) :-
+	!.GoalExpr = if_then_else(Vars, Cond0, Then0, Else0),
+    annotate_constructions_goal(Graph, ResurRenamingProc, Cond0, Cond,
+		!NameToVar, !VarSet, !VarTypes),
+    annotate_constructions_goal(Graph, ResurRenamingProc, Then0, Then,
+		!NameToVar, !VarSet, !VarTypes),
+    annotate_constructions_goal(Graph, ResurRenamingProc, Else0, Else,
+		!NameToVar, !VarSet, !VarTypes),
+	!:GoalExpr = if_then_else(Vars, Cond, Then, Else).
+
+annotate_constructions_goal_expr(_, _, _, !NameToVar, !VarSet, !VarTypes,
+		!GoalExpr) :-
+	!.GoalExpr = shorthand(_),
+    unexpected(this_file,
+		"annotate_constructions_goal_expr: shorthand not handled").
+
+:- pred annotate_constructions_case(rpt_graph::in, renaming_proc::in,
+	case::in, case::out, name_to_prog_var::in, name_to_prog_var::out,
+	prog_varset::in, prog_varset::out, vartypes::in, vartypes::out) is det.
+
+annotate_constructions_case(Graph, ResurRenamingProc, !Case,
+		!NameToVar, !VarSet, !VarTypes) :-
+    !.Case = case(Functor, Goal0),
+    annotate_constructions_goal(Graph, ResurRenamingProc, Goal0, Goal,
+		!NameToVar, !VarSet, !VarTypes),
+	!:Case = case(Functor, Goal).
+ 
+:- pred annotate_constructions_unification(rpt_graph::in, renaming_proc::in,
+	hlds_goal_info::in, name_to_prog_var::in, name_to_prog_var::out,
+	prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
+	unification::in, unification::out) is det.
+
+annotate_constructions_unification(Graph, ResurRenamingProc, Info,
+		!NameToVar, !VarSet, !VarTypes, !Unification) :-
+	!.Unification = construct(Var, ConsId, Args, ArgModes, _HowToConstruct0,
+		IsUnique, SubInfo),
+	get_node_by_variable(Graph, Var, Node),
+	Name = rptg_lookup_region_name(Graph, Node),
+	ProgPoint = program_point_init(Info),
+	( if	map.search(ResurRenamingProc, ProgPoint, Renaming0)
+	  then
+	  		Renaming = Renaming0
+	  else
+	  		Renaming = map.init
+	),
+	name_to_reg_var(Name, Renaming, RegVar, !VarSet, !VarTypes, !NameToVar),
+	HowToConstruct = construct_in_region(RegVar),
+	!:Unification = construct(Var, ConsId, Args, ArgModes, HowToConstruct,
+		IsUnique, SubInfo).
+
+annotate_constructions_unification(_, _, _, !VarSet, !VarTypes, !NameToVar,
+		!Unification) :-
+	(
+		( !.Unification = deconstruct(_, _, _, _, _, _)
+		; !.Unification = assign(_, _)
+		; !.Unification = simple_test(_, _)
+		),
+		true
+	;
+		!.Unification = complicated_unify(_, _, _),
+		unexpected(this_file, "annotate_construction_unification: "
+			++ "encounter complicated unify")
+	).
+	
+:- pred name_to_reg_var(string::in, renaming::in, prog_var::out,
+	prog_varset::in, prog_varset::out, vartypes::in, vartypes::out,
+	name_to_prog_var::in, name_to_prog_var::out) is det.
+
+name_to_reg_var(Name0, ResurRenaming, RegVar, !VarSet, !VarTypes,
+		!NameToVar) :-
+	( if	map.search(ResurRenaming, Name0, Name1)
+	  then
+	  		Name = Name1
+	  else
+			Name = Name0
+	),
+	( if	map.search(!.NameToVar, Name, RegVar0)
+	  then
+			RegVar = RegVar0
+	  else
+			varset.new_named_var(!.VarSet, Name,
+				RegVar, !:VarSet),
+			svmap.det_insert(RegVar, make_region_type, !VarTypes),
+			svmap.det_insert(Name, RegVar, !NameToVar)
+	).
+
+:- func make_region_type = mer_type.
+
+make_region_type = RegionType :-
+	RegionTypeName = qualified(mercury_region_builtin_module, "region"),
+	RegionType = defined_type(RegionTypeName, [], kind_star).
+
+%-----------------------------------------------------------------------------%
+
+:- func this_file = string.
+
+this_file = "rbmm.constructions_in_region.m".
+
+%-----------------------------------------------------------------------------%
Index: rbmm.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/compiler/rbmm.m,v
retrieving revision 1.3
diff -u -u -r1.3 rbmm.m
--- rbmm.m	8 Jun 2007 06:45:11 -0000	1.3
+++ rbmm.m	12 Jun 2007 05:16:12 -0000
@@ -18,6 +18,7 @@
 
 :- include_module actual_region_arguments.
 :- include_module condition_renaming.
+:- include_module constructions_in_region.
 :- include_module execution_path.
 :- include_module interproc_region_lifetime.
 :- include_module live_region_analysis.
@@ -45,6 +46,7 @@
 
 :- import_module transform_hlds.rbmm.actual_region_arguments.
 :- import_module transform_hlds.rbmm.condition_renaming.
+:- import_module transform_hlds.rbmm.constructions_in_region.
 :- import_module transform_hlds.rbmm.execution_path.
 :- import_module transform_hlds.rbmm.interproc_region_lifetime.
 :- import_module transform_hlds.rbmm.live_region_analysis.
@@ -52,6 +54,7 @@
 :- import_module transform_hlds.rbmm.points_to_analysis.
 :- import_module transform_hlds.rbmm.region_instruction.
 :- import_module transform_hlds.rbmm.region_resurrection_renaming.
+
 %-----------------------------------------------------------------------------%
 
 do_region_analysis(!ModuleInfo, !IO) :-
@@ -95,7 +98,7 @@
 		JoinPointTable),
     collect_renaming_and_annotation(ResurrectionRenameTable, JoinPointTable,
 		LRBeforeTable, BornRTable, RptaInfoTable, ResurrectionPathTable,
-		ExecPathTable, _RenamingAnnotationTable, _RenamingTable),
+		ExecPathTable, _RenamingAnnotationTable, ResurRenamingTable),
 
 	collect_non_local_and_in_cond_regions(!.ModuleInfo, LRBeforeTable,
 		LRAfterTable, LocalRegionsTable, InCondRegionsTable),
@@ -104,9 +107,10 @@
 	collect_ite_renaming(!.ModuleInfo, RptaInfoTable, RenamedRegionsTable,
 		IteRenamingTable),
 	collect_ite_annotation(RenamedRegionsTable, ExecPathTable, 
-		RptaInfoTable, IteRenamingTable, _IteAnnoTable).
-
+		RptaInfoTable, IteRenamingTable, _IteAnnoTable),
 
+	annotate_constructions(RptaInfoTable, ResurRenamingTable, 
+		map.init, _NameToVarTable, !.ModuleInfo, _),
 %-----------------------------------------------------------------------------%
 :- end_module transform_hlds.rbmm.
 
--------------------------------------------------------------------------
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