This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
MS/CW-style inline assembly for GCC
- From: Stan Shebs <shebs at apple dot com>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 03 May 2004 15:16:00 -0700
- Subject: MS/CW-style inline assembly for GCC
One of the long-notorious features of GCC has been its inline assembly
extension for C-dialect languages. The extension is both logical and
powerful, giving programmers a high degree of control over how the
assembly instructions are to be integrated and optimized within the
instruction stream, for instance by using the compiler's own
constraint letters to express preferred locations for data.
However, the syntax has also been somewhat inaccessible. Those
constraint letters were often undocumented, and are still somewhat
mysterious to users, while the use of strings for instructions makes
the construction of longer assembly sequences a rather difficult
undertaking. In addition, the "capture" of GCC's constraint letters
and pattern syntax into source code has the potential to reduce our
flexibility to change compiler internals later.
Metrowerks and Microsoft have taken a somewhat different approach to
inline asm, namely to introduce the notion of a block of assembly
code, bounded by { }, within which the syntax is more "assembly-like",
while still providing access to a variety of C constructs. Here is an
example using Metrowerks' CodeWarrior syntax (the MS version is very
similar, but uses __asm instead):
#define SH 5
int myfun(int x)
{
int rslt, loc = bar (x);
asm
{
add. rslt,loc,x
rlwinm rslt,rslt,SH*2+8,0,7
}
return rslt;
}
While this syntax is not as general or as powerful as GCC's syntax,
the tradeoff is that it is simpler for programmers who are not
compiler experts, and of course the compatibility with other compilers
is a plus for people doing ports of existing code. (In the Mac case,
heavily hand-tuned programs like QuickTime and MacMAME have thousands
of lines of source in this syntax; presumably there are Windows
programs in a similar situation.)
I implemented the CW syntax in Apple's version of GCC a while ago, and
it's been very helpful in enabling developers to use GCC in projects
that were formerly limited to being built with CW only.
It turns out to be almost entirely a frontend change; the lexer gets a
mode where it returns beginning-of-line/end-of-line as tokens, the
parser gets a small set of additional rules for understanding assembly
blocks, and the semantic actions merely construct the usual GCC asm
statements using rules that are partly generic and partly target
specific (for instance, which constraint letters to use for which
types of variables).
So before starting on a series of patches, I'd like to get an idea of
whether we all want this feature in FSF GCC. This will complicate the
C-family frontends somewhat, and there will be some difficulties in
specifying precisely what GCC must accept, since assembly languages
vary; there will likely need to be some sort of hookery in lexing for
instance, and maybe not all assembly languages can be supported
verbatim. But if the added complexity is worthwhile for FSF GCC, then
I'll set it up to be usable for the Microsoft syntax as well.
Otherwise this will continue to be an Apple-GCC-only feature, and only
support CW syntax.
Stan