Re: [fpc-pascal] Converting Delphi7 code to FreePascal with interfacing to protection key?

2023-03-29 Thread Bo Berglund via fpc-pascal
On Wed, 29 Mar 2023 19:27:12 -0400, Dennis Lee Bieber via fpc-pascal
 wrote:

>   I'd expect trying to link a 32-bit object file into a 64-bit
>application to result in all sorts of possible errors.

That is not the case here, they have supplied two obj files, one is 32 and the
other 64 bit.
The problem seems to be that neither is a valid COFF file according to my Fpc
(or Lazarus) in any case. Both generate the COFF error when reaching the linker
step.

And in the case of the 32 bit version of the obj file I have set my Lazarus to
target a 32 bit Win application for the i386 processor (I have installed the 32
bit cross compiler found at SourceForge for this).
https://sourceforge.net/projects/lazarus/files/Lazarus%20Windows%2064%20bits/Lazarus%202.2.6/


-- 
Bo Berglund
Developer in Sweden

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Ralf Quint via fpc-pascal

On 3/29/2023 2:38 PM, Ched via fpc-pascal wrote:

Hello,

Ok for the records for internal calculations. But sometimes, records 
are used for reading/writing structured files. Does "packed" in 
"packed array" and "packed record" always forbid the compiler to play 
with alignments ? 


If that record/array is specifically defined as "packed", then yes, of 
course. However, arrays/records that are no so defined should be aligned...



Ralf


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Ched via fpc-pascal

Hello,

Ok for the records for internal calculations. But sometimes, records are used for reading/writing 
structured files. Does "packed" in "packed array" and "packed record" always forbid the compiler to play 
with alignments ?


Kindest regards, Ched'




Le 29.03.23 à 21:07, Jonas Maebe via fpc-pascal a écrit :

On 29/03/2023 16:00, Matias Ezequiel Vara Larsen via fpc-pascal wrote:

So if I have a type like:

test = record
   r: DWord;
   s: Word;
end;

Then, I define the variable `nada` as an array of `test` type:

var
   nada: array[0..2] of test;

Is the `@nada[1].r` element aligned to 4-bytes?


Records always get padded to a multiple of their internal alignment requirement. Unless you're on a 
system where the default packrecords value is 2 (msdos-i8086 has that probably), the above record's 
internal field alignment is 4 and hence its size will also be padded to a multiple of 4.



Jonas

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Jonas Maebe via fpc-pascal

On 29/03/2023 16:00, Matias Ezequiel Vara Larsen via fpc-pascal wrote:

So if I have a type like:

test = record
   r: DWord;
   s: Word;
end;

Then, I define the variable `nada` as an array of `test` type:

var
   nada: array[0..2] of test;

Is the `@nada[1].r` element aligned to 4-bytes?


Records always get padded to a multiple of their internal alignment 
requirement. Unless you're on a system where the default packrecords 
value is 2 (msdos-i8086 has that probably), the above record's internal 
field alignment is 4 and hence its size will also be padded to a 
multiple of 4.



Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Converting Delphi7 code to FreePascal with interfacing to protection key?

2023-03-29 Thread Bo Berglund via fpc-pascal
On Wed, 22 Mar 2023 14:17:23 +0100, Sven Barth via fpc-pascal
 wrote:

>Then you should ask them for a suitable 64-bit object file (I assume that
>they'll provide the one for C which they probably also did for you back
>then and then you might need to adjust the import unit a bit to be usable
>with x86_64-win64; as long as they also provide a C header that should be
>doable).
>

Back here again after I raised a ticket with Thales and got a response!
I think they provide something that will work with Delphi rather than for C.

The reply from THALES is linking to no less than 7 different archive files for
various use of the keys.

In one of these I found an example for *Delphi 10*, which contained an updated
interface file and a 32 bit object file:
spromeps.pas
superpro.obj

(I do not think I even ever had Delphi 10...)

I copied these to my project and disabled the old versions.

Then I tried to cross-compile to Win32 on Lazarus 2.2.4 and Fpc 3.2.2.

Syntax check (Run/Quick_Compile) finishes successfully.

