In TP mode global alignment for byte size variables is in correct.
Original Reporter info from Mantis: Thaddy
-
Reporter name: Thaddy de Koning
Original Reporter info from Mantis: Thaddy
- Reporter name: Thaddy de Koning
Description:
In TP mode global alignment for byte size variables is in correct.
Byte size variables are always byte aligned, see:
"In TP mode global alignment for byte size variables is in correct.
"The $A+ State
In the $A+ state, all variables and typed constants larger than 1 byte are aligned on a machine-wd boundary (an even-numbered address).
If required, unused bytes are inserted between variables to achieve word alignment.
$A+ does not affect byte-sized variables, fields of record structures, or elements of arrays.
A field in a record will align on a word boundary only if the total size of all fields before it is even.
For every element of an array to align on a word boundary, the size of the elements must be even.
The $A- State
In the $A- state, no alignment measures are taken.
Variables and typed constants are simply placed at the next available address, regardless of their size.
NOTE: Regardless of the state of $A, each global var and const declaration section always starts at a word boundary.
The compiler always keeps the stack pointer (SP) word-aligned by allocating an extra unused byte in a procedure's stack frame if required."
Steps to reproduce:
{$mode fpc}{$align 1}{$codealign varmin=1}{$codealign varmax=1}
var
A,B: Byte;
R:record
A,B: Byte;
end;
begin
WriteLn(Ofs(B) - Ofs(A)); // <-- wrong answer, should be 1, always returns 16
WriteLn(Ofs(R.B) - Ofs(R.A)); // <-- wrapped in record, corrct answer 1
end.
Additional information:
This can break existing TP code where a programmer accesses the variables based on memory offsets.
See
https://forum.lazarus.freepascal.org/index.php/topic,48174.msg346546/topicseen.html#new
https://forum.lazarus.freepascal.org/index.php/topic,48172.msg346545/topicseen.html#new
Mantis conversion info:
- Mantis ID: 36599
- OS: dos
- OS Build: dosbox
- Build: 43965
- Platform: x86
- Version: 3.3.1