[m-dev.] diff: MLDS decl flags changes

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Feb 26 17:18:49 AEDT 2001


This is for the main branch only, since it fixes problems that only
occur on the main branch (except for the ml_elim_nested.m one where
we were declaring the environment struct type public rather than private,
but that is pretty harmless).

----------

Estimated hours taken: 4

Fix some problems with the mlds__decl_flags that were breaking the
GCC back-end.

Julien's recent change to add `local' access was incomplete, because
as noted in the review, the changes to mlds_to_gcc.m weren't right.
But in addition, ml_elim_nested.m needed to be changed to convert `local'
access into `public' when hoisting out local variables and nested functions,
and to declare local variables used for the environment itself as
`local' rather than `public'.  And mlds_to_gcc.m and mlds_to_c.m
needed to be changed to declare the fields used for base classes as
`public' rather than `local'.

Also ml_elim_nested.m was declaring the environment struct type as if
it was a local variable (public per_instance), rather than a (private) type.

compiler/mlds_to_gcc.m:
	Handle local variables with access `local'.
	Handle `one_copy' functions.

compiler/mlds_to_c.m:
compiler/mlds_to_gcc.m:
	Change the code for mlds_make_base_class so that it declares
	the field for the base class with access `public' rather than `local'.

compiler/ml_elim_nested.m:
	For locals which get converted into fields of environment
	structures, change their access from `local' to `public'.
	For nested functions which get hoisted out, set their access
	to `private' and their per_instance to `one_copy'.
	Mark the environment struct types as `private' and `one_copy'
	rather than `public' and `per_instance'.
	Use `ml_gen_local_var_decl_flags' rather than `env_decl_flags'
	for the local variables used to hold the environment struct
	and the environment struct pointer.

compiler/mlds.m:
	Add some comments.

Workspace: /mnt/hg/home/hg/fjh/gcc-cvs/gcc/mercury
Index: compiler/ml_elim_nested.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_elim_nested.m,v
retrieving revision 1.22
diff -u -d -r1.22 ml_elim_nested.m
--- compiler/ml_elim_nested.m	2001/02/20 07:52:17	1.22
+++ compiler/ml_elim_nested.m	2001/02/26 05:30:55
@@ -374,9 +374,10 @@
 	EnvTypeName = class_type(qual(ModuleName, EnvClassName), 0,
 		EnvTypeKind),
 	EnvTypeEntityName = type(EnvClassName, 0),
-	EnvTypeFlags = env_decl_flags,
+	EnvTypeFlags = env_type_decl_flags,
+	Fields = list__map(convert_local_to_field, LocalVars),
 	EnvTypeDefnBody = mlds__class(mlds__class_defn(EnvTypeKind, [], 
-		[mlds__generic_env_ptr_type], [], LocalVars)),
+		[mlds__generic_env_ptr_type], [], Fields)),
 	EnvTypeDefn = mlds__defn(EnvTypeEntityName, Context, EnvTypeFlags,
 		EnvTypeDefnBody),
 
@@ -386,7 +387,7 @@
 	%	struct <EnvClassName> env;
 	%
 	EnvVarName = data(var("env")),
-	EnvVarFlags = env_decl_flags,
+	EnvVarFlags = ml_gen_local_var_decl_flags,
 	EnvVarDefnBody = mlds__data(EnvTypeName, no_initializer),
 	EnvVarDecl = mlds__defn(EnvVarName, Context, EnvVarFlags,
 		EnvVarDefnBody),
@@ -421,6 +422,21 @@
 		EnvDecls = [EnvVarDecl, EnvPtrVarDecl]
 	).
 
+	% When converting local variables into fields of the
+	% environment struct, we need to change `local' access
+	% into something else, since `local' is only supposed to be
+	% used for entities that are local to a function or block,
+	% not for fields.  Currently we change it to `public'.
+	% (Perhaps changing it to `default' might be better?)
+	% 
+:- func convert_local_to_field(mlds__defn) = mlds__defn.
+convert_local_to_field(mlds__defn(Name, Context, Flags0, Body)) =
+		mlds__defn(Name, Context, Flags, Body) :-
+	( access(Flags0) = local ->
+		Flags = set_access(Flags0, public)
+	;
+		Flags = Flags0
+	).
 
 	% ml_insert_init_env:
 	%	If the definition is a nested function definition, and it's
@@ -482,7 +498,7 @@
 	%	<EnvTypeName> *env_ptr;
 	%
 	EnvPtrVarName = data(var("env_ptr")),
-	EnvPtrVarFlags = env_decl_flags,
+	EnvPtrVarFlags = ml_gen_local_var_decl_flags,
 	globals__get_target(Globals, Target),
 		% IL uses classes instead of structs, so the type
 		% is a little different.
@@ -519,15 +535,16 @@
 :- mode ml_conv_arg_to_var(in, in, out) is det.
 
 ml_conv_arg_to_var(Context, Name - Type, LocalVar) :-
