Recent

Author Topic: Old question about vars (data) alignment...  (Read 1021 times)

Avinash

  • New Member
  • *
  • Posts: 25
Old question about vars (data) alignment...
« on: January 18, 2020, 07:52:36 am »
I searched the forum and the question seemed to be raised repeatedly, but... how to get something different from 16 here? (Win32 FPC 3.0.4)
Code: Pascal  [Select][+][-]
  1. Var A,B: Byte;
  2. begin
  3.   WriteLn(Ofs(B) - Ofs(A));
  4. end.

All these $CODEALIGN/$ALIGN have no effect...

I need a result of Turbo Pascal — 1

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Old question about vars (data) alignment...
« Reply #1 on: January 18, 2020, 08:17:12 am »
Code: Pascal  [Select][+][-]
  1. {$mode tp}{$align 1}
  2. Var R:record
  3.        A,B: Byte;
  4.       end;
  5. begin
  6.   WriteLn(Ofs(R.B) - Ofs(R.A));
  7. end.
Note I believe later TP versions may also align like fpc in tp mode (16). If not it is a bug.
By using the record you get the requested answer.
« Last Edit: January 18, 2020, 08:27:18 am by Thaddy »

Avinash

  • New Member
  • *
  • Posts: 25
Re: Old question about vars (data) alignment...
« Reply #2 on: January 18, 2020, 08:43:33 am »
In fact, I need the working state of the old code written based on alignment 1, rather than alignment of new code. After all, if you have sufficiently large sources on Turbo Pascal, with many units, you won’t even know where to rewrite the code until you encounter bugs. Not to mention the fact that rewriting itself can be time consuming in some places.
It is strange that FPC does not have problems with alignment inside records, but this does not work for variables in general.

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Old question about vars (data) alignment...
« Reply #3 on: January 18, 2020, 08:56:43 am »
I still have to check but afair the behavior is compatible with TP7. The alignment you describe is from before TP5.5?
TP55 behaves as you expect (1) I will also test Borland Pascal 7.
« Last Edit: January 18, 2020, 09:13:29 am by Thaddy »

Avinash

  • New Member
  • *
  • Posts: 25
Re: Old question about vars (data) alignment...
« Reply #4 on: January 18, 2020, 09:04:01 am »
Borland Pascal 7.0

Quote
The $A+ State

In the $A+ state, all variables and typed constants larger than 1 byte are aligned on a machine-word 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.

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Old question about vars (data) alignment...
« Reply #5 on: January 18, 2020, 09:19:32 am »
Ah, I see. My answer was only correct for A+ and never for bytes. That is a bug.
You can work around it by either a packed record or a packed array.
Maybe the packed array is a bit easier.

I will file a bug report, just in case.

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Old question about vars (data) alignment...
« Reply #6 on: January 18, 2020, 09:30:07 am »
Reported as 0036599 in the bug tracker. Also added the two links in our small discussion.

Avinash

  • New Member
  • *
  • Posts: 25
Re: Old question about vars (data) alignment...
« Reply #7 on: January 18, 2020, 09:48:40 am »
Thanks. But this applies not only to bytes, but also to other small variables: words, longints, small records — all are aligned on 16 bytes by FPC, and you won’t get 2 (or 4 or 8 ).
(also about the other thread — there the question that FPC remove not used explicitly variables)

Avinash

  • New Member
  • *
  • Posts: 25
Re: Old question about vars (data) alignment...
« Reply #8 on: January 18, 2020, 03:33:02 pm »
It seems that compiling with with -Aas/wasm/nasmwin32 options gives the result even without any alignment directives. Probably this is what i was looking for.

mas steindorff

  • Sr. Member
  • ****
  • Posts: 444
Re: Old question about vars (data) alignment...
« Reply #9 on: January 24, 2020, 07:58:16 am »
Code: Pascal  [Select][+][-]
  1. {$mode tp}{$align 1}
  2. Var R:record
  3.        A,B: Byte;
  4.       end;
  5. begin
  6.   WriteLn(Ofs(R.B) - Ofs(R.A));
  7. end.
