Open negge opened 3 years ago
In-line assembler is one pass only and can not do backpatch because instructions are generated by assembler not by code generator. if you use jump back then in-line assembler is able optimize this jump, but if you use forward reference then can not do optimization, best solution is to use "short" keyword before target label as example
static int abs(int x);
#pragma aux abs = \
"L1: test eax, eax" \
"jl L1" \
"jge short L2" \
"neg eax" \
"L2:" \
parm [ eax ] value [ eax ]
#pragma aux main modify [ eax ebx ecx edx edi esi ebp ]
int main() {
return abs(-1);
}
disassembled code
Segment: _TEXT BYTE USE32 0000000E bytes
0000 main_:
0000 B8 FF FF FF FF mov eax,0xffffffff
0005 L$1:
0005 85 C0 test eax,eax
0007 7C FC jl L$1
0009 7D 02 jge L$2
000B F7 D8 neg eax
000D L$2:
000D C3 ret
Thanks, this is the work-around I am currently using.
Is there a reason why the in-line assembler cannot be two pass?
Take into account that all labels in C/C++/Fortran code are handled by code generator. In-line assembler is able handle only in-line code local labels (which are invisible for code generator) or global labels created by code generator. in-line assembly code is inserted as bytes sequence created by in-line assembler there is very limited interaction between code generator and in-line assembler. Two pass assembly is absolutely useless for in-line assembler. in-line assembler is only instruction encoder. Purpose of in-line assembler is to use simple code no complex assembly code therefore functionality is limited. Use "short" word is not big complication. We could add some kind of back-patching, but very small gain for big work.
The inline assembly code generator always uses 32-bit relative jumps even when the target is within a short jump range.
Consider this
test_abs.c
example:Building with
wcc386 -s test_abs.c
and disassembling gives:which is 4 bytes bigger than using a short jump:
This is something that the assembler should be able to automatically figure out.
Example above was generated with
Version 2.0 beta Sep 15 2020 00:23:46 (32-bit)
.