home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:19139 comp.sys.ibm.pc.programmer:726 alt.msdos.programmer:3070 comp.sys.ibm.pc.misc:16286
- Newsgroups: comp.lang.c,comp.sys.ibm.pc.programmer,alt.msdos.programmer,comp.sys.ibm.pc.misc
- Path: sparky!uunet!munnari.oz.au!bunyip.cc.uq.oz.au!marlin.jcu.edu.au!coral.cs.jcu.edu.au!stuart
- From: stuart@coral.cs.jcu.edu.au (Stuart Kemp)
- Subject: BEWARE! Borland's BCC produces bad code (with example!)
- Message-ID: <stuart.726023333@coral>
- Summary: '-Z' on BCC produces bad code
- Keywords: BCC, PC, MSDOS
- Sender: news@marlin.jcu.edu.au (USENET News System)
- Organization: James Cook University
- Date: 3 Jan 93 01:08:53 GMT
- Lines: 73
-
-
- The appended program was gutted from something I am working on ...
- If you compile it with "bcc -ml -Z file.c" it will produce bad code;
- compile with "bcc -ml file.c" and all is okay. (The output generated should
- always list the same segment, with different offsets.) This happens on both
- BCC v3.0 and v3.1. Unfortunately, you will never know when this is going
- to bite you ... would suggest that you be beware of compiling with (just)
- '-O2' (or '-Ox') since this implies '-Z' as well; better to use '-Ox -Z-'.
-
- The "bad" portion of code generated with the '-Z' is in the printf()
- statement in the 'for' loop ... the assembly code (generated with '-S') is:
-
- ;
- ; fprintf(log, "%2d %p\n", i, T->Hdrs[i]);
- ;
- mov es,word ptr es:[bx+6] ; BUG!!!!!
- push word ptr es:[si+2]
- push word ptr es:[si]
-
- The marked statement should be omitted completely. Doing so and compiling
- the resultant assembly code yields a working program.
-
- Anyone got any other (small!) examples of where compilers produce wrong
- code?
-
- -SRK
-
- -------------- Snip Snip -------------
- #include <stdio.h>
- #include <stdlib.h>
-
- typedef
- struct _GTbl
- { void *Data;
- void **Hdrs;
- unsigned short StructSize;
- } GTbl;
-
- FILE *log;
-
- /* Allocates a buffer area, then set some pointers into this area */
- /* NULL checks removed while examining assembly code! */
- void
- Bug(GTbl *T)
- {
- short i;
-
- T->Hdrs = (void**) malloc(10 * sizeof(void*));
- T->Data = (void*) malloc(10 * T->StructSize);
-
- fprintf(log, "Hdrs: %p\n", T->Hdrs);
- fprintf(log, "Data: %p\n", T->Data);
- for (i = 0; i < 10; i++)
- { T->Hdrs[i] = (void*) ((char*)T->Data + i * T->StructSize);
- fprintf(log, "%2d %p\n", i, T->Hdrs[i]); /* BUG IN ASSEMBLY HERE */
- }
- }
-
- main()
- {
- GTbl T;
-
- log = fopen("LOG2", "w");
- T.StructSize = 16;
- Bug(&T);
- fclose(log);
- return(0);
- }
- --
- Dept of Computer Science Internet: stuart@coral.cs.jcu.edu.au
- James Cook University
- Townsville Tel: +61 77 814992
- Qld 4811 Australia FAX: +61 77 814092
-