[m-rev.] for prelim review: remove hand-coded RTTI

Peter Ross pro at missioncriticalit.com
Tue Nov 18 02:22:47 AEDT 2003


Hi,

For fjh and zs to have a look at.

With the change below I can now build the standard library in the il
grade and no longer require hand-coded RTTI for int.

So far all I have done is satisfy myself that it works for int, however
I would like some confirmation that there is nothing fundamentally wrong
with this approach, plus any suggestions on how the approach could be
improved.

Here are the modifications that I made to the standard library.

Index: library/builtin.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/builtin.m,v
retrieving revision 1.95
diff -u -r1.95 builtin.m
--- library/builtin.m	14 Nov 2003 16:02:21 -0000	1.95
+++ library/builtin.m	17 Nov 2003 15:11:30 -0000
@@ -550,9 +550,12 @@
 
 ").
 
+% This needs to be defined so that the type_ctor is genrated for it.
+:- type int.
+
 :- pragma foreign_code("MC++", "
 
-MR_DEFINE_BUILTIN_TYPE_CTOR_INFO(builtin, int, 0, MR_TYPECTOR_REP_INT) 
+//MR_DEFINE_BUILTIN_TYPE_CTOR_INFO(builtin, int, 0, MR_TYPECTOR_REP_INT) 
 MR_DEFINE_BUILTIN_TYPE_CTOR_INFO(builtin, character, 0, MR_TYPECTOR_REP_CHAR) 
 MR_DEFINE_BUILTIN_TYPE_CTOR_INFO(builtin, string, 0, MR_TYPECTOR_REP_STRING) 
 MR_DEFINE_BUILTIN_TYPE_CTOR_INFO(builtin, c_pointer, 0,

===================================================================


Estimated hours taken: 10

compiler/make_hlds.m:
	Rename builtin.int to int when building the special preds.

compiler/mlds_to_il.m:
	No longer generate references to the int RTTI in the MC++.

compiler/special_pred.m:
	Allow the abstract type builtin.int to have special clauses.
	
compiler/type_ctor_info.m:
	When processing builtin also build a type_ctor_info for
	builtin.int.
	builtin.int is an "abstract" type, so change the handling of
	abstract types.

compiler/unify_proc.m:
	Rename builtin.int as int when generating the special
	predicates.
    Add code which builds calls to the builtin_compare_int and
    builtin_unify_int when constructing the special preds for int.
	

Index: compiler/make_hlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make_hlds.m,v
retrieving revision 1.451
diff -u -r1.451 make_hlds.m
--- compiler/make_hlds.m	6 Nov 2003 11:11:43 -0000	1.451
+++ compiler/make_hlds.m	17 Nov 2003 15:11:13 -0000
@@ -3743,8 +3743,16 @@
 		)
 	).
 
