Use of in-line assembler using ‘asm’ variants
Just a “small” post to expand on my most recent tweet. In-line assembler is quite a difficult topic, but unavoidable in most embedded environments. And the syntactic variants are more numerous than the bugs in your code.
For that reason I give a piece of advice: For each different assembler semantic, use a different macro. For an assembler function, use ASM_FN like this:
ASM_FN int MyAssemblerFunction(void)For an in-line assembler block, use
{
...assembler instructions...
}
ASM_BLOCK like this:And for single-line in-line assembler instructions use
...
a += 4;
ASM_BLOCK volatile {
...assembler instructions...
};
ASM_LINE like this:Now, all these uses will need to expand into the non-standard keyword
...
a += 4;
ASM_LINE movb ax,0b00000010;
...
asm for the compiler to process everything correctly. Many compilers accept different forms of the keyword, so you may use asm, __asm and __asm__ interchangeably. If you want to, you can take these three variants instead of the ASM_(FN|BLOCK|LINE) as I suggested.
The idea is to enable Lint to expand each of these three forms differently: The function containing only assembler instructions shall be ignored by Lint, but its prototype needs to be known. Therefore we need to enable the Lint keyword _ignore_init (the body of the function is seen as a form of “initialization”), and provide the options:The plus-sign in the second option prevents the definition in our code (to
+rw(_ignore_init)
+dASM_FN=_ignore_init
asm or one its variants) to override the Lint-specific definition. However, for ASM_BLOCK this replacement will not work, so we need a different replacement: And the third form again needs a different replacement, since in this case, no brackets need be present at all:
+rw(_up_to_brackets)
+dASM_BLOCK=_up_to_brackets
With some other form of in-line assembler definition you might even need
+rw(_to_semi)
+dASM_LINE=_to_semi
_to_eol, or one of the other gobblers. But make sure to use different macros for different syntactic usages of asm, so you have the chance to use different gobblers for all situations.
Happy Linting!
Tags: Lint
Deutsch
English
Nederlands