Issue with function declarations when using register based parameter passing

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issue with function declarations when using register based parameter passing

Jump to solution
1,679 Views
vier_kuifjes
Senior Contributor I

I managed to convert my Coldfire Lite based applications to work correctly when compiled with the new CodeWarrior 7.2. All it actually took was to modify the assembler routines to use register based parameter passing.

 

I first had an issue with the UDP server in my app, but this was caused by an incorrect declaration of a function. The actual error there was that I forgot to assign a type to the parameters. Instead of writing...

 

 

void process_udp_packet(ip_addr host_ip, char *data, unsigned int data_len)
{
...
}

 

 

...I wrote...

 

 

void process_udp_packet( host_ip, data, data_len )
{
...
}

 

I did not use a function prototype for this function.

 

The odd thing is, this worked fine with stack-based parameter passing. The second odd thing is that the compiler did not throw an error or a warning when compiling this.

 

Is this normal behaviour for a C compiler? Is it possible to enable flagging of this kind of omissions?

Labels (1)
0 Kudos
1 Solution
593 Views
bkatt
Contributor IV

"data" without the "char *" is assumed to have type int. 32-bit pointers and ints are handled the same with stack based parameter passing, but differently with register based parameters (pointers use the address registers, non-pointers use the data registers).

 

All modern code should require function prototypes and prohibit defaulting to int. Compilers may have specific options for these settings, or you may have specify a "c99" mode. The "normal" behavior that does not enforce these rules is specified by the c90 standard for compatibility with really old K&R style code. 

 

If you are not careful with declarations and header files, you can still get into trouble with different declarations for the same function.

View solution in original post

0 Kudos
4 Replies
594 Views
bkatt
Contributor IV

"data" without the "char *" is assumed to have type int. 32-bit pointers and ints are handled the same with stack based parameter passing, but differently with register based parameters (pointers use the address registers, non-pointers use the data registers).

 

All modern code should require function prototypes and prohibit defaulting to int. Compilers may have specific options for these settings, or you may have specify a "c99" mode. The "normal" behavior that does not enforce these rules is specified by the c90 standard for compatibility with really old K&R style code. 

 

If you are not careful with declarations and header files, you can still get into trouble with different declarations for the same function.

0 Kudos
593 Views
vier_kuifjes
Senior Contributor I

Thanks, I learned something new again! I found a checkbox "enable C99 extensions"  in the project settings and now the compiler throws an error when the parameter type is not declared.

 

Apparently the "bug" was already there in the Coldfire Lite sample code (file freescale_UDP_server.c), on which my code is based. So it appears that I'm not the one to be blamed! :smileywink:

 

I guess I really need a good resource (book) about C programming. There's a lot I don't know yet. All I know about C programming I learned by myself, which means a lot of info is missing...

 

Any suggestions on a good book?

0 Kudos
593 Views
jbezem
Contributor III

Since you seem to know already quite a lot, get the latest edition of H&S (5th ed.):

" C A Reference Manual", Samuel P. Harbison, Guy L. Steele Jr, ISBN 0-13-089592X, on Amazon: http://www.amzn.com/013089592X

Written by "frustrated" compiler-writers fed-up with the uncharted areas of C in the 80-ies, it covers most if not all dark corners of the language, the fifth edition covering the Standard up to C99, as well as many deviations commonly adopted by various compilers.

I haven't found any real but uncovered issue with it yet. Strongly recommended, but not for a beginner.

 

HTH,

 

Johan

0 Kudos
593 Views
vier_kuifjes
Senior Contributor I
Thanks!
0 Kudos