[m-rev.] for review: improve procname mangling for .NET backend.

Tyson Dowd trd at cs.mu.OZ.AU
Wed May 9 02:32:59 AEST 2001


Hi,

Fergus and I came up with several alternative name mangling schemes
while at the last dev-lab -- this is a variation on one of the 90/10
solutions (90% of the benefit for 10% of the work)

To achieve better (i.e. less) mangling than this, we need to work much
harder, and (probably) introduce some sort of renaming pragma.

I'd appreciate some comments on the mangling scheme as I'm still not
100% certain it really works (perhaps it was a bit of a bad idea to quit
drinking coffee last week...).

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


Estimated hours taken: 5
Branches: main, dotnet-foreign

Minimize the amount of procedure name mangling done by the .NET backend.

compiler/ml_code_util.m:
compiler/ml_elim_nested.m:
compiler/ml_util.m:
compiler/mlds.m:
compiler/mlds_to_c.m:
compiler/mlds_to_java.m:
	Add the code_model to the proc_label.

compiler/mlds_to_il.m:
	Mangle much more carefully.  We still mangle in a predictable,
	context-insensitive manner, however we take advantage of the
	.NET backend to avoid name clashes in most cases.

Index: compiler/ml_code_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_code_util.m,v
retrieving revision 1.34
diff -u -r1.34 ml_code_util.m
--- compiler/ml_code_util.m	2001/05/02 11:36:36	1.34
+++ compiler/ml_code_util.m	2001/05/08 15:27:39
@@ -1183,7 +1183,7 @@
 ml_gen_pred_label_from_rtti(RttiProcLabel, MLDS_PredLabel, MLDS_Module) :-
 	RttiProcLabel = rtti_proc_label(PredOrFunc, ThisModule, PredModule,	
 		PredName, PredArity, ArgTypes, _PredId, ProcId,
-		_VarSet, _HeadVars, _ArgModes, _CodeModel,
+		_VarSet, _HeadVars, _ArgModes, CodeModel,
 		IsImported, _IsPseudoImported, _IsExported,
 		IsSpecialPredInstance),
 	(
@@ -1246,7 +1246,7 @@
 			MaybeDeclaringModule = no
 		),
 		MLDS_PredLabel = pred(PredOrFunc, MaybeDeclaringModule,
-				PredName, PredArity)
+				PredName, PredArity, CodeModel)
 	),
 	MLDS_Module = mercury_module_name_to_mlds(DefiningModule).
 
Index: compiler/ml_elim_nested.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_elim_nested.m,v
retrieving revision 1.24
diff -u -r1.24 ml_elim_nested.m
--- compiler/ml_elim_nested.m	2001/05/02 11:36:37	1.24
+++ compiler/ml_elim_nested.m	2001/05/08 15:27:40
@@ -606,8 +606,8 @@
 
 :- func ml_pred_label_name(mlds__pred_label) = string.
 
-ml_pred_label_name(pred(PredOrFunc, MaybeDefiningModule, Name, Arity))
-		= LabelName :-
+ml_pred_label_name(pred(PredOrFunc, MaybeDefiningModule, Name, Arity,
+		_CodeModel)) = LabelName :-
 	( PredOrFunc = predicate, Suffix = "p"
 	; PredOrFunc = function, Suffix = "f"
 	),
Index: compiler/ml_util.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/ml_util.m,v
retrieving revision 1.8
diff -u -r1.8 ml_util.m
--- compiler/ml_util.m	2001/05/02 11:36:37	1.8
+++ compiler/ml_util.m	2001/05/08 15:27:40
@@ -143,7 +143,7 @@
 	list__member(Defn, Defns),
 	Defn = mlds__defn(Name, _, _, _),
 	Name = function(FuncName, _, _, _), 
