[m-rev.] diff: avoid more unnecessary casts

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Jun 27 23:18:49 AEST 2001


Estimated hours taken: 3
Branches: main

runtime/mercury_tags.h:
	Add `_hl' variants for MR_field() (and MR_mask_field(), etc.)
	which are the same except that the return type is changed
	from MR_Word to MR_Box.

compiler/mlds_to_c.m:
	Use the `_hl' variants, so that we can avoid generating some casts.
	Also don't generate casts for field rvals where the type
	being cast from is the same as the type being cast to.

compiler/ml_elim_nested.m:
	Fix a bug where it was using the wrong type for the pointer type
	in the field rvals that it generates -- it was using a struct
	type rather than a pointer to struct type.
	Also reorganize the code a bit to improve modularity and to
	eliminate some unnecessary casts in the generated code.

Workspace: /home/mars/fjh/ws1/mercury
Index: compiler/ml_elim_nested.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_elim_nested.m,v
retrieving revision 1.28
diff -u -d -r1.28 ml_elim_nested.m
--- compiler/ml_elim_nested.m	2001/06/22 09:14:32	1.28
+++ compiler/ml_elim_nested.m	2001/06/27 12:41:33
@@ -168,12 +168,7 @@
 		ml_create_env(EnvName, [], Context, ModuleName, Globals,
 			_EnvTypeDefn, EnvTypeName, _EnvDecls, _InitEnv),
 		
-		globals__get_target(Globals, Target),
-		( Target = il ->
-			EnvPtrTypeName = EnvTypeName
-		;
-			EnvPtrTypeName = mlds__ptr_type(EnvTypeName)
-		),
+		EnvPtrTypeName = ml_make_env_ptr_type(Globals, EnvTypeName),
 
 		%
 		% traverse the function body, finding (and removing)
@@ -472,8 +467,10 @@
 		EnvPtrVal = lval(var(qual(ModuleName,
 				mlds__var_name("env_ptr_arg", no)),
 				mlds__generic_env_ptr_type)),
-		ml_init_env(TypeName, EnvPtrVal, Context, ModuleName, Globals,
-			EnvPtrDecl, InitEnvPtr),
+		EnvPtrVarType = ml_make_env_ptr_type(Globals, TypeName),
+		CastEnvPtrVal = unop(cast(EnvPtrVarType), EnvPtrVal),
+		ml_init_env(TypeName, CastEnvPtrVal, Context, ModuleName,
+			Globals, EnvPtrDecl, InitEnvPtr),
 		FuncBody = mlds__statement(block([EnvPtrDecl],
 				[InitEnvPtr, FuncBody0]), Context),
 		DefnBody = mlds__function(PredProcId, Params, yes(FuncBody)),
@@ -484,6 +481,19 @@
 		Init = Init0
 	).
 
+:- func ml_make_env_ptr_type(globals, mlds__type) = mlds__type.
+ml_make_env_ptr_type(Globals, EnvType)  = EnvPtrType :-
+		% IL uses classes instead of structs, so the type
+		% is a little different.
+		% XXX Perhaps if we used value classes this could go
+		% away.
+	globals__get_target(Globals, Target),
+	( Target = il ->
+		EnvPtrType = EnvType
+	;
+		EnvPtrType = mlds__ptr_type(EnvType)
+	).
+
 	% Create the environment pointer and initialize it:
 	%
 	%	struct <EnvClassName> *env_ptr;
@@ -503,16 +513,7 @@
 	%
 	EnvPtrVarName = data(var(mlds__var_name("env_ptr", no))),
 	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.
-		% XXX Perhaps if we used value classes this could go
-		% away.
-	( Target = il ->
-		EnvPtrVarType = EnvTypeName
-	;
-		EnvPtrVarType = mlds__ptr_type(EnvTypeName)
-	),
+	EnvPtrVarType = ml_make_env_ptr_type(Globals, EnvTypeName),
 	EnvPtrVarDefnBody = mlds__data(EnvPtrVarType, no_initializer),
 	EnvPtrVarDecl = mlds__defn(EnvPtrVarName, Context, EnvPtrVarFlags,
 		EnvPtrVarDefnBody),
@@ -520,13 +521,13 @@
 	%
 	% generate the following statement:
 	%
-	%	env_ptr = (EnvPtrVarType) <EnvPtrVal>;
+	%	env_ptr = <EnvPtrVal>;
 	%
-	% XXX Do we need the cast? If so, why?
+	% (note that the caller of this routine is responsible
+	% for inserting a cast in <EnvPtrVal> if needed).
 	%
 	EnvPtrVar = qual(ModuleName, mlds__var_name("env_ptr", no)),
-	AssignEnvPtr = assign(var(EnvPtrVar, EnvPtrVarType),
-		unop(cast(EnvPtrVarType), EnvPtrVal)),
+	AssignEnvPtr = assign(var(EnvPtrVar, EnvPtrVarType), EnvPtrVal),
 	InitEnvPtr = mlds__statement(atomic(AssignEnvPtr), Context).
 
 	% Given the declaration for a function parameter, produce a
@@ -1068,7 +1069,7 @@
 		FieldName = named_field(qual(EnvModuleName, ThisVarFieldName),
 			EnvPtrVarType),
 		Tag = yes(0),