But the Compile does not, it fails like this:

-
Compile Project, Mode: Default, OS: win32, CPU: i386, Target: DongleTest.exe:
Exit code 1, Errors: 1
DongleTest.lpr(28,1) Error: Illegal COFF Magic while reading
D:\Engineering\Projects\Subversion\PC\DongleTest\superpro.obj
-

The same happens if I set the target to default everywhere so going for 64 bit.
In tyhis case I had to switch to the spromeps.pas superpro.obj files from the
same archive but for 64 bit applications.
Here the error when I try to compile:

--
Compile Project, Mode: Default, Target: DongleTest.exe: Exit code 1, Errors: 1
DongleTest.lpr(28,1) Error: Failed reading coff file, illegal reloctype $000A
while reading D:\Engineering\Projects\Subversion\PC\AgiDongleTest\superpro.obj
--

So again I have the problem with the obj file(s) which are now more recent than
the old single one.

How can I check the obj file for "type"?

I have asked this before here but now cannot find if I got an answer or not...

Is there some command I can use to check what kind of an obj file I have?

Is it possible to find anything by looking at the interface file spromeps.pas,
which is the interface from the main application towards the obj file?

ThHis pretty much looks like the old file to me except it has more available API
calls and also some defines for MSWINDOWS and LINUX respectively in the
implementation part defining all the API calls into the driver.

My problem is that I do not have any Delphi on my new (from 2019) laptop except
Delphi-7...

So I guess I have to check if my test app can be opened in Delphi7 using the
supplied obj file and if it works there, how do I proceed?

TIA


-- 
Bo Berglund
Developer in Sweden

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Matias Ezequiel Vara Larsen via fpc-pascal
On Wed, Mar 29, 2023 at 08:11:40PM +0700, Hairy Pixels via fpc-pascal wrote:
> 
> 
> > On Mar 29, 2023, at 1:33 PM, Sven Barth via fpc-pascal 
> >  wrote:
> > 
> > No, array types are defined as never having padding between the elements. 
> > You need to make sure that the element is appropriately sized then. 
> > 
> 
> Wouldn’t that be better for performance if each element was padded to align 
> with the cache? That seems like something the compiler could do easily for 
> you behind the scenes. I may not understand the need to "align the elements 
> on the cache” though.

I do not think you would like to have everything aligned to the
cacheline. This use-case requires it because each element in the array
will be accessed from a different CPU and you would try to prevent cache
ping-pong, i.e., sharing the same cacheline between different cpu's cache.

Matias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Matias Ezequiel Vara Larsen via fpc-pascal
On Wed, Mar 29, 2023 at 08:33:58AM +0200, Sven Barth via fpc-pascal wrote:
> Matias Ezequiel Vara Larsen via fpc-pascal 
> schrieb am Mi., 29. März 2023, 00:27:
> 
> > Hello,
> >
> > On Mon, Mar 27, 2023 at 09:35:38PM +0200, Jonas Maebe via fpc-pascal wrote:
> > > On 27/03/2023 21:25, denisgolovan via fpc-pascal wrote:
> > >
> > > > But it's still not possible to attach alignment to type itself instead
> > of variable, right?
> > >
> > > It is possible (
> > https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/talignrec1.pp
> > ),
> > > but it is subject to the same limitations when declaring variables of
> > those
> > > types.
> > >
> >
> > Thanks, this is what I was looking for, however, when the type is
> > defined as an array, only the [0] element is aligned. I was expecting
> > that each element in the array would be aligned.
> >
> 
> No, array types are defined as never having padding between the elements.
> You need to make sure that the element is appropriately sized then.
> 

So if I have a type like:

test = record
  r: DWord;
  s: Word;
end;

Then, I define the variable `nada` as an array of `test` type:

var
  nada: array[0..2] of test;

Is the `@nada[1].r` element aligned to 4-bytes?

Thanks, Matias.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Hairy Pixels via fpc-pascal


> On Mar 29, 2023, at 1:33 PM, Sven Barth via fpc-pascal 
>  wrote:
> 
> No, array types are defined as never having padding between the elements. You 
> need to make sure that the element is appropriately sized then. 
> 