-	FuncName = pred(predicate, _, "main", 2).
+	FuncName = pred(predicate, _, "main", 2, _).
 
 can_optimize_tailcall(Name, Call) :-
 	Call = call(_Signature, FuncRval, MaybeObject, _CallArgs,
Index: compiler/mlds.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds.m,v
retrieving revision 1.52
diff -u -r1.52 mlds.m
--- compiler/mlds.m	2001/05/08 09:55:42	1.52
+++ compiler/mlds.m	2001/05/08 15:27:40
@@ -278,7 +278,7 @@
 :- interface.
 
 :- import_module hlds_module, hlds_pred, hlds_data.
-:- import_module prog_data, builtin_ops, rtti.
+:- import_module prog_data, builtin_ops, rtti, code_model.
 :- import_module type_util.
 
 % To avoid duplication, we use a few things from the LLDS
@@ -1375,7 +1375,8 @@
 				% if different to the defining module
 			maybe(mercury_module_name),
 			string,			% name
-			arity			% arity
+			arity,			% arity
+			code_model		% code model
 		)
 			
 	;	special_pred(
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.85
diff -u -r1.85 mlds_to_c.m
--- compiler/mlds_to_c.m	2001/05/02 11:36:38	1.85
+++ compiler/mlds_to_c.m	2001/05/08 15:27:40
@@ -52,7 +52,7 @@
 :- import_module export.	% for export__type_to_type_string
 :- import_module globals, options, passes_aux.
 :- import_module builtin_ops, c_util, modules.
-:- import_module prog_data, prog_out, type_util, error_util.
+:- import_module prog_data, prog_out, type_util, error_util, code_model.
 
 :- import_module bool, int, string, library, list.
 :- import_module assoc_list, term, std_util, require.
@@ -1336,7 +1336,8 @@
 			% don't module-qualify main/2
 			%
 			{ Name = function(PredLabel, _, _, _) },
-			{ PredLabel = pred(predicate, no, "main", 2) }
+			{ PredLabel = pred(predicate, no, "main", 2,
+				model_det) }
 		;
 			%
 			% don't module-qualify base_typeclass_infos
@@ -1369,7 +1370,7 @@
 		%
 		{ QualifiedName = qual(_ModuleName, Name) },
 		{ Name = PredLabel - _ProcId },
-		{ PredLabel = pred(predicate, no, "main", 2) }
+		{ PredLabel = pred(predicate, no, "main", 2, model_det) }
 	->
 		mlds_output_proc_label(Name)
 	;
@@ -1422,7 +1423,8 @@
 :- pred mlds_output_pred_label(mlds__pred_label, io__state, io__state).
 :- mode mlds_output_pred_label(in, di, uo) is det.
 
-mlds_output_pred_label(pred(PredOrFunc, MaybeDefiningModule, Name, Arity)) -->
+mlds_output_pred_label(pred(PredOrFunc, MaybeDefiningModule, Name, Arity,
+		_CodeModel)) -->
 	( { PredOrFunc = predicate, Suffix = "p" }
 	; { PredOrFunc = function, Suffix = "f" }
 	),
Index: compiler/mlds_to_il.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.23
diff -u -r1.23 mlds_to_il.m
--- compiler/mlds_to_il.m	2001/05/02 16:34:36	1.23
+++ compiler/mlds_to_il.m	2001/05/08 15:27:45
@@ -136,7 +136,7 @@
 :- import_module globals, options, passes_aux.
 :- import_module builtin_ops, c_util, modules, tree.
 :- import_module prog_data, prog_out, llds_out.
-:- import_module rtti, type_util.
+:- import_module rtti, type_util, code_model.
 
 :- import_module ilasm, il_peephole.
 :- import_module ml_util, ml_code_util, error_util.
@@ -329,7 +329,7 @@
 			% If this is main, add the entrypoint, set a
 			% flag, and call the initialization instructions
 			% in the cctor of this module.
-		( { PredLabel = pred(predicate, no, "main", 2) },
+		( { PredLabel = pred(predicate, no, "main", 2, model_det) },
 		  { MaybeSeqNum = no }
 		->
 			{ EntryPoint = [entrypoint] },
@@ -1967,30 +1967,81 @@
 % Name mangling.
 
 
-	% XXX we should check into the name mangling done here to make
-	% sure it is all necessary.
-	% We may need to do different name mangling for CLS compliance
+	% XXX We may need to do different name mangling for CLS compliance
 	% than we would otherwise need.
-predlabel_to_id(pred(PredOrFunc, MaybeModuleName, Name, Arity), ProcId, 
-			MaybeSeqNum, Id) :-
-		( PredOrFunc = predicate, PredOrFuncStr = "p" 
-		; PredOrFunc = function, PredOrFuncStr = "f" 
-		),
-		proc_id_to_int(ProcId, ProcIdInt),
+	%
+	% We mangle as follows:
+	%	- Problem:
+	%	  Two preds or funcs with different arities in Mercury end
+	%	  up having the same types and arities in IL.
+	%
+	%	  To avoid this we append _<arity> to every procedure
+	%	  name.
+	%
+	%	- Problem:
+	%	  A semidet pred returns its success value, and so has
+	%	  the same return type (bool) as a function.
+	%
+	%	  To avoid this, we mangle all semidet preds and
+	%	  functions to include whether they are a pred or a
+	%	  function using _p or _f.
+	%
+	%	- Problem:
+	%	  A predicate or function with more than one mode
+	%
+	%	  To avoid this, we mangle all modes > 0 by adding
+	%	  _m<modenum> to the procedure name.
+	%
+	%	- We append the sequence number (if there is one) as
+	%	  _i<seqnum>.
+	%
+	%	- We prepend the module name (if there is one) as
+	%	  <modulename>_.
+	%	
+	% So the mangled name is:
+	% (<modulename>_)<procname>_<arity>(_f|_p)(_m<modenum>)(_i<seqnum>)
+	%
+	% Where parentheses indicate optional components.
+	%
+	% Since each optional component (except the modulename) is after
+	% the mandatory arity, and the components have unique prefixes,
+	% it isn't possible to generate names that conflict with user
+	% names. 
+	%
+predlabel_to_id(pred(PredOrFunc, MaybeModuleName, Name, Arity, CodeModel),
+			ProcId, MaybeSeqNum, Id) :-
 		( MaybeModuleName = yes(ModuleName) ->
 			mlds_to_il__sym_name_to_string(ModuleName, MStr),
 			string__format("%s_", [s(MStr)], MaybeModuleStr)
 		;
 			MaybeModuleStr = ""
 		),
+		( CodeModel = model_semi ->
+			( 
+				PredOrFunc = predicate,
+				PredOrFuncStr = "_p" 
+			;
+				PredOrFunc = function,
+				PredOrFuncStr = "_f" 
+			)
+		;
+			PredOrFuncStr = ""
+		),
+		proc_id_to_int(ProcId, ProcIdInt),
+		( ProcIdInt = 0 ->
+			MaybeProcIdInt = ""
+		;
+			string__format("_m%d", [i(ProcIdInt)], MaybeProcIdInt)
+		),
 		( MaybeSeqNum = yes(SeqNum) ->
-			string__format("_%d", [i(SeqNum)], MaybeSeqNumStr)
+			string__format("_i%d", [i(SeqNum)], MaybeSeqNumStr)
 		;
 			MaybeSeqNumStr = ""
 		),
-		string__format("%s%s_%d_%s_%d%s", [s(MaybeModuleStr), s(Name),
-			 i(Arity), s(PredOrFuncStr), i(ProcIdInt),
-			 s(MaybeSeqNumStr)], UnMangledId),
+		string__format("%s%s_%d%s%s%s", [
+			s(MaybeModuleStr), s(Name),
+			i(Arity), s(PredOrFuncStr), s(MaybeProcIdInt),
+			s(MaybeSeqNumStr)], UnMangledId),
 		llds_out__name_mangle(UnMangledId, Id).
 
 predlabel_to_id(special_pred(PredName, MaybeModuleName, TypeName, Arity),
Index: compiler/mlds_to_java.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_java.m,v
retrieving revision 1.3
diff -u -r1.3 mlds_to_java.m
--- compiler/mlds_to_java.m	2001/05/02 11:36:39	1.3
+++ compiler/mlds_to_java.m	2001/05/08 15:27:46
@@ -346,7 +346,7 @@
    	 	( 
 			Label = special_pred(PredName, _, _, _) 
 		;
-			Label = pred(_, _, PredName, _)
+			Label = pred(_, _, PredName, _, _)
 		)
 	->
 		
@@ -1119,7 +1119,8 @@
 :- pred output_pred_label(mlds__pred_label, io__state, io__state).
 :- mode output_pred_label(in, di, uo) is det.
 
-output_pred_label(pred(PredOrFunc, MaybeDefiningModule, Name, Arity)) -->
+output_pred_label(pred(PredOrFunc, MaybeDefiningModule, Name, Arity,
+		_CodeModel)) -->
 	( { PredOrFunc = predicate, Suffix = "p" }
 	; { PredOrFunc = function, Suffix = "f" }
 	),

--------------------------------------------------------------------------
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