Newsgroups: | comp.arch,comp.compilers |
From: | pardo@cs.washington.edu (David Keppel) |
Keywords: | optimize, C |
Organization: | Computer Science & Engineering, U. of Washington, Seattle |
References: | 94-10-108 94-10-157 |
Date: | Mon, 31 Oct 1994 18:51:01 GMT |
>>[Why not permit compilers to perform "illegal" reorganizations?"]
>[Lots of things would break:
> where C structures describe hardware layouts,
> across system calls, things that read structures from disk, ....]
The C standard allows arbitrary *padding* choices for structure layout,
except that the first element appears at offset zero. Thus, two
identical structures compiled on the same machine may have differing
memory layouts.
The upshot is that *-> even without any reordering optimizations <-* all
of the following may break:
- Structures that describe hardware layouts, such as device registers
- Structure I/O to networks, disks, etc.
- OS calls that pass structures
- Inter-language calls passing structure data
- Calls between C code fragments compiled with different compilers
- Calls between C code compiled with the same compiler with different
optimization levels
In practice, C compiler vendors try to maintain compatability with
whatever layout convention is used by the first compiler for the
machine, so programmers typically ``get away'' with writing unsafe
code.
Structure *reordering* optimizations would break lots of C code because
the C standard guarantees the *order* of structure elements. However,
much of the code I've seen that relies on structure order also relies
on structure *padding* (offsets) that the standard does not define.
Technically, such code is already broken. As optimizers improve, this
code is going to fail more and more often.
Blowing my own horn: see the `Portability' section of _Recommended C
Style and Coding Standards_, available via anonymous ftp from
`ftp.cs.washington.edu' in `pub/cstyle.tar.Z' or as formatted
PostScript(tm) from `pub/pardo/cstyle.ps.Z'. The files are compressed,
so be sure to do the transfer in binary (image) mode. Or via WWW, see
`http://www.cs.washington.edu/homes/pardo/papers.d/cstyle.html'. A
japanese translation of the style guide is also available.
;-D on ( Data strictures ) Pardo
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.