Is this a compiler bug?
program Project44;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
function Test: integer;
asm
xor eax,eax
add eax,SizeOf(NativeInt);
end;
begin
WriteLn(Test); //Typically outputs 31 or 49
{$ifdef CPUX86}
WriteLn('should be 4');
{$else}
{$ifdef CPUX64}
WriteLn('should be 8');
{$endif}{$endif}
ReadLn
end.
This program outputs all kinds of things, except for 4/8.
Is this an bug or is it documented that I cannot use SizeOf and other compiler intrinsics in assembler?
If I would want to use SizeOf(xx)
in an asm block what do I do?
Using intrinsics Compiler intrinsics are special functions with implementations that are known to the compiler. These intrinsics enable you to easily incorporate domain-specific operations in C and C++ source code without resorting to complex implementations in assembly language.
''Intrinsics'' are those features of a language that a compiler recognizes and implements without any need for the program to declare them. The compiler may—or may not—link to a runtime library to perform the operation.
What Are Intrinsics? An intrinsic function is a function that has special handling by the compiler or interpreter for our programming language. More specifically, it's a special case where the compiler or interpreter can replace the function with an alternative implementation for various reasons.
Inline Assembly armclang provides an inline assembler that enables you to write assembly language sequences in C and C++ language source files. The inline assembler also provides access to features of the target processor that are not available from C or C++.
You cannot use compiler intrinsics because they are processed by the Delphi compiler rather than the assembler. Intrinsics are resolved by the Pascal compiler processing and parsing Pascal expressions, and then emitting code. That's the job of a compiler rather than an assembler. At least, that's my mental model.
In the case of SizeOf
you need to use the type
assembly expression operator:
add eax, type NativeInt
Or indeed:
function Test: integer;
asm
mov eax, type NativeInt
end;
This function performs as you expect.
Documentation here: Assembly Expressions, Expression Operators.
And yes, the fact that your code compiles should be considered a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With