-		Lval = field(Tag, EnvPtr, FieldName, FieldType, ClassType)
+		Lval = field(Tag, EnvPtr, FieldName, FieldType, EnvPtrVarType)
 	;
 		%
 		% leave everything else unchanged
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.90
diff -u -d -r1.90 mlds_to_c.m
--- compiler/mlds_to_c.m	2001/06/27 11:29:29	1.90
+++ compiler/mlds_to_c.m	2001/06/27 13:05:17
@@ -2525,13 +2525,13 @@
 	% represented as MR_Box, so we need to box them if necessary.
 	%
 	mlds_indent(Context, Indent),
-	io__write_string("MR_field("),
+	io__write_string("MR_hl_field("),
 	mlds_output_tag(Tag),
 	io__write_string(", "),
 	mlds_output_lval(Target),
 	io__write_string(", "),
 	io__write_int(ArgNum),
-	io__write_string(") = (MR_Word) "),
+	io__write_string(") = "),
 	mlds_output_boxed_rval(ArgType, Arg),
 	io__write_string(";\n"),
 	mlds_output_init_args(Args, ArgTypes, Context,
@@ -2552,35 +2552,33 @@
 		; FieldType = mlds__mercury_type(term__variable(_), _)
 		}
 	->
-		% XXX this generated code is ugly;
-		% it would be nicer to use a different macro
-		% than MR_field(), one which had type `MR_Box'
-		% rather than `MR_Word'.
-		io__write_string("(* (MR_Box *) &")
+		io__write_string("(")
 	;
 		% The field type for field(_, _, offset(_), _, _) lvals
 		% must be something that maps to MR_Box.
 		{ error("unexpected field type") }
 	),
 	( { MaybeTag = yes(Tag) } ->
-		io__write_string("MR_field("),
+		io__write_string("MR_hl_field("),
 		mlds_output_tag(Tag),
 		io__write_string(", ")
 	;
-		io__write_string("MR_mask_field(")
+		io__write_string("MR_hl_mask_field("),
+		io__write_string("(MR_Word) ")
 	),
-	io__write_string("(MR_Word) "),
 	mlds_output_rval(Rval),
 	io__write_string(", "),
 	mlds_output_rval(OffsetRval),
 	io__write_string("))").
 mlds_output_lval(field(MaybeTag, PtrRval, named_field(FieldName, CtorType),
-		_FieldType, _PtrType)) -->
-	% XXX we shouldn't bother with this cast in the case where
-	% PtrType == CtorType
+		_FieldType, PtrType)) -->
 	io__write_string("("),
-	mlds_output_cast(CtorType),
 	( { MaybeTag = yes(0) } ->
+		( { PtrType \= CtorType } ->
+			mlds_output_cast(CtorType)
+		;
+			[]
+		),
 		( { PtrRval = mem_addr(Lval) } ->
 			mlds_output_lval(Lval),
 			io__write_string(").")
@@ -2589,6 +2587,7 @@
 			io__write_string(")->")
 		)
 	;
+		mlds_output_cast(CtorType),
 		( { MaybeTag = yes(Tag) } ->
 			io__write_string("MR_body("),
 			mlds_output_rval(PtrRval),
@@ -2665,16 +2664,16 @@
 /**** XXX do we need this?
 mlds_output_rval(lval(Lval)) -->
 	% if a field is used as an rval, then we need to use
-	% the MR_const_field() macro, not the MR_field() macro,
+	% the MR_hl_const_field() macro, not the MR_hl_field() macro,
 	% to avoid warnings about discarding const,
 	% and similarly for MR_mask_field.
 	( { Lval = field(MaybeTag, Rval, FieldNum, _, _) } ->
 		( { MaybeTag = yes(Tag) } ->
-			io__write_string("MR_const_field("),
+			io__write_string("MR_hl_const_field("),
 			mlds_output_tag(Tag),
 			io__write_string(", ")
 		;
-			io__write_string("MR_const_mask_field(")
+			io__write_string("MR_hl_const_mask_field(")
 		),
 		mlds_output_rval(Rval),
 		io__write_string(", "),
Index: runtime/mercury_tags.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_tags.h,v
retrieving revision 1.12
diff -u -d -r1.12 mercury_tags.h
--- runtime/mercury_tags.h	2001/06/02 09:35:04	1.12
+++ runtime/mercury_tags.h	2001/06/27 13:00:42
@@ -73,6 +73,16 @@
 #define	MR_const_mask_field(p, i)	((const MR_Word *) MR_strip_tag(p))[i]
 
 /*
+** The hl_ variants are the same, except their return type is MR_Box
+** rather than MR_Word.  These are used by the MLDS->C back-end.
+*/
+#define	MR_hl_field(t, p, i)		((MR_Box *) MR_body((p), (t)))[i]
+#define	MR_hl_const_field(t, p, i)	((const MR_Box *) MR_body((p), (t)))[i]
+
+#define	MR_hl_mask_field(p, i)		((MR_Box *) MR_strip_tag(p))[i]
+#define	MR_hl_const_mask_field(p, i)	((const MR_Box *) MR_strip_tag(p))[i]
+
+/*
 ** the following macros are used by handwritten C code that needs to access 
 ** Mercury data structures. The definitions of these macros depend on the data 
 ** representation scheme used by compiler/make_tags.m.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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