Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!utgpu!water!watmath!clyde!rutgers!rochester!PT.CS.CMU.EDU!sei!sei.cmu.edu!firth
From: firth@sei.cmu.edu.UUCP
Newsgroups: comp.lang.c
Subject: Re: Check for function results
Message-ID: <3379@aw.sei.cmu.edu>
Date: Wed, 25-Nov-87 13:16:55 EST
Article-I.D.: aw.3379
Posted: Wed Nov 25 13:16:55 1987
Date-Received: Sun, 29-Nov-87 01:40:22 EST
References: <10530@brl-adm.ARPA>
Sender: netnews@sei.cmu.edu
Reply-To: firth@bd.sei.cmu.edu.UUCP (PUT YOUR NAME HERE)
Organization: Carnegie-Mellon University, SEI, Pgh, Pa
Lines: 61

In article <10530@brl-adm.ARPA> I1090801%DBSTU1.BITNET@CUNYVM.CUNY.EDU writes:
 I am a student of computer science and I have to write a C-compiler.
 The problem I came across is: does a C-compiler have to generate code
 to check if a function really delivers a result in all cases?
 Example: In the following (nonsense) function
    int f()
    {
         int a;
         a = 1;              /* only to set a to a non-garbage value */
         if (...)
              return a;
         else
              ...            /* statements without any return */
    }
 a value is delivered in the then part but in the else part nothing
 happens and the program just runs out of the end of the function.
 So, in this case, some garbage is delivered. In this example you can
 easily see this, but things get worse if the function is more
 complicated. I think it isn't easy for a compiler to see all this
 at compile-time, so things should be checked during runtime of a
 program. My idea is to generate code that sets an internal flag
 to FALSE at the start of the function. Each return(expression) should
 set this flag to TRUE and then jump to the end of the function where
 the return is handled. Here the flag should be checked and an error-
 message be generated if it is still FALSE. This way it can be deteced
 if the way through the function passed a return(expression).
 I wonder if all this is really necessary, but I can't find anything
 about this problem in K&R. Do other compilers check this? I think,
 some strange errors can be detected by this method.

Well, a C compiler probably doesn't have to check ANYTHING!  In addition,
this check is not necessarily a good one to put in: it is legal to call a
C function and throw away the result, so, who knows, maybe every call that
bypasses a return statement is one that doesn't want the result.

However, if you do put the check in, there is a much simpler mechanism:
have the return statements all execute a proper return, and plug the end
of the function body with your error code.  That is essentially free: you
execute no extra code to do the check.  A typical implementation might
look like this:

	f: ; procedure entry sequence

	   ...
	   ; return a

	   move a into R0
	   jump to exitlabel

	   ...

	    ; end of function
	   goto NO_RETURN_VALUE_ERROR

	exitlabel:
	   ; normal return sequence goes here

I assume you know enough to hoist that code at exitlabel if there is only
one return statement in the function.  (To do that is one pass: generate
the exit code at the point of the first return statement, and if necessary
jump back to it.)