[m-rev.] diff: mlds_to_c.m: fix tail-call type error

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Feb 6 21:04:00 AEDT 2002


Estimated hours taken: 0.5
Branches: main

Fix a bug caused by my previous change to ml_call_gen.m
where calls to procedures with determinism `erroneous' or `failure'
get marked as tail calls.

compiler/mlds_to_c.m:
	Only output `return' statements for calls marked as tail calls
	if the callee argument types match the caller argument types.

Workspace: /home/earth/fjh/ws-earth4/mercury
Index: compiler/mlds_to_c.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_c.m,v
retrieving revision 1.116
diff -u -d -r1.116 mlds_to_c.m
--- compiler/mlds_to_c.m	28 Jan 2002 05:30:24 -0000	1.116
+++ compiler/mlds_to_c.m	6 Feb 2002 09:52:00 -0000
@@ -991,7 +991,7 @@
 		% XXX this value for FuncInfo is bogus
 		% However, this output is only for debugging anyway,
 		% so it doesn't really matter.
-		{ FuncInfo = func_info(Name) },
+		{ FuncInfo = func_info(Name, mlds__func_signature([], [])) },
 		mlds_output_statement(Indent, FuncInfo, GC_TraceCode),
 		io__write_string("#endif\n")
 	).
@@ -1276,8 +1276,8 @@
 		func_params, function_body, io__state, io__state).
 :- mode mlds_output_func(in, in, in, in, in, di, uo) is det.
 
-mlds_output_func(Indent, Name, Context, Signature, FunctionBody) -->
-	mlds_output_func_decl(Indent, Name, Context, Signature),
+mlds_output_func(Indent, Name, Context, Params, FunctionBody) -->
+	mlds_output_func_decl(Indent, Name, Context, Params),
 	(
 		{ FunctionBody = external },
 		io__write_string(";\n")
@@ -1290,7 +1290,8 @@
 
 		mlds_maybe_output_time_profile_instr(Context, Indent + 1, Name),
 
-		{ FuncInfo = func_info(Name) },
+		{ Signature = mlds__get_func_signature(Params) },
+		{ FuncInfo = func_info(Name, Signature) },
 		mlds_output_statement(Indent + 1, FuncInfo, Body),
 
 		mlds_indent(Context, Indent),
@@ -1965,7 +1966,7 @@
 %
 
 :- type func_info
-	--->	func_info(mlds__qualified_entity_name).
+	--->	func_info(mlds__qualified_entity_name, mlds__func_signature).
 
 :- pred mlds_output_statements(indent, func_info, list(mlds__statement),
 		io__state, io__state).
@@ -1993,7 +1994,7 @@
 	mlds_indent(Indent),
 	io__write_string("{\n"),
 	( { Defns \= [] } ->
-		{ FuncInfo = func_info(FuncName) },
+		{ FuncInfo = func_info(FuncName, _) },
 		{ FuncName = qual(ModuleName, _) },
 
 		% output forward declarations for any nested functions
@@ -2156,9 +2157,23 @@
 	% function call/return
 	%
 mlds_output_stmt(Indent, CallerFuncInfo, Call, Context) -->
-	{ Call = call(_Signature, FuncRval, MaybeObject, CallArgs,
+	{ Call = call(Signature, FuncRval, MaybeObject, CallArgs,
 		Results, IsTailCall) },
-	{ CallerFuncInfo = func_info(Name) },
+	{ CallerFuncInfo = func_info(CallerName, CallerSignature) },
+
+	% 
+	% We need to enclose the generated code inside an extra pair
+	% of curly braces, in case we generate more than one statement
+	% (e.g. because we generate extra statements for profiling
+	% or for tail call optimization) and the generated code is
+	% e.g. inside an if-then-else.
+	% 
+	mlds_indent(Indent),
+	io__write_string("{\n"),
+
+	mlds_maybe_output_call_profile_instr(Context,
+			Indent + 1, FuncRval, CallerName),
+
 	%
 	% Optimize general tail calls.
 	% We can't really do much here except to insert `return'
@@ -2169,19 +2184,21 @@
 	% then this would result in code that is not legal ANSI C
 	% (although it _is_ legal in GNU C and in C++),
 	% so for that case, we put the return statement after
-	% the call -- see below.  We need to enclose it inside
-	% an extra pair of curly braces in case this `call'
-	% is e.g. inside an if-then-else.
+	% the call -- see below.
+	%
+	% Note that it's only safe to add such a return statement if
+	% the calling procedure has the same argument types as the callee.
+	% (Calls where the types are different can be marked as tail calls
+	% if they are known to never return.)
 	%
-	mlds_indent(Indent),
-	io__write_string("{\n"),
-
-	mlds_maybe_output_call_profile_instr(Context,
-			Indent + 1, FuncRval, Name),
-
 	mlds_indent(Context, Indent + 1),
-
-	( { IsTailCall = tail_call, Results \= [] } ->
+	{ Signature = mlds__func_signature(_, RetTypes) },
+	{ CallerSignature = mlds__func_signature(_, CallerRetTypes) },
+	(
+		{ IsTailCall = tail_call },
+		{ Results \= [] },
+		{ RetTypes = CallerRetTypes }
+	->
 		io__write_string("return ")
 	;
 		[]
@@ -2206,12 +2223,16 @@
 	io__write_list(CallArgs, ", ", mlds_output_rval),
 	io__write_string(");\n"),
 
-	( { IsTailCall = tail_call, Results = [] } ->
+	(
+		{ IsTailCall = tail_call },
+		{ Results = [] },
+		{ RetTypes = CallerRetTypes }
+	->
 		mlds_indent(Context, Indent + 1),
 		io__write_string("return;\n")
 	;
 		mlds_maybe_output_time_profile_instr(Context,
-				Indent + 1, Name)
+				Indent + 1, CallerName)
 	),
 	mlds_indent(Indent),
 	io__write_string("}\n").
@@ -2534,7 +2555,7 @@
 	mlds_indent(Indent),
 	io__write_string("{\n"),
 
-	{ FuncInfo = func_info(FuncName) },
+	{ FuncInfo = func_info(FuncName, _FuncSignature) },
 	mlds_maybe_output_heap_profile_instr(Context, Indent + 1, Args,
 			FuncName, MaybeCtorName),
 

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