Jump to content
Sign in to follow this  
Steve_DeGroof

Odd Compiler Behavior

Recommended Posts

I recently ran across an oddity in the compiler. It appears that, under

certain conditions, a function call will move to the beginning of the function

containing it.

 

The following example is the simplest code I could find that produces the

behavior. In this case, the call to function c() is moved to the beginning of

function d() during compile.

 

#include <system.h>

int c()
{
 return 2;
}

int d(int e)
{
 if(e==1)
 {
    int b = c();
 }
}

void main (void)
{
 int a = d(4);
}

 

Here's the compiled d() function. The first line is "call _c". So, instead of

c() being called only when e is 1, it's called unconditionally.

 

;;;;;;;;int d(int e)
_d__code
call _c
bcf PCLATH, 3
bcf PCLATH, 4
movwf _code_tmp_0001 
movf __ret__hibyte, W
movwf _code_tmp_0000 
movf _code_tmp_0001 , W
movwf _b_d
movf _code_tmp_0000 , W
movwf _b_d+D'1'
;;;;;;;;{
;;;;;;;; if(e==1)
movlw D'0'
subwf param00_d+1, W
btfss STATUS, Z
goto label_0000
movlw D'1'
subwf param00_d, W
btfss STATUS, Z
goto label_0000
;;;;;;;; {
;;;;;;;; int b = c();
;;;;;;;; }
;;;;;;;;}
label_0000
return
_d__end

 

The key to the problem seems to be declaring a variable within a conditional

statement. If I declare b at the beginning of d(), though, the resulting assembly

code is correct.

 

int d(int e)
{
 int b;
 if(e==1)
 {
    b = c();
 }
}

 

Here's the new version of the compiled d() function. The "call _c" line is now

where it should be.

 

;;;;;;;;int d(int e)
_d__code
;;;;;;;;{
;;;;;;;; int b;
;;;;;;;; if(e==1)
movlw D'0'
subwf param00_d+1, W
btfss STATUS, Z
goto label_0000
movlw D'1'
subwf param00_d, W
btfss STATUS, Z
goto label_0000
;;;;;;;; {
;;;;;;;; b = c();
call _c
bcf PCLATH, 3
bcf PCLATH, 4
movwf _code_tmp_0001 
movf __ret__hibyte, W
movwf _code_tmp_0000 
movf _code_tmp_0001 , W
movwf _b_d
movf _code_tmp_0000 , W
movwf _b_d+D'1'
;;;;;;;; }
;;;;;;;;}
label_0000
return
_d__end

 

So, there's a relatively easy workaround.

 

I'm not really interested in submitting a bug report, since I'm using an older

version (4.6.3e) of the compiler. I would be interested in knowing if this

compiles differently with version 5, though.

Share this post


Link to post
Share on other sites

This happens because you have a variable declared inside if-scope and initialize it with function call. C2C compiler handles all locals at the beginning of a function (it doesn't have notion of variables declared in internal scopes within a function). That's why variable 'b' is initialized unconditionally.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Ya, I figured it was a scope issue. Easy enough to avoid by declaring your

variables up front. Is it safe to assume that this sort of thing would happen

with any conditional or loop (for/while) statement? If so, it might be handy

if the compiler threw an error when it encounters something like this.

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...