-add_special_pred_for_real(SpecialPredId, TVarSet, Type, TypeCtor,
+add_special_pred_for_real(SpecialPredId, TVarSet, Type0, TypeCtor,
 		TypeBody, Context, Status0, !Module) :-
+	(
+		type_to_ctor_and_args(Type0, TypeCtor, []),
+		TypeCtor = qualified(unqualified("builtin"), "int") - 0
+	->
+		Type = int_type
+	;
+		Type = Type0
+	),
 	adjust_special_pred_status(Status0, SpecialPredId, Status),
 	module_info_get_special_pred_map(!.Module, SpecialPredMap0),
 	( map__contains(SpecialPredMap0, SpecialPredId - TypeCtor) ->
Index: compiler/mlds_to_il.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.135
diff -u -r1.135 mlds_to_il.m
--- compiler/mlds_to_il.m	15 Nov 2003 12:57:32 -0000	1.135
+++ compiler/mlds_to_il.m	17 Nov 2003 15:11:14 -0000
@@ -3373,8 +3373,7 @@
 			),
 			( LibModuleName0 = "builtin",
 				(
-				  Name = "int", Arity = 0
-				; Name = "string", Arity = 0
+				  Name = "string", Arity = 0
 				; Name = "float", Arity = 0
 				; Name = "character", Arity = 0
 				; Name = "void", Arity = 0
Index: compiler/special_pred.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/special_pred.m,v
retrieving revision 1.42
diff -u -r1.42 special_pred.m
--- compiler/special_pred.m	31 Oct 2003 14:27:23 -0000	1.42
+++ compiler/special_pred.m	17 Nov 2003 15:11:14 -0000
@@ -227,7 +227,9 @@
 	).
 
 can_generate_special_pred_clauses_for_type(ModuleInfo, TypeCtor, Body) :-
-	Body \= abstract_type(_),
+	( Body \= abstract_type(_)
+	; TypeCtor = qualified(unqualified("builtin"), "int") - 0
+	),
 	\+ type_ctor_has_hand_defined_rtti(TypeCtor, Body),
 	\+ type_body_has_user_defined_equality_pred(ModuleInfo, Body,
 		abstract_noncanonical_type).
Index: compiler/type_ctor_info.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/type_ctor_info.m,v
retrieving revision 1.46
diff -u -r1.46 type_ctor_info.m
--- compiler/type_ctor_info.m	31 Oct 2003 03:27:30 -0000	1.46
+++ compiler/type_ctor_info.m	17 Nov 2003 15:11:14 -0000
@@ -86,7 +86,13 @@
 type_ctor_info__generate_hlds(!ModuleInfo) :-
 	module_info_name(!.ModuleInfo, ModuleName),
 	module_info_types(!.ModuleInfo, TypeTable),
-	map__keys(TypeTable, TypeCtors),
+	map__keys(TypeTable, TypeCtors0),
+	( ModuleName = unqualified("builtin") ->
+		TypeCtors = [qualified(unqualified("builtin"), "int") - 0
+				| TypeCtors0]
+	;
+		TypeCtors = TypeCtors0
+	),
 	type_ctor_info__gen_type_ctor_gen_infos(TypeCtors, TypeTable,
 		ModuleName, !.ModuleInfo, TypeCtorGenInfos),
 	module_info_set_type_ctor_gen_infos(TypeCtorGenInfos, !ModuleInfo).
@@ -111,10 +117,14 @@
 			TypeModuleName = ModuleName,
 			map__lookup(TypeTable, TypeCtor, TypeDefn),
 			hlds_data__get_type_defn_body(TypeDefn, TypeBody),
-			TypeBody \= abstract_type(_),
-			\+ type_ctor_has_hand_defined_rtti(TypeCtor, TypeBody),
-			( are_equivalence_types_expanded(ModuleInfo)
-				=> TypeBody \= eqv_type(_) )
+			(
+				TypeBody \= abstract_type(_),
+				\+ type_ctor_has_hand_defined_rtti(TypeCtor, TypeBody),
+				( are_equivalence_types_expanded(ModuleInfo)
+					=> TypeBody \= eqv_type(_) )
+			;
+				TypeName = "int"
+			)
 		->
 			type_ctor_info__gen_type_ctor_gen_info(TypeCtor,
 				TypeName, TypeArity, TypeDefn,
@@ -218,7 +228,21 @@
 	Version = type_ctor_info_rtti_version,
 	(
 		TypeBody = abstract_type(_),
-		error("type_ctor_info__gen_type_ctor_data: abstract_type")
+		(
+			ModuleName = unqualified(ModuleStr1),
+			builtin_type_ctor(ModuleStr1, TypeName, TypeArity,
+				BuiltinCtor)
+		->
+			Details = builtin(BuiltinCtor)
+		;
+			ModuleName = unqualified(ModuleStr),
+			impl_type_ctor(ModuleStr, TypeName, TypeArity,
+				ImplCtor)
+		->
+			Details = impl_artifact(ImplCtor)
+		;
+			error("type_ctor_info__gen_type_ctor_data: abstract_type")
+		)
 	;
 		TypeBody = foreign_type(_, _),
 		(
Index: compiler/unify_proc.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/unify_proc.m,v
retrieving revision 1.127
diff -u -r1.127 unify_proc.m
--- compiler/unify_proc.m	31 Oct 2003 03:27:30 -0000	1.127
+++ compiler/unify_proc.m	17 Nov 2003 15:11:15 -0000
@@ -684,8 +684,17 @@
 
 %-----------------------------------------------------------------------------%
 
-unify_proc__generate_clause_info(SpecialPredId, Type, TypeBody, Context,
+unify_proc__generate_clause_info(SpecialPredId, Type0, TypeBody, Context,
 		ModuleInfo, ClauseInfo) :-
+	% XXX petdr handle the special types.
+	(
+		type_to_ctor_and_args(Type0, TypeCtor, []),
+		TypeCtor = qualified(unqualified("builtin"), "int") - 0
+	->
+		Type = int_type
+	;
+		Type = Type0
+	),
 	special_pred_interface(SpecialPredId, Type, ArgTypes, _Modes, _Det),
 	unify_proc__info_init(ModuleInfo, VarTypeInfo0),
 	unify_proc__make_fresh_named_vars_from_types(ArgTypes, "HeadVar__", 1,
@@ -756,11 +765,35 @@
 			generate_unify_clauses_eqv_type(c_pointer_type,
 				H1, H2, Context, Clauses, !Info)
 		;
+			% XXX this can only be a builtin type
 			TypeBody = abstract_type(_),
-			error("trying to create unify proc for abstract type")
+			generate_builtin_unify(H1, H2, Context, Clauses, !Info)
+			% error("trying to create unify proc for abstract type")
 		)
 	).
 
+:- pred generate_builtin_unify(
+	prog_var::in, prog_var::in, prog_context::in, list(clause)::out,
+	unify_proc_info::in, unify_proc_info::out) is det.
+
+generate_builtin_unify(H1, H2, Context, Clauses, !Info) :-
+	%
+	% Just generate a call to the specified predicate,
+	% which is the user-defined equality pred for this
+	% type.
+	% (The pred_id and proc_id will be figured
+	% out by type checking and mode analysis.)
+	%
+	PredId = invalid_pred_id,
+	ModeId = invalid_proc_id,
+	mercury_private_builtin_module(PrivateBuiltin),
+	PredName = qualified(PrivateBuiltin, "builtin_unify_int"),
+	Call = call(PredId, ModeId, [H1, H2], not_builtin, no, PredName),
+	goal_info_init(Context, GoalInfo),
+	Goal = Call - GoalInfo,
+	quantify_clauses_body([H1, H2], Goal, Context, Clauses, !Info).
+	
+
 :- pred unify_proc__generate_user_defined_unify_clauses(unify_compare::in,
 	prog_var::in, prog_var::in, prog_context::in, list(clause)::out,
 	unify_proc_info::in, unify_proc_info::out) is det.
@@ -958,10 +991,31 @@
 			generate_compare_clauses_eqv_type(c_pointer_type,
 				Res, H1, H2, Context, Clauses, !Info)
 		;
+			% XXX only can be a builtin type
 			TypeBody = abstract_type(_),
-			error("trying to create compare proc for abstract type")
+			generate_builtin_compare_clauses(
+				Res, H1, H2, Context, Clauses, !Info)
+			%error("trying to create compare proc for abstract type")
 		)
 	).
+
+:- pred generate_builtin_compare_clauses(
+	prog_var::in, prog_var::in, prog_var::in,
+	prog_context::in, list(clause)::out,
+	unify_proc_info::in, unify_proc_info::out) is det.
+
+generate_builtin_compare_clauses(Res, H1, H2, Context, Clauses, !Info) :-
+	%
+	% Just generate a call to the specified predicate,
+	% which is the user-defined comparison pred for this
+	% type.
+	% (The pred_id and proc_id will be figured
+	% out by type checking and mode analysis.)
+	%
+	ArgVars = [Res, H1, H2],
+	unify_proc__build_call("builtin_compare_int",
+			ArgVars, Context, CompareGoal, !Info),
+	quantify_clauses_body(ArgVars, CompareGoal, Context, Clauses, !Info).
 
 :- pred generate_user_defined_compare_clauses(unify_compare::in,
 	prog_var::in, prog_var::in, prog_var::in,


-- 
Peter Ross		
Software Engineer                                (Work)   +32 2 757 10 15
Mission Critical                                 (Mobile) +32 485 482 559
--------------------------------------------------------------------------
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