Oberon Community Platform Forum
November 22, 2019, 06:04:44 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Compiler : RETURN Problem  (Read 3846 times)
maliya
Newbie
*
Posts: 6


« on: September 11, 2008, 09:04:03 AM »

  if function no return statement,the compiler only warning,still generate code.
but the code seem has bug.

example:(* compare func width func2*)

MODULE Test1;

   PROCEDURE Func();
   BEGIN
   
   END Func;
   
   PROCEDURE Func2():LONGINT;
   BEGIN
   
   END Func2;
   
   PROCEDURE Do*();
      VAR j:LONGINT;
   BEGIN
      Func();
      j:=Func2();
   END Do;

   

END Test1.Do

SystemTools.Free Test1 ~

decode:

Func:
codeOffset = 00000007H

00000000H   55    PUSH   EBP   
00000001H   8B EC    MOV   EBP, ESP   
00000003H   8B E5    MOV   ESP, EBP   
00000005H   5D    POP   EBP   
00000006H   C3    RET   



Func2:
codeOffset = 0000000EH

00000000H   55    PUSH   EBP   
00000001H   8B EC    MOV   EBP, ESP   
00000003H   6A 03    PUSH   3   
00000005H   CC    INT   3
Logged
negelef
Administrator
Jr. Member
*****
Posts: 55


« Reply #1 on: September 11, 2008, 11:56:12 AM »

Sorry, but the compiler does actually generate an error for missing return statements. With some sort of control flow analysis it could do so reliably at compile time. Otherwise, and that's currently the case, it can generate code for a trap (the INT instruction) if the control flow reaches the end of a procedure that's supposed to return a value beforehand. So your "bug" is actually a feature.
Logged
maliya
Newbie
*
Posts: 6


« Reply #2 on: September 11, 2008, 05:28:59 PM »

Try in WinAos PET compile the Test1.Mod,there are no error ,only 3 warning!
 
In programming,we often ignore  warnings,because warnings  mean not
perfect( not optimize )only.

  if a application raise a trap for a warning( a red window), is this a good  feature for programmer, a good feature for user?

  so I think the compiler should give a error,dont generate codes(like blackbox).
 


 
Logged
negelef
Administrator
Jr. Member
*****
Posts: 55


« Reply #3 on: September 11, 2008, 06:29:11 PM »

I also think that a good compiler should refuse to compile any invalid module. But statically checking the validity of a module is very hard and sometimes impossible. That's why our compiler for simplicity emits runtime checks for things it didn't check at compile time. Don't forget: Correct code never triggers any of these checks. Therefore they are, both at compile and runtime, indeed a good feature for the programmer as they reveal programming errors of any sort.

The reason why the compiler prefers to warn you about missing return statements instead of diagnose this error is actually quite simple: It doesn't analyse the control flow as required and said before but merely counts the occurences of the return statement in a function (try LOOP EXIT; RETURN 0 END in Func2).
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!