[m-rev.] for review: make foreign_proc impure by default

Tyson Dowd trd at cs.mu.OZ.AU
Mon Jan 21 08:47:21 AEDT 2002


Hi,

For review by Simon or Pete.

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


Estimated hours taken: 12
Branches: main

Make foreign_proc impure by default.

The majority of this change is to correctly handle older pragma c_code
and import declarations, which are assumed to have the the purity of
their declaration. 

The declared purity is not known at the time we parse the pragma c_code,
so we need to handle these cases at purity checking time by setting the
purity attribute on the procedure to the declared purity.  We can then
continue with normal purity checking.  

compiler/make_hlds.m:
	Make fact table implementations pure.
	(no need to mess around with legacy purity behaviour here as we
	know for fact tables what the purity is declared to be).

compiler/prog_data.m:
	Set the default purity to impure.

	Add a new attribute on foreign_procs, indicating whether the
	foreign_proc should have legacy purity behaviour (because it was
	written using pragma c_code or pragma import) or whether it
	should use the new purity behaviour.

compiler/prog_io_pragma.m:
	Set the legacy_purity_behaviour attribute depending upon the
	c_code and import behaviour.

compiler/purity.m:
	Handle legacy_purity_behaviour attributes.
	Change an XXX about assuming foreign_procs to be pure -- the
	code near there seems to be correct in either case, we still
	don't assume that a foreign_proc is impure simply because it has
	multiple modes.


Index: compiler/make_hlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/make_hlds.m,v
retrieving revision 1.397
diff -u -r1.397 make_hlds.m
--- compiler/make_hlds.m	16 Jan 2002 01:13:24 -0000	1.397
+++ compiler/make_hlds.m	20 Jan 2002 12:12:26 -0000
@@ -8366,7 +8366,9 @@
 	% XXX this should be modified to use nondet pragma c_code.
 	{ default_attributes(c, Attrs0) },
 	{ set_may_call_mercury(Attrs0, will_not_call_mercury, Attrs1) },
-	{ set_thread_safe(Attrs1, thread_safe, Attrs) },
+	{ set_thread_safe(Attrs1, thread_safe, Attrs2) },
+		% fact tables procedures should be considered pure
+	{ set_purity(Attrs2, pure, Attrs) },
 	module_add_pragma_foreign_proc(Attrs, SymName, PredOrFunc, 
 		PragmaVars, VarSet, ordinary(C_ProcCode, no),
 		Status, Context, Module0, Module1, Info0, Info),
Index: compiler/prog_data.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/prog_data.m,v
retrieving revision 1.74
diff -u -r1.74 prog_data.m
--- compiler/prog_data.m	16 Jan 2002 01:13:39 -0000	1.74
+++ compiler/prog_data.m	20 Jan 2002 07:32:43 -0000
@@ -554,6 +554,9 @@
 :- pred purity(pragma_foreign_proc_attributes, purity).
 :- mode purity(in, out) is det.
 
+:- pred legacy_purity_behaviour(pragma_foreign_proc_attributes, bool).
+:- mode legacy_purity_behaviour(in, out) is det.
+
 :- pred set_thread_safe(pragma_foreign_proc_attributes, thread_safe,
 		pragma_foreign_proc_attributes).
 :- mode set_thread_safe(in, in, out) is det.
@@ -576,6 +579,10 @@
 		pragma_foreign_proc_attributes).
 :- mode set_purity(in, in, out) is det.
 
+:- pred set_legacy_purity_behaviour(pragma_foreign_proc_attributes, bool,
+		pragma_foreign_proc_attributes).
+:- mode set_legacy_purity_behaviour(in, in, out) is det.
+
 :- pred add_extra_attribute(pragma_foreign_proc_attributes, 
 		pragma_foreign_proc_extra_attribute,
 		pragma_foreign_proc_attributes).
@@ -1026,19 +1033,18 @@
 			thread_safe		:: thread_safe,
 			tabled_for_io		:: tabled_for_io,
 			purity			:: purity,