This will work if you add the keyword "packed" before "record".  Otherwise the compiler may add a gap.  I use this technic all of the time.
Mas
windows 7/10 - laz 2.0 / 1.2.6 general releases

Thaddy

  • Hero Member
  • *****
  • Posts: 10516
Re: Old question about vars (data) alignment...
« Reply #10 on: January 24, 2020, 08:30:25 am »
Code: Pascal  [Select][+][-]
  1. {$mode tp}{$align 1}
  2. Var R:record
  3.        A,B: Byte;
  4.       end;
  5. begin
  6.   WriteLn(Ofs(R.B) - Ofs(R.A));
  7. end.
This will work if you add the keyword "packed" before "record".  Otherwise the compiler may add a gap.  I use this technic all of the time.
Mas
Partially wrong: {$align 1} takes care of that. But you can also add {$packrecords 1} apart from declaring the records as packed.

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: Old question about vars (data) alignment...
« Reply #11 on: January 24, 2020, 03:51:56 pm »
Hi $CODEALIGN work i'm using it for My Vectors and SIMD methods. With $CODEALIGN you must use additonnal keywords, and surround the variables

Like this

 
Code: Pascal  [Select][+][-]
  1. {$ALIGN 16}
  2.  // Not sur those 3 lines below are necessary, but in doubt.
  3.  {$CODEALIGN CONSTMIN=16}
  4.  {$CODEALIGN LOCALMIN=16}
  5.  {$CODEALIGN VARMIN=16}
  6. // What i'm sure It's not sufficient, you must also declare $CODEALIGN as below
  7.  
  8. Type
  9.    // Not necessary here !!!!! and if it's correct ????
  10.    {$CODEALIGN RECORDMIN=16}
  11.    TMyRec = record
  12.      v1, v2, v3, v4 : Byte
  13.    end;
  14.    {$CODEALIGN RECORDMIN=16}
  15.  
  16.    // in a class
  17.    TMyClass = Class
  18.    private
  19.     {$CODEALIGN RECORDMIN=16}
  20.      FMyRec : TMyRec;
  21.     {$CODEALIGN RECORDMIN=4}
  22.    ...
  23.    end;
  24.  
  25. // As constant
  26. Const
  27.    {$CODEALIGN CONSTMIN=16}
  28.    cMyRec : TMyRec =(v1:1;v2:2;v3:3;v4:4);
  29.    {$CODEALIGN CONSTMIN=4}
  30.  
  31. // As variable
  32. Procedure dojob;
  33. var
  34.   {$CODEALIGN VARMIN=16}
  35.   LMyRec : TMyRec;
  36.   {$CODEALIGN VARMIN=4}
  37. begin
  38.   // Do the job
  39. end;

Best regards

Jérôme
« Last Edit: January 24, 2020, 03:53:54 pm by BeanzMaster »

PaulRowntree

  • Full Member
  • ***
  • Posts: 132
    • Paul Rowntree
Re: Old question about vars (data) alignment...
« Reply #12 on: January 24, 2020, 04:53:09 pm »
In fact, I need the working state of the old code written based on alignment 1, rather than alignment of new code. After all, if you have sufficiently large sources on Turbo Pascal, with many units, you won’t even know where to rewrite the code until you encounter bugs. Not to mention the fact that rewriting itself can be time consuming in some places.
It is strange that FPC does not have problems with alignment inside records, but this does not work for variables in general.
I am sympathetic about wanting to use old code, but there are rational limits to this practice. Sometimes we have to review and revise, if only to see if there is a better way to do something that wasn't available (or thought of) back in the TP days. Sometimes entire blocks of code can be replaced by new functionality.

To find offending code, can you use 'Find in Files' the ofs( function ?
Paul Rowntree
- coding for instrument control, data acquisition & analysis, CNC systems

 

TinyPortal © 2005-2018