From: SourceForge.net <no...@so...> - 2004-04-15 19:19:27
|
Bugs item #604575, was opened at 2002-09-04 17:22 Message generated for change (Comment added) made by maartenbrock You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=100599&aid=604575&group_id=599 Category: C-Front End Group: fixed Status: Open Resolution: Fixed Priority: 5 Submitted By: Felipe Massia Pereira (felmasper) Assigned to: Johan Knol (johanknol) Summary: ptr ++ with cast and derref Initial Comment: GCC compiles this in ANSI mode. ~/test $ cat ptr++.c #include <stdio.h> char x[4] = { 1, 2, 3, 4 }; int acc, i; int *p; void main() { acc = 0; p = (int *) x; for (i=0; i<2; i++) acc += *((int *)p)++; printf("%d\n",acc); } ~/test $ sdcc -mz80 ptr++.c ptr++.c:13: error: 'lvalue' required for '++' operation . ptr++.c:15: error: code not generated for 'main' due to previous errors ---------------------------------------------------------------------- Comment By: Maarten Brock (maartenbrock) Date: 2004-04-15 11:37 Message: Logged In: YES user_id=888171 Hello all, Why is this BUG still open? Nowhere in the documentation have I seen this non-ansi extension is legal for sdcc. And if it is not supported this cannot be a bug. At most it can be a feature request I think (which is already there: 905167). Can we close this one? Trying to clean up the bug list, Maarten ---------------------------------------------------------------------- Comment By: Nobody/Anonymous (nobody) Date: 2003-04-09 12:07 Message: Logged In: NO It was late when I added that last comment so apologies for the bad grammar. Maybe I should clarify here. The C standard HAS to prohibit the OP's code example because a (void*) and an (int*) do not necessarily have the same representation. AIUI some supercomputers have void* (and char*) that is bigger than int* although I have never used one of these machines. OTOH, there is no reason why a compiler that has all its pointers the same size cannot support this as an extension. There have recently been a number of "bugs" logged where function pointers have been converted to void*. Again this is not guaranteed by the standard. Indeed, data and code may not share an address space at all Consider the following: [tim@twlinux tim]$ cat test.c #include <stdio.h> void *p; void f(void) { printf("Hello world\n"); } int main(void) { p = f; ((void (*)(void))p)(); return 0; } [tim@twlinux tim]$ gcc -Wall -ansi test.c [tim@twlinux tim]$ gcc -Wall -ansi -pedantic test.c test.c: In function `main': test.c:9: warning: ISO C forbids assignment between function pointer and `void *' ---------------------------------------------------------------------- Comment By: Nobody/Anonymous (nobody) Date: 2003-04-09 00:43 Message: Logged In: NO I don't have a copy of the C90 standard (and it isn't available anymore :-) But in C99 6.5.4(4) footnote 85 "A cast does not yield an lvalue. ..." Aside: AIUI the IP checksum doesn't require the htons() either. The checksum should be endian independent. See RFC1071 And, assuming that the cast is an optimization attempt then the &0xff isn't required either for the odd byte addition. Finally, I'm sure it would be faster to do the carry wraparound in the checksum routine and return a 16bit value. (I would expect a tcp checksum to be faster on 16bit machines if the carry is done per word rather than deferred although this will require assembly rather than C and so won't be portable) ---------------------------------------------------------------------- Comment By: Felipe Massia Pereira (felmasper) Date: 2003-04-08 23:32 Message: Logged In: YES user_id=387191 I compiled without -pedantic option. (gcc -ansi test.c) This code snippet is, in fact, an IP checksum calculation routine: sum the 1's complement of the 16-bit words. I thought it was legal ANSI. I guess I have modified the code a little from the original that I'm getting right now.... ok. Here it is. static u32_t chksum(void *dataptr, int len) { u32_t acc; for(acc = 0; len > 1; len -= 2) { acc += *((u16_t *)dataptr)++; } /* add up any odd byte */ if(len == 1) { acc += htons((u16_t)((*(u8_t *)dataptr) & 0xff) << 8); DEBUGF(INET_DEBUG, ("inet: chksum: odd byte %d\n", *(u8_t *)dataptr)); } return acc; } This portion of code is supposed to be ANSI. Copyright note: The above code snippet is from lwIP stack by Adam Dunkels / SICS. Felipe ---------------------------------------------------------------------- Comment By: Johan Knol (johanknol) Date: 2003-04-08 14:59 Message: Logged In: YES user_id=63512 This needs a second thought, disabled the "fix" in SDCCicode.c:153 ---------------------------------------------------------------------- Comment By: Nobody/Anonymous (nobody) Date: 2003-04-08 13:53 Message: Logged In: NO [tim@twlinux tim]$ gcc -Wall -ansi -pedantic test.c test.c:8: warning: return type of `main' is not `int' test.c: In function `main': test.c:13: invalid lvalue in increment How did you get this to compile with gcc? ---------------------------------------------------------------------- Comment By: Johan Knol (johanknol) Date: 2003-04-08 13:00 Message: Logged In: YES user_id=63512 fixed in SDCCicode.c:1.152 ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=100599&aid=604575&group_id=599 |