+				% there is some special case behaviour for 
+				% pragma c_code and pragma import purity
+				% if legacy_purity_behaviour is `yes'
+			legacy_purity_behaviour	:: bool,
 			extra_attributes	:: 
 				list(pragma_foreign_proc_extra_attribute)
 		).
 
 
-	% XXX we define Purity as being "pure" by default, but we should
-	% change this to "impure" once the promise_pure syntax is available
-	% and all the uses of foreign_proc in the library have the appropriate
-	% promises on them.
 default_attributes(Language, 
 	attributes(Language, may_call_mercury, not_thread_safe, 
-		not_tabled_for_io, pure, [])).		% delete me and 
-%		not_tabled_for_io, impure, [])).	% uncomment me soon!
+		not_tabled_for_io, impure, no, [])).
 
 may_call_mercury(Attrs, Attrs ^ may_call_mercury).
 
@@ -1050,6 +1056,8 @@
 
 purity(Attrs, Attrs ^ purity).
 
+legacy_purity_behaviour(Attrs, Attrs ^ legacy_purity_behaviour).
+
 set_may_call_mercury(Attrs0, MayCallMercury, Attrs) :-
 	Attrs = Attrs0 ^ may_call_mercury := MayCallMercury.
 
@@ -1065,12 +1073,16 @@
 set_purity(Attrs0, Purity, Attrs) :-
 	Attrs = Attrs0 ^ purity := Purity.
 
+set_legacy_purity_behaviour(Attrs0, Legacy, Attrs) :-
+	Attrs = Attrs0 ^ legacy_purity_behaviour := Legacy.
+
+
 attributes_to_strings(Attrs, StringList) :-
 	% We ignore Lang because it isn't an attribute that you can put
 	% in the attribute list -- the foreign language specifier string
 	% is at the start of the pragma.
 	Attrs = attributes(_Lang, MayCallMercury, ThreadSafe, TabledForIO,
-			Purity,	ExtraAttributes),
+			Purity,	_LegacyBehaviour, ExtraAttributes),
 	(
 		MayCallMercury = may_call_mercury,
 		MayCallMercuryStr = "may_call_mercury"
Index: compiler/prog_io_pragma.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/prog_io_pragma.m,v
retrieving revision 1.41
diff -u -r1.41 prog_io_pragma.m
--- compiler/prog_io_pragma.m	16 Jan 2002 01:13:40 -0000	1.41
+++ compiler/prog_io_pragma.m	20 Jan 2002 07:32:43 -0000
@@ -495,7 +495,9 @@
 			Pragma = "c_code"
 		->
 			% may_call_mercury is a conservative default.
-			default_attributes(ForeignLanguage, Attributes),
+			default_attributes(ForeignLanguage, Attributes0),
+			set_legacy_purity_behaviour(Attributes0, yes,
+				Attributes),
 			(
 			    CodeTerm = term__functor(term__string(Code), [],
 				Context)
@@ -581,7 +583,9 @@
 	        )
 	    ;
 		PragmaTerms = [PredAndModesTerm, FunctionTerm],
-		default_attributes(ForeignLanguage, Flags),
+		default_attributes(ForeignLanguage, Flags0),
+			% pragma import uses legacy purity behaviour
+		set_legacy_purity_behaviour(Flags0, yes, Flags),
 		FlagsResult = ok(Flags)
 	    )	
  	-> 
@@ -1069,9 +1073,10 @@
 		MaybeAttributes) :-
 	default_attributes(ForeignLanguage, Attributes0),
 	( ( Pragma = "c_code" ; Pragma = "import" ) ->
-		set_purity(Attributes0, pure, Attributes1)
+		set_legacy_purity_behaviour(Attributes0, yes, Attributes1),
+		set_purity(Attributes1, pure, Attributes2)
 	;
-		Attributes1 = Attributes0
+		Attributes2 = Attributes0
 	),
 	ConflictingAttributes = [
 		may_call_mercury(will_not_call_mercury) - 
@@ -1097,7 +1102,7 @@
 		;
 			list__foldl(
 				process_attribute,
-				AttrList, Attributes1, Attributes),
+				AttrList, Attributes2, Attributes),
 			MaybeAttributes = check_required_attributes(
 				ForeignLanguage, Attributes, Term)
 		)
Index: compiler/purity.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/purity.m,v
retrieving revision 1.40
diff -u -r1.40 purity.m
--- compiler/purity.m	16 Dec 2001 08:11:11 -0000	1.40
+++ compiler/purity.m	20 Jan 2002 12:15:23 -0000
@@ -431,7 +431,7 @@
 puritycheck_pred(PredId, PredInfo0, PredInfo, ModuleInfo, NumErrors) -->
 	{ pred_info_get_purity(PredInfo0, DeclPurity) } ,
 	{ pred_info_get_promised_purity(PredInfo0, PromisedPurity) },
-  
+
 	{ pred_info_clauses_info(PredInfo0, ClausesInfo0) },
 	{ pred_info_procids(PredInfo0, ProcIds) },
 	{ clauses_info_clauses(ClausesInfo0, Clauses0) },
@@ -563,8 +563,8 @@
 	% If this clause doesn't apply to all modes of this procedure,
 	% i.e. the procedure has different clauses for different modes,
 	% then we must treat it as impure.
-	% XXX Currently `:- pragma foreign_proc' procedures are
-	% assumed to be pure. This will change.
+	% foreign_proc procedures are not treated as impure because they
+	% have different clauses for different modes
 	{
 		( applies_to_all_modes(Clause0, ProcIds)
 		; GoalType = pragmas
@@ -774,22 +774,22 @@
 	compute_goal_purity(Goale0, Goale, InClosure, Purity3),
 	{ worst_purity(Purity1, Purity2, Purity12) },
 	{ worst_purity(Purity12, Purity3, Purity) }.
-compute_expr_purity(Ccode, Ccode, _, _, Purity) -->
-	{ Ccode = foreign_proc(Attributes, PredId, _,_,_,_,_) },
-	{ purity(Attributes, AttributesPurity) },
-
-	%
-	% If there were no purity attributes, the purity of the goal
-	% is the purity of the predicate.
-	% XXX Currently the default purity is `pure'. Eventually it
-	% should default to `impure', and require promises for any
-	% other purity levels, i.e. Purity = AttributesPurity.
-	%
+compute_expr_purity(ForeignProc0, ForeignProc, _, _, Purity) -->
+	{ Attributes = ForeignProc0 ^ foreign_attr },
+	{ PredId = ForeignProc0 ^ foreign_pred_id },
 	ModuleInfo =^ module_info,
-	{ AttributesPurity = (pure) ->
+	{ 
+		legacy_purity_behaviour(Attributes, yes)
+	->
+			% get the purity from the declaration, and set it 
+			% here so that it is correct for later use
 		module_info_pred_info(ModuleInfo, PredId, PredInfo),
-		pred_info_get_purity(PredInfo, Purity)
+		pred_info_get_purity(PredInfo, Purity),
+		set_purity(Attributes, Purity, NewAttributes),
+		ForeignProc = ForeignProc0 ^ foreign_attr := NewAttributes
 	;
+		ForeignProc = ForeignProc0,
+		purity(Attributes, AttributesPurity),
 		Purity = AttributesPurity
 	}.
 


-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
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