-	Flags = env_decl_flags,
+	Flags = ml_gen_local_var_decl_flags,
 	DefnBody = mlds__data(Type, no_initializer),
 	LocalVar = mlds__defn(Name, Context, Flags, DefnBody).
 
-	% Return the declaration flags appropriate for a local variable.
-:- func env_decl_flags = mlds__decl_flags.
-env_decl_flags = MLDS_DeclFlags :-
-	Access = public,
-	PerInstance = per_instance,
+	% Return the declaration flags appropriate for an environment struct
+	% type declaration.
+:- func env_type_decl_flags = mlds__decl_flags.
+env_type_decl_flags = MLDS_DeclFlags :-
+	Access = private,
+	PerInstance = one_copy,
 	Virtuality = non_virtual,
 	Finality = overridable,
 	Constness = modifiable,
@@ -759,13 +776,22 @@
 :- mode flatten_nested_defn(in, in, in, out, in, out) is det.
 
 flatten_nested_defn(Defn0, FollowingDefns, FollowingStatements, Defns) -->
-	{ Defn0 = mlds__defn(Name, Context, Flags, DefnBody0) },
+	{ Defn0 = mlds__defn(Name, Context, Flags0, DefnBody0) },
 	(
 		{ DefnBody0 = mlds__function(PredProcId, Params, FuncBody0) },
 		%
 		% recursively flatten the nested function
 		%
 		flatten_maybe_statement(FuncBody0, FuncBody),
+
+		%
+		% mark the function as private / one_copy,
+		% rather than as local / per_instance,
+		% since we're about to hoist it out to the top level
+		%
+		{ Flags1 = set_access(Flags0, private) },
+		{ Flags = set_per_instance(Flags1, one_copy) },
+
 		{ DefnBody = mlds__function(PredProcId, Params, FuncBody) },
 		{ Defn = mlds__defn(Name, Context, Flags, DefnBody) },
 
Index: compiler/mlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds.m,v
retrieving revision 1.47
diff -u -d -r1.47 mlds.m
--- compiler/mlds.m	2001/02/20 07:52:15	1.47
+++ compiler/mlds.m	2001/02/26 04:39:04
@@ -387,7 +387,7 @@
 	%	constant or variable
 	%	function
 	%	class, including
-	%		package (class with only static members)
+	%		package (class with only static (one_copy) members)
 	%		interface (abstract class, no data members)
 	%		struct (value class)
 	%		enum
@@ -572,16 +572,23 @@
 :- type mlds__decl_flags.
 
 :- type access
+	%
+	% used for class members (this includes globals,
+	% which are actually members of the top-level package)
+	%
 	--->	public		% accessible to anyone
 	;	protected	% only accessible to the class and to
 				% derived classes
 	;	private		% only accessible to the class
 	;	default		% Java "default" access: accessible to anything
 				% defined in the same package.
-	;	local		% use for local variables:
-				% only accessible within the function
-				% (or block) in which the variable is
-				% defined
+	%
+	% used for entities defined in a block/2 statement,
+	% i.e. local variables and nested functions
+	%
+	;	local		% only accessible within the block
+				% in which the entity (variable or
+				% nested function) is defined
 	.
 
 :- type per_instance
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.81
diff -u -d -r1.81 mlds_to_c.m
--- compiler/mlds_to_c.m	2001/02/20 07:52:18	1.81
+++ compiler/mlds_to_c.m	2001/02/26 05:54:49
@@ -39,8 +39,8 @@
 :- import_module rtti.		% for rtti__addr_to_string.
 :- import_module rtti_to_mlds.	% for mlds_rtti_type_name.
 :- import_module hlds_pred.	% for pred_proc_id.
-:- import_module ml_code_util.	% for ml_gen_mlds_var_decl, which is used by
-				% the code that handles derived classes
+:- import_module ml_code_util.	% for ml_gen_public_field_decl_flags, which is
+				% used by the code that handles derived classes
 :- import_module ml_type_gen.	% for ml_gen_type_name
 :- import_module export.	% for export__type_to_type_string
 :- import_module globals, options, passes_aux.
@@ -1030,7 +1030,8 @@
 mlds_make_base_class(Context, ClassId, MLDS_Defn, BaseNum0, BaseNum) :-
 	BaseName = string__format("base_%d", [i(BaseNum0)]),
 	Type = ClassId,
-	MLDS_Defn = ml_gen_mlds_var_decl(var(BaseName), Type, Context),
+	MLDS_Defn = mlds__defn(data(var(BaseName)), Context,
+		ml_gen_public_field_decl_flags, data(Type, no_initializer)),
 	BaseNum = BaseNum0 + 1.
 
 	% Output the definitions of the enumeration constants
@@ -1770,11 +1771,11 @@
 :- pred mlds_output_access_comment_2(access, io__state, io__state).
 :- mode mlds_output_access_comment_2(in, di, uo) is det.
 
-mlds_output_access_comment_2(public)    --> [].
+mlds_output_access_comment_2(public)    --> io__write_string("/* public: */ ").
 mlds_output_access_comment_2(private)   --> io__write_string("/* private: */ ").
 mlds_output_access_comment_2(protected) --> io__write_string("/* protected: */ ").
 mlds_output_access_comment_2(default)   --> io__write_string("/* default access */ ").
-mlds_output_access_comment_2(local)     --> [].
+mlds_output_access_comment_2(local)     --> io__write_string("/* local: */ ").
 
 :- pred mlds_output_per_instance_comment(per_instance, io__state, io__state).
 :- mode mlds_output_per_instance_comment(in, di, uo) is det.
Index: compiler/mlds_to_gcc.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_gcc.m,v
retrieving revision 1.33
diff -u -d -r1.33 mlds_to_gcc.m
--- compiler/mlds_to_gcc.m	2001/02/26 01:33:52	1.33
+++ compiler/mlds_to_gcc.m	2001/02/26 06:02:13
@@ -128,8 +128,8 @@
 				% llds_out__sym_name_mangle,
 				% llds_out__make_base_typeclass_info_name,
 :- import_module rtti.		% for rtti__addr_to_string.
-:- import_module ml_code_util.	% for ml_gen_mlds_var_decl, which is used by
-				% the code that handles derived classes
+:- import_module ml_code_util.	% for ml_gen_public_field_decl_flags, which is
+				% used by the code that handles derived classes
 :- import_module hlds_pred.	% for proc_id_to_int and invalid_pred_id
 :- import_module globals, options, passes_aux.
 :- import_module builtin_ops, modules.
@@ -1047,14 +1047,17 @@
 add_var_access_flag(public, GCC_Defn) -->
 	gcc__set_var_decl_public(GCC_Defn).
 add_var_access_flag(private, _GCC_Defn) -->
-	% this is the default
+	% this should only be used for global variables,
+	% where it is the default
 	[].
 add_var_access_flag(protected, _GCC_Defn) -->
 	{ sorry(this_file, "`protected' access") }.
 add_var_access_flag(default, _GCC_Defn) -->
 	{ sorry(this_file, "`default' access") }.
 add_var_access_flag(local, _GCC_Defn) -->
-	{ sorry(this_file, "`local' access") }.
+	% this should only be used for local variables,
+	% where it is the default
+	[].
 
 :- pred add_var_virtuality_flag(mlds__virtuality, gcc__var_decl,
 	io__state, io__state).
@@ -1126,11 +1129,11 @@
 add_field_access_flag(private, _GCC_Defn) -->
 	{ sorry(this_file, "`private' field") }.
 add_field_access_flag(protected, _GCC_Defn) -->
-	{ sorry(this_file, "`protected' access") }.
+	{ sorry(this_file, "`protected' field") }.
 add_field_access_flag(default, _GCC_Defn) -->
-	{ sorry(this_file, "`default' access") }.
+	{ sorry(this_file, "`default' field") }.
 add_field_access_flag(local, _GCC_Defn) -->
-	{ sorry(this_file, "`local' access") }.
+	{ sorry(this_file, "`local' field") }.
 
 :- pred add_field_per_instance_flag(mlds__per_instance, gcc__field_decl,
 	io__state, io__state).
@@ -1213,17 +1216,19 @@
 add_func_access_flag(default, _GCC_Defn) -->
 	{ sorry(this_file, "`default' access") }.
 add_func_access_flag(local, _GCC_Defn) -->
+	% nested functions are not supported
 	{ sorry(this_file, "`local' access") }.
 
 :- pred add_func_per_instance_flag(mlds__per_instance, gcc__func_decl,
 	io__state, io__state).
 :- mode add_func_per_instance_flag(in, in, di, uo) is det.
 
-add_func_per_instance_flag(per_instance, _GCC_Defn) -->
-	% this is the default
-	[].
-add_func_per_instance_flag(one_copy, _GCC_Defn) -->
-	{ sorry(this_file, "`one_copy' function") }.
+	% For functions, we ignore the `per_instance' flag here.
+	% For global functions, this flag is meaningless;
+	% and currently we don't support nested functions
+	% or class member functions.
+add_func_per_instance_flag(per_instance, _GCC_Defn) --> [].
+add_func_per_instance_flag(one_copy, _GCC_Defn) --> [].
 
 :- pred add_func_virtuality_flag(mlds__virtuality, gcc__func_decl,
 	io__state, io__state).
@@ -1515,7 +1520,8 @@
 mlds_make_base_class(Context, ClassId, MLDS_Defn, BaseNum0, BaseNum) :-
 	BaseName = string__format("base_%d", [i(BaseNum0)]),
 	Type = ClassId,
-	MLDS_Defn = ml_gen_mlds_var_decl(var(BaseName), Type, Context),
+	MLDS_Defn = mlds__defn(data(var(BaseName)), Context,
+		ml_gen_public_field_decl_flags, data(Type, no_initializer)),
 	BaseNum = BaseNum0 + 1.
 
 /***********

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list