Wouldn’t that be better for performance if each element was padded to align 
with the cache? That seems like something the compiler could do easily for you 
behind the scenes. I may not understand the need to "align the elements on the 
cache” though.

Regards,
Ryan Joseph

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Matias Ezequiel Vara Larsen via fpc-pascal
On Mon, Mar 27, 2023 at 08:52:40PM +0200, Jonas Maebe via fpc-pascal wrote:
> On 27/03/2023 09:28, Matias Ezequiel Vara Larsen via fpc-pascal wrote:
> > Apologies in the case this subject has been already discussed. I require
> > that some global variables be cache-line aligned. This is a requirement
> > for per-CPU variables that are statically defined to prevent cache
> > ping-pong. Is there any way to do this at fpc when the record is
> > declared?
> 
> {$push}
> {$codealign varmin=16}
> {$codealign constmin=16}
> const
>   constdata: array[0..3] of longint = (1,2,3,4);
> 
> var
>   x: array[0..15] of longint;
> {$pop}
> 
> However, the maximum alignment you can specify this way is limited per
> target (hardcoded in the compiler). In 3.2.x, it's 16 bytes for most desktop
> targets. On 3.3.x, it's 64 bytes for most desktop targets.
> 

I've tried this (fpc 3.2.0) and it correctly aligns to 64 bytes (x86 cacheline):

{$push}
{$codealign varmin=64}
var
   x: array[0..15] of longint;
{$pop}

The generated assembler is:

.section .bss
.balign 64
.type U_$P$PROGRAM_$$_X,@object
.size U_$P$PROGRAM_$$_X,64
U_$P$PROGRAM_$$_X:
.zero 64

Is it possible to define "varmin" as a constant or symbol instead of using
the actual value each time?

Matias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Matias Ezequiel Vara Larsen via fpc-pascal
On Wed, Mar 29, 2023 at 08:33:58AM +0200, Sven Barth via fpc-pascal wrote:
> Matias Ezequiel Vara Larsen via fpc-pascal 
> schrieb am Mi., 29. März 2023, 00:27:
> 
> > Hello,
> >
> > On Mon, Mar 27, 2023 at 09:35:38PM +0200, Jonas Maebe via fpc-pascal wrote:
> > > On 27/03/2023 21:25, denisgolovan via fpc-pascal wrote:
> > >
> > > > But it's still not possible to attach alignment to type itself instead
> > of variable, right?
> > >
> > > It is possible (
> > https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/talignrec1.pp
> > ),
> > > but it is subject to the same limitations when declaring variables of
> > those
> > > types.
> > >
> >
> > Thanks, this is what I was looking for, however, when the type is
> > defined as an array, only the [0] element is aligned. I was expecting
> > that each element in the array would be aligned.
> >
> 
> No, array types are defined as never having padding between the elements.
> You need to make sure that the element is appropriately sized then.
> 
Thanks for the comment. I will pad those records used for per-cpu
variables so they fit in different cache-lines when defined as an array.

Matias
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Cache-line alignment for records

2023-03-29 Thread Sven Barth via fpc-pascal
Matias Ezequiel Vara Larsen via fpc-pascal 
schrieb am Mi., 29. März 2023, 00:27:

> Hello,
>
> On Mon, Mar 27, 2023 at 09:35:38PM +0200, Jonas Maebe via fpc-pascal wrote:
> > On 27/03/2023 21:25, denisgolovan via fpc-pascal wrote:
> >
> > > But it's still not possible to attach alignment to type itself instead
> of variable, right?
> >
> > It is possible (
> https://gitlab.com/freepascal.org/fpc/source/-/blob/main/tests/test/talignrec1.pp
> ),
> > but it is subject to the same limitations when declaring variables of
> those
> > types.
> >
>
> Thanks, this is what I was looking for, however, when the type is
> defined as an array, only the [0] element is aligned. I was expecting
> that each element in the array would be aligned.
>

No, array types are defined as never having padding between the elements.
You need to make sure that the element is appropriately sized then.

Regards,
Sven

>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal