Inline assembly syntax
Ian Lance Taylor
ian@wasabisystems.com
Sat May 8 00:56:00 GMT 2004
I'm going to try to write this from the point of a view of a gcc user
rather than a gcc developer. Contributions welcome. In particular, I
haven't used CW asm syntax, so I am, to some extent, guessing.
Traditional GCC asm syntax
Pro:
* Permits reasonably precise description of types of operands
which may appear in assembler code.
* Explicitly describes which operands are modified and which are
not, including read-write operands.
* Explicitly describes anything else which may be affected by the
assembler code.
* Precise description of side-effects permit various
optimizations, including scheduling the asm or simply
eliminating it entirely.
Con:
* Mapping from C variables to assembler operands is awkward.
* Use of constraints is cryptic. Documentation is insufficient.
* Particular pairs of constraints are especially confusing, such
as 'm' vs. 'o'.
* No way to describe affects on particular memory locations.
* No way disable scheduling without introducing inappropriate
clobbers--e.g., you can't safely grab a semaphore without
clobbering all of memory.
* Requiring \n or \; between instructions is tedious.
* In fact, string constants and backslash handling in general is
tedious.
CW asm syntax
Pro:
* Mapping from C variables to assembler operands is
straightforward.
* Avoiding string constants makes code easier to write.
* Compiler already knows side-effects of ordinary instructions.
* Compiler knows register requirements of ordinary instructions.
Con:
* No way to describe side-effects for unusual operations, such as
supervisor calls.
* No way to disable scheduling across instructions.
* No obvious way to encourage compiler to put operand in the right
place, other than using an instruction to move it there, unless
the compiler is smart enough to detect that case and eliminate
the instruction if it is not required.
* Requires extensive assembly language parsing for any fully
supported target.
So, putting these together, the ideal assembler syntax would have
characteristics like these:
* Straightforward mapping from C variables to assembler operands.
* Avoid string constants and associated backslash syntax.
* Compiler would automatically determine side effects of known
instructions.
* Support specifying additional side effects.
* Scheduling barriers could be inserted.
* Clobbering of precise memory locations could be discerned and
described.
* Compiler would automatically determine required locations for
operands.
* Some mechanism would be available to specify required operand
location.
These lead us in the direction of
asm
{
add r0, foo, bar // register r0 = variable foo + variable bar
add r0, "r1", r2 // register r0 = variable r1 + register r2
{ // instructions in block may not be rearranged
cli // disable interrupts
add 0(r0), 1 // * (register r0) += 1
sti // enable interuppts
}
svc 21 __clobber__(r0,r1,r2) // supervisor call
__noschedule__ // scheduling barrier
addi r0, r1, constant __constraint__(i) // specify constraint
}
Hmmm.
Ian
More information about the Gcc